/** Sets the Job's runtime constraints. See {@link #getConstraints()}. */ @NonNull public Builder setConstraints(@Nullable @JobConstraint int... constraints) { this.constraints = constraints; return this; }
/** Adds the provided constraint to the current list of runtime constraints. */ @NonNull public Builder addConstraint(@JobConstraint int constraint) { // Create a new, longer constraints array int[] newConstraints = new int[constraints == null ? 1 : constraints.length + 1]; if (constraints != null && constraints.length != 0) { // Copy all the old values over System.arraycopy(constraints, 0, newConstraints, 0, constraints.length); } // add the new value newConstraints[newConstraints.length - 1] = constraint; // update the pointer constraints = newConstraints; return this; }
/** * The runtime constraints applied to this Job. A Job is not run until the trigger is activated * and all the runtime constraints are satisfied. */ @JobConstraint @Nullable int[] getConstraints();
@NonNull @JobConstraint private final int[] constraints;
@NonNull @JobConstraint private int[] constraints;
/** * Returns true if the specified jobConstraints are satisfied. We only check whether network * constraints are satisfied. All other constraints are assumed to be satisfied. */ private boolean areNetworkConstraintsSatisfied(@JobConstraint int jobConstraints) { // Network constraints are always satisfied for jobs that don't need a network if (!wantsNetwork(jobConstraints)) { return true; } // Ensure basic network connectivity is available. ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (!isNetworkConnected(connectivityManager)) { return false; } // Note: Constraint.ON_ANY_NETWORK and Constraint.ON_UNMETERED_NETWORK are mutually exclusive. // Constraints satisfied if we don't need an unmetered network (as that implies any network is // OK) or current network is unmetered. return !wantsUnmeteredNetwork(jobConstraints) || isNetworkUnmetered(connectivityManager); }
/** Returns true if any of the given {@code jobConstraints} require a network. */ private static boolean wantsNetwork(@JobConstraint int jobConstraints) { return wantsAnyNetwork(jobConstraints) || wantsUnmeteredNetwork(jobConstraints); }
/** {@inheritDoc} */ @Override @NonNull @JobConstraint public int[] getConstraints() { return constraints == null ? new int[] {} : constraints; }
Builder setConstraints(@JobConstraint int... constraints) { this.constraints = constraints; return this; }
/** Returns true if any of the given {@code jobConstraints} require an unmetered network. */ private static boolean wantsUnmeteredNetwork(@JobConstraint int jobConstraints) { return (jobConstraints & Constraint.ON_UNMETERED_NETWORK) != 0; }
/** * Returns true if any of the given {@code jobConstraints} is set to {@code * Constraint.ON_ANY_NETWORK} */ private static boolean wantsAnyNetwork(@JobConstraint int jobConstraints) { return (jobConstraints & Constraint.ON_ANY_NETWORK) != 0; }