Add smart battery tip.

Also move the action method from BatteryTip to TipAction. Since it
need extra data that we don't want to store it in parcel.

Bug: 71502850
Test: RunSettingsRoboTests
Change-Id: Ib658426725158d26fcdd437fa8bf6bf24e9a8c14
This commit is contained in:
jackqdyulei
2018-01-10 09:51:52 -08:00
parent 92aa8583c6
commit e682126a11
17 changed files with 352 additions and 33 deletions

View File

@@ -266,7 +266,7 @@ public class PowerUsageSummary extends PowerUsageBase implements OnLongClickList
KEY_APP_LIST, lifecycle, activity, this);
controllers.add(mBatteryAppListPreferenceController);
mBatteryTipPreferenceController = new BatteryTipPreferenceController(context,
KEY_BATTERY_TIP, this, this);
KEY_BATTERY_TIP, (SettingsActivity) getActivity(), this, this);
controllers.add(mBatteryTipPreferenceController);
controllers.add(new BatterySaverController(context, getLifecycle()));
controllers.add(new BatteryPercentagePreferenceController(context));

View File

@@ -27,8 +27,10 @@ import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
import com.android.settings.fuelgauge.batterytip.BatteryTipPreferenceController.BatteryTipListener;
import com.android.settings.fuelgauge.batterytip.actions.BatteryTipAction;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
import com.android.settings.fuelgauge.batterytip.tips.HighUsageTip;
@@ -97,7 +99,11 @@ public class BatteryTipDialogFragment extends InstrumentedDialogFragment impleme
if (lsn == null) {
return;
}
mBatteryTip.action();
final BatteryTipAction action = BatteryTipUtils.getActionForBatteryTip(mBatteryTip,
(SettingsActivity) getActivity(), this);
if (action != null) {
action.handlePositiveAction();
}
lsn.onBatteryTipHandled(mBatteryTip);
}

View File

@@ -25,6 +25,7 @@ import com.android.settings.fuelgauge.BatteryUtils;
import com.android.settings.fuelgauge.batterytip.detectors.BatteryTipDetector;
import com.android.settings.fuelgauge.batterytip.detectors.HighUsageDetector;
import com.android.settings.fuelgauge.batterytip.detectors.LowBatteryDetector;
import com.android.settings.fuelgauge.batterytip.detectors.SmartBatteryDetector;
import com.android.settings.fuelgauge.batterytip.detectors.SummaryDetector;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
import com.android.settings.fuelgauge.batterytip.tips.LowBatteryTip;
@@ -68,6 +69,8 @@ public class BatteryTipLoader extends AsyncLoader<List<BatteryTip>> {
addBatteryTipFromDetector(tips, new LowBatteryDetector(policy, batteryInfo));
addBatteryTipFromDetector(tips,
new HighUsageDetector(getContext(), policy, mBatteryStatsHelper));
addBatteryTipFromDetector(tips,
new SmartBatteryDetector(policy, getContext().getContentResolver()));
// Add summary detector at last since it need other detectors to update the mVisibleTips
addBatteryTipFromDetector(tips, new SummaryDetector(policy, mVisibleTips));

View File

@@ -23,7 +23,9 @@ import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceGroup;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.SettingsActivity;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.fuelgauge.batterytip.actions.BatteryTipAction;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
import com.android.settings.fuelgauge.batterytip.tips.SummaryTip;
@@ -41,6 +43,7 @@ public class BatteryTipPreferenceController extends BasePreferenceController {
private BatteryTipListener mBatteryTipListener;
private List<BatteryTip> mBatteryTips;
private Map<String, BatteryTip> mBatteryTipMap;
private SettingsActivity mSettingsActivity;
@VisibleForTesting
PreferenceGroup mPreferenceGroup;
@VisibleForTesting
@@ -48,15 +51,17 @@ public class BatteryTipPreferenceController extends BasePreferenceController {
PreferenceFragment mFragment;
public BatteryTipPreferenceController(Context context, String preferenceKey) {
this(context, preferenceKey, null, null);
this(context, preferenceKey, null, null, null);
}
public BatteryTipPreferenceController(Context context, String preferenceKey,
PreferenceFragment fragment, BatteryTipListener batteryTipListener) {
SettingsActivity settingsActivity, PreferenceFragment fragment,
BatteryTipListener batteryTipListener) {
super(context, preferenceKey);
mBatteryTipListener = batteryTipListener;
mBatteryTipMap = new HashMap<>();
mFragment = fragment;
mSettingsActivity = settingsActivity;
}
@Override
@@ -107,7 +112,11 @@ public class BatteryTipPreferenceController extends BasePreferenceController {
dialogFragment.setTargetFragment(mFragment, REQUEST_ANOMALY_ACTION);
dialogFragment.show(mFragment.getFragmentManager(), TAG);
} else {
batteryTip.action();
final BatteryTipAction action = BatteryTipUtils.getActionForBatteryTip(batteryTip,
mSettingsActivity, mFragment);
if (action != null) {
action.handlePositiveAction();
}
if (mBatteryTipListener != null) {
mBatteryTipListener.onBatteryTipHandled(batteryTip);
}

View File

@@ -0,0 +1,47 @@
/*
* 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;
import android.app.Fragment;
import com.android.settings.SettingsActivity;
import com.android.settings.fuelgauge.batterytip.actions.BatteryTipAction;
import com.android.settings.fuelgauge.batterytip.actions.SmartBatteryAction;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
/**
* Utility class for {@link BatteryTip}
*/
public class BatteryTipUtils {
/**
* Get a corresponding action based on {@code batteryTip}
* @param batteryTip used to detect which action to choose
* @param settingsActivity used to populate {@link BatteryTipAction}
* @param fragment used to populate {@link BatteryTipAction}
* @return an action for {@code batteryTip}
*/
public static BatteryTipAction getActionForBatteryTip(BatteryTip batteryTip,
SettingsActivity settingsActivity, Fragment fragment) {
switch (batteryTip.getType()) {
case BatteryTip.TipType.SMART_BATTERY_MANAGER:
return new SmartBatteryAction(settingsActivity, fragment);
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 com.android.settings.core.instrumentation.MetricsFeatureProvider;
/**
* Abstract class for battery tip action, which is triggered if we need to handle the battery tip
*/
public abstract class BatteryTipAction {
protected Context mContext;
public BatteryTipAction(Context context) {
mContext = context;
}
/**
* Handle the action when user clicks positive button
*/
public abstract void handlePositiveAction();
}

View File

@@ -0,0 +1,48 @@
/*
* 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.app.Fragment;
import android.os.UserHandle;
import android.support.v14.preference.PreferenceFragment;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import com.android.settings.core.instrumentation.MetricsFeatureProvider;
import com.android.settings.fuelgauge.SmartBatterySettings;
public class SmartBatteryAction extends BatteryTipAction {
private SettingsActivity mSettingsActivity;
private Fragment mFragment;
public SmartBatteryAction(SettingsActivity settingsActivity, Fragment fragment) {
super(settingsActivity.getApplicationContext());
mSettingsActivity = settingsActivity;
mFragment = fragment;
}
/**
* Handle the action when user clicks positive button
*/
@Override
public void handlePositiveAction() {
mSettingsActivity.startPreferencePanelAsUser(mFragment,
SmartBatterySettings.class.getName(), null /* args */,
R.string.smart_battery_manager_title, null /* titleText */,
new UserHandle(UserHandle.myUserId()));
}
}

View File

@@ -0,0 +1,47 @@
/*
* 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.ContentResolver;
import android.provider.Settings;
import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
import com.android.settings.fuelgauge.batterytip.tips.SmartBatteryTip;
/**
* Detect whether to show smart battery tip.
*/
public class SmartBatteryDetector implements BatteryTipDetector {
private BatteryTipPolicy mPolicy;
private ContentResolver mContentResolver;
public SmartBatteryDetector(BatteryTipPolicy policy, ContentResolver contentResolver) {
mPolicy = policy;
mContentResolver = contentResolver;
}
@Override
public BatteryTip detect() {
// Show it if there is no other tips shown
final boolean smartBatteryOn = Settings.Global.getInt(mContentResolver,
Settings.Global.APP_STANDBY_ENABLED, 1) != 0;
final int state =
smartBatteryOn ? BatteryTip.StateType.INVISIBLE : BatteryTip.StateType.NEW;
return new SmartBatteryTip(state);
}
}

View File

@@ -105,11 +105,6 @@ public abstract class BatteryTip implements Comparable<BatteryTip>, Parcelable {
*/
public abstract void updateState(BatteryTip tip);
/**
* Execute the action for this {@link BatteryTip}
*/
public abstract void action();
public Preference buildPreference(Context context) {
Preference preference = new Preference(context);

View File

@@ -78,11 +78,6 @@ public class HighUsageTip extends BatteryTip {
mState = tip.mState;
}
@Override
public void action() {
// do nothing
}
public long getScreenTimeMs() {
return mScreenTimeMs;
}

View File

@@ -55,11 +55,6 @@ public class LowBatteryTip extends BatteryTip {
mState = tip.mState;
}
@Override
public void action() {
// do nothing
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public BatteryTip createFromParcel(Parcel in) {
return new LowBatteryTip(in);

View File

@@ -0,0 +1,68 @@
/*
* 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 android.provider.Settings;
import com.android.settings.R;
/**
* Tip to suggest turn on smart battery if it is not on
*/
public class SmartBatteryTip extends BatteryTip {
public SmartBatteryTip(@StateType int state) {
super(TipType.SMART_BATTERY_MANAGER, state, false /* showDialog */);
}
private SmartBatteryTip(Parcel in) {
super(in);
}
@Override
public CharSequence getTitle(Context context) {
return context.getString(R.string.battery_tip_smart_battery_title);
}
@Override
public CharSequence getSummary(Context context) {
return context.getString(R.string.battery_tip_smart_battery_summary);
}
@Override
public int getIconId() {
return R.drawable.ic_perm_device_information_red_24dp;
}
@Override
public void updateState(BatteryTip tip) {
mState = tip.mState;
}
public static final Creator CREATOR = new Creator() {
public BatteryTip createFromParcel(Parcel in) {
return new SmartBatteryTip(in);
}
public BatteryTip[] newArray(int size) {
return new SmartBatteryTip[size];
}
};
}

View File

@@ -55,11 +55,6 @@ public class SummaryTip extends BatteryTip {
mState = tip.mState;
}
@Override
public void action() {
// do nothing
}
public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public BatteryTip createFromParcel(Parcel in) {
return new SummaryTip(in);