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

(cherry picked from commit 70c10fd0de14fd2dc042d60bdc98ad10c3108d1c)
This commit is contained in:
Daniel Gultsch 2020-08-19 13:57:33 +02:00 committed by Christian Schneppe
parent 3af2e65946
commit a8f0bfb0a8
4 changed files with 33 additions and 26 deletions

View file

@ -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());

View file

@ -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);
}

View file

@ -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) {

View file

@ -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);