code clean up in onStartCommand

This commit is contained in:
Daniel Gultsch 2023-10-18 21:37:00 +02:00
parent 69425e677c
commit d1f648f2e3
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2

View file

@ -186,7 +186,9 @@ public class XmppConnectionService extends Service {
public static final String ACTION_TRY_AGAIN = "try_again"; public static final String ACTION_TRY_AGAIN = "try_again";
public static final String ACTION_TEMPORARILY_DISABLE = "temporarily_disable"; public static final String ACTION_TEMPORARILY_DISABLE = "temporarily_disable";
public static final String ACTION_PING = "ping";
public static final String ACTION_IDLE_PING = "idle_ping"; public static final String ACTION_IDLE_PING = "idle_ping";
public static final String ACTION_INTERNAL_PING = "internal_ping";
public static final String ACTION_FCM_TOKEN_REFRESH = "fcm_token_refresh"; public static final String ACTION_FCM_TOKEN_REFRESH = "fcm_token_refresh";
public static final String ACTION_FCM_MESSAGE_RECEIVED = "fcm_message_received"; public static final String ACTION_FCM_MESSAGE_RECEIVED = "fcm_message_received";
public static final String ACTION_DISMISS_CALL = "dismiss_call"; public static final String ACTION_DISMISS_CALL = "dismiss_call";
@ -653,17 +655,14 @@ public class XmppConnectionService extends Service {
} }
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(final Intent intent, int flags, int startId) {
final String action = intent == null ? null : intent.getAction(); final String action = Strings.nullToEmpty(intent == null ? null : intent.getAction());
final boolean needsForegroundService = intent != null && intent.getBooleanExtra(EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE, false); final boolean needsForegroundService = intent != null && intent.getBooleanExtra(EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE, false);
if (needsForegroundService) { if (needsForegroundService) {
Log.d(Config.LOGTAG, "toggle forced foreground service after receiving event (action=" + action + ")"); Log.d(Config.LOGTAG, "toggle forced foreground service after receiving event (action=" + action + ")");
toggleForegroundService(true); toggleForegroundService(true);
} }
String pushedAccountHash = null; final String uuid = intent == null ? null : intent.getStringExtra("uuid");
boolean interactive = false;
if (action != null) {
final String uuid = intent.getStringExtra("uuid");
switch (action) { switch (action) {
case QuickConversationsService.SMS_RETRIEVED_ACTION: case QuickConversationsService.SMS_RETRIEVED_ACTION:
mQuickConversationsService.handleSmsReceived(intent); mQuickConversationsService.handleSmsReceived(intent);
@ -715,13 +714,16 @@ public class XmppConnectionService extends Service {
}); });
break; break;
case ACTION_DISMISS_CALL: { case ACTION_DISMISS_CALL: {
if (intent == null) {
break;
}
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; break;
} }
case TorServiceUtils.ACTION_STATUS: case TorServiceUtils.ACTION_STATUS:
final String status = intent.getStringExtra(TorServiceUtils.EXTRA_STATUS); final String status = intent == null ? null : intent.getStringExtra(TorServiceUtils.EXTRA_STATUS);
//TODO port and host are in 'extras' - but this may not be a reliable source? //TODO port and host are in 'extras' - but this may not be a reliable source?
if ("ON".equals(status)) { if ("ON".equals(status)) {
handleOrbotStartedEvent(); handleOrbotStartedEvent();
@ -729,12 +731,18 @@ public class XmppConnectionService extends Service {
} }
break; break;
case ACTION_END_CALL: { case ACTION_END_CALL: {
if (intent == null) {
break;
}
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 end call with session id " + sessionId); Log.d(Config.LOGTAG, "received intent to end call with session id " + sessionId);
mJingleConnectionManager.endRtpSession(sessionId); mJingleConnectionManager.endRtpSession(sessionId);
} }
break; break;
case ACTION_PROVISION_ACCOUNT: { case ACTION_PROVISION_ACCOUNT: {
if (intent == null) {
break;
}
final String address = intent.getStringExtra("address"); final String address = intent.getStringExtra("address");
final String password = intent.getStringExtra("password"); final String password = intent.getStringExtra("password");
if (QuickConversationsService.isQuicksy() || Strings.isNullOrEmpty(address) || Strings.isNullOrEmpty(password)) { if (QuickConversationsService.isQuicksy() || Strings.isNullOrEmpty(address) || Strings.isNullOrEmpty(password)) {
@ -748,10 +756,9 @@ public class XmppConnectionService extends Service {
break; break;
case ACTION_TRY_AGAIN: case ACTION_TRY_AGAIN:
resetAllAttemptCounts(false, true); resetAllAttemptCounts(false, true);
interactive = true;
break; break;
case ACTION_REPLY_TO_CONVERSATION: case ACTION_REPLY_TO_CONVERSATION:
Bundle remoteInput = RemoteInput.getResultsFromIntent(intent); final Bundle remoteInput = intent == null ? null : RemoteInput.getResultsFromIntent(intent);
if (remoteInput == null) { if (remoteInput == null) {
break; break;
} }
@ -818,6 +825,9 @@ public class XmppConnectionService extends Service {
refreshAllFcmTokens(); refreshAllFcmTokens();
break; break;
case ACTION_RENEW_UNIFIED_PUSH_ENDPOINTS: case ACTION_RENEW_UNIFIED_PUSH_ENDPOINTS:
if (intent == null) {
break;
}
final String instance = intent.getStringExtra("instance"); final String instance = intent.getStringExtra("instance");
final String application = intent.getStringExtra("application"); final String application = intent.getStringExtra("application");
final Messenger messenger = intent.getParcelableExtra("messenger"); final Messenger messenger = intent.getParcelableExtra("messenger");
@ -839,11 +849,10 @@ public class XmppConnectionService extends Service {
} }
break; break;
case ACTION_FCM_MESSAGE_RECEIVED: case ACTION_FCM_MESSAGE_RECEIVED:
pushedAccountHash = intent.getStringExtra("account"); Log.d(Config.LOGTAG, "push message arrived in service. account");
Log.d(Config.LOGTAG, "push message arrived in service. account=" + pushedAccountHash);
break; break;
case Intent.ACTION_SEND: case Intent.ACTION_SEND:
Uri uri = intent.getData(); final Uri uri = intent == null ? null : intent.getData();
if (uri != null) { if (uri != null) {
Log.d(Config.LOGTAG, "received uri permission for " + uri); Log.d(Config.LOGTAG, "received uri permission for " + uri);
} }
@ -852,13 +861,21 @@ public class XmppConnectionService extends Service {
toggleSoftDisabled(true); toggleSoftDisabled(true);
return START_NOT_STICKY; return START_NOT_STICKY;
} }
manageAccountConnectionStates(action, intent == null ? null : intent.getExtras());
if (SystemClock.elapsedRealtime() - mLastExpiryRun.get() >= Config.EXPIRY_INTERVAL) {
expireOldMessages();
} }
synchronized (this) { return START_STICKY;
}
private synchronized void manageAccountConnectionStates(final String action, final Bundle extras) {
final String pushedAccountHash = extras == null ? null : extras.getString("account");
final boolean interactive = Arrays.asList(ACTION_TRY_AGAIN).contains(action);
WakeLockHelper.acquire(wakeLock); WakeLockHelper.acquire(wakeLock);
boolean pingNow = ConnectivityManager.CONNECTIVITY_ACTION.equals(action) || (Config.POST_CONNECTIVITY_CHANGE_PING_INTERVAL > 0 && ACTION_POST_CONNECTIVITY_CHANGE.equals(action)); boolean pingNow = ConnectivityManager.CONNECTIVITY_ACTION.equals(action) || (Config.POST_CONNECTIVITY_CHANGE_PING_INTERVAL > 0 && ACTION_POST_CONNECTIVITY_CHANGE.equals(action));
final HashSet<Account> pingCandidates = new HashSet<>(); final HashSet<Account> pingCandidates = new HashSet<>();
final String androidId = PhoneHelper.getAndroidId(this); final String androidId = PhoneHelper.getAndroidId(this);
for (Account account : accounts) { for (final Account account : accounts) {
final boolean pushWasMeantForThisAccount = CryptoHelper.getAccountFingerprint(account, androidId).equals(pushedAccountHash); final boolean pushWasMeantForThisAccount = CryptoHelper.getAccountFingerprint(account, androidId).equals(pushedAccountHash);
pingNow |= processAccountState(account, pingNow |= processAccountState(account,
interactive, interactive,
@ -876,11 +893,6 @@ public class XmppConnectionService extends Service {
} }
WakeLockHelper.release(wakeLock); WakeLockHelper.release(wakeLock);
} }
if (SystemClock.elapsedRealtime() - mLastExpiryRun.get() >= Config.EXPIRY_INTERVAL) {
expireOldMessages();
}
return START_STICKY;
}
private void handleOrbotStartedEvent() { private void handleOrbotStartedEvent() {
for (final Account account : accounts) { for (final Account account : accounts) {
@ -1500,14 +1512,14 @@ public class XmppConnectionService extends Service {
} }
} }
public void scheduleWakeUpCall(int seconds, int requestCode) { public void scheduleWakeUpCall(final int seconds, final int requestCode) {
final long timeToWake = SystemClock.elapsedRealtime() + (seconds < 0 ? 1 : seconds + 1) * 1000L; final long timeToWake = SystemClock.elapsedRealtime() + (seconds < 0 ? 1 : seconds + 1) * 1000L;
final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
if (alarmManager == null) { if (alarmManager == null) {
return; return;
} }
final Intent intent = new Intent(this, EventReceiver.class); final Intent intent = new Intent(this, EventReceiver.class);
intent.setAction("ping"); intent.setAction(ACTION_PING);
try { try {
final PendingIntent pendingIntent; final PendingIntent pendingIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {