blob: edccf53e0c8f1e0bba6806eac9e71a32a82464cc [file] [log] [blame]
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070019import static android.net.ConnectivityManager.TYPE_ETHERNET;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070020import static android.net.ConnectivityManager.TYPE_MOBILE;
Jeff Sharkeybdf98e82011-11-10 17:17:24 -080021import static android.net.ConnectivityManager.TYPE_WIFI;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070022import static android.net.ConnectivityManager.TYPE_WIMAX;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070023import static android.net.NetworkPolicy.LIMIT_DISABLED;
Jeff Sharkeya53188f2011-09-13 19:56:45 -070024import static android.net.NetworkPolicy.WARNING_DISABLED;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070025import static android.net.NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070026import static android.net.NetworkPolicyManager.POLICY_NONE;
27import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND;
Jeff Sharkey8a503642011-06-10 13:31:21 -070028import static android.net.NetworkPolicyManager.computeLastCycleBoundary;
29import static android.net.NetworkPolicyManager.computeNextCycleBoundary;
Jeff Sharkeya662e492011-06-18 21:57:06 -070030import static android.net.NetworkTemplate.MATCH_MOBILE_3G_LOWER;
31import static android.net.NetworkTemplate.MATCH_MOBILE_4G;
32import static android.net.NetworkTemplate.MATCH_MOBILE_ALL;
33import static android.net.NetworkTemplate.MATCH_WIFI;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070034import static android.net.NetworkTemplate.buildTemplateEthernet;
35import static android.net.NetworkTemplate.buildTemplateMobile3gLower;
36import static android.net.NetworkTemplate.buildTemplateMobile4g;
37import static android.net.NetworkTemplate.buildTemplateMobileAll;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070038import static android.net.NetworkTemplate.buildTemplateWifiWildcard;
Jeff Sharkey77dae912012-02-03 14:50:33 -080039import static android.net.TrafficStats.GB_IN_BYTES;
40import static android.net.TrafficStats.MB_IN_BYTES;
Jeff Sharkeya83a24f2011-09-16 01:52:39 -070041import static android.net.TrafficStats.UID_REMOVED;
42import static android.net.TrafficStats.UID_TETHERING;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070043import static android.telephony.TelephonyManager.SIM_STATE_READY;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070044import static android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
45import static android.text.format.DateUtils.FORMAT_SHOW_DATE;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070046import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -070047import static com.android.internal.util.Preconditions.checkNotNull;
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -070048import static com.android.settings.Utils.prepareCustomPreferencesList;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070049
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070050import android.animation.LayoutTransition;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070051import android.app.AlertDialog;
52import android.app.Dialog;
53import android.app.DialogFragment;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070054import android.app.Fragment;
Amith Yamasani5ba0a022011-11-07 12:29:00 -080055import android.app.FragmentManager;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070056import android.app.FragmentTransaction;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070057import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070058import android.content.ContentResolver;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070059import android.content.Context;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070060import android.content.DialogInterface;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -070061import android.content.Intent;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070062import android.content.Loader;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070063import android.content.SharedPreferences;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070064import android.content.pm.PackageManager;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070065import android.content.res.Resources;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070066import android.graphics.Color;
Jeff Sharkey5d706792011-09-08 18:57:17 -070067import android.graphics.drawable.ColorDrawable;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070068import android.graphics.drawable.Drawable;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070069import android.net.ConnectivityManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070070import android.net.INetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070071import android.net.INetworkStatsService;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070072import android.net.INetworkStatsSession;
Jeff Sharkey8a503642011-06-10 13:31:21 -070073import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070074import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070075import android.net.NetworkStats;
76import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070077import android.net.NetworkTemplate;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070078import android.net.TrafficStats;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070079import android.net.Uri;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070080import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070081import android.os.Bundle;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070082import android.os.INetworkManagementService;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070083import android.os.Parcel;
84import android.os.Parcelable;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070085import android.os.RemoteException;
86import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070087import android.os.SystemProperties;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070088import android.os.UserId;
Jeff Sharkey8a503642011-06-10 13:31:21 -070089import android.preference.Preference;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070090import android.preference.PreferenceActivity;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070091import android.provider.Settings;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070092import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070093import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070094import android.text.format.DateUtils;
95import android.text.format.Formatter;
Jeff Sharkeye5223a02012-03-09 17:11:14 -080096import android.text.format.Time;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070097import android.util.Log;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070098import android.util.SparseArray;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070099import android.util.SparseBooleanArray;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700100import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700101import android.view.Menu;
102import android.view.MenuInflater;
103import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700104import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700105import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700106import android.view.ViewGroup;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700107import android.widget.AdapterView;
108import android.widget.AdapterView.OnItemClickListener;
109import android.widget.AdapterView.OnItemSelectedListener;
110import android.widget.ArrayAdapter;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700111import android.widget.BaseAdapter;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700112import android.widget.Button;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700113import android.widget.CheckBox;
114import android.widget.CompoundButton;
115import android.widget.CompoundButton.OnCheckedChangeListener;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700116import android.widget.ImageView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700117import android.widget.LinearLayout;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700118import android.widget.ListView;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700119import android.widget.NumberPicker;
Jeff Sharkey2412b0f2011-07-17 20:31:40 -0700120import android.widget.ProgressBar;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700121import android.widget.Spinner;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700122import android.widget.Switch;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700123import android.widget.TabHost;
124import android.widget.TabHost.OnTabChangeListener;
125import android.widget.TabHost.TabContentFactory;
126import android.widget.TabHost.TabSpec;
127import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700128import android.widget.TextView;
129
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700130import com.android.internal.telephony.Phone;
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 Sharkey29d56b32011-06-20 17:06:52 -0700450
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700451 mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700452 mMenuDataRoaming.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700453 mMenuDataRoaming.setChecked(getDataRoaming());
454
455 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700456 mMenuRestrictBackground.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
457 mMenuRestrictBackground.setChecked(mPolicyManager.getRestrictBackground());
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700458
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700459 mMenuAutoSync = menu.findItem(R.id.data_usage_menu_auto_sync);
460 mMenuAutoSync.setChecked(ContentResolver.getMasterSyncAutomatically());
461
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700462 final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g);
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700463 split4g.setVisible(hasReadyMobile4gRadio(context) && !appDetailMode);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700464 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700465
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700466 final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700467 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700468 showWifi.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700469 showWifi.setChecked(mShowWifi);
470 } else {
471 showWifi.setVisible(false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700472 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700473
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700474 final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700475 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700476 showEthernet.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700477 showEthernet.setChecked(mShowEthernet);
478 } else {
479 showEthernet.setVisible(false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700480 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700481
482 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
483 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
484 metered.setVisible(!appDetailMode);
485 } else {
486 metered.setVisible(false);
487 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700488
489 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
490 String helpUrl;
491 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
492 Intent helpIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
493 helpIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
494 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
495 help.setIntent(helpIntent);
496 help.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
497 } else {
498 help.setVisible(false);
499 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700500 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700501
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700502 @Override
503 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700504 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700505 case R.id.data_usage_menu_roaming: {
506 final boolean dataRoaming = !item.isChecked();
507 if (dataRoaming) {
508 ConfirmDataRoamingFragment.show(this);
509 } else {
510 // no confirmation to disable roaming
511 setDataRoaming(false);
512 }
513 return true;
514 }
515 case R.id.data_usage_menu_restrict_background: {
516 final boolean restrictBackground = !item.isChecked();
517 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800518 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700519 } else {
520 // no confirmation to drop restriction
521 setRestrictBackground(false);
522 }
523 return true;
524 }
525 case R.id.data_usage_menu_split_4g: {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700526 final boolean mobileSplit = !item.isChecked();
Jeff Sharkeya662e492011-06-18 21:57:06 -0700527 setMobilePolicySplit(mobileSplit);
528 item.setChecked(isMobilePolicySplit());
Jeff Sharkey8a503642011-06-10 13:31:21 -0700529 updateTabs();
530 return true;
531 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700532 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700533 mShowWifi = !item.isChecked();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700534 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700535 item.setChecked(mShowWifi);
536 updateTabs();
537 return true;
538 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700539 case R.id.data_usage_menu_show_ethernet: {
540 mShowEthernet = !item.isChecked();
541 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
542 item.setChecked(mShowEthernet);
543 updateTabs();
544 return true;
545 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700546 case R.id.data_usage_menu_metered: {
547 final PreferenceActivity activity = (PreferenceActivity) getActivity();
548 activity.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
549 R.string.data_usage_metered_title, null, this, 0);
550 return true;
551 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700552 case R.id.data_usage_menu_auto_sync: {
553 ConfirmAutoSyncChangeFragment.show(this, !item.isChecked());
554 return true;
555 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700556 }
557 return false;
558 }
559
Jeff Sharkey94a90952011-06-13 22:31:09 -0700560 @Override
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700561 public void onDestroy() {
Jeff Sharkey94a90952011-06-13 22:31:09 -0700562 mDataEnabledView = null;
563 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700564
565 mUidDetailProvider.clearCache();
566 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700567
568 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700569
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800570 if (this.isRemoving()) {
571 getFragmentManager()
572 .popBackStack(TAG_APP_DETAILS, FragmentManager.POP_BACK_STACK_INCLUSIVE);
573 }
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700574
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800575 super.onDestroy();
576 }
577
Jeff Sharkey8a503642011-06-10 13:31:21 -0700578 /**
Jeff Sharkey92811822012-05-04 11:47:29 -0700579 * Build and assign {@link LayoutTransition} to various containers. Should
580 * only be assigned after initial layout is complete.
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700581 */
Jeff Sharkey92811822012-05-04 11:47:29 -0700582 private void ensureLayoutTransitions() {
583 // skip when already setup
584 if (mChart.getLayoutTransition() != null) return;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700585
Jeff Sharkey92811822012-05-04 11:47:29 -0700586 mTabsContainer.setLayoutTransition(buildLayoutTransition());
587 mHeader.setLayoutTransition(buildLayoutTransition());
588 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700589
Jeff Sharkey92811822012-05-04 11:47:29 -0700590 final LayoutTransition chartTransition = buildLayoutTransition();
591 chartTransition.disableTransitionType(LayoutTransition.APPEARING);
592 chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
593 mChart.setLayoutTransition(chartTransition);
594 }
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700595
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700596 private static LayoutTransition buildLayoutTransition() {
597 final LayoutTransition transition = new LayoutTransition();
598 if (TEST_ANIM) {
599 transition.setDuration(1500);
600 }
601 transition.setAnimateParentHierarchy(false);
602 return transition;
603 }
604
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700605 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700606 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700607 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
608 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700609 */
610 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700611 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700612 mTabHost.clearAllTabs();
613
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700614 final boolean mobileSplit = isMobilePolicySplit();
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700615 if (mobileSplit && hasReadyMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700616 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
617 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700618 } else if (hasReadyMobileRadio(context)) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700619 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700620 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700621 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700622 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
623 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700624 if (mShowEthernet && hasEthernet(context)) {
625 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
626 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700627
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700628 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700629 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
630 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
631 if (mIntentTab != null) {
632 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700633 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700634 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700635 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700636 mTabHost.setCurrentTabByTag(mIntentTab);
637 }
638 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700639 } else if (noTabs) {
640 // no usable tabs, so hide body
641 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700642 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700643 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700644 }
645 }
646
Jeff Sharkey8a503642011-06-10 13:31:21 -0700647 /**
648 * Factory that provide empty {@link View} to make {@link TabHost} happy.
649 */
650 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700651 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700652 public View createTabContent(String tag) {
653 return new View(mTabHost.getContext());
654 }
655 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700656
Jeff Sharkey8a503642011-06-10 13:31:21 -0700657 /**
658 * Build {@link TabSpec} with thin indicator, and empty content.
659 */
660 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700661 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
662 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700663 }
664
665 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700666 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700667 public void onTabChanged(String tabId) {
668 // user changed tab; update body
669 updateBody();
670 }
671 };
672
673 /**
674 * Update body content based on current tab. Loads
675 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
676 * binds them to visible controls.
677 */
678 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700679 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700680 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700681
Jeff Sharkeya662e492011-06-18 21:57:06 -0700682 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700683 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700684
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700685 if (currentTab == null) {
686 Log.w(TAG, "no tab selected; hiding body");
687 mListView.setVisibility(View.GONE);
688 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700689 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700690 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700691 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700692
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700693 final boolean tabChanged = !currentTab.equals(mCurrentTab);
694 mCurrentTab = currentTab;
695
Jeff Sharkey8a503642011-06-10 13:31:21 -0700696 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
697
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700698 mDataEnabledView.setVisibility(View.VISIBLE);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700699
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700700 // TODO: remove mobile tabs when SIM isn't ready
701 final TelephonyManager tele = TelephonyManager.from(context);
702
Jeff Sharkey8a503642011-06-10 13:31:21 -0700703 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700704 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
705 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700706 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700707
708 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700709 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
710 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
711 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700712 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700713
714 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700715 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
716 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
717 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700718 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700719
720 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700721 // wifi doesn't have any controls
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700722 mDataEnabledView.setVisibility(View.GONE);
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700723 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700724 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700725
726 } else if (TAB_ETHERNET.equals(currentTab)) {
727 // ethernet doesn't have any controls
728 mDataEnabledView.setVisibility(View.GONE);
729 mDisableAtLimitView.setVisibility(View.GONE);
730 mTemplate = buildTemplateEthernet();
731
732 } else {
733 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700734 }
735
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700736 // kick off loader for network history
737 // TODO: consider chaining two loaders together instead of reloading
738 // network history when showing app detail.
739 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700740 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700741
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700742 // detail mode can change visible menus, invalidate
743 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700744
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700745 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700746 }
747
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700748 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700749 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700750 }
751
752 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700753 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700754 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700755 */
756 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700757 final Context context = getActivity();
758 final PackageManager pm = context.getPackageManager();
759 final LayoutInflater inflater = getActivity().getLayoutInflater();
760
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700761 if (isAppDetailMode()) {
762 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700763 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700764 } else {
765 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700766 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700767
768 // hide detail stats when not in detail mode
769 mChart.bindDetailNetworkStats(null);
770 return;
771 }
772
773 // remove warning/limit sweeps while in detail mode
774 mChart.bindNetworkPolicy(null);
775
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700776 // show icon and all labels appearing under this app
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700777 final int appId = mCurrentApp.appId;
778 final UidDetail detail = mUidDetailProvider.getUidDetail(appId, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700779 mAppIcon.setImageDrawable(detail.icon);
780
781 mAppTitles.removeAllViews();
782 if (detail.detailLabels != null) {
783 for (CharSequence label : detail.detailLabels) {
784 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label));
785 }
786 } else {
787 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label));
788 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700789
790 // enable settings button when package provides it
791 // TODO: target torwards entire UID instead of just first package
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700792 final String[] packageNames = pm.getPackagesForUid(appId);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700793 if (packageNames != null && packageNames.length > 0) {
794 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
795 mAppSettingsIntent.setPackage(packageNames[0]);
796 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
797
798 final boolean matchFound = pm.resolveActivity(mAppSettingsIntent, 0) != null;
799 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -0700800 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700801
802 } else {
803 mAppSettingsIntent = null;
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700804 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700805 }
806
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700807 updateDetailData();
808
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700809 if (UserId.isApp(appId) && !mPolicyManager.getRestrictBackground()
810 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700811 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800812 setPreferenceSummary(mAppRestrictView,
813 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700814
815 mAppRestrictView.setVisibility(View.VISIBLE);
816 mAppRestrict.setChecked(getAppRestrictBackground());
817
818 } else {
819 mAppRestrictView.setVisibility(View.GONE);
820 }
821 }
822
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700823 private void setPolicyWarningBytes(long warningBytes) {
824 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700825 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700826 updatePolicy(false);
827 }
828
829 private void setPolicyLimitBytes(long limitBytes) {
830 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700831 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700832 updatePolicy(false);
833 }
834
Jeff Sharkey28130d92011-09-02 16:10:24 -0700835 /**
836 * Local cache of value, used to work around delay when
837 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
838 */
839 private Boolean mMobileDataEnabled;
840
841 private boolean isMobileDataEnabled() {
842 if (mMobileDataEnabled != null) {
843 // TODO: deprecate and remove this once enabled flag is on policy
844 return mMobileDataEnabled;
845 } else {
846 return mConnService.getMobileDataEnabled();
847 }
848 }
849
850 private void setMobileDataEnabled(boolean enabled) {
851 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
852 mConnService.setMobileDataEnabled(enabled);
853 mMobileDataEnabled = enabled;
854 updatePolicy(false);
855 }
856
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700857 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
858 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked();
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700859 }
860
861 private boolean isBandwidthControlEnabled() {
862 try {
863 return mNetworkService.isBandwidthControlEnabled();
864 } catch (RemoteException e) {
865 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
866 return false;
867 }
868 }
869
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700870 private boolean getDataRoaming() {
871 final ContentResolver resolver = getActivity().getContentResolver();
872 return Settings.Secure.getInt(resolver, Settings.Secure.DATA_ROAMING, 0) != 0;
873 }
874
875 private void setDataRoaming(boolean enabled) {
876 // TODO: teach telephony DataConnectionTracker to watch and apply
877 // updates when changed.
878 final ContentResolver resolver = getActivity().getContentResolver();
879 Settings.Secure.putInt(resolver, Settings.Secure.DATA_ROAMING, enabled ? 1 : 0);
880 mMenuDataRoaming.setChecked(enabled);
881 }
882
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700883 public void setRestrictBackground(boolean restrictBackground) {
884 mPolicyManager.setRestrictBackground(restrictBackground);
885 mMenuRestrictBackground.setChecked(restrictBackground);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700886 }
887
888 private boolean getAppRestrictBackground() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700889 final int appId = mCurrentApp.appId;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700890 final int uidPolicy = mPolicyManager.getAppPolicy(appId);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700891 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
892 }
893
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700894 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700895 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700896 final int appId = mCurrentApp.appId;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700897 mPolicyManager.setAppPolicy(appId,
898 restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700899 mAppRestrict.setChecked(restrictBackground);
900 }
901
Jeff Sharkey8a503642011-06-10 13:31:21 -0700902 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700903 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
904 * current {@link #mTemplate}.
905 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700906 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700907 if (isAppDetailMode()) {
908 mNetworkSwitches.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700909 } else {
910 mNetworkSwitches.setVisibility(View.VISIBLE);
911 }
912
Jeff Sharkey28130d92011-09-02 16:10:24 -0700913 // TODO: move enabled state directly into policy
914 if (TAB_MOBILE.equals(mCurrentTab)) {
915 mBinding = true;
916 mDataEnabled.setChecked(isMobileDataEnabled());
917 mBinding = false;
918 }
919
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700920 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
921 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700922 mDisableAtLimitView.setVisibility(View.VISIBLE);
923 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700924 if (!isAppDetailMode()) {
925 mChart.bindNetworkPolicy(policy);
926 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700927
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700928 } else {
929 // controls are disabled; don't bind warning/limit sweeps
930 mDisableAtLimitView.setVisibility(View.GONE);
931 mChart.bindNetworkPolicy(null);
932 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700933
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700934 if (refreshCycle) {
935 // generate cycle list based on policy and available history
936 updateCycleList(policy);
937 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700938 }
939
940 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700941 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
942 * and available {@link NetworkStatsHistory} data. Always selects the newest
943 * item, updating the inspection range on {@link #mChart}.
944 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700945 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700946 // stash away currently selected cycle to try restoring below
947 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700948 mCycleAdapter.clear();
949
950 final Context context = mCycleSpinner.getContext();
951
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700952 long historyStart = Long.MAX_VALUE;
953 long historyEnd = Long.MIN_VALUE;
954 if (mChartData != null) {
955 historyStart = mChartData.network.getStart();
956 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700957 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700958
Jeff Sharkey461842a2011-09-25 18:22:48 -0700959 final long now = System.currentTimeMillis();
960 if (historyStart == Long.MAX_VALUE) historyStart = now;
961 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700962
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700963 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700964 if (policy != null) {
965 // find the next cycle boundary
966 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700967
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700968 // walk backwards, generating all valid cycle ranges
969 while (cycleEnd > historyStart) {
970 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
971 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
972 + historyStart);
973 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
974 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700975 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700976 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700977
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700978 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700979 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700980 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700981
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700982 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700983 // no policy defined cycles; show entry for each four-week period
984 long cycleEnd = historyEnd;
985 while (cycleEnd > historyStart) {
986 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
987 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
988 cycleEnd = cycleStart;
989 }
990
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700991 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700992 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700993
994 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700995 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700996 final int position = mCycleAdapter.findNearestPosition(previousItem);
997 mCycleSpinner.setSelection(position);
998
999 // only force-update cycle when changed; skipping preserves any
1000 // user-defined inspection region.
1001 final CycleItem selectedItem = mCycleAdapter.getItem(position);
1002 if (!Objects.equal(selectedItem, previousItem)) {
1003 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1004 } else {
1005 // but still kick off loader for detailed list
1006 updateDetailData();
1007 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001008 } else {
1009 updateDetailData();
1010 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001011 }
1012
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001013 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001014 @Override
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001015 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
1016 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001017
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001018 final boolean dataEnabled = isChecked;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001019 final String currentTab = mCurrentTab;
1020 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001021 if (dataEnabled) {
1022 setMobileDataEnabled(true);
1023 } else {
1024 // disabling data; show confirmation dialog which eventually
1025 // calls setMobileDataEnabled() once user confirms.
1026 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1027 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001028 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001029
Jeff Sharkey28130d92011-09-02 16:10:24 -07001030 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001031 }
1032 };
1033
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001034 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001035 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001036 public void onClick(View v) {
1037 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001038 if (disableAtLimit) {
1039 // enabling limit; show confirmation dialog which eventually
1040 // calls setPolicyLimitBytes() once user confirms.
1041 ConfirmLimitFragment.show(DataUsageSummary.this);
1042 } else {
1043 setPolicyLimitBytes(LIMIT_DISABLED);
1044 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001045 }
1046 };
1047
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001048 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001049 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001050 public void onClick(View v) {
1051 final boolean restrictBackground = !mAppRestrict.isChecked();
1052
1053 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001054 // enabling restriction; show confirmation dialog which
1055 // eventually calls setRestrictBackground() once user
1056 // confirms.
1057 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001058 } else {
1059 setAppRestrictBackground(false);
1060 }
1061 }
1062 };
1063
1064 private OnClickListener mAppSettingsListener = new OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001065 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001066 public void onClick(View v) {
Jeff Sharkeyd92e0412012-04-24 11:35:43 -07001067 if (!isAdded()) return;
1068
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001069 // TODO: target torwards entire UID instead of just first package
1070 startActivity(mAppSettingsIntent);
1071 }
1072 };
1073
Jeff Sharkey8a503642011-06-10 13:31:21 -07001074 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001075 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001076 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001077 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001078 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001079
1080 // TODO: sigh, remove this hack once we understand 6450986
1081 if (mUidDetailProvider == null || app == null) return;
1082
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001083 final UidDetail detail = mUidDetailProvider.getUidDetail(app.appId, true);
1084 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001085 }
1086 };
1087
1088 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001089 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001090 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1091 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1092 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001093 // show cycle editor; will eventually call setPolicyCycleDay()
1094 // when user finishes editing.
1095 CycleEditorFragment.show(DataUsageSummary.this);
1096
1097 // reset spinner to something other than "change cycle..."
1098 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001099
1100 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001101 if (LOGD) {
1102 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1103 + cycle.end + "]");
1104 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001105
1106 // update chart to show selected cycle, and update detail data
1107 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001108 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001109
1110 updateDetailData();
1111 }
1112 }
1113
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001114 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001115 public void onNothingSelected(AdapterView<?> parent) {
1116 // ignored
1117 }
1118 };
1119
1120 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001121 * Update details based on {@link #mChart} inspection range depending on
1122 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1123 * of applications data usage, and when {@link #isAppDetailMode()} update
1124 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001125 */
1126 private void updateDetailData() {
1127 if (LOGD) Log.d(TAG, "updateDetailData()");
1128
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001129 final long start = mChart.getInspectStart();
1130 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001131 final long now = System.currentTimeMillis();
1132
1133 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001134
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001135 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001136 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001137 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001138 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001139 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001140 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001141 final long foregroundBytes = entry.rxBytes + entry.txBytes;
1142
1143 mAppPieChart.setOriginAngle(175);
1144
1145 mAppPieChart.removeAllSlices();
1146 mAppPieChart.addSlice(foregroundBytes, Color.parseColor("#d88d3a"));
1147 mAppPieChart.addSlice(defaultBytes, Color.parseColor("#666666"));
1148
1149 mAppPieChart.generatePath();
1150
1151 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1152 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1153
1154 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001155 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001156
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001157 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001158
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001159 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001160 if (mChartData != null) {
1161 entry = mChartData.network.getValues(start, end, now, null);
1162 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001163
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001164 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001165 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001166 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001167 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001168
1169 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1170 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001171 final String rangePhrase = formatDateRange(context, start, end);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001172
Jeff Sharkeye557c332012-04-13 16:04:07 -07001173 final int summaryRes;
László Dávid0b8bf4e2012-10-24 23:31:47 +02001174 if (TAB_MOBILE.equals(mCurrentTab) || TAB_3G.equals(mCurrentTab)
1175 || TAB_4G.equals(mCurrentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001176 summaryRes = R.string.data_usage_total_during_range_mobile;
1177 } else {
1178 summaryRes = R.string.data_usage_total_during_range;
1179 }
1180
1181 mUsageSummary.setText(getString(summaryRes, totalPhrase, rangePhrase));
Jeff Sharkey92811822012-05-04 11:47:29 -07001182
1183 // initial layout is finished above, ensure we have transitions
1184 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001185 }
1186
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001187 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1188 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001189 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001190 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001191 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001192 }
1193
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001194 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001195 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1196 mChartData = data;
1197 mChart.bindNetworkStats(mChartData.network);
1198 mChart.bindDetailNetworkStats(mChartData.detail);
1199
1200 // calcuate policy cycles based on available data
1201 updatePolicy(true);
1202 updateAppDetail();
1203
1204 // force scroll to top of body when showing detail
1205 if (mChartData.detail != null) {
1206 mListView.smoothScrollToPosition(0);
1207 }
1208 }
1209
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001210 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001211 public void onLoaderReset(Loader<ChartData> loader) {
1212 mChartData = null;
1213 mChart.bindNetworkStats(null);
1214 mChart.bindDetailNetworkStats(null);
1215 }
1216 };
1217
1218 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001219 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001220 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001221 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001222 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001223 }
1224
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001225 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001226 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001227 final int[] restrictedAppIds = mPolicyManager.getAppsWithPolicy(
1228 POLICY_REJECT_METERED_BACKGROUND);
1229 mAdapter.bindStats(data, restrictedAppIds);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001230 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001231 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001232
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001233 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001234 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001235 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001236 updateEmptyVisible();
1237 }
1238
1239 private void updateEmptyVisible() {
1240 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1241 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001242 }
1243 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001244
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001245 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001246 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001247 final Context context = getActivity();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001248 if (hasReadyMobileRadio(context)) {
1249 final TelephonyManager tele = TelephonyManager.from(context);
1250 return mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001251 } else {
1252 return false;
1253 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001254 }
1255
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001256 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001257 private void setMobilePolicySplit(boolean split) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001258 final Context context = getActivity();
1259 if (hasReadyMobileRadio(context)) {
1260 final TelephonyManager tele = TelephonyManager.from(context);
1261 mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split);
1262 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001263 }
1264
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001265 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001266 final TelephonyManager tele = TelephonyManager.from(context);
1267 final String actualSubscriberId = tele.getSubscriberId();
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -08001268 return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001269 }
1270
Jeff Sharkey8a503642011-06-10 13:31:21 -07001271 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001272 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001273 public void onInspectRangeChanged() {
1274 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
1275 updateDetailData();
1276 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001277
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001278 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001279 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001280 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001281 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001282
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001283 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001284 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001285 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001286 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001287
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001288 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001289 public void requestWarningEdit() {
1290 WarningEditorFragment.show(DataUsageSummary.this);
1291 }
1292
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001293 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001294 public void requestLimitEdit() {
1295 LimitEditorFragment.show(DataUsageSummary.this);
1296 }
1297 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001298
1299 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001300 * List item that reflects a specific data usage cycle.
1301 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001302 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001303 public CharSequence label;
1304 public long start;
1305 public long end;
1306
Jeff Sharkey8a503642011-06-10 13:31:21 -07001307 CycleItem(CharSequence label) {
1308 this.label = label;
1309 }
1310
1311 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001312 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001313 this.start = start;
1314 this.end = end;
1315 }
1316
Jeff Sharkey8a503642011-06-10 13:31:21 -07001317 @Override
1318 public String toString() {
1319 return label.toString();
1320 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001321
1322 @Override
1323 public boolean equals(Object o) {
1324 if (o instanceof CycleItem) {
1325 final CycleItem another = (CycleItem) o;
1326 return start == another.start && end == another.end;
1327 }
1328 return false;
1329 }
1330
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001331 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001332 public int compareTo(CycleItem another) {
1333 return Long.compare(start, another.start);
1334 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001335 }
1336
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001337 private static final StringBuilder sBuilder = new StringBuilder(50);
1338 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1339 sBuilder, Locale.getDefault());
1340
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001341 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001342 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001343
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001344 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001345 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001346 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1347 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001348 }
1349 }
1350
Jeff Sharkey8a503642011-06-10 13:31:21 -07001351 /**
1352 * Special-case data usage cycle that triggers dialog to change
1353 * {@link NetworkPolicy#cycleDay}.
1354 */
1355 public static class CycleChangeItem extends CycleItem {
1356 public CycleChangeItem(Context context) {
1357 super(context.getString(R.string.data_usage_change_cycle));
1358 }
1359 }
1360
1361 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001362 private boolean mChangePossible = false;
1363 private boolean mChangeVisible = false;
1364
1365 private final CycleChangeItem mChangeItem;
1366
Jeff Sharkey8a503642011-06-10 13:31:21 -07001367 public CycleAdapter(Context context) {
1368 super(context, android.R.layout.simple_spinner_item);
1369 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001370 mChangeItem = new CycleChangeItem(context);
1371 }
1372
1373 public void setChangePossible(boolean possible) {
1374 mChangePossible = possible;
1375 updateChange();
1376 }
1377
1378 public void setChangeVisible(boolean visible) {
1379 mChangeVisible = visible;
1380 updateChange();
1381 }
1382
1383 private void updateChange() {
1384 remove(mChangeItem);
1385 if (mChangePossible && mChangeVisible) {
1386 add(mChangeItem);
1387 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001388 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001389
1390 /**
1391 * Find position of {@link CycleItem} in this adapter which is nearest
1392 * the given {@link CycleItem}.
1393 */
1394 public int findNearestPosition(CycleItem target) {
1395 if (target != null) {
1396 final int count = getCount();
1397 for (int i = count - 1; i >= 0; i--) {
1398 final CycleItem item = getItem(i);
1399 if (item instanceof CycleChangeItem) {
1400 continue;
1401 } else if (item.compareTo(target) >= 0) {
1402 return i;
1403 }
1404 }
1405 }
1406 return 0;
1407 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001408 }
1409
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001410 public static class AppItem implements Comparable<AppItem>, Parcelable {
1411 public final int appId;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001412 public boolean restricted;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001413 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001414 public long total;
1415
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001416 public AppItem(int appId) {
1417 this.appId = appId;
1418 }
1419
1420 public AppItem(Parcel parcel) {
1421 appId = parcel.readInt();
1422 uids = parcel.readSparseBooleanArray();
1423 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001424 }
1425
1426 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001427 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001428 }
1429
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001430 @Override
1431 public void writeToParcel(Parcel dest, int flags) {
1432 dest.writeInt(appId);
1433 dest.writeSparseBooleanArray(uids);
1434 dest.writeLong(total);
1435 }
1436
1437 @Override
1438 public int describeContents() {
1439 return 0;
1440 }
1441
1442 @Override
1443 public int compareTo(AppItem another) {
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001444 return Long.compare(another.total, total);
1445 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001446
1447 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1448 @Override
1449 public AppItem createFromParcel(Parcel in) {
1450 return new AppItem(in);
1451 }
1452
1453 @Override
1454 public AppItem[] newArray(int size) {
1455 return new AppItem[size];
1456 }
1457 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001458 }
1459
Jeff Sharkey8a503642011-06-10 13:31:21 -07001460 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001461 * Adapter of applications, sorted by total usage descending.
1462 */
1463 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001464 private final UidDetailProvider mProvider;
1465 private final int mInsetSide;
1466
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001467 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001468 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001469
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001470 public DataUsageAdapter(UidDetailProvider provider, int insetSide) {
1471 mProvider = checkNotNull(provider);
1472 mInsetSide = insetSide;
1473 }
1474
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001475 /**
1476 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1477 */
Jeff Sharkeye557c332012-04-13 16:04:07 -07001478 public void bindStats(NetworkStats stats, int[] restrictedAppIds) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001479 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001480
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001481 final AppItem systemItem = new AppItem(android.os.Process.SYSTEM_UID);
1482 final SparseArray<AppItem> knownUids = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001483
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001484 NetworkStats.Entry entry = null;
1485 final int size = stats != null ? stats.size() : 0;
1486 for (int i = 0; i < size; i++) {
1487 entry = stats.getValues(i, entry);
1488
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001489 final boolean isApp = UserId.isApp(entry.uid);
1490 final int appId = isApp ? UserId.getAppId(entry.uid) : entry.uid;
1491 if (isApp || appId == UID_REMOVED || appId == UID_TETHERING) {
1492 AppItem item = knownUids.get(appId);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001493 if (item == null) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001494 item = new AppItem(appId);
1495 knownUids.put(appId, item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001496 mItems.add(item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001497 }
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001498
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001499 item.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001500 item.addUid(entry.uid);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001501 } else {
1502 systemItem.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001503 systemItem.addUid(entry.uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001504 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001505 }
1506
Jeff Sharkeye557c332012-04-13 16:04:07 -07001507 for (int appId : restrictedAppIds) {
1508 AppItem item = knownUids.get(appId);
1509 if (item == null) {
1510 item = new AppItem(appId);
1511 item.total = -1;
1512 mItems.add(item);
1513 }
1514 item.restricted = true;
1515 }
1516
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001517 if (systemItem.total > 0) {
1518 mItems.add(systemItem);
1519 }
1520
Jeff Sharkey8a503642011-06-10 13:31:21 -07001521 Collections.sort(mItems);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001522 mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001523 notifyDataSetChanged();
1524 }
1525
1526 @Override
1527 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001528 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001529 }
1530
1531 @Override
1532 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001533 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001534 }
1535
1536 @Override
1537 public long getItemId(int position) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001538 return mItems.get(position).appId;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001539 }
1540
1541 @Override
1542 public View getView(int position, View convertView, ViewGroup parent) {
1543 if (convertView == null) {
1544 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey5d706792011-09-08 18:57:17 -07001545 R.layout.data_usage_item, parent, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001546
1547 if (mInsetSide > 0) {
1548 convertView.setPadding(mInsetSide, 0, mInsetSide, 0);
1549 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001550 }
1551
1552 final Context context = parent.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001553
Jeff Sharkey28130d92011-09-02 16:10:24 -07001554 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001555 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1556 android.R.id.progress);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001557
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001558 // kick off async load of app details
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001559 final AppItem item = mItems.get(position);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001560 UidDetailTask.bindView(mProvider, item, convertView);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001561
Jeff Sharkeye557c332012-04-13 16:04:07 -07001562 if (item.restricted && item.total <= 0) {
1563 text1.setText(R.string.data_usage_app_restricted);
1564 progress.setVisibility(View.GONE);
1565 } else {
1566 text1.setText(Formatter.formatFileSize(context, item.total));
1567 progress.setVisibility(View.VISIBLE);
1568 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001569
1570 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1571 progress.setProgress(percentTotal);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001572
1573 return convertView;
1574 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001575 }
1576
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001577 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001578 * Empty {@link Fragment} that controls display of UID details in
1579 * {@link DataUsageSummary}.
1580 */
1581 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001582 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001583
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001584 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001585 if (!parent.isAdded()) return;
1586
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001587 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001588 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001589
1590 final AppDetailsFragment fragment = new AppDetailsFragment();
1591 fragment.setArguments(args);
1592 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001593 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1594 ft.add(fragment, TAG_APP_DETAILS);
1595 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001596 ft.setBreadCrumbTitle(label);
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001597 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001598 }
1599
1600 @Override
1601 public void onStart() {
1602 super.onStart();
1603 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001604 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001605 target.updateBody();
1606 }
1607
1608 @Override
1609 public void onStop() {
1610 super.onStop();
1611 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001612 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001613 target.updateBody();
1614 }
1615 }
1616
1617 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001618 * Dialog to request user confirmation before setting
1619 * {@link NetworkPolicy#limitBytes}.
1620 */
1621 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001622 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001623 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001624
1625 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001626 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001627
Jeff Sharkey461842a2011-09-25 18:22:48 -07001628 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001629 final CharSequence message;
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001630 final long minLimitBytes = (long) (
1631 parent.mPolicyEditor.getPolicy(parent.mTemplate).warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001632 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001633
1634 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001635 final String currentTab = parent.mCurrentTab;
1636 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001637 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001638 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001639 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001640 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001641 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001642 } else if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001643 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001644 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001645 } else {
1646 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001647 }
1648
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001649 final Bundle args = new Bundle();
1650 args.putCharSequence(EXTRA_MESSAGE, message);
1651 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1652
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001653 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1654 dialog.setArguments(args);
1655 dialog.setTargetFragment(parent, 0);
1656 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1657 }
1658
1659 @Override
1660 public Dialog onCreateDialog(Bundle savedInstanceState) {
1661 final Context context = getActivity();
1662
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001663 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001664 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1665
1666 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1667 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001668 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001669
1670 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001671 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001672 public void onClick(DialogInterface dialog, int which) {
1673 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1674 if (target != null) {
1675 target.setPolicyLimitBytes(limitBytes);
1676 }
1677 }
1678 });
1679
1680 return builder.create();
1681 }
1682 }
1683
1684 /**
1685 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1686 */
1687 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001688 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001689
1690 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001691 if (!parent.isAdded()) return;
1692
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001693 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001694 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001695
1696 final CycleEditorFragment dialog = new CycleEditorFragment();
1697 dialog.setArguments(args);
1698 dialog.setTargetFragment(parent, 0);
1699 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1700 }
1701
1702 @Override
1703 public Dialog onCreateDialog(Bundle savedInstanceState) {
1704 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001705 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1706 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001707
1708 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1709 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1710
1711 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1712 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1713
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001714 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1715 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001716
1717 cycleDayPicker.setMinValue(1);
1718 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001719 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001720 cycleDayPicker.setWrapSelectorWheel(true);
1721
1722 builder.setTitle(R.string.data_usage_cycle_editor_title);
1723 builder.setView(view);
1724
1725 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1726 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001727 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001728 public void onClick(DialogInterface dialog, int which) {
1729 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001730 final String cycleTimezone = new Time().timezone;
1731 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001732 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001733 }
1734 });
1735
1736 return builder.create();
1737 }
1738 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001739
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001740 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001741 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1742 */
1743 public static class WarningEditorFragment extends DialogFragment {
1744 private static final String EXTRA_TEMPLATE = "template";
1745
1746 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001747 if (!parent.isAdded()) return;
1748
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001749 final Bundle args = new Bundle();
1750 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1751
1752 final WarningEditorFragment dialog = new WarningEditorFragment();
1753 dialog.setArguments(args);
1754 dialog.setTargetFragment(parent, 0);
1755 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1756 }
1757
1758 @Override
1759 public Dialog onCreateDialog(Bundle savedInstanceState) {
1760 final Context context = getActivity();
1761 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1762 final NetworkPolicyEditor editor = target.mPolicyEditor;
1763
1764 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1765 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1766
1767 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1768 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1769
1770 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1771 final long warningBytes = editor.getPolicyWarningBytes(template);
1772 final long limitBytes = editor.getPolicyLimitBytes(template);
1773
1774 bytesPicker.setMinValue(0);
1775 if (limitBytes != LIMIT_DISABLED) {
1776 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1777 } else {
1778 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1779 }
1780 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1781 bytesPicker.setWrapSelectorWheel(false);
1782
1783 builder.setTitle(R.string.data_usage_warning_editor_title);
1784 builder.setView(view);
1785
1786 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1787 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001788 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001789 public void onClick(DialogInterface dialog, int which) {
1790 // clear focus to finish pending text edits
1791 bytesPicker.clearFocus();
1792
1793 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1794 editor.setPolicyWarningBytes(template, bytes);
1795 target.updatePolicy(false);
1796 }
1797 });
1798
1799 return builder.create();
1800 }
1801 }
1802
1803 /**
1804 * Dialog to edit {@link NetworkPolicy#limitBytes}.
1805 */
1806 public static class LimitEditorFragment extends DialogFragment {
1807 private static final String EXTRA_TEMPLATE = "template";
1808
1809 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001810 if (!parent.isAdded()) return;
1811
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001812 final Bundle args = new Bundle();
1813 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1814
1815 final LimitEditorFragment dialog = new LimitEditorFragment();
1816 dialog.setArguments(args);
1817 dialog.setTargetFragment(parent, 0);
1818 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
1819 }
1820
1821 @Override
1822 public Dialog onCreateDialog(Bundle savedInstanceState) {
1823 final Context context = getActivity();
1824 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1825 final NetworkPolicyEditor editor = target.mPolicyEditor;
1826
1827 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1828 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1829
1830 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1831 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1832
1833 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1834 final long warningBytes = editor.getPolicyWarningBytes(template);
1835 final long limitBytes = editor.getPolicyLimitBytes(template);
1836
1837 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09001838 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001839 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
1840 } else {
1841 bytesPicker.setMinValue(0);
1842 }
1843 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
1844 bytesPicker.setWrapSelectorWheel(false);
1845
1846 builder.setTitle(R.string.data_usage_limit_editor_title);
1847 builder.setView(view);
1848
1849 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1850 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001851 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001852 public void onClick(DialogInterface dialog, int which) {
1853 // clear focus to finish pending text edits
1854 bytesPicker.clearFocus();
1855
1856 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1857 editor.setPolicyLimitBytes(template, bytes);
1858 target.updatePolicy(false);
1859 }
1860 });
1861
1862 return builder.create();
1863 }
1864 }
1865 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07001866 * Dialog to request user confirmation before disabling data.
1867 */
1868 public static class ConfirmDataDisableFragment extends DialogFragment {
1869 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001870 if (!parent.isAdded()) return;
1871
Jeff Sharkey28130d92011-09-02 16:10:24 -07001872 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
1873 dialog.setTargetFragment(parent, 0);
1874 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
1875 }
1876
1877 @Override
1878 public Dialog onCreateDialog(Bundle savedInstanceState) {
1879 final Context context = getActivity();
1880
1881 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1882 builder.setMessage(R.string.data_usage_disable_mobile);
1883
1884 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001885 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07001886 public void onClick(DialogInterface dialog, int which) {
1887 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1888 if (target != null) {
1889 // TODO: extend to modify policy enabled flag.
1890 target.setMobileDataEnabled(false);
1891 }
1892 }
1893 });
1894 builder.setNegativeButton(android.R.string.cancel, null);
1895
1896 return builder.create();
1897 }
1898 }
1899
1900 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001901 * Dialog to request user confirmation before setting
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001902 * {@link android.provider.Settings.Secure#DATA_ROAMING}.
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001903 */
1904 public static class ConfirmDataRoamingFragment extends DialogFragment {
1905 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001906 if (!parent.isAdded()) return;
1907
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001908 final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment();
1909 dialog.setTargetFragment(parent, 0);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001910 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_ROAMING);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001911 }
1912
1913 @Override
1914 public Dialog onCreateDialog(Bundle savedInstanceState) {
1915 final Context context = getActivity();
1916
1917 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1918 builder.setTitle(R.string.roaming_reenable_title);
1919 builder.setMessage(R.string.roaming_warning);
1920
1921 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001922 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001923 public void onClick(DialogInterface dialog, int which) {
1924 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1925 if (target != null) {
1926 target.setDataRoaming(true);
1927 }
1928 }
1929 });
1930 builder.setNegativeButton(android.R.string.cancel, null);
1931
1932 return builder.create();
1933 }
1934 }
1935
1936 /**
1937 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001938 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001939 */
1940 public static class ConfirmRestrictFragment extends DialogFragment {
1941 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001942 if (!parent.isAdded()) return;
1943
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001944 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1945 dialog.setTargetFragment(parent, 0);
1946 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1947 }
1948
1949 @Override
1950 public Dialog onCreateDialog(Bundle savedInstanceState) {
1951 final Context context = getActivity();
1952
1953 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001954 builder.setTitle(R.string.data_usage_restrict_background_title);
Jeff Sharkey461842a2011-09-25 18:22:48 -07001955 builder.setMessage(getString(R.string.data_usage_restrict_background));
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001956
1957 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001958 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001959 public void onClick(DialogInterface dialog, int which) {
1960 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1961 if (target != null) {
1962 target.setRestrictBackground(true);
1963 }
1964 }
1965 });
1966 builder.setNegativeButton(android.R.string.cancel, null);
1967
1968 return builder.create();
1969 }
1970 }
1971
1972 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001973 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
1974 * change has been denied, usually based on
1975 * {@link DataUsageSummary#hasLimitedNetworks()}.
1976 */
1977 public static class DeniedRestrictFragment extends DialogFragment {
1978 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001979 if (!parent.isAdded()) return;
1980
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001981 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
1982 dialog.setTargetFragment(parent, 0);
1983 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
1984 }
1985
1986 @Override
1987 public Dialog onCreateDialog(Bundle savedInstanceState) {
1988 final Context context = getActivity();
1989
1990 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1991 builder.setTitle(R.string.data_usage_app_restrict_background);
1992 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
1993 builder.setPositiveButton(android.R.string.ok, null);
1994
1995 return builder.create();
1996 }
1997 }
1998
1999 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002000 * Dialog to request user confirmation before setting
2001 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
2002 */
2003 public static class ConfirmAppRestrictFragment extends DialogFragment {
2004 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002005 if (!parent.isAdded()) return;
2006
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002007 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2008 dialog.setTargetFragment(parent, 0);
2009 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2010 }
2011
2012 @Override
2013 public Dialog onCreateDialog(Bundle savedInstanceState) {
2014 final Context context = getActivity();
2015
2016 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002017 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2018 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2019
2020 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002021 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002022 public void onClick(DialogInterface dialog, int which) {
2023 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2024 if (target != null) {
2025 target.setAppRestrictBackground(true);
2026 }
2027 }
2028 });
2029 builder.setNegativeButton(android.R.string.cancel, null);
2030
2031 return builder.create();
2032 }
2033 }
2034
2035 /**
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002036 * Dialog to inform user about changing auto-sync setting
2037 */
2038 public static class ConfirmAutoSyncChangeFragment extends DialogFragment {
Amith Yamasani665235f2012-06-07 19:58:34 -07002039 private static final String SAVE_ENABLING = "enabling";
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002040 private boolean mEnabling;
2041
2042 public static void show(DataUsageSummary parent, boolean enabling) {
2043 if (!parent.isAdded()) return;
2044
2045 final ConfirmAutoSyncChangeFragment dialog = new ConfirmAutoSyncChangeFragment();
2046 dialog.mEnabling = enabling;
2047 dialog.setTargetFragment(parent, 0);
2048 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_AUTO_SYNC_CHANGE);
2049 }
2050
2051 @Override
2052 public Dialog onCreateDialog(Bundle savedInstanceState) {
2053 final Context context = getActivity();
Amith Yamasani665235f2012-06-07 19:58:34 -07002054 if (savedInstanceState != null) {
2055 mEnabling = savedInstanceState.getBoolean(SAVE_ENABLING);
2056 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002057
2058 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2059 if (!mEnabling) {
2060 builder.setTitle(R.string.data_usage_auto_sync_off_dialog_title);
2061 builder.setMessage(R.string.data_usage_auto_sync_off_dialog);
2062 } else {
2063 builder.setTitle(R.string.data_usage_auto_sync_on_dialog_title);
2064 builder.setMessage(R.string.data_usage_auto_sync_on_dialog);
2065 }
2066
2067 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
2068 @Override
2069 public void onClick(DialogInterface dialog, int which) {
2070 ContentResolver.setMasterSyncAutomatically(mEnabling);
2071 }
2072 });
2073 builder.setNegativeButton(android.R.string.cancel, null);
2074
2075 return builder.create();
2076 }
Amith Yamasani665235f2012-06-07 19:58:34 -07002077
2078 @Override
2079 public void onSaveInstanceState(Bundle outState) {
2080 super.onSaveInstanceState(outState);
2081 outState.putBoolean(SAVE_ENABLING, mEnabling);
2082 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002083 }
2084
2085 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002086 * Compute default tab that should be selected, based on
2087 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2088 */
2089 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002090 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
2091 if (template == null) return null;
2092
2093 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002094 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002095 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002096 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002097 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002098 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002099 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002100 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002101 return TAB_WIFI;
2102 default:
2103 return null;
2104 }
2105 }
2106
2107 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002108 * Background task that loads {@link UidDetail}, binding to
2109 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002110 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002111 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2112 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002113 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002114 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002115
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002116 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002117 mProvider = checkNotNull(provider);
2118 mItem = checkNotNull(item);
2119 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002120 }
2121
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002122 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002123 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002124 final UidDetailTask existing = (UidDetailTask) target.getTag();
2125 if (existing != null) {
2126 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002127 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002128
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002129 final UidDetail cachedDetail = provider.getUidDetail(item.appId, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002130 if (cachedDetail != null) {
2131 bindView(cachedDetail, target);
2132 } else {
2133 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2134 AsyncTask.THREAD_POOL_EXECUTOR));
2135 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002136 }
2137
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002138 private static void bindView(UidDetail detail, View target) {
2139 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2140 final TextView title = (TextView) target.findViewById(android.R.id.title);
2141
2142 if (detail != null) {
2143 icon.setImageDrawable(detail.icon);
2144 title.setText(detail.label);
2145 } else {
2146 icon.setImageDrawable(null);
2147 title.setText(null);
2148 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002149 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002150
2151 @Override
2152 protected void onPreExecute() {
2153 bindView(null, mTarget);
2154 }
2155
2156 @Override
2157 protected UidDetail doInBackground(Void... params) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002158 return mProvider.getUidDetail(mItem.appId, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002159 }
2160
2161 @Override
2162 protected void onPostExecute(UidDetail result) {
2163 bindView(result, mTarget);
2164 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002165 }
2166
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002167 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002168 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002169 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002170 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002171 if (TEST_RADIOS) {
2172 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2173 }
2174
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002175 final ConnectivityManager conn = ConnectivityManager.from(context);
2176 final TelephonyManager tele = TelephonyManager.from(context);
2177
2178 // require both supported network and ready SIM
2179 return conn.isNetworkSupported(TYPE_MOBILE) && tele.getSimState() == SIM_STATE_READY;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002180 }
2181
2182 /**
2183 * Test if device has a mobile 4G data radio.
2184 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002185 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002186 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2187 return false;
2188 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002189 if (TEST_RADIOS) {
2190 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2191 }
2192
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002193 final ConnectivityManager conn = ConnectivityManager.from(context);
2194 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002195
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002196 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Wink Saville55434042012-06-14 12:33:43 -07002197 final boolean hasLte = (tele.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE)
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002198 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002199 return hasWimax || hasLte;
2200 }
2201
2202 /**
2203 * Test if device has a Wi-Fi data radio.
2204 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002205 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002206 if (TEST_RADIOS) {
2207 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2208 }
2209
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002210 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002211 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002212 }
2213
2214 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002215 * Test if device has an ethernet network connection.
2216 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002217 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002218 if (TEST_RADIOS) {
2219 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2220 }
2221
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002222 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002223 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2224
2225 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002226 if (mStatsSession != null) {
2227 try {
2228 ethernetBytes = mStatsSession.getSummaryForNetwork(
2229 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2230 .getTotalBytes();
2231 } catch (RemoteException e) {
2232 throw new RuntimeException(e);
2233 }
2234 } else {
2235 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002236 }
2237
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002238 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002239 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002240 }
2241
2242 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002243 * Inflate a {@link Preference} style layout, adding the given {@link View}
2244 * widget into {@link android.R.id#widget_frame}.
2245 */
2246 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2247 final View view = inflater.inflate(R.layout.preference, root, false);
2248 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2249 android.R.id.widget_frame);
2250 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2251 return view;
2252 }
2253
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002254 private static View inflateAppTitle(
2255 LayoutInflater inflater, ViewGroup root, CharSequence label) {
2256 final TextView view = (TextView) inflater.inflate(
2257 R.layout.data_usage_app_title, root, false);
2258 view.setText(label);
2259 return view;
2260 }
2261
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002262 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002263 * Test if any networks are currently limited.
2264 */
2265 private boolean hasLimitedNetworks() {
2266 return !buildLimitedNetworksList().isEmpty();
2267 }
2268
2269 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002270 * Build string describing currently limited networks, which defines when
2271 * background data is restricted.
2272 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002273 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002274 private CharSequence buildLimitedNetworksString() {
2275 final List<CharSequence> limited = buildLimitedNetworksList();
2276
2277 // handle case where no networks limited
2278 if (limited.isEmpty()) {
2279 limited.add(getText(R.string.data_usage_list_none));
2280 }
2281
2282 return TextUtils.join(limited);
2283 }
2284
2285 /**
2286 * Build list of currently limited networks, which defines when background
2287 * data is restricted.
2288 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002289 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002290 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002291 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002292
2293 // build combined list of all limited networks
2294 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002295
2296 final TelephonyManager tele = TelephonyManager.from(context);
2297 if (tele.getSimState() == SIM_STATE_READY) {
2298 final String subscriberId = getActiveSubscriberId(context);
2299 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2300 limited.add(getText(R.string.data_usage_list_mobile));
2301 }
2302 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2303 limited.add(getText(R.string.data_usage_tab_3g));
2304 }
2305 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2306 limited.add(getText(R.string.data_usage_tab_4g));
2307 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002308 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002309
2310 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002311 limited.add(getText(R.string.data_usage_tab_wifi));
2312 }
2313 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2314 limited.add(getText(R.string.data_usage_tab_ethernet));
2315 }
2316
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002317 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002318 }
2319
2320 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002321 * Inset both selector and divider {@link Drawable} on the given
2322 * {@link ListView} by the requested dimensions.
2323 */
2324 private static void insetListViewDrawables(ListView view, int insetSide) {
2325 final Drawable selector = view.getSelector();
2326 final Drawable divider = view.getDivider();
2327
2328 // fully unregister these drawables so callbacks can be maintained after
2329 // wrapping below.
2330 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2331 view.setSelector(stub);
2332 view.setDivider(stub);
2333
2334 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2335 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2336 }
2337
2338 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002339 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002340 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002341 */
2342 private static void setPreferenceTitle(View parent, int resId) {
2343 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2344 title.setText(resId);
2345 }
2346
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002347 /**
2348 * Set {@link android.R.id#summary} for a preference view inflated with
2349 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2350 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002351 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002352 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2353 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002354 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002355 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002356}