Search in sources :

Example 1 with NotificationRule

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());
}
Also used : NotificationManager(android.app.NotificationManager) SharedPreferences(android.content.SharedPreferences) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) SpannableString(android.text.SpannableString) Date(java.util.Date) Line(im.tny.segvault.subway.Line) NotificationRule(im.tny.segvault.disturbances.model.NotificationRule) Network(im.tny.segvault.subway.Network) NotificationCompat(android.support.v4.app.NotificationCompat) PendingIntent(android.app.PendingIntent) Realm(io.realm.Realm)

Aggregations

NotificationManager (android.app.NotificationManager)1 PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 NotificationCompat (android.support.v4.app.NotificationCompat)1 SpannableString (android.text.SpannableString)1 NotificationRule (im.tny.segvault.disturbances.model.NotificationRule)1 Line (im.tny.segvault.subway.Line)1 Network (im.tny.segvault.subway.Network)1 Realm (io.realm.Realm)1 Date (java.util.Date)1