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:
Greyson Parrelli 2019-07-22 18:06:26 -04:00
parent a210ef3136
commit 965de16de1
3 changed files with 24 additions and 5 deletions

View File

@ -2,10 +2,14 @@ package org.thoughtcrime.securesms.jobmanager.impl;
import android.app.Application; import android.app.Application;
import android.app.job.JobInfo; import android.app.job.JobInfo;
import android.telephony.ServiceState;
import android.telephony.TelephonyManager;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import org.thoughtcrime.securesms.jobmanager.Constraint; import org.thoughtcrime.securesms.jobmanager.Constraint;
import org.thoughtcrime.securesms.sms.TelephonyServiceState; import org.thoughtcrime.securesms.sms.TelephonyServiceState;
import org.thoughtcrime.securesms.util.ServiceUtil;
public class CellServiceConstraint implements Constraint { public class CellServiceConstraint implements Constraint {
@ -24,8 +28,7 @@ public class CellServiceConstraint implements Constraint {
@Override @Override
public boolean isMet() { public boolean isMet() {
TelephonyServiceState telephonyServiceState = new TelephonyServiceState(); return CellServiceConstraintObserver.getInstance(application).hasService();
return telephonyServiceState.isConnected(application);
} }
@Override @Override

View File

@ -13,9 +13,19 @@ public class CellServiceConstraintObserver implements ConstraintObserver {
private static final String REASON = CellServiceConstraintObserver.class.getSimpleName(); private static final String REASON = CellServiceConstraintObserver.class.getSimpleName();
private Notifier notifier; 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); TelephonyManager telephonyManager = (TelephonyManager) application.getSystemService(Context.TELEPHONY_SERVICE);
ServiceStateListener serviceStateListener = new ServiceStateListener(); ServiceStateListener serviceStateListener = new ServiceStateListener();
@ -27,9 +37,15 @@ public class CellServiceConstraintObserver implements ConstraintObserver {
this.notifier = notifier; this.notifier = notifier;
} }
public boolean hasService() {
return lastKnownState != null && lastKnownState.getState() == ServiceState.STATE_IN_SERVICE;
}
private class ServiceStateListener extends PhoneStateListener { private class ServiceStateListener extends PhoneStateListener {
@Override @Override
public void onServiceStateChanged(ServiceState serviceState) { public void onServiceStateChanged(ServiceState serviceState) {
lastKnownState = serviceState;
if (serviceState.getState() == ServiceState.STATE_IN_SERVICE && notifier != null) { if (serviceState.getState() == ServiceState.STATE_IN_SERVICE && notifier != null) {
notifier.onConstraintMet(REASON); notifier.onConstraintMet(REASON);
} }

View File

@ -85,7 +85,7 @@ public final class JobManagerFactories {
} }
public static List<ConstraintObserver> getConstraintObservers(@NonNull Application application) { public static List<ConstraintObserver> getConstraintObservers(@NonNull Application application) {
return Arrays.asList(new CellServiceConstraintObserver(application), return Arrays.asList(CellServiceConstraintObserver.getInstance(application),
new NetworkConstraintObserver(application), new NetworkConstraintObserver(application),
new SqlCipherMigrationConstraintObserver()); new SqlCipherMigrationConstraintObserver());
} }