blob: 7c1832f692337f70c7cbaf57901907bc1f53510f [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());
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700455 mMenuDataRoaming.setVisible(isOwner);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700456
457 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700458 mMenuRestrictBackground.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
459 mMenuRestrictBackground.setChecked(mPolicyManager.getRestrictBackground());
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700460 mMenuRestrictBackground.setVisible(isOwner);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700461
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700462 mMenuAutoSync = menu.findItem(R.id.data_usage_menu_auto_sync);
463 mMenuAutoSync.setChecked(ContentResolver.getMasterSyncAutomatically());
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700464 mMenuAutoSync.setVisible(isOwner && !appDetailMode);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700465
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700466 final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g);
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700467 split4g.setVisible(hasReadyMobile4gRadio(context) && isOwner && !appDetailMode);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700468 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700469
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700470 final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700471 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700472 showWifi.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700473 showWifi.setChecked(mShowWifi);
474 } else {
475 showWifi.setVisible(false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700476 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700477
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700478 final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700479 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700480 showEthernet.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700481 showEthernet.setChecked(mShowEthernet);
482 } else {
483 showEthernet.setVisible(false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700484 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700485
486 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
487 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700488 metered.setVisible(isOwner && !appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700489 } else {
490 metered.setVisible(false);
491 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700492
493 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
494 String helpUrl;
495 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
496 Intent helpIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
497 helpIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
498 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
499 help.setIntent(helpIntent);
500 help.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
501 } else {
502 help.setVisible(false);
503 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700504 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700505
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700506 @Override
507 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700508 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700509 case R.id.data_usage_menu_roaming: {
510 final boolean dataRoaming = !item.isChecked();
511 if (dataRoaming) {
512 ConfirmDataRoamingFragment.show(this);
513 } else {
514 // no confirmation to disable roaming
515 setDataRoaming(false);
516 }
517 return true;
518 }
519 case R.id.data_usage_menu_restrict_background: {
520 final boolean restrictBackground = !item.isChecked();
521 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800522 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700523 } else {
524 // no confirmation to drop restriction
525 setRestrictBackground(false);
526 }
527 return true;
528 }
529 case R.id.data_usage_menu_split_4g: {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700530 final boolean mobileSplit = !item.isChecked();
Jeff Sharkeya662e492011-06-18 21:57:06 -0700531 setMobilePolicySplit(mobileSplit);
532 item.setChecked(isMobilePolicySplit());
Jeff Sharkey8a503642011-06-10 13:31:21 -0700533 updateTabs();
534 return true;
535 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700536 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700537 mShowWifi = !item.isChecked();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700538 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700539 item.setChecked(mShowWifi);
540 updateTabs();
541 return true;
542 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700543 case R.id.data_usage_menu_show_ethernet: {
544 mShowEthernet = !item.isChecked();
545 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
546 item.setChecked(mShowEthernet);
547 updateTabs();
548 return true;
549 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700550 case R.id.data_usage_menu_metered: {
551 final PreferenceActivity activity = (PreferenceActivity) getActivity();
552 activity.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
553 R.string.data_usage_metered_title, null, this, 0);
554 return true;
555 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700556 case R.id.data_usage_menu_auto_sync: {
557 ConfirmAutoSyncChangeFragment.show(this, !item.isChecked());
558 return true;
559 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700560 }
561 return false;
562 }
563
Jeff Sharkey94a90952011-06-13 22:31:09 -0700564 @Override
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700565 public void onDestroy() {
Jeff Sharkey94a90952011-06-13 22:31:09 -0700566 mDataEnabledView = null;
567 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700568
569 mUidDetailProvider.clearCache();
570 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700571
572 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700573
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800574 if (this.isRemoving()) {
575 getFragmentManager()
576 .popBackStack(TAG_APP_DETAILS, FragmentManager.POP_BACK_STACK_INCLUSIVE);
577 }
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700578
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800579 super.onDestroy();
580 }
581
Jeff Sharkey8a503642011-06-10 13:31:21 -0700582 /**
Jeff Sharkey92811822012-05-04 11:47:29 -0700583 * Build and assign {@link LayoutTransition} to various containers. Should
584 * only be assigned after initial layout is complete.
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700585 */
Jeff Sharkey92811822012-05-04 11:47:29 -0700586 private void ensureLayoutTransitions() {
587 // skip when already setup
588 if (mChart.getLayoutTransition() != null) return;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700589
Jeff Sharkey92811822012-05-04 11:47:29 -0700590 mTabsContainer.setLayoutTransition(buildLayoutTransition());
591 mHeader.setLayoutTransition(buildLayoutTransition());
592 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700593
Jeff Sharkey92811822012-05-04 11:47:29 -0700594 final LayoutTransition chartTransition = buildLayoutTransition();
595 chartTransition.disableTransitionType(LayoutTransition.APPEARING);
596 chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
597 mChart.setLayoutTransition(chartTransition);
598 }
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700599
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700600 private static LayoutTransition buildLayoutTransition() {
601 final LayoutTransition transition = new LayoutTransition();
602 if (TEST_ANIM) {
603 transition.setDuration(1500);
604 }
605 transition.setAnimateParentHierarchy(false);
606 return transition;
607 }
608
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700609 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700610 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700611 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
612 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700613 */
614 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700615 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700616 mTabHost.clearAllTabs();
617
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700618 final boolean mobileSplit = isMobilePolicySplit();
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700619 if (mobileSplit && hasReadyMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700620 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
621 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700622 } else if (hasReadyMobileRadio(context)) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700623 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700624 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700625 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700626 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
627 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700628 if (mShowEthernet && hasEthernet(context)) {
629 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
630 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700631
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700632 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700633 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
634 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
635 if (mIntentTab != null) {
636 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700637 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700638 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700639 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700640 mTabHost.setCurrentTabByTag(mIntentTab);
641 }
642 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700643 } else if (noTabs) {
644 // no usable tabs, so hide body
645 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700646 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700647 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700648 }
649 }
650
Jeff Sharkey8a503642011-06-10 13:31:21 -0700651 /**
652 * Factory that provide empty {@link View} to make {@link TabHost} happy.
653 */
654 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700655 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700656 public View createTabContent(String tag) {
657 return new View(mTabHost.getContext());
658 }
659 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700660
Jeff Sharkey8a503642011-06-10 13:31:21 -0700661 /**
662 * Build {@link TabSpec} with thin indicator, and empty content.
663 */
664 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700665 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
666 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700667 }
668
669 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700670 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700671 public void onTabChanged(String tabId) {
672 // user changed tab; update body
673 updateBody();
674 }
675 };
676
677 /**
678 * Update body content based on current tab. Loads
679 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
680 * binds them to visible controls.
681 */
682 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700683 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700684 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700685
Jeff Sharkeya662e492011-06-18 21:57:06 -0700686 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700687 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700688 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700689
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700690 if (currentTab == null) {
691 Log.w(TAG, "no tab selected; hiding body");
692 mListView.setVisibility(View.GONE);
693 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700694 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700695 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700696 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700697
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700698 final boolean tabChanged = !currentTab.equals(mCurrentTab);
699 mCurrentTab = currentTab;
700
Jeff Sharkey8a503642011-06-10 13:31:21 -0700701 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
702
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700703 mDataEnabledView.setVisibility(isOwner ? View.VISIBLE : View.GONE);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700704
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700705 // TODO: remove mobile tabs when SIM isn't ready
706 final TelephonyManager tele = TelephonyManager.from(context);
707
Jeff Sharkey8a503642011-06-10 13:31:21 -0700708 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700709 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
710 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700711 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700712
713 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700714 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
715 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
716 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700717 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700718
719 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700720 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
721 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
722 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700723 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700724
725 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700726 // wifi doesn't have any controls
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700727 mDataEnabledView.setVisibility(View.GONE);
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700728 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700729 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700730
731 } else if (TAB_ETHERNET.equals(currentTab)) {
732 // ethernet doesn't have any controls
733 mDataEnabledView.setVisibility(View.GONE);
734 mDisableAtLimitView.setVisibility(View.GONE);
735 mTemplate = buildTemplateEthernet();
736
737 } else {
738 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700739 }
740
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700741 // kick off loader for network history
742 // TODO: consider chaining two loaders together instead of reloading
743 // network history when showing app detail.
744 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700745 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700746
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700747 // detail mode can change visible menus, invalidate
748 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700749
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700750 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700751 }
752
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700753 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700754 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700755 }
756
757 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700758 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700759 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700760 */
761 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700762 final Context context = getActivity();
763 final PackageManager pm = context.getPackageManager();
764 final LayoutInflater inflater = getActivity().getLayoutInflater();
765
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700766 if (isAppDetailMode()) {
767 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700768 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700769 } else {
770 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700771 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700772
773 // hide detail stats when not in detail mode
774 mChart.bindDetailNetworkStats(null);
775 return;
776 }
777
778 // remove warning/limit sweeps while in detail mode
779 mChart.bindNetworkPolicy(null);
780
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700781 // show icon and all labels appearing under this app
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700782 final int uid = mCurrentApp.key;
783 final UidDetail detail = mUidDetailProvider.getUidDetail(uid, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700784 mAppIcon.setImageDrawable(detail.icon);
785
786 mAppTitles.removeAllViews();
787 if (detail.detailLabels != null) {
788 for (CharSequence label : detail.detailLabels) {
789 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label));
790 }
791 } else {
792 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label));
793 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700794
795 // enable settings button when package provides it
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700796 final String[] packageNames = pm.getPackagesForUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700797 if (packageNames != null && packageNames.length > 0) {
798 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700799 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
800
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700801 // Search for match across all packages
802 boolean matchFound = false;
803 for (String packageName : packageNames) {
804 mAppSettingsIntent.setPackage(packageName);
805 if (pm.resolveActivity(mAppSettingsIntent, 0) != null) {
806 matchFound = true;
807 break;
808 }
809 }
810
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700811 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -0700812 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700813
814 } else {
815 mAppSettingsIntent = null;
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700816 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700817 }
818
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700819 updateDetailData();
820
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700821 if (UserHandle.isApp(uid) && !mPolicyManager.getRestrictBackground()
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700822 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700823 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800824 setPreferenceSummary(mAppRestrictView,
825 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700826
827 mAppRestrictView.setVisibility(View.VISIBLE);
828 mAppRestrict.setChecked(getAppRestrictBackground());
829
830 } else {
831 mAppRestrictView.setVisibility(View.GONE);
832 }
833 }
834
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700835 private void setPolicyWarningBytes(long warningBytes) {
836 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700837 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700838 updatePolicy(false);
839 }
840
841 private void setPolicyLimitBytes(long limitBytes) {
842 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700843 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700844 updatePolicy(false);
845 }
846
Jeff Sharkey28130d92011-09-02 16:10:24 -0700847 /**
848 * Local cache of value, used to work around delay when
849 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
850 */
851 private Boolean mMobileDataEnabled;
852
853 private boolean isMobileDataEnabled() {
854 if (mMobileDataEnabled != null) {
855 // TODO: deprecate and remove this once enabled flag is on policy
856 return mMobileDataEnabled;
857 } else {
858 return mConnService.getMobileDataEnabled();
859 }
860 }
861
862 private void setMobileDataEnabled(boolean enabled) {
863 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
864 mConnService.setMobileDataEnabled(enabled);
865 mMobileDataEnabled = enabled;
866 updatePolicy(false);
867 }
868
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700869 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700870 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked()
871 && ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700872 }
873
874 private boolean isBandwidthControlEnabled() {
875 try {
876 return mNetworkService.isBandwidthControlEnabled();
877 } catch (RemoteException e) {
878 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
879 return false;
880 }
881 }
882
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700883 private boolean getDataRoaming() {
884 final ContentResolver resolver = getActivity().getContentResolver();
Christopher Tate5a64c732012-09-07 13:35:31 -0700885 return Settings.Global.getInt(resolver, Settings.Global.DATA_ROAMING, 0) != 0;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700886 }
887
888 private void setDataRoaming(boolean enabled) {
889 // TODO: teach telephony DataConnectionTracker to watch and apply
890 // updates when changed.
891 final ContentResolver resolver = getActivity().getContentResolver();
Christopher Tate5a64c732012-09-07 13:35:31 -0700892 Settings.Global.putInt(resolver, Settings.Global.DATA_ROAMING, enabled ? 1 : 0);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700893 mMenuDataRoaming.setChecked(enabled);
894 }
895
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700896 public void setRestrictBackground(boolean restrictBackground) {
897 mPolicyManager.setRestrictBackground(restrictBackground);
898 mMenuRestrictBackground.setChecked(restrictBackground);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700899 }
900
901 private boolean getAppRestrictBackground() {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700902 final int uid = mCurrentApp.key;
903 final int uidPolicy = mPolicyManager.getUidPolicy(uid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700904 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
905 }
906
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700907 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700908 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700909 final int uid = mCurrentApp.key;
910 mPolicyManager.setUidPolicy(
911 uid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700912 mAppRestrict.setChecked(restrictBackground);
913 }
914
Jeff Sharkey8a503642011-06-10 13:31:21 -0700915 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700916 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
917 * current {@link #mTemplate}.
918 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700919 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700920 if (isAppDetailMode()) {
921 mNetworkSwitches.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700922 } else {
923 mNetworkSwitches.setVisibility(View.VISIBLE);
924 }
925
Jeff Sharkey28130d92011-09-02 16:10:24 -0700926 // TODO: move enabled state directly into policy
927 if (TAB_MOBILE.equals(mCurrentTab)) {
928 mBinding = true;
929 mDataEnabled.setChecked(isMobileDataEnabled());
930 mBinding = false;
931 }
932
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700933 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
934 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700935 mDisableAtLimitView.setVisibility(View.VISIBLE);
936 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700937 if (!isAppDetailMode()) {
938 mChart.bindNetworkPolicy(policy);
939 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700940
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700941 } else {
942 // controls are disabled; don't bind warning/limit sweeps
943 mDisableAtLimitView.setVisibility(View.GONE);
944 mChart.bindNetworkPolicy(null);
945 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700946
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700947 if (refreshCycle) {
948 // generate cycle list based on policy and available history
949 updateCycleList(policy);
950 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700951 }
952
953 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700954 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
955 * and available {@link NetworkStatsHistory} data. Always selects the newest
956 * item, updating the inspection range on {@link #mChart}.
957 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700958 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700959 // stash away currently selected cycle to try restoring below
960 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700961 mCycleAdapter.clear();
962
963 final Context context = mCycleSpinner.getContext();
964
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700965 long historyStart = Long.MAX_VALUE;
966 long historyEnd = Long.MIN_VALUE;
967 if (mChartData != null) {
968 historyStart = mChartData.network.getStart();
969 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700970 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700971
Jeff Sharkey461842a2011-09-25 18:22:48 -0700972 final long now = System.currentTimeMillis();
973 if (historyStart == Long.MAX_VALUE) historyStart = now;
974 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700975
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700976 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700977 if (policy != null) {
978 // find the next cycle boundary
979 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700980
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700981 // walk backwards, generating all valid cycle ranges
982 while (cycleEnd > historyStart) {
983 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
984 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
985 + historyStart);
986 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
987 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700988 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700989 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700990
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700991 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700992 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700993 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700994
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700995 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700996 // no policy defined cycles; show entry for each four-week period
997 long cycleEnd = historyEnd;
998 while (cycleEnd > historyStart) {
999 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
1000 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1001 cycleEnd = cycleStart;
1002 }
1003
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001004 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001005 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001006
1007 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001008 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001009 final int position = mCycleAdapter.findNearestPosition(previousItem);
1010 mCycleSpinner.setSelection(position);
1011
1012 // only force-update cycle when changed; skipping preserves any
1013 // user-defined inspection region.
1014 final CycleItem selectedItem = mCycleAdapter.getItem(position);
1015 if (!Objects.equal(selectedItem, previousItem)) {
1016 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1017 } else {
1018 // but still kick off loader for detailed list
1019 updateDetailData();
1020 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001021 } else {
1022 updateDetailData();
1023 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001024 }
1025
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001026 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001027 @Override
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001028 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
1029 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001030
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001031 final boolean dataEnabled = isChecked;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001032 final String currentTab = mCurrentTab;
1033 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001034 if (dataEnabled) {
1035 setMobileDataEnabled(true);
1036 } else {
1037 // disabling data; show confirmation dialog which eventually
1038 // calls setMobileDataEnabled() once user confirms.
1039 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1040 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001041 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001042
Jeff Sharkey28130d92011-09-02 16:10:24 -07001043 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001044 }
1045 };
1046
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001047 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001048 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001049 public void onClick(View v) {
1050 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001051 if (disableAtLimit) {
1052 // enabling limit; show confirmation dialog which eventually
1053 // calls setPolicyLimitBytes() once user confirms.
1054 ConfirmLimitFragment.show(DataUsageSummary.this);
1055 } else {
1056 setPolicyLimitBytes(LIMIT_DISABLED);
1057 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001058 }
1059 };
1060
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001061 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001062 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001063 public void onClick(View v) {
1064 final boolean restrictBackground = !mAppRestrict.isChecked();
1065
1066 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001067 // enabling restriction; show confirmation dialog which
1068 // eventually calls setRestrictBackground() once user
1069 // confirms.
1070 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001071 } else {
1072 setAppRestrictBackground(false);
1073 }
1074 }
1075 };
1076
1077 private OnClickListener mAppSettingsListener = new OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001078 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001079 public void onClick(View v) {
Jeff Sharkeyd92e0412012-04-24 11:35:43 -07001080 if (!isAdded()) return;
1081
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001082 // TODO: target torwards entire UID instead of just first package
1083 startActivity(mAppSettingsIntent);
1084 }
1085 };
1086
Jeff Sharkey8a503642011-06-10 13:31:21 -07001087 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001088 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001089 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001090 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001091 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001092
1093 // TODO: sigh, remove this hack once we understand 6450986
1094 if (mUidDetailProvider == null || app == null) return;
1095
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001096 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001097 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001098 }
1099 };
1100
1101 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001102 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001103 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1104 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1105 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001106 // show cycle editor; will eventually call setPolicyCycleDay()
1107 // when user finishes editing.
1108 CycleEditorFragment.show(DataUsageSummary.this);
1109
1110 // reset spinner to something other than "change cycle..."
1111 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001112
1113 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001114 if (LOGD) {
1115 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1116 + cycle.end + "]");
1117 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001118
1119 // update chart to show selected cycle, and update detail data
1120 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001121 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001122
1123 updateDetailData();
1124 }
1125 }
1126
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001127 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001128 public void onNothingSelected(AdapterView<?> parent) {
1129 // ignored
1130 }
1131 };
1132
1133 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001134 * Update details based on {@link #mChart} inspection range depending on
1135 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1136 * of applications data usage, and when {@link #isAppDetailMode()} update
1137 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001138 */
1139 private void updateDetailData() {
1140 if (LOGD) Log.d(TAG, "updateDetailData()");
1141
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001142 final long start = mChart.getInspectStart();
1143 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001144 final long now = System.currentTimeMillis();
1145
1146 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001147
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001148 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001149 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001150 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001151 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001152 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001153 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001154 final long foregroundBytes = entry.rxBytes + entry.txBytes;
1155
1156 mAppPieChart.setOriginAngle(175);
1157
1158 mAppPieChart.removeAllSlices();
1159 mAppPieChart.addSlice(foregroundBytes, Color.parseColor("#d88d3a"));
1160 mAppPieChart.addSlice(defaultBytes, Color.parseColor("#666666"));
1161
1162 mAppPieChart.generatePath();
1163
1164 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1165 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1166
1167 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001168 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001169
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001170 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001171
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001172 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001173 if (mChartData != null) {
1174 entry = mChartData.network.getValues(start, end, now, null);
1175 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001176
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001177 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001178 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001179 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001180 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001181
1182 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1183 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001184 final String rangePhrase = formatDateRange(context, start, end);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001185
Jeff Sharkeye557c332012-04-13 16:04:07 -07001186 final int summaryRes;
1187 if (TAB_MOBILE.equals(mCurrentTab) || TAB_3G.equals(mCurrentApp)
1188 || TAB_4G.equals(mCurrentApp)) {
1189 summaryRes = R.string.data_usage_total_during_range_mobile;
1190 } else {
1191 summaryRes = R.string.data_usage_total_during_range;
1192 }
1193
1194 mUsageSummary.setText(getString(summaryRes, totalPhrase, rangePhrase));
Jeff Sharkey92811822012-05-04 11:47:29 -07001195
1196 // initial layout is finished above, ensure we have transitions
1197 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001198 }
1199
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001200 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1201 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001202 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001203 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001204 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001205 }
1206
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001207 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001208 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1209 mChartData = data;
1210 mChart.bindNetworkStats(mChartData.network);
1211 mChart.bindDetailNetworkStats(mChartData.detail);
1212
1213 // calcuate policy cycles based on available data
1214 updatePolicy(true);
1215 updateAppDetail();
1216
1217 // force scroll to top of body when showing detail
1218 if (mChartData.detail != null) {
1219 mListView.smoothScrollToPosition(0);
1220 }
1221 }
1222
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001223 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001224 public void onLoaderReset(Loader<ChartData> loader) {
1225 mChartData = null;
1226 mChart.bindNetworkStats(null);
1227 mChart.bindDetailNetworkStats(null);
1228 }
1229 };
1230
1231 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001232 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001233 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001234 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001235 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001236 }
1237
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001238 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001239 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001240 final int[] restrictedUids = mPolicyManager.getUidsWithPolicy(
Jeff Sharkeye557c332012-04-13 16:04:07 -07001241 POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001242 mAdapter.bindStats(data, restrictedUids);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001243 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001244 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001245
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001246 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001247 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001248 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001249 updateEmptyVisible();
1250 }
1251
1252 private void updateEmptyVisible() {
1253 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1254 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001255 }
1256 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001257
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001258 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001259 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001260 final Context context = getActivity();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001261 if (hasReadyMobileRadio(context)) {
1262 final TelephonyManager tele = TelephonyManager.from(context);
1263 return mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001264 } else {
1265 return false;
1266 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001267 }
1268
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001269 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001270 private void setMobilePolicySplit(boolean split) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001271 final Context context = getActivity();
1272 if (hasReadyMobileRadio(context)) {
1273 final TelephonyManager tele = TelephonyManager.from(context);
1274 mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split);
1275 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001276 }
1277
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001278 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001279 final TelephonyManager tele = TelephonyManager.from(context);
1280 final String actualSubscriberId = tele.getSubscriberId();
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -08001281 return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001282 }
1283
Jeff Sharkey8a503642011-06-10 13:31:21 -07001284 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001285 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001286 public void onInspectRangeChanged() {
1287 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
1288 updateDetailData();
1289 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001290
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001291 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001292 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001293 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001294 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001295
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001296 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001297 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001298 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001299 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001300
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001301 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001302 public void requestWarningEdit() {
1303 WarningEditorFragment.show(DataUsageSummary.this);
1304 }
1305
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001306 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001307 public void requestLimitEdit() {
1308 LimitEditorFragment.show(DataUsageSummary.this);
1309 }
1310 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001311
1312 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001313 * List item that reflects a specific data usage cycle.
1314 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001315 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001316 public CharSequence label;
1317 public long start;
1318 public long end;
1319
Jeff Sharkey8a503642011-06-10 13:31:21 -07001320 CycleItem(CharSequence label) {
1321 this.label = label;
1322 }
1323
1324 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001325 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001326 this.start = start;
1327 this.end = end;
1328 }
1329
Jeff Sharkey8a503642011-06-10 13:31:21 -07001330 @Override
1331 public String toString() {
1332 return label.toString();
1333 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001334
1335 @Override
1336 public boolean equals(Object o) {
1337 if (o instanceof CycleItem) {
1338 final CycleItem another = (CycleItem) o;
1339 return start == another.start && end == another.end;
1340 }
1341 return false;
1342 }
1343
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001344 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001345 public int compareTo(CycleItem another) {
1346 return Long.compare(start, another.start);
1347 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001348 }
1349
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001350 private static final StringBuilder sBuilder = new StringBuilder(50);
1351 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1352 sBuilder, Locale.getDefault());
1353
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001354 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001355 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001356
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001357 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001358 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001359 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1360 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001361 }
1362 }
1363
Jeff Sharkey8a503642011-06-10 13:31:21 -07001364 /**
1365 * Special-case data usage cycle that triggers dialog to change
1366 * {@link NetworkPolicy#cycleDay}.
1367 */
1368 public static class CycleChangeItem extends CycleItem {
1369 public CycleChangeItem(Context context) {
1370 super(context.getString(R.string.data_usage_change_cycle));
1371 }
1372 }
1373
1374 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001375 private boolean mChangePossible = false;
1376 private boolean mChangeVisible = false;
1377
1378 private final CycleChangeItem mChangeItem;
1379
Jeff Sharkey8a503642011-06-10 13:31:21 -07001380 public CycleAdapter(Context context) {
1381 super(context, android.R.layout.simple_spinner_item);
1382 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001383 mChangeItem = new CycleChangeItem(context);
1384 }
1385
1386 public void setChangePossible(boolean possible) {
1387 mChangePossible = possible;
1388 updateChange();
1389 }
1390
1391 public void setChangeVisible(boolean visible) {
1392 mChangeVisible = visible;
1393 updateChange();
1394 }
1395
1396 private void updateChange() {
1397 remove(mChangeItem);
1398 if (mChangePossible && mChangeVisible) {
1399 add(mChangeItem);
1400 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001401 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001402
1403 /**
1404 * Find position of {@link CycleItem} in this adapter which is nearest
1405 * the given {@link CycleItem}.
1406 */
1407 public int findNearestPosition(CycleItem target) {
1408 if (target != null) {
1409 final int count = getCount();
1410 for (int i = count - 1; i >= 0; i--) {
1411 final CycleItem item = getItem(i);
1412 if (item instanceof CycleChangeItem) {
1413 continue;
1414 } else if (item.compareTo(target) >= 0) {
1415 return i;
1416 }
1417 }
1418 }
1419 return 0;
1420 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001421 }
1422
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001423 public static class AppItem implements Comparable<AppItem>, Parcelable {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001424 public final int key;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001425 public boolean restricted;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001426 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001427 public long total;
1428
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001429 public AppItem(int key) {
1430 this.key = key;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001431 }
1432
1433 public AppItem(Parcel parcel) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001434 key = parcel.readInt();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001435 uids = parcel.readSparseBooleanArray();
1436 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001437 }
1438
1439 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001440 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001441 }
1442
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001443 @Override
1444 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001445 dest.writeInt(key);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001446 dest.writeSparseBooleanArray(uids);
1447 dest.writeLong(total);
1448 }
1449
1450 @Override
1451 public int describeContents() {
1452 return 0;
1453 }
1454
1455 @Override
1456 public int compareTo(AppItem another) {
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001457 return Long.compare(another.total, total);
1458 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001459
1460 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1461 @Override
1462 public AppItem createFromParcel(Parcel in) {
1463 return new AppItem(in);
1464 }
1465
1466 @Override
1467 public AppItem[] newArray(int size) {
1468 return new AppItem[size];
1469 }
1470 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001471 }
1472
Jeff Sharkey8a503642011-06-10 13:31:21 -07001473 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001474 * Adapter of applications, sorted by total usage descending.
1475 */
1476 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001477 private final UidDetailProvider mProvider;
1478 private final int mInsetSide;
1479
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001480 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001481 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001482
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001483 public DataUsageAdapter(UidDetailProvider provider, int insetSide) {
1484 mProvider = checkNotNull(provider);
1485 mInsetSide = insetSide;
1486 }
1487
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001488 /**
1489 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1490 */
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001491 public void bindStats(NetworkStats stats, int[] restrictedUids) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001492 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001493
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001494 final int currentUserId = ActivityManager.getCurrentUser();
1495 final SparseArray<AppItem> knownItems = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001496
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001497 NetworkStats.Entry entry = null;
1498 final int size = stats != null ? stats.size() : 0;
1499 for (int i = 0; i < size; i++) {
1500 entry = stats.getValues(i, entry);
1501
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001502 // Decide how to collapse items together
1503 final int uid = entry.uid;
1504 final int collapseKey;
1505 if (UserHandle.isApp(uid)) {
1506 if (UserHandle.getUserId(uid) == currentUserId) {
1507 collapseKey = uid;
1508 } else {
1509 collapseKey = UidDetailProvider.buildKeyForUser(UserHandle.getUserId(uid));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001510 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001511 } else if (uid == UID_REMOVED || uid == UID_TETHERING) {
1512 collapseKey = uid;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001513 } else {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001514 collapseKey = android.os.Process.SYSTEM_UID;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001515 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001516
1517 AppItem item = knownItems.get(collapseKey);
1518 if (item == null) {
1519 item = new AppItem(collapseKey);
1520 mItems.add(item);
1521 knownItems.put(item.key, item);
1522 }
1523 item.addUid(uid);
1524 item.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001525 }
1526
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001527 for (int uid : restrictedUids) {
1528 // Only splice in restricted state for current user
1529 if (UserHandle.getUserId(uid) != currentUserId) continue;
1530
1531 AppItem item = knownItems.get(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001532 if (item == null) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001533 item = new AppItem(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001534 item.total = -1;
1535 mItems.add(item);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001536 knownItems.put(item.key, item);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001537 }
1538 item.restricted = true;
1539 }
1540
Jeff Sharkey8a503642011-06-10 13:31:21 -07001541 Collections.sort(mItems);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001542 mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001543 notifyDataSetChanged();
1544 }
1545
1546 @Override
1547 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001548 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001549 }
1550
1551 @Override
1552 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001553 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001554 }
1555
1556 @Override
1557 public long getItemId(int position) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001558 return mItems.get(position).key;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001559 }
1560
1561 @Override
1562 public View getView(int position, View convertView, ViewGroup parent) {
1563 if (convertView == null) {
1564 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey5d706792011-09-08 18:57:17 -07001565 R.layout.data_usage_item, parent, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001566
1567 if (mInsetSide > 0) {
1568 convertView.setPadding(mInsetSide, 0, mInsetSide, 0);
1569 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001570 }
1571
1572 final Context context = parent.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001573
Jeff Sharkey28130d92011-09-02 16:10:24 -07001574 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001575 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1576 android.R.id.progress);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001577
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001578 // kick off async load of app details
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001579 final AppItem item = mItems.get(position);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001580 UidDetailTask.bindView(mProvider, item, convertView);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001581
Jeff Sharkeye557c332012-04-13 16:04:07 -07001582 if (item.restricted && item.total <= 0) {
1583 text1.setText(R.string.data_usage_app_restricted);
1584 progress.setVisibility(View.GONE);
1585 } else {
1586 text1.setText(Formatter.formatFileSize(context, item.total));
1587 progress.setVisibility(View.VISIBLE);
1588 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001589
1590 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1591 progress.setProgress(percentTotal);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001592
1593 return convertView;
1594 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001595 }
1596
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001597 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001598 * Empty {@link Fragment} that controls display of UID details in
1599 * {@link DataUsageSummary}.
1600 */
1601 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001602 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001603
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001604 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001605 if (!parent.isAdded()) return;
1606
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001607 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001608 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001609
1610 final AppDetailsFragment fragment = new AppDetailsFragment();
1611 fragment.setArguments(args);
1612 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001613 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1614 ft.add(fragment, TAG_APP_DETAILS);
1615 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001616 ft.setBreadCrumbTitle(label);
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001617 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001618 }
1619
1620 @Override
1621 public void onStart() {
1622 super.onStart();
1623 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001624 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001625 target.updateBody();
1626 }
1627
1628 @Override
1629 public void onStop() {
1630 super.onStop();
1631 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001632 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001633 target.updateBody();
1634 }
1635 }
1636
1637 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001638 * Dialog to request user confirmation before setting
1639 * {@link NetworkPolicy#limitBytes}.
1640 */
1641 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001642 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001643 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001644
1645 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001646 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001647
Jeff Sharkey461842a2011-09-25 18:22:48 -07001648 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001649 final CharSequence message;
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001650 final long minLimitBytes = (long) (
1651 parent.mPolicyEditor.getPolicy(parent.mTemplate).warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001652 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001653
1654 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001655 final String currentTab = parent.mCurrentTab;
1656 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001657 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001658 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001659 } else if (TAB_4G.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_MOBILE.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 Sharkey131f9d62011-08-17 17:08:19 -07001665 } else {
1666 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001667 }
1668
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001669 final Bundle args = new Bundle();
1670 args.putCharSequence(EXTRA_MESSAGE, message);
1671 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1672
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001673 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1674 dialog.setArguments(args);
1675 dialog.setTargetFragment(parent, 0);
1676 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1677 }
1678
1679 @Override
1680 public Dialog onCreateDialog(Bundle savedInstanceState) {
1681 final Context context = getActivity();
1682
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001683 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001684 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1685
1686 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1687 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001688 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001689
1690 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001691 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001692 public void onClick(DialogInterface dialog, int which) {
1693 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1694 if (target != null) {
1695 target.setPolicyLimitBytes(limitBytes);
1696 }
1697 }
1698 });
1699
1700 return builder.create();
1701 }
1702 }
1703
1704 /**
1705 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1706 */
1707 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001708 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001709
1710 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001711 if (!parent.isAdded()) return;
1712
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001713 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001714 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001715
1716 final CycleEditorFragment dialog = new CycleEditorFragment();
1717 dialog.setArguments(args);
1718 dialog.setTargetFragment(parent, 0);
1719 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1720 }
1721
1722 @Override
1723 public Dialog onCreateDialog(Bundle savedInstanceState) {
1724 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001725 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1726 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001727
1728 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1729 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1730
1731 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1732 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1733
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001734 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1735 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001736
1737 cycleDayPicker.setMinValue(1);
1738 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001739 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001740 cycleDayPicker.setWrapSelectorWheel(true);
1741
1742 builder.setTitle(R.string.data_usage_cycle_editor_title);
1743 builder.setView(view);
1744
1745 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1746 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001747 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001748 public void onClick(DialogInterface dialog, int which) {
1749 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001750 final String cycleTimezone = new Time().timezone;
1751 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001752 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001753 }
1754 });
1755
1756 return builder.create();
1757 }
1758 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001759
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001760 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001761 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1762 */
1763 public static class WarningEditorFragment extends DialogFragment {
1764 private static final String EXTRA_TEMPLATE = "template";
1765
1766 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001767 if (!parent.isAdded()) return;
1768
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001769 final Bundle args = new Bundle();
1770 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1771
1772 final WarningEditorFragment dialog = new WarningEditorFragment();
1773 dialog.setArguments(args);
1774 dialog.setTargetFragment(parent, 0);
1775 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1776 }
1777
1778 @Override
1779 public Dialog onCreateDialog(Bundle savedInstanceState) {
1780 final Context context = getActivity();
1781 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1782 final NetworkPolicyEditor editor = target.mPolicyEditor;
1783
1784 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1785 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1786
1787 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1788 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1789
1790 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1791 final long warningBytes = editor.getPolicyWarningBytes(template);
1792 final long limitBytes = editor.getPolicyLimitBytes(template);
1793
1794 bytesPicker.setMinValue(0);
1795 if (limitBytes != LIMIT_DISABLED) {
1796 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1797 } else {
1798 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1799 }
1800 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1801 bytesPicker.setWrapSelectorWheel(false);
1802
1803 builder.setTitle(R.string.data_usage_warning_editor_title);
1804 builder.setView(view);
1805
1806 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1807 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001808 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001809 public void onClick(DialogInterface dialog, int which) {
1810 // clear focus to finish pending text edits
1811 bytesPicker.clearFocus();
1812
1813 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1814 editor.setPolicyWarningBytes(template, bytes);
1815 target.updatePolicy(false);
1816 }
1817 });
1818
1819 return builder.create();
1820 }
1821 }
1822
1823 /**
1824 * Dialog to edit {@link NetworkPolicy#limitBytes}.
1825 */
1826 public static class LimitEditorFragment extends DialogFragment {
1827 private static final String EXTRA_TEMPLATE = "template";
1828
1829 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001830 if (!parent.isAdded()) return;
1831
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001832 final Bundle args = new Bundle();
1833 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1834
1835 final LimitEditorFragment dialog = new LimitEditorFragment();
1836 dialog.setArguments(args);
1837 dialog.setTargetFragment(parent, 0);
1838 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
1839 }
1840
1841 @Override
1842 public Dialog onCreateDialog(Bundle savedInstanceState) {
1843 final Context context = getActivity();
1844 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1845 final NetworkPolicyEditor editor = target.mPolicyEditor;
1846
1847 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1848 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1849
1850 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1851 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1852
1853 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1854 final long warningBytes = editor.getPolicyWarningBytes(template);
1855 final long limitBytes = editor.getPolicyLimitBytes(template);
1856
1857 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09001858 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001859 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
1860 } else {
1861 bytesPicker.setMinValue(0);
1862 }
1863 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
1864 bytesPicker.setWrapSelectorWheel(false);
1865
1866 builder.setTitle(R.string.data_usage_limit_editor_title);
1867 builder.setView(view);
1868
1869 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1870 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001871 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001872 public void onClick(DialogInterface dialog, int which) {
1873 // clear focus to finish pending text edits
1874 bytesPicker.clearFocus();
1875
1876 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1877 editor.setPolicyLimitBytes(template, bytes);
1878 target.updatePolicy(false);
1879 }
1880 });
1881
1882 return builder.create();
1883 }
1884 }
1885 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07001886 * Dialog to request user confirmation before disabling data.
1887 */
1888 public static class ConfirmDataDisableFragment extends DialogFragment {
1889 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001890 if (!parent.isAdded()) return;
1891
Jeff Sharkey28130d92011-09-02 16:10:24 -07001892 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
1893 dialog.setTargetFragment(parent, 0);
1894 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
1895 }
1896
1897 @Override
1898 public Dialog onCreateDialog(Bundle savedInstanceState) {
1899 final Context context = getActivity();
1900
1901 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1902 builder.setMessage(R.string.data_usage_disable_mobile);
1903
1904 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001905 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07001906 public void onClick(DialogInterface dialog, int which) {
1907 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1908 if (target != null) {
1909 // TODO: extend to modify policy enabled flag.
1910 target.setMobileDataEnabled(false);
1911 }
1912 }
1913 });
1914 builder.setNegativeButton(android.R.string.cancel, null);
1915
1916 return builder.create();
1917 }
1918 }
1919
1920 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001921 * Dialog to request user confirmation before setting
Christopher Tate5a64c732012-09-07 13:35:31 -07001922 * {@link android.provider.Settings.Global#DATA_ROAMING}.
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001923 */
1924 public static class ConfirmDataRoamingFragment extends DialogFragment {
1925 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001926 if (!parent.isAdded()) return;
1927
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001928 final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment();
1929 dialog.setTargetFragment(parent, 0);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001930 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_ROAMING);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001931 }
1932
1933 @Override
1934 public Dialog onCreateDialog(Bundle savedInstanceState) {
1935 final Context context = getActivity();
1936
1937 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1938 builder.setTitle(R.string.roaming_reenable_title);
1939 builder.setMessage(R.string.roaming_warning);
1940
1941 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001942 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001943 public void onClick(DialogInterface dialog, int which) {
1944 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1945 if (target != null) {
1946 target.setDataRoaming(true);
1947 }
1948 }
1949 });
1950 builder.setNegativeButton(android.R.string.cancel, null);
1951
1952 return builder.create();
1953 }
1954 }
1955
1956 /**
1957 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001958 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001959 */
1960 public static class ConfirmRestrictFragment extends DialogFragment {
1961 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001962 if (!parent.isAdded()) return;
1963
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001964 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1965 dialog.setTargetFragment(parent, 0);
1966 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1967 }
1968
1969 @Override
1970 public Dialog onCreateDialog(Bundle savedInstanceState) {
1971 final Context context = getActivity();
1972
1973 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001974 builder.setTitle(R.string.data_usage_restrict_background_title);
Jeff Sharkey461842a2011-09-25 18:22:48 -07001975 builder.setMessage(getString(R.string.data_usage_restrict_background));
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001976
1977 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001978 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001979 public void onClick(DialogInterface dialog, int which) {
1980 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1981 if (target != null) {
1982 target.setRestrictBackground(true);
1983 }
1984 }
1985 });
1986 builder.setNegativeButton(android.R.string.cancel, null);
1987
1988 return builder.create();
1989 }
1990 }
1991
1992 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001993 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
1994 * change has been denied, usually based on
1995 * {@link DataUsageSummary#hasLimitedNetworks()}.
1996 */
1997 public static class DeniedRestrictFragment extends DialogFragment {
1998 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001999 if (!parent.isAdded()) return;
2000
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002001 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
2002 dialog.setTargetFragment(parent, 0);
2003 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
2004 }
2005
2006 @Override
2007 public Dialog onCreateDialog(Bundle savedInstanceState) {
2008 final Context context = getActivity();
2009
2010 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2011 builder.setTitle(R.string.data_usage_app_restrict_background);
2012 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
2013 builder.setPositiveButton(android.R.string.ok, null);
2014
2015 return builder.create();
2016 }
2017 }
2018
2019 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002020 * Dialog to request user confirmation before setting
2021 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
2022 */
2023 public static class ConfirmAppRestrictFragment extends DialogFragment {
2024 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002025 if (!parent.isAdded()) return;
2026
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002027 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2028 dialog.setTargetFragment(parent, 0);
2029 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2030 }
2031
2032 @Override
2033 public Dialog onCreateDialog(Bundle savedInstanceState) {
2034 final Context context = getActivity();
2035
2036 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002037 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2038 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2039
2040 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002041 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002042 public void onClick(DialogInterface dialog, int which) {
2043 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2044 if (target != null) {
2045 target.setAppRestrictBackground(true);
2046 }
2047 }
2048 });
2049 builder.setNegativeButton(android.R.string.cancel, null);
2050
2051 return builder.create();
2052 }
2053 }
2054
2055 /**
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002056 * Dialog to inform user about changing auto-sync setting
2057 */
2058 public static class ConfirmAutoSyncChangeFragment extends DialogFragment {
Amith Yamasani665235f2012-06-07 19:58:34 -07002059 private static final String SAVE_ENABLING = "enabling";
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002060 private boolean mEnabling;
2061
2062 public static void show(DataUsageSummary parent, boolean enabling) {
2063 if (!parent.isAdded()) return;
2064
2065 final ConfirmAutoSyncChangeFragment dialog = new ConfirmAutoSyncChangeFragment();
2066 dialog.mEnabling = enabling;
2067 dialog.setTargetFragment(parent, 0);
2068 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_AUTO_SYNC_CHANGE);
2069 }
2070
2071 @Override
2072 public Dialog onCreateDialog(Bundle savedInstanceState) {
2073 final Context context = getActivity();
Amith Yamasani665235f2012-06-07 19:58:34 -07002074 if (savedInstanceState != null) {
2075 mEnabling = savedInstanceState.getBoolean(SAVE_ENABLING);
2076 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002077
2078 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2079 if (!mEnabling) {
2080 builder.setTitle(R.string.data_usage_auto_sync_off_dialog_title);
2081 builder.setMessage(R.string.data_usage_auto_sync_off_dialog);
2082 } else {
2083 builder.setTitle(R.string.data_usage_auto_sync_on_dialog_title);
2084 builder.setMessage(R.string.data_usage_auto_sync_on_dialog);
2085 }
2086
2087 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
2088 @Override
2089 public void onClick(DialogInterface dialog, int which) {
2090 ContentResolver.setMasterSyncAutomatically(mEnabling);
2091 }
2092 });
2093 builder.setNegativeButton(android.R.string.cancel, null);
2094
2095 return builder.create();
2096 }
Amith Yamasani665235f2012-06-07 19:58:34 -07002097
2098 @Override
2099 public void onSaveInstanceState(Bundle outState) {
2100 super.onSaveInstanceState(outState);
2101 outState.putBoolean(SAVE_ENABLING, mEnabling);
2102 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002103 }
2104
2105 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002106 * Compute default tab that should be selected, based on
2107 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2108 */
2109 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002110 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
2111 if (template == null) return null;
2112
2113 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002114 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002115 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002116 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002117 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002118 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002119 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002120 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002121 return TAB_WIFI;
2122 default:
2123 return null;
2124 }
2125 }
2126
2127 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002128 * Background task that loads {@link UidDetail}, binding to
2129 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002130 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002131 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2132 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002133 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002134 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002135
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002136 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002137 mProvider = checkNotNull(provider);
2138 mItem = checkNotNull(item);
2139 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002140 }
2141
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002142 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002143 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002144 final UidDetailTask existing = (UidDetailTask) target.getTag();
2145 if (existing != null) {
2146 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002147 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002148
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002149 final UidDetail cachedDetail = provider.getUidDetail(item.key, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002150 if (cachedDetail != null) {
2151 bindView(cachedDetail, target);
2152 } else {
2153 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2154 AsyncTask.THREAD_POOL_EXECUTOR));
2155 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002156 }
2157
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002158 private static void bindView(UidDetail detail, View target) {
2159 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2160 final TextView title = (TextView) target.findViewById(android.R.id.title);
2161
2162 if (detail != null) {
2163 icon.setImageDrawable(detail.icon);
2164 title.setText(detail.label);
2165 } else {
2166 icon.setImageDrawable(null);
2167 title.setText(null);
2168 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002169 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002170
2171 @Override
2172 protected void onPreExecute() {
2173 bindView(null, mTarget);
2174 }
2175
2176 @Override
2177 protected UidDetail doInBackground(Void... params) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002178 return mProvider.getUidDetail(mItem.key, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002179 }
2180
2181 @Override
2182 protected void onPostExecute(UidDetail result) {
2183 bindView(result, mTarget);
2184 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002185 }
2186
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002187 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002188 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002189 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002190 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002191 if (TEST_RADIOS) {
2192 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2193 }
2194
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002195 final ConnectivityManager conn = ConnectivityManager.from(context);
2196 final TelephonyManager tele = TelephonyManager.from(context);
2197
2198 // require both supported network and ready SIM
2199 return conn.isNetworkSupported(TYPE_MOBILE) && tele.getSimState() == SIM_STATE_READY;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002200 }
2201
2202 /**
2203 * Test if device has a mobile 4G data radio.
2204 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002205 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002206 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2207 return false;
2208 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002209 if (TEST_RADIOS) {
2210 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2211 }
2212
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002213 final ConnectivityManager conn = ConnectivityManager.from(context);
2214 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002215
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002216 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Wink Saville55434042012-06-14 12:33:43 -07002217 final boolean hasLte = (tele.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE)
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002218 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002219 return hasWimax || hasLte;
2220 }
2221
2222 /**
2223 * Test if device has a Wi-Fi data radio.
2224 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002225 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002226 if (TEST_RADIOS) {
2227 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2228 }
2229
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002230 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002231 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002232 }
2233
2234 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002235 * Test if device has an ethernet network connection.
2236 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002237 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002238 if (TEST_RADIOS) {
2239 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2240 }
2241
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002242 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002243 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2244
2245 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002246 if (mStatsSession != null) {
2247 try {
2248 ethernetBytes = mStatsSession.getSummaryForNetwork(
2249 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2250 .getTotalBytes();
2251 } catch (RemoteException e) {
2252 throw new RuntimeException(e);
2253 }
2254 } else {
2255 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002256 }
2257
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002258 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002259 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002260 }
2261
2262 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002263 * Inflate a {@link Preference} style layout, adding the given {@link View}
2264 * widget into {@link android.R.id#widget_frame}.
2265 */
2266 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2267 final View view = inflater.inflate(R.layout.preference, root, false);
2268 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2269 android.R.id.widget_frame);
2270 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2271 return view;
2272 }
2273
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002274 private static View inflateAppTitle(
2275 LayoutInflater inflater, ViewGroup root, CharSequence label) {
2276 final TextView view = (TextView) inflater.inflate(
2277 R.layout.data_usage_app_title, root, false);
2278 view.setText(label);
2279 return view;
2280 }
2281
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002282 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002283 * Test if any networks are currently limited.
2284 */
2285 private boolean hasLimitedNetworks() {
2286 return !buildLimitedNetworksList().isEmpty();
2287 }
2288
2289 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002290 * Build string describing currently limited networks, which defines when
2291 * background data is restricted.
2292 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002293 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002294 private CharSequence buildLimitedNetworksString() {
2295 final List<CharSequence> limited = buildLimitedNetworksList();
2296
2297 // handle case where no networks limited
2298 if (limited.isEmpty()) {
2299 limited.add(getText(R.string.data_usage_list_none));
2300 }
2301
2302 return TextUtils.join(limited);
2303 }
2304
2305 /**
2306 * Build list of currently limited networks, which defines when background
2307 * data is restricted.
2308 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002309 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002310 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002311 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002312
2313 // build combined list of all limited networks
2314 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002315
2316 final TelephonyManager tele = TelephonyManager.from(context);
2317 if (tele.getSimState() == SIM_STATE_READY) {
2318 final String subscriberId = getActiveSubscriberId(context);
2319 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2320 limited.add(getText(R.string.data_usage_list_mobile));
2321 }
2322 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2323 limited.add(getText(R.string.data_usage_tab_3g));
2324 }
2325 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2326 limited.add(getText(R.string.data_usage_tab_4g));
2327 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002328 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002329
2330 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002331 limited.add(getText(R.string.data_usage_tab_wifi));
2332 }
2333 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2334 limited.add(getText(R.string.data_usage_tab_ethernet));
2335 }
2336
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002337 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002338 }
2339
2340 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002341 * Inset both selector and divider {@link Drawable} on the given
2342 * {@link ListView} by the requested dimensions.
2343 */
2344 private static void insetListViewDrawables(ListView view, int insetSide) {
2345 final Drawable selector = view.getSelector();
2346 final Drawable divider = view.getDivider();
2347
2348 // fully unregister these drawables so callbacks can be maintained after
2349 // wrapping below.
2350 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2351 view.setSelector(stub);
2352 view.setDivider(stub);
2353
2354 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2355 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2356 }
2357
2358 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002359 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002360 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002361 */
2362 private static void setPreferenceTitle(View parent, int resId) {
2363 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2364 title.setText(resId);
2365 }
2366
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002367 /**
2368 * Set {@link android.R.id#summary} for a preference view inflated with
2369 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2370 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002371 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002372 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2373 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002374 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002375 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002376}