blob: ae9c1cba5ff8b7ffe03804b6d8e5e2c7211b9383 [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;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070079import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070080import android.os.Bundle;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070081import android.os.INetworkManagementService;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070082import android.os.Parcel;
83import android.os.Parcelable;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070084import android.os.RemoteException;
85import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070086import android.os.SystemProperties;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070087import android.os.UserId;
Jeff Sharkey8a503642011-06-10 13:31:21 -070088import android.preference.Preference;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070089import android.preference.PreferenceActivity;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070090import android.provider.Settings;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070091import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070092import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070093import android.text.format.DateUtils;
94import android.text.format.Formatter;
Jeff Sharkeye5223a02012-03-09 17:11:14 -080095import android.text.format.Time;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070096import android.util.Log;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070097import android.util.SparseArray;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070098import android.util.SparseBooleanArray;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070099import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700100import android.view.Menu;
101import android.view.MenuInflater;
102import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700103import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700104import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700105import android.view.ViewGroup;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700106import android.view.ViewTreeObserver.OnGlobalLayoutListener;
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 Sharkey05cc0cc2011-06-12 23:11:24 -0700280 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700281 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700282
Jeff Sharkey8a503642011-06-10 13:31:21 -0700283 @Override
284 public View onCreateView(LayoutInflater inflater, ViewGroup container,
285 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700286
Jeff Sharkey8a503642011-06-10 13:31:21 -0700287 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700288 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
289
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700290 mUidDetailProvider = new UidDetailProvider(context);
291
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700292 try {
293 mStatsSession = mStatsService.openSession();
294 } catch (RemoteException e) {
295 throw new RuntimeException(e);
296 }
297
Jeff Sharkey8a503642011-06-10 13:31:21 -0700298 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700299 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700300 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
301 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700302
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700303 // decide if we need to manually inset our content, or if we should rely
304 // on parent container for inset.
305 final boolean shouldInset = mListView.getScrollBarStyle()
306 == View.SCROLLBARS_OUTSIDE_OVERLAY;
307 if (shouldInset) {
308 mInsetSide = view.getResources().getDimensionPixelOffset(
309 com.android.internal.R.dimen.preference_fragment_padding_side);
310 } else {
311 mInsetSide = 0;
312 }
313
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700314 // adjust padding around tabwidget as needed
Jeff Sharkey5d706792011-09-08 18:57:17 -0700315 prepareCustomPreferencesList(container, view, mListView, true);
316
Jeff Sharkey8a503642011-06-10 13:31:21 -0700317 mTabHost.setup();
318 mTabHost.setOnTabChangedListener(mTabListener);
319
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700320 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700321 mHeader.setClickable(true);
322
323 mListView.addHeaderView(mHeader, null, true);
324 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700325
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700326 if (mInsetSide > 0) {
327 // inset selector and divider drawables
328 insetListViewDrawables(mListView, mInsetSide);
329 mHeader.setPadding(mInsetSide, 0, mInsetSide, 0);
330 }
331
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700332 {
333 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700334 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
335 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700336 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700337
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700338 mDataEnabled = new Switch(inflater.getContext());
339 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
340 mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener);
341 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700342
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700343 mDisableAtLimit = new CheckBox(inflater.getContext());
344 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700345 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700346 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700347 mDisableAtLimitView.setClickable(true);
348 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700349 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
350 mNetworkSwitches.addView(mDisableAtLimitView);
351 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700352
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700353 // bind cycle dropdown
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700354 mCycleView = mHeader.findViewById(R.id.cycles);
355 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700356 mCycleAdapter = new CycleAdapter(context);
357 mCycleSpinner.setAdapter(mCycleAdapter);
358 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
359
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700360 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700361 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800362 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700363
364 {
365 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700366 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700367 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
368 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700369 mAppPieChart = (PieChartView) mAppDetail.findViewById(R.id.app_pie_chart);
370 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
371 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700372 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700373
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700374 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700375 mAppSettings.setOnClickListener(mAppSettingsListener);
376
377 mAppRestrict = new CheckBox(inflater.getContext());
378 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700379 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700380 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700381 mAppRestrictView.setClickable(true);
382 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700383 mAppRestrictView.setOnClickListener(mAppRestrictListener);
384 mAppSwitches.addView(mAppRestrictView);
385 }
386
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700387 mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700388 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700389
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700390 // only assign layout transitions once first layout is finished
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700391 mListView.getViewTreeObserver().addOnGlobalLayoutListener(mFirstLayoutListener);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700392
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700393 mAdapter = new DataUsageAdapter(mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700394 mListView.setOnItemClickListener(mListListener);
395 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700396
397 return view;
398 }
399
400 @Override
401 public void onResume() {
402 super.onResume();
403
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700404 // pick default tab based on incoming intent
405 final Intent intent = getActivity().getIntent();
406 mIntentTab = computeTabFromIntent(intent);
407
Jeff Sharkey8a503642011-06-10 13:31:21 -0700408 // this kicks off chain reaction which creates tabs, binds the body to
409 // selected network, and binds chart, cycles and detail list.
410 updateTabs();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700411
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700412 // kick off background task to update stats
413 new AsyncTask<Void, Void, Void>() {
414 @Override
415 protected Void doInBackground(Void... params) {
416 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700417 // wait a few seconds before kicking off
418 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700419 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700420 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700421 } catch (RemoteException e) {
422 }
423 return null;
424 }
425
426 @Override
427 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700428 if (isAdded()) {
429 updateBody();
430 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700431 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700432 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700433 }
434
Jeff Sharkey8a503642011-06-10 13:31:21 -0700435 @Override
436 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
437 inflater.inflate(R.menu.data_usage, menu);
438 }
439
440 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700441 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700442 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700443 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700444
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700445 mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700446 mMenuDataRoaming.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700447 mMenuDataRoaming.setChecked(getDataRoaming());
448
449 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700450 mMenuRestrictBackground.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
451 mMenuRestrictBackground.setChecked(mPolicyManager.getRestrictBackground());
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700452
453 final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g);
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700454 split4g.setVisible(hasReadyMobile4gRadio(context) && !appDetailMode);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700455 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700456
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700457 final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700458 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700459 showWifi.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700460 showWifi.setChecked(mShowWifi);
461 } else {
462 showWifi.setVisible(false);
463 mShowWifi = true;
464 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700465
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700466 final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700467 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700468 showEthernet.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700469 showEthernet.setChecked(mShowEthernet);
470 } else {
471 showEthernet.setVisible(false);
472 mShowEthernet = true;
473 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700474
475 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
476 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
477 metered.setVisible(!appDetailMode);
478 } else {
479 metered.setVisible(false);
480 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700481 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700482
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700483 @Override
484 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700485 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700486 case R.id.data_usage_menu_roaming: {
487 final boolean dataRoaming = !item.isChecked();
488 if (dataRoaming) {
489 ConfirmDataRoamingFragment.show(this);
490 } else {
491 // no confirmation to disable roaming
492 setDataRoaming(false);
493 }
494 return true;
495 }
496 case R.id.data_usage_menu_restrict_background: {
497 final boolean restrictBackground = !item.isChecked();
498 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800499 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700500 } else {
501 // no confirmation to drop restriction
502 setRestrictBackground(false);
503 }
504 return true;
505 }
506 case R.id.data_usage_menu_split_4g: {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700507 final boolean mobileSplit = !item.isChecked();
Jeff Sharkeya662e492011-06-18 21:57:06 -0700508 setMobilePolicySplit(mobileSplit);
509 item.setChecked(isMobilePolicySplit());
Jeff Sharkey8a503642011-06-10 13:31:21 -0700510 updateTabs();
511 return true;
512 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700513 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700514 mShowWifi = !item.isChecked();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700515 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700516 item.setChecked(mShowWifi);
517 updateTabs();
518 return true;
519 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700520 case R.id.data_usage_menu_show_ethernet: {
521 mShowEthernet = !item.isChecked();
522 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
523 item.setChecked(mShowEthernet);
524 updateTabs();
525 return true;
526 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700527 case R.id.data_usage_menu_metered: {
528 final PreferenceActivity activity = (PreferenceActivity) getActivity();
529 activity.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
530 R.string.data_usage_metered_title, null, this, 0);
531 return true;
532 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700533 }
534 return false;
535 }
536
Jeff Sharkey94a90952011-06-13 22:31:09 -0700537 @Override
538 public void onDestroyView() {
539 super.onDestroyView();
540
541 mDataEnabledView = null;
542 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700543
544 mUidDetailProvider.clearCache();
545 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700546
547 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700548 }
549
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800550 @Override
551 public void onDestroy() {
552 if (this.isRemoving()) {
553 getFragmentManager()
554 .popBackStack(TAG_APP_DETAILS, FragmentManager.POP_BACK_STACK_INCLUSIVE);
555 }
556 super.onDestroy();
557 }
558
Jeff Sharkey8a503642011-06-10 13:31:21 -0700559 /**
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700560 * Listener to setup {@link LayoutTransition} after first layout pass.
561 */
562 private OnGlobalLayoutListener mFirstLayoutListener = new OnGlobalLayoutListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700563 @Override
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700564 public void onGlobalLayout() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700565 mListView.getViewTreeObserver().removeOnGlobalLayoutListener(mFirstLayoutListener);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700566
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700567 mTabsContainer.setLayoutTransition(buildLayoutTransition());
568 mHeader.setLayoutTransition(buildLayoutTransition());
569 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700570
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700571 final LayoutTransition chartTransition = buildLayoutTransition();
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700572 chartTransition.setStartDelay(LayoutTransition.APPEARING, 0);
573 chartTransition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700574 chartTransition.setAnimator(LayoutTransition.APPEARING, null);
Jeff Sharkey28130d92011-09-02 16:10:24 -0700575 chartTransition.setAnimator(LayoutTransition.DISAPPEARING, null);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700576 mChart.setLayoutTransition(chartTransition);
577 }
578 };
579
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700580 private static LayoutTransition buildLayoutTransition() {
581 final LayoutTransition transition = new LayoutTransition();
582 if (TEST_ANIM) {
583 transition.setDuration(1500);
584 }
585 transition.setAnimateParentHierarchy(false);
586 return transition;
587 }
588
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700589 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700590 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700591 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
592 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700593 */
594 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700595 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700596 mTabHost.clearAllTabs();
597
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700598 final boolean mobileSplit = isMobilePolicySplit();
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700599 if (mobileSplit && hasReadyMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700600 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
601 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700602 } else if (hasReadyMobileRadio(context)) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700603 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700604 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700605 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700606 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
607 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700608 if (mShowEthernet && hasEthernet(context)) {
609 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
610 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700611
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700612 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700613 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
614 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
615 if (mIntentTab != null) {
616 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700617 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700618 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700619 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700620 mTabHost.setCurrentTabByTag(mIntentTab);
621 }
622 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700623 } else if (noTabs) {
624 // no usable tabs, so hide body
625 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700626 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700627 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700628 }
629 }
630
Jeff Sharkey8a503642011-06-10 13:31:21 -0700631 /**
632 * Factory that provide empty {@link View} to make {@link TabHost} happy.
633 */
634 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700635 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700636 public View createTabContent(String tag) {
637 return new View(mTabHost.getContext());
638 }
639 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700640
Jeff Sharkey8a503642011-06-10 13:31:21 -0700641 /**
642 * Build {@link TabSpec} with thin indicator, and empty content.
643 */
644 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700645 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
646 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700647 }
648
649 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700650 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700651 public void onTabChanged(String tabId) {
652 // user changed tab; update body
653 updateBody();
654 }
655 };
656
657 /**
658 * Update body content based on current tab. Loads
659 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
660 * binds them to visible controls.
661 */
662 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700663 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700664 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700665
Jeff Sharkeya662e492011-06-18 21:57:06 -0700666 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700667 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700668
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700669 if (currentTab == null) {
670 Log.w(TAG, "no tab selected; hiding body");
671 mListView.setVisibility(View.GONE);
672 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700673 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700674 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700675 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700676
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700677 final boolean tabChanged = !currentTab.equals(mCurrentTab);
678 mCurrentTab = currentTab;
679
Jeff Sharkey8a503642011-06-10 13:31:21 -0700680 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
681
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700682 mDataEnabledView.setVisibility(View.VISIBLE);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700683
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700684 // TODO: remove mobile tabs when SIM isn't ready
685 final TelephonyManager tele = TelephonyManager.from(context);
686
Jeff Sharkey8a503642011-06-10 13:31:21 -0700687 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700688 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
689 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700690 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700691
692 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700693 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
694 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
695 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700696 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700697
698 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700699 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
700 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
701 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700702 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700703
704 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700705 // wifi doesn't have any controls
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700706 mDataEnabledView.setVisibility(View.GONE);
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700707 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700708 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700709
710 } else if (TAB_ETHERNET.equals(currentTab)) {
711 // ethernet doesn't have any controls
712 mDataEnabledView.setVisibility(View.GONE);
713 mDisableAtLimitView.setVisibility(View.GONE);
714 mTemplate = buildTemplateEthernet();
715
716 } else {
717 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700718 }
719
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700720 // kick off loader for network history
721 // TODO: consider chaining two loaders together instead of reloading
722 // network history when showing app detail.
723 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700724 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700725
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700726 // detail mode can change visible menus, invalidate
727 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700728
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700729 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700730 }
731
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700732 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700733 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700734 }
735
736 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700737 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700738 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700739 */
740 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700741 final Context context = getActivity();
742 final PackageManager pm = context.getPackageManager();
743 final LayoutInflater inflater = getActivity().getLayoutInflater();
744
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700745 if (isAppDetailMode()) {
746 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700747 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700748 } else {
749 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700750 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700751
752 // hide detail stats when not in detail mode
753 mChart.bindDetailNetworkStats(null);
754 return;
755 }
756
757 // remove warning/limit sweeps while in detail mode
758 mChart.bindNetworkPolicy(null);
759
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700760 // show icon and all labels appearing under this app
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700761 final int appId = mCurrentApp.appId;
762 final UidDetail detail = mUidDetailProvider.getUidDetail(appId, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700763 mAppIcon.setImageDrawable(detail.icon);
764
765 mAppTitles.removeAllViews();
766 if (detail.detailLabels != null) {
767 for (CharSequence label : detail.detailLabels) {
768 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label));
769 }
770 } else {
771 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label));
772 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700773
774 // enable settings button when package provides it
775 // TODO: target torwards entire UID instead of just first package
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700776 final String[] packageNames = pm.getPackagesForUid(appId);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700777 if (packageNames != null && packageNames.length > 0) {
778 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
779 mAppSettingsIntent.setPackage(packageNames[0]);
780 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
781
782 final boolean matchFound = pm.resolveActivity(mAppSettingsIntent, 0) != null;
783 mAppSettings.setEnabled(matchFound);
784
785 } else {
786 mAppSettingsIntent = null;
787 mAppSettings.setEnabled(false);
788 }
789
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700790 updateDetailData();
791
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700792 if (UserId.isApp(appId) && !mPolicyManager.getRestrictBackground()
793 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700794 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800795 setPreferenceSummary(mAppRestrictView,
796 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700797
798 mAppRestrictView.setVisibility(View.VISIBLE);
799 mAppRestrict.setChecked(getAppRestrictBackground());
800
801 } else {
802 mAppRestrictView.setVisibility(View.GONE);
803 }
804 }
805
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700806 private void setPolicyWarningBytes(long warningBytes) {
807 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700808 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700809 updatePolicy(false);
810 }
811
812 private void setPolicyLimitBytes(long limitBytes) {
813 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700814 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700815 updatePolicy(false);
816 }
817
Jeff Sharkey28130d92011-09-02 16:10:24 -0700818 /**
819 * Local cache of value, used to work around delay when
820 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
821 */
822 private Boolean mMobileDataEnabled;
823
824 private boolean isMobileDataEnabled() {
825 if (mMobileDataEnabled != null) {
826 // TODO: deprecate and remove this once enabled flag is on policy
827 return mMobileDataEnabled;
828 } else {
829 return mConnService.getMobileDataEnabled();
830 }
831 }
832
833 private void setMobileDataEnabled(boolean enabled) {
834 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
835 mConnService.setMobileDataEnabled(enabled);
836 mMobileDataEnabled = enabled;
837 updatePolicy(false);
838 }
839
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700840 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
841 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked();
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700842 }
843
844 private boolean isBandwidthControlEnabled() {
845 try {
846 return mNetworkService.isBandwidthControlEnabled();
847 } catch (RemoteException e) {
848 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
849 return false;
850 }
851 }
852
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700853 private boolean getDataRoaming() {
854 final ContentResolver resolver = getActivity().getContentResolver();
855 return Settings.Secure.getInt(resolver, Settings.Secure.DATA_ROAMING, 0) != 0;
856 }
857
858 private void setDataRoaming(boolean enabled) {
859 // TODO: teach telephony DataConnectionTracker to watch and apply
860 // updates when changed.
861 final ContentResolver resolver = getActivity().getContentResolver();
862 Settings.Secure.putInt(resolver, Settings.Secure.DATA_ROAMING, enabled ? 1 : 0);
863 mMenuDataRoaming.setChecked(enabled);
864 }
865
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700866 public void setRestrictBackground(boolean restrictBackground) {
867 mPolicyManager.setRestrictBackground(restrictBackground);
868 mMenuRestrictBackground.setChecked(restrictBackground);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700869 }
870
871 private boolean getAppRestrictBackground() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700872 final int appId = mCurrentApp.appId;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700873 final int uidPolicy = mPolicyManager.getAppPolicy(appId);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700874 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
875 }
876
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700877 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700878 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700879 final int appId = mCurrentApp.appId;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700880 mPolicyManager.setAppPolicy(appId,
881 restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700882 mAppRestrict.setChecked(restrictBackground);
883 }
884
Jeff Sharkey8a503642011-06-10 13:31:21 -0700885 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700886 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
887 * current {@link #mTemplate}.
888 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700889 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700890 if (isAppDetailMode()) {
891 mNetworkSwitches.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700892 } else {
893 mNetworkSwitches.setVisibility(View.VISIBLE);
894 }
895
Jeff Sharkey28130d92011-09-02 16:10:24 -0700896 // TODO: move enabled state directly into policy
897 if (TAB_MOBILE.equals(mCurrentTab)) {
898 mBinding = true;
899 mDataEnabled.setChecked(isMobileDataEnabled());
900 mBinding = false;
901 }
902
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700903 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
904 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700905 mDisableAtLimitView.setVisibility(View.VISIBLE);
906 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700907 if (!isAppDetailMode()) {
908 mChart.bindNetworkPolicy(policy);
909 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700910
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700911 } else {
912 // controls are disabled; don't bind warning/limit sweeps
913 mDisableAtLimitView.setVisibility(View.GONE);
914 mChart.bindNetworkPolicy(null);
915 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700916
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700917 if (refreshCycle) {
918 // generate cycle list based on policy and available history
919 updateCycleList(policy);
920 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700921 }
922
923 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700924 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
925 * and available {@link NetworkStatsHistory} data. Always selects the newest
926 * item, updating the inspection range on {@link #mChart}.
927 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700928 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700929 // stash away currently selected cycle to try restoring below
930 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700931 mCycleAdapter.clear();
932
933 final Context context = mCycleSpinner.getContext();
934
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700935 long historyStart = Long.MAX_VALUE;
936 long historyEnd = Long.MIN_VALUE;
937 if (mChartData != null) {
938 historyStart = mChartData.network.getStart();
939 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700940 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700941
Jeff Sharkey461842a2011-09-25 18:22:48 -0700942 final long now = System.currentTimeMillis();
943 if (historyStart == Long.MAX_VALUE) historyStart = now;
944 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700945
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700946 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700947 if (policy != null) {
948 // find the next cycle boundary
949 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700950
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700951 // walk backwards, generating all valid cycle ranges
952 while (cycleEnd > historyStart) {
953 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
954 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
955 + historyStart);
956 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
957 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700958 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700959 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700960
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700961 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700962 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700963 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700964
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700965 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700966 // no policy defined cycles; show entry for each four-week period
967 long cycleEnd = historyEnd;
968 while (cycleEnd > historyStart) {
969 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
970 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
971 cycleEnd = cycleStart;
972 }
973
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700974 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700975 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700976
977 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700978 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700979 final int position = mCycleAdapter.findNearestPosition(previousItem);
980 mCycleSpinner.setSelection(position);
981
982 // only force-update cycle when changed; skipping preserves any
983 // user-defined inspection region.
984 final CycleItem selectedItem = mCycleAdapter.getItem(position);
985 if (!Objects.equal(selectedItem, previousItem)) {
986 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
987 } else {
988 // but still kick off loader for detailed list
989 updateDetailData();
990 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700991 } else {
992 updateDetailData();
993 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700994 }
995
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700996 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700997 @Override
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700998 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
999 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001000
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001001 final boolean dataEnabled = isChecked;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001002 final String currentTab = mCurrentTab;
1003 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001004 if (dataEnabled) {
1005 setMobileDataEnabled(true);
1006 } else {
1007 // disabling data; show confirmation dialog which eventually
1008 // calls setMobileDataEnabled() once user confirms.
1009 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1010 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001011 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001012
Jeff Sharkey28130d92011-09-02 16:10:24 -07001013 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001014 }
1015 };
1016
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001017 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001018 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001019 public void onClick(View v) {
1020 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001021 if (disableAtLimit) {
1022 // enabling limit; show confirmation dialog which eventually
1023 // calls setPolicyLimitBytes() once user confirms.
1024 ConfirmLimitFragment.show(DataUsageSummary.this);
1025 } else {
1026 setPolicyLimitBytes(LIMIT_DISABLED);
1027 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001028 }
1029 };
1030
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001031 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001032 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001033 public void onClick(View v) {
1034 final boolean restrictBackground = !mAppRestrict.isChecked();
1035
1036 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001037 // enabling restriction; show confirmation dialog which
1038 // eventually calls setRestrictBackground() once user
1039 // confirms.
1040 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001041 } else {
1042 setAppRestrictBackground(false);
1043 }
1044 }
1045 };
1046
1047 private OnClickListener mAppSettingsListener = new OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001048 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001049 public void onClick(View v) {
1050 // TODO: target torwards entire UID instead of just first package
1051 startActivity(mAppSettingsIntent);
1052 }
1053 };
1054
Jeff Sharkey8a503642011-06-10 13:31:21 -07001055 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001056 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001057 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001058 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001059 final AppItem app = (AppItem) parent.getItemAtPosition(position);
1060 final UidDetail detail = mUidDetailProvider.getUidDetail(app.appId, true);
1061 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001062 }
1063 };
1064
1065 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001066 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001067 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1068 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1069 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001070 // show cycle editor; will eventually call setPolicyCycleDay()
1071 // when user finishes editing.
1072 CycleEditorFragment.show(DataUsageSummary.this);
1073
1074 // reset spinner to something other than "change cycle..."
1075 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001076
1077 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001078 if (LOGD) {
1079 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1080 + cycle.end + "]");
1081 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001082
1083 // update chart to show selected cycle, and update detail data
1084 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001085 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001086
1087 updateDetailData();
1088 }
1089 }
1090
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001091 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001092 public void onNothingSelected(AdapterView<?> parent) {
1093 // ignored
1094 }
1095 };
1096
1097 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001098 * Update details based on {@link #mChart} inspection range depending on
1099 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1100 * of applications data usage, and when {@link #isAppDetailMode()} update
1101 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001102 */
1103 private void updateDetailData() {
1104 if (LOGD) Log.d(TAG, "updateDetailData()");
1105
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001106 final long start = mChart.getInspectStart();
1107 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001108 final long now = System.currentTimeMillis();
1109
1110 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001111
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001112 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001113 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001114 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001115 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001116 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001117 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001118 final long foregroundBytes = entry.rxBytes + entry.txBytes;
1119
1120 mAppPieChart.setOriginAngle(175);
1121
1122 mAppPieChart.removeAllSlices();
1123 mAppPieChart.addSlice(foregroundBytes, Color.parseColor("#d88d3a"));
1124 mAppPieChart.addSlice(defaultBytes, Color.parseColor("#666666"));
1125
1126 mAppPieChart.generatePath();
1127
1128 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1129 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1130
1131 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001132 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001133
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001134 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001135
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001136 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001137 if (mChartData != null) {
1138 entry = mChartData.network.getValues(start, end, now, null);
1139 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001140
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001141 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001142 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001143 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001144 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001145
1146 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1147 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001148 final String rangePhrase = formatDateRange(context, start, end);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001149
Jeff Sharkeye557c332012-04-13 16:04:07 -07001150 final int summaryRes;
1151 if (TAB_MOBILE.equals(mCurrentTab) || TAB_3G.equals(mCurrentApp)
1152 || TAB_4G.equals(mCurrentApp)) {
1153 summaryRes = R.string.data_usage_total_during_range_mobile;
1154 } else {
1155 summaryRes = R.string.data_usage_total_during_range;
1156 }
1157
1158 mUsageSummary.setText(getString(summaryRes, totalPhrase, rangePhrase));
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001159 }
1160
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001161 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1162 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001163 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001164 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001165 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001166 }
1167
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001168 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001169 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1170 mChartData = data;
1171 mChart.bindNetworkStats(mChartData.network);
1172 mChart.bindDetailNetworkStats(mChartData.detail);
1173
1174 // calcuate policy cycles based on available data
1175 updatePolicy(true);
1176 updateAppDetail();
1177
1178 // force scroll to top of body when showing detail
1179 if (mChartData.detail != null) {
1180 mListView.smoothScrollToPosition(0);
1181 }
1182 }
1183
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001184 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001185 public void onLoaderReset(Loader<ChartData> loader) {
1186 mChartData = null;
1187 mChart.bindNetworkStats(null);
1188 mChart.bindDetailNetworkStats(null);
1189 }
1190 };
1191
1192 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001193 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001194 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001195 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001196 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001197 }
1198
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001199 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001200 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001201 final int[] restrictedAppIds = mPolicyManager.getAppsWithPolicy(
1202 POLICY_REJECT_METERED_BACKGROUND);
1203 mAdapter.bindStats(data, restrictedAppIds);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001204 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001205 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001206
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001207 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001208 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001209 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001210 updateEmptyVisible();
1211 }
1212
1213 private void updateEmptyVisible() {
1214 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1215 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001216 }
1217 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001218
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001219 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001220 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001221 final Context context = getActivity();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001222 if (hasReadyMobileRadio(context)) {
1223 final TelephonyManager tele = TelephonyManager.from(context);
1224 return mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001225 } else {
1226 return false;
1227 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001228 }
1229
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001230 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001231 private void setMobilePolicySplit(boolean split) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001232 final Context context = getActivity();
1233 if (hasReadyMobileRadio(context)) {
1234 final TelephonyManager tele = TelephonyManager.from(context);
1235 mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split);
1236 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001237 }
1238
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001239 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001240 final TelephonyManager tele = TelephonyManager.from(context);
1241 final String actualSubscriberId = tele.getSubscriberId();
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -08001242 return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001243 }
1244
Jeff Sharkey8a503642011-06-10 13:31:21 -07001245 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001246 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001247 public void onInspectRangeChanged() {
1248 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
1249 updateDetailData();
1250 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001251
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001252 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001253 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001254 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001255 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001256
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001257 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001258 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001259 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001260 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001261
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001262 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001263 public void requestWarningEdit() {
1264 WarningEditorFragment.show(DataUsageSummary.this);
1265 }
1266
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001267 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001268 public void requestLimitEdit() {
1269 LimitEditorFragment.show(DataUsageSummary.this);
1270 }
1271 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001272
1273 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001274 * List item that reflects a specific data usage cycle.
1275 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001276 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001277 public CharSequence label;
1278 public long start;
1279 public long end;
1280
Jeff Sharkey8a503642011-06-10 13:31:21 -07001281 CycleItem(CharSequence label) {
1282 this.label = label;
1283 }
1284
1285 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001286 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001287 this.start = start;
1288 this.end = end;
1289 }
1290
Jeff Sharkey8a503642011-06-10 13:31:21 -07001291 @Override
1292 public String toString() {
1293 return label.toString();
1294 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001295
1296 @Override
1297 public boolean equals(Object o) {
1298 if (o instanceof CycleItem) {
1299 final CycleItem another = (CycleItem) o;
1300 return start == another.start && end == another.end;
1301 }
1302 return false;
1303 }
1304
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001305 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001306 public int compareTo(CycleItem another) {
1307 return Long.compare(start, another.start);
1308 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001309 }
1310
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001311 private static final StringBuilder sBuilder = new StringBuilder(50);
1312 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1313 sBuilder, Locale.getDefault());
1314
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001315 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001316 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001317
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001318 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001319 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001320 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1321 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001322 }
1323 }
1324
Jeff Sharkey8a503642011-06-10 13:31:21 -07001325 /**
1326 * Special-case data usage cycle that triggers dialog to change
1327 * {@link NetworkPolicy#cycleDay}.
1328 */
1329 public static class CycleChangeItem extends CycleItem {
1330 public CycleChangeItem(Context context) {
1331 super(context.getString(R.string.data_usage_change_cycle));
1332 }
1333 }
1334
1335 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001336 private boolean mChangePossible = false;
1337 private boolean mChangeVisible = false;
1338
1339 private final CycleChangeItem mChangeItem;
1340
Jeff Sharkey8a503642011-06-10 13:31:21 -07001341 public CycleAdapter(Context context) {
1342 super(context, android.R.layout.simple_spinner_item);
1343 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001344 mChangeItem = new CycleChangeItem(context);
1345 }
1346
1347 public void setChangePossible(boolean possible) {
1348 mChangePossible = possible;
1349 updateChange();
1350 }
1351
1352 public void setChangeVisible(boolean visible) {
1353 mChangeVisible = visible;
1354 updateChange();
1355 }
1356
1357 private void updateChange() {
1358 remove(mChangeItem);
1359 if (mChangePossible && mChangeVisible) {
1360 add(mChangeItem);
1361 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001362 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001363
1364 /**
1365 * Find position of {@link CycleItem} in this adapter which is nearest
1366 * the given {@link CycleItem}.
1367 */
1368 public int findNearestPosition(CycleItem target) {
1369 if (target != null) {
1370 final int count = getCount();
1371 for (int i = count - 1; i >= 0; i--) {
1372 final CycleItem item = getItem(i);
1373 if (item instanceof CycleChangeItem) {
1374 continue;
1375 } else if (item.compareTo(target) >= 0) {
1376 return i;
1377 }
1378 }
1379 }
1380 return 0;
1381 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001382 }
1383
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001384 public static class AppItem implements Comparable<AppItem>, Parcelable {
1385 public final int appId;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001386 public boolean restricted;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001387 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001388 public long total;
1389
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001390 public AppItem(int appId) {
1391 this.appId = appId;
1392 }
1393
1394 public AppItem(Parcel parcel) {
1395 appId = parcel.readInt();
1396 uids = parcel.readSparseBooleanArray();
1397 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001398 }
1399
1400 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001401 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001402 }
1403
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001404 @Override
1405 public void writeToParcel(Parcel dest, int flags) {
1406 dest.writeInt(appId);
1407 dest.writeSparseBooleanArray(uids);
1408 dest.writeLong(total);
1409 }
1410
1411 @Override
1412 public int describeContents() {
1413 return 0;
1414 }
1415
1416 @Override
1417 public int compareTo(AppItem another) {
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001418 return Long.compare(another.total, total);
1419 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001420
1421 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1422 @Override
1423 public AppItem createFromParcel(Parcel in) {
1424 return new AppItem(in);
1425 }
1426
1427 @Override
1428 public AppItem[] newArray(int size) {
1429 return new AppItem[size];
1430 }
1431 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001432 }
1433
Jeff Sharkey8a503642011-06-10 13:31:21 -07001434 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001435 * Adapter of applications, sorted by total usage descending.
1436 */
1437 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001438 private final UidDetailProvider mProvider;
1439 private final int mInsetSide;
1440
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001441 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001442 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001443
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001444 public DataUsageAdapter(UidDetailProvider provider, int insetSide) {
1445 mProvider = checkNotNull(provider);
1446 mInsetSide = insetSide;
1447 }
1448
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001449 /**
1450 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1451 */
Jeff Sharkeye557c332012-04-13 16:04:07 -07001452 public void bindStats(NetworkStats stats, int[] restrictedAppIds) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001453 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001454
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001455 final AppItem systemItem = new AppItem(android.os.Process.SYSTEM_UID);
1456 final SparseArray<AppItem> knownUids = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001457
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001458 NetworkStats.Entry entry = null;
1459 final int size = stats != null ? stats.size() : 0;
1460 for (int i = 0; i < size; i++) {
1461 entry = stats.getValues(i, entry);
1462
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001463 final boolean isApp = UserId.isApp(entry.uid);
1464 final int appId = isApp ? UserId.getAppId(entry.uid) : entry.uid;
1465 if (isApp || appId == UID_REMOVED || appId == UID_TETHERING) {
1466 AppItem item = knownUids.get(appId);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001467 if (item == null) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001468 item = new AppItem(appId);
1469 knownUids.put(appId, item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001470 mItems.add(item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001471 }
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001472
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001473 item.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001474 item.addUid(entry.uid);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001475 } else {
1476 systemItem.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001477 systemItem.addUid(entry.uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001478 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001479 }
1480
Jeff Sharkeye557c332012-04-13 16:04:07 -07001481 for (int appId : restrictedAppIds) {
1482 AppItem item = knownUids.get(appId);
1483 if (item == null) {
1484 item = new AppItem(appId);
1485 item.total = -1;
1486 mItems.add(item);
1487 }
1488 item.restricted = true;
1489 }
1490
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001491 if (systemItem.total > 0) {
1492 mItems.add(systemItem);
1493 }
1494
Jeff Sharkey8a503642011-06-10 13:31:21 -07001495 Collections.sort(mItems);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001496 mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001497 notifyDataSetChanged();
1498 }
1499
1500 @Override
1501 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001502 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001503 }
1504
1505 @Override
1506 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001507 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001508 }
1509
1510 @Override
1511 public long getItemId(int position) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001512 return mItems.get(position).appId;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001513 }
1514
1515 @Override
1516 public View getView(int position, View convertView, ViewGroup parent) {
1517 if (convertView == null) {
1518 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey5d706792011-09-08 18:57:17 -07001519 R.layout.data_usage_item, parent, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001520
1521 if (mInsetSide > 0) {
1522 convertView.setPadding(mInsetSide, 0, mInsetSide, 0);
1523 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001524 }
1525
1526 final Context context = parent.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001527
Jeff Sharkey28130d92011-09-02 16:10:24 -07001528 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001529 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1530 android.R.id.progress);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001531
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001532 // kick off async load of app details
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001533 final AppItem item = mItems.get(position);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001534 UidDetailTask.bindView(mProvider, item, convertView);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001535
Jeff Sharkeye557c332012-04-13 16:04:07 -07001536 if (item.restricted && item.total <= 0) {
1537 text1.setText(R.string.data_usage_app_restricted);
1538 progress.setVisibility(View.GONE);
1539 } else {
1540 text1.setText(Formatter.formatFileSize(context, item.total));
1541 progress.setVisibility(View.VISIBLE);
1542 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001543
1544 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1545 progress.setProgress(percentTotal);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001546
1547 return convertView;
1548 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001549 }
1550
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001551 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001552 * Empty {@link Fragment} that controls display of UID details in
1553 * {@link DataUsageSummary}.
1554 */
1555 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001556 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001557
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001558 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001559 if (!parent.isAdded()) return;
1560
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001561 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001562 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001563
1564 final AppDetailsFragment fragment = new AppDetailsFragment();
1565 fragment.setArguments(args);
1566 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001567 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1568 ft.add(fragment, TAG_APP_DETAILS);
1569 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001570 ft.setBreadCrumbTitle(label);
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001571 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001572 }
1573
1574 @Override
1575 public void onStart() {
1576 super.onStart();
1577 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001578 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001579 target.updateBody();
1580 }
1581
1582 @Override
1583 public void onStop() {
1584 super.onStop();
1585 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001586 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001587 target.updateBody();
1588 }
1589 }
1590
1591 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001592 * Dialog to request user confirmation before setting
1593 * {@link NetworkPolicy#limitBytes}.
1594 */
1595 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001596 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001597 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001598
1599 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001600 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001601
Jeff Sharkey461842a2011-09-25 18:22:48 -07001602 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001603 final CharSequence message;
1604 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001605
1606 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001607 final String currentTab = parent.mCurrentTab;
1608 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001609 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001610 limitBytes = 5 * GB_IN_BYTES;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001611 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001612 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001613 limitBytes = 5 * GB_IN_BYTES;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001614 } else if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001615 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001616 limitBytes = 5 * GB_IN_BYTES;
1617 } else {
1618 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001619 }
1620
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001621 final Bundle args = new Bundle();
1622 args.putCharSequence(EXTRA_MESSAGE, message);
1623 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1624
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001625 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1626 dialog.setArguments(args);
1627 dialog.setTargetFragment(parent, 0);
1628 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1629 }
1630
1631 @Override
1632 public Dialog onCreateDialog(Bundle savedInstanceState) {
1633 final Context context = getActivity();
1634
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001635 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001636 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1637
1638 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1639 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001640 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001641
1642 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001643 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001644 public void onClick(DialogInterface dialog, int which) {
1645 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1646 if (target != null) {
1647 target.setPolicyLimitBytes(limitBytes);
1648 }
1649 }
1650 });
1651
1652 return builder.create();
1653 }
1654 }
1655
1656 /**
1657 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1658 */
1659 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001660 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001661
1662 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001663 if (!parent.isAdded()) return;
1664
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001665 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001666 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001667
1668 final CycleEditorFragment dialog = new CycleEditorFragment();
1669 dialog.setArguments(args);
1670 dialog.setTargetFragment(parent, 0);
1671 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1672 }
1673
1674 @Override
1675 public Dialog onCreateDialog(Bundle savedInstanceState) {
1676 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001677 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1678 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001679
1680 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1681 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1682
1683 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1684 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1685
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001686 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1687 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001688
1689 cycleDayPicker.setMinValue(1);
1690 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001691 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001692 cycleDayPicker.setWrapSelectorWheel(true);
1693
1694 builder.setTitle(R.string.data_usage_cycle_editor_title);
1695 builder.setView(view);
1696
1697 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1698 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001699 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001700 public void onClick(DialogInterface dialog, int which) {
1701 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001702 final String cycleTimezone = new Time().timezone;
1703 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001704 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001705 }
1706 });
1707
1708 return builder.create();
1709 }
1710 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001711
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001712 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001713 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1714 */
1715 public static class WarningEditorFragment extends DialogFragment {
1716 private static final String EXTRA_TEMPLATE = "template";
1717
1718 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001719 if (!parent.isAdded()) return;
1720
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001721 final Bundle args = new Bundle();
1722 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1723
1724 final WarningEditorFragment dialog = new WarningEditorFragment();
1725 dialog.setArguments(args);
1726 dialog.setTargetFragment(parent, 0);
1727 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1728 }
1729
1730 @Override
1731 public Dialog onCreateDialog(Bundle savedInstanceState) {
1732 final Context context = getActivity();
1733 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1734 final NetworkPolicyEditor editor = target.mPolicyEditor;
1735
1736 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1737 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1738
1739 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1740 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1741
1742 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1743 final long warningBytes = editor.getPolicyWarningBytes(template);
1744 final long limitBytes = editor.getPolicyLimitBytes(template);
1745
1746 bytesPicker.setMinValue(0);
1747 if (limitBytes != LIMIT_DISABLED) {
1748 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1749 } else {
1750 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1751 }
1752 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1753 bytesPicker.setWrapSelectorWheel(false);
1754
1755 builder.setTitle(R.string.data_usage_warning_editor_title);
1756 builder.setView(view);
1757
1758 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1759 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001760 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001761 public void onClick(DialogInterface dialog, int which) {
1762 // clear focus to finish pending text edits
1763 bytesPicker.clearFocus();
1764
1765 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1766 editor.setPolicyWarningBytes(template, bytes);
1767 target.updatePolicy(false);
1768 }
1769 });
1770
1771 return builder.create();
1772 }
1773 }
1774
1775 /**
1776 * Dialog to edit {@link NetworkPolicy#limitBytes}.
1777 */
1778 public static class LimitEditorFragment extends DialogFragment {
1779 private static final String EXTRA_TEMPLATE = "template";
1780
1781 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001782 if (!parent.isAdded()) return;
1783
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001784 final Bundle args = new Bundle();
1785 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1786
1787 final LimitEditorFragment dialog = new LimitEditorFragment();
1788 dialog.setArguments(args);
1789 dialog.setTargetFragment(parent, 0);
1790 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
1791 }
1792
1793 @Override
1794 public Dialog onCreateDialog(Bundle savedInstanceState) {
1795 final Context context = getActivity();
1796 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1797 final NetworkPolicyEditor editor = target.mPolicyEditor;
1798
1799 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1800 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1801
1802 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1803 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1804
1805 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1806 final long warningBytes = editor.getPolicyWarningBytes(template);
1807 final long limitBytes = editor.getPolicyLimitBytes(template);
1808
1809 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09001810 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001811 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
1812 } else {
1813 bytesPicker.setMinValue(0);
1814 }
1815 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
1816 bytesPicker.setWrapSelectorWheel(false);
1817
1818 builder.setTitle(R.string.data_usage_limit_editor_title);
1819 builder.setView(view);
1820
1821 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1822 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001823 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001824 public void onClick(DialogInterface dialog, int which) {
1825 // clear focus to finish pending text edits
1826 bytesPicker.clearFocus();
1827
1828 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1829 editor.setPolicyLimitBytes(template, bytes);
1830 target.updatePolicy(false);
1831 }
1832 });
1833
1834 return builder.create();
1835 }
1836 }
1837 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07001838 * Dialog to request user confirmation before disabling data.
1839 */
1840 public static class ConfirmDataDisableFragment extends DialogFragment {
1841 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001842 if (!parent.isAdded()) return;
1843
Jeff Sharkey28130d92011-09-02 16:10:24 -07001844 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
1845 dialog.setTargetFragment(parent, 0);
1846 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
1847 }
1848
1849 @Override
1850 public Dialog onCreateDialog(Bundle savedInstanceState) {
1851 final Context context = getActivity();
1852
1853 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1854 builder.setMessage(R.string.data_usage_disable_mobile);
1855
1856 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001857 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07001858 public void onClick(DialogInterface dialog, int which) {
1859 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1860 if (target != null) {
1861 // TODO: extend to modify policy enabled flag.
1862 target.setMobileDataEnabled(false);
1863 }
1864 }
1865 });
1866 builder.setNegativeButton(android.R.string.cancel, null);
1867
1868 return builder.create();
1869 }
1870 }
1871
1872 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001873 * Dialog to request user confirmation before setting
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001874 * {@link android.provider.Settings.Secure#DATA_ROAMING}.
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001875 */
1876 public static class ConfirmDataRoamingFragment extends DialogFragment {
1877 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001878 if (!parent.isAdded()) return;
1879
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001880 final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment();
1881 dialog.setTargetFragment(parent, 0);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001882 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_ROAMING);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001883 }
1884
1885 @Override
1886 public Dialog onCreateDialog(Bundle savedInstanceState) {
1887 final Context context = getActivity();
1888
1889 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1890 builder.setTitle(R.string.roaming_reenable_title);
1891 builder.setMessage(R.string.roaming_warning);
1892
1893 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001894 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001895 public void onClick(DialogInterface dialog, int which) {
1896 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1897 if (target != null) {
1898 target.setDataRoaming(true);
1899 }
1900 }
1901 });
1902 builder.setNegativeButton(android.R.string.cancel, null);
1903
1904 return builder.create();
1905 }
1906 }
1907
1908 /**
1909 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001910 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001911 */
1912 public static class ConfirmRestrictFragment extends DialogFragment {
1913 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001914 if (!parent.isAdded()) return;
1915
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001916 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1917 dialog.setTargetFragment(parent, 0);
1918 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1919 }
1920
1921 @Override
1922 public Dialog onCreateDialog(Bundle savedInstanceState) {
1923 final Context context = getActivity();
1924
1925 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001926 builder.setTitle(R.string.data_usage_restrict_background_title);
Jeff Sharkey461842a2011-09-25 18:22:48 -07001927 builder.setMessage(getString(R.string.data_usage_restrict_background));
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001928
1929 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001930 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001931 public void onClick(DialogInterface dialog, int which) {
1932 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1933 if (target != null) {
1934 target.setRestrictBackground(true);
1935 }
1936 }
1937 });
1938 builder.setNegativeButton(android.R.string.cancel, null);
1939
1940 return builder.create();
1941 }
1942 }
1943
1944 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001945 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
1946 * change has been denied, usually based on
1947 * {@link DataUsageSummary#hasLimitedNetworks()}.
1948 */
1949 public static class DeniedRestrictFragment extends DialogFragment {
1950 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001951 if (!parent.isAdded()) return;
1952
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001953 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
1954 dialog.setTargetFragment(parent, 0);
1955 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
1956 }
1957
1958 @Override
1959 public Dialog onCreateDialog(Bundle savedInstanceState) {
1960 final Context context = getActivity();
1961
1962 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1963 builder.setTitle(R.string.data_usage_app_restrict_background);
1964 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
1965 builder.setPositiveButton(android.R.string.ok, null);
1966
1967 return builder.create();
1968 }
1969 }
1970
1971 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001972 * Dialog to request user confirmation before setting
1973 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
1974 */
1975 public static class ConfirmAppRestrictFragment extends DialogFragment {
1976 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001977 if (!parent.isAdded()) return;
1978
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001979 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
1980 dialog.setTargetFragment(parent, 0);
1981 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
1982 }
1983
1984 @Override
1985 public Dialog onCreateDialog(Bundle savedInstanceState) {
1986 final Context context = getActivity();
1987
1988 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001989 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
1990 builder.setMessage(R.string.data_usage_app_restrict_dialog);
1991
1992 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001993 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001994 public void onClick(DialogInterface dialog, int which) {
1995 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1996 if (target != null) {
1997 target.setAppRestrictBackground(true);
1998 }
1999 }
2000 });
2001 builder.setNegativeButton(android.R.string.cancel, null);
2002
2003 return builder.create();
2004 }
2005 }
2006
2007 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002008 * Compute default tab that should be selected, based on
2009 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2010 */
2011 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002012 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
2013 if (template == null) return null;
2014
2015 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002016 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002017 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002018 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002019 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002020 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002021 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002022 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002023 return TAB_WIFI;
2024 default:
2025 return null;
2026 }
2027 }
2028
2029 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002030 * Background task that loads {@link UidDetail}, binding to
2031 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002032 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002033 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2034 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002035 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002036 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002037
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002038 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002039 mProvider = checkNotNull(provider);
2040 mItem = checkNotNull(item);
2041 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002042 }
2043
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002044 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002045 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002046 final UidDetailTask existing = (UidDetailTask) target.getTag();
2047 if (existing != null) {
2048 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002049 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002050
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002051 final UidDetail cachedDetail = provider.getUidDetail(item.appId, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002052 if (cachedDetail != null) {
2053 bindView(cachedDetail, target);
2054 } else {
2055 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2056 AsyncTask.THREAD_POOL_EXECUTOR));
2057 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002058 }
2059
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002060 private static void bindView(UidDetail detail, View target) {
2061 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2062 final TextView title = (TextView) target.findViewById(android.R.id.title);
2063
2064 if (detail != null) {
2065 icon.setImageDrawable(detail.icon);
2066 title.setText(detail.label);
2067 } else {
2068 icon.setImageDrawable(null);
2069 title.setText(null);
2070 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002071 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002072
2073 @Override
2074 protected void onPreExecute() {
2075 bindView(null, mTarget);
2076 }
2077
2078 @Override
2079 protected UidDetail doInBackground(Void... params) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002080 return mProvider.getUidDetail(mItem.appId, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002081 }
2082
2083 @Override
2084 protected void onPostExecute(UidDetail result) {
2085 bindView(result, mTarget);
2086 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002087 }
2088
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002089 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002090 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002091 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002092 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002093 if (TEST_RADIOS) {
2094 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2095 }
2096
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002097 final ConnectivityManager conn = ConnectivityManager.from(context);
2098 final TelephonyManager tele = TelephonyManager.from(context);
2099
2100 // require both supported network and ready SIM
2101 return conn.isNetworkSupported(TYPE_MOBILE) && tele.getSimState() == SIM_STATE_READY;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002102 }
2103
2104 /**
2105 * Test if device has a mobile 4G data radio.
2106 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002107 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002108 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2109 return false;
2110 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002111 if (TEST_RADIOS) {
2112 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2113 }
2114
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002115 final ConnectivityManager conn = ConnectivityManager.from(context);
2116 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002117
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002118 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002119 final boolean hasLte = (tele.getLteOnCdmaMode() == Phone.LTE_ON_CDMA_TRUE)
2120 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002121 return hasWimax || hasLte;
2122 }
2123
2124 /**
2125 * Test if device has a Wi-Fi data radio.
2126 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002127 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002128 if (TEST_RADIOS) {
2129 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2130 }
2131
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002132 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002133 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002134 }
2135
2136 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002137 * Test if device has an ethernet network connection.
2138 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002139 public static boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002140 if (TEST_RADIOS) {
2141 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2142 }
2143
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002144 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002145 return conn.isNetworkSupported(TYPE_ETHERNET);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002146 }
2147
2148 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002149 * Inflate a {@link Preference} style layout, adding the given {@link View}
2150 * widget into {@link android.R.id#widget_frame}.
2151 */
2152 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2153 final View view = inflater.inflate(R.layout.preference, root, false);
2154 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2155 android.R.id.widget_frame);
2156 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2157 return view;
2158 }
2159
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002160 private static View inflateAppTitle(
2161 LayoutInflater inflater, ViewGroup root, CharSequence label) {
2162 final TextView view = (TextView) inflater.inflate(
2163 R.layout.data_usage_app_title, root, false);
2164 view.setText(label);
2165 return view;
2166 }
2167
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002168 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002169 * Test if any networks are currently limited.
2170 */
2171 private boolean hasLimitedNetworks() {
2172 return !buildLimitedNetworksList().isEmpty();
2173 }
2174
2175 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002176 * Build string describing currently limited networks, which defines when
2177 * background data is restricted.
2178 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002179 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002180 private CharSequence buildLimitedNetworksString() {
2181 final List<CharSequence> limited = buildLimitedNetworksList();
2182
2183 // handle case where no networks limited
2184 if (limited.isEmpty()) {
2185 limited.add(getText(R.string.data_usage_list_none));
2186 }
2187
2188 return TextUtils.join(limited);
2189 }
2190
2191 /**
2192 * Build list of currently limited networks, which defines when background
2193 * data is restricted.
2194 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002195 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002196 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002197 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002198
2199 // build combined list of all limited networks
2200 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002201
2202 final TelephonyManager tele = TelephonyManager.from(context);
2203 if (tele.getSimState() == SIM_STATE_READY) {
2204 final String subscriberId = getActiveSubscriberId(context);
2205 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2206 limited.add(getText(R.string.data_usage_list_mobile));
2207 }
2208 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2209 limited.add(getText(R.string.data_usage_tab_3g));
2210 }
2211 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2212 limited.add(getText(R.string.data_usage_tab_4g));
2213 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002214 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002215
2216 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002217 limited.add(getText(R.string.data_usage_tab_wifi));
2218 }
2219 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2220 limited.add(getText(R.string.data_usage_tab_ethernet));
2221 }
2222
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002223 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002224 }
2225
2226 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002227 * Inset both selector and divider {@link Drawable} on the given
2228 * {@link ListView} by the requested dimensions.
2229 */
2230 private static void insetListViewDrawables(ListView view, int insetSide) {
2231 final Drawable selector = view.getSelector();
2232 final Drawable divider = view.getDivider();
2233
2234 // fully unregister these drawables so callbacks can be maintained after
2235 // wrapping below.
2236 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2237 view.setSelector(stub);
2238 view.setDivider(stub);
2239
2240 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2241 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2242 }
2243
2244 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002245 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002246 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002247 */
2248 private static void setPreferenceTitle(View parent, int resId) {
2249 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2250 title.setText(resId);
2251 }
2252
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002253 /**
2254 * Set {@link android.R.id#summary} for a preference view inflated with
2255 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2256 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002257 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002258 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2259 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002260 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002261 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002262}