From 47efb678eb564e9e99380f744107a7d904b42753 Mon Sep 17 00:00:00 2001 From: Moez Bhatti Date: Tue, 14 Jun 2016 00:42:26 -0400 Subject: [PATCH] Implemented Android N quick reply API --- QKSMS/src/main/AndroidManifest.xml | 2 +- .../com/moez/QKSMS/enums/QKPreference.java | 3 +- ...iver.java => RemoteMessagingReceiver.java} | 94 +++++++++---------- .../QKSMS/service/NotificationService.java | 3 +- .../transaction/NotificationManager.java | 53 ++++++----- QKSMS/src/main/res/values-v19/bools.xml | 2 - QKSMS/src/main/res/values-v21/bools.xml | 2 - QKSMS/src/main/res/values-v23/bools.xml | 5 + QKSMS/src/main/res/values/bools.xml | 4 + QKSMS/src/main/res/xml/settings.xml | 2 +- 10 files changed, 92 insertions(+), 78 deletions(-) rename QKSMS/src/main/java/com/moez/QKSMS/receiver/{WearableIntentReceiver.java => RemoteMessagingReceiver.java} (88%) create mode 100644 QKSMS/src/main/res/values-v23/bools.xml diff --git a/QKSMS/src/main/AndroidManifest.xml b/QKSMS/src/main/AndroidManifest.xml index 6b7f8d52..2706bbc3 100644 --- a/QKSMS/src/main/AndroidManifest.xml +++ b/QKSMS/src/main/AndroidManifest.xml @@ -348,7 +348,7 @@ diff --git a/QKSMS/src/main/java/com/moez/QKSMS/enums/QKPreference.java b/QKSMS/src/main/java/com/moez/QKSMS/enums/QKPreference.java index 8ca59518..8c189aad 100644 --- a/QKSMS/src/main/java/com/moez/QKSMS/enums/QKPreference.java +++ b/QKSMS/src/main/java/com/moez/QKSMS/enums/QKPreference.java @@ -1,5 +1,6 @@ package com.moez.QKSMS.enums; +import android.os.Build; import com.moez.QKSMS.ui.ThemeManager; import java.util.Arrays; @@ -85,7 +86,7 @@ public enum QKPreference { MMS_PROXY("mms_proxy", true), // QK Reply - QK_REPLY("pref_key_quickreply_enabled", true), + QK_REPLY("pref_key_quickreply_enabled", Build.VERSION.SDK_INT < 23), TAP_DISMISS("pref_key_quickreply_dismiss", true), // QK Compose diff --git a/QKSMS/src/main/java/com/moez/QKSMS/receiver/WearableIntentReceiver.java b/QKSMS/src/main/java/com/moez/QKSMS/receiver/RemoteMessagingReceiver.java similarity index 88% rename from QKSMS/src/main/java/com/moez/QKSMS/receiver/WearableIntentReceiver.java rename to QKSMS/src/main/java/com/moez/QKSMS/receiver/RemoteMessagingReceiver.java index ce97ec20..4fc6d3b0 100644 --- a/QKSMS/src/main/java/com/moez/QKSMS/receiver/WearableIntentReceiver.java +++ b/QKSMS/src/main/java/com/moez/QKSMS/receiver/RemoteMessagingReceiver.java @@ -13,11 +13,10 @@ import android.preference.PreferenceManager; import android.support.v4.app.NotificationCompat; import android.support.v4.app.RemoteInput; import android.view.Gravity; - -import com.moez.QKSMS.mmssms.Message; -import com.moez.QKSMS.mmssms.Transaction; import com.moez.QKSMS.R; import com.moez.QKSMS.data.ContactHelper; +import com.moez.QKSMS.mmssms.Message; +import com.moez.QKSMS.mmssms.Transaction; import com.moez.QKSMS.service.MarkReadService; import com.moez.QKSMS.transaction.NotificationManager; import com.moez.QKSMS.transaction.SmsHelper; @@ -33,7 +32,7 @@ import java.util.Set; import static android.support.v4.app.NotificationCompat.BigTextStyle; import static android.support.v4.app.NotificationCompat.WearableExtender; -public class WearableIntentReceiver extends BroadcastReceiver { +public class RemoteMessagingReceiver extends BroadcastReceiver { public static final String ACTION_REPLY = "com.moez.QKSMS.receiver.WearableIntentReceiver.REPLY"; @@ -41,8 +40,29 @@ public class WearableIntentReceiver extends BroadcastReceiver { public static final String EXTRA_THREAD_ID = "thread_id"; public static final String EXTRA_VOICE_REPLY = "voice_reply"; - public static WearableExtender getSingleConversationExtender(Context context, String name, String address, long threadId) { + @Override + public void onReceive(Context context, Intent intent) { + Bundle remoteInput = RemoteInput.getResultsFromIntent(intent); + Bundle bundle = intent.getExtras(); + if (remoteInput != null && bundle != null) { + if (intent.getAction().equals(ACTION_REPLY)) { + Message message = new Message( + remoteInput.getCharSequence(EXTRA_VOICE_REPLY).toString(), + new String[]{bundle.getString(EXTRA_ADDRESS)} + ); + + Transaction sendTransaction = new Transaction(context, SmsHelper.getSendSettings(context)); + sendTransaction.sendNewMessage(message, bundle.getLong(EXTRA_THREAD_ID)); + + Intent i = new Intent(context, MarkReadService.class); + i.putExtra(EXTRA_THREAD_ID, bundle.getLong(EXTRA_THREAD_ID)); + context.startService(i); + } + } + } + + public static WearableExtender getConversationExtender(Context context, String name, String address, long threadId) { WearableExtender wearableExtender = new WearableExtender(); wearableExtender.setGravity(Gravity.BOTTOM); wearableExtender.setStartScrollBottom(true); @@ -77,32 +97,7 @@ public class WearableIntentReceiver extends BroadcastReceiver { wearableExtender.addPage(chatPage); } - - - Intent replyIntent = new Intent(ACTION_REPLY).setClass(context, WearableIntentReceiver.class); - replyIntent.putExtra(EXTRA_ADDRESS, address); - replyIntent.putExtra(EXTRA_THREAD_ID, threadId); - - Set defaultResponses = new HashSet<>(Arrays.asList(context.getResources().getStringArray(R.array.qk_responses))); - Set responseSet = prefs.getStringSet(SettingsFragment.QK_RESPONSES, defaultResponses); - ArrayList responses = new ArrayList(); - responses.addAll(responseSet); - Collections.sort(responses); - - PendingIntent replyPI = PendingIntent.getBroadcast(context, 0, replyIntent, PendingIntent.FLAG_UPDATE_CURRENT); - RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY) - .setLabel(context.getString(R.string.reply)) - .setChoices(responses.toArray(new String[responses.size()])) - .build(); - - NotificationCompat.Action replyAction = new NotificationCompat.Action.Builder( - R.drawable.ic_reply, - context.getString(R.string.reply_qksms), replyPI) - .addRemoteInput(remoteInput) - .build(); - - wearableExtender.addAction(replyAction); - + wearableExtender.addAction(getReplyAction(context, address, threadId)); Intent readIntent = new Intent(NotificationManager.ACTION_MARK_READ); readIntent.putExtra(EXTRA_THREAD_ID, threadId); @@ -119,26 +114,29 @@ public class WearableIntentReceiver extends BroadcastReceiver { return wearableExtender; } - @Override - public void onReceive(Context context, Intent intent) { + public static NotificationCompat.Action getReplyAction(Context context, String address, long threadId) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - Bundle remoteInput = RemoteInput.getResultsFromIntent(intent); - Bundle bundle = intent.getExtras(); - if (remoteInput != null && bundle != null) { - if (intent.getAction().equals(ACTION_REPLY)) { + Intent replyIntent = new Intent(ACTION_REPLY).setClass(context, RemoteMessagingReceiver.class); + replyIntent.putExtra(EXTRA_ADDRESS, address); + replyIntent.putExtra(EXTRA_THREAD_ID, threadId); - Message message = new Message( - remoteInput.getCharSequence(EXTRA_VOICE_REPLY).toString(), - new String[]{bundle.getString(EXTRA_ADDRESS)} - ); + Set defaultResponses = new HashSet<>(Arrays.asList(context.getResources().getStringArray(R.array.qk_responses))); + Set responseSet = prefs.getStringSet(SettingsFragment.QK_RESPONSES, defaultResponses); + ArrayList responses = new ArrayList<>(); + responses.addAll(responseSet); + Collections.sort(responses); - Transaction sendTransaction = new Transaction(context, SmsHelper.getSendSettings(context)); - sendTransaction.sendNewMessage(message, bundle.getLong(EXTRA_THREAD_ID)); + PendingIntent replyPI = PendingIntent.getBroadcast(context, 0, replyIntent, PendingIntent.FLAG_UPDATE_CURRENT); + RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY) + .setLabel(context.getString(R.string.reply)) + .setChoices(responses.toArray(new String[responses.size()])) + .build(); - Intent i = new Intent(context, MarkReadService.class); - i.putExtra(EXTRA_THREAD_ID, bundle.getLong(EXTRA_THREAD_ID)); - context.startService(i); - } - } + return new NotificationCompat.Action.Builder( + R.drawable.ic_reply, + context.getString(R.string.reply), replyPI) + .addRemoteInput(remoteInput) + .build(); } } diff --git a/QKSMS/src/main/java/com/moez/QKSMS/service/NotificationService.java b/QKSMS/src/main/java/com/moez/QKSMS/service/NotificationService.java index 3fac9883..98736552 100644 --- a/QKSMS/src/main/java/com/moez/QKSMS/service/NotificationService.java +++ b/QKSMS/src/main/java/com/moez/QKSMS/service/NotificationService.java @@ -7,6 +7,7 @@ import android.content.SharedPreferences; import android.database.Cursor; import android.graphics.Bitmap; import android.net.Uri; +import android.os.Build; import android.os.IBinder; import android.preference.PreferenceManager; import com.moez.QKSMS.common.ConversationPrefsHelper; @@ -53,7 +54,7 @@ public class NotificationService extends Service { if (conversationPrefs.getNotificationsEnabled()) { // Only show QuickReply if we're outside of the app, and they have popups and QuickReply enabled. if (!LifecycleHandler.isApplicationVisible() && - intent.getBooleanExtra(EXTRA_POPUP, false) && prefs.getBoolean(SettingsFragment.QUICKREPLY, true)) { + intent.getBooleanExtra(EXTRA_POPUP, false) && prefs.getBoolean(SettingsFragment.QUICKREPLY, Build.VERSION.SDK_INT < 23)) { popupIntent = new Intent(context, QKReplyActivity.class); popupIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); diff --git a/QKSMS/src/main/java/com/moez/QKSMS/transaction/NotificationManager.java b/QKSMS/src/main/java/com/moez/QKSMS/transaction/NotificationManager.java index 0a7477f3..447ccb3d 100644 --- a/QKSMS/src/main/java/com/moez/QKSMS/transaction/NotificationManager.java +++ b/QKSMS/src/main/java/com/moez/QKSMS/transaction/NotificationManager.java @@ -23,7 +23,6 @@ import android.provider.Telephony; import android.support.v4.app.NotificationCompat; import android.text.Html; import android.util.Log; - import com.android.mms.transaction.TransactionService; import com.android.mms.transaction.TransactionState; import com.google.android.mms.pdu_alt.PduHeaders; @@ -35,7 +34,7 @@ import com.moez.QKSMS.data.ContactHelper; import com.moez.QKSMS.data.Message; import com.moez.QKSMS.model.ImageModel; import com.moez.QKSMS.model.SlideshowModel; -import com.moez.QKSMS.receiver.WearableIntentReceiver; +import com.moez.QKSMS.receiver.RemoteMessagingReceiver; import com.moez.QKSMS.ui.MainActivity; import com.moez.QKSMS.ui.ThemeManager; import com.moez.QKSMS.ui.messagelist.MessageItem; @@ -267,10 +266,10 @@ public class NotificationManager { NotificationCompat.Builder builder = new NotificationCompat.Builder(context) .setSmallIcon(R.drawable.ic_notification) - // SMS messages are high priority + // SMS messages are high priority .setPriority(getNotificationPriority(context)) - // Silent here because this is just an update, not a new - // notification + // Silent here because this is just an update, not a new + // notification .setSound(null) .setVibrate(VIBRATION_SILENT) .setAutoCancel(true); @@ -469,12 +468,6 @@ public class NotificationManager { MessageItem message = messages.get(0); - Intent replyIntent = new Intent(context, QKReplyActivity.class); - replyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - replyIntent.putExtra(QKReplyActivity.EXTRA_THREAD_ID, threadId); - replyIntent.putExtra(QKReplyActivity.EXTRA_SHOW_KEYBOARD, true); - final PendingIntent replyPI = PendingIntent.getActivity(context, buildRequestCode(threadId, 0), replyIntent, PendingIntent.FLAG_UPDATE_CURRENT); - Intent threadIntent = new Intent(context, MainActivity.class); threadIntent.putExtra(MessageListActivity.ARG_THREAD_ID, threadId); final PendingIntent threadPI = PendingIntent.getActivity(context, buildRequestCode(threadId, 1), threadIntent, PendingIntent.FLAG_UPDATE_CURRENT); @@ -517,10 +510,21 @@ public class NotificationManager { .setNumber(unreadMessageCount) .setStyle(nstyle) .setColor(ThemeManager.getColor()) - .addAction(R.drawable.ic_reply, sRes.getString(R.string.reply), replyPI) .addAction(R.drawable.ic_accept, sRes.getString(R.string.read), readPI) - .extend(WearableIntentReceiver.getSingleConversationExtender(context, message.mContact, message.mAddress, threadId)) + .extend(RemoteMessagingReceiver.getConversationExtender(context, message.mContact, message.mAddress, threadId)) .setDeleteIntent(seenPI); + + if (Build.VERSION.SDK_INT < 23) { + Intent replyIntent = new Intent(context, QKReplyActivity.class); + replyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + replyIntent.putExtra(QKReplyActivity.EXTRA_THREAD_ID, threadId); + replyIntent.putExtra(QKReplyActivity.EXTRA_SHOW_KEYBOARD, true); + PendingIntent replyPI = PendingIntent.getActivity(context, buildRequestCode(threadId, 0), replyIntent, PendingIntent.FLAG_UPDATE_CURRENT); + builder.addAction(R.drawable.ic_reply, sRes.getString(R.string.reply), replyPI); + } else { + builder.addAction(RemoteMessagingReceiver.getReplyAction(context, message.mAddress, threadId)); + } + if (conversationPrefs.getDimissedReadEnabled()) { builder.setDeleteIntent(readPI); } @@ -570,12 +574,6 @@ public class NotificationManager { MessageItem message = messages.get(0); - Intent replyIntent = new Intent(context, QKReplyActivity.class); - replyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - replyIntent.putExtra(QKReplyActivity.EXTRA_THREAD_ID, threadId); - replyIntent.putExtra(QKReplyActivity.EXTRA_SHOW_KEYBOARD, true); - PendingIntent replyPI = PendingIntent.getActivity(context, buildRequestCode(threadId, 0), replyIntent, PendingIntent.FLAG_UPDATE_CURRENT); - Intent threadIntent = new Intent(context, MainActivity.class); threadIntent.putExtra(MessageListActivity.ARG_THREAD_ID, threadId); PendingIntent threadPI = PendingIntent.getActivity(context, buildRequestCode(threadId, 1), threadIntent, PendingIntent.FLAG_UPDATE_CURRENT); @@ -605,11 +603,21 @@ public class NotificationManager { .setNumber(unreadMessageCount) .setStyle(inboxStyle) .setColor(ThemeManager.getColor()) - .addAction(R.drawable.ic_reply, sRes.getString(R.string.reply), replyPI) .addAction(R.drawable.ic_accept, sRes.getString(R.string.read), readPI) - .extend(WearableIntentReceiver.getSingleConversationExtender(context, message.mContact, message.mAddress, threadId)) + .extend(RemoteMessagingReceiver.getConversationExtender(context, message.mContact, message.mAddress, threadId)) .setDeleteIntent(seenPI); + if (Build.VERSION.SDK_INT < 23) { + Intent replyIntent = new Intent(context, QKReplyActivity.class); + replyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + replyIntent.putExtra(QKReplyActivity.EXTRA_THREAD_ID, threadId); + replyIntent.putExtra(QKReplyActivity.EXTRA_SHOW_KEYBOARD, true); + PendingIntent replyPI = PendingIntent.getActivity(context, buildRequestCode(threadId, 0), replyIntent, PendingIntent.FLAG_UPDATE_CURRENT); + builder.addAction(R.drawable.ic_reply, sRes.getString(R.string.reply), replyPI); + } else { + builder.addAction(RemoteMessagingReceiver.getReplyAction(context, message.mAddress, threadId)); + } + if (conversationPrefs.getCallButtonEnabled()) { Intent callIntent = new Intent(Intent.ACTION_CALL); callIntent.setData(Uri.parse("tel:" + message.mAddress)); @@ -624,6 +632,7 @@ public class NotificationManager { * Creates a unique action ID for notification actions (Open, Mark read, Call, etc) */ private static int buildRequestCode(long threadId, int action) { + action++; // Fixes issue on some 4.3 phones | http://stackoverflow.com/questions/19031861/pendingintent-not-opening-activity-in-android-4-3 return (int) (action * 100000 + threadId); } @@ -740,7 +749,7 @@ public class NotificationManager { */ private static int getNotificationPriority(Context context) { boolean qkreplyEnabled = PreferenceManager.getDefaultSharedPreferences(context) - .getBoolean(SettingsFragment.QUICKREPLY, true); + .getBoolean(SettingsFragment.QUICKREPLY, Build.VERSION.SDK_INT < 23); if (qkreplyEnabled) { return NotificationCompat.PRIORITY_DEFAULT; } else { diff --git a/QKSMS/src/main/res/values-v19/bools.xml b/QKSMS/src/main/res/values-v19/bools.xml index 555f55c6..c83652f2 100644 --- a/QKSMS/src/main/res/values-v19/bools.xml +++ b/QKSMS/src/main/res/values-v19/bools.xml @@ -2,6 +2,4 @@ true false - false - true \ No newline at end of file diff --git a/QKSMS/src/main/res/values-v21/bools.xml b/QKSMS/src/main/res/values-v21/bools.xml index a1c6a7bc..9cd94006 100644 --- a/QKSMS/src/main/res/values-v21/bools.xml +++ b/QKSMS/src/main/res/values-v21/bools.xml @@ -1,7 +1,5 @@ - true - false true false \ No newline at end of file diff --git a/QKSMS/src/main/res/values-v23/bools.xml b/QKSMS/src/main/res/values-v23/bools.xml new file mode 100644 index 00000000..e96a807a --- /dev/null +++ b/QKSMS/src/main/res/values-v23/bools.xml @@ -0,0 +1,5 @@ + + + true + false + \ No newline at end of file diff --git a/QKSMS/src/main/res/values/bools.xml b/QKSMS/src/main/res/values/bools.xml index 918d2362..e0dcf9ff 100644 --- a/QKSMS/src/main/res/values/bools.xml +++ b/QKSMS/src/main/res/values/bools.xml @@ -2,6 +2,10 @@ false true + false true + + false + true \ No newline at end of file diff --git a/QKSMS/src/main/res/xml/settings.xml b/QKSMS/src/main/res/xml/settings.xml index 2c934944..d4547efe 100644 --- a/QKSMS/src/main/res/xml/settings.xml +++ b/QKSMS/src/main/res/xml/settings.xml @@ -151,7 +151,7 @@