Add app restrict tip and detector
1. Add RestrictAppTip with two state(new and handled) 2. Add RestrictAppDetector, and future cl will hook up it with anomaly database 3. Add related dialog in BatteryTipDialogFragment Bug: 72385333 Test: RunSettingsRoboTests Change-Id: Ic10efc6387150e62b6c6ad8d4c0d16ff75564fac
This commit is contained in:
@@ -34,6 +34,9 @@ import com.android.settings.fuelgauge.batterytip.BatteryTipPreferenceController.
|
||||
import com.android.settings.fuelgauge.batterytip.actions.BatteryTipAction;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.HighUsageTip;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.RestrictAppTip;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Dialog Fragment to show action dialog for each anomaly
|
||||
@@ -84,6 +87,23 @@ public class BatteryTipDialogFragment extends InstrumentedDialogFragment impleme
|
||||
.setView(view)
|
||||
.setPositiveButton(android.R.string.ok, null)
|
||||
.create();
|
||||
case BatteryTip.TipType.APP_RESTRICTION:
|
||||
final RestrictAppTip restrictAppTip = (RestrictAppTip) mBatteryTip;
|
||||
final RecyclerView restrictionView = (RecyclerView) LayoutInflater.from(
|
||||
context).inflate(R.layout.recycler_view, null);
|
||||
final List<AppInfo> restrictedAppList = restrictAppTip.getRestrictAppList();
|
||||
final int num = restrictedAppList.size();
|
||||
restrictionView.setLayoutManager(new LinearLayoutManager(context));
|
||||
restrictionView.setAdapter(new HighUsageAdapter(context, restrictedAppList));
|
||||
|
||||
return new AlertDialog.Builder(context)
|
||||
.setTitle(context.getResources().getQuantityString(
|
||||
R.plurals.battery_tip_restrict_title, num, num))
|
||||
.setMessage(getString(R.string.battery_tip_restrict_app_dialog_message))
|
||||
.setView(restrictionView)
|
||||
.setPositiveButton(R.string.battery_tip_restrict_app_dialog_ok, this)
|
||||
.setNegativeButton(android.R.string.cancel, null)
|
||||
.create();
|
||||
default:
|
||||
throw new IllegalArgumentException("unknown type " + mBatteryTip.getType());
|
||||
}
|
||||
|
@@ -26,6 +26,7 @@ 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;
|
||||
import com.android.settings.fuelgauge.batterytip.detectors.RestrictAppDetector;
|
||||
import com.android.settings.fuelgauge.batterytip.detectors.SummaryDetector;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.LowBatteryTip;
|
||||
@@ -70,6 +71,7 @@ public class BatteryTipLoader extends AsyncLoader<List<BatteryTip>> {
|
||||
tips.add(new SmartBatteryDetector(policy, context.getContentResolver()).detect());
|
||||
tips.add(new EarlyWarningDetector(policy, context).detect());
|
||||
tips.add(new SummaryDetector(policy).detect());
|
||||
tips.add(new RestrictAppDetector(policy).detect());
|
||||
|
||||
Collections.sort(tips);
|
||||
return tips;
|
||||
|
@@ -77,7 +77,9 @@ public class HighUsageAdapter extends RecyclerView.Adapter<HighUsageAdapter.View
|
||||
Utils.getBadgedIcon(mIconDrawableFactory, mPackageManager, app.packageName,
|
||||
UserHandle.myUserId()));
|
||||
holder.appName.setText(Utils.getApplicationLabel(mContext, app.packageName));
|
||||
holder.appTime.setText(Utils.formatElapsedTime(mContext, app.screenOnTimeMs, false));
|
||||
if (app.screenOnTimeMs != 0) {
|
||||
holder.appTime.setText(Utils.formatElapsedTime(mContext, app.screenOnTimeMs, false));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* 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 com.android.settings.fuelgauge.batterytip.AppInfo;
|
||||
import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
|
||||
import com.android.settings.fuelgauge.batterytip.tips.RestrictAppTip;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Detector whether to show summary tip. This detector should be executed as the last
|
||||
* {@link BatteryTipDetector} since it need the most up-to-date {@code visibleTips}
|
||||
*/
|
||||
public class RestrictAppDetector implements BatteryTipDetector {
|
||||
private BatteryTipPolicy mPolicy;
|
||||
|
||||
public RestrictAppDetector(BatteryTipPolicy policy) {
|
||||
mPolicy = policy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BatteryTip detect() {
|
||||
// TODO(b/70570352): Detect restrict apps here, get data from database
|
||||
final List<AppInfo> highUsageApps = new ArrayList<>();
|
||||
return new RestrictAppTip(
|
||||
highUsageApps.isEmpty() ? BatteryTip.StateType.INVISIBLE : BatteryTip.StateType.NEW,
|
||||
highUsageApps);
|
||||
}
|
||||
}
|
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* 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.content.res.Resources;
|
||||
import android.os.Parcel;
|
||||
|
||||
import com.android.internal.annotations.VisibleForTesting;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.Utils;
|
||||
import com.android.settings.fuelgauge.batterytip.AppInfo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Tip to suggest user to restrict some bad apps
|
||||
*/
|
||||
public class RestrictAppTip extends BatteryTip {
|
||||
private List<AppInfo> mRestrictAppList;
|
||||
|
||||
public RestrictAppTip(@StateType int state, List<AppInfo> highUsageApps) {
|
||||
super(TipType.APP_RESTRICTION, state, true /* showDialog */);
|
||||
mRestrictAppList = highUsageApps;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
RestrictAppTip(Parcel in) {
|
||||
super(in);
|
||||
mRestrictAppList = in.createTypedArrayList(AppInfo.CREATOR);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getTitle(Context context) {
|
||||
final int num = mRestrictAppList.size();
|
||||
return context.getResources().getQuantityString(
|
||||
mState == StateType.HANDLED
|
||||
? R.plurals.battery_tip_restrict_handled_title
|
||||
: R.plurals.battery_tip_restrict_title,
|
||||
num, num);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getSummary(Context context) {
|
||||
final int num = mRestrictAppList.size();
|
||||
final CharSequence appLabel = num > 0 ? Utils.getApplicationLabel(context,
|
||||
mRestrictAppList.get(0).packageName) : "";
|
||||
return mState == StateType.HANDLED
|
||||
? context.getString(R.string.battery_tip_restrict_handled_summary)
|
||||
: context.getResources().getQuantityString(R.plurals.battery_tip_restrict_summary,
|
||||
num, appLabel, num);
|
||||
}
|
||||
|
||||
@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) {
|
||||
mState = tip.mState;
|
||||
}
|
||||
|
||||
public List<AppInfo> getRestrictAppList() {
|
||||
return mRestrictAppList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel dest, int flags) {
|
||||
super.writeToParcel(dest, flags);
|
||||
dest.writeTypedList(mRestrictAppList);
|
||||
}
|
||||
|
||||
public static final Creator CREATOR = new Creator() {
|
||||
public BatteryTip createFromParcel(Parcel in) {
|
||||
return new RestrictAppTip(in);
|
||||
}
|
||||
|
||||
public BatteryTip[] newArray(int size) {
|
||||
return new RestrictAppTip[size];
|
||||
}
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user