Merge "Add early warning tip and detector"

This commit is contained in:
Lei Yu
2018-01-17 01:28:08 +00:00
committed by Android (Google) Code Review
9 changed files with 423 additions and 2 deletions

View File

@@ -23,6 +23,7 @@ import com.android.internal.os.BatteryStatsHelper;
import com.android.settings.fuelgauge.BatteryInfo;
import com.android.settings.fuelgauge.BatteryUtils;
import com.android.settings.fuelgauge.batterytip.detectors.BatteryTipDetector;
import com.android.settings.fuelgauge.batterytip.detectors.EarlyWarningDetector;
import com.android.settings.fuelgauge.batterytip.detectors.HighUsageDetector;
import com.android.settings.fuelgauge.batterytip.detectors.LowBatteryDetector;
import com.android.settings.fuelgauge.batterytip.detectors.SmartBatteryDetector;
@@ -64,13 +65,15 @@ public class BatteryTipLoader extends AsyncLoader<List<BatteryTip>> {
final List<BatteryTip> tips = new ArrayList<>();
final BatteryTipPolicy policy = new BatteryTipPolicy(getContext());
final BatteryInfo batteryInfo = mBatteryUtils.getBatteryInfo(mBatteryStatsHelper, TAG);
final Context context = getContext();
mVisibleTips = 0;
addBatteryTipFromDetector(tips, new LowBatteryDetector(policy, batteryInfo));
addBatteryTipFromDetector(tips,
new HighUsageDetector(getContext(), policy, mBatteryStatsHelper));
new HighUsageDetector(context, policy, mBatteryStatsHelper));
addBatteryTipFromDetector(tips,
new SmartBatteryDetector(policy, getContext().getContentResolver()));
new SmartBatteryDetector(policy, context.getContentResolver()));
addBatteryTipFromDetector(tips, new EarlyWarningDetector(policy, context));
// Add summary detector at last since it need other detectors to update the mVisibleTips
addBatteryTipFromDetector(tips, new SummaryDetector(policy, mVisibleTips));

View File

@@ -19,6 +19,7 @@ package com.android.settings.fuelgauge.batterytip;
import android.app.Fragment;
import com.android.settings.SettingsActivity;
import com.android.settings.fuelgauge.batterytip.actions.BatterySaverAction;
import com.android.settings.fuelgauge.batterytip.actions.BatteryTipAction;
import com.android.settings.fuelgauge.batterytip.actions.SmartBatteryAction;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
@@ -40,6 +41,8 @@ public class BatteryTipUtils {
switch (batteryTip.getType()) {
case BatteryTip.TipType.SMART_BATTERY_MANAGER:
return new SmartBatteryAction(settingsActivity, fragment);
case BatteryTip.TipType.BATTERY_SAVER:
return new BatterySaverAction(settingsActivity.getApplicationContext());
default:
return null;
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.fuelgauge.batterytip.actions;
import android.content.Context;
import android.os.PowerManager;
public class BatterySaverAction extends BatteryTipAction {
private PowerManager mPowerManager;
public BatterySaverAction(Context context) {
super(context);
mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
}
/**
* Handle the action when user clicks positive button
*/
@Override
public void handlePositiveAction() {
mPowerManager.setPowerSaveMode(true);
}
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.fuelgauge.batterytip.detectors;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.PowerManager;
import com.android.settings.fuelgauge.PowerUsageFeatureProvider;
import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
import com.android.settings.fuelgauge.batterytip.tips.EarlyWarningTip;
import com.android.settings.overlay.FeatureFactory;
/**
* Detector whether to early warning tip.
*/
public class EarlyWarningDetector implements BatteryTipDetector {
private BatteryTipPolicy mPolicy;
private PowerManager mPowerManager;
private Context mContext;
private PowerUsageFeatureProvider mPowerUsageFeatureProvider;
public EarlyWarningDetector(BatteryTipPolicy policy, Context context) {
mPolicy = policy;
mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
mContext = context;
mPowerUsageFeatureProvider = FeatureFactory.getFactory(
context).getPowerUsageFeatureProvider(context);
}
@Override
public BatteryTip detect() {
final Intent batteryBroadcast = mContext.registerReceiver(null,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
final boolean discharging =
batteryBroadcast.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) == 0;
final boolean powerSaveModeOn = mPowerManager.isPowerSaveMode();
final boolean earlyWarning = mPowerUsageFeatureProvider.getEarlyWarningSignal(mContext,
EarlyWarningDetector.class.getName());
final int state =
mPolicy.batterySaverTipEnabled && !powerSaveModeOn && discharging && earlyWarning
? BatteryTip.StateType.NEW
: BatteryTip.StateType.INVISIBLE;
return new EarlyWarningTip(state, powerSaveModeOn);
}
}

View File

@@ -0,0 +1,93 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.fuelgauge.batterytip.tips;
import android.content.Context;
import android.os.Parcel;
import com.android.settings.R;
/**
* Tip to show early warning if battery couldn't make to usual charging time
*/
public class EarlyWarningTip extends BatteryTip {
private boolean mPowerSaveModeOn;
public EarlyWarningTip(@StateType int state, boolean powerSaveModeOn) {
super(TipType.BATTERY_SAVER, state, false /* showDialog */);
mPowerSaveModeOn = powerSaveModeOn;
}
public EarlyWarningTip(Parcel in) {
super(in);
mPowerSaveModeOn = in.readBoolean();
}
@Override
public CharSequence getTitle(Context context) {
return context.getString(
mState == StateType.HANDLED
? R.string.battery_tip_early_heads_up_done_title
: R.string.battery_tip_early_heads_up_title);
}
@Override
public CharSequence getSummary(Context context) {
return context.getString(
mState == StateType.HANDLED
? R.string.battery_tip_early_heads_up_done_summary
: R.string.battery_tip_early_heads_up_summary);
}
@Override
public int getIconId() {
return mState == StateType.HANDLED
? R.drawable.ic_perm_device_information_green_24dp
: R.drawable.ic_battery_alert_24dp;
}
@Override
public void updateState(BatteryTip tip) {
final EarlyWarningTip earlyHeadsUpTip = (EarlyWarningTip) tip;
if (mPowerSaveModeOn != earlyHeadsUpTip.mPowerSaveModeOn) {
mPowerSaveModeOn = earlyHeadsUpTip.mPowerSaveModeOn;
mState = earlyHeadsUpTip.mPowerSaveModeOn ? StateType.HANDLED : StateType.NEW;
} else if (mState != StateType.HANDLED) {
mState = earlyHeadsUpTip.getState();
}
}
@Override
public void writeToParcel(Parcel dest, int flags) {
super.writeToParcel(dest, flags);
dest.writeBoolean(mPowerSaveModeOn);
}
public boolean isPowerSaveModeOn() {
return mPowerSaveModeOn;
}
public static final Creator CREATOR = new Creator() {
public BatteryTip createFromParcel(Parcel in) {
return new EarlyWarningTip(in);
}
public BatteryTip[] newArray(int size) {
return new EarlyWarningTip[size];
}
};
}