BatterySaverSettings.java revision 5fe76205b607f193b5925eccfabddc145118b5ea
1/*
2 * Copyright (C) 2014 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.content.ContentResolver;
20import android.content.Context;
21import android.content.res.Resources;
22import android.database.ContentObserver;
23import android.net.Uri;
24import android.os.AsyncTask;
25import android.os.Bundle;
26import android.os.Handler;
27import android.provider.Settings.Global;
28import android.util.Log;
29
30import com.android.settings.R;
31import com.android.settings.SettingsPreferenceFragment;
32import com.android.settings.notification.SettingPref;
33
34public class BatterySaverSettings extends SettingsPreferenceFragment {
35    private static final String TAG = "BatterySaverSettings";
36    private static final String KEY_ALWAYS_ON = "always_on";
37    private static final String KEY_TURN_ON_AUTOMATICALLY = "turn_on_automatically";
38    private static final long WAIT_FOR_SWITCH_ANIM = 500;
39
40    private final Handler mHandler = new Handler();
41    private final SettingsObserver mSettingsObserver = new SettingsObserver(mHandler);
42
43    private Context mContext;
44    private boolean mCreated;
45    private SettingPref mAlwaysOnPref;
46    private SettingPref mTriggerPref;
47
48    @Override
49    public void onActivityCreated(Bundle savedInstanceState) {
50        super.onActivityCreated(savedInstanceState);
51        if (mCreated) return;
52        mCreated = true;
53        addPreferencesFromResource(R.xml.battery_saver_settings);
54
55        mContext = getActivity();
56        mAlwaysOnPref = new SettingPref(SettingPref.TYPE_GLOBAL, KEY_ALWAYS_ON,
57                Global.LOW_POWER_MODE, 0) {
58            @Override
59            protected boolean setSetting(Context context, int value) {
60                mHandler.removeCallbacks(mStartMode);
61                if (value == 0) {
62                    return super.setSetting(context, value);
63                } else {
64                    // about lose animations, make sure we don't turn the mode on until the switch
65                    // stops moving
66                    mHandler.postDelayed(mStartMode, WAIT_FOR_SWITCH_ANIM);
67                    return true;
68                }
69            }
70        };
71        mTriggerPref = new SettingPref(SettingPref.TYPE_GLOBAL, KEY_TURN_ON_AUTOMATICALLY,
72                Global.LOW_POWER_MODE_TRIGGER_LEVEL,
73                mContext.getResources().getInteger(
74                        com.android.internal.R.integer.config_lowBatteryWarningLevel),
75                getResources().getIntArray(R.array.battery_saver_trigger_values)) {
76            @Override
77            protected String getCaption(Resources res, int value) {
78                if (value > 0 && value < 100) {
79                    return res.getString(R.string.battery_saver_turn_on_automatically_pct, value);
80                }
81                return res.getString(R.string.battery_saver_turn_on_automatically_never);
82            }
83        };
84        mAlwaysOnPref.init(this);
85        mTriggerPref.init(this);
86    }
87
88    @Override
89    public void onResume() {
90        super.onResume();
91        mSettingsObserver.setListening(true);
92    }
93
94    @Override
95    public void onPause() {
96        super.onPause();
97        mSettingsObserver.setListening(false);
98    }
99
100    private final Runnable mStartMode = new Runnable() {
101        @Override
102        public void run() {
103            AsyncTask.execute(new Runnable() {
104                @Override
105                public void run() {
106                    Log.d(TAG, "Starting LOW_POWER_MODE from settings");
107                    Global.putInt(mContext.getContentResolver(), Global.LOW_POWER_MODE, 1);
108                }
109            });
110        }
111    };
112
113    private final class SettingsObserver extends ContentObserver {
114        private final Uri LOW_POWER_MODE_URI = Global.getUriFor(Global.LOW_POWER_MODE);
115        private final Uri LOW_POWER_MODE_TRIGGER_LEVEL_URI
116                = Global.getUriFor(Global.LOW_POWER_MODE_TRIGGER_LEVEL);
117
118        public SettingsObserver(Handler handler) {
119            super(handler);
120        }
121
122        @Override
123        public void onChange(boolean selfChange, Uri uri) {
124            if (LOW_POWER_MODE_URI.equals(uri)) {
125                mAlwaysOnPref.update(mContext);
126            }
127            if (LOW_POWER_MODE_TRIGGER_LEVEL_URI.equals(uri)) {
128                mTriggerPref.update(mContext);
129            }
130        }
131
132        public void setListening(boolean listening) {
133            final ContentResolver cr = getContentResolver();
134            if (listening) {
135                cr.registerContentObserver(LOW_POWER_MODE_URI, false, this);
136                cr.registerContentObserver(LOW_POWER_MODE_TRIGGER_LEVEL_URI, false, this);
137            } else {
138                cr.unregisterContentObserver(this);
139            }
140        }
141    }
142}
143