blob: c58e001c8b28b7c0432e343fcbe8175e8f7f20dc [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 Sharkey8a503642011-06-10 13:31:21 -0700107import android.widget.AdapterView;
108import android.widget.AdapterView.OnItemClickListener;
109import android.widget.AdapterView.OnItemSelectedListener;
110import android.widget.ArrayAdapter;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700111import android.widget.BaseAdapter;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700112import android.widget.Button;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700113import android.widget.CheckBox;
114import android.widget.CompoundButton;
115import android.widget.CompoundButton.OnCheckedChangeListener;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700116import android.widget.ImageView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700117import android.widget.LinearLayout;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700118import android.widget.ListView;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700119import android.widget.NumberPicker;
Jeff Sharkey2412b0f2011-07-17 20:31:40 -0700120import android.widget.ProgressBar;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700121import android.widget.Spinner;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700122import android.widget.Switch;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700123import android.widget.TabHost;
124import android.widget.TabHost.OnTabChangeListener;
125import android.widget.TabHost.TabContentFactory;
126import android.widget.TabHost.TabSpec;
127import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700128import android.widget.TextView;
129
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700130import com.android.internal.telephony.Phone;
Jeff Sharkey5d706792011-09-08 18:57:17 -0700131import com.android.settings.drawable.InsetBoundsDrawable;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700132import com.android.settings.net.ChartData;
133import com.android.settings.net.ChartDataLoader;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700134import com.android.settings.net.DataUsageMeteredSettings;
Jeff Sharkeya662e492011-06-18 21:57:06 -0700135import com.android.settings.net.NetworkPolicyEditor;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700136import com.android.settings.net.SummaryForAllUidLoader;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700137import com.android.settings.net.UidDetail;
138import com.android.settings.net.UidDetailProvider;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700139import com.android.settings.widget.ChartDataUsageView;
140import com.android.settings.widget.ChartDataUsageView.DataUsageChartListener;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700141import com.android.settings.widget.PieChartView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700142import com.google.android.collect.Lists;
143
144import java.util.ArrayList;
145import java.util.Collections;
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700146import java.util.List;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700147import java.util.Locale;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700148
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700149import libcore.util.Objects;
150
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700151/**
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700152 * Panel showing data usage history across various networks, including options
153 * to inspect based on usage cycle and control through {@link NetworkPolicy}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700154 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700155public class DataUsageSummary extends Fragment {
156 private static final String TAG = "DataUsage";
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800157 private static final boolean LOGD = false;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700158
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700159 // TODO: remove this testing code
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700160 private static final boolean TEST_ANIM = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700161 private static final boolean TEST_RADIOS = false;
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800162
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700163 private static final String TEST_RADIOS_PROP = "test.radios";
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800164 private static final String TEST_SUBSCRIBER_PROP = "test.subscriberid";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700165
Jeff Sharkey8a503642011-06-10 13:31:21 -0700166 private static final String TAB_3G = "3g";
167 private static final String TAB_4G = "4g";
168 private static final String TAB_MOBILE = "mobile";
169 private static final String TAB_WIFI = "wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700170 private static final String TAB_ETHERNET = "ethernet";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700171
Jeff Sharkey28130d92011-09-02 16:10:24 -0700172 private static final String TAG_CONFIRM_DATA_DISABLE = "confirmDataDisable";
173 private static final String TAG_CONFIRM_DATA_ROAMING = "confirmDataRoaming";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700174 private static final String TAG_CONFIRM_LIMIT = "confirmLimit";
175 private static final String TAG_CYCLE_EDITOR = "cycleEditor";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700176 private static final String TAG_WARNING_EDITOR = "warningEditor";
177 private static final String TAG_LIMIT_EDITOR = "limitEditor";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700178 private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700179 private static final String TAG_DENIED_RESTRICT = "deniedRestrict";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700180 private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700181 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700182
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700183 private static final int LOADER_CHART_DATA = 2;
184 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700185
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700186 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700187 private INetworkStatsService mStatsService;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700188 private NetworkPolicyManager mPolicyManager;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700189 private ConnectivityManager mConnService;
190
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700191 private INetworkStatsSession mStatsSession;
192
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700193 private static final String PREF_FILE = "data_usage";
194 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700195 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700196
197 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700198
Jeff Sharkey8a503642011-06-10 13:31:21 -0700199 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700200 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700201 private TabWidget mTabWidget;
202 private ListView mListView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700203 private DataUsageAdapter mAdapter;
204
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700205 /** Distance to inset content from sides, when needed. */
206 private int mInsetSide = 0;
207
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700208 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700209
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700210 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700211 private LinearLayout mNetworkSwitches;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700212 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700213 private View mDataEnabledView;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700214 private CheckBox mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700215 private View mDisableAtLimitView;
216
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700217 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700218 private Spinner mCycleSpinner;
219 private CycleAdapter mCycleAdapter;
220
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700221 private ChartDataUsageView mChart;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700222 private TextView mUsageSummary;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700223 private TextView mEmpty;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700224
225 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700226 private ImageView mAppIcon;
227 private ViewGroup mAppTitles;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700228 private PieChartView mAppPieChart;
229 private TextView mAppForeground;
230 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700231 private Button mAppSettings;
232
233 private LinearLayout mAppSwitches;
234 private CheckBox mAppRestrict;
235 private View mAppRestrictView;
236
Jeff Sharkey8a503642011-06-10 13:31:21 -0700237 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700238 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700239
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700240 private NetworkTemplate mTemplate;
241 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700242
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700243 private AppItem mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700244
245 private Intent mAppSettingsIntent;
246
Jeff Sharkeya662e492011-06-18 21:57:06 -0700247 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700248
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700249 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700250 private String mIntentTab = null;
251
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700252 private MenuItem mMenuDataRoaming;
253 private MenuItem mMenuRestrictBackground;
254
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700255 /** Flag used to ignore listeners during binding. */
256 private boolean mBinding;
257
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700258 private UidDetailProvider mUidDetailProvider;
259
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700260 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700261 public void onCreate(Bundle savedInstanceState) {
262 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700263 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700264
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700265 mNetworkService = INetworkManagementService.Stub.asInterface(
266 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700267 mStatsService = INetworkStatsService.Stub.asInterface(
268 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700269 mPolicyManager = NetworkPolicyManager.from(context);
270 mConnService = ConnectivityManager.from(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700271
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700272 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700273
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700274 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700275 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700276
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700277 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700278 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700279
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700280 // override preferences when no mobile radio
281 if (!hasReadyMobileRadio(context)) {
282 mShowWifi = hasWifiRadio(context);
283 mShowEthernet = hasEthernet(context);
284 }
285
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700286 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700287 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700288
Jeff Sharkey8a503642011-06-10 13:31:21 -0700289 @Override
290 public View onCreateView(LayoutInflater inflater, ViewGroup container,
291 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700292
Jeff Sharkey8a503642011-06-10 13:31:21 -0700293 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700294 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
295
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700296 mUidDetailProvider = new UidDetailProvider(context);
297
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700298 try {
299 mStatsSession = mStatsService.openSession();
300 } catch (RemoteException e) {
301 throw new RuntimeException(e);
302 }
303
Jeff Sharkey8a503642011-06-10 13:31:21 -0700304 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700305 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700306 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
307 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700308
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700309 // decide if we need to manually inset our content, or if we should rely
310 // on parent container for inset.
311 final boolean shouldInset = mListView.getScrollBarStyle()
312 == View.SCROLLBARS_OUTSIDE_OVERLAY;
313 if (shouldInset) {
314 mInsetSide = view.getResources().getDimensionPixelOffset(
315 com.android.internal.R.dimen.preference_fragment_padding_side);
316 } else {
317 mInsetSide = 0;
318 }
319
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700320 // adjust padding around tabwidget as needed
Jeff Sharkey5d706792011-09-08 18:57:17 -0700321 prepareCustomPreferencesList(container, view, mListView, true);
322
Jeff Sharkey8a503642011-06-10 13:31:21 -0700323 mTabHost.setup();
324 mTabHost.setOnTabChangedListener(mTabListener);
325
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700326 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700327 mHeader.setClickable(true);
328
329 mListView.addHeaderView(mHeader, null, true);
330 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700331
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700332 if (mInsetSide > 0) {
333 // inset selector and divider drawables
334 insetListViewDrawables(mListView, mInsetSide);
335 mHeader.setPadding(mInsetSide, 0, mInsetSide, 0);
336 }
337
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700338 {
339 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700340 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
341 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700342 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700343
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700344 mDataEnabled = new Switch(inflater.getContext());
345 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
346 mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener);
347 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700348
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700349 mDisableAtLimit = new CheckBox(inflater.getContext());
350 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700351 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700352 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700353 mDisableAtLimitView.setClickable(true);
354 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700355 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
356 mNetworkSwitches.addView(mDisableAtLimitView);
357 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700358
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700359 // bind cycle dropdown
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700360 mCycleView = mHeader.findViewById(R.id.cycles);
361 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700362 mCycleAdapter = new CycleAdapter(context);
363 mCycleSpinner.setAdapter(mCycleAdapter);
364 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
365
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700366 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700367 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800368 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700369
370 {
371 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700372 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700373 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
374 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700375 mAppPieChart = (PieChartView) mAppDetail.findViewById(R.id.app_pie_chart);
376 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
377 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700378 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700379
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700380 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700381 mAppSettings.setOnClickListener(mAppSettingsListener);
382
383 mAppRestrict = new CheckBox(inflater.getContext());
384 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700385 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700386 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700387 mAppRestrictView.setClickable(true);
388 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700389 mAppRestrictView.setOnClickListener(mAppRestrictListener);
390 mAppSwitches.addView(mAppRestrictView);
391 }
392
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700393 mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700394 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700395
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700396 mAdapter = new DataUsageAdapter(mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700397 mListView.setOnItemClickListener(mListListener);
398 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700399
400 return view;
401 }
402
403 @Override
404 public void onResume() {
405 super.onResume();
406
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700407 // pick default tab based on incoming intent
408 final Intent intent = getActivity().getIntent();
409 mIntentTab = computeTabFromIntent(intent);
410
Jeff Sharkey8a503642011-06-10 13:31:21 -0700411 // this kicks off chain reaction which creates tabs, binds the body to
412 // selected network, and binds chart, cycles and detail list.
413 updateTabs();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700414
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700415 // kick off background task to update stats
416 new AsyncTask<Void, Void, Void>() {
417 @Override
418 protected Void doInBackground(Void... params) {
419 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700420 // wait a few seconds before kicking off
421 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700422 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700423 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700424 } catch (RemoteException e) {
425 }
426 return null;
427 }
428
429 @Override
430 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700431 if (isAdded()) {
432 updateBody();
433 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700434 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700435 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700436 }
437
Jeff Sharkey8a503642011-06-10 13:31:21 -0700438 @Override
439 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
440 inflater.inflate(R.menu.data_usage, menu);
441 }
442
443 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700444 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700445 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700446 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700447
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700448 mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700449 mMenuDataRoaming.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700450 mMenuDataRoaming.setChecked(getDataRoaming());
451
452 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700453 mMenuRestrictBackground.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
454 mMenuRestrictBackground.setChecked(mPolicyManager.getRestrictBackground());
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700455
456 final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g);
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700457 split4g.setVisible(hasReadyMobile4gRadio(context) && !appDetailMode);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700458 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700459
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700460 final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700461 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700462 showWifi.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700463 showWifi.setChecked(mShowWifi);
464 } else {
465 showWifi.setVisible(false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700466 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700467
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700468 final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700469 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700470 showEthernet.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700471 showEthernet.setChecked(mShowEthernet);
472 } else {
473 showEthernet.setVisible(false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700474 }
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
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700551 public void onDestroy() {
Jeff Sharkey94a90952011-06-13 22:31:09 -0700552 mDataEnabledView = null;
553 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700554
555 mUidDetailProvider.clearCache();
556 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700557
558 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700559
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800560 if (this.isRemoving()) {
561 getFragmentManager()
562 .popBackStack(TAG_APP_DETAILS, FragmentManager.POP_BACK_STACK_INCLUSIVE);
563 }
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700564
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800565 super.onDestroy();
566 }
567
Jeff Sharkey8a503642011-06-10 13:31:21 -0700568 /**
Jeff Sharkey92811822012-05-04 11:47:29 -0700569 * Build and assign {@link LayoutTransition} to various containers. Should
570 * only be assigned after initial layout is complete.
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700571 */
Jeff Sharkey92811822012-05-04 11:47:29 -0700572 private void ensureLayoutTransitions() {
573 // skip when already setup
574 if (mChart.getLayoutTransition() != null) return;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700575
Jeff Sharkey92811822012-05-04 11:47:29 -0700576 mTabsContainer.setLayoutTransition(buildLayoutTransition());
577 mHeader.setLayoutTransition(buildLayoutTransition());
578 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700579
Jeff Sharkey92811822012-05-04 11:47:29 -0700580 final LayoutTransition chartTransition = buildLayoutTransition();
581 chartTransition.disableTransitionType(LayoutTransition.APPEARING);
582 chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
583 mChart.setLayoutTransition(chartTransition);
584 }
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700585
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700586 private static LayoutTransition buildLayoutTransition() {
587 final LayoutTransition transition = new LayoutTransition();
588 if (TEST_ANIM) {
589 transition.setDuration(1500);
590 }
591 transition.setAnimateParentHierarchy(false);
592 return transition;
593 }
594
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700595 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700596 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700597 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
598 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700599 */
600 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700601 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700602 mTabHost.clearAllTabs();
603
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700604 final boolean mobileSplit = isMobilePolicySplit();
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700605 if (mobileSplit && hasReadyMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700606 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
607 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700608 } else if (hasReadyMobileRadio(context)) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700609 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700610 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700611 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700612 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
613 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700614 if (mShowEthernet && hasEthernet(context)) {
615 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
616 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700617
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700618 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700619 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
620 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
621 if (mIntentTab != null) {
622 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700623 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700624 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700625 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700626 mTabHost.setCurrentTabByTag(mIntentTab);
627 }
628 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700629 } else if (noTabs) {
630 // no usable tabs, so hide body
631 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700632 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700633 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700634 }
635 }
636
Jeff Sharkey8a503642011-06-10 13:31:21 -0700637 /**
638 * Factory that provide empty {@link View} to make {@link TabHost} happy.
639 */
640 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700641 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700642 public View createTabContent(String tag) {
643 return new View(mTabHost.getContext());
644 }
645 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700646
Jeff Sharkey8a503642011-06-10 13:31:21 -0700647 /**
648 * Build {@link TabSpec} with thin indicator, and empty content.
649 */
650 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700651 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
652 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700653 }
654
655 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700656 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700657 public void onTabChanged(String tabId) {
658 // user changed tab; update body
659 updateBody();
660 }
661 };
662
663 /**
664 * Update body content based on current tab. Loads
665 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
666 * binds them to visible controls.
667 */
668 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700669 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700670 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700671
Jeff Sharkeya662e492011-06-18 21:57:06 -0700672 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700673 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700674
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700675 if (currentTab == null) {
676 Log.w(TAG, "no tab selected; hiding body");
677 mListView.setVisibility(View.GONE);
678 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700679 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700680 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700681 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700682
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700683 final boolean tabChanged = !currentTab.equals(mCurrentTab);
684 mCurrentTab = currentTab;
685
Jeff Sharkey8a503642011-06-10 13:31:21 -0700686 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
687
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700688 mDataEnabledView.setVisibility(View.VISIBLE);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700689
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700690 // TODO: remove mobile tabs when SIM isn't ready
691 final TelephonyManager tele = TelephonyManager.from(context);
692
Jeff Sharkey8a503642011-06-10 13:31:21 -0700693 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700694 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
695 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700696 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700697
698 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700699 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
700 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
701 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700702 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700703
704 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700705 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
706 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
707 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700708 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700709
710 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700711 // wifi doesn't have any controls
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700712 mDataEnabledView.setVisibility(View.GONE);
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700713 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700714 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700715
716 } else if (TAB_ETHERNET.equals(currentTab)) {
717 // ethernet doesn't have any controls
718 mDataEnabledView.setVisibility(View.GONE);
719 mDisableAtLimitView.setVisibility(View.GONE);
720 mTemplate = buildTemplateEthernet();
721
722 } else {
723 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700724 }
725
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700726 // kick off loader for network history
727 // TODO: consider chaining two loaders together instead of reloading
728 // network history when showing app detail.
729 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700730 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700731
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700732 // detail mode can change visible menus, invalidate
733 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700734
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700735 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700736 }
737
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700738 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700739 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700740 }
741
742 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700743 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700744 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700745 */
746 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700747 final Context context = getActivity();
748 final PackageManager pm = context.getPackageManager();
749 final LayoutInflater inflater = getActivity().getLayoutInflater();
750
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700751 if (isAppDetailMode()) {
752 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700753 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700754 } else {
755 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700756 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700757
758 // hide detail stats when not in detail mode
759 mChart.bindDetailNetworkStats(null);
760 return;
761 }
762
763 // remove warning/limit sweeps while in detail mode
764 mChart.bindNetworkPolicy(null);
765
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700766 // show icon and all labels appearing under this app
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700767 final int appId = mCurrentApp.appId;
768 final UidDetail detail = mUidDetailProvider.getUidDetail(appId, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700769 mAppIcon.setImageDrawable(detail.icon);
770
771 mAppTitles.removeAllViews();
772 if (detail.detailLabels != null) {
773 for (CharSequence label : detail.detailLabels) {
774 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label));
775 }
776 } else {
777 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label));
778 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700779
780 // enable settings button when package provides it
781 // TODO: target torwards entire UID instead of just first package
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700782 final String[] packageNames = pm.getPackagesForUid(appId);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700783 if (packageNames != null && packageNames.length > 0) {
784 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
785 mAppSettingsIntent.setPackage(packageNames[0]);
786 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
787
788 final boolean matchFound = pm.resolveActivity(mAppSettingsIntent, 0) != null;
789 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -0700790 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700791
792 } else {
793 mAppSettingsIntent = null;
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700794 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700795 }
796
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700797 updateDetailData();
798
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700799 if (UserId.isApp(appId) && !mPolicyManager.getRestrictBackground()
800 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700801 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800802 setPreferenceSummary(mAppRestrictView,
803 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700804
805 mAppRestrictView.setVisibility(View.VISIBLE);
806 mAppRestrict.setChecked(getAppRestrictBackground());
807
808 } else {
809 mAppRestrictView.setVisibility(View.GONE);
810 }
811 }
812
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700813 private void setPolicyWarningBytes(long warningBytes) {
814 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700815 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700816 updatePolicy(false);
817 }
818
819 private void setPolicyLimitBytes(long limitBytes) {
820 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700821 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700822 updatePolicy(false);
823 }
824
Jeff Sharkey28130d92011-09-02 16:10:24 -0700825 /**
826 * Local cache of value, used to work around delay when
827 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
828 */
829 private Boolean mMobileDataEnabled;
830
831 private boolean isMobileDataEnabled() {
832 if (mMobileDataEnabled != null) {
833 // TODO: deprecate and remove this once enabled flag is on policy
834 return mMobileDataEnabled;
835 } else {
836 return mConnService.getMobileDataEnabled();
837 }
838 }
839
840 private void setMobileDataEnabled(boolean enabled) {
841 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
842 mConnService.setMobileDataEnabled(enabled);
843 mMobileDataEnabled = enabled;
844 updatePolicy(false);
845 }
846
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700847 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
848 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked();
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700849 }
850
851 private boolean isBandwidthControlEnabled() {
852 try {
853 return mNetworkService.isBandwidthControlEnabled();
854 } catch (RemoteException e) {
855 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
856 return false;
857 }
858 }
859
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700860 private boolean getDataRoaming() {
861 final ContentResolver resolver = getActivity().getContentResolver();
862 return Settings.Secure.getInt(resolver, Settings.Secure.DATA_ROAMING, 0) != 0;
863 }
864
865 private void setDataRoaming(boolean enabled) {
866 // TODO: teach telephony DataConnectionTracker to watch and apply
867 // updates when changed.
868 final ContentResolver resolver = getActivity().getContentResolver();
869 Settings.Secure.putInt(resolver, Settings.Secure.DATA_ROAMING, enabled ? 1 : 0);
870 mMenuDataRoaming.setChecked(enabled);
871 }
872
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700873 public void setRestrictBackground(boolean restrictBackground) {
874 mPolicyManager.setRestrictBackground(restrictBackground);
875 mMenuRestrictBackground.setChecked(restrictBackground);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700876 }
877
878 private boolean getAppRestrictBackground() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700879 final int appId = mCurrentApp.appId;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700880 final int uidPolicy = mPolicyManager.getAppPolicy(appId);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700881 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
882 }
883
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700884 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700885 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700886 final int appId = mCurrentApp.appId;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700887 mPolicyManager.setAppPolicy(appId,
888 restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700889 mAppRestrict.setChecked(restrictBackground);
890 }
891
Jeff Sharkey8a503642011-06-10 13:31:21 -0700892 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700893 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
894 * current {@link #mTemplate}.
895 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700896 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700897 if (isAppDetailMode()) {
898 mNetworkSwitches.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700899 } else {
900 mNetworkSwitches.setVisibility(View.VISIBLE);
901 }
902
Jeff Sharkey28130d92011-09-02 16:10:24 -0700903 // TODO: move enabled state directly into policy
904 if (TAB_MOBILE.equals(mCurrentTab)) {
905 mBinding = true;
906 mDataEnabled.setChecked(isMobileDataEnabled());
907 mBinding = false;
908 }
909
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700910 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
911 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700912 mDisableAtLimitView.setVisibility(View.VISIBLE);
913 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700914 if (!isAppDetailMode()) {
915 mChart.bindNetworkPolicy(policy);
916 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700917
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700918 } else {
919 // controls are disabled; don't bind warning/limit sweeps
920 mDisableAtLimitView.setVisibility(View.GONE);
921 mChart.bindNetworkPolicy(null);
922 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700923
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700924 if (refreshCycle) {
925 // generate cycle list based on policy and available history
926 updateCycleList(policy);
927 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700928 }
929
930 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700931 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
932 * and available {@link NetworkStatsHistory} data. Always selects the newest
933 * item, updating the inspection range on {@link #mChart}.
934 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700935 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700936 // stash away currently selected cycle to try restoring below
937 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700938 mCycleAdapter.clear();
939
940 final Context context = mCycleSpinner.getContext();
941
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700942 long historyStart = Long.MAX_VALUE;
943 long historyEnd = Long.MIN_VALUE;
944 if (mChartData != null) {
945 historyStart = mChartData.network.getStart();
946 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700947 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700948
Jeff Sharkey461842a2011-09-25 18:22:48 -0700949 final long now = System.currentTimeMillis();
950 if (historyStart == Long.MAX_VALUE) historyStart = now;
951 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700952
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700953 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700954 if (policy != null) {
955 // find the next cycle boundary
956 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700957
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700958 // walk backwards, generating all valid cycle ranges
959 while (cycleEnd > historyStart) {
960 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
961 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
962 + historyStart);
963 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
964 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700965 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700966 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700967
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700968 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700969 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700970 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700971
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700972 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700973 // no policy defined cycles; show entry for each four-week period
974 long cycleEnd = historyEnd;
975 while (cycleEnd > historyStart) {
976 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
977 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
978 cycleEnd = cycleStart;
979 }
980
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700981 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700982 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700983
984 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700985 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700986 final int position = mCycleAdapter.findNearestPosition(previousItem);
987 mCycleSpinner.setSelection(position);
988
989 // only force-update cycle when changed; skipping preserves any
990 // user-defined inspection region.
991 final CycleItem selectedItem = mCycleAdapter.getItem(position);
992 if (!Objects.equal(selectedItem, previousItem)) {
993 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
994 } else {
995 // but still kick off loader for detailed list
996 updateDetailData();
997 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700998 } else {
999 updateDetailData();
1000 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001001 }
1002
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001003 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001004 @Override
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001005 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
1006 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001007
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001008 final boolean dataEnabled = isChecked;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001009 final String currentTab = mCurrentTab;
1010 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001011 if (dataEnabled) {
1012 setMobileDataEnabled(true);
1013 } else {
1014 // disabling data; show confirmation dialog which eventually
1015 // calls setMobileDataEnabled() once user confirms.
1016 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1017 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001018 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001019
Jeff Sharkey28130d92011-09-02 16:10:24 -07001020 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001021 }
1022 };
1023
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001024 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001025 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001026 public void onClick(View v) {
1027 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001028 if (disableAtLimit) {
1029 // enabling limit; show confirmation dialog which eventually
1030 // calls setPolicyLimitBytes() once user confirms.
1031 ConfirmLimitFragment.show(DataUsageSummary.this);
1032 } else {
1033 setPolicyLimitBytes(LIMIT_DISABLED);
1034 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001035 }
1036 };
1037
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001038 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001039 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001040 public void onClick(View v) {
1041 final boolean restrictBackground = !mAppRestrict.isChecked();
1042
1043 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001044 // enabling restriction; show confirmation dialog which
1045 // eventually calls setRestrictBackground() once user
1046 // confirms.
1047 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001048 } else {
1049 setAppRestrictBackground(false);
1050 }
1051 }
1052 };
1053
1054 private OnClickListener mAppSettingsListener = new OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001055 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001056 public void onClick(View v) {
Jeff Sharkeyd92e0412012-04-24 11:35:43 -07001057 if (!isAdded()) return;
1058
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001059 // TODO: target torwards entire UID instead of just first package
1060 startActivity(mAppSettingsIntent);
1061 }
1062 };
1063
Jeff Sharkey8a503642011-06-10 13:31:21 -07001064 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001065 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001066 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001067 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001068 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001069
1070 // TODO: sigh, remove this hack once we understand 6450986
1071 if (mUidDetailProvider == null || app == null) return;
1072
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001073 final UidDetail detail = mUidDetailProvider.getUidDetail(app.appId, true);
1074 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001075 }
1076 };
1077
1078 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001079 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001080 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1081 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1082 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001083 // show cycle editor; will eventually call setPolicyCycleDay()
1084 // when user finishes editing.
1085 CycleEditorFragment.show(DataUsageSummary.this);
1086
1087 // reset spinner to something other than "change cycle..."
1088 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001089
1090 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001091 if (LOGD) {
1092 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1093 + cycle.end + "]");
1094 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001095
1096 // update chart to show selected cycle, and update detail data
1097 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001098 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001099
1100 updateDetailData();
1101 }
1102 }
1103
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001104 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001105 public void onNothingSelected(AdapterView<?> parent) {
1106 // ignored
1107 }
1108 };
1109
1110 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001111 * Update details based on {@link #mChart} inspection range depending on
1112 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1113 * of applications data usage, and when {@link #isAppDetailMode()} update
1114 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001115 */
1116 private void updateDetailData() {
1117 if (LOGD) Log.d(TAG, "updateDetailData()");
1118
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001119 final long start = mChart.getInspectStart();
1120 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001121 final long now = System.currentTimeMillis();
1122
1123 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001124
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001125 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001126 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001127 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001128 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001129 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001130 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001131 final long foregroundBytes = entry.rxBytes + entry.txBytes;
1132
1133 mAppPieChart.setOriginAngle(175);
1134
1135 mAppPieChart.removeAllSlices();
1136 mAppPieChart.addSlice(foregroundBytes, Color.parseColor("#d88d3a"));
1137 mAppPieChart.addSlice(defaultBytes, Color.parseColor("#666666"));
1138
1139 mAppPieChart.generatePath();
1140
1141 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1142 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1143
1144 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001145 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001146
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001147 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001148
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001149 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001150 if (mChartData != null) {
1151 entry = mChartData.network.getValues(start, end, now, null);
1152 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001153
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001154 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001155 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001156 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001157 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001158
1159 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1160 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001161 final String rangePhrase = formatDateRange(context, start, end);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001162
Jeff Sharkeye557c332012-04-13 16:04:07 -07001163 final int summaryRes;
1164 if (TAB_MOBILE.equals(mCurrentTab) || TAB_3G.equals(mCurrentApp)
1165 || TAB_4G.equals(mCurrentApp)) {
1166 summaryRes = R.string.data_usage_total_during_range_mobile;
1167 } else {
1168 summaryRes = R.string.data_usage_total_during_range;
1169 }
1170
1171 mUsageSummary.setText(getString(summaryRes, totalPhrase, rangePhrase));
Jeff Sharkey92811822012-05-04 11:47:29 -07001172
1173 // initial layout is finished above, ensure we have transitions
1174 ensureLayoutTransitions();
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 Sharkeyb67c4a82012-05-24 11:01:29 -07002157 public 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 Sharkeyb67c4a82012-05-24 11:01:29 -07002163 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2164
2165 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002166 if (mStatsSession != null) {
2167 try {
2168 ethernetBytes = mStatsSession.getSummaryForNetwork(
2169 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2170 .getTotalBytes();
2171 } catch (RemoteException e) {
2172 throw new RuntimeException(e);
2173 }
2174 } else {
2175 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002176 }
2177
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002178 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002179 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002180 }
2181
2182 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002183 * Inflate a {@link Preference} style layout, adding the given {@link View}
2184 * widget into {@link android.R.id#widget_frame}.
2185 */
2186 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2187 final View view = inflater.inflate(R.layout.preference, root, false);
2188 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2189 android.R.id.widget_frame);
2190 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2191 return view;
2192 }
2193
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002194 private static View inflateAppTitle(
2195 LayoutInflater inflater, ViewGroup root, CharSequence label) {
2196 final TextView view = (TextView) inflater.inflate(
2197 R.layout.data_usage_app_title, root, false);
2198 view.setText(label);
2199 return view;
2200 }
2201
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002202 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002203 * Test if any networks are currently limited.
2204 */
2205 private boolean hasLimitedNetworks() {
2206 return !buildLimitedNetworksList().isEmpty();
2207 }
2208
2209 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002210 * Build string describing currently limited networks, which defines when
2211 * background data is restricted.
2212 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002213 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002214 private CharSequence buildLimitedNetworksString() {
2215 final List<CharSequence> limited = buildLimitedNetworksList();
2216
2217 // handle case where no networks limited
2218 if (limited.isEmpty()) {
2219 limited.add(getText(R.string.data_usage_list_none));
2220 }
2221
2222 return TextUtils.join(limited);
2223 }
2224
2225 /**
2226 * Build list of currently limited networks, which defines when background
2227 * data is restricted.
2228 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002229 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002230 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002231 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002232
2233 // build combined list of all limited networks
2234 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002235
2236 final TelephonyManager tele = TelephonyManager.from(context);
2237 if (tele.getSimState() == SIM_STATE_READY) {
2238 final String subscriberId = getActiveSubscriberId(context);
2239 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2240 limited.add(getText(R.string.data_usage_list_mobile));
2241 }
2242 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2243 limited.add(getText(R.string.data_usage_tab_3g));
2244 }
2245 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2246 limited.add(getText(R.string.data_usage_tab_4g));
2247 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002248 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002249
2250 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002251 limited.add(getText(R.string.data_usage_tab_wifi));
2252 }
2253 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2254 limited.add(getText(R.string.data_usage_tab_ethernet));
2255 }
2256
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002257 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002258 }
2259
2260 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002261 * Inset both selector and divider {@link Drawable} on the given
2262 * {@link ListView} by the requested dimensions.
2263 */
2264 private static void insetListViewDrawables(ListView view, int insetSide) {
2265 final Drawable selector = view.getSelector();
2266 final Drawable divider = view.getDivider();
2267
2268 // fully unregister these drawables so callbacks can be maintained after
2269 // wrapping below.
2270 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2271 view.setSelector(stub);
2272 view.setDivider(stub);
2273
2274 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2275 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2276 }
2277
2278 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002279 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002280 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002281 */
2282 private static void setPreferenceTitle(View parent, int resId) {
2283 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2284 title.setText(resId);
2285 }
2286
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002287 /**
2288 * Set {@link android.R.id#summary} for a preference view inflated with
2289 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2290 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002291 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002292 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2293 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002294 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002295 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002296}