blob: 62c86d833bc392392a197f1629c0634a6b85d7b5 [file] [log] [blame]
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070019import static android.net.ConnectivityManager.TYPE_ETHERNET;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070020import static android.net.ConnectivityManager.TYPE_MOBILE;
Jeff Sharkeybdf98e82011-11-10 17:17:24 -080021import static android.net.ConnectivityManager.TYPE_WIFI;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070022import static android.net.ConnectivityManager.TYPE_WIMAX;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070023import static android.net.NetworkPolicy.LIMIT_DISABLED;
Jeff Sharkeya53188f2011-09-13 19:56:45 -070024import static android.net.NetworkPolicy.WARNING_DISABLED;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070025import static android.net.NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070026import static android.net.NetworkPolicyManager.POLICY_NONE;
27import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND;
Jeff Sharkey8a503642011-06-10 13:31:21 -070028import static android.net.NetworkPolicyManager.computeLastCycleBoundary;
29import static android.net.NetworkPolicyManager.computeNextCycleBoundary;
Jeff Sharkeya662e492011-06-18 21:57:06 -070030import static android.net.NetworkTemplate.MATCH_MOBILE_3G_LOWER;
31import static android.net.NetworkTemplate.MATCH_MOBILE_4G;
32import static android.net.NetworkTemplate.MATCH_MOBILE_ALL;
33import static android.net.NetworkTemplate.MATCH_WIFI;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070034import static android.net.NetworkTemplate.buildTemplateEthernet;
35import static android.net.NetworkTemplate.buildTemplateMobile3gLower;
36import static android.net.NetworkTemplate.buildTemplateMobile4g;
37import static android.net.NetworkTemplate.buildTemplateMobileAll;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070038import static android.net.NetworkTemplate.buildTemplateWifiWildcard;
Jeff Sharkey77dae912012-02-03 14:50:33 -080039import static android.net.TrafficStats.GB_IN_BYTES;
40import static android.net.TrafficStats.MB_IN_BYTES;
Jeff Sharkeya83a24f2011-09-16 01:52:39 -070041import static android.net.TrafficStats.UID_REMOVED;
42import static android.net.TrafficStats.UID_TETHERING;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070043import static android.telephony.TelephonyManager.SIM_STATE_READY;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070044import static android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
45import static android.text.format.DateUtils.FORMAT_SHOW_DATE;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070046import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -070047import static com.android.internal.util.Preconditions.checkNotNull;
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -070048import static com.android.settings.Utils.prepareCustomPreferencesList;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070049
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070050import android.animation.LayoutTransition;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070051import android.app.AlertDialog;
52import android.app.Dialog;
53import android.app.DialogFragment;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070054import android.app.Fragment;
Amith Yamasani5ba0a022011-11-07 12:29:00 -080055import android.app.FragmentManager;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070056import android.app.FragmentTransaction;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070057import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070058import android.content.ContentResolver;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070059import android.content.Context;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070060import android.content.DialogInterface;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -070061import android.content.Intent;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070062import android.content.Loader;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070063import android.content.SharedPreferences;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070064import android.content.pm.PackageManager;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070065import android.content.res.Resources;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070066import android.graphics.Color;
Jeff Sharkey5d706792011-09-08 18:57:17 -070067import android.graphics.drawable.ColorDrawable;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070068import android.graphics.drawable.Drawable;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070069import android.net.ConnectivityManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070070import android.net.INetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070071import android.net.INetworkStatsService;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070072import android.net.INetworkStatsSession;
Jeff Sharkey8a503642011-06-10 13:31:21 -070073import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070074import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070075import android.net.NetworkStats;
76import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070077import android.net.NetworkTemplate;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070078import android.net.TrafficStats;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070079import android.net.Uri;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070080import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070081import android.os.Bundle;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070082import android.os.INetworkManagementService;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070083import android.os.Parcel;
84import android.os.Parcelable;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070085import android.os.RemoteException;
86import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070087import android.os.SystemProperties;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070088import android.os.UserId;
Jeff Sharkey8a503642011-06-10 13:31:21 -070089import android.preference.Preference;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070090import android.preference.PreferenceActivity;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070091import android.provider.Settings;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070092import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070093import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070094import android.text.format.DateUtils;
95import android.text.format.Formatter;
Jeff Sharkeye5223a02012-03-09 17:11:14 -080096import android.text.format.Time;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070097import android.util.Log;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070098import android.util.SparseArray;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070099import android.util.SparseBooleanArray;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700100import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700101import android.view.Menu;
102import android.view.MenuInflater;
103import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700104import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700105import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700106import android.view.ViewGroup;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700107import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700108import android.widget.AdapterView;
109import android.widget.AdapterView.OnItemClickListener;
110import android.widget.AdapterView.OnItemSelectedListener;
111import android.widget.ArrayAdapter;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700112import android.widget.BaseAdapter;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700113import android.widget.Button;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700114import android.widget.CheckBox;
115import android.widget.CompoundButton;
116import android.widget.CompoundButton.OnCheckedChangeListener;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700117import android.widget.ImageView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700118import android.widget.LinearLayout;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700119import android.widget.ListView;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700120import android.widget.NumberPicker;
Jeff Sharkey2412b0f2011-07-17 20:31:40 -0700121import android.widget.ProgressBar;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700122import android.widget.Spinner;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700123import android.widget.Switch;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700124import android.widget.TabHost;
125import android.widget.TabHost.OnTabChangeListener;
126import android.widget.TabHost.TabContentFactory;
127import android.widget.TabHost.TabSpec;
128import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700129import android.widget.TextView;
130
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700131import com.android.internal.telephony.Phone;
Jeff Sharkey5d706792011-09-08 18:57:17 -0700132import com.android.settings.drawable.InsetBoundsDrawable;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700133import com.android.settings.net.ChartData;
134import com.android.settings.net.ChartDataLoader;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700135import com.android.settings.net.DataUsageMeteredSettings;
Jeff Sharkeya662e492011-06-18 21:57:06 -0700136import com.android.settings.net.NetworkPolicyEditor;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700137import com.android.settings.net.SummaryForAllUidLoader;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700138import com.android.settings.net.UidDetail;
139import com.android.settings.net.UidDetailProvider;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700140import com.android.settings.widget.ChartDataUsageView;
141import com.android.settings.widget.ChartDataUsageView.DataUsageChartListener;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700142import com.android.settings.widget.PieChartView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700143import com.google.android.collect.Lists;
144
145import java.util.ArrayList;
146import java.util.Collections;
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700147import java.util.List;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700148import java.util.Locale;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700149
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700150import libcore.util.Objects;
151
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700152/**
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700153 * Panel showing data usage history across various networks, including options
154 * to inspect based on usage cycle and control through {@link NetworkPolicy}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700155 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700156public class DataUsageSummary extends Fragment {
157 private static final String TAG = "DataUsage";
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800158 private static final boolean LOGD = false;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700159
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700160 // TODO: remove this testing code
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700161 private static final boolean TEST_ANIM = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700162 private static final boolean TEST_RADIOS = false;
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800163
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700164 private static final String TEST_RADIOS_PROP = "test.radios";
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800165 private static final String TEST_SUBSCRIBER_PROP = "test.subscriberid";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700166
Jeff Sharkey8a503642011-06-10 13:31:21 -0700167 private static final String TAB_3G = "3g";
168 private static final String TAB_4G = "4g";
169 private static final String TAB_MOBILE = "mobile";
170 private static final String TAB_WIFI = "wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700171 private static final String TAB_ETHERNET = "ethernet";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700172
Jeff Sharkey28130d92011-09-02 16:10:24 -0700173 private static final String TAG_CONFIRM_DATA_DISABLE = "confirmDataDisable";
174 private static final String TAG_CONFIRM_DATA_ROAMING = "confirmDataRoaming";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700175 private static final String TAG_CONFIRM_LIMIT = "confirmLimit";
176 private static final String TAG_CYCLE_EDITOR = "cycleEditor";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700177 private static final String TAG_WARNING_EDITOR = "warningEditor";
178 private static final String TAG_LIMIT_EDITOR = "limitEditor";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700179 private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700180 private static final String TAG_DENIED_RESTRICT = "deniedRestrict";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700181 private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700182 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700183
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700184 private static final int LOADER_CHART_DATA = 2;
185 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700186
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700187 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700188 private INetworkStatsService mStatsService;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700189 private NetworkPolicyManager mPolicyManager;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700190 private ConnectivityManager mConnService;
191
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700192 private INetworkStatsSession mStatsSession;
193
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700194 private static final String PREF_FILE = "data_usage";
195 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700196 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700197
198 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700199
Jeff Sharkey8a503642011-06-10 13:31:21 -0700200 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700201 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700202 private TabWidget mTabWidget;
203 private ListView mListView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700204 private DataUsageAdapter mAdapter;
205
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700206 /** Distance to inset content from sides, when needed. */
207 private int mInsetSide = 0;
208
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700209 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700210
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700211 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700212 private LinearLayout mNetworkSwitches;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700213 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700214 private View mDataEnabledView;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700215 private CheckBox mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700216 private View mDisableAtLimitView;
217
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700218 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700219 private Spinner mCycleSpinner;
220 private CycleAdapter mCycleAdapter;
221
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700222 private ChartDataUsageView mChart;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700223 private TextView mUsageSummary;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700224 private TextView mEmpty;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700225
226 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700227 private ImageView mAppIcon;
228 private ViewGroup mAppTitles;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700229 private PieChartView mAppPieChart;
230 private TextView mAppForeground;
231 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700232 private Button mAppSettings;
233
234 private LinearLayout mAppSwitches;
235 private CheckBox mAppRestrict;
236 private View mAppRestrictView;
237
Jeff Sharkey8a503642011-06-10 13:31:21 -0700238 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700239 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700240
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700241 private NetworkTemplate mTemplate;
242 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700243
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700244 private AppItem mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700245
246 private Intent mAppSettingsIntent;
247
Jeff Sharkeya662e492011-06-18 21:57:06 -0700248 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700249
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700250 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700251 private String mIntentTab = null;
252
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700253 private MenuItem mMenuDataRoaming;
254 private MenuItem mMenuRestrictBackground;
255
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700256 /** Flag used to ignore listeners during binding. */
257 private boolean mBinding;
258
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700259 private UidDetailProvider mUidDetailProvider;
260
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700261 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700262 public void onCreate(Bundle savedInstanceState) {
263 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700264 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700265
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700266 mNetworkService = INetworkManagementService.Stub.asInterface(
267 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700268 mStatsService = INetworkStatsService.Stub.asInterface(
269 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700270 mPolicyManager = NetworkPolicyManager.from(context);
271 mConnService = ConnectivityManager.from(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700272
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700273 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700274
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700275 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700276 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700277
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700278 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700279 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700280
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700281 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700282 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700283
Jeff Sharkey8a503642011-06-10 13:31:21 -0700284 @Override
285 public View onCreateView(LayoutInflater inflater, ViewGroup container,
286 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700287
Jeff Sharkey8a503642011-06-10 13:31:21 -0700288 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700289 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
290
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700291 mUidDetailProvider = new UidDetailProvider(context);
292
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700293 try {
294 mStatsSession = mStatsService.openSession();
295 } catch (RemoteException e) {
296 throw new RuntimeException(e);
297 }
298
Jeff Sharkey8a503642011-06-10 13:31:21 -0700299 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700300 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700301 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
302 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700303
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700304 // decide if we need to manually inset our content, or if we should rely
305 // on parent container for inset.
306 final boolean shouldInset = mListView.getScrollBarStyle()
307 == View.SCROLLBARS_OUTSIDE_OVERLAY;
308 if (shouldInset) {
309 mInsetSide = view.getResources().getDimensionPixelOffset(
310 com.android.internal.R.dimen.preference_fragment_padding_side);
311 } else {
312 mInsetSide = 0;
313 }
314
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700315 // adjust padding around tabwidget as needed
Jeff Sharkey5d706792011-09-08 18:57:17 -0700316 prepareCustomPreferencesList(container, view, mListView, true);
317
Jeff Sharkey8a503642011-06-10 13:31:21 -0700318 mTabHost.setup();
319 mTabHost.setOnTabChangedListener(mTabListener);
320
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700321 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700322 mHeader.setClickable(true);
323
324 mListView.addHeaderView(mHeader, null, true);
325 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700326
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700327 if (mInsetSide > 0) {
328 // inset selector and divider drawables
329 insetListViewDrawables(mListView, mInsetSide);
330 mHeader.setPadding(mInsetSide, 0, mInsetSide, 0);
331 }
332
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700333 {
334 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700335 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
336 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700337 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700338
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700339 mDataEnabled = new Switch(inflater.getContext());
340 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
341 mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener);
342 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700343
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700344 mDisableAtLimit = new CheckBox(inflater.getContext());
345 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700346 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700347 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700348 mDisableAtLimitView.setClickable(true);
349 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700350 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
351 mNetworkSwitches.addView(mDisableAtLimitView);
352 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700353
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700354 // bind cycle dropdown
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700355 mCycleView = mHeader.findViewById(R.id.cycles);
356 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700357 mCycleAdapter = new CycleAdapter(context);
358 mCycleSpinner.setAdapter(mCycleAdapter);
359 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
360
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700361 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700362 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800363 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700364
365 {
366 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700367 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700368 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
369 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700370 mAppPieChart = (PieChartView) mAppDetail.findViewById(R.id.app_pie_chart);
371 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
372 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700373 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700374
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700375 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700376 mAppSettings.setOnClickListener(mAppSettingsListener);
377
378 mAppRestrict = new CheckBox(inflater.getContext());
379 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700380 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700381 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700382 mAppRestrictView.setClickable(true);
383 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700384 mAppRestrictView.setOnClickListener(mAppRestrictListener);
385 mAppSwitches.addView(mAppRestrictView);
386 }
387
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700388 mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700389 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700390
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700391 // only assign layout transitions once first layout is finished
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700392 mListView.getViewTreeObserver().addOnGlobalLayoutListener(mFirstLayoutListener);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700393
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700394 mAdapter = new DataUsageAdapter(mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700395 mListView.setOnItemClickListener(mListListener);
396 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700397
398 return view;
399 }
400
401 @Override
402 public void onResume() {
403 super.onResume();
404
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700405 // pick default tab based on incoming intent
406 final Intent intent = getActivity().getIntent();
407 mIntentTab = computeTabFromIntent(intent);
408
Jeff Sharkey8a503642011-06-10 13:31:21 -0700409 // this kicks off chain reaction which creates tabs, binds the body to
410 // selected network, and binds chart, cycles and detail list.
411 updateTabs();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700412
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700413 // kick off background task to update stats
414 new AsyncTask<Void, Void, Void>() {
415 @Override
416 protected Void doInBackground(Void... params) {
417 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700418 // wait a few seconds before kicking off
419 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700420 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700421 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700422 } catch (RemoteException e) {
423 }
424 return null;
425 }
426
427 @Override
428 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700429 if (isAdded()) {
430 updateBody();
431 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700432 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700433 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700434 }
435
Jeff Sharkey8a503642011-06-10 13:31:21 -0700436 @Override
437 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
438 inflater.inflate(R.menu.data_usage, menu);
439 }
440
441 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700442 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700443 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700444 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700445
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700446 mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700447 mMenuDataRoaming.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700448 mMenuDataRoaming.setChecked(getDataRoaming());
449
450 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700451 mMenuRestrictBackground.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
452 mMenuRestrictBackground.setChecked(mPolicyManager.getRestrictBackground());
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700453
454 final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g);
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700455 split4g.setVisible(hasReadyMobile4gRadio(context) && !appDetailMode);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700456 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700457
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700458 final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700459 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700460 showWifi.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700461 showWifi.setChecked(mShowWifi);
462 } else {
463 showWifi.setVisible(false);
464 mShowWifi = true;
465 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700466
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700467 final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700468 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700469 showEthernet.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700470 showEthernet.setChecked(mShowEthernet);
471 } else {
472 showEthernet.setVisible(false);
473 mShowEthernet = true;
474 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700475
476 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
477 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
478 metered.setVisible(!appDetailMode);
479 } else {
480 metered.setVisible(false);
481 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700482
483 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
484 String helpUrl;
485 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
486 Intent helpIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
487 helpIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
488 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
489 help.setIntent(helpIntent);
490 help.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
491 } else {
492 help.setVisible(false);
493 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700494 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700495
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700496 @Override
497 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700498 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700499 case R.id.data_usage_menu_roaming: {
500 final boolean dataRoaming = !item.isChecked();
501 if (dataRoaming) {
502 ConfirmDataRoamingFragment.show(this);
503 } else {
504 // no confirmation to disable roaming
505 setDataRoaming(false);
506 }
507 return true;
508 }
509 case R.id.data_usage_menu_restrict_background: {
510 final boolean restrictBackground = !item.isChecked();
511 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800512 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700513 } else {
514 // no confirmation to drop restriction
515 setRestrictBackground(false);
516 }
517 return true;
518 }
519 case R.id.data_usage_menu_split_4g: {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700520 final boolean mobileSplit = !item.isChecked();
Jeff Sharkeya662e492011-06-18 21:57:06 -0700521 setMobilePolicySplit(mobileSplit);
522 item.setChecked(isMobilePolicySplit());
Jeff Sharkey8a503642011-06-10 13:31:21 -0700523 updateTabs();
524 return true;
525 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700526 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700527 mShowWifi = !item.isChecked();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700528 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700529 item.setChecked(mShowWifi);
530 updateTabs();
531 return true;
532 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700533 case R.id.data_usage_menu_show_ethernet: {
534 mShowEthernet = !item.isChecked();
535 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
536 item.setChecked(mShowEthernet);
537 updateTabs();
538 return true;
539 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700540 case R.id.data_usage_menu_metered: {
541 final PreferenceActivity activity = (PreferenceActivity) getActivity();
542 activity.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
543 R.string.data_usage_metered_title, null, this, 0);
544 return true;
545 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700546 }
547 return false;
548 }
549
Jeff Sharkey94a90952011-06-13 22:31:09 -0700550 @Override
551 public void onDestroyView() {
552 super.onDestroyView();
553
554 mDataEnabledView = null;
555 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700556
557 mUidDetailProvider.clearCache();
558 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700559
560 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700561 }
562
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800563 @Override
564 public void onDestroy() {
565 if (this.isRemoving()) {
566 getFragmentManager()
567 .popBackStack(TAG_APP_DETAILS, FragmentManager.POP_BACK_STACK_INCLUSIVE);
568 }
569 super.onDestroy();
570 }
571
Jeff Sharkey8a503642011-06-10 13:31:21 -0700572 /**
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700573 * Listener to setup {@link LayoutTransition} after first layout pass.
574 */
575 private OnGlobalLayoutListener mFirstLayoutListener = new OnGlobalLayoutListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700576 @Override
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700577 public void onGlobalLayout() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700578 mListView.getViewTreeObserver().removeOnGlobalLayoutListener(mFirstLayoutListener);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700579
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700580 mTabsContainer.setLayoutTransition(buildLayoutTransition());
581 mHeader.setLayoutTransition(buildLayoutTransition());
582 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700583
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700584 final LayoutTransition chartTransition = buildLayoutTransition();
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700585 chartTransition.setStartDelay(LayoutTransition.APPEARING, 0);
586 chartTransition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700587 chartTransition.setAnimator(LayoutTransition.APPEARING, null);
Jeff Sharkey28130d92011-09-02 16:10:24 -0700588 chartTransition.setAnimator(LayoutTransition.DISAPPEARING, null);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700589 mChart.setLayoutTransition(chartTransition);
590 }
591 };
592
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700593 private static LayoutTransition buildLayoutTransition() {
594 final LayoutTransition transition = new LayoutTransition();
595 if (TEST_ANIM) {
596 transition.setDuration(1500);
597 }
598 transition.setAnimateParentHierarchy(false);
599 return transition;
600 }
601
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700602 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700603 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700604 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
605 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700606 */
607 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700608 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700609 mTabHost.clearAllTabs();
610
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700611 final boolean mobileSplit = isMobilePolicySplit();
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700612 if (mobileSplit && hasReadyMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700613 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
614 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700615 } else if (hasReadyMobileRadio(context)) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700616 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700617 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700618 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700619 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
620 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700621 if (mShowEthernet && hasEthernet(context)) {
622 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
623 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700624
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700625 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700626 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
627 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
628 if (mIntentTab != null) {
629 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700630 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700631 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700632 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700633 mTabHost.setCurrentTabByTag(mIntentTab);
634 }
635 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700636 } else if (noTabs) {
637 // no usable tabs, so hide body
638 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700639 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700640 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700641 }
642 }
643
Jeff Sharkey8a503642011-06-10 13:31:21 -0700644 /**
645 * Factory that provide empty {@link View} to make {@link TabHost} happy.
646 */
647 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700648 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700649 public View createTabContent(String tag) {
650 return new View(mTabHost.getContext());
651 }
652 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700653
Jeff Sharkey8a503642011-06-10 13:31:21 -0700654 /**
655 * Build {@link TabSpec} with thin indicator, and empty content.
656 */
657 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700658 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
659 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700660 }
661
662 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700663 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700664 public void onTabChanged(String tabId) {
665 // user changed tab; update body
666 updateBody();
667 }
668 };
669
670 /**
671 * Update body content based on current tab. Loads
672 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
673 * binds them to visible controls.
674 */
675 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700676 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700677 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700678
Jeff Sharkeya662e492011-06-18 21:57:06 -0700679 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700680 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700681
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700682 if (currentTab == null) {
683 Log.w(TAG, "no tab selected; hiding body");
684 mListView.setVisibility(View.GONE);
685 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700686 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700687 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700688 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700689
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700690 final boolean tabChanged = !currentTab.equals(mCurrentTab);
691 mCurrentTab = currentTab;
692
Jeff Sharkey8a503642011-06-10 13:31:21 -0700693 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
694
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700695 mDataEnabledView.setVisibility(View.VISIBLE);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700696
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700697 // TODO: remove mobile tabs when SIM isn't ready
698 final TelephonyManager tele = TelephonyManager.from(context);
699
Jeff Sharkey8a503642011-06-10 13:31:21 -0700700 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700701 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
702 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700703 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700704
705 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700706 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
707 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
708 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700709 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700710
711 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700712 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
713 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
714 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700715 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700716
717 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700718 // wifi doesn't have any controls
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700719 mDataEnabledView.setVisibility(View.GONE);
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700720 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700721 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700722
723 } else if (TAB_ETHERNET.equals(currentTab)) {
724 // ethernet doesn't have any controls
725 mDataEnabledView.setVisibility(View.GONE);
726 mDisableAtLimitView.setVisibility(View.GONE);
727 mTemplate = buildTemplateEthernet();
728
729 } else {
730 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700731 }
732
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700733 // kick off loader for network history
734 // TODO: consider chaining two loaders together instead of reloading
735 // network history when showing app detail.
736 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700737 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700738
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700739 // detail mode can change visible menus, invalidate
740 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700741
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700742 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700743 }
744
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700745 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700746 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700747 }
748
749 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700750 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700751 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700752 */
753 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700754 final Context context = getActivity();
755 final PackageManager pm = context.getPackageManager();
756 final LayoutInflater inflater = getActivity().getLayoutInflater();
757
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700758 if (isAppDetailMode()) {
759 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700760 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700761 } else {
762 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700763 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700764
765 // hide detail stats when not in detail mode
766 mChart.bindDetailNetworkStats(null);
767 return;
768 }
769
770 // remove warning/limit sweeps while in detail mode
771 mChart.bindNetworkPolicy(null);
772
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700773 // show icon and all labels appearing under this app
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700774 final int appId = mCurrentApp.appId;
775 final UidDetail detail = mUidDetailProvider.getUidDetail(appId, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700776 mAppIcon.setImageDrawable(detail.icon);
777
778 mAppTitles.removeAllViews();
779 if (detail.detailLabels != null) {
780 for (CharSequence label : detail.detailLabels) {
781 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label));
782 }
783 } else {
784 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label));
785 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700786
787 // enable settings button when package provides it
788 // TODO: target torwards entire UID instead of just first package
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700789 final String[] packageNames = pm.getPackagesForUid(appId);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700790 if (packageNames != null && packageNames.length > 0) {
791 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
792 mAppSettingsIntent.setPackage(packageNames[0]);
793 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
794
795 final boolean matchFound = pm.resolveActivity(mAppSettingsIntent, 0) != null;
796 mAppSettings.setEnabled(matchFound);
797
798 } else {
799 mAppSettingsIntent = null;
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700800 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700801 }
802
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700803 updateDetailData();
804
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700805 if (UserId.isApp(appId) && !mPolicyManager.getRestrictBackground()
806 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700807 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800808 setPreferenceSummary(mAppRestrictView,
809 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700810
811 mAppRestrictView.setVisibility(View.VISIBLE);
812 mAppRestrict.setChecked(getAppRestrictBackground());
813
814 } else {
815 mAppRestrictView.setVisibility(View.GONE);
816 }
817 }
818
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700819 private void setPolicyWarningBytes(long warningBytes) {
820 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700821 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700822 updatePolicy(false);
823 }
824
825 private void setPolicyLimitBytes(long limitBytes) {
826 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700827 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700828 updatePolicy(false);
829 }
830
Jeff Sharkey28130d92011-09-02 16:10:24 -0700831 /**
832 * Local cache of value, used to work around delay when
833 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
834 */
835 private Boolean mMobileDataEnabled;
836
837 private boolean isMobileDataEnabled() {
838 if (mMobileDataEnabled != null) {
839 // TODO: deprecate and remove this once enabled flag is on policy
840 return mMobileDataEnabled;
841 } else {
842 return mConnService.getMobileDataEnabled();
843 }
844 }
845
846 private void setMobileDataEnabled(boolean enabled) {
847 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
848 mConnService.setMobileDataEnabled(enabled);
849 mMobileDataEnabled = enabled;
850 updatePolicy(false);
851 }
852
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700853 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
854 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked();
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700855 }
856
857 private boolean isBandwidthControlEnabled() {
858 try {
859 return mNetworkService.isBandwidthControlEnabled();
860 } catch (RemoteException e) {
861 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
862 return false;
863 }
864 }
865
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700866 private boolean getDataRoaming() {
867 final ContentResolver resolver = getActivity().getContentResolver();
868 return Settings.Secure.getInt(resolver, Settings.Secure.DATA_ROAMING, 0) != 0;
869 }
870
871 private void setDataRoaming(boolean enabled) {
872 // TODO: teach telephony DataConnectionTracker to watch and apply
873 // updates when changed.
874 final ContentResolver resolver = getActivity().getContentResolver();
875 Settings.Secure.putInt(resolver, Settings.Secure.DATA_ROAMING, enabled ? 1 : 0);
876 mMenuDataRoaming.setChecked(enabled);
877 }
878
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700879 public void setRestrictBackground(boolean restrictBackground) {
880 mPolicyManager.setRestrictBackground(restrictBackground);
881 mMenuRestrictBackground.setChecked(restrictBackground);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700882 }
883
884 private boolean getAppRestrictBackground() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700885 final int appId = mCurrentApp.appId;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700886 final int uidPolicy = mPolicyManager.getAppPolicy(appId);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700887 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
888 }
889
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700890 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700891 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700892 final int appId = mCurrentApp.appId;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700893 mPolicyManager.setAppPolicy(appId,
894 restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700895 mAppRestrict.setChecked(restrictBackground);
896 }
897
Jeff Sharkey8a503642011-06-10 13:31:21 -0700898 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700899 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
900 * current {@link #mTemplate}.
901 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700902 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700903 if (isAppDetailMode()) {
904 mNetworkSwitches.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700905 } else {
906 mNetworkSwitches.setVisibility(View.VISIBLE);
907 }
908
Jeff Sharkey28130d92011-09-02 16:10:24 -0700909 // TODO: move enabled state directly into policy
910 if (TAB_MOBILE.equals(mCurrentTab)) {
911 mBinding = true;
912 mDataEnabled.setChecked(isMobileDataEnabled());
913 mBinding = false;
914 }
915
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700916 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
917 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700918 mDisableAtLimitView.setVisibility(View.VISIBLE);
919 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700920 if (!isAppDetailMode()) {
921 mChart.bindNetworkPolicy(policy);
922 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700923
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700924 } else {
925 // controls are disabled; don't bind warning/limit sweeps
926 mDisableAtLimitView.setVisibility(View.GONE);
927 mChart.bindNetworkPolicy(null);
928 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700929
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700930 if (refreshCycle) {
931 // generate cycle list based on policy and available history
932 updateCycleList(policy);
933 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700934 }
935
936 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700937 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
938 * and available {@link NetworkStatsHistory} data. Always selects the newest
939 * item, updating the inspection range on {@link #mChart}.
940 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700941 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700942 // stash away currently selected cycle to try restoring below
943 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700944 mCycleAdapter.clear();
945
946 final Context context = mCycleSpinner.getContext();
947
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700948 long historyStart = Long.MAX_VALUE;
949 long historyEnd = Long.MIN_VALUE;
950 if (mChartData != null) {
951 historyStart = mChartData.network.getStart();
952 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700953 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700954
Jeff Sharkey461842a2011-09-25 18:22:48 -0700955 final long now = System.currentTimeMillis();
956 if (historyStart == Long.MAX_VALUE) historyStart = now;
957 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700958
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700959 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700960 if (policy != null) {
961 // find the next cycle boundary
962 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700963
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700964 // walk backwards, generating all valid cycle ranges
965 while (cycleEnd > historyStart) {
966 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
967 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
968 + historyStart);
969 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
970 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700971 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700972 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700973
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700974 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700975 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700976 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700977
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700978 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700979 // no policy defined cycles; show entry for each four-week period
980 long cycleEnd = historyEnd;
981 while (cycleEnd > historyStart) {
982 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
983 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
984 cycleEnd = cycleStart;
985 }
986
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700987 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700988 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700989
990 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700991 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700992 final int position = mCycleAdapter.findNearestPosition(previousItem);
993 mCycleSpinner.setSelection(position);
994
995 // only force-update cycle when changed; skipping preserves any
996 // user-defined inspection region.
997 final CycleItem selectedItem = mCycleAdapter.getItem(position);
998 if (!Objects.equal(selectedItem, previousItem)) {
999 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1000 } else {
1001 // but still kick off loader for detailed list
1002 updateDetailData();
1003 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001004 } else {
1005 updateDetailData();
1006 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001007 }
1008
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001009 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001010 @Override
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001011 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
1012 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001013
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001014 final boolean dataEnabled = isChecked;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001015 final String currentTab = mCurrentTab;
1016 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001017 if (dataEnabled) {
1018 setMobileDataEnabled(true);
1019 } else {
1020 // disabling data; show confirmation dialog which eventually
1021 // calls setMobileDataEnabled() once user confirms.
1022 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1023 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001024 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001025
Jeff Sharkey28130d92011-09-02 16:10:24 -07001026 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001027 }
1028 };
1029
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001030 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001031 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001032 public void onClick(View v) {
1033 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001034 if (disableAtLimit) {
1035 // enabling limit; show confirmation dialog which eventually
1036 // calls setPolicyLimitBytes() once user confirms.
1037 ConfirmLimitFragment.show(DataUsageSummary.this);
1038 } else {
1039 setPolicyLimitBytes(LIMIT_DISABLED);
1040 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001041 }
1042 };
1043
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001044 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001045 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001046 public void onClick(View v) {
1047 final boolean restrictBackground = !mAppRestrict.isChecked();
1048
1049 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001050 // enabling restriction; show confirmation dialog which
1051 // eventually calls setRestrictBackground() once user
1052 // confirms.
1053 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001054 } else {
1055 setAppRestrictBackground(false);
1056 }
1057 }
1058 };
1059
1060 private OnClickListener mAppSettingsListener = new OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001061 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001062 public void onClick(View v) {
1063 // TODO: target torwards entire UID instead of just first package
1064 startActivity(mAppSettingsIntent);
1065 }
1066 };
1067
Jeff Sharkey8a503642011-06-10 13:31:21 -07001068 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001069 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001070 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001071 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001072 final AppItem app = (AppItem) parent.getItemAtPosition(position);
1073 final UidDetail detail = mUidDetailProvider.getUidDetail(app.appId, true);
1074 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001075 }
1076 };
1077
1078 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001079 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001080 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1081 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1082 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001083 // show cycle editor; will eventually call setPolicyCycleDay()
1084 // when user finishes editing.
1085 CycleEditorFragment.show(DataUsageSummary.this);
1086
1087 // reset spinner to something other than "change cycle..."
1088 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001089
1090 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001091 if (LOGD) {
1092 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1093 + cycle.end + "]");
1094 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001095
1096 // update chart to show selected cycle, and update detail data
1097 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001098 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001099
1100 updateDetailData();
1101 }
1102 }
1103
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001104 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001105 public void onNothingSelected(AdapterView<?> parent) {
1106 // ignored
1107 }
1108 };
1109
1110 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001111 * Update details based on {@link #mChart} inspection range depending on
1112 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1113 * of applications data usage, and when {@link #isAppDetailMode()} update
1114 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001115 */
1116 private void updateDetailData() {
1117 if (LOGD) Log.d(TAG, "updateDetailData()");
1118
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001119 final long start = mChart.getInspectStart();
1120 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001121 final long now = System.currentTimeMillis();
1122
1123 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001124
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001125 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001126 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001127 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001128 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001129 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001130 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001131 final long foregroundBytes = entry.rxBytes + entry.txBytes;
1132
1133 mAppPieChart.setOriginAngle(175);
1134
1135 mAppPieChart.removeAllSlices();
1136 mAppPieChart.addSlice(foregroundBytes, Color.parseColor("#d88d3a"));
1137 mAppPieChart.addSlice(defaultBytes, Color.parseColor("#666666"));
1138
1139 mAppPieChart.generatePath();
1140
1141 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1142 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1143
1144 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001145 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001146
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001147 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001148
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001149 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001150 if (mChartData != null) {
1151 entry = mChartData.network.getValues(start, end, now, null);
1152 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001153
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001154 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001155 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001156 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001157 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001158
1159 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1160 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001161 final String rangePhrase = formatDateRange(context, start, end);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001162
Jeff Sharkeye557c332012-04-13 16:04:07 -07001163 final int summaryRes;
1164 if (TAB_MOBILE.equals(mCurrentTab) || TAB_3G.equals(mCurrentApp)
1165 || TAB_4G.equals(mCurrentApp)) {
1166 summaryRes = R.string.data_usage_total_during_range_mobile;
1167 } else {
1168 summaryRes = R.string.data_usage_total_during_range;
1169 }
1170
1171 mUsageSummary.setText(getString(summaryRes, totalPhrase, rangePhrase));
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001172 }
1173
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001174 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1175 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001176 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001177 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001178 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001179 }
1180
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001181 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001182 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1183 mChartData = data;
1184 mChart.bindNetworkStats(mChartData.network);
1185 mChart.bindDetailNetworkStats(mChartData.detail);
1186
1187 // calcuate policy cycles based on available data
1188 updatePolicy(true);
1189 updateAppDetail();
1190
1191 // force scroll to top of body when showing detail
1192 if (mChartData.detail != null) {
1193 mListView.smoothScrollToPosition(0);
1194 }
1195 }
1196
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001197 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001198 public void onLoaderReset(Loader<ChartData> loader) {
1199 mChartData = null;
1200 mChart.bindNetworkStats(null);
1201 mChart.bindDetailNetworkStats(null);
1202 }
1203 };
1204
1205 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001206 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001207 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001208 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001209 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001210 }
1211
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001212 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001213 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001214 final int[] restrictedAppIds = mPolicyManager.getAppsWithPolicy(
1215 POLICY_REJECT_METERED_BACKGROUND);
1216 mAdapter.bindStats(data, restrictedAppIds);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001217 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001218 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001219
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001220 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001221 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001222 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001223 updateEmptyVisible();
1224 }
1225
1226 private void updateEmptyVisible() {
1227 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1228 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001229 }
1230 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001231
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001232 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001233 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001234 final Context context = getActivity();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001235 if (hasReadyMobileRadio(context)) {
1236 final TelephonyManager tele = TelephonyManager.from(context);
1237 return mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001238 } else {
1239 return false;
1240 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001241 }
1242
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001243 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001244 private void setMobilePolicySplit(boolean split) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001245 final Context context = getActivity();
1246 if (hasReadyMobileRadio(context)) {
1247 final TelephonyManager tele = TelephonyManager.from(context);
1248 mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split);
1249 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001250 }
1251
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001252 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001253 final TelephonyManager tele = TelephonyManager.from(context);
1254 final String actualSubscriberId = tele.getSubscriberId();
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -08001255 return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001256 }
1257
Jeff Sharkey8a503642011-06-10 13:31:21 -07001258 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001259 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001260 public void onInspectRangeChanged() {
1261 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
1262 updateDetailData();
1263 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001264
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001265 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001266 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001267 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001268 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001269
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001270 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001271 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001272 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001273 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001274
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001275 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001276 public void requestWarningEdit() {
1277 WarningEditorFragment.show(DataUsageSummary.this);
1278 }
1279
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001280 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001281 public void requestLimitEdit() {
1282 LimitEditorFragment.show(DataUsageSummary.this);
1283 }
1284 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001285
1286 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001287 * List item that reflects a specific data usage cycle.
1288 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001289 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001290 public CharSequence label;
1291 public long start;
1292 public long end;
1293
Jeff Sharkey8a503642011-06-10 13:31:21 -07001294 CycleItem(CharSequence label) {
1295 this.label = label;
1296 }
1297
1298 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001299 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001300 this.start = start;
1301 this.end = end;
1302 }
1303
Jeff Sharkey8a503642011-06-10 13:31:21 -07001304 @Override
1305 public String toString() {
1306 return label.toString();
1307 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001308
1309 @Override
1310 public boolean equals(Object o) {
1311 if (o instanceof CycleItem) {
1312 final CycleItem another = (CycleItem) o;
1313 return start == another.start && end == another.end;
1314 }
1315 return false;
1316 }
1317
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001318 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001319 public int compareTo(CycleItem another) {
1320 return Long.compare(start, another.start);
1321 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001322 }
1323
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001324 private static final StringBuilder sBuilder = new StringBuilder(50);
1325 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1326 sBuilder, Locale.getDefault());
1327
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001328 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001329 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001330
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001331 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001332 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001333 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1334 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001335 }
1336 }
1337
Jeff Sharkey8a503642011-06-10 13:31:21 -07001338 /**
1339 * Special-case data usage cycle that triggers dialog to change
1340 * {@link NetworkPolicy#cycleDay}.
1341 */
1342 public static class CycleChangeItem extends CycleItem {
1343 public CycleChangeItem(Context context) {
1344 super(context.getString(R.string.data_usage_change_cycle));
1345 }
1346 }
1347
1348 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001349 private boolean mChangePossible = false;
1350 private boolean mChangeVisible = false;
1351
1352 private final CycleChangeItem mChangeItem;
1353
Jeff Sharkey8a503642011-06-10 13:31:21 -07001354 public CycleAdapter(Context context) {
1355 super(context, android.R.layout.simple_spinner_item);
1356 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001357 mChangeItem = new CycleChangeItem(context);
1358 }
1359
1360 public void setChangePossible(boolean possible) {
1361 mChangePossible = possible;
1362 updateChange();
1363 }
1364
1365 public void setChangeVisible(boolean visible) {
1366 mChangeVisible = visible;
1367 updateChange();
1368 }
1369
1370 private void updateChange() {
1371 remove(mChangeItem);
1372 if (mChangePossible && mChangeVisible) {
1373 add(mChangeItem);
1374 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001375 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001376
1377 /**
1378 * Find position of {@link CycleItem} in this adapter which is nearest
1379 * the given {@link CycleItem}.
1380 */
1381 public int findNearestPosition(CycleItem target) {
1382 if (target != null) {
1383 final int count = getCount();
1384 for (int i = count - 1; i >= 0; i--) {
1385 final CycleItem item = getItem(i);
1386 if (item instanceof CycleChangeItem) {
1387 continue;
1388 } else if (item.compareTo(target) >= 0) {
1389 return i;
1390 }
1391 }
1392 }
1393 return 0;
1394 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001395 }
1396
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001397 public static class AppItem implements Comparable<AppItem>, Parcelable {
1398 public final int appId;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001399 public boolean restricted;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001400 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001401 public long total;
1402
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001403 public AppItem(int appId) {
1404 this.appId = appId;
1405 }
1406
1407 public AppItem(Parcel parcel) {
1408 appId = parcel.readInt();
1409 uids = parcel.readSparseBooleanArray();
1410 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001411 }
1412
1413 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001414 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001415 }
1416
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001417 @Override
1418 public void writeToParcel(Parcel dest, int flags) {
1419 dest.writeInt(appId);
1420 dest.writeSparseBooleanArray(uids);
1421 dest.writeLong(total);
1422 }
1423
1424 @Override
1425 public int describeContents() {
1426 return 0;
1427 }
1428
1429 @Override
1430 public int compareTo(AppItem another) {
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001431 return Long.compare(another.total, total);
1432 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001433
1434 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1435 @Override
1436 public AppItem createFromParcel(Parcel in) {
1437 return new AppItem(in);
1438 }
1439
1440 @Override
1441 public AppItem[] newArray(int size) {
1442 return new AppItem[size];
1443 }
1444 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001445 }
1446
Jeff Sharkey8a503642011-06-10 13:31:21 -07001447 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001448 * Adapter of applications, sorted by total usage descending.
1449 */
1450 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001451 private final UidDetailProvider mProvider;
1452 private final int mInsetSide;
1453
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001454 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001455 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001456
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001457 public DataUsageAdapter(UidDetailProvider provider, int insetSide) {
1458 mProvider = checkNotNull(provider);
1459 mInsetSide = insetSide;
1460 }
1461
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001462 /**
1463 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1464 */
Jeff Sharkeye557c332012-04-13 16:04:07 -07001465 public void bindStats(NetworkStats stats, int[] restrictedAppIds) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001466 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001467
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001468 final AppItem systemItem = new AppItem(android.os.Process.SYSTEM_UID);
1469 final SparseArray<AppItem> knownUids = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001470
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001471 NetworkStats.Entry entry = null;
1472 final int size = stats != null ? stats.size() : 0;
1473 for (int i = 0; i < size; i++) {
1474 entry = stats.getValues(i, entry);
1475
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001476 final boolean isApp = UserId.isApp(entry.uid);
1477 final int appId = isApp ? UserId.getAppId(entry.uid) : entry.uid;
1478 if (isApp || appId == UID_REMOVED || appId == UID_TETHERING) {
1479 AppItem item = knownUids.get(appId);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001480 if (item == null) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001481 item = new AppItem(appId);
1482 knownUids.put(appId, item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001483 mItems.add(item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001484 }
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001485
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001486 item.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001487 item.addUid(entry.uid);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001488 } else {
1489 systemItem.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001490 systemItem.addUid(entry.uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001491 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001492 }
1493
Jeff Sharkeye557c332012-04-13 16:04:07 -07001494 for (int appId : restrictedAppIds) {
1495 AppItem item = knownUids.get(appId);
1496 if (item == null) {
1497 item = new AppItem(appId);
1498 item.total = -1;
1499 mItems.add(item);
1500 }
1501 item.restricted = true;
1502 }
1503
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001504 if (systemItem.total > 0) {
1505 mItems.add(systemItem);
1506 }
1507
Jeff Sharkey8a503642011-06-10 13:31:21 -07001508 Collections.sort(mItems);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001509 mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001510 notifyDataSetChanged();
1511 }
1512
1513 @Override
1514 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001515 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001516 }
1517
1518 @Override
1519 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001520 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001521 }
1522
1523 @Override
1524 public long getItemId(int position) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001525 return mItems.get(position).appId;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001526 }
1527
1528 @Override
1529 public View getView(int position, View convertView, ViewGroup parent) {
1530 if (convertView == null) {
1531 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey5d706792011-09-08 18:57:17 -07001532 R.layout.data_usage_item, parent, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001533
1534 if (mInsetSide > 0) {
1535 convertView.setPadding(mInsetSide, 0, mInsetSide, 0);
1536 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001537 }
1538
1539 final Context context = parent.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001540
Jeff Sharkey28130d92011-09-02 16:10:24 -07001541 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001542 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1543 android.R.id.progress);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001544
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001545 // kick off async load of app details
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001546 final AppItem item = mItems.get(position);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001547 UidDetailTask.bindView(mProvider, item, convertView);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001548
Jeff Sharkeye557c332012-04-13 16:04:07 -07001549 if (item.restricted && item.total <= 0) {
1550 text1.setText(R.string.data_usage_app_restricted);
1551 progress.setVisibility(View.GONE);
1552 } else {
1553 text1.setText(Formatter.formatFileSize(context, item.total));
1554 progress.setVisibility(View.VISIBLE);
1555 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001556
1557 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1558 progress.setProgress(percentTotal);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001559
1560 return convertView;
1561 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001562 }
1563
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001564 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001565 * Empty {@link Fragment} that controls display of UID details in
1566 * {@link DataUsageSummary}.
1567 */
1568 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001569 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001570
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001571 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001572 if (!parent.isAdded()) return;
1573
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001574 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001575 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001576
1577 final AppDetailsFragment fragment = new AppDetailsFragment();
1578 fragment.setArguments(args);
1579 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001580 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1581 ft.add(fragment, TAG_APP_DETAILS);
1582 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001583 ft.setBreadCrumbTitle(label);
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001584 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001585 }
1586
1587 @Override
1588 public void onStart() {
1589 super.onStart();
1590 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001591 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001592 target.updateBody();
1593 }
1594
1595 @Override
1596 public void onStop() {
1597 super.onStop();
1598 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001599 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001600 target.updateBody();
1601 }
1602 }
1603
1604 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001605 * Dialog to request user confirmation before setting
1606 * {@link NetworkPolicy#limitBytes}.
1607 */
1608 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001609 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001610 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001611
1612 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001613 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001614
Jeff Sharkey461842a2011-09-25 18:22:48 -07001615 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001616 final CharSequence message;
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001617 final long minLimitBytes = (long) (
1618 parent.mPolicyEditor.getPolicy(parent.mTemplate).warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001619 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001620
1621 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001622 final String currentTab = parent.mCurrentTab;
1623 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001624 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001625 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001626 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001627 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001628 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001629 } else if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001630 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001631 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001632 } else {
1633 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001634 }
1635
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001636 final Bundle args = new Bundle();
1637 args.putCharSequence(EXTRA_MESSAGE, message);
1638 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1639
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001640 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1641 dialog.setArguments(args);
1642 dialog.setTargetFragment(parent, 0);
1643 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1644 }
1645
1646 @Override
1647 public Dialog onCreateDialog(Bundle savedInstanceState) {
1648 final Context context = getActivity();
1649
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001650 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001651 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1652
1653 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1654 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001655 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001656
1657 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001658 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001659 public void onClick(DialogInterface dialog, int which) {
1660 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1661 if (target != null) {
1662 target.setPolicyLimitBytes(limitBytes);
1663 }
1664 }
1665 });
1666
1667 return builder.create();
1668 }
1669 }
1670
1671 /**
1672 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1673 */
1674 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001675 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001676
1677 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001678 if (!parent.isAdded()) return;
1679
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001680 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001681 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001682
1683 final CycleEditorFragment dialog = new CycleEditorFragment();
1684 dialog.setArguments(args);
1685 dialog.setTargetFragment(parent, 0);
1686 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1687 }
1688
1689 @Override
1690 public Dialog onCreateDialog(Bundle savedInstanceState) {
1691 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001692 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1693 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001694
1695 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1696 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1697
1698 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1699 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1700
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001701 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1702 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001703
1704 cycleDayPicker.setMinValue(1);
1705 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001706 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001707 cycleDayPicker.setWrapSelectorWheel(true);
1708
1709 builder.setTitle(R.string.data_usage_cycle_editor_title);
1710 builder.setView(view);
1711
1712 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1713 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001714 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001715 public void onClick(DialogInterface dialog, int which) {
1716 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001717 final String cycleTimezone = new Time().timezone;
1718 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001719 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001720 }
1721 });
1722
1723 return builder.create();
1724 }
1725 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001726
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001727 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001728 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1729 */
1730 public static class WarningEditorFragment extends DialogFragment {
1731 private static final String EXTRA_TEMPLATE = "template";
1732
1733 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001734 if (!parent.isAdded()) return;
1735
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001736 final Bundle args = new Bundle();
1737 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1738
1739 final WarningEditorFragment dialog = new WarningEditorFragment();
1740 dialog.setArguments(args);
1741 dialog.setTargetFragment(parent, 0);
1742 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1743 }
1744
1745 @Override
1746 public Dialog onCreateDialog(Bundle savedInstanceState) {
1747 final Context context = getActivity();
1748 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1749 final NetworkPolicyEditor editor = target.mPolicyEditor;
1750
1751 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1752 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1753
1754 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1755 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1756
1757 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1758 final long warningBytes = editor.getPolicyWarningBytes(template);
1759 final long limitBytes = editor.getPolicyLimitBytes(template);
1760
1761 bytesPicker.setMinValue(0);
1762 if (limitBytes != LIMIT_DISABLED) {
1763 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1764 } else {
1765 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1766 }
1767 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1768 bytesPicker.setWrapSelectorWheel(false);
1769
1770 builder.setTitle(R.string.data_usage_warning_editor_title);
1771 builder.setView(view);
1772
1773 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1774 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001775 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001776 public void onClick(DialogInterface dialog, int which) {
1777 // clear focus to finish pending text edits
1778 bytesPicker.clearFocus();
1779
1780 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1781 editor.setPolicyWarningBytes(template, bytes);
1782 target.updatePolicy(false);
1783 }
1784 });
1785
1786 return builder.create();
1787 }
1788 }
1789
1790 /**
1791 * Dialog to edit {@link NetworkPolicy#limitBytes}.
1792 */
1793 public static class LimitEditorFragment extends DialogFragment {
1794 private static final String EXTRA_TEMPLATE = "template";
1795
1796 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001797 if (!parent.isAdded()) return;
1798
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001799 final Bundle args = new Bundle();
1800 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1801
1802 final LimitEditorFragment dialog = new LimitEditorFragment();
1803 dialog.setArguments(args);
1804 dialog.setTargetFragment(parent, 0);
1805 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
1806 }
1807
1808 @Override
1809 public Dialog onCreateDialog(Bundle savedInstanceState) {
1810 final Context context = getActivity();
1811 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1812 final NetworkPolicyEditor editor = target.mPolicyEditor;
1813
1814 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1815 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1816
1817 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1818 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1819
1820 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1821 final long warningBytes = editor.getPolicyWarningBytes(template);
1822 final long limitBytes = editor.getPolicyLimitBytes(template);
1823
1824 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09001825 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001826 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
1827 } else {
1828 bytesPicker.setMinValue(0);
1829 }
1830 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
1831 bytesPicker.setWrapSelectorWheel(false);
1832
1833 builder.setTitle(R.string.data_usage_limit_editor_title);
1834 builder.setView(view);
1835
1836 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1837 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001838 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001839 public void onClick(DialogInterface dialog, int which) {
1840 // clear focus to finish pending text edits
1841 bytesPicker.clearFocus();
1842
1843 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1844 editor.setPolicyLimitBytes(template, bytes);
1845 target.updatePolicy(false);
1846 }
1847 });
1848
1849 return builder.create();
1850 }
1851 }
1852 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07001853 * Dialog to request user confirmation before disabling data.
1854 */
1855 public static class ConfirmDataDisableFragment extends DialogFragment {
1856 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001857 if (!parent.isAdded()) return;
1858
Jeff Sharkey28130d92011-09-02 16:10:24 -07001859 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
1860 dialog.setTargetFragment(parent, 0);
1861 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
1862 }
1863
1864 @Override
1865 public Dialog onCreateDialog(Bundle savedInstanceState) {
1866 final Context context = getActivity();
1867
1868 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1869 builder.setMessage(R.string.data_usage_disable_mobile);
1870
1871 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001872 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07001873 public void onClick(DialogInterface dialog, int which) {
1874 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1875 if (target != null) {
1876 // TODO: extend to modify policy enabled flag.
1877 target.setMobileDataEnabled(false);
1878 }
1879 }
1880 });
1881 builder.setNegativeButton(android.R.string.cancel, null);
1882
1883 return builder.create();
1884 }
1885 }
1886
1887 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001888 * Dialog to request user confirmation before setting
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001889 * {@link android.provider.Settings.Secure#DATA_ROAMING}.
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001890 */
1891 public static class ConfirmDataRoamingFragment extends DialogFragment {
1892 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001893 if (!parent.isAdded()) return;
1894
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001895 final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment();
1896 dialog.setTargetFragment(parent, 0);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001897 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_ROAMING);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001898 }
1899
1900 @Override
1901 public Dialog onCreateDialog(Bundle savedInstanceState) {
1902 final Context context = getActivity();
1903
1904 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1905 builder.setTitle(R.string.roaming_reenable_title);
1906 builder.setMessage(R.string.roaming_warning);
1907
1908 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001909 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001910 public void onClick(DialogInterface dialog, int which) {
1911 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1912 if (target != null) {
1913 target.setDataRoaming(true);
1914 }
1915 }
1916 });
1917 builder.setNegativeButton(android.R.string.cancel, null);
1918
1919 return builder.create();
1920 }
1921 }
1922
1923 /**
1924 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001925 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001926 */
1927 public static class ConfirmRestrictFragment extends DialogFragment {
1928 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001929 if (!parent.isAdded()) return;
1930
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001931 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1932 dialog.setTargetFragment(parent, 0);
1933 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1934 }
1935
1936 @Override
1937 public Dialog onCreateDialog(Bundle savedInstanceState) {
1938 final Context context = getActivity();
1939
1940 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001941 builder.setTitle(R.string.data_usage_restrict_background_title);
Jeff Sharkey461842a2011-09-25 18:22:48 -07001942 builder.setMessage(getString(R.string.data_usage_restrict_background));
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001943
1944 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001945 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001946 public void onClick(DialogInterface dialog, int which) {
1947 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1948 if (target != null) {
1949 target.setRestrictBackground(true);
1950 }
1951 }
1952 });
1953 builder.setNegativeButton(android.R.string.cancel, null);
1954
1955 return builder.create();
1956 }
1957 }
1958
1959 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001960 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
1961 * change has been denied, usually based on
1962 * {@link DataUsageSummary#hasLimitedNetworks()}.
1963 */
1964 public static class DeniedRestrictFragment extends DialogFragment {
1965 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001966 if (!parent.isAdded()) return;
1967
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001968 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
1969 dialog.setTargetFragment(parent, 0);
1970 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
1971 }
1972
1973 @Override
1974 public Dialog onCreateDialog(Bundle savedInstanceState) {
1975 final Context context = getActivity();
1976
1977 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1978 builder.setTitle(R.string.data_usage_app_restrict_background);
1979 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
1980 builder.setPositiveButton(android.R.string.ok, null);
1981
1982 return builder.create();
1983 }
1984 }
1985
1986 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001987 * Dialog to request user confirmation before setting
1988 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
1989 */
1990 public static class ConfirmAppRestrictFragment extends DialogFragment {
1991 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001992 if (!parent.isAdded()) return;
1993
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001994 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
1995 dialog.setTargetFragment(parent, 0);
1996 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
1997 }
1998
1999 @Override
2000 public Dialog onCreateDialog(Bundle savedInstanceState) {
2001 final Context context = getActivity();
2002
2003 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002004 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2005 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2006
2007 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002008 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002009 public void onClick(DialogInterface dialog, int which) {
2010 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2011 if (target != null) {
2012 target.setAppRestrictBackground(true);
2013 }
2014 }
2015 });
2016 builder.setNegativeButton(android.R.string.cancel, null);
2017
2018 return builder.create();
2019 }
2020 }
2021
2022 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002023 * Compute default tab that should be selected, based on
2024 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2025 */
2026 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002027 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
2028 if (template == null) return null;
2029
2030 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002031 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002032 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002033 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002034 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002035 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002036 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002037 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002038 return TAB_WIFI;
2039 default:
2040 return null;
2041 }
2042 }
2043
2044 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002045 * Background task that loads {@link UidDetail}, binding to
2046 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002047 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002048 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2049 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002050 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002051 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002052
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002053 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002054 mProvider = checkNotNull(provider);
2055 mItem = checkNotNull(item);
2056 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002057 }
2058
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002059 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002060 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002061 final UidDetailTask existing = (UidDetailTask) target.getTag();
2062 if (existing != null) {
2063 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002064 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002065
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002066 final UidDetail cachedDetail = provider.getUidDetail(item.appId, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002067 if (cachedDetail != null) {
2068 bindView(cachedDetail, target);
2069 } else {
2070 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2071 AsyncTask.THREAD_POOL_EXECUTOR));
2072 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002073 }
2074
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002075 private static void bindView(UidDetail detail, View target) {
2076 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2077 final TextView title = (TextView) target.findViewById(android.R.id.title);
2078
2079 if (detail != null) {
2080 icon.setImageDrawable(detail.icon);
2081 title.setText(detail.label);
2082 } else {
2083 icon.setImageDrawable(null);
2084 title.setText(null);
2085 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002086 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002087
2088 @Override
2089 protected void onPreExecute() {
2090 bindView(null, mTarget);
2091 }
2092
2093 @Override
2094 protected UidDetail doInBackground(Void... params) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002095 return mProvider.getUidDetail(mItem.appId, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002096 }
2097
2098 @Override
2099 protected void onPostExecute(UidDetail result) {
2100 bindView(result, mTarget);
2101 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002102 }
2103
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002104 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002105 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002106 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002107 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002108 if (TEST_RADIOS) {
2109 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2110 }
2111
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002112 final ConnectivityManager conn = ConnectivityManager.from(context);
2113 final TelephonyManager tele = TelephonyManager.from(context);
2114
2115 // require both supported network and ready SIM
2116 return conn.isNetworkSupported(TYPE_MOBILE) && tele.getSimState() == SIM_STATE_READY;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002117 }
2118
2119 /**
2120 * Test if device has a mobile 4G data radio.
2121 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002122 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002123 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2124 return false;
2125 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002126 if (TEST_RADIOS) {
2127 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2128 }
2129
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002130 final ConnectivityManager conn = ConnectivityManager.from(context);
2131 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002132
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002133 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002134 final boolean hasLte = (tele.getLteOnCdmaMode() == Phone.LTE_ON_CDMA_TRUE)
2135 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002136 return hasWimax || hasLte;
2137 }
2138
2139 /**
2140 * Test if device has a Wi-Fi data radio.
2141 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002142 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002143 if (TEST_RADIOS) {
2144 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2145 }
2146
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002147 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002148 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002149 }
2150
2151 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002152 * Test if device has an ethernet network connection.
2153 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002154 public static boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002155 if (TEST_RADIOS) {
2156 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2157 }
2158
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002159 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002160 return conn.isNetworkSupported(TYPE_ETHERNET);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002161 }
2162
2163 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002164 * Inflate a {@link Preference} style layout, adding the given {@link View}
2165 * widget into {@link android.R.id#widget_frame}.
2166 */
2167 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2168 final View view = inflater.inflate(R.layout.preference, root, false);
2169 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2170 android.R.id.widget_frame);
2171 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2172 return view;
2173 }
2174
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002175 private static View inflateAppTitle(
2176 LayoutInflater inflater, ViewGroup root, CharSequence label) {
2177 final TextView view = (TextView) inflater.inflate(
2178 R.layout.data_usage_app_title, root, false);
2179 view.setText(label);
2180 return view;
2181 }
2182
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002183 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002184 * Test if any networks are currently limited.
2185 */
2186 private boolean hasLimitedNetworks() {
2187 return !buildLimitedNetworksList().isEmpty();
2188 }
2189
2190 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002191 * Build string describing currently limited networks, which defines when
2192 * background data is restricted.
2193 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002194 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002195 private CharSequence buildLimitedNetworksString() {
2196 final List<CharSequence> limited = buildLimitedNetworksList();
2197
2198 // handle case where no networks limited
2199 if (limited.isEmpty()) {
2200 limited.add(getText(R.string.data_usage_list_none));
2201 }
2202
2203 return TextUtils.join(limited);
2204 }
2205
2206 /**
2207 * Build list of currently limited networks, which defines when background
2208 * data is restricted.
2209 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002210 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002211 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002212 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002213
2214 // build combined list of all limited networks
2215 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002216
2217 final TelephonyManager tele = TelephonyManager.from(context);
2218 if (tele.getSimState() == SIM_STATE_READY) {
2219 final String subscriberId = getActiveSubscriberId(context);
2220 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2221 limited.add(getText(R.string.data_usage_list_mobile));
2222 }
2223 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2224 limited.add(getText(R.string.data_usage_tab_3g));
2225 }
2226 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2227 limited.add(getText(R.string.data_usage_tab_4g));
2228 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002229 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002230
2231 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002232 limited.add(getText(R.string.data_usage_tab_wifi));
2233 }
2234 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2235 limited.add(getText(R.string.data_usage_tab_ethernet));
2236 }
2237
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002238 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002239 }
2240
2241 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002242 * Inset both selector and divider {@link Drawable} on the given
2243 * {@link ListView} by the requested dimensions.
2244 */
2245 private static void insetListViewDrawables(ListView view, int insetSide) {
2246 final Drawable selector = view.getSelector();
2247 final Drawable divider = view.getDivider();
2248
2249 // fully unregister these drawables so callbacks can be maintained after
2250 // wrapping below.
2251 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2252 view.setSelector(stub);
2253 view.setDivider(stub);
2254
2255 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2256 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2257 }
2258
2259 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002260 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002261 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002262 */
2263 private static void setPreferenceTitle(View parent, int resId) {
2264 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2265 title.setText(resId);
2266 }
2267
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002268 /**
2269 * Set {@link android.R.id#summary} for a preference view inflated with
2270 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2271 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002272 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002273 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2274 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002275 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002276 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002277}