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