1/*
2 * Copyright (C) 2012 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.net;
18
19import static android.net.NetworkPolicy.LIMIT_DISABLED;
20import static android.net.wifi.WifiInfo.removeDoubleQuotes;
21import static com.android.settings.DataUsageSummary.hasReadyMobileRadio;
22import static com.android.settings.DataUsageSummary.hasWifiRadio;
23
24import android.content.Context;
25import android.content.res.Resources;
26import android.net.NetworkPolicy;
27import android.net.NetworkPolicyManager;
28import android.net.NetworkTemplate;
29import android.net.wifi.WifiConfiguration;
30import android.net.wifi.WifiManager;
31import android.os.Bundle;
32import android.preference.Preference;
33import android.preference.PreferenceCategory;
34import android.preference.SwitchPreference;
35import android.telephony.TelephonyManager;
36
37import com.android.settings.R;
38import com.android.settings.SettingsPreferenceFragment;
39import com.android.settings.search.BaseSearchIndexProvider;
40import com.android.settings.search.Indexable;
41import com.android.settings.search.SearchIndexableRaw;
42
43import java.util.ArrayList;
44import java.util.List;
45
46/**
47 * Panel to configure {@link NetworkPolicy#metered} for networks.
48 */
49public class DataUsageMeteredSettings extends SettingsPreferenceFragment implements Indexable {
50
51    private static final boolean SHOW_MOBILE_CATEGORY = false;
52
53    private NetworkPolicyManager mPolicyManager;
54    private WifiManager mWifiManager;
55
56    private NetworkPolicyEditor mPolicyEditor;
57
58    private PreferenceCategory mMobileCategory;
59    private PreferenceCategory mWifiCategory;
60    private Preference mWifiDisabled;
61
62    @Override
63    public void onCreate(Bundle icicle) {
64        super.onCreate(icicle);
65        final Context context = getActivity();
66
67        mPolicyManager = NetworkPolicyManager.from(context);
68        mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
69
70        mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
71        mPolicyEditor.read();
72
73        addPreferencesFromResource(R.xml.data_usage_metered_prefs);
74        mMobileCategory = (PreferenceCategory) findPreference("mobile");
75        mWifiCategory = (PreferenceCategory) findPreference("wifi");
76        mWifiDisabled = findPreference("wifi_disabled");
77
78        updateNetworks(context);
79    }
80
81    private void updateNetworks(Context context) {
82        if (SHOW_MOBILE_CATEGORY && hasReadyMobileRadio(context)) {
83            mMobileCategory.removeAll();
84            mMobileCategory.addPreference(buildMobilePref(context));
85        } else {
86            getPreferenceScreen().removePreference(mMobileCategory);
87        }
88
89        mWifiCategory.removeAll();
90        if (hasWifiRadio(context) && mWifiManager.isWifiEnabled()) {
91            for (WifiConfiguration config : mWifiManager.getConfiguredNetworks()) {
92                if (config.SSID != null) {
93                    mWifiCategory.addPreference(buildWifiPref(context, config));
94                }
95            }
96        } else {
97            mWifiCategory.addPreference(mWifiDisabled);
98        }
99    }
100
101    private Preference buildMobilePref(Context context) {
102        final TelephonyManager tele = TelephonyManager.from(context);
103        final NetworkTemplate template = NetworkTemplate.buildTemplateMobileAll(
104                tele.getSubscriberId());
105        final MeteredPreference pref = new MeteredPreference(context, template);
106        pref.setTitle(tele.getNetworkOperatorName());
107        return pref;
108    }
109
110    private Preference buildWifiPref(Context context, WifiConfiguration config) {
111        final String networkId = config.SSID;
112        final NetworkTemplate template = NetworkTemplate.buildTemplateWifi(networkId);
113        final MeteredPreference pref = new MeteredPreference(context, template);
114        pref.setTitle(removeDoubleQuotes(networkId));
115        return pref;
116    }
117
118    private class MeteredPreference extends SwitchPreference {
119        private final NetworkTemplate mTemplate;
120        private boolean mBinding;
121
122        public MeteredPreference(Context context, NetworkTemplate template) {
123            super(context);
124            mTemplate = template;
125
126            setPersistent(false);
127
128            mBinding = true;
129            final NetworkPolicy policy = mPolicyEditor.getPolicyMaybeUnquoted(template);
130            if (policy != null) {
131                if (policy.limitBytes != LIMIT_DISABLED) {
132                    setChecked(true);
133                    setEnabled(false);
134                } else {
135                    setChecked(policy.metered);
136                }
137            } else {
138                setChecked(false);
139            }
140            mBinding = false;
141        }
142
143        @Override
144        protected void notifyChanged() {
145            super.notifyChanged();
146            if (!mBinding) {
147                mPolicyEditor.setPolicyMetered(mTemplate, isChecked());
148            }
149        }
150    }
151
152    /**
153     * For search
154     */
155    public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
156        new BaseSearchIndexProvider() {
157            @Override
158            public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
159                final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
160                final Resources res = context.getResources();
161
162                // Add fragment title
163                SearchIndexableRaw data = new SearchIndexableRaw(context);
164                data.title = res.getString(R.string.data_usage_menu_metered);
165                data.screenTitle = res.getString(R.string.data_usage_menu_metered);
166                result.add(data);
167
168                // Body
169                data = new SearchIndexableRaw(context);
170                data.title = res.getString(R.string.data_usage_metered_body);
171                data.screenTitle = res.getString(R.string.data_usage_menu_metered);
172                result.add(data);
173
174                if (SHOW_MOBILE_CATEGORY && hasReadyMobileRadio(context)) {
175                    // Mobile networks category
176                    data = new SearchIndexableRaw(context);
177                    data.title = res.getString(R.string.data_usage_metered_mobile);
178                    data.screenTitle = res.getString(R.string.data_usage_menu_metered);
179                    result.add(data);
180
181                    final TelephonyManager tele = TelephonyManager.from(context);
182
183                    data = new SearchIndexableRaw(context);
184                    data.title = tele.getNetworkOperatorName();
185                    data.screenTitle = res.getString(R.string.data_usage_menu_metered);
186                    result.add(data);
187                }
188
189                // Wi-Fi networks category
190                data = new SearchIndexableRaw(context);
191                data.title = res.getString(R.string.data_usage_metered_wifi);
192                data.screenTitle = res.getString(R.string.data_usage_menu_metered);
193                result.add(data);
194
195                final WifiManager wifiManager =
196                        (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
197                if (hasWifiRadio(context) && wifiManager.isWifiEnabled()) {
198                    for (WifiConfiguration config : wifiManager.getConfiguredNetworks()) {
199                        if (config.SSID != null) {
200                            final String networkId = config.SSID;
201
202                            data = new SearchIndexableRaw(context);
203                            data.title = removeDoubleQuotes(networkId);
204                            data.screenTitle = res.getString(R.string.data_usage_menu_metered);
205                            result.add(data);
206                        }
207                    }
208                } else {
209                    data = new SearchIndexableRaw(context);
210                    data.title = res.getString(R.string.data_usage_metered_wifi_disabled);
211                    data.screenTitle = res.getString(R.string.data_usage_menu_metered);
212                    result.add(data);
213                }
214
215                return result;
216            }
217
218            @Override
219            public List<String> getNonIndexableKeys(Context context) {
220                final ArrayList<String> result = new ArrayList<String>();
221                if (!SHOW_MOBILE_CATEGORY || !hasReadyMobileRadio(context)) {
222                    result.add("mobile");
223                }
224
225                return result;
226            }
227        };
228
229}
230