2015-05-19 10:24:08 +02:00
|
|
|
/**
|
|
|
|
* Copyright (C) 2011 Whisper Systems
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.thoughtcrime.securesms.notifications;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.os.Bundle;
|
2015-06-13 20:23:30 -07:00
|
|
|
import android.support.annotation.Nullable;
|
2015-05-19 10:24:08 +02:00
|
|
|
import android.support.v4.app.RemoteInput;
|
|
|
|
|
2016-02-19 17:07:41 -08:00
|
|
|
import org.thoughtcrime.securesms.ApplicationContext;
|
2015-10-12 18:25:05 -07:00
|
|
|
import org.thoughtcrime.securesms.attachments.Attachment;
|
2015-05-19 10:24:08 +02:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
2016-02-19 17:07:41 -08:00
|
|
|
import org.thoughtcrime.securesms.database.MessagingDatabase;
|
|
|
|
import org.thoughtcrime.securesms.database.MessagingDatabase.SyncMessageId;
|
2016-02-05 16:10:33 -08:00
|
|
|
import org.thoughtcrime.securesms.database.RecipientPreferenceDatabase.RecipientsPreferences;
|
2016-02-19 17:07:41 -08:00
|
|
|
import org.thoughtcrime.securesms.jobs.MultiDeviceReadUpdateJob;
|
2015-06-13 20:23:30 -07:00
|
|
|
import org.thoughtcrime.securesms.mms.OutgoingMediaMessage;
|
2015-05-19 10:24:08 +02:00
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFactory;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
|
|
|
import org.thoughtcrime.securesms.sms.MessageSender;
|
|
|
|
import org.thoughtcrime.securesms.sms.OutgoingTextMessage;
|
2016-03-23 10:34:41 -07:00
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
2015-05-19 10:24:08 +02:00
|
|
|
|
2015-10-12 18:25:05 -07:00
|
|
|
import java.util.LinkedList;
|
2016-02-19 17:07:41 -08:00
|
|
|
import java.util.List;
|
2015-10-12 18:25:05 -07:00
|
|
|
|
2015-05-19 10:24:08 +02:00
|
|
|
/**
|
|
|
|
* Get the response text from the Wearable Device and sends an message as a reply
|
|
|
|
*/
|
2015-06-13 20:23:30 -07:00
|
|
|
public class WearReplyReceiver extends MasterSecretBroadcastReceiver {
|
2015-05-19 10:24:08 +02:00
|
|
|
|
2015-06-13 20:23:30 -07:00
|
|
|
public static final String TAG = WearReplyReceiver.class.getSimpleName();
|
|
|
|
public static final String REPLY_ACTION = "org.thoughtcrime.securesms.notifications.WEAR_REPLY";
|
|
|
|
public static final String RECIPIENT_IDS_EXTRA = "recipient_ids";
|
2015-05-19 10:24:08 +02:00
|
|
|
|
|
|
|
@Override
|
2015-06-13 20:23:30 -07:00
|
|
|
protected void onReceive(final Context context, Intent intent,
|
|
|
|
final @Nullable MasterSecret masterSecret)
|
|
|
|
{
|
|
|
|
if (!REPLY_ACTION.equals(intent.getAction())) return;
|
2015-05-19 10:24:08 +02:00
|
|
|
|
|
|
|
Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
|
|
|
|
|
2015-06-13 20:23:30 -07:00
|
|
|
if (remoteInput == null) return;
|
2015-05-19 10:24:08 +02:00
|
|
|
|
2015-06-13 20:23:30 -07:00
|
|
|
final long[] recipientIds = intent.getLongArrayExtra(RECIPIENT_IDS_EXTRA);
|
|
|
|
final CharSequence responseText = remoteInput.getCharSequence(MessageNotifier.EXTRA_VOICE_REPLY);
|
|
|
|
final Recipients recipients = RecipientFactory.getRecipientsForIds(context, recipientIds, false);
|
2015-05-19 10:24:08 +02:00
|
|
|
|
2015-06-13 20:23:30 -07:00
|
|
|
if (masterSecret != null && responseText != null) {
|
2015-05-19 10:24:08 +02:00
|
|
|
new AsyncTask<Void, Void, Void>() {
|
|
|
|
@Override
|
|
|
|
protected Void doInBackground(Void... params) {
|
2015-06-13 20:23:30 -07:00
|
|
|
long threadId;
|
2015-05-19 10:24:08 +02:00
|
|
|
|
2016-02-05 16:10:33 -08:00
|
|
|
Optional<RecipientsPreferences> preferences = DatabaseFactory.getRecipientPreferenceDatabase(context).getRecipientsPreferences(recipientIds);
|
2016-08-15 20:23:56 -07:00
|
|
|
int subscriptionId = preferences.isPresent() ? preferences.get().getDefaultSubscriptionId().or(-1) : -1;
|
|
|
|
long expiresIn = preferences.isPresent() ? preferences.get().getExpireMessages() * 1000 : 0;
|
2016-02-05 16:10:33 -08:00
|
|
|
|
2015-06-13 20:23:30 -07:00
|
|
|
if (recipients.isGroupRecipient()) {
|
2016-08-15 20:23:56 -07:00
|
|
|
OutgoingMediaMessage reply = new OutgoingMediaMessage(recipients, responseText.toString(), new LinkedList<Attachment>(), System.currentTimeMillis(), subscriptionId, expiresIn, 0);
|
2015-06-13 20:23:30 -07:00
|
|
|
threadId = MessageSender.send(context, masterSecret, reply, -1, false);
|
|
|
|
} else {
|
2016-08-15 20:23:56 -07:00
|
|
|
OutgoingTextMessage reply = new OutgoingTextMessage(recipients, responseText.toString(), expiresIn, subscriptionId);
|
2015-06-13 20:23:30 -07:00
|
|
|
threadId = MessageSender.send(context, masterSecret, reply, -1, false);
|
|
|
|
}
|
2015-05-19 10:24:08 +02:00
|
|
|
|
2016-02-19 17:07:41 -08:00
|
|
|
List<SyncMessageId> messageIds = DatabaseFactory.getThreadDatabase(context).setRead(threadId);
|
2015-05-19 10:24:08 +02:00
|
|
|
MessageNotifier.updateNotification(context, masterSecret);
|
2015-06-13 20:23:30 -07:00
|
|
|
|
2016-02-19 17:07:41 -08:00
|
|
|
if (!messageIds.isEmpty()) {
|
|
|
|
ApplicationContext.getInstance(context)
|
|
|
|
.getJobManager()
|
|
|
|
.add(new MultiDeviceReadUpdateJob(context, messageIds));
|
|
|
|
}
|
|
|
|
|
2015-05-19 10:24:08 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}.execute();
|
|
|
|
}
|
2015-06-13 20:23:30 -07:00
|
|
|
|
2015-05-19 10:24:08 +02:00
|
|
|
}
|
|
|
|
}
|