Add method to Requirements to get not met requirements

PiperOrigin-RevId: 225549778
This commit is contained in:
eguven 2018-12-14 16:57:29 +00:00 committed by Oliver Woodman
parent 1a3d735bc3
commit 28dca0b339
3 changed files with 60 additions and 14 deletions

View File

@ -129,7 +129,7 @@ public final class JobDispatcherScheduler implements Scheduler {
Bundle extras = new Bundle();
extras.putString(KEY_SERVICE_ACTION, serviceAction);
extras.putString(KEY_SERVICE_PACKAGE, servicePackage);
extras.putInt(KEY_REQUIREMENTS, requirements.getRequirementsData());
extras.putInt(KEY_REQUIREMENTS, requirements.getRequirements());
builder.setExtras(extras);
return builder.build();

View File

@ -130,7 +130,7 @@ public final class PlatformScheduler implements Scheduler {
PersistableBundle extras = new PersistableBundle();
extras.putString(KEY_SERVICE_ACTION, serviceAction);
extras.putString(KEY_SERVICE_PACKAGE, servicePackage);
extras.putInt(KEY_REQUIREMENTS, requirements.getRequirementsData());
extras.putInt(KEY_REQUIREMENTS, requirements.getRequirements());
builder.setExtras(extras);
return builder.build();

View File

@ -25,6 +25,7 @@ import android.net.NetworkInfo;
import android.os.BatteryManager;
import android.os.PowerManager;
import android.support.annotation.IntDef;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Log;
import com.google.android.exoplayer2.util.Util;
import java.lang.annotation.Documented;
@ -50,22 +51,49 @@ public final class Requirements {
NETWORK_TYPE_METERED,
})
public @interface NetworkType {}
/**
* Requirement flags.
*
* <p>Combination of the following values is possible:
*
* <ul>
* <li>Only one of {@link #NETWORK_TYPE_ANY}, {@link #NETWORK_TYPE_UNMETERED}, {@link
* #NETWORK_TYPE_NOT_ROAMING} or {@link #NETWORK_TYPE_METERED}.
* <li>{@link #DEVICE_IDLE}
* <li>{@link #DEVICE_CHARGING}
* <ul>
*/
@Documented
@Retention(RetentionPolicy.SOURCE)
@IntDef(
flag = true,
value = {
NETWORK_TYPE_ANY,
NETWORK_TYPE_UNMETERED,
NETWORK_TYPE_NOT_ROAMING,
NETWORK_TYPE_METERED,
DEVICE_IDLE,
DEVICE_CHARGING
})
public @interface RequirementFlags {}
/** This job doesn't require network connectivity. */
public static final int NETWORK_TYPE_NONE = 0;
/** This job requires network connectivity. */
public static final int NETWORK_TYPE_ANY = 1;
/** This job requires network connectivity that is unmetered. */
public static final int NETWORK_TYPE_UNMETERED = 2;
public static final int NETWORK_TYPE_UNMETERED = 1 << 1;
/** This job requires network connectivity that is not roaming. */
public static final int NETWORK_TYPE_NOT_ROAMING = 3;
public static final int NETWORK_TYPE_NOT_ROAMING = 1 << 2;
/** This job requires metered connectivity such as most cellular data networks. */
public static final int NETWORK_TYPE_METERED = 4;
public static final int NETWORK_TYPE_METERED = 1 << 3;
/** This job requires the device to be idle. */
private static final int DEVICE_IDLE = 8;
public static final int DEVICE_IDLE = 1 << 4;
/** This job requires the device to be charging. */
private static final int DEVICE_CHARGING = 16;
public static final int DEVICE_CHARGING = 1 << 5;
private static final int NETWORK_TYPE_MASK = 7;
private static final int NETWORK_TYPE_MASK = 0b1111;
private static final String TAG = "Requirements";
@ -86,7 +114,7 @@ public final class Requirements {
}
}
private final int requirements;
@RequirementFlags private final int requirements;
/**
* @param networkType Required network type.
@ -97,9 +125,12 @@ public final class Requirements {
this(networkType | (charging ? DEVICE_CHARGING : 0) | (idle ? DEVICE_IDLE : 0));
}
/** @param requirementsData The value returned by {@link #getRequirementsData()}. */
public Requirements(int requirementsData) {
this.requirements = requirementsData;
/** @param requirements A combination of requirement flags. */
public Requirements(@RequirementFlags int requirements) {
this.requirements = requirements;
int networkType = getRequiredNetworkType();
// Check if only one network type is specified.
Assertions.checkState((networkType & (networkType - 1)) == 0);
}
/** Returns required network type. */
@ -121,6 +152,7 @@ public final class Requirements {
* Returns whether the requirements are met.
*
* @param context Any context.
* @return Whether the requirements are met.
*/
public boolean checkRequirements(Context context) {
return checkNetworkRequirements(context)
@ -128,8 +160,22 @@ public final class Requirements {
&& checkIdleRequirement(context);
}
/** Returns the encoded requirements data which can be used with {@link #Requirements(int)}. */
public int getRequirementsData() {
/**
* Returns the requirement flags that are not met, or 0.
*
* @param context Any context.
* @return The requirement flags that are not met, or 0.
*/
@RequirementFlags
public int getNotMetRequirements(Context context) {
return (!checkNetworkRequirements(context) ? getRequiredNetworkType() : 0)
| (!checkChargingRequirement(context) ? DEVICE_CHARGING : 0)
| (!checkIdleRequirement(context) ? DEVICE_IDLE : 0);
}
/** Returns the requirement flags. */
@RequirementFlags
public int getRequirements() {
return requirements;
}