blob: 1255b6bcf7d2073f955a72a378eb04ed783e67d6 [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;
Jeff Sharkeybdf98e82011-11-10 17:17:24 -080021import static android.net.ConnectivityManager.TYPE_WIFI;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070022import static android.net.ConnectivityManager.TYPE_WIMAX;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070023import static android.net.NetworkPolicy.LIMIT_DISABLED;
Jeff Sharkeya53188f2011-09-13 19:56:45 -070024import static android.net.NetworkPolicy.WARNING_DISABLED;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070025import static android.net.NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070026import static android.net.NetworkPolicyManager.POLICY_NONE;
27import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND;
Jeff Sharkey8a503642011-06-10 13:31:21 -070028import static android.net.NetworkPolicyManager.computeLastCycleBoundary;
29import static android.net.NetworkPolicyManager.computeNextCycleBoundary;
Jeff Sharkeya662e492011-06-18 21:57:06 -070030import static android.net.NetworkTemplate.MATCH_MOBILE_3G_LOWER;
31import static android.net.NetworkTemplate.MATCH_MOBILE_4G;
32import static android.net.NetworkTemplate.MATCH_MOBILE_ALL;
33import static android.net.NetworkTemplate.MATCH_WIFI;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070034import static android.net.NetworkTemplate.buildTemplateEthernet;
35import static android.net.NetworkTemplate.buildTemplateMobile3gLower;
36import static android.net.NetworkTemplate.buildTemplateMobile4g;
37import static android.net.NetworkTemplate.buildTemplateMobileAll;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070038import static android.net.NetworkTemplate.buildTemplateWifiWildcard;
Jeff Sharkey77dae912012-02-03 14:50:33 -080039import static android.net.TrafficStats.GB_IN_BYTES;
40import static android.net.TrafficStats.MB_IN_BYTES;
Jeff Sharkeya83a24f2011-09-16 01:52:39 -070041import static android.net.TrafficStats.UID_REMOVED;
42import static android.net.TrafficStats.UID_TETHERING;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070043import static android.telephony.TelephonyManager.SIM_STATE_READY;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070044import static android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
45import static android.text.format.DateUtils.FORMAT_SHOW_DATE;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070046import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -070047import static com.android.internal.util.Preconditions.checkNotNull;
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -070048import static com.android.settings.Utils.prepareCustomPreferencesList;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070049
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070050import android.animation.LayoutTransition;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070051import android.app.AlertDialog;
52import android.app.Dialog;
53import android.app.DialogFragment;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070054import android.app.Fragment;
Amith Yamasani5ba0a022011-11-07 12:29:00 -080055import android.app.FragmentManager;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070056import android.app.FragmentTransaction;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070057import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070058import android.content.ContentResolver;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070059import android.content.Context;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070060import android.content.DialogInterface;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -070061import android.content.Intent;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070062import android.content.Loader;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070063import android.content.SharedPreferences;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070064import android.content.pm.PackageManager;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070065import android.content.res.Resources;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070066import android.graphics.Color;
Jeff Sharkey5d706792011-09-08 18:57:17 -070067import android.graphics.drawable.ColorDrawable;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070068import android.graphics.drawable.Drawable;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070069import android.net.ConnectivityManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070070import android.net.INetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070071import android.net.INetworkStatsService;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070072import android.net.INetworkStatsSession;
Jeff Sharkey8a503642011-06-10 13:31:21 -070073import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070074import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070075import android.net.NetworkStats;
76import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070077import android.net.NetworkTemplate;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070078import android.net.TrafficStats;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070079import android.net.Uri;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070080import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070081import android.os.Bundle;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070082import android.os.INetworkManagementService;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070083import android.os.Parcel;
84import android.os.Parcelable;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070085import android.os.RemoteException;
86import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070087import android.os.SystemProperties;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070088import android.os.UserId;
Jeff Sharkey8a503642011-06-10 13:31:21 -070089import android.preference.Preference;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070090import android.preference.PreferenceActivity;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070091import android.provider.Settings;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070092import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070093import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070094import android.text.format.DateUtils;
95import android.text.format.Formatter;
Jeff Sharkeye5223a02012-03-09 17:11:14 -080096import android.text.format.Time;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070097import android.util.Log;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070098import android.util.SparseArray;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070099import android.util.SparseBooleanArray;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700100import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700101import android.view.Menu;
102import android.view.MenuInflater;
103import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700104import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700105import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700106import android.view.ViewGroup;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700107import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700108import android.widget.AdapterView;
109import android.widget.AdapterView.OnItemClickListener;
110import android.widget.AdapterView.OnItemSelectedListener;
111import android.widget.ArrayAdapter;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700112import android.widget.BaseAdapter;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700113import android.widget.Button;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700114import android.widget.CheckBox;
115import android.widget.CompoundButton;
116import android.widget.CompoundButton.OnCheckedChangeListener;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700117import android.widget.ImageView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700118import android.widget.LinearLayout;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700119import android.widget.ListView;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700120import android.widget.NumberPicker;
Jeff Sharkey2412b0f2011-07-17 20:31:40 -0700121import android.widget.ProgressBar;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700122import android.widget.Spinner;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700123import android.widget.Switch;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700124import android.widget.TabHost;
125import android.widget.TabHost.OnTabChangeListener;
126import android.widget.TabHost.TabContentFactory;
127import android.widget.TabHost.TabSpec;
128import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700129import android.widget.TextView;
130
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700131import com.android.internal.telephony.Phone;
Jeff Sharkey5d706792011-09-08 18:57:17 -0700132import com.android.settings.drawable.InsetBoundsDrawable;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700133import com.android.settings.net.ChartData;
134import com.android.settings.net.ChartDataLoader;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700135import com.android.settings.net.DataUsageMeteredSettings;
Jeff Sharkeya662e492011-06-18 21:57:06 -0700136import com.android.settings.net.NetworkPolicyEditor;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700137import com.android.settings.net.SummaryForAllUidLoader;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700138import com.android.settings.net.UidDetail;
139import com.android.settings.net.UidDetailProvider;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700140import com.android.settings.widget.ChartDataUsageView;
141import com.android.settings.widget.ChartDataUsageView.DataUsageChartListener;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700142import com.android.settings.widget.PieChartView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700143import com.google.android.collect.Lists;
144
145import java.util.ArrayList;
146import java.util.Collections;
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700147import java.util.List;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700148import java.util.Locale;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700149
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700150import libcore.util.Objects;
151
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700152/**
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700153 * Panel showing data usage history across various networks, including options
154 * to inspect based on usage cycle and control through {@link NetworkPolicy}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700155 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700156public class DataUsageSummary extends Fragment {
157 private static final String TAG = "DataUsage";
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800158 private static final boolean LOGD = false;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700159
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700160 // TODO: remove this testing code
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700161 private static final boolean TEST_ANIM = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700162 private static final boolean TEST_RADIOS = false;
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800163
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700164 private static final String TEST_RADIOS_PROP = "test.radios";
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800165 private static final String TEST_SUBSCRIBER_PROP = "test.subscriberid";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700166
Jeff Sharkey8a503642011-06-10 13:31:21 -0700167 private static final String TAB_3G = "3g";
168 private static final String TAB_4G = "4g";
169 private static final String TAB_MOBILE = "mobile";
170 private static final String TAB_WIFI = "wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700171 private static final String TAB_ETHERNET = "ethernet";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700172
Jeff Sharkey28130d92011-09-02 16:10:24 -0700173 private static final String TAG_CONFIRM_DATA_DISABLE = "confirmDataDisable";
174 private static final String TAG_CONFIRM_DATA_ROAMING = "confirmDataRoaming";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700175 private static final String TAG_CONFIRM_LIMIT = "confirmLimit";
176 private static final String TAG_CYCLE_EDITOR = "cycleEditor";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700177 private static final String TAG_WARNING_EDITOR = "warningEditor";
178 private static final String TAG_LIMIT_EDITOR = "limitEditor";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700179 private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700180 private static final String TAG_DENIED_RESTRICT = "deniedRestrict";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700181 private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700182 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700183
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700184 private static final int LOADER_CHART_DATA = 2;
185 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700186
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700187 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700188 private INetworkStatsService mStatsService;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700189 private NetworkPolicyManager mPolicyManager;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700190 private ConnectivityManager mConnService;
191
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700192 private INetworkStatsSession mStatsSession;
193
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700194 private static final String PREF_FILE = "data_usage";
195 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700196 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700197
198 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700199
Jeff Sharkey8a503642011-06-10 13:31:21 -0700200 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700201 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700202 private TabWidget mTabWidget;
203 private ListView mListView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700204 private DataUsageAdapter mAdapter;
205
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700206 /** Distance to inset content from sides, when needed. */
207 private int mInsetSide = 0;
208
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700209 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700210
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700211 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700212 private LinearLayout mNetworkSwitches;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700213 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700214 private View mDataEnabledView;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700215 private CheckBox mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700216 private View mDisableAtLimitView;
217
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700218 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700219 private Spinner mCycleSpinner;
220 private CycleAdapter mCycleAdapter;
221
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700222 private ChartDataUsageView mChart;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700223 private TextView mUsageSummary;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700224 private TextView mEmpty;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700225
226 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700227 private ImageView mAppIcon;
228 private ViewGroup mAppTitles;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700229 private PieChartView mAppPieChart;
230 private TextView mAppForeground;
231 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700232 private Button mAppSettings;
233
234 private LinearLayout mAppSwitches;
235 private CheckBox mAppRestrict;
236 private View mAppRestrictView;
237
Jeff Sharkey8a503642011-06-10 13:31:21 -0700238 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700239 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700240
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700241 private NetworkTemplate mTemplate;
242 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700243
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700244 private AppItem mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700245
246 private Intent mAppSettingsIntent;
247
Jeff Sharkeya662e492011-06-18 21:57:06 -0700248 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700249
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700250 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700251 private String mIntentTab = null;
252
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700253 private MenuItem mMenuDataRoaming;
254 private MenuItem mMenuRestrictBackground;
255
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700256 /** Flag used to ignore listeners during binding. */
257 private boolean mBinding;
258
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700259 private UidDetailProvider mUidDetailProvider;
260
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700261 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700262 public void onCreate(Bundle savedInstanceState) {
263 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700264 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700265
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700266 mNetworkService = INetworkManagementService.Stub.asInterface(
267 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700268 mStatsService = INetworkStatsService.Stub.asInterface(
269 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700270 mPolicyManager = NetworkPolicyManager.from(context);
271 mConnService = ConnectivityManager.from(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700272
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700273 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700274
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700275 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700276 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700277
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700278 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700279 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700280
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700281 // override preferences when no mobile radio
282 if (!hasReadyMobileRadio(context)) {
283 mShowWifi = hasWifiRadio(context);
284 mShowEthernet = hasEthernet(context);
285 }
286
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700287 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700288 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700289
Jeff Sharkey8a503642011-06-10 13:31:21 -0700290 @Override
291 public View onCreateView(LayoutInflater inflater, ViewGroup container,
292 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700293
Jeff Sharkey8a503642011-06-10 13:31:21 -0700294 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700295 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
296
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700297 mUidDetailProvider = new UidDetailProvider(context);
298
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700299 try {
300 mStatsSession = mStatsService.openSession();
301 } catch (RemoteException e) {
302 throw new RuntimeException(e);
303 }
304
Jeff Sharkey8a503642011-06-10 13:31:21 -0700305 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700306 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700307 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
308 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700309
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700310 // decide if we need to manually inset our content, or if we should rely
311 // on parent container for inset.
312 final boolean shouldInset = mListView.getScrollBarStyle()
313 == View.SCROLLBARS_OUTSIDE_OVERLAY;
314 if (shouldInset) {
315 mInsetSide = view.getResources().getDimensionPixelOffset(
316 com.android.internal.R.dimen.preference_fragment_padding_side);
317 } else {
318 mInsetSide = 0;
319 }
320
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700321 // adjust padding around tabwidget as needed
Jeff Sharkey5d706792011-09-08 18:57:17 -0700322 prepareCustomPreferencesList(container, view, mListView, true);
323
Jeff Sharkey8a503642011-06-10 13:31:21 -0700324 mTabHost.setup();
325 mTabHost.setOnTabChangedListener(mTabListener);
326
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700327 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700328 mHeader.setClickable(true);
329
330 mListView.addHeaderView(mHeader, null, true);
331 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700332
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700333 if (mInsetSide > 0) {
334 // inset selector and divider drawables
335 insetListViewDrawables(mListView, mInsetSide);
336 mHeader.setPadding(mInsetSide, 0, mInsetSide, 0);
337 }
338
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700339 {
340 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700341 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
342 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700343 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700344
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700345 mDataEnabled = new Switch(inflater.getContext());
346 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
347 mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener);
348 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700349
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700350 mDisableAtLimit = new CheckBox(inflater.getContext());
351 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700352 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700353 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700354 mDisableAtLimitView.setClickable(true);
355 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700356 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
357 mNetworkSwitches.addView(mDisableAtLimitView);
358 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700359
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700360 // bind cycle dropdown
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700361 mCycleView = mHeader.findViewById(R.id.cycles);
362 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700363 mCycleAdapter = new CycleAdapter(context);
364 mCycleSpinner.setAdapter(mCycleAdapter);
365 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
366
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700367 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700368 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800369 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700370
371 {
372 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700373 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700374 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
375 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700376 mAppPieChart = (PieChartView) mAppDetail.findViewById(R.id.app_pie_chart);
377 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
378 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700379 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700380
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700381 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700382 mAppSettings.setOnClickListener(mAppSettingsListener);
383
384 mAppRestrict = new CheckBox(inflater.getContext());
385 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700386 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700387 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700388 mAppRestrictView.setClickable(true);
389 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700390 mAppRestrictView.setOnClickListener(mAppRestrictListener);
391 mAppSwitches.addView(mAppRestrictView);
392 }
393
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700394 mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700395 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700396
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700397 // only assign layout transitions once first layout is finished
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700398 mListView.getViewTreeObserver().addOnGlobalLayoutListener(mFirstLayoutListener);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700399
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700400 mAdapter = new DataUsageAdapter(mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700401 mListView.setOnItemClickListener(mListListener);
402 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700403
404 return view;
405 }
406
407 @Override
408 public void onResume() {
409 super.onResume();
410
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700411 // pick default tab based on incoming intent
412 final Intent intent = getActivity().getIntent();
413 mIntentTab = computeTabFromIntent(intent);
414
Jeff Sharkey8a503642011-06-10 13:31:21 -0700415 // this kicks off chain reaction which creates tabs, binds the body to
416 // selected network, and binds chart, cycles and detail list.
417 updateTabs();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700418
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700419 // kick off background task to update stats
420 new AsyncTask<Void, Void, Void>() {
421 @Override
422 protected Void doInBackground(Void... params) {
423 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700424 // wait a few seconds before kicking off
425 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700426 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700427 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700428 } catch (RemoteException e) {
429 }
430 return null;
431 }
432
433 @Override
434 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700435 if (isAdded()) {
436 updateBody();
437 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700438 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700439 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700440 }
441
Jeff Sharkey8a503642011-06-10 13:31:21 -0700442 @Override
443 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
444 inflater.inflate(R.menu.data_usage, menu);
445 }
446
447 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700448 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700449 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700450 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700451
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700452 mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700453 mMenuDataRoaming.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700454 mMenuDataRoaming.setChecked(getDataRoaming());
455
456 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700457 mMenuRestrictBackground.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
458 mMenuRestrictBackground.setChecked(mPolicyManager.getRestrictBackground());
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700459
460 final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g);
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700461 split4g.setVisible(hasReadyMobile4gRadio(context) && !appDetailMode);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700462 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700463
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700464 final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700465 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700466 showWifi.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700467 showWifi.setChecked(mShowWifi);
468 } else {
469 showWifi.setVisible(false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700470 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700471
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700472 final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700473 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700474 showEthernet.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700475 showEthernet.setChecked(mShowEthernet);
476 } else {
477 showEthernet.setVisible(false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700478 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700479
480 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
481 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
482 metered.setVisible(!appDetailMode);
483 } else {
484 metered.setVisible(false);
485 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700486
487 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
488 String helpUrl;
489 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
490 Intent helpIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
491 helpIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
492 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
493 help.setIntent(helpIntent);
494 help.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
495 } else {
496 help.setVisible(false);
497 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700498 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700499
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700500 @Override
501 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700502 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700503 case R.id.data_usage_menu_roaming: {
504 final boolean dataRoaming = !item.isChecked();
505 if (dataRoaming) {
506 ConfirmDataRoamingFragment.show(this);
507 } else {
508 // no confirmation to disable roaming
509 setDataRoaming(false);
510 }
511 return true;
512 }
513 case R.id.data_usage_menu_restrict_background: {
514 final boolean restrictBackground = !item.isChecked();
515 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800516 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700517 } else {
518 // no confirmation to drop restriction
519 setRestrictBackground(false);
520 }
521 return true;
522 }
523 case R.id.data_usage_menu_split_4g: {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700524 final boolean mobileSplit = !item.isChecked();
Jeff Sharkeya662e492011-06-18 21:57:06 -0700525 setMobilePolicySplit(mobileSplit);
526 item.setChecked(isMobilePolicySplit());
Jeff Sharkey8a503642011-06-10 13:31:21 -0700527 updateTabs();
528 return true;
529 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700530 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700531 mShowWifi = !item.isChecked();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700532 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700533 item.setChecked(mShowWifi);
534 updateTabs();
535 return true;
536 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700537 case R.id.data_usage_menu_show_ethernet: {
538 mShowEthernet = !item.isChecked();
539 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
540 item.setChecked(mShowEthernet);
541 updateTabs();
542 return true;
543 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700544 case R.id.data_usage_menu_metered: {
545 final PreferenceActivity activity = (PreferenceActivity) getActivity();
546 activity.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
547 R.string.data_usage_metered_title, null, this, 0);
548 return true;
549 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700550 }
551 return false;
552 }
553
Jeff Sharkey94a90952011-06-13 22:31:09 -0700554 @Override
555 public void onDestroyView() {
556 super.onDestroyView();
557
558 mDataEnabledView = null;
559 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700560
561 mUidDetailProvider.clearCache();
562 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700563
564 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700565 }
566
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800567 @Override
568 public void onDestroy() {
569 if (this.isRemoving()) {
570 getFragmentManager()
571 .popBackStack(TAG_APP_DETAILS, FragmentManager.POP_BACK_STACK_INCLUSIVE);
572 }
573 super.onDestroy();
574 }
575
Jeff Sharkey8a503642011-06-10 13:31:21 -0700576 /**
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700577 * Listener to setup {@link LayoutTransition} after first layout pass.
578 */
579 private OnGlobalLayoutListener mFirstLayoutListener = new OnGlobalLayoutListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700580 @Override
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700581 public void onGlobalLayout() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700582 mListView.getViewTreeObserver().removeOnGlobalLayoutListener(mFirstLayoutListener);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700583
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700584 mTabsContainer.setLayoutTransition(buildLayoutTransition());
585 mHeader.setLayoutTransition(buildLayoutTransition());
586 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700587
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700588 final LayoutTransition chartTransition = buildLayoutTransition();
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700589 chartTransition.setStartDelay(LayoutTransition.APPEARING, 0);
590 chartTransition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700591 chartTransition.setAnimator(LayoutTransition.APPEARING, null);
Jeff Sharkey28130d92011-09-02 16:10:24 -0700592 chartTransition.setAnimator(LayoutTransition.DISAPPEARING, null);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700593 mChart.setLayoutTransition(chartTransition);
594 }
595 };
596
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700597 private static LayoutTransition buildLayoutTransition() {
598 final LayoutTransition transition = new LayoutTransition();
599 if (TEST_ANIM) {
600 transition.setDuration(1500);
601 }
602 transition.setAnimateParentHierarchy(false);
603 return transition;
604 }
605
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700606 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700607 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700608 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
609 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700610 */
611 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700612 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700613 mTabHost.clearAllTabs();
614
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700615 final boolean mobileSplit = isMobilePolicySplit();
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700616 if (mobileSplit && hasReadyMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700617 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
618 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700619 } else if (hasReadyMobileRadio(context)) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700620 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700621 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700622 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700623 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
624 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700625 if (mShowEthernet && hasEthernet(context)) {
626 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
627 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700628
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700629 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700630 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
631 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
632 if (mIntentTab != null) {
633 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700634 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700635 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700636 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700637 mTabHost.setCurrentTabByTag(mIntentTab);
638 }
639 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700640 } else if (noTabs) {
641 // no usable tabs, so hide body
642 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700643 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700644 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700645 }
646 }
647
Jeff Sharkey8a503642011-06-10 13:31:21 -0700648 /**
649 * Factory that provide empty {@link View} to make {@link TabHost} happy.
650 */
651 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700652 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700653 public View createTabContent(String tag) {
654 return new View(mTabHost.getContext());
655 }
656 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700657
Jeff Sharkey8a503642011-06-10 13:31:21 -0700658 /**
659 * Build {@link TabSpec} with thin indicator, and empty content.
660 */
661 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700662 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
663 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700664 }
665
666 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700667 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700668 public void onTabChanged(String tabId) {
669 // user changed tab; update body
670 updateBody();
671 }
672 };
673
674 /**
675 * Update body content based on current tab. Loads
676 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
677 * binds them to visible controls.
678 */
679 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700680 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700681 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700682
Jeff Sharkeya662e492011-06-18 21:57:06 -0700683 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700684 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700685
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700686 if (currentTab == null) {
687 Log.w(TAG, "no tab selected; hiding body");
688 mListView.setVisibility(View.GONE);
689 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700690 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700691 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700692 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700693
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700694 final boolean tabChanged = !currentTab.equals(mCurrentTab);
695 mCurrentTab = currentTab;
696
Jeff Sharkey8a503642011-06-10 13:31:21 -0700697 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
698
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700699 mDataEnabledView.setVisibility(View.VISIBLE);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700700
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700701 // TODO: remove mobile tabs when SIM isn't ready
702 final TelephonyManager tele = TelephonyManager.from(context);
703
Jeff Sharkey8a503642011-06-10 13:31:21 -0700704 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700705 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
706 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700707 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700708
709 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700710 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
711 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
712 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700713 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700714
715 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700716 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
717 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
718 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700719 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700720
721 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700722 // wifi doesn't have any controls
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700723 mDataEnabledView.setVisibility(View.GONE);
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700724 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700725 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700726
727 } else if (TAB_ETHERNET.equals(currentTab)) {
728 // ethernet doesn't have any controls
729 mDataEnabledView.setVisibility(View.GONE);
730 mDisableAtLimitView.setVisibility(View.GONE);
731 mTemplate = buildTemplateEthernet();
732
733 } else {
734 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700735 }
736
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700737 // kick off loader for network history
738 // TODO: consider chaining two loaders together instead of reloading
739 // network history when showing app detail.
740 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700741 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700742
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700743 // detail mode can change visible menus, invalidate
744 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700745
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700746 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700747 }
748
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700749 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700750 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700751 }
752
753 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700754 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700755 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700756 */
757 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700758 final Context context = getActivity();
759 final PackageManager pm = context.getPackageManager();
760 final LayoutInflater inflater = getActivity().getLayoutInflater();
761
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700762 if (isAppDetailMode()) {
763 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700764 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700765 } else {
766 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700767 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700768
769 // hide detail stats when not in detail mode
770 mChart.bindDetailNetworkStats(null);
771 return;
772 }
773
774 // remove warning/limit sweeps while in detail mode
775 mChart.bindNetworkPolicy(null);
776
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700777 // show icon and all labels appearing under this app
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700778 final int appId = mCurrentApp.appId;
779 final UidDetail detail = mUidDetailProvider.getUidDetail(appId, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700780 mAppIcon.setImageDrawable(detail.icon);
781
782 mAppTitles.removeAllViews();
783 if (detail.detailLabels != null) {
784 for (CharSequence label : detail.detailLabels) {
785 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label));
786 }
787 } else {
788 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label));
789 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700790
791 // enable settings button when package provides it
792 // TODO: target torwards entire UID instead of just first package
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700793 final String[] packageNames = pm.getPackagesForUid(appId);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700794 if (packageNames != null && packageNames.length > 0) {
795 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
796 mAppSettingsIntent.setPackage(packageNames[0]);
797 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
798
799 final boolean matchFound = pm.resolveActivity(mAppSettingsIntent, 0) != null;
800 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -0700801 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700802
803 } else {
804 mAppSettingsIntent = null;
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700805 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700806 }
807
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700808 updateDetailData();
809
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700810 if (UserId.isApp(appId) && !mPolicyManager.getRestrictBackground()
811 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700812 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800813 setPreferenceSummary(mAppRestrictView,
814 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700815
816 mAppRestrictView.setVisibility(View.VISIBLE);
817 mAppRestrict.setChecked(getAppRestrictBackground());
818
819 } else {
820 mAppRestrictView.setVisibility(View.GONE);
821 }
822 }
823
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700824 private void setPolicyWarningBytes(long warningBytes) {
825 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700826 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700827 updatePolicy(false);
828 }
829
830 private void setPolicyLimitBytes(long limitBytes) {
831 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700832 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700833 updatePolicy(false);
834 }
835
Jeff Sharkey28130d92011-09-02 16:10:24 -0700836 /**
837 * Local cache of value, used to work around delay when
838 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
839 */
840 private Boolean mMobileDataEnabled;
841
842 private boolean isMobileDataEnabled() {
843 if (mMobileDataEnabled != null) {
844 // TODO: deprecate and remove this once enabled flag is on policy
845 return mMobileDataEnabled;
846 } else {
847 return mConnService.getMobileDataEnabled();
848 }
849 }
850
851 private void setMobileDataEnabled(boolean enabled) {
852 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
853 mConnService.setMobileDataEnabled(enabled);
854 mMobileDataEnabled = enabled;
855 updatePolicy(false);
856 }
857
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700858 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
859 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked();
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700860 }
861
862 private boolean isBandwidthControlEnabled() {
863 try {
864 return mNetworkService.isBandwidthControlEnabled();
865 } catch (RemoteException e) {
866 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
867 return false;
868 }
869 }
870
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700871 private boolean getDataRoaming() {
872 final ContentResolver resolver = getActivity().getContentResolver();
873 return Settings.Secure.getInt(resolver, Settings.Secure.DATA_ROAMING, 0) != 0;
874 }
875
876 private void setDataRoaming(boolean enabled) {
877 // TODO: teach telephony DataConnectionTracker to watch and apply
878 // updates when changed.
879 final ContentResolver resolver = getActivity().getContentResolver();
880 Settings.Secure.putInt(resolver, Settings.Secure.DATA_ROAMING, enabled ? 1 : 0);
881 mMenuDataRoaming.setChecked(enabled);
882 }
883
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700884 public void setRestrictBackground(boolean restrictBackground) {
885 mPolicyManager.setRestrictBackground(restrictBackground);
886 mMenuRestrictBackground.setChecked(restrictBackground);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700887 }
888
889 private boolean getAppRestrictBackground() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700890 final int appId = mCurrentApp.appId;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700891 final int uidPolicy = mPolicyManager.getAppPolicy(appId);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700892 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
893 }
894
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700895 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700896 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700897 final int appId = mCurrentApp.appId;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700898 mPolicyManager.setAppPolicy(appId,
899 restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700900 mAppRestrict.setChecked(restrictBackground);
901 }
902
Jeff Sharkey8a503642011-06-10 13:31:21 -0700903 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700904 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
905 * current {@link #mTemplate}.
906 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700907 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700908 if (isAppDetailMode()) {
909 mNetworkSwitches.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700910 } else {
911 mNetworkSwitches.setVisibility(View.VISIBLE);
912 }
913
Jeff Sharkey28130d92011-09-02 16:10:24 -0700914 // TODO: move enabled state directly into policy
915 if (TAB_MOBILE.equals(mCurrentTab)) {
916 mBinding = true;
917 mDataEnabled.setChecked(isMobileDataEnabled());
918 mBinding = false;
919 }
920
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700921 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
922 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700923 mDisableAtLimitView.setVisibility(View.VISIBLE);
924 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700925 if (!isAppDetailMode()) {
926 mChart.bindNetworkPolicy(policy);
927 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700928
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700929 } else {
930 // controls are disabled; don't bind warning/limit sweeps
931 mDisableAtLimitView.setVisibility(View.GONE);
932 mChart.bindNetworkPolicy(null);
933 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700934
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700935 if (refreshCycle) {
936 // generate cycle list based on policy and available history
937 updateCycleList(policy);
938 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700939 }
940
941 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700942 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
943 * and available {@link NetworkStatsHistory} data. Always selects the newest
944 * item, updating the inspection range on {@link #mChart}.
945 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700946 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700947 // stash away currently selected cycle to try restoring below
948 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700949 mCycleAdapter.clear();
950
951 final Context context = mCycleSpinner.getContext();
952
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700953 long historyStart = Long.MAX_VALUE;
954 long historyEnd = Long.MIN_VALUE;
955 if (mChartData != null) {
956 historyStart = mChartData.network.getStart();
957 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700958 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700959
Jeff Sharkey461842a2011-09-25 18:22:48 -0700960 final long now = System.currentTimeMillis();
961 if (historyStart == Long.MAX_VALUE) historyStart = now;
962 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700963
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700964 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700965 if (policy != null) {
966 // find the next cycle boundary
967 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700968
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700969 // walk backwards, generating all valid cycle ranges
970 while (cycleEnd > historyStart) {
971 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
972 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
973 + historyStart);
974 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
975 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700976 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700977 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700978
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700979 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700980 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700981 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700982
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700983 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700984 // no policy defined cycles; show entry for each four-week period
985 long cycleEnd = historyEnd;
986 while (cycleEnd > historyStart) {
987 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
988 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
989 cycleEnd = cycleStart;
990 }
991
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700992 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700993 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700994
995 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700996 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700997 final int position = mCycleAdapter.findNearestPosition(previousItem);
998 mCycleSpinner.setSelection(position);
999
1000 // only force-update cycle when changed; skipping preserves any
1001 // user-defined inspection region.
1002 final CycleItem selectedItem = mCycleAdapter.getItem(position);
1003 if (!Objects.equal(selectedItem, previousItem)) {
1004 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1005 } else {
1006 // but still kick off loader for detailed list
1007 updateDetailData();
1008 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001009 } else {
1010 updateDetailData();
1011 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001012 }
1013
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001014 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001015 @Override
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001016 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
1017 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001018
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001019 final boolean dataEnabled = isChecked;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001020 final String currentTab = mCurrentTab;
1021 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001022 if (dataEnabled) {
1023 setMobileDataEnabled(true);
1024 } else {
1025 // disabling data; show confirmation dialog which eventually
1026 // calls setMobileDataEnabled() once user confirms.
1027 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1028 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001029 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001030
Jeff Sharkey28130d92011-09-02 16:10:24 -07001031 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001032 }
1033 };
1034
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001035 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001036 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001037 public void onClick(View v) {
1038 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001039 if (disableAtLimit) {
1040 // enabling limit; show confirmation dialog which eventually
1041 // calls setPolicyLimitBytes() once user confirms.
1042 ConfirmLimitFragment.show(DataUsageSummary.this);
1043 } else {
1044 setPolicyLimitBytes(LIMIT_DISABLED);
1045 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001046 }
1047 };
1048
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001049 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001050 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001051 public void onClick(View v) {
1052 final boolean restrictBackground = !mAppRestrict.isChecked();
1053
1054 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001055 // enabling restriction; show confirmation dialog which
1056 // eventually calls setRestrictBackground() once user
1057 // confirms.
1058 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001059 } else {
1060 setAppRestrictBackground(false);
1061 }
1062 }
1063 };
1064
1065 private OnClickListener mAppSettingsListener = new OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001066 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001067 public void onClick(View v) {
Jeff Sharkeyd92e0412012-04-24 11:35:43 -07001068 if (!isAdded()) return;
1069
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001070 // TODO: target torwards entire UID instead of just first package
1071 startActivity(mAppSettingsIntent);
1072 }
1073 };
1074
Jeff Sharkey8a503642011-06-10 13:31:21 -07001075 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001076 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001077 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001078 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001079 final AppItem app = (AppItem) parent.getItemAtPosition(position);
1080 final UidDetail detail = mUidDetailProvider.getUidDetail(app.appId, true);
1081 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001082 }
1083 };
1084
1085 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001086 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001087 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1088 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1089 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001090 // show cycle editor; will eventually call setPolicyCycleDay()
1091 // when user finishes editing.
1092 CycleEditorFragment.show(DataUsageSummary.this);
1093
1094 // reset spinner to something other than "change cycle..."
1095 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001096
1097 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001098 if (LOGD) {
1099 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1100 + cycle.end + "]");
1101 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001102
1103 // update chart to show selected cycle, and update detail data
1104 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001105 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001106
1107 updateDetailData();
1108 }
1109 }
1110
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001111 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001112 public void onNothingSelected(AdapterView<?> parent) {
1113 // ignored
1114 }
1115 };
1116
1117 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001118 * Update details based on {@link #mChart} inspection range depending on
1119 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1120 * of applications data usage, and when {@link #isAppDetailMode()} update
1121 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001122 */
1123 private void updateDetailData() {
1124 if (LOGD) Log.d(TAG, "updateDetailData()");
1125
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001126 final long start = mChart.getInspectStart();
1127 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001128 final long now = System.currentTimeMillis();
1129
1130 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001131
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001132 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001133 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001134 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001135 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001136 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001137 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001138 final long foregroundBytes = entry.rxBytes + entry.txBytes;
1139
1140 mAppPieChart.setOriginAngle(175);
1141
1142 mAppPieChart.removeAllSlices();
1143 mAppPieChart.addSlice(foregroundBytes, Color.parseColor("#d88d3a"));
1144 mAppPieChart.addSlice(defaultBytes, Color.parseColor("#666666"));
1145
1146 mAppPieChart.generatePath();
1147
1148 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1149 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1150
1151 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001152 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001153
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001154 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001155
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001156 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001157 if (mChartData != null) {
1158 entry = mChartData.network.getValues(start, end, now, null);
1159 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001160
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001161 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001162 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001163 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001164 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001165
1166 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1167 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001168 final String rangePhrase = formatDateRange(context, start, end);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001169
Jeff Sharkeye557c332012-04-13 16:04:07 -07001170 final int summaryRes;
1171 if (TAB_MOBILE.equals(mCurrentTab) || TAB_3G.equals(mCurrentApp)
1172 || TAB_4G.equals(mCurrentApp)) {
1173 summaryRes = R.string.data_usage_total_during_range_mobile;
1174 } else {
1175 summaryRes = R.string.data_usage_total_during_range;
1176 }
1177
1178 mUsageSummary.setText(getString(summaryRes, totalPhrase, rangePhrase));
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001179 }
1180
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001181 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1182 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001183 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001184 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001185 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001186 }
1187
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001188 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001189 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1190 mChartData = data;
1191 mChart.bindNetworkStats(mChartData.network);
1192 mChart.bindDetailNetworkStats(mChartData.detail);
1193
1194 // calcuate policy cycles based on available data
1195 updatePolicy(true);
1196 updateAppDetail();
1197
1198 // force scroll to top of body when showing detail
1199 if (mChartData.detail != null) {
1200 mListView.smoothScrollToPosition(0);
1201 }
1202 }
1203
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001204 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001205 public void onLoaderReset(Loader<ChartData> loader) {
1206 mChartData = null;
1207 mChart.bindNetworkStats(null);
1208 mChart.bindDetailNetworkStats(null);
1209 }
1210 };
1211
1212 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001213 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001214 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001215 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001216 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001217 }
1218
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001219 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001220 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001221 final int[] restrictedAppIds = mPolicyManager.getAppsWithPolicy(
1222 POLICY_REJECT_METERED_BACKGROUND);
1223 mAdapter.bindStats(data, restrictedAppIds);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001224 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001225 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001226
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001227 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001228 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001229 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001230 updateEmptyVisible();
1231 }
1232
1233 private void updateEmptyVisible() {
1234 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1235 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001236 }
1237 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001238
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001239 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001240 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001241 final Context context = getActivity();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001242 if (hasReadyMobileRadio(context)) {
1243 final TelephonyManager tele = TelephonyManager.from(context);
1244 return mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001245 } else {
1246 return false;
1247 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001248 }
1249
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001250 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001251 private void setMobilePolicySplit(boolean split) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001252 final Context context = getActivity();
1253 if (hasReadyMobileRadio(context)) {
1254 final TelephonyManager tele = TelephonyManager.from(context);
1255 mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split);
1256 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001257 }
1258
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001259 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001260 final TelephonyManager tele = TelephonyManager.from(context);
1261 final String actualSubscriberId = tele.getSubscriberId();
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -08001262 return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001263 }
1264
Jeff Sharkey8a503642011-06-10 13:31:21 -07001265 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001266 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001267 public void onInspectRangeChanged() {
1268 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
1269 updateDetailData();
1270 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001271
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001272 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001273 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001274 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001275 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001276
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001277 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001278 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001279 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001280 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001281
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001282 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001283 public void requestWarningEdit() {
1284 WarningEditorFragment.show(DataUsageSummary.this);
1285 }
1286
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001287 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001288 public void requestLimitEdit() {
1289 LimitEditorFragment.show(DataUsageSummary.this);
1290 }
1291 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001292
1293 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001294 * List item that reflects a specific data usage cycle.
1295 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001296 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001297 public CharSequence label;
1298 public long start;
1299 public long end;
1300
Jeff Sharkey8a503642011-06-10 13:31:21 -07001301 CycleItem(CharSequence label) {
1302 this.label = label;
1303 }
1304
1305 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001306 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001307 this.start = start;
1308 this.end = end;
1309 }
1310
Jeff Sharkey8a503642011-06-10 13:31:21 -07001311 @Override
1312 public String toString() {
1313 return label.toString();
1314 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001315
1316 @Override
1317 public boolean equals(Object o) {
1318 if (o instanceof CycleItem) {
1319 final CycleItem another = (CycleItem) o;
1320 return start == another.start && end == another.end;
1321 }
1322 return false;
1323 }
1324
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001325 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001326 public int compareTo(CycleItem another) {
1327 return Long.compare(start, another.start);
1328 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001329 }
1330
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001331 private static final StringBuilder sBuilder = new StringBuilder(50);
1332 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1333 sBuilder, Locale.getDefault());
1334
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001335 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001336 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001337
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001338 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001339 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001340 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1341 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001342 }
1343 }
1344
Jeff Sharkey8a503642011-06-10 13:31:21 -07001345 /**
1346 * Special-case data usage cycle that triggers dialog to change
1347 * {@link NetworkPolicy#cycleDay}.
1348 */
1349 public static class CycleChangeItem extends CycleItem {
1350 public CycleChangeItem(Context context) {
1351 super(context.getString(R.string.data_usage_change_cycle));
1352 }
1353 }
1354
1355 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001356 private boolean mChangePossible = false;
1357 private boolean mChangeVisible = false;
1358
1359 private final CycleChangeItem mChangeItem;
1360
Jeff Sharkey8a503642011-06-10 13:31:21 -07001361 public CycleAdapter(Context context) {
1362 super(context, android.R.layout.simple_spinner_item);
1363 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001364 mChangeItem = new CycleChangeItem(context);
1365 }
1366
1367 public void setChangePossible(boolean possible) {
1368 mChangePossible = possible;
1369 updateChange();
1370 }
1371
1372 public void setChangeVisible(boolean visible) {
1373 mChangeVisible = visible;
1374 updateChange();
1375 }
1376
1377 private void updateChange() {
1378 remove(mChangeItem);
1379 if (mChangePossible && mChangeVisible) {
1380 add(mChangeItem);
1381 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001382 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001383
1384 /**
1385 * Find position of {@link CycleItem} in this adapter which is nearest
1386 * the given {@link CycleItem}.
1387 */
1388 public int findNearestPosition(CycleItem target) {
1389 if (target != null) {
1390 final int count = getCount();
1391 for (int i = count - 1; i >= 0; i--) {
1392 final CycleItem item = getItem(i);
1393 if (item instanceof CycleChangeItem) {
1394 continue;
1395 } else if (item.compareTo(target) >= 0) {
1396 return i;
1397 }
1398 }
1399 }
1400 return 0;
1401 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001402 }
1403
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001404 public static class AppItem implements Comparable<AppItem>, Parcelable {
1405 public final int appId;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001406 public boolean restricted;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001407 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001408 public long total;
1409
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001410 public AppItem(int appId) {
1411 this.appId = appId;
1412 }
1413
1414 public AppItem(Parcel parcel) {
1415 appId = parcel.readInt();
1416 uids = parcel.readSparseBooleanArray();
1417 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001418 }
1419
1420 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001421 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001422 }
1423
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001424 @Override
1425 public void writeToParcel(Parcel dest, int flags) {
1426 dest.writeInt(appId);
1427 dest.writeSparseBooleanArray(uids);
1428 dest.writeLong(total);
1429 }
1430
1431 @Override
1432 public int describeContents() {
1433 return 0;
1434 }
1435
1436 @Override
1437 public int compareTo(AppItem another) {
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001438 return Long.compare(another.total, total);
1439 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001440
1441 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1442 @Override
1443 public AppItem createFromParcel(Parcel in) {
1444 return new AppItem(in);
1445 }
1446
1447 @Override
1448 public AppItem[] newArray(int size) {
1449 return new AppItem[size];
1450 }
1451 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001452 }
1453
Jeff Sharkey8a503642011-06-10 13:31:21 -07001454 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001455 * Adapter of applications, sorted by total usage descending.
1456 */
1457 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001458 private final UidDetailProvider mProvider;
1459 private final int mInsetSide;
1460
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001461 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001462 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001463
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001464 public DataUsageAdapter(UidDetailProvider provider, int insetSide) {
1465 mProvider = checkNotNull(provider);
1466 mInsetSide = insetSide;
1467 }
1468
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001469 /**
1470 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1471 */
Jeff Sharkeye557c332012-04-13 16:04:07 -07001472 public void bindStats(NetworkStats stats, int[] restrictedAppIds) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001473 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001474
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001475 final AppItem systemItem = new AppItem(android.os.Process.SYSTEM_UID);
1476 final SparseArray<AppItem> knownUids = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001477
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001478 NetworkStats.Entry entry = null;
1479 final int size = stats != null ? stats.size() : 0;
1480 for (int i = 0; i < size; i++) {
1481 entry = stats.getValues(i, entry);
1482
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001483 final boolean isApp = UserId.isApp(entry.uid);
1484 final int appId = isApp ? UserId.getAppId(entry.uid) : entry.uid;
1485 if (isApp || appId == UID_REMOVED || appId == UID_TETHERING) {
1486 AppItem item = knownUids.get(appId);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001487 if (item == null) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001488 item = new AppItem(appId);
1489 knownUids.put(appId, item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001490 mItems.add(item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001491 }
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001492
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001493 item.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001494 item.addUid(entry.uid);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001495 } else {
1496 systemItem.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001497 systemItem.addUid(entry.uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001498 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001499 }
1500
Jeff Sharkeye557c332012-04-13 16:04:07 -07001501 for (int appId : restrictedAppIds) {
1502 AppItem item = knownUids.get(appId);
1503 if (item == null) {
1504 item = new AppItem(appId);
1505 item.total = -1;
1506 mItems.add(item);
1507 }
1508 item.restricted = true;
1509 }
1510
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001511 if (systemItem.total > 0) {
1512 mItems.add(systemItem);
1513 }
1514
Jeff Sharkey8a503642011-06-10 13:31:21 -07001515 Collections.sort(mItems);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001516 mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001517 notifyDataSetChanged();
1518 }
1519
1520 @Override
1521 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001522 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001523 }
1524
1525 @Override
1526 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001527 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001528 }
1529
1530 @Override
1531 public long getItemId(int position) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001532 return mItems.get(position).appId;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001533 }
1534
1535 @Override
1536 public View getView(int position, View convertView, ViewGroup parent) {
1537 if (convertView == null) {
1538 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey5d706792011-09-08 18:57:17 -07001539 R.layout.data_usage_item, parent, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001540
1541 if (mInsetSide > 0) {
1542 convertView.setPadding(mInsetSide, 0, mInsetSide, 0);
1543 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001544 }
1545
1546 final Context context = parent.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001547
Jeff Sharkey28130d92011-09-02 16:10:24 -07001548 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001549 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1550 android.R.id.progress);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001551
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001552 // kick off async load of app details
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001553 final AppItem item = mItems.get(position);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001554 UidDetailTask.bindView(mProvider, item, convertView);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001555
Jeff Sharkeye557c332012-04-13 16:04:07 -07001556 if (item.restricted && item.total <= 0) {
1557 text1.setText(R.string.data_usage_app_restricted);
1558 progress.setVisibility(View.GONE);
1559 } else {
1560 text1.setText(Formatter.formatFileSize(context, item.total));
1561 progress.setVisibility(View.VISIBLE);
1562 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001563
1564 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1565 progress.setProgress(percentTotal);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001566
1567 return convertView;
1568 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001569 }
1570
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001571 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001572 * Empty {@link Fragment} that controls display of UID details in
1573 * {@link DataUsageSummary}.
1574 */
1575 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001576 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001577
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001578 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001579 if (!parent.isAdded()) return;
1580
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001581 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001582 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001583
1584 final AppDetailsFragment fragment = new AppDetailsFragment();
1585 fragment.setArguments(args);
1586 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001587 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1588 ft.add(fragment, TAG_APP_DETAILS);
1589 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001590 ft.setBreadCrumbTitle(label);
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001591 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001592 }
1593
1594 @Override
1595 public void onStart() {
1596 super.onStart();
1597 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001598 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001599 target.updateBody();
1600 }
1601
1602 @Override
1603 public void onStop() {
1604 super.onStop();
1605 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001606 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001607 target.updateBody();
1608 }
1609 }
1610
1611 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001612 * Dialog to request user confirmation before setting
1613 * {@link NetworkPolicy#limitBytes}.
1614 */
1615 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001616 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001617 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001618
1619 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001620 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001621
Jeff Sharkey461842a2011-09-25 18:22:48 -07001622 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001623 final CharSequence message;
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001624 final long minLimitBytes = (long) (
1625 parent.mPolicyEditor.getPolicy(parent.mTemplate).warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001626 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001627
1628 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001629 final String currentTab = parent.mCurrentTab;
1630 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001631 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001632 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001633 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001634 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001635 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001636 } else if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001637 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001638 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001639 } else {
1640 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001641 }
1642
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001643 final Bundle args = new Bundle();
1644 args.putCharSequence(EXTRA_MESSAGE, message);
1645 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1646
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001647 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1648 dialog.setArguments(args);
1649 dialog.setTargetFragment(parent, 0);
1650 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1651 }
1652
1653 @Override
1654 public Dialog onCreateDialog(Bundle savedInstanceState) {
1655 final Context context = getActivity();
1656
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001657 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001658 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1659
1660 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1661 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001662 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001663
1664 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001665 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001666 public void onClick(DialogInterface dialog, int which) {
1667 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1668 if (target != null) {
1669 target.setPolicyLimitBytes(limitBytes);
1670 }
1671 }
1672 });
1673
1674 return builder.create();
1675 }
1676 }
1677
1678 /**
1679 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1680 */
1681 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001682 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001683
1684 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001685 if (!parent.isAdded()) return;
1686
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001687 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001688 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001689
1690 final CycleEditorFragment dialog = new CycleEditorFragment();
1691 dialog.setArguments(args);
1692 dialog.setTargetFragment(parent, 0);
1693 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1694 }
1695
1696 @Override
1697 public Dialog onCreateDialog(Bundle savedInstanceState) {
1698 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001699 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1700 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001701
1702 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1703 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1704
1705 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1706 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1707
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001708 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1709 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001710
1711 cycleDayPicker.setMinValue(1);
1712 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001713 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001714 cycleDayPicker.setWrapSelectorWheel(true);
1715
1716 builder.setTitle(R.string.data_usage_cycle_editor_title);
1717 builder.setView(view);
1718
1719 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1720 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001721 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001722 public void onClick(DialogInterface dialog, int which) {
1723 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001724 final String cycleTimezone = new Time().timezone;
1725 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001726 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001727 }
1728 });
1729
1730 return builder.create();
1731 }
1732 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001733
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001734 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001735 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1736 */
1737 public static class WarningEditorFragment extends DialogFragment {
1738 private static final String EXTRA_TEMPLATE = "template";
1739
1740 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001741 if (!parent.isAdded()) return;
1742
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001743 final Bundle args = new Bundle();
1744 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1745
1746 final WarningEditorFragment dialog = new WarningEditorFragment();
1747 dialog.setArguments(args);
1748 dialog.setTargetFragment(parent, 0);
1749 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1750 }
1751
1752 @Override
1753 public Dialog onCreateDialog(Bundle savedInstanceState) {
1754 final Context context = getActivity();
1755 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1756 final NetworkPolicyEditor editor = target.mPolicyEditor;
1757
1758 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1759 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1760
1761 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1762 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1763
1764 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1765 final long warningBytes = editor.getPolicyWarningBytes(template);
1766 final long limitBytes = editor.getPolicyLimitBytes(template);
1767
1768 bytesPicker.setMinValue(0);
1769 if (limitBytes != LIMIT_DISABLED) {
1770 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1771 } else {
1772 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1773 }
1774 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1775 bytesPicker.setWrapSelectorWheel(false);
1776
1777 builder.setTitle(R.string.data_usage_warning_editor_title);
1778 builder.setView(view);
1779
1780 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1781 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001782 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001783 public void onClick(DialogInterface dialog, int which) {
1784 // clear focus to finish pending text edits
1785 bytesPicker.clearFocus();
1786
1787 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1788 editor.setPolicyWarningBytes(template, bytes);
1789 target.updatePolicy(false);
1790 }
1791 });
1792
1793 return builder.create();
1794 }
1795 }
1796
1797 /**
1798 * Dialog to edit {@link NetworkPolicy#limitBytes}.
1799 */
1800 public static class LimitEditorFragment extends DialogFragment {
1801 private static final String EXTRA_TEMPLATE = "template";
1802
1803 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001804 if (!parent.isAdded()) return;
1805
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001806 final Bundle args = new Bundle();
1807 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1808
1809 final LimitEditorFragment dialog = new LimitEditorFragment();
1810 dialog.setArguments(args);
1811 dialog.setTargetFragment(parent, 0);
1812 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
1813 }
1814
1815 @Override
1816 public Dialog onCreateDialog(Bundle savedInstanceState) {
1817 final Context context = getActivity();
1818 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1819 final NetworkPolicyEditor editor = target.mPolicyEditor;
1820
1821 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1822 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1823
1824 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1825 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1826
1827 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1828 final long warningBytes = editor.getPolicyWarningBytes(template);
1829 final long limitBytes = editor.getPolicyLimitBytes(template);
1830
1831 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09001832 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001833 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
1834 } else {
1835 bytesPicker.setMinValue(0);
1836 }
1837 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
1838 bytesPicker.setWrapSelectorWheel(false);
1839
1840 builder.setTitle(R.string.data_usage_limit_editor_title);
1841 builder.setView(view);
1842
1843 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1844 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001845 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001846 public void onClick(DialogInterface dialog, int which) {
1847 // clear focus to finish pending text edits
1848 bytesPicker.clearFocus();
1849
1850 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1851 editor.setPolicyLimitBytes(template, bytes);
1852 target.updatePolicy(false);
1853 }
1854 });
1855
1856 return builder.create();
1857 }
1858 }
1859 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07001860 * Dialog to request user confirmation before disabling data.
1861 */
1862 public static class ConfirmDataDisableFragment extends DialogFragment {
1863 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001864 if (!parent.isAdded()) return;
1865
Jeff Sharkey28130d92011-09-02 16:10:24 -07001866 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
1867 dialog.setTargetFragment(parent, 0);
1868 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
1869 }
1870
1871 @Override
1872 public Dialog onCreateDialog(Bundle savedInstanceState) {
1873 final Context context = getActivity();
1874
1875 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1876 builder.setMessage(R.string.data_usage_disable_mobile);
1877
1878 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001879 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07001880 public void onClick(DialogInterface dialog, int which) {
1881 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1882 if (target != null) {
1883 // TODO: extend to modify policy enabled flag.
1884 target.setMobileDataEnabled(false);
1885 }
1886 }
1887 });
1888 builder.setNegativeButton(android.R.string.cancel, null);
1889
1890 return builder.create();
1891 }
1892 }
1893
1894 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001895 * Dialog to request user confirmation before setting
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001896 * {@link android.provider.Settings.Secure#DATA_ROAMING}.
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001897 */
1898 public static class ConfirmDataRoamingFragment extends DialogFragment {
1899 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001900 if (!parent.isAdded()) return;
1901
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001902 final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment();
1903 dialog.setTargetFragment(parent, 0);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001904 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_ROAMING);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001905 }
1906
1907 @Override
1908 public Dialog onCreateDialog(Bundle savedInstanceState) {
1909 final Context context = getActivity();
1910
1911 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1912 builder.setTitle(R.string.roaming_reenable_title);
1913 builder.setMessage(R.string.roaming_warning);
1914
1915 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001916 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001917 public void onClick(DialogInterface dialog, int which) {
1918 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1919 if (target != null) {
1920 target.setDataRoaming(true);
1921 }
1922 }
1923 });
1924 builder.setNegativeButton(android.R.string.cancel, null);
1925
1926 return builder.create();
1927 }
1928 }
1929
1930 /**
1931 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001932 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001933 */
1934 public static class ConfirmRestrictFragment extends DialogFragment {
1935 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001936 if (!parent.isAdded()) return;
1937
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001938 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1939 dialog.setTargetFragment(parent, 0);
1940 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1941 }
1942
1943 @Override
1944 public Dialog onCreateDialog(Bundle savedInstanceState) {
1945 final Context context = getActivity();
1946
1947 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001948 builder.setTitle(R.string.data_usage_restrict_background_title);
Jeff Sharkey461842a2011-09-25 18:22:48 -07001949 builder.setMessage(getString(R.string.data_usage_restrict_background));
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001950
1951 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001952 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001953 public void onClick(DialogInterface dialog, int which) {
1954 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1955 if (target != null) {
1956 target.setRestrictBackground(true);
1957 }
1958 }
1959 });
1960 builder.setNegativeButton(android.R.string.cancel, null);
1961
1962 return builder.create();
1963 }
1964 }
1965
1966 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001967 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
1968 * change has been denied, usually based on
1969 * {@link DataUsageSummary#hasLimitedNetworks()}.
1970 */
1971 public static class DeniedRestrictFragment extends DialogFragment {
1972 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001973 if (!parent.isAdded()) return;
1974
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001975 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
1976 dialog.setTargetFragment(parent, 0);
1977 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
1978 }
1979
1980 @Override
1981 public Dialog onCreateDialog(Bundle savedInstanceState) {
1982 final Context context = getActivity();
1983
1984 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1985 builder.setTitle(R.string.data_usage_app_restrict_background);
1986 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
1987 builder.setPositiveButton(android.R.string.ok, null);
1988
1989 return builder.create();
1990 }
1991 }
1992
1993 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001994 * Dialog to request user confirmation before setting
1995 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
1996 */
1997 public static class ConfirmAppRestrictFragment extends DialogFragment {
1998 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001999 if (!parent.isAdded()) return;
2000
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002001 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2002 dialog.setTargetFragment(parent, 0);
2003 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2004 }
2005
2006 @Override
2007 public Dialog onCreateDialog(Bundle savedInstanceState) {
2008 final Context context = getActivity();
2009
2010 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002011 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2012 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2013
2014 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002015 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002016 public void onClick(DialogInterface dialog, int which) {
2017 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2018 if (target != null) {
2019 target.setAppRestrictBackground(true);
2020 }
2021 }
2022 });
2023 builder.setNegativeButton(android.R.string.cancel, null);
2024
2025 return builder.create();
2026 }
2027 }
2028
2029 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002030 * Compute default tab that should be selected, based on
2031 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2032 */
2033 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002034 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
2035 if (template == null) return null;
2036
2037 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002038 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002039 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002040 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002041 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002042 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002043 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002044 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002045 return TAB_WIFI;
2046 default:
2047 return null;
2048 }
2049 }
2050
2051 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002052 * Background task that loads {@link UidDetail}, binding to
2053 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002054 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002055 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2056 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002057 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002058 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002059
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002060 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002061 mProvider = checkNotNull(provider);
2062 mItem = checkNotNull(item);
2063 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002064 }
2065
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002066 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002067 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002068 final UidDetailTask existing = (UidDetailTask) target.getTag();
2069 if (existing != null) {
2070 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002071 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002072
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002073 final UidDetail cachedDetail = provider.getUidDetail(item.appId, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002074 if (cachedDetail != null) {
2075 bindView(cachedDetail, target);
2076 } else {
2077 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2078 AsyncTask.THREAD_POOL_EXECUTOR));
2079 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002080 }
2081
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002082 private static void bindView(UidDetail detail, View target) {
2083 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2084 final TextView title = (TextView) target.findViewById(android.R.id.title);
2085
2086 if (detail != null) {
2087 icon.setImageDrawable(detail.icon);
2088 title.setText(detail.label);
2089 } else {
2090 icon.setImageDrawable(null);
2091 title.setText(null);
2092 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002093 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002094
2095 @Override
2096 protected void onPreExecute() {
2097 bindView(null, mTarget);
2098 }
2099
2100 @Override
2101 protected UidDetail doInBackground(Void... params) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002102 return mProvider.getUidDetail(mItem.appId, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002103 }
2104
2105 @Override
2106 protected void onPostExecute(UidDetail result) {
2107 bindView(result, mTarget);
2108 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002109 }
2110
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002111 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002112 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002113 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002114 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002115 if (TEST_RADIOS) {
2116 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2117 }
2118
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002119 final ConnectivityManager conn = ConnectivityManager.from(context);
2120 final TelephonyManager tele = TelephonyManager.from(context);
2121
2122 // require both supported network and ready SIM
2123 return conn.isNetworkSupported(TYPE_MOBILE) && tele.getSimState() == SIM_STATE_READY;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002124 }
2125
2126 /**
2127 * Test if device has a mobile 4G data radio.
2128 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002129 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002130 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2131 return false;
2132 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002133 if (TEST_RADIOS) {
2134 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2135 }
2136
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002137 final ConnectivityManager conn = ConnectivityManager.from(context);
2138 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002139
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002140 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002141 final boolean hasLte = (tele.getLteOnCdmaMode() == Phone.LTE_ON_CDMA_TRUE)
2142 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002143 return hasWimax || hasLte;
2144 }
2145
2146 /**
2147 * Test if device has a Wi-Fi data radio.
2148 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002149 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002150 if (TEST_RADIOS) {
2151 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2152 }
2153
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002154 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002155 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002156 }
2157
2158 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002159 * Test if device has an ethernet network connection.
2160 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002161 public static boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002162 if (TEST_RADIOS) {
2163 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2164 }
2165
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002166 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002167 return conn.isNetworkSupported(TYPE_ETHERNET);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002168 }
2169
2170 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002171 * Inflate a {@link Preference} style layout, adding the given {@link View}
2172 * widget into {@link android.R.id#widget_frame}.
2173 */
2174 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2175 final View view = inflater.inflate(R.layout.preference, root, false);
2176 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2177 android.R.id.widget_frame);
2178 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2179 return view;
2180 }
2181
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002182 private static View inflateAppTitle(
2183 LayoutInflater inflater, ViewGroup root, CharSequence label) {
2184 final TextView view = (TextView) inflater.inflate(
2185 R.layout.data_usage_app_title, root, false);
2186 view.setText(label);
2187 return view;
2188 }
2189
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002190 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002191 * Test if any networks are currently limited.
2192 */
2193 private boolean hasLimitedNetworks() {
2194 return !buildLimitedNetworksList().isEmpty();
2195 }
2196
2197 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002198 * Build string describing currently limited networks, which defines when
2199 * background data is restricted.
2200 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002201 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002202 private CharSequence buildLimitedNetworksString() {
2203 final List<CharSequence> limited = buildLimitedNetworksList();
2204
2205 // handle case where no networks limited
2206 if (limited.isEmpty()) {
2207 limited.add(getText(R.string.data_usage_list_none));
2208 }
2209
2210 return TextUtils.join(limited);
2211 }
2212
2213 /**
2214 * Build list of currently limited networks, which defines when background
2215 * data is restricted.
2216 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002217 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002218 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002219 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002220
2221 // build combined list of all limited networks
2222 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002223
2224 final TelephonyManager tele = TelephonyManager.from(context);
2225 if (tele.getSimState() == SIM_STATE_READY) {
2226 final String subscriberId = getActiveSubscriberId(context);
2227 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2228 limited.add(getText(R.string.data_usage_list_mobile));
2229 }
2230 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2231 limited.add(getText(R.string.data_usage_tab_3g));
2232 }
2233 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2234 limited.add(getText(R.string.data_usage_tab_4g));
2235 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002236 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002237
2238 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002239 limited.add(getText(R.string.data_usage_tab_wifi));
2240 }
2241 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2242 limited.add(getText(R.string.data_usage_tab_ethernet));
2243 }
2244
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002245 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002246 }
2247
2248 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002249 * Inset both selector and divider {@link Drawable} on the given
2250 * {@link ListView} by the requested dimensions.
2251 */
2252 private static void insetListViewDrawables(ListView view, int insetSide) {
2253 final Drawable selector = view.getSelector();
2254 final Drawable divider = view.getDivider();
2255
2256 // fully unregister these drawables so callbacks can be maintained after
2257 // wrapping below.
2258 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2259 view.setSelector(stub);
2260 view.setDivider(stub);
2261
2262 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2263 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2264 }
2265
2266 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002267 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002268 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002269 */
2270 private static void setPreferenceTitle(View parent, int resId) {
2271 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2272 title.setText(resId);
2273 }
2274
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002275 /**
2276 * Set {@link android.R.id#summary} for a preference view inflated with
2277 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2278 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002279 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002280 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2281 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002282 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002283 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002284}