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.content.Intent;
20 import android.content.IntentFilter;
21 import android.os.BatteryManager;
22 import android.os.Build;
23 import android.support.annotation.NonNull;
24 import android.support.annotation.RestrictTo;
25 import android.util.Log;
26 
27 /**
28  * Tracks whether or not the device's battery is charging.
29  * @hide
30  */
31 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
32 public class BatteryChargingTracker extends BroadcastReceiverConstraintTracker<Boolean> {
33 
34     private static final String TAG = "BatteryChrgTracker";
35 
36     /**
37      * Create an instance of {@link BatteryChargingTracker}.
38      * @param context The application {@link Context}
39      */
BatteryChargingTracker(Context context)40     public BatteryChargingTracker(Context context) {
41         super(context);
42     }
43 
44     @Override
getInitialState()45     public Boolean getInitialState() {
46         // {@link ACTION_CHARGING} and {@link ACTION_DISCHARGING} are not sticky broadcasts, so
47         // we use {@link ACTION_BATTERY_CHANGED} on all APIs to get the initial state.
48         IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
49         Intent intent = mAppContext.registerReceiver(null, intentFilter);
50         if (intent == null) {
51             Log.e(TAG, "getInitialState - null intent received");
52             return null;
53         }
54         return isBatteryChangedIntentCharging(intent);
55     }
56 
57     @Override
getIntentFilter()58     public IntentFilter getIntentFilter() {
59         IntentFilter intentFilter = new IntentFilter();
60         if (Build.VERSION.SDK_INT >= 23) {
61             intentFilter.addAction(BatteryManager.ACTION_CHARGING);
62             intentFilter.addAction(BatteryManager.ACTION_DISCHARGING);
63         } else {
64             intentFilter.addAction(Intent.ACTION_POWER_CONNECTED);
65             intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);
66         }
67         return intentFilter;
68     }
69 
70     @Override
onBroadcastReceive(Context context, @NonNull Intent intent)71     public void onBroadcastReceive(Context context, @NonNull Intent intent) {
72         String action = intent.getAction();
73         if (action == null) {
74             return;
75         }
76 
77         Log.d(TAG, String.format("Received %s", action));
78         switch (action) {
79             case BatteryManager.ACTION_CHARGING:
80                 setState(true);
81                 break;
82 
83             case BatteryManager.ACTION_DISCHARGING:
84                 setState(false);
85                 break;
86 
87             case Intent.ACTION_POWER_CONNECTED:
88                 setState(true);
89                 break;
90 
91             case Intent.ACTION_POWER_DISCONNECTED:
92                 setState(false);
93                 break;
94         }
95     }
96 
isBatteryChangedIntentCharging(Intent intent)97     private boolean isBatteryChangedIntentCharging(Intent intent) {
98         boolean charging;
99         if (Build.VERSION.SDK_INT >= 23) {
100             int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
101             charging = (status == BatteryManager.BATTERY_STATUS_CHARGING
102                     || status == BatteryManager.BATTERY_STATUS_FULL);
103         } else {
104             int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
105             charging = (chargePlug != 0);
106         }
107         return charging;
108     }
109 }
110