blob: 94c8cda4a0b301e2fe4daef8336e16de848ea021 [file] [log] [blame]
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070019import static android.net.ConnectivityManager.TYPE_ETHERNET;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070020import static android.net.ConnectivityManager.TYPE_MOBILE;
Jeff Sharkeybdf98e82011-11-10 17:17:24 -080021import static android.net.ConnectivityManager.TYPE_WIFI;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070022import static android.net.ConnectivityManager.TYPE_WIMAX;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070023import static android.net.NetworkPolicy.LIMIT_DISABLED;
Jeff Sharkeya53188f2011-09-13 19:56:45 -070024import static android.net.NetworkPolicy.WARNING_DISABLED;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070025import static android.net.NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070026import static android.net.NetworkPolicyManager.POLICY_NONE;
27import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND;
Jeff Sharkey8a503642011-06-10 13:31:21 -070028import static android.net.NetworkPolicyManager.computeLastCycleBoundary;
29import static android.net.NetworkPolicyManager.computeNextCycleBoundary;
Jeff Sharkeya662e492011-06-18 21:57:06 -070030import static android.net.NetworkTemplate.MATCH_MOBILE_3G_LOWER;
31import static android.net.NetworkTemplate.MATCH_MOBILE_4G;
32import static android.net.NetworkTemplate.MATCH_MOBILE_ALL;
33import static android.net.NetworkTemplate.MATCH_WIFI;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070034import static android.net.NetworkTemplate.buildTemplateEthernet;
35import static android.net.NetworkTemplate.buildTemplateMobile3gLower;
36import static android.net.NetworkTemplate.buildTemplateMobile4g;
37import static android.net.NetworkTemplate.buildTemplateMobileAll;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070038import static android.net.NetworkTemplate.buildTemplateWifiWildcard;
Jeff Sharkey77dae912012-02-03 14:50:33 -080039import static android.net.TrafficStats.GB_IN_BYTES;
40import static android.net.TrafficStats.MB_IN_BYTES;
Jeff Sharkeya83a24f2011-09-16 01:52:39 -070041import static android.net.TrafficStats.UID_REMOVED;
42import static android.net.TrafficStats.UID_TETHERING;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070043import static android.telephony.TelephonyManager.SIM_STATE_READY;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070044import static android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
45import static android.text.format.DateUtils.FORMAT_SHOW_DATE;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070046import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -070047import static com.android.internal.util.Preconditions.checkNotNull;
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -070048import static com.android.settings.Utils.prepareCustomPreferencesList;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070049
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070050import android.animation.LayoutTransition;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070051import android.app.AlertDialog;
52import android.app.Dialog;
53import android.app.DialogFragment;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070054import android.app.Fragment;
Amith Yamasani5ba0a022011-11-07 12:29:00 -080055import android.app.FragmentManager;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070056import android.app.FragmentTransaction;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070057import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070058import android.content.ContentResolver;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070059import android.content.Context;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070060import android.content.DialogInterface;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -070061import android.content.Intent;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070062import android.content.Loader;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070063import android.content.SharedPreferences;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070064import android.content.pm.PackageManager;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070065import android.content.res.Resources;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070066import android.graphics.Color;
Jeff Sharkey5d706792011-09-08 18:57:17 -070067import android.graphics.drawable.ColorDrawable;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070068import android.graphics.drawable.Drawable;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070069import android.net.ConnectivityManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070070import android.net.INetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070071import android.net.INetworkStatsService;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070072import android.net.INetworkStatsSession;
Jeff Sharkey8a503642011-06-10 13:31:21 -070073import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070074import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070075import android.net.NetworkStats;
76import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070077import android.net.NetworkTemplate;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070078import android.net.TrafficStats;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070079import android.net.Uri;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070080import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070081import android.os.Bundle;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070082import android.os.INetworkManagementService;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070083import android.os.Parcel;
84import android.os.Parcelable;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070085import android.os.RemoteException;
86import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070087import android.os.SystemProperties;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070088import android.os.UserId;
Jeff Sharkey8a503642011-06-10 13:31:21 -070089import android.preference.Preference;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070090import android.preference.PreferenceActivity;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070091import android.provider.Settings;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070092import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070093import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070094import android.text.format.DateUtils;
95import android.text.format.Formatter;
Jeff Sharkeye5223a02012-03-09 17:11:14 -080096import android.text.format.Time;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070097import android.util.Log;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070098import android.util.SparseArray;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070099import android.util.SparseBooleanArray;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700100import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700101import android.view.Menu;
102import android.view.MenuInflater;
103import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700104import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700105import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700106import android.view.ViewGroup;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700107import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700108import android.widget.AdapterView;
109import android.widget.AdapterView.OnItemClickListener;
110import android.widget.AdapterView.OnItemSelectedListener;
111import android.widget.ArrayAdapter;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700112import android.widget.BaseAdapter;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700113import android.widget.Button;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700114import android.widget.CheckBox;
115import android.widget.CompoundButton;
116import android.widget.CompoundButton.OnCheckedChangeListener;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700117import android.widget.ImageView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700118import android.widget.LinearLayout;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700119import android.widget.ListView;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700120import android.widget.NumberPicker;
Jeff Sharkey2412b0f2011-07-17 20:31:40 -0700121import android.widget.ProgressBar;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700122import android.widget.Spinner;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700123import android.widget.Switch;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700124import android.widget.TabHost;
125import android.widget.TabHost.OnTabChangeListener;
126import android.widget.TabHost.TabContentFactory;
127import android.widget.TabHost.TabSpec;
128import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700129import android.widget.TextView;
130
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700131import com.android.internal.telephony.Phone;
Jeff Sharkey5d706792011-09-08 18:57:17 -0700132import com.android.settings.drawable.InsetBoundsDrawable;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700133import com.android.settings.net.ChartData;
134import com.android.settings.net.ChartDataLoader;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700135import com.android.settings.net.DataUsageMeteredSettings;
Jeff Sharkeya662e492011-06-18 21:57:06 -0700136import com.android.settings.net.NetworkPolicyEditor;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700137import com.android.settings.net.SummaryForAllUidLoader;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700138import com.android.settings.net.UidDetail;
139import com.android.settings.net.UidDetailProvider;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700140import com.android.settings.widget.ChartDataUsageView;
141import com.android.settings.widget.ChartDataUsageView.DataUsageChartListener;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700142import com.android.settings.widget.PieChartView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700143import com.google.android.collect.Lists;
144
145import java.util.ArrayList;
146import java.util.Collections;
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700147import java.util.List;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700148import java.util.Locale;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700149
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700150import libcore.util.Objects;
151
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700152/**
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700153 * Panel showing data usage history across various networks, including options
154 * to inspect based on usage cycle and control through {@link NetworkPolicy}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700155 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700156public class DataUsageSummary extends Fragment {
157 private static final String TAG = "DataUsage";
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800158 private static final boolean LOGD = false;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700159
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700160 // TODO: remove this testing code
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700161 private static final boolean TEST_ANIM = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700162 private static final boolean TEST_RADIOS = false;
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800163
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700164 private static final String TEST_RADIOS_PROP = "test.radios";
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800165 private static final String TEST_SUBSCRIBER_PROP = "test.subscriberid";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700166
Jeff Sharkey8a503642011-06-10 13:31:21 -0700167 private static final String TAB_3G = "3g";
168 private static final String TAB_4G = "4g";
169 private static final String TAB_MOBILE = "mobile";
170 private static final String TAB_WIFI = "wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700171 private static final String TAB_ETHERNET = "ethernet";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700172
Jeff Sharkey28130d92011-09-02 16:10:24 -0700173 private static final String TAG_CONFIRM_DATA_DISABLE = "confirmDataDisable";
174 private static final String TAG_CONFIRM_DATA_ROAMING = "confirmDataRoaming";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700175 private static final String TAG_CONFIRM_LIMIT = "confirmLimit";
176 private static final String TAG_CYCLE_EDITOR = "cycleEditor";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700177 private static final String TAG_WARNING_EDITOR = "warningEditor";
178 private static final String TAG_LIMIT_EDITOR = "limitEditor";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700179 private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700180 private static final String TAG_DENIED_RESTRICT = "deniedRestrict";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700181 private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700182 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700183
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700184 private static final int LOADER_CHART_DATA = 2;
185 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700186
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700187 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700188 private INetworkStatsService mStatsService;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700189 private NetworkPolicyManager mPolicyManager;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700190 private ConnectivityManager mConnService;
191
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700192 private INetworkStatsSession mStatsSession;
193
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700194 private static final String PREF_FILE = "data_usage";
195 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700196 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700197
198 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700199
Jeff Sharkey8a503642011-06-10 13:31:21 -0700200 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700201 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700202 private TabWidget mTabWidget;
203 private ListView mListView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700204 private DataUsageAdapter mAdapter;
205
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700206 /** Distance to inset content from sides, when needed. */
207 private int mInsetSide = 0;
208
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700209 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700210
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700211 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700212 private LinearLayout mNetworkSwitches;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700213 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700214 private View mDataEnabledView;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700215 private CheckBox mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700216 private View mDisableAtLimitView;
217
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700218 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700219 private Spinner mCycleSpinner;
220 private CycleAdapter mCycleAdapter;
221
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700222 private ChartDataUsageView mChart;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700223 private TextView mUsageSummary;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700224 private TextView mEmpty;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700225
226 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700227 private ImageView mAppIcon;
228 private ViewGroup mAppTitles;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700229 private PieChartView mAppPieChart;
230 private TextView mAppForeground;
231 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700232 private Button mAppSettings;
233
234 private LinearLayout mAppSwitches;
235 private CheckBox mAppRestrict;
236 private View mAppRestrictView;
237
Jeff Sharkey8a503642011-06-10 13:31:21 -0700238 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700239 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700240
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700241 private NetworkTemplate mTemplate;
242 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700243
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700244 private AppItem mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700245
246 private Intent mAppSettingsIntent;
247
Jeff Sharkeya662e492011-06-18 21:57:06 -0700248 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700249
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700250 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700251 private String mIntentTab = null;
252
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700253 private MenuItem mMenuDataRoaming;
254 private MenuItem mMenuRestrictBackground;
255
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700256 /** Flag used to ignore listeners during binding. */
257 private boolean mBinding;
258
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700259 private UidDetailProvider mUidDetailProvider;
260
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700261 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700262 public void onCreate(Bundle savedInstanceState) {
263 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700264 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700265
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700266 mNetworkService = INetworkManagementService.Stub.asInterface(
267 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700268 mStatsService = INetworkStatsService.Stub.asInterface(
269 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700270 mPolicyManager = NetworkPolicyManager.from(context);
271 mConnService = ConnectivityManager.from(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700272
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700273 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700274
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700275 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700276 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700277
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700278 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700279 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700280
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700281 // override preferences when no mobile radio
282 if (!hasReadyMobileRadio(context)) {
283 mShowWifi = hasWifiRadio(context);
284 mShowEthernet = hasEthernet(context);
285 }
286
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700287 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700288 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700289
Jeff Sharkey8a503642011-06-10 13:31:21 -0700290 @Override
291 public View onCreateView(LayoutInflater inflater, ViewGroup container,
292 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700293
Jeff Sharkey8a503642011-06-10 13:31:21 -0700294 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700295 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
296
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700297 mUidDetailProvider = new UidDetailProvider(context);
298
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700299 try {
300 mStatsSession = mStatsService.openSession();
301 } catch (RemoteException e) {
302 throw new RuntimeException(e);
303 }
304
Jeff Sharkey8a503642011-06-10 13:31:21 -0700305 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700306 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700307 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
308 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700309
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700310 // decide if we need to manually inset our content, or if we should rely
311 // on parent container for inset.
312 final boolean shouldInset = mListView.getScrollBarStyle()
313 == View.SCROLLBARS_OUTSIDE_OVERLAY;
314 if (shouldInset) {
315 mInsetSide = view.getResources().getDimensionPixelOffset(
316 com.android.internal.R.dimen.preference_fragment_padding_side);
317 } else {
318 mInsetSide = 0;
319 }
320
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700321 // adjust padding around tabwidget as needed
Jeff Sharkey5d706792011-09-08 18:57:17 -0700322 prepareCustomPreferencesList(container, view, mListView, true);
323
Jeff Sharkey8a503642011-06-10 13:31:21 -0700324 mTabHost.setup();
325 mTabHost.setOnTabChangedListener(mTabListener);
326
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700327 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700328 mHeader.setClickable(true);
329
330 mListView.addHeaderView(mHeader, null, true);
331 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700332
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700333 if (mInsetSide > 0) {
334 // inset selector and divider drawables
335 insetListViewDrawables(mListView, mInsetSide);
336 mHeader.setPadding(mInsetSide, 0, mInsetSide, 0);
337 }
338
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700339 {
340 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700341 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
342 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700343 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700344
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700345 mDataEnabled = new Switch(inflater.getContext());
346 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
347 mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener);
348 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700349
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700350 mDisableAtLimit = new CheckBox(inflater.getContext());
351 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700352 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700353 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700354 mDisableAtLimitView.setClickable(true);
355 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700356 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
357 mNetworkSwitches.addView(mDisableAtLimitView);
358 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700359
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700360 // bind cycle dropdown
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700361 mCycleView = mHeader.findViewById(R.id.cycles);
362 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700363 mCycleAdapter = new CycleAdapter(context);
364 mCycleSpinner.setAdapter(mCycleAdapter);
365 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
366
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700367 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700368 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800369 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700370
371 {
372 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700373 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700374 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
375 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700376 mAppPieChart = (PieChartView) mAppDetail.findViewById(R.id.app_pie_chart);
377 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
378 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700379 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700380
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700381 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700382 mAppSettings.setOnClickListener(mAppSettingsListener);
383
384 mAppRestrict = new CheckBox(inflater.getContext());
385 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700386 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700387 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700388 mAppRestrictView.setClickable(true);
389 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700390 mAppRestrictView.setOnClickListener(mAppRestrictListener);
391 mAppSwitches.addView(mAppRestrictView);
392 }
393
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700394 mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700395 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700396
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700397 mAdapter = new DataUsageAdapter(mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700398 mListView.setOnItemClickListener(mListListener);
399 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700400
401 return view;
402 }
403
404 @Override
405 public void onResume() {
406 super.onResume();
407
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700408 // pick default tab based on incoming intent
409 final Intent intent = getActivity().getIntent();
410 mIntentTab = computeTabFromIntent(intent);
411
Jeff Sharkey8a503642011-06-10 13:31:21 -0700412 // this kicks off chain reaction which creates tabs, binds the body to
413 // selected network, and binds chart, cycles and detail list.
414 updateTabs();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700415
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700416 // kick off background task to update stats
417 new AsyncTask<Void, Void, Void>() {
418 @Override
419 protected Void doInBackground(Void... params) {
420 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700421 // wait a few seconds before kicking off
422 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700423 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700424 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700425 } catch (RemoteException e) {
426 }
427 return null;
428 }
429
430 @Override
431 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700432 if (isAdded()) {
433 updateBody();
434 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700435 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700436 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700437 }
438
Jeff Sharkey8a503642011-06-10 13:31:21 -0700439 @Override
440 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
441 inflater.inflate(R.menu.data_usage, menu);
442 }
443
444 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700445 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700446 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700447 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700448
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700449 mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700450 mMenuDataRoaming.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700451 mMenuDataRoaming.setChecked(getDataRoaming());
452
453 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700454 mMenuRestrictBackground.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
455 mMenuRestrictBackground.setChecked(mPolicyManager.getRestrictBackground());
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700456
457 final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g);
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700458 split4g.setVisible(hasReadyMobile4gRadio(context) && !appDetailMode);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700459 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700460
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700461 final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700462 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700463 showWifi.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700464 showWifi.setChecked(mShowWifi);
465 } else {
466 showWifi.setVisible(false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700467 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700468
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700469 final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700470 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700471 showEthernet.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700472 showEthernet.setChecked(mShowEthernet);
473 } else {
474 showEthernet.setVisible(false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700475 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700476
477 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
478 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
479 metered.setVisible(!appDetailMode);
480 } else {
481 metered.setVisible(false);
482 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700483
484 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
485 String helpUrl;
486 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
487 Intent helpIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
488 helpIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
489 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
490 help.setIntent(helpIntent);
491 help.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
492 } else {
493 help.setVisible(false);
494 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700495 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700496
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700497 @Override
498 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700499 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700500 case R.id.data_usage_menu_roaming: {
501 final boolean dataRoaming = !item.isChecked();
502 if (dataRoaming) {
503 ConfirmDataRoamingFragment.show(this);
504 } else {
505 // no confirmation to disable roaming
506 setDataRoaming(false);
507 }
508 return true;
509 }
510 case R.id.data_usage_menu_restrict_background: {
511 final boolean restrictBackground = !item.isChecked();
512 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800513 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700514 } else {
515 // no confirmation to drop restriction
516 setRestrictBackground(false);
517 }
518 return true;
519 }
520 case R.id.data_usage_menu_split_4g: {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700521 final boolean mobileSplit = !item.isChecked();
Jeff Sharkeya662e492011-06-18 21:57:06 -0700522 setMobilePolicySplit(mobileSplit);
523 item.setChecked(isMobilePolicySplit());
Jeff Sharkey8a503642011-06-10 13:31:21 -0700524 updateTabs();
525 return true;
526 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700527 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700528 mShowWifi = !item.isChecked();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700529 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700530 item.setChecked(mShowWifi);
531 updateTabs();
532 return true;
533 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700534 case R.id.data_usage_menu_show_ethernet: {
535 mShowEthernet = !item.isChecked();
536 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
537 item.setChecked(mShowEthernet);
538 updateTabs();
539 return true;
540 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700541 case R.id.data_usage_menu_metered: {
542 final PreferenceActivity activity = (PreferenceActivity) getActivity();
543 activity.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
544 R.string.data_usage_metered_title, null, this, 0);
545 return true;
546 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700547 }
548 return false;
549 }
550
Jeff Sharkey94a90952011-06-13 22:31:09 -0700551 @Override
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700552 public void onDestroy() {
Jeff Sharkey94a90952011-06-13 22:31:09 -0700553 mDataEnabledView = null;
554 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700555
556 mUidDetailProvider.clearCache();
557 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700558
559 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700560
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800561 if (this.isRemoving()) {
562 getFragmentManager()
563 .popBackStack(TAG_APP_DETAILS, FragmentManager.POP_BACK_STACK_INCLUSIVE);
564 }
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700565
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800566 super.onDestroy();
567 }
568
Jeff Sharkey8a503642011-06-10 13:31:21 -0700569 /**
Jeff Sharkey92811822012-05-04 11:47:29 -0700570 * Build and assign {@link LayoutTransition} to various containers. Should
571 * only be assigned after initial layout is complete.
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700572 */
Jeff Sharkey92811822012-05-04 11:47:29 -0700573 private void ensureLayoutTransitions() {
574 // skip when already setup
575 if (mChart.getLayoutTransition() != null) return;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700576
Jeff Sharkey92811822012-05-04 11:47:29 -0700577 mTabsContainer.setLayoutTransition(buildLayoutTransition());
578 mHeader.setLayoutTransition(buildLayoutTransition());
579 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700580
Jeff Sharkey92811822012-05-04 11:47:29 -0700581 final LayoutTransition chartTransition = buildLayoutTransition();
582 chartTransition.disableTransitionType(LayoutTransition.APPEARING);
583 chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
584 mChart.setLayoutTransition(chartTransition);
585 }
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700586
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700587 private static LayoutTransition buildLayoutTransition() {
588 final LayoutTransition transition = new LayoutTransition();
589 if (TEST_ANIM) {
590 transition.setDuration(1500);
591 }
592 transition.setAnimateParentHierarchy(false);
593 return transition;
594 }
595
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700596 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700597 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700598 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
599 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700600 */
601 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700602 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700603 mTabHost.clearAllTabs();
604
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700605 final boolean mobileSplit = isMobilePolicySplit();
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700606 if (mobileSplit && hasReadyMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700607 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
608 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700609 } else if (hasReadyMobileRadio(context)) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700610 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700611 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700612 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700613 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
614 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700615 if (mShowEthernet && hasEthernet(context)) {
616 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
617 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700618
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700619 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700620 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
621 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
622 if (mIntentTab != null) {
623 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700624 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700625 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700626 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700627 mTabHost.setCurrentTabByTag(mIntentTab);
628 }
629 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700630 } else if (noTabs) {
631 // no usable tabs, so hide body
632 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700633 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700634 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700635 }
636 }
637
Jeff Sharkey8a503642011-06-10 13:31:21 -0700638 /**
639 * Factory that provide empty {@link View} to make {@link TabHost} happy.
640 */
641 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700642 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700643 public View createTabContent(String tag) {
644 return new View(mTabHost.getContext());
645 }
646 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700647
Jeff Sharkey8a503642011-06-10 13:31:21 -0700648 /**
649 * Build {@link TabSpec} with thin indicator, and empty content.
650 */
651 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700652 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
653 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700654 }
655
656 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700657 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700658 public void onTabChanged(String tabId) {
659 // user changed tab; update body
660 updateBody();
661 }
662 };
663
664 /**
665 * Update body content based on current tab. Loads
666 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
667 * binds them to visible controls.
668 */
669 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700670 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700671 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700672
Jeff Sharkeya662e492011-06-18 21:57:06 -0700673 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700674 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700675
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700676 if (currentTab == null) {
677 Log.w(TAG, "no tab selected; hiding body");
678 mListView.setVisibility(View.GONE);
679 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700680 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700681 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700682 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700683
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700684 final boolean tabChanged = !currentTab.equals(mCurrentTab);
685 mCurrentTab = currentTab;
686
Jeff Sharkey8a503642011-06-10 13:31:21 -0700687 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
688
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700689 mDataEnabledView.setVisibility(View.VISIBLE);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700690
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700691 // TODO: remove mobile tabs when SIM isn't ready
692 final TelephonyManager tele = TelephonyManager.from(context);
693
Jeff Sharkey8a503642011-06-10 13:31:21 -0700694 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700695 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
696 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700697 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700698
699 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700700 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
701 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
702 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700703 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700704
705 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700706 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
707 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
708 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700709 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700710
711 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700712 // wifi doesn't have any controls
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700713 mDataEnabledView.setVisibility(View.GONE);
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700714 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700715 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700716
717 } else if (TAB_ETHERNET.equals(currentTab)) {
718 // ethernet doesn't have any controls
719 mDataEnabledView.setVisibility(View.GONE);
720 mDisableAtLimitView.setVisibility(View.GONE);
721 mTemplate = buildTemplateEthernet();
722
723 } else {
724 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700725 }
726
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700727 // kick off loader for network history
728 // TODO: consider chaining two loaders together instead of reloading
729 // network history when showing app detail.
730 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700731 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700732
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700733 // detail mode can change visible menus, invalidate
734 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700735
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700736 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700737 }
738
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700739 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700740 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700741 }
742
743 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700744 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700745 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700746 */
747 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700748 final Context context = getActivity();
749 final PackageManager pm = context.getPackageManager();
750 final LayoutInflater inflater = getActivity().getLayoutInflater();
751
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700752 if (isAppDetailMode()) {
753 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700754 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700755 } else {
756 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700757 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700758
759 // hide detail stats when not in detail mode
760 mChart.bindDetailNetworkStats(null);
761 return;
762 }
763
764 // remove warning/limit sweeps while in detail mode
765 mChart.bindNetworkPolicy(null);
766
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700767 // show icon and all labels appearing under this app
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700768 final int appId = mCurrentApp.appId;
769 final UidDetail detail = mUidDetailProvider.getUidDetail(appId, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700770 mAppIcon.setImageDrawable(detail.icon);
771
772 mAppTitles.removeAllViews();
773 if (detail.detailLabels != null) {
774 for (CharSequence label : detail.detailLabels) {
775 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label));
776 }
777 } else {
778 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label));
779 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700780
781 // enable settings button when package provides it
782 // TODO: target torwards entire UID instead of just first package
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700783 final String[] packageNames = pm.getPackagesForUid(appId);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700784 if (packageNames != null && packageNames.length > 0) {
785 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
786 mAppSettingsIntent.setPackage(packageNames[0]);
787 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
788
789 final boolean matchFound = pm.resolveActivity(mAppSettingsIntent, 0) != null;
790 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -0700791 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700792
793 } else {
794 mAppSettingsIntent = null;
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700795 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700796 }
797
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700798 updateDetailData();
799
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700800 if (UserId.isApp(appId) && !mPolicyManager.getRestrictBackground()
801 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700802 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800803 setPreferenceSummary(mAppRestrictView,
804 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700805
806 mAppRestrictView.setVisibility(View.VISIBLE);
807 mAppRestrict.setChecked(getAppRestrictBackground());
808
809 } else {
810 mAppRestrictView.setVisibility(View.GONE);
811 }
812 }
813
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700814 private void setPolicyWarningBytes(long warningBytes) {
815 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700816 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700817 updatePolicy(false);
818 }
819
820 private void setPolicyLimitBytes(long limitBytes) {
821 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700822 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700823 updatePolicy(false);
824 }
825
Jeff Sharkey28130d92011-09-02 16:10:24 -0700826 /**
827 * Local cache of value, used to work around delay when
828 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
829 */
830 private Boolean mMobileDataEnabled;
831
832 private boolean isMobileDataEnabled() {
833 if (mMobileDataEnabled != null) {
834 // TODO: deprecate and remove this once enabled flag is on policy
835 return mMobileDataEnabled;
836 } else {
837 return mConnService.getMobileDataEnabled();
838 }
839 }
840
841 private void setMobileDataEnabled(boolean enabled) {
842 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
843 mConnService.setMobileDataEnabled(enabled);
844 mMobileDataEnabled = enabled;
845 updatePolicy(false);
846 }
847
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700848 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
849 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked();
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700850 }
851
852 private boolean isBandwidthControlEnabled() {
853 try {
854 return mNetworkService.isBandwidthControlEnabled();
855 } catch (RemoteException e) {
856 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
857 return false;
858 }
859 }
860
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700861 private boolean getDataRoaming() {
862 final ContentResolver resolver = getActivity().getContentResolver();
863 return Settings.Secure.getInt(resolver, Settings.Secure.DATA_ROAMING, 0) != 0;
864 }
865
866 private void setDataRoaming(boolean enabled) {
867 // TODO: teach telephony DataConnectionTracker to watch and apply
868 // updates when changed.
869 final ContentResolver resolver = getActivity().getContentResolver();
870 Settings.Secure.putInt(resolver, Settings.Secure.DATA_ROAMING, enabled ? 1 : 0);
871 mMenuDataRoaming.setChecked(enabled);
872 }
873
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700874 public void setRestrictBackground(boolean restrictBackground) {
875 mPolicyManager.setRestrictBackground(restrictBackground);
876 mMenuRestrictBackground.setChecked(restrictBackground);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700877 }
878
879 private boolean getAppRestrictBackground() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700880 final int appId = mCurrentApp.appId;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700881 final int uidPolicy = mPolicyManager.getAppPolicy(appId);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700882 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
883 }
884
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700885 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700886 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700887 final int appId = mCurrentApp.appId;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700888 mPolicyManager.setAppPolicy(appId,
889 restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700890 mAppRestrict.setChecked(restrictBackground);
891 }
892
Jeff Sharkey8a503642011-06-10 13:31:21 -0700893 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700894 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
895 * current {@link #mTemplate}.
896 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700897 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700898 if (isAppDetailMode()) {
899 mNetworkSwitches.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700900 } else {
901 mNetworkSwitches.setVisibility(View.VISIBLE);
902 }
903
Jeff Sharkey28130d92011-09-02 16:10:24 -0700904 // TODO: move enabled state directly into policy
905 if (TAB_MOBILE.equals(mCurrentTab)) {
906 mBinding = true;
907 mDataEnabled.setChecked(isMobileDataEnabled());
908 mBinding = false;
909 }
910
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700911 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
912 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700913 mDisableAtLimitView.setVisibility(View.VISIBLE);
914 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700915 if (!isAppDetailMode()) {
916 mChart.bindNetworkPolicy(policy);
917 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700918
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700919 } else {
920 // controls are disabled; don't bind warning/limit sweeps
921 mDisableAtLimitView.setVisibility(View.GONE);
922 mChart.bindNetworkPolicy(null);
923 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700924
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700925 if (refreshCycle) {
926 // generate cycle list based on policy and available history
927 updateCycleList(policy);
928 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700929 }
930
931 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700932 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
933 * and available {@link NetworkStatsHistory} data. Always selects the newest
934 * item, updating the inspection range on {@link #mChart}.
935 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700936 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700937 // stash away currently selected cycle to try restoring below
938 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700939 mCycleAdapter.clear();
940
941 final Context context = mCycleSpinner.getContext();
942
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700943 long historyStart = Long.MAX_VALUE;
944 long historyEnd = Long.MIN_VALUE;
945 if (mChartData != null) {
946 historyStart = mChartData.network.getStart();
947 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700948 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700949
Jeff Sharkey461842a2011-09-25 18:22:48 -0700950 final long now = System.currentTimeMillis();
951 if (historyStart == Long.MAX_VALUE) historyStart = now;
952 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700953
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700954 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700955 if (policy != null) {
956 // find the next cycle boundary
957 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700958
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700959 // walk backwards, generating all valid cycle ranges
960 while (cycleEnd > historyStart) {
961 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
962 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
963 + historyStart);
964 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
965 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700966 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700967 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700968
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700969 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700970 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700971 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700972
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700973 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700974 // no policy defined cycles; show entry for each four-week period
975 long cycleEnd = historyEnd;
976 while (cycleEnd > historyStart) {
977 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
978 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
979 cycleEnd = cycleStart;
980 }
981
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700982 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700983 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700984
985 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700986 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700987 final int position = mCycleAdapter.findNearestPosition(previousItem);
988 mCycleSpinner.setSelection(position);
989
990 // only force-update cycle when changed; skipping preserves any
991 // user-defined inspection region.
992 final CycleItem selectedItem = mCycleAdapter.getItem(position);
993 if (!Objects.equal(selectedItem, previousItem)) {
994 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
995 } else {
996 // but still kick off loader for detailed list
997 updateDetailData();
998 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700999 } else {
1000 updateDetailData();
1001 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001002 }
1003
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001004 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001005 @Override
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001006 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
1007 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001008
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001009 final boolean dataEnabled = isChecked;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001010 final String currentTab = mCurrentTab;
1011 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001012 if (dataEnabled) {
1013 setMobileDataEnabled(true);
1014 } else {
1015 // disabling data; show confirmation dialog which eventually
1016 // calls setMobileDataEnabled() once user confirms.
1017 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1018 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001019 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001020
Jeff Sharkey28130d92011-09-02 16:10:24 -07001021 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001022 }
1023 };
1024
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001025 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001026 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001027 public void onClick(View v) {
1028 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001029 if (disableAtLimit) {
1030 // enabling limit; show confirmation dialog which eventually
1031 // calls setPolicyLimitBytes() once user confirms.
1032 ConfirmLimitFragment.show(DataUsageSummary.this);
1033 } else {
1034 setPolicyLimitBytes(LIMIT_DISABLED);
1035 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001036 }
1037 };
1038
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001039 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001040 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001041 public void onClick(View v) {
1042 final boolean restrictBackground = !mAppRestrict.isChecked();
1043
1044 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001045 // enabling restriction; show confirmation dialog which
1046 // eventually calls setRestrictBackground() once user
1047 // confirms.
1048 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001049 } else {
1050 setAppRestrictBackground(false);
1051 }
1052 }
1053 };
1054
1055 private OnClickListener mAppSettingsListener = new OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001056 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001057 public void onClick(View v) {
Jeff Sharkeyd92e0412012-04-24 11:35:43 -07001058 if (!isAdded()) return;
1059
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001060 // TODO: target torwards entire UID instead of just first package
1061 startActivity(mAppSettingsIntent);
1062 }
1063 };
1064
Jeff Sharkey8a503642011-06-10 13:31:21 -07001065 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001066 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001067 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001068 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001069 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001070
1071 // TODO: sigh, remove this hack once we understand 6450986
1072 if (mUidDetailProvider == null || app == null) return;
1073
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001074 final UidDetail detail = mUidDetailProvider.getUidDetail(app.appId, true);
1075 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001076 }
1077 };
1078
1079 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001080 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001081 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1082 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1083 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001084 // show cycle editor; will eventually call setPolicyCycleDay()
1085 // when user finishes editing.
1086 CycleEditorFragment.show(DataUsageSummary.this);
1087
1088 // reset spinner to something other than "change cycle..."
1089 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001090
1091 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001092 if (LOGD) {
1093 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1094 + cycle.end + "]");
1095 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001096
1097 // update chart to show selected cycle, and update detail data
1098 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001099 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001100
1101 updateDetailData();
1102 }
1103 }
1104
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001105 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001106 public void onNothingSelected(AdapterView<?> parent) {
1107 // ignored
1108 }
1109 };
1110
1111 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001112 * Update details based on {@link #mChart} inspection range depending on
1113 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1114 * of applications data usage, and when {@link #isAppDetailMode()} update
1115 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001116 */
1117 private void updateDetailData() {
1118 if (LOGD) Log.d(TAG, "updateDetailData()");
1119
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001120 final long start = mChart.getInspectStart();
1121 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001122 final long now = System.currentTimeMillis();
1123
1124 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001125
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001126 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001127 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001128 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001129 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001130 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001131 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001132 final long foregroundBytes = entry.rxBytes + entry.txBytes;
1133
1134 mAppPieChart.setOriginAngle(175);
1135
1136 mAppPieChart.removeAllSlices();
1137 mAppPieChart.addSlice(foregroundBytes, Color.parseColor("#d88d3a"));
1138 mAppPieChart.addSlice(defaultBytes, Color.parseColor("#666666"));
1139
1140 mAppPieChart.generatePath();
1141
1142 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1143 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1144
1145 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001146 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001147
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001148 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001149
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001150 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001151 if (mChartData != null) {
1152 entry = mChartData.network.getValues(start, end, now, null);
1153 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001154
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001155 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001156 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001157 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001158 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001159
1160 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1161 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001162 final String rangePhrase = formatDateRange(context, start, end);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001163
Jeff Sharkeye557c332012-04-13 16:04:07 -07001164 final int summaryRes;
1165 if (TAB_MOBILE.equals(mCurrentTab) || TAB_3G.equals(mCurrentApp)
1166 || TAB_4G.equals(mCurrentApp)) {
1167 summaryRes = R.string.data_usage_total_during_range_mobile;
1168 } else {
1169 summaryRes = R.string.data_usage_total_during_range;
1170 }
1171
1172 mUsageSummary.setText(getString(summaryRes, totalPhrase, rangePhrase));
Jeff Sharkey92811822012-05-04 11:47:29 -07001173
1174 // initial layout is finished above, ensure we have transitions
1175 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001176 }
1177
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001178 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1179 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001180 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001181 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001182 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001183 }
1184
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001185 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001186 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1187 mChartData = data;
1188 mChart.bindNetworkStats(mChartData.network);
1189 mChart.bindDetailNetworkStats(mChartData.detail);
1190
1191 // calcuate policy cycles based on available data
1192 updatePolicy(true);
1193 updateAppDetail();
1194
1195 // force scroll to top of body when showing detail
1196 if (mChartData.detail != null) {
1197 mListView.smoothScrollToPosition(0);
1198 }
1199 }
1200
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001201 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001202 public void onLoaderReset(Loader<ChartData> loader) {
1203 mChartData = null;
1204 mChart.bindNetworkStats(null);
1205 mChart.bindDetailNetworkStats(null);
1206 }
1207 };
1208
1209 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001210 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001211 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001212 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001213 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001214 }
1215
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001216 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001217 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001218 final int[] restrictedAppIds = mPolicyManager.getAppsWithPolicy(
1219 POLICY_REJECT_METERED_BACKGROUND);
1220 mAdapter.bindStats(data, restrictedAppIds);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001221 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001222 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001223
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001224 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001225 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001226 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001227 updateEmptyVisible();
1228 }
1229
1230 private void updateEmptyVisible() {
1231 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1232 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001233 }
1234 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001235
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001236 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001237 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001238 final Context context = getActivity();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001239 if (hasReadyMobileRadio(context)) {
1240 final TelephonyManager tele = TelephonyManager.from(context);
1241 return mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001242 } else {
1243 return false;
1244 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001245 }
1246
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001247 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001248 private void setMobilePolicySplit(boolean split) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001249 final Context context = getActivity();
1250 if (hasReadyMobileRadio(context)) {
1251 final TelephonyManager tele = TelephonyManager.from(context);
1252 mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split);
1253 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001254 }
1255
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001256 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001257 final TelephonyManager tele = TelephonyManager.from(context);
1258 final String actualSubscriberId = tele.getSubscriberId();
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -08001259 return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001260 }
1261
Jeff Sharkey8a503642011-06-10 13:31:21 -07001262 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001263 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001264 public void onInspectRangeChanged() {
1265 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
1266 updateDetailData();
1267 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001268
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001269 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001270 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001271 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001272 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001273
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001274 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001275 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001276 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001277 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001278
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001279 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001280 public void requestWarningEdit() {
1281 WarningEditorFragment.show(DataUsageSummary.this);
1282 }
1283
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001284 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001285 public void requestLimitEdit() {
1286 LimitEditorFragment.show(DataUsageSummary.this);
1287 }
1288 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001289
1290 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001291 * List item that reflects a specific data usage cycle.
1292 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001293 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001294 public CharSequence label;
1295 public long start;
1296 public long end;
1297
Jeff Sharkey8a503642011-06-10 13:31:21 -07001298 CycleItem(CharSequence label) {
1299 this.label = label;
1300 }
1301
1302 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001303 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001304 this.start = start;
1305 this.end = end;
1306 }
1307
Jeff Sharkey8a503642011-06-10 13:31:21 -07001308 @Override
1309 public String toString() {
1310 return label.toString();
1311 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001312
1313 @Override
1314 public boolean equals(Object o) {
1315 if (o instanceof CycleItem) {
1316 final CycleItem another = (CycleItem) o;
1317 return start == another.start && end == another.end;
1318 }
1319 return false;
1320 }
1321
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001322 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001323 public int compareTo(CycleItem another) {
1324 return Long.compare(start, another.start);
1325 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001326 }
1327
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001328 private static final StringBuilder sBuilder = new StringBuilder(50);
1329 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1330 sBuilder, Locale.getDefault());
1331
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001332 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001333 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001334
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001335 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001336 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001337 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1338 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001339 }
1340 }
1341
Jeff Sharkey8a503642011-06-10 13:31:21 -07001342 /**
1343 * Special-case data usage cycle that triggers dialog to change
1344 * {@link NetworkPolicy#cycleDay}.
1345 */
1346 public static class CycleChangeItem extends CycleItem {
1347 public CycleChangeItem(Context context) {
1348 super(context.getString(R.string.data_usage_change_cycle));
1349 }
1350 }
1351
1352 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001353 private boolean mChangePossible = false;
1354 private boolean mChangeVisible = false;
1355
1356 private final CycleChangeItem mChangeItem;
1357
Jeff Sharkey8a503642011-06-10 13:31:21 -07001358 public CycleAdapter(Context context) {
1359 super(context, android.R.layout.simple_spinner_item);
1360 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001361 mChangeItem = new CycleChangeItem(context);
1362 }
1363
1364 public void setChangePossible(boolean possible) {
1365 mChangePossible = possible;
1366 updateChange();
1367 }
1368
1369 public void setChangeVisible(boolean visible) {
1370 mChangeVisible = visible;
1371 updateChange();
1372 }
1373
1374 private void updateChange() {
1375 remove(mChangeItem);
1376 if (mChangePossible && mChangeVisible) {
1377 add(mChangeItem);
1378 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001379 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001380
1381 /**
1382 * Find position of {@link CycleItem} in this adapter which is nearest
1383 * the given {@link CycleItem}.
1384 */
1385 public int findNearestPosition(CycleItem target) {
1386 if (target != null) {
1387 final int count = getCount();
1388 for (int i = count - 1; i >= 0; i--) {
1389 final CycleItem item = getItem(i);
1390 if (item instanceof CycleChangeItem) {
1391 continue;
1392 } else if (item.compareTo(target) >= 0) {
1393 return i;
1394 }
1395 }
1396 }
1397 return 0;
1398 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001399 }
1400
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001401 public static class AppItem implements Comparable<AppItem>, Parcelable {
1402 public final int appId;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001403 public boolean restricted;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001404 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001405 public long total;
1406
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001407 public AppItem(int appId) {
1408 this.appId = appId;
1409 }
1410
1411 public AppItem(Parcel parcel) {
1412 appId = parcel.readInt();
1413 uids = parcel.readSparseBooleanArray();
1414 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001415 }
1416
1417 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001418 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001419 }
1420
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001421 @Override
1422 public void writeToParcel(Parcel dest, int flags) {
1423 dest.writeInt(appId);
1424 dest.writeSparseBooleanArray(uids);
1425 dest.writeLong(total);
1426 }
1427
1428 @Override
1429 public int describeContents() {
1430 return 0;
1431 }
1432
1433 @Override
1434 public int compareTo(AppItem another) {
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001435 return Long.compare(another.total, total);
1436 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001437
1438 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1439 @Override
1440 public AppItem createFromParcel(Parcel in) {
1441 return new AppItem(in);
1442 }
1443
1444 @Override
1445 public AppItem[] newArray(int size) {
1446 return new AppItem[size];
1447 }
1448 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001449 }
1450
Jeff Sharkey8a503642011-06-10 13:31:21 -07001451 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001452 * Adapter of applications, sorted by total usage descending.
1453 */
1454 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001455 private final UidDetailProvider mProvider;
1456 private final int mInsetSide;
1457
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001458 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001459 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001460
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001461 public DataUsageAdapter(UidDetailProvider provider, int insetSide) {
1462 mProvider = checkNotNull(provider);
1463 mInsetSide = insetSide;
1464 }
1465
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001466 /**
1467 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1468 */
Jeff Sharkeye557c332012-04-13 16:04:07 -07001469 public void bindStats(NetworkStats stats, int[] restrictedAppIds) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001470 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001471
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001472 final AppItem systemItem = new AppItem(android.os.Process.SYSTEM_UID);
1473 final SparseArray<AppItem> knownUids = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001474
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001475 NetworkStats.Entry entry = null;
1476 final int size = stats != null ? stats.size() : 0;
1477 for (int i = 0; i < size; i++) {
1478 entry = stats.getValues(i, entry);
1479
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001480 final boolean isApp = UserId.isApp(entry.uid);
1481 final int appId = isApp ? UserId.getAppId(entry.uid) : entry.uid;
1482 if (isApp || appId == UID_REMOVED || appId == UID_TETHERING) {
1483 AppItem item = knownUids.get(appId);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001484 if (item == null) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001485 item = new AppItem(appId);
1486 knownUids.put(appId, item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001487 mItems.add(item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001488 }
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001489
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001490 item.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001491 item.addUid(entry.uid);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001492 } else {
1493 systemItem.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001494 systemItem.addUid(entry.uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001495 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001496 }
1497
Jeff Sharkeye557c332012-04-13 16:04:07 -07001498 for (int appId : restrictedAppIds) {
1499 AppItem item = knownUids.get(appId);
1500 if (item == null) {
1501 item = new AppItem(appId);
1502 item.total = -1;
1503 mItems.add(item);
1504 }
1505 item.restricted = true;
1506 }
1507
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001508 if (systemItem.total > 0) {
1509 mItems.add(systemItem);
1510 }
1511
Jeff Sharkey8a503642011-06-10 13:31:21 -07001512 Collections.sort(mItems);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001513 mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001514 notifyDataSetChanged();
1515 }
1516
1517 @Override
1518 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001519 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001520 }
1521
1522 @Override
1523 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001524 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001525 }
1526
1527 @Override
1528 public long getItemId(int position) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001529 return mItems.get(position).appId;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001530 }
1531
1532 @Override
1533 public View getView(int position, View convertView, ViewGroup parent) {
1534 if (convertView == null) {
1535 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey5d706792011-09-08 18:57:17 -07001536 R.layout.data_usage_item, parent, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001537
1538 if (mInsetSide > 0) {
1539 convertView.setPadding(mInsetSide, 0, mInsetSide, 0);
1540 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001541 }
1542
1543 final Context context = parent.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001544
Jeff Sharkey28130d92011-09-02 16:10:24 -07001545 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001546 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1547 android.R.id.progress);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001548
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001549 // kick off async load of app details
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001550 final AppItem item = mItems.get(position);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001551 UidDetailTask.bindView(mProvider, item, convertView);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001552
Jeff Sharkeye557c332012-04-13 16:04:07 -07001553 if (item.restricted && item.total <= 0) {
1554 text1.setText(R.string.data_usage_app_restricted);
1555 progress.setVisibility(View.GONE);
1556 } else {
1557 text1.setText(Formatter.formatFileSize(context, item.total));
1558 progress.setVisibility(View.VISIBLE);
1559 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001560
1561 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1562 progress.setProgress(percentTotal);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001563
1564 return convertView;
1565 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001566 }
1567
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001568 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001569 * Empty {@link Fragment} that controls display of UID details in
1570 * {@link DataUsageSummary}.
1571 */
1572 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001573 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001574
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001575 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001576 if (!parent.isAdded()) return;
1577
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001578 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001579 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001580
1581 final AppDetailsFragment fragment = new AppDetailsFragment();
1582 fragment.setArguments(args);
1583 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001584 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1585 ft.add(fragment, TAG_APP_DETAILS);
1586 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001587 ft.setBreadCrumbTitle(label);
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001588 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001589 }
1590
1591 @Override
1592 public void onStart() {
1593 super.onStart();
1594 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001595 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001596 target.updateBody();
1597 }
1598
1599 @Override
1600 public void onStop() {
1601 super.onStop();
1602 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001603 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001604 target.updateBody();
1605 }
1606 }
1607
1608 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001609 * Dialog to request user confirmation before setting
1610 * {@link NetworkPolicy#limitBytes}.
1611 */
1612 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001613 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001614 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001615
1616 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001617 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001618
Jeff Sharkey461842a2011-09-25 18:22:48 -07001619 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001620 final CharSequence message;
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001621 final long minLimitBytes = (long) (
1622 parent.mPolicyEditor.getPolicy(parent.mTemplate).warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001623 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001624
1625 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001626 final String currentTab = parent.mCurrentTab;
1627 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001628 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001629 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001630 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001631 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001632 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001633 } else if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001634 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001635 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001636 } else {
1637 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001638 }
1639
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001640 final Bundle args = new Bundle();
1641 args.putCharSequence(EXTRA_MESSAGE, message);
1642 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1643
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001644 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1645 dialog.setArguments(args);
1646 dialog.setTargetFragment(parent, 0);
1647 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1648 }
1649
1650 @Override
1651 public Dialog onCreateDialog(Bundle savedInstanceState) {
1652 final Context context = getActivity();
1653
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001654 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001655 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1656
1657 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1658 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001659 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001660
1661 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001662 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001663 public void onClick(DialogInterface dialog, int which) {
1664 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1665 if (target != null) {
1666 target.setPolicyLimitBytes(limitBytes);
1667 }
1668 }
1669 });
1670
1671 return builder.create();
1672 }
1673 }
1674
1675 /**
1676 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1677 */
1678 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001679 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001680
1681 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001682 if (!parent.isAdded()) return;
1683
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001684 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001685 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001686
1687 final CycleEditorFragment dialog = new CycleEditorFragment();
1688 dialog.setArguments(args);
1689 dialog.setTargetFragment(parent, 0);
1690 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1691 }
1692
1693 @Override
1694 public Dialog onCreateDialog(Bundle savedInstanceState) {
1695 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001696 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1697 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001698
1699 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1700 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1701
1702 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1703 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1704
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001705 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1706 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001707
1708 cycleDayPicker.setMinValue(1);
1709 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001710 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001711 cycleDayPicker.setWrapSelectorWheel(true);
1712
1713 builder.setTitle(R.string.data_usage_cycle_editor_title);
1714 builder.setView(view);
1715
1716 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1717 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001718 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001719 public void onClick(DialogInterface dialog, int which) {
1720 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001721 final String cycleTimezone = new Time().timezone;
1722 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001723 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001724 }
1725 });
1726
1727 return builder.create();
1728 }
1729 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001730
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001731 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001732 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1733 */
1734 public static class WarningEditorFragment extends DialogFragment {
1735 private static final String EXTRA_TEMPLATE = "template";
1736
1737 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001738 if (!parent.isAdded()) return;
1739
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001740 final Bundle args = new Bundle();
1741 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1742
1743 final WarningEditorFragment dialog = new WarningEditorFragment();
1744 dialog.setArguments(args);
1745 dialog.setTargetFragment(parent, 0);
1746 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1747 }
1748
1749 @Override
1750 public Dialog onCreateDialog(Bundle savedInstanceState) {
1751 final Context context = getActivity();
1752 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1753 final NetworkPolicyEditor editor = target.mPolicyEditor;
1754
1755 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1756 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1757
1758 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1759 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1760
1761 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1762 final long warningBytes = editor.getPolicyWarningBytes(template);
1763 final long limitBytes = editor.getPolicyLimitBytes(template);
1764
1765 bytesPicker.setMinValue(0);
1766 if (limitBytes != LIMIT_DISABLED) {
1767 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1768 } else {
1769 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1770 }
1771 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1772 bytesPicker.setWrapSelectorWheel(false);
1773
1774 builder.setTitle(R.string.data_usage_warning_editor_title);
1775 builder.setView(view);
1776
1777 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1778 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001779 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001780 public void onClick(DialogInterface dialog, int which) {
1781 // clear focus to finish pending text edits
1782 bytesPicker.clearFocus();
1783
1784 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1785 editor.setPolicyWarningBytes(template, bytes);
1786 target.updatePolicy(false);
1787 }
1788 });
1789
1790 return builder.create();
1791 }
1792 }
1793
1794 /**
1795 * Dialog to edit {@link NetworkPolicy#limitBytes}.
1796 */
1797 public static class LimitEditorFragment extends DialogFragment {
1798 private static final String EXTRA_TEMPLATE = "template";
1799
1800 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001801 if (!parent.isAdded()) return;
1802
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001803 final Bundle args = new Bundle();
1804 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1805
1806 final LimitEditorFragment dialog = new LimitEditorFragment();
1807 dialog.setArguments(args);
1808 dialog.setTargetFragment(parent, 0);
1809 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
1810 }
1811
1812 @Override
1813 public Dialog onCreateDialog(Bundle savedInstanceState) {
1814 final Context context = getActivity();
1815 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1816 final NetworkPolicyEditor editor = target.mPolicyEditor;
1817
1818 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1819 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1820
1821 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1822 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1823
1824 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1825 final long warningBytes = editor.getPolicyWarningBytes(template);
1826 final long limitBytes = editor.getPolicyLimitBytes(template);
1827
1828 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09001829 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001830 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
1831 } else {
1832 bytesPicker.setMinValue(0);
1833 }
1834 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
1835 bytesPicker.setWrapSelectorWheel(false);
1836
1837 builder.setTitle(R.string.data_usage_limit_editor_title);
1838 builder.setView(view);
1839
1840 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1841 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001842 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001843 public void onClick(DialogInterface dialog, int which) {
1844 // clear focus to finish pending text edits
1845 bytesPicker.clearFocus();
1846
1847 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1848 editor.setPolicyLimitBytes(template, bytes);
1849 target.updatePolicy(false);
1850 }
1851 });
1852
1853 return builder.create();
1854 }
1855 }
1856 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07001857 * Dialog to request user confirmation before disabling data.
1858 */
1859 public static class ConfirmDataDisableFragment extends DialogFragment {
1860 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001861 if (!parent.isAdded()) return;
1862
Jeff Sharkey28130d92011-09-02 16:10:24 -07001863 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
1864 dialog.setTargetFragment(parent, 0);
1865 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
1866 }
1867
1868 @Override
1869 public Dialog onCreateDialog(Bundle savedInstanceState) {
1870 final Context context = getActivity();
1871
1872 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1873 builder.setMessage(R.string.data_usage_disable_mobile);
1874
1875 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001876 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07001877 public void onClick(DialogInterface dialog, int which) {
1878 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1879 if (target != null) {
1880 // TODO: extend to modify policy enabled flag.
1881 target.setMobileDataEnabled(false);
1882 }
1883 }
1884 });
1885 builder.setNegativeButton(android.R.string.cancel, null);
1886
1887 return builder.create();
1888 }
1889 }
1890
1891 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001892 * Dialog to request user confirmation before setting
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001893 * {@link android.provider.Settings.Secure#DATA_ROAMING}.
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001894 */
1895 public static class ConfirmDataRoamingFragment extends DialogFragment {
1896 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001897 if (!parent.isAdded()) return;
1898
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001899 final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment();
1900 dialog.setTargetFragment(parent, 0);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001901 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_ROAMING);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001902 }
1903
1904 @Override
1905 public Dialog onCreateDialog(Bundle savedInstanceState) {
1906 final Context context = getActivity();
1907
1908 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1909 builder.setTitle(R.string.roaming_reenable_title);
1910 builder.setMessage(R.string.roaming_warning);
1911
1912 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001913 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001914 public void onClick(DialogInterface dialog, int which) {
1915 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1916 if (target != null) {
1917 target.setDataRoaming(true);
1918 }
1919 }
1920 });
1921 builder.setNegativeButton(android.R.string.cancel, null);
1922
1923 return builder.create();
1924 }
1925 }
1926
1927 /**
1928 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001929 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001930 */
1931 public static class ConfirmRestrictFragment extends DialogFragment {
1932 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001933 if (!parent.isAdded()) return;
1934
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001935 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1936 dialog.setTargetFragment(parent, 0);
1937 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1938 }
1939
1940 @Override
1941 public Dialog onCreateDialog(Bundle savedInstanceState) {
1942 final Context context = getActivity();
1943
1944 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001945 builder.setTitle(R.string.data_usage_restrict_background_title);
Jeff Sharkey461842a2011-09-25 18:22:48 -07001946 builder.setMessage(getString(R.string.data_usage_restrict_background));
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001947
1948 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001949 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001950 public void onClick(DialogInterface dialog, int which) {
1951 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1952 if (target != null) {
1953 target.setRestrictBackground(true);
1954 }
1955 }
1956 });
1957 builder.setNegativeButton(android.R.string.cancel, null);
1958
1959 return builder.create();
1960 }
1961 }
1962
1963 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001964 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
1965 * change has been denied, usually based on
1966 * {@link DataUsageSummary#hasLimitedNetworks()}.
1967 */
1968 public static class DeniedRestrictFragment extends DialogFragment {
1969 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001970 if (!parent.isAdded()) return;
1971
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001972 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
1973 dialog.setTargetFragment(parent, 0);
1974 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
1975 }
1976
1977 @Override
1978 public Dialog onCreateDialog(Bundle savedInstanceState) {
1979 final Context context = getActivity();
1980
1981 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1982 builder.setTitle(R.string.data_usage_app_restrict_background);
1983 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
1984 builder.setPositiveButton(android.R.string.ok, null);
1985
1986 return builder.create();
1987 }
1988 }
1989
1990 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001991 * Dialog to request user confirmation before setting
1992 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
1993 */
1994 public static class ConfirmAppRestrictFragment extends DialogFragment {
1995 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001996 if (!parent.isAdded()) return;
1997
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001998 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
1999 dialog.setTargetFragment(parent, 0);
2000 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2001 }
2002
2003 @Override
2004 public Dialog onCreateDialog(Bundle savedInstanceState) {
2005 final Context context = getActivity();
2006
2007 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002008 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2009 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2010
2011 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002012 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002013 public void onClick(DialogInterface dialog, int which) {
2014 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2015 if (target != null) {
2016 target.setAppRestrictBackground(true);
2017 }
2018 }
2019 });
2020 builder.setNegativeButton(android.R.string.cancel, null);
2021
2022 return builder.create();
2023 }
2024 }
2025
2026 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002027 * Compute default tab that should be selected, based on
2028 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2029 */
2030 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002031 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
2032 if (template == null) return null;
2033
2034 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002035 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002036 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002037 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002038 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002039 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002040 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002041 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002042 return TAB_WIFI;
2043 default:
2044 return null;
2045 }
2046 }
2047
2048 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002049 * Background task that loads {@link UidDetail}, binding to
2050 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002051 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002052 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2053 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002054 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002055 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002056
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002057 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002058 mProvider = checkNotNull(provider);
2059 mItem = checkNotNull(item);
2060 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002061 }
2062
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002063 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002064 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002065 final UidDetailTask existing = (UidDetailTask) target.getTag();
2066 if (existing != null) {
2067 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002068 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002069
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002070 final UidDetail cachedDetail = provider.getUidDetail(item.appId, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002071 if (cachedDetail != null) {
2072 bindView(cachedDetail, target);
2073 } else {
2074 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2075 AsyncTask.THREAD_POOL_EXECUTOR));
2076 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002077 }
2078
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002079 private static void bindView(UidDetail detail, View target) {
2080 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2081 final TextView title = (TextView) target.findViewById(android.R.id.title);
2082
2083 if (detail != null) {
2084 icon.setImageDrawable(detail.icon);
2085 title.setText(detail.label);
2086 } else {
2087 icon.setImageDrawable(null);
2088 title.setText(null);
2089 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002090 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002091
2092 @Override
2093 protected void onPreExecute() {
2094 bindView(null, mTarget);
2095 }
2096
2097 @Override
2098 protected UidDetail doInBackground(Void... params) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002099 return mProvider.getUidDetail(mItem.appId, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002100 }
2101
2102 @Override
2103 protected void onPostExecute(UidDetail result) {
2104 bindView(result, mTarget);
2105 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002106 }
2107
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002108 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002109 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002110 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002111 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002112 if (TEST_RADIOS) {
2113 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2114 }
2115
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002116 final ConnectivityManager conn = ConnectivityManager.from(context);
2117 final TelephonyManager tele = TelephonyManager.from(context);
2118
2119 // require both supported network and ready SIM
2120 return conn.isNetworkSupported(TYPE_MOBILE) && tele.getSimState() == SIM_STATE_READY;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002121 }
2122
2123 /**
2124 * Test if device has a mobile 4G data radio.
2125 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002126 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002127 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2128 return false;
2129 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002130 if (TEST_RADIOS) {
2131 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2132 }
2133
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002134 final ConnectivityManager conn = ConnectivityManager.from(context);
2135 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002136
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002137 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002138 final boolean hasLte = (tele.getLteOnCdmaMode() == Phone.LTE_ON_CDMA_TRUE)
2139 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002140 return hasWimax || hasLte;
2141 }
2142
2143 /**
2144 * Test if device has a Wi-Fi data radio.
2145 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002146 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002147 if (TEST_RADIOS) {
2148 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2149 }
2150
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002151 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002152 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002153 }
2154
2155 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002156 * Test if device has an ethernet network connection.
2157 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002158 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002159 if (TEST_RADIOS) {
2160 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2161 }
2162
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002163 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002164 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2165
2166 final long ethernetBytes;
2167 try {
2168 ethernetBytes = mStatsSession.getSummaryForNetwork(
2169 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2170 .getTotalBytes();
2171 } catch (RemoteException e) {
2172 throw new RuntimeException(e);
2173 }
2174
2175 // suppress ethernet unless traffic has occurred
2176 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002177 }
2178
2179 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002180 * Inflate a {@link Preference} style layout, adding the given {@link View}
2181 * widget into {@link android.R.id#widget_frame}.
2182 */
2183 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2184 final View view = inflater.inflate(R.layout.preference, root, false);
2185 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2186 android.R.id.widget_frame);
2187 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2188 return view;
2189 }
2190
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002191 private static View inflateAppTitle(
2192 LayoutInflater inflater, ViewGroup root, CharSequence label) {
2193 final TextView view = (TextView) inflater.inflate(
2194 R.layout.data_usage_app_title, root, false);
2195 view.setText(label);
2196 return view;
2197 }
2198
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002199 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002200 * Test if any networks are currently limited.
2201 */
2202 private boolean hasLimitedNetworks() {
2203 return !buildLimitedNetworksList().isEmpty();
2204 }
2205
2206 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002207 * Build string describing currently limited networks, which defines when
2208 * background data is restricted.
2209 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002210 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002211 private CharSequence buildLimitedNetworksString() {
2212 final List<CharSequence> limited = buildLimitedNetworksList();
2213
2214 // handle case where no networks limited
2215 if (limited.isEmpty()) {
2216 limited.add(getText(R.string.data_usage_list_none));
2217 }
2218
2219 return TextUtils.join(limited);
2220 }
2221
2222 /**
2223 * Build list of currently limited networks, which defines when background
2224 * data is restricted.
2225 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002226 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002227 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002228 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002229
2230 // build combined list of all limited networks
2231 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002232
2233 final TelephonyManager tele = TelephonyManager.from(context);
2234 if (tele.getSimState() == SIM_STATE_READY) {
2235 final String subscriberId = getActiveSubscriberId(context);
2236 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2237 limited.add(getText(R.string.data_usage_list_mobile));
2238 }
2239 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2240 limited.add(getText(R.string.data_usage_tab_3g));
2241 }
2242 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2243 limited.add(getText(R.string.data_usage_tab_4g));
2244 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002245 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002246
2247 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002248 limited.add(getText(R.string.data_usage_tab_wifi));
2249 }
2250 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2251 limited.add(getText(R.string.data_usage_tab_ethernet));
2252 }
2253
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002254 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002255 }
2256
2257 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002258 * Inset both selector and divider {@link Drawable} on the given
2259 * {@link ListView} by the requested dimensions.
2260 */
2261 private static void insetListViewDrawables(ListView view, int insetSide) {
2262 final Drawable selector = view.getSelector();
2263 final Drawable divider = view.getDivider();
2264
2265 // fully unregister these drawables so callbacks can be maintained after
2266 // wrapping below.
2267 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2268 view.setSelector(stub);
2269 view.setDivider(stub);
2270
2271 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2272 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2273 }
2274
2275 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002276 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002277 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002278 */
2279 private static void setPreferenceTitle(View parent, int resId) {
2280 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2281 title.setText(resId);
2282 }
2283
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002284 /**
2285 * Set {@link android.R.id#summary} for a preference view inflated with
2286 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2287 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002288 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002289 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2290 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002291 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002292 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002293}