2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.fuelgauge;
19 import android.app.AlertDialog;
20 import android.app.Dialog;
21 import android.app.Fragment;
22 import android.content.Context;
23 import android.content.DialogInterface;
24 import android.content.DialogInterface.OnClickListener;
25 import android.content.pm.PackageManager;
26 import android.content.pm.PackageManager.NameNotFoundException;
27 import android.os.Bundle;
28 import android.view.View;
29 import android.widget.Checkable;
30 import android.widget.TextView;
32 import com.android.internal.annotations.VisibleForTesting;
33 import com.android.internal.logging.nano.MetricsProto;
34 import com.android.settings.R;
35 import com.android.settings.applications.AppInfoBase;
36 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
37 import com.android.settings.overlay.FeatureFactory;
38 import com.android.settingslib.applications.ApplicationsState.AppEntry;
39 import com.android.settingslib.fuelgauge.PowerWhitelistBackend;
41 public class HighPowerDetail extends InstrumentedDialogFragment implements OnClickListener,
42 View.OnClickListener {
44 private static final String ARG_DEFAULT_ON = "default_on";
46 private final PowerWhitelistBackend mBackend = PowerWhitelistBackend.getInstance();
48 private String mPackageName;
49 private CharSequence mLabel;
50 private boolean mDefaultOn;
51 private boolean mIsEnabled;
52 private Checkable mOptionOn;
53 private Checkable mOptionOff;
56 public int getMetricsCategory() {
57 return MetricsProto.MetricsEvent.DIALOG_HIGH_POWER_DETAILS;
61 public void onCreate(Bundle savedInstanceState) {
62 super.onCreate(savedInstanceState);
64 mPackageName = getArguments().getString(AppInfoBase.ARG_PACKAGE_NAME);
65 PackageManager pm = getContext().getPackageManager();
67 mLabel = pm.getApplicationInfo(mPackageName, 0).loadLabel(pm);
68 } catch (NameNotFoundException e) {
69 mLabel = mPackageName;
71 mDefaultOn = getArguments().getBoolean(ARG_DEFAULT_ON);
72 mIsEnabled = mDefaultOn || mBackend.isWhitelisted(mPackageName);
75 public Checkable setup(View view, boolean on) {
76 ((TextView) view.findViewById(android.R.id.title)).setText(on
77 ? R.string.ignore_optimizations_on : R.string.ignore_optimizations_off);
78 ((TextView) view.findViewById(android.R.id.summary)).setText(on
79 ? R.string.ignore_optimizations_on_desc : R.string.ignore_optimizations_off_desc);
80 view.setClickable(true);
81 view.setOnClickListener(this);
82 if (!on && mBackend.isSysWhitelisted(mPackageName)) {
83 view.setEnabled(false);
85 return (Checkable) view;
89 public Dialog onCreateDialog(Bundle savedInstanceState) {
90 AlertDialog.Builder b = new AlertDialog.Builder(getContext())
92 .setNegativeButton(R.string.cancel, null)
93 .setView(R.layout.ignore_optimizations_content);
94 if (!mBackend.isSysWhitelisted(mPackageName)) {
95 b.setPositiveButton(R.string.done, this);
101 public void onStart() {
103 mOptionOn = setup(getDialog().findViewById(R.id.ignore_on), true);
104 mOptionOff = setup(getDialog().findViewById(R.id.ignore_off), false);
108 private void updateViews() {
109 mOptionOn.setChecked(mIsEnabled);
110 mOptionOff.setChecked(!mIsEnabled);
114 public void onClick(View v) {
115 if (v == mOptionOn) {
118 } else if (v == mOptionOff) {
125 public void onClick(DialogInterface dialog, int which) {
126 if (which == DialogInterface.BUTTON_POSITIVE) {
127 boolean newValue = mIsEnabled;
128 boolean oldValue = mBackend.isWhitelisted(mPackageName);
129 if (newValue != oldValue) {
130 logSpecialPermissionChange(newValue, mPackageName, getContext());
132 mBackend.addApp(mPackageName);
134 mBackend.removeApp(mPackageName);
141 static void logSpecialPermissionChange(boolean whitelist, String packageName, Context context) {
142 int logCategory = whitelist ? MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_BATTERY_DENY
143 : MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_BATTERY_ALLOW;
144 FeatureFactory.getFactory(context).getMetricsFeatureProvider().action(context, logCategory,
149 public void onDismiss(DialogInterface dialog) {
150 super.onDismiss(dialog);
151 Fragment target = getTargetFragment();
152 if (target != null && target.getActivity() != null) {
153 target.onActivityResult(getTargetRequestCode(), 0, null);
157 public static CharSequence getSummary(Context context, AppEntry entry) {
158 return getSummary(context, entry.info.packageName);
161 public static CharSequence getSummary(Context context, String pkg) {
162 PowerWhitelistBackend powerWhitelist = PowerWhitelistBackend.getInstance();
163 return context.getString(powerWhitelist.isSysWhitelisted(pkg) ? R.string.high_power_system
164 : powerWhitelist.isWhitelisted(pkg) ? R.string.high_power_on
165 : R.string.high_power_off);
168 public static void show(Fragment caller, String packageName, int requestCode) {
169 HighPowerDetail fragment = new HighPowerDetail();
170 Bundle args = new Bundle();
171 args.putString(AppInfoBase.ARG_PACKAGE_NAME, packageName);
172 fragment.setArguments(args);
173 fragment.setTargetFragment(caller, requestCode);
174 fragment.show(caller.getFragmentManager(), HighPowerDetail.class.getSimpleName());