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 private static final String ARG_METRICS_KEY = "metrics_key"; 55 56 @VisibleForTesting 57 BatteryTip mBatteryTip; 58 @VisibleForTesting 59 int mMetricsKey; 60 61 public static BatteryTipDialogFragment newInstance(BatteryTip batteryTip, int metricsKey) { 62 BatteryTipDialogFragment dialogFragment = new BatteryTipDialogFragment(); 63 64 Bundle args = new Bundle(1); 65 args.putParcelable(ARG_BATTERY_TIP, batteryTip); 66 args.putInt(ARG_METRICS_KEY, metricsKey); 67 dialogFragment.setArguments(args); 68 69 return dialogFragment; 70 } 71 72 @Override 73 public Dialog onCreateDialog(Bundle savedInstanceState) { 74 final Bundle bundle = getArguments(); 75 final Context context = getContext(); 76 77 mBatteryTip = bundle.getParcelable(ARG_BATTERY_TIP); 78 mMetricsKey = bundle.getInt(ARG_METRICS_KEY); 79 80 switch (mBatteryTip.getType()) { 81 case BatteryTip.TipType.SUMMARY: 82 return new AlertDialog.Builder(context) 83 .setMessage(R.string.battery_tip_dialog_summary_message) 84 .setPositiveButton(android.R.string.ok, null) 85 .create(); 86 case BatteryTip.TipType.HIGH_DEVICE_USAGE: 87 final HighUsageTip highUsageTip = (HighUsageTip) mBatteryTip; 88 final RecyclerView view = (RecyclerView) LayoutInflater.from(context).inflate( 89 R.layout.recycler_view, 90 null); 91 view.setLayoutManager(new LinearLayoutManager(context)); 92 view.setAdapter(new HighUsageAdapter(context, 93 highUsageTip.getHighUsageAppList())); 94 95 return new AlertDialog.Builder(context) 96 .setMessage(getString(R.string.battery_tip_dialog_message, 97 highUsageTip.getHighUsageAppList().size())) 98 .setView(view) 99 .setPositiveButton(android.R.string.ok, null) 100 .create(); 101 case BatteryTip.TipType.APP_RESTRICTION: 102 final RestrictAppTip restrictAppTip = (RestrictAppTip) mBatteryTip; 103 final List<AppInfo> restrictedAppList = restrictAppTip.getRestrictAppList(); 104 final int num = restrictedAppList.size(); 105 final CharSequence appLabel = Utils.getApplicationLabel(context, 106 restrictedAppList.get(0).packageName); 107 108 final AlertDialog.Builder builder = new AlertDialog.Builder(context) 109 .setTitle(context.getResources().getQuantityString( 110 R.plurals.battery_tip_restrict_app_dialog_title, num, num)) 111 .setPositiveButton(R.string.battery_tip_restrict_app_dialog_ok, this) 112 .setNegativeButton(android.R.string.cancel, null); 113 if (num == 1) { 114 builder.setMessage( 115 getString(R.string.battery_tip_restrict_app_dialog_message, appLabel)); 116 } else if (num <= 5) { 117 builder.setMessage( 118 getString( 119 R.string.battery_tip_restrict_apps_less_than_5_dialog_message)); 120 final RecyclerView restrictionView = (RecyclerView) LayoutInflater.from( 121 context).inflate(R.layout.recycler_view, null); 122 restrictionView.setLayoutManager(new LinearLayoutManager(context)); 123 restrictionView.setAdapter(new HighUsageAdapter(context, restrictedAppList)); 124 builder.setView(restrictionView); 125 } else { 126 builder.setMessage(context.getString( 127 R.string.battery_tip_restrict_apps_more_than_5_dialog_message, 128 restrictAppTip.getRestrictAppsString(context))); 129 } 130 131 return builder.create(); 132 case BatteryTip.TipType.REMOVE_APP_RESTRICTION: 133 final UnrestrictAppTip unrestrictAppTip = (UnrestrictAppTip) mBatteryTip; 134 final CharSequence name = Utils.getApplicationLabel(context, 135 unrestrictAppTip.getPackageName()); 136 137 return new AlertDialog.Builder(context) 138 .setTitle(getString(R.string.battery_tip_unrestrict_app_dialog_title)) 139 .setMessage(R.string.battery_tip_unrestrict_app_dialog_message) 140 .setPositiveButton(R.string.battery_tip_unrestrict_app_dialog_ok, this) 141 .setNegativeButton(R.string.battery_tip_unrestrict_app_dialog_cancel, null) 142 .create(); 143 default: 144 throw new IllegalArgumentException("unknown type " + mBatteryTip.getType()); 145 } 146 } 147 148 @Override 149 public int getMetricsCategory() { 150 return MetricsProto.MetricsEvent.FUELGAUGE_BATTERY_TIP_DIALOG; 151 } 152 153 @Override 154 public void onClick(DialogInterface dialog, int which) { 155 final BatteryTipListener lsn = (BatteryTipListener) getTargetFragment(); 156 if (lsn == null) { 157 return; 158 } 159 final BatteryTipAction action = BatteryTipUtils.getActionForBatteryTip(mBatteryTip, 160 (SettingsActivity) getActivity(), 161 (InstrumentedPreferenceFragment) getTargetFragment()); 162 if (action != null) { 163 action.handlePositiveAction(mMetricsKey); 164 } 165 lsn.onBatteryTipHandled(mBatteryTip); 166 } 167 168 } 169