OSDN Git Service

Merge "Update the restrict app list" into pi-dev
[android-x86/packages-apps-Settings.git] / src / com / android / settings / fuelgauge / batterytip / BatteryTipDialogFragment.java
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;
18
19 import android.app.AlertDialog;
20 import android.app.Dialog;
21 import android.content.Context;
22 import android.content.DialogInterface;
23 import android.os.Bundle;
24 import android.support.annotation.VisibleForTesting;
25 import android.support.v7.widget.LinearLayoutManager;
26 import android.support.v7.widget.RecyclerView;
27 import android.view.LayoutInflater;
28
29 import com.android.internal.logging.nano.MetricsProto;
30 import com.android.settings.R;
31 import com.android.settings.SettingsActivity;
32 import com.android.settings.Utils;
33 import com.android.settings.core.InstrumentedPreferenceFragment;
34 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
35 import com.android.settings.fuelgauge.Estimate;
36 import com.android.settings.fuelgauge.batterytip.BatteryTipPreferenceController.BatteryTipListener;
37 import com.android.settings.fuelgauge.batterytip.actions.BatteryTipAction;
38 import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
39 import com.android.settings.fuelgauge.batterytip.tips.HighUsageTip;
40 import com.android.settings.fuelgauge.batterytip.tips.RestrictAppTip;
41 import com.android.settings.fuelgauge.batterytip.tips.SummaryTip;
42 import com.android.settings.fuelgauge.batterytip.tips.UnrestrictAppTip;
43 import com.android.settingslib.utils.StringUtil;
44
45 import java.util.List;
46
47 /**
48  * Dialog Fragment to show action dialog for each anomaly
49  */
50 public class BatteryTipDialogFragment extends InstrumentedDialogFragment implements
51         DialogInterface.OnClickListener {
52
53     private static final String ARG_BATTERY_TIP = "battery_tip";
54
55     @VisibleForTesting
56     BatteryTip mBatteryTip;
57
58     public static BatteryTipDialogFragment newInstance(BatteryTip batteryTip) {
59         BatteryTipDialogFragment dialogFragment = new BatteryTipDialogFragment();
60
61         Bundle args = new Bundle(1);
62         args.putParcelable(ARG_BATTERY_TIP, batteryTip);
63         dialogFragment.setArguments(args);
64
65         return dialogFragment;
66     }
67
68     @Override
69     public Dialog onCreateDialog(Bundle savedInstanceState) {
70         final Bundle bundle = getArguments();
71         final Context context = getContext();
72
73         mBatteryTip = bundle.getParcelable(ARG_BATTERY_TIP);
74
75         switch (mBatteryTip.getType()) {
76             case BatteryTip.TipType.SUMMARY:
77                 final long averageTimeMs = ((SummaryTip) mBatteryTip).getAverageTimeMs();
78                 final String message = context.getString(
79                         averageTimeMs == Estimate.AVERAGE_TIME_TO_DISCHARGE_UNKNOWN
80                                 ? R.string.battery_tip_dialog_summary_message_no_estimation
81                                 : R.string.battery_tip_dialog_summary_message,
82                         StringUtil.formatElapsedTime(context, averageTimeMs,
83                                 false /* withSeconds */));
84
85                 return new AlertDialog.Builder(context)
86                         .setMessage(message)
87                         .setPositiveButton(android.R.string.ok, null)
88                         .create();
89             case BatteryTip.TipType.HIGH_DEVICE_USAGE:
90                 final HighUsageTip highUsageTip = (HighUsageTip) mBatteryTip;
91                 final RecyclerView view = (RecyclerView) LayoutInflater.from(context).inflate(
92                         R.layout.recycler_view,
93                         null);
94                 view.setLayoutManager(new LinearLayoutManager(context));
95                 view.setAdapter(new HighUsageAdapter(context,
96                         highUsageTip.getHighUsageAppList()));
97
98                 return new AlertDialog.Builder(context)
99                         .setMessage(getString(R.string.battery_tip_dialog_message,
100                                 StringUtil.formatElapsedTime(
101                                         context, highUsageTip.getScreenTimeMs(),
102                                         false /* withSeconds */)))
103                         .setView(view)
104                         .setPositiveButton(android.R.string.ok, null)
105                         .create();
106             case BatteryTip.TipType.APP_RESTRICTION:
107                 final RestrictAppTip restrictAppTip = (RestrictAppTip) mBatteryTip;
108                 final List<AppInfo> restrictedAppList = restrictAppTip.getRestrictAppList();
109                 final int num = restrictedAppList.size();
110                 final CharSequence appLabel = Utils.getApplicationLabel(context,
111                         restrictedAppList.get(0).packageName);
112
113                 final AlertDialog.Builder builder = new AlertDialog.Builder(context)
114                         .setTitle(context.getResources().getQuantityString(
115                                 R.plurals.battery_tip_restrict_app_dialog_title, num, num))
116                         .setPositiveButton(R.string.battery_tip_restrict_app_dialog_ok, this)
117                         .setNegativeButton(android.R.string.cancel, null);
118                 if (num == 1) {
119                     builder.setMessage(
120                             getString(R.string.battery_tip_restrict_app_dialog_message, appLabel));
121                 } else if (num <= 5) {
122                     builder.setMessage(
123                             getString(
124                                     R.string.battery_tip_restrict_apps_less_than_5_dialog_message));
125                     final RecyclerView restrictionView = (RecyclerView) LayoutInflater.from(
126                             context).inflate(R.layout.recycler_view, null);
127                     restrictionView.setLayoutManager(new LinearLayoutManager(context));
128                     restrictionView.setAdapter(new HighUsageAdapter(context, restrictedAppList));
129                     builder.setView(restrictionView);
130                 } else {
131                     builder.setMessage(context.getString(
132                             R.string.battery_tip_restrict_apps_more_than_5_dialog_message,
133                             restrictAppTip.getRestrictAppsString(context)));
134                 }
135
136                 return builder.create();
137             case BatteryTip.TipType.REMOVE_APP_RESTRICTION:
138                 final UnrestrictAppTip unrestrictAppTip = (UnrestrictAppTip) mBatteryTip;
139                 final CharSequence name = Utils.getApplicationLabel(context,
140                         unrestrictAppTip.getPackageName());
141
142                 return new AlertDialog.Builder(context)
143                         .setTitle(getString(R.string.battery_tip_unrestrict_app_dialog_title, name))
144                         .setMessage(R.string.battery_tip_unrestrict_app_dialog_message)
145                         .setPositiveButton(R.string.battery_tip_unrestrict_app_dialog_ok, this)
146                         .setNegativeButton(R.string.battery_tip_unrestrict_app_dialog_cancel, null)
147                         .create();
148             default:
149                 throw new IllegalArgumentException("unknown type " + mBatteryTip.getType());
150         }
151     }
152
153     @Override
154     public int getMetricsCategory() {
155         return MetricsProto.MetricsEvent.FUELGAUGE_BATTERY_TIP_DIALOG;
156     }
157
158     @Override
159     public void onClick(DialogInterface dialog, int which) {
160         final BatteryTipListener lsn = (BatteryTipListener) getTargetFragment();
161         if (lsn == null) {
162             return;
163         }
164         final BatteryTipAction action = BatteryTipUtils.getActionForBatteryTip(mBatteryTip,
165                 (SettingsActivity) getActivity(),
166                 (InstrumentedPreferenceFragment) getTargetFragment());
167         if (action != null) {
168             action.handlePositiveAction();
169         }
170         lsn.onBatteryTipHandled(mBatteryTip);
171     }
172
173 }