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.LayoutInflater;
30 import android.view.View;
31 import android.view.ViewGroup;
32 import android.view.ViewGroup.LayoutParams;
33 import android.widget.Checkable;
34 import android.widget.LinearLayout;
35 import android.widget.TextView;
37 import com.android.settings.R;
38 import com.android.settings.applications.AppInfoBase;
39 import com.android.settingslib.applications.ApplicationsState.AppEntry;
41 public class HighPowerDetail extends DialogFragment 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 void onCreate(Bundle savedInstanceState) {
57 super.onCreate(savedInstanceState);
59 mPackageName = getArguments().getString(AppInfoBase.ARG_PACKAGE_NAME);
60 PackageManager pm = getContext().getPackageManager();
62 mLabel = pm.getApplicationInfo(mPackageName, 0).loadLabel(pm);
63 } catch (NameNotFoundException e) {
64 mLabel = mPackageName;
66 mDefaultOn = getArguments().getBoolean(ARG_DEFAULT_ON);
67 mIsEnabled = mDefaultOn || mBackend.isWhitelisted(mPackageName);
70 public Checkable setup(View view, boolean on) {
71 ((TextView) view.findViewById(android.R.id.title)).setText(on
72 ? R.string.ignore_optimizations_on : R.string.ignore_optimizations_off);
73 ((TextView) view.findViewById(android.R.id.summary)).setText(on
74 ? R.string.ignore_optimizations_on_desc : R.string.ignore_optimizations_off_desc);
75 view.setClickable(true);
76 view.setOnClickListener(this);
77 if (!on && mBackend.isSysWhitelisted(mPackageName)) {
78 view.setEnabled(false);
80 return (Checkable) view;
84 public Dialog onCreateDialog(Bundle savedInstanceState) {
85 AlertDialog.Builder b = new AlertDialog.Builder(getContext())
86 .setTitle(getString(R.string.ignore_optimizations_title, mLabel))
87 .setNegativeButton(R.string.cancel, null)
88 .setView(R.layout.ignore_optimizations_content);
89 if (!mBackend.isSysWhitelisted(mPackageName)) {
90 b.setPositiveButton(R.string.done, this);
96 public void onStart() {
98 mOptionOn = setup(getDialog().findViewById(R.id.ignore_on), true);
99 mOptionOff = setup(getDialog().findViewById(R.id.ignore_off), false);
103 private void updateViews() {
104 mOptionOn.setChecked(mIsEnabled);
105 mOptionOff.setChecked(!mIsEnabled);
109 public void onClick(View v) {
110 if (v == mOptionOn) {
113 } else if (v == mOptionOff) {
120 public void onClick(DialogInterface dialog, int which) {
121 if (which == DialogInterface.BUTTON_POSITIVE) {
122 boolean newValue = mIsEnabled;
123 boolean oldValue = mBackend.isWhitelisted(mPackageName);
124 if (newValue != oldValue) {
126 mBackend.addApp(mPackageName);
128 mBackend.removeApp(mPackageName);
135 public void onDismiss(DialogInterface dialog) {
136 super.onDismiss(dialog);
137 Fragment target = getTargetFragment();
138 if (target != null) {
139 target.onActivityResult(getTargetRequestCode(), 0, null);
143 public static CharSequence getSummary(Context context, AppEntry entry) {
144 return getSummary(context, entry.info.packageName);
147 public static CharSequence getSummary(Context context, String pkg) {
148 return context.getString(PowerWhitelistBackend.getInstance().isWhitelisted(pkg)
149 ? R.string.high_power_on : R.string.high_power_off);
152 public static void show(Fragment caller, String packageName, int requestCode,
153 boolean defaultToOn) {
154 HighPowerDetail fragment = new HighPowerDetail();
155 Bundle args = new Bundle();
156 args.putString(AppInfoBase.ARG_PACKAGE_NAME, packageName);
157 args.putBoolean(ARG_DEFAULT_ON, defaultToOn);
158 fragment.setArguments(args);
159 fragment.setTargetFragment(caller, requestCode);
160 fragment.show(caller.getFragmentManager(), HighPowerDetail.class.getSimpleName());