2015-05-19 10:24:08 +02:00
|
|
|
/**
|
2016-12-25 19:00:33 -08:00
|
|
|
* Copyright (C) 2016 Open Whisper Systems
|
2015-05-19 10:24:08 +02:00
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
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;
|
2017-07-26 09:59:15 -07:00
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
2015-05-19 10:24:08 +02:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
2016-10-10 11:13:37 -07:00
|
|
|
import org.thoughtcrime.securesms.database.MessagingDatabase.MarkedMessageInfo;
|
2016-02-05 16:10:33 -08:00
|
|
|
import org.thoughtcrime.securesms.database.RecipientPreferenceDatabase.RecipientsPreferences;
|
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
|
|
|
|
*/
|
2016-12-25 19:00:33 -08:00
|
|
|
public class RemoteReplyReceiver extends MasterSecretBroadcastReceiver {
|
2015-05-19 10:24:08 +02:00
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
public static final String TAG = RemoteReplyReceiver.class.getSimpleName();
|
|
|
|
public static final String REPLY_ACTION = "org.thoughtcrime.securesms.notifications.WEAR_REPLY";
|
|
|
|
public static final String ADDRESSES_EXTRA = "addresses";
|
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
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
final Address[] addresses = Address.fromParcelable(intent.getParcelableArrayExtra(ADDRESSES_EXTRA));
|
2016-12-25 19:00:33 -08:00
|
|
|
final CharSequence responseText = remoteInput.getCharSequence(MessageNotifier.EXTRA_REMOTE_REPLY);
|
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
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
Optional<RecipientsPreferences> preferences = DatabaseFactory.getRecipientPreferenceDatabase(context).getRecipientsPreferences(addresses);
|
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
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
Recipients recipients = RecipientFactory.getRecipientsFor(context, addresses, false);
|
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);
|
2017-04-22 16:29:26 -07:00
|
|
|
threadId = MessageSender.send(context, masterSecret, reply, -1, false, null);
|
2015-06-13 20:23:30 -07:00
|
|
|
} else {
|
2016-08-15 20:23:56 -07:00
|
|
|
OutgoingTextMessage reply = new OutgoingTextMessage(recipients, responseText.toString(), expiresIn, subscriptionId);
|
2017-04-22 16:29:26 -07:00
|
|
|
threadId = MessageSender.send(context, masterSecret, reply, -1, false, null);
|
2015-06-13 20:23:30 -07:00
|
|
|
}
|
2015-05-19 10:24:08 +02:00
|
|
|
|
2017-02-22 15:05:35 -08:00
|
|
|
List<MarkedMessageInfo> messageIds = DatabaseFactory.getThreadDatabase(context).setRead(threadId, true);
|
2017-02-13 22:35:47 -08:00
|
|
|
|
2015-05-19 10:24:08 +02:00
|
|
|
MessageNotifier.updateNotification(context, masterSecret);
|
2016-10-10 11:13:37 -07:00
|
|
|
MarkReadReceiver.process(context, messageIds);
|
2016-02-19 17:07:41 -08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|