blob: bccc5a5c2dd3731559947188988aacb8eaa72d7e [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 Sharkey1ae43f92011-08-03 17:16:09 -070028import static android.net.NetworkStats.TAG_NONE;
29import static android.net.NetworkStatsHistory.FIELD_RX_BYTES;
30import static android.net.NetworkStatsHistory.FIELD_TX_BYTES;
Jeff Sharkeya662e492011-06-18 21:57:06 -070031import static android.net.NetworkTemplate.MATCH_MOBILE_3G_LOWER;
32import static android.net.NetworkTemplate.MATCH_MOBILE_4G;
33import static android.net.NetworkTemplate.MATCH_MOBILE_ALL;
34import static android.net.NetworkTemplate.MATCH_WIFI;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070035import static android.net.NetworkTemplate.buildTemplateEthernet;
36import static android.net.NetworkTemplate.buildTemplateMobile3gLower;
37import static android.net.NetworkTemplate.buildTemplateMobile4g;
38import static android.net.NetworkTemplate.buildTemplateMobileAll;
39import static android.net.NetworkTemplate.buildTemplateWifi;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070040import static android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
41import static android.text.format.DateUtils.FORMAT_SHOW_DATE;
42import static android.text.format.Time.TIMEZONE_UTC;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070043import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070044
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070045import android.animation.LayoutTransition;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070046import android.app.AlertDialog;
47import android.app.Dialog;
48import android.app.DialogFragment;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070049import android.app.Fragment;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070050import android.app.FragmentTransaction;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070051import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070052import android.content.ContentResolver;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070053import android.content.Context;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070054import android.content.DialogInterface;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -070055import android.content.Intent;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070056import android.content.Loader;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070057import android.content.SharedPreferences;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070058import android.content.pm.ApplicationInfo;
59import android.content.pm.PackageInfo;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070060import android.content.pm.PackageManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070061import android.content.pm.PackageManager.NameNotFoundException;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070062import android.content.res.Resources;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070063import android.graphics.drawable.Drawable;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070064import android.net.ConnectivityManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070065import android.net.INetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070066import android.net.INetworkStatsService;
Jeff Sharkey8a503642011-06-10 13:31:21 -070067import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070068import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070069import android.net.NetworkStats;
70import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070071import android.net.NetworkTemplate;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070072import android.net.TrafficStats;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070073import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070074import android.os.Bundle;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070075import android.os.INetworkManagementService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070076import android.os.RemoteException;
77import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070078import android.os.SystemProperties;
Jeff Sharkey8a503642011-06-10 13:31:21 -070079import android.preference.Preference;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070080import android.provider.Settings;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070081import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070082import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070083import android.text.format.DateUtils;
84import android.text.format.Formatter;
85import android.util.Log;
86import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -070087import android.view.Menu;
88import android.view.MenuInflater;
89import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070090import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070091import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070092import android.view.ViewGroup;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -070093import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Jeff Sharkey8a503642011-06-10 13:31:21 -070094import android.widget.AdapterView;
95import android.widget.AdapterView.OnItemClickListener;
96import android.widget.AdapterView.OnItemSelectedListener;
97import android.widget.ArrayAdapter;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070098import android.widget.BaseAdapter;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070099import android.widget.Button;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700100import android.widget.CheckBox;
101import android.widget.CompoundButton;
102import android.widget.CompoundButton.OnCheckedChangeListener;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700103import android.widget.ImageView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700104import android.widget.LinearLayout;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700105import android.widget.ListView;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700106import android.widget.NumberPicker;
Jeff Sharkey2412b0f2011-07-17 20:31:40 -0700107import android.widget.ProgressBar;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700108import android.widget.Spinner;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700109import android.widget.Switch;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700110import android.widget.TabHost;
111import android.widget.TabHost.OnTabChangeListener;
112import android.widget.TabHost.TabContentFactory;
113import android.widget.TabHost.TabSpec;
114import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700115import android.widget.TextView;
116
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700117import com.android.internal.telephony.Phone;
Jeff Sharkeya662e492011-06-18 21:57:06 -0700118import com.android.settings.net.NetworkPolicyEditor;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700119import com.android.settings.net.SummaryForAllUidLoader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700120import com.android.settings.widget.DataUsageChartView;
121import com.android.settings.widget.DataUsageChartView.DataUsageChartListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700122import com.google.android.collect.Lists;
123
124import java.util.ArrayList;
125import java.util.Collections;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700126import java.util.Locale;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700127
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700128import libcore.util.Objects;
129
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700130/**
131 * Panel show data usage history across various networks, including options to
132 * inspect based on usage cycle and control through {@link NetworkPolicy}.
133 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700134public class DataUsageSummary extends Fragment {
135 private static final String TAG = "DataUsage";
Jeff Sharkey8a503642011-06-10 13:31:21 -0700136 private static final boolean LOGD = true;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700137
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700138 // TODO: remove this testing code
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700139 private static final boolean TEST_ANIM = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700140 private static final boolean TEST_RADIOS = false;
141 private static final String TEST_RADIOS_PROP = "test.radios";
142
Jeff Sharkey8a503642011-06-10 13:31:21 -0700143 private static final String TAB_3G = "3g";
144 private static final String TAB_4G = "4g";
145 private static final String TAB_MOBILE = "mobile";
146 private static final String TAB_WIFI = "wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700147 private static final String TAB_ETHERNET = "ethernet";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700148
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700149 private static final String TAG_CONFIRM_ROAMING = "confirmRoaming";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700150 private static final String TAG_CONFIRM_LIMIT = "confirmLimit";
151 private static final String TAG_CYCLE_EDITOR = "cycleEditor";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700152 private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700153 private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700154 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700155
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700156 private static final int LOADER_SUMMARY = 2;
157
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700158 private static final long KB_IN_BYTES = 1024;
159 private static final long MB_IN_BYTES = KB_IN_BYTES * 1024;
160 private static final long GB_IN_BYTES = MB_IN_BYTES * 1024;
161
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700162 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700163 private INetworkStatsService mStatsService;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700164 private INetworkPolicyManager mPolicyService;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700165 private ConnectivityManager mConnService;
166
167 private static final String PREF_FILE = "data_usage";
168 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700169 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700170
171 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700172
Jeff Sharkey8a503642011-06-10 13:31:21 -0700173 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700174 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700175 private TabWidget mTabWidget;
176 private ListView mListView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700177 private DataUsageAdapter mAdapter;
178
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700179 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700180
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700181 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700182 private LinearLayout mNetworkSwitches;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700183 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700184 private View mDataEnabledView;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700185 private CheckBox mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700186 private View mDisableAtLimitView;
187
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700188 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700189 private Spinner mCycleSpinner;
190 private CycleAdapter mCycleAdapter;
191
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700192 private DataUsageChartView mChart;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700193 private TextView mUsageSummary;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700194 private TextView mEmpty;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700195
196 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700197 private ImageView mAppIcon;
198 private ViewGroup mAppTitles;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700199 private Button mAppSettings;
200
201 private LinearLayout mAppSwitches;
202 private CheckBox mAppRestrict;
203 private View mAppRestrictView;
204
Jeff Sharkey8a503642011-06-10 13:31:21 -0700205 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700206 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700207
Jeff Sharkeya662e492011-06-18 21:57:06 -0700208 private NetworkTemplate mTemplate = null;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700209
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700210 private static final int UID_NONE = -1;
211 private int mUid = UID_NONE;
212
213 private Intent mAppSettingsIntent;
214
Jeff Sharkeya662e492011-06-18 21:57:06 -0700215 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700216
Jeff Sharkey8a503642011-06-10 13:31:21 -0700217 private NetworkStatsHistory mHistory;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700218 private NetworkStatsHistory mDetailHistory;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700219
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700220 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700221 private String mIntentTab = null;
222
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700223 private MenuItem mMenuDataRoaming;
224 private MenuItem mMenuRestrictBackground;
225
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700226 /** Flag used to ignore listeners during binding. */
227 private boolean mBinding;
228
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700229 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700230 public void onCreate(Bundle savedInstanceState) {
231 super.onCreate(savedInstanceState);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700232
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700233 mNetworkService = INetworkManagementService.Stub.asInterface(
234 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700235 mStatsService = INetworkStatsService.Stub.asInterface(
236 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700237 mPolicyService = INetworkPolicyManager.Stub.asInterface(
238 ServiceManager.getService(Context.NETWORK_POLICY_SERVICE));
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700239 mConnService = (ConnectivityManager) getActivity().getSystemService(
240 Context.CONNECTIVITY_SERVICE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700241
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700242 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700243
Jeff Sharkeya662e492011-06-18 21:57:06 -0700244 mPolicyEditor = new NetworkPolicyEditor(mPolicyService);
245 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700246
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700247 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700248 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700249
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700250 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700251 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700252
Jeff Sharkey8a503642011-06-10 13:31:21 -0700253 @Override
254 public View onCreateView(LayoutInflater inflater, ViewGroup container,
255 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700256
Jeff Sharkey8a503642011-06-10 13:31:21 -0700257 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700258 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
259
Jeff Sharkey8a503642011-06-10 13:31:21 -0700260 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700261 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700262 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
263 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700264
Jeff Sharkey8a503642011-06-10 13:31:21 -0700265 mTabHost.setup();
266 mTabHost.setOnTabChangedListener(mTabListener);
267
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700268 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700269 mListView.addHeaderView(mHeader, null, false);
270
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700271 {
272 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700273 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
274 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700275 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700276
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700277 mDataEnabled = new Switch(inflater.getContext());
278 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
279 mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener);
280 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700281
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700282 mDisableAtLimit = new CheckBox(inflater.getContext());
283 mDisableAtLimit.setClickable(false);
284 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
285 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
286 mNetworkSwitches.addView(mDisableAtLimitView);
287 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700288
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700289 // bind cycle dropdown
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700290 mCycleView = mHeader.findViewById(R.id.cycles);
291 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700292 mCycleAdapter = new CycleAdapter(context);
293 mCycleSpinner.setAdapter(mCycleAdapter);
294 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
295
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700296 mChart = (DataUsageChartView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700297 mChart.setListener(mChartListener);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700298
299 {
300 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700301 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700302 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
303 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700304 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700305
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700306 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700307 mAppSettings.setOnClickListener(mAppSettingsListener);
308
309 mAppRestrict = new CheckBox(inflater.getContext());
310 mAppRestrict.setClickable(false);
311 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
312 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
313 setPreferenceSummary(
314 mAppRestrictView, R.string.data_usage_app_restrict_background_summary);
315 mAppRestrictView.setOnClickListener(mAppRestrictListener);
316 mAppSwitches.addView(mAppRestrictView);
317 }
318
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700319 mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700320 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700321
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700322 // only assign layout transitions once first layout is finished
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700323 mListView.getViewTreeObserver().addOnGlobalLayoutListener(mFirstLayoutListener);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700324
325 mAdapter = new DataUsageAdapter();
326 mListView.setOnItemClickListener(mListListener);
327 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700328
329 return view;
330 }
331
332 @Override
333 public void onResume() {
334 super.onResume();
335
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700336 // pick default tab based on incoming intent
337 final Intent intent = getActivity().getIntent();
338 mIntentTab = computeTabFromIntent(intent);
339
Jeff Sharkey8a503642011-06-10 13:31:21 -0700340 // this kicks off chain reaction which creates tabs, binds the body to
341 // selected network, and binds chart, cycles and detail list.
342 updateTabs();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700343
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700344 // kick off background task to update stats
345 new AsyncTask<Void, Void, Void>() {
346 @Override
347 protected Void doInBackground(Void... params) {
348 try {
349 mStatsService.forceUpdate();
350 } catch (RemoteException e) {
351 }
352 return null;
353 }
354
355 @Override
356 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700357 if (isAdded()) {
358 updateBody();
359 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700360 }
361 }.execute();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700362 }
363
Jeff Sharkey8a503642011-06-10 13:31:21 -0700364 @Override
365 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
366 inflater.inflate(R.menu.data_usage, menu);
367 }
368
369 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700370 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700371 final Context context = getActivity();
372
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700373 mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming);
374 mMenuDataRoaming.setVisible(hasMobileRadio(context));
375 mMenuDataRoaming.setChecked(getDataRoaming());
376
377 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
378 mMenuRestrictBackground.setChecked(getRestrictBackground());
379
380 final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700381 split4g.setVisible(hasMobile4gRadio(context));
Jeff Sharkeya662e492011-06-18 21:57:06 -0700382 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700383
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700384 final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700385 if (hasWifiRadio(context) && hasMobileRadio(context)) {
386 showWifi.setVisible(true);
387 showWifi.setChecked(mShowWifi);
388 } else {
389 showWifi.setVisible(false);
390 mShowWifi = true;
391 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700392
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700393 final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
394 if (hasEthernet(context) && hasMobileRadio(context)) {
395 showEthernet.setVisible(true);
396 showEthernet.setChecked(mShowEthernet);
397 } else {
398 showEthernet.setVisible(false);
399 mShowEthernet = true;
400 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700401 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700402
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700403 @Override
404 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700405 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700406 case R.id.data_usage_menu_roaming: {
407 final boolean dataRoaming = !item.isChecked();
408 if (dataRoaming) {
409 ConfirmDataRoamingFragment.show(this);
410 } else {
411 // no confirmation to disable roaming
412 setDataRoaming(false);
413 }
414 return true;
415 }
416 case R.id.data_usage_menu_restrict_background: {
417 final boolean restrictBackground = !item.isChecked();
418 if (restrictBackground) {
419 ConfirmRestrictFragment.show(this);
420 } else {
421 // no confirmation to drop restriction
422 setRestrictBackground(false);
423 }
424 return true;
425 }
426 case R.id.data_usage_menu_split_4g: {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700427 final boolean mobileSplit = !item.isChecked();
Jeff Sharkeya662e492011-06-18 21:57:06 -0700428 setMobilePolicySplit(mobileSplit);
429 item.setChecked(isMobilePolicySplit());
Jeff Sharkey8a503642011-06-10 13:31:21 -0700430 updateTabs();
431 return true;
432 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700433 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700434 mShowWifi = !item.isChecked();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700435 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700436 item.setChecked(mShowWifi);
437 updateTabs();
438 return true;
439 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700440 case R.id.data_usage_menu_show_ethernet: {
441 mShowEthernet = !item.isChecked();
442 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
443 item.setChecked(mShowEthernet);
444 updateTabs();
445 return true;
446 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700447 }
448 return false;
449 }
450
Jeff Sharkey94a90952011-06-13 22:31:09 -0700451 @Override
452 public void onDestroyView() {
453 super.onDestroyView();
454
455 mDataEnabledView = null;
456 mDisableAtLimitView = null;
457 }
458
Jeff Sharkey8a503642011-06-10 13:31:21 -0700459 /**
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700460 * Listener to setup {@link LayoutTransition} after first layout pass.
461 */
462 private OnGlobalLayoutListener mFirstLayoutListener = new OnGlobalLayoutListener() {
463 /** {@inheritDoc} */
464 public void onGlobalLayout() {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700465 mListView.getViewTreeObserver().removeGlobalOnLayoutListener(mFirstLayoutListener);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700466
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700467 mTabsContainer.setLayoutTransition(buildLayoutTransition());
468 mHeader.setLayoutTransition(buildLayoutTransition());
469 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700470
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700471 final LayoutTransition chartTransition = buildLayoutTransition();
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700472 chartTransition.setStartDelay(LayoutTransition.APPEARING, 0);
473 chartTransition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
474 mChart.setLayoutTransition(chartTransition);
475 }
476 };
477
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700478 private static LayoutTransition buildLayoutTransition() {
479 final LayoutTransition transition = new LayoutTransition();
480 if (TEST_ANIM) {
481 transition.setDuration(1500);
482 }
483 transition.setAnimateParentHierarchy(false);
484 return transition;
485 }
486
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700487 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700488 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700489 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
490 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700491 */
492 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700493 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700494 mTabHost.clearAllTabs();
495
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700496 final boolean mobileSplit = isMobilePolicySplit();
497 if (mobileSplit && hasMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700498 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
499 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700500 } else if (hasMobileRadio(context)) {
501 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700502 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700503 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700504 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
505 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700506 if (mShowEthernet && hasEthernet(context)) {
507 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
508 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700509
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700510 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
511 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
512 if (mIntentTab != null) {
513 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
514 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700515 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700516 mTabHost.setCurrentTabByTag(mIntentTab);
517 }
518 mIntentTab = null;
519 } else {
520 if (mTabHost.getCurrentTab() == 0) {
521 updateBody();
522 } else {
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700523 mTabHost.setCurrentTab(0);
524 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700525 }
526 }
527
Jeff Sharkey8a503642011-06-10 13:31:21 -0700528 /**
529 * Factory that provide empty {@link View} to make {@link TabHost} happy.
530 */
531 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
532 /** {@inheritDoc} */
533 public View createTabContent(String tag) {
534 return new View(mTabHost.getContext());
535 }
536 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700537
Jeff Sharkey8a503642011-06-10 13:31:21 -0700538 /**
539 * Build {@link TabSpec} with thin indicator, and empty content.
540 */
541 private TabSpec buildTabSpec(String tag, int titleRes) {
542 final LayoutInflater inflater = LayoutInflater.from(mTabWidget.getContext());
543 final View indicator = inflater.inflate(
544 R.layout.tab_indicator_thin_holo, mTabWidget, false);
545 final TextView title = (TextView) indicator.findViewById(android.R.id.title);
546 title.setText(titleRes);
547 return mTabHost.newTabSpec(tag).setIndicator(indicator).setContent(mEmptyTabContent);
548 }
549
550 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
551 /** {@inheritDoc} */
552 public void onTabChanged(String tabId) {
553 // user changed tab; update body
554 updateBody();
555 }
556 };
557
558 /**
559 * Update body content based on current tab. Loads
560 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
561 * binds them to visible controls.
562 */
563 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700564 mBinding = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700565
Jeff Sharkeya662e492011-06-18 21:57:06 -0700566 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700567 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700568
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700569 if (currentTab == null) {
570 Log.w(TAG, "no tab selected; hiding body");
571 mListView.setVisibility(View.GONE);
572 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700573 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700574 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700575 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700576
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700577 final boolean tabChanged = !currentTab.equals(mCurrentTab);
578 mCurrentTab = currentTab;
579
Jeff Sharkey8a503642011-06-10 13:31:21 -0700580 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
581
582 if (TAB_WIFI.equals(currentTab)) {
583 // wifi doesn't have any controls
584 mDataEnabledView.setVisibility(View.GONE);
585 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700586 mTemplate = buildTemplateWifi();
587
588 } else if (TAB_ETHERNET.equals(currentTab)) {
589 // ethernet doesn't have any controls
590 mDataEnabledView.setVisibility(View.GONE);
591 mDisableAtLimitView.setVisibility(View.GONE);
592 mTemplate = buildTemplateEthernet();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700593
594 } else {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700595 mDataEnabledView.setVisibility(View.VISIBLE);
596 mDisableAtLimitView.setVisibility(View.VISIBLE);
597 }
598
599 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700600 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
601 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
602 mDataEnabled.setChecked(mConnService.getMobileDataEnabled());
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700603 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700604
605 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700606 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
607 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
608 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700609 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700610
611 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700612 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
613 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
614 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700615 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700616 }
617
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700618 try {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700619 // load stats for current template
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700620 mHistory = mStatsService.getHistoryForNetwork(
621 mTemplate, FIELD_RX_BYTES | FIELD_TX_BYTES);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700622 } catch (RemoteException e) {
623 // since we can't do much without policy or history, and we don't
624 // want to leave with half-baked UI, we bail hard.
625 throw new RuntimeException("problem reading network policy or stats", e);
626 }
627
Jeff Sharkey8a503642011-06-10 13:31:21 -0700628 // bind chart to historical stats
Jeff Sharkey8a503642011-06-10 13:31:21 -0700629 mChart.bindNetworkStats(mHistory);
630
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700631 // only update policy when switching tabs
632 updatePolicy(tabChanged);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700633 updateAppDetail();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700634
635 // force scroll to top of body
636 mListView.smoothScrollToPosition(0);
637
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700638 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700639 }
640
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700641 private boolean isAppDetailMode() {
642 return mUid != UID_NONE;
643 }
644
645 /**
646 * Update UID details panels to match {@link #mUid}, showing or hiding them
647 * depending on {@link #isAppDetailMode()}.
648 */
649 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700650 final Context context = getActivity();
651 final PackageManager pm = context.getPackageManager();
652 final LayoutInflater inflater = getActivity().getLayoutInflater();
653
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700654 if (isAppDetailMode()) {
655 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700656 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700657 } else {
658 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700659 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700660
661 // hide detail stats when not in detail mode
662 mChart.bindDetailNetworkStats(null);
663 return;
664 }
665
666 // remove warning/limit sweeps while in detail mode
667 mChart.bindNetworkPolicy(null);
668
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700669 // show icon and all labels appearing under this app
670 final UidDetail detail = resolveDetailForUid(context, mUid);
671 mAppIcon.setImageDrawable(detail.icon);
672
673 mAppTitles.removeAllViews();
674 if (detail.detailLabels != null) {
675 for (CharSequence label : detail.detailLabels) {
676 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label));
677 }
678 } else {
679 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label));
680 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700681
682 // enable settings button when package provides it
683 // TODO: target torwards entire UID instead of just first package
684 final String[] packageNames = pm.getPackagesForUid(mUid);
685 if (packageNames != null && packageNames.length > 0) {
686 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
687 mAppSettingsIntent.setPackage(packageNames[0]);
688 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
689
690 final boolean matchFound = pm.resolveActivity(mAppSettingsIntent, 0) != null;
691 mAppSettings.setEnabled(matchFound);
692
693 } else {
694 mAppSettingsIntent = null;
695 mAppSettings.setEnabled(false);
696 }
697
698 try {
699 // load stats for current uid and template
700 // TODO: read template from extras
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700701 mDetailHistory = mStatsService.getHistoryForUid(
702 mTemplate, mUid, TAG_NONE, FIELD_RX_BYTES | FIELD_TX_BYTES);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700703 } catch (RemoteException e) {
704 // since we can't do much without history, and we don't want to
705 // leave with half-baked UI, we bail hard.
706 throw new RuntimeException("problem reading network stats", e);
707 }
708
709 // bind chart to historical stats
710 mChart.bindDetailNetworkStats(mDetailHistory);
711
712 updateDetailData();
713
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700714 if (NetworkPolicyManager.isUidValidForPolicy(context, mUid) && !getRestrictBackground()
715 && isBandwidthControlEnabled()) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700716 mAppRestrictView.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700717 mAppRestrict.setChecked(getAppRestrictBackground());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700718
719 } else {
720 mAppRestrictView.setVisibility(View.GONE);
721 }
722
723 }
724
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700725 private void setPolicyCycleDay(int cycleDay) {
726 if (LOGD) Log.d(TAG, "setPolicyCycleDay()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700727 mPolicyEditor.setPolicyCycleDay(mTemplate, cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700728 updatePolicy(true);
729 }
730
731 private void setPolicyWarningBytes(long warningBytes) {
732 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700733 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700734 updatePolicy(false);
735 }
736
737 private void setPolicyLimitBytes(long limitBytes) {
738 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700739 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700740 updatePolicy(false);
741 }
742
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700743 private boolean isNetworkPolicyModifiable() {
744 return isBandwidthControlEnabled() && mDataEnabled.isChecked();
745 }
746
747 private boolean isBandwidthControlEnabled() {
748 try {
749 return mNetworkService.isBandwidthControlEnabled();
750 } catch (RemoteException e) {
751 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
752 return false;
753 }
754 }
755
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700756 private boolean getDataRoaming() {
757 final ContentResolver resolver = getActivity().getContentResolver();
758 return Settings.Secure.getInt(resolver, Settings.Secure.DATA_ROAMING, 0) != 0;
759 }
760
761 private void setDataRoaming(boolean enabled) {
762 // TODO: teach telephony DataConnectionTracker to watch and apply
763 // updates when changed.
764 final ContentResolver resolver = getActivity().getContentResolver();
765 Settings.Secure.putInt(resolver, Settings.Secure.DATA_ROAMING, enabled ? 1 : 0);
766 mMenuDataRoaming.setChecked(enabled);
767 }
768
769 private boolean getRestrictBackground() {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700770 try {
771 return mPolicyService.getRestrictBackground();
772 } catch (RemoteException e) {
773 Log.w(TAG, "problem talking with policy service: " + e);
774 return false;
775 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700776 }
777
778 private void setRestrictBackground(boolean restrictBackground) {
779 if (LOGD) Log.d(TAG, "setRestrictBackground()");
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700780 try {
781 mPolicyService.setRestrictBackground(restrictBackground);
782 mMenuRestrictBackground.setChecked(restrictBackground);
783 } catch (RemoteException e) {
784 Log.w(TAG, "problem talking with policy service: " + e);
785 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700786 }
787
788 private boolean getAppRestrictBackground() {
789 final int uidPolicy;
790 try {
791 uidPolicy = mPolicyService.getUidPolicy(mUid);
792 } catch (RemoteException e) {
793 // since we can't do much without policy, we bail hard.
794 throw new RuntimeException("problem reading network policy", e);
795 }
796
797 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
798 }
799
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700800 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700801 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700802 try {
803 mPolicyService.setUidPolicy(
804 mUid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
805 } catch (RemoteException e) {
806 throw new RuntimeException("unable to save policy", e);
807 }
808
809 mAppRestrict.setChecked(restrictBackground);
810 }
811
Jeff Sharkey8a503642011-06-10 13:31:21 -0700812 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700813 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
814 * current {@link #mTemplate}.
815 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700816 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700817 if (isAppDetailMode()) {
818 mNetworkSwitches.setVisibility(View.GONE);
819 // we fall through to update cycle list for detail mode
820 } else {
821 mNetworkSwitches.setVisibility(View.VISIBLE);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700822
823 // when heading back to summary without cycle refresh, kick details
824 // update to repopulate list.
825 if (!refreshCycle) {
826 updateDetailData();
827 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700828 }
829
Jeff Sharkeya662e492011-06-18 21:57:06 -0700830 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700831 if (isNetworkPolicyModifiable()) {
832 mDisableAtLimitView.setVisibility(View.VISIBLE);
833 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700834 if (!isAppDetailMode()) {
835 mChart.bindNetworkPolicy(policy);
836 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700837
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700838 } else {
839 // controls are disabled; don't bind warning/limit sweeps
840 mDisableAtLimitView.setVisibility(View.GONE);
841 mChart.bindNetworkPolicy(null);
842 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700843
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700844 if (refreshCycle) {
845 // generate cycle list based on policy and available history
846 updateCycleList(policy);
847 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700848 }
849
850 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700851 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
852 * and available {@link NetworkStatsHistory} data. Always selects the newest
853 * item, updating the inspection range on {@link #mChart}.
854 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700855 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700856 mCycleAdapter.clear();
857
858 final Context context = mCycleSpinner.getContext();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700859 long historyStart = mHistory.getStart();
860 long historyEnd = mHistory.getEnd();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700861
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700862 if (historyStart == Long.MAX_VALUE || historyEnd == Long.MIN_VALUE) {
863 historyStart = System.currentTimeMillis();
864 historyEnd = System.currentTimeMillis();
865 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700866
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700867 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700868 if (policy != null) {
869 // find the next cycle boundary
870 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700871
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700872 int guardCount = 0;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700873
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700874 // walk backwards, generating all valid cycle ranges
875 while (cycleEnd > historyStart) {
876 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
877 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
878 + historyStart);
879 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
880 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700881 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700882
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700883 // TODO: remove this guard once we have better testing
884 if (guardCount++ > 50) {
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700885 Log.wtf(TAG, "stuck generating ranges for historyStart=" + historyStart
886 + ", historyEnd=" + historyEnd + " and policy=" + policy);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700887 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700888 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700889
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700890 // one last cycle entry to modify policy cycle day
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700891 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable());
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700892 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700893
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700894 if (!hasCycles) {
895 // no valid cycles; show all data
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700896 // TODO: offer simple ranges like "last week" etc
897 mCycleAdapter.add(new CycleItem(context, historyStart, historyEnd));
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700898 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700899 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700900
901 // force pick the current cycle (first item)
902 mCycleSpinner.setSelection(0);
903 mCycleListener.onItemSelected(mCycleSpinner, null, 0, 0);
904 }
905
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700906 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700907 /** {@inheritDoc} */
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700908 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
909 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700910
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700911 final boolean dataEnabled = isChecked;
912 mDataEnabled.setChecked(dataEnabled);
913
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700914 final String currentTab = mCurrentTab;
915 if (TAB_MOBILE.equals(currentTab)) {
916 mConnService.setMobileDataEnabled(dataEnabled);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700917 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700918
919 // rebind policy to match radio state
920 updatePolicy(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700921 }
922 };
923
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700924 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700925 /** {@inheritDoc} */
926 public void onClick(View v) {
927 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700928 if (disableAtLimit) {
929 // enabling limit; show confirmation dialog which eventually
930 // calls setPolicyLimitBytes() once user confirms.
931 ConfirmLimitFragment.show(DataUsageSummary.this);
932 } else {
933 setPolicyLimitBytes(LIMIT_DISABLED);
934 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700935 }
936 };
937
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700938 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
939 /** {@inheritDoc} */
940 public void onClick(View v) {
941 final boolean restrictBackground = !mAppRestrict.isChecked();
942
943 if (restrictBackground) {
944 // enabling restriction; show confirmation dialog which
945 // eventually calls setRestrictBackground() once user confirms.
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700946 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700947 } else {
948 setAppRestrictBackground(false);
949 }
950 }
951 };
952
953 private OnClickListener mAppSettingsListener = new OnClickListener() {
954 /** {@inheritDoc} */
955 public void onClick(View v) {
956 // TODO: target torwards entire UID instead of just first package
957 startActivity(mAppSettingsIntent);
958 }
959 };
960
Jeff Sharkey8a503642011-06-10 13:31:21 -0700961 private OnItemClickListener mListListener = new OnItemClickListener() {
962 /** {@inheritDoc} */
963 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700964 final AppUsageItem app = (AppUsageItem) parent.getItemAtPosition(position);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700965 AppDetailsFragment.show(DataUsageSummary.this, app.uid);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700966 }
967 };
968
969 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
970 /** {@inheritDoc} */
971 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
972 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
973 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700974 // show cycle editor; will eventually call setPolicyCycleDay()
975 // when user finishes editing.
976 CycleEditorFragment.show(DataUsageSummary.this);
977
978 // reset spinner to something other than "change cycle..."
979 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700980
981 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -0700982 if (LOGD) {
983 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
984 + cycle.end + "]");
985 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700986
987 // update chart to show selected cycle, and update detail data
988 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700989 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700990
991 updateDetailData();
992 }
993 }
994
995 /** {@inheritDoc} */
996 public void onNothingSelected(AdapterView<?> parent) {
997 // ignored
998 }
999 };
1000
1001 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001002 * Update details based on {@link #mChart} inspection range depending on
1003 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1004 * of applications data usage, and when {@link #isAppDetailMode()} update
1005 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001006 */
1007 private void updateDetailData() {
1008 if (LOGD) Log.d(TAG, "updateDetailData()");
1009
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001010 final long start = mChart.getInspectStart();
1011 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001012 final long now = System.currentTimeMillis();
1013
1014 final Context context = getActivity();
1015 final NetworkStatsHistory.Entry entry;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001016
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001017 if (isAppDetailMode()) {
1018 if (mDetailHistory != null) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001019 entry = mDetailHistory.getValues(start, end, now, null);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001020 } else {
1021 entry = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001022 }
1023
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001024 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001025
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001026 } else {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001027 entry = mHistory.getValues(start, end, now, null);
1028
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001029 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001030 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001031 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryForAllUid);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001032 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001033
1034 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1035 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001036 final String rangePhrase = formatDateRange(context, start, end, false);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001037
1038 mUsageSummary.setText(
1039 getString(R.string.data_usage_total_during_range, totalPhrase, rangePhrase));
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001040 }
1041
1042 private final LoaderCallbacks<NetworkStats> mSummaryForAllUid = new LoaderCallbacks<
1043 NetworkStats>() {
1044 /** {@inheritDoc} */
1045 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
1046 return new SummaryForAllUidLoader(getActivity(), mStatsService, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001047 }
1048
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001049 /** {@inheritDoc} */
1050 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
1051 mAdapter.bindStats(data);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001052 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001053 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001054
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001055 /** {@inheritDoc} */
1056 public void onLoaderReset(Loader<NetworkStats> loader) {
1057 mAdapter.bindStats(null);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001058 updateEmptyVisible();
1059 }
1060
1061 private void updateEmptyVisible() {
1062 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1063 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001064 }
1065 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001066
Jeff Sharkeya662e492011-06-18 21:57:06 -07001067 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001068 final Context context = getActivity();
1069 if (hasMobileRadio(context)) {
1070 final String subscriberId = getActiveSubscriberId(context);
1071 return mPolicyEditor.isMobilePolicySplit(subscriberId);
1072 } else {
1073 return false;
1074 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001075 }
1076
1077 private void setMobilePolicySplit(boolean split) {
1078 final String subscriberId = getActiveSubscriberId(getActivity());
1079 mPolicyEditor.setMobilePolicySplit(subscriberId, split);
1080 }
1081
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001082 private static String getActiveSubscriberId(Context context) {
1083 final TelephonyManager telephony = (TelephonyManager) context.getSystemService(
1084 Context.TELEPHONY_SERVICE);
1085 return telephony.getSubscriberId();
1086 }
1087
Jeff Sharkey8a503642011-06-10 13:31:21 -07001088 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
1089 /** {@inheritDoc} */
1090 public void onInspectRangeChanged() {
1091 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
1092 updateDetailData();
1093 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001094
Jeff Sharkey8a503642011-06-10 13:31:21 -07001095 /** {@inheritDoc} */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001096 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001097 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001098 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001099
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001100 /** {@inheritDoc} */
1101 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001102 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001103 }
1104 };
1105
1106
1107 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001108 * List item that reflects a specific data usage cycle.
1109 */
1110 public static class CycleItem {
1111 public CharSequence label;
1112 public long start;
1113 public long end;
1114
Jeff Sharkey8a503642011-06-10 13:31:21 -07001115 CycleItem(CharSequence label) {
1116 this.label = label;
1117 }
1118
1119 public CycleItem(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001120 this.label = formatDateRange(context, start, end, true);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001121 this.start = start;
1122 this.end = end;
1123 }
1124
Jeff Sharkey8a503642011-06-10 13:31:21 -07001125 @Override
1126 public String toString() {
1127 return label.toString();
1128 }
1129 }
1130
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001131 private static final StringBuilder sBuilder = new StringBuilder(50);
1132 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1133 sBuilder, Locale.getDefault());
1134
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001135 public static String formatDateRange(Context context, long start, long end, boolean utcTime) {
1136 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
1137 final String timezone = utcTime ? TIMEZONE_UTC : null;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001138
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001139 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001140 sBuilder.setLength(0);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001141 return DateUtils
1142 .formatDateRange(context, sFormatter, start, end, flags, timezone).toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001143 }
1144 }
1145
Jeff Sharkey8a503642011-06-10 13:31:21 -07001146 /**
1147 * Special-case data usage cycle that triggers dialog to change
1148 * {@link NetworkPolicy#cycleDay}.
1149 */
1150 public static class CycleChangeItem extends CycleItem {
1151 public CycleChangeItem(Context context) {
1152 super(context.getString(R.string.data_usage_change_cycle));
1153 }
1154 }
1155
1156 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001157 private boolean mChangePossible = false;
1158 private boolean mChangeVisible = false;
1159
1160 private final CycleChangeItem mChangeItem;
1161
Jeff Sharkey8a503642011-06-10 13:31:21 -07001162 public CycleAdapter(Context context) {
1163 super(context, android.R.layout.simple_spinner_item);
1164 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001165 mChangeItem = new CycleChangeItem(context);
1166 }
1167
1168 public void setChangePossible(boolean possible) {
1169 mChangePossible = possible;
1170 updateChange();
1171 }
1172
1173 public void setChangeVisible(boolean visible) {
1174 mChangeVisible = visible;
1175 updateChange();
1176 }
1177
1178 private void updateChange() {
1179 remove(mChangeItem);
1180 if (mChangePossible && mChangeVisible) {
1181 add(mChangeItem);
1182 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001183 }
1184 }
1185
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001186 private static class AppUsageItem implements Comparable<AppUsageItem> {
1187 public int uid;
1188 public long total;
1189
1190 /** {@inheritDoc} */
1191 public int compareTo(AppUsageItem another) {
1192 return Long.compare(another.total, total);
1193 }
1194 }
1195
Jeff Sharkey8a503642011-06-10 13:31:21 -07001196 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001197 * Adapter of applications, sorted by total usage descending.
1198 */
1199 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001200 private ArrayList<AppUsageItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001201 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001202
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001203 /**
1204 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1205 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001206 public void bindStats(NetworkStats stats) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001207 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001208
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001209 if (stats != null) {
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001210 final AppUsageItem systemItem = new AppUsageItem();
1211 systemItem.uid = android.os.Process.SYSTEM_UID;
1212
Jeff Sharkeyebae6592011-07-12 13:53:11 -07001213 NetworkStats.Entry entry = null;
1214 for (int i = 0; i < stats.size(); i++) {
1215 entry = stats.getValues(i, entry);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001216
1217 final boolean isApp = entry.uid >= android.os.Process.FIRST_APPLICATION_UID
1218 && entry.uid <= android.os.Process.LAST_APPLICATION_UID;
1219 if (isApp || entry.uid == TrafficStats.UID_REMOVED) {
1220 final AppUsageItem item = new AppUsageItem();
1221 item.uid = entry.uid;
1222 item.total = entry.rxBytes + entry.txBytes;
1223 mItems.add(item);
1224 } else {
1225 systemItem.total += entry.rxBytes + entry.txBytes;
1226 }
1227 }
1228
1229 if (systemItem.total > 0) {
1230 mItems.add(systemItem);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001231 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001232 }
1233
Jeff Sharkey8a503642011-06-10 13:31:21 -07001234 Collections.sort(mItems);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001235 mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001236 notifyDataSetChanged();
1237 }
1238
1239 @Override
1240 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001241 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001242 }
1243
1244 @Override
1245 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001246 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001247 }
1248
1249 @Override
1250 public long getItemId(int position) {
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001251 return mItems.get(position).uid;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001252 }
1253
1254 @Override
1255 public View getView(int position, View convertView, ViewGroup parent) {
1256 if (convertView == null) {
1257 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey52c3f442011-06-23 00:39:38 -07001258 R.layout.data_usage_item, parent, false);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001259 }
1260
1261 final Context context = parent.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001262
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001263 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001264 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
1265 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
1266 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1267 android.R.id.progress);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001268
Jeff Sharkey8a503642011-06-10 13:31:21 -07001269 final AppUsageItem item = mItems.get(position);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001270 final UidDetail detail = resolveDetailForUid(context, item.uid);
1271
1272 icon.setImageDrawable(detail.icon);
1273 title.setText(detail.label);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001274 summary.setText(Formatter.formatFileSize(context, item.total));
1275
1276 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1277 progress.setProgress(percentTotal);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001278
1279 return convertView;
1280 }
1281
1282 }
1283
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001284 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001285 * Empty {@link Fragment} that controls display of UID details in
1286 * {@link DataUsageSummary}.
1287 */
1288 public static class AppDetailsFragment extends Fragment {
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001289 private static final String EXTRA_UID = "uid";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001290
1291 public static void show(DataUsageSummary parent, int uid) {
1292 final Bundle args = new Bundle();
1293 args.putInt(EXTRA_UID, uid);
1294
1295 final AppDetailsFragment fragment = new AppDetailsFragment();
1296 fragment.setArguments(args);
1297 fragment.setTargetFragment(parent, 0);
1298
1299 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1300 ft.add(fragment, TAG_APP_DETAILS);
1301 ft.addToBackStack(TAG_APP_DETAILS);
1302 ft.commit();
1303 }
1304
1305 @Override
1306 public void onStart() {
1307 super.onStart();
1308 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1309 target.mUid = getArguments().getInt(EXTRA_UID);
1310 target.updateBody();
1311 }
1312
1313 @Override
1314 public void onStop() {
1315 super.onStop();
1316 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1317 target.mUid = UID_NONE;
1318 target.updateBody();
1319 }
1320 }
1321
1322 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001323 * Dialog to request user confirmation before setting
1324 * {@link NetworkPolicy#limitBytes}.
1325 */
1326 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001327 private static final String EXTRA_MESSAGE_ID = "messageId";
1328 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001329
1330 public static void show(DataUsageSummary parent) {
1331 final Bundle args = new Bundle();
1332
1333 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001334 final String currentTab = parent.mCurrentTab;
1335 if (TAB_3G.equals(currentTab)) {
1336 args.putInt(EXTRA_MESSAGE_ID, R.string.data_usage_limit_dialog_3g);
1337 args.putLong(EXTRA_LIMIT_BYTES, 5 * GB_IN_BYTES);
1338 } else if (TAB_4G.equals(currentTab)) {
1339 args.putInt(EXTRA_MESSAGE_ID, R.string.data_usage_limit_dialog_4g);
1340 args.putLong(EXTRA_LIMIT_BYTES, 5 * GB_IN_BYTES);
1341 } else if (TAB_MOBILE.equals(currentTab)) {
1342 args.putInt(EXTRA_MESSAGE_ID, R.string.data_usage_limit_dialog_mobile);
1343 args.putLong(EXTRA_LIMIT_BYTES, 5 * GB_IN_BYTES);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001344 }
1345
1346 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1347 dialog.setArguments(args);
1348 dialog.setTargetFragment(parent, 0);
1349 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1350 }
1351
1352 @Override
1353 public Dialog onCreateDialog(Bundle savedInstanceState) {
1354 final Context context = getActivity();
1355
1356 final int messageId = getArguments().getInt(EXTRA_MESSAGE_ID);
1357 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1358
1359 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1360 builder.setTitle(R.string.data_usage_limit_dialog_title);
1361 builder.setMessage(messageId);
1362
1363 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1364 public void onClick(DialogInterface dialog, int which) {
1365 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1366 if (target != null) {
1367 target.setPolicyLimitBytes(limitBytes);
1368 }
1369 }
1370 });
1371
1372 return builder.create();
1373 }
1374 }
1375
1376 /**
1377 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1378 */
1379 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001380 private static final String EXTRA_CYCLE_DAY = "cycleDay";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001381
1382 public static void show(DataUsageSummary parent) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07001383 final NetworkPolicy policy = parent.mPolicyEditor.getPolicy(parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001384 final Bundle args = new Bundle();
1385 args.putInt(CycleEditorFragment.EXTRA_CYCLE_DAY, policy.cycleDay);
1386
1387 final CycleEditorFragment dialog = new CycleEditorFragment();
1388 dialog.setArguments(args);
1389 dialog.setTargetFragment(parent, 0);
1390 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1391 }
1392
1393 @Override
1394 public Dialog onCreateDialog(Bundle savedInstanceState) {
1395 final Context context = getActivity();
1396
1397 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1398 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1399
1400 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1401 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1402
1403 final int oldCycleDay = getArguments().getInt(EXTRA_CYCLE_DAY, 1);
1404
1405 cycleDayPicker.setMinValue(1);
1406 cycleDayPicker.setMaxValue(31);
1407 cycleDayPicker.setValue(oldCycleDay);
1408 cycleDayPicker.setWrapSelectorWheel(true);
1409
1410 builder.setTitle(R.string.data_usage_cycle_editor_title);
1411 builder.setView(view);
1412
1413 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1414 new DialogInterface.OnClickListener() {
1415 public void onClick(DialogInterface dialog, int which) {
1416 final int cycleDay = cycleDayPicker.getValue();
1417 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1418 if (target != null) {
1419 target.setPolicyCycleDay(cycleDay);
1420 }
1421 }
1422 });
1423
1424 return builder.create();
1425 }
1426 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001427
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001428 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001429 * Dialog to request user confirmation before setting
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001430 * {@link Settings.Secure#DATA_ROAMING}.
1431 */
1432 public static class ConfirmDataRoamingFragment extends DialogFragment {
1433 public static void show(DataUsageSummary parent) {
1434 final Bundle args = new Bundle();
1435
1436 final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment();
1437 dialog.setTargetFragment(parent, 0);
1438 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_ROAMING);
1439 }
1440
1441 @Override
1442 public Dialog onCreateDialog(Bundle savedInstanceState) {
1443 final Context context = getActivity();
1444
1445 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1446 builder.setTitle(R.string.roaming_reenable_title);
1447 builder.setMessage(R.string.roaming_warning);
1448
1449 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1450 public void onClick(DialogInterface dialog, int which) {
1451 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1452 if (target != null) {
1453 target.setDataRoaming(true);
1454 }
1455 }
1456 });
1457 builder.setNegativeButton(android.R.string.cancel, null);
1458
1459 return builder.create();
1460 }
1461 }
1462
1463 /**
1464 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001465 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001466 */
1467 public static class ConfirmRestrictFragment extends DialogFragment {
1468 public static void show(DataUsageSummary parent) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001469 final Bundle args = new Bundle();
1470
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001471 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1472 dialog.setTargetFragment(parent, 0);
1473 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1474 }
1475
1476 @Override
1477 public Dialog onCreateDialog(Bundle savedInstanceState) {
1478 final Context context = getActivity();
1479
1480 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001481 builder.setTitle(R.string.data_usage_restrict_background_title);
1482 builder.setMessage(R.string.data_usage_restrict_background);
1483
1484 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1485 public void onClick(DialogInterface dialog, int which) {
1486 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1487 if (target != null) {
1488 target.setRestrictBackground(true);
1489 }
1490 }
1491 });
1492 builder.setNegativeButton(android.R.string.cancel, null);
1493
1494 return builder.create();
1495 }
1496 }
1497
1498 /**
1499 * Dialog to request user confirmation before setting
1500 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
1501 */
1502 public static class ConfirmAppRestrictFragment extends DialogFragment {
1503 public static void show(DataUsageSummary parent) {
1504 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
1505 dialog.setTargetFragment(parent, 0);
1506 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
1507 }
1508
1509 @Override
1510 public Dialog onCreateDialog(Bundle savedInstanceState) {
1511 final Context context = getActivity();
1512
1513 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001514 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
1515 builder.setMessage(R.string.data_usage_app_restrict_dialog);
1516
1517 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1518 public void onClick(DialogInterface dialog, int which) {
1519 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1520 if (target != null) {
1521 target.setAppRestrictBackground(true);
1522 }
1523 }
1524 });
1525 builder.setNegativeButton(android.R.string.cancel, null);
1526
1527 return builder.create();
1528 }
1529 }
1530
1531 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001532 * Compute default tab that should be selected, based on
1533 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
1534 */
1535 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07001536 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
1537 if (template == null) return null;
1538
1539 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07001540 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001541 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001542 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001543 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001544 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001545 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001546 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001547 return TAB_WIFI;
1548 default:
1549 return null;
1550 }
1551 }
1552
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001553 public static class UidDetail {
1554 public CharSequence label;
1555 public CharSequence[] detailLabels;
1556 public Drawable icon;
1557 }
1558
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001559 /**
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001560 * Resolve best descriptive label for the given UID.
1561 */
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001562 public static UidDetail resolveDetailForUid(Context context, int uid) {
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001563 final Resources res = context.getResources();
1564 final PackageManager pm = context.getPackageManager();
1565
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001566 final UidDetail detail = new UidDetail();
1567 detail.label = pm.getNameForUid(uid);
1568 detail.icon = pm.getDefaultActivityIcon();
1569
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001570 // handle special case labels
1571 switch (uid) {
1572 case android.os.Process.SYSTEM_UID:
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001573 detail.label = res.getString(R.string.process_kernel_label);
1574 detail.icon = pm.getDefaultActivityIcon();
1575 return detail;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001576 case TrafficStats.UID_REMOVED:
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001577 detail.label = res.getString(R.string.data_usage_uninstalled_apps);
1578 detail.icon = pm.getDefaultActivityIcon();
1579 return detail;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001580 }
1581
1582 // otherwise fall back to using packagemanager labels
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001583 final String[] packageNames = pm.getPackagesForUid(uid);
1584 final int length = packageNames != null ? packageNames.length : 0;
1585
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001586 try {
1587 if (length == 1) {
1588 final ApplicationInfo info = pm.getApplicationInfo(packageNames[0], 0);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001589 detail.label = info.loadLabel(pm).toString();
1590 detail.icon = info.loadIcon(pm);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001591 } else if (length > 1) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001592 detail.detailLabels = new CharSequence[length];
1593 for (int i = 0; i < length; i++) {
1594 final String packageName = packageNames[i];
1595 final PackageInfo packageInfo = pm.getPackageInfo(packageName, 0);
1596 final ApplicationInfo appInfo = pm.getApplicationInfo(packageName, 0);
1597
1598 detail.detailLabels[i] = appInfo.loadLabel(pm).toString();
1599 if (packageInfo.sharedUserLabel != 0) {
1600 detail.label = pm.getText(packageName, packageInfo.sharedUserLabel,
1601 packageInfo.applicationInfo).toString();
1602 detail.icon = appInfo.loadIcon(pm);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001603 }
1604 }
1605 }
1606 } catch (NameNotFoundException e) {
1607 }
1608
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001609 if (TextUtils.isEmpty(detail.label)) {
1610 detail.label = Integer.toString(uid);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001611 }
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001612 return detail;
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001613 }
1614
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001615 /**
1616 * Test if device has a mobile data radio.
1617 */
1618 private static boolean hasMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001619 if (TEST_RADIOS) {
1620 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
1621 }
1622
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001623 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
1624 Context.CONNECTIVITY_SERVICE);
1625
1626 // mobile devices should have MOBILE network tracker regardless of
1627 // connection status.
1628 return conn.getNetworkInfo(TYPE_MOBILE) != null;
1629 }
1630
1631 /**
1632 * Test if device has a mobile 4G data radio.
1633 */
1634 private static boolean hasMobile4gRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001635 if (TEST_RADIOS) {
1636 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
1637 }
1638
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001639 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
1640 Context.CONNECTIVITY_SERVICE);
1641 final TelephonyManager telephony = (TelephonyManager) context.getSystemService(
1642 Context.TELEPHONY_SERVICE);
1643
1644 // WiMAX devices should have WiMAX network tracker regardless of
1645 // connection status.
1646 final boolean hasWimax = conn.getNetworkInfo(TYPE_WIMAX) != null;
1647 final boolean hasLte = telephony.getLteOnCdmaMode() == Phone.LTE_ON_CDMA_TRUE;
1648 return hasWimax || hasLte;
1649 }
1650
1651 /**
1652 * Test if device has a Wi-Fi data radio.
1653 */
1654 private static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001655 if (TEST_RADIOS) {
1656 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
1657 }
1658
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001659 return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI);
1660 }
1661
1662 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001663 * Test if device has an ethernet network connection.
1664 */
1665 private static boolean hasEthernet(Context context) {
1666 if (TEST_RADIOS) {
1667 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
1668 }
1669
1670 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
1671 Context.CONNECTIVITY_SERVICE);
1672 return conn.getNetworkInfo(TYPE_ETHERNET) != null;
1673 }
1674
1675 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001676 * Inflate a {@link Preference} style layout, adding the given {@link View}
1677 * widget into {@link android.R.id#widget_frame}.
1678 */
1679 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
1680 final View view = inflater.inflate(R.layout.preference, root, false);
1681 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
1682 android.R.id.widget_frame);
1683 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
1684 return view;
1685 }
1686
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001687 private static View inflateAppTitle(
1688 LayoutInflater inflater, ViewGroup root, CharSequence label) {
1689 final TextView view = (TextView) inflater.inflate(
1690 R.layout.data_usage_app_title, root, false);
1691 view.setText(label);
1692 return view;
1693 }
1694
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001695 /**
1696 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07001697 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001698 */
1699 private static void setPreferenceTitle(View parent, int resId) {
1700 final TextView title = (TextView) parent.findViewById(android.R.id.title);
1701 title.setText(resId);
1702 }
1703
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001704 /**
1705 * Set {@link android.R.id#summary} for a preference view inflated with
1706 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
1707 */
1708 private static void setPreferenceSummary(View parent, int resId) {
1709 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
1710 summary.setVisibility(View.VISIBLE);
1711 summary.setText(resId);
1712 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001713}