Search in sources :

Example 16 with WearableExtender

use of android.support.v4.app.NotificationCompat.WearableExtender in project Gadgetbridge by Freeyourgadget.

the class NotificationListener method onNotificationPosted.

@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    /*
        * return early if DeviceCommunicationService is not running,
        * else the service would get started every time we get a notification.
        * unfortunately we cannot enable/disable NotificationListener at runtime like we do with
        * broadcast receivers because it seems to invalidate the permissions that are
        * necessary for NotificationListenerService
        */
    if (!isServiceRunning()) {
        return;
    }
    switch(GBApplication.getGrantedInterruptionFilter()) {
        case NotificationManager.INTERRUPTION_FILTER_ALL:
            break;
        case NotificationManager.INTERRUPTION_FILTER_ALARMS:
        case NotificationManager.INTERRUPTION_FILTER_NONE:
            return;
        case NotificationManager.INTERRUPTION_FILTER_PRIORITY:
            // FIXME: Handle Reminders and Events if they are enabled in Do Not Disturb
            return;
    }
    String source = sbn.getPackageName();
    Notification notification = sbn.getNotification();
    if (handleMediaSessionNotification(notification))
        return;
    Prefs prefs = GBApplication.getPrefs();
    if (!prefs.getBoolean("notifications_generic_whenscreenon", false)) {
        PowerManager powermanager = (PowerManager) getSystemService(POWER_SERVICE);
        if (powermanager.isScreenOn()) {
            //                LOG.info("Not forwarding notification, screen seems to be on and settings do not allow this");
            return;
        }
    }
    if ((notification.flags & Notification.FLAG_ONGOING_EVENT) == Notification.FLAG_ONGOING_EVENT) {
        //            LOG.info("Not forwarding notification, FLAG_ONGOING_EVENT is set. Notification flags: " + notification.flags);
        return;
    }
    if (source.equals("android") || source.equals("com.android.systemui") || source.equals("com.android.dialer") || source.equals("com.cyanogenmod.eleven")) {
        LOG.info("Not forwarding notification, is a system event");
        return;
    }
    if (source.equals("com.moez.QKSMS") || source.equals("com.android.mms") || source.equals("com.sonyericsson.conversations") || source.equals("com.android.messaging") || source.equals("org.smssecure.smssecure")) {
        if (!"never".equals(prefs.getString("notification_mode_sms", "when_screen_off"))) {
            return;
        }
    }
    if (GBApplication.blacklist != null && GBApplication.blacklist.contains(source)) {
        LOG.info("Not forwarding notification, application is blacklisted");
        return;
    }
    NotificationSpec notificationSpec = new NotificationSpec();
    // determinate Source App Name ("Label")
    PackageManager pm = getPackageManager();
    ApplicationInfo ai = null;
    try {
        ai = pm.getApplicationInfo(source, 0);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    if (ai != null) {
        notificationSpec.sourceName = (String) pm.getApplicationLabel(ai);
    }
    boolean preferBigText = false;
    notificationSpec.type = AppNotificationType.getInstance().get(source);
    if (source.startsWith("com.fsck.k9")) {
        // we dont want group summaries at all for k9
        if ((notification.flags & Notification.FLAG_GROUP_SUMMARY) == Notification.FLAG_GROUP_SUMMARY) {
            return;
        }
        preferBigText = true;
    }
    if (notificationSpec.type == null) {
        notificationSpec.type = NotificationType.UNKNOWN;
    }
    LOG.info("Processing notification from source " + source + " with flags: " + notification.flags);
    dissectNotificationTo(notification, notificationSpec, preferBigText);
    //FIMXE: a truly unique id would be better
    notificationSpec.id = (int) sbn.getPostTime();
    // ignore Gadgetbridge's very own notifications, except for those from the debug screen
    if (getApplicationContext().getPackageName().equals(source)) {
        if (!getApplicationContext().getString(R.string.test_notification).equals(notificationSpec.title)) {
            return;
        }
    }
    NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender(notification);
    List<NotificationCompat.Action> actions = wearableExtender.getActions();
    if (actions.isEmpty() && (notification.flags & Notification.FLAG_GROUP_SUMMARY) == Notification.FLAG_GROUP_SUMMARY) {
        //this could cause #395 to come back
        LOG.info("Not forwarding notification, FLAG_GROUP_SUMMARY is set and no wearable action present. Notification flags: " + notification.flags);
        return;
    }
    for (NotificationCompat.Action act : actions) {
        if (act != null && act.getRemoteInputs() != null) {
            LOG.info("found wearable action: " + act.getTitle() + "  " + sbn.getTag());
            mActionLookup.add(notificationSpec.id, act);
            notificationSpec.flags |= NotificationSpec.FLAG_WEARABLE_REPLY;
            break;
        }
    }
    GBApplication.deviceService().onNotification(notificationSpec);
}
Also used : ApplicationInfo(android.content.pm.ApplicationInfo) Prefs(nodomain.freeyourgadget.gadgetbridge.util.Prefs) Notification(android.app.Notification) StatusBarNotification(android.service.notification.StatusBarNotification) PowerManager(android.os.PowerManager) PackageManager(android.content.pm.PackageManager) NotificationSpec(nodomain.freeyourgadget.gadgetbridge.model.NotificationSpec) NotificationCompat(android.support.v4.app.NotificationCompat)

Example 17 with WearableExtender

use of android.support.v4.app.NotificationCompat.WearableExtender in project qksms by moezbhatti.

the class RemoteMessagingReceiver method getConversationExtender.

public static WearableExtender getConversationExtender(Context context, String name, String address, long threadId) {
    WearableExtender wearableExtender = new WearableExtender();
    wearableExtender.setGravity(Gravity.BOTTOM);
    wearableExtender.setStartScrollBottom(true);
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    Bitmap background = ContactHelper.getBitmap(context, ContactHelper.getId(context, address));
    if (background == null) {
        background = Bitmap.createBitmap(640, 400, Bitmap.Config.ARGB_8888);
        // We can't use ThemeManager here because it might not be initialized
        background.eraseColor(Integer.parseInt(prefs.getString(SettingsFragment.THEME, "" + ThemeManager.DEFAULT_COLOR)));
    }
    wearableExtender.setBackground(background);
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
        BigTextStyle chatPageStyle = new BigTextStyle();
        chatPageStyle.setBigContentTitle(name).setSummaryText(address).bigText(SmsHelper.getHistoryForWearable(context, name, threadId));
        Notification chatPage = new NotificationCompat.Builder(context).setStyle(chatPageStyle).extend(new NotificationCompat.WearableExtender().setStartScrollBottom(true)).build();
        wearableExtender.addPage(chatPage);
    }
    wearableExtender.addAction(getReplyAction(context, address, threadId));
    Intent readIntent = new Intent(NotificationManager.ACTION_MARK_READ);
    readIntent.putExtra(EXTRA_THREAD_ID, threadId);
    PendingIntent readPI = PendingIntent.getBroadcast(context, 2, readIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Action readAction = new NotificationCompat.Action.Builder(R.drawable.ic_accept, context.getString(R.string.mark_read), readPI).build();
    wearableExtender.addAction(readAction);
    return wearableExtender;
}
Also used : SharedPreferences(android.content.SharedPreferences) WearableExtender(android.support.v4.app.NotificationCompat.WearableExtender) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) Notification(android.app.Notification) WearableExtender(android.support.v4.app.NotificationCompat.WearableExtender) Bitmap(android.graphics.Bitmap) BigTextStyle(android.support.v4.app.NotificationCompat.BigTextStyle) NotificationCompat(android.support.v4.app.NotificationCompat) PendingIntent(android.app.PendingIntent)

Aggregations

NotificationCompat (android.support.v4.app.NotificationCompat)17 PendingIntent (android.app.PendingIntent)12 MessageReference (com.fsck.k9.activity.MessageReference)8 Intent (android.content.Intent)4 WearableExtender (android.support.v4.app.NotificationCompat.WearableExtender)4 Account (com.fsck.k9.Account)4 Notification (android.app.Notification)3 Bitmap (android.graphics.Bitmap)2 RemoteInput (android.support.v4.app.RemoteInput)2 SuppressLint (android.annotation.SuppressLint)1 NotificationManager (android.app.NotificationManager)1 SharedPreferences (android.content.SharedPreferences)1 ApplicationInfo (android.content.pm.ApplicationInfo)1 PackageManager (android.content.pm.PackageManager)1 Paint (android.graphics.Paint)1 PowerManager (android.os.PowerManager)1 NotificationManagerCompat (android.preview.support.v4.app.NotificationManagerCompat)1 StatusBarNotification (android.service.notification.StatusBarNotification)1 BigTextStyle (android.support.v4.app.NotificationCompat.BigTextStyle)1 IOException (java.io.IOException)1