1 /* 2 * Copyright (C) 2018 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.batterytip.detectors; 18 19 import android.content.Context; 20 import android.content.Intent; 21 import android.content.IntentFilter; 22 import android.os.BatteryManager; 23 import android.os.PowerManager; 24 25 import com.android.settings.fuelgauge.PowerUsageFeatureProvider; 26 import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy; 27 import com.android.settings.fuelgauge.batterytip.tips.BatteryTip; 28 import com.android.settings.fuelgauge.batterytip.tips.EarlyWarningTip; 29 import com.android.settings.overlay.FeatureFactory; 30 31 /** 32 * Detector whether to early warning tip. 33 */ 34 public class EarlyWarningDetector implements BatteryTipDetector { 35 private BatteryTipPolicy mPolicy; 36 private PowerManager mPowerManager; 37 private Context mContext; 38 private PowerUsageFeatureProvider mPowerUsageFeatureProvider; 39 40 public EarlyWarningDetector(BatteryTipPolicy policy, Context context) { 41 mPolicy = policy; 42 mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 43 mContext = context; 44 mPowerUsageFeatureProvider = FeatureFactory.getFactory( 45 context).getPowerUsageFeatureProvider(context); 46 } 47 48 @Override 49 public BatteryTip detect() { 50 final Intent batteryBroadcast = mContext.registerReceiver(null, 51 new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); 52 final boolean discharging = 53 batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) == 0; 54 final boolean powerSaveModeOn = mPowerManager.isPowerSaveMode(); 55 final boolean earlyWarning = mPowerUsageFeatureProvider.getEarlyWarningSignal(mContext, 56 EarlyWarningDetector.class.getName()) || mPolicy.testBatterySaverTip; 57 58 final int state = powerSaveModeOn 59 ? BatteryTip.StateType.HANDLED 60 : mPolicy.batterySaverTipEnabled && discharging && earlyWarning 61 ? BatteryTip.StateType.NEW 62 : BatteryTip.StateType.INVISIBLE; 63 return new EarlyWarningTip(state, powerSaveModeOn); 64 } 65 } 66