HighPowerDetail.java revision 1eb54eb2ff250eccdd700601011bd6457ddcbec1
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
17package com.android.settings.fuelgauge;
18
19import android.app.AlertDialog;
20import android.content.Context;
21import android.os.Bundle;
22import android.preference.Preference;
23import android.preference.Preference.OnPreferenceChangeListener;
24import android.preference.SwitchPreference;
25
26import com.android.settings.InstrumentedFragment;
27import com.android.settings.R;
28import com.android.settings.applications.AppInfoWithHeader;
29import com.android.settings.applications.ApplicationsState.AppEntry;
30
31public class HighPowerDetail extends AppInfoWithHeader implements OnPreferenceChangeListener {
32
33    private static final String KEY_HIGH_POWER_SWITCH = "high_power_switch";
34
35    private final PowerWhitelistBackend mBackend = PowerWhitelistBackend.getInstance();
36
37    private SwitchPreference mUsageSwitch;
38
39    @Override
40    public void onCreate(Bundle savedInstanceState) {
41        super.onCreate(savedInstanceState);
42
43        addPreferencesFromResource(R.xml.high_power_details);
44        mUsageSwitch = (SwitchPreference) findPreference(KEY_HIGH_POWER_SWITCH);
45        mUsageSwitch.setOnPreferenceChangeListener(this);
46    }
47
48    @Override
49    protected boolean refreshUi() {
50        mUsageSwitch.setEnabled(!mBackend.isSysWhitelisted(mPackageName));
51        mUsageSwitch.setChecked(mBackend.isWhitelisted(mPackageName));
52        return true;
53    }
54
55    @Override
56    public boolean onPreferenceChange(Preference preference, Object newValue) {
57        if (newValue == Boolean.TRUE) {
58            mBackend.addApp(mPackageName);
59        } else {
60            mBackend.removeApp(mPackageName);
61        }
62        return true;
63    }
64
65    @Override
66    protected AlertDialog createDialog(int id, int errorCode) {
67        return null;
68    }
69
70    @Override
71    protected int getMetricsCategory() {
72        return InstrumentedFragment.VIEW_CATEGORY_HIGH_POWER_DETAILS;
73    }
74
75    public static CharSequence getSummary(Context context, AppEntry entry) {
76        return getSummary(context, entry.info.packageName);
77    }
78
79    public static CharSequence getSummary(Context context, String pkg) {
80        return context.getString(PowerWhitelistBackend.getInstance().isWhitelisted(pkg)
81                ? R.string.high_power_on : R.string.high_power_off);
82    }
83
84}
85