blob: bd79669a952c03f2f515fe0855c899a4a53df774 [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 Sharkey29d56b32011-06-20 17:06:52 -070019import static android.net.ConnectivityManager.TYPE_MOBILE;
20import static android.net.ConnectivityManager.TYPE_WIMAX;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070021import static android.net.NetworkPolicy.LIMIT_DISABLED;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070022import static android.net.NetworkPolicyManager.ACTION_DATA_USAGE_LIMIT;
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 Sharkey29d56b32011-06-20 17:06:52 -070032import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070033
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070034import android.animation.LayoutTransition;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070035import android.app.AlertDialog;
36import android.app.Dialog;
37import android.app.DialogFragment;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070038import android.app.Fragment;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070039import android.app.FragmentTransaction;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070040import android.content.Context;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070041import android.content.DialogInterface;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -070042import android.content.Intent;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070043import android.content.SharedPreferences;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070044import android.content.pm.ApplicationInfo;
45import android.content.pm.PackageInfo;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070046import android.content.pm.PackageManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070047import android.content.pm.PackageManager.NameNotFoundException;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070048import android.net.ConnectivityManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070049import android.net.INetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070050import android.net.INetworkStatsService;
Jeff Sharkey8a503642011-06-10 13:31:21 -070051import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070052import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070053import android.net.NetworkStats;
54import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070055import android.net.NetworkTemplate;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070056import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070057import android.os.Bundle;
58import android.os.RemoteException;
59import android.os.ServiceManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070060import android.preference.Preference;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070061import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070062import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070063import android.text.format.DateUtils;
64import android.text.format.Formatter;
Jeff Sharkey8a503642011-06-10 13:31:21 -070065import android.text.format.Time;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070066import android.util.Log;
67import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -070068import android.view.Menu;
69import android.view.MenuInflater;
70import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070071import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070072import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070073import android.view.ViewGroup;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -070074import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Jeff Sharkey8a503642011-06-10 13:31:21 -070075import android.widget.AdapterView;
76import android.widget.AdapterView.OnItemClickListener;
77import android.widget.AdapterView.OnItemSelectedListener;
78import android.widget.ArrayAdapter;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070079import android.widget.BaseAdapter;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070080import android.widget.Button;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070081import android.widget.CheckBox;
82import android.widget.CompoundButton;
83import android.widget.CompoundButton.OnCheckedChangeListener;
Jeff Sharkey8a503642011-06-10 13:31:21 -070084import android.widget.LinearLayout;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070085import android.widget.ListView;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070086import android.widget.NumberPicker;
Jeff Sharkey8a503642011-06-10 13:31:21 -070087import android.widget.Spinner;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070088import android.widget.Switch;
Jeff Sharkey8a503642011-06-10 13:31:21 -070089import android.widget.TabHost;
90import android.widget.TabHost.OnTabChangeListener;
91import android.widget.TabHost.TabContentFactory;
92import android.widget.TabHost.TabSpec;
93import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070094import android.widget.TextView;
95
Jeff Sharkey29d56b32011-06-20 17:06:52 -070096import com.android.internal.telephony.Phone;
Jeff Sharkeya662e492011-06-18 21:57:06 -070097import com.android.settings.net.NetworkPolicyEditor;
Jeff Sharkey8a503642011-06-10 13:31:21 -070098import com.android.settings.widget.DataUsageChartView;
99import com.android.settings.widget.DataUsageChartView.DataUsageChartListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700100import com.google.android.collect.Lists;
101
102import java.util.ArrayList;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700103import java.util.Arrays;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700104import java.util.Collections;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700105import java.util.Locale;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700106
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700107/**
108 * Panel show data usage history across various networks, including options to
109 * inspect based on usage cycle and control through {@link NetworkPolicy}.
110 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700111public class DataUsageSummary extends Fragment {
112 private static final String TAG = "DataUsage";
Jeff Sharkey8a503642011-06-10 13:31:21 -0700113 private static final boolean LOGD = true;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700114
Jeff Sharkey8a503642011-06-10 13:31:21 -0700115 private static final String TAB_3G = "3g";
116 private static final String TAB_4G = "4g";
117 private static final String TAB_MOBILE = "mobile";
118 private static final String TAB_WIFI = "wifi";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700119
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700120 private static final String TAG_CONFIRM_LIMIT = "confirmLimit";
121 private static final String TAG_CYCLE_EDITOR = "cycleEditor";
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700122 private static final String TAG_POLICY_LIMIT = "policyLimit";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700123 private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
124 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700125
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700126 private static final long KB_IN_BYTES = 1024;
127 private static final long MB_IN_BYTES = KB_IN_BYTES * 1024;
128 private static final long GB_IN_BYTES = MB_IN_BYTES * 1024;
129
130 private INetworkStatsService mStatsService;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700131 private INetworkPolicyManager mPolicyService;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700132 private ConnectivityManager mConnService;
133
134 private static final String PREF_FILE = "data_usage";
135 private static final String PREF_SHOW_WIFI = "show_wifi";
136
137 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700138
Jeff Sharkey8a503642011-06-10 13:31:21 -0700139 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700140 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700141 private TabWidget mTabWidget;
142 private ListView mListView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700143 private DataUsageAdapter mAdapter;
144
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700145 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700146
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700147 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700148 private LinearLayout mNetworkSwitches;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700149 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700150 private View mDataEnabledView;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700151 private CheckBox mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700152 private View mDisableAtLimitView;
153
Jeff Sharkey8a503642011-06-10 13:31:21 -0700154 private Spinner mCycleSpinner;
155 private CycleAdapter mCycleAdapter;
156
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700157 private DataUsageChartView mChart;
158
159 private View mAppDetail;
160 private TextView mAppTitle;
161 private TextView mAppSubtitle;
162 private Button mAppSettings;
163
164 private LinearLayout mAppSwitches;
165 private CheckBox mAppRestrict;
166 private View mAppRestrictView;
167
Jeff Sharkey8a503642011-06-10 13:31:21 -0700168 private boolean mShowWifi = false;
169
Jeff Sharkeya662e492011-06-18 21:57:06 -0700170 private NetworkTemplate mTemplate = null;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700171
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700172 private static final int UID_NONE = -1;
173 private int mUid = UID_NONE;
174
175 private Intent mAppSettingsIntent;
176
Jeff Sharkeya662e492011-06-18 21:57:06 -0700177 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700178
Jeff Sharkey8a503642011-06-10 13:31:21 -0700179 private NetworkStatsHistory mHistory;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700180 private NetworkStatsHistory mDetailHistory;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700181
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700182 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700183 private String mIntentTab = null;
184
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700185 /** Flag used to ignore listeners during binding. */
186 private boolean mBinding;
187
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700188 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700189 public void onCreate(Bundle savedInstanceState) {
190 super.onCreate(savedInstanceState);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700191
192 mStatsService = INetworkStatsService.Stub.asInterface(
193 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700194 mPolicyService = INetworkPolicyManager.Stub.asInterface(
195 ServiceManager.getService(Context.NETWORK_POLICY_SERVICE));
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700196 mConnService = (ConnectivityManager) getActivity().getSystemService(
197 Context.CONNECTIVITY_SERVICE);
198 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700199
Jeff Sharkeya662e492011-06-18 21:57:06 -0700200 mPolicyEditor = new NetworkPolicyEditor(mPolicyService);
201 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700202
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700203 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
204
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700205 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700206 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700207
Jeff Sharkey8a503642011-06-10 13:31:21 -0700208 @Override
209 public View onCreateView(LayoutInflater inflater, ViewGroup container,
210 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700211
Jeff Sharkey8a503642011-06-10 13:31:21 -0700212 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700213 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
214
Jeff Sharkey8a503642011-06-10 13:31:21 -0700215 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700216 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700217 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
218 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700219
Jeff Sharkey8a503642011-06-10 13:31:21 -0700220 mTabHost.setup();
221 mTabHost.setOnTabChangedListener(mTabListener);
222
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700223 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700224 mListView.addHeaderView(mHeader, null, false);
225
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700226 {
227 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700228 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
229 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700230 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700231
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700232 mDataEnabled = new Switch(inflater.getContext());
233 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
234 mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener);
235 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700236
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700237 mDisableAtLimit = new CheckBox(inflater.getContext());
238 mDisableAtLimit.setClickable(false);
239 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
240 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
241 mNetworkSwitches.addView(mDisableAtLimitView);
242 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700243
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700244 // bind cycle dropdown
Jeff Sharkey8a503642011-06-10 13:31:21 -0700245 mCycleSpinner = (Spinner) mHeader.findViewById(R.id.cycles);
246 mCycleAdapter = new CycleAdapter(context);
247 mCycleSpinner.setAdapter(mCycleAdapter);
248 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
249
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700250 mChart = (DataUsageChartView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700251 mChart.setListener(mChartListener);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700252
253 {
254 // bind app detail controls
255 mAppDetail = view.findViewById(R.id.app_detail);
256 mAppTitle = (TextView) view.findViewById(R.id.app_title);
257 mAppSubtitle = (TextView) view.findViewById(R.id.app_subtitle);
258 mAppSwitches = (LinearLayout) view.findViewById(R.id.app_switches);
259
260 mAppSettings = (Button) view.findViewById(R.id.app_settings);
261 mAppSettings.setOnClickListener(mAppSettingsListener);
262
263 mAppRestrict = new CheckBox(inflater.getContext());
264 mAppRestrict.setClickable(false);
265 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
266 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
267 setPreferenceSummary(
268 mAppRestrictView, R.string.data_usage_app_restrict_background_summary);
269 mAppRestrictView.setOnClickListener(mAppRestrictListener);
270 mAppSwitches.addView(mAppRestrictView);
271 }
272
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700273 // only assign layout transitions once first layout is finished
274 mHeader.getViewTreeObserver().addOnGlobalLayoutListener(mFirstLayoutListener);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700275
276 mAdapter = new DataUsageAdapter();
277 mListView.setOnItemClickListener(mListListener);
278 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700279
280 return view;
281 }
282
283 @Override
284 public void onResume() {
285 super.onResume();
286
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700287 // pick default tab based on incoming intent
288 final Intent intent = getActivity().getIntent();
289 mIntentTab = computeTabFromIntent(intent);
290
Jeff Sharkey8a503642011-06-10 13:31:21 -0700291 // this kicks off chain reaction which creates tabs, binds the body to
292 // selected network, and binds chart, cycles and detail list.
293 updateTabs();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700294
295 // template and tab has been selected; show dialog if limit passed
296 final String action = intent.getAction();
297 if (ACTION_DATA_USAGE_LIMIT.equals(action)) {
298 PolicyLimitFragment.show(this);
299 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700300 }
301
Jeff Sharkey8a503642011-06-10 13:31:21 -0700302 @Override
303 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
304 inflater.inflate(R.menu.data_usage, menu);
305 }
306
307 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700308 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700309 final Context context = getActivity();
310
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700311 final MenuItem split4g = menu.findItem(R.id.action_split_4g);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700312 split4g.setVisible(hasMobile4gRadio(context));
Jeff Sharkeya662e492011-06-18 21:57:06 -0700313 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700314
315 final MenuItem showWifi = menu.findItem(R.id.action_show_wifi);
316 showWifi.setVisible(hasMobileRadio(context) && hasWifiRadio(context));
317 showWifi.setChecked(mShowWifi);
318
319 final MenuItem settings = menu.findItem(R.id.action_settings);
320 settings.setVisible(split4g.isVisible() || showWifi.isVisible());
321
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700322 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700323
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700324 @Override
325 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700326 switch (item.getItemId()) {
327 case R.id.action_split_4g: {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700328 final boolean mobileSplit = !item.isChecked();
Jeff Sharkeya662e492011-06-18 21:57:06 -0700329 setMobilePolicySplit(mobileSplit);
330 item.setChecked(isMobilePolicySplit());
Jeff Sharkey8a503642011-06-10 13:31:21 -0700331 updateTabs();
332 return true;
333 }
334 case R.id.action_show_wifi: {
335 mShowWifi = !item.isChecked();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700336 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700337 item.setChecked(mShowWifi);
338 updateTabs();
339 return true;
340 }
341 }
342 return false;
343 }
344
Jeff Sharkey94a90952011-06-13 22:31:09 -0700345 @Override
346 public void onDestroyView() {
347 super.onDestroyView();
348
349 mDataEnabledView = null;
350 mDisableAtLimitView = null;
351 }
352
Jeff Sharkey8a503642011-06-10 13:31:21 -0700353 /**
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700354 * Listener to setup {@link LayoutTransition} after first layout pass.
355 */
356 private OnGlobalLayoutListener mFirstLayoutListener = new OnGlobalLayoutListener() {
357 /** {@inheritDoc} */
358 public void onGlobalLayout() {
359 mHeader.getViewTreeObserver().removeGlobalOnLayoutListener(mFirstLayoutListener);
360
361 mTabsContainer.setLayoutTransition(new LayoutTransition());
362 mHeader.setLayoutTransition(new LayoutTransition());
363 mNetworkSwitchesContainer.setLayoutTransition(new LayoutTransition());
364
365 final LayoutTransition chartTransition = new LayoutTransition();
366 chartTransition.setStartDelay(LayoutTransition.APPEARING, 0);
367 chartTransition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
368 mChart.setLayoutTransition(chartTransition);
369 }
370 };
371
372 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700373 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700374 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
375 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700376 */
377 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700378 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700379 mTabHost.clearAllTabs();
380
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700381 final boolean mobileSplit = isMobilePolicySplit();
382 if (mobileSplit && hasMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700383 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
384 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
385 }
386
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700387 if (mShowWifi && hasWifiRadio(context) && hasMobileRadio(context)) {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700388 if (!mobileSplit) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700389 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
390 }
391 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
392 }
393
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700394 final boolean hasTabs = mTabWidget.getTabCount() > 0;
395 mTabWidget.setVisibility(hasTabs ? View.VISIBLE : View.GONE);
396 if (hasTabs) {
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700397 if (mIntentTab != null) {
398 // select default tab, which will kick off updateBody()
399 mTabHost.setCurrentTabByTag(mIntentTab);
400 } else {
401 // select first tab, which will kick off updateBody()
402 mTabHost.setCurrentTab(0);
403 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700404 } else {
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700405 // no tabs visible; update body manually
Jeff Sharkey8a503642011-06-10 13:31:21 -0700406 updateBody();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700407 }
408 }
409
Jeff Sharkey8a503642011-06-10 13:31:21 -0700410 /**
411 * Factory that provide empty {@link View} to make {@link TabHost} happy.
412 */
413 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
414 /** {@inheritDoc} */
415 public View createTabContent(String tag) {
416 return new View(mTabHost.getContext());
417 }
418 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700419
Jeff Sharkey8a503642011-06-10 13:31:21 -0700420 /**
421 * Build {@link TabSpec} with thin indicator, and empty content.
422 */
423 private TabSpec buildTabSpec(String tag, int titleRes) {
424 final LayoutInflater inflater = LayoutInflater.from(mTabWidget.getContext());
425 final View indicator = inflater.inflate(
426 R.layout.tab_indicator_thin_holo, mTabWidget, false);
427 final TextView title = (TextView) indicator.findViewById(android.R.id.title);
428 title.setText(titleRes);
429 return mTabHost.newTabSpec(tag).setIndicator(indicator).setContent(mEmptyTabContent);
430 }
431
432 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
433 /** {@inheritDoc} */
434 public void onTabChanged(String tabId) {
435 // user changed tab; update body
436 updateBody();
437 }
438 };
439
440 /**
441 * Update body content based on current tab. Loads
442 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
443 * binds them to visible controls.
444 */
445 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700446 mBinding = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700447
Jeff Sharkeya662e492011-06-18 21:57:06 -0700448 final Context context = getActivity();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700449 final String tabTag = mTabHost.getCurrentTabTag();
450
451 final String currentTab;
452 if (tabTag != null) {
453 currentTab = tabTag;
454 } else if (hasMobileRadio(context)) {
455 currentTab = TAB_MOBILE;
456 } else if (hasWifiRadio(context)) {
457 currentTab = TAB_WIFI;
458 } else {
459 throw new IllegalStateException("no mobile or wifi radios");
460 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700461
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700462 final boolean tabChanged = !currentTab.equals(mCurrentTab);
463 mCurrentTab = currentTab;
464
Jeff Sharkey8a503642011-06-10 13:31:21 -0700465 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
466
467 if (TAB_WIFI.equals(currentTab)) {
468 // wifi doesn't have any controls
469 mDataEnabledView.setVisibility(View.GONE);
470 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700471 mTemplate = new NetworkTemplate(MATCH_WIFI, null);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700472
473 } else {
474 // make sure we show for non-wifi
475 mDataEnabledView.setVisibility(View.VISIBLE);
476 mDisableAtLimitView.setVisibility(View.VISIBLE);
477 }
478
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700479 final String subscriberId = getActiveSubscriberId(context);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700480 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700481 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
482 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
483 mDataEnabled.setChecked(mConnService.getMobileDataEnabled());
Jeff Sharkeya662e492011-06-18 21:57:06 -0700484 mTemplate = new NetworkTemplate(MATCH_MOBILE_ALL, subscriberId);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700485
486 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700487 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
488 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
489 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkeya662e492011-06-18 21:57:06 -0700490 mTemplate = new NetworkTemplate(MATCH_MOBILE_3G_LOWER, subscriberId);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700491
492 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700493 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
494 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
495 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkeya662e492011-06-18 21:57:06 -0700496 mTemplate = new NetworkTemplate(MATCH_MOBILE_4G, subscriberId);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700497 }
498
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700499 try {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700500 // load stats for current template
Jeff Sharkey8a503642011-06-10 13:31:21 -0700501 mHistory = mStatsService.getHistoryForNetwork(mTemplate);
502 } catch (RemoteException e) {
503 // since we can't do much without policy or history, and we don't
504 // want to leave with half-baked UI, we bail hard.
505 throw new RuntimeException("problem reading network policy or stats", e);
506 }
507
Jeff Sharkey8a503642011-06-10 13:31:21 -0700508 // bind chart to historical stats
Jeff Sharkey8a503642011-06-10 13:31:21 -0700509 mChart.bindNetworkStats(mHistory);
510
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700511 // only update policy when switching tabs
512 updatePolicy(tabChanged);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700513 updateAppDetail();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700514
515 // force scroll to top of body
516 mListView.smoothScrollToPosition(0);
517
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700518 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700519 }
520
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700521 private boolean isAppDetailMode() {
522 return mUid != UID_NONE;
523 }
524
525 /**
526 * Update UID details panels to match {@link #mUid}, showing or hiding them
527 * depending on {@link #isAppDetailMode()}.
528 */
529 private void updateAppDetail() {
530 if (isAppDetailMode()) {
531 mAppDetail.setVisibility(View.VISIBLE);
532 } else {
533 mAppDetail.setVisibility(View.GONE);
534
535 // hide detail stats when not in detail mode
536 mChart.bindDetailNetworkStats(null);
537 return;
538 }
539
540 // remove warning/limit sweeps while in detail mode
541 mChart.bindNetworkPolicy(null);
542
543 final PackageManager pm = getActivity().getPackageManager();
544 mAppTitle.setText(pm.getNameForUid(mUid));
545
546 // enable settings button when package provides it
547 // TODO: target torwards entire UID instead of just first package
548 final String[] packageNames = pm.getPackagesForUid(mUid);
549 if (packageNames != null && packageNames.length > 0) {
550 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
551 mAppSettingsIntent.setPackage(packageNames[0]);
552 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
553
554 final boolean matchFound = pm.resolveActivity(mAppSettingsIntent, 0) != null;
555 mAppSettings.setEnabled(matchFound);
556
557 } else {
558 mAppSettingsIntent = null;
559 mAppSettings.setEnabled(false);
560 }
561
562 try {
563 // load stats for current uid and template
564 // TODO: read template from extras
565 mDetailHistory = mStatsService.getHistoryForUid(mTemplate, mUid, NetworkStats.TAG_NONE);
566 } catch (RemoteException e) {
567 // since we can't do much without history, and we don't want to
568 // leave with half-baked UI, we bail hard.
569 throw new RuntimeException("problem reading network stats", e);
570 }
571
572 // bind chart to historical stats
573 mChart.bindDetailNetworkStats(mDetailHistory);
574
575 updateDetailData();
576
577 final Context context = getActivity();
578 if (NetworkPolicyManager.isUidValidForPolicy(context, mUid)) {
579 mAppRestrictView.setVisibility(View.VISIBLE);
580
581 final int uidPolicy;
582 try {
583 uidPolicy = mPolicyService.getUidPolicy(mUid);
584 } catch (RemoteException e) {
585 // since we can't do much without policy, we bail hard.
586 throw new RuntimeException("problem reading network policy", e);
587 }
588
589 // update policy checkbox
590 final boolean restrictBackground = (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
591 mAppRestrict.setChecked(restrictBackground);
592
593 } else {
594 mAppRestrictView.setVisibility(View.GONE);
595 }
596
597 }
598
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700599 private void setPolicyCycleDay(int cycleDay) {
600 if (LOGD) Log.d(TAG, "setPolicyCycleDay()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700601 mPolicyEditor.setPolicyCycleDay(mTemplate, cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700602 updatePolicy(true);
603 }
604
605 private void setPolicyWarningBytes(long warningBytes) {
606 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700607 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700608 updatePolicy(false);
609 }
610
611 private void setPolicyLimitBytes(long limitBytes) {
612 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700613 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700614 updatePolicy(false);
615 }
616
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700617 private void setAppRestrictBackground(boolean restrictBackground) {
618 if (LOGD) Log.d(TAG, "setRestrictBackground()");
619 try {
620 mPolicyService.setUidPolicy(
621 mUid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
622 } catch (RemoteException e) {
623 throw new RuntimeException("unable to save policy", e);
624 }
625
626 mAppRestrict.setChecked(restrictBackground);
627 }
628
Jeff Sharkey8a503642011-06-10 13:31:21 -0700629 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700630 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
631 * current {@link #mTemplate}.
632 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700633 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700634 if (isAppDetailMode()) {
635 mNetworkSwitches.setVisibility(View.GONE);
636 // we fall through to update cycle list for detail mode
637 } else {
638 mNetworkSwitches.setVisibility(View.VISIBLE);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700639
640 // when heading back to summary without cycle refresh, kick details
641 // update to repopulate list.
642 if (!refreshCycle) {
643 updateDetailData();
644 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700645 }
646
Jeff Sharkeya662e492011-06-18 21:57:06 -0700647 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700648
649 // reflect policy limit in checkbox
650 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
651 mChart.bindNetworkPolicy(policy);
652
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700653 if (refreshCycle) {
654 // generate cycle list based on policy and available history
655 updateCycleList(policy);
656 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700657 }
658
659 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700660 * Return full time bounds (earliest and latest time recorded) of the given
661 * {@link NetworkStatsHistory}.
662 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700663 public static long[] getHistoryBounds(NetworkStatsHistory history) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700664 final long currentTime = System.currentTimeMillis();
665
666 long start = currentTime;
667 long end = currentTime;
668 if (history.bucketCount > 0) {
669 start = history.bucketStart[0];
670 end = history.bucketStart[history.bucketCount - 1];
671 }
672
673 return new long[] { start, end };
674 }
675
676 /**
677 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
678 * and available {@link NetworkStatsHistory} data. Always selects the newest
679 * item, updating the inspection range on {@link #mChart}.
680 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700681 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700682 mCycleAdapter.clear();
683
684 final Context context = mCycleSpinner.getContext();
685
686 final long[] bounds = getHistoryBounds(mHistory);
687 final long historyStart = bounds[0];
688 final long historyEnd = bounds[1];
689
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700690 if (policy != null) {
691 // find the next cycle boundary
692 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700693
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700694 int guardCount = 0;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700695
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700696 // walk backwards, generating all valid cycle ranges
697 while (cycleEnd > historyStart) {
698 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
699 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
700 + historyStart);
701 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
702 cycleEnd = cycleStart;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700703
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700704 // TODO: remove this guard once we have better testing
705 if (guardCount++ > 50) {
706 Log.wtf(TAG, "stuck generating ranges for bounds=" + Arrays.toString(bounds)
707 + " and policy=" + policy);
708 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700709 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700710
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700711 // one last cycle entry to modify policy cycle day
712 mCycleAdapter.add(new CycleChangeItem(context));
713
714 } else {
715 // no valid cycle; show all data
716 // TODO: offer simple ranges like "last week" etc
717 mCycleAdapter.add(new CycleItem(context, historyStart, historyEnd));
718
719 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700720
721 // force pick the current cycle (first item)
722 mCycleSpinner.setSelection(0);
723 mCycleListener.onItemSelected(mCycleSpinner, null, 0, 0);
724 }
725
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700726 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700727 /** {@inheritDoc} */
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700728 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
729 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700730
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700731 final boolean dataEnabled = isChecked;
732 mDataEnabled.setChecked(dataEnabled);
733
734 switch (mTemplate.getMatchRule()) {
735 case MATCH_MOBILE_ALL: {
736 mConnService.setMobileDataEnabled(dataEnabled);
737 }
738 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700739 }
740 };
741
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700742 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700743 /** {@inheritDoc} */
744 public void onClick(View v) {
745 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700746 if (disableAtLimit) {
747 // enabling limit; show confirmation dialog which eventually
748 // calls setPolicyLimitBytes() once user confirms.
749 ConfirmLimitFragment.show(DataUsageSummary.this);
750 } else {
751 setPolicyLimitBytes(LIMIT_DISABLED);
752 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700753 }
754 };
755
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700756 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
757 /** {@inheritDoc} */
758 public void onClick(View v) {
759 final boolean restrictBackground = !mAppRestrict.isChecked();
760
761 if (restrictBackground) {
762 // enabling restriction; show confirmation dialog which
763 // eventually calls setRestrictBackground() once user confirms.
764 ConfirmRestrictFragment.show(DataUsageSummary.this);
765 } else {
766 setAppRestrictBackground(false);
767 }
768 }
769 };
770
771 private OnClickListener mAppSettingsListener = new OnClickListener() {
772 /** {@inheritDoc} */
773 public void onClick(View v) {
774 // TODO: target torwards entire UID instead of just first package
775 startActivity(mAppSettingsIntent);
776 }
777 };
778
Jeff Sharkey8a503642011-06-10 13:31:21 -0700779 private OnItemClickListener mListListener = new OnItemClickListener() {
780 /** {@inheritDoc} */
781 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700782 final AppUsageItem app = (AppUsageItem) parent.getItemAtPosition(position);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700783 AppDetailsFragment.show(DataUsageSummary.this, app.uid);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700784 }
785 };
786
787 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
788 /** {@inheritDoc} */
789 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
790 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
791 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700792 // show cycle editor; will eventually call setPolicyCycleDay()
793 // when user finishes editing.
794 CycleEditorFragment.show(DataUsageSummary.this);
795
796 // reset spinner to something other than "change cycle..."
797 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700798
799 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -0700800 if (LOGD) {
801 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
802 + cycle.end + "]");
803 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700804
805 // update chart to show selected cycle, and update detail data
806 // to match updated sweep bounds.
807 final long[] bounds = getHistoryBounds(mHistory);
808 mChart.setVisibleRange(cycle.start, cycle.end, bounds[1]);
809
810 updateDetailData();
811 }
812 }
813
814 /** {@inheritDoc} */
815 public void onNothingSelected(AdapterView<?> parent) {
816 // ignored
817 }
818 };
819
820 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700821 * Update details based on {@link #mChart} inspection range depending on
822 * current mode. In network mode, updates {@link #mAdapter} with sorted list
823 * of applications data usage, and when {@link #isAppDetailMode()} update
824 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700825 */
826 private void updateDetailData() {
827 if (LOGD) Log.d(TAG, "updateDetailData()");
828
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700829 if (isAppDetailMode()) {
830 if (mDetailHistory != null) {
831 final Context context = mChart.getContext();
832 final long[] range = mChart.getInspectRange();
833 final long[] total = mDetailHistory.getTotalData(range[0], range[1], null);
834 final long totalCombined = total[0] + total[1];
835 mAppSubtitle.setText(Formatter.formatFileSize(context, totalCombined));
836 }
837
838 // clear any existing app list details
839 mAdapter.bindStats(null);
840
841 return;
842 }
843
844 // otherwise kick off task to update list
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -0700845 new AsyncTask<Void, Void, NetworkStats>() {
846 @Override
847 protected NetworkStats doInBackground(Void... params) {
848 try {
849 final long[] range = mChart.getInspectRange();
Jeff Sharkey827fde32011-06-19 20:56:31 -0700850 return mStatsService.getSummaryForAllUid(mTemplate, range[0], range[1], false);
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -0700851 } catch (RemoteException e) {
852 Log.w(TAG, "problem reading stats");
853 }
854 return null;
855 }
856
857 @Override
858 protected void onPostExecute(NetworkStats stats) {
859 if (stats != null) {
860 mAdapter.bindStats(stats);
861 }
862 }
863 }.execute();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700864 }
865
Jeff Sharkeya662e492011-06-18 21:57:06 -0700866 private boolean isMobilePolicySplit() {
867 final String subscriberId = getActiveSubscriberId(getActivity());
868 return mPolicyEditor.isMobilePolicySplit(subscriberId);
869 }
870
871 private void setMobilePolicySplit(boolean split) {
872 final String subscriberId = getActiveSubscriberId(getActivity());
873 mPolicyEditor.setMobilePolicySplit(subscriberId, split);
874 }
875
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700876 private static String getActiveSubscriberId(Context context) {
877 final TelephonyManager telephony = (TelephonyManager) context.getSystemService(
878 Context.TELEPHONY_SERVICE);
879 return telephony.getSubscriberId();
880 }
881
Jeff Sharkey8a503642011-06-10 13:31:21 -0700882 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
883 /** {@inheritDoc} */
884 public void onInspectRangeChanged() {
885 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
886 updateDetailData();
887 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700888
Jeff Sharkey8a503642011-06-10 13:31:21 -0700889 /** {@inheritDoc} */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700890 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700891 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700892 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700893
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700894 /** {@inheritDoc} */
895 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700896 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700897 }
898 };
899
900
901 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700902 * List item that reflects a specific data usage cycle.
903 */
904 public static class CycleItem {
905 public CharSequence label;
906 public long start;
907 public long end;
908
909 private static final StringBuilder sBuilder = new StringBuilder(50);
910 private static final java.util.Formatter sFormatter = new java.util.Formatter(
911 sBuilder, Locale.getDefault());
912
913 CycleItem(CharSequence label) {
914 this.label = label;
915 }
916
917 public CycleItem(Context context, long start, long end) {
918 this.label = formatDateRangeUtc(context, start, end);
919 this.start = start;
920 this.end = end;
921 }
922
923 private static String formatDateRangeUtc(Context context, long start, long end) {
924 synchronized (sBuilder) {
925 sBuilder.setLength(0);
926 return DateUtils.formatDateRange(context, sFormatter, start, end,
927 DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_MONTH,
928 Time.TIMEZONE_UTC).toString();
929 }
930 }
931
932 @Override
933 public String toString() {
934 return label.toString();
935 }
936 }
937
938 /**
939 * Special-case data usage cycle that triggers dialog to change
940 * {@link NetworkPolicy#cycleDay}.
941 */
942 public static class CycleChangeItem extends CycleItem {
943 public CycleChangeItem(Context context) {
944 super(context.getString(R.string.data_usage_change_cycle));
945 }
946 }
947
948 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
949 public CycleAdapter(Context context) {
950 super(context, android.R.layout.simple_spinner_item);
951 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
952 }
953 }
954
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700955 private static class AppUsageItem implements Comparable<AppUsageItem> {
956 public int uid;
957 public long total;
958
959 /** {@inheritDoc} */
960 public int compareTo(AppUsageItem another) {
961 return Long.compare(another.total, total);
962 }
963 }
964
Jeff Sharkey8a503642011-06-10 13:31:21 -0700965 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700966 * Adapter of applications, sorted by total usage descending.
967 */
968 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700969 private ArrayList<AppUsageItem> mItems = Lists.newArrayList();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700970
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700971 /**
972 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
973 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700974 public void bindStats(NetworkStats stats) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700975 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700976
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700977 if (stats != null) {
978 for (int i = 0; i < stats.size; i++) {
979 final long total = stats.rx[i] + stats.tx[i];
980 final AppUsageItem item = new AppUsageItem();
981 item.uid = stats.uid[i];
982 item.total = total;
983 mItems.add(item);
984 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700985 }
986
Jeff Sharkey8a503642011-06-10 13:31:21 -0700987 Collections.sort(mItems);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700988 notifyDataSetChanged();
989 }
990
991 @Override
992 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700993 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700994 }
995
996 @Override
997 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700998 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700999 }
1000
1001 @Override
1002 public long getItemId(int position) {
1003 return position;
1004 }
1005
1006 @Override
1007 public View getView(int position, View convertView, ViewGroup parent) {
1008 if (convertView == null) {
1009 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey52c3f442011-06-23 00:39:38 -07001010 R.layout.data_usage_item, parent, false);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001011 }
1012
1013 final Context context = parent.getContext();
1014 final PackageManager pm = context.getPackageManager();
1015
1016 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
1017 final TextView text2 = (TextView) convertView.findViewById(android.R.id.text2);
1018
Jeff Sharkey8a503642011-06-10 13:31:21 -07001019 final AppUsageItem item = mItems.get(position);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001020 text1.setText(resolveLabelForUid(pm, item.uid));
Jeff Sharkey8a503642011-06-10 13:31:21 -07001021 text2.setText(Formatter.formatFileSize(context, item.total));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001022
1023 return convertView;
1024 }
1025
1026 }
1027
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001028 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001029 * Empty {@link Fragment} that controls display of UID details in
1030 * {@link DataUsageSummary}.
1031 */
1032 public static class AppDetailsFragment extends Fragment {
1033 public static final String EXTRA_UID = "uid";
1034
1035 public static void show(DataUsageSummary parent, int uid) {
1036 final Bundle args = new Bundle();
1037 args.putInt(EXTRA_UID, uid);
1038
1039 final AppDetailsFragment fragment = new AppDetailsFragment();
1040 fragment.setArguments(args);
1041 fragment.setTargetFragment(parent, 0);
1042
1043 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1044 ft.add(fragment, TAG_APP_DETAILS);
1045 ft.addToBackStack(TAG_APP_DETAILS);
1046 ft.commit();
1047 }
1048
1049 @Override
1050 public void onStart() {
1051 super.onStart();
1052 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1053 target.mUid = getArguments().getInt(EXTRA_UID);
1054 target.updateBody();
1055 }
1056
1057 @Override
1058 public void onStop() {
1059 super.onStop();
1060 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1061 target.mUid = UID_NONE;
1062 target.updateBody();
1063 }
1064 }
1065
1066 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001067 * Dialog to request user confirmation before setting
1068 * {@link NetworkPolicy#limitBytes}.
1069 */
1070 public static class ConfirmLimitFragment extends DialogFragment {
1071 public static final String EXTRA_MESSAGE_ID = "messageId";
1072 public static final String EXTRA_LIMIT_BYTES = "limitBytes";
1073
1074 public static void show(DataUsageSummary parent) {
1075 final Bundle args = new Bundle();
1076
1077 // TODO: customize default limits based on network template
Jeff Sharkeya662e492011-06-18 21:57:06 -07001078 switch (parent.mTemplate.getMatchRule()) {
1079 case MATCH_MOBILE_3G_LOWER: {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001080 args.putInt(EXTRA_MESSAGE_ID, R.string.data_usage_limit_dialog_3g);
1081 args.putLong(EXTRA_LIMIT_BYTES, 5 * GB_IN_BYTES);
1082 break;
1083 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001084 case MATCH_MOBILE_4G: {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001085 args.putInt(EXTRA_MESSAGE_ID, R.string.data_usage_limit_dialog_4g);
1086 args.putLong(EXTRA_LIMIT_BYTES, 5 * GB_IN_BYTES);
1087 break;
1088 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001089 case MATCH_MOBILE_ALL: {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001090 args.putInt(EXTRA_MESSAGE_ID, R.string.data_usage_limit_dialog_mobile);
1091 args.putLong(EXTRA_LIMIT_BYTES, 5 * GB_IN_BYTES);
1092 break;
1093 }
1094 }
1095
1096 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1097 dialog.setArguments(args);
1098 dialog.setTargetFragment(parent, 0);
1099 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1100 }
1101
1102 @Override
1103 public Dialog onCreateDialog(Bundle savedInstanceState) {
1104 final Context context = getActivity();
1105
1106 final int messageId = getArguments().getInt(EXTRA_MESSAGE_ID);
1107 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1108
1109 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1110 builder.setTitle(R.string.data_usage_limit_dialog_title);
1111 builder.setMessage(messageId);
1112
1113 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1114 public void onClick(DialogInterface dialog, int which) {
1115 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1116 if (target != null) {
1117 target.setPolicyLimitBytes(limitBytes);
1118 }
1119 }
1120 });
1121
1122 return builder.create();
1123 }
1124 }
1125
1126 /**
1127 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1128 */
1129 public static class CycleEditorFragment extends DialogFragment {
1130 public static final String EXTRA_CYCLE_DAY = "cycleDay";
1131
1132 public static void show(DataUsageSummary parent) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07001133 final NetworkPolicy policy = parent.mPolicyEditor.getPolicy(parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001134 final Bundle args = new Bundle();
1135 args.putInt(CycleEditorFragment.EXTRA_CYCLE_DAY, policy.cycleDay);
1136
1137 final CycleEditorFragment dialog = new CycleEditorFragment();
1138 dialog.setArguments(args);
1139 dialog.setTargetFragment(parent, 0);
1140 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1141 }
1142
1143 @Override
1144 public Dialog onCreateDialog(Bundle savedInstanceState) {
1145 final Context context = getActivity();
1146
1147 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1148 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1149
1150 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1151 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1152
1153 final int oldCycleDay = getArguments().getInt(EXTRA_CYCLE_DAY, 1);
1154
1155 cycleDayPicker.setMinValue(1);
1156 cycleDayPicker.setMaxValue(31);
1157 cycleDayPicker.setValue(oldCycleDay);
1158 cycleDayPicker.setWrapSelectorWheel(true);
1159
1160 builder.setTitle(R.string.data_usage_cycle_editor_title);
1161 builder.setView(view);
1162
1163 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1164 new DialogInterface.OnClickListener() {
1165 public void onClick(DialogInterface dialog, int which) {
1166 final int cycleDay = cycleDayPicker.getValue();
1167 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1168 if (target != null) {
1169 target.setPolicyCycleDay(cycleDay);
1170 }
1171 }
1172 });
1173
1174 return builder.create();
1175 }
1176 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001177
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001178 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001179 * Dialog explaining that {@link NetworkPolicy#limitBytes} has been passed,
1180 * and giving the user an option to bypass.
1181 */
1182 public static class PolicyLimitFragment extends DialogFragment {
1183 public static final String EXTRA_TITLE_ID = "titleId";
1184
1185 public static void show(DataUsageSummary parent) {
1186 final Bundle args = new Bundle();
1187
Jeff Sharkeya662e492011-06-18 21:57:06 -07001188 switch (parent.mTemplate.getMatchRule()) {
1189 case MATCH_MOBILE_3G_LOWER: {
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001190 args.putInt(EXTRA_TITLE_ID, R.string.data_usage_disabled_dialog_3g_title);
1191 break;
1192 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001193 case MATCH_MOBILE_4G: {
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001194 args.putInt(EXTRA_TITLE_ID, R.string.data_usage_disabled_dialog_4g_title);
1195 break;
1196 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001197 case MATCH_MOBILE_ALL: {
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001198 args.putInt(EXTRA_TITLE_ID, R.string.data_usage_disabled_dialog_mobile_title);
1199 break;
1200 }
1201 }
1202
1203 final PolicyLimitFragment dialog = new PolicyLimitFragment();
1204 dialog.setArguments(args);
1205 dialog.setTargetFragment(parent, 0);
1206 dialog.show(parent.getFragmentManager(), TAG_POLICY_LIMIT);
1207 }
1208
1209 @Override
1210 public Dialog onCreateDialog(Bundle savedInstanceState) {
1211 final Context context = getActivity();
1212
1213 final int titleId = getArguments().getInt(EXTRA_TITLE_ID);
1214
1215 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1216 builder.setTitle(titleId);
1217 builder.setMessage(R.string.data_usage_disabled_dialog);
1218
1219 builder.setPositiveButton(android.R.string.ok, null);
1220 builder.setNegativeButton(R.string.data_usage_disabled_dialog_enable,
1221 new DialogInterface.OnClickListener() {
1222 public void onClick(DialogInterface dialog, int which) {
1223 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1224 if (target != null) {
1225 // TODO: consider "allow 100mb more data", or
1226 // only bypass limit for current cycle.
1227 target.setPolicyLimitBytes(LIMIT_DISABLED);
1228 }
1229 }
1230 });
1231
1232 return builder.create();
1233 }
1234 }
1235
1236 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001237 * Dialog to request user confirmation before setting
1238 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
1239 */
1240 public static class ConfirmRestrictFragment extends DialogFragment {
1241 public static void show(DataUsageSummary parent) {
1242 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1243 dialog.setTargetFragment(parent, 0);
1244 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1245 }
1246
1247 @Override
1248 public Dialog onCreateDialog(Bundle savedInstanceState) {
1249 final Context context = getActivity();
1250
1251 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1252 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
1253 builder.setMessage(R.string.data_usage_app_restrict_dialog);
1254
1255 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1256 public void onClick(DialogInterface dialog, int which) {
1257 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1258 if (target != null) {
1259 target.setAppRestrictBackground(true);
1260 }
1261 }
1262 });
1263 builder.setNegativeButton(android.R.string.cancel, null);
1264
1265 return builder.create();
1266 }
1267 }
1268
1269 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001270 * Compute default tab that should be selected, based on
1271 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
1272 */
1273 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001274 final int networkTemplate = intent.getIntExtra(EXTRA_NETWORK_TEMPLATE, MATCH_MOBILE_ALL);
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001275 switch (networkTemplate) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07001276 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001277 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001278 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001279 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001280 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001281 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001282 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001283 return TAB_WIFI;
1284 default:
1285 return null;
1286 }
1287 }
1288
1289 /**
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001290 * Resolve best descriptive label for the given UID.
1291 */
1292 public static CharSequence resolveLabelForUid(PackageManager pm, int uid) {
1293 final String[] packageNames = pm.getPackagesForUid(uid);
1294 final int length = packageNames != null ? packageNames.length : 0;
1295
1296 CharSequence label = pm.getNameForUid(uid);
1297 try {
1298 if (length == 1) {
1299 final ApplicationInfo info = pm.getApplicationInfo(packageNames[0], 0);
1300 label = info.loadLabel(pm);
1301 } else if (length > 1) {
1302 for (String packageName : packageNames) {
1303 final PackageInfo info = pm.getPackageInfo(packageName, 0);
1304 if (info.sharedUserLabel != 0) {
1305 label = pm.getText(packageName, info.sharedUserLabel, info.applicationInfo);
1306 if (!TextUtils.isEmpty(label)) {
1307 break;
1308 }
1309 }
1310 }
1311 }
1312 } catch (NameNotFoundException e) {
1313 }
1314
1315 if (TextUtils.isEmpty(label)) {
1316 label = Integer.toString(uid);
1317 }
1318 return label;
1319 }
1320
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001321 /**
1322 * Test if device has a mobile data radio.
1323 */
1324 private static boolean hasMobileRadio(Context context) {
1325 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
1326 Context.CONNECTIVITY_SERVICE);
1327
1328 // mobile devices should have MOBILE network tracker regardless of
1329 // connection status.
1330 return conn.getNetworkInfo(TYPE_MOBILE) != null;
1331 }
1332
1333 /**
1334 * Test if device has a mobile 4G data radio.
1335 */
1336 private static boolean hasMobile4gRadio(Context context) {
1337 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
1338 Context.CONNECTIVITY_SERVICE);
1339 final TelephonyManager telephony = (TelephonyManager) context.getSystemService(
1340 Context.TELEPHONY_SERVICE);
1341
1342 // WiMAX devices should have WiMAX network tracker regardless of
1343 // connection status.
1344 final boolean hasWimax = conn.getNetworkInfo(TYPE_WIMAX) != null;
1345 final boolean hasLte = telephony.getLteOnCdmaMode() == Phone.LTE_ON_CDMA_TRUE;
1346 return hasWimax || hasLte;
1347 }
1348
1349 /**
1350 * Test if device has a Wi-Fi data radio.
1351 */
1352 private static boolean hasWifiRadio(Context context) {
1353 return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI);
1354 }
1355
1356 /**
1357 * Inflate a {@link Preference} style layout, adding the given {@link View}
1358 * widget into {@link android.R.id#widget_frame}.
1359 */
1360 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
1361 final View view = inflater.inflate(R.layout.preference, root, false);
1362 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
1363 android.R.id.widget_frame);
1364 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
1365 return view;
1366 }
1367
1368 /**
1369 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07001370 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001371 */
1372 private static void setPreferenceTitle(View parent, int resId) {
1373 final TextView title = (TextView) parent.findViewById(android.R.id.title);
1374 title.setText(resId);
1375 }
1376
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001377 /**
1378 * Set {@link android.R.id#summary} for a preference view inflated with
1379 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
1380 */
1381 private static void setPreferenceSummary(View parent, int resId) {
1382 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
1383 summary.setVisibility(View.VISIBLE);
1384 summary.setText(resId);
1385 }
1386
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001387}