mirror of
https://github.com/oxen-io/session-android.git
synced 2025-06-09 14:18:34 +00:00
Fix possible hangup with CellServiceConstraint.
On phones with no SIM card, if you manage to enqueue a job with a CellServiceConstraint, the previous check we were using to check if there was cell service could hang indefinitely on some devices. This changes it to a fast check, which all constraints should be.
This commit is contained in:
parent
a210ef3136
commit
965de16de1
@ -2,10 +2,14 @@ package org.thoughtcrime.securesms.jobmanager.impl;
|
||||
|
||||
import android.app.Application;
|
||||
import android.app.job.JobInfo;
|
||||
import android.telephony.ServiceState;
|
||||
import android.telephony.TelephonyManager;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import org.thoughtcrime.securesms.jobmanager.Constraint;
|
||||
import org.thoughtcrime.securesms.sms.TelephonyServiceState;
|
||||
import org.thoughtcrime.securesms.util.ServiceUtil;
|
||||
|
||||
public class CellServiceConstraint implements Constraint {
|
||||
|
||||
@ -24,8 +28,7 @@ public class CellServiceConstraint implements Constraint {
|
||||
|
||||
@Override
|
||||
public boolean isMet() {
|
||||
TelephonyServiceState telephonyServiceState = new TelephonyServiceState();
|
||||
return telephonyServiceState.isConnected(application);
|
||||
return CellServiceConstraintObserver.getInstance(application).hasService();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -14,8 +14,18 @@ public class CellServiceConstraintObserver implements ConstraintObserver {
|
||||
private static final String REASON = CellServiceConstraintObserver.class.getSimpleName();
|
||||
|
||||
private Notifier notifier;
|
||||
private ServiceState lastKnownState;
|
||||
|
||||
public CellServiceConstraintObserver(@NonNull Application application) {
|
||||
private static CellServiceConstraintObserver instance;
|
||||
|
||||
public static synchronized CellServiceConstraintObserver getInstance(@NonNull Application application) {
|
||||
if (instance == null) {
|
||||
instance = new CellServiceConstraintObserver(application);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
private CellServiceConstraintObserver(@NonNull Application application) {
|
||||
TelephonyManager telephonyManager = (TelephonyManager) application.getSystemService(Context.TELEPHONY_SERVICE);
|
||||
ServiceStateListener serviceStateListener = new ServiceStateListener();
|
||||
|
||||
@ -27,9 +37,15 @@ public class CellServiceConstraintObserver implements ConstraintObserver {
|
||||
this.notifier = notifier;
|
||||
}
|
||||
|
||||
public boolean hasService() {
|
||||
return lastKnownState != null && lastKnownState.getState() == ServiceState.STATE_IN_SERVICE;
|
||||
}
|
||||
|
||||
private class ServiceStateListener extends PhoneStateListener {
|
||||
@Override
|
||||
public void onServiceStateChanged(ServiceState serviceState) {
|
||||
lastKnownState = serviceState;
|
||||
|
||||
if (serviceState.getState() == ServiceState.STATE_IN_SERVICE && notifier != null) {
|
||||
notifier.onConstraintMet(REASON);
|
||||
}
|
||||
|
@ -85,7 +85,7 @@ public final class JobManagerFactories {
|
||||
}
|
||||
|
||||
public static List<ConstraintObserver> getConstraintObservers(@NonNull Application application) {
|
||||
return Arrays.asList(new CellServiceConstraintObserver(application),
|
||||
return Arrays.asList(CellServiceConstraintObserver.getInstance(application),
|
||||
new NetworkConstraintObserver(application),
|
||||
new SqlCipherMigrationConstraintObserver());
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user