Search in sources :

Example 11 with RemoteInput

use of android.support.v4.app.RemoteInput in project Rocket.Chat.Android by RocketChat.

the class PushNotificationHandler method createActions.

private void createActions(Context context, Bundle extras, NotificationCompat.Builder mBuilder, Resources resources, String packageName, int notId) {
    Log.d(LOG_TAG, "create actions: with in-line");
    String actions = extras.getString(ACTIONS);
    if (actions == null) {
        return;
    }
    try {
        JSONArray actionsArray = new JSONArray(actions);
        ArrayList<NotificationCompat.Action> wActions = new ArrayList<>();
        for (int i = 0; i < actionsArray.length(); i++) {
            int min = 1;
            int max = 2000000000;
            Random random = new Random();
            int uniquePendingIntentRequestCode = random.nextInt((max - min) + 1) + min;
            Log.d(LOG_TAG, "adding action");
            JSONObject action = actionsArray.getJSONObject(i);
            Log.d(LOG_TAG, "adding callback = " + action.getString(CALLBACK));
            boolean foreground = action.optBoolean(FOREGROUND, true);
            boolean inline = action.optBoolean("inline", false);
            Intent intent;
            PendingIntent pIntent;
            if (inline) {
                Log.d(LOG_TAG, "Version: " + android.os.Build.VERSION.SDK_INT + " = " + android.os.Build.VERSION_CODES.M);
                if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.M) {
                    Log.d(LOG_TAG, "push activity");
                    intent = new Intent(context, MainActivity.class);
                } else {
                    Log.d(LOG_TAG, "push receiver");
                    intent = new Intent(context, BackgroundActionButtonHandler.class);
                }
                updateIntent(intent, action.getString(CALLBACK), extras, foreground, notId);
                if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.M) {
                    Log.d(LOG_TAG, "push activity for notId " + notId);
                    pIntent = PendingIntent.getActivity(context, uniquePendingIntentRequestCode, intent, PendingIntent.FLAG_ONE_SHOT);
                } else {
                    Log.d(LOG_TAG, "push receiver for notId " + notId);
                    pIntent = PendingIntent.getBroadcast(context, uniquePendingIntentRequestCode, intent, PendingIntent.FLAG_ONE_SHOT);
                }
            } else if (foreground) {
                intent = new Intent(context, MainActivity.class);
                updateIntent(intent, action.getString(CALLBACK), extras, foreground, notId);
                pIntent = PendingIntent.getActivity(context, uniquePendingIntentRequestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            } else {
                intent = new Intent(context, BackgroundActionButtonHandler.class);
                updateIntent(intent, action.getString(CALLBACK), extras, foreground, notId);
                pIntent = PendingIntent.getBroadcast(context, uniquePendingIntentRequestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            }
            NotificationCompat.Action.Builder actionBuilder = new NotificationCompat.Action.Builder(resources.getIdentifier(action.optString(ICON, ""), DRAWABLE, packageName), action.getString(TITLE), pIntent);
            RemoteInput remoteInput;
            if (inline) {
                Log.d(LOG_TAG, "create remote input");
                String replyLabel = "Enter your reply here";
                remoteInput = new RemoteInput.Builder(INLINE_REPLY).setLabel(replyLabel).build();
                actionBuilder.addRemoteInput(remoteInput);
            }
            NotificationCompat.Action wAction = actionBuilder.build();
            wActions.add(actionBuilder.build());
            if (inline) {
                mBuilder.addAction(wAction);
            } else {
                mBuilder.addAction(resources.getIdentifier(action.optString(ICON, ""), DRAWABLE, packageName), action.getString(TITLE), pIntent);
            }
            wAction = null;
            pIntent = null;
        }
        mBuilder.extend(new NotificationCompat.WearableExtender().addActions(wActions));
        wActions.clear();
    } catch (JSONException e) {
    // nope
    }
}
Also used : RemoteInput(android.support.v4.app.RemoteInput) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) MainActivity(chat.rocket.android.activity.MainActivity) Random(java.util.Random) JSONObject(org.json.JSONObject) NotificationCompat(android.support.v4.app.NotificationCompat) PendingIntent(android.app.PendingIntent)

Example 12 with RemoteInput

use of android.support.v4.app.RemoteInput 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 13 with RemoteInput

use of android.support.v4.app.RemoteInput 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)

Aggregations

NotificationCompat (android.support.v4.app.NotificationCompat)13 RemoteInput (android.support.v4.app.RemoteInput)13 PendingIntent (android.app.PendingIntent)11 Intent (android.content.Intent)11 SharedPreferences (android.content.SharedPreferences)5 NotificationManagerCompat (android.support.v4.app.NotificationManagerCompat)4 PowerManager (android.os.PowerManager)3 AppSettings (com.klinker.android.twitter.settings.AppSettings)3 ArrayList (java.util.ArrayList)3 Bitmap (android.graphics.Bitmap)2 Random (java.util.Random)2 JSONArray (org.json.JSONArray)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 SuppressLint (android.annotation.SuppressLint)1 NotificationManager (android.app.NotificationManager)1 ComponentName (android.content.ComponentName)1 ContentResolver (android.content.ContentResolver)1 ContentValues (android.content.ContentValues)1 Cursor (android.database.Cursor)1