use of im.tny.segvault.disturbances.model.NotificationRule in project underlx by underlx.
the class MainService method handleDisturbanceNotification.
private void handleDisturbanceNotification(String network, String line, String id, String status, boolean downtime, long msgtime) {
Log.d("MainService", "handleDisturbanceNotification");
SharedPreferences sharedPref = getSharedPreferences("notifsettings", MODE_PRIVATE);
Set<String> linePref = sharedPref.getStringSet(PreferenceNames.NotifsLines, null);
Network snetwork;
synchronized (lock) {
if (!networks.containsKey(network)) {
return;
}
snetwork = networks.get(network);
}
Line sline = snetwork.getLine(line);
if (sline == null) {
return;
}
if (downtime) {
lineStatusCache.markLineAsDown(sline, new Date(msgtime));
} else {
lineStatusCache.markLineAsUp(sline);
}
if (linePref != null && !linePref.contains(line)) {
// notifications disabled for this line
return;
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (!downtime && !sharedPref.getBoolean(PreferenceNames.NotifsServiceResumed, true)) {
// notifications for normal service resumed disabled
notificationManager.cancel(id.hashCode());
return;
}
Realm realm = Application.getDefaultRealmInstance(this);
for (NotificationRule rule : realm.where(NotificationRule.class).findAll()) {
if (rule.isEnabled() && rule.applies(new Date(msgtime))) {
realm.close();
return;
}
}
realm.close();
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra(MainActivity.EXTRA_INITIAL_FRAGMENT, "nav_disturbances");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
String title = String.format(getString(R.string.notif_disturbance_title), Util.getLineNames(this, sline)[0]);
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle.setBigContentTitle(title);
bigTextStyle.bigText(status);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this).setStyle(bigTextStyle).setSmallIcon(R.drawable.ic_disturbance_notif).setColor(sline.getColor()).setContentTitle(title).setContentText(status).setAutoCancel(true).setWhen(msgtime).setSound(Uri.parse(sharedPref.getString(downtime ? PreferenceNames.NotifsRingtone : PreferenceNames.NotifsRegularizationRingtone, "content://settings/system/notification_sound"))).setVisibility(Notification.VISIBILITY_PUBLIC).setContentIntent(pendingIntent);
if (sharedPref.getBoolean(downtime ? PreferenceNames.NotifsVibrate : PreferenceNames.NotifsRegularizationVibrate, false)) {
notificationBuilder.setVibrate(new long[] { 0, 100, 100, 150, 150, 200 });
} else {
notificationBuilder.setVibrate(new long[] { 0l });
}
notificationManager.notify(id.hashCode(), notificationBuilder.build());
}
Aggregations