blob: 8dc3d53e6926d22ee8b11879242f78d1768b5c62 [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 Sharkey38305fb2012-09-14 16:26:51 -070051import android.app.ActivityManager;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070052import android.app.AlertDialog;
53import android.app.Dialog;
54import android.app.DialogFragment;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070055import android.app.Fragment;
Amith Yamasani5ba0a022011-11-07 12:29:00 -080056import android.app.FragmentManager;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070057import android.app.FragmentTransaction;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070058import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070059import android.content.ContentResolver;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070060import android.content.Context;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070061import android.content.DialogInterface;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -070062import android.content.Intent;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070063import android.content.Loader;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070064import android.content.SharedPreferences;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070065import android.content.pm.PackageManager;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070066import android.content.res.Resources;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070067import android.graphics.Color;
Jeff Sharkey5d706792011-09-08 18:57:17 -070068import android.graphics.drawable.ColorDrawable;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070069import android.graphics.drawable.Drawable;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070070import android.net.ConnectivityManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070071import android.net.INetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070072import android.net.INetworkStatsService;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070073import android.net.INetworkStatsSession;
Jeff Sharkey8a503642011-06-10 13:31:21 -070074import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070075import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070076import android.net.NetworkStats;
77import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070078import android.net.NetworkTemplate;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070079import android.net.TrafficStats;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070080import android.net.Uri;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070081import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070082import android.os.Bundle;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070083import android.os.INetworkManagementService;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070084import android.os.Parcel;
85import android.os.Parcelable;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070086import android.os.RemoteException;
87import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070088import android.os.SystemProperties;
Dianne Hackbornbb06a422012-08-16 11:01:57 -070089import android.os.UserHandle;
Jeff Sharkey8a503642011-06-10 13:31:21 -070090import android.preference.Preference;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070091import android.preference.PreferenceActivity;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070092import android.provider.Settings;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070093import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070094import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070095import android.text.format.DateUtils;
96import android.text.format.Formatter;
Jeff Sharkeye5223a02012-03-09 17:11:14 -080097import android.text.format.Time;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070098import android.util.Log;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070099import android.util.SparseArray;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700100import android.util.SparseBooleanArray;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700101import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700102import android.view.Menu;
103import android.view.MenuInflater;
104import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700105import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700106import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700107import android.view.ViewGroup;
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
Wink Saville55434042012-06-14 12:33:43 -0700131import com.android.internal.telephony.PhoneConstants;
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";
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700182 private static final String TAG_CONFIRM_AUTO_SYNC_CHANGE = "confirmAutoSyncChange";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700183 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700184
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700185 private static final int LOADER_CHART_DATA = 2;
186 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700187
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700188 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700189 private INetworkStatsService mStatsService;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700190 private NetworkPolicyManager mPolicyManager;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700191 private ConnectivityManager mConnService;
192
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700193 private INetworkStatsSession mStatsSession;
194
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700195 private static final String PREF_FILE = "data_usage";
196 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700197 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700198
199 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700200
Jeff Sharkey8a503642011-06-10 13:31:21 -0700201 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700202 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700203 private TabWidget mTabWidget;
204 private ListView mListView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700205 private DataUsageAdapter mAdapter;
206
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700207 /** Distance to inset content from sides, when needed. */
208 private int mInsetSide = 0;
209
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700210 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700211
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700212 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700213 private LinearLayout mNetworkSwitches;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700214 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700215 private View mDataEnabledView;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700216 private CheckBox mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700217 private View mDisableAtLimitView;
218
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700219 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700220 private Spinner mCycleSpinner;
221 private CycleAdapter mCycleAdapter;
222
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700223 private ChartDataUsageView mChart;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700224 private TextView mUsageSummary;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700225 private TextView mEmpty;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700226
227 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700228 private ImageView mAppIcon;
229 private ViewGroup mAppTitles;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700230 private PieChartView mAppPieChart;
231 private TextView mAppForeground;
232 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700233 private Button mAppSettings;
234
235 private LinearLayout mAppSwitches;
236 private CheckBox mAppRestrict;
237 private View mAppRestrictView;
238
Jeff Sharkey8a503642011-06-10 13:31:21 -0700239 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700240 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700241
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700242 private NetworkTemplate mTemplate;
243 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700244
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700245 private AppItem mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700246
247 private Intent mAppSettingsIntent;
248
Jeff Sharkeya662e492011-06-18 21:57:06 -0700249 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700250
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700251 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700252 private String mIntentTab = null;
253
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700254 private MenuItem mMenuDataRoaming;
255 private MenuItem mMenuRestrictBackground;
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700256 private MenuItem mMenuAutoSync;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700257
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700258 /** Flag used to ignore listeners during binding. */
259 private boolean mBinding;
260
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700261 private UidDetailProvider mUidDetailProvider;
262
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700263 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700264 public void onCreate(Bundle savedInstanceState) {
265 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700266 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700267
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700268 mNetworkService = INetworkManagementService.Stub.asInterface(
269 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700270 mStatsService = INetworkStatsService.Stub.asInterface(
271 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700272 mPolicyManager = NetworkPolicyManager.from(context);
273 mConnService = ConnectivityManager.from(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700274
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700275 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700276
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700277 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700278 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700279
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700280 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700281 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700282
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700283 // override preferences when no mobile radio
284 if (!hasReadyMobileRadio(context)) {
285 mShowWifi = hasWifiRadio(context);
286 mShowEthernet = hasEthernet(context);
287 }
288
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700289 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700290 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700291
Jeff Sharkey8a503642011-06-10 13:31:21 -0700292 @Override
293 public View onCreateView(LayoutInflater inflater, ViewGroup container,
294 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700295
Jeff Sharkey8a503642011-06-10 13:31:21 -0700296 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700297 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
298
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700299 mUidDetailProvider = new UidDetailProvider(context);
300
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700301 try {
302 mStatsSession = mStatsService.openSession();
303 } catch (RemoteException e) {
304 throw new RuntimeException(e);
305 }
306
Jeff Sharkey8a503642011-06-10 13:31:21 -0700307 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700308 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700309 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
310 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700311
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700312 // decide if we need to manually inset our content, or if we should rely
313 // on parent container for inset.
314 final boolean shouldInset = mListView.getScrollBarStyle()
315 == View.SCROLLBARS_OUTSIDE_OVERLAY;
316 if (shouldInset) {
317 mInsetSide = view.getResources().getDimensionPixelOffset(
318 com.android.internal.R.dimen.preference_fragment_padding_side);
319 } else {
320 mInsetSide = 0;
321 }
322
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700323 // adjust padding around tabwidget as needed
Jeff Sharkey5d706792011-09-08 18:57:17 -0700324 prepareCustomPreferencesList(container, view, mListView, true);
325
Jeff Sharkey8a503642011-06-10 13:31:21 -0700326 mTabHost.setup();
327 mTabHost.setOnTabChangedListener(mTabListener);
328
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700329 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700330 mHeader.setClickable(true);
331
332 mListView.addHeaderView(mHeader, null, true);
333 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700334
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700335 if (mInsetSide > 0) {
336 // inset selector and divider drawables
337 insetListViewDrawables(mListView, mInsetSide);
338 mHeader.setPadding(mInsetSide, 0, mInsetSide, 0);
339 }
340
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700341 {
342 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700343 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
344 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700345 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700346
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700347 mDataEnabled = new Switch(inflater.getContext());
348 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
349 mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener);
350 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700351
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700352 mDisableAtLimit = new CheckBox(inflater.getContext());
353 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700354 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700355 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700356 mDisableAtLimitView.setClickable(true);
357 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700358 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
359 mNetworkSwitches.addView(mDisableAtLimitView);
360 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700361
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700362 // bind cycle dropdown
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700363 mCycleView = mHeader.findViewById(R.id.cycles);
364 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700365 mCycleAdapter = new CycleAdapter(context);
366 mCycleSpinner.setAdapter(mCycleAdapter);
367 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
368
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700369 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700370 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800371 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700372
373 {
374 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700375 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700376 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
377 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700378 mAppPieChart = (PieChartView) mAppDetail.findViewById(R.id.app_pie_chart);
379 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
380 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700381 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700382
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700383 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700384 mAppSettings.setOnClickListener(mAppSettingsListener);
385
386 mAppRestrict = new CheckBox(inflater.getContext());
387 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700388 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700389 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700390 mAppRestrictView.setClickable(true);
391 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700392 mAppRestrictView.setOnClickListener(mAppRestrictListener);
393 mAppSwitches.addView(mAppRestrictView);
394 }
395
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700396 mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700397 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700398
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700399 mAdapter = new DataUsageAdapter(mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700400 mListView.setOnItemClickListener(mListListener);
401 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700402
403 return view;
404 }
405
406 @Override
407 public void onResume() {
408 super.onResume();
409
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700410 // pick default tab based on incoming intent
411 final Intent intent = getActivity().getIntent();
412 mIntentTab = computeTabFromIntent(intent);
413
Jeff Sharkey8a503642011-06-10 13:31:21 -0700414 // this kicks off chain reaction which creates tabs, binds the body to
415 // selected network, and binds chart, cycles and detail list.
416 updateTabs();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700417
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700418 // kick off background task to update stats
419 new AsyncTask<Void, Void, Void>() {
420 @Override
421 protected Void doInBackground(Void... params) {
422 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700423 // wait a few seconds before kicking off
424 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700425 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700426 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700427 } catch (RemoteException e) {
428 }
429 return null;
430 }
431
432 @Override
433 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700434 if (isAdded()) {
435 updateBody();
436 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700437 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700438 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700439 }
440
Jeff Sharkey8a503642011-06-10 13:31:21 -0700441 @Override
442 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
443 inflater.inflate(R.menu.data_usage, menu);
444 }
445
446 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700447 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700448 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700449 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700450 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700451
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700452 mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700453 mMenuDataRoaming.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700454 mMenuDataRoaming.setChecked(getDataRoaming());
455
456 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700457 mMenuRestrictBackground.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
458 mMenuRestrictBackground.setChecked(mPolicyManager.getRestrictBackground());
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700459 mMenuRestrictBackground.setVisible(isOwner);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700460
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700461 mMenuAutoSync = menu.findItem(R.id.data_usage_menu_auto_sync);
462 mMenuAutoSync.setChecked(ContentResolver.getMasterSyncAutomatically());
Jeff Sharkeyfda48e32012-09-18 15:03:47 -0700463 mMenuAutoSync.setVisible(!appDetailMode);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700464
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700465 final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g);
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700466 split4g.setVisible(hasReadyMobile4gRadio(context) && isOwner && !appDetailMode);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700467 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700468
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700469 final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700470 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700471 showWifi.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700472 showWifi.setChecked(mShowWifi);
473 } else {
474 showWifi.setVisible(false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700475 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700476
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700477 final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700478 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700479 showEthernet.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700480 showEthernet.setChecked(mShowEthernet);
481 } else {
482 showEthernet.setVisible(false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700483 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700484
485 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
486 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700487 metered.setVisible(!appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700488 } else {
489 metered.setVisible(false);
490 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700491
492 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
493 String helpUrl;
494 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
495 Intent helpIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
496 helpIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
497 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
498 help.setIntent(helpIntent);
499 help.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
500 } else {
501 help.setVisible(false);
502 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700503 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700504
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700505 @Override
506 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700507 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700508 case R.id.data_usage_menu_roaming: {
509 final boolean dataRoaming = !item.isChecked();
510 if (dataRoaming) {
511 ConfirmDataRoamingFragment.show(this);
512 } else {
513 // no confirmation to disable roaming
514 setDataRoaming(false);
515 }
516 return true;
517 }
518 case R.id.data_usage_menu_restrict_background: {
519 final boolean restrictBackground = !item.isChecked();
520 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800521 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700522 } else {
523 // no confirmation to drop restriction
524 setRestrictBackground(false);
525 }
526 return true;
527 }
528 case R.id.data_usage_menu_split_4g: {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700529 final boolean mobileSplit = !item.isChecked();
Jeff Sharkeya662e492011-06-18 21:57:06 -0700530 setMobilePolicySplit(mobileSplit);
531 item.setChecked(isMobilePolicySplit());
Jeff Sharkey8a503642011-06-10 13:31:21 -0700532 updateTabs();
533 return true;
534 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700535 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700536 mShowWifi = !item.isChecked();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700537 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700538 item.setChecked(mShowWifi);
539 updateTabs();
540 return true;
541 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700542 case R.id.data_usage_menu_show_ethernet: {
543 mShowEthernet = !item.isChecked();
544 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
545 item.setChecked(mShowEthernet);
546 updateTabs();
547 return true;
548 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700549 case R.id.data_usage_menu_metered: {
550 final PreferenceActivity activity = (PreferenceActivity) getActivity();
551 activity.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
552 R.string.data_usage_metered_title, null, this, 0);
553 return true;
554 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700555 case R.id.data_usage_menu_auto_sync: {
Guang Zhu167ba2a2012-10-19 17:55:05 -0700556 if (ActivityManager.isUserAMonkey()) {
557 Log.d("SyncState", "ignoring monkey's attempt to flip global sync state");
558 } else {
559 ConfirmAutoSyncChangeFragment.show(this, !item.isChecked());
560 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700561 return true;
562 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700563 }
564 return false;
565 }
566
Jeff Sharkey94a90952011-06-13 22:31:09 -0700567 @Override
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700568 public void onDestroy() {
Jeff Sharkey94a90952011-06-13 22:31:09 -0700569 mDataEnabledView = null;
570 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700571
572 mUidDetailProvider.clearCache();
573 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700574
575 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700576
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800577 if (this.isRemoving()) {
578 getFragmentManager()
579 .popBackStack(TAG_APP_DETAILS, FragmentManager.POP_BACK_STACK_INCLUSIVE);
580 }
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700581
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800582 super.onDestroy();
583 }
584
Jeff Sharkey8a503642011-06-10 13:31:21 -0700585 /**
Jeff Sharkey92811822012-05-04 11:47:29 -0700586 * Build and assign {@link LayoutTransition} to various containers. Should
587 * only be assigned after initial layout is complete.
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700588 */
Jeff Sharkey92811822012-05-04 11:47:29 -0700589 private void ensureLayoutTransitions() {
590 // skip when already setup
591 if (mChart.getLayoutTransition() != null) return;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700592
Jeff Sharkey92811822012-05-04 11:47:29 -0700593 mTabsContainer.setLayoutTransition(buildLayoutTransition());
594 mHeader.setLayoutTransition(buildLayoutTransition());
595 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700596
Jeff Sharkey92811822012-05-04 11:47:29 -0700597 final LayoutTransition chartTransition = buildLayoutTransition();
598 chartTransition.disableTransitionType(LayoutTransition.APPEARING);
599 chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
600 mChart.setLayoutTransition(chartTransition);
601 }
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700602
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700603 private static LayoutTransition buildLayoutTransition() {
604 final LayoutTransition transition = new LayoutTransition();
605 if (TEST_ANIM) {
606 transition.setDuration(1500);
607 }
608 transition.setAnimateParentHierarchy(false);
609 return transition;
610 }
611
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700612 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700613 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700614 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
615 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700616 */
617 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700618 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700619 mTabHost.clearAllTabs();
620
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700621 final boolean mobileSplit = isMobilePolicySplit();
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700622 if (mobileSplit && hasReadyMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700623 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
624 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700625 } else if (hasReadyMobileRadio(context)) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700626 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700627 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700628 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700629 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
630 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700631 if (mShowEthernet && hasEthernet(context)) {
632 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
633 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700634
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700635 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700636 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
637 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
638 if (mIntentTab != null) {
639 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700640 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700641 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700642 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700643 mTabHost.setCurrentTabByTag(mIntentTab);
644 }
645 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700646 } else if (noTabs) {
647 // no usable tabs, so hide body
648 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700649 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700650 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700651 }
652 }
653
Jeff Sharkey8a503642011-06-10 13:31:21 -0700654 /**
655 * Factory that provide empty {@link View} to make {@link TabHost} happy.
656 */
657 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700658 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700659 public View createTabContent(String tag) {
660 return new View(mTabHost.getContext());
661 }
662 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700663
Jeff Sharkey8a503642011-06-10 13:31:21 -0700664 /**
665 * Build {@link TabSpec} with thin indicator, and empty content.
666 */
667 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700668 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
669 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700670 }
671
672 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700673 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700674 public void onTabChanged(String tabId) {
675 // user changed tab; update body
676 updateBody();
677 }
678 };
679
680 /**
681 * Update body content based on current tab. Loads
682 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
683 * binds them to visible controls.
684 */
685 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700686 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700687 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700688
Jeff Sharkeya662e492011-06-18 21:57:06 -0700689 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700690 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700691 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700692
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700693 if (currentTab == null) {
694 Log.w(TAG, "no tab selected; hiding body");
695 mListView.setVisibility(View.GONE);
696 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700697 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700698 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700699 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700700
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700701 final boolean tabChanged = !currentTab.equals(mCurrentTab);
702 mCurrentTab = currentTab;
703
Jeff Sharkey8a503642011-06-10 13:31:21 -0700704 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
705
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700706 mDataEnabledView.setVisibility(isOwner ? View.VISIBLE : View.GONE);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700707
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700708 // TODO: remove mobile tabs when SIM isn't ready
709 final TelephonyManager tele = TelephonyManager.from(context);
710
Jeff Sharkey8a503642011-06-10 13:31:21 -0700711 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700712 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
713 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700714 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700715
716 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700717 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
718 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
719 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700720 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700721
722 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700723 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
724 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
725 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700726 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700727
728 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700729 // wifi doesn't have any controls
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700730 mDataEnabledView.setVisibility(View.GONE);
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700731 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700732 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700733
734 } else if (TAB_ETHERNET.equals(currentTab)) {
735 // ethernet doesn't have any controls
736 mDataEnabledView.setVisibility(View.GONE);
737 mDisableAtLimitView.setVisibility(View.GONE);
738 mTemplate = buildTemplateEthernet();
739
740 } else {
741 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700742 }
743
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700744 // kick off loader for network history
745 // TODO: consider chaining two loaders together instead of reloading
746 // network history when showing app detail.
747 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700748 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700749
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700750 // detail mode can change visible menus, invalidate
751 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700752
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700753 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700754 }
755
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700756 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700757 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700758 }
759
760 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700761 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700762 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700763 */
764 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700765 final Context context = getActivity();
766 final PackageManager pm = context.getPackageManager();
767 final LayoutInflater inflater = getActivity().getLayoutInflater();
768
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700769 if (isAppDetailMode()) {
770 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700771 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700772 } else {
773 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700774 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700775
776 // hide detail stats when not in detail mode
777 mChart.bindDetailNetworkStats(null);
778 return;
779 }
780
781 // remove warning/limit sweeps while in detail mode
782 mChart.bindNetworkPolicy(null);
783
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700784 // show icon and all labels appearing under this app
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700785 final int uid = mCurrentApp.key;
786 final UidDetail detail = mUidDetailProvider.getUidDetail(uid, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700787 mAppIcon.setImageDrawable(detail.icon);
788
789 mAppTitles.removeAllViews();
790 if (detail.detailLabels != null) {
791 for (CharSequence label : detail.detailLabels) {
792 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label));
793 }
794 } else {
795 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label));
796 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700797
798 // enable settings button when package provides it
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700799 final String[] packageNames = pm.getPackagesForUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700800 if (packageNames != null && packageNames.length > 0) {
801 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700802 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
803
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700804 // Search for match across all packages
805 boolean matchFound = false;
806 for (String packageName : packageNames) {
807 mAppSettingsIntent.setPackage(packageName);
808 if (pm.resolveActivity(mAppSettingsIntent, 0) != null) {
809 matchFound = true;
810 break;
811 }
812 }
813
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700814 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -0700815 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700816
817 } else {
818 mAppSettingsIntent = null;
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700819 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700820 }
821
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700822 updateDetailData();
823
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700824 if (UserHandle.isApp(uid) && !mPolicyManager.getRestrictBackground()
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700825 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700826 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800827 setPreferenceSummary(mAppRestrictView,
828 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700829
830 mAppRestrictView.setVisibility(View.VISIBLE);
831 mAppRestrict.setChecked(getAppRestrictBackground());
832
833 } else {
834 mAppRestrictView.setVisibility(View.GONE);
835 }
836 }
837
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700838 private void setPolicyWarningBytes(long warningBytes) {
839 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700840 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700841 updatePolicy(false);
842 }
843
844 private void setPolicyLimitBytes(long limitBytes) {
845 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700846 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700847 updatePolicy(false);
848 }
849
Jeff Sharkey28130d92011-09-02 16:10:24 -0700850 /**
851 * Local cache of value, used to work around delay when
852 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
853 */
854 private Boolean mMobileDataEnabled;
855
856 private boolean isMobileDataEnabled() {
857 if (mMobileDataEnabled != null) {
858 // TODO: deprecate and remove this once enabled flag is on policy
859 return mMobileDataEnabled;
860 } else {
861 return mConnService.getMobileDataEnabled();
862 }
863 }
864
865 private void setMobileDataEnabled(boolean enabled) {
866 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
867 mConnService.setMobileDataEnabled(enabled);
868 mMobileDataEnabled = enabled;
869 updatePolicy(false);
870 }
871
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700872 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700873 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked()
874 && ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700875 }
876
877 private boolean isBandwidthControlEnabled() {
878 try {
879 return mNetworkService.isBandwidthControlEnabled();
880 } catch (RemoteException e) {
881 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
882 return false;
883 }
884 }
885
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700886 private boolean getDataRoaming() {
887 final ContentResolver resolver = getActivity().getContentResolver();
Christopher Tate5a64c732012-09-07 13:35:31 -0700888 return Settings.Global.getInt(resolver, Settings.Global.DATA_ROAMING, 0) != 0;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700889 }
890
891 private void setDataRoaming(boolean enabled) {
892 // TODO: teach telephony DataConnectionTracker to watch and apply
893 // updates when changed.
894 final ContentResolver resolver = getActivity().getContentResolver();
Christopher Tate5a64c732012-09-07 13:35:31 -0700895 Settings.Global.putInt(resolver, Settings.Global.DATA_ROAMING, enabled ? 1 : 0);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700896 mMenuDataRoaming.setChecked(enabled);
897 }
898
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700899 public void setRestrictBackground(boolean restrictBackground) {
900 mPolicyManager.setRestrictBackground(restrictBackground);
901 mMenuRestrictBackground.setChecked(restrictBackground);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700902 }
903
904 private boolean getAppRestrictBackground() {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700905 final int uid = mCurrentApp.key;
906 final int uidPolicy = mPolicyManager.getUidPolicy(uid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700907 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
908 }
909
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700910 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700911 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700912 final int uid = mCurrentApp.key;
913 mPolicyManager.setUidPolicy(
914 uid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700915 mAppRestrict.setChecked(restrictBackground);
916 }
917
Jeff Sharkey8a503642011-06-10 13:31:21 -0700918 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700919 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
920 * current {@link #mTemplate}.
921 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700922 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700923 if (isAppDetailMode()) {
924 mNetworkSwitches.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700925 } else {
926 mNetworkSwitches.setVisibility(View.VISIBLE);
927 }
928
Jeff Sharkey28130d92011-09-02 16:10:24 -0700929 // TODO: move enabled state directly into policy
930 if (TAB_MOBILE.equals(mCurrentTab)) {
931 mBinding = true;
932 mDataEnabled.setChecked(isMobileDataEnabled());
933 mBinding = false;
934 }
935
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700936 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
937 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700938 mDisableAtLimitView.setVisibility(View.VISIBLE);
939 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700940 if (!isAppDetailMode()) {
941 mChart.bindNetworkPolicy(policy);
942 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700943
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700944 } else {
945 // controls are disabled; don't bind warning/limit sweeps
946 mDisableAtLimitView.setVisibility(View.GONE);
947 mChart.bindNetworkPolicy(null);
948 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700949
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700950 if (refreshCycle) {
951 // generate cycle list based on policy and available history
952 updateCycleList(policy);
953 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700954 }
955
956 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700957 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
958 * and available {@link NetworkStatsHistory} data. Always selects the newest
959 * item, updating the inspection range on {@link #mChart}.
960 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700961 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700962 // stash away currently selected cycle to try restoring below
963 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700964 mCycleAdapter.clear();
965
966 final Context context = mCycleSpinner.getContext();
967
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700968 long historyStart = Long.MAX_VALUE;
969 long historyEnd = Long.MIN_VALUE;
970 if (mChartData != null) {
971 historyStart = mChartData.network.getStart();
972 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700973 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700974
Jeff Sharkey461842a2011-09-25 18:22:48 -0700975 final long now = System.currentTimeMillis();
976 if (historyStart == Long.MAX_VALUE) historyStart = now;
977 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700978
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700979 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700980 if (policy != null) {
981 // find the next cycle boundary
982 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700983
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700984 // walk backwards, generating all valid cycle ranges
985 while (cycleEnd > historyStart) {
986 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
987 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
988 + historyStart);
989 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
990 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700991 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700992 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700993
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700994 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700995 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700996 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700997
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700998 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700999 // no policy defined cycles; show entry for each four-week period
1000 long cycleEnd = historyEnd;
1001 while (cycleEnd > historyStart) {
1002 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
1003 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1004 cycleEnd = cycleStart;
1005 }
1006
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001007 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001008 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001009
1010 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001011 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001012 final int position = mCycleAdapter.findNearestPosition(previousItem);
1013 mCycleSpinner.setSelection(position);
1014
1015 // only force-update cycle when changed; skipping preserves any
1016 // user-defined inspection region.
1017 final CycleItem selectedItem = mCycleAdapter.getItem(position);
1018 if (!Objects.equal(selectedItem, previousItem)) {
1019 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1020 } else {
1021 // but still kick off loader for detailed list
1022 updateDetailData();
1023 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001024 } else {
1025 updateDetailData();
1026 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001027 }
1028
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001029 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001030 @Override
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001031 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
1032 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001033
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001034 final boolean dataEnabled = isChecked;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001035 final String currentTab = mCurrentTab;
1036 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001037 if (dataEnabled) {
1038 setMobileDataEnabled(true);
1039 } else {
1040 // disabling data; show confirmation dialog which eventually
1041 // calls setMobileDataEnabled() once user confirms.
1042 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1043 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001044 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001045
Jeff Sharkey28130d92011-09-02 16:10:24 -07001046 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001047 }
1048 };
1049
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001050 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001051 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001052 public void onClick(View v) {
1053 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001054 if (disableAtLimit) {
1055 // enabling limit; show confirmation dialog which eventually
1056 // calls setPolicyLimitBytes() once user confirms.
1057 ConfirmLimitFragment.show(DataUsageSummary.this);
1058 } else {
1059 setPolicyLimitBytes(LIMIT_DISABLED);
1060 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001061 }
1062 };
1063
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001064 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001065 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001066 public void onClick(View v) {
1067 final boolean restrictBackground = !mAppRestrict.isChecked();
1068
1069 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001070 // enabling restriction; show confirmation dialog which
1071 // eventually calls setRestrictBackground() once user
1072 // confirms.
1073 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001074 } else {
1075 setAppRestrictBackground(false);
1076 }
1077 }
1078 };
1079
1080 private OnClickListener mAppSettingsListener = new OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001081 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001082 public void onClick(View v) {
Jeff Sharkeyd92e0412012-04-24 11:35:43 -07001083 if (!isAdded()) return;
1084
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001085 // TODO: target torwards entire UID instead of just first package
1086 startActivity(mAppSettingsIntent);
1087 }
1088 };
1089
Jeff Sharkey8a503642011-06-10 13:31:21 -07001090 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001091 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001092 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001093 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001094 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001095
1096 // TODO: sigh, remove this hack once we understand 6450986
1097 if (mUidDetailProvider == null || app == null) return;
1098
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001099 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001100 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001101 }
1102 };
1103
1104 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001105 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001106 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1107 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1108 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001109 // show cycle editor; will eventually call setPolicyCycleDay()
1110 // when user finishes editing.
1111 CycleEditorFragment.show(DataUsageSummary.this);
1112
1113 // reset spinner to something other than "change cycle..."
1114 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001115
1116 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001117 if (LOGD) {
1118 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1119 + cycle.end + "]");
1120 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001121
1122 // update chart to show selected cycle, and update detail data
1123 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001124 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001125
1126 updateDetailData();
1127 }
1128 }
1129
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001130 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001131 public void onNothingSelected(AdapterView<?> parent) {
1132 // ignored
1133 }
1134 };
1135
1136 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001137 * Update details based on {@link #mChart} inspection range depending on
1138 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1139 * of applications data usage, and when {@link #isAppDetailMode()} update
1140 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001141 */
1142 private void updateDetailData() {
1143 if (LOGD) Log.d(TAG, "updateDetailData()");
1144
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001145 final long start = mChart.getInspectStart();
1146 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001147 final long now = System.currentTimeMillis();
1148
1149 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001150
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001151 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001152 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001153 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001154 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001155 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001156 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001157 final long foregroundBytes = entry.rxBytes + entry.txBytes;
1158
1159 mAppPieChart.setOriginAngle(175);
1160
1161 mAppPieChart.removeAllSlices();
1162 mAppPieChart.addSlice(foregroundBytes, Color.parseColor("#d88d3a"));
1163 mAppPieChart.addSlice(defaultBytes, Color.parseColor("#666666"));
1164
1165 mAppPieChart.generatePath();
1166
1167 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1168 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1169
1170 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001171 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001172
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001173 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001174
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001175 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001176 if (mChartData != null) {
1177 entry = mChartData.network.getValues(start, end, now, null);
1178 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001179
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001180 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001181 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001182 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001183 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001184
1185 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1186 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001187 final String rangePhrase = formatDateRange(context, start, end);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001188
Jeff Sharkeye557c332012-04-13 16:04:07 -07001189 final int summaryRes;
1190 if (TAB_MOBILE.equals(mCurrentTab) || TAB_3G.equals(mCurrentApp)
1191 || TAB_4G.equals(mCurrentApp)) {
1192 summaryRes = R.string.data_usage_total_during_range_mobile;
1193 } else {
1194 summaryRes = R.string.data_usage_total_during_range;
1195 }
1196
1197 mUsageSummary.setText(getString(summaryRes, totalPhrase, rangePhrase));
Jeff Sharkey92811822012-05-04 11:47:29 -07001198
1199 // initial layout is finished above, ensure we have transitions
1200 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001201 }
1202
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001203 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1204 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001205 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001206 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001207 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001208 }
1209
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001210 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001211 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1212 mChartData = data;
1213 mChart.bindNetworkStats(mChartData.network);
1214 mChart.bindDetailNetworkStats(mChartData.detail);
1215
1216 // calcuate policy cycles based on available data
1217 updatePolicy(true);
1218 updateAppDetail();
1219
1220 // force scroll to top of body when showing detail
1221 if (mChartData.detail != null) {
1222 mListView.smoothScrollToPosition(0);
1223 }
1224 }
1225
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001226 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001227 public void onLoaderReset(Loader<ChartData> loader) {
1228 mChartData = null;
1229 mChart.bindNetworkStats(null);
1230 mChart.bindDetailNetworkStats(null);
1231 }
1232 };
1233
1234 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001235 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001236 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001237 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001238 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001239 }
1240
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001241 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001242 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001243 final int[] restrictedUids = mPolicyManager.getUidsWithPolicy(
Jeff Sharkeye557c332012-04-13 16:04:07 -07001244 POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001245 mAdapter.bindStats(data, restrictedUids);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001246 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001247 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001248
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001249 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001250 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001251 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001252 updateEmptyVisible();
1253 }
1254
1255 private void updateEmptyVisible() {
1256 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1257 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001258 }
1259 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001260
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001261 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001262 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001263 final Context context = getActivity();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001264 if (hasReadyMobileRadio(context)) {
1265 final TelephonyManager tele = TelephonyManager.from(context);
1266 return mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001267 } else {
1268 return false;
1269 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001270 }
1271
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001272 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001273 private void setMobilePolicySplit(boolean split) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001274 final Context context = getActivity();
1275 if (hasReadyMobileRadio(context)) {
1276 final TelephonyManager tele = TelephonyManager.from(context);
1277 mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split);
1278 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001279 }
1280
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001281 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001282 final TelephonyManager tele = TelephonyManager.from(context);
1283 final String actualSubscriberId = tele.getSubscriberId();
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -08001284 return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001285 }
1286
Jeff Sharkey8a503642011-06-10 13:31:21 -07001287 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001288 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001289 public void onInspectRangeChanged() {
1290 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
1291 updateDetailData();
1292 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001293
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001294 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001295 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001296 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001297 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001298
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001299 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001300 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001301 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001302 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001303
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001304 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001305 public void requestWarningEdit() {
1306 WarningEditorFragment.show(DataUsageSummary.this);
1307 }
1308
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001309 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001310 public void requestLimitEdit() {
1311 LimitEditorFragment.show(DataUsageSummary.this);
1312 }
1313 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001314
1315 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001316 * List item that reflects a specific data usage cycle.
1317 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001318 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001319 public CharSequence label;
1320 public long start;
1321 public long end;
1322
Jeff Sharkey8a503642011-06-10 13:31:21 -07001323 CycleItem(CharSequence label) {
1324 this.label = label;
1325 }
1326
1327 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001328 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001329 this.start = start;
1330 this.end = end;
1331 }
1332
Jeff Sharkey8a503642011-06-10 13:31:21 -07001333 @Override
1334 public String toString() {
1335 return label.toString();
1336 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001337
1338 @Override
1339 public boolean equals(Object o) {
1340 if (o instanceof CycleItem) {
1341 final CycleItem another = (CycleItem) o;
1342 return start == another.start && end == another.end;
1343 }
1344 return false;
1345 }
1346
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001347 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001348 public int compareTo(CycleItem another) {
1349 return Long.compare(start, another.start);
1350 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001351 }
1352
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001353 private static final StringBuilder sBuilder = new StringBuilder(50);
1354 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1355 sBuilder, Locale.getDefault());
1356
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001357 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001358 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001359
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001360 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001361 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001362 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1363 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001364 }
1365 }
1366
Jeff Sharkey8a503642011-06-10 13:31:21 -07001367 /**
1368 * Special-case data usage cycle that triggers dialog to change
1369 * {@link NetworkPolicy#cycleDay}.
1370 */
1371 public static class CycleChangeItem extends CycleItem {
1372 public CycleChangeItem(Context context) {
1373 super(context.getString(R.string.data_usage_change_cycle));
1374 }
1375 }
1376
1377 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001378 private boolean mChangePossible = false;
1379 private boolean mChangeVisible = false;
1380
1381 private final CycleChangeItem mChangeItem;
1382
Jeff Sharkey8a503642011-06-10 13:31:21 -07001383 public CycleAdapter(Context context) {
1384 super(context, android.R.layout.simple_spinner_item);
1385 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001386 mChangeItem = new CycleChangeItem(context);
1387 }
1388
1389 public void setChangePossible(boolean possible) {
1390 mChangePossible = possible;
1391 updateChange();
1392 }
1393
1394 public void setChangeVisible(boolean visible) {
1395 mChangeVisible = visible;
1396 updateChange();
1397 }
1398
1399 private void updateChange() {
1400 remove(mChangeItem);
1401 if (mChangePossible && mChangeVisible) {
1402 add(mChangeItem);
1403 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001404 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001405
1406 /**
1407 * Find position of {@link CycleItem} in this adapter which is nearest
1408 * the given {@link CycleItem}.
1409 */
1410 public int findNearestPosition(CycleItem target) {
1411 if (target != null) {
1412 final int count = getCount();
1413 for (int i = count - 1; i >= 0; i--) {
1414 final CycleItem item = getItem(i);
1415 if (item instanceof CycleChangeItem) {
1416 continue;
1417 } else if (item.compareTo(target) >= 0) {
1418 return i;
1419 }
1420 }
1421 }
1422 return 0;
1423 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001424 }
1425
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001426 public static class AppItem implements Comparable<AppItem>, Parcelable {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001427 public final int key;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001428 public boolean restricted;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001429 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001430 public long total;
1431
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001432 public AppItem(int key) {
1433 this.key = key;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001434 }
1435
1436 public AppItem(Parcel parcel) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001437 key = parcel.readInt();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001438 uids = parcel.readSparseBooleanArray();
1439 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001440 }
1441
1442 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001443 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001444 }
1445
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001446 @Override
1447 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001448 dest.writeInt(key);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001449 dest.writeSparseBooleanArray(uids);
1450 dest.writeLong(total);
1451 }
1452
1453 @Override
1454 public int describeContents() {
1455 return 0;
1456 }
1457
1458 @Override
1459 public int compareTo(AppItem another) {
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001460 return Long.compare(another.total, total);
1461 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001462
1463 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1464 @Override
1465 public AppItem createFromParcel(Parcel in) {
1466 return new AppItem(in);
1467 }
1468
1469 @Override
1470 public AppItem[] newArray(int size) {
1471 return new AppItem[size];
1472 }
1473 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001474 }
1475
Jeff Sharkey8a503642011-06-10 13:31:21 -07001476 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001477 * Adapter of applications, sorted by total usage descending.
1478 */
1479 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001480 private final UidDetailProvider mProvider;
1481 private final int mInsetSide;
1482
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001483 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001484 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001485
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001486 public DataUsageAdapter(UidDetailProvider provider, int insetSide) {
1487 mProvider = checkNotNull(provider);
1488 mInsetSide = insetSide;
1489 }
1490
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001491 /**
1492 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1493 */
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001494 public void bindStats(NetworkStats stats, int[] restrictedUids) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001495 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001496
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001497 final int currentUserId = ActivityManager.getCurrentUser();
1498 final SparseArray<AppItem> knownItems = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001499
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001500 NetworkStats.Entry entry = null;
1501 final int size = stats != null ? stats.size() : 0;
1502 for (int i = 0; i < size; i++) {
1503 entry = stats.getValues(i, entry);
1504
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001505 // Decide how to collapse items together
1506 final int uid = entry.uid;
1507 final int collapseKey;
1508 if (UserHandle.isApp(uid)) {
1509 if (UserHandle.getUserId(uid) == currentUserId) {
1510 collapseKey = uid;
1511 } else {
1512 collapseKey = UidDetailProvider.buildKeyForUser(UserHandle.getUserId(uid));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001513 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001514 } else if (uid == UID_REMOVED || uid == UID_TETHERING) {
1515 collapseKey = uid;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001516 } else {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001517 collapseKey = android.os.Process.SYSTEM_UID;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001518 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001519
1520 AppItem item = knownItems.get(collapseKey);
1521 if (item == null) {
1522 item = new AppItem(collapseKey);
1523 mItems.add(item);
1524 knownItems.put(item.key, item);
1525 }
1526 item.addUid(uid);
1527 item.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001528 }
1529
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001530 for (int uid : restrictedUids) {
1531 // Only splice in restricted state for current user
1532 if (UserHandle.getUserId(uid) != currentUserId) continue;
1533
1534 AppItem item = knownItems.get(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001535 if (item == null) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001536 item = new AppItem(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001537 item.total = -1;
1538 mItems.add(item);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001539 knownItems.put(item.key, item);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001540 }
1541 item.restricted = true;
1542 }
1543
Jeff Sharkey8a503642011-06-10 13:31:21 -07001544 Collections.sort(mItems);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001545 mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001546 notifyDataSetChanged();
1547 }
1548
1549 @Override
1550 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001551 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001552 }
1553
1554 @Override
1555 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001556 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001557 }
1558
1559 @Override
1560 public long getItemId(int position) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001561 return mItems.get(position).key;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001562 }
1563
1564 @Override
1565 public View getView(int position, View convertView, ViewGroup parent) {
1566 if (convertView == null) {
1567 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey5d706792011-09-08 18:57:17 -07001568 R.layout.data_usage_item, parent, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001569
1570 if (mInsetSide > 0) {
1571 convertView.setPadding(mInsetSide, 0, mInsetSide, 0);
1572 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001573 }
1574
1575 final Context context = parent.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001576
Jeff Sharkey28130d92011-09-02 16:10:24 -07001577 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001578 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1579 android.R.id.progress);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001580
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001581 // kick off async load of app details
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001582 final AppItem item = mItems.get(position);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001583 UidDetailTask.bindView(mProvider, item, convertView);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001584
Jeff Sharkeye557c332012-04-13 16:04:07 -07001585 if (item.restricted && item.total <= 0) {
1586 text1.setText(R.string.data_usage_app_restricted);
1587 progress.setVisibility(View.GONE);
1588 } else {
1589 text1.setText(Formatter.formatFileSize(context, item.total));
1590 progress.setVisibility(View.VISIBLE);
1591 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001592
1593 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1594 progress.setProgress(percentTotal);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001595
1596 return convertView;
1597 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001598 }
1599
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001600 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001601 * Empty {@link Fragment} that controls display of UID details in
1602 * {@link DataUsageSummary}.
1603 */
1604 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001605 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001606
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001607 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001608 if (!parent.isAdded()) return;
1609
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001610 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001611 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001612
1613 final AppDetailsFragment fragment = new AppDetailsFragment();
1614 fragment.setArguments(args);
1615 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001616 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1617 ft.add(fragment, TAG_APP_DETAILS);
1618 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001619 ft.setBreadCrumbTitle(label);
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001620 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001621 }
1622
1623 @Override
1624 public void onStart() {
1625 super.onStart();
1626 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001627 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001628 target.updateBody();
1629 }
1630
1631 @Override
1632 public void onStop() {
1633 super.onStop();
1634 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001635 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001636 target.updateBody();
1637 }
1638 }
1639
1640 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001641 * Dialog to request user confirmation before setting
1642 * {@link NetworkPolicy#limitBytes}.
1643 */
1644 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001645 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001646 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001647
1648 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001649 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001650
Jeff Sharkey461842a2011-09-25 18:22:48 -07001651 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001652 final CharSequence message;
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001653 final long minLimitBytes = (long) (
1654 parent.mPolicyEditor.getPolicy(parent.mTemplate).warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001655 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001656
1657 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001658 final String currentTab = parent.mCurrentTab;
1659 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001660 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001661 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001662 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001663 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001664 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001665 } else if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001666 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001667 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001668 } else {
1669 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001670 }
1671
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001672 final Bundle args = new Bundle();
1673 args.putCharSequence(EXTRA_MESSAGE, message);
1674 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1675
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001676 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1677 dialog.setArguments(args);
1678 dialog.setTargetFragment(parent, 0);
1679 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1680 }
1681
1682 @Override
1683 public Dialog onCreateDialog(Bundle savedInstanceState) {
1684 final Context context = getActivity();
1685
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001686 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001687 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1688
1689 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1690 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001691 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001692
1693 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001694 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001695 public void onClick(DialogInterface dialog, int which) {
1696 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1697 if (target != null) {
1698 target.setPolicyLimitBytes(limitBytes);
1699 }
1700 }
1701 });
1702
1703 return builder.create();
1704 }
1705 }
1706
1707 /**
1708 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1709 */
1710 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001711 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001712
1713 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001714 if (!parent.isAdded()) return;
1715
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001716 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001717 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001718
1719 final CycleEditorFragment dialog = new CycleEditorFragment();
1720 dialog.setArguments(args);
1721 dialog.setTargetFragment(parent, 0);
1722 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1723 }
1724
1725 @Override
1726 public Dialog onCreateDialog(Bundle savedInstanceState) {
1727 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001728 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1729 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001730
1731 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1732 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1733
1734 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1735 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1736
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001737 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1738 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001739
1740 cycleDayPicker.setMinValue(1);
1741 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001742 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001743 cycleDayPicker.setWrapSelectorWheel(true);
1744
1745 builder.setTitle(R.string.data_usage_cycle_editor_title);
1746 builder.setView(view);
1747
1748 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1749 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001750 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001751 public void onClick(DialogInterface dialog, int which) {
1752 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001753 final String cycleTimezone = new Time().timezone;
1754 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001755 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001756 }
1757 });
1758
1759 return builder.create();
1760 }
1761 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001762
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001763 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001764 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1765 */
1766 public static class WarningEditorFragment extends DialogFragment {
1767 private static final String EXTRA_TEMPLATE = "template";
1768
1769 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001770 if (!parent.isAdded()) return;
1771
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001772 final Bundle args = new Bundle();
1773 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1774
1775 final WarningEditorFragment dialog = new WarningEditorFragment();
1776 dialog.setArguments(args);
1777 dialog.setTargetFragment(parent, 0);
1778 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1779 }
1780
1781 @Override
1782 public Dialog onCreateDialog(Bundle savedInstanceState) {
1783 final Context context = getActivity();
1784 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1785 final NetworkPolicyEditor editor = target.mPolicyEditor;
1786
1787 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1788 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1789
1790 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1791 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1792
1793 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1794 final long warningBytes = editor.getPolicyWarningBytes(template);
1795 final long limitBytes = editor.getPolicyLimitBytes(template);
1796
1797 bytesPicker.setMinValue(0);
1798 if (limitBytes != LIMIT_DISABLED) {
1799 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1800 } else {
1801 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1802 }
1803 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1804 bytesPicker.setWrapSelectorWheel(false);
1805
1806 builder.setTitle(R.string.data_usage_warning_editor_title);
1807 builder.setView(view);
1808
1809 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1810 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001811 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001812 public void onClick(DialogInterface dialog, int which) {
1813 // clear focus to finish pending text edits
1814 bytesPicker.clearFocus();
1815
1816 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1817 editor.setPolicyWarningBytes(template, bytes);
1818 target.updatePolicy(false);
1819 }
1820 });
1821
1822 return builder.create();
1823 }
1824 }
1825
1826 /**
1827 * Dialog to edit {@link NetworkPolicy#limitBytes}.
1828 */
1829 public static class LimitEditorFragment extends DialogFragment {
1830 private static final String EXTRA_TEMPLATE = "template";
1831
1832 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001833 if (!parent.isAdded()) return;
1834
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001835 final Bundle args = new Bundle();
1836 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1837
1838 final LimitEditorFragment dialog = new LimitEditorFragment();
1839 dialog.setArguments(args);
1840 dialog.setTargetFragment(parent, 0);
1841 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
1842 }
1843
1844 @Override
1845 public Dialog onCreateDialog(Bundle savedInstanceState) {
1846 final Context context = getActivity();
1847 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1848 final NetworkPolicyEditor editor = target.mPolicyEditor;
1849
1850 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1851 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1852
1853 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1854 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1855
1856 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1857 final long warningBytes = editor.getPolicyWarningBytes(template);
1858 final long limitBytes = editor.getPolicyLimitBytes(template);
1859
1860 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09001861 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001862 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
1863 } else {
1864 bytesPicker.setMinValue(0);
1865 }
1866 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
1867 bytesPicker.setWrapSelectorWheel(false);
1868
1869 builder.setTitle(R.string.data_usage_limit_editor_title);
1870 builder.setView(view);
1871
1872 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1873 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001874 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001875 public void onClick(DialogInterface dialog, int which) {
1876 // clear focus to finish pending text edits
1877 bytesPicker.clearFocus();
1878
1879 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1880 editor.setPolicyLimitBytes(template, bytes);
1881 target.updatePolicy(false);
1882 }
1883 });
1884
1885 return builder.create();
1886 }
1887 }
1888 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07001889 * Dialog to request user confirmation before disabling data.
1890 */
1891 public static class ConfirmDataDisableFragment extends DialogFragment {
1892 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001893 if (!parent.isAdded()) return;
1894
Jeff Sharkey28130d92011-09-02 16:10:24 -07001895 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
1896 dialog.setTargetFragment(parent, 0);
1897 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
1898 }
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.setMessage(R.string.data_usage_disable_mobile);
1906
1907 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001908 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07001909 public void onClick(DialogInterface dialog, int which) {
1910 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1911 if (target != null) {
1912 // TODO: extend to modify policy enabled flag.
1913 target.setMobileDataEnabled(false);
1914 }
1915 }
1916 });
1917 builder.setNegativeButton(android.R.string.cancel, null);
1918
1919 return builder.create();
1920 }
1921 }
1922
1923 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001924 * Dialog to request user confirmation before setting
Christopher Tate5a64c732012-09-07 13:35:31 -07001925 * {@link android.provider.Settings.Global#DATA_ROAMING}.
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001926 */
1927 public static class ConfirmDataRoamingFragment extends DialogFragment {
1928 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001929 if (!parent.isAdded()) return;
1930
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001931 final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment();
1932 dialog.setTargetFragment(parent, 0);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001933 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_ROAMING);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001934 }
1935
1936 @Override
1937 public Dialog onCreateDialog(Bundle savedInstanceState) {
1938 final Context context = getActivity();
1939
1940 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1941 builder.setTitle(R.string.roaming_reenable_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07001942 if (Utils.hasMultipleUsers(context)) {
1943 builder.setMessage(R.string.roaming_warning_multiuser);
1944 } else {
1945 builder.setMessage(R.string.roaming_warning);
1946 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001947
1948 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001949 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001950 public void onClick(DialogInterface dialog, int which) {
1951 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1952 if (target != null) {
1953 target.setDataRoaming(true);
1954 }
1955 }
1956 });
1957 builder.setNegativeButton(android.R.string.cancel, null);
1958
1959 return builder.create();
1960 }
1961 }
1962
1963 /**
1964 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001965 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001966 */
1967 public static class ConfirmRestrictFragment extends DialogFragment {
1968 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001969 if (!parent.isAdded()) return;
1970
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001971 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1972 dialog.setTargetFragment(parent, 0);
1973 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1974 }
1975
1976 @Override
1977 public Dialog onCreateDialog(Bundle savedInstanceState) {
1978 final Context context = getActivity();
1979
1980 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001981 builder.setTitle(R.string.data_usage_restrict_background_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07001982 if (Utils.hasMultipleUsers(context)) {
1983 builder.setMessage(R.string.data_usage_restrict_background_multiuser);
1984 } else {
1985 builder.setMessage(R.string.data_usage_restrict_background);
1986 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001987
1988 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001989 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001990 public void onClick(DialogInterface dialog, int which) {
1991 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1992 if (target != null) {
1993 target.setRestrictBackground(true);
1994 }
1995 }
1996 });
1997 builder.setNegativeButton(android.R.string.cancel, null);
1998
1999 return builder.create();
2000 }
2001 }
2002
2003 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002004 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
2005 * change has been denied, usually based on
2006 * {@link DataUsageSummary#hasLimitedNetworks()}.
2007 */
2008 public static class DeniedRestrictFragment extends DialogFragment {
2009 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002010 if (!parent.isAdded()) return;
2011
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002012 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
2013 dialog.setTargetFragment(parent, 0);
2014 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
2015 }
2016
2017 @Override
2018 public Dialog onCreateDialog(Bundle savedInstanceState) {
2019 final Context context = getActivity();
2020
2021 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2022 builder.setTitle(R.string.data_usage_app_restrict_background);
2023 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
2024 builder.setPositiveButton(android.R.string.ok, null);
2025
2026 return builder.create();
2027 }
2028 }
2029
2030 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002031 * Dialog to request user confirmation before setting
2032 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
2033 */
2034 public static class ConfirmAppRestrictFragment extends DialogFragment {
2035 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002036 if (!parent.isAdded()) return;
2037
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002038 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2039 dialog.setTargetFragment(parent, 0);
2040 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2041 }
2042
2043 @Override
2044 public Dialog onCreateDialog(Bundle savedInstanceState) {
2045 final Context context = getActivity();
2046
2047 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002048 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2049 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2050
2051 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002052 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002053 public void onClick(DialogInterface dialog, int which) {
2054 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2055 if (target != null) {
2056 target.setAppRestrictBackground(true);
2057 }
2058 }
2059 });
2060 builder.setNegativeButton(android.R.string.cancel, null);
2061
2062 return builder.create();
2063 }
2064 }
2065
2066 /**
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002067 * Dialog to inform user about changing auto-sync setting
2068 */
2069 public static class ConfirmAutoSyncChangeFragment extends DialogFragment {
Amith Yamasani665235f2012-06-07 19:58:34 -07002070 private static final String SAVE_ENABLING = "enabling";
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002071 private boolean mEnabling;
2072
2073 public static void show(DataUsageSummary parent, boolean enabling) {
2074 if (!parent.isAdded()) return;
2075
2076 final ConfirmAutoSyncChangeFragment dialog = new ConfirmAutoSyncChangeFragment();
2077 dialog.mEnabling = enabling;
2078 dialog.setTargetFragment(parent, 0);
2079 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_AUTO_SYNC_CHANGE);
2080 }
2081
2082 @Override
2083 public Dialog onCreateDialog(Bundle savedInstanceState) {
2084 final Context context = getActivity();
Amith Yamasani665235f2012-06-07 19:58:34 -07002085 if (savedInstanceState != null) {
2086 mEnabling = savedInstanceState.getBoolean(SAVE_ENABLING);
2087 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002088
2089 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2090 if (!mEnabling) {
2091 builder.setTitle(R.string.data_usage_auto_sync_off_dialog_title);
2092 builder.setMessage(R.string.data_usage_auto_sync_off_dialog);
2093 } else {
2094 builder.setTitle(R.string.data_usage_auto_sync_on_dialog_title);
2095 builder.setMessage(R.string.data_usage_auto_sync_on_dialog);
2096 }
2097
2098 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
2099 @Override
2100 public void onClick(DialogInterface dialog, int which) {
2101 ContentResolver.setMasterSyncAutomatically(mEnabling);
2102 }
2103 });
2104 builder.setNegativeButton(android.R.string.cancel, null);
2105
2106 return builder.create();
2107 }
Amith Yamasani665235f2012-06-07 19:58:34 -07002108
2109 @Override
2110 public void onSaveInstanceState(Bundle outState) {
2111 super.onSaveInstanceState(outState);
2112 outState.putBoolean(SAVE_ENABLING, mEnabling);
2113 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002114 }
2115
2116 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002117 * Compute default tab that should be selected, based on
2118 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2119 */
2120 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002121 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
2122 if (template == null) return null;
2123
2124 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002125 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002126 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002127 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002128 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002129 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002130 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002131 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002132 return TAB_WIFI;
2133 default:
2134 return null;
2135 }
2136 }
2137
2138 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002139 * Background task that loads {@link UidDetail}, binding to
2140 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002141 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002142 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2143 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002144 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002145 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002146
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002147 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002148 mProvider = checkNotNull(provider);
2149 mItem = checkNotNull(item);
2150 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002151 }
2152
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002153 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002154 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002155 final UidDetailTask existing = (UidDetailTask) target.getTag();
2156 if (existing != null) {
2157 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002158 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002159
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002160 final UidDetail cachedDetail = provider.getUidDetail(item.key, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002161 if (cachedDetail != null) {
2162 bindView(cachedDetail, target);
2163 } else {
2164 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2165 AsyncTask.THREAD_POOL_EXECUTOR));
2166 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002167 }
2168
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002169 private static void bindView(UidDetail detail, View target) {
2170 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2171 final TextView title = (TextView) target.findViewById(android.R.id.title);
2172
2173 if (detail != null) {
2174 icon.setImageDrawable(detail.icon);
2175 title.setText(detail.label);
2176 } else {
2177 icon.setImageDrawable(null);
2178 title.setText(null);
2179 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002180 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002181
2182 @Override
2183 protected void onPreExecute() {
2184 bindView(null, mTarget);
2185 }
2186
2187 @Override
2188 protected UidDetail doInBackground(Void... params) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002189 return mProvider.getUidDetail(mItem.key, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002190 }
2191
2192 @Override
2193 protected void onPostExecute(UidDetail result) {
2194 bindView(result, mTarget);
2195 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002196 }
2197
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002198 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002199 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002200 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002201 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002202 if (TEST_RADIOS) {
2203 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2204 }
2205
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002206 final ConnectivityManager conn = ConnectivityManager.from(context);
2207 final TelephonyManager tele = TelephonyManager.from(context);
2208
2209 // require both supported network and ready SIM
2210 return conn.isNetworkSupported(TYPE_MOBILE) && tele.getSimState() == SIM_STATE_READY;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002211 }
2212
2213 /**
2214 * Test if device has a mobile 4G data radio.
2215 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002216 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002217 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2218 return false;
2219 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002220 if (TEST_RADIOS) {
2221 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2222 }
2223
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002224 final ConnectivityManager conn = ConnectivityManager.from(context);
2225 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002226
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002227 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Wink Saville55434042012-06-14 12:33:43 -07002228 final boolean hasLte = (tele.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE)
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002229 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002230 return hasWimax || hasLte;
2231 }
2232
2233 /**
2234 * Test if device has a Wi-Fi data radio.
2235 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002236 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002237 if (TEST_RADIOS) {
2238 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2239 }
2240
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002241 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002242 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002243 }
2244
2245 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002246 * Test if device has an ethernet network connection.
2247 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002248 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002249 if (TEST_RADIOS) {
2250 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2251 }
2252
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002253 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002254 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2255
2256 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002257 if (mStatsSession != null) {
2258 try {
2259 ethernetBytes = mStatsSession.getSummaryForNetwork(
2260 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2261 .getTotalBytes();
2262 } catch (RemoteException e) {
2263 throw new RuntimeException(e);
2264 }
2265 } else {
2266 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002267 }
2268
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002269 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002270 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002271 }
2272
2273 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002274 * Inflate a {@link Preference} style layout, adding the given {@link View}
2275 * widget into {@link android.R.id#widget_frame}.
2276 */
2277 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2278 final View view = inflater.inflate(R.layout.preference, root, false);
2279 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2280 android.R.id.widget_frame);
2281 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2282 return view;
2283 }
2284
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002285 private static View inflateAppTitle(
2286 LayoutInflater inflater, ViewGroup root, CharSequence label) {
2287 final TextView view = (TextView) inflater.inflate(
2288 R.layout.data_usage_app_title, root, false);
2289 view.setText(label);
2290 return view;
2291 }
2292
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002293 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002294 * Test if any networks are currently limited.
2295 */
2296 private boolean hasLimitedNetworks() {
2297 return !buildLimitedNetworksList().isEmpty();
2298 }
2299
2300 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002301 * Build string describing currently limited networks, which defines when
2302 * background data is restricted.
2303 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002304 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002305 private CharSequence buildLimitedNetworksString() {
2306 final List<CharSequence> limited = buildLimitedNetworksList();
2307
2308 // handle case where no networks limited
2309 if (limited.isEmpty()) {
2310 limited.add(getText(R.string.data_usage_list_none));
2311 }
2312
2313 return TextUtils.join(limited);
2314 }
2315
2316 /**
2317 * Build list of currently limited networks, which defines when background
2318 * data is restricted.
2319 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002320 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002321 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002322 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002323
2324 // build combined list of all limited networks
2325 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002326
2327 final TelephonyManager tele = TelephonyManager.from(context);
2328 if (tele.getSimState() == SIM_STATE_READY) {
2329 final String subscriberId = getActiveSubscriberId(context);
2330 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2331 limited.add(getText(R.string.data_usage_list_mobile));
2332 }
2333 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2334 limited.add(getText(R.string.data_usage_tab_3g));
2335 }
2336 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2337 limited.add(getText(R.string.data_usage_tab_4g));
2338 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002339 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002340
2341 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002342 limited.add(getText(R.string.data_usage_tab_wifi));
2343 }
2344 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2345 limited.add(getText(R.string.data_usage_tab_ethernet));
2346 }
2347
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002348 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002349 }
2350
2351 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002352 * Inset both selector and divider {@link Drawable} on the given
2353 * {@link ListView} by the requested dimensions.
2354 */
2355 private static void insetListViewDrawables(ListView view, int insetSide) {
2356 final Drawable selector = view.getSelector();
2357 final Drawable divider = view.getDivider();
2358
2359 // fully unregister these drawables so callbacks can be maintained after
2360 // wrapping below.
2361 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2362 view.setSelector(stub);
2363 view.setDivider(stub);
2364
2365 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2366 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2367 }
2368
2369 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002370 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002371 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002372 */
2373 private static void setPreferenceTitle(View parent, int resId) {
2374 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2375 title.setText(resId);
2376 }
2377
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002378 /**
2379 * Set {@link android.R.id#summary} for a preference view inflated with
2380 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2381 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002382 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002383 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2384 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002385 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002386 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002387}