Skip to content

Commit f0b2cc5

Browse files
chr1shaefn3rmoxie0
authored andcommitted
Added WearReplyReceiver
1 parent 69e1252 commit f0b2cc5

9 files changed

Lines changed: 137 additions & 6 deletions

File tree

‎AndroidManifest.xml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,14 @@
316316
</intent-filter>
317317
</receiver>
318318

319+
<receiver android:name=".notifications.WearReplyReceiver"
320+
android:enabled="true"
321+
android:exported="true">
322+
<intent-filter>
323+
<action android:name="org.thoughtcrime.securesms.notifications.WEAR_REPLY"/>
324+
</intent-filter>
325+
</receiver>
326+
319327
<provider android:name=".providers.PartProvider"
320328
android:grantUriPermissions="true"
321329
android:authorities="org.thoughtcrime.provider.securesms" />

‎res/drawable-hdpi/ic_reply.png‎

462 Bytes
Loading

‎res/drawable-mdpi/ic_reply.png‎

343 Bytes
Loading

‎res/drawable-xhdpi/ic_reply.png‎

561 Bytes
Loading

‎res/drawable-xxhdpi/ic_reply.png‎

775 Bytes
Loading

‎res/values/strings.xml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,14 @@
982982
<string name="transport_selection_list_item__transport_icon">Transport icon</string>
983983

984984

985+
<!-- Wear Reply -->
986+
<string name="wear_reply_label">Reply</string>
987+
<string-array name="wear_replies">
988+
<item>Yes</item>
989+
<item>No</item>
990+
<item>OK</item>
991+
<item>Thanks</item>
992+
</string-array>
985993
<!-- EOF -->
986994

987995
</resources>

‎src/org/thoughtcrime/securesms/notifications/MessageNotifier.java‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import android.support.v4.app.NotificationCompat.Action;
3737
import android.support.v4.app.NotificationCompat.BigTextStyle;
3838
import android.support.v4.app.NotificationCompat.InboxStyle;
39+
import android.support.v4.app.RemoteInput;
3940
import android.text.Spannable;
4041
import android.text.SpannableString;
4142
import android.text.SpannableStringBuilder;
@@ -86,6 +87,8 @@ public class MessageNotifier {
8687

8788
private volatile static long visibleThread = -1;
8889

90+
public static final String EXTRA_VOICE_REPLY = "extra_voice_reply";
91+
8992
public static void setVisibleThread(long threadId) {
9093
visibleThread = threadId;
9194
}
@@ -222,9 +225,22 @@ private static void sendSingleThreadNotification(Context context,
222225
Action markAsReadAction = new Action(R.drawable.check,
223226
context.getString(R.string.MessageNotifier_mark_read),
224227
notificationState.getMarkAsReadIntent(context, masterSecret));
228+
229+
Action replyAction = new Action(R.drawable.ic_reply_white_36dp,
230+
context.getString(R.string.MessageNotifier_reply),
231+
notifications.get(0).getReplyIntent(context));
232+
233+
Action wearableReplyAction = new Action.Builder(R.drawable.ic_reply,
234+
context.getString(R.string.wear_reply_label),
235+
notificationState.getReplyIntent(context, masterSecret, recipient.getRecipientId()))
236+
.addRemoteInput(new RemoteInput.Builder(EXTRA_VOICE_REPLY).setLabel(context.getString(R.string.wear_reply_label)).build())
237+
.build();
238+
225239
builder.addAction(markAsReadAction);
226-
builder.addAction(new Action(R.drawable.ic_reply_white_36dp, context.getString(R.string.MessageNotifier_reply), notifications.get(0).getReplyIntent(context)));
227-
builder.extend(new NotificationCompat.WearableExtender().addAction(markAsReadAction));
240+
builder.addAction(replyAction);
241+
242+
builder.extend(new NotificationCompat.WearableExtender().addAction(markAsReadAction)
243+
.addAction(wearableReplyAction));
228244
}
229245

230246
SpannableStringBuilder content = new SpannableStringBuilder();

‎src/org/thoughtcrime/securesms/notifications/NotificationState.java‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
import android.content.Context;
55
import android.content.Intent;
66
import android.net.Uri;
7+
import android.os.Bundle;
78
import android.support.annotation.Nullable;
89
import android.util.Log;
910

1011
import org.thoughtcrime.securesms.crypto.MasterSecret;
11-
import org.thoughtcrime.securesms.database.RecipientPreferenceDatabase;
1212
import org.thoughtcrime.securesms.database.RecipientPreferenceDatabase.VibrateState;
1313
import org.thoughtcrime.securesms.recipients.Recipients;
1414

@@ -71,6 +71,19 @@ public List<NotificationItem> getNotifications() {
7171
}
7272

7373
public PendingIntent getMarkAsReadIntent(Context context, MasterSecret masterSecret) {
74+
Bundle extras = new Bundle();
75+
extras.putParcelable("master_secret", masterSecret);
76+
return craftIntent(context, MarkReadReceiver.CLEAR_ACTION, extras);
77+
}
78+
79+
public PendingIntent getReplyIntent(Context context, MasterSecret masterSecret, long recipientId) {
80+
Bundle extras = new Bundle();
81+
extras.putParcelable("master_secret", masterSecret);
82+
extras.putLong("recipient_id", recipientId);
83+
return craftIntent(context, WearReplyReceiver.REPLY_ACTION, extras);
84+
}
85+
86+
private PendingIntent craftIntent(Context context, String intentAction, Bundle extras) {
7487
long[] threadArray = new long[threads.size()];
7588
int index = 0;
7689

@@ -79,16 +92,16 @@ public PendingIntent getMarkAsReadIntent(Context context, MasterSecret masterSec
7992
threadArray[index++] = thread;
8093
}
8194

82-
Intent intent = new Intent(MarkReadReceiver.CLEAR_ACTION);
95+
Intent intent = new Intent(intentAction);
8396
intent.putExtra("thread_ids", threadArray);
84-
intent.putExtra("master_secret", masterSecret);
97+
intent.putExtras(extras);
8598
intent.setPackage(context.getPackageName());
8699

87100
// XXX : This is an Android bug. If we don't pull off the extra
88101
// once before handing off the PendingIntent, the array will be
89102
// truncated to one element when the PendingIntent fires. Thanks guys!
90103
Log.w("NotificationState", "Pending array off intent length: " +
91-
intent.getLongArrayExtra("thread_ids").length);
104+
intent.getLongArrayExtra("thread_ids").length);
92105

93106
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
94107
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* Copyright (C) 2011 Whisper Systems
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package org.thoughtcrime.securesms.notifications;
19+
20+
import android.app.NotificationManager;
21+
import android.content.BroadcastReceiver;
22+
import android.content.Context;
23+
import android.content.Intent;
24+
import android.os.AsyncTask;
25+
import android.os.Bundle;
26+
import android.support.v4.app.RemoteInput;
27+
import android.util.Log;
28+
29+
import org.thoughtcrime.securesms.crypto.MasterSecret;
30+
import org.thoughtcrime.securesms.database.DatabaseFactory;
31+
import org.thoughtcrime.securesms.recipients.Recipient;
32+
import org.thoughtcrime.securesms.recipients.RecipientFactory;
33+
import org.thoughtcrime.securesms.recipients.RecipientProvider;
34+
import org.thoughtcrime.securesms.recipients.Recipients;
35+
import org.thoughtcrime.securesms.sms.MessageSender;
36+
import org.thoughtcrime.securesms.sms.OutgoingTextMessage;
37+
38+
/**
39+
* Get the response text from the Wearable Device and sends an message as a reply
40+
*
41+
* @author Alix Ducros (Ported to TextSecure-Codebase by Christoph Haefner)
42+
*/
43+
public class WearReplyReceiver extends BroadcastReceiver {
44+
45+
public static final String TAG = WearReplyReceiver.class.getSimpleName();
46+
public static final String REPLY_ACTION = "org.thoughtcrime.securesms.notifications.WEAR_REPLY";
47+
48+
@Override
49+
public void onReceive(final Context context, Intent intent) {
50+
if (!intent.getAction().equals(REPLY_ACTION))
51+
return;
52+
53+
Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
54+
if (remoteInput == null)
55+
return;
56+
57+
final long[] threadIds = intent.getLongArrayExtra("thread_ids");
58+
final MasterSecret masterSecret = intent.getParcelableExtra("master_secret");
59+
final long recipientId = intent.getLongExtra("recipient_id", -1);
60+
final CharSequence responseText = remoteInput.getCharSequence(MessageNotifier.EXTRA_VOICE_REPLY);
61+
62+
final Recipients recipients = RecipientFactory.getRecipientsForIds(context, new long[]{recipientId}, false);
63+
64+
if (threadIds != null && masterSecret != null) {
65+
66+
((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE))
67+
.cancel(MessageNotifier.NOTIFICATION_ID);
68+
69+
new AsyncTask<Void, Void, Void>() {
70+
@Override
71+
protected Void doInBackground(Void... params) {
72+
for (long threadId : threadIds) {
73+
Log.w(TAG, "Marking as read: " + threadId);
74+
DatabaseFactory.getThreadDatabase(context).setRead(threadId);
75+
}
76+
77+
OutgoingTextMessage reply = new OutgoingTextMessage(recipients, responseText.toString());
78+
MessageSender.send(context, masterSecret, reply, threadIds[0], false);
79+
80+
MessageNotifier.updateNotification(context, masterSecret);
81+
return null;
82+
}
83+
}.execute();
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)