listen to orbot events instead of using intent result to reconnect account

This commit is contained in:
Daniel Gultsch 2020-08-19 13:57:33 +02:00
parent 1958cded23
commit 70c10fd0de
4 changed files with 33 additions and 28 deletions

View File

@ -8,6 +8,8 @@ import android.preference.PreferenceManager;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.util.Log; import android.util.Log;
import com.google.common.base.Strings;
import eu.siacs.conversations.Config; import eu.siacs.conversations.Config;
import eu.siacs.conversations.utils.Compatibility; import eu.siacs.conversations.utils.Compatibility;
@ -19,17 +21,13 @@ public class EventReceiver extends BroadcastReceiver {
@Override @Override
public void onReceive(final Context context, final Intent originalIntent) { public void onReceive(final Context context, final Intent originalIntent) {
final Intent intentForService = new Intent(context, XmppConnectionService.class); final Intent intentForService = new Intent(context, XmppConnectionService.class);
if (originalIntent.getAction() != null) { final String action = originalIntent.getAction();
intentForService.setAction(originalIntent.getAction()); intentForService.setAction(Strings.isNullOrEmpty(action) ? "other" : action);
final Bundle extras = originalIntent.getExtras(); final Bundle extras = originalIntent.getExtras();
if (extras != null) { if (extras != null) {
intentForService.putExtras(extras); intentForService.putExtras(extras);
} }
} else { if ("ui".equals(action) || hasEnabledAccounts(context)) {
intentForService.setAction("other");
}
final String action = originalIntent.getAction();
if (action.equals("ui") || hasEnabledAccounts(context)) {
Compatibility.startService(context, intentForService); Compatibility.startService(context, intentForService);
} else { } else {
Log.d(Config.LOGTAG, "EventReceiver ignored action " + intentForService.getAction()); Log.d(Config.LOGTAG, "EventReceiver ignored action " + intentForService.getAction());

View File

@ -127,6 +127,7 @@ import eu.siacs.conversations.utils.ReplacingTaskManager;
import eu.siacs.conversations.utils.Resolver; import eu.siacs.conversations.utils.Resolver;
import eu.siacs.conversations.utils.SerialSingleThreadExecutor; import eu.siacs.conversations.utils.SerialSingleThreadExecutor;
import eu.siacs.conversations.utils.StringUtils; import eu.siacs.conversations.utils.StringUtils;
import eu.siacs.conversations.utils.TorServiceUtils;
import eu.siacs.conversations.utils.WakeLockHelper; import eu.siacs.conversations.utils.WakeLockHelper;
import eu.siacs.conversations.utils.XmppUri; import eu.siacs.conversations.utils.XmppUri;
import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xml.Element;
@ -652,6 +653,13 @@ public class XmppConnectionService extends Service {
final String sessionId = intent.getStringExtra(RtpSessionActivity.EXTRA_SESSION_ID); final String sessionId = intent.getStringExtra(RtpSessionActivity.EXTRA_SESSION_ID);
Log.d(Config.LOGTAG, "received intent to dismiss call with session id " + sessionId); Log.d(Config.LOGTAG, "received intent to dismiss call with session id " + sessionId);
mJingleConnectionManager.rejectRtpSession(sessionId); mJingleConnectionManager.rejectRtpSession(sessionId);
break;
}
case TorServiceUtils.ACTION_STATUS:
final String status = intent.getStringExtra(TorServiceUtils.EXTRA_STATUS);
if ("ON".equals(status)) {
handleOrbotStartedEvent();
return START_STICKY;
} }
break; break;
case ACTION_END_CALL: { case ACTION_END_CALL: {
@ -787,6 +795,14 @@ public class XmppConnectionService extends Service {
return START_STICKY; 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<Account> pingCandidates) { private boolean processAccountState(Account account, boolean interactive, boolean isUiAction, boolean isAccountPushed, HashSet<Account> pingCandidates) {
boolean pingNow = false; boolean pingNow = false;
if (account.getStatus().isAttemptReconnect()) { if (account.getStatus().isAttemptReconnect()) {
@ -1128,15 +1144,16 @@ public class XmppConnectionService extends Service {
toggleForegroundService(); toggleForegroundService();
updateUnreadCountBadge(); updateUnreadCountBadge();
toggleScreenEventReceiver(); toggleScreenEventReceiver();
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(TorServiceUtils.ACTION_STATUS);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
scheduleNextIdlePing(); scheduleNextIdlePing();
IntentFilter intentFilter = new IntentFilter();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
} }
intentFilter.addAction(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED); intentFilter.addAction(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED);
registerReceiver(this.mInternalEventReceiver, intentFilter);
} }
registerReceiver(this.mInternalEventReceiver, intentFilter);
mForceDuringOnCreate.set(false); mForceDuringOnCreate.set(false);
toggleForegroundService(); toggleForegroundService();
} }
@ -1192,7 +1209,7 @@ public class XmppConnectionService extends Service {
public void onDestroy() { public void onDestroy() {
try { try {
unregisterReceiver(this.mInternalEventReceiver); unregisterReceiver(this.mInternalEventReceiver);
} catch (IllegalArgumentException e) { } catch (final IllegalArgumentException e) {
//ignored //ignored
} }
destroyed = false; destroyed = false;
@ -2193,7 +2210,7 @@ public class XmppConnectionService extends Service {
final Jid jid = Jid.ofEscaped(address); final Jid jid = Jid.ofEscaped(address);
final Account account = new Account(jid, password); final Account account = new Account(jid, password);
account.setOption(Account.OPTION_DISABLED, true); 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); createAccount(account);
} }

View File

@ -89,7 +89,6 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
private static final int REQUEST_CHANGE_STATUS = 0xee11; private static final int REQUEST_CHANGE_STATUS = 0xee11;
private static final int REQUEST_ORBOT = 0xff22; private static final int REQUEST_ORBOT = 0xff22;
private final PendingItem<PresenceTemplate> mPendingPresenceTemplate = new PendingItem<>(); private final PendingItem<PresenceTemplate> mPendingPresenceTemplate = new PendingItem<>();
private final AtomicBoolean mPendingReconnect = new AtomicBoolean(false);
private AlertDialog mCaptchaDialog = null; private AlertDialog mCaptchaDialog = null;
private Jid jidToEdit; private Jid jidToEdit;
private boolean mInitMode = false; private boolean mInitMode = false;
@ -475,13 +474,6 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
Log.d(Config.LOGTAG, "pgp result not ok"); 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 @Override
@ -781,11 +773,6 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
} }
if (mAccount != null) { if (mAccount != null) {
if (mPendingReconnect.compareAndSet(true, false)) {
xmppConnectionService.reconnectAccountInBackground(mAccount);
}
this.mInitMode |= this.mAccount.isOptionSet(Account.OPTION_REGISTER); this.mInitMode |= this.mAccount.isOptionSet(Account.OPTION_REGISTER);
this.mUsernameMode |= mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE) && mAccount.isOptionSet(Account.OPTION_REGISTER); this.mUsernameMode |= mAccount.isOptionSet(Account.OPTION_MAGIC_CREATE) && mAccount.isOptionSet(Account.OPTION_REGISTER);
if (mPendingFingerprintVerificationUri != null) { if (mPendingFingerprintVerificationUri != null) {

View File

@ -16,6 +16,9 @@ public class TorServiceUtils {
private static final Uri ORBOT_PLAYSTORE_URI = Uri.parse("market://details?id=" + URI_ORBOT); 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"; 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) { public static boolean isOrbotInstalled(Context context) {
try { try {
context.getPackageManager().getPackageInfo(URI_ORBOT, PackageManager.GET_ACTIVITIES); context.getPackageManager().getPackageInfo(URI_ORBOT, PackageManager.GET_ACTIVITIES);