Implemented Android N quick reply API

This commit is contained in:
Moez Bhatti
2016-06-14 00:42:26 -04:00
parent 45bec07de1
commit 47efb678eb
10 changed files with 92 additions and 78 deletions
@@ -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
@@ -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<String> defaultResponses = new HashSet<>(Arrays.asList(context.getResources().getStringArray(R.array.qk_responses)));
Set<String> responseSet = prefs.getStringSet(SettingsFragment.QK_RESPONSES, defaultResponses);
ArrayList<String> responses = new ArrayList<String>();
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<String> defaultResponses = new HashSet<>(Arrays.asList(context.getResources().getStringArray(R.array.qk_responses)));
Set<String> responseSet = prefs.getStringSet(SettingsFragment.QK_RESPONSES, defaultResponses);
ArrayList<String> 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();
}
}
@@ -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);
@@ -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 {