• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.android.settings.fuelgauge;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.os.PowerManager;
24 import android.support.annotation.IntDef;
25 import android.support.annotation.VisibleForTesting;
26 
27 import com.android.settings.Utils;
28 import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
29 
30 import java.lang.annotation.Retention;
31 import java.lang.annotation.RetentionPolicy;
32 
33 /**
34  * Use this broadcastReceiver to listen to the battery change, and it will invoke
35  * {@link OnBatteryChangedListener} if any of the following has been changed:
36  *
37  * 1. Battery level(e.g. 100%->99%)
38  * 2. Battery status(e.g. plugged->unplugged)
39  * 3. Battery saver(e.g. off->on)
40  */
41 public class BatteryBroadcastReceiver extends BroadcastReceiver {
42 
43     /**
44      * Callback when the following has been changed:
45      *
46      * Battery level(e.g. 100%->99%)
47      * Battery status(e.g. plugged->unplugged)
48      * Battery saver(e.g. off->on)
49      */
50     public interface OnBatteryChangedListener {
onBatteryChanged(@atteryUpdateType int type)51         void onBatteryChanged(@BatteryUpdateType int type);
52     }
53 
54     @Retention(RetentionPolicy.SOURCE)
55     @IntDef({BatteryUpdateType.MANUAL,
56             BatteryUpdateType.BATTERY_LEVEL,
57             BatteryUpdateType.BATTERY_SAVER,
58             BatteryUpdateType.BATTERY_STATUS})
59     public @interface BatteryUpdateType {
60         int MANUAL = 0;
61         int BATTERY_LEVEL = 1;
62         int BATTERY_SAVER = 2;
63         int BATTERY_STATUS = 3;
64     }
65 
66     @VisibleForTesting
67     String mBatteryLevel;
68     @VisibleForTesting
69     String mBatteryStatus;
70     private OnBatteryChangedListener mBatteryListener;
71     private Context mContext;
72 
BatteryBroadcastReceiver(Context context)73     public BatteryBroadcastReceiver(Context context) {
74         mContext = context;
75     }
76 
77     @Override
onReceive(Context context, Intent intent)78     public void onReceive(Context context, Intent intent) {
79         updateBatteryStatus(intent, false /* forceUpdate */);
80     }
81 
setBatteryChangedListener(OnBatteryChangedListener lsn)82     public void setBatteryChangedListener(OnBatteryChangedListener lsn) {
83         mBatteryListener = lsn;
84     }
85 
register()86     public void register() {
87         final IntentFilter intentFilter = new IntentFilter();
88         intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
89         intentFilter.addAction(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED);
90 
91         final Intent intent = mContext.registerReceiver(this, intentFilter);
92         updateBatteryStatus(intent, true /* forceUpdate */);
93     }
94 
unRegister()95     public void unRegister() {
96         mContext.unregisterReceiver(this);
97     }
98 
updateBatteryStatus(Intent intent, boolean forceUpdate)99     private void updateBatteryStatus(Intent intent, boolean forceUpdate) {
100         if (intent != null && mBatteryListener != null) {
101             if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
102                 final String batteryLevel = Utils.getBatteryPercentage(intent);
103                 final String batteryStatus = Utils.getBatteryStatus(
104                         mContext.getResources(), intent);
105                 if (forceUpdate) {
106                     mBatteryListener.onBatteryChanged(BatteryUpdateType.MANUAL);
107                 } else if(!batteryLevel.equals(mBatteryLevel)) {
108                     mBatteryListener.onBatteryChanged(BatteryUpdateType.BATTERY_LEVEL);
109                 } else if (!batteryStatus.equals(mBatteryStatus)) {
110                     mBatteryListener.onBatteryChanged(BatteryUpdateType.BATTERY_STATUS);
111                 }
112                 mBatteryLevel = batteryLevel;
113                 mBatteryStatus = batteryStatus;
114             } else if (PowerManager.ACTION_POWER_SAVE_MODE_CHANGED.equals(intent.getAction())) {
115                 mBatteryListener.onBatteryChanged(BatteryUpdateType.BATTERY_SAVER);
116             }
117         }
118     }
119 
120 }