2018-01-25 04:17:44 +01:00
|
|
|
/*
|
2016-09-14 23:58:24 +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;
|
|
|
|
|
2018-01-25 04:17:44 +01:00
|
|
|
import android.annotation.SuppressLint;
|
2018-02-02 04:22:48 +01:00
|
|
|
import android.content.BroadcastReceiver;
|
2016-09-14 23:58:24 +02:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.os.Bundle;
|
2021-10-04 09:51:19 +02:00
|
|
|
|
2020-08-19 02:06:26 +02:00
|
|
|
import androidx.core.app.RemoteInput;
|
2016-09-14 23:58:24 +02:00
|
|
|
|
2021-10-04 09:51:19 +02:00
|
|
|
import org.session.libsession.messaging.messages.signal.OutgoingMediaMessage;
|
|
|
|
import org.session.libsession.messaging.messages.signal.OutgoingTextMessage;
|
2021-03-02 02:24:09 +01:00
|
|
|
import org.session.libsession.messaging.messages.visible.VisibleMessage;
|
|
|
|
import org.session.libsession.messaging.sending_receiving.MessageSender;
|
2021-05-18 08:11:38 +02:00
|
|
|
import org.session.libsession.utilities.Address;
|
2021-05-18 08:03:47 +02:00
|
|
|
import org.session.libsession.utilities.recipients.Recipient;
|
2021-05-18 01:12:33 +02:00
|
|
|
import org.session.libsignal.utilities.Log;
|
2021-10-04 09:51:19 +02:00
|
|
|
import org.thoughtcrime.securesms.ApplicationContext;
|
|
|
|
import org.thoughtcrime.securesms.database.MessagingDatabase.MarkedMessageInfo;
|
|
|
|
import org.thoughtcrime.securesms.dependencies.DatabaseComponent;
|
|
|
|
import org.thoughtcrime.securesms.mms.MmsException;
|
2016-09-14 23:58:24 +02:00
|
|
|
|
2018-04-27 02:03:54 +02:00
|
|
|
import java.util.Collections;
|
2016-09-14 23:58:24 +02:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the response text from the Android Auto and sends an message as a reply
|
|
|
|
*/
|
2018-02-02 04:22:48 +01:00
|
|
|
public class AndroidAutoReplyReceiver extends BroadcastReceiver {
|
2016-09-14 23:58:24 +02:00
|
|
|
|
2017-07-26 18:59:15 +02:00
|
|
|
public static final String TAG = AndroidAutoReplyReceiver.class.getSimpleName();
|
2020-03-11 23:52:42 +01:00
|
|
|
public static final String REPLY_ACTION = "network.loki.securesms.notifications.ANDROID_AUTO_REPLY";
|
2017-08-01 17:56:00 +02:00
|
|
|
public static final String ADDRESS_EXTRA = "car_address";
|
2017-07-26 18:59:15 +02:00
|
|
|
public static final String VOICE_REPLY_KEY = "car_voice_reply_key";
|
|
|
|
public static final String THREAD_ID_EXTRA = "car_reply_thread_id";
|
2016-09-14 23:58:24 +02:00
|
|
|
|
2018-01-25 04:17:44 +01:00
|
|
|
@SuppressLint("StaticFieldLeak")
|
2016-09-14 23:58:24 +02:00
|
|
|
@Override
|
2018-02-02 04:22:48 +01:00
|
|
|
public void onReceive(final Context context, Intent intent)
|
2016-09-14 23:58:24 +02:00
|
|
|
{
|
|
|
|
if (!REPLY_ACTION.equals(intent.getAction())) return;
|
|
|
|
|
|
|
|
Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
|
|
|
|
|
|
|
|
if (remoteInput == null) return;
|
|
|
|
|
2017-08-01 17:56:00 +02:00
|
|
|
final Address address = intent.getParcelableExtra(ADDRESS_EXTRA);
|
2016-09-14 23:58:24 +02:00
|
|
|
final long threadId = intent.getLongExtra(THREAD_ID_EXTRA, -1);
|
|
|
|
final CharSequence responseText = getMessageText(intent);
|
2017-08-22 03:32:38 +02:00
|
|
|
final Recipient recipient = Recipient.from(context, address, false);
|
2016-09-14 23:58:24 +02:00
|
|
|
|
|
|
|
if (responseText != null) {
|
|
|
|
new AsyncTask<Void, Void, Void>() {
|
|
|
|
@Override
|
|
|
|
protected Void doInBackground(Void... params) {
|
|
|
|
|
|
|
|
long replyThreadId;
|
|
|
|
|
2021-03-02 02:24:09 +01:00
|
|
|
if (threadId == -1) {
|
2021-10-04 09:51:19 +02:00
|
|
|
replyThreadId = DatabaseComponent.get(context).threadDatabase().getOrCreateThreadIdFor(recipient);
|
2021-03-02 02:24:09 +01:00
|
|
|
} else {
|
|
|
|
replyThreadId = threadId;
|
|
|
|
}
|
|
|
|
|
|
|
|
VisibleMessage message = new VisibleMessage();
|
|
|
|
message.setText(responseText.toString());
|
|
|
|
message.setSentTimestamp(System.currentTimeMillis());
|
|
|
|
MessageSender.send(message, recipient.getAddress());
|
2016-09-14 23:58:24 +02:00
|
|
|
|
2017-08-01 17:56:00 +02:00
|
|
|
if (recipient.isGroupRecipient()) {
|
2017-04-23 01:29:26 +02:00
|
|
|
Log.w("AndroidAutoReplyReceiver", "GroupRecipient, Sending media message");
|
2021-03-02 02:24:09 +01:00
|
|
|
OutgoingMediaMessage reply = OutgoingMediaMessage.from(message, recipient, Collections.emptyList(), null, null);
|
|
|
|
try {
|
2021-10-04 09:51:19 +02:00
|
|
|
DatabaseComponent.get(context).mmsDatabase().insertMessageOutbox(reply, replyThreadId, false, null);
|
2021-03-02 02:24:09 +01:00
|
|
|
} catch (MmsException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
2016-09-14 23:58:24 +02:00
|
|
|
} else {
|
2017-04-23 01:29:26 +02:00
|
|
|
Log.w("AndroidAutoReplyReceiver", "Sending regular message ");
|
2021-03-02 02:24:09 +01:00
|
|
|
OutgoingTextMessage reply = OutgoingTextMessage.from(message, recipient);
|
2021-10-04 09:51:19 +02:00
|
|
|
DatabaseComponent.get(context).smsDatabase().insertMessageOutbox(replyThreadId, reply, false, System.currentTimeMillis(), null);
|
2016-09-14 23:58:24 +02:00
|
|
|
}
|
|
|
|
|
2021-10-04 09:51:19 +02:00
|
|
|
List<MarkedMessageInfo> messageIds = DatabaseComponent.get(context).threadDatabase().setRead(replyThreadId, true);
|
2017-02-14 07:35:47 +01:00
|
|
|
|
2020-06-26 08:18:19 +02:00
|
|
|
ApplicationContext.getInstance(context).messageNotifier.updateNotification(context);
|
2016-09-14 23:58:24 +02:00
|
|
|
MarkReadReceiver.process(context, messageIds);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2017-10-23 22:03:32 +02:00
|
|
|
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2016-09-14 23:58:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private CharSequence getMessageText(Intent intent) {
|
|
|
|
Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
|
|
|
|
if (remoteInput != null) {
|
|
|
|
return remoteInput.getCharSequence(VOICE_REPLY_KEY);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|