Home | History | Annotate | Download | only in controllers
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package androidx.work.impl.constraints.controllers;
     17 
     18 import android.support.annotation.NonNull;
     19 import android.support.annotation.Nullable;
     20 
     21 import androidx.work.impl.constraints.ConstraintListener;
     22 import androidx.work.impl.constraints.trackers.ConstraintTracker;
     23 import androidx.work.impl.model.WorkSpec;
     24 
     25 import java.util.ArrayList;
     26 import java.util.List;
     27 
     28 /**
     29  * A controller for a particular constraint.
     30  *
     31  * @param <T> the constraint data type managed by this controller.
     32  */
     33 
     34 public abstract class ConstraintController<T> implements ConstraintListener<T> {
     35 
     36     /**
     37      * A callback for when a constraint changes.
     38      */
     39     public interface OnConstraintUpdatedCallback {
     40 
     41         /**
     42          * Called when a constraint is met.
     43          *
     44          * @param workSpecIds A list of {@link WorkSpec} IDs that may have become eligible to run
     45          */
     46         void onConstraintMet(@NonNull List<String> workSpecIds);
     47 
     48         /**
     49          * Called when a constraint is not met.
     50          *
     51          * @param workSpecIds A list of {@link WorkSpec} IDs that have become ineligible to run
     52          */
     53         void onConstraintNotMet(@NonNull List<String> workSpecIds);
     54     }
     55 
     56     private final List<String> mMatchingWorkSpecIds = new ArrayList<>();
     57 
     58     private T mCurrentValue;
     59     private ConstraintTracker<T> mTracker;
     60     private OnConstraintUpdatedCallback mCallback;
     61 
     62     ConstraintController(ConstraintTracker<T> tracker, OnConstraintUpdatedCallback callback) {
     63         mTracker = tracker;
     64         mCallback = callback;
     65     }
     66 
     67     abstract boolean hasConstraint(@NonNull WorkSpec workSpec);
     68 
     69     abstract boolean isConstrained(@NonNull T currentValue);
     70 
     71     /**
     72      * Replaces the list of {@link WorkSpec}s to monitor constraints for.
     73      *
     74      * @param workSpecs A list of {@link WorkSpec}s to monitor constraints for
     75      */
     76     public void replace(@NonNull List<WorkSpec> workSpecs) {
     77         mMatchingWorkSpecIds.clear();
     78 
     79         for (WorkSpec workSpec : workSpecs) {
     80             if (hasConstraint(workSpec)) {
     81                 mMatchingWorkSpecIds.add(workSpec.id);
     82             }
     83         }
     84 
     85         if (mMatchingWorkSpecIds.isEmpty()) {
     86             mTracker.removeListener(this);
     87         } else {
     88             mTracker.addListener(this);
     89         }
     90         updateCallback();
     91     }
     92 
     93     /**
     94      * Clears all tracked {@link WorkSpec}s.
     95      */
     96     public void reset() {
     97         if (!mMatchingWorkSpecIds.isEmpty()) {
     98             mMatchingWorkSpecIds.clear();
     99             mTracker.removeListener(this);
    100         }
    101     }
    102 
    103     /**
    104      * Determines if a particular {@link WorkSpec} is constrained. It is constrained if it is
    105      * tracked by this controller, and the controller constraint was set, but not satisfied.
    106      *
    107      * @param workSpecId The ID of the {@link WorkSpec} to check if it is constrained.
    108      * @return {@code true} if the {@link WorkSpec} is considered constrained
    109      */
    110     public boolean isWorkSpecConstrained(@NonNull String workSpecId) {
    111         return mCurrentValue != null && isConstrained(mCurrentValue)
    112                 && mMatchingWorkSpecIds.contains(workSpecId);
    113     }
    114 
    115     private void updateCallback() {
    116         if (mMatchingWorkSpecIds.isEmpty()) {
    117             return;
    118         }
    119 
    120         if (mCurrentValue == null || isConstrained(mCurrentValue)) {
    121             mCallback.onConstraintNotMet(mMatchingWorkSpecIds);
    122         } else {
    123             mCallback.onConstraintMet(mMatchingWorkSpecIds);
    124         }
    125     }
    126 
    127     @Override
    128     public void onConstraintChanged(@Nullable T newValue) {
    129         mCurrentValue = newValue;
    130         updateCallback();
    131     }
    132 }
    133