From 70c10fd0de14fd2dc042d60bdc98ad10c3108d1c 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 --- .../conversations/services/EventReceiver.java | 18 ++++++------- .../services/XmppConnectionService.java | 27 +++++++++++++++---- .../conversations/ui/EditAccountActivity.java | 13 --------- .../conversations/utils/TorServiceUtils.java | 3 +++ 4 files changed, 33 insertions(+), 28 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/services/EventReceiver.java b/src/main/java/eu/siacs/conversations/services/EventReceiver.java index 985effc3a..500eb25e5 100644 --- a/src/main/java/eu/siacs/conversations/services/EventReceiver.java +++ b/src/main/java/eu/siacs/conversations/services/EventReceiver.java @@ -8,6 +8,8 @@ import android.preference.PreferenceManager; import android.support.v4.content.ContextCompat; import android.util.Log; +import com.google.common.base.Strings; + import eu.siacs.conversations.Config; import eu.siacs.conversations.utils.Compatibility; @@ -19,17 +21,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 8bc43c99f..240c64ac3 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -127,6 +127,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; @@ -652,8 +653,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); @@ -787,6 +795,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 boolean processAccountState(Account account, boolean interactive, boolean isUiAction, boolean isAccountPushed, HashSet pingCandidates) { boolean pingNow = false; if (account.getStatus().isAttemptReconnect()) { @@ -1128,15 +1144,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(); } @@ -1192,7 +1209,7 @@ public class XmppConnectionService extends Service { public void onDestroy() { try { unregisterReceiver(this.mInternalEventReceiver); - } catch (IllegalArgumentException e) { + } catch (final IllegalArgumentException e) { //ignored } destroyed = false; @@ -2193,7 +2210,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 92d6f2093..dcccec104 100644 --- a/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java @@ -89,7 +89,6 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat private static final int REQUEST_CHANGE_STATUS = 0xee11; private static final int REQUEST_ORBOT = 0xff22; private final PendingItem mPendingPresenceTemplate = new PendingItem<>(); - private final AtomicBoolean mPendingReconnect = new AtomicBoolean(false); private AlertDialog mCaptchaDialog = null; private Jid jidToEdit; private boolean mInitMode = false; @@ -475,13 +474,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 @@ -781,11 +773,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); 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 3e333483b..c3f6fd3c6 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);