session-android/app/src/main/java/org/thoughtcrime/securesms/notifications/RemoteReplyReceiver.java

107 lines
4.4 KiB
Java
Raw Normal View History

/*
* 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.annotation.SuppressLint;
2018-02-02 04:22:48 +01:00
import android.content.BroadcastReceiver;
2015-05-19 10:24:08 +02:00
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import androidx.core.app.RemoteInput;
2015-05-19 10:24:08 +02:00
2021-03-02 02:24:09 +01:00
import org.session.libsession.messaging.messages.signal.OutgoingTextMessage;
import org.session.libsession.messaging.messages.visible.VisibleMessage;
import org.session.libsignal.utilities.logging.Log;
import org.thoughtcrime.securesms.ApplicationContext;
2021-01-13 07:11:30 +01:00
import org.session.libsession.messaging.threads.Address;
2015-05-19 10:24:08 +02:00
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.MessagingDatabase.MarkedMessageInfo;
2021-03-02 02:24:09 +01:00
import org.thoughtcrime.securesms.mms.MmsException;
import org.thoughtcrime.securesms.mms.OutgoingMediaMessage;
2021-01-13 07:11:30 +01:00
import org.session.libsession.messaging.threads.recipients.Recipient;
2015-05-19 10:24:08 +02:00
import java.util.Collections;
import java.util.List;
2015-05-19 10:24:08 +02:00
/**
* Get the response text from the Wearable Device and sends an message as a reply
*/
2018-02-02 04:22:48 +01:00
public class RemoteReplyReceiver extends BroadcastReceiver {
2015-05-19 10:24:08 +02:00
public static final String TAG = RemoteReplyReceiver.class.getSimpleName();
public static final String REPLY_ACTION = "network.loki.securesms.notifications.WEAR_REPLY";
public static final String ADDRESS_EXTRA = "address";
public static final String REPLY_METHOD = "reply_method";
2015-05-19 10:24:08 +02:00
@SuppressLint("StaticFieldLeak")
2015-05-19 10:24:08 +02:00
@Override
2018-02-02 04:22:48 +01:00
public void onReceive(final Context context, Intent intent) {
if (!REPLY_ACTION.equals(intent.getAction())) return;
2015-05-19 10:24:08 +02:00
Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
if (remoteInput == null) return;
2015-05-19 10:24:08 +02:00
final Address address = intent.getParcelableExtra(ADDRESS_EXTRA);
final ReplyMethod replyMethod = (ReplyMethod) intent.getSerializableExtra(REPLY_METHOD);
final CharSequence responseText = remoteInput.getCharSequence(DefaultMessageNotifier.EXTRA_REMOTE_REPLY);
2015-05-19 10:24:08 +02:00
if (address == null) throw new AssertionError("No address specified");
if (replyMethod == null) throw new AssertionError("No reply method specified");
2018-02-02 04:22:48 +01:00
if (responseText != null) {
2015-05-19 10:24:08 +02:00
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
Recipient recipient = Recipient.from(context, address, false);
2021-03-02 02:24:09 +01:00
long threadId = DatabaseFactory.getThreadDatabase(context).getOrCreateThreadIdFor(recipient);
VisibleMessage message = new VisibleMessage();
message.setText(responseText.toString());
switch (replyMethod) {
case GroupMessage: {
2021-03-02 02:24:09 +01:00
OutgoingMediaMessage reply = OutgoingMediaMessage.from(message, recipient, Collections.emptyList(), null, null);
try {
DatabaseFactory.getMmsDatabase(context).insertMessageOutbox(reply, threadId, false, null);
} catch (MmsException e) {
Log.w(TAG, e);
}
break;
}
case SecureMessage: {
2021-03-02 02:24:09 +01:00
OutgoingTextMessage reply = OutgoingTextMessage.from(message, recipient);
DatabaseFactory.getSmsDatabase(context).insertMessageOutbox(threadId, reply, false, System.currentTimeMillis(), null);
break;
}
default:
throw new AssertionError("Unknown Reply method");
}
2015-05-19 10:24:08 +02:00
List<MarkedMessageInfo> messageIds = DatabaseFactory.getThreadDatabase(context).setRead(threadId, true);
ApplicationContext.getInstance(context).messageNotifier.updateNotification(context);
MarkReadReceiver.process(context, messageIds);
2015-05-19 10:24:08 +02:00
return null;
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2015-05-19 10:24:08 +02:00
}
}
}