From a8f0bfb0a8c3c3f09075118fe0b1070a0a4529c3 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Wed, 19 Aug 2020 13:57:33 +0200 Subject: [PATCH] listen to orbot events instead of using intent result to reconnect account (cherry picked from commit 70c10fd0de14fd2dc042d60bdc98ad10c3108d1c) --- .../conversations/services/EventReceiver.java | 18 ++++++------- .../services/XmppConnectionService.java | 27 +++++++++++++++---- .../conversations/ui/EditAccountActivity.java | 11 -------- .../conversations/utils/TorServiceUtils.java | 3 +++ 4 files changed, 33 insertions(+), 26 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/services/EventReceiver.java b/src/main/java/eu/siacs/conversations/services/EventReceiver.java index d5f4cf13a..93fe7686f 100644 --- a/src/main/java/eu/siacs/conversations/services/EventReceiver.java +++ b/src/main/java/eu/siacs/conversations/services/EventReceiver.java @@ -7,6 +7,8 @@ import android.os.Bundle; import android.preference.PreferenceManager; import android.util.Log; +import com.google.common.base.Strings; + import eu.siacs.conversations.Config; import eu.siacs.conversations.utils.Compatibility; @@ -18,17 +20,13 @@ public class EventReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, final Intent originalIntent) { final Intent intentForService = new Intent(context, XmppConnectionService.class); - if (originalIntent.getAction() != null) { - intentForService.setAction(originalIntent.getAction()); - final Bundle extras = originalIntent.getExtras(); - if (extras != null) { - intentForService.putExtras(extras); - } - } else { - intentForService.setAction("other"); - } final String action = originalIntent.getAction(); - if (action.equals("ui") || hasEnabledAccounts(context)) { + intentForService.setAction(Strings.isNullOrEmpty(action) ? "other" : action); + final Bundle extras = originalIntent.getExtras(); + if (extras != null) { + intentForService.putExtras(extras); + } + if ("ui".equals(action) || hasEnabledAccounts(context)) { Compatibility.startService(context, intentForService); } else { Log.d(Config.LOGTAG, "EventReceiver ignored action " + intentForService.getAction()); diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index ea0507200..affd78c7c 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -136,6 +136,7 @@ import eu.siacs.conversations.utils.ReplacingTaskManager; import eu.siacs.conversations.utils.Resolver; import eu.siacs.conversations.utils.SerialSingleThreadExecutor; import eu.siacs.conversations.utils.StringUtils; +import eu.siacs.conversations.utils.TorServiceUtils; import eu.siacs.conversations.utils.WakeLockHelper; import eu.siacs.conversations.utils.XmppUri; import eu.siacs.conversations.xml.Element; @@ -668,8 +669,15 @@ public class XmppConnectionService extends Service { final String sessionId = intent.getStringExtra(RtpSessionActivity.EXTRA_SESSION_ID); Log.d(Config.LOGTAG, "received intent to dismiss call with session id " + sessionId); mJingleConnectionManager.rejectRtpSession(sessionId); + break; } - break; + case TorServiceUtils.ACTION_STATUS: + final String status = intent.getStringExtra(TorServiceUtils.EXTRA_STATUS); + if ("ON".equals(status)) { + handleOrbotStartedEvent(); + return START_STICKY; + } + break; case ACTION_END_CALL: { final String sessionId = intent.getStringExtra(RtpSessionActivity.EXTRA_SESSION_ID); Log.d(Config.LOGTAG, "received intent to end call with session id " + sessionId); @@ -820,6 +828,14 @@ public class XmppConnectionService extends Service { return START_STICKY; } + private void handleOrbotStartedEvent() { + for (final Account account : accounts) { + if (account.getStatus() == Account.State.TOR_NOT_AVAILABLE) { + reconnectAccount(account, true, false); + } + } + } + private void deleteWebpreviewCache() { new Thread(() -> { try { @@ -1327,15 +1343,16 @@ public class XmppConnectionService extends Service { toggleForegroundService(); updateUnreadCountBadge(); toggleScreenEventReceiver(); + final IntentFilter intentFilter = new IntentFilter(); + intentFilter.addAction(TorServiceUtils.ACTION_STATUS); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { scheduleNextIdlePing(); - IntentFilter intentFilter = new IntentFilter(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); } intentFilter.addAction(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED); - registerReceiver(this.mInternalEventReceiver, intentFilter); } + registerReceiver(this.mInternalEventReceiver, intentFilter); mForceDuringOnCreate.set(false); toggleForegroundService(); //start export log service every day at given time @@ -1395,7 +1412,7 @@ public class XmppConnectionService extends Service { public void onDestroy() { try { unregisterReceiver(this.mInternalEventReceiver); - } catch (IllegalArgumentException e) { + } catch (final IllegalArgumentException e) { //ignored } destroyed = false; @@ -2451,7 +2468,7 @@ public class XmppConnectionService extends Service { final Jid jid = Jid.ofEscaped(address); final Account account = new Account(jid, password); account.setOption(Account.OPTION_DISABLED, true); - Log.d(Config.LOGTAG,jid.asBareJid().toEscapedString()+": provisioning account"); + Log.d(Config.LOGTAG, jid.asBareJid().toEscapedString() + ": provisioning account"); createAccount(account); } diff --git a/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java b/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java index ee2b7c2c6..9a4a4fe8e 100644 --- a/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java @@ -95,7 +95,6 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat private static final int REQUEST_CHANGE_STATUS = 0xee11; private static final int REQUEST_ORBOT = 0xff22; private static final int REQUEST_IMPORT_BACKUP = 0x63fb; - private AlertDialog mCaptchaDialog = null; private final AtomicBoolean mPendingReconnect = new AtomicBoolean(false); private final AtomicBoolean redirectInProgress = new AtomicBoolean(false); @@ -487,13 +486,6 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat Log.d(Config.LOGTAG, "pgp result not ok"); } } - if (requestCode == REQUEST_ORBOT) { - if (xmppConnectionService != null && mAccount != null) { - xmppConnectionService.reconnectAccountInBackground(mAccount); - } else { - mPendingReconnect.set(true); - } - } } @Override @@ -862,9 +854,6 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat } if (mAccount != null) { - if (mPendingReconnect.compareAndSet(true, false)) { - xmppConnectionService.reconnectAccountInBackground(mAccount); - } this.mInitMode |= this.mAccount.isOptionSet(Account.OPTION_REGISTER); this.mUsernameMode |= mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE) && mAccount.isOptionSet(Account.OPTION_REGISTER) && !useOwnProvider; if (mPendingFingerprintVerificationUri != null) { diff --git a/src/main/java/eu/siacs/conversations/utils/TorServiceUtils.java b/src/main/java/eu/siacs/conversations/utils/TorServiceUtils.java index 493fdd333..58f756987 100644 --- a/src/main/java/eu/siacs/conversations/utils/TorServiceUtils.java +++ b/src/main/java/eu/siacs/conversations/utils/TorServiceUtils.java @@ -16,6 +16,9 @@ public class TorServiceUtils { private static final Uri ORBOT_PLAYSTORE_URI = Uri.parse("market://details?id=" + URI_ORBOT); private final static String ACTION_START_TOR = "org.torproject.android.START_TOR"; + public final static String ACTION_STATUS = "org.torproject.android.intent.action.STATUS"; + public final static String EXTRA_STATUS = "org.torproject.android.intent.extra.STATUS"; + public static boolean isOrbotInstalled(Context context) { try { context.getPackageManager().getPackageInfo(URI_ORBOT, PackageManager.GET_ACTIVITIES);