Search in sources :

Example 71 with StatusBarNotification

use of android.service.notification.StatusBarNotification in project muzei by romannurik.

the class ActivateMuzeiIntentService method maybeShowActivateMuzeiNotification.

public static void maybeShowActivateMuzeiNotification(Context context) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    if (preferences.getBoolean(ACTIVATE_MUZEI_NOTIF_SHOWN_PREF_KEY, false)) {
        return;
    }
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
    StatusBarNotification[] notifications = notificationManager.getActiveNotifications();
    boolean hasInstallNotification = false;
    boolean hasActivateNotification = false;
    for (StatusBarNotification notification : notifications) {
        if (notification.getId() == INSTALL_NOTIFICATION_ID) {
            hasInstallNotification = true;
        } else if (notification.getId() == ACTIVATE_NOTIFICATION_ID) {
            hasActivateNotification = true;
        }
    }
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setSmallIcon(R.drawable.ic_stat_muzei).setColor(ContextCompat.getColor(context, R.color.notification)).setPriority(NotificationCompat.PRIORITY_MAX).setDefaults(NotificationCompat.DEFAULT_VIBRATE).setAutoCancel(true).setContentTitle(context.getString(R.string.activate_title));
    Intent deleteIntent = new Intent(context, ActivateMuzeiIntentService.class);
    deleteIntent.setAction(ACTION_MARK_NOTIFICATION_READ);
    builder.setDeleteIntent(PendingIntent.getService(context, 0, deleteIntent, 0));
    // Check if the Muzei main app is installed
    GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context).addApi(Wearable.API).build();
    ConnectionResult connectionResult = googleApiClient.blockingConnect(30, TimeUnit.SECONDS);
    Set<Node> nodes = new TreeSet<>();
    if (connectionResult.isSuccess()) {
        nodes = Wearable.CapabilityApi.getCapability(googleApiClient, "activate_muzei", CapabilityApi.FILTER_ALL).await().getCapability().getNodes();
        googleApiClient.disconnect();
    }
    if (nodes.isEmpty()) {
        if (hasInstallNotification) {
            // No need to repost the notification
            return;
        }
        // Send an install Muzei notification
        if (PlayStoreAvailability.getPlayStoreAvailabilityOnPhone(context) != PlayStoreAvailability.PLAY_STORE_ON_PHONE_AVAILABLE) {
            builder.setContentText(context.getString(R.string.activate_no_play_store));
            FirebaseAnalytics.getInstance(context).logEvent("activate_notif_no_play_store", null);
        } else {
            builder.setContentText(context.getString(R.string.activate_install_muzei));
            Intent installMuzeiIntent = new Intent(context, ActivateMuzeiIntentService.class);
            installMuzeiIntent.setAction(ACTION_REMOTE_INSTALL_MUZEI);
            PendingIntent pendingIntent = PendingIntent.getService(context, 0, installMuzeiIntent, 0);
            builder.addAction(new NotificationCompat.Action.Builder(R.drawable.open_on_phone, context.getString(R.string.activate_install_action), pendingIntent).extend(new NotificationCompat.Action.WearableExtender().setHintDisplayActionInline(true).setAvailableOffline(false)).build());
            builder.extend(new NotificationCompat.WearableExtender().setContentAction(0));
            FirebaseAnalytics.getInstance(context).logEvent("activate_notif_play_store", null);
        }
        notificationManager.notify(INSTALL_NOTIFICATION_ID, builder.build());
        return;
    }
    // else, Muzei is installed on the phone/tablet, but not activated
    if (hasInstallNotification) {
        // Clear any install Muzei notification
        notificationManager.cancel(INSTALL_NOTIFICATION_ID);
    }
    if (hasActivateNotification) {
        // No need to repost the notification
        return;
    }
    String nodeName = nodes.iterator().next().getDisplayName();
    builder.setContentText(context.getString(R.string.activate_enable_muzei, nodeName));
    Intent launchMuzeiIntent = new Intent(context, ActivateMuzeiIntentService.class);
    PendingIntent pendingIntent = PendingIntent.getService(context, 0, launchMuzeiIntent, 0);
    builder.addAction(new NotificationCompat.Action.Builder(R.drawable.open_on_phone, context.getString(R.string.activate_action, nodeName), pendingIntent).extend(new NotificationCompat.Action.WearableExtender().setHintDisplayActionInline(true).setAvailableOffline(false)).build());
    Bitmap background = null;
    try {
        background = BitmapFactory.decodeStream(context.getAssets().open("starrynight.jpg"));
    } catch (IOException e) {
        Log.e(TAG, "Error reading default background asset", e);
    }
    builder.extend(new NotificationCompat.WearableExtender().setContentAction(0).setBackground(background));
    FirebaseAnalytics.getInstance(context).logEvent("activate_notif_installed", null);
    notificationManager.notify(ACTIVATE_NOTIFICATION_ID, builder.build());
}
Also used : GoogleApiClient(com.google.android.gms.common.api.GoogleApiClient) NotificationManager(android.app.NotificationManager) SharedPreferences(android.content.SharedPreferences) Node(com.google.android.gms.wearable.Node) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) RemoteIntent(com.google.android.wearable.intent.RemoteIntent) IOException(java.io.IOException) Bitmap(android.graphics.Bitmap) StatusBarNotification(android.service.notification.StatusBarNotification) TreeSet(java.util.TreeSet) ConnectionResult(com.google.android.gms.common.ConnectionResult) NotificationCompat(android.support.v4.app.NotificationCompat) PendingIntent(android.app.PendingIntent)

Example 72 with StatusBarNotification

use of android.service.notification.StatusBarNotification in project Signal-Android by WhisperSystems.

the class MessageNotifier method cancelActiveNotifications.

private static void cancelActiveNotifications(@NonNull Context context) {
    NotificationManager notifications = ServiceUtil.getNotificationManager(context);
    notifications.cancel(SUMMARY_NOTIFICATION_ID);
    if (Build.VERSION.SDK_INT >= 23) {
        try {
            StatusBarNotification[] activeNotifications = notifications.getActiveNotifications();
            for (StatusBarNotification activeNotification : activeNotifications) {
                if (activeNotification.getId() != CallNotificationBuilder.WEBRTC_NOTIFICATION) {
                    notifications.cancel(activeNotification.getId());
                }
            }
        } catch (Throwable e) {
            // XXX Appears to be a ROM bug, see #6043
            Log.w(TAG, e);
            notifications.cancelAll();
        }
    }
}
Also used : NotificationManager(android.app.NotificationManager) StatusBarNotification(android.service.notification.StatusBarNotification)

Example 73 with StatusBarNotification

use of android.service.notification.StatusBarNotification in project Signal-Android by WhisperSystems.

the class MessageNotifier method cancelOrphanedNotifications.

private static void cancelOrphanedNotifications(@NonNull Context context, NotificationState notificationState) {
    if (Build.VERSION.SDK_INT >= 23) {
        try {
            NotificationManager notifications = ServiceUtil.getNotificationManager(context);
            StatusBarNotification[] activeNotifications = notifications.getActiveNotifications();
            for (StatusBarNotification notification : activeNotifications) {
                boolean validNotification = false;
                if (notification.getId() != SUMMARY_NOTIFICATION_ID && notification.getId() != CallNotificationBuilder.WEBRTC_NOTIFICATION && notification.getId() != KeyCachingService.SERVICE_RUNNING_ID && notification.getId() != MessageRetrievalService.FOREGROUND_ID) {
                    for (NotificationItem item : notificationState.getNotifications()) {
                        if (notification.getId() == (SUMMARY_NOTIFICATION_ID + item.getThreadId())) {
                            validNotification = true;
                            break;
                        }
                    }
                    if (!validNotification) {
                        notifications.cancel(notification.getId());
                    }
                }
            }
        } catch (Throwable e) {
            // XXX Android ROM Bug, see #6043
            Log.w(TAG, e);
        }
    }
}
Also used : NotificationManager(android.app.NotificationManager) StatusBarNotification(android.service.notification.StatusBarNotification)

Example 74 with StatusBarNotification

use of android.service.notification.StatusBarNotification in project Gadgetbridge by Freeyourgadget.

the class NotificationListener method onNotificationRemoved.

@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
    //FIXME: deduplicate code
    String source = sbn.getPackageName();
    Notification notification = sbn.getNotification();
    if ((notification.flags & Notification.FLAG_ONGOING_EVENT) == Notification.FLAG_ONGOING_EVENT) {
        return;
    }
    if (source.equals("android") || source.equals("com.android.systemui") || source.equals("com.android.dialer") || source.equals("com.cyanogenmod.eleven")) {
        return;
    }
    Prefs prefs = GBApplication.getPrefs();
    if (prefs.getBoolean("autoremove_notifications", false)) {
        LOG.info("notification removed, will ask device to delete it");
        //FIMXE: a truly unique id would be better
        GBApplication.deviceService().onDeleteNotification((int) sbn.getPostTime());
    }
}
Also used : Prefs(nodomain.freeyourgadget.gadgetbridge.util.Prefs) Notification(android.app.Notification) StatusBarNotification(android.service.notification.StatusBarNotification)

Example 75 with StatusBarNotification

use of android.service.notification.StatusBarNotification 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)

Aggregations

StatusBarNotification (android.service.notification.StatusBarNotification)144 Notification (android.app.Notification)61 View (android.view.View)24 PendingIntent (android.app.PendingIntent)23 TextView (android.widget.TextView)23 ApplicationInfo (android.content.pm.ApplicationInfo)21 ImageView (android.widget.ImageView)21 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)17 StatusBarIcon (com.android.internal.statusbar.StatusBarIcon)16 ITransientNotification (android.app.ITransientNotification)15 NotificationData (com.android.systemui.statusbar.NotificationData)14 Point (android.graphics.Point)13 RemoteViews (android.widget.RemoteViews)12 Entry (com.android.systemui.statusbar.NotificationData.Entry)12 PackageManager (android.content.pm.PackageManager)11 UserHandle (android.os.UserHandle)10 NavigationBarView (com.android.systemui.statusbar.phone.NavigationBarView)10 RemoteInputView (com.android.systemui.statusbar.policy.RemoteInputView)10 Drawable (android.graphics.drawable.Drawable)9 NotificationPanelView (com.android.systemui.statusbar.phone.NotificationPanelView)9