blob: ac9300f60abd165cebc34666e30446b3af2f1fcf [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);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700325 mAppRestrictView.setOnClickListener(mAppRestrictListener);
326 mAppSwitches.addView(mAppRestrictView);
327 }
328
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700329 mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700330 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700331
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700332 // only assign layout transitions once first layout is finished
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700333 mListView.getViewTreeObserver().addOnGlobalLayoutListener(mFirstLayoutListener);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700334
335 mAdapter = new DataUsageAdapter();
336 mListView.setOnItemClickListener(mListListener);
337 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700338
339 return view;
340 }
341
342 @Override
343 public void onResume() {
344 super.onResume();
345
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700346 // pick default tab based on incoming intent
347 final Intent intent = getActivity().getIntent();
348 mIntentTab = computeTabFromIntent(intent);
349
Jeff Sharkey8a503642011-06-10 13:31:21 -0700350 // this kicks off chain reaction which creates tabs, binds the body to
351 // selected network, and binds chart, cycles and detail list.
352 updateTabs();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700353
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700354 // kick off background task to update stats
355 new AsyncTask<Void, Void, Void>() {
356 @Override
357 protected Void doInBackground(Void... params) {
358 try {
359 mStatsService.forceUpdate();
360 } catch (RemoteException e) {
361 }
362 return null;
363 }
364
365 @Override
366 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700367 if (isAdded()) {
368 updateBody();
369 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700370 }
371 }.execute();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700372 }
373
Jeff Sharkey8a503642011-06-10 13:31:21 -0700374 @Override
375 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
376 inflater.inflate(R.menu.data_usage, menu);
377 }
378
379 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700380 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700381 final Context context = getActivity();
382
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700383 mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming);
384 mMenuDataRoaming.setVisible(hasMobileRadio(context));
385 mMenuDataRoaming.setChecked(getDataRoaming());
386
387 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
388 mMenuRestrictBackground.setChecked(getRestrictBackground());
389
390 final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700391 split4g.setVisible(hasMobile4gRadio(context));
Jeff Sharkeya662e492011-06-18 21:57:06 -0700392 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700393
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700394 final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700395 if (hasWifiRadio(context) && hasMobileRadio(context)) {
396 showWifi.setVisible(true);
397 showWifi.setChecked(mShowWifi);
398 } else {
399 showWifi.setVisible(false);
400 mShowWifi = true;
401 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700402
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700403 final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
404 if (hasEthernet(context) && hasMobileRadio(context)) {
405 showEthernet.setVisible(true);
406 showEthernet.setChecked(mShowEthernet);
407 } else {
408 showEthernet.setVisible(false);
409 mShowEthernet = true;
410 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700411 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700412
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700413 @Override
414 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700415 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700416 case R.id.data_usage_menu_roaming: {
417 final boolean dataRoaming = !item.isChecked();
418 if (dataRoaming) {
419 ConfirmDataRoamingFragment.show(this);
420 } else {
421 // no confirmation to disable roaming
422 setDataRoaming(false);
423 }
424 return true;
425 }
426 case R.id.data_usage_menu_restrict_background: {
427 final boolean restrictBackground = !item.isChecked();
428 if (restrictBackground) {
429 ConfirmRestrictFragment.show(this);
430 } else {
431 // no confirmation to drop restriction
432 setRestrictBackground(false);
433 }
434 return true;
435 }
436 case R.id.data_usage_menu_split_4g: {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700437 final boolean mobileSplit = !item.isChecked();
Jeff Sharkeya662e492011-06-18 21:57:06 -0700438 setMobilePolicySplit(mobileSplit);
439 item.setChecked(isMobilePolicySplit());
Jeff Sharkey8a503642011-06-10 13:31:21 -0700440 updateTabs();
441 return true;
442 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700443 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700444 mShowWifi = !item.isChecked();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700445 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700446 item.setChecked(mShowWifi);
447 updateTabs();
448 return true;
449 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700450 case R.id.data_usage_menu_show_ethernet: {
451 mShowEthernet = !item.isChecked();
452 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
453 item.setChecked(mShowEthernet);
454 updateTabs();
455 return true;
456 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700457 }
458 return false;
459 }
460
Jeff Sharkey94a90952011-06-13 22:31:09 -0700461 @Override
462 public void onDestroyView() {
463 super.onDestroyView();
464
465 mDataEnabledView = null;
466 mDisableAtLimitView = null;
467 }
468
Jeff Sharkey8a503642011-06-10 13:31:21 -0700469 /**
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700470 * Listener to setup {@link LayoutTransition} after first layout pass.
471 */
472 private OnGlobalLayoutListener mFirstLayoutListener = new OnGlobalLayoutListener() {
473 /** {@inheritDoc} */
474 public void onGlobalLayout() {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700475 mListView.getViewTreeObserver().removeGlobalOnLayoutListener(mFirstLayoutListener);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700476
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700477 mTabsContainer.setLayoutTransition(buildLayoutTransition());
478 mHeader.setLayoutTransition(buildLayoutTransition());
479 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700480
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700481 final LayoutTransition chartTransition = buildLayoutTransition();
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700482 chartTransition.setStartDelay(LayoutTransition.APPEARING, 0);
483 chartTransition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
484 mChart.setLayoutTransition(chartTransition);
485 }
486 };
487
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700488 private static LayoutTransition buildLayoutTransition() {
489 final LayoutTransition transition = new LayoutTransition();
490 if (TEST_ANIM) {
491 transition.setDuration(1500);
492 }
493 transition.setAnimateParentHierarchy(false);
494 return transition;
495 }
496
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700497 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700498 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700499 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
500 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700501 */
502 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700503 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700504 mTabHost.clearAllTabs();
505
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700506 final boolean mobileSplit = isMobilePolicySplit();
507 if (mobileSplit && hasMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700508 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
509 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700510 } else if (hasMobileRadio(context)) {
511 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700512 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700513 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700514 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
515 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700516 if (mShowEthernet && hasEthernet(context)) {
517 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
518 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700519
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700520 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
521 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
522 if (mIntentTab != null) {
523 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
524 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700525 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700526 mTabHost.setCurrentTabByTag(mIntentTab);
527 }
528 mIntentTab = null;
529 } else {
530 if (mTabHost.getCurrentTab() == 0) {
531 updateBody();
532 } else {
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700533 mTabHost.setCurrentTab(0);
534 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700535 }
536 }
537
Jeff Sharkey8a503642011-06-10 13:31:21 -0700538 /**
539 * Factory that provide empty {@link View} to make {@link TabHost} happy.
540 */
541 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
542 /** {@inheritDoc} */
543 public View createTabContent(String tag) {
544 return new View(mTabHost.getContext());
545 }
546 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700547
Jeff Sharkey8a503642011-06-10 13:31:21 -0700548 /**
549 * Build {@link TabSpec} with thin indicator, and empty content.
550 */
551 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700552 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
553 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700554 }
555
556 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
557 /** {@inheritDoc} */
558 public void onTabChanged(String tabId) {
559 // user changed tab; update body
560 updateBody();
561 }
562 };
563
564 /**
565 * Update body content based on current tab. Loads
566 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
567 * binds them to visible controls.
568 */
569 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700570 mBinding = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700571
Jeff Sharkeya662e492011-06-18 21:57:06 -0700572 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700573 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700574
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700575 if (currentTab == null) {
576 Log.w(TAG, "no tab selected; hiding body");
577 mListView.setVisibility(View.GONE);
578 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700579 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700580 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700581 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700582
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700583 final boolean tabChanged = !currentTab.equals(mCurrentTab);
584 mCurrentTab = currentTab;
585
Jeff Sharkey8a503642011-06-10 13:31:21 -0700586 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
587
588 if (TAB_WIFI.equals(currentTab)) {
589 // wifi doesn't have any controls
590 mDataEnabledView.setVisibility(View.GONE);
591 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700592 mTemplate = buildTemplateWifi();
593
594 } else if (TAB_ETHERNET.equals(currentTab)) {
595 // ethernet doesn't have any controls
596 mDataEnabledView.setVisibility(View.GONE);
597 mDisableAtLimitView.setVisibility(View.GONE);
598 mTemplate = buildTemplateEthernet();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700599
600 } else {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700601 mDataEnabledView.setVisibility(View.VISIBLE);
602 mDisableAtLimitView.setVisibility(View.VISIBLE);
603 }
604
605 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700606 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
607 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
608 mDataEnabled.setChecked(mConnService.getMobileDataEnabled());
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700609 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700610
611 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700612 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
613 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
614 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700615 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700616
617 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700618 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
619 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
620 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700621 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700622 }
623
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700624 try {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700625 // load stats for current template
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700626 mHistory = mStatsService.getHistoryForNetwork(
627 mTemplate, FIELD_RX_BYTES | FIELD_TX_BYTES);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700628 } catch (RemoteException e) {
629 // since we can't do much without policy or history, and we don't
630 // want to leave with half-baked UI, we bail hard.
631 throw new RuntimeException("problem reading network policy or stats", e);
632 }
633
Jeff Sharkey8a503642011-06-10 13:31:21 -0700634 // bind chart to historical stats
Jeff Sharkey8a503642011-06-10 13:31:21 -0700635 mChart.bindNetworkStats(mHistory);
636
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700637 // only update policy when switching tabs
638 updatePolicy(tabChanged);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700639 updateAppDetail();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700640
641 // force scroll to top of body
642 mListView.smoothScrollToPosition(0);
643
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700644 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700645 }
646
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700647 private boolean isAppDetailMode() {
648 return mUid != UID_NONE;
649 }
650
651 /**
652 * Update UID details panels to match {@link #mUid}, showing or hiding them
653 * depending on {@link #isAppDetailMode()}.
654 */
655 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700656 final Context context = getActivity();
657 final PackageManager pm = context.getPackageManager();
658 final LayoutInflater inflater = getActivity().getLayoutInflater();
659
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700660 if (isAppDetailMode()) {
661 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700662 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700663 } else {
664 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700665 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700666
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700667 mDetailHistory = null;
668 mDetailHistoryDefault = null;
669 mDetailHistoryForeground = null;
670
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700671 // hide detail stats when not in detail mode
672 mChart.bindDetailNetworkStats(null);
673 return;
674 }
675
676 // remove warning/limit sweeps while in detail mode
677 mChart.bindNetworkPolicy(null);
678
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700679 // show icon and all labels appearing under this app
680 final UidDetail detail = resolveDetailForUid(context, mUid);
681 mAppIcon.setImageDrawable(detail.icon);
682
683 mAppTitles.removeAllViews();
684 if (detail.detailLabels != null) {
685 for (CharSequence label : detail.detailLabels) {
686 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label));
687 }
688 } else {
689 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label));
690 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700691
692 // enable settings button when package provides it
693 // TODO: target torwards entire UID instead of just first package
694 final String[] packageNames = pm.getPackagesForUid(mUid);
695 if (packageNames != null && packageNames.length > 0) {
696 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
697 mAppSettingsIntent.setPackage(packageNames[0]);
698 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
699
700 final boolean matchFound = pm.resolveActivity(mAppSettingsIntent, 0) != null;
701 mAppSettings.setEnabled(matchFound);
702
703 } else {
704 mAppSettingsIntent = null;
705 mAppSettings.setEnabled(false);
706 }
707
708 try {
709 // load stats for current uid and template
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700710 mDetailHistoryDefault = mStatsService.getHistoryForUid(
711 mTemplate, mUid, SET_DEFAULT, TAG_NONE, FIELD_RX_BYTES | FIELD_TX_BYTES);
712 mDetailHistoryForeground = mStatsService.getHistoryForUid(
713 mTemplate, mUid, SET_FOREGROUND, TAG_NONE, FIELD_RX_BYTES | FIELD_TX_BYTES);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700714 } catch (RemoteException e) {
715 // since we can't do much without history, and we don't want to
716 // leave with half-baked UI, we bail hard.
717 throw new RuntimeException("problem reading network stats", e);
718 }
719
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700720 mDetailHistory = new NetworkStatsHistory(mDetailHistoryForeground.getBucketDuration());
721 mDetailHistory.recordEntireHistory(mDetailHistoryDefault);
722 mDetailHistory.recordEntireHistory(mDetailHistoryForeground);
723
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700724 // bind chart to historical stats
725 mChart.bindDetailNetworkStats(mDetailHistory);
726
727 updateDetailData();
728
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700729 if (NetworkPolicyManager.isUidValidForPolicy(context, mUid) && !getRestrictBackground()
730 && isBandwidthControlEnabled()) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -0700731 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
732 setPreferenceSummary(mAppRestrictView,
733 getString(R.string.data_usage_app_restrict_background_summary,
734 buildLimitedNetworksList()));
735
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700736 mAppRestrictView.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700737 mAppRestrict.setChecked(getAppRestrictBackground());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700738
739 } else {
740 mAppRestrictView.setVisibility(View.GONE);
741 }
742
743 }
744
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700745 private void setPolicyCycleDay(int cycleDay) {
746 if (LOGD) Log.d(TAG, "setPolicyCycleDay()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700747 mPolicyEditor.setPolicyCycleDay(mTemplate, cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700748 updatePolicy(true);
749 }
750
751 private void setPolicyWarningBytes(long warningBytes) {
752 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700753 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700754 updatePolicy(false);
755 }
756
757 private void setPolicyLimitBytes(long limitBytes) {
758 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700759 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700760 updatePolicy(false);
761 }
762
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700763 private boolean isNetworkPolicyModifiable() {
764 return isBandwidthControlEnabled() && mDataEnabled.isChecked();
765 }
766
767 private boolean isBandwidthControlEnabled() {
768 try {
769 return mNetworkService.isBandwidthControlEnabled();
770 } catch (RemoteException e) {
771 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
772 return false;
773 }
774 }
775
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700776 private boolean getDataRoaming() {
777 final ContentResolver resolver = getActivity().getContentResolver();
778 return Settings.Secure.getInt(resolver, Settings.Secure.DATA_ROAMING, 0) != 0;
779 }
780
781 private void setDataRoaming(boolean enabled) {
782 // TODO: teach telephony DataConnectionTracker to watch and apply
783 // updates when changed.
784 final ContentResolver resolver = getActivity().getContentResolver();
785 Settings.Secure.putInt(resolver, Settings.Secure.DATA_ROAMING, enabled ? 1 : 0);
786 mMenuDataRoaming.setChecked(enabled);
787 }
788
789 private boolean getRestrictBackground() {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700790 try {
791 return mPolicyService.getRestrictBackground();
792 } catch (RemoteException e) {
793 Log.w(TAG, "problem talking with policy service: " + e);
794 return false;
795 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700796 }
797
798 private void setRestrictBackground(boolean restrictBackground) {
799 if (LOGD) Log.d(TAG, "setRestrictBackground()");
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700800 try {
801 mPolicyService.setRestrictBackground(restrictBackground);
802 mMenuRestrictBackground.setChecked(restrictBackground);
803 } catch (RemoteException e) {
804 Log.w(TAG, "problem talking with policy service: " + e);
805 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700806 }
807
808 private boolean getAppRestrictBackground() {
809 final int uidPolicy;
810 try {
811 uidPolicy = mPolicyService.getUidPolicy(mUid);
812 } catch (RemoteException e) {
813 // since we can't do much without policy, we bail hard.
814 throw new RuntimeException("problem reading network policy", e);
815 }
816
817 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
818 }
819
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700820 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700821 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700822 try {
823 mPolicyService.setUidPolicy(
824 mUid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
825 } catch (RemoteException e) {
826 throw new RuntimeException("unable to save policy", e);
827 }
828
829 mAppRestrict.setChecked(restrictBackground);
830 }
831
Jeff Sharkey8a503642011-06-10 13:31:21 -0700832 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700833 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
834 * current {@link #mTemplate}.
835 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700836 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700837 if (isAppDetailMode()) {
838 mNetworkSwitches.setVisibility(View.GONE);
839 // we fall through to update cycle list for detail mode
840 } else {
841 mNetworkSwitches.setVisibility(View.VISIBLE);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700842
843 // when heading back to summary without cycle refresh, kick details
844 // update to repopulate list.
845 if (!refreshCycle) {
846 updateDetailData();
847 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700848 }
849
Jeff Sharkeya662e492011-06-18 21:57:06 -0700850 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700851 if (isNetworkPolicyModifiable()) {
852 mDisableAtLimitView.setVisibility(View.VISIBLE);
853 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700854 if (!isAppDetailMode()) {
855 mChart.bindNetworkPolicy(policy);
856 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700857
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700858 } else {
859 // controls are disabled; don't bind warning/limit sweeps
860 mDisableAtLimitView.setVisibility(View.GONE);
861 mChart.bindNetworkPolicy(null);
862 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700863
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700864 if (refreshCycle) {
865 // generate cycle list based on policy and available history
866 updateCycleList(policy);
867 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700868 }
869
870 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700871 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
872 * and available {@link NetworkStatsHistory} data. Always selects the newest
873 * item, updating the inspection range on {@link #mChart}.
874 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700875 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700876 mCycleAdapter.clear();
877
878 final Context context = mCycleSpinner.getContext();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700879 long historyStart = mHistory.getStart();
880 long historyEnd = mHistory.getEnd();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700881
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700882 if (historyStart == Long.MAX_VALUE || historyEnd == Long.MIN_VALUE) {
883 historyStart = System.currentTimeMillis();
884 historyEnd = System.currentTimeMillis();
885 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700886
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700887 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700888 if (policy != null) {
889 // find the next cycle boundary
890 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700891
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700892 int guardCount = 0;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700893
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700894 // walk backwards, generating all valid cycle ranges
895 while (cycleEnd > historyStart) {
896 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
897 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
898 + historyStart);
899 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
900 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700901 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700902
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700903 // TODO: remove this guard once we have better testing
904 if (guardCount++ > 50) {
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700905 Log.wtf(TAG, "stuck generating ranges for historyStart=" + historyStart
906 + ", historyEnd=" + historyEnd + " and policy=" + policy);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700907 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700908 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700909
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700910 // one last cycle entry to modify policy cycle day
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700911 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable());
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700912 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700913
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700914 if (!hasCycles) {
915 // no valid cycles; show all data
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700916 // TODO: offer simple ranges like "last week" etc
917 mCycleAdapter.add(new CycleItem(context, historyStart, historyEnd));
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700918 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700919 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700920
921 // force pick the current cycle (first item)
922 mCycleSpinner.setSelection(0);
923 mCycleListener.onItemSelected(mCycleSpinner, null, 0, 0);
924 }
925
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700926 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700927 /** {@inheritDoc} */
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700928 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
929 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700930
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700931 final boolean dataEnabled = isChecked;
932 mDataEnabled.setChecked(dataEnabled);
933
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700934 final String currentTab = mCurrentTab;
935 if (TAB_MOBILE.equals(currentTab)) {
936 mConnService.setMobileDataEnabled(dataEnabled);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700937 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700938
939 // rebind policy to match radio state
940 updatePolicy(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700941 }
942 };
943
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700944 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700945 /** {@inheritDoc} */
946 public void onClick(View v) {
947 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700948 if (disableAtLimit) {
949 // enabling limit; show confirmation dialog which eventually
950 // calls setPolicyLimitBytes() once user confirms.
951 ConfirmLimitFragment.show(DataUsageSummary.this);
952 } else {
953 setPolicyLimitBytes(LIMIT_DISABLED);
954 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700955 }
956 };
957
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700958 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
959 /** {@inheritDoc} */
960 public void onClick(View v) {
961 final boolean restrictBackground = !mAppRestrict.isChecked();
962
963 if (restrictBackground) {
964 // enabling restriction; show confirmation dialog which
965 // eventually calls setRestrictBackground() once user confirms.
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700966 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700967 } else {
968 setAppRestrictBackground(false);
969 }
970 }
971 };
972
973 private OnClickListener mAppSettingsListener = new OnClickListener() {
974 /** {@inheritDoc} */
975 public void onClick(View v) {
976 // TODO: target torwards entire UID instead of just first package
977 startActivity(mAppSettingsIntent);
978 }
979 };
980
Jeff Sharkey8a503642011-06-10 13:31:21 -0700981 private OnItemClickListener mListListener = new OnItemClickListener() {
982 /** {@inheritDoc} */
983 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700984 final AppUsageItem app = (AppUsageItem) parent.getItemAtPosition(position);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700985 AppDetailsFragment.show(DataUsageSummary.this, app.uid);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700986 }
987 };
988
989 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
990 /** {@inheritDoc} */
991 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
992 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
993 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700994 // show cycle editor; will eventually call setPolicyCycleDay()
995 // when user finishes editing.
996 CycleEditorFragment.show(DataUsageSummary.this);
997
998 // reset spinner to something other than "change cycle..."
999 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001000
1001 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001002 if (LOGD) {
1003 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1004 + cycle.end + "]");
1005 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001006
1007 // update chart to show selected cycle, and update detail data
1008 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001009 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001010
1011 updateDetailData();
1012 }
1013 }
1014
1015 /** {@inheritDoc} */
1016 public void onNothingSelected(AdapterView<?> parent) {
1017 // ignored
1018 }
1019 };
1020
1021 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001022 * Update details based on {@link #mChart} inspection range depending on
1023 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1024 * of applications data usage, and when {@link #isAppDetailMode()} update
1025 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001026 */
1027 private void updateDetailData() {
1028 if (LOGD) Log.d(TAG, "updateDetailData()");
1029
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001030 final long start = mChart.getInspectStart();
1031 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001032 final long now = System.currentTimeMillis();
1033
1034 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001035
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001036 NetworkStatsHistory.Entry entry = null;
1037 if (isAppDetailMode() && mDetailHistory != null) {
1038 // bind foreground/background to piechart and labels
1039 entry = mDetailHistoryDefault.getValues(start, end, now, entry);
1040 final long defaultBytes = entry.rxBytes + entry.txBytes;
1041 entry = mDetailHistoryForeground.getValues(start, end, now, entry);
1042 final long foregroundBytes = entry.rxBytes + entry.txBytes;
1043
1044 mAppPieChart.setOriginAngle(175);
1045
1046 mAppPieChart.removeAllSlices();
1047 mAppPieChart.addSlice(foregroundBytes, Color.parseColor("#d88d3a"));
1048 mAppPieChart.addSlice(defaultBytes, Color.parseColor("#666666"));
1049
1050 mAppPieChart.generatePath();
1051
1052 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1053 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1054
1055 // and finally leave with summary data for label below
1056 entry = mDetailHistory.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001057
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001058 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001059
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001060 } else {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001061 entry = mHistory.getValues(start, end, now, null);
1062
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001063 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001064 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001065 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryForAllUid);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001066 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001067
1068 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1069 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001070 final String rangePhrase = formatDateRange(context, start, end, false);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001071
1072 mUsageSummary.setText(
1073 getString(R.string.data_usage_total_during_range, totalPhrase, rangePhrase));
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001074 }
1075
1076 private final LoaderCallbacks<NetworkStats> mSummaryForAllUid = new LoaderCallbacks<
1077 NetworkStats>() {
1078 /** {@inheritDoc} */
1079 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
1080 return new SummaryForAllUidLoader(getActivity(), mStatsService, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001081 }
1082
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001083 /** {@inheritDoc} */
1084 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
1085 mAdapter.bindStats(data);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001086 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001087 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001088
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001089 /** {@inheritDoc} */
1090 public void onLoaderReset(Loader<NetworkStats> loader) {
1091 mAdapter.bindStats(null);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001092 updateEmptyVisible();
1093 }
1094
1095 private void updateEmptyVisible() {
1096 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1097 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001098 }
1099 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001100
Jeff Sharkeya662e492011-06-18 21:57:06 -07001101 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001102 final Context context = getActivity();
1103 if (hasMobileRadio(context)) {
1104 final String subscriberId = getActiveSubscriberId(context);
1105 return mPolicyEditor.isMobilePolicySplit(subscriberId);
1106 } else {
1107 return false;
1108 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001109 }
1110
1111 private void setMobilePolicySplit(boolean split) {
1112 final String subscriberId = getActiveSubscriberId(getActivity());
1113 mPolicyEditor.setMobilePolicySplit(subscriberId, split);
1114 }
1115
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001116 private static String getActiveSubscriberId(Context context) {
1117 final TelephonyManager telephony = (TelephonyManager) context.getSystemService(
1118 Context.TELEPHONY_SERVICE);
1119 return telephony.getSubscriberId();
1120 }
1121
Jeff Sharkey8a503642011-06-10 13:31:21 -07001122 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
1123 /** {@inheritDoc} */
1124 public void onInspectRangeChanged() {
1125 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
1126 updateDetailData();
1127 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001128
Jeff Sharkey8a503642011-06-10 13:31:21 -07001129 /** {@inheritDoc} */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001130 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001131 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001132 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001133
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001134 /** {@inheritDoc} */
1135 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001136 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001137 }
1138 };
1139
1140
1141 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001142 * List item that reflects a specific data usage cycle.
1143 */
1144 public static class CycleItem {
1145 public CharSequence label;
1146 public long start;
1147 public long end;
1148
Jeff Sharkey8a503642011-06-10 13:31:21 -07001149 CycleItem(CharSequence label) {
1150 this.label = label;
1151 }
1152
1153 public CycleItem(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001154 this.label = formatDateRange(context, start, end, true);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001155 this.start = start;
1156 this.end = end;
1157 }
1158
Jeff Sharkey8a503642011-06-10 13:31:21 -07001159 @Override
1160 public String toString() {
1161 return label.toString();
1162 }
1163 }
1164
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001165 private static final StringBuilder sBuilder = new StringBuilder(50);
1166 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1167 sBuilder, Locale.getDefault());
1168
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001169 public static String formatDateRange(Context context, long start, long end, boolean utcTime) {
1170 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
1171 final String timezone = utcTime ? TIMEZONE_UTC : null;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001172
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001173 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001174 sBuilder.setLength(0);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001175 return DateUtils
1176 .formatDateRange(context, sFormatter, start, end, flags, timezone).toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001177 }
1178 }
1179
Jeff Sharkey8a503642011-06-10 13:31:21 -07001180 /**
1181 * Special-case data usage cycle that triggers dialog to change
1182 * {@link NetworkPolicy#cycleDay}.
1183 */
1184 public static class CycleChangeItem extends CycleItem {
1185 public CycleChangeItem(Context context) {
1186 super(context.getString(R.string.data_usage_change_cycle));
1187 }
1188 }
1189
1190 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001191 private boolean mChangePossible = false;
1192 private boolean mChangeVisible = false;
1193
1194 private final CycleChangeItem mChangeItem;
1195
Jeff Sharkey8a503642011-06-10 13:31:21 -07001196 public CycleAdapter(Context context) {
1197 super(context, android.R.layout.simple_spinner_item);
1198 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001199 mChangeItem = new CycleChangeItem(context);
1200 }
1201
1202 public void setChangePossible(boolean possible) {
1203 mChangePossible = possible;
1204 updateChange();
1205 }
1206
1207 public void setChangeVisible(boolean visible) {
1208 mChangeVisible = visible;
1209 updateChange();
1210 }
1211
1212 private void updateChange() {
1213 remove(mChangeItem);
1214 if (mChangePossible && mChangeVisible) {
1215 add(mChangeItem);
1216 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001217 }
1218 }
1219
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001220 private static class AppUsageItem implements Comparable<AppUsageItem> {
1221 public int uid;
1222 public long total;
1223
1224 /** {@inheritDoc} */
1225 public int compareTo(AppUsageItem another) {
1226 return Long.compare(another.total, total);
1227 }
1228 }
1229
Jeff Sharkey8a503642011-06-10 13:31:21 -07001230 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001231 * Adapter of applications, sorted by total usage descending.
1232 */
1233 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001234 private ArrayList<AppUsageItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001235 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001236
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001237 /**
1238 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1239 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001240 public void bindStats(NetworkStats stats) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001241 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001242
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001243 final AppUsageItem systemItem = new AppUsageItem();
1244 systemItem.uid = android.os.Process.SYSTEM_UID;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001245
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001246 final SparseArray<AppUsageItem> knownUids = new SparseArray<AppUsageItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001247
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001248 NetworkStats.Entry entry = null;
1249 final int size = stats != null ? stats.size() : 0;
1250 for (int i = 0; i < size; i++) {
1251 entry = stats.getValues(i, entry);
1252
1253 final int uid = entry.uid;
1254 final boolean isApp = uid >= android.os.Process.FIRST_APPLICATION_UID
1255 && uid <= android.os.Process.LAST_APPLICATION_UID;
1256 if (isApp || uid == TrafficStats.UID_REMOVED) {
1257 AppUsageItem item = knownUids.get(uid);
1258 if (item == null) {
1259 item = new AppUsageItem();
1260 item.uid = uid;
1261 knownUids.put(uid, item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001262 mItems.add(item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001263 }
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001264
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001265 item.total += entry.rxBytes + entry.txBytes;
1266 } else {
1267 systemItem.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001268 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001269 }
1270
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001271 if (systemItem.total > 0) {
1272 mItems.add(systemItem);
1273 }
1274
Jeff Sharkey8a503642011-06-10 13:31:21 -07001275 Collections.sort(mItems);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001276 mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001277 notifyDataSetChanged();
1278 }
1279
1280 @Override
1281 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001282 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001283 }
1284
1285 @Override
1286 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001287 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001288 }
1289
1290 @Override
1291 public long getItemId(int position) {
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001292 return mItems.get(position).uid;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001293 }
1294
1295 @Override
1296 public View getView(int position, View convertView, ViewGroup parent) {
1297 if (convertView == null) {
1298 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey52c3f442011-06-23 00:39:38 -07001299 R.layout.data_usage_item, parent, false);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001300 }
1301
1302 final Context context = parent.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001303
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001304 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001305 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
1306 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
1307 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1308 android.R.id.progress);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001309
Jeff Sharkey8a503642011-06-10 13:31:21 -07001310 final AppUsageItem item = mItems.get(position);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001311 final UidDetail detail = resolveDetailForUid(context, item.uid);
1312
1313 icon.setImageDrawable(detail.icon);
1314 title.setText(detail.label);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001315 summary.setText(Formatter.formatFileSize(context, item.total));
1316
1317 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1318 progress.setProgress(percentTotal);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001319
1320 return convertView;
1321 }
1322
1323 }
1324
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001325 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001326 * Empty {@link Fragment} that controls display of UID details in
1327 * {@link DataUsageSummary}.
1328 */
1329 public static class AppDetailsFragment extends Fragment {
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001330 private static final String EXTRA_UID = "uid";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001331
1332 public static void show(DataUsageSummary parent, int uid) {
1333 final Bundle args = new Bundle();
1334 args.putInt(EXTRA_UID, uid);
1335
1336 final AppDetailsFragment fragment = new AppDetailsFragment();
1337 fragment.setArguments(args);
1338 fragment.setTargetFragment(parent, 0);
1339
1340 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1341 ft.add(fragment, TAG_APP_DETAILS);
1342 ft.addToBackStack(TAG_APP_DETAILS);
1343 ft.commit();
1344 }
1345
1346 @Override
1347 public void onStart() {
1348 super.onStart();
1349 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1350 target.mUid = getArguments().getInt(EXTRA_UID);
1351 target.updateBody();
1352 }
1353
1354 @Override
1355 public void onStop() {
1356 super.onStop();
1357 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1358 target.mUid = UID_NONE;
1359 target.updateBody();
1360 }
1361 }
1362
1363 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001364 * Dialog to request user confirmation before setting
1365 * {@link NetworkPolicy#limitBytes}.
1366 */
1367 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001368 private static final String EXTRA_MESSAGE_ID = "messageId";
1369 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001370
1371 public static void show(DataUsageSummary parent) {
1372 final Bundle args = new Bundle();
1373
1374 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001375 final String currentTab = parent.mCurrentTab;
1376 if (TAB_3G.equals(currentTab)) {
1377 args.putInt(EXTRA_MESSAGE_ID, R.string.data_usage_limit_dialog_3g);
1378 args.putLong(EXTRA_LIMIT_BYTES, 5 * GB_IN_BYTES);
1379 } else if (TAB_4G.equals(currentTab)) {
1380 args.putInt(EXTRA_MESSAGE_ID, R.string.data_usage_limit_dialog_4g);
1381 args.putLong(EXTRA_LIMIT_BYTES, 5 * GB_IN_BYTES);
1382 } else if (TAB_MOBILE.equals(currentTab)) {
1383 args.putInt(EXTRA_MESSAGE_ID, R.string.data_usage_limit_dialog_mobile);
1384 args.putLong(EXTRA_LIMIT_BYTES, 5 * GB_IN_BYTES);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001385 }
1386
1387 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1388 dialog.setArguments(args);
1389 dialog.setTargetFragment(parent, 0);
1390 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1391 }
1392
1393 @Override
1394 public Dialog onCreateDialog(Bundle savedInstanceState) {
1395 final Context context = getActivity();
1396
1397 final int messageId = getArguments().getInt(EXTRA_MESSAGE_ID);
1398 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1399
1400 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1401 builder.setTitle(R.string.data_usage_limit_dialog_title);
1402 builder.setMessage(messageId);
1403
1404 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1405 public void onClick(DialogInterface dialog, int which) {
1406 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1407 if (target != null) {
1408 target.setPolicyLimitBytes(limitBytes);
1409 }
1410 }
1411 });
1412
1413 return builder.create();
1414 }
1415 }
1416
1417 /**
1418 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1419 */
1420 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001421 private static final String EXTRA_CYCLE_DAY = "cycleDay";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001422
1423 public static void show(DataUsageSummary parent) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07001424 final NetworkPolicy policy = parent.mPolicyEditor.getPolicy(parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001425 final Bundle args = new Bundle();
1426 args.putInt(CycleEditorFragment.EXTRA_CYCLE_DAY, policy.cycleDay);
1427
1428 final CycleEditorFragment dialog = new CycleEditorFragment();
1429 dialog.setArguments(args);
1430 dialog.setTargetFragment(parent, 0);
1431 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1432 }
1433
1434 @Override
1435 public Dialog onCreateDialog(Bundle savedInstanceState) {
1436 final Context context = getActivity();
1437
1438 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1439 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1440
1441 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1442 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1443
1444 final int oldCycleDay = getArguments().getInt(EXTRA_CYCLE_DAY, 1);
1445
1446 cycleDayPicker.setMinValue(1);
1447 cycleDayPicker.setMaxValue(31);
1448 cycleDayPicker.setValue(oldCycleDay);
1449 cycleDayPicker.setWrapSelectorWheel(true);
1450
1451 builder.setTitle(R.string.data_usage_cycle_editor_title);
1452 builder.setView(view);
1453
1454 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1455 new DialogInterface.OnClickListener() {
1456 public void onClick(DialogInterface dialog, int which) {
1457 final int cycleDay = cycleDayPicker.getValue();
1458 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1459 if (target != null) {
1460 target.setPolicyCycleDay(cycleDay);
1461 }
1462 }
1463 });
1464
1465 return builder.create();
1466 }
1467 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001468
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001469 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001470 * Dialog to request user confirmation before setting
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001471 * {@link Settings.Secure#DATA_ROAMING}.
1472 */
1473 public static class ConfirmDataRoamingFragment extends DialogFragment {
1474 public static void show(DataUsageSummary parent) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001475 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) {
1508 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1509 dialog.setTargetFragment(parent, 0);
1510 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1511 }
1512
1513 @Override
1514 public Dialog onCreateDialog(Bundle savedInstanceState) {
1515 final Context context = getActivity();
1516
1517 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001518 builder.setTitle(R.string.data_usage_restrict_background_title);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07001519
1520 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1521 if (target != null) {
1522 final CharSequence limitedNetworks = target.buildLimitedNetworksList();
1523 builder.setMessage(
1524 getString(R.string.data_usage_restrict_background, limitedNetworks));
1525 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001526
1527 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1528 public void onClick(DialogInterface dialog, int which) {
1529 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1530 if (target != null) {
1531 target.setRestrictBackground(true);
1532 }
1533 }
1534 });
1535 builder.setNegativeButton(android.R.string.cancel, null);
1536
1537 return builder.create();
1538 }
1539 }
1540
1541 /**
1542 * Dialog to request user confirmation before setting
1543 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
1544 */
1545 public static class ConfirmAppRestrictFragment extends DialogFragment {
1546 public static void show(DataUsageSummary parent) {
1547 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
1548 dialog.setTargetFragment(parent, 0);
1549 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
1550 }
1551
1552 @Override
1553 public Dialog onCreateDialog(Bundle savedInstanceState) {
1554 final Context context = getActivity();
1555
1556 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001557 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
1558 builder.setMessage(R.string.data_usage_app_restrict_dialog);
1559
1560 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1561 public void onClick(DialogInterface dialog, int which) {
1562 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1563 if (target != null) {
1564 target.setAppRestrictBackground(true);
1565 }
1566 }
1567 });
1568 builder.setNegativeButton(android.R.string.cancel, null);
1569
1570 return builder.create();
1571 }
1572 }
1573
1574 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001575 * Compute default tab that should be selected, based on
1576 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
1577 */
1578 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07001579 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
1580 if (template == null) return null;
1581
1582 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07001583 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001584 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001585 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001586 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001587 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001588 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001589 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001590 return TAB_WIFI;
1591 default:
1592 return null;
1593 }
1594 }
1595
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001596 public static class UidDetail {
1597 public CharSequence label;
1598 public CharSequence[] detailLabels;
1599 public Drawable icon;
1600 }
1601
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001602 /**
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001603 * Resolve best descriptive label for the given UID.
1604 */
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001605 public static UidDetail resolveDetailForUid(Context context, int uid) {
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001606 final Resources res = context.getResources();
1607 final PackageManager pm = context.getPackageManager();
1608
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001609 final UidDetail detail = new UidDetail();
1610 detail.label = pm.getNameForUid(uid);
1611 detail.icon = pm.getDefaultActivityIcon();
1612
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001613 // handle special case labels
1614 switch (uid) {
1615 case android.os.Process.SYSTEM_UID:
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001616 detail.label = res.getString(R.string.process_kernel_label);
1617 detail.icon = pm.getDefaultActivityIcon();
1618 return detail;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001619 case TrafficStats.UID_REMOVED:
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001620 detail.label = res.getString(R.string.data_usage_uninstalled_apps);
1621 detail.icon = pm.getDefaultActivityIcon();
1622 return detail;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001623 }
1624
1625 // otherwise fall back to using packagemanager labels
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001626 final String[] packageNames = pm.getPackagesForUid(uid);
1627 final int length = packageNames != null ? packageNames.length : 0;
1628
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001629 try {
1630 if (length == 1) {
1631 final ApplicationInfo info = pm.getApplicationInfo(packageNames[0], 0);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001632 detail.label = info.loadLabel(pm).toString();
1633 detail.icon = info.loadIcon(pm);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001634 } else if (length > 1) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001635 detail.detailLabels = new CharSequence[length];
1636 for (int i = 0; i < length; i++) {
1637 final String packageName = packageNames[i];
1638 final PackageInfo packageInfo = pm.getPackageInfo(packageName, 0);
1639 final ApplicationInfo appInfo = pm.getApplicationInfo(packageName, 0);
1640
1641 detail.detailLabels[i] = appInfo.loadLabel(pm).toString();
1642 if (packageInfo.sharedUserLabel != 0) {
1643 detail.label = pm.getText(packageName, packageInfo.sharedUserLabel,
1644 packageInfo.applicationInfo).toString();
1645 detail.icon = appInfo.loadIcon(pm);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001646 }
1647 }
1648 }
1649 } catch (NameNotFoundException e) {
1650 }
1651
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001652 if (TextUtils.isEmpty(detail.label)) {
1653 detail.label = Integer.toString(uid);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001654 }
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001655 return detail;
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001656 }
1657
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001658 /**
1659 * Test if device has a mobile data radio.
1660 */
1661 private static boolean hasMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001662 if (TEST_RADIOS) {
1663 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
1664 }
1665
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001666 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
1667 Context.CONNECTIVITY_SERVICE);
1668
1669 // mobile devices should have MOBILE network tracker regardless of
1670 // connection status.
1671 return conn.getNetworkInfo(TYPE_MOBILE) != null;
1672 }
1673
1674 /**
1675 * Test if device has a mobile 4G data radio.
1676 */
1677 private static boolean hasMobile4gRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001678 if (TEST_RADIOS) {
1679 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
1680 }
1681
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001682 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
1683 Context.CONNECTIVITY_SERVICE);
1684 final TelephonyManager telephony = (TelephonyManager) context.getSystemService(
1685 Context.TELEPHONY_SERVICE);
1686
1687 // WiMAX devices should have WiMAX network tracker regardless of
1688 // connection status.
1689 final boolean hasWimax = conn.getNetworkInfo(TYPE_WIMAX) != null;
1690 final boolean hasLte = telephony.getLteOnCdmaMode() == Phone.LTE_ON_CDMA_TRUE;
1691 return hasWimax || hasLte;
1692 }
1693
1694 /**
1695 * Test if device has a Wi-Fi data radio.
1696 */
1697 private static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001698 if (TEST_RADIOS) {
1699 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
1700 }
1701
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001702 return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI);
1703 }
1704
1705 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001706 * Test if device has an ethernet network connection.
1707 */
1708 private static boolean hasEthernet(Context context) {
1709 if (TEST_RADIOS) {
1710 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
1711 }
1712
1713 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
1714 Context.CONNECTIVITY_SERVICE);
1715 return conn.getNetworkInfo(TYPE_ETHERNET) != null;
1716 }
1717
1718 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001719 * Inflate a {@link Preference} style layout, adding the given {@link View}
1720 * widget into {@link android.R.id#widget_frame}.
1721 */
1722 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
1723 final View view = inflater.inflate(R.layout.preference, root, false);
1724 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
1725 android.R.id.widget_frame);
1726 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
1727 return view;
1728 }
1729
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001730 private static View inflateAppTitle(
1731 LayoutInflater inflater, ViewGroup root, CharSequence label) {
1732 final TextView view = (TextView) inflater.inflate(
1733 R.layout.data_usage_app_title, root, false);
1734 view.setText(label);
1735 return view;
1736 }
1737
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001738 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07001739 * Build string describing currently limited networks, which defines when
1740 * background data is restricted.
1741 */
1742 private CharSequence buildLimitedNetworksList() {
1743 final Context context = getActivity();
1744 final String subscriberId = getActiveSubscriberId(context);
1745
1746 // build combined list of all limited networks
1747 final ArrayList<CharSequence> limited = Lists.newArrayList();
1748 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
1749 limited.add(getText(R.string.data_usage_list_mobile));
1750 }
1751 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
1752 limited.add(getText(R.string.data_usage_tab_3g));
1753 }
1754 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
1755 limited.add(getText(R.string.data_usage_tab_4g));
1756 }
1757 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifi())) {
1758 limited.add(getText(R.string.data_usage_tab_wifi));
1759 }
1760 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
1761 limited.add(getText(R.string.data_usage_tab_ethernet));
1762 }
1763
1764 // handle case where no networks limited
1765 if (limited.isEmpty()) {
1766 limited.add(getText(R.string.data_usage_list_none));
1767 }
1768
1769 return TextUtils.join(limited);
1770 }
1771
1772 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001773 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07001774 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001775 */
1776 private static void setPreferenceTitle(View parent, int resId) {
1777 final TextView title = (TextView) parent.findViewById(android.R.id.title);
1778 title.setText(resId);
1779 }
1780
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001781 /**
1782 * Set {@link android.R.id#summary} for a preference view inflated with
1783 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
1784 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07001785 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001786 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
1787 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07001788 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001789 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001790}