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_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_INTERNAL_PING = "internal_ping";
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_DISMISS_CALL = "dismiss_call";
@ -653,17 +655,14 @@ public class XmppConnectionService extends Service {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
final String action = intent == null ? null : intent.getAction();
public int onStartCommand(final Intent intent, int flags, int startId) {
final String action = Strings.nullToEmpty(intent == null ? null : intent.getAction());
final boolean needsForegroundService = intent != null && intent.getBooleanExtra(EventReceiver.EXTRA_NEEDS_FOREGROUND_SERVICE, false);
if (needsForegroundService) {
Log.d(Config.LOGTAG, "toggle forced foreground service after receiving event (action=" + action + ")");
toggleForegroundService(true);
}
String pushedAccountHash = null;
boolean interactive = false;
if (action != null) {
final String uuid = intent.getStringExtra("uuid");
final String uuid = intent == null ? null : intent.getStringExtra("uuid");
switch (action) {
case QuickConversationsService.SMS_RETRIEVED_ACTION:
mQuickConversationsService.handleSmsReceived(intent);
@ -715,13 +714,16 @@ public class XmppConnectionService extends Service {
});
break;
case ACTION_DISMISS_CALL: {
if (intent == null) {
break;
}
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;
}
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?
if ("ON".equals(status)) {
handleOrbotStartedEvent();
@ -729,12 +731,18 @@ public class XmppConnectionService extends Service {
}
break;
case ACTION_END_CALL: {
if (intent == null) {
break;
}
final String sessionId = intent.getStringExtra(RtpSessionActivity.EXTRA_SESSION_ID);
Log.d(Config.LOGTAG, "received intent to end call with session id " + sessionId);
mJingleConnectionManager.endRtpSession(sessionId);
}
break;
case ACTION_PROVISION_ACCOUNT: {
if (intent == null) {
break;
}
final String address = intent.getStringExtra("address");
final String password = intent.getStringExtra("password");
if (QuickConversationsService.isQuicksy() || Strings.isNullOrEmpty(address) || Strings.isNullOrEmpty(password)) {
@ -748,10 +756,9 @@ public class XmppConnectionService extends Service {
break;
case ACTION_TRY_AGAIN:
resetAllAttemptCounts(false, true);
interactive = true;
break;
case ACTION_REPLY_TO_CONVERSATION:
Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
final Bundle remoteInput = intent == null ? null : RemoteInput.getResultsFromIntent(intent);
if (remoteInput == null) {
break;
}
@ -818,6 +825,9 @@ public class XmppConnectionService extends Service {
refreshAllFcmTokens();
break;
case ACTION_RENEW_UNIFIED_PUSH_ENDPOINTS:
if (intent == null) {
break;
}
final String instance = intent.getStringExtra("instance");
final String application = intent.getStringExtra("application");
final Messenger messenger = intent.getParcelableExtra("messenger");
@ -839,11 +849,10 @@ public class XmppConnectionService extends Service {
}
break;
case ACTION_FCM_MESSAGE_RECEIVED:
pushedAccountHash = intent.getStringExtra("account");
Log.d(Config.LOGTAG, "push message arrived in service. account=" + pushedAccountHash);
Log.d(Config.LOGTAG, "push message arrived in service. account");
break;
case Intent.ACTION_SEND:
Uri uri = intent.getData();
final Uri uri = intent == null ? null : intent.getData();
if (uri != null) {
Log.d(Config.LOGTAG, "received uri permission for " + uri);
}
@ -852,13 +861,21 @@ public class XmppConnectionService extends Service {
toggleSoftDisabled(true);
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);
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 String androidId = PhoneHelper.getAndroidId(this);
for (Account account : accounts) {
for (final Account account : accounts) {
final boolean pushWasMeantForThisAccount = CryptoHelper.getAccountFingerprint(account, androidId).equals(pushedAccountHash);
pingNow |= processAccountState(account,
interactive,
@ -876,11 +893,6 @@ public class XmppConnectionService extends Service {
}
WakeLockHelper.release(wakeLock);
}
if (SystemClock.elapsedRealtime() - mLastExpiryRun.get() >= Config.EXPIRY_INTERVAL) {
expireOldMessages();
}
return START_STICKY;
}
private void handleOrbotStartedEvent() {
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 AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
if (alarmManager == null) {
return;
}
final Intent intent = new Intent(this, EventReceiver.class);
intent.setAction("ping");
intent.setAction(ACTION_PING);
try {
final PendingIntent pendingIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {