blob: 4a2c26255905522b6a5410f166bd5dc746f0d51b [file] [log] [blame]
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070019import static android.net.ConnectivityManager.TYPE_ETHERNET;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070020import static android.net.ConnectivityManager.TYPE_MOBILE;
Jeff Sharkeybdf98e82011-11-10 17:17:24 -080021import static android.net.ConnectivityManager.TYPE_WIFI;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070022import static android.net.ConnectivityManager.TYPE_WIMAX;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070023import static android.net.NetworkPolicy.LIMIT_DISABLED;
Jeff Sharkeya53188f2011-09-13 19:56:45 -070024import static android.net.NetworkPolicy.WARNING_DISABLED;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070025import static android.net.NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070026import static android.net.NetworkPolicyManager.POLICY_NONE;
27import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND;
Jeff Sharkey8a503642011-06-10 13:31:21 -070028import static android.net.NetworkPolicyManager.computeLastCycleBoundary;
29import static android.net.NetworkPolicyManager.computeNextCycleBoundary;
Jeff Sharkeya662e492011-06-18 21:57:06 -070030import static android.net.NetworkTemplate.MATCH_MOBILE_3G_LOWER;
31import static android.net.NetworkTemplate.MATCH_MOBILE_4G;
32import static android.net.NetworkTemplate.MATCH_MOBILE_ALL;
33import static android.net.NetworkTemplate.MATCH_WIFI;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070034import static android.net.NetworkTemplate.buildTemplateEthernet;
35import static android.net.NetworkTemplate.buildTemplateMobile3gLower;
36import static android.net.NetworkTemplate.buildTemplateMobile4g;
37import static android.net.NetworkTemplate.buildTemplateMobileAll;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070038import static android.net.NetworkTemplate.buildTemplateWifiWildcard;
Jeff Sharkey77dae912012-02-03 14:50:33 -080039import static android.net.TrafficStats.GB_IN_BYTES;
40import static android.net.TrafficStats.MB_IN_BYTES;
Jeff Sharkeya83a24f2011-09-16 01:52:39 -070041import static android.net.TrafficStats.UID_REMOVED;
42import static android.net.TrafficStats.UID_TETHERING;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070043import static android.telephony.TelephonyManager.SIM_STATE_READY;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070044import static android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
45import static android.text.format.DateUtils.FORMAT_SHOW_DATE;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070046import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -070047import static com.android.internal.util.Preconditions.checkNotNull;
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -070048import static com.android.settings.Utils.prepareCustomPreferencesList;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070049
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070050import android.animation.LayoutTransition;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070051import android.app.AlertDialog;
52import android.app.Dialog;
53import android.app.DialogFragment;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070054import android.app.Fragment;
Amith Yamasani5ba0a022011-11-07 12:29:00 -080055import android.app.FragmentManager;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070056import android.app.FragmentTransaction;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070057import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070058import android.content.ContentResolver;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070059import android.content.Context;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070060import android.content.DialogInterface;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -070061import android.content.Intent;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070062import android.content.Loader;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070063import android.content.SharedPreferences;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070064import android.content.pm.PackageManager;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070065import android.content.res.Resources;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070066import android.graphics.Color;
Jeff Sharkey5d706792011-09-08 18:57:17 -070067import android.graphics.drawable.ColorDrawable;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070068import android.graphics.drawable.Drawable;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070069import android.net.ConnectivityManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070070import android.net.INetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070071import android.net.INetworkStatsService;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070072import android.net.INetworkStatsSession;
Jeff Sharkey8a503642011-06-10 13:31:21 -070073import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070074import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070075import android.net.NetworkStats;
76import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070077import android.net.NetworkTemplate;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070078import android.net.TrafficStats;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070079import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070080import android.os.Bundle;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070081import android.os.INetworkManagementService;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070082import android.os.Parcel;
83import android.os.Parcelable;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070084import android.os.RemoteException;
85import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070086import android.os.SystemProperties;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070087import android.os.UserId;
Jeff Sharkey8a503642011-06-10 13:31:21 -070088import android.preference.Preference;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070089import android.preference.PreferenceActivity;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070090import android.provider.Settings;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070091import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070092import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070093import android.text.format.DateUtils;
94import android.text.format.Formatter;
Jeff Sharkeye5223a02012-03-09 17:11:14 -080095import android.text.format.Time;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070096import android.util.Log;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070097import android.util.SparseArray;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070098import android.util.SparseBooleanArray;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070099import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700100import android.view.Menu;
101import android.view.MenuInflater;
102import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700103import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700104import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700105import android.view.ViewGroup;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700106import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700107import android.widget.AdapterView;
108import android.widget.AdapterView.OnItemClickListener;
109import android.widget.AdapterView.OnItemSelectedListener;
110import android.widget.ArrayAdapter;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700111import android.widget.BaseAdapter;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700112import android.widget.Button;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700113import android.widget.CheckBox;
114import android.widget.CompoundButton;
115import android.widget.CompoundButton.OnCheckedChangeListener;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700116import android.widget.ImageView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700117import android.widget.LinearLayout;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700118import android.widget.ListView;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700119import android.widget.NumberPicker;
Jeff Sharkey2412b0f2011-07-17 20:31:40 -0700120import android.widget.ProgressBar;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700121import android.widget.Spinner;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700122import android.widget.Switch;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700123import android.widget.TabHost;
124import android.widget.TabHost.OnTabChangeListener;
125import android.widget.TabHost.TabContentFactory;
126import android.widget.TabHost.TabSpec;
127import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700128import android.widget.TextView;
129
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700130import com.android.internal.telephony.Phone;
Jeff Sharkey5d706792011-09-08 18:57:17 -0700131import com.android.settings.drawable.InsetBoundsDrawable;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700132import com.android.settings.net.ChartData;
133import com.android.settings.net.ChartDataLoader;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700134import com.android.settings.net.DataUsageMeteredSettings;
Jeff Sharkeya662e492011-06-18 21:57:06 -0700135import com.android.settings.net.NetworkPolicyEditor;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700136import com.android.settings.net.SummaryForAllUidLoader;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700137import com.android.settings.net.UidDetail;
138import com.android.settings.net.UidDetailProvider;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700139import com.android.settings.widget.ChartDataUsageView;
140import com.android.settings.widget.ChartDataUsageView.DataUsageChartListener;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700141import com.android.settings.widget.PieChartView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700142import com.google.android.collect.Lists;
143
144import java.util.ArrayList;
145import java.util.Collections;
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700146import java.util.List;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700147import java.util.Locale;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700148
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700149import libcore.util.Objects;
150
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700151/**
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700152 * Panel showing data usage history across various networks, including options
153 * to inspect based on usage cycle and control through {@link NetworkPolicy}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700154 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700155public class DataUsageSummary extends Fragment {
156 private static final String TAG = "DataUsage";
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800157 private static final boolean LOGD = false;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700158
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700159 // TODO: remove this testing code
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700160 private static final boolean TEST_ANIM = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700161 private static final boolean TEST_RADIOS = false;
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800162
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700163 private static final String TEST_RADIOS_PROP = "test.radios";
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800164 private static final String TEST_SUBSCRIBER_PROP = "test.subscriberid";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700165
Jeff Sharkey8a503642011-06-10 13:31:21 -0700166 private static final String TAB_3G = "3g";
167 private static final String TAB_4G = "4g";
168 private static final String TAB_MOBILE = "mobile";
169 private static final String TAB_WIFI = "wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700170 private static final String TAB_ETHERNET = "ethernet";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700171
Jeff Sharkey28130d92011-09-02 16:10:24 -0700172 private static final String TAG_CONFIRM_DATA_DISABLE = "confirmDataDisable";
173 private static final String TAG_CONFIRM_DATA_ROAMING = "confirmDataRoaming";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700174 private static final String TAG_CONFIRM_LIMIT = "confirmLimit";
175 private static final String TAG_CYCLE_EDITOR = "cycleEditor";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700176 private static final String TAG_WARNING_EDITOR = "warningEditor";
177 private static final String TAG_LIMIT_EDITOR = "limitEditor";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700178 private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700179 private static final String TAG_DENIED_RESTRICT = "deniedRestrict";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700180 private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700181 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700182
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700183 private static final int LOADER_CHART_DATA = 2;
184 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700185
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700186 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700187 private INetworkStatsService mStatsService;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700188 private NetworkPolicyManager mPolicyManager;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700189 private ConnectivityManager mConnService;
190
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700191 private INetworkStatsSession mStatsSession;
192
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700193 private static final String PREF_FILE = "data_usage";
194 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700195 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700196
197 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700198
Jeff Sharkey8a503642011-06-10 13:31:21 -0700199 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700200 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700201 private TabWidget mTabWidget;
202 private ListView mListView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700203 private DataUsageAdapter mAdapter;
204
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700205 /** Distance to inset content from sides, when needed. */
206 private int mInsetSide = 0;
207
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700208 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700209
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700210 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700211 private LinearLayout mNetworkSwitches;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700212 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700213 private View mDataEnabledView;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700214 private CheckBox mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700215 private View mDisableAtLimitView;
216
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700217 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700218 private Spinner mCycleSpinner;
219 private CycleAdapter mCycleAdapter;
220
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700221 private ChartDataUsageView mChart;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700222 private TextView mUsageSummary;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700223 private TextView mEmpty;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700224
225 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700226 private ImageView mAppIcon;
227 private ViewGroup mAppTitles;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700228 private PieChartView mAppPieChart;
229 private TextView mAppForeground;
230 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700231 private Button mAppSettings;
232
233 private LinearLayout mAppSwitches;
234 private CheckBox mAppRestrict;
235 private View mAppRestrictView;
236
Jeff Sharkey8a503642011-06-10 13:31:21 -0700237 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700238 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700239
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700240 private NetworkTemplate mTemplate;
241 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700242
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700243 private AppItem mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700244
245 private Intent mAppSettingsIntent;
246
Jeff Sharkeya662e492011-06-18 21:57:06 -0700247 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700248
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700249 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700250 private String mIntentTab = null;
251
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700252 private MenuItem mMenuDataRoaming;
253 private MenuItem mMenuRestrictBackground;
254
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700255 /** Flag used to ignore listeners during binding. */
256 private boolean mBinding;
257
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700258 private UidDetailProvider mUidDetailProvider;
259
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700260 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700261 public void onCreate(Bundle savedInstanceState) {
262 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700263 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700264
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700265 mNetworkService = INetworkManagementService.Stub.asInterface(
266 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700267 mStatsService = INetworkStatsService.Stub.asInterface(
268 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700269 mPolicyManager = NetworkPolicyManager.from(context);
270 mConnService = ConnectivityManager.from(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700271
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700272 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700273
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700274 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700275 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700276
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700277 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700278 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700279
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700280 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700281 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700282
Jeff Sharkey8a503642011-06-10 13:31:21 -0700283 @Override
284 public View onCreateView(LayoutInflater inflater, ViewGroup container,
285 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700286
Jeff Sharkey8a503642011-06-10 13:31:21 -0700287 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700288 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
289
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700290 mUidDetailProvider = new UidDetailProvider(context);
291
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700292 try {
293 mStatsSession = mStatsService.openSession();
294 } catch (RemoteException e) {
295 throw new RuntimeException(e);
296 }
297
Jeff Sharkey8a503642011-06-10 13:31:21 -0700298 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700299 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700300 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
301 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700302
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700303 // decide if we need to manually inset our content, or if we should rely
304 // on parent container for inset.
305 final boolean shouldInset = mListView.getScrollBarStyle()
306 == View.SCROLLBARS_OUTSIDE_OVERLAY;
307 if (shouldInset) {
308 mInsetSide = view.getResources().getDimensionPixelOffset(
309 com.android.internal.R.dimen.preference_fragment_padding_side);
310 } else {
311 mInsetSide = 0;
312 }
313
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700314 // adjust padding around tabwidget as needed
Jeff Sharkey5d706792011-09-08 18:57:17 -0700315 prepareCustomPreferencesList(container, view, mListView, true);
316
Jeff Sharkey8a503642011-06-10 13:31:21 -0700317 mTabHost.setup();
318 mTabHost.setOnTabChangedListener(mTabListener);
319
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700320 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700321 mHeader.setClickable(true);
322
323 mListView.addHeaderView(mHeader, null, true);
324 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700325
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700326 if (mInsetSide > 0) {
327 // inset selector and divider drawables
328 insetListViewDrawables(mListView, mInsetSide);
329 mHeader.setPadding(mInsetSide, 0, mInsetSide, 0);
330 }
331
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700332 {
333 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700334 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
335 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700336 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700337
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700338 mDataEnabled = new Switch(inflater.getContext());
339 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
340 mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener);
341 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700342
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700343 mDisableAtLimit = new CheckBox(inflater.getContext());
344 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700345 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700346 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700347 mDisableAtLimitView.setClickable(true);
348 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700349 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
350 mNetworkSwitches.addView(mDisableAtLimitView);
351 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700352
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700353 // bind cycle dropdown
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700354 mCycleView = mHeader.findViewById(R.id.cycles);
355 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700356 mCycleAdapter = new CycleAdapter(context);
357 mCycleSpinner.setAdapter(mCycleAdapter);
358 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
359
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700360 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700361 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800362 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700363
364 {
365 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700366 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700367 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
368 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700369 mAppPieChart = (PieChartView) mAppDetail.findViewById(R.id.app_pie_chart);
370 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
371 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700372 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700373
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700374 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700375 mAppSettings.setOnClickListener(mAppSettingsListener);
376
377 mAppRestrict = new CheckBox(inflater.getContext());
378 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700379 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700380 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700381 mAppRestrictView.setClickable(true);
382 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700383 mAppRestrictView.setOnClickListener(mAppRestrictListener);
384 mAppSwitches.addView(mAppRestrictView);
385 }
386
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700387 mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700388 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700389
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700390 // only assign layout transitions once first layout is finished
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700391 mListView.getViewTreeObserver().addOnGlobalLayoutListener(mFirstLayoutListener);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700392
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700393 mAdapter = new DataUsageAdapter(mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700394 mListView.setOnItemClickListener(mListListener);
395 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700396
397 return view;
398 }
399
400 @Override
401 public void onResume() {
402 super.onResume();
403
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700404 // pick default tab based on incoming intent
405 final Intent intent = getActivity().getIntent();
406 mIntentTab = computeTabFromIntent(intent);
407
Jeff Sharkey8a503642011-06-10 13:31:21 -0700408 // this kicks off chain reaction which creates tabs, binds the body to
409 // selected network, and binds chart, cycles and detail list.
410 updateTabs();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700411
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700412 // kick off background task to update stats
413 new AsyncTask<Void, Void, Void>() {
414 @Override
415 protected Void doInBackground(Void... params) {
416 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700417 // wait a few seconds before kicking off
418 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700419 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700420 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700421 } catch (RemoteException e) {
422 }
423 return null;
424 }
425
426 @Override
427 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700428 if (isAdded()) {
429 updateBody();
430 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700431 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700432 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700433 }
434
Jeff Sharkey8a503642011-06-10 13:31:21 -0700435 @Override
436 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
437 inflater.inflate(R.menu.data_usage, menu);
438 }
439
440 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700441 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700442 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700443 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700444
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700445 mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700446 mMenuDataRoaming.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700447 mMenuDataRoaming.setChecked(getDataRoaming());
448
449 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700450 mMenuRestrictBackground.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
451 mMenuRestrictBackground.setChecked(mPolicyManager.getRestrictBackground());
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700452
453 final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g);
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700454 split4g.setVisible(hasReadyMobile4gRadio(context) && !appDetailMode);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700455 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700456
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700457 final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700458 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700459 showWifi.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700460 showWifi.setChecked(mShowWifi);
461 } else {
462 showWifi.setVisible(false);
463 mShowWifi = true;
464 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700465
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700466 final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700467 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700468 showEthernet.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700469 showEthernet.setChecked(mShowEthernet);
470 } else {
471 showEthernet.setVisible(false);
472 mShowEthernet = true;
473 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700474
475 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
476 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
477 metered.setVisible(!appDetailMode);
478 } else {
479 metered.setVisible(false);
480 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700481 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700482
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700483 @Override
484 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700485 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700486 case R.id.data_usage_menu_roaming: {
487 final boolean dataRoaming = !item.isChecked();
488 if (dataRoaming) {
489 ConfirmDataRoamingFragment.show(this);
490 } else {
491 // no confirmation to disable roaming
492 setDataRoaming(false);
493 }
494 return true;
495 }
496 case R.id.data_usage_menu_restrict_background: {
497 final boolean restrictBackground = !item.isChecked();
498 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800499 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700500 } else {
501 // no confirmation to drop restriction
502 setRestrictBackground(false);
503 }
504 return true;
505 }
506 case R.id.data_usage_menu_split_4g: {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700507 final boolean mobileSplit = !item.isChecked();
Jeff Sharkeya662e492011-06-18 21:57:06 -0700508 setMobilePolicySplit(mobileSplit);
509 item.setChecked(isMobilePolicySplit());
Jeff Sharkey8a503642011-06-10 13:31:21 -0700510 updateTabs();
511 return true;
512 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700513 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700514 mShowWifi = !item.isChecked();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700515 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700516 item.setChecked(mShowWifi);
517 updateTabs();
518 return true;
519 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700520 case R.id.data_usage_menu_show_ethernet: {
521 mShowEthernet = !item.isChecked();
522 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
523 item.setChecked(mShowEthernet);
524 updateTabs();
525 return true;
526 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700527 case R.id.data_usage_menu_metered: {
528 final PreferenceActivity activity = (PreferenceActivity) getActivity();
529 activity.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
530 R.string.data_usage_metered_title, null, this, 0);
531 return true;
532 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700533 }
534 return false;
535 }
536
Jeff Sharkey94a90952011-06-13 22:31:09 -0700537 @Override
538 public void onDestroyView() {
539 super.onDestroyView();
540
541 mDataEnabledView = null;
542 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700543
544 mUidDetailProvider.clearCache();
545 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700546
547 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700548 }
549
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800550 @Override
551 public void onDestroy() {
552 if (this.isRemoving()) {
553 getFragmentManager()
554 .popBackStack(TAG_APP_DETAILS, FragmentManager.POP_BACK_STACK_INCLUSIVE);
555 }
556 super.onDestroy();
557 }
558
Jeff Sharkey8a503642011-06-10 13:31:21 -0700559 /**
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700560 * Listener to setup {@link LayoutTransition} after first layout pass.
561 */
562 private OnGlobalLayoutListener mFirstLayoutListener = new OnGlobalLayoutListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700563 @Override
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700564 public void onGlobalLayout() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700565 mListView.getViewTreeObserver().removeOnGlobalLayoutListener(mFirstLayoutListener);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700566
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700567 mTabsContainer.setLayoutTransition(buildLayoutTransition());
568 mHeader.setLayoutTransition(buildLayoutTransition());
569 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700570
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700571 final LayoutTransition chartTransition = buildLayoutTransition();
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700572 chartTransition.setStartDelay(LayoutTransition.APPEARING, 0);
573 chartTransition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700574 chartTransition.setAnimator(LayoutTransition.APPEARING, null);
Jeff Sharkey28130d92011-09-02 16:10:24 -0700575 chartTransition.setAnimator(LayoutTransition.DISAPPEARING, null);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700576 mChart.setLayoutTransition(chartTransition);
577 }
578 };
579
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700580 private static LayoutTransition buildLayoutTransition() {
581 final LayoutTransition transition = new LayoutTransition();
582 if (TEST_ANIM) {
583 transition.setDuration(1500);
584 }
585 transition.setAnimateParentHierarchy(false);
586 return transition;
587 }
588
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700589 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700590 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700591 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
592 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700593 */
594 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700595 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700596 mTabHost.clearAllTabs();
597
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700598 final boolean mobileSplit = isMobilePolicySplit();
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700599 if (mobileSplit && hasReadyMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700600 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
601 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700602 } else if (hasReadyMobileRadio(context)) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700603 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700604 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700605 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700606 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
607 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700608 if (mShowEthernet && hasEthernet(context)) {
609 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
610 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700611
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700612 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700613 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
614 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
615 if (mIntentTab != null) {
616 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700617 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700618 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700619 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700620 mTabHost.setCurrentTabByTag(mIntentTab);
621 }
622 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700623 } else if (noTabs) {
624 // no usable tabs, so hide body
625 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700626 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700627 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700628 }
629 }
630
Jeff Sharkey8a503642011-06-10 13:31:21 -0700631 /**
632 * Factory that provide empty {@link View} to make {@link TabHost} happy.
633 */
634 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700635 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700636 public View createTabContent(String tag) {
637 return new View(mTabHost.getContext());
638 }
639 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700640
Jeff Sharkey8a503642011-06-10 13:31:21 -0700641 /**
642 * Build {@link TabSpec} with thin indicator, and empty content.
643 */
644 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700645 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
646 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700647 }
648
649 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700650 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700651 public void onTabChanged(String tabId) {
652 // user changed tab; update body
653 updateBody();
654 }
655 };
656
657 /**
658 * Update body content based on current tab. Loads
659 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
660 * binds them to visible controls.
661 */
662 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700663 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700664 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700665
Jeff Sharkeya662e492011-06-18 21:57:06 -0700666 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700667 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700668
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700669 if (currentTab == null) {
670 Log.w(TAG, "no tab selected; hiding body");
671 mListView.setVisibility(View.GONE);
672 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700673 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700674 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700675 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700676
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700677 final boolean tabChanged = !currentTab.equals(mCurrentTab);
678 mCurrentTab = currentTab;
679
Jeff Sharkey8a503642011-06-10 13:31:21 -0700680 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
681
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700682 mDataEnabledView.setVisibility(View.VISIBLE);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700683
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700684 // TODO: remove mobile tabs when SIM isn't ready
685 final TelephonyManager tele = TelephonyManager.from(context);
686
Jeff Sharkey8a503642011-06-10 13:31:21 -0700687 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700688 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
689 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700690 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700691
692 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700693 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
694 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
695 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700696 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700697
698 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700699 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
700 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
701 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700702 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700703
704 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700705 // wifi doesn't have any controls
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700706 mDataEnabledView.setVisibility(View.GONE);
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700707 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700708 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700709
710 } else if (TAB_ETHERNET.equals(currentTab)) {
711 // ethernet doesn't have any controls
712 mDataEnabledView.setVisibility(View.GONE);
713 mDisableAtLimitView.setVisibility(View.GONE);
714 mTemplate = buildTemplateEthernet();
715
716 } else {
717 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700718 }
719
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700720 // kick off loader for network history
721 // TODO: consider chaining two loaders together instead of reloading
722 // network history when showing app detail.
723 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700724 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700725
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700726 // detail mode can change visible menus, invalidate
727 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700728
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700729 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700730 }
731
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700732 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700733 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700734 }
735
736 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700737 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700738 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700739 */
740 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700741 final Context context = getActivity();
742 final PackageManager pm = context.getPackageManager();
743 final LayoutInflater inflater = getActivity().getLayoutInflater();
744
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700745 if (isAppDetailMode()) {
746 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700747 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700748 } else {
749 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700750 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700751
752 // hide detail stats when not in detail mode
753 mChart.bindDetailNetworkStats(null);
754 return;
755 }
756
757 // remove warning/limit sweeps while in detail mode
758 mChart.bindNetworkPolicy(null);
759
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700760 // show icon and all labels appearing under this app
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700761 final int appId = mCurrentApp.appId;
762 final UidDetail detail = mUidDetailProvider.getUidDetail(appId, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700763 mAppIcon.setImageDrawable(detail.icon);
764
765 mAppTitles.removeAllViews();
766 if (detail.detailLabels != null) {
767 for (CharSequence label : detail.detailLabels) {
768 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label));
769 }
770 } else {
771 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label));
772 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700773
774 // enable settings button when package provides it
775 // TODO: target torwards entire UID instead of just first package
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700776 final String[] packageNames = pm.getPackagesForUid(appId);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700777 if (packageNames != null && packageNames.length > 0) {
778 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
779 mAppSettingsIntent.setPackage(packageNames[0]);
780 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
781
782 final boolean matchFound = pm.resolveActivity(mAppSettingsIntent, 0) != null;
783 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -0700784 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700785
786 } else {
787 mAppSettingsIntent = null;
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700788 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700789 }
790
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700791 updateDetailData();
792
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700793 if (UserId.isApp(appId) && !mPolicyManager.getRestrictBackground()
794 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700795 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800796 setPreferenceSummary(mAppRestrictView,
797 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700798
799 mAppRestrictView.setVisibility(View.VISIBLE);
800 mAppRestrict.setChecked(getAppRestrictBackground());
801
802 } else {
803 mAppRestrictView.setVisibility(View.GONE);
804 }
805 }
806
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700807 private void setPolicyWarningBytes(long warningBytes) {
808 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700809 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700810 updatePolicy(false);
811 }
812
813 private void setPolicyLimitBytes(long limitBytes) {
814 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700815 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700816 updatePolicy(false);
817 }
818
Jeff Sharkey28130d92011-09-02 16:10:24 -0700819 /**
820 * Local cache of value, used to work around delay when
821 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
822 */
823 private Boolean mMobileDataEnabled;
824
825 private boolean isMobileDataEnabled() {
826 if (mMobileDataEnabled != null) {
827 // TODO: deprecate and remove this once enabled flag is on policy
828 return mMobileDataEnabled;
829 } else {
830 return mConnService.getMobileDataEnabled();
831 }
832 }
833
834 private void setMobileDataEnabled(boolean enabled) {
835 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
836 mConnService.setMobileDataEnabled(enabled);
837 mMobileDataEnabled = enabled;
838 updatePolicy(false);
839 }
840
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700841 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
842 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked();
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700843 }
844
845 private boolean isBandwidthControlEnabled() {
846 try {
847 return mNetworkService.isBandwidthControlEnabled();
848 } catch (RemoteException e) {
849 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
850 return false;
851 }
852 }
853
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700854 private boolean getDataRoaming() {
855 final ContentResolver resolver = getActivity().getContentResolver();
856 return Settings.Secure.getInt(resolver, Settings.Secure.DATA_ROAMING, 0) != 0;
857 }
858
859 private void setDataRoaming(boolean enabled) {
860 // TODO: teach telephony DataConnectionTracker to watch and apply
861 // updates when changed.
862 final ContentResolver resolver = getActivity().getContentResolver();
863 Settings.Secure.putInt(resolver, Settings.Secure.DATA_ROAMING, enabled ? 1 : 0);
864 mMenuDataRoaming.setChecked(enabled);
865 }
866
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700867 public void setRestrictBackground(boolean restrictBackground) {
868 mPolicyManager.setRestrictBackground(restrictBackground);
869 mMenuRestrictBackground.setChecked(restrictBackground);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700870 }
871
872 private boolean getAppRestrictBackground() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700873 final int appId = mCurrentApp.appId;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700874 final int uidPolicy = mPolicyManager.getAppPolicy(appId);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700875 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
876 }
877
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700878 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700879 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700880 final int appId = mCurrentApp.appId;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700881 mPolicyManager.setAppPolicy(appId,
882 restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700883 mAppRestrict.setChecked(restrictBackground);
884 }
885
Jeff Sharkey8a503642011-06-10 13:31:21 -0700886 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700887 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
888 * current {@link #mTemplate}.
889 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700890 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700891 if (isAppDetailMode()) {
892 mNetworkSwitches.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700893 } else {
894 mNetworkSwitches.setVisibility(View.VISIBLE);
895 }
896
Jeff Sharkey28130d92011-09-02 16:10:24 -0700897 // TODO: move enabled state directly into policy
898 if (TAB_MOBILE.equals(mCurrentTab)) {
899 mBinding = true;
900 mDataEnabled.setChecked(isMobileDataEnabled());
901 mBinding = false;
902 }
903
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700904 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
905 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700906 mDisableAtLimitView.setVisibility(View.VISIBLE);
907 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700908 if (!isAppDetailMode()) {
909 mChart.bindNetworkPolicy(policy);
910 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700911
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700912 } else {
913 // controls are disabled; don't bind warning/limit sweeps
914 mDisableAtLimitView.setVisibility(View.GONE);
915 mChart.bindNetworkPolicy(null);
916 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700917
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700918 if (refreshCycle) {
919 // generate cycle list based on policy and available history
920 updateCycleList(policy);
921 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700922 }
923
924 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700925 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
926 * and available {@link NetworkStatsHistory} data. Always selects the newest
927 * item, updating the inspection range on {@link #mChart}.
928 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700929 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700930 // stash away currently selected cycle to try restoring below
931 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700932 mCycleAdapter.clear();
933
934 final Context context = mCycleSpinner.getContext();
935
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700936 long historyStart = Long.MAX_VALUE;
937 long historyEnd = Long.MIN_VALUE;
938 if (mChartData != null) {
939 historyStart = mChartData.network.getStart();
940 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700941 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700942
Jeff Sharkey461842a2011-09-25 18:22:48 -0700943 final long now = System.currentTimeMillis();
944 if (historyStart == Long.MAX_VALUE) historyStart = now;
945 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700946
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700947 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700948 if (policy != null) {
949 // find the next cycle boundary
950 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700951
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700952 // walk backwards, generating all valid cycle ranges
953 while (cycleEnd > historyStart) {
954 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
955 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
956 + historyStart);
957 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
958 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700959 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700960 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700961
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700962 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700963 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700964 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700965
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700966 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700967 // no policy defined cycles; show entry for each four-week period
968 long cycleEnd = historyEnd;
969 while (cycleEnd > historyStart) {
970 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
971 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
972 cycleEnd = cycleStart;
973 }
974
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700975 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700976 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700977
978 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700979 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700980 final int position = mCycleAdapter.findNearestPosition(previousItem);
981 mCycleSpinner.setSelection(position);
982
983 // only force-update cycle when changed; skipping preserves any
984 // user-defined inspection region.
985 final CycleItem selectedItem = mCycleAdapter.getItem(position);
986 if (!Objects.equal(selectedItem, previousItem)) {
987 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
988 } else {
989 // but still kick off loader for detailed list
990 updateDetailData();
991 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700992 } else {
993 updateDetailData();
994 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700995 }
996
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700997 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700998 @Override
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700999 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
1000 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001001
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001002 final boolean dataEnabled = isChecked;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001003 final String currentTab = mCurrentTab;
1004 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001005 if (dataEnabled) {
1006 setMobileDataEnabled(true);
1007 } else {
1008 // disabling data; show confirmation dialog which eventually
1009 // calls setMobileDataEnabled() once user confirms.
1010 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1011 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001012 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001013
Jeff Sharkey28130d92011-09-02 16:10:24 -07001014 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001015 }
1016 };
1017
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001018 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001019 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001020 public void onClick(View v) {
1021 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001022 if (disableAtLimit) {
1023 // enabling limit; show confirmation dialog which eventually
1024 // calls setPolicyLimitBytes() once user confirms.
1025 ConfirmLimitFragment.show(DataUsageSummary.this);
1026 } else {
1027 setPolicyLimitBytes(LIMIT_DISABLED);
1028 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001029 }
1030 };
1031
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001032 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001033 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001034 public void onClick(View v) {
1035 final boolean restrictBackground = !mAppRestrict.isChecked();
1036
1037 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001038 // enabling restriction; show confirmation dialog which
1039 // eventually calls setRestrictBackground() once user
1040 // confirms.
1041 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001042 } else {
1043 setAppRestrictBackground(false);
1044 }
1045 }
1046 };
1047
1048 private OnClickListener mAppSettingsListener = new OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001049 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001050 public void onClick(View v) {
Jeff Sharkeyd92e0412012-04-24 11:35:43 -07001051 if (!isAdded()) return;
1052
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001053 // TODO: target torwards entire UID instead of just first package
1054 startActivity(mAppSettingsIntent);
1055 }
1056 };
1057
Jeff Sharkey8a503642011-06-10 13:31:21 -07001058 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001059 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001060 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001061 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001062 final AppItem app = (AppItem) parent.getItemAtPosition(position);
1063 final UidDetail detail = mUidDetailProvider.getUidDetail(app.appId, true);
1064 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001065 }
1066 };
1067
1068 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001069 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001070 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1071 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1072 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001073 // show cycle editor; will eventually call setPolicyCycleDay()
1074 // when user finishes editing.
1075 CycleEditorFragment.show(DataUsageSummary.this);
1076
1077 // reset spinner to something other than "change cycle..."
1078 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001079
1080 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001081 if (LOGD) {
1082 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1083 + cycle.end + "]");
1084 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001085
1086 // update chart to show selected cycle, and update detail data
1087 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001088 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001089
1090 updateDetailData();
1091 }
1092 }
1093
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001094 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001095 public void onNothingSelected(AdapterView<?> parent) {
1096 // ignored
1097 }
1098 };
1099
1100 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001101 * Update details based on {@link #mChart} inspection range depending on
1102 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1103 * of applications data usage, and when {@link #isAppDetailMode()} update
1104 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001105 */
1106 private void updateDetailData() {
1107 if (LOGD) Log.d(TAG, "updateDetailData()");
1108
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001109 final long start = mChart.getInspectStart();
1110 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001111 final long now = System.currentTimeMillis();
1112
1113 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001114
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001115 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001116 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001117 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001118 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001119 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001120 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001121 final long foregroundBytes = entry.rxBytes + entry.txBytes;
1122
1123 mAppPieChart.setOriginAngle(175);
1124
1125 mAppPieChart.removeAllSlices();
1126 mAppPieChart.addSlice(foregroundBytes, Color.parseColor("#d88d3a"));
1127 mAppPieChart.addSlice(defaultBytes, Color.parseColor("#666666"));
1128
1129 mAppPieChart.generatePath();
1130
1131 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1132 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1133
1134 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001135 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001136
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001137 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001138
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001139 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001140 if (mChartData != null) {
1141 entry = mChartData.network.getValues(start, end, now, null);
1142 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001143
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001144 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001145 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001146 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001147 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001148
1149 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1150 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001151 final String rangePhrase = formatDateRange(context, start, end);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001152
Jeff Sharkeye557c332012-04-13 16:04:07 -07001153 final int summaryRes;
1154 if (TAB_MOBILE.equals(mCurrentTab) || TAB_3G.equals(mCurrentApp)
1155 || TAB_4G.equals(mCurrentApp)) {
1156 summaryRes = R.string.data_usage_total_during_range_mobile;
1157 } else {
1158 summaryRes = R.string.data_usage_total_during_range;
1159 }
1160
1161 mUsageSummary.setText(getString(summaryRes, totalPhrase, rangePhrase));
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001162 }
1163
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001164 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1165 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001166 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001167 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001168 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001169 }
1170
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001171 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001172 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1173 mChartData = data;
1174 mChart.bindNetworkStats(mChartData.network);
1175 mChart.bindDetailNetworkStats(mChartData.detail);
1176
1177 // calcuate policy cycles based on available data
1178 updatePolicy(true);
1179 updateAppDetail();
1180
1181 // force scroll to top of body when showing detail
1182 if (mChartData.detail != null) {
1183 mListView.smoothScrollToPosition(0);
1184 }
1185 }
1186
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001187 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001188 public void onLoaderReset(Loader<ChartData> loader) {
1189 mChartData = null;
1190 mChart.bindNetworkStats(null);
1191 mChart.bindDetailNetworkStats(null);
1192 }
1193 };
1194
1195 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001196 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001197 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001198 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001199 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001200 }
1201
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001202 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001203 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001204 final int[] restrictedAppIds = mPolicyManager.getAppsWithPolicy(
1205 POLICY_REJECT_METERED_BACKGROUND);
1206 mAdapter.bindStats(data, restrictedAppIds);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001207 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001208 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001209
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001210 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001211 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001212 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001213 updateEmptyVisible();
1214 }
1215
1216 private void updateEmptyVisible() {
1217 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1218 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001219 }
1220 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001221
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001222 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001223 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001224 final Context context = getActivity();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001225 if (hasReadyMobileRadio(context)) {
1226 final TelephonyManager tele = TelephonyManager.from(context);
1227 return mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001228 } else {
1229 return false;
1230 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001231 }
1232
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001233 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001234 private void setMobilePolicySplit(boolean split) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001235 final Context context = getActivity();
1236 if (hasReadyMobileRadio(context)) {
1237 final TelephonyManager tele = TelephonyManager.from(context);
1238 mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split);
1239 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001240 }
1241
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001242 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001243 final TelephonyManager tele = TelephonyManager.from(context);
1244 final String actualSubscriberId = tele.getSubscriberId();
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -08001245 return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001246 }
1247
Jeff Sharkey8a503642011-06-10 13:31:21 -07001248 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001249 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001250 public void onInspectRangeChanged() {
1251 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
1252 updateDetailData();
1253 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001254
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001255 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001256 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001257 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001258 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001259
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001260 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001261 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001262 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001263 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001264
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001265 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001266 public void requestWarningEdit() {
1267 WarningEditorFragment.show(DataUsageSummary.this);
1268 }
1269
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001270 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001271 public void requestLimitEdit() {
1272 LimitEditorFragment.show(DataUsageSummary.this);
1273 }
1274 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001275
1276 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001277 * List item that reflects a specific data usage cycle.
1278 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001279 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001280 public CharSequence label;
1281 public long start;
1282 public long end;
1283
Jeff Sharkey8a503642011-06-10 13:31:21 -07001284 CycleItem(CharSequence label) {
1285 this.label = label;
1286 }
1287
1288 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001289 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001290 this.start = start;
1291 this.end = end;
1292 }
1293
Jeff Sharkey8a503642011-06-10 13:31:21 -07001294 @Override
1295 public String toString() {
1296 return label.toString();
1297 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001298
1299 @Override
1300 public boolean equals(Object o) {
1301 if (o instanceof CycleItem) {
1302 final CycleItem another = (CycleItem) o;
1303 return start == another.start && end == another.end;
1304 }
1305 return false;
1306 }
1307
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001308 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001309 public int compareTo(CycleItem another) {
1310 return Long.compare(start, another.start);
1311 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001312 }
1313
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001314 private static final StringBuilder sBuilder = new StringBuilder(50);
1315 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1316 sBuilder, Locale.getDefault());
1317
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001318 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001319 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001320
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001321 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001322 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001323 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1324 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001325 }
1326 }
1327
Jeff Sharkey8a503642011-06-10 13:31:21 -07001328 /**
1329 * Special-case data usage cycle that triggers dialog to change
1330 * {@link NetworkPolicy#cycleDay}.
1331 */
1332 public static class CycleChangeItem extends CycleItem {
1333 public CycleChangeItem(Context context) {
1334 super(context.getString(R.string.data_usage_change_cycle));
1335 }
1336 }
1337
1338 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001339 private boolean mChangePossible = false;
1340 private boolean mChangeVisible = false;
1341
1342 private final CycleChangeItem mChangeItem;
1343
Jeff Sharkey8a503642011-06-10 13:31:21 -07001344 public CycleAdapter(Context context) {
1345 super(context, android.R.layout.simple_spinner_item);
1346 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001347 mChangeItem = new CycleChangeItem(context);
1348 }
1349
1350 public void setChangePossible(boolean possible) {
1351 mChangePossible = possible;
1352 updateChange();
1353 }
1354
1355 public void setChangeVisible(boolean visible) {
1356 mChangeVisible = visible;
1357 updateChange();
1358 }
1359
1360 private void updateChange() {
1361 remove(mChangeItem);
1362 if (mChangePossible && mChangeVisible) {
1363 add(mChangeItem);
1364 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001365 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001366
1367 /**
1368 * Find position of {@link CycleItem} in this adapter which is nearest
1369 * the given {@link CycleItem}.
1370 */
1371 public int findNearestPosition(CycleItem target) {
1372 if (target != null) {
1373 final int count = getCount();
1374 for (int i = count - 1; i >= 0; i--) {
1375 final CycleItem item = getItem(i);
1376 if (item instanceof CycleChangeItem) {
1377 continue;
1378 } else if (item.compareTo(target) >= 0) {
1379 return i;
1380 }
1381 }
1382 }
1383 return 0;
1384 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001385 }
1386
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001387 public static class AppItem implements Comparable<AppItem>, Parcelable {
1388 public final int appId;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001389 public boolean restricted;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001390 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001391 public long total;
1392
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001393 public AppItem(int appId) {
1394 this.appId = appId;
1395 }
1396
1397 public AppItem(Parcel parcel) {
1398 appId = parcel.readInt();
1399 uids = parcel.readSparseBooleanArray();
1400 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001401 }
1402
1403 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001404 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001405 }
1406
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001407 @Override
1408 public void writeToParcel(Parcel dest, int flags) {
1409 dest.writeInt(appId);
1410 dest.writeSparseBooleanArray(uids);
1411 dest.writeLong(total);
1412 }
1413
1414 @Override
1415 public int describeContents() {
1416 return 0;
1417 }
1418
1419 @Override
1420 public int compareTo(AppItem another) {
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001421 return Long.compare(another.total, total);
1422 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001423
1424 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1425 @Override
1426 public AppItem createFromParcel(Parcel in) {
1427 return new AppItem(in);
1428 }
1429
1430 @Override
1431 public AppItem[] newArray(int size) {
1432 return new AppItem[size];
1433 }
1434 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001435 }
1436
Jeff Sharkey8a503642011-06-10 13:31:21 -07001437 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001438 * Adapter of applications, sorted by total usage descending.
1439 */
1440 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001441 private final UidDetailProvider mProvider;
1442 private final int mInsetSide;
1443
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001444 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001445 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001446
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001447 public DataUsageAdapter(UidDetailProvider provider, int insetSide) {
1448 mProvider = checkNotNull(provider);
1449 mInsetSide = insetSide;
1450 }
1451
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001452 /**
1453 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1454 */
Jeff Sharkeye557c332012-04-13 16:04:07 -07001455 public void bindStats(NetworkStats stats, int[] restrictedAppIds) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001456 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001457
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001458 final AppItem systemItem = new AppItem(android.os.Process.SYSTEM_UID);
1459 final SparseArray<AppItem> knownUids = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001460
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001461 NetworkStats.Entry entry = null;
1462 final int size = stats != null ? stats.size() : 0;
1463 for (int i = 0; i < size; i++) {
1464 entry = stats.getValues(i, entry);
1465
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001466 final boolean isApp = UserId.isApp(entry.uid);
1467 final int appId = isApp ? UserId.getAppId(entry.uid) : entry.uid;
1468 if (isApp || appId == UID_REMOVED || appId == UID_TETHERING) {
1469 AppItem item = knownUids.get(appId);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001470 if (item == null) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001471 item = new AppItem(appId);
1472 knownUids.put(appId, item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001473 mItems.add(item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001474 }
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001475
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001476 item.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001477 item.addUid(entry.uid);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001478 } else {
1479 systemItem.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001480 systemItem.addUid(entry.uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001481 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001482 }
1483
Jeff Sharkeye557c332012-04-13 16:04:07 -07001484 for (int appId : restrictedAppIds) {
1485 AppItem item = knownUids.get(appId);
1486 if (item == null) {
1487 item = new AppItem(appId);
1488 item.total = -1;
1489 mItems.add(item);
1490 }
1491 item.restricted = true;
1492 }
1493
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001494 if (systemItem.total > 0) {
1495 mItems.add(systemItem);
1496 }
1497
Jeff Sharkey8a503642011-06-10 13:31:21 -07001498 Collections.sort(mItems);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001499 mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001500 notifyDataSetChanged();
1501 }
1502
1503 @Override
1504 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001505 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001506 }
1507
1508 @Override
1509 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001510 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001511 }
1512
1513 @Override
1514 public long getItemId(int position) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001515 return mItems.get(position).appId;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001516 }
1517
1518 @Override
1519 public View getView(int position, View convertView, ViewGroup parent) {
1520 if (convertView == null) {
1521 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey5d706792011-09-08 18:57:17 -07001522 R.layout.data_usage_item, parent, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001523
1524 if (mInsetSide > 0) {
1525 convertView.setPadding(mInsetSide, 0, mInsetSide, 0);
1526 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001527 }
1528
1529 final Context context = parent.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001530
Jeff Sharkey28130d92011-09-02 16:10:24 -07001531 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001532 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1533 android.R.id.progress);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001534
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001535 // kick off async load of app details
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001536 final AppItem item = mItems.get(position);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001537 UidDetailTask.bindView(mProvider, item, convertView);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001538
Jeff Sharkeye557c332012-04-13 16:04:07 -07001539 if (item.restricted && item.total <= 0) {
1540 text1.setText(R.string.data_usage_app_restricted);
1541 progress.setVisibility(View.GONE);
1542 } else {
1543 text1.setText(Formatter.formatFileSize(context, item.total));
1544 progress.setVisibility(View.VISIBLE);
1545 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001546
1547 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1548 progress.setProgress(percentTotal);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001549
1550 return convertView;
1551 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001552 }
1553
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001554 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001555 * Empty {@link Fragment} that controls display of UID details in
1556 * {@link DataUsageSummary}.
1557 */
1558 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001559 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001560
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001561 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001562 if (!parent.isAdded()) return;
1563
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001564 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001565 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001566
1567 final AppDetailsFragment fragment = new AppDetailsFragment();
1568 fragment.setArguments(args);
1569 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001570 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1571 ft.add(fragment, TAG_APP_DETAILS);
1572 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001573 ft.setBreadCrumbTitle(label);
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001574 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001575 }
1576
1577 @Override
1578 public void onStart() {
1579 super.onStart();
1580 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001581 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001582 target.updateBody();
1583 }
1584
1585 @Override
1586 public void onStop() {
1587 super.onStop();
1588 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001589 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001590 target.updateBody();
1591 }
1592 }
1593
1594 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001595 * Dialog to request user confirmation before setting
1596 * {@link NetworkPolicy#limitBytes}.
1597 */
1598 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001599 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001600 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001601
1602 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001603 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001604
Jeff Sharkey461842a2011-09-25 18:22:48 -07001605 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001606 final CharSequence message;
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001607 final long minLimitBytes = (long) (
1608 parent.mPolicyEditor.getPolicy(parent.mTemplate).warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001609 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001610
1611 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001612 final String currentTab = parent.mCurrentTab;
1613 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001614 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001615 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001616 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001617 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001618 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001619 } else if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001620 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001621 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001622 } else {
1623 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001624 }
1625
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001626 final Bundle args = new Bundle();
1627 args.putCharSequence(EXTRA_MESSAGE, message);
1628 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1629
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001630 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1631 dialog.setArguments(args);
1632 dialog.setTargetFragment(parent, 0);
1633 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1634 }
1635
1636 @Override
1637 public Dialog onCreateDialog(Bundle savedInstanceState) {
1638 final Context context = getActivity();
1639
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001640 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001641 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1642
1643 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1644 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001645 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001646
1647 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001648 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001649 public void onClick(DialogInterface dialog, int which) {
1650 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1651 if (target != null) {
1652 target.setPolicyLimitBytes(limitBytes);
1653 }
1654 }
1655 });
1656
1657 return builder.create();
1658 }
1659 }
1660
1661 /**
1662 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1663 */
1664 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001665 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001666
1667 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001668 if (!parent.isAdded()) return;
1669
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001670 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001671 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001672
1673 final CycleEditorFragment dialog = new CycleEditorFragment();
1674 dialog.setArguments(args);
1675 dialog.setTargetFragment(parent, 0);
1676 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1677 }
1678
1679 @Override
1680 public Dialog onCreateDialog(Bundle savedInstanceState) {
1681 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001682 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1683 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001684
1685 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1686 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1687
1688 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1689 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1690
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001691 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1692 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001693
1694 cycleDayPicker.setMinValue(1);
1695 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001696 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001697 cycleDayPicker.setWrapSelectorWheel(true);
1698
1699 builder.setTitle(R.string.data_usage_cycle_editor_title);
1700 builder.setView(view);
1701
1702 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1703 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001704 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001705 public void onClick(DialogInterface dialog, int which) {
1706 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001707 final String cycleTimezone = new Time().timezone;
1708 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001709 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001710 }
1711 });
1712
1713 return builder.create();
1714 }
1715 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001716
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001717 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001718 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1719 */
1720 public static class WarningEditorFragment extends DialogFragment {
1721 private static final String EXTRA_TEMPLATE = "template";
1722
1723 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001724 if (!parent.isAdded()) return;
1725
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001726 final Bundle args = new Bundle();
1727 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1728
1729 final WarningEditorFragment dialog = new WarningEditorFragment();
1730 dialog.setArguments(args);
1731 dialog.setTargetFragment(parent, 0);
1732 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1733 }
1734
1735 @Override
1736 public Dialog onCreateDialog(Bundle savedInstanceState) {
1737 final Context context = getActivity();
1738 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1739 final NetworkPolicyEditor editor = target.mPolicyEditor;
1740
1741 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1742 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1743
1744 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1745 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1746
1747 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1748 final long warningBytes = editor.getPolicyWarningBytes(template);
1749 final long limitBytes = editor.getPolicyLimitBytes(template);
1750
1751 bytesPicker.setMinValue(0);
1752 if (limitBytes != LIMIT_DISABLED) {
1753 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1754 } else {
1755 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1756 }
1757 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1758 bytesPicker.setWrapSelectorWheel(false);
1759
1760 builder.setTitle(R.string.data_usage_warning_editor_title);
1761 builder.setView(view);
1762
1763 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1764 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001765 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001766 public void onClick(DialogInterface dialog, int which) {
1767 // clear focus to finish pending text edits
1768 bytesPicker.clearFocus();
1769
1770 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1771 editor.setPolicyWarningBytes(template, bytes);
1772 target.updatePolicy(false);
1773 }
1774 });
1775
1776 return builder.create();
1777 }
1778 }
1779
1780 /**
1781 * Dialog to edit {@link NetworkPolicy#limitBytes}.
1782 */
1783 public static class LimitEditorFragment extends DialogFragment {
1784 private static final String EXTRA_TEMPLATE = "template";
1785
1786 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001787 if (!parent.isAdded()) return;
1788
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001789 final Bundle args = new Bundle();
1790 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1791
1792 final LimitEditorFragment dialog = new LimitEditorFragment();
1793 dialog.setArguments(args);
1794 dialog.setTargetFragment(parent, 0);
1795 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
1796 }
1797
1798 @Override
1799 public Dialog onCreateDialog(Bundle savedInstanceState) {
1800 final Context context = getActivity();
1801 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1802 final NetworkPolicyEditor editor = target.mPolicyEditor;
1803
1804 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1805 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1806
1807 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1808 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1809
1810 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1811 final long warningBytes = editor.getPolicyWarningBytes(template);
1812 final long limitBytes = editor.getPolicyLimitBytes(template);
1813
1814 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09001815 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001816 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
1817 } else {
1818 bytesPicker.setMinValue(0);
1819 }
1820 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
1821 bytesPicker.setWrapSelectorWheel(false);
1822
1823 builder.setTitle(R.string.data_usage_limit_editor_title);
1824 builder.setView(view);
1825
1826 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1827 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001828 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001829 public void onClick(DialogInterface dialog, int which) {
1830 // clear focus to finish pending text edits
1831 bytesPicker.clearFocus();
1832
1833 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1834 editor.setPolicyLimitBytes(template, bytes);
1835 target.updatePolicy(false);
1836 }
1837 });
1838
1839 return builder.create();
1840 }
1841 }
1842 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07001843 * Dialog to request user confirmation before disabling data.
1844 */
1845 public static class ConfirmDataDisableFragment extends DialogFragment {
1846 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001847 if (!parent.isAdded()) return;
1848
Jeff Sharkey28130d92011-09-02 16:10:24 -07001849 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
1850 dialog.setTargetFragment(parent, 0);
1851 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
1852 }
1853
1854 @Override
1855 public Dialog onCreateDialog(Bundle savedInstanceState) {
1856 final Context context = getActivity();
1857
1858 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1859 builder.setMessage(R.string.data_usage_disable_mobile);
1860
1861 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001862 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07001863 public void onClick(DialogInterface dialog, int which) {
1864 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1865 if (target != null) {
1866 // TODO: extend to modify policy enabled flag.
1867 target.setMobileDataEnabled(false);
1868 }
1869 }
1870 });
1871 builder.setNegativeButton(android.R.string.cancel, null);
1872
1873 return builder.create();
1874 }
1875 }
1876
1877 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001878 * Dialog to request user confirmation before setting
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001879 * {@link android.provider.Settings.Secure#DATA_ROAMING}.
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001880 */
1881 public static class ConfirmDataRoamingFragment extends DialogFragment {
1882 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001883 if (!parent.isAdded()) return;
1884
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001885 final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment();
1886 dialog.setTargetFragment(parent, 0);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001887 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_ROAMING);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001888 }
1889
1890 @Override
1891 public Dialog onCreateDialog(Bundle savedInstanceState) {
1892 final Context context = getActivity();
1893
1894 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1895 builder.setTitle(R.string.roaming_reenable_title);
1896 builder.setMessage(R.string.roaming_warning);
1897
1898 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001899 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001900 public void onClick(DialogInterface dialog, int which) {
1901 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1902 if (target != null) {
1903 target.setDataRoaming(true);
1904 }
1905 }
1906 });
1907 builder.setNegativeButton(android.R.string.cancel, null);
1908
1909 return builder.create();
1910 }
1911 }
1912
1913 /**
1914 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001915 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001916 */
1917 public static class ConfirmRestrictFragment extends DialogFragment {
1918 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001919 if (!parent.isAdded()) return;
1920
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001921 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1922 dialog.setTargetFragment(parent, 0);
1923 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1924 }
1925
1926 @Override
1927 public Dialog onCreateDialog(Bundle savedInstanceState) {
1928 final Context context = getActivity();
1929
1930 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001931 builder.setTitle(R.string.data_usage_restrict_background_title);
Jeff Sharkey461842a2011-09-25 18:22:48 -07001932 builder.setMessage(getString(R.string.data_usage_restrict_background));
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001933
1934 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001935 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001936 public void onClick(DialogInterface dialog, int which) {
1937 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1938 if (target != null) {
1939 target.setRestrictBackground(true);
1940 }
1941 }
1942 });
1943 builder.setNegativeButton(android.R.string.cancel, null);
1944
1945 return builder.create();
1946 }
1947 }
1948
1949 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001950 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
1951 * change has been denied, usually based on
1952 * {@link DataUsageSummary#hasLimitedNetworks()}.
1953 */
1954 public static class DeniedRestrictFragment extends DialogFragment {
1955 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001956 if (!parent.isAdded()) return;
1957
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001958 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
1959 dialog.setTargetFragment(parent, 0);
1960 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
1961 }
1962
1963 @Override
1964 public Dialog onCreateDialog(Bundle savedInstanceState) {
1965 final Context context = getActivity();
1966
1967 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1968 builder.setTitle(R.string.data_usage_app_restrict_background);
1969 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
1970 builder.setPositiveButton(android.R.string.ok, null);
1971
1972 return builder.create();
1973 }
1974 }
1975
1976 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001977 * Dialog to request user confirmation before setting
1978 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
1979 */
1980 public static class ConfirmAppRestrictFragment extends DialogFragment {
1981 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001982 if (!parent.isAdded()) return;
1983
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001984 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
1985 dialog.setTargetFragment(parent, 0);
1986 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
1987 }
1988
1989 @Override
1990 public Dialog onCreateDialog(Bundle savedInstanceState) {
1991 final Context context = getActivity();
1992
1993 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001994 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
1995 builder.setMessage(R.string.data_usage_app_restrict_dialog);
1996
1997 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001998 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001999 public void onClick(DialogInterface dialog, int which) {
2000 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2001 if (target != null) {
2002 target.setAppRestrictBackground(true);
2003 }
2004 }
2005 });
2006 builder.setNegativeButton(android.R.string.cancel, null);
2007
2008 return builder.create();
2009 }
2010 }
2011
2012 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002013 * Compute default tab that should be selected, based on
2014 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2015 */
2016 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002017 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
2018 if (template == null) return null;
2019
2020 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002021 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002022 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002023 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002024 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002025 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002026 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002027 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002028 return TAB_WIFI;
2029 default:
2030 return null;
2031 }
2032 }
2033
2034 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002035 * Background task that loads {@link UidDetail}, binding to
2036 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002037 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002038 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2039 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002040 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002041 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002042
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002043 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002044 mProvider = checkNotNull(provider);
2045 mItem = checkNotNull(item);
2046 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002047 }
2048
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002049 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002050 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002051 final UidDetailTask existing = (UidDetailTask) target.getTag();
2052 if (existing != null) {
2053 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002054 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002055
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002056 final UidDetail cachedDetail = provider.getUidDetail(item.appId, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002057 if (cachedDetail != null) {
2058 bindView(cachedDetail, target);
2059 } else {
2060 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2061 AsyncTask.THREAD_POOL_EXECUTOR));
2062 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002063 }
2064
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002065 private static void bindView(UidDetail detail, View target) {
2066 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2067 final TextView title = (TextView) target.findViewById(android.R.id.title);
2068
2069 if (detail != null) {
2070 icon.setImageDrawable(detail.icon);
2071 title.setText(detail.label);
2072 } else {
2073 icon.setImageDrawable(null);
2074 title.setText(null);
2075 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002076 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002077
2078 @Override
2079 protected void onPreExecute() {
2080 bindView(null, mTarget);
2081 }
2082
2083 @Override
2084 protected UidDetail doInBackground(Void... params) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002085 return mProvider.getUidDetail(mItem.appId, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002086 }
2087
2088 @Override
2089 protected void onPostExecute(UidDetail result) {
2090 bindView(result, mTarget);
2091 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002092 }
2093
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002094 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002095 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002096 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002097 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002098 if (TEST_RADIOS) {
2099 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2100 }
2101
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002102 final ConnectivityManager conn = ConnectivityManager.from(context);
2103 final TelephonyManager tele = TelephonyManager.from(context);
2104
2105 // require both supported network and ready SIM
2106 return conn.isNetworkSupported(TYPE_MOBILE) && tele.getSimState() == SIM_STATE_READY;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002107 }
2108
2109 /**
2110 * Test if device has a mobile 4G data radio.
2111 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002112 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002113 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2114 return false;
2115 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002116 if (TEST_RADIOS) {
2117 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2118 }
2119
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002120 final ConnectivityManager conn = ConnectivityManager.from(context);
2121 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002122
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002123 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002124 final boolean hasLte = (tele.getLteOnCdmaMode() == Phone.LTE_ON_CDMA_TRUE)
2125 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002126 return hasWimax || hasLte;
2127 }
2128
2129 /**
2130 * Test if device has a Wi-Fi data radio.
2131 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002132 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002133 if (TEST_RADIOS) {
2134 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2135 }
2136
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002137 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002138 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002139 }
2140
2141 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002142 * Test if device has an ethernet network connection.
2143 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002144 public static boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002145 if (TEST_RADIOS) {
2146 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2147 }
2148
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002149 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002150 return conn.isNetworkSupported(TYPE_ETHERNET);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002151 }
2152
2153 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002154 * Inflate a {@link Preference} style layout, adding the given {@link View}
2155 * widget into {@link android.R.id#widget_frame}.
2156 */
2157 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2158 final View view = inflater.inflate(R.layout.preference, root, false);
2159 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2160 android.R.id.widget_frame);
2161 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2162 return view;
2163 }
2164
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002165 private static View inflateAppTitle(
2166 LayoutInflater inflater, ViewGroup root, CharSequence label) {
2167 final TextView view = (TextView) inflater.inflate(
2168 R.layout.data_usage_app_title, root, false);
2169 view.setText(label);
2170 return view;
2171 }
2172
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002173 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002174 * Test if any networks are currently limited.
2175 */
2176 private boolean hasLimitedNetworks() {
2177 return !buildLimitedNetworksList().isEmpty();
2178 }
2179
2180 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002181 * Build string describing currently limited networks, which defines when
2182 * background data is restricted.
2183 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002184 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002185 private CharSequence buildLimitedNetworksString() {
2186 final List<CharSequence> limited = buildLimitedNetworksList();
2187
2188 // handle case where no networks limited
2189 if (limited.isEmpty()) {
2190 limited.add(getText(R.string.data_usage_list_none));
2191 }
2192
2193 return TextUtils.join(limited);
2194 }
2195
2196 /**
2197 * Build list of currently limited networks, which defines when background
2198 * data is restricted.
2199 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002200 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002201 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002202 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002203
2204 // build combined list of all limited networks
2205 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002206
2207 final TelephonyManager tele = TelephonyManager.from(context);
2208 if (tele.getSimState() == SIM_STATE_READY) {
2209 final String subscriberId = getActiveSubscriberId(context);
2210 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2211 limited.add(getText(R.string.data_usage_list_mobile));
2212 }
2213 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2214 limited.add(getText(R.string.data_usage_tab_3g));
2215 }
2216 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2217 limited.add(getText(R.string.data_usage_tab_4g));
2218 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002219 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002220
2221 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002222 limited.add(getText(R.string.data_usage_tab_wifi));
2223 }
2224 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2225 limited.add(getText(R.string.data_usage_tab_ethernet));
2226 }
2227
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002228 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002229 }
2230
2231 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002232 * Inset both selector and divider {@link Drawable} on the given
2233 * {@link ListView} by the requested dimensions.
2234 */
2235 private static void insetListViewDrawables(ListView view, int insetSide) {
2236 final Drawable selector = view.getSelector();
2237 final Drawable divider = view.getDivider();
2238
2239 // fully unregister these drawables so callbacks can be maintained after
2240 // wrapping below.
2241 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2242 view.setSelector(stub);
2243 view.setDivider(stub);
2244
2245 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2246 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2247 }
2248
2249 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002250 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002251 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002252 */
2253 private static void setPreferenceTitle(View parent, int resId) {
2254 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2255 title.setText(resId);
2256 }
2257
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002258 /**
2259 * Set {@link android.R.id#summary} for a preference view inflated with
2260 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2261 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002262 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002263 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2264 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002265 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002266 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002267}