blob: 656d4c488062bac6aba03b561438e5239dee846a [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;
Jeff Sharkey5d706792011-09-08 18:57:17 -0700131import com.android.settings.drawable.InsetBoundsDrawable;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700132import com.android.settings.net.ChartData;
133import com.android.settings.net.ChartDataLoader;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700134import com.android.settings.net.DataUsageMeteredSettings;
Jeff Sharkeya662e492011-06-18 21:57:06 -0700135import com.android.settings.net.NetworkPolicyEditor;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700136import com.android.settings.net.SummaryForAllUidLoader;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700137import com.android.settings.net.UidDetail;
138import com.android.settings.net.UidDetailProvider;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700139import com.android.settings.widget.ChartDataUsageView;
140import com.android.settings.widget.ChartDataUsageView.DataUsageChartListener;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700141import com.android.settings.widget.PieChartView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700142import com.google.android.collect.Lists;
143
144import java.util.ArrayList;
145import java.util.Collections;
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700146import java.util.List;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700147import java.util.Locale;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700148
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700149import libcore.util.Objects;
150
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700151/**
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700152 * Panel showing data usage history across various networks, including options
153 * to inspect based on usage cycle and control through {@link NetworkPolicy}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700154 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700155public class DataUsageSummary extends Fragment {
156 private static final String TAG = "DataUsage";
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800157 private static final boolean LOGD = false;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700158
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700159 // TODO: remove this testing code
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700160 private static final boolean TEST_ANIM = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700161 private static final boolean TEST_RADIOS = false;
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800162
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700163 private static final String TEST_RADIOS_PROP = "test.radios";
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800164 private static final String TEST_SUBSCRIBER_PROP = "test.subscriberid";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700165
Jeff Sharkey8a503642011-06-10 13:31:21 -0700166 private static final String TAB_3G = "3g";
167 private static final String TAB_4G = "4g";
168 private static final String TAB_MOBILE = "mobile";
169 private static final String TAB_WIFI = "wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700170 private static final String TAB_ETHERNET = "ethernet";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700171
Jeff Sharkey28130d92011-09-02 16:10:24 -0700172 private static final String TAG_CONFIRM_DATA_DISABLE = "confirmDataDisable";
173 private static final String TAG_CONFIRM_DATA_ROAMING = "confirmDataRoaming";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700174 private static final String TAG_CONFIRM_LIMIT = "confirmLimit";
175 private static final String TAG_CYCLE_EDITOR = "cycleEditor";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700176 private static final String TAG_WARNING_EDITOR = "warningEditor";
177 private static final String TAG_LIMIT_EDITOR = "limitEditor";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700178 private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700179 private static final String TAG_DENIED_RESTRICT = "deniedRestrict";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700180 private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict";
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700181 private static final String TAG_CONFIRM_AUTO_SYNC_CHANGE = "confirmAutoSyncChange";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700182 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700183
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700184 private static final int LOADER_CHART_DATA = 2;
185 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700186
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700187 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700188 private INetworkStatsService mStatsService;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700189 private NetworkPolicyManager mPolicyManager;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700190 private ConnectivityManager mConnService;
191
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700192 private INetworkStatsSession mStatsSession;
193
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700194 private static final String PREF_FILE = "data_usage";
195 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700196 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700197
198 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700199
Jeff Sharkey8a503642011-06-10 13:31:21 -0700200 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700201 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700202 private TabWidget mTabWidget;
203 private ListView mListView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700204 private DataUsageAdapter mAdapter;
205
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700206 /** Distance to inset content from sides, when needed. */
207 private int mInsetSide = 0;
208
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700209 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700210
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700211 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700212 private LinearLayout mNetworkSwitches;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700213 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700214 private View mDataEnabledView;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700215 private CheckBox mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700216 private View mDisableAtLimitView;
217
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700218 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700219 private Spinner mCycleSpinner;
220 private CycleAdapter mCycleAdapter;
221
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700222 private ChartDataUsageView mChart;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700223 private TextView mUsageSummary;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700224 private TextView mEmpty;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700225
226 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700227 private ImageView mAppIcon;
228 private ViewGroup mAppTitles;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700229 private PieChartView mAppPieChart;
230 private TextView mAppForeground;
231 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700232 private Button mAppSettings;
233
234 private LinearLayout mAppSwitches;
235 private CheckBox mAppRestrict;
236 private View mAppRestrictView;
237
Jeff Sharkey8a503642011-06-10 13:31:21 -0700238 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700239 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700240
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700241 private NetworkTemplate mTemplate;
242 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700243
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700244 private AppItem mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700245
246 private Intent mAppSettingsIntent;
247
Jeff Sharkeya662e492011-06-18 21:57:06 -0700248 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700249
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700250 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700251 private String mIntentTab = null;
252
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700253 private MenuItem mMenuDataRoaming;
254 private MenuItem mMenuRestrictBackground;
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700255 private MenuItem mMenuAutoSync;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700256
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700257 /** Flag used to ignore listeners during binding. */
258 private boolean mBinding;
259
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700260 private UidDetailProvider mUidDetailProvider;
261
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700262 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700263 public void onCreate(Bundle savedInstanceState) {
264 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700265 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700266
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700267 mNetworkService = INetworkManagementService.Stub.asInterface(
268 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700269 mStatsService = INetworkStatsService.Stub.asInterface(
270 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700271 mPolicyManager = NetworkPolicyManager.from(context);
272 mConnService = ConnectivityManager.from(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700273
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700274 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700275
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700276 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700277 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700278
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700279 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700280 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700281
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700282 // override preferences when no mobile radio
283 if (!hasReadyMobileRadio(context)) {
284 mShowWifi = hasWifiRadio(context);
285 mShowEthernet = hasEthernet(context);
286 }
287
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700288 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700289 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700290
Jeff Sharkey8a503642011-06-10 13:31:21 -0700291 @Override
292 public View onCreateView(LayoutInflater inflater, ViewGroup container,
293 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700294
Jeff Sharkey8a503642011-06-10 13:31:21 -0700295 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700296 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
297
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700298 mUidDetailProvider = new UidDetailProvider(context);
299
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700300 try {
301 mStatsSession = mStatsService.openSession();
302 } catch (RemoteException e) {
303 throw new RuntimeException(e);
304 }
305
Jeff Sharkey8a503642011-06-10 13:31:21 -0700306 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700307 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700308 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
309 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700310
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700311 // decide if we need to manually inset our content, or if we should rely
312 // on parent container for inset.
313 final boolean shouldInset = mListView.getScrollBarStyle()
314 == View.SCROLLBARS_OUTSIDE_OVERLAY;
315 if (shouldInset) {
316 mInsetSide = view.getResources().getDimensionPixelOffset(
317 com.android.internal.R.dimen.preference_fragment_padding_side);
318 } else {
319 mInsetSide = 0;
320 }
321
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700322 // adjust padding around tabwidget as needed
Jeff Sharkey5d706792011-09-08 18:57:17 -0700323 prepareCustomPreferencesList(container, view, mListView, true);
324
Jeff Sharkey8a503642011-06-10 13:31:21 -0700325 mTabHost.setup();
326 mTabHost.setOnTabChangedListener(mTabListener);
327
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700328 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700329 mHeader.setClickable(true);
330
331 mListView.addHeaderView(mHeader, null, true);
332 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700333
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700334 if (mInsetSide > 0) {
335 // inset selector and divider drawables
336 insetListViewDrawables(mListView, mInsetSide);
337 mHeader.setPadding(mInsetSide, 0, mInsetSide, 0);
338 }
339
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700340 {
341 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700342 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
343 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700344 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700345
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700346 mDataEnabled = new Switch(inflater.getContext());
347 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
348 mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener);
349 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700350
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700351 mDisableAtLimit = new CheckBox(inflater.getContext());
352 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700353 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700354 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700355 mDisableAtLimitView.setClickable(true);
356 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700357 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
358 mNetworkSwitches.addView(mDisableAtLimitView);
359 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700360
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700361 // bind cycle dropdown
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700362 mCycleView = mHeader.findViewById(R.id.cycles);
363 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700364 mCycleAdapter = new CycleAdapter(context);
365 mCycleSpinner.setAdapter(mCycleAdapter);
366 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
367
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700368 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700369 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800370 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700371
372 {
373 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700374 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700375 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
376 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700377 mAppPieChart = (PieChartView) mAppDetail.findViewById(R.id.app_pie_chart);
378 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
379 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700380 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700381
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700382 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700383 mAppSettings.setOnClickListener(mAppSettingsListener);
384
385 mAppRestrict = new CheckBox(inflater.getContext());
386 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700387 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700388 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700389 mAppRestrictView.setClickable(true);
390 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700391 mAppRestrictView.setOnClickListener(mAppRestrictListener);
392 mAppSwitches.addView(mAppRestrictView);
393 }
394
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700395 mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700396 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700397
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700398 mAdapter = new DataUsageAdapter(mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700399 mListView.setOnItemClickListener(mListListener);
400 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700401
402 return view;
403 }
404
405 @Override
406 public void onResume() {
407 super.onResume();
408
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700409 // pick default tab based on incoming intent
410 final Intent intent = getActivity().getIntent();
411 mIntentTab = computeTabFromIntent(intent);
412
Jeff Sharkey8a503642011-06-10 13:31:21 -0700413 // this kicks off chain reaction which creates tabs, binds the body to
414 // selected network, and binds chart, cycles and detail list.
415 updateTabs();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700416
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700417 // kick off background task to update stats
418 new AsyncTask<Void, Void, Void>() {
419 @Override
420 protected Void doInBackground(Void... params) {
421 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700422 // wait a few seconds before kicking off
423 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700424 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700425 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700426 } catch (RemoteException e) {
427 }
428 return null;
429 }
430
431 @Override
432 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700433 if (isAdded()) {
434 updateBody();
435 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700436 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700437 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700438 }
439
Jeff Sharkey8a503642011-06-10 13:31:21 -0700440 @Override
441 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
442 inflater.inflate(R.menu.data_usage, menu);
443 }
444
445 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700446 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700447 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700448 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700449
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700450 mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700451 mMenuDataRoaming.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700452 mMenuDataRoaming.setChecked(getDataRoaming());
453
454 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700455 mMenuRestrictBackground.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
456 mMenuRestrictBackground.setChecked(mPolicyManager.getRestrictBackground());
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700457
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700458 mMenuAutoSync = menu.findItem(R.id.data_usage_menu_auto_sync);
459 mMenuAutoSync.setChecked(ContentResolver.getMasterSyncAutomatically());
460
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700461 final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g);
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700462 split4g.setVisible(hasReadyMobile4gRadio(context) && !appDetailMode);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700463 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700464
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700465 final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700466 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700467 showWifi.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700468 showWifi.setChecked(mShowWifi);
469 } else {
470 showWifi.setVisible(false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700471 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700472
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700473 final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700474 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700475 showEthernet.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700476 showEthernet.setChecked(mShowEthernet);
477 } else {
478 showEthernet.setVisible(false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700479 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700480
481 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
482 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
483 metered.setVisible(!appDetailMode);
484 } else {
485 metered.setVisible(false);
486 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700487
488 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
489 String helpUrl;
490 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
491 Intent helpIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
492 helpIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
493 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
494 help.setIntent(helpIntent);
495 help.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
496 } else {
497 help.setVisible(false);
498 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700499 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700500
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700501 @Override
502 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700503 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700504 case R.id.data_usage_menu_roaming: {
505 final boolean dataRoaming = !item.isChecked();
506 if (dataRoaming) {
507 ConfirmDataRoamingFragment.show(this);
508 } else {
509 // no confirmation to disable roaming
510 setDataRoaming(false);
511 }
512 return true;
513 }
514 case R.id.data_usage_menu_restrict_background: {
515 final boolean restrictBackground = !item.isChecked();
516 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800517 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700518 } else {
519 // no confirmation to drop restriction
520 setRestrictBackground(false);
521 }
522 return true;
523 }
524 case R.id.data_usage_menu_split_4g: {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700525 final boolean mobileSplit = !item.isChecked();
Jeff Sharkeya662e492011-06-18 21:57:06 -0700526 setMobilePolicySplit(mobileSplit);
527 item.setChecked(isMobilePolicySplit());
Jeff Sharkey8a503642011-06-10 13:31:21 -0700528 updateTabs();
529 return true;
530 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700531 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700532 mShowWifi = !item.isChecked();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700533 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700534 item.setChecked(mShowWifi);
535 updateTabs();
536 return true;
537 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700538 case R.id.data_usage_menu_show_ethernet: {
539 mShowEthernet = !item.isChecked();
540 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
541 item.setChecked(mShowEthernet);
542 updateTabs();
543 return true;
544 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700545 case R.id.data_usage_menu_metered: {
546 final PreferenceActivity activity = (PreferenceActivity) getActivity();
547 activity.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
548 R.string.data_usage_metered_title, null, this, 0);
549 return true;
550 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700551 case R.id.data_usage_menu_auto_sync: {
552 ConfirmAutoSyncChangeFragment.show(this, !item.isChecked());
553 return true;
554 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700555 }
556 return false;
557 }
558
Jeff Sharkey94a90952011-06-13 22:31:09 -0700559 @Override
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700560 public void onDestroy() {
Jeff Sharkey94a90952011-06-13 22:31:09 -0700561 mDataEnabledView = null;
562 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700563
564 mUidDetailProvider.clearCache();
565 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700566
567 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700568
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800569 if (this.isRemoving()) {
570 getFragmentManager()
571 .popBackStack(TAG_APP_DETAILS, FragmentManager.POP_BACK_STACK_INCLUSIVE);
572 }
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700573
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800574 super.onDestroy();
575 }
576
Jeff Sharkey8a503642011-06-10 13:31:21 -0700577 /**
Jeff Sharkey92811822012-05-04 11:47:29 -0700578 * Build and assign {@link LayoutTransition} to various containers. Should
579 * only be assigned after initial layout is complete.
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700580 */
Jeff Sharkey92811822012-05-04 11:47:29 -0700581 private void ensureLayoutTransitions() {
582 // skip when already setup
583 if (mChart.getLayoutTransition() != null) return;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700584
Jeff Sharkey92811822012-05-04 11:47:29 -0700585 mTabsContainer.setLayoutTransition(buildLayoutTransition());
586 mHeader.setLayoutTransition(buildLayoutTransition());
587 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700588
Jeff Sharkey92811822012-05-04 11:47:29 -0700589 final LayoutTransition chartTransition = buildLayoutTransition();
590 chartTransition.disableTransitionType(LayoutTransition.APPEARING);
591 chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
592 mChart.setLayoutTransition(chartTransition);
593 }
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700594
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700595 private static LayoutTransition buildLayoutTransition() {
596 final LayoutTransition transition = new LayoutTransition();
597 if (TEST_ANIM) {
598 transition.setDuration(1500);
599 }
600 transition.setAnimateParentHierarchy(false);
601 return transition;
602 }
603
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700604 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700605 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700606 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
607 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700608 */
609 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700610 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700611 mTabHost.clearAllTabs();
612
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700613 final boolean mobileSplit = isMobilePolicySplit();
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700614 if (mobileSplit && hasReadyMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700615 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
616 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700617 } else if (hasReadyMobileRadio(context)) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700618 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700619 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700620 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700621 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
622 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700623 if (mShowEthernet && hasEthernet(context)) {
624 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
625 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700626
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700627 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700628 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
629 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
630 if (mIntentTab != null) {
631 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700632 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700633 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700634 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700635 mTabHost.setCurrentTabByTag(mIntentTab);
636 }
637 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700638 } else if (noTabs) {
639 // no usable tabs, so hide body
640 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700641 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700642 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700643 }
644 }
645
Jeff Sharkey8a503642011-06-10 13:31:21 -0700646 /**
647 * Factory that provide empty {@link View} to make {@link TabHost} happy.
648 */
649 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700650 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700651 public View createTabContent(String tag) {
652 return new View(mTabHost.getContext());
653 }
654 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700655
Jeff Sharkey8a503642011-06-10 13:31:21 -0700656 /**
657 * Build {@link TabSpec} with thin indicator, and empty content.
658 */
659 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700660 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
661 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700662 }
663
664 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700665 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700666 public void onTabChanged(String tabId) {
667 // user changed tab; update body
668 updateBody();
669 }
670 };
671
672 /**
673 * Update body content based on current tab. Loads
674 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
675 * binds them to visible controls.
676 */
677 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700678 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700679 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700680
Jeff Sharkeya662e492011-06-18 21:57:06 -0700681 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700682 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700683
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700684 if (currentTab == null) {
685 Log.w(TAG, "no tab selected; hiding body");
686 mListView.setVisibility(View.GONE);
687 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700688 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700689 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700690 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700691
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700692 final boolean tabChanged = !currentTab.equals(mCurrentTab);
693 mCurrentTab = currentTab;
694
Jeff Sharkey8a503642011-06-10 13:31:21 -0700695 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
696
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700697 mDataEnabledView.setVisibility(View.VISIBLE);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700698
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700699 // TODO: remove mobile tabs when SIM isn't ready
700 final TelephonyManager tele = TelephonyManager.from(context);
701
Jeff Sharkey8a503642011-06-10 13:31:21 -0700702 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700703 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
704 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700705 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700706
707 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700708 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
709 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
710 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700711 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700712
713 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700714 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
715 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
716 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700717 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700718
719 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700720 // wifi doesn't have any controls
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700721 mDataEnabledView.setVisibility(View.GONE);
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700722 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700723 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700724
725 } else if (TAB_ETHERNET.equals(currentTab)) {
726 // ethernet doesn't have any controls
727 mDataEnabledView.setVisibility(View.GONE);
728 mDisableAtLimitView.setVisibility(View.GONE);
729 mTemplate = buildTemplateEthernet();
730
731 } else {
732 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700733 }
734
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700735 // kick off loader for network history
736 // TODO: consider chaining two loaders together instead of reloading
737 // network history when showing app detail.
738 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700739 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700740
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700741 // detail mode can change visible menus, invalidate
742 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700743
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700744 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700745 }
746
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700747 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700748 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700749 }
750
751 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700752 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700753 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700754 */
755 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700756 final Context context = getActivity();
757 final PackageManager pm = context.getPackageManager();
758 final LayoutInflater inflater = getActivity().getLayoutInflater();
759
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700760 if (isAppDetailMode()) {
761 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700762 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700763 } else {
764 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700765 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700766
767 // hide detail stats when not in detail mode
768 mChart.bindDetailNetworkStats(null);
769 return;
770 }
771
772 // remove warning/limit sweeps while in detail mode
773 mChart.bindNetworkPolicy(null);
774
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700775 // show icon and all labels appearing under this app
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700776 final int appId = mCurrentApp.appId;
777 final UidDetail detail = mUidDetailProvider.getUidDetail(appId, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700778 mAppIcon.setImageDrawable(detail.icon);
779
780 mAppTitles.removeAllViews();
781 if (detail.detailLabels != null) {
782 for (CharSequence label : detail.detailLabels) {
783 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label));
784 }
785 } else {
786 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label));
787 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700788
789 // enable settings button when package provides it
790 // TODO: target torwards entire UID instead of just first package
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700791 final String[] packageNames = pm.getPackagesForUid(appId);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700792 if (packageNames != null && packageNames.length > 0) {
793 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
794 mAppSettingsIntent.setPackage(packageNames[0]);
795 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
796
797 final boolean matchFound = pm.resolveActivity(mAppSettingsIntent, 0) != null;
798 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -0700799 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700800
801 } else {
802 mAppSettingsIntent = null;
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700803 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700804 }
805
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700806 updateDetailData();
807
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700808 if (UserId.isApp(appId) && !mPolicyManager.getRestrictBackground()
809 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700810 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800811 setPreferenceSummary(mAppRestrictView,
812 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700813
814 mAppRestrictView.setVisibility(View.VISIBLE);
815 mAppRestrict.setChecked(getAppRestrictBackground());
816
817 } else {
818 mAppRestrictView.setVisibility(View.GONE);
819 }
820 }
821
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700822 private void setPolicyWarningBytes(long warningBytes) {
823 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700824 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700825 updatePolicy(false);
826 }
827
828 private void setPolicyLimitBytes(long limitBytes) {
829 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700830 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700831 updatePolicy(false);
832 }
833
Jeff Sharkey28130d92011-09-02 16:10:24 -0700834 /**
835 * Local cache of value, used to work around delay when
836 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
837 */
838 private Boolean mMobileDataEnabled;
839
840 private boolean isMobileDataEnabled() {
841 if (mMobileDataEnabled != null) {
842 // TODO: deprecate and remove this once enabled flag is on policy
843 return mMobileDataEnabled;
844 } else {
845 return mConnService.getMobileDataEnabled();
846 }
847 }
848
849 private void setMobileDataEnabled(boolean enabled) {
850 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
851 mConnService.setMobileDataEnabled(enabled);
852 mMobileDataEnabled = enabled;
853 updatePolicy(false);
854 }
855
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700856 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
857 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked();
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700858 }
859
860 private boolean isBandwidthControlEnabled() {
861 try {
862 return mNetworkService.isBandwidthControlEnabled();
863 } catch (RemoteException e) {
864 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
865 return false;
866 }
867 }
868
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700869 private boolean getDataRoaming() {
870 final ContentResolver resolver = getActivity().getContentResolver();
871 return Settings.Secure.getInt(resolver, Settings.Secure.DATA_ROAMING, 0) != 0;
872 }
873
874 private void setDataRoaming(boolean enabled) {
875 // TODO: teach telephony DataConnectionTracker to watch and apply
876 // updates when changed.
877 final ContentResolver resolver = getActivity().getContentResolver();
878 Settings.Secure.putInt(resolver, Settings.Secure.DATA_ROAMING, enabled ? 1 : 0);
879 mMenuDataRoaming.setChecked(enabled);
880 }
881
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700882 public void setRestrictBackground(boolean restrictBackground) {
883 mPolicyManager.setRestrictBackground(restrictBackground);
884 mMenuRestrictBackground.setChecked(restrictBackground);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700885 }
886
887 private boolean getAppRestrictBackground() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700888 final int appId = mCurrentApp.appId;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700889 final int uidPolicy = mPolicyManager.getAppPolicy(appId);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700890 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
891 }
892
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700893 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700894 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700895 final int appId = mCurrentApp.appId;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700896 mPolicyManager.setAppPolicy(appId,
897 restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700898 mAppRestrict.setChecked(restrictBackground);
899 }
900
Jeff Sharkey8a503642011-06-10 13:31:21 -0700901 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700902 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
903 * current {@link #mTemplate}.
904 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700905 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700906 if (isAppDetailMode()) {
907 mNetworkSwitches.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700908 } else {
909 mNetworkSwitches.setVisibility(View.VISIBLE);
910 }
911
Jeff Sharkey28130d92011-09-02 16:10:24 -0700912 // TODO: move enabled state directly into policy
913 if (TAB_MOBILE.equals(mCurrentTab)) {
914 mBinding = true;
915 mDataEnabled.setChecked(isMobileDataEnabled());
916 mBinding = false;
917 }
918
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700919 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
920 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700921 mDisableAtLimitView.setVisibility(View.VISIBLE);
922 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700923 if (!isAppDetailMode()) {
924 mChart.bindNetworkPolicy(policy);
925 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700926
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700927 } else {
928 // controls are disabled; don't bind warning/limit sweeps
929 mDisableAtLimitView.setVisibility(View.GONE);
930 mChart.bindNetworkPolicy(null);
931 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700932
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700933 if (refreshCycle) {
934 // generate cycle list based on policy and available history
935 updateCycleList(policy);
936 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700937 }
938
939 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700940 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
941 * and available {@link NetworkStatsHistory} data. Always selects the newest
942 * item, updating the inspection range on {@link #mChart}.
943 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700944 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700945 // stash away currently selected cycle to try restoring below
946 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700947 mCycleAdapter.clear();
948
949 final Context context = mCycleSpinner.getContext();
950
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700951 long historyStart = Long.MAX_VALUE;
952 long historyEnd = Long.MIN_VALUE;
953 if (mChartData != null) {
954 historyStart = mChartData.network.getStart();
955 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700956 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700957
Jeff Sharkey461842a2011-09-25 18:22:48 -0700958 final long now = System.currentTimeMillis();
959 if (historyStart == Long.MAX_VALUE) historyStart = now;
960 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700961
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700962 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700963 if (policy != null) {
964 // find the next cycle boundary
965 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700966
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700967 // walk backwards, generating all valid cycle ranges
968 while (cycleEnd > historyStart) {
969 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
970 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
971 + historyStart);
972 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
973 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700974 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700975 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700976
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700977 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700978 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700979 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700980
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700981 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700982 // no policy defined cycles; show entry for each four-week period
983 long cycleEnd = historyEnd;
984 while (cycleEnd > historyStart) {
985 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
986 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
987 cycleEnd = cycleStart;
988 }
989
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700990 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700991 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700992
993 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700994 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700995 final int position = mCycleAdapter.findNearestPosition(previousItem);
996 mCycleSpinner.setSelection(position);
997
998 // only force-update cycle when changed; skipping preserves any
999 // user-defined inspection region.
1000 final CycleItem selectedItem = mCycleAdapter.getItem(position);
1001 if (!Objects.equal(selectedItem, previousItem)) {
1002 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1003 } else {
1004 // but still kick off loader for detailed list
1005 updateDetailData();
1006 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001007 } else {
1008 updateDetailData();
1009 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001010 }
1011
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001012 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001013 @Override
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001014 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
1015 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001016
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001017 final boolean dataEnabled = isChecked;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001018 final String currentTab = mCurrentTab;
1019 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001020 if (dataEnabled) {
1021 setMobileDataEnabled(true);
1022 } else {
1023 // disabling data; show confirmation dialog which eventually
1024 // calls setMobileDataEnabled() once user confirms.
1025 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1026 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001027 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001028
Jeff Sharkey28130d92011-09-02 16:10:24 -07001029 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001030 }
1031 };
1032
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001033 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001034 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001035 public void onClick(View v) {
1036 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001037 if (disableAtLimit) {
1038 // enabling limit; show confirmation dialog which eventually
1039 // calls setPolicyLimitBytes() once user confirms.
1040 ConfirmLimitFragment.show(DataUsageSummary.this);
1041 } else {
1042 setPolicyLimitBytes(LIMIT_DISABLED);
1043 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001044 }
1045 };
1046
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001047 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001048 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001049 public void onClick(View v) {
1050 final boolean restrictBackground = !mAppRestrict.isChecked();
1051
1052 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001053 // enabling restriction; show confirmation dialog which
1054 // eventually calls setRestrictBackground() once user
1055 // confirms.
1056 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001057 } else {
1058 setAppRestrictBackground(false);
1059 }
1060 }
1061 };
1062
1063 private OnClickListener mAppSettingsListener = new OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001064 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001065 public void onClick(View v) {
Jeff Sharkeyd92e0412012-04-24 11:35:43 -07001066 if (!isAdded()) return;
1067
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001068 // TODO: target torwards entire UID instead of just first package
1069 startActivity(mAppSettingsIntent);
1070 }
1071 };
1072
Jeff Sharkey8a503642011-06-10 13:31:21 -07001073 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001074 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001075 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001076 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001077 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001078
1079 // TODO: sigh, remove this hack once we understand 6450986
1080 if (mUidDetailProvider == null || app == null) return;
1081
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001082 final UidDetail detail = mUidDetailProvider.getUidDetail(app.appId, true);
1083 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001084 }
1085 };
1086
1087 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001088 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001089 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1090 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1091 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001092 // show cycle editor; will eventually call setPolicyCycleDay()
1093 // when user finishes editing.
1094 CycleEditorFragment.show(DataUsageSummary.this);
1095
1096 // reset spinner to something other than "change cycle..."
1097 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001098
1099 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001100 if (LOGD) {
1101 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1102 + cycle.end + "]");
1103 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001104
1105 // update chart to show selected cycle, and update detail data
1106 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001107 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001108
1109 updateDetailData();
1110 }
1111 }
1112
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001113 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001114 public void onNothingSelected(AdapterView<?> parent) {
1115 // ignored
1116 }
1117 };
1118
1119 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001120 * Update details based on {@link #mChart} inspection range depending on
1121 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1122 * of applications data usage, and when {@link #isAppDetailMode()} update
1123 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001124 */
1125 private void updateDetailData() {
1126 if (LOGD) Log.d(TAG, "updateDetailData()");
1127
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001128 final long start = mChart.getInspectStart();
1129 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001130 final long now = System.currentTimeMillis();
1131
1132 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001133
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001134 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001135 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001136 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001137 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001138 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001139 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001140 final long foregroundBytes = entry.rxBytes + entry.txBytes;
1141
1142 mAppPieChart.setOriginAngle(175);
1143
1144 mAppPieChart.removeAllSlices();
1145 mAppPieChart.addSlice(foregroundBytes, Color.parseColor("#d88d3a"));
1146 mAppPieChart.addSlice(defaultBytes, Color.parseColor("#666666"));
1147
1148 mAppPieChart.generatePath();
1149
1150 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1151 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1152
1153 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001154 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001155
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001156 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001157
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001158 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001159 if (mChartData != null) {
1160 entry = mChartData.network.getValues(start, end, now, null);
1161 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001162
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001163 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001164 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001165 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001166 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001167
1168 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1169 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001170 final String rangePhrase = formatDateRange(context, start, end);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001171
Jeff Sharkeye557c332012-04-13 16:04:07 -07001172 final int summaryRes;
1173 if (TAB_MOBILE.equals(mCurrentTab) || TAB_3G.equals(mCurrentApp)
1174 || TAB_4G.equals(mCurrentApp)) {
1175 summaryRes = R.string.data_usage_total_during_range_mobile;
1176 } else {
1177 summaryRes = R.string.data_usage_total_during_range;
1178 }
1179
1180 mUsageSummary.setText(getString(summaryRes, totalPhrase, rangePhrase));
Jeff Sharkey92811822012-05-04 11:47:29 -07001181
1182 // initial layout is finished above, ensure we have transitions
1183 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001184 }
1185
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001186 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1187 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001188 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001189 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001190 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001191 }
1192
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001193 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001194 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1195 mChartData = data;
1196 mChart.bindNetworkStats(mChartData.network);
1197 mChart.bindDetailNetworkStats(mChartData.detail);
1198
1199 // calcuate policy cycles based on available data
1200 updatePolicy(true);
1201 updateAppDetail();
1202
1203 // force scroll to top of body when showing detail
1204 if (mChartData.detail != null) {
1205 mListView.smoothScrollToPosition(0);
1206 }
1207 }
1208
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001209 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001210 public void onLoaderReset(Loader<ChartData> loader) {
1211 mChartData = null;
1212 mChart.bindNetworkStats(null);
1213 mChart.bindDetailNetworkStats(null);
1214 }
1215 };
1216
1217 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001218 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001219 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001220 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001221 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001222 }
1223
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001224 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001225 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001226 final int[] restrictedAppIds = mPolicyManager.getAppsWithPolicy(
1227 POLICY_REJECT_METERED_BACKGROUND);
1228 mAdapter.bindStats(data, restrictedAppIds);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001229 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001230 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001231
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001232 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001233 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001234 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001235 updateEmptyVisible();
1236 }
1237
1238 private void updateEmptyVisible() {
1239 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1240 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001241 }
1242 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001243
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001244 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001245 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001246 final Context context = getActivity();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001247 if (hasReadyMobileRadio(context)) {
1248 final TelephonyManager tele = TelephonyManager.from(context);
1249 return mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001250 } else {
1251 return false;
1252 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001253 }
1254
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001255 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001256 private void setMobilePolicySplit(boolean split) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001257 final Context context = getActivity();
1258 if (hasReadyMobileRadio(context)) {
1259 final TelephonyManager tele = TelephonyManager.from(context);
1260 mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split);
1261 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001262 }
1263
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001264 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001265 final TelephonyManager tele = TelephonyManager.from(context);
1266 final String actualSubscriberId = tele.getSubscriberId();
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -08001267 return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001268 }
1269
Jeff Sharkey8a503642011-06-10 13:31:21 -07001270 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001271 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001272 public void onInspectRangeChanged() {
1273 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
1274 updateDetailData();
1275 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001276
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001277 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001278 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001279 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001280 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001281
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001282 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001283 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001284 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001285 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001286
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001287 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001288 public void requestWarningEdit() {
1289 WarningEditorFragment.show(DataUsageSummary.this);
1290 }
1291
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001292 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001293 public void requestLimitEdit() {
1294 LimitEditorFragment.show(DataUsageSummary.this);
1295 }
1296 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001297
1298 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001299 * List item that reflects a specific data usage cycle.
1300 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001301 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001302 public CharSequence label;
1303 public long start;
1304 public long end;
1305
Jeff Sharkey8a503642011-06-10 13:31:21 -07001306 CycleItem(CharSequence label) {
1307 this.label = label;
1308 }
1309
1310 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001311 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001312 this.start = start;
1313 this.end = end;
1314 }
1315
Jeff Sharkey8a503642011-06-10 13:31:21 -07001316 @Override
1317 public String toString() {
1318 return label.toString();
1319 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001320
1321 @Override
1322 public boolean equals(Object o) {
1323 if (o instanceof CycleItem) {
1324 final CycleItem another = (CycleItem) o;
1325 return start == another.start && end == another.end;
1326 }
1327 return false;
1328 }
1329
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001330 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001331 public int compareTo(CycleItem another) {
1332 return Long.compare(start, another.start);
1333 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001334 }
1335
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001336 private static final StringBuilder sBuilder = new StringBuilder(50);
1337 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1338 sBuilder, Locale.getDefault());
1339
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001340 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001341 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001342
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001343 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001344 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001345 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1346 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001347 }
1348 }
1349
Jeff Sharkey8a503642011-06-10 13:31:21 -07001350 /**
1351 * Special-case data usage cycle that triggers dialog to change
1352 * {@link NetworkPolicy#cycleDay}.
1353 */
1354 public static class CycleChangeItem extends CycleItem {
1355 public CycleChangeItem(Context context) {
1356 super(context.getString(R.string.data_usage_change_cycle));
1357 }
1358 }
1359
1360 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001361 private boolean mChangePossible = false;
1362 private boolean mChangeVisible = false;
1363
1364 private final CycleChangeItem mChangeItem;
1365
Jeff Sharkey8a503642011-06-10 13:31:21 -07001366 public CycleAdapter(Context context) {
1367 super(context, android.R.layout.simple_spinner_item);
1368 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001369 mChangeItem = new CycleChangeItem(context);
1370 }
1371
1372 public void setChangePossible(boolean possible) {
1373 mChangePossible = possible;
1374 updateChange();
1375 }
1376
1377 public void setChangeVisible(boolean visible) {
1378 mChangeVisible = visible;
1379 updateChange();
1380 }
1381
1382 private void updateChange() {
1383 remove(mChangeItem);
1384 if (mChangePossible && mChangeVisible) {
1385 add(mChangeItem);
1386 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001387 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001388
1389 /**
1390 * Find position of {@link CycleItem} in this adapter which is nearest
1391 * the given {@link CycleItem}.
1392 */
1393 public int findNearestPosition(CycleItem target) {
1394 if (target != null) {
1395 final int count = getCount();
1396 for (int i = count - 1; i >= 0; i--) {
1397 final CycleItem item = getItem(i);
1398 if (item instanceof CycleChangeItem) {
1399 continue;
1400 } else if (item.compareTo(target) >= 0) {
1401 return i;
1402 }
1403 }
1404 }
1405 return 0;
1406 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001407 }
1408
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001409 public static class AppItem implements Comparable<AppItem>, Parcelable {
1410 public final int appId;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001411 public boolean restricted;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001412 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001413 public long total;
1414
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001415 public AppItem(int appId) {
1416 this.appId = appId;
1417 }
1418
1419 public AppItem(Parcel parcel) {
1420 appId = parcel.readInt();
1421 uids = parcel.readSparseBooleanArray();
1422 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001423 }
1424
1425 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001426 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001427 }
1428
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001429 @Override
1430 public void writeToParcel(Parcel dest, int flags) {
1431 dest.writeInt(appId);
1432 dest.writeSparseBooleanArray(uids);
1433 dest.writeLong(total);
1434 }
1435
1436 @Override
1437 public int describeContents() {
1438 return 0;
1439 }
1440
1441 @Override
1442 public int compareTo(AppItem another) {
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001443 return Long.compare(another.total, total);
1444 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001445
1446 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1447 @Override
1448 public AppItem createFromParcel(Parcel in) {
1449 return new AppItem(in);
1450 }
1451
1452 @Override
1453 public AppItem[] newArray(int size) {
1454 return new AppItem[size];
1455 }
1456 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001457 }
1458
Jeff Sharkey8a503642011-06-10 13:31:21 -07001459 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001460 * Adapter of applications, sorted by total usage descending.
1461 */
1462 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001463 private final UidDetailProvider mProvider;
1464 private final int mInsetSide;
1465
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001466 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001467 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001468
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001469 public DataUsageAdapter(UidDetailProvider provider, int insetSide) {
1470 mProvider = checkNotNull(provider);
1471 mInsetSide = insetSide;
1472 }
1473
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001474 /**
1475 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1476 */
Jeff Sharkeye557c332012-04-13 16:04:07 -07001477 public void bindStats(NetworkStats stats, int[] restrictedAppIds) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001478 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001479
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001480 final AppItem systemItem = new AppItem(android.os.Process.SYSTEM_UID);
1481 final SparseArray<AppItem> knownUids = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001482
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001483 NetworkStats.Entry entry = null;
1484 final int size = stats != null ? stats.size() : 0;
1485 for (int i = 0; i < size; i++) {
1486 entry = stats.getValues(i, entry);
1487
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001488 final boolean isApp = UserId.isApp(entry.uid);
1489 final int appId = isApp ? UserId.getAppId(entry.uid) : entry.uid;
1490 if (isApp || appId == UID_REMOVED || appId == UID_TETHERING) {
1491 AppItem item = knownUids.get(appId);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001492 if (item == null) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001493 item = new AppItem(appId);
1494 knownUids.put(appId, item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001495 mItems.add(item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001496 }
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001497
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001498 item.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001499 item.addUid(entry.uid);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001500 } else {
1501 systemItem.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001502 systemItem.addUid(entry.uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001503 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001504 }
1505
Jeff Sharkeye557c332012-04-13 16:04:07 -07001506 for (int appId : restrictedAppIds) {
1507 AppItem item = knownUids.get(appId);
1508 if (item == null) {
1509 item = new AppItem(appId);
1510 item.total = -1;
1511 mItems.add(item);
1512 }
1513 item.restricted = true;
1514 }
1515
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001516 if (systemItem.total > 0) {
1517 mItems.add(systemItem);
1518 }
1519
Jeff Sharkey8a503642011-06-10 13:31:21 -07001520 Collections.sort(mItems);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001521 mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001522 notifyDataSetChanged();
1523 }
1524
1525 @Override
1526 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001527 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001528 }
1529
1530 @Override
1531 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001532 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001533 }
1534
1535 @Override
1536 public long getItemId(int position) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001537 return mItems.get(position).appId;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001538 }
1539
1540 @Override
1541 public View getView(int position, View convertView, ViewGroup parent) {
1542 if (convertView == null) {
1543 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey5d706792011-09-08 18:57:17 -07001544 R.layout.data_usage_item, parent, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001545
1546 if (mInsetSide > 0) {
1547 convertView.setPadding(mInsetSide, 0, mInsetSide, 0);
1548 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001549 }
1550
1551 final Context context = parent.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001552
Jeff Sharkey28130d92011-09-02 16:10:24 -07001553 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001554 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1555 android.R.id.progress);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001556
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001557 // kick off async load of app details
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001558 final AppItem item = mItems.get(position);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001559 UidDetailTask.bindView(mProvider, item, convertView);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001560
Jeff Sharkeye557c332012-04-13 16:04:07 -07001561 if (item.restricted && item.total <= 0) {
1562 text1.setText(R.string.data_usage_app_restricted);
1563 progress.setVisibility(View.GONE);
1564 } else {
1565 text1.setText(Formatter.formatFileSize(context, item.total));
1566 progress.setVisibility(View.VISIBLE);
1567 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001568
1569 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1570 progress.setProgress(percentTotal);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001571
1572 return convertView;
1573 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001574 }
1575
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001576 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001577 * Empty {@link Fragment} that controls display of UID details in
1578 * {@link DataUsageSummary}.
1579 */
1580 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001581 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001582
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001583 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001584 if (!parent.isAdded()) return;
1585
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001586 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001587 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001588
1589 final AppDetailsFragment fragment = new AppDetailsFragment();
1590 fragment.setArguments(args);
1591 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001592 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1593 ft.add(fragment, TAG_APP_DETAILS);
1594 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001595 ft.setBreadCrumbTitle(label);
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001596 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001597 }
1598
1599 @Override
1600 public void onStart() {
1601 super.onStart();
1602 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001603 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001604 target.updateBody();
1605 }
1606
1607 @Override
1608 public void onStop() {
1609 super.onStop();
1610 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001611 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001612 target.updateBody();
1613 }
1614 }
1615
1616 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001617 * Dialog to request user confirmation before setting
1618 * {@link NetworkPolicy#limitBytes}.
1619 */
1620 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001621 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001622 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001623
1624 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001625 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001626
Jeff Sharkey461842a2011-09-25 18:22:48 -07001627 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001628 final CharSequence message;
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001629 final long minLimitBytes = (long) (
1630 parent.mPolicyEditor.getPolicy(parent.mTemplate).warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001631 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001632
1633 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001634 final String currentTab = parent.mCurrentTab;
1635 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001636 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001637 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001638 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001639 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001640 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001641 } else if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001642 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001643 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001644 } else {
1645 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001646 }
1647
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001648 final Bundle args = new Bundle();
1649 args.putCharSequence(EXTRA_MESSAGE, message);
1650 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1651
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001652 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1653 dialog.setArguments(args);
1654 dialog.setTargetFragment(parent, 0);
1655 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1656 }
1657
1658 @Override
1659 public Dialog onCreateDialog(Bundle savedInstanceState) {
1660 final Context context = getActivity();
1661
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001662 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001663 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1664
1665 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1666 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001667 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001668
1669 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001670 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001671 public void onClick(DialogInterface dialog, int which) {
1672 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1673 if (target != null) {
1674 target.setPolicyLimitBytes(limitBytes);
1675 }
1676 }
1677 });
1678
1679 return builder.create();
1680 }
1681 }
1682
1683 /**
1684 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1685 */
1686 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001687 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001688
1689 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001690 if (!parent.isAdded()) return;
1691
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001692 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001693 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001694
1695 final CycleEditorFragment dialog = new CycleEditorFragment();
1696 dialog.setArguments(args);
1697 dialog.setTargetFragment(parent, 0);
1698 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1699 }
1700
1701 @Override
1702 public Dialog onCreateDialog(Bundle savedInstanceState) {
1703 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001704 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1705 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001706
1707 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1708 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1709
1710 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1711 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1712
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001713 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1714 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001715
1716 cycleDayPicker.setMinValue(1);
1717 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001718 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001719 cycleDayPicker.setWrapSelectorWheel(true);
1720
1721 builder.setTitle(R.string.data_usage_cycle_editor_title);
1722 builder.setView(view);
1723
1724 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1725 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001726 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001727 public void onClick(DialogInterface dialog, int which) {
1728 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001729 final String cycleTimezone = new Time().timezone;
1730 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001731 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001732 }
1733 });
1734
1735 return builder.create();
1736 }
1737 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001738
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001739 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001740 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1741 */
1742 public static class WarningEditorFragment extends DialogFragment {
1743 private static final String EXTRA_TEMPLATE = "template";
1744
1745 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001746 if (!parent.isAdded()) return;
1747
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001748 final Bundle args = new Bundle();
1749 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1750
1751 final WarningEditorFragment dialog = new WarningEditorFragment();
1752 dialog.setArguments(args);
1753 dialog.setTargetFragment(parent, 0);
1754 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1755 }
1756
1757 @Override
1758 public Dialog onCreateDialog(Bundle savedInstanceState) {
1759 final Context context = getActivity();
1760 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1761 final NetworkPolicyEditor editor = target.mPolicyEditor;
1762
1763 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1764 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1765
1766 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1767 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1768
1769 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1770 final long warningBytes = editor.getPolicyWarningBytes(template);
1771 final long limitBytes = editor.getPolicyLimitBytes(template);
1772
1773 bytesPicker.setMinValue(0);
1774 if (limitBytes != LIMIT_DISABLED) {
1775 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1776 } else {
1777 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1778 }
1779 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1780 bytesPicker.setWrapSelectorWheel(false);
1781
1782 builder.setTitle(R.string.data_usage_warning_editor_title);
1783 builder.setView(view);
1784
1785 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1786 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001787 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001788 public void onClick(DialogInterface dialog, int which) {
1789 // clear focus to finish pending text edits
1790 bytesPicker.clearFocus();
1791
1792 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1793 editor.setPolicyWarningBytes(template, bytes);
1794 target.updatePolicy(false);
1795 }
1796 });
1797
1798 return builder.create();
1799 }
1800 }
1801
1802 /**
1803 * Dialog to edit {@link NetworkPolicy#limitBytes}.
1804 */
1805 public static class LimitEditorFragment extends DialogFragment {
1806 private static final String EXTRA_TEMPLATE = "template";
1807
1808 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001809 if (!parent.isAdded()) return;
1810
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001811 final Bundle args = new Bundle();
1812 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1813
1814 final LimitEditorFragment dialog = new LimitEditorFragment();
1815 dialog.setArguments(args);
1816 dialog.setTargetFragment(parent, 0);
1817 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
1818 }
1819
1820 @Override
1821 public Dialog onCreateDialog(Bundle savedInstanceState) {
1822 final Context context = getActivity();
1823 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1824 final NetworkPolicyEditor editor = target.mPolicyEditor;
1825
1826 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1827 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1828
1829 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1830 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1831
1832 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1833 final long warningBytes = editor.getPolicyWarningBytes(template);
1834 final long limitBytes = editor.getPolicyLimitBytes(template);
1835
1836 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09001837 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001838 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
1839 } else {
1840 bytesPicker.setMinValue(0);
1841 }
1842 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
1843 bytesPicker.setWrapSelectorWheel(false);
1844
1845 builder.setTitle(R.string.data_usage_limit_editor_title);
1846 builder.setView(view);
1847
1848 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1849 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001850 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001851 public void onClick(DialogInterface dialog, int which) {
1852 // clear focus to finish pending text edits
1853 bytesPicker.clearFocus();
1854
1855 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1856 editor.setPolicyLimitBytes(template, bytes);
1857 target.updatePolicy(false);
1858 }
1859 });
1860
1861 return builder.create();
1862 }
1863 }
1864 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07001865 * Dialog to request user confirmation before disabling data.
1866 */
1867 public static class ConfirmDataDisableFragment extends DialogFragment {
1868 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001869 if (!parent.isAdded()) return;
1870
Jeff Sharkey28130d92011-09-02 16:10:24 -07001871 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
1872 dialog.setTargetFragment(parent, 0);
1873 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
1874 }
1875
1876 @Override
1877 public Dialog onCreateDialog(Bundle savedInstanceState) {
1878 final Context context = getActivity();
1879
1880 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1881 builder.setMessage(R.string.data_usage_disable_mobile);
1882
1883 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001884 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07001885 public void onClick(DialogInterface dialog, int which) {
1886 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1887 if (target != null) {
1888 // TODO: extend to modify policy enabled flag.
1889 target.setMobileDataEnabled(false);
1890 }
1891 }
1892 });
1893 builder.setNegativeButton(android.R.string.cancel, null);
1894
1895 return builder.create();
1896 }
1897 }
1898
1899 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001900 * Dialog to request user confirmation before setting
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001901 * {@link android.provider.Settings.Secure#DATA_ROAMING}.
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001902 */
1903 public static class ConfirmDataRoamingFragment extends DialogFragment {
1904 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001905 if (!parent.isAdded()) return;
1906
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001907 final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment();
1908 dialog.setTargetFragment(parent, 0);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001909 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_ROAMING);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001910 }
1911
1912 @Override
1913 public Dialog onCreateDialog(Bundle savedInstanceState) {
1914 final Context context = getActivity();
1915
1916 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1917 builder.setTitle(R.string.roaming_reenable_title);
1918 builder.setMessage(R.string.roaming_warning);
1919
1920 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001921 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001922 public void onClick(DialogInterface dialog, int which) {
1923 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1924 if (target != null) {
1925 target.setDataRoaming(true);
1926 }
1927 }
1928 });
1929 builder.setNegativeButton(android.R.string.cancel, null);
1930
1931 return builder.create();
1932 }
1933 }
1934
1935 /**
1936 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001937 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001938 */
1939 public static class ConfirmRestrictFragment extends DialogFragment {
1940 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001941 if (!parent.isAdded()) return;
1942
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001943 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1944 dialog.setTargetFragment(parent, 0);
1945 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1946 }
1947
1948 @Override
1949 public Dialog onCreateDialog(Bundle savedInstanceState) {
1950 final Context context = getActivity();
1951
1952 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001953 builder.setTitle(R.string.data_usage_restrict_background_title);
Jeff Sharkey461842a2011-09-25 18:22:48 -07001954 builder.setMessage(getString(R.string.data_usage_restrict_background));
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001955
1956 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001957 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001958 public void onClick(DialogInterface dialog, int which) {
1959 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1960 if (target != null) {
1961 target.setRestrictBackground(true);
1962 }
1963 }
1964 });
1965 builder.setNegativeButton(android.R.string.cancel, null);
1966
1967 return builder.create();
1968 }
1969 }
1970
1971 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001972 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
1973 * change has been denied, usually based on
1974 * {@link DataUsageSummary#hasLimitedNetworks()}.
1975 */
1976 public static class DeniedRestrictFragment extends DialogFragment {
1977 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001978 if (!parent.isAdded()) return;
1979
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001980 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
1981 dialog.setTargetFragment(parent, 0);
1982 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
1983 }
1984
1985 @Override
1986 public Dialog onCreateDialog(Bundle savedInstanceState) {
1987 final Context context = getActivity();
1988
1989 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1990 builder.setTitle(R.string.data_usage_app_restrict_background);
1991 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
1992 builder.setPositiveButton(android.R.string.ok, null);
1993
1994 return builder.create();
1995 }
1996 }
1997
1998 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001999 * Dialog to request user confirmation before setting
2000 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
2001 */
2002 public static class ConfirmAppRestrictFragment extends DialogFragment {
2003 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002004 if (!parent.isAdded()) return;
2005
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002006 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2007 dialog.setTargetFragment(parent, 0);
2008 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2009 }
2010
2011 @Override
2012 public Dialog onCreateDialog(Bundle savedInstanceState) {
2013 final Context context = getActivity();
2014
2015 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002016 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2017 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2018
2019 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002020 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002021 public void onClick(DialogInterface dialog, int which) {
2022 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2023 if (target != null) {
2024 target.setAppRestrictBackground(true);
2025 }
2026 }
2027 });
2028 builder.setNegativeButton(android.R.string.cancel, null);
2029
2030 return builder.create();
2031 }
2032 }
2033
2034 /**
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002035 * Dialog to inform user about changing auto-sync setting
2036 */
2037 public static class ConfirmAutoSyncChangeFragment extends DialogFragment {
2038 private boolean mEnabling;
2039
2040 public static void show(DataUsageSummary parent, boolean enabling) {
2041 if (!parent.isAdded()) return;
2042
2043 final ConfirmAutoSyncChangeFragment dialog = new ConfirmAutoSyncChangeFragment();
2044 dialog.mEnabling = enabling;
2045 dialog.setTargetFragment(parent, 0);
2046 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_AUTO_SYNC_CHANGE);
2047 }
2048
2049 @Override
2050 public Dialog onCreateDialog(Bundle savedInstanceState) {
2051 final Context context = getActivity();
2052
2053 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2054 if (!mEnabling) {
2055 builder.setTitle(R.string.data_usage_auto_sync_off_dialog_title);
2056 builder.setMessage(R.string.data_usage_auto_sync_off_dialog);
2057 } else {
2058 builder.setTitle(R.string.data_usage_auto_sync_on_dialog_title);
2059 builder.setMessage(R.string.data_usage_auto_sync_on_dialog);
2060 }
2061
2062 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
2063 @Override
2064 public void onClick(DialogInterface dialog, int which) {
2065 ContentResolver.setMasterSyncAutomatically(mEnabling);
2066 }
2067 });
2068 builder.setNegativeButton(android.R.string.cancel, null);
2069
2070 return builder.create();
2071 }
2072 }
2073
2074 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002075 * Compute default tab that should be selected, based on
2076 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2077 */
2078 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002079 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
2080 if (template == null) return null;
2081
2082 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002083 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002084 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002085 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002086 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002087 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002088 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002089 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002090 return TAB_WIFI;
2091 default:
2092 return null;
2093 }
2094 }
2095
2096 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002097 * Background task that loads {@link UidDetail}, binding to
2098 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002099 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002100 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2101 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002102 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002103 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002104
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002105 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002106 mProvider = checkNotNull(provider);
2107 mItem = checkNotNull(item);
2108 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002109 }
2110
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002111 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002112 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002113 final UidDetailTask existing = (UidDetailTask) target.getTag();
2114 if (existing != null) {
2115 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002116 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002117
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002118 final UidDetail cachedDetail = provider.getUidDetail(item.appId, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002119 if (cachedDetail != null) {
2120 bindView(cachedDetail, target);
2121 } else {
2122 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2123 AsyncTask.THREAD_POOL_EXECUTOR));
2124 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002125 }
2126
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002127 private static void bindView(UidDetail detail, View target) {
2128 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2129 final TextView title = (TextView) target.findViewById(android.R.id.title);
2130
2131 if (detail != null) {
2132 icon.setImageDrawable(detail.icon);
2133 title.setText(detail.label);
2134 } else {
2135 icon.setImageDrawable(null);
2136 title.setText(null);
2137 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002138 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002139
2140 @Override
2141 protected void onPreExecute() {
2142 bindView(null, mTarget);
2143 }
2144
2145 @Override
2146 protected UidDetail doInBackground(Void... params) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002147 return mProvider.getUidDetail(mItem.appId, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002148 }
2149
2150 @Override
2151 protected void onPostExecute(UidDetail result) {
2152 bindView(result, mTarget);
2153 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002154 }
2155
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002156 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002157 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002158 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002159 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002160 if (TEST_RADIOS) {
2161 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2162 }
2163
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002164 final ConnectivityManager conn = ConnectivityManager.from(context);
2165 final TelephonyManager tele = TelephonyManager.from(context);
2166
2167 // require both supported network and ready SIM
2168 return conn.isNetworkSupported(TYPE_MOBILE) && tele.getSimState() == SIM_STATE_READY;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002169 }
2170
2171 /**
2172 * Test if device has a mobile 4G data radio.
2173 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002174 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002175 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2176 return false;
2177 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002178 if (TEST_RADIOS) {
2179 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2180 }
2181
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002182 final ConnectivityManager conn = ConnectivityManager.from(context);
2183 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002184
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002185 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002186 final boolean hasLte = (tele.getLteOnCdmaMode() == Phone.LTE_ON_CDMA_TRUE)
2187 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002188 return hasWimax || hasLte;
2189 }
2190
2191 /**
2192 * Test if device has a Wi-Fi data radio.
2193 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002194 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002195 if (TEST_RADIOS) {
2196 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2197 }
2198
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002199 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002200 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002201 }
2202
2203 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002204 * Test if device has an ethernet network connection.
2205 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002206 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002207 if (TEST_RADIOS) {
2208 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2209 }
2210
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002211 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002212 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2213
2214 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002215 if (mStatsSession != null) {
2216 try {
2217 ethernetBytes = mStatsSession.getSummaryForNetwork(
2218 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2219 .getTotalBytes();
2220 } catch (RemoteException e) {
2221 throw new RuntimeException(e);
2222 }
2223 } else {
2224 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002225 }
2226
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002227 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002228 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002229 }
2230
2231 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002232 * Inflate a {@link Preference} style layout, adding the given {@link View}
2233 * widget into {@link android.R.id#widget_frame}.
2234 */
2235 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2236 final View view = inflater.inflate(R.layout.preference, root, false);
2237 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2238 android.R.id.widget_frame);
2239 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2240 return view;
2241 }
2242
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002243 private static View inflateAppTitle(
2244 LayoutInflater inflater, ViewGroup root, CharSequence label) {
2245 final TextView view = (TextView) inflater.inflate(
2246 R.layout.data_usage_app_title, root, false);
2247 view.setText(label);
2248 return view;
2249 }
2250
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002251 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002252 * Test if any networks are currently limited.
2253 */
2254 private boolean hasLimitedNetworks() {
2255 return !buildLimitedNetworksList().isEmpty();
2256 }
2257
2258 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002259 * Build string describing currently limited networks, which defines when
2260 * background data is restricted.
2261 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002262 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002263 private CharSequence buildLimitedNetworksString() {
2264 final List<CharSequence> limited = buildLimitedNetworksList();
2265
2266 // handle case where no networks limited
2267 if (limited.isEmpty()) {
2268 limited.add(getText(R.string.data_usage_list_none));
2269 }
2270
2271 return TextUtils.join(limited);
2272 }
2273
2274 /**
2275 * Build list of currently limited networks, which defines when background
2276 * data is restricted.
2277 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002278 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002279 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002280 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002281
2282 // build combined list of all limited networks
2283 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002284
2285 final TelephonyManager tele = TelephonyManager.from(context);
2286 if (tele.getSimState() == SIM_STATE_READY) {
2287 final String subscriberId = getActiveSubscriberId(context);
2288 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2289 limited.add(getText(R.string.data_usage_list_mobile));
2290 }
2291 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2292 limited.add(getText(R.string.data_usage_tab_3g));
2293 }
2294 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2295 limited.add(getText(R.string.data_usage_tab_4g));
2296 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002297 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002298
2299 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002300 limited.add(getText(R.string.data_usage_tab_wifi));
2301 }
2302 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2303 limited.add(getText(R.string.data_usage_tab_ethernet));
2304 }
2305
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002306 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002307 }
2308
2309 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002310 * Inset both selector and divider {@link Drawable} on the given
2311 * {@link ListView} by the requested dimensions.
2312 */
2313 private static void insetListViewDrawables(ListView view, int insetSide) {
2314 final Drawable selector = view.getSelector();
2315 final Drawable divider = view.getDivider();
2316
2317 // fully unregister these drawables so callbacks can be maintained after
2318 // wrapping below.
2319 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2320 view.setSelector(stub);
2321 view.setDivider(stub);
2322
2323 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2324 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2325 }
2326
2327 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002328 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002329 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002330 */
2331 private static void setPreferenceTitle(View parent, int resId) {
2332 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2333 title.setText(resId);
2334 }
2335
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002336 /**
2337 * Set {@link android.R.id#summary} for a preference view inflated with
2338 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2339 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002340 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002341 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2342 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002343 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002344 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002345}