• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.wifi;
18 
19 import android.content.Context;
20 import android.provider.Settings;
21 import android.support.v7.preference.ListPreference;
22 import android.support.v7.preference.Preference;
23 import android.util.Log;
24 import android.widget.Toast;
25 
26 import com.android.settings.R;
27 import com.android.settings.Utils;
28 import com.android.settings.core.PreferenceController;
29 
30 import static com.android.internal.os.MemoryPowerCalculator.TAG;
31 
32 public class WifiSleepPolicyPreferenceController extends PreferenceController implements
33         Preference.OnPreferenceChangeListener {
34 
35     private static final String KEY_SLEEP_POLICY = "sleep_policy";
36 
WifiSleepPolicyPreferenceController(Context context)37     public WifiSleepPolicyPreferenceController(Context context) {
38         super(context);
39     }
40 
41     @Override
isAvailable()42     public boolean isAvailable() {
43         return true;
44     }
45 
46     @Override
getPreferenceKey()47     public String getPreferenceKey() {
48         return KEY_SLEEP_POLICY;
49     }
50 
51     @Override
updateState(Preference preference)52     public void updateState(Preference preference) {
53         ListPreference sleepPolicyPref = (ListPreference) preference;
54         if (sleepPolicyPref != null) {
55             if (Utils.isWifiOnly(mContext)) {
56                 sleepPolicyPref.setEntries(R.array.wifi_sleep_policy_entries_wifi_only);
57             }
58             int value = Settings.Global.getInt(mContext.getContentResolver(),
59                     Settings.Global.WIFI_SLEEP_POLICY,
60                     Settings.Global.WIFI_SLEEP_POLICY_NEVER);
61             String stringValue = String.valueOf(value);
62             sleepPolicyPref.setValue(stringValue);
63             updateSleepPolicySummary(sleepPolicyPref, stringValue);
64         }
65     }
66 
67     @Override
onPreferenceChange(Preference preference, Object newValue)68     public boolean onPreferenceChange(Preference preference, Object newValue) {
69         try {
70             String stringValue = (String) newValue;
71             Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.WIFI_SLEEP_POLICY,
72                     Integer.parseInt(stringValue));
73             updateSleepPolicySummary(preference, stringValue);
74         } catch (NumberFormatException e) {
75             Toast.makeText(mContext, R.string.wifi_setting_sleep_policy_error,
76                     Toast.LENGTH_SHORT).show();
77             return false;
78         }
79         return true;
80     }
81 
updateSleepPolicySummary(Preference sleepPolicyPref, String value)82     private void updateSleepPolicySummary(Preference sleepPolicyPref, String value) {
83         if (value != null) {
84             String[] values = mContext.getResources().getStringArray(R.array
85                     .wifi_sleep_policy_values);
86             final int summaryArrayResId = Utils.isWifiOnly(mContext)
87                     ? R.array.wifi_sleep_policy_entries_wifi_only
88                     : R.array.wifi_sleep_policy_entries;
89             String[] summaries = mContext.getResources().getStringArray(summaryArrayResId);
90             for (int i = 0; i < values.length; i++) {
91                 if (value.equals(values[i])) {
92                     if (i < summaries.length) {
93                         sleepPolicyPref.setSummary(summaries[i]);
94                         return;
95                     }
96                 }
97             }
98         }
99 
100         sleepPolicyPref.setSummary("");
101         Log.e(TAG, "Invalid sleep policy value: " + value);
102     }
103 
104 }
105