blob: a95ab3f6ab5bdccbe834b3cd2f8049c90436b701 [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 Sharkey54d0af52011-08-11 18:26:57 -070028import static android.net.NetworkStats.SET_DEFAULT;
29import static android.net.NetworkStats.SET_FOREGROUND;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070030import static android.net.NetworkStats.TAG_NONE;
31import static android.net.NetworkStatsHistory.FIELD_RX_BYTES;
32import static android.net.NetworkStatsHistory.FIELD_TX_BYTES;
Jeff Sharkeya662e492011-06-18 21:57:06 -070033import static android.net.NetworkTemplate.MATCH_MOBILE_3G_LOWER;
34import static android.net.NetworkTemplate.MATCH_MOBILE_4G;
35import static android.net.NetworkTemplate.MATCH_MOBILE_ALL;
36import static android.net.NetworkTemplate.MATCH_WIFI;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070037import static android.net.NetworkTemplate.buildTemplateEthernet;
38import static android.net.NetworkTemplate.buildTemplateMobile3gLower;
39import static android.net.NetworkTemplate.buildTemplateMobile4g;
40import static android.net.NetworkTemplate.buildTemplateMobileAll;
41import static android.net.NetworkTemplate.buildTemplateWifi;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070042import static android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
43import static android.text.format.DateUtils.FORMAT_SHOW_DATE;
44import static android.text.format.Time.TIMEZONE_UTC;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070045import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070046
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070047import android.animation.LayoutTransition;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070048import android.app.AlertDialog;
49import android.app.Dialog;
50import android.app.DialogFragment;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070051import android.app.Fragment;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070052import android.app.FragmentTransaction;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070053import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070054import android.content.ContentResolver;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070055import android.content.Context;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070056import android.content.DialogInterface;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -070057import android.content.Intent;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070058import android.content.Loader;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070059import android.content.SharedPreferences;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070060import android.content.pm.ApplicationInfo;
61import android.content.pm.PackageInfo;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070062import android.content.pm.PackageManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070063import android.content.pm.PackageManager.NameNotFoundException;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070064import android.content.res.Resources;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070065import android.graphics.Color;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070066import android.graphics.drawable.Drawable;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070067import android.net.ConnectivityManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070068import android.net.INetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070069import android.net.INetworkStatsService;
Jeff Sharkey8a503642011-06-10 13:31:21 -070070import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070071import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070072import android.net.NetworkStats;
73import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070074import android.net.NetworkTemplate;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070075import android.net.TrafficStats;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070076import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070077import android.os.Bundle;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070078import android.os.INetworkManagementService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070079import android.os.RemoteException;
80import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070081import android.os.SystemProperties;
Jeff Sharkey8a503642011-06-10 13:31:21 -070082import android.preference.Preference;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070083import android.provider.Settings;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070084import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070085import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070086import android.text.format.DateUtils;
87import android.text.format.Formatter;
88import android.util.Log;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070089import android.util.SparseArray;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070090import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -070091import android.view.Menu;
92import android.view.MenuInflater;
93import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070094import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070095import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070096import android.view.ViewGroup;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -070097import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Jeff Sharkey8a503642011-06-10 13:31:21 -070098import android.widget.AdapterView;
99import android.widget.AdapterView.OnItemClickListener;
100import android.widget.AdapterView.OnItemSelectedListener;
101import android.widget.ArrayAdapter;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700102import android.widget.BaseAdapter;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700103import android.widget.Button;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700104import android.widget.CheckBox;
105import android.widget.CompoundButton;
106import android.widget.CompoundButton.OnCheckedChangeListener;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700107import android.widget.ImageView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700108import android.widget.LinearLayout;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700109import android.widget.ListView;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700110import android.widget.NumberPicker;
Jeff Sharkey2412b0f2011-07-17 20:31:40 -0700111import android.widget.ProgressBar;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700112import android.widget.Spinner;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700113import android.widget.Switch;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700114import android.widget.TabHost;
115import android.widget.TabHost.OnTabChangeListener;
116import android.widget.TabHost.TabContentFactory;
117import android.widget.TabHost.TabSpec;
118import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700119import android.widget.TextView;
120
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700121import com.android.internal.telephony.Phone;
Jeff Sharkeya662e492011-06-18 21:57:06 -0700122import com.android.settings.net.NetworkPolicyEditor;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700123import com.android.settings.net.SummaryForAllUidLoader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700124import com.android.settings.widget.DataUsageChartView;
125import com.android.settings.widget.DataUsageChartView.DataUsageChartListener;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700126import com.android.settings.widget.PieChartView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700127import com.google.android.collect.Lists;
128
129import java.util.ArrayList;
130import java.util.Collections;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700131import java.util.Locale;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700132
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700133import libcore.util.Objects;
134
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700135/**
136 * Panel show data usage history across various networks, including options to
137 * inspect based on usage cycle and control through {@link NetworkPolicy}.
138 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700139public class DataUsageSummary extends Fragment {
140 private static final String TAG = "DataUsage";
Jeff Sharkey8a503642011-06-10 13:31:21 -0700141 private static final boolean LOGD = true;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700142
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700143 // TODO: remove this testing code
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700144 private static final boolean TEST_ANIM = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700145 private static final boolean TEST_RADIOS = false;
146 private static final String TEST_RADIOS_PROP = "test.radios";
147
Jeff Sharkey8a503642011-06-10 13:31:21 -0700148 private static final String TAB_3G = "3g";
149 private static final String TAB_4G = "4g";
150 private static final String TAB_MOBILE = "mobile";
151 private static final String TAB_WIFI = "wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700152 private static final String TAB_ETHERNET = "ethernet";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700153
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700154 private static final String TAG_CONFIRM_ROAMING = "confirmRoaming";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700155 private static final String TAG_CONFIRM_LIMIT = "confirmLimit";
156 private static final String TAG_CYCLE_EDITOR = "cycleEditor";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700157 private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700158 private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700159 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700160
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700161 private static final int LOADER_SUMMARY = 2;
162
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700163 private static final long KB_IN_BYTES = 1024;
164 private static final long MB_IN_BYTES = KB_IN_BYTES * 1024;
165 private static final long GB_IN_BYTES = MB_IN_BYTES * 1024;
166
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700167 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700168 private INetworkStatsService mStatsService;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700169 private INetworkPolicyManager mPolicyService;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700170 private ConnectivityManager mConnService;
171
172 private static final String PREF_FILE = "data_usage";
173 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700174 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700175
176 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700177
Jeff Sharkey8a503642011-06-10 13:31:21 -0700178 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700179 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700180 private TabWidget mTabWidget;
181 private ListView mListView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700182 private DataUsageAdapter mAdapter;
183
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700184 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700185
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700186 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700187 private LinearLayout mNetworkSwitches;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700188 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700189 private View mDataEnabledView;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700190 private CheckBox mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700191 private View mDisableAtLimitView;
192
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700193 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700194 private Spinner mCycleSpinner;
195 private CycleAdapter mCycleAdapter;
196
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700197 private DataUsageChartView mChart;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700198 private TextView mUsageSummary;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700199 private TextView mEmpty;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700200
201 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700202 private ImageView mAppIcon;
203 private ViewGroup mAppTitles;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700204 private PieChartView mAppPieChart;
205 private TextView mAppForeground;
206 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700207 private Button mAppSettings;
208
209 private LinearLayout mAppSwitches;
210 private CheckBox mAppRestrict;
211 private View mAppRestrictView;
212
Jeff Sharkey8a503642011-06-10 13:31:21 -0700213 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700214 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700215
Jeff Sharkeya662e492011-06-18 21:57:06 -0700216 private NetworkTemplate mTemplate = null;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700217
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700218 private static final int UID_NONE = -1;
219 private int mUid = UID_NONE;
220
221 private Intent mAppSettingsIntent;
222
Jeff Sharkeya662e492011-06-18 21:57:06 -0700223 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700224
Jeff Sharkey8a503642011-06-10 13:31:21 -0700225 private NetworkStatsHistory mHistory;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700226 private NetworkStatsHistory mDetailHistory;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700227 private NetworkStatsHistory mDetailHistoryDefault;
228 private NetworkStatsHistory mDetailHistoryForeground;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700229
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700230 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700231 private String mIntentTab = null;
232
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700233 private MenuItem mMenuDataRoaming;
234 private MenuItem mMenuRestrictBackground;
235
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700236 /** Flag used to ignore listeners during binding. */
237 private boolean mBinding;
238
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700239 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700240 public void onCreate(Bundle savedInstanceState) {
241 super.onCreate(savedInstanceState);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700242
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700243 mNetworkService = INetworkManagementService.Stub.asInterface(
244 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700245 mStatsService = INetworkStatsService.Stub.asInterface(
246 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700247 mPolicyService = INetworkPolicyManager.Stub.asInterface(
248 ServiceManager.getService(Context.NETWORK_POLICY_SERVICE));
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700249 mConnService = (ConnectivityManager) getActivity().getSystemService(
250 Context.CONNECTIVITY_SERVICE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700251
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700252 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700253
Jeff Sharkeya662e492011-06-18 21:57:06 -0700254 mPolicyEditor = new NetworkPolicyEditor(mPolicyService);
255 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700256
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700257 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700258 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700259
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700260 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700261 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700262
Jeff Sharkey8a503642011-06-10 13:31:21 -0700263 @Override
264 public View onCreateView(LayoutInflater inflater, ViewGroup container,
265 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700266
Jeff Sharkey8a503642011-06-10 13:31:21 -0700267 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700268 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
269
Jeff Sharkey8a503642011-06-10 13:31:21 -0700270 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700271 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700272 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
273 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700274
Jeff Sharkey8a503642011-06-10 13:31:21 -0700275 mTabHost.setup();
276 mTabHost.setOnTabChangedListener(mTabListener);
277
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700278 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700279 mListView.addHeaderView(mHeader, null, false);
280
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700281 {
282 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700283 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
284 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700285 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700286
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700287 mDataEnabled = new Switch(inflater.getContext());
288 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
289 mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener);
290 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700291
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700292 mDisableAtLimit = new CheckBox(inflater.getContext());
293 mDisableAtLimit.setClickable(false);
294 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
295 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
296 mNetworkSwitches.addView(mDisableAtLimitView);
297 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700298
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700299 // bind cycle dropdown
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700300 mCycleView = mHeader.findViewById(R.id.cycles);
301 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700302 mCycleAdapter = new CycleAdapter(context);
303 mCycleSpinner.setAdapter(mCycleAdapter);
304 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
305
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700306 mChart = (DataUsageChartView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700307 mChart.setListener(mChartListener);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700308
309 {
310 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700311 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700312 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
313 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700314 mAppPieChart = (PieChartView) mAppDetail.findViewById(R.id.app_pie_chart);
315 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
316 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700317 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700318
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700319 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700320 mAppSettings.setOnClickListener(mAppSettingsListener);
321
322 mAppRestrict = new CheckBox(inflater.getContext());
323 mAppRestrict.setClickable(false);
324 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
325 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
326 setPreferenceSummary(
327 mAppRestrictView, R.string.data_usage_app_restrict_background_summary);
328 mAppRestrictView.setOnClickListener(mAppRestrictListener);
329 mAppSwitches.addView(mAppRestrictView);
330 }
331
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700332 mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700333 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700334
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700335 // only assign layout transitions once first layout is finished
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700336 mListView.getViewTreeObserver().addOnGlobalLayoutListener(mFirstLayoutListener);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700337
338 mAdapter = new DataUsageAdapter();
339 mListView.setOnItemClickListener(mListListener);
340 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700341
342 return view;
343 }
344
345 @Override
346 public void onResume() {
347 super.onResume();
348
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700349 // pick default tab based on incoming intent
350 final Intent intent = getActivity().getIntent();
351 mIntentTab = computeTabFromIntent(intent);
352
Jeff Sharkey8a503642011-06-10 13:31:21 -0700353 // this kicks off chain reaction which creates tabs, binds the body to
354 // selected network, and binds chart, cycles and detail list.
355 updateTabs();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700356
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700357 // kick off background task to update stats
358 new AsyncTask<Void, Void, Void>() {
359 @Override
360 protected Void doInBackground(Void... params) {
361 try {
362 mStatsService.forceUpdate();
363 } catch (RemoteException e) {
364 }
365 return null;
366 }
367
368 @Override
369 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700370 if (isAdded()) {
371 updateBody();
372 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700373 }
374 }.execute();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700375 }
376
Jeff Sharkey8a503642011-06-10 13:31:21 -0700377 @Override
378 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
379 inflater.inflate(R.menu.data_usage, menu);
380 }
381
382 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700383 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700384 final Context context = getActivity();
385
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700386 mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming);
387 mMenuDataRoaming.setVisible(hasMobileRadio(context));
388 mMenuDataRoaming.setChecked(getDataRoaming());
389
390 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
391 mMenuRestrictBackground.setChecked(getRestrictBackground());
392
393 final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700394 split4g.setVisible(hasMobile4gRadio(context));
Jeff Sharkeya662e492011-06-18 21:57:06 -0700395 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700396
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700397 final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700398 if (hasWifiRadio(context) && hasMobileRadio(context)) {
399 showWifi.setVisible(true);
400 showWifi.setChecked(mShowWifi);
401 } else {
402 showWifi.setVisible(false);
403 mShowWifi = true;
404 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700405
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700406 final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
407 if (hasEthernet(context) && hasMobileRadio(context)) {
408 showEthernet.setVisible(true);
409 showEthernet.setChecked(mShowEthernet);
410 } else {
411 showEthernet.setVisible(false);
412 mShowEthernet = true;
413 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700414 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700415
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700416 @Override
417 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700418 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700419 case R.id.data_usage_menu_roaming: {
420 final boolean dataRoaming = !item.isChecked();
421 if (dataRoaming) {
422 ConfirmDataRoamingFragment.show(this);
423 } else {
424 // no confirmation to disable roaming
425 setDataRoaming(false);
426 }
427 return true;
428 }
429 case R.id.data_usage_menu_restrict_background: {
430 final boolean restrictBackground = !item.isChecked();
431 if (restrictBackground) {
432 ConfirmRestrictFragment.show(this);
433 } else {
434 // no confirmation to drop restriction
435 setRestrictBackground(false);
436 }
437 return true;
438 }
439 case R.id.data_usage_menu_split_4g: {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700440 final boolean mobileSplit = !item.isChecked();
Jeff Sharkeya662e492011-06-18 21:57:06 -0700441 setMobilePolicySplit(mobileSplit);
442 item.setChecked(isMobilePolicySplit());
Jeff Sharkey8a503642011-06-10 13:31:21 -0700443 updateTabs();
444 return true;
445 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700446 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700447 mShowWifi = !item.isChecked();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700448 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700449 item.setChecked(mShowWifi);
450 updateTabs();
451 return true;
452 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700453 case R.id.data_usage_menu_show_ethernet: {
454 mShowEthernet = !item.isChecked();
455 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
456 item.setChecked(mShowEthernet);
457 updateTabs();
458 return true;
459 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700460 }
461 return false;
462 }
463
Jeff Sharkey94a90952011-06-13 22:31:09 -0700464 @Override
465 public void onDestroyView() {
466 super.onDestroyView();
467
468 mDataEnabledView = null;
469 mDisableAtLimitView = null;
470 }
471
Jeff Sharkey8a503642011-06-10 13:31:21 -0700472 /**
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700473 * Listener to setup {@link LayoutTransition} after first layout pass.
474 */
475 private OnGlobalLayoutListener mFirstLayoutListener = new OnGlobalLayoutListener() {
476 /** {@inheritDoc} */
477 public void onGlobalLayout() {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700478 mListView.getViewTreeObserver().removeGlobalOnLayoutListener(mFirstLayoutListener);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700479
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700480 mTabsContainer.setLayoutTransition(buildLayoutTransition());
481 mHeader.setLayoutTransition(buildLayoutTransition());
482 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700483
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700484 final LayoutTransition chartTransition = buildLayoutTransition();
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700485 chartTransition.setStartDelay(LayoutTransition.APPEARING, 0);
486 chartTransition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
487 mChart.setLayoutTransition(chartTransition);
488 }
489 };
490
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700491 private static LayoutTransition buildLayoutTransition() {
492 final LayoutTransition transition = new LayoutTransition();
493 if (TEST_ANIM) {
494 transition.setDuration(1500);
495 }
496 transition.setAnimateParentHierarchy(false);
497 return transition;
498 }
499
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700500 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700501 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700502 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
503 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700504 */
505 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700506 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700507 mTabHost.clearAllTabs();
508
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700509 final boolean mobileSplit = isMobilePolicySplit();
510 if (mobileSplit && hasMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700511 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
512 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700513 } else if (hasMobileRadio(context)) {
514 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700515 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700516 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700517 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
518 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700519 if (mShowEthernet && hasEthernet(context)) {
520 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
521 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700522
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700523 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
524 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
525 if (mIntentTab != null) {
526 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
527 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700528 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700529 mTabHost.setCurrentTabByTag(mIntentTab);
530 }
531 mIntentTab = null;
532 } else {
533 if (mTabHost.getCurrentTab() == 0) {
534 updateBody();
535 } else {
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700536 mTabHost.setCurrentTab(0);
537 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700538 }
539 }
540
Jeff Sharkey8a503642011-06-10 13:31:21 -0700541 /**
542 * Factory that provide empty {@link View} to make {@link TabHost} happy.
543 */
544 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
545 /** {@inheritDoc} */
546 public View createTabContent(String tag) {
547 return new View(mTabHost.getContext());
548 }
549 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700550
Jeff Sharkey8a503642011-06-10 13:31:21 -0700551 /**
552 * Build {@link TabSpec} with thin indicator, and empty content.
553 */
554 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700555 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
556 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700557 }
558
559 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
560 /** {@inheritDoc} */
561 public void onTabChanged(String tabId) {
562 // user changed tab; update body
563 updateBody();
564 }
565 };
566
567 /**
568 * Update body content based on current tab. Loads
569 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
570 * binds them to visible controls.
571 */
572 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700573 mBinding = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700574
Jeff Sharkeya662e492011-06-18 21:57:06 -0700575 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700576 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700577
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700578 if (currentTab == null) {
579 Log.w(TAG, "no tab selected; hiding body");
580 mListView.setVisibility(View.GONE);
581 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700582 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700583 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700584 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700585
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700586 final boolean tabChanged = !currentTab.equals(mCurrentTab);
587 mCurrentTab = currentTab;
588
Jeff Sharkey8a503642011-06-10 13:31:21 -0700589 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
590
591 if (TAB_WIFI.equals(currentTab)) {
592 // wifi doesn't have any controls
593 mDataEnabledView.setVisibility(View.GONE);
594 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700595 mTemplate = buildTemplateWifi();
596
597 } else if (TAB_ETHERNET.equals(currentTab)) {
598 // ethernet doesn't have any controls
599 mDataEnabledView.setVisibility(View.GONE);
600 mDisableAtLimitView.setVisibility(View.GONE);
601 mTemplate = buildTemplateEthernet();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700602
603 } else {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700604 mDataEnabledView.setVisibility(View.VISIBLE);
605 mDisableAtLimitView.setVisibility(View.VISIBLE);
606 }
607
608 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700609 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
610 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
611 mDataEnabled.setChecked(mConnService.getMobileDataEnabled());
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700612 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700613
614 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700615 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
616 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
617 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700618 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700619
620 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700621 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
622 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
623 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700624 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700625 }
626
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700627 try {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700628 // load stats for current template
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700629 mHistory = mStatsService.getHistoryForNetwork(
630 mTemplate, FIELD_RX_BYTES | FIELD_TX_BYTES);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700631 } catch (RemoteException e) {
632 // since we can't do much without policy or history, and we don't
633 // want to leave with half-baked UI, we bail hard.
634 throw new RuntimeException("problem reading network policy or stats", e);
635 }
636
Jeff Sharkey8a503642011-06-10 13:31:21 -0700637 // bind chart to historical stats
Jeff Sharkey8a503642011-06-10 13:31:21 -0700638 mChart.bindNetworkStats(mHistory);
639
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700640 // only update policy when switching tabs
641 updatePolicy(tabChanged);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700642 updateAppDetail();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700643
644 // force scroll to top of body
645 mListView.smoothScrollToPosition(0);
646
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700647 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700648 }
649
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700650 private boolean isAppDetailMode() {
651 return mUid != UID_NONE;
652 }
653
654 /**
655 * Update UID details panels to match {@link #mUid}, showing or hiding them
656 * depending on {@link #isAppDetailMode()}.
657 */
658 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700659 final Context context = getActivity();
660 final PackageManager pm = context.getPackageManager();
661 final LayoutInflater inflater = getActivity().getLayoutInflater();
662
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700663 if (isAppDetailMode()) {
664 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700665 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700666 } else {
667 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700668 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700669
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700670 mDetailHistory = null;
671 mDetailHistoryDefault = null;
672 mDetailHistoryForeground = null;
673
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700674 // hide detail stats when not in detail mode
675 mChart.bindDetailNetworkStats(null);
676 return;
677 }
678
679 // remove warning/limit sweeps while in detail mode
680 mChart.bindNetworkPolicy(null);
681
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700682 // show icon and all labels appearing under this app
683 final UidDetail detail = resolveDetailForUid(context, mUid);
684 mAppIcon.setImageDrawable(detail.icon);
685
686 mAppTitles.removeAllViews();
687 if (detail.detailLabels != null) {
688 for (CharSequence label : detail.detailLabels) {
689 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label));
690 }
691 } else {
692 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label));
693 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700694
695 // enable settings button when package provides it
696 // TODO: target torwards entire UID instead of just first package
697 final String[] packageNames = pm.getPackagesForUid(mUid);
698 if (packageNames != null && packageNames.length > 0) {
699 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
700 mAppSettingsIntent.setPackage(packageNames[0]);
701 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
702
703 final boolean matchFound = pm.resolveActivity(mAppSettingsIntent, 0) != null;
704 mAppSettings.setEnabled(matchFound);
705
706 } else {
707 mAppSettingsIntent = null;
708 mAppSettings.setEnabled(false);
709 }
710
711 try {
712 // load stats for current uid and template
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700713 mDetailHistoryDefault = mStatsService.getHistoryForUid(
714 mTemplate, mUid, SET_DEFAULT, TAG_NONE, FIELD_RX_BYTES | FIELD_TX_BYTES);
715 mDetailHistoryForeground = mStatsService.getHistoryForUid(
716 mTemplate, mUid, SET_FOREGROUND, TAG_NONE, FIELD_RX_BYTES | FIELD_TX_BYTES);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700717 } catch (RemoteException e) {
718 // since we can't do much without history, and we don't want to
719 // leave with half-baked UI, we bail hard.
720 throw new RuntimeException("problem reading network stats", e);
721 }
722
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700723 mDetailHistory = new NetworkStatsHistory(mDetailHistoryForeground.getBucketDuration());
724 mDetailHistory.recordEntireHistory(mDetailHistoryDefault);
725 mDetailHistory.recordEntireHistory(mDetailHistoryForeground);
726
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700727 // bind chart to historical stats
728 mChart.bindDetailNetworkStats(mDetailHistory);
729
730 updateDetailData();
731
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700732 if (NetworkPolicyManager.isUidValidForPolicy(context, mUid) && !getRestrictBackground()
733 && isBandwidthControlEnabled()) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700734 mAppRestrictView.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700735 mAppRestrict.setChecked(getAppRestrictBackground());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700736
737 } else {
738 mAppRestrictView.setVisibility(View.GONE);
739 }
740
741 }
742
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700743 private void setPolicyCycleDay(int cycleDay) {
744 if (LOGD) Log.d(TAG, "setPolicyCycleDay()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700745 mPolicyEditor.setPolicyCycleDay(mTemplate, cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700746 updatePolicy(true);
747 }
748
749 private void setPolicyWarningBytes(long warningBytes) {
750 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700751 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700752 updatePolicy(false);
753 }
754
755 private void setPolicyLimitBytes(long limitBytes) {
756 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700757 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700758 updatePolicy(false);
759 }
760
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700761 private boolean isNetworkPolicyModifiable() {
762 return isBandwidthControlEnabled() && mDataEnabled.isChecked();
763 }
764
765 private boolean isBandwidthControlEnabled() {
766 try {
767 return mNetworkService.isBandwidthControlEnabled();
768 } catch (RemoteException e) {
769 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
770 return false;
771 }
772 }
773
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700774 private boolean getDataRoaming() {
775 final ContentResolver resolver = getActivity().getContentResolver();
776 return Settings.Secure.getInt(resolver, Settings.Secure.DATA_ROAMING, 0) != 0;
777 }
778
779 private void setDataRoaming(boolean enabled) {
780 // TODO: teach telephony DataConnectionTracker to watch and apply
781 // updates when changed.
782 final ContentResolver resolver = getActivity().getContentResolver();
783 Settings.Secure.putInt(resolver, Settings.Secure.DATA_ROAMING, enabled ? 1 : 0);
784 mMenuDataRoaming.setChecked(enabled);
785 }
786
787 private boolean getRestrictBackground() {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700788 try {
789 return mPolicyService.getRestrictBackground();
790 } catch (RemoteException e) {
791 Log.w(TAG, "problem talking with policy service: " + e);
792 return false;
793 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700794 }
795
796 private void setRestrictBackground(boolean restrictBackground) {
797 if (LOGD) Log.d(TAG, "setRestrictBackground()");
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700798 try {
799 mPolicyService.setRestrictBackground(restrictBackground);
800 mMenuRestrictBackground.setChecked(restrictBackground);
801 } catch (RemoteException e) {
802 Log.w(TAG, "problem talking with policy service: " + e);
803 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700804 }
805
806 private boolean getAppRestrictBackground() {
807 final int uidPolicy;
808 try {
809 uidPolicy = mPolicyService.getUidPolicy(mUid);
810 } catch (RemoteException e) {
811 // since we can't do much without policy, we bail hard.
812 throw new RuntimeException("problem reading network policy", e);
813 }
814
815 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
816 }
817
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700818 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700819 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700820 try {
821 mPolicyService.setUidPolicy(
822 mUid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
823 } catch (RemoteException e) {
824 throw new RuntimeException("unable to save policy", e);
825 }
826
827 mAppRestrict.setChecked(restrictBackground);
828 }
829
Jeff Sharkey8a503642011-06-10 13:31:21 -0700830 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700831 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
832 * current {@link #mTemplate}.
833 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700834 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700835 if (isAppDetailMode()) {
836 mNetworkSwitches.setVisibility(View.GONE);
837 // we fall through to update cycle list for detail mode
838 } else {
839 mNetworkSwitches.setVisibility(View.VISIBLE);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700840
841 // when heading back to summary without cycle refresh, kick details
842 // update to repopulate list.
843 if (!refreshCycle) {
844 updateDetailData();
845 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700846 }
847
Jeff Sharkeya662e492011-06-18 21:57:06 -0700848 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700849 if (isNetworkPolicyModifiable()) {
850 mDisableAtLimitView.setVisibility(View.VISIBLE);
851 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700852 if (!isAppDetailMode()) {
853 mChart.bindNetworkPolicy(policy);
854 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700855
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700856 } else {
857 // controls are disabled; don't bind warning/limit sweeps
858 mDisableAtLimitView.setVisibility(View.GONE);
859 mChart.bindNetworkPolicy(null);
860 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700861
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700862 if (refreshCycle) {
863 // generate cycle list based on policy and available history
864 updateCycleList(policy);
865 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700866 }
867
868 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700869 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
870 * and available {@link NetworkStatsHistory} data. Always selects the newest
871 * item, updating the inspection range on {@link #mChart}.
872 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700873 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700874 mCycleAdapter.clear();
875
876 final Context context = mCycleSpinner.getContext();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700877 long historyStart = mHistory.getStart();
878 long historyEnd = mHistory.getEnd();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700879
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700880 if (historyStart == Long.MAX_VALUE || historyEnd == Long.MIN_VALUE) {
881 historyStart = System.currentTimeMillis();
882 historyEnd = System.currentTimeMillis();
883 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700884
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700885 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700886 if (policy != null) {
887 // find the next cycle boundary
888 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700889
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700890 int guardCount = 0;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700891
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700892 // walk backwards, generating all valid cycle ranges
893 while (cycleEnd > historyStart) {
894 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
895 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
896 + historyStart);
897 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
898 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700899 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700900
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700901 // TODO: remove this guard once we have better testing
902 if (guardCount++ > 50) {
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700903 Log.wtf(TAG, "stuck generating ranges for historyStart=" + historyStart
904 + ", historyEnd=" + historyEnd + " and policy=" + policy);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700905 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700906 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700907
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700908 // one last cycle entry to modify policy cycle day
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700909 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable());
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700910 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700911
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700912 if (!hasCycles) {
913 // no valid cycles; show all data
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700914 // TODO: offer simple ranges like "last week" etc
915 mCycleAdapter.add(new CycleItem(context, historyStart, historyEnd));
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700916 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700917 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700918
919 // force pick the current cycle (first item)
920 mCycleSpinner.setSelection(0);
921 mCycleListener.onItemSelected(mCycleSpinner, null, 0, 0);
922 }
923
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700924 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700925 /** {@inheritDoc} */
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700926 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
927 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700928
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700929 final boolean dataEnabled = isChecked;
930 mDataEnabled.setChecked(dataEnabled);
931
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700932 final String currentTab = mCurrentTab;
933 if (TAB_MOBILE.equals(currentTab)) {
934 mConnService.setMobileDataEnabled(dataEnabled);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700935 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700936
937 // rebind policy to match radio state
938 updatePolicy(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700939 }
940 };
941
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700942 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700943 /** {@inheritDoc} */
944 public void onClick(View v) {
945 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700946 if (disableAtLimit) {
947 // enabling limit; show confirmation dialog which eventually
948 // calls setPolicyLimitBytes() once user confirms.
949 ConfirmLimitFragment.show(DataUsageSummary.this);
950 } else {
951 setPolicyLimitBytes(LIMIT_DISABLED);
952 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700953 }
954 };
955
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700956 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
957 /** {@inheritDoc} */
958 public void onClick(View v) {
959 final boolean restrictBackground = !mAppRestrict.isChecked();
960
961 if (restrictBackground) {
962 // enabling restriction; show confirmation dialog which
963 // eventually calls setRestrictBackground() once user confirms.
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700964 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700965 } else {
966 setAppRestrictBackground(false);
967 }
968 }
969 };
970
971 private OnClickListener mAppSettingsListener = new OnClickListener() {
972 /** {@inheritDoc} */
973 public void onClick(View v) {
974 // TODO: target torwards entire UID instead of just first package
975 startActivity(mAppSettingsIntent);
976 }
977 };
978
Jeff Sharkey8a503642011-06-10 13:31:21 -0700979 private OnItemClickListener mListListener = new OnItemClickListener() {
980 /** {@inheritDoc} */
981 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700982 final AppUsageItem app = (AppUsageItem) parent.getItemAtPosition(position);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700983 AppDetailsFragment.show(DataUsageSummary.this, app.uid);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700984 }
985 };
986
987 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
988 /** {@inheritDoc} */
989 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
990 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
991 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700992 // show cycle editor; will eventually call setPolicyCycleDay()
993 // when user finishes editing.
994 CycleEditorFragment.show(DataUsageSummary.this);
995
996 // reset spinner to something other than "change cycle..."
997 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700998
999 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001000 if (LOGD) {
1001 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1002 + cycle.end + "]");
1003 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001004
1005 // update chart to show selected cycle, and update detail data
1006 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001007 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001008
1009 updateDetailData();
1010 }
1011 }
1012
1013 /** {@inheritDoc} */
1014 public void onNothingSelected(AdapterView<?> parent) {
1015 // ignored
1016 }
1017 };
1018
1019 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001020 * Update details based on {@link #mChart} inspection range depending on
1021 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1022 * of applications data usage, and when {@link #isAppDetailMode()} update
1023 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001024 */
1025 private void updateDetailData() {
1026 if (LOGD) Log.d(TAG, "updateDetailData()");
1027
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001028 final long start = mChart.getInspectStart();
1029 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001030 final long now = System.currentTimeMillis();
1031
1032 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001033
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001034 NetworkStatsHistory.Entry entry = null;
1035 if (isAppDetailMode() && mDetailHistory != null) {
1036 // bind foreground/background to piechart and labels
1037 entry = mDetailHistoryDefault.getValues(start, end, now, entry);
1038 final long defaultBytes = entry.rxBytes + entry.txBytes;
1039 entry = mDetailHistoryForeground.getValues(start, end, now, entry);
1040 final long foregroundBytes = entry.rxBytes + entry.txBytes;
1041
1042 mAppPieChart.setOriginAngle(175);
1043
1044 mAppPieChart.removeAllSlices();
1045 mAppPieChart.addSlice(foregroundBytes, Color.parseColor("#d88d3a"));
1046 mAppPieChart.addSlice(defaultBytes, Color.parseColor("#666666"));
1047
1048 mAppPieChart.generatePath();
1049
1050 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1051 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1052
1053 // and finally leave with summary data for label below
1054 entry = mDetailHistory.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001055
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001056 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001057
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001058 } else {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001059 entry = mHistory.getValues(start, end, now, null);
1060
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001061 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001062 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001063 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryForAllUid);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001064 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001065
1066 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1067 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001068 final String rangePhrase = formatDateRange(context, start, end, false);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001069
1070 mUsageSummary.setText(
1071 getString(R.string.data_usage_total_during_range, totalPhrase, rangePhrase));
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001072 }
1073
1074 private final LoaderCallbacks<NetworkStats> mSummaryForAllUid = new LoaderCallbacks<
1075 NetworkStats>() {
1076 /** {@inheritDoc} */
1077 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
1078 return new SummaryForAllUidLoader(getActivity(), mStatsService, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001079 }
1080
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001081 /** {@inheritDoc} */
1082 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
1083 mAdapter.bindStats(data);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001084 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001085 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001086
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001087 /** {@inheritDoc} */
1088 public void onLoaderReset(Loader<NetworkStats> loader) {
1089 mAdapter.bindStats(null);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001090 updateEmptyVisible();
1091 }
1092
1093 private void updateEmptyVisible() {
1094 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1095 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001096 }
1097 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001098
Jeff Sharkeya662e492011-06-18 21:57:06 -07001099 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001100 final Context context = getActivity();
1101 if (hasMobileRadio(context)) {
1102 final String subscriberId = getActiveSubscriberId(context);
1103 return mPolicyEditor.isMobilePolicySplit(subscriberId);
1104 } else {
1105 return false;
1106 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001107 }
1108
1109 private void setMobilePolicySplit(boolean split) {
1110 final String subscriberId = getActiveSubscriberId(getActivity());
1111 mPolicyEditor.setMobilePolicySplit(subscriberId, split);
1112 }
1113
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001114 private static String getActiveSubscriberId(Context context) {
1115 final TelephonyManager telephony = (TelephonyManager) context.getSystemService(
1116 Context.TELEPHONY_SERVICE);
1117 return telephony.getSubscriberId();
1118 }
1119
Jeff Sharkey8a503642011-06-10 13:31:21 -07001120 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
1121 /** {@inheritDoc} */
1122 public void onInspectRangeChanged() {
1123 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
1124 updateDetailData();
1125 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001126
Jeff Sharkey8a503642011-06-10 13:31:21 -07001127 /** {@inheritDoc} */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001128 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001129 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001130 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001131
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001132 /** {@inheritDoc} */
1133 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001134 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001135 }
1136 };
1137
1138
1139 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001140 * List item that reflects a specific data usage cycle.
1141 */
1142 public static class CycleItem {
1143 public CharSequence label;
1144 public long start;
1145 public long end;
1146
Jeff Sharkey8a503642011-06-10 13:31:21 -07001147 CycleItem(CharSequence label) {
1148 this.label = label;
1149 }
1150
1151 public CycleItem(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001152 this.label = formatDateRange(context, start, end, true);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001153 this.start = start;
1154 this.end = end;
1155 }
1156
Jeff Sharkey8a503642011-06-10 13:31:21 -07001157 @Override
1158 public String toString() {
1159 return label.toString();
1160 }
1161 }
1162
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001163 private static final StringBuilder sBuilder = new StringBuilder(50);
1164 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1165 sBuilder, Locale.getDefault());
1166
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001167 public static String formatDateRange(Context context, long start, long end, boolean utcTime) {
1168 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
1169 final String timezone = utcTime ? TIMEZONE_UTC : null;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001170
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001171 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001172 sBuilder.setLength(0);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001173 return DateUtils
1174 .formatDateRange(context, sFormatter, start, end, flags, timezone).toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001175 }
1176 }
1177
Jeff Sharkey8a503642011-06-10 13:31:21 -07001178 /**
1179 * Special-case data usage cycle that triggers dialog to change
1180 * {@link NetworkPolicy#cycleDay}.
1181 */
1182 public static class CycleChangeItem extends CycleItem {
1183 public CycleChangeItem(Context context) {
1184 super(context.getString(R.string.data_usage_change_cycle));
1185 }
1186 }
1187
1188 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001189 private boolean mChangePossible = false;
1190 private boolean mChangeVisible = false;
1191
1192 private final CycleChangeItem mChangeItem;
1193
Jeff Sharkey8a503642011-06-10 13:31:21 -07001194 public CycleAdapter(Context context) {
1195 super(context, android.R.layout.simple_spinner_item);
1196 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001197 mChangeItem = new CycleChangeItem(context);
1198 }
1199
1200 public void setChangePossible(boolean possible) {
1201 mChangePossible = possible;
1202 updateChange();
1203 }
1204
1205 public void setChangeVisible(boolean visible) {
1206 mChangeVisible = visible;
1207 updateChange();
1208 }
1209
1210 private void updateChange() {
1211 remove(mChangeItem);
1212 if (mChangePossible && mChangeVisible) {
1213 add(mChangeItem);
1214 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001215 }
1216 }
1217
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001218 private static class AppUsageItem implements Comparable<AppUsageItem> {
1219 public int uid;
1220 public long total;
1221
1222 /** {@inheritDoc} */
1223 public int compareTo(AppUsageItem another) {
1224 return Long.compare(another.total, total);
1225 }
1226 }
1227
Jeff Sharkey8a503642011-06-10 13:31:21 -07001228 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001229 * Adapter of applications, sorted by total usage descending.
1230 */
1231 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001232 private ArrayList<AppUsageItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001233 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001234
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001235 /**
1236 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1237 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001238 public void bindStats(NetworkStats stats) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001239 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001240
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001241 final AppUsageItem systemItem = new AppUsageItem();
1242 systemItem.uid = android.os.Process.SYSTEM_UID;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001243
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001244 final SparseArray<AppUsageItem> knownUids = new SparseArray<AppUsageItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001245
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001246 NetworkStats.Entry entry = null;
1247 final int size = stats != null ? stats.size() : 0;
1248 for (int i = 0; i < size; i++) {
1249 entry = stats.getValues(i, entry);
1250
1251 final int uid = entry.uid;
1252 final boolean isApp = uid >= android.os.Process.FIRST_APPLICATION_UID
1253 && uid <= android.os.Process.LAST_APPLICATION_UID;
1254 if (isApp || uid == TrafficStats.UID_REMOVED) {
1255 AppUsageItem item = knownUids.get(uid);
1256 if (item == null) {
1257 item = new AppUsageItem();
1258 item.uid = uid;
1259 knownUids.put(uid, item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001260 mItems.add(item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001261 }
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001262
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001263 item.total += entry.rxBytes + entry.txBytes;
1264 } else {
1265 systemItem.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001266 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001267 }
1268
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001269 if (systemItem.total > 0) {
1270 mItems.add(systemItem);
1271 }
1272
Jeff Sharkey8a503642011-06-10 13:31:21 -07001273 Collections.sort(mItems);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001274 mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001275 notifyDataSetChanged();
1276 }
1277
1278 @Override
1279 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001280 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001281 }
1282
1283 @Override
1284 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001285 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001286 }
1287
1288 @Override
1289 public long getItemId(int position) {
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001290 return mItems.get(position).uid;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001291 }
1292
1293 @Override
1294 public View getView(int position, View convertView, ViewGroup parent) {
1295 if (convertView == null) {
1296 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey52c3f442011-06-23 00:39:38 -07001297 R.layout.data_usage_item, parent, false);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001298 }
1299
1300 final Context context = parent.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001301
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001302 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001303 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
1304 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
1305 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1306 android.R.id.progress);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001307
Jeff Sharkey8a503642011-06-10 13:31:21 -07001308 final AppUsageItem item = mItems.get(position);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001309 final UidDetail detail = resolveDetailForUid(context, item.uid);
1310
1311 icon.setImageDrawable(detail.icon);
1312 title.setText(detail.label);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001313 summary.setText(Formatter.formatFileSize(context, item.total));
1314
1315 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1316 progress.setProgress(percentTotal);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001317
1318 return convertView;
1319 }
1320
1321 }
1322
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001323 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001324 * Empty {@link Fragment} that controls display of UID details in
1325 * {@link DataUsageSummary}.
1326 */
1327 public static class AppDetailsFragment extends Fragment {
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001328 private static final String EXTRA_UID = "uid";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001329
1330 public static void show(DataUsageSummary parent, int uid) {
1331 final Bundle args = new Bundle();
1332 args.putInt(EXTRA_UID, uid);
1333
1334 final AppDetailsFragment fragment = new AppDetailsFragment();
1335 fragment.setArguments(args);
1336 fragment.setTargetFragment(parent, 0);
1337
1338 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1339 ft.add(fragment, TAG_APP_DETAILS);
1340 ft.addToBackStack(TAG_APP_DETAILS);
1341 ft.commit();
1342 }
1343
1344 @Override
1345 public void onStart() {
1346 super.onStart();
1347 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1348 target.mUid = getArguments().getInt(EXTRA_UID);
1349 target.updateBody();
1350 }
1351
1352 @Override
1353 public void onStop() {
1354 super.onStop();
1355 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1356 target.mUid = UID_NONE;
1357 target.updateBody();
1358 }
1359 }
1360
1361 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001362 * Dialog to request user confirmation before setting
1363 * {@link NetworkPolicy#limitBytes}.
1364 */
1365 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001366 private static final String EXTRA_MESSAGE_ID = "messageId";
1367 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001368
1369 public static void show(DataUsageSummary parent) {
1370 final Bundle args = new Bundle();
1371
1372 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001373 final String currentTab = parent.mCurrentTab;
1374 if (TAB_3G.equals(currentTab)) {
1375 args.putInt(EXTRA_MESSAGE_ID, R.string.data_usage_limit_dialog_3g);
1376 args.putLong(EXTRA_LIMIT_BYTES, 5 * GB_IN_BYTES);
1377 } else if (TAB_4G.equals(currentTab)) {
1378 args.putInt(EXTRA_MESSAGE_ID, R.string.data_usage_limit_dialog_4g);
1379 args.putLong(EXTRA_LIMIT_BYTES, 5 * GB_IN_BYTES);
1380 } else if (TAB_MOBILE.equals(currentTab)) {
1381 args.putInt(EXTRA_MESSAGE_ID, R.string.data_usage_limit_dialog_mobile);
1382 args.putLong(EXTRA_LIMIT_BYTES, 5 * GB_IN_BYTES);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001383 }
1384
1385 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1386 dialog.setArguments(args);
1387 dialog.setTargetFragment(parent, 0);
1388 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1389 }
1390
1391 @Override
1392 public Dialog onCreateDialog(Bundle savedInstanceState) {
1393 final Context context = getActivity();
1394
1395 final int messageId = getArguments().getInt(EXTRA_MESSAGE_ID);
1396 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1397
1398 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1399 builder.setTitle(R.string.data_usage_limit_dialog_title);
1400 builder.setMessage(messageId);
1401
1402 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1403 public void onClick(DialogInterface dialog, int which) {
1404 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1405 if (target != null) {
1406 target.setPolicyLimitBytes(limitBytes);
1407 }
1408 }
1409 });
1410
1411 return builder.create();
1412 }
1413 }
1414
1415 /**
1416 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1417 */
1418 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001419 private static final String EXTRA_CYCLE_DAY = "cycleDay";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001420
1421 public static void show(DataUsageSummary parent) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07001422 final NetworkPolicy policy = parent.mPolicyEditor.getPolicy(parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001423 final Bundle args = new Bundle();
1424 args.putInt(CycleEditorFragment.EXTRA_CYCLE_DAY, policy.cycleDay);
1425
1426 final CycleEditorFragment dialog = new CycleEditorFragment();
1427 dialog.setArguments(args);
1428 dialog.setTargetFragment(parent, 0);
1429 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1430 }
1431
1432 @Override
1433 public Dialog onCreateDialog(Bundle savedInstanceState) {
1434 final Context context = getActivity();
1435
1436 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1437 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1438
1439 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1440 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1441
1442 final int oldCycleDay = getArguments().getInt(EXTRA_CYCLE_DAY, 1);
1443
1444 cycleDayPicker.setMinValue(1);
1445 cycleDayPicker.setMaxValue(31);
1446 cycleDayPicker.setValue(oldCycleDay);
1447 cycleDayPicker.setWrapSelectorWheel(true);
1448
1449 builder.setTitle(R.string.data_usage_cycle_editor_title);
1450 builder.setView(view);
1451
1452 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1453 new DialogInterface.OnClickListener() {
1454 public void onClick(DialogInterface dialog, int which) {
1455 final int cycleDay = cycleDayPicker.getValue();
1456 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1457 if (target != null) {
1458 target.setPolicyCycleDay(cycleDay);
1459 }
1460 }
1461 });
1462
1463 return builder.create();
1464 }
1465 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001466
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001467 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001468 * Dialog to request user confirmation before setting
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001469 * {@link Settings.Secure#DATA_ROAMING}.
1470 */
1471 public static class ConfirmDataRoamingFragment extends DialogFragment {
1472 public static void show(DataUsageSummary parent) {
1473 final Bundle args = new Bundle();
1474
1475 final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment();
1476 dialog.setTargetFragment(parent, 0);
1477 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_ROAMING);
1478 }
1479
1480 @Override
1481 public Dialog onCreateDialog(Bundle savedInstanceState) {
1482 final Context context = getActivity();
1483
1484 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1485 builder.setTitle(R.string.roaming_reenable_title);
1486 builder.setMessage(R.string.roaming_warning);
1487
1488 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1489 public void onClick(DialogInterface dialog, int which) {
1490 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1491 if (target != null) {
1492 target.setDataRoaming(true);
1493 }
1494 }
1495 });
1496 builder.setNegativeButton(android.R.string.cancel, null);
1497
1498 return builder.create();
1499 }
1500 }
1501
1502 /**
1503 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001504 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001505 */
1506 public static class ConfirmRestrictFragment extends DialogFragment {
1507 public static void show(DataUsageSummary parent) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001508 final Bundle args = new Bundle();
1509
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001510 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1511 dialog.setTargetFragment(parent, 0);
1512 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1513 }
1514
1515 @Override
1516 public Dialog onCreateDialog(Bundle savedInstanceState) {
1517 final Context context = getActivity();
1518
1519 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001520 builder.setTitle(R.string.data_usage_restrict_background_title);
1521 builder.setMessage(R.string.data_usage_restrict_background);
1522
1523 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1524 public void onClick(DialogInterface dialog, int which) {
1525 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1526 if (target != null) {
1527 target.setRestrictBackground(true);
1528 }
1529 }
1530 });
1531 builder.setNegativeButton(android.R.string.cancel, null);
1532
1533 return builder.create();
1534 }
1535 }
1536
1537 /**
1538 * Dialog to request user confirmation before setting
1539 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
1540 */
1541 public static class ConfirmAppRestrictFragment extends DialogFragment {
1542 public static void show(DataUsageSummary parent) {
1543 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
1544 dialog.setTargetFragment(parent, 0);
1545 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
1546 }
1547
1548 @Override
1549 public Dialog onCreateDialog(Bundle savedInstanceState) {
1550 final Context context = getActivity();
1551
1552 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001553 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
1554 builder.setMessage(R.string.data_usage_app_restrict_dialog);
1555
1556 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1557 public void onClick(DialogInterface dialog, int which) {
1558 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1559 if (target != null) {
1560 target.setAppRestrictBackground(true);
1561 }
1562 }
1563 });
1564 builder.setNegativeButton(android.R.string.cancel, null);
1565
1566 return builder.create();
1567 }
1568 }
1569
1570 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001571 * Compute default tab that should be selected, based on
1572 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
1573 */
1574 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07001575 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
1576 if (template == null) return null;
1577
1578 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07001579 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001580 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001581 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001582 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001583 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001584 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001585 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001586 return TAB_WIFI;
1587 default:
1588 return null;
1589 }
1590 }
1591
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001592 public static class UidDetail {
1593 public CharSequence label;
1594 public CharSequence[] detailLabels;
1595 public Drawable icon;
1596 }
1597
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001598 /**
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001599 * Resolve best descriptive label for the given UID.
1600 */
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001601 public static UidDetail resolveDetailForUid(Context context, int uid) {
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001602 final Resources res = context.getResources();
1603 final PackageManager pm = context.getPackageManager();
1604
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001605 final UidDetail detail = new UidDetail();
1606 detail.label = pm.getNameForUid(uid);
1607 detail.icon = pm.getDefaultActivityIcon();
1608
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001609 // handle special case labels
1610 switch (uid) {
1611 case android.os.Process.SYSTEM_UID:
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001612 detail.label = res.getString(R.string.process_kernel_label);
1613 detail.icon = pm.getDefaultActivityIcon();
1614 return detail;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001615 case TrafficStats.UID_REMOVED:
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001616 detail.label = res.getString(R.string.data_usage_uninstalled_apps);
1617 detail.icon = pm.getDefaultActivityIcon();
1618 return detail;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001619 }
1620
1621 // otherwise fall back to using packagemanager labels
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001622 final String[] packageNames = pm.getPackagesForUid(uid);
1623 final int length = packageNames != null ? packageNames.length : 0;
1624
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001625 try {
1626 if (length == 1) {
1627 final ApplicationInfo info = pm.getApplicationInfo(packageNames[0], 0);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001628 detail.label = info.loadLabel(pm).toString();
1629 detail.icon = info.loadIcon(pm);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001630 } else if (length > 1) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001631 detail.detailLabels = new CharSequence[length];
1632 for (int i = 0; i < length; i++) {
1633 final String packageName = packageNames[i];
1634 final PackageInfo packageInfo = pm.getPackageInfo(packageName, 0);
1635 final ApplicationInfo appInfo = pm.getApplicationInfo(packageName, 0);
1636
1637 detail.detailLabels[i] = appInfo.loadLabel(pm).toString();
1638 if (packageInfo.sharedUserLabel != 0) {
1639 detail.label = pm.getText(packageName, packageInfo.sharedUserLabel,
1640 packageInfo.applicationInfo).toString();
1641 detail.icon = appInfo.loadIcon(pm);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001642 }
1643 }
1644 }
1645 } catch (NameNotFoundException e) {
1646 }
1647
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001648 if (TextUtils.isEmpty(detail.label)) {
1649 detail.label = Integer.toString(uid);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001650 }
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001651 return detail;
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001652 }
1653
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001654 /**
1655 * Test if device has a mobile data radio.
1656 */
1657 private static boolean hasMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001658 if (TEST_RADIOS) {
1659 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
1660 }
1661
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001662 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
1663 Context.CONNECTIVITY_SERVICE);
1664
1665 // mobile devices should have MOBILE network tracker regardless of
1666 // connection status.
1667 return conn.getNetworkInfo(TYPE_MOBILE) != null;
1668 }
1669
1670 /**
1671 * Test if device has a mobile 4G data radio.
1672 */
1673 private static boolean hasMobile4gRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001674 if (TEST_RADIOS) {
1675 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
1676 }
1677
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001678 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
1679 Context.CONNECTIVITY_SERVICE);
1680 final TelephonyManager telephony = (TelephonyManager) context.getSystemService(
1681 Context.TELEPHONY_SERVICE);
1682
1683 // WiMAX devices should have WiMAX network tracker regardless of
1684 // connection status.
1685 final boolean hasWimax = conn.getNetworkInfo(TYPE_WIMAX) != null;
1686 final boolean hasLte = telephony.getLteOnCdmaMode() == Phone.LTE_ON_CDMA_TRUE;
1687 return hasWimax || hasLte;
1688 }
1689
1690 /**
1691 * Test if device has a Wi-Fi data radio.
1692 */
1693 private static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001694 if (TEST_RADIOS) {
1695 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
1696 }
1697
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001698 return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI);
1699 }
1700
1701 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001702 * Test if device has an ethernet network connection.
1703 */
1704 private static boolean hasEthernet(Context context) {
1705 if (TEST_RADIOS) {
1706 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
1707 }
1708
1709 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
1710 Context.CONNECTIVITY_SERVICE);
1711 return conn.getNetworkInfo(TYPE_ETHERNET) != null;
1712 }
1713
1714 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001715 * Inflate a {@link Preference} style layout, adding the given {@link View}
1716 * widget into {@link android.R.id#widget_frame}.
1717 */
1718 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
1719 final View view = inflater.inflate(R.layout.preference, root, false);
1720 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
1721 android.R.id.widget_frame);
1722 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
1723 return view;
1724 }
1725
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001726 private static View inflateAppTitle(
1727 LayoutInflater inflater, ViewGroup root, CharSequence label) {
1728 final TextView view = (TextView) inflater.inflate(
1729 R.layout.data_usage_app_title, root, false);
1730 view.setText(label);
1731 return view;
1732 }
1733
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001734 /**
1735 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07001736 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001737 */
1738 private static void setPreferenceTitle(View parent, int resId) {
1739 final TextView title = (TextView) parent.findViewById(android.R.id.title);
1740 title.setText(resId);
1741 }
1742
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001743 /**
1744 * Set {@link android.R.id#summary} for a preference view inflated with
1745 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
1746 */
1747 private static void setPreferenceSummary(View parent, int resId) {
1748 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
1749 summary.setVisibility(View.VISIBLE);
1750 summary.setText(resId);
1751 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001752}