blob: 85814212241fca91393f00cc41a1a6451d328123 [file] [log] [blame]
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001/*
2 * Copyright (C) 2011 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;
18
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070019import static android.net.ConnectivityManager.TYPE_ETHERNET;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070020import static android.net.ConnectivityManager.TYPE_MOBILE;
21import static android.net.ConnectivityManager.TYPE_WIMAX;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070022import static android.net.NetworkPolicy.LIMIT_DISABLED;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070023import static android.net.NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070024import static android.net.NetworkPolicyManager.POLICY_NONE;
25import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND;
Jeff Sharkey8a503642011-06-10 13:31:21 -070026import static android.net.NetworkPolicyManager.computeLastCycleBoundary;
27import static android.net.NetworkPolicyManager.computeNextCycleBoundary;
Jeff Sharkeya662e492011-06-18 21:57:06 -070028import static android.net.NetworkTemplate.MATCH_MOBILE_3G_LOWER;
29import static android.net.NetworkTemplate.MATCH_MOBILE_4G;
30import static android.net.NetworkTemplate.MATCH_MOBILE_ALL;
31import static android.net.NetworkTemplate.MATCH_WIFI;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070032import static android.net.NetworkTemplate.buildTemplateEthernet;
33import static android.net.NetworkTemplate.buildTemplateMobile3gLower;
34import static android.net.NetworkTemplate.buildTemplateMobile4g;
35import static android.net.NetworkTemplate.buildTemplateMobileAll;
36import static android.net.NetworkTemplate.buildTemplateWifi;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070037import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070038
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070039import android.animation.LayoutTransition;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070040import android.app.AlertDialog;
41import android.app.Dialog;
42import android.app.DialogFragment;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070043import android.app.Fragment;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070044import android.app.FragmentTransaction;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070045import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070046import android.content.ContentResolver;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070047import android.content.Context;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070048import android.content.DialogInterface;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -070049import android.content.Intent;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070050import android.content.Loader;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070051import android.content.SharedPreferences;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070052import android.content.pm.ApplicationInfo;
53import android.content.pm.PackageInfo;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070054import android.content.pm.PackageManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070055import android.content.pm.PackageManager.NameNotFoundException;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070056import android.content.res.Resources;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070057import android.net.ConnectivityManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070058import android.net.INetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070059import android.net.INetworkStatsService;
Jeff Sharkey8a503642011-06-10 13:31:21 -070060import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070061import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070062import android.net.NetworkStats;
63import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070064import android.net.NetworkTemplate;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070065import android.net.TrafficStats;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070066import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070067import android.os.Bundle;
68import android.os.RemoteException;
69import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070070import android.os.SystemProperties;
Jeff Sharkey8a503642011-06-10 13:31:21 -070071import android.preference.Preference;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070072import android.provider.Settings;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070073import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070074import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070075import android.text.format.DateUtils;
76import android.text.format.Formatter;
Jeff Sharkey8a503642011-06-10 13:31:21 -070077import android.text.format.Time;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070078import android.util.Log;
79import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -070080import android.view.Menu;
81import android.view.MenuInflater;
82import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070083import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070084import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070085import android.view.ViewGroup;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -070086import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Jeff Sharkey8a503642011-06-10 13:31:21 -070087import android.widget.AdapterView;
88import android.widget.AdapterView.OnItemClickListener;
89import android.widget.AdapterView.OnItemSelectedListener;
90import android.widget.ArrayAdapter;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070091import android.widget.BaseAdapter;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070092import android.widget.Button;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070093import android.widget.CheckBox;
94import android.widget.CompoundButton;
95import android.widget.CompoundButton.OnCheckedChangeListener;
Jeff Sharkey8a503642011-06-10 13:31:21 -070096import android.widget.LinearLayout;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070097import android.widget.ListView;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070098import android.widget.NumberPicker;
Jeff Sharkey2412b0f2011-07-17 20:31:40 -070099import android.widget.ProgressBar;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700100import android.widget.Spinner;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700101import android.widget.Switch;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700102import android.widget.TabHost;
103import android.widget.TabHost.OnTabChangeListener;
104import android.widget.TabHost.TabContentFactory;
105import android.widget.TabHost.TabSpec;
106import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700107import android.widget.TextView;
108
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700109import com.android.internal.telephony.Phone;
Jeff Sharkeya662e492011-06-18 21:57:06 -0700110import com.android.settings.net.NetworkPolicyEditor;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700111import com.android.settings.net.SummaryForAllUidLoader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700112import com.android.settings.widget.DataUsageChartView;
113import com.android.settings.widget.DataUsageChartView.DataUsageChartListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700114import com.google.android.collect.Lists;
115
116import java.util.ArrayList;
117import java.util.Collections;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700118import java.util.Locale;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700119
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700120import libcore.util.Objects;
121
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700122/**
123 * Panel show data usage history across various networks, including options to
124 * inspect based on usage cycle and control through {@link NetworkPolicy}.
125 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700126public class DataUsageSummary extends Fragment {
127 private static final String TAG = "DataUsage";
Jeff Sharkey8a503642011-06-10 13:31:21 -0700128 private static final boolean LOGD = true;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700129
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700130 // TODO: remove this testing code
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700131 private static final boolean TEST_ANIM = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700132 private static final boolean TEST_RADIOS = false;
133 private static final String TEST_RADIOS_PROP = "test.radios";
134
Jeff Sharkey8a503642011-06-10 13:31:21 -0700135 private static final String TAB_3G = "3g";
136 private static final String TAB_4G = "4g";
137 private static final String TAB_MOBILE = "mobile";
138 private static final String TAB_WIFI = "wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700139 private static final String TAB_ETHERNET = "ethernet";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700140
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700141 private static final String TAG_CONFIRM_ROAMING = "confirmRoaming";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700142 private static final String TAG_CONFIRM_LIMIT = "confirmLimit";
143 private static final String TAG_CYCLE_EDITOR = "cycleEditor";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700144 private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700145 private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700146 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700147
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700148 private static final int LOADER_SUMMARY = 2;
149
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700150 private static final long KB_IN_BYTES = 1024;
151 private static final long MB_IN_BYTES = KB_IN_BYTES * 1024;
152 private static final long GB_IN_BYTES = MB_IN_BYTES * 1024;
153
154 private INetworkStatsService mStatsService;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700155 private INetworkPolicyManager mPolicyService;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700156 private ConnectivityManager mConnService;
157
158 private static final String PREF_FILE = "data_usage";
159 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700160 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700161
162 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700163
Jeff Sharkey8a503642011-06-10 13:31:21 -0700164 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700165 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700166 private TabWidget mTabWidget;
167 private ListView mListView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700168 private DataUsageAdapter mAdapter;
169
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700170 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700171
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700172 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700173 private LinearLayout mNetworkSwitches;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700174 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700175 private View mDataEnabledView;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700176 private CheckBox mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700177 private View mDisableAtLimitView;
178
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700179 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700180 private Spinner mCycleSpinner;
181 private CycleAdapter mCycleAdapter;
182
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700183 private DataUsageChartView mChart;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700184 private TextView mUsageSummary;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700185 private TextView mEmpty;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700186
187 private View mAppDetail;
188 private TextView mAppTitle;
189 private TextView mAppSubtitle;
190 private Button mAppSettings;
191
192 private LinearLayout mAppSwitches;
193 private CheckBox mAppRestrict;
194 private View mAppRestrictView;
195
Jeff Sharkey8a503642011-06-10 13:31:21 -0700196 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700197 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700198
Jeff Sharkeya662e492011-06-18 21:57:06 -0700199 private NetworkTemplate mTemplate = null;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700200
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700201 private static final int UID_NONE = -1;
202 private int mUid = UID_NONE;
203
204 private Intent mAppSettingsIntent;
205
Jeff Sharkeya662e492011-06-18 21:57:06 -0700206 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700207
Jeff Sharkey8a503642011-06-10 13:31:21 -0700208 private NetworkStatsHistory mHistory;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700209 private NetworkStatsHistory mDetailHistory;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700210
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700211 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700212 private String mIntentTab = null;
213
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700214 private MenuItem mMenuDataRoaming;
215 private MenuItem mMenuRestrictBackground;
216
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700217 /** Flag used to ignore listeners during binding. */
218 private boolean mBinding;
219
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700220 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700221 public void onCreate(Bundle savedInstanceState) {
222 super.onCreate(savedInstanceState);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700223
224 mStatsService = INetworkStatsService.Stub.asInterface(
225 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700226 mPolicyService = INetworkPolicyManager.Stub.asInterface(
227 ServiceManager.getService(Context.NETWORK_POLICY_SERVICE));
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700228 mConnService = (ConnectivityManager) getActivity().getSystemService(
229 Context.CONNECTIVITY_SERVICE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700230
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700231 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700232
Jeff Sharkeya662e492011-06-18 21:57:06 -0700233 mPolicyEditor = new NetworkPolicyEditor(mPolicyService);
234 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700235
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700236 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700237 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700238
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700239 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700240 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700241
Jeff Sharkey8a503642011-06-10 13:31:21 -0700242 @Override
243 public View onCreateView(LayoutInflater inflater, ViewGroup container,
244 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700245
Jeff Sharkey8a503642011-06-10 13:31:21 -0700246 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700247 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
248
Jeff Sharkey8a503642011-06-10 13:31:21 -0700249 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700250 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700251 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
252 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700253
Jeff Sharkey8a503642011-06-10 13:31:21 -0700254 mTabHost.setup();
255 mTabHost.setOnTabChangedListener(mTabListener);
256
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700257 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700258 mListView.addHeaderView(mHeader, null, false);
259
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700260 {
261 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700262 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
263 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700264 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700265
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700266 mDataEnabled = new Switch(inflater.getContext());
267 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
268 mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener);
269 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700270
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700271 mDisableAtLimit = new CheckBox(inflater.getContext());
272 mDisableAtLimit.setClickable(false);
273 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
274 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
275 mNetworkSwitches.addView(mDisableAtLimitView);
276 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700277
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700278 // bind cycle dropdown
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700279 mCycleView = mHeader.findViewById(R.id.cycles);
280 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700281 mCycleAdapter = new CycleAdapter(context);
282 mCycleSpinner.setAdapter(mCycleAdapter);
283 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
284
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700285 mChart = (DataUsageChartView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700286 mChart.setListener(mChartListener);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700287
288 {
289 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700290 mAppDetail = mHeader.findViewById(R.id.app_detail);
291 mAppTitle = (TextView) mAppDetail.findViewById(R.id.app_title);
292 mAppSubtitle = (TextView) mAppDetail.findViewById(R.id.app_subtitle);
293 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700294
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700295 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700296 mAppSettings.setOnClickListener(mAppSettingsListener);
297
298 mAppRestrict = new CheckBox(inflater.getContext());
299 mAppRestrict.setClickable(false);
300 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
301 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
302 setPreferenceSummary(
303 mAppRestrictView, R.string.data_usage_app_restrict_background_summary);
304 mAppRestrictView.setOnClickListener(mAppRestrictListener);
305 mAppSwitches.addView(mAppRestrictView);
306 }
307
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700308 mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700309 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700310
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700311 // only assign layout transitions once first layout is finished
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700312 mListView.getViewTreeObserver().addOnGlobalLayoutListener(mFirstLayoutListener);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700313
314 mAdapter = new DataUsageAdapter();
315 mListView.setOnItemClickListener(mListListener);
316 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700317
318 return view;
319 }
320
321 @Override
322 public void onResume() {
323 super.onResume();
324
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700325 // pick default tab based on incoming intent
326 final Intent intent = getActivity().getIntent();
327 mIntentTab = computeTabFromIntent(intent);
328
Jeff Sharkey8a503642011-06-10 13:31:21 -0700329 // this kicks off chain reaction which creates tabs, binds the body to
330 // selected network, and binds chart, cycles and detail list.
331 updateTabs();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700332
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700333 // kick off background task to update stats
334 new AsyncTask<Void, Void, Void>() {
335 @Override
336 protected Void doInBackground(Void... params) {
337 try {
338 mStatsService.forceUpdate();
339 } catch (RemoteException e) {
340 }
341 return null;
342 }
343
344 @Override
345 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700346 if (isAdded()) {
347 updateBody();
348 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700349 }
350 }.execute();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700351 }
352
Jeff Sharkey8a503642011-06-10 13:31:21 -0700353 @Override
354 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
355 inflater.inflate(R.menu.data_usage, menu);
356 }
357
358 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700359 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700360 final Context context = getActivity();
361
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700362 mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming);
363 mMenuDataRoaming.setVisible(hasMobileRadio(context));
364 mMenuDataRoaming.setChecked(getDataRoaming());
365
366 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
367 mMenuRestrictBackground.setChecked(getRestrictBackground());
368
369 final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700370 split4g.setVisible(hasMobile4gRadio(context));
Jeff Sharkeya662e492011-06-18 21:57:06 -0700371 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700372
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700373 final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700374 if (hasWifiRadio(context) && hasMobileRadio(context)) {
375 showWifi.setVisible(true);
376 showWifi.setChecked(mShowWifi);
377 } else {
378 showWifi.setVisible(false);
379 mShowWifi = true;
380 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700381
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700382 final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
383 if (hasEthernet(context) && hasMobileRadio(context)) {
384 showEthernet.setVisible(true);
385 showEthernet.setChecked(mShowEthernet);
386 } else {
387 showEthernet.setVisible(false);
388 mShowEthernet = true;
389 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700390 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700391
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700392 @Override
393 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700394 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700395 case R.id.data_usage_menu_roaming: {
396 final boolean dataRoaming = !item.isChecked();
397 if (dataRoaming) {
398 ConfirmDataRoamingFragment.show(this);
399 } else {
400 // no confirmation to disable roaming
401 setDataRoaming(false);
402 }
403 return true;
404 }
405 case R.id.data_usage_menu_restrict_background: {
406 final boolean restrictBackground = !item.isChecked();
407 if (restrictBackground) {
408 ConfirmRestrictFragment.show(this);
409 } else {
410 // no confirmation to drop restriction
411 setRestrictBackground(false);
412 }
413 return true;
414 }
415 case R.id.data_usage_menu_split_4g: {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700416 final boolean mobileSplit = !item.isChecked();
Jeff Sharkeya662e492011-06-18 21:57:06 -0700417 setMobilePolicySplit(mobileSplit);
418 item.setChecked(isMobilePolicySplit());
Jeff Sharkey8a503642011-06-10 13:31:21 -0700419 updateTabs();
420 return true;
421 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700422 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700423 mShowWifi = !item.isChecked();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700424 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700425 item.setChecked(mShowWifi);
426 updateTabs();
427 return true;
428 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700429 case R.id.data_usage_menu_show_ethernet: {
430 mShowEthernet = !item.isChecked();
431 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
432 item.setChecked(mShowEthernet);
433 updateTabs();
434 return true;
435 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700436 }
437 return false;
438 }
439
Jeff Sharkey94a90952011-06-13 22:31:09 -0700440 @Override
441 public void onDestroyView() {
442 super.onDestroyView();
443
444 mDataEnabledView = null;
445 mDisableAtLimitView = null;
446 }
447
Jeff Sharkey8a503642011-06-10 13:31:21 -0700448 /**
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700449 * Listener to setup {@link LayoutTransition} after first layout pass.
450 */
451 private OnGlobalLayoutListener mFirstLayoutListener = new OnGlobalLayoutListener() {
452 /** {@inheritDoc} */
453 public void onGlobalLayout() {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700454 mListView.getViewTreeObserver().removeGlobalOnLayoutListener(mFirstLayoutListener);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700455
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700456 mTabsContainer.setLayoutTransition(buildLayoutTransition());
457 mHeader.setLayoutTransition(buildLayoutTransition());
458 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700459
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700460 final LayoutTransition chartTransition = buildLayoutTransition();
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700461 chartTransition.setStartDelay(LayoutTransition.APPEARING, 0);
462 chartTransition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
463 mChart.setLayoutTransition(chartTransition);
464 }
465 };
466
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700467 private static LayoutTransition buildLayoutTransition() {
468 final LayoutTransition transition = new LayoutTransition();
469 if (TEST_ANIM) {
470 transition.setDuration(1500);
471 }
472 transition.setAnimateParentHierarchy(false);
473 return transition;
474 }
475
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700476 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700477 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700478 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
479 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700480 */
481 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700482 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700483 mTabHost.clearAllTabs();
484
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700485 final boolean mobileSplit = isMobilePolicySplit();
486 if (mobileSplit && hasMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700487 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
488 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700489 } else if (hasMobileRadio(context)) {
490 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700491 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700492 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700493 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
494 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700495 if (mShowEthernet && hasEthernet(context)) {
496 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
497 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700498
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700499 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
500 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
501 if (mIntentTab != null) {
502 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
503 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700504 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700505 mTabHost.setCurrentTabByTag(mIntentTab);
506 }
507 mIntentTab = null;
508 } else {
509 if (mTabHost.getCurrentTab() == 0) {
510 updateBody();
511 } else {
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700512 mTabHost.setCurrentTab(0);
513 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700514 }
515 }
516
Jeff Sharkey8a503642011-06-10 13:31:21 -0700517 /**
518 * Factory that provide empty {@link View} to make {@link TabHost} happy.
519 */
520 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
521 /** {@inheritDoc} */
522 public View createTabContent(String tag) {
523 return new View(mTabHost.getContext());
524 }
525 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700526
Jeff Sharkey8a503642011-06-10 13:31:21 -0700527 /**
528 * Build {@link TabSpec} with thin indicator, and empty content.
529 */
530 private TabSpec buildTabSpec(String tag, int titleRes) {
531 final LayoutInflater inflater = LayoutInflater.from(mTabWidget.getContext());
532 final View indicator = inflater.inflate(
533 R.layout.tab_indicator_thin_holo, mTabWidget, false);
534 final TextView title = (TextView) indicator.findViewById(android.R.id.title);
535 title.setText(titleRes);
536 return mTabHost.newTabSpec(tag).setIndicator(indicator).setContent(mEmptyTabContent);
537 }
538
539 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
540 /** {@inheritDoc} */
541 public void onTabChanged(String tabId) {
542 // user changed tab; update body
543 updateBody();
544 }
545 };
546
547 /**
548 * Update body content based on current tab. Loads
549 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
550 * binds them to visible controls.
551 */
552 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700553 mBinding = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700554
Jeff Sharkeya662e492011-06-18 21:57:06 -0700555 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700556 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700557
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700558 if (currentTab == null) {
559 Log.w(TAG, "no tab selected; hiding body");
560 mListView.setVisibility(View.GONE);
561 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700562 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700563 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700564 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700565
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700566 final boolean tabChanged = !currentTab.equals(mCurrentTab);
567 mCurrentTab = currentTab;
568
Jeff Sharkey8a503642011-06-10 13:31:21 -0700569 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
570
571 if (TAB_WIFI.equals(currentTab)) {
572 // wifi doesn't have any controls
573 mDataEnabledView.setVisibility(View.GONE);
574 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700575 mTemplate = buildTemplateWifi();
576
577 } else if (TAB_ETHERNET.equals(currentTab)) {
578 // ethernet doesn't have any controls
579 mDataEnabledView.setVisibility(View.GONE);
580 mDisableAtLimitView.setVisibility(View.GONE);
581 mTemplate = buildTemplateEthernet();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700582
583 } else {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700584 mDataEnabledView.setVisibility(View.VISIBLE);
585 mDisableAtLimitView.setVisibility(View.VISIBLE);
586 }
587
588 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700589 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
590 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
591 mDataEnabled.setChecked(mConnService.getMobileDataEnabled());
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700592 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700593
594 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700595 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
596 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
597 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700598 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700599
600 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700601 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
602 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
603 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700604 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700605 }
606
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700607 try {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700608 // load stats for current template
Jeff Sharkey8a503642011-06-10 13:31:21 -0700609 mHistory = mStatsService.getHistoryForNetwork(mTemplate);
610 } catch (RemoteException e) {
611 // since we can't do much without policy or history, and we don't
612 // want to leave with half-baked UI, we bail hard.
613 throw new RuntimeException("problem reading network policy or stats", e);
614 }
615
Jeff Sharkey8a503642011-06-10 13:31:21 -0700616 // bind chart to historical stats
Jeff Sharkey8a503642011-06-10 13:31:21 -0700617 mChart.bindNetworkStats(mHistory);
618
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700619 // only update policy when switching tabs
620 updatePolicy(tabChanged);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700621 updateAppDetail();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700622
623 // force scroll to top of body
624 mListView.smoothScrollToPosition(0);
625
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700626 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700627 }
628
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700629 private boolean isAppDetailMode() {
630 return mUid != UID_NONE;
631 }
632
633 /**
634 * Update UID details panels to match {@link #mUid}, showing or hiding them
635 * depending on {@link #isAppDetailMode()}.
636 */
637 private void updateAppDetail() {
638 if (isAppDetailMode()) {
639 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700640 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700641 } else {
642 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700643 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700644
645 // hide detail stats when not in detail mode
646 mChart.bindDetailNetworkStats(null);
647 return;
648 }
649
650 // remove warning/limit sweeps while in detail mode
651 mChart.bindNetworkPolicy(null);
652
653 final PackageManager pm = getActivity().getPackageManager();
654 mAppTitle.setText(pm.getNameForUid(mUid));
655
656 // enable settings button when package provides it
657 // TODO: target torwards entire UID instead of just first package
658 final String[] packageNames = pm.getPackagesForUid(mUid);
659 if (packageNames != null && packageNames.length > 0) {
660 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
661 mAppSettingsIntent.setPackage(packageNames[0]);
662 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
663
664 final boolean matchFound = pm.resolveActivity(mAppSettingsIntent, 0) != null;
665 mAppSettings.setEnabled(matchFound);
666
667 } else {
668 mAppSettingsIntent = null;
669 mAppSettings.setEnabled(false);
670 }
671
672 try {
673 // load stats for current uid and template
674 // TODO: read template from extras
675 mDetailHistory = mStatsService.getHistoryForUid(mTemplate, mUid, NetworkStats.TAG_NONE);
676 } catch (RemoteException e) {
677 // since we can't do much without history, and we don't want to
678 // leave with half-baked UI, we bail hard.
679 throw new RuntimeException("problem reading network stats", e);
680 }
681
682 // bind chart to historical stats
683 mChart.bindDetailNetworkStats(mDetailHistory);
684
685 updateDetailData();
686
687 final Context context = getActivity();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700688 if (NetworkPolicyManager.isUidValidForPolicy(context, mUid) && !getRestrictBackground()) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700689 mAppRestrictView.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700690 mAppRestrict.setChecked(getAppRestrictBackground());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700691
692 } else {
693 mAppRestrictView.setVisibility(View.GONE);
694 }
695
696 }
697
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700698 private void setPolicyCycleDay(int cycleDay) {
699 if (LOGD) Log.d(TAG, "setPolicyCycleDay()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700700 mPolicyEditor.setPolicyCycleDay(mTemplate, cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700701 updatePolicy(true);
702 }
703
704 private void setPolicyWarningBytes(long warningBytes) {
705 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700706 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700707 updatePolicy(false);
708 }
709
710 private void setPolicyLimitBytes(long limitBytes) {
711 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700712 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700713 updatePolicy(false);
714 }
715
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700716 private boolean getDataRoaming() {
717 final ContentResolver resolver = getActivity().getContentResolver();
718 return Settings.Secure.getInt(resolver, Settings.Secure.DATA_ROAMING, 0) != 0;
719 }
720
721 private void setDataRoaming(boolean enabled) {
722 // TODO: teach telephony DataConnectionTracker to watch and apply
723 // updates when changed.
724 final ContentResolver resolver = getActivity().getContentResolver();
725 Settings.Secure.putInt(resolver, Settings.Secure.DATA_ROAMING, enabled ? 1 : 0);
726 mMenuDataRoaming.setChecked(enabled);
727 }
728
729 private boolean getRestrictBackground() {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700730 try {
731 return mPolicyService.getRestrictBackground();
732 } catch (RemoteException e) {
733 Log.w(TAG, "problem talking with policy service: " + e);
734 return false;
735 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700736 }
737
738 private void setRestrictBackground(boolean restrictBackground) {
739 if (LOGD) Log.d(TAG, "setRestrictBackground()");
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700740 try {
741 mPolicyService.setRestrictBackground(restrictBackground);
742 mMenuRestrictBackground.setChecked(restrictBackground);
743 } catch (RemoteException e) {
744 Log.w(TAG, "problem talking with policy service: " + e);
745 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700746 }
747
748 private boolean getAppRestrictBackground() {
749 final int uidPolicy;
750 try {
751 uidPolicy = mPolicyService.getUidPolicy(mUid);
752 } catch (RemoteException e) {
753 // since we can't do much without policy, we bail hard.
754 throw new RuntimeException("problem reading network policy", e);
755 }
756
757 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
758 }
759
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700760 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700761 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700762 try {
763 mPolicyService.setUidPolicy(
764 mUid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
765 } catch (RemoteException e) {
766 throw new RuntimeException("unable to save policy", e);
767 }
768
769 mAppRestrict.setChecked(restrictBackground);
770 }
771
Jeff Sharkey8a503642011-06-10 13:31:21 -0700772 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700773 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
774 * current {@link #mTemplate}.
775 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700776 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700777 if (isAppDetailMode()) {
778 mNetworkSwitches.setVisibility(View.GONE);
779 // we fall through to update cycle list for detail mode
780 } else {
781 mNetworkSwitches.setVisibility(View.VISIBLE);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700782
783 // when heading back to summary without cycle refresh, kick details
784 // update to repopulate list.
785 if (!refreshCycle) {
786 updateDetailData();
787 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700788 }
789
Jeff Sharkeya662e492011-06-18 21:57:06 -0700790 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700791
792 // reflect policy limit in checkbox
793 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
794 mChart.bindNetworkPolicy(policy);
795
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700796 if (refreshCycle) {
797 // generate cycle list based on policy and available history
798 updateCycleList(policy);
799 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700800 }
801
802 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700803 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
804 * and available {@link NetworkStatsHistory} data. Always selects the newest
805 * item, updating the inspection range on {@link #mChart}.
806 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700807 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700808 mCycleAdapter.clear();
809
810 final Context context = mCycleSpinner.getContext();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700811 long historyStart = mHistory.getStart();
812 long historyEnd = mHistory.getEnd();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700813
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700814 if (historyStart == Long.MAX_VALUE || historyEnd == Long.MIN_VALUE) {
815 historyStart = System.currentTimeMillis();
816 historyEnd = System.currentTimeMillis();
817 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700818
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700819 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700820 if (policy != null) {
821 // find the next cycle boundary
822 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700823
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700824 int guardCount = 0;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700825
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700826 // walk backwards, generating all valid cycle ranges
827 while (cycleEnd > historyStart) {
828 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
829 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
830 + historyStart);
831 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
832 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700833 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700834
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700835 // TODO: remove this guard once we have better testing
836 if (guardCount++ > 50) {
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700837 Log.wtf(TAG, "stuck generating ranges for historyStart=" + historyStart
838 + ", historyEnd=" + historyEnd + " and policy=" + policy);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700839 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700840 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700841
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700842 // one last cycle entry to modify policy cycle day
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700843 mCycleAdapter.setChangePossible(true);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700844 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700845
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700846 if (!hasCycles) {
847 // no valid cycles; show all data
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700848 // TODO: offer simple ranges like "last week" etc
849 mCycleAdapter.add(new CycleItem(context, historyStart, historyEnd));
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700850 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700851 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700852
853 // force pick the current cycle (first item)
854 mCycleSpinner.setSelection(0);
855 mCycleListener.onItemSelected(mCycleSpinner, null, 0, 0);
856 }
857
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700858 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700859 /** {@inheritDoc} */
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700860 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
861 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700862
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700863 final boolean dataEnabled = isChecked;
864 mDataEnabled.setChecked(dataEnabled);
865
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700866 final String currentTab = mCurrentTab;
867 if (TAB_MOBILE.equals(currentTab)) {
868 mConnService.setMobileDataEnabled(dataEnabled);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700869 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700870 }
871 };
872
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700873 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700874 /** {@inheritDoc} */
875 public void onClick(View v) {
876 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700877 if (disableAtLimit) {
878 // enabling limit; show confirmation dialog which eventually
879 // calls setPolicyLimitBytes() once user confirms.
880 ConfirmLimitFragment.show(DataUsageSummary.this);
881 } else {
882 setPolicyLimitBytes(LIMIT_DISABLED);
883 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700884 }
885 };
886
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700887 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
888 /** {@inheritDoc} */
889 public void onClick(View v) {
890 final boolean restrictBackground = !mAppRestrict.isChecked();
891
892 if (restrictBackground) {
893 // enabling restriction; show confirmation dialog which
894 // eventually calls setRestrictBackground() once user confirms.
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700895 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700896 } else {
897 setAppRestrictBackground(false);
898 }
899 }
900 };
901
902 private OnClickListener mAppSettingsListener = new OnClickListener() {
903 /** {@inheritDoc} */
904 public void onClick(View v) {
905 // TODO: target torwards entire UID instead of just first package
906 startActivity(mAppSettingsIntent);
907 }
908 };
909
Jeff Sharkey8a503642011-06-10 13:31:21 -0700910 private OnItemClickListener mListListener = new OnItemClickListener() {
911 /** {@inheritDoc} */
912 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700913 final AppUsageItem app = (AppUsageItem) parent.getItemAtPosition(position);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700914 AppDetailsFragment.show(DataUsageSummary.this, app.uid);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700915 }
916 };
917
918 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
919 /** {@inheritDoc} */
920 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
921 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
922 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700923 // show cycle editor; will eventually call setPolicyCycleDay()
924 // when user finishes editing.
925 CycleEditorFragment.show(DataUsageSummary.this);
926
927 // reset spinner to something other than "change cycle..."
928 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700929
930 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -0700931 if (LOGD) {
932 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
933 + cycle.end + "]");
934 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700935
936 // update chart to show selected cycle, and update detail data
937 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700938 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700939
940 updateDetailData();
941 }
942 }
943
944 /** {@inheritDoc} */
945 public void onNothingSelected(AdapterView<?> parent) {
946 // ignored
947 }
948 };
949
950 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700951 * Update details based on {@link #mChart} inspection range depending on
952 * current mode. In network mode, updates {@link #mAdapter} with sorted list
953 * of applications data usage, and when {@link #isAppDetailMode()} update
954 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700955 */
956 private void updateDetailData() {
957 if (LOGD) Log.d(TAG, "updateDetailData()");
958
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700959 final long start = mChart.getInspectStart();
960 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700961 final long now = System.currentTimeMillis();
962
963 final Context context = getActivity();
964 final NetworkStatsHistory.Entry entry;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700965
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700966 if (isAppDetailMode()) {
967 if (mDetailHistory != null) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700968 entry = mDetailHistory.getValues(start, end, now, null);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -0700969
970 mAppSubtitle.setText(
971 getString(R.string.data_usage_received_sent,
972 Formatter.formatFileSize(context, entry.rxBytes),
973 Formatter.formatFileSize(context, entry.txBytes)));
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700974 } else {
975 entry = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700976 }
977
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700978 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700979
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700980 } else {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700981 entry = mHistory.getValues(start, end, now, null);
982
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700983 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700984 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700985 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryForAllUid);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700986
987 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700988
989 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
990 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700991 final String rangePhrase = formatDateRange(context, start, end, null);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700992
993 mUsageSummary.setText(
994 getString(R.string.data_usage_total_during_range, totalPhrase, rangePhrase));
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700995 }
996
997 private final LoaderCallbacks<NetworkStats> mSummaryForAllUid = new LoaderCallbacks<
998 NetworkStats>() {
999 /** {@inheritDoc} */
1000 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
1001 return new SummaryForAllUidLoader(getActivity(), mStatsService, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001002 }
1003
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001004 /** {@inheritDoc} */
1005 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
1006 mAdapter.bindStats(data);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001007 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001008 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001009
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001010 /** {@inheritDoc} */
1011 public void onLoaderReset(Loader<NetworkStats> loader) {
1012 mAdapter.bindStats(null);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001013 updateEmptyVisible();
1014 }
1015
1016 private void updateEmptyVisible() {
1017 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1018 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001019 }
1020 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001021
Jeff Sharkeya662e492011-06-18 21:57:06 -07001022 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001023 final Context context = getActivity();
1024 if (hasMobileRadio(context)) {
1025 final String subscriberId = getActiveSubscriberId(context);
1026 return mPolicyEditor.isMobilePolicySplit(subscriberId);
1027 } else {
1028 return false;
1029 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001030 }
1031
1032 private void setMobilePolicySplit(boolean split) {
1033 final String subscriberId = getActiveSubscriberId(getActivity());
1034 mPolicyEditor.setMobilePolicySplit(subscriberId, split);
1035 }
1036
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001037 private static String getActiveSubscriberId(Context context) {
1038 final TelephonyManager telephony = (TelephonyManager) context.getSystemService(
1039 Context.TELEPHONY_SERVICE);
1040 return telephony.getSubscriberId();
1041 }
1042
Jeff Sharkey8a503642011-06-10 13:31:21 -07001043 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
1044 /** {@inheritDoc} */
1045 public void onInspectRangeChanged() {
1046 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
1047 updateDetailData();
1048 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001049
Jeff Sharkey8a503642011-06-10 13:31:21 -07001050 /** {@inheritDoc} */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001051 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001052 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001053 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001054
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001055 /** {@inheritDoc} */
1056 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001057 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001058 }
1059 };
1060
1061
1062 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001063 * List item that reflects a specific data usage cycle.
1064 */
1065 public static class CycleItem {
1066 public CharSequence label;
1067 public long start;
1068 public long end;
1069
Jeff Sharkey8a503642011-06-10 13:31:21 -07001070 CycleItem(CharSequence label) {
1071 this.label = label;
1072 }
1073
1074 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001075 this.label = formatDateRange(context, start, end, Time.TIMEZONE_UTC);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001076 this.start = start;
1077 this.end = end;
1078 }
1079
Jeff Sharkey8a503642011-06-10 13:31:21 -07001080 @Override
1081 public String toString() {
1082 return label.toString();
1083 }
1084 }
1085
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001086 private static final StringBuilder sBuilder = new StringBuilder(50);
1087 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1088 sBuilder, Locale.getDefault());
1089
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001090 private static String formatDateRange(Context context, long start, long end, String timezone) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001091 synchronized (sBuilder) {
1092 int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_MONTH;
1093 if (Time.getJulianDay(start, 0) == Time.getJulianDay(end, 0)) {
1094 // when times are on same day, include time detail
1095 flags |= DateUtils.FORMAT_SHOW_TIME;
1096 }
1097
1098 sBuilder.setLength(0);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001099 return DateUtils
1100 .formatDateRange(context, sFormatter, start, end, flags, timezone).toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001101 }
1102 }
1103
Jeff Sharkey8a503642011-06-10 13:31:21 -07001104 /**
1105 * Special-case data usage cycle that triggers dialog to change
1106 * {@link NetworkPolicy#cycleDay}.
1107 */
1108 public static class CycleChangeItem extends CycleItem {
1109 public CycleChangeItem(Context context) {
1110 super(context.getString(R.string.data_usage_change_cycle));
1111 }
1112 }
1113
1114 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001115 private boolean mChangePossible = false;
1116 private boolean mChangeVisible = false;
1117
1118 private final CycleChangeItem mChangeItem;
1119
Jeff Sharkey8a503642011-06-10 13:31:21 -07001120 public CycleAdapter(Context context) {
1121 super(context, android.R.layout.simple_spinner_item);
1122 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001123 mChangeItem = new CycleChangeItem(context);
1124 }
1125
1126 public void setChangePossible(boolean possible) {
1127 mChangePossible = possible;
1128 updateChange();
1129 }
1130
1131 public void setChangeVisible(boolean visible) {
1132 mChangeVisible = visible;
1133 updateChange();
1134 }
1135
1136 private void updateChange() {
1137 remove(mChangeItem);
1138 if (mChangePossible && mChangeVisible) {
1139 add(mChangeItem);
1140 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001141 }
1142 }
1143
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001144 private static class AppUsageItem implements Comparable<AppUsageItem> {
1145 public int uid;
1146 public long total;
1147
1148 /** {@inheritDoc} */
1149 public int compareTo(AppUsageItem another) {
1150 return Long.compare(another.total, total);
1151 }
1152 }
1153
Jeff Sharkey8a503642011-06-10 13:31:21 -07001154 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001155 * Adapter of applications, sorted by total usage descending.
1156 */
1157 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001158 private ArrayList<AppUsageItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001159 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001160
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001161 /**
1162 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1163 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001164 public void bindStats(NetworkStats stats) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001165 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001166
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001167 if (stats != null) {
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001168 final AppUsageItem systemItem = new AppUsageItem();
1169 systemItem.uid = android.os.Process.SYSTEM_UID;
1170
Jeff Sharkeyebae6592011-07-12 13:53:11 -07001171 NetworkStats.Entry entry = null;
1172 for (int i = 0; i < stats.size(); i++) {
1173 entry = stats.getValues(i, entry);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001174
1175 final boolean isApp = entry.uid >= android.os.Process.FIRST_APPLICATION_UID
1176 && entry.uid <= android.os.Process.LAST_APPLICATION_UID;
1177 if (isApp || entry.uid == TrafficStats.UID_REMOVED) {
1178 final AppUsageItem item = new AppUsageItem();
1179 item.uid = entry.uid;
1180 item.total = entry.rxBytes + entry.txBytes;
1181 mItems.add(item);
1182 } else {
1183 systemItem.total += entry.rxBytes + entry.txBytes;
1184 }
1185 }
1186
1187 if (systemItem.total > 0) {
1188 mItems.add(systemItem);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001189 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001190 }
1191
Jeff Sharkey8a503642011-06-10 13:31:21 -07001192 Collections.sort(mItems);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001193 mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001194 notifyDataSetChanged();
1195 }
1196
1197 @Override
1198 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001199 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001200 }
1201
1202 @Override
1203 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001204 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001205 }
1206
1207 @Override
1208 public long getItemId(int position) {
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001209 return mItems.get(position).uid;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001210 }
1211
1212 @Override
1213 public View getView(int position, View convertView, ViewGroup parent) {
1214 if (convertView == null) {
1215 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey52c3f442011-06-23 00:39:38 -07001216 R.layout.data_usage_item, parent, false);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001217 }
1218
1219 final Context context = parent.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001220
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001221 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
1222 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
1223 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1224 android.R.id.progress);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001225
Jeff Sharkey8a503642011-06-10 13:31:21 -07001226 final AppUsageItem item = mItems.get(position);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001227 title.setText(resolveLabelForUid(context, item.uid));
1228 summary.setText(Formatter.formatFileSize(context, item.total));
1229
1230 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1231 progress.setProgress(percentTotal);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001232
1233 return convertView;
1234 }
1235
1236 }
1237
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001238 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001239 * Empty {@link Fragment} that controls display of UID details in
1240 * {@link DataUsageSummary}.
1241 */
1242 public static class AppDetailsFragment extends Fragment {
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001243 private static final String EXTRA_UID = "uid";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001244
1245 public static void show(DataUsageSummary parent, int uid) {
1246 final Bundle args = new Bundle();
1247 args.putInt(EXTRA_UID, uid);
1248
1249 final AppDetailsFragment fragment = new AppDetailsFragment();
1250 fragment.setArguments(args);
1251 fragment.setTargetFragment(parent, 0);
1252
1253 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1254 ft.add(fragment, TAG_APP_DETAILS);
1255 ft.addToBackStack(TAG_APP_DETAILS);
1256 ft.commit();
1257 }
1258
1259 @Override
1260 public void onStart() {
1261 super.onStart();
1262 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1263 target.mUid = getArguments().getInt(EXTRA_UID);
1264 target.updateBody();
1265 }
1266
1267 @Override
1268 public void onStop() {
1269 super.onStop();
1270 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1271 target.mUid = UID_NONE;
1272 target.updateBody();
1273 }
1274 }
1275
1276 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001277 * Dialog to request user confirmation before setting
1278 * {@link NetworkPolicy#limitBytes}.
1279 */
1280 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001281 private static final String EXTRA_MESSAGE_ID = "messageId";
1282 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001283
1284 public static void show(DataUsageSummary parent) {
1285 final Bundle args = new Bundle();
1286
1287 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001288 final String currentTab = parent.mCurrentTab;
1289 if (TAB_3G.equals(currentTab)) {
1290 args.putInt(EXTRA_MESSAGE_ID, R.string.data_usage_limit_dialog_3g);
1291 args.putLong(EXTRA_LIMIT_BYTES, 5 * GB_IN_BYTES);
1292 } else if (TAB_4G.equals(currentTab)) {
1293 args.putInt(EXTRA_MESSAGE_ID, R.string.data_usage_limit_dialog_4g);
1294 args.putLong(EXTRA_LIMIT_BYTES, 5 * GB_IN_BYTES);
1295 } else if (TAB_MOBILE.equals(currentTab)) {
1296 args.putInt(EXTRA_MESSAGE_ID, R.string.data_usage_limit_dialog_mobile);
1297 args.putLong(EXTRA_LIMIT_BYTES, 5 * GB_IN_BYTES);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001298 }
1299
1300 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1301 dialog.setArguments(args);
1302 dialog.setTargetFragment(parent, 0);
1303 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1304 }
1305
1306 @Override
1307 public Dialog onCreateDialog(Bundle savedInstanceState) {
1308 final Context context = getActivity();
1309
1310 final int messageId = getArguments().getInt(EXTRA_MESSAGE_ID);
1311 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1312
1313 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1314 builder.setTitle(R.string.data_usage_limit_dialog_title);
1315 builder.setMessage(messageId);
1316
1317 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1318 public void onClick(DialogInterface dialog, int which) {
1319 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1320 if (target != null) {
1321 target.setPolicyLimitBytes(limitBytes);
1322 }
1323 }
1324 });
1325
1326 return builder.create();
1327 }
1328 }
1329
1330 /**
1331 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1332 */
1333 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001334 private static final String EXTRA_CYCLE_DAY = "cycleDay";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001335
1336 public static void show(DataUsageSummary parent) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07001337 final NetworkPolicy policy = parent.mPolicyEditor.getPolicy(parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001338 final Bundle args = new Bundle();
1339 args.putInt(CycleEditorFragment.EXTRA_CYCLE_DAY, policy.cycleDay);
1340
1341 final CycleEditorFragment dialog = new CycleEditorFragment();
1342 dialog.setArguments(args);
1343 dialog.setTargetFragment(parent, 0);
1344 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1345 }
1346
1347 @Override
1348 public Dialog onCreateDialog(Bundle savedInstanceState) {
1349 final Context context = getActivity();
1350
1351 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1352 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1353
1354 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1355 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1356
1357 final int oldCycleDay = getArguments().getInt(EXTRA_CYCLE_DAY, 1);
1358
1359 cycleDayPicker.setMinValue(1);
1360 cycleDayPicker.setMaxValue(31);
1361 cycleDayPicker.setValue(oldCycleDay);
1362 cycleDayPicker.setWrapSelectorWheel(true);
1363
1364 builder.setTitle(R.string.data_usage_cycle_editor_title);
1365 builder.setView(view);
1366
1367 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1368 new DialogInterface.OnClickListener() {
1369 public void onClick(DialogInterface dialog, int which) {
1370 final int cycleDay = cycleDayPicker.getValue();
1371 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1372 if (target != null) {
1373 target.setPolicyCycleDay(cycleDay);
1374 }
1375 }
1376 });
1377
1378 return builder.create();
1379 }
1380 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001381
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001382 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001383 * Dialog to request user confirmation before setting
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001384 * {@link Settings.Secure#DATA_ROAMING}.
1385 */
1386 public static class ConfirmDataRoamingFragment extends DialogFragment {
1387 public static void show(DataUsageSummary parent) {
1388 final Bundle args = new Bundle();
1389
1390 final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment();
1391 dialog.setTargetFragment(parent, 0);
1392 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_ROAMING);
1393 }
1394
1395 @Override
1396 public Dialog onCreateDialog(Bundle savedInstanceState) {
1397 final Context context = getActivity();
1398
1399 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1400 builder.setTitle(R.string.roaming_reenable_title);
1401 builder.setMessage(R.string.roaming_warning);
1402
1403 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1404 public void onClick(DialogInterface dialog, int which) {
1405 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1406 if (target != null) {
1407 target.setDataRoaming(true);
1408 }
1409 }
1410 });
1411 builder.setNegativeButton(android.R.string.cancel, null);
1412
1413 return builder.create();
1414 }
1415 }
1416
1417 /**
1418 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001419 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001420 */
1421 public static class ConfirmRestrictFragment extends DialogFragment {
1422 public static void show(DataUsageSummary parent) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001423 final Bundle args = new Bundle();
1424
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001425 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1426 dialog.setTargetFragment(parent, 0);
1427 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1428 }
1429
1430 @Override
1431 public Dialog onCreateDialog(Bundle savedInstanceState) {
1432 final Context context = getActivity();
1433
1434 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001435 builder.setTitle(R.string.data_usage_restrict_background_title);
1436 builder.setMessage(R.string.data_usage_restrict_background);
1437
1438 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1439 public void onClick(DialogInterface dialog, int which) {
1440 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1441 if (target != null) {
1442 target.setRestrictBackground(true);
1443 }
1444 }
1445 });
1446 builder.setNegativeButton(android.R.string.cancel, null);
1447
1448 return builder.create();
1449 }
1450 }
1451
1452 /**
1453 * Dialog to request user confirmation before setting
1454 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
1455 */
1456 public static class ConfirmAppRestrictFragment extends DialogFragment {
1457 public static void show(DataUsageSummary parent) {
1458 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
1459 dialog.setTargetFragment(parent, 0);
1460 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
1461 }
1462
1463 @Override
1464 public Dialog onCreateDialog(Bundle savedInstanceState) {
1465 final Context context = getActivity();
1466
1467 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001468 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
1469 builder.setMessage(R.string.data_usage_app_restrict_dialog);
1470
1471 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1472 public void onClick(DialogInterface dialog, int which) {
1473 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1474 if (target != null) {
1475 target.setAppRestrictBackground(true);
1476 }
1477 }
1478 });
1479 builder.setNegativeButton(android.R.string.cancel, null);
1480
1481 return builder.create();
1482 }
1483 }
1484
1485 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001486 * Compute default tab that should be selected, based on
1487 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
1488 */
1489 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07001490 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
1491 if (template == null) return null;
1492
1493 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07001494 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001495 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001496 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001497 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001498 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001499 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001500 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001501 return TAB_WIFI;
1502 default:
1503 return null;
1504 }
1505 }
1506
1507 /**
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001508 * Resolve best descriptive label for the given UID.
1509 */
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001510 public static CharSequence resolveLabelForUid(Context context, int uid) {
1511 final Resources res = context.getResources();
1512 final PackageManager pm = context.getPackageManager();
1513
1514 // handle special case labels
1515 switch (uid) {
1516 case android.os.Process.SYSTEM_UID:
1517 return res.getText(R.string.process_kernel_label);
1518 case TrafficStats.UID_REMOVED:
1519 return res.getText(R.string.data_usage_uninstalled_apps);
1520 }
1521
1522 // otherwise fall back to using packagemanager labels
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001523 final String[] packageNames = pm.getPackagesForUid(uid);
1524 final int length = packageNames != null ? packageNames.length : 0;
1525
1526 CharSequence label = pm.getNameForUid(uid);
1527 try {
1528 if (length == 1) {
1529 final ApplicationInfo info = pm.getApplicationInfo(packageNames[0], 0);
1530 label = info.loadLabel(pm);
1531 } else if (length > 1) {
1532 for (String packageName : packageNames) {
1533 final PackageInfo info = pm.getPackageInfo(packageName, 0);
1534 if (info.sharedUserLabel != 0) {
1535 label = pm.getText(packageName, info.sharedUserLabel, info.applicationInfo);
1536 if (!TextUtils.isEmpty(label)) {
1537 break;
1538 }
1539 }
1540 }
1541 }
1542 } catch (NameNotFoundException e) {
1543 }
1544
1545 if (TextUtils.isEmpty(label)) {
1546 label = Integer.toString(uid);
1547 }
1548 return label;
1549 }
1550
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001551 /**
1552 * Test if device has a mobile data radio.
1553 */
1554 private static boolean hasMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001555 if (TEST_RADIOS) {
1556 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
1557 }
1558
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001559 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
1560 Context.CONNECTIVITY_SERVICE);
1561
1562 // mobile devices should have MOBILE network tracker regardless of
1563 // connection status.
1564 return conn.getNetworkInfo(TYPE_MOBILE) != null;
1565 }
1566
1567 /**
1568 * Test if device has a mobile 4G data radio.
1569 */
1570 private static boolean hasMobile4gRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001571 if (TEST_RADIOS) {
1572 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
1573 }
1574
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001575 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
1576 Context.CONNECTIVITY_SERVICE);
1577 final TelephonyManager telephony = (TelephonyManager) context.getSystemService(
1578 Context.TELEPHONY_SERVICE);
1579
1580 // WiMAX devices should have WiMAX network tracker regardless of
1581 // connection status.
1582 final boolean hasWimax = conn.getNetworkInfo(TYPE_WIMAX) != null;
1583 final boolean hasLte = telephony.getLteOnCdmaMode() == Phone.LTE_ON_CDMA_TRUE;
1584 return hasWimax || hasLte;
1585 }
1586
1587 /**
1588 * Test if device has a Wi-Fi data radio.
1589 */
1590 private static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001591 if (TEST_RADIOS) {
1592 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
1593 }
1594
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001595 return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI);
1596 }
1597
1598 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001599 * Test if device has an ethernet network connection.
1600 */
1601 private static boolean hasEthernet(Context context) {
1602 if (TEST_RADIOS) {
1603 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
1604 }
1605
1606 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
1607 Context.CONNECTIVITY_SERVICE);
1608 return conn.getNetworkInfo(TYPE_ETHERNET) != null;
1609 }
1610
1611 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001612 * Inflate a {@link Preference} style layout, adding the given {@link View}
1613 * widget into {@link android.R.id#widget_frame}.
1614 */
1615 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
1616 final View view = inflater.inflate(R.layout.preference, root, false);
1617 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
1618 android.R.id.widget_frame);
1619 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
1620 return view;
1621 }
1622
1623 /**
1624 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07001625 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001626 */
1627 private static void setPreferenceTitle(View parent, int resId) {
1628 final TextView title = (TextView) parent.findViewById(android.R.id.title);
1629 title.setText(resId);
1630 }
1631
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001632 /**
1633 * Set {@link android.R.id#summary} for a preference view inflated with
1634 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
1635 */
1636 private static void setPreferenceSummary(View parent, int resId) {
1637 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
1638 summary.setVisibility(View.VISIBLE);
1639 summary.setText(resId);
1640 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001641}