Home | History | Annotate | Download | only in trackers
      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.trackers;
     17 
     18 import android.content.Context;
     19 import android.support.annotation.NonNull;
     20 import android.support.annotation.RestrictTo;
     21 import android.support.annotation.VisibleForTesting;
     22 
     23 /**
     24  * A singleton class to hold an instance of each {@link ConstraintTracker}.
     25  * @hide
     26  */
     27 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
     28 public class Trackers {
     29 
     30     private static Trackers sInstance;
     31 
     32     /**
     33      * Gets the singleton instance of {@link Trackers}.
     34      *
     35      * @param context The initializing context (we only use the application context)
     36      * @return The singleton instance of {@link Trackers}.
     37      */
     38     public static synchronized Trackers getInstance(Context context) {
     39         if (sInstance == null) {
     40             sInstance = new Trackers(context);
     41         }
     42         return sInstance;
     43     }
     44 
     45     /**
     46      * Sets an instance of the {@link Trackers} for testing.
     47      */
     48     @VisibleForTesting
     49     public static synchronized void setInstance(@NonNull Trackers trackers) {
     50         sInstance = trackers;
     51     }
     52 
     53     private BatteryChargingTracker mBatteryChargingTracker;
     54     private BatteryNotLowTracker mBatteryNotLowTracker;
     55     private NetworkStateTracker mNetworkStateTracker;
     56     private StorageNotLowTracker mStorageNotLowTracker;
     57 
     58     private Trackers(Context context) {
     59         Context appContext = context.getApplicationContext();
     60         mBatteryChargingTracker = new BatteryChargingTracker(appContext);
     61         mBatteryNotLowTracker = new BatteryNotLowTracker(appContext);
     62         mNetworkStateTracker = new NetworkStateTracker(appContext);
     63         mStorageNotLowTracker = new StorageNotLowTracker(appContext);
     64     }
     65 
     66     /**
     67      * Gets the tracker used to track the battery charging status.
     68      *
     69      * @return The tracker used to track battery charging status
     70      */
     71     public BatteryChargingTracker getBatteryChargingTracker() {
     72         return mBatteryChargingTracker;
     73     }
     74 
     75     /**
     76      * Gets the tracker used to track if the battery is okay or low.
     77      *
     78      * @return The tracker used to track if the battery is okay or low
     79      */
     80     public BatteryNotLowTracker getBatteryNotLowTracker() {
     81         return mBatteryNotLowTracker;
     82     }
     83 
     84     /**
     85      * Gets the tracker used to track network state changes.
     86      *
     87      * @return The tracker used to track state of the network
     88      */
     89     public NetworkStateTracker getNetworkStateTracker() {
     90         return mNetworkStateTracker;
     91     }
     92 
     93     /**
     94      * Gets the tracker used to track if device storage is okay or low.
     95      *
     96      * @return The tracker used to track if device storage is okay or low.
     97      */
     98     public StorageNotLowTracker getStorageNotLowTracker() {
     99         return mStorageNotLowTracker;
    100     }
    101 }
    102