blob: 7d45461935fece58b22902437663da9d890936d5 [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 Sharkey05cc0cc2011-06-12 23:11:24 -0700281 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700282 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700283
Jeff Sharkey8a503642011-06-10 13:31:21 -0700284 @Override
285 public View onCreateView(LayoutInflater inflater, ViewGroup container,
286 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700287
Jeff Sharkey8a503642011-06-10 13:31:21 -0700288 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700289 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
290
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700291 mUidDetailProvider = new UidDetailProvider(context);
292
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700293 try {
294 mStatsSession = mStatsService.openSession();
295 } catch (RemoteException e) {
296 throw new RuntimeException(e);
297 }
298
Jeff Sharkey8a503642011-06-10 13:31:21 -0700299 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700300 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700301 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
302 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700303
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700304 // decide if we need to manually inset our content, or if we should rely
305 // on parent container for inset.
306 final boolean shouldInset = mListView.getScrollBarStyle()
307 == View.SCROLLBARS_OUTSIDE_OVERLAY;
308 if (shouldInset) {
309 mInsetSide = view.getResources().getDimensionPixelOffset(
310 com.android.internal.R.dimen.preference_fragment_padding_side);
311 } else {
312 mInsetSide = 0;
313 }
314
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700315 // adjust padding around tabwidget as needed
Jeff Sharkey5d706792011-09-08 18:57:17 -0700316 prepareCustomPreferencesList(container, view, mListView, true);
317
Jeff Sharkey8a503642011-06-10 13:31:21 -0700318 mTabHost.setup();
319 mTabHost.setOnTabChangedListener(mTabListener);
320
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700321 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700322 mHeader.setClickable(true);
323
324 mListView.addHeaderView(mHeader, null, true);
325 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700326
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700327 if (mInsetSide > 0) {
328 // inset selector and divider drawables
329 insetListViewDrawables(mListView, mInsetSide);
330 mHeader.setPadding(mInsetSide, 0, mInsetSide, 0);
331 }
332
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700333 {
334 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700335 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
336 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700337 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700338
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700339 mDataEnabled = new Switch(inflater.getContext());
340 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
341 mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener);
342 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700343
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700344 mDisableAtLimit = new CheckBox(inflater.getContext());
345 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700346 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700347 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700348 mDisableAtLimitView.setClickable(true);
349 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700350 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
351 mNetworkSwitches.addView(mDisableAtLimitView);
352 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700353
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700354 // bind cycle dropdown
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700355 mCycleView = mHeader.findViewById(R.id.cycles);
356 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700357 mCycleAdapter = new CycleAdapter(context);
358 mCycleSpinner.setAdapter(mCycleAdapter);
359 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
360
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700361 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700362 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800363 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700364
365 {
366 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700367 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700368 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
369 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700370 mAppPieChart = (PieChartView) mAppDetail.findViewById(R.id.app_pie_chart);
371 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
372 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700373 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700374
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700375 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700376 mAppSettings.setOnClickListener(mAppSettingsListener);
377
378 mAppRestrict = new CheckBox(inflater.getContext());
379 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700380 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700381 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700382 mAppRestrictView.setClickable(true);
383 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700384 mAppRestrictView.setOnClickListener(mAppRestrictListener);
385 mAppSwitches.addView(mAppRestrictView);
386 }
387
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700388 mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700389 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700390
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700391 // only assign layout transitions once first layout is finished
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700392 mListView.getViewTreeObserver().addOnGlobalLayoutListener(mFirstLayoutListener);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700393
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700394 mAdapter = new DataUsageAdapter(mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700395 mListView.setOnItemClickListener(mListListener);
396 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700397
398 return view;
399 }
400
401 @Override
402 public void onResume() {
403 super.onResume();
404
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700405 // pick default tab based on incoming intent
406 final Intent intent = getActivity().getIntent();
407 mIntentTab = computeTabFromIntent(intent);
408
Jeff Sharkey8a503642011-06-10 13:31:21 -0700409 // this kicks off chain reaction which creates tabs, binds the body to
410 // selected network, and binds chart, cycles and detail list.
411 updateTabs();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700412
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700413 // kick off background task to update stats
414 new AsyncTask<Void, Void, Void>() {
415 @Override
416 protected Void doInBackground(Void... params) {
417 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700418 // wait a few seconds before kicking off
419 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700420 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700421 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700422 } catch (RemoteException e) {
423 }
424 return null;
425 }
426
427 @Override
428 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700429 if (isAdded()) {
430 updateBody();
431 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700432 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700433 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700434 }
435
Jeff Sharkey8a503642011-06-10 13:31:21 -0700436 @Override
437 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
438 inflater.inflate(R.menu.data_usage, menu);
439 }
440
441 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700442 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700443 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700444 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700445
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700446 mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700447 mMenuDataRoaming.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700448 mMenuDataRoaming.setChecked(getDataRoaming());
449
450 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700451 mMenuRestrictBackground.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
452 mMenuRestrictBackground.setChecked(mPolicyManager.getRestrictBackground());
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700453
454 final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g);
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700455 split4g.setVisible(hasReadyMobile4gRadio(context) && !appDetailMode);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700456 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700457
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700458 final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700459 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700460 showWifi.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700461 showWifi.setChecked(mShowWifi);
462 } else {
463 showWifi.setVisible(false);
464 mShowWifi = true;
465 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700466
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700467 final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700468 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700469 showEthernet.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700470 showEthernet.setChecked(mShowEthernet);
471 } else {
472 showEthernet.setVisible(false);
473 mShowEthernet = true;
474 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700475
476 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
477 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
478 metered.setVisible(!appDetailMode);
479 } else {
480 metered.setVisible(false);
481 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700482
483 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
484 String helpUrl;
485 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
486 Intent helpIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
487 helpIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
488 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
489 help.setIntent(helpIntent);
490 help.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
491 } else {
492 help.setVisible(false);
493 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700494 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700495
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700496 @Override
497 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700498 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700499 case R.id.data_usage_menu_roaming: {
500 final boolean dataRoaming = !item.isChecked();
501 if (dataRoaming) {
502 ConfirmDataRoamingFragment.show(this);
503 } else {
504 // no confirmation to disable roaming
505 setDataRoaming(false);
506 }
507 return true;
508 }
509 case R.id.data_usage_menu_restrict_background: {
510 final boolean restrictBackground = !item.isChecked();
511 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800512 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700513 } else {
514 // no confirmation to drop restriction
515 setRestrictBackground(false);
516 }
517 return true;
518 }
519 case R.id.data_usage_menu_split_4g: {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700520 final boolean mobileSplit = !item.isChecked();
Jeff Sharkeya662e492011-06-18 21:57:06 -0700521 setMobilePolicySplit(mobileSplit);
522 item.setChecked(isMobilePolicySplit());
Jeff Sharkey8a503642011-06-10 13:31:21 -0700523 updateTabs();
524 return true;
525 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700526 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700527 mShowWifi = !item.isChecked();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700528 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700529 item.setChecked(mShowWifi);
530 updateTabs();
531 return true;
532 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700533 case R.id.data_usage_menu_show_ethernet: {
534 mShowEthernet = !item.isChecked();
535 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
536 item.setChecked(mShowEthernet);
537 updateTabs();
538 return true;
539 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700540 case R.id.data_usage_menu_metered: {
541 final PreferenceActivity activity = (PreferenceActivity) getActivity();
542 activity.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
543 R.string.data_usage_metered_title, null, this, 0);
544 return true;
545 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700546 }
547 return false;
548 }
549
Jeff Sharkey94a90952011-06-13 22:31:09 -0700550 @Override
551 public void onDestroyView() {
552 super.onDestroyView();
553
554 mDataEnabledView = null;
555 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700556
557 mUidDetailProvider.clearCache();
558 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700559
560 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700561 }
562
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800563 @Override
564 public void onDestroy() {
565 if (this.isRemoving()) {
566 getFragmentManager()
567 .popBackStack(TAG_APP_DETAILS, FragmentManager.POP_BACK_STACK_INCLUSIVE);
568 }
569 super.onDestroy();
570 }
571
Jeff Sharkey8a503642011-06-10 13:31:21 -0700572 /**
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700573 * Listener to setup {@link LayoutTransition} after first layout pass.
574 */
575 private OnGlobalLayoutListener mFirstLayoutListener = new OnGlobalLayoutListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700576 @Override
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700577 public void onGlobalLayout() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700578 mListView.getViewTreeObserver().removeOnGlobalLayoutListener(mFirstLayoutListener);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700579
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700580 mTabsContainer.setLayoutTransition(buildLayoutTransition());
581 mHeader.setLayoutTransition(buildLayoutTransition());
582 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700583
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700584 final LayoutTransition chartTransition = buildLayoutTransition();
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700585 chartTransition.setStartDelay(LayoutTransition.APPEARING, 0);
586 chartTransition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700587 chartTransition.setAnimator(LayoutTransition.APPEARING, null);
Jeff Sharkey28130d92011-09-02 16:10:24 -0700588 chartTransition.setAnimator(LayoutTransition.DISAPPEARING, null);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700589 mChart.setLayoutTransition(chartTransition);
590 }
591 };
592
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700593 private static LayoutTransition buildLayoutTransition() {
594 final LayoutTransition transition = new LayoutTransition();
595 if (TEST_ANIM) {
596 transition.setDuration(1500);
597 }
598 transition.setAnimateParentHierarchy(false);
599 return transition;
600 }
601
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700602 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700603 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700604 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
605 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700606 */
607 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700608 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700609 mTabHost.clearAllTabs();
610
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700611 final boolean mobileSplit = isMobilePolicySplit();
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700612 if (mobileSplit && hasReadyMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700613 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
614 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700615 } else if (hasReadyMobileRadio(context)) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700616 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700617 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700618 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700619 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
620 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700621 if (mShowEthernet && hasEthernet(context)) {
622 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
623 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700624
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700625 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700626 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
627 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
628 if (mIntentTab != null) {
629 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700630 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700631 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700632 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700633 mTabHost.setCurrentTabByTag(mIntentTab);
634 }
635 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700636 } else if (noTabs) {
637 // no usable tabs, so hide body
638 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700639 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700640 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700641 }
642 }
643
Jeff Sharkey8a503642011-06-10 13:31:21 -0700644 /**
645 * Factory that provide empty {@link View} to make {@link TabHost} happy.
646 */
647 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700648 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700649 public View createTabContent(String tag) {
650 return new View(mTabHost.getContext());
651 }
652 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700653
Jeff Sharkey8a503642011-06-10 13:31:21 -0700654 /**
655 * Build {@link TabSpec} with thin indicator, and empty content.
656 */
657 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700658 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
659 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700660 }
661
662 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700663 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700664 public void onTabChanged(String tabId) {
665 // user changed tab; update body
666 updateBody();
667 }
668 };
669
670 /**
671 * Update body content based on current tab. Loads
672 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
673 * binds them to visible controls.
674 */
675 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700676 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700677 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700678
Jeff Sharkeya662e492011-06-18 21:57:06 -0700679 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700680 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700681
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700682 if (currentTab == null) {
683 Log.w(TAG, "no tab selected; hiding body");
684 mListView.setVisibility(View.GONE);
685 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700686 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700687 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700688 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700689
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700690 final boolean tabChanged = !currentTab.equals(mCurrentTab);
691 mCurrentTab = currentTab;
692
Jeff Sharkey8a503642011-06-10 13:31:21 -0700693 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
694
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700695 mDataEnabledView.setVisibility(View.VISIBLE);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700696
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700697 // TODO: remove mobile tabs when SIM isn't ready
698 final TelephonyManager tele = TelephonyManager.from(context);
699
Jeff Sharkey8a503642011-06-10 13:31:21 -0700700 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700701 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
702 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700703 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700704
705 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700706 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
707 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
708 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700709 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700710
711 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700712 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
713 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
714 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700715 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700716
717 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700718 // wifi doesn't have any controls
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700719 mDataEnabledView.setVisibility(View.GONE);
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700720 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700721 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700722
723 } else if (TAB_ETHERNET.equals(currentTab)) {
724 // ethernet doesn't have any controls
725 mDataEnabledView.setVisibility(View.GONE);
726 mDisableAtLimitView.setVisibility(View.GONE);
727 mTemplate = buildTemplateEthernet();
728
729 } else {
730 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700731 }
732
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700733 // kick off loader for network history
734 // TODO: consider chaining two loaders together instead of reloading
735 // network history when showing app detail.
736 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700737 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700738
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700739 // detail mode can change visible menus, invalidate
740 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700741
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700742 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700743 }
744
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700745 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700746 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700747 }
748
749 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700750 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700751 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700752 */
753 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700754 final Context context = getActivity();
755 final PackageManager pm = context.getPackageManager();
756 final LayoutInflater inflater = getActivity().getLayoutInflater();
757
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700758 if (isAppDetailMode()) {
759 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700760 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700761 } else {
762 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700763 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700764
765 // hide detail stats when not in detail mode
766 mChart.bindDetailNetworkStats(null);
767 return;
768 }
769
770 // remove warning/limit sweeps while in detail mode
771 mChart.bindNetworkPolicy(null);
772
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700773 // show icon and all labels appearing under this app
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700774 final int appId = mCurrentApp.appId;
775 final UidDetail detail = mUidDetailProvider.getUidDetail(appId, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700776 mAppIcon.setImageDrawable(detail.icon);
777
778 mAppTitles.removeAllViews();
779 if (detail.detailLabels != null) {
780 for (CharSequence label : detail.detailLabels) {
781 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label));
782 }
783 } else {
784 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label));
785 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700786
787 // enable settings button when package provides it
788 // TODO: target torwards entire UID instead of just first package
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700789 final String[] packageNames = pm.getPackagesForUid(appId);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700790 if (packageNames != null && packageNames.length > 0) {
791 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
792 mAppSettingsIntent.setPackage(packageNames[0]);
793 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
794
795 final boolean matchFound = pm.resolveActivity(mAppSettingsIntent, 0) != null;
796 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -0700797 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700798
799 } else {
800 mAppSettingsIntent = null;
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700801 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700802 }
803
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700804 updateDetailData();
805
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700806 if (UserId.isApp(appId) && !mPolicyManager.getRestrictBackground()
807 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700808 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800809 setPreferenceSummary(mAppRestrictView,
810 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700811
812 mAppRestrictView.setVisibility(View.VISIBLE);
813 mAppRestrict.setChecked(getAppRestrictBackground());
814
815 } else {
816 mAppRestrictView.setVisibility(View.GONE);
817 }
818 }
819
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700820 private void setPolicyWarningBytes(long warningBytes) {
821 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700822 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700823 updatePolicy(false);
824 }
825
826 private void setPolicyLimitBytes(long limitBytes) {
827 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700828 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700829 updatePolicy(false);
830 }
831
Jeff Sharkey28130d92011-09-02 16:10:24 -0700832 /**
833 * Local cache of value, used to work around delay when
834 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
835 */
836 private Boolean mMobileDataEnabled;
837
838 private boolean isMobileDataEnabled() {
839 if (mMobileDataEnabled != null) {
840 // TODO: deprecate and remove this once enabled flag is on policy
841 return mMobileDataEnabled;
842 } else {
843 return mConnService.getMobileDataEnabled();
844 }
845 }
846
847 private void setMobileDataEnabled(boolean enabled) {
848 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
849 mConnService.setMobileDataEnabled(enabled);
850 mMobileDataEnabled = enabled;
851 updatePolicy(false);
852 }
853
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700854 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
855 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked();
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700856 }
857
858 private boolean isBandwidthControlEnabled() {
859 try {
860 return mNetworkService.isBandwidthControlEnabled();
861 } catch (RemoteException e) {
862 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
863 return false;
864 }
865 }
866
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700867 private boolean getDataRoaming() {
868 final ContentResolver resolver = getActivity().getContentResolver();
869 return Settings.Secure.getInt(resolver, Settings.Secure.DATA_ROAMING, 0) != 0;
870 }
871
872 private void setDataRoaming(boolean enabled) {
873 // TODO: teach telephony DataConnectionTracker to watch and apply
874 // updates when changed.
875 final ContentResolver resolver = getActivity().getContentResolver();
876 Settings.Secure.putInt(resolver, Settings.Secure.DATA_ROAMING, enabled ? 1 : 0);
877 mMenuDataRoaming.setChecked(enabled);
878 }
879
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700880 public void setRestrictBackground(boolean restrictBackground) {
881 mPolicyManager.setRestrictBackground(restrictBackground);
882 mMenuRestrictBackground.setChecked(restrictBackground);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700883 }
884
885 private boolean getAppRestrictBackground() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700886 final int appId = mCurrentApp.appId;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700887 final int uidPolicy = mPolicyManager.getAppPolicy(appId);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700888 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
889 }
890
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700891 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700892 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700893 final int appId = mCurrentApp.appId;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700894 mPolicyManager.setAppPolicy(appId,
895 restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700896 mAppRestrict.setChecked(restrictBackground);
897 }
898
Jeff Sharkey8a503642011-06-10 13:31:21 -0700899 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700900 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
901 * current {@link #mTemplate}.
902 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700903 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700904 if (isAppDetailMode()) {
905 mNetworkSwitches.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700906 } else {
907 mNetworkSwitches.setVisibility(View.VISIBLE);
908 }
909
Jeff Sharkey28130d92011-09-02 16:10:24 -0700910 // TODO: move enabled state directly into policy
911 if (TAB_MOBILE.equals(mCurrentTab)) {
912 mBinding = true;
913 mDataEnabled.setChecked(isMobileDataEnabled());
914 mBinding = false;
915 }
916
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700917 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
918 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700919 mDisableAtLimitView.setVisibility(View.VISIBLE);
920 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700921 if (!isAppDetailMode()) {
922 mChart.bindNetworkPolicy(policy);
923 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700924
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700925 } else {
926 // controls are disabled; don't bind warning/limit sweeps
927 mDisableAtLimitView.setVisibility(View.GONE);
928 mChart.bindNetworkPolicy(null);
929 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700930
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700931 if (refreshCycle) {
932 // generate cycle list based on policy and available history
933 updateCycleList(policy);
934 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700935 }
936
937 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700938 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
939 * and available {@link NetworkStatsHistory} data. Always selects the newest
940 * item, updating the inspection range on {@link #mChart}.
941 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700942 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700943 // stash away currently selected cycle to try restoring below
944 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700945 mCycleAdapter.clear();
946
947 final Context context = mCycleSpinner.getContext();
948
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700949 long historyStart = Long.MAX_VALUE;
950 long historyEnd = Long.MIN_VALUE;
951 if (mChartData != null) {
952 historyStart = mChartData.network.getStart();
953 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700954 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700955
Jeff Sharkey461842a2011-09-25 18:22:48 -0700956 final long now = System.currentTimeMillis();
957 if (historyStart == Long.MAX_VALUE) historyStart = now;
958 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700959
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700960 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700961 if (policy != null) {
962 // find the next cycle boundary
963 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700964
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700965 // walk backwards, generating all valid cycle ranges
966 while (cycleEnd > historyStart) {
967 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
968 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
969 + historyStart);
970 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
971 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700972 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700973 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700974
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700975 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700976 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700977 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700978
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700979 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700980 // no policy defined cycles; show entry for each four-week period
981 long cycleEnd = historyEnd;
982 while (cycleEnd > historyStart) {
983 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
984 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
985 cycleEnd = cycleStart;
986 }
987
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700988 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700989 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700990
991 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700992 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700993 final int position = mCycleAdapter.findNearestPosition(previousItem);
994 mCycleSpinner.setSelection(position);
995
996 // only force-update cycle when changed; skipping preserves any
997 // user-defined inspection region.
998 final CycleItem selectedItem = mCycleAdapter.getItem(position);
999 if (!Objects.equal(selectedItem, previousItem)) {
1000 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1001 } else {
1002 // but still kick off loader for detailed list
1003 updateDetailData();
1004 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001005 } else {
1006 updateDetailData();
1007 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001008 }
1009
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001010 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001011 @Override
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001012 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
1013 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001014
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001015 final boolean dataEnabled = isChecked;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001016 final String currentTab = mCurrentTab;
1017 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001018 if (dataEnabled) {
1019 setMobileDataEnabled(true);
1020 } else {
1021 // disabling data; show confirmation dialog which eventually
1022 // calls setMobileDataEnabled() once user confirms.
1023 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1024 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001025 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001026
Jeff Sharkey28130d92011-09-02 16:10:24 -07001027 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001028 }
1029 };
1030
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001031 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001032 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001033 public void onClick(View v) {
1034 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001035 if (disableAtLimit) {
1036 // enabling limit; show confirmation dialog which eventually
1037 // calls setPolicyLimitBytes() once user confirms.
1038 ConfirmLimitFragment.show(DataUsageSummary.this);
1039 } else {
1040 setPolicyLimitBytes(LIMIT_DISABLED);
1041 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001042 }
1043 };
1044
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001045 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001046 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001047 public void onClick(View v) {
1048 final boolean restrictBackground = !mAppRestrict.isChecked();
1049
1050 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001051 // enabling restriction; show confirmation dialog which
1052 // eventually calls setRestrictBackground() once user
1053 // confirms.
1054 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001055 } else {
1056 setAppRestrictBackground(false);
1057 }
1058 }
1059 };
1060
1061 private OnClickListener mAppSettingsListener = new OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001062 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001063 public void onClick(View v) {
Jeff Sharkeyd92e0412012-04-24 11:35:43 -07001064 if (!isAdded()) return;
1065
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001066 // TODO: target torwards entire UID instead of just first package
1067 startActivity(mAppSettingsIntent);
1068 }
1069 };
1070
Jeff Sharkey8a503642011-06-10 13:31:21 -07001071 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001072 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001073 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001074 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001075 final AppItem app = (AppItem) parent.getItemAtPosition(position);
1076 final UidDetail detail = mUidDetailProvider.getUidDetail(app.appId, true);
1077 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001078 }
1079 };
1080
1081 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001082 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001083 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1084 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1085 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001086 // show cycle editor; will eventually call setPolicyCycleDay()
1087 // when user finishes editing.
1088 CycleEditorFragment.show(DataUsageSummary.this);
1089
1090 // reset spinner to something other than "change cycle..."
1091 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001092
1093 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001094 if (LOGD) {
1095 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1096 + cycle.end + "]");
1097 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001098
1099 // update chart to show selected cycle, and update detail data
1100 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001101 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001102
1103 updateDetailData();
1104 }
1105 }
1106
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001107 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001108 public void onNothingSelected(AdapterView<?> parent) {
1109 // ignored
1110 }
1111 };
1112
1113 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001114 * Update details based on {@link #mChart} inspection range depending on
1115 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1116 * of applications data usage, and when {@link #isAppDetailMode()} update
1117 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001118 */
1119 private void updateDetailData() {
1120 if (LOGD) Log.d(TAG, "updateDetailData()");
1121
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001122 final long start = mChart.getInspectStart();
1123 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001124 final long now = System.currentTimeMillis();
1125
1126 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001127
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001128 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001129 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001130 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001131 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001132 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001133 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001134 final long foregroundBytes = entry.rxBytes + entry.txBytes;
1135
1136 mAppPieChart.setOriginAngle(175);
1137
1138 mAppPieChart.removeAllSlices();
1139 mAppPieChart.addSlice(foregroundBytes, Color.parseColor("#d88d3a"));
1140 mAppPieChart.addSlice(defaultBytes, Color.parseColor("#666666"));
1141
1142 mAppPieChart.generatePath();
1143
1144 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1145 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1146
1147 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001148 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001149
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001150 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001151
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001152 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001153 if (mChartData != null) {
1154 entry = mChartData.network.getValues(start, end, now, null);
1155 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001156
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001157 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001158 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001159 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001160 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001161
1162 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1163 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001164 final String rangePhrase = formatDateRange(context, start, end);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001165
Jeff Sharkeye557c332012-04-13 16:04:07 -07001166 final int summaryRes;
1167 if (TAB_MOBILE.equals(mCurrentTab) || TAB_3G.equals(mCurrentApp)
1168 || TAB_4G.equals(mCurrentApp)) {
1169 summaryRes = R.string.data_usage_total_during_range_mobile;
1170 } else {
1171 summaryRes = R.string.data_usage_total_during_range;
1172 }
1173
1174 mUsageSummary.setText(getString(summaryRes, totalPhrase, rangePhrase));
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001175 }
1176
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001177 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1178 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001179 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001180 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001181 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001182 }
1183
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001184 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001185 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1186 mChartData = data;
1187 mChart.bindNetworkStats(mChartData.network);
1188 mChart.bindDetailNetworkStats(mChartData.detail);
1189
1190 // calcuate policy cycles based on available data
1191 updatePolicy(true);
1192 updateAppDetail();
1193
1194 // force scroll to top of body when showing detail
1195 if (mChartData.detail != null) {
1196 mListView.smoothScrollToPosition(0);
1197 }
1198 }
1199
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001200 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001201 public void onLoaderReset(Loader<ChartData> loader) {
1202 mChartData = null;
1203 mChart.bindNetworkStats(null);
1204 mChart.bindDetailNetworkStats(null);
1205 }
1206 };
1207
1208 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001209 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001210 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001211 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001212 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001213 }
1214
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001215 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001216 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001217 final int[] restrictedAppIds = mPolicyManager.getAppsWithPolicy(
1218 POLICY_REJECT_METERED_BACKGROUND);
1219 mAdapter.bindStats(data, restrictedAppIds);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001220 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001221 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001222
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001223 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001224 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001225 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001226 updateEmptyVisible();
1227 }
1228
1229 private void updateEmptyVisible() {
1230 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1231 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001232 }
1233 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001234
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001235 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001236 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001237 final Context context = getActivity();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001238 if (hasReadyMobileRadio(context)) {
1239 final TelephonyManager tele = TelephonyManager.from(context);
1240 return mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001241 } else {
1242 return false;
1243 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001244 }
1245
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001246 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001247 private void setMobilePolicySplit(boolean split) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001248 final Context context = getActivity();
1249 if (hasReadyMobileRadio(context)) {
1250 final TelephonyManager tele = TelephonyManager.from(context);
1251 mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split);
1252 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001253 }
1254
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001255 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001256 final TelephonyManager tele = TelephonyManager.from(context);
1257 final String actualSubscriberId = tele.getSubscriberId();
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -08001258 return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001259 }
1260
Jeff Sharkey8a503642011-06-10 13:31:21 -07001261 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001262 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001263 public void onInspectRangeChanged() {
1264 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
1265 updateDetailData();
1266 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001267
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001268 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001269 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001270 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001271 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001272
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001273 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001274 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001275 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001276 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001277
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001278 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001279 public void requestWarningEdit() {
1280 WarningEditorFragment.show(DataUsageSummary.this);
1281 }
1282
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001283 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001284 public void requestLimitEdit() {
1285 LimitEditorFragment.show(DataUsageSummary.this);
1286 }
1287 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001288
1289 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001290 * List item that reflects a specific data usage cycle.
1291 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001292 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001293 public CharSequence label;
1294 public long start;
1295 public long end;
1296
Jeff Sharkey8a503642011-06-10 13:31:21 -07001297 CycleItem(CharSequence label) {
1298 this.label = label;
1299 }
1300
1301 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001302 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001303 this.start = start;
1304 this.end = end;
1305 }
1306
Jeff Sharkey8a503642011-06-10 13:31:21 -07001307 @Override
1308 public String toString() {
1309 return label.toString();
1310 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001311
1312 @Override
1313 public boolean equals(Object o) {
1314 if (o instanceof CycleItem) {
1315 final CycleItem another = (CycleItem) o;
1316 return start == another.start && end == another.end;
1317 }
1318 return false;
1319 }
1320
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001321 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001322 public int compareTo(CycleItem another) {
1323 return Long.compare(start, another.start);
1324 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001325 }
1326
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001327 private static final StringBuilder sBuilder = new StringBuilder(50);
1328 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1329 sBuilder, Locale.getDefault());
1330
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001331 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001332 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001333
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001334 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001335 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001336 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1337 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001338 }
1339 }
1340
Jeff Sharkey8a503642011-06-10 13:31:21 -07001341 /**
1342 * Special-case data usage cycle that triggers dialog to change
1343 * {@link NetworkPolicy#cycleDay}.
1344 */
1345 public static class CycleChangeItem extends CycleItem {
1346 public CycleChangeItem(Context context) {
1347 super(context.getString(R.string.data_usage_change_cycle));
1348 }
1349 }
1350
1351 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001352 private boolean mChangePossible = false;
1353 private boolean mChangeVisible = false;
1354
1355 private final CycleChangeItem mChangeItem;
1356
Jeff Sharkey8a503642011-06-10 13:31:21 -07001357 public CycleAdapter(Context context) {
1358 super(context, android.R.layout.simple_spinner_item);
1359 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001360 mChangeItem = new CycleChangeItem(context);
1361 }
1362
1363 public void setChangePossible(boolean possible) {
1364 mChangePossible = possible;
1365 updateChange();
1366 }
1367
1368 public void setChangeVisible(boolean visible) {
1369 mChangeVisible = visible;
1370 updateChange();
1371 }
1372
1373 private void updateChange() {
1374 remove(mChangeItem);
1375 if (mChangePossible && mChangeVisible) {
1376 add(mChangeItem);
1377 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001378 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001379
1380 /**
1381 * Find position of {@link CycleItem} in this adapter which is nearest
1382 * the given {@link CycleItem}.
1383 */
1384 public int findNearestPosition(CycleItem target) {
1385 if (target != null) {
1386 final int count = getCount();
1387 for (int i = count - 1; i >= 0; i--) {
1388 final CycleItem item = getItem(i);
1389 if (item instanceof CycleChangeItem) {
1390 continue;
1391 } else if (item.compareTo(target) >= 0) {
1392 return i;
1393 }
1394 }
1395 }
1396 return 0;
1397 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001398 }
1399
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001400 public static class AppItem implements Comparable<AppItem>, Parcelable {
1401 public final int appId;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001402 public boolean restricted;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001403 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001404 public long total;
1405
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001406 public AppItem(int appId) {
1407 this.appId = appId;
1408 }
1409
1410 public AppItem(Parcel parcel) {
1411 appId = parcel.readInt();
1412 uids = parcel.readSparseBooleanArray();
1413 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001414 }
1415
1416 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001417 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001418 }
1419
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001420 @Override
1421 public void writeToParcel(Parcel dest, int flags) {
1422 dest.writeInt(appId);
1423 dest.writeSparseBooleanArray(uids);
1424 dest.writeLong(total);
1425 }
1426
1427 @Override
1428 public int describeContents() {
1429 return 0;
1430 }
1431
1432 @Override
1433 public int compareTo(AppItem another) {
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001434 return Long.compare(another.total, total);
1435 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001436
1437 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1438 @Override
1439 public AppItem createFromParcel(Parcel in) {
1440 return new AppItem(in);
1441 }
1442
1443 @Override
1444 public AppItem[] newArray(int size) {
1445 return new AppItem[size];
1446 }
1447 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001448 }
1449
Jeff Sharkey8a503642011-06-10 13:31:21 -07001450 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001451 * Adapter of applications, sorted by total usage descending.
1452 */
1453 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001454 private final UidDetailProvider mProvider;
1455 private final int mInsetSide;
1456
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001457 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001458 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001459
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001460 public DataUsageAdapter(UidDetailProvider provider, int insetSide) {
1461 mProvider = checkNotNull(provider);
1462 mInsetSide = insetSide;
1463 }
1464
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001465 /**
1466 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1467 */
Jeff Sharkeye557c332012-04-13 16:04:07 -07001468 public void bindStats(NetworkStats stats, int[] restrictedAppIds) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001469 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001470
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001471 final AppItem systemItem = new AppItem(android.os.Process.SYSTEM_UID);
1472 final SparseArray<AppItem> knownUids = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001473
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001474 NetworkStats.Entry entry = null;
1475 final int size = stats != null ? stats.size() : 0;
1476 for (int i = 0; i < size; i++) {
1477 entry = stats.getValues(i, entry);
1478
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001479 final boolean isApp = UserId.isApp(entry.uid);
1480 final int appId = isApp ? UserId.getAppId(entry.uid) : entry.uid;
1481 if (isApp || appId == UID_REMOVED || appId == UID_TETHERING) {
1482 AppItem item = knownUids.get(appId);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001483 if (item == null) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001484 item = new AppItem(appId);
1485 knownUids.put(appId, item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001486 mItems.add(item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001487 }
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001488
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001489 item.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001490 item.addUid(entry.uid);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001491 } else {
1492 systemItem.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001493 systemItem.addUid(entry.uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001494 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001495 }
1496
Jeff Sharkeye557c332012-04-13 16:04:07 -07001497 for (int appId : restrictedAppIds) {
1498 AppItem item = knownUids.get(appId);
1499 if (item == null) {
1500 item = new AppItem(appId);
1501 item.total = -1;
1502 mItems.add(item);
1503 }
1504 item.restricted = true;
1505 }
1506
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001507 if (systemItem.total > 0) {
1508 mItems.add(systemItem);
1509 }
1510
Jeff Sharkey8a503642011-06-10 13:31:21 -07001511 Collections.sort(mItems);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001512 mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001513 notifyDataSetChanged();
1514 }
1515
1516 @Override
1517 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001518 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001519 }
1520
1521 @Override
1522 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001523 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001524 }
1525
1526 @Override
1527 public long getItemId(int position) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001528 return mItems.get(position).appId;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001529 }
1530
1531 @Override
1532 public View getView(int position, View convertView, ViewGroup parent) {
1533 if (convertView == null) {
1534 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey5d706792011-09-08 18:57:17 -07001535 R.layout.data_usage_item, parent, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001536
1537 if (mInsetSide > 0) {
1538 convertView.setPadding(mInsetSide, 0, mInsetSide, 0);
1539 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001540 }
1541
1542 final Context context = parent.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001543
Jeff Sharkey28130d92011-09-02 16:10:24 -07001544 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001545 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1546 android.R.id.progress);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001547
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001548 // kick off async load of app details
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001549 final AppItem item = mItems.get(position);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001550 UidDetailTask.bindView(mProvider, item, convertView);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001551
Jeff Sharkeye557c332012-04-13 16:04:07 -07001552 if (item.restricted && item.total <= 0) {
1553 text1.setText(R.string.data_usage_app_restricted);
1554 progress.setVisibility(View.GONE);
1555 } else {
1556 text1.setText(Formatter.formatFileSize(context, item.total));
1557 progress.setVisibility(View.VISIBLE);
1558 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001559
1560 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1561 progress.setProgress(percentTotal);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001562
1563 return convertView;
1564 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001565 }
1566
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001567 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001568 * Empty {@link Fragment} that controls display of UID details in
1569 * {@link DataUsageSummary}.
1570 */
1571 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001572 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001573
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001574 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001575 if (!parent.isAdded()) return;
1576
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001577 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001578 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001579
1580 final AppDetailsFragment fragment = new AppDetailsFragment();
1581 fragment.setArguments(args);
1582 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001583 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1584 ft.add(fragment, TAG_APP_DETAILS);
1585 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001586 ft.setBreadCrumbTitle(label);
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001587 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001588 }
1589
1590 @Override
1591 public void onStart() {
1592 super.onStart();
1593 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001594 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001595 target.updateBody();
1596 }
1597
1598 @Override
1599 public void onStop() {
1600 super.onStop();
1601 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001602 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001603 target.updateBody();
1604 }
1605 }
1606
1607 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001608 * Dialog to request user confirmation before setting
1609 * {@link NetworkPolicy#limitBytes}.
1610 */
1611 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001612 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001613 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001614
1615 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001616 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001617
Jeff Sharkey461842a2011-09-25 18:22:48 -07001618 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001619 final CharSequence message;
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001620 final long minLimitBytes = (long) (
1621 parent.mPolicyEditor.getPolicy(parent.mTemplate).warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001622 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001623
1624 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001625 final String currentTab = parent.mCurrentTab;
1626 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001627 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001628 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001629 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001630 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001631 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001632 } else if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001633 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001634 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001635 } else {
1636 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001637 }
1638
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001639 final Bundle args = new Bundle();
1640 args.putCharSequence(EXTRA_MESSAGE, message);
1641 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1642
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001643 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1644 dialog.setArguments(args);
1645 dialog.setTargetFragment(parent, 0);
1646 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1647 }
1648
1649 @Override
1650 public Dialog onCreateDialog(Bundle savedInstanceState) {
1651 final Context context = getActivity();
1652
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001653 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001654 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1655
1656 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1657 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001658 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001659
1660 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001661 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001662 public void onClick(DialogInterface dialog, int which) {
1663 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1664 if (target != null) {
1665 target.setPolicyLimitBytes(limitBytes);
1666 }
1667 }
1668 });
1669
1670 return builder.create();
1671 }
1672 }
1673
1674 /**
1675 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1676 */
1677 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001678 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001679
1680 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001681 if (!parent.isAdded()) return;
1682
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001683 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001684 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001685
1686 final CycleEditorFragment dialog = new CycleEditorFragment();
1687 dialog.setArguments(args);
1688 dialog.setTargetFragment(parent, 0);
1689 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1690 }
1691
1692 @Override
1693 public Dialog onCreateDialog(Bundle savedInstanceState) {
1694 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001695 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1696 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001697
1698 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1699 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1700
1701 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1702 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1703
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001704 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1705 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001706
1707 cycleDayPicker.setMinValue(1);
1708 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001709 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001710 cycleDayPicker.setWrapSelectorWheel(true);
1711
1712 builder.setTitle(R.string.data_usage_cycle_editor_title);
1713 builder.setView(view);
1714
1715 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1716 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001717 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001718 public void onClick(DialogInterface dialog, int which) {
1719 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001720 final String cycleTimezone = new Time().timezone;
1721 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001722 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001723 }
1724 });
1725
1726 return builder.create();
1727 }
1728 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001729
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001730 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001731 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1732 */
1733 public static class WarningEditorFragment extends DialogFragment {
1734 private static final String EXTRA_TEMPLATE = "template";
1735
1736 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001737 if (!parent.isAdded()) return;
1738
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001739 final Bundle args = new Bundle();
1740 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1741
1742 final WarningEditorFragment dialog = new WarningEditorFragment();
1743 dialog.setArguments(args);
1744 dialog.setTargetFragment(parent, 0);
1745 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1746 }
1747
1748 @Override
1749 public Dialog onCreateDialog(Bundle savedInstanceState) {
1750 final Context context = getActivity();
1751 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1752 final NetworkPolicyEditor editor = target.mPolicyEditor;
1753
1754 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1755 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1756
1757 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1758 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1759
1760 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1761 final long warningBytes = editor.getPolicyWarningBytes(template);
1762 final long limitBytes = editor.getPolicyLimitBytes(template);
1763
1764 bytesPicker.setMinValue(0);
1765 if (limitBytes != LIMIT_DISABLED) {
1766 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1767 } else {
1768 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1769 }
1770 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1771 bytesPicker.setWrapSelectorWheel(false);
1772
1773 builder.setTitle(R.string.data_usage_warning_editor_title);
1774 builder.setView(view);
1775
1776 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1777 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001778 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001779 public void onClick(DialogInterface dialog, int which) {
1780 // clear focus to finish pending text edits
1781 bytesPicker.clearFocus();
1782
1783 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1784 editor.setPolicyWarningBytes(template, bytes);
1785 target.updatePolicy(false);
1786 }
1787 });
1788
1789 return builder.create();
1790 }
1791 }
1792
1793 /**
1794 * Dialog to edit {@link NetworkPolicy#limitBytes}.
1795 */
1796 public static class LimitEditorFragment extends DialogFragment {
1797 private static final String EXTRA_TEMPLATE = "template";
1798
1799 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001800 if (!parent.isAdded()) return;
1801
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001802 final Bundle args = new Bundle();
1803 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1804
1805 final LimitEditorFragment dialog = new LimitEditorFragment();
1806 dialog.setArguments(args);
1807 dialog.setTargetFragment(parent, 0);
1808 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
1809 }
1810
1811 @Override
1812 public Dialog onCreateDialog(Bundle savedInstanceState) {
1813 final Context context = getActivity();
1814 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1815 final NetworkPolicyEditor editor = target.mPolicyEditor;
1816
1817 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1818 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1819
1820 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1821 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1822
1823 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1824 final long warningBytes = editor.getPolicyWarningBytes(template);
1825 final long limitBytes = editor.getPolicyLimitBytes(template);
1826
1827 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09001828 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001829 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
1830 } else {
1831 bytesPicker.setMinValue(0);
1832 }
1833 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
1834 bytesPicker.setWrapSelectorWheel(false);
1835
1836 builder.setTitle(R.string.data_usage_limit_editor_title);
1837 builder.setView(view);
1838
1839 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1840 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001841 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001842 public void onClick(DialogInterface dialog, int which) {
1843 // clear focus to finish pending text edits
1844 bytesPicker.clearFocus();
1845
1846 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1847 editor.setPolicyLimitBytes(template, bytes);
1848 target.updatePolicy(false);
1849 }
1850 });
1851
1852 return builder.create();
1853 }
1854 }
1855 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07001856 * Dialog to request user confirmation before disabling data.
1857 */
1858 public static class ConfirmDataDisableFragment extends DialogFragment {
1859 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001860 if (!parent.isAdded()) return;
1861
Jeff Sharkey28130d92011-09-02 16:10:24 -07001862 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
1863 dialog.setTargetFragment(parent, 0);
1864 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
1865 }
1866
1867 @Override
1868 public Dialog onCreateDialog(Bundle savedInstanceState) {
1869 final Context context = getActivity();
1870
1871 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1872 builder.setMessage(R.string.data_usage_disable_mobile);
1873
1874 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001875 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07001876 public void onClick(DialogInterface dialog, int which) {
1877 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1878 if (target != null) {
1879 // TODO: extend to modify policy enabled flag.
1880 target.setMobileDataEnabled(false);
1881 }
1882 }
1883 });
1884 builder.setNegativeButton(android.R.string.cancel, null);
1885
1886 return builder.create();
1887 }
1888 }
1889
1890 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001891 * Dialog to request user confirmation before setting
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001892 * {@link android.provider.Settings.Secure#DATA_ROAMING}.
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001893 */
1894 public static class ConfirmDataRoamingFragment extends DialogFragment {
1895 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001896 if (!parent.isAdded()) return;
1897
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001898 final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment();
1899 dialog.setTargetFragment(parent, 0);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001900 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_ROAMING);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001901 }
1902
1903 @Override
1904 public Dialog onCreateDialog(Bundle savedInstanceState) {
1905 final Context context = getActivity();
1906
1907 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1908 builder.setTitle(R.string.roaming_reenable_title);
1909 builder.setMessage(R.string.roaming_warning);
1910
1911 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001912 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001913 public void onClick(DialogInterface dialog, int which) {
1914 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1915 if (target != null) {
1916 target.setDataRoaming(true);
1917 }
1918 }
1919 });
1920 builder.setNegativeButton(android.R.string.cancel, null);
1921
1922 return builder.create();
1923 }
1924 }
1925
1926 /**
1927 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001928 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001929 */
1930 public static class ConfirmRestrictFragment extends DialogFragment {
1931 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001932 if (!parent.isAdded()) return;
1933
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001934 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1935 dialog.setTargetFragment(parent, 0);
1936 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1937 }
1938
1939 @Override
1940 public Dialog onCreateDialog(Bundle savedInstanceState) {
1941 final Context context = getActivity();
1942
1943 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001944 builder.setTitle(R.string.data_usage_restrict_background_title);
Jeff Sharkey461842a2011-09-25 18:22:48 -07001945 builder.setMessage(getString(R.string.data_usage_restrict_background));
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001946
1947 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001948 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001949 public void onClick(DialogInterface dialog, int which) {
1950 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1951 if (target != null) {
1952 target.setRestrictBackground(true);
1953 }
1954 }
1955 });
1956 builder.setNegativeButton(android.R.string.cancel, null);
1957
1958 return builder.create();
1959 }
1960 }
1961
1962 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001963 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
1964 * change has been denied, usually based on
1965 * {@link DataUsageSummary#hasLimitedNetworks()}.
1966 */
1967 public static class DeniedRestrictFragment extends DialogFragment {
1968 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001969 if (!parent.isAdded()) return;
1970
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001971 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
1972 dialog.setTargetFragment(parent, 0);
1973 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
1974 }
1975
1976 @Override
1977 public Dialog onCreateDialog(Bundle savedInstanceState) {
1978 final Context context = getActivity();
1979
1980 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1981 builder.setTitle(R.string.data_usage_app_restrict_background);
1982 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
1983 builder.setPositiveButton(android.R.string.ok, null);
1984
1985 return builder.create();
1986 }
1987 }
1988
1989 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001990 * Dialog to request user confirmation before setting
1991 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
1992 */
1993 public static class ConfirmAppRestrictFragment extends DialogFragment {
1994 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001995 if (!parent.isAdded()) return;
1996
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001997 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
1998 dialog.setTargetFragment(parent, 0);
1999 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2000 }
2001
2002 @Override
2003 public Dialog onCreateDialog(Bundle savedInstanceState) {
2004 final Context context = getActivity();
2005
2006 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002007 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2008 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2009
2010 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002011 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002012 public void onClick(DialogInterface dialog, int which) {
2013 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2014 if (target != null) {
2015 target.setAppRestrictBackground(true);
2016 }
2017 }
2018 });
2019 builder.setNegativeButton(android.R.string.cancel, null);
2020
2021 return builder.create();
2022 }
2023 }
2024
2025 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002026 * Compute default tab that should be selected, based on
2027 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2028 */
2029 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002030 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
2031 if (template == null) return null;
2032
2033 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002034 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002035 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002036 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002037 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002038 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002039 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002040 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002041 return TAB_WIFI;
2042 default:
2043 return null;
2044 }
2045 }
2046
2047 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002048 * Background task that loads {@link UidDetail}, binding to
2049 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002050 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002051 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2052 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002053 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002054 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002055
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002056 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002057 mProvider = checkNotNull(provider);
2058 mItem = checkNotNull(item);
2059 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002060 }
2061
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002062 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002063 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002064 final UidDetailTask existing = (UidDetailTask) target.getTag();
2065 if (existing != null) {
2066 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002067 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002068
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002069 final UidDetail cachedDetail = provider.getUidDetail(item.appId, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002070 if (cachedDetail != null) {
2071 bindView(cachedDetail, target);
2072 } else {
2073 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2074 AsyncTask.THREAD_POOL_EXECUTOR));
2075 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002076 }
2077
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002078 private static void bindView(UidDetail detail, View target) {
2079 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2080 final TextView title = (TextView) target.findViewById(android.R.id.title);
2081
2082 if (detail != null) {
2083 icon.setImageDrawable(detail.icon);
2084 title.setText(detail.label);
2085 } else {
2086 icon.setImageDrawable(null);
2087 title.setText(null);
2088 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002089 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002090
2091 @Override
2092 protected void onPreExecute() {
2093 bindView(null, mTarget);
2094 }
2095
2096 @Override
2097 protected UidDetail doInBackground(Void... params) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002098 return mProvider.getUidDetail(mItem.appId, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002099 }
2100
2101 @Override
2102 protected void onPostExecute(UidDetail result) {
2103 bindView(result, mTarget);
2104 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002105 }
2106
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002107 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002108 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002109 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002110 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002111 if (TEST_RADIOS) {
2112 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2113 }
2114
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002115 final ConnectivityManager conn = ConnectivityManager.from(context);
2116 final TelephonyManager tele = TelephonyManager.from(context);
2117
2118 // require both supported network and ready SIM
2119 return conn.isNetworkSupported(TYPE_MOBILE) && tele.getSimState() == SIM_STATE_READY;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002120 }
2121
2122 /**
2123 * Test if device has a mobile 4G data radio.
2124 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002125 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002126 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2127 return false;
2128 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002129 if (TEST_RADIOS) {
2130 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2131 }
2132
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002133 final ConnectivityManager conn = ConnectivityManager.from(context);
2134 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002135
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002136 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002137 final boolean hasLte = (tele.getLteOnCdmaMode() == Phone.LTE_ON_CDMA_TRUE)
2138 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002139 return hasWimax || hasLte;
2140 }
2141
2142 /**
2143 * Test if device has a Wi-Fi data radio.
2144 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002145 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002146 if (TEST_RADIOS) {
2147 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2148 }
2149
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002150 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002151 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002152 }
2153
2154 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002155 * Test if device has an ethernet network connection.
2156 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002157 public static boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002158 if (TEST_RADIOS) {
2159 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2160 }
2161
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002162 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002163 return conn.isNetworkSupported(TYPE_ETHERNET);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002164 }
2165
2166 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002167 * Inflate a {@link Preference} style layout, adding the given {@link View}
2168 * widget into {@link android.R.id#widget_frame}.
2169 */
2170 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2171 final View view = inflater.inflate(R.layout.preference, root, false);
2172 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2173 android.R.id.widget_frame);
2174 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2175 return view;
2176 }
2177
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002178 private static View inflateAppTitle(
2179 LayoutInflater inflater, ViewGroup root, CharSequence label) {
2180 final TextView view = (TextView) inflater.inflate(
2181 R.layout.data_usage_app_title, root, false);
2182 view.setText(label);
2183 return view;
2184 }
2185
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002186 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002187 * Test if any networks are currently limited.
2188 */
2189 private boolean hasLimitedNetworks() {
2190 return !buildLimitedNetworksList().isEmpty();
2191 }
2192
2193 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002194 * Build string describing currently limited networks, which defines when
2195 * background data is restricted.
2196 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002197 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002198 private CharSequence buildLimitedNetworksString() {
2199 final List<CharSequence> limited = buildLimitedNetworksList();
2200
2201 // handle case where no networks limited
2202 if (limited.isEmpty()) {
2203 limited.add(getText(R.string.data_usage_list_none));
2204 }
2205
2206 return TextUtils.join(limited);
2207 }
2208
2209 /**
2210 * Build list of currently limited networks, which defines when background
2211 * data is restricted.
2212 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002213 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002214 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002215 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002216
2217 // build combined list of all limited networks
2218 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002219
2220 final TelephonyManager tele = TelephonyManager.from(context);
2221 if (tele.getSimState() == SIM_STATE_READY) {
2222 final String subscriberId = getActiveSubscriberId(context);
2223 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2224 limited.add(getText(R.string.data_usage_list_mobile));
2225 }
2226 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2227 limited.add(getText(R.string.data_usage_tab_3g));
2228 }
2229 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2230 limited.add(getText(R.string.data_usage_tab_4g));
2231 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002232 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002233
2234 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002235 limited.add(getText(R.string.data_usage_tab_wifi));
2236 }
2237 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2238 limited.add(getText(R.string.data_usage_tab_ethernet));
2239 }
2240
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002241 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002242 }
2243
2244 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002245 * Inset both selector and divider {@link Drawable} on the given
2246 * {@link ListView} by the requested dimensions.
2247 */
2248 private static void insetListViewDrawables(ListView view, int insetSide) {
2249 final Drawable selector = view.getSelector();
2250 final Drawable divider = view.getDivider();
2251
2252 // fully unregister these drawables so callbacks can be maintained after
2253 // wrapping below.
2254 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2255 view.setSelector(stub);
2256 view.setDivider(stub);
2257
2258 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2259 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2260 }
2261
2262 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002263 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002264 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002265 */
2266 private static void setPreferenceTitle(View parent, int resId) {
2267 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2268 title.setText(resId);
2269 }
2270
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002271 /**
2272 * Set {@link android.R.id#summary} for a preference view inflated with
2273 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2274 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002275 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002276 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2277 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002278 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002279 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002280}