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.DialogFragment;
22 import android.app.Fragment;
23 import android.content.Context;
24 import android.content.DialogInterface;
25 import android.content.DialogInterface.OnClickListener;
26 import android.content.pm.PackageManager;
27 import android.content.pm.PackageManager.NameNotFoundException;
28 import android.os.Bundle;
29 import android.view.View;
30 import android.widget.Checkable;
31 import android.widget.TextView;
33 import com.android.settings.R;
34 import com.android.settings.applications.AppInfoBase;
35 import com.android.settingslib.applications.ApplicationsState.AppEntry;
37 public class HighPowerDetail extends DialogFragment implements OnClickListener,
38 View.OnClickListener {
40 private static final String ARG_DEFAULT_ON = "default_on";
42 private final PowerWhitelistBackend mBackend = PowerWhitelistBackend.getInstance();
44 private String mPackageName;
45 private CharSequence mLabel;
46 private boolean mDefaultOn;
47 private boolean mIsEnabled;
48 private Checkable mOptionOn;
49 private Checkable mOptionOff;
52 public void onCreate(Bundle savedInstanceState) {
53 super.onCreate(savedInstanceState);
55 mPackageName = getArguments().getString(AppInfoBase.ARG_PACKAGE_NAME);
56 PackageManager pm = getContext().getPackageManager();
58 mLabel = pm.getApplicationInfo(mPackageName, 0).loadLabel(pm);
59 } catch (NameNotFoundException e) {
60 mLabel = mPackageName;
62 mDefaultOn = getArguments().getBoolean(ARG_DEFAULT_ON);
63 mIsEnabled = mDefaultOn || mBackend.isWhitelisted(mPackageName);
66 public Checkable setup(View view, boolean on) {
67 ((TextView) view.findViewById(android.R.id.title)).setText(on
68 ? R.string.ignore_optimizations_on : R.string.ignore_optimizations_off);
69 ((TextView) view.findViewById(android.R.id.summary)).setText(on
70 ? R.string.ignore_optimizations_on_desc : R.string.ignore_optimizations_off_desc);
71 view.setClickable(true);
72 view.setOnClickListener(this);
73 if (!on && mBackend.isSysWhitelisted(mPackageName)) {
74 view.setEnabled(false);
76 return (Checkable) view;
80 public Dialog onCreateDialog(Bundle savedInstanceState) {
81 AlertDialog.Builder b = new AlertDialog.Builder(getContext())
82 .setTitle(getString(R.string.ignore_optimizations_title, mLabel))
83 .setNegativeButton(R.string.cancel, null)
84 .setView(R.layout.ignore_optimizations_content);
85 if (!mBackend.isSysWhitelisted(mPackageName)) {
86 b.setPositiveButton(R.string.done, this);
92 public void onStart() {
94 mOptionOn = setup(getDialog().findViewById(R.id.ignore_on), true);
95 mOptionOff = setup(getDialog().findViewById(R.id.ignore_off), false);
99 private void updateViews() {
100 mOptionOn.setChecked(mIsEnabled);
101 mOptionOff.setChecked(!mIsEnabled);
105 public void onClick(View v) {
106 if (v == mOptionOn) {
109 } else if (v == mOptionOff) {
116 public void onClick(DialogInterface dialog, int which) {
117 if (which == DialogInterface.BUTTON_POSITIVE) {
118 boolean newValue = mIsEnabled;
119 boolean oldValue = mBackend.isWhitelisted(mPackageName);
120 if (newValue != oldValue) {
122 mBackend.addApp(mPackageName);
124 mBackend.removeApp(mPackageName);
131 public void onDismiss(DialogInterface dialog) {
132 super.onDismiss(dialog);
133 Fragment target = getTargetFragment();
134 if (target != null) {
135 target.onActivityResult(getTargetRequestCode(), 0, null);
139 public static CharSequence getSummary(Context context, AppEntry entry) {
140 return getSummary(context, entry.info.packageName);
143 public static CharSequence getSummary(Context context, String pkg) {
144 return context.getString(PowerWhitelistBackend.getInstance().isWhitelisted(pkg)
145 ? R.string.high_power_on : R.string.high_power_off);
148 public static void show(Fragment caller, String packageName, int requestCode,
149 boolean defaultToOn) {
150 HighPowerDetail fragment = new HighPowerDetail();
151 Bundle args = new Bundle();
152 args.putString(AppInfoBase.ARG_PACKAGE_NAME, packageName);
153 args.putBoolean(ARG_DEFAULT_ON, defaultToOn);
154 fragment.setArguments(args);
155 fragment.setTargetFragment(caller, requestCode);
156 fragment.show(caller.getFragmentManager(), HighPowerDetail.class.getSimpleName());