<string name="battery_tip_low_battery_title">Low battery capacity</string>
<!-- Summary for the low battery tip [CHAR LIMIT=NONE] -->
<string name="battery_tip_low_battery_summary">Battery can\'t provide good battery life</string>
+ <!-- Title for the smart battery tip [CHAR LIMIT=NONE] -->
+ <string name="battery_tip_smart_battery_title">Turn on smart battery manager</string>
+ <!-- Summary for the smart battery tip [CHAR LIMIT=NONE] -->
+ <string name="battery_tip_smart_battery_summary">Turn on to optimize battery usage</string>
<!-- Title for the battery high usage tip [CHAR LIMIT=NONE] -->
<string name="battery_tip_high_usage_title" product="default">Phone used heavily</string>
<!-- Title for the battery high usage tip [CHAR LIMIT=NONE] -->
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));
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;
if (lsn == null) {
return;
}
- mBatteryTip.action();
+ final BatteryTipAction action = BatteryTipUtils.getActionForBatteryTip(mBatteryTip,
+ (SettingsActivity) getActivity(), this);
+ if (action != null) {
+ action.handlePositiveAction();
+ }
lsn.onBatteryTipHandled(mBatteryTip);
}
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;
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));
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;
private BatteryTipListener mBatteryTipListener;
private List<BatteryTip> mBatteryTips;
private Map<String, BatteryTip> mBatteryTipMap;
+ private SettingsActivity mSettingsActivity;
@VisibleForTesting
PreferenceGroup mPreferenceGroup;
@VisibleForTesting
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
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);
}
--- /dev/null
+/*
+ * 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;
+ }
+ }
+}
--- /dev/null
+/*
+ * 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();
+}
--- /dev/null
+/*
+ * 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()));
+ }
+}
--- /dev/null
+/*
+ * 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);
+ }
+}
*/
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);
mState = tip.mState;
}
- @Override
- public void action() {
- // do nothing
- }
-
public long getScreenTimeMs() {
return mScreenTimeMs;
}
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);
--- /dev/null
+/*
+ * 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];
+ }
+ };
+
+}
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);
*/
package com.android.settings.fuelgauge.batterytip;
+import static com.android.settings.fuelgauge.batterytip.tips.BatteryTip.TipType
+ .SMART_BATTERY_MANAGER;
+
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
import android.support.v7.preference.PreferenceManager;
import android.support.v7.preference.PreferenceScreen;
+import com.android.settings.SettingsActivity;
import com.android.settings.TestConfig;
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
import com.android.settings.fuelgauge.batterytip.tips.SummaryTip;
private PreferenceScreen mPreferenceScreen;
@Mock
private BatteryTip mBatteryTip;
+ @Mock
+ private SettingsActivity mSettingsActivity;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private PreferenceManager mPreferenceManager;
mNewBatteryTips.add(new SummaryTip(BatteryTip.StateType.INVISIBLE));
mBatteryTipPreferenceController = new BatteryTipPreferenceController(mContext, KEY_PREF,
- null, mBatteryTipListener);
+ mSettingsActivity, null, mBatteryTipListener);
mBatteryTipPreferenceController.mPreferenceGroup = mPreferenceGroup;
mBatteryTipPreferenceController.mPrefContext = mContext;
}
}
@Test
- public void testHandlePreferenceTreeClick_noDialog_invokeAction() {
+ public void testHandlePreferenceTreeClick_noDialog_invokeCallback() {
+ doReturn(SMART_BATTERY_MANAGER).when(mBatteryTip).getType();
List<BatteryTip> batteryTips = new ArrayList<>();
batteryTips.add(mBatteryTip);
doReturn(mPreference).when(mBatteryTip).buildPreference(any());
mBatteryTipPreferenceController.handlePreferenceTreeClick(mPreference);
- verify(mBatteryTip).action();
+ verify(mBatteryTipListener).onBatteryTipHandled(mBatteryTip);
}
private void assertOnlyContainsSummaryTip(final PreferenceGroup preferenceGroup) {
--- /dev/null
+/*
+ * 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 static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.spy;
+
+import android.content.Context;
+import android.provider.Settings;
+
+import com.android.settings.TestConfig;
+import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy;
+import com.android.settings.testutils.SettingsRobolectricTestRunner;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
+
+@RunWith(SettingsRobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+public class SmartBatteryDetectorTest {
+ private Context mContext;
+ private BatteryTipPolicy mPolicy;
+ private SmartBatteryDetector mSmartBatteryDetector;
+
+ @Before
+ public void setUp() {
+ MockitoAnnotations.initMocks(this);
+
+ mContext = RuntimeEnvironment.application;
+ mPolicy = spy(new BatteryTipPolicy(mContext));
+ mSmartBatteryDetector = new SmartBatteryDetector(mPolicy, mContext.getContentResolver());
+ }
+
+ @Test
+ public void testDetect_smartBatteryOff_tipVisible() {
+ Settings.Global.putInt(mContext.getContentResolver(),
+ Settings.Global.APP_STANDBY_ENABLED, 0);
+
+ assertThat(mSmartBatteryDetector.detect().isVisible()).isTrue();
+ }
+
+ @Test
+ public void testDetect_smartBatteryOn_tipInvisible() {
+ Settings.Global.putInt(mContext.getContentResolver(),
+ Settings.Global.APP_STANDBY_ENABLED, 1);
+
+ assertThat(mSmartBatteryDetector.detect().isVisible()).isFalse();
+ }
+}
// do nothing
}
- @Override
- public void action() {
- // do nothing
- }
-
public final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public BatteryTip createFromParcel(Parcel in) {
return new TestBatteryTip(in);