OSDN Git Service

8c75868cace33af0a7edfbccab66cc33c5bb2a0d
[android-x86/packages-apps-Settings.git] / src / com / android / settings / fuelgauge / HighPowerDetail.java
1 /*
2  * Copyright (C) 2015 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;
18
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;
36
37 import com.android.settings.R;
38 import com.android.settings.applications.AppInfoBase;
39 import com.android.settingslib.applications.ApplicationsState.AppEntry;
40
41 public class HighPowerDetail extends DialogFragment implements OnClickListener,
42         View.OnClickListener {
43
44     private static final String ARG_DEFAULT_ON = "default_on";
45
46     private final PowerWhitelistBackend mBackend = PowerWhitelistBackend.getInstance();
47
48     private String mPackageName;
49     private CharSequence mLabel;
50     private boolean mDefaultOn;
51     private boolean mIsEnabled;
52     private Checkable mOptionOn;
53     private Checkable mOptionOff;
54
55     @Override
56     public void onCreate(Bundle savedInstanceState) {
57         super.onCreate(savedInstanceState);
58
59         mPackageName = getArguments().getString(AppInfoBase.ARG_PACKAGE_NAME);
60         PackageManager pm = getContext().getPackageManager();
61         try {
62             mLabel = pm.getApplicationInfo(mPackageName, 0).loadLabel(pm);
63         } catch (NameNotFoundException e) {
64             mLabel = mPackageName;
65         }
66         mDefaultOn = getArguments().getBoolean(ARG_DEFAULT_ON);
67         mIsEnabled = mDefaultOn || mBackend.isWhitelisted(mPackageName);
68     }
69
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);
79         }
80         return (Checkable) view;
81     }
82
83     @Override
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);
91         }
92         return b.create();
93     }
94
95     @Override
96     public void onStart() {
97         super.onStart();
98         mOptionOn = setup(getDialog().findViewById(R.id.ignore_on), true);
99         mOptionOff = setup(getDialog().findViewById(R.id.ignore_off), false);
100         updateViews();
101     }
102
103     private void updateViews() {
104         mOptionOn.setChecked(mIsEnabled);
105         mOptionOff.setChecked(!mIsEnabled);
106     }
107
108     @Override
109     public void onClick(View v) {
110         if (v == mOptionOn) {
111             mIsEnabled = true;
112             updateViews();
113         } else if (v == mOptionOff) {
114             mIsEnabled = false;
115             updateViews();
116         }
117     }
118
119     @Override
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) {
125                 if (newValue) {
126                     mBackend.addApp(mPackageName);
127                 } else {
128                     mBackend.removeApp(mPackageName);
129                 }
130             }
131         }
132     }
133
134     @Override
135     public void onDismiss(DialogInterface dialog) {
136         super.onDismiss(dialog);
137         Fragment target = getTargetFragment();
138         if (target != null) {
139             target.onActivityResult(getTargetRequestCode(), 0, null);
140         }
141     }
142
143     public static CharSequence getSummary(Context context, AppEntry entry) {
144         return getSummary(context, entry.info.packageName);
145     }
146
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);
150     }
151
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());
161     }
162 }