Home | History | Annotate | Download | only in fuelgauge
      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.BatteryManager;
     24 import android.os.PowerManager;
     25 import android.util.Log;
     26 
     27 public class BatterySaverReceiver extends BroadcastReceiver {
     28     private static final String TAG = "BatterySaverReceiver";
     29     private static final boolean DEBUG = false;
     30     private boolean mRegistered;
     31     private Context mContext;
     32     private BatterySaverListener mBatterySaverListener;
     33 
     34     public BatterySaverReceiver(Context context) {
     35         mContext = context;
     36     }
     37 
     38     @Override
     39     public void onReceive(Context context, Intent intent) {
     40         if (DEBUG) Log.d(TAG, "Received " + intent.getAction());
     41         String action = intent.getAction();
     42         if (PowerManager.ACTION_POWER_SAVE_MODE_CHANGING.equals(action)) {
     43             if (mBatterySaverListener != null) {
     44                 mBatterySaverListener.onPowerSaveModeChanged();
     45             }
     46         } else if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
     47             // disable BSM switch if phone is plugged in
     48             if (mBatterySaverListener != null) {
     49                 final boolean pluggedIn = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0;
     50                 mBatterySaverListener.onBatteryChanged(pluggedIn);
     51             }
     52         }
     53     }
     54 
     55     public void setListening(boolean listening) {
     56         if (listening && !mRegistered) {
     57             final IntentFilter ifilter = new IntentFilter();
     58             ifilter.addAction(PowerManager.ACTION_POWER_SAVE_MODE_CHANGING);
     59             ifilter.addAction(Intent.ACTION_BATTERY_CHANGED);
     60             mContext.registerReceiver(this, ifilter);
     61             mRegistered = true;
     62         } else if (!listening && mRegistered) {
     63             mContext.unregisterReceiver(this);
     64             mRegistered = false;
     65         }
     66     }
     67 
     68     public void setBatterySaverListener(BatterySaverListener lsn) {
     69         mBatterySaverListener = lsn;
     70     }
     71 
     72     public interface BatterySaverListener {
     73         void onPowerSaveModeChanged();
     74         void onBatteryChanged(boolean pluggedIn);
     75     }
     76 }
     77