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 
     17 package androidx.work.impl.constraints.trackers;
     18 
     19 import android.content.BroadcastReceiver;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.content.IntentFilter;
     23 import android.support.annotation.NonNull;
     24 import android.support.annotation.RestrictTo;
     25 import android.util.Log;
     26 
     27 /**
     28  * A {@link ConstraintTracker} with a {@link BroadcastReceiver} for monitoring constraint changes.
     29  *
     30  * @param <T> the constraint data type observed by this tracker
     31  * @hide
     32  */
     33 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
     34 public abstract class BroadcastReceiverConstraintTracker<T> extends ConstraintTracker<T> {
     35     private static final String TAG = "BrdcstRcvrCnstrntTrckr";
     36 
     37     private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
     38         @Override
     39         public void onReceive(Context context, Intent intent) {
     40             if (intent != null) {
     41                 onBroadcastReceive(context, intent);
     42             }
     43         }
     44     };
     45 
     46     public BroadcastReceiverConstraintTracker(Context context) {
     47         super(context);
     48     }
     49 
     50     /**
     51      * Called when the {@link BroadcastReceiver} is receiving an {@link Intent} broadcast and should
     52      * handle the received {@link Intent}.
     53      *
     54      * @param context The {@link Context} in which the receiver is running.
     55      * @param intent  The {@link Intent} being received.
     56      */
     57     public abstract void onBroadcastReceive(Context context, @NonNull Intent intent);
     58 
     59     /**
     60      * @return The {@link IntentFilter} associated with this tracker.
     61      */
     62     public abstract IntentFilter getIntentFilter();
     63 
     64     @Override
     65     public void startTracking() {
     66         Log.d(TAG, String.format("%s: registering receiver", getClass().getSimpleName()));
     67         mAppContext.registerReceiver(mBroadcastReceiver, getIntentFilter());
     68     }
     69 
     70     @Override
     71     public void stopTracking() {
     72         Log.d(TAG, String.format("%s: unregistering receiver", getClass().getSimpleName()));
     73         mAppContext.unregisterReceiver(mBroadcastReceiver);
     74     }
     75 }
     76