Search in sources :

Example 36 with NotificationManagerCompat

use of android.support.v4.app.NotificationManagerCompat in project Talon-for-Twitter by klinker24.

the class NotificationUtils method notifySecondMentions.

public static void notifySecondMentions(Context context, int secondAccount) {
    MentionsDataSource data = MentionsDataSource.getInstance(context);
    int numberNew = data.getUnreadCount(secondAccount);
    int smallIcon = R.drawable.ic_stat_icon;
    Bitmap largeIcon;
    Intent resultIntent = new Intent(context, SwitchAccountsRedirect.class);
    PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, 0);
    NotificationCompat.Builder mBuilder;
    String title = context.getResources().getString(R.string.app_name) + " - " + context.getResources().getString(R.string.sec_acc);
    ;
    String name = null;
    String message;
    String messageLong;
    String tweetText = null;
    NotificationCompat.Action replyAction = null;
    if (numberNew == 1) {
        name = data.getNewestName(secondAccount);
        SharedPreferences sharedPrefs = context.getSharedPreferences("com.klinker.android.twitter_world_preferences", 0);
        // then just quit
        if (sharedPrefs.getString("muted_users", "").contains(name) && !sharedPrefs.getBoolean("show_muted_mentions", false)) {
            return;
        }
        message = context.getResources().getString(R.string.mentioned_by) + " @" + name;
        tweetText = data.getNewestMessage(secondAccount);
        messageLong = "<b>@" + name + "</b>: " + tweetText;
        largeIcon = getImage(context, name);
        Intent reply = new Intent(context, NotificationComposeSecondAcc.class);
        sharedPrefs.edit().putString("from_notification_second", "@" + name).commit();
        long id = data.getLastIds(secondAccount)[0];
        PendingIntent replyPending = PendingIntent.getActivity(context, 0, reply, 0);
        sharedPrefs.edit().putLong("from_notification_long_second", id).commit();
        sharedPrefs.edit().putString("from_notification_text_second", "@" + name + ": " + TweetLinkUtils.removeColorHtml(tweetText, AppSettings.getInstance(context))).commit();
        // Create the remote input
        RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY).setLabel("@" + name + " ").build();
        // Create the notification action
        replyAction = new NotificationCompat.Action.Builder(R.drawable.ic_action_reply_dark, context.getResources().getString(R.string.noti_reply), replyPending).addRemoteInput(remoteInput).build();
    } else {
        // more than one mention
        message = numberNew + " " + context.getResources().getString(R.string.new_mentions);
        messageLong = "<b>" + context.getResources().getString(R.string.mentions) + "</b>: " + numberNew + " " + context.getResources().getString(R.string.new_mentions);
        largeIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.drawer_user_dark);
    }
    Intent markRead = new Intent(context, MarkReadSecondAccService.class);
    PendingIntent readPending = PendingIntent.getService(context, 0, markRead, 0);
    AppSettings settings = AppSettings.getInstance(context);
    Intent deleteIntent = new Intent(context, NotificationDeleteReceiverTwo.class);
    mBuilder = new NotificationCompat.Builder(context, TALON_SERVICE_CHANNEL_ID).setContentTitle(title).setContentText(TweetLinkUtils.removeColorHtml(message, settings)).setSmallIcon(smallIcon).setLargeIcon(largeIcon).setContentIntent(resultPendingIntent).setAutoCancel(true).setDeleteIntent(PendingIntent.getBroadcast(context, 0, deleteIntent, 0)).setPriority(NotificationCompat.PRIORITY_HIGH);
    if (numberNew == 1) {
        mBuilder.addAction(replyAction);
        mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(Html.fromHtml(settings.addonTheme ? messageLong.replaceAll("FF8800", settings.accentColor) : messageLong)));
    } else {
        NotificationCompat.InboxStyle inbox = getMentionsInboxStyle(numberNew, secondAccount, context, TweetLinkUtils.removeColorHtml(message, settings));
        mBuilder.setStyle(inbox);
    }
    if (settings.vibrate) {
        mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
    }
    if (settings.sound) {
        try {
            mBuilder.setSound(Uri.parse(settings.ringtone));
        } catch (Exception e) {
            mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
        }
    }
    if (settings.led)
        mBuilder.setLights(0xFFFFFF, 1000, 1000);
    if (settings.notifications) {
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        notificationManager.notify(9, mBuilder.build());
        // if we want to wake the screen on a new message
        if (settings.wakeScreen) {
            PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            final PowerManager.WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
            wakeLock.acquire(5000);
        }
        // Pebble notification
        if (context.getSharedPreferences("com.klinker.android.twitter_world_preferences", 0).getBoolean("pebble_notification", false)) {
            sendAlertToPebble(context, title, messageLong);
        }
        // Light Flow notification
        sendToLightFlow(context, title, messageLong);
    }
}
Also used : RemoteInput(android.support.v4.app.RemoteInput) AppSettings(com.klinker.android.twitter.settings.AppSettings) SharedPreferences(android.content.SharedPreferences) NotificationManagerCompat(android.support.v4.app.NotificationManagerCompat) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) PowerManager(android.os.PowerManager) Bitmap(android.graphics.Bitmap) NotificationCompat(android.support.v4.app.NotificationCompat) PendingIntent(android.app.PendingIntent)

Example 37 with NotificationManagerCompat

use of android.support.v4.app.NotificationManagerCompat in project Talon-for-Twitter by klinker24.

the class NotificationUtils method refreshNotification.

public static void refreshNotification(Context context, boolean noTimeline) {
    AppSettings settings = AppSettings.getInstance(context);
    SharedPreferences sharedPrefs = context.getSharedPreferences("com.klinker.android.twitter_world_preferences", 0);
    int currentAccount = sharedPrefs.getInt("current_account", 1);
    // int[] unreadCounts = new int[] {4, 1, 2}; // for testing
    int[] unreadCounts = getUnreads(context);
    int timeline = unreadCounts[0];
    int realTimelineCount = timeline;
    // if they don't want that type of notification, simply set it to zero
    if (!settings.timelineNot || (settings.pushNotifications && settings.liveStreaming) || noTimeline) {
        unreadCounts[0] = 0;
    }
    if (!settings.mentionsNot) {
        unreadCounts[1] = 0;
    }
    if (!settings.dmsNot) {
        unreadCounts[2] = 0;
    }
    if (unreadCounts[0] == 0 && unreadCounts[1] == 0 && unreadCounts[2] == 0) {
    } else {
        Intent markRead = new Intent(context, MarkReadService.class);
        PendingIntent readPending = PendingIntent.getService(context, 0, markRead, 0);
        String shortText = getShortText(unreadCounts, context, currentAccount);
        String longText = getLongText(unreadCounts, context, currentAccount);
        // [0] is the full title and [1] is the screenname
        String[] title = getTitle(unreadCounts, context, currentAccount);
        boolean useExpanded = useExp(context);
        boolean addButton = addBtn(unreadCounts);
        if (title == null) {
            return;
        }
        Intent resultIntent;
        if (unreadCounts[1] != 0 && unreadCounts[0] == 0) {
            // it is a mention notification (could also have a direct message)
            resultIntent = new Intent(context, RedirectToMentions.class);
        } else if (unreadCounts[2] != 0 && unreadCounts[0] == 0 && unreadCounts[1] == 0) {
            // it is a direct message
            resultIntent = new Intent(context, RedirectToDMs.class);
        } else {
            resultIntent = new Intent(context, MainActivity.class);
        }
        PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, 0);
        NotificationCompat.Builder mBuilder;
        Intent deleteIntent = new Intent(context, NotificationDeleteReceiverOne.class);
        mBuilder = new NotificationCompat.Builder(context, TALON_SERVICE_CHANNEL_ID).setContentTitle(title[0]).setContentText(TweetLinkUtils.removeColorHtml(shortText, settings)).setSmallIcon(R.drawable.ic_stat_icon).setLargeIcon(getIcon(context, unreadCounts, title[1])).setContentIntent(resultPendingIntent).setAutoCancel(true).setTicker(TweetLinkUtils.removeColorHtml(shortText, settings)).setDeleteIntent(PendingIntent.getBroadcast(context, 0, deleteIntent, 0)).setPriority(NotificationCompat.PRIORITY_HIGH);
        if (unreadCounts[1] > 1 && unreadCounts[0] == 0 && unreadCounts[2] == 0) {
            // inbox style notification for mentions
            mBuilder.setStyle(getMentionsInboxStyle(unreadCounts[1], currentAccount, context, TweetLinkUtils.removeColorHtml(shortText, settings)));
        } else if (unreadCounts[2] > 1 && unreadCounts[0] == 0 && unreadCounts[1] == 0) {
            // inbox style notification for direct messages
            mBuilder.setStyle(getDMInboxStyle(unreadCounts[1], currentAccount, context, TweetLinkUtils.removeColorHtml(shortText, settings)));
        } else {
            // big text style for an unread count on timeline, mentions, and direct messages
            mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(Html.fromHtml(settings.addonTheme ? longText.replaceAll("FF8800", settings.accentColor) : longText)));
        }
        // Pebble notification
        if (sharedPrefs.getBoolean("pebble_notification", false)) {
            sendAlertToPebble(context, title[0], shortText);
        }
        // Light Flow notification
        sendToLightFlow(context, title[0], shortText);
        int homeTweets = unreadCounts[0];
        int mentionsTweets = unreadCounts[1];
        int dmTweets = unreadCounts[2];
        int newC = 0;
        if (homeTweets > 0) {
            newC++;
        }
        if (mentionsTweets > 0) {
            newC++;
        }
        if (dmTweets > 0) {
            newC++;
        }
        if (settings.notifications && newC > 0) {
            if (settings.vibrate) {
                mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
            }
            if (settings.sound) {
                try {
                    mBuilder.setSound(Uri.parse(settings.ringtone));
                } catch (Exception e) {
                    mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
                }
            }
            if (settings.led)
                mBuilder.setLights(0xFFFFFF, 1000, 1000);
            // Get an instance of the NotificationManager service
            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
            if (addButton) {
                // the reply and read button should be shown
                Intent reply;
                if (unreadCounts[1] == 1) {
                    reply = new Intent(context, NotificationCompose.class);
                } else {
                    reply = new Intent(context, NotificationDMCompose.class);
                }
                Log.v("username_for_noti", title[1]);
                sharedPrefs.edit().putString("from_notification", "@" + title[1] + " " + title[2]).commit();
                MentionsDataSource data = MentionsDataSource.getInstance(context);
                long id = data.getLastIds(currentAccount)[0];
                PendingIntent replyPending = PendingIntent.getActivity(context, 0, reply, 0);
                sharedPrefs.edit().putLong("from_notification_long", id).commit();
                sharedPrefs.edit().putString("from_notification_text", "@" + title[1] + ": " + TweetLinkUtils.removeColorHtml(shortText, settings)).commit();
                // Create the remote input
                RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY).setLabel("@" + title[1] + " ").build();
                // Create the notification action
                NotificationCompat.Action replyAction = new NotificationCompat.Action.Builder(R.drawable.ic_action_reply_dark, context.getResources().getString(R.string.noti_reply), replyPending).addRemoteInput(remoteInput).build();
                NotificationCompat.Action.Builder action = new NotificationCompat.Action.Builder(R.drawable.ic_action_read_dark, context.getResources().getString(R.string.mark_read), readPending);
                mBuilder.addAction(replyAction);
                mBuilder.addAction(action.build());
            } else {
                // otherwise, if they can use the expanded notifications, the popup button will be shown
                Intent popup = new Intent(context, RedirectToPopup.class);
                popup.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                popup.putExtra("from_notification", true);
                PendingIntent popupPending = PendingIntent.getActivity(context, 0, popup, 0);
                NotificationCompat.Action.Builder action = new NotificationCompat.Action.Builder(R.drawable.ic_popup, context.getResources().getString(R.string.popup), popupPending);
                mBuilder.addAction(action.build());
            }
            // Build the notification and issues it with notification manager.
            notificationManager.notify(1, mBuilder.build());
            // if we want to wake the screen on a new message
            if (settings.wakeScreen) {
                PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
                final PowerManager.WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
                wakeLock.acquire(5000);
            }
        }
        // if there are unread tweets on the timeline, check them for favorite users
        if (settings.favoriteUserNotifications && realTimelineCount > 0) {
            favUsersNotification(currentAccount, context);
        }
    }
    try {
        ContentValues cv = new ContentValues();
        cv.put("tag", "com.klinker.android.twitter/com.klinker.android.twitter.ui.MainActivity");
        // add the direct messages and mentions
        cv.put("count", unreadCounts[1] + unreadCounts[2]);
        context.getContentResolver().insert(Uri.parse("content://com.teslacoilsw.notifier/unread_count"), cv);
    } catch (IllegalArgumentException ex) {
    /* Fine, TeslaUnread is not installed. */
    } catch (Exception ex) {
        /* Some other error, possibly because the format
               of the ContentValues are incorrect.

                Log but do not crash over this. */
        ex.printStackTrace();
    }
}
Also used : AppSettings(com.klinker.android.twitter.settings.AppSettings) NotificationManagerCompat(android.support.v4.app.NotificationManagerCompat) PowerManager(android.os.PowerManager) NotificationCompat(android.support.v4.app.NotificationCompat) RedirectToMentions(com.klinker.android.twitter.utils.redirects.RedirectToMentions) ContentValues(android.content.ContentValues) RemoteInput(android.support.v4.app.RemoteInput) NotificationCompose(com.klinker.android.twitter.activities.compose.NotificationCompose) SharedPreferences(android.content.SharedPreferences) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) NotificationDMCompose(com.klinker.android.twitter.activities.compose.NotificationDMCompose) PendingIntent(android.app.PendingIntent)

Example 38 with NotificationManagerCompat

use of android.support.v4.app.NotificationManagerCompat in project Talon-for-Twitter by klinker24.

the class NotificationUtils method makeFavsNotification.

public static void makeFavsNotification(ArrayList<String[]> tweets, Context context, boolean toDrawer) {
    String shortText;
    String longText;
    String title;
    int smallIcon = R.drawable.ic_stat_icon;
    Bitmap largeIcon;
    Intent resultIntent;
    if (toDrawer) {
        resultIntent = new Intent(context, RedirectToDrawer.class);
    } else {
        resultIntent = new Intent(context, NotiTweetPager.class);
    }
    PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, 0);
    NotificationCompat.InboxStyle inbox = null;
    if (tweets.size() == 1) {
        title = tweets.get(0)[0];
        shortText = tweets.get(0)[1];
        longText = shortText;
        largeIcon = getImage(context, tweets.get(0)[2]);
    } else {
        inbox = new NotificationCompat.InboxStyle();
        title = context.getResources().getString(R.string.favorite_users);
        shortText = tweets.size() + " " + context.getResources().getString(R.string.fav_user_tweets);
        longText = "";
        try {
            inbox.setBigContentTitle(shortText);
        } catch (Exception e) {
        }
        if (tweets.size() <= 5) {
            for (String[] s : tweets) {
                inbox.addLine(Html.fromHtml("<b>" + s[0] + ":</b> " + s[1]));
            }
        } else {
            for (int i = 0; i < 5; i++) {
                inbox.addLine(Html.fromHtml("<b>" + tweets.get(i)[0] + ":</b> " + tweets.get(i)[1]));
            }
            inbox.setSummaryText("+" + (tweets.size() - 5) + " " + context.getString(R.string.tweets));
        }
        largeIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.drawer_user_dark);
    }
    NotificationCompat.Builder mBuilder;
    AppSettings settings = AppSettings.getInstance(context);
    if (shortText.contains("@" + settings.myScreenName)) {
        // return because there is a mention notification for this already
        return;
    }
    Intent deleteIntent = new Intent(context, NotificationDeleteReceiverOne.class);
    mBuilder = new NotificationCompat.Builder(context, TALON_SERVICE_CHANNEL_ID).setContentTitle(title).setContentText(TweetLinkUtils.removeColorHtml(shortText, settings)).setSmallIcon(smallIcon).setLargeIcon(largeIcon).setContentIntent(resultPendingIntent).setAutoCancel(true).setDeleteIntent(PendingIntent.getBroadcast(context, 0, deleteIntent, 0)).setPriority(NotificationCompat.PRIORITY_HIGH);
    if (inbox == null) {
        mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(Html.fromHtml(settings.addonTheme ? longText.replaceAll("FF8800", settings.accentColor) : longText)));
    } else {
        mBuilder.setStyle(inbox);
    }
    if (settings.vibrate) {
        mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
    }
    if (settings.sound) {
        try {
            mBuilder.setSound(Uri.parse(settings.ringtone));
        } catch (Exception e) {
            mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
        }
    }
    if (settings.led)
        mBuilder.setLights(0xFFFFFF, 1000, 1000);
    if (settings.notifications) {
        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        notificationManager.notify(2, mBuilder.build());
        // if we want to wake the screen on a new message
        if (settings.wakeScreen) {
            PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            final PowerManager.WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
            wakeLock.acquire(5000);
        }
        // Pebble notification
        if (context.getSharedPreferences("com.klinker.android.twitter_world_preferences", 0).getBoolean("pebble_notification", false)) {
            sendAlertToPebble(context, title, shortText);
        }
        // Light Flow notification
        sendToLightFlow(context, title, shortText);
    }
}
Also used : NotiTweetPager(com.klinker.android.twitter.activities.tweet_viewer.NotiTweetPager) AppSettings(com.klinker.android.twitter.settings.AppSettings) NotificationManagerCompat(android.support.v4.app.NotificationManagerCompat) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) PowerManager(android.os.PowerManager) Bitmap(android.graphics.Bitmap) RedirectToDrawer(com.klinker.android.twitter.utils.redirects.RedirectToDrawer) NotificationCompat(android.support.v4.app.NotificationCompat) PendingIntent(android.app.PendingIntent)

Example 39 with NotificationManagerCompat

use of android.support.v4.app.NotificationManagerCompat in project Talon-for-Twitter by klinker24.

the class TrimDataService method notifyNewVersion.

public void notifyNewVersion(String version) {
    Intent goToStore = new Intent(this, RedirectToPlayStore.class);
    PendingIntent storePending = PendingIntent.getActivity(this, 0, goToStore, 0);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, TALON_SERVICE_CHANNEL_ID);
    builder.setContentTitle("Talon Version " + version);
    builder.setContentText("Click to update.");
    builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
    builder.setSmallIcon(R.drawable.ic_stat_icon);
    builder.setContentIntent(storePending);
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    notificationManager.notify(552, builder.build());
}
Also used : NotificationManagerCompat(android.support.v4.app.NotificationManagerCompat) NotificationCompat(android.support.v4.app.NotificationCompat) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent)

Aggregations

NotificationManagerCompat (android.support.v4.app.NotificationManagerCompat)38 NotificationCompat (android.support.v4.app.NotificationCompat)17 PendingIntent (android.app.PendingIntent)15 Intent (android.content.Intent)15 SharedPreferences (android.content.SharedPreferences)7 PowerManager (android.os.PowerManager)7 Bitmap (android.graphics.Bitmap)6 AppSettings (com.klinker.android.twitter.settings.AppSettings)6 Notification (android.app.Notification)5 Context (android.content.Context)4 Cursor (android.database.Cursor)4 RemoteInput (android.support.v4.app.RemoteInput)4 Bundle (android.os.Bundle)3 ComponentName (android.content.ComponentName)2 ContentResolver (android.content.ContentResolver)2 ContentValues (android.content.ContentValues)2 Uri (android.net.Uri)2 Builder (android.support.v4.app.NotificationCompat.Builder)2 TaskStackBuilder (android.support.v4.app.TaskStackBuilder)2 ArrayList (java.util.ArrayList)2