blob: 6af16048c4b56630971946df27749f4ad4f5bd74 [file] [log] [blame]
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070019import static android.net.ConnectivityManager.TYPE_ETHERNET;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070020import static android.net.ConnectivityManager.TYPE_MOBILE;
Jeff Sharkeybdf98e82011-11-10 17:17:24 -080021import static android.net.ConnectivityManager.TYPE_WIFI;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070022import static android.net.ConnectivityManager.TYPE_WIMAX;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070023import static android.net.NetworkPolicy.LIMIT_DISABLED;
Jeff Sharkeya53188f2011-09-13 19:56:45 -070024import static android.net.NetworkPolicy.WARNING_DISABLED;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070025import static android.net.NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070026import static android.net.NetworkPolicyManager.POLICY_NONE;
27import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND;
Jeff Sharkey8a503642011-06-10 13:31:21 -070028import static android.net.NetworkPolicyManager.computeLastCycleBoundary;
29import static android.net.NetworkPolicyManager.computeNextCycleBoundary;
Jeff Sharkeya662e492011-06-18 21:57:06 -070030import static android.net.NetworkTemplate.MATCH_MOBILE_3G_LOWER;
31import static android.net.NetworkTemplate.MATCH_MOBILE_4G;
32import static android.net.NetworkTemplate.MATCH_MOBILE_ALL;
33import static android.net.NetworkTemplate.MATCH_WIFI;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070034import static android.net.NetworkTemplate.buildTemplateEthernet;
35import static android.net.NetworkTemplate.buildTemplateMobile3gLower;
36import static android.net.NetworkTemplate.buildTemplateMobile4g;
37import static android.net.NetworkTemplate.buildTemplateMobileAll;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070038import static android.net.NetworkTemplate.buildTemplateWifiWildcard;
Jeff Sharkey77dae912012-02-03 14:50:33 -080039import static android.net.TrafficStats.GB_IN_BYTES;
40import static android.net.TrafficStats.MB_IN_BYTES;
Jeff Sharkeya83a24f2011-09-16 01:52:39 -070041import static android.net.TrafficStats.UID_REMOVED;
42import static android.net.TrafficStats.UID_TETHERING;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070043import static android.telephony.TelephonyManager.SIM_STATE_READY;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070044import static android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
45import static android.text.format.DateUtils.FORMAT_SHOW_DATE;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070046import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -070047import static com.android.internal.util.Preconditions.checkNotNull;
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -070048import static com.android.settings.Utils.prepareCustomPreferencesList;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070049
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070050import android.animation.LayoutTransition;
Jeff Sharkey38305fb2012-09-14 16:26:51 -070051import android.app.ActivityManager;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070052import android.app.AlertDialog;
53import android.app.Dialog;
54import android.app.DialogFragment;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070055import android.app.Fragment;
Amith Yamasani5ba0a022011-11-07 12:29:00 -080056import android.app.FragmentManager;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070057import android.app.FragmentTransaction;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070058import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070059import android.content.ContentResolver;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070060import android.content.Context;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070061import android.content.DialogInterface;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -070062import android.content.Intent;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070063import android.content.Loader;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070064import android.content.SharedPreferences;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070065import android.content.pm.PackageManager;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070066import android.content.res.Resources;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070067import android.graphics.Color;
Jeff Sharkey5d706792011-09-08 18:57:17 -070068import android.graphics.drawable.ColorDrawable;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070069import android.graphics.drawable.Drawable;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070070import android.net.ConnectivityManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070071import android.net.INetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070072import android.net.INetworkStatsService;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070073import android.net.INetworkStatsSession;
Jeff Sharkey8a503642011-06-10 13:31:21 -070074import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070075import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070076import android.net.NetworkStats;
77import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070078import android.net.NetworkTemplate;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070079import android.net.TrafficStats;
Amith Yamasanib0b37ae2012-04-23 15:35:36 -070080import android.net.Uri;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070081import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070082import android.os.Bundle;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070083import android.os.INetworkManagementService;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070084import android.os.Parcel;
85import android.os.Parcelable;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070086import android.os.RemoteException;
87import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070088import android.os.SystemProperties;
Dianne Hackbornbb06a422012-08-16 11:01:57 -070089import android.os.UserHandle;
Jeff Sharkey8a503642011-06-10 13:31:21 -070090import android.preference.Preference;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070091import android.preference.PreferenceActivity;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070092import android.provider.Settings;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070093import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070094import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070095import android.text.format.DateUtils;
96import android.text.format.Formatter;
Jeff Sharkeye5223a02012-03-09 17:11:14 -080097import android.text.format.Time;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070098import android.util.Log;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070099import android.util.SparseArray;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700100import android.util.SparseBooleanArray;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700101import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700102import android.view.Menu;
103import android.view.MenuInflater;
104import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700105import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700106import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700107import android.view.ViewGroup;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700108import android.widget.AdapterView;
109import android.widget.AdapterView.OnItemClickListener;
110import android.widget.AdapterView.OnItemSelectedListener;
111import android.widget.ArrayAdapter;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700112import android.widget.BaseAdapter;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700113import android.widget.Button;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700114import android.widget.CheckBox;
115import android.widget.CompoundButton;
116import android.widget.CompoundButton.OnCheckedChangeListener;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700117import android.widget.ImageView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700118import android.widget.LinearLayout;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700119import android.widget.ListView;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700120import android.widget.NumberPicker;
Jeff Sharkey2412b0f2011-07-17 20:31:40 -0700121import android.widget.ProgressBar;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700122import android.widget.Spinner;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700123import android.widget.Switch;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700124import android.widget.TabHost;
125import android.widget.TabHost.OnTabChangeListener;
126import android.widget.TabHost.TabContentFactory;
127import android.widget.TabHost.TabSpec;
128import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700129import android.widget.TextView;
130
Wink Saville55434042012-06-14 12:33:43 -0700131import com.android.internal.telephony.PhoneConstants;
Jeff Sharkey5d706792011-09-08 18:57:17 -0700132import com.android.settings.drawable.InsetBoundsDrawable;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700133import com.android.settings.net.ChartData;
134import com.android.settings.net.ChartDataLoader;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700135import com.android.settings.net.DataUsageMeteredSettings;
Jeff Sharkeya662e492011-06-18 21:57:06 -0700136import com.android.settings.net.NetworkPolicyEditor;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700137import com.android.settings.net.SummaryForAllUidLoader;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700138import com.android.settings.net.UidDetail;
139import com.android.settings.net.UidDetailProvider;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700140import com.android.settings.widget.ChartDataUsageView;
141import com.android.settings.widget.ChartDataUsageView.DataUsageChartListener;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700142import com.android.settings.widget.PieChartView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700143import com.google.android.collect.Lists;
144
145import java.util.ArrayList;
146import java.util.Collections;
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700147import java.util.List;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700148import java.util.Locale;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700149
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700150import libcore.util.Objects;
151
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700152/**
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700153 * Panel showing data usage history across various networks, including options
154 * to inspect based on usage cycle and control through {@link NetworkPolicy}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700155 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700156public class DataUsageSummary extends Fragment {
157 private static final String TAG = "DataUsage";
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800158 private static final boolean LOGD = false;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700159
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700160 // TODO: remove this testing code
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700161 private static final boolean TEST_ANIM = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700162 private static final boolean TEST_RADIOS = false;
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800163
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700164 private static final String TEST_RADIOS_PROP = "test.radios";
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800165 private static final String TEST_SUBSCRIBER_PROP = "test.subscriberid";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700166
Jeff Sharkey8a503642011-06-10 13:31:21 -0700167 private static final String TAB_3G = "3g";
168 private static final String TAB_4G = "4g";
169 private static final String TAB_MOBILE = "mobile";
170 private static final String TAB_WIFI = "wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700171 private static final String TAB_ETHERNET = "ethernet";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700172
Jeff Sharkey28130d92011-09-02 16:10:24 -0700173 private static final String TAG_CONFIRM_DATA_DISABLE = "confirmDataDisable";
174 private static final String TAG_CONFIRM_DATA_ROAMING = "confirmDataRoaming";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700175 private static final String TAG_CONFIRM_LIMIT = "confirmLimit";
176 private static final String TAG_CYCLE_EDITOR = "cycleEditor";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700177 private static final String TAG_WARNING_EDITOR = "warningEditor";
178 private static final String TAG_LIMIT_EDITOR = "limitEditor";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700179 private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700180 private static final String TAG_DENIED_RESTRICT = "deniedRestrict";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700181 private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict";
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700182 private static final String TAG_CONFIRM_AUTO_SYNC_CHANGE = "confirmAutoSyncChange";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700183 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700184
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700185 private static final int LOADER_CHART_DATA = 2;
186 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700187
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700188 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700189 private INetworkStatsService mStatsService;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700190 private NetworkPolicyManager mPolicyManager;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700191 private ConnectivityManager mConnService;
192
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700193 private INetworkStatsSession mStatsSession;
194
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700195 private static final String PREF_FILE = "data_usage";
196 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700197 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700198
199 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700200
Jeff Sharkey8a503642011-06-10 13:31:21 -0700201 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700202 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700203 private TabWidget mTabWidget;
204 private ListView mListView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700205 private DataUsageAdapter mAdapter;
206
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700207 /** Distance to inset content from sides, when needed. */
208 private int mInsetSide = 0;
209
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700210 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700211
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700212 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700213 private LinearLayout mNetworkSwitches;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700214 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700215 private View mDataEnabledView;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700216 private CheckBox mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700217 private View mDisableAtLimitView;
218
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700219 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700220 private Spinner mCycleSpinner;
221 private CycleAdapter mCycleAdapter;
222
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700223 private ChartDataUsageView mChart;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700224 private TextView mUsageSummary;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700225 private TextView mEmpty;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700226
227 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700228 private ImageView mAppIcon;
229 private ViewGroup mAppTitles;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700230 private PieChartView mAppPieChart;
231 private TextView mAppForeground;
232 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700233 private Button mAppSettings;
234
235 private LinearLayout mAppSwitches;
236 private CheckBox mAppRestrict;
237 private View mAppRestrictView;
238
Jeff Sharkey8a503642011-06-10 13:31:21 -0700239 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700240 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700241
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700242 private NetworkTemplate mTemplate;
243 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700244
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700245 private AppItem mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700246
247 private Intent mAppSettingsIntent;
248
Jeff Sharkeya662e492011-06-18 21:57:06 -0700249 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700250
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700251 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700252 private String mIntentTab = null;
253
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700254 private MenuItem mMenuDataRoaming;
255 private MenuItem mMenuRestrictBackground;
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700256 private MenuItem mMenuAutoSync;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700257
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700258 /** Flag used to ignore listeners during binding. */
259 private boolean mBinding;
260
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700261 private UidDetailProvider mUidDetailProvider;
262
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700263 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700264 public void onCreate(Bundle savedInstanceState) {
265 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700266 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700267
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700268 mNetworkService = INetworkManagementService.Stub.asInterface(
269 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700270 mStatsService = INetworkStatsService.Stub.asInterface(
271 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700272 mPolicyManager = NetworkPolicyManager.from(context);
273 mConnService = ConnectivityManager.from(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700274
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700275 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700276
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700277 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700278 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700279
Jaewan Kimffce9c12013-03-19 16:53:45 +0900280 try {
281 mStatsSession = mStatsService.openSession();
282 } catch (RemoteException e) {
283 throw new RuntimeException(e);
284 }
285
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700286 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700287 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700288
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700289 // override preferences when no mobile radio
290 if (!hasReadyMobileRadio(context)) {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900291 mShowWifi = true;
292 mShowEthernet = true;
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700293 }
294
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700295 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700296 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700297
Jeff Sharkey8a503642011-06-10 13:31:21 -0700298 @Override
299 public View onCreateView(LayoutInflater inflater, ViewGroup container,
300 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700301
Jeff Sharkey8a503642011-06-10 13:31:21 -0700302 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700303 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
304
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700305 mUidDetailProvider = new UidDetailProvider(context);
306
Jeff Sharkey8a503642011-06-10 13:31:21 -0700307 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700308 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700309 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
310 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700311
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700312 // decide if we need to manually inset our content, or if we should rely
313 // on parent container for inset.
314 final boolean shouldInset = mListView.getScrollBarStyle()
315 == View.SCROLLBARS_OUTSIDE_OVERLAY;
316 if (shouldInset) {
317 mInsetSide = view.getResources().getDimensionPixelOffset(
318 com.android.internal.R.dimen.preference_fragment_padding_side);
319 } else {
320 mInsetSide = 0;
321 }
322
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700323 // adjust padding around tabwidget as needed
Jeff Sharkey5d706792011-09-08 18:57:17 -0700324 prepareCustomPreferencesList(container, view, mListView, true);
325
Jeff Sharkey8a503642011-06-10 13:31:21 -0700326 mTabHost.setup();
327 mTabHost.setOnTabChangedListener(mTabListener);
328
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700329 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700330 mHeader.setClickable(true);
331
332 mListView.addHeaderView(mHeader, null, true);
333 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700334
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700335 if (mInsetSide > 0) {
336 // inset selector and divider drawables
337 insetListViewDrawables(mListView, mInsetSide);
Fabrice Di Megliob27223f2013-01-15 18:54:11 -0800338 mHeader.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700339 }
340
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700341 {
342 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700343 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
344 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700345 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700346
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700347 mDataEnabled = new Switch(inflater.getContext());
348 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
349 mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener);
350 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700351
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700352 mDisableAtLimit = new CheckBox(inflater.getContext());
353 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700354 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700355 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700356 mDisableAtLimitView.setClickable(true);
357 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700358 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
359 mNetworkSwitches.addView(mDisableAtLimitView);
360 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700361
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700362 // bind cycle dropdown
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700363 mCycleView = mHeader.findViewById(R.id.cycles);
364 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700365 mCycleAdapter = new CycleAdapter(context);
366 mCycleSpinner.setAdapter(mCycleAdapter);
367 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
368
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700369 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700370 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800371 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700372
373 {
374 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700375 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700376 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
377 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700378 mAppPieChart = (PieChartView) mAppDetail.findViewById(R.id.app_pie_chart);
379 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
380 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700381 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700382
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700383 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700384 mAppSettings.setOnClickListener(mAppSettingsListener);
385
386 mAppRestrict = new CheckBox(inflater.getContext());
387 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700388 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700389 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700390 mAppRestrictView.setClickable(true);
391 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700392 mAppRestrictView.setOnClickListener(mAppRestrictListener);
393 mAppSwitches.addView(mAppRestrictView);
394 }
395
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700396 mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700397 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700398
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700399 mAdapter = new DataUsageAdapter(mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700400 mListView.setOnItemClickListener(mListListener);
401 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700402
403 return view;
404 }
405
406 @Override
407 public void onResume() {
408 super.onResume();
409
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700410 // pick default tab based on incoming intent
411 final Intent intent = getActivity().getIntent();
412 mIntentTab = computeTabFromIntent(intent);
413
Jeff Sharkey8a503642011-06-10 13:31:21 -0700414 // this kicks off chain reaction which creates tabs, binds the body to
415 // selected network, and binds chart, cycles and detail list.
416 updateTabs();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700417
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700418 // kick off background task to update stats
419 new AsyncTask<Void, Void, Void>() {
420 @Override
421 protected Void doInBackground(Void... params) {
422 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700423 // wait a few seconds before kicking off
424 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700425 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700426 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700427 } catch (RemoteException e) {
428 }
429 return null;
430 }
431
432 @Override
433 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700434 if (isAdded()) {
435 updateBody();
436 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700437 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700438 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700439 }
440
Jeff Sharkey8a503642011-06-10 13:31:21 -0700441 @Override
442 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
443 inflater.inflate(R.menu.data_usage, menu);
444 }
445
446 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700447 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700448 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700449 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700450 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700451
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700452 mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700453 mMenuDataRoaming.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700454 mMenuDataRoaming.setChecked(getDataRoaming());
455
456 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700457 mMenuRestrictBackground.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
458 mMenuRestrictBackground.setChecked(mPolicyManager.getRestrictBackground());
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700459 mMenuRestrictBackground.setVisible(isOwner);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700460
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700461 mMenuAutoSync = menu.findItem(R.id.data_usage_menu_auto_sync);
462 mMenuAutoSync.setChecked(ContentResolver.getMasterSyncAutomatically());
Jeff Sharkeyfda48e32012-09-18 15:03:47 -0700463 mMenuAutoSync.setVisible(!appDetailMode);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700464
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700465 final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g);
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700466 split4g.setVisible(hasReadyMobile4gRadio(context) && isOwner && !appDetailMode);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700467 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700468
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700469 final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700470 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700471 showWifi.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700472 showWifi.setChecked(mShowWifi);
473 } else {
474 showWifi.setVisible(false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700475 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700476
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700477 final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700478 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700479 showEthernet.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700480 showEthernet.setChecked(mShowEthernet);
481 } else {
482 showEthernet.setVisible(false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700483 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700484
485 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
486 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700487 metered.setVisible(!appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700488 } else {
489 metered.setVisible(false);
490 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700491
492 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
493 String helpUrl;
494 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800495 HelpUtils.prepareHelpMenuItem(context, help, helpUrl);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700496 } 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: {
Guang Zhu167ba2a2012-10-19 17:55:05 -0700552 if (ActivityManager.isUserAMonkey()) {
553 Log.d("SyncState", "ignoring monkey's attempt to flip global sync state");
554 } else {
555 ConfirmAutoSyncChangeFragment.show(this, !item.isChecked());
556 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700557 return true;
558 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700559 }
560 return false;
561 }
562
Jeff Sharkey94a90952011-06-13 22:31:09 -0700563 @Override
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700564 public void onDestroy() {
Jeff Sharkey94a90952011-06-13 22:31:09 -0700565 mDataEnabledView = null;
566 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700567
568 mUidDetailProvider.clearCache();
569 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700570
571 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700572
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800573 if (this.isRemoving()) {
574 getFragmentManager()
575 .popBackStack(TAG_APP_DETAILS, FragmentManager.POP_BACK_STACK_INCLUSIVE);
576 }
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700577
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800578 super.onDestroy();
579 }
580
Jeff Sharkey8a503642011-06-10 13:31:21 -0700581 /**
Jeff Sharkey92811822012-05-04 11:47:29 -0700582 * Build and assign {@link LayoutTransition} to various containers. Should
583 * only be assigned after initial layout is complete.
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700584 */
Jeff Sharkey92811822012-05-04 11:47:29 -0700585 private void ensureLayoutTransitions() {
586 // skip when already setup
587 if (mChart.getLayoutTransition() != null) return;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700588
Jeff Sharkey92811822012-05-04 11:47:29 -0700589 mTabsContainer.setLayoutTransition(buildLayoutTransition());
590 mHeader.setLayoutTransition(buildLayoutTransition());
591 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700592
Jeff Sharkey92811822012-05-04 11:47:29 -0700593 final LayoutTransition chartTransition = buildLayoutTransition();
594 chartTransition.disableTransitionType(LayoutTransition.APPEARING);
595 chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
596 mChart.setLayoutTransition(chartTransition);
597 }
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700598
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700599 private static LayoutTransition buildLayoutTransition() {
600 final LayoutTransition transition = new LayoutTransition();
601 if (TEST_ANIM) {
602 transition.setDuration(1500);
603 }
604 transition.setAnimateParentHierarchy(false);
605 return transition;
606 }
607
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700608 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700609 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700610 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
611 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700612 */
613 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700614 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700615 mTabHost.clearAllTabs();
616
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700617 final boolean mobileSplit = isMobilePolicySplit();
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700618 if (mobileSplit && hasReadyMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700619 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
620 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700621 } else if (hasReadyMobileRadio(context)) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700622 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700623 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700624 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700625 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
626 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700627 if (mShowEthernet && hasEthernet(context)) {
628 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
629 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700630
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700631 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700632 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
633 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
634 if (mIntentTab != null) {
635 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700636 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700637 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700638 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700639 mTabHost.setCurrentTabByTag(mIntentTab);
640 }
641 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700642 } else if (noTabs) {
643 // no usable tabs, so hide body
644 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700645 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700646 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700647 }
648 }
649
Jeff Sharkey8a503642011-06-10 13:31:21 -0700650 /**
651 * Factory that provide empty {@link View} to make {@link TabHost} happy.
652 */
653 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700654 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700655 public View createTabContent(String tag) {
656 return new View(mTabHost.getContext());
657 }
658 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700659
Jeff Sharkey8a503642011-06-10 13:31:21 -0700660 /**
661 * Build {@link TabSpec} with thin indicator, and empty content.
662 */
663 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700664 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
665 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700666 }
667
668 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700669 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700670 public void onTabChanged(String tabId) {
671 // user changed tab; update body
672 updateBody();
673 }
674 };
675
676 /**
677 * Update body content based on current tab. Loads
678 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
679 * binds them to visible controls.
680 */
681 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700682 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700683 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700684
Jeff Sharkeya662e492011-06-18 21:57:06 -0700685 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700686 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700687 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700688
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700689 if (currentTab == null) {
690 Log.w(TAG, "no tab selected; hiding body");
691 mListView.setVisibility(View.GONE);
692 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700693 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700694 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700695 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700696
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700697 final boolean tabChanged = !currentTab.equals(mCurrentTab);
698 mCurrentTab = currentTab;
699
Jeff Sharkey8a503642011-06-10 13:31:21 -0700700 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
701
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700702 mDataEnabledView.setVisibility(isOwner ? View.VISIBLE : View.GONE);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700703
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700704 // TODO: remove mobile tabs when SIM isn't ready
705 final TelephonyManager tele = TelephonyManager.from(context);
706
Jeff Sharkey8a503642011-06-10 13:31:21 -0700707 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700708 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
709 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700710 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700711
712 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700713 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
714 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
715 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700716 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700717
718 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700719 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
720 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
721 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700722 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700723
724 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700725 // wifi doesn't have any controls
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700726 mDataEnabledView.setVisibility(View.GONE);
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700727 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700728 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700729
730 } else if (TAB_ETHERNET.equals(currentTab)) {
731 // ethernet doesn't have any controls
732 mDataEnabledView.setVisibility(View.GONE);
733 mDisableAtLimitView.setVisibility(View.GONE);
734 mTemplate = buildTemplateEthernet();
735
736 } else {
737 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700738 }
739
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700740 // kick off loader for network history
741 // TODO: consider chaining two loaders together instead of reloading
742 // network history when showing app detail.
743 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700744 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700745
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700746 // detail mode can change visible menus, invalidate
747 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700748
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700749 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700750 }
751
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700752 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700753 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700754 }
755
756 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700757 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700758 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700759 */
760 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700761 final Context context = getActivity();
762 final PackageManager pm = context.getPackageManager();
763 final LayoutInflater inflater = getActivity().getLayoutInflater();
764
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700765 if (isAppDetailMode()) {
766 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700767 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700768 } else {
769 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700770 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700771
772 // hide detail stats when not in detail mode
773 mChart.bindDetailNetworkStats(null);
774 return;
775 }
776
777 // remove warning/limit sweeps while in detail mode
778 mChart.bindNetworkPolicy(null);
779
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700780 // show icon and all labels appearing under this app
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700781 final int uid = mCurrentApp.key;
782 final UidDetail detail = mUidDetailProvider.getUidDetail(uid, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700783 mAppIcon.setImageDrawable(detail.icon);
784
785 mAppTitles.removeAllViews();
786 if (detail.detailLabels != null) {
787 for (CharSequence label : detail.detailLabels) {
788 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label));
789 }
790 } else {
791 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label));
792 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700793
794 // enable settings button when package provides it
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700795 final String[] packageNames = pm.getPackagesForUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700796 if (packageNames != null && packageNames.length > 0) {
797 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700798 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
799
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700800 // Search for match across all packages
801 boolean matchFound = false;
802 for (String packageName : packageNames) {
803 mAppSettingsIntent.setPackage(packageName);
804 if (pm.resolveActivity(mAppSettingsIntent, 0) != null) {
805 matchFound = true;
806 break;
807 }
808 }
809
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700810 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -0700811 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700812
813 } else {
814 mAppSettingsIntent = null;
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700815 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700816 }
817
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700818 updateDetailData();
819
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700820 if (UserHandle.isApp(uid) && !mPolicyManager.getRestrictBackground()
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700821 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700822 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800823 setPreferenceSummary(mAppRestrictView,
824 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700825
826 mAppRestrictView.setVisibility(View.VISIBLE);
827 mAppRestrict.setChecked(getAppRestrictBackground());
828
829 } else {
830 mAppRestrictView.setVisibility(View.GONE);
831 }
832 }
833
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700834 private void setPolicyWarningBytes(long warningBytes) {
835 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700836 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700837 updatePolicy(false);
838 }
839
840 private void setPolicyLimitBytes(long limitBytes) {
841 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700842 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700843 updatePolicy(false);
844 }
845
Jeff Sharkey28130d92011-09-02 16:10:24 -0700846 /**
847 * Local cache of value, used to work around delay when
848 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
849 */
850 private Boolean mMobileDataEnabled;
851
852 private boolean isMobileDataEnabled() {
853 if (mMobileDataEnabled != null) {
854 // TODO: deprecate and remove this once enabled flag is on policy
855 return mMobileDataEnabled;
856 } else {
857 return mConnService.getMobileDataEnabled();
858 }
859 }
860
861 private void setMobileDataEnabled(boolean enabled) {
862 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
863 mConnService.setMobileDataEnabled(enabled);
864 mMobileDataEnabled = enabled;
865 updatePolicy(false);
866 }
867
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700868 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700869 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked()
870 && ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700871 }
872
873 private boolean isBandwidthControlEnabled() {
874 try {
875 return mNetworkService.isBandwidthControlEnabled();
876 } catch (RemoteException e) {
877 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
878 return false;
879 }
880 }
881
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700882 private boolean getDataRoaming() {
883 final ContentResolver resolver = getActivity().getContentResolver();
Christopher Tate5a64c732012-09-07 13:35:31 -0700884 return Settings.Global.getInt(resolver, Settings.Global.DATA_ROAMING, 0) != 0;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700885 }
886
887 private void setDataRoaming(boolean enabled) {
888 // TODO: teach telephony DataConnectionTracker to watch and apply
889 // updates when changed.
890 final ContentResolver resolver = getActivity().getContentResolver();
Christopher Tate5a64c732012-09-07 13:35:31 -0700891 Settings.Global.putInt(resolver, Settings.Global.DATA_ROAMING, enabled ? 1 : 0);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700892 mMenuDataRoaming.setChecked(enabled);
893 }
894
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700895 public void setRestrictBackground(boolean restrictBackground) {
896 mPolicyManager.setRestrictBackground(restrictBackground);
897 mMenuRestrictBackground.setChecked(restrictBackground);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700898 }
899
900 private boolean getAppRestrictBackground() {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700901 final int uid = mCurrentApp.key;
902 final int uidPolicy = mPolicyManager.getUidPolicy(uid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700903 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
904 }
905
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700906 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700907 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700908 final int uid = mCurrentApp.key;
909 mPolicyManager.setUidPolicy(
910 uid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700911 mAppRestrict.setChecked(restrictBackground);
912 }
913
Jeff Sharkey8a503642011-06-10 13:31:21 -0700914 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700915 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
916 * current {@link #mTemplate}.
917 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700918 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700919 if (isAppDetailMode()) {
920 mNetworkSwitches.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700921 } else {
922 mNetworkSwitches.setVisibility(View.VISIBLE);
923 }
924
Jeff Sharkey28130d92011-09-02 16:10:24 -0700925 // TODO: move enabled state directly into policy
926 if (TAB_MOBILE.equals(mCurrentTab)) {
927 mBinding = true;
928 mDataEnabled.setChecked(isMobileDataEnabled());
929 mBinding = false;
930 }
931
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700932 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
933 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700934 mDisableAtLimitView.setVisibility(View.VISIBLE);
935 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700936 if (!isAppDetailMode()) {
937 mChart.bindNetworkPolicy(policy);
938 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700939
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700940 } else {
941 // controls are disabled; don't bind warning/limit sweeps
942 mDisableAtLimitView.setVisibility(View.GONE);
943 mChart.bindNetworkPolicy(null);
944 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700945
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700946 if (refreshCycle) {
947 // generate cycle list based on policy and available history
948 updateCycleList(policy);
949 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700950 }
951
952 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700953 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
954 * and available {@link NetworkStatsHistory} data. Always selects the newest
955 * item, updating the inspection range on {@link #mChart}.
956 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700957 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700958 // stash away currently selected cycle to try restoring below
959 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700960 mCycleAdapter.clear();
961
962 final Context context = mCycleSpinner.getContext();
963
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700964 long historyStart = Long.MAX_VALUE;
965 long historyEnd = Long.MIN_VALUE;
966 if (mChartData != null) {
967 historyStart = mChartData.network.getStart();
968 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700969 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700970
Jeff Sharkey461842a2011-09-25 18:22:48 -0700971 final long now = System.currentTimeMillis();
972 if (historyStart == Long.MAX_VALUE) historyStart = now;
973 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700974
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700975 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700976 if (policy != null) {
977 // find the next cycle boundary
978 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700979
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700980 // walk backwards, generating all valid cycle ranges
981 while (cycleEnd > historyStart) {
982 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
983 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
984 + historyStart);
985 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
986 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700987 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700988 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700989
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700990 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700991 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700992 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700993
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700994 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700995 // no policy defined cycles; show entry for each four-week period
996 long cycleEnd = historyEnd;
997 while (cycleEnd > historyStart) {
998 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
999 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1000 cycleEnd = cycleStart;
1001 }
1002
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001003 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001004 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001005
1006 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001007 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001008 final int position = mCycleAdapter.findNearestPosition(previousItem);
1009 mCycleSpinner.setSelection(position);
1010
1011 // only force-update cycle when changed; skipping preserves any
1012 // user-defined inspection region.
1013 final CycleItem selectedItem = mCycleAdapter.getItem(position);
1014 if (!Objects.equal(selectedItem, previousItem)) {
1015 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1016 } else {
1017 // but still kick off loader for detailed list
1018 updateDetailData();
1019 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001020 } else {
1021 updateDetailData();
1022 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001023 }
1024
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001025 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001026 @Override
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001027 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
1028 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001029
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001030 final boolean dataEnabled = isChecked;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001031 final String currentTab = mCurrentTab;
1032 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001033 if (dataEnabled) {
1034 setMobileDataEnabled(true);
1035 } else {
1036 // disabling data; show confirmation dialog which eventually
1037 // calls setMobileDataEnabled() once user confirms.
1038 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1039 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001040 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001041
Jeff Sharkey28130d92011-09-02 16:10:24 -07001042 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001043 }
1044 };
1045
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001046 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001047 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001048 public void onClick(View v) {
1049 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001050 if (disableAtLimit) {
1051 // enabling limit; show confirmation dialog which eventually
1052 // calls setPolicyLimitBytes() once user confirms.
1053 ConfirmLimitFragment.show(DataUsageSummary.this);
1054 } else {
1055 setPolicyLimitBytes(LIMIT_DISABLED);
1056 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001057 }
1058 };
1059
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001060 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001061 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001062 public void onClick(View v) {
1063 final boolean restrictBackground = !mAppRestrict.isChecked();
1064
1065 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001066 // enabling restriction; show confirmation dialog which
1067 // eventually calls setRestrictBackground() once user
1068 // confirms.
1069 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001070 } else {
1071 setAppRestrictBackground(false);
1072 }
1073 }
1074 };
1075
1076 private OnClickListener mAppSettingsListener = new OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001077 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001078 public void onClick(View v) {
Jeff Sharkeyd92e0412012-04-24 11:35:43 -07001079 if (!isAdded()) return;
1080
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001081 // TODO: target torwards entire UID instead of just first package
1082 startActivity(mAppSettingsIntent);
1083 }
1084 };
1085
Jeff Sharkey8a503642011-06-10 13:31:21 -07001086 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001087 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001088 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001089 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001090 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001091
1092 // TODO: sigh, remove this hack once we understand 6450986
1093 if (mUidDetailProvider == null || app == null) return;
1094
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001095 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001096 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001097 }
1098 };
1099
1100 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001101 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001102 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1103 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1104 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001105 // show cycle editor; will eventually call setPolicyCycleDay()
1106 // when user finishes editing.
1107 CycleEditorFragment.show(DataUsageSummary.this);
1108
1109 // reset spinner to something other than "change cycle..."
1110 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001111
1112 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001113 if (LOGD) {
1114 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1115 + cycle.end + "]");
1116 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001117
1118 // update chart to show selected cycle, and update detail data
1119 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001120 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001121
1122 updateDetailData();
1123 }
1124 }
1125
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001126 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001127 public void onNothingSelected(AdapterView<?> parent) {
1128 // ignored
1129 }
1130 };
1131
1132 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001133 * Update details based on {@link #mChart} inspection range depending on
1134 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1135 * of applications data usage, and when {@link #isAppDetailMode()} update
1136 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001137 */
1138 private void updateDetailData() {
1139 if (LOGD) Log.d(TAG, "updateDetailData()");
1140
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001141 final long start = mChart.getInspectStart();
1142 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001143 final long now = System.currentTimeMillis();
1144
1145 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001146
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001147 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001148 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001149 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001150 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001151 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001152 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001153 final long foregroundBytes = entry.rxBytes + entry.txBytes;
1154
1155 mAppPieChart.setOriginAngle(175);
1156
1157 mAppPieChart.removeAllSlices();
1158 mAppPieChart.addSlice(foregroundBytes, Color.parseColor("#d88d3a"));
1159 mAppPieChart.addSlice(defaultBytes, Color.parseColor("#666666"));
1160
1161 mAppPieChart.generatePath();
1162
1163 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1164 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1165
1166 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001167 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001168
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001169 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001170
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001171 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001172 if (mChartData != null) {
1173 entry = mChartData.network.getValues(start, end, now, null);
1174 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001175
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001176 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001177 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001178 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001179 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001180
1181 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1182 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001183 final String rangePhrase = formatDateRange(context, start, end);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001184
Jeff Sharkeye557c332012-04-13 16:04:07 -07001185 final int summaryRes;
1186 if (TAB_MOBILE.equals(mCurrentTab) || TAB_3G.equals(mCurrentApp)
1187 || TAB_4G.equals(mCurrentApp)) {
1188 summaryRes = R.string.data_usage_total_during_range_mobile;
1189 } else {
1190 summaryRes = R.string.data_usage_total_during_range;
1191 }
1192
1193 mUsageSummary.setText(getString(summaryRes, totalPhrase, rangePhrase));
Jeff Sharkey92811822012-05-04 11:47:29 -07001194
1195 // initial layout is finished above, ensure we have transitions
1196 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001197 }
1198
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001199 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1200 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001201 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001202 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001203 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001204 }
1205
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001206 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001207 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1208 mChartData = data;
1209 mChart.bindNetworkStats(mChartData.network);
1210 mChart.bindDetailNetworkStats(mChartData.detail);
1211
1212 // calcuate policy cycles based on available data
1213 updatePolicy(true);
1214 updateAppDetail();
1215
1216 // force scroll to top of body when showing detail
1217 if (mChartData.detail != null) {
1218 mListView.smoothScrollToPosition(0);
1219 }
1220 }
1221
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001222 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001223 public void onLoaderReset(Loader<ChartData> loader) {
1224 mChartData = null;
1225 mChart.bindNetworkStats(null);
1226 mChart.bindDetailNetworkStats(null);
1227 }
1228 };
1229
1230 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001231 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001232 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001233 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001234 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001235 }
1236
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001237 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001238 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001239 final int[] restrictedUids = mPolicyManager.getUidsWithPolicy(
Jeff Sharkeye557c332012-04-13 16:04:07 -07001240 POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001241 mAdapter.bindStats(data, restrictedUids);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001242 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001243 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001244
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001245 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001246 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001247 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001248 updateEmptyVisible();
1249 }
1250
1251 private void updateEmptyVisible() {
1252 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1253 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001254 }
1255 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001256
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001257 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001258 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001259 final Context context = getActivity();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001260 if (hasReadyMobileRadio(context)) {
1261 final TelephonyManager tele = TelephonyManager.from(context);
1262 return mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001263 } else {
1264 return false;
1265 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001266 }
1267
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001268 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001269 private void setMobilePolicySplit(boolean split) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001270 final Context context = getActivity();
1271 if (hasReadyMobileRadio(context)) {
1272 final TelephonyManager tele = TelephonyManager.from(context);
1273 mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split);
1274 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001275 }
1276
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001277 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001278 final TelephonyManager tele = TelephonyManager.from(context);
1279 final String actualSubscriberId = tele.getSubscriberId();
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -08001280 return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001281 }
1282
Jeff Sharkey8a503642011-06-10 13:31:21 -07001283 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001284 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001285 public void onInspectRangeChanged() {
1286 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
1287 updateDetailData();
1288 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001289
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001290 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001291 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001292 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001293 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001294
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001295 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001296 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001297 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001298 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001299
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001300 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001301 public void requestWarningEdit() {
1302 WarningEditorFragment.show(DataUsageSummary.this);
1303 }
1304
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001305 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001306 public void requestLimitEdit() {
1307 LimitEditorFragment.show(DataUsageSummary.this);
1308 }
1309 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001310
1311 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001312 * List item that reflects a specific data usage cycle.
1313 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001314 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001315 public CharSequence label;
1316 public long start;
1317 public long end;
1318
Jeff Sharkey8a503642011-06-10 13:31:21 -07001319 CycleItem(CharSequence label) {
1320 this.label = label;
1321 }
1322
1323 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001324 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001325 this.start = start;
1326 this.end = end;
1327 }
1328
Jeff Sharkey8a503642011-06-10 13:31:21 -07001329 @Override
1330 public String toString() {
1331 return label.toString();
1332 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001333
1334 @Override
1335 public boolean equals(Object o) {
1336 if (o instanceof CycleItem) {
1337 final CycleItem another = (CycleItem) o;
1338 return start == another.start && end == another.end;
1339 }
1340 return false;
1341 }
1342
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001343 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001344 public int compareTo(CycleItem another) {
1345 return Long.compare(start, another.start);
1346 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001347 }
1348
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001349 private static final StringBuilder sBuilder = new StringBuilder(50);
1350 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1351 sBuilder, Locale.getDefault());
1352
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001353 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001354 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001355
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001356 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001357 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001358 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1359 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001360 }
1361 }
1362
Jeff Sharkey8a503642011-06-10 13:31:21 -07001363 /**
1364 * Special-case data usage cycle that triggers dialog to change
1365 * {@link NetworkPolicy#cycleDay}.
1366 */
1367 public static class CycleChangeItem extends CycleItem {
1368 public CycleChangeItem(Context context) {
1369 super(context.getString(R.string.data_usage_change_cycle));
1370 }
1371 }
1372
1373 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001374 private boolean mChangePossible = false;
1375 private boolean mChangeVisible = false;
1376
1377 private final CycleChangeItem mChangeItem;
1378
Jeff Sharkey8a503642011-06-10 13:31:21 -07001379 public CycleAdapter(Context context) {
1380 super(context, android.R.layout.simple_spinner_item);
1381 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001382 mChangeItem = new CycleChangeItem(context);
1383 }
1384
1385 public void setChangePossible(boolean possible) {
1386 mChangePossible = possible;
1387 updateChange();
1388 }
1389
1390 public void setChangeVisible(boolean visible) {
1391 mChangeVisible = visible;
1392 updateChange();
1393 }
1394
1395 private void updateChange() {
1396 remove(mChangeItem);
1397 if (mChangePossible && mChangeVisible) {
1398 add(mChangeItem);
1399 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001400 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001401
1402 /**
1403 * Find position of {@link CycleItem} in this adapter which is nearest
1404 * the given {@link CycleItem}.
1405 */
1406 public int findNearestPosition(CycleItem target) {
1407 if (target != null) {
1408 final int count = getCount();
1409 for (int i = count - 1; i >= 0; i--) {
1410 final CycleItem item = getItem(i);
1411 if (item instanceof CycleChangeItem) {
1412 continue;
1413 } else if (item.compareTo(target) >= 0) {
1414 return i;
1415 }
1416 }
1417 }
1418 return 0;
1419 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001420 }
1421
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001422 public static class AppItem implements Comparable<AppItem>, Parcelable {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001423 public final int key;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001424 public boolean restricted;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001425 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001426 public long total;
1427
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001428 public AppItem(int key) {
1429 this.key = key;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001430 }
1431
1432 public AppItem(Parcel parcel) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001433 key = parcel.readInt();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001434 uids = parcel.readSparseBooleanArray();
1435 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001436 }
1437
1438 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001439 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001440 }
1441
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001442 @Override
1443 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001444 dest.writeInt(key);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001445 dest.writeSparseBooleanArray(uids);
1446 dest.writeLong(total);
1447 }
1448
1449 @Override
1450 public int describeContents() {
1451 return 0;
1452 }
1453
1454 @Override
1455 public int compareTo(AppItem another) {
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001456 return Long.compare(another.total, total);
1457 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001458
1459 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1460 @Override
1461 public AppItem createFromParcel(Parcel in) {
1462 return new AppItem(in);
1463 }
1464
1465 @Override
1466 public AppItem[] newArray(int size) {
1467 return new AppItem[size];
1468 }
1469 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001470 }
1471
Jeff Sharkey8a503642011-06-10 13:31:21 -07001472 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001473 * Adapter of applications, sorted by total usage descending.
1474 */
1475 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001476 private final UidDetailProvider mProvider;
1477 private final int mInsetSide;
1478
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001479 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001480 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001481
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001482 public DataUsageAdapter(UidDetailProvider provider, int insetSide) {
1483 mProvider = checkNotNull(provider);
1484 mInsetSide = insetSide;
1485 }
1486
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001487 /**
1488 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1489 */
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001490 public void bindStats(NetworkStats stats, int[] restrictedUids) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001491 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001492
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001493 final int currentUserId = ActivityManager.getCurrentUser();
1494 final SparseArray<AppItem> knownItems = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001495
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001496 NetworkStats.Entry entry = null;
1497 final int size = stats != null ? stats.size() : 0;
1498 for (int i = 0; i < size; i++) {
1499 entry = stats.getValues(i, entry);
1500
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001501 // Decide how to collapse items together
1502 final int uid = entry.uid;
1503 final int collapseKey;
1504 if (UserHandle.isApp(uid)) {
1505 if (UserHandle.getUserId(uid) == currentUserId) {
1506 collapseKey = uid;
1507 } else {
1508 collapseKey = UidDetailProvider.buildKeyForUser(UserHandle.getUserId(uid));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001509 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001510 } else if (uid == UID_REMOVED || uid == UID_TETHERING) {
1511 collapseKey = uid;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001512 } else {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001513 collapseKey = android.os.Process.SYSTEM_UID;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001514 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001515
1516 AppItem item = knownItems.get(collapseKey);
1517 if (item == null) {
1518 item = new AppItem(collapseKey);
1519 mItems.add(item);
1520 knownItems.put(item.key, item);
1521 }
1522 item.addUid(uid);
1523 item.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001524 }
1525
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001526 for (int uid : restrictedUids) {
1527 // Only splice in restricted state for current user
1528 if (UserHandle.getUserId(uid) != currentUserId) continue;
1529
1530 AppItem item = knownItems.get(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001531 if (item == null) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001532 item = new AppItem(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001533 item.total = -1;
1534 mItems.add(item);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001535 knownItems.put(item.key, item);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001536 }
1537 item.restricted = true;
1538 }
1539
Jeff Sharkey8a503642011-06-10 13:31:21 -07001540 Collections.sort(mItems);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001541 mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001542 notifyDataSetChanged();
1543 }
1544
1545 @Override
1546 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001547 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001548 }
1549
1550 @Override
1551 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001552 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001553 }
1554
1555 @Override
1556 public long getItemId(int position) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001557 return mItems.get(position).key;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001558 }
1559
1560 @Override
1561 public View getView(int position, View convertView, ViewGroup parent) {
1562 if (convertView == null) {
1563 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey5d706792011-09-08 18:57:17 -07001564 R.layout.data_usage_item, parent, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001565
1566 if (mInsetSide > 0) {
Fabrice Di Megliob27223f2013-01-15 18:54:11 -08001567 convertView.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001568 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001569 }
1570
1571 final Context context = parent.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001572
Jeff Sharkey28130d92011-09-02 16:10:24 -07001573 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001574 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1575 android.R.id.progress);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001576
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001577 // kick off async load of app details
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001578 final AppItem item = mItems.get(position);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001579 UidDetailTask.bindView(mProvider, item, convertView);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001580
Jeff Sharkeye557c332012-04-13 16:04:07 -07001581 if (item.restricted && item.total <= 0) {
1582 text1.setText(R.string.data_usage_app_restricted);
1583 progress.setVisibility(View.GONE);
1584 } else {
1585 text1.setText(Formatter.formatFileSize(context, item.total));
1586 progress.setVisibility(View.VISIBLE);
1587 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001588
1589 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1590 progress.setProgress(percentTotal);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001591
1592 return convertView;
1593 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001594 }
1595
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001596 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001597 * Empty {@link Fragment} that controls display of UID details in
1598 * {@link DataUsageSummary}.
1599 */
1600 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001601 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001602
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001603 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001604 if (!parent.isAdded()) return;
1605
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001606 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001607 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001608
1609 final AppDetailsFragment fragment = new AppDetailsFragment();
1610 fragment.setArguments(args);
1611 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001612 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1613 ft.add(fragment, TAG_APP_DETAILS);
1614 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001615 ft.setBreadCrumbTitle(label);
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001616 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001617 }
1618
1619 @Override
1620 public void onStart() {
1621 super.onStart();
1622 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001623 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001624 target.updateBody();
1625 }
1626
1627 @Override
1628 public void onStop() {
1629 super.onStop();
1630 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001631 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001632 target.updateBody();
1633 }
1634 }
1635
1636 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001637 * Dialog to request user confirmation before setting
1638 * {@link NetworkPolicy#limitBytes}.
1639 */
1640 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001641 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001642 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001643
1644 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001645 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001646
Jeff Sharkey461842a2011-09-25 18:22:48 -07001647 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001648 final CharSequence message;
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001649 final long minLimitBytes = (long) (
1650 parent.mPolicyEditor.getPolicy(parent.mTemplate).warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001651 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001652
1653 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001654 final String currentTab = parent.mCurrentTab;
1655 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001656 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001657 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001658 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001659 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001660 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001661 } else if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001662 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001663 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001664 } else {
1665 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001666 }
1667
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001668 final Bundle args = new Bundle();
1669 args.putCharSequence(EXTRA_MESSAGE, message);
1670 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1671
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001672 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1673 dialog.setArguments(args);
1674 dialog.setTargetFragment(parent, 0);
1675 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1676 }
1677
1678 @Override
1679 public Dialog onCreateDialog(Bundle savedInstanceState) {
1680 final Context context = getActivity();
1681
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001682 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001683 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1684
1685 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1686 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001687 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001688
1689 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001690 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001691 public void onClick(DialogInterface dialog, int which) {
1692 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1693 if (target != null) {
1694 target.setPolicyLimitBytes(limitBytes);
1695 }
1696 }
1697 });
1698
1699 return builder.create();
1700 }
1701 }
1702
1703 /**
1704 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1705 */
1706 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001707 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001708
1709 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001710 if (!parent.isAdded()) return;
1711
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001712 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001713 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001714
1715 final CycleEditorFragment dialog = new CycleEditorFragment();
1716 dialog.setArguments(args);
1717 dialog.setTargetFragment(parent, 0);
1718 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1719 }
1720
1721 @Override
1722 public Dialog onCreateDialog(Bundle savedInstanceState) {
1723 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001724 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1725 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001726
1727 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1728 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1729
1730 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1731 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1732
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001733 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1734 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001735
1736 cycleDayPicker.setMinValue(1);
1737 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001738 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001739 cycleDayPicker.setWrapSelectorWheel(true);
1740
1741 builder.setTitle(R.string.data_usage_cycle_editor_title);
1742 builder.setView(view);
1743
1744 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1745 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001746 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001747 public void onClick(DialogInterface dialog, int which) {
Jeff Sharkeyd277de92013-03-25 15:11:25 -07001748 // clear focus to finish pending text edits
1749 cycleDayPicker.clearFocus();
1750
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001751 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001752 final String cycleTimezone = new Time().timezone;
1753 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001754 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001755 }
1756 });
1757
1758 return builder.create();
1759 }
1760 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001761
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001762 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001763 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1764 */
1765 public static class WarningEditorFragment extends DialogFragment {
1766 private static final String EXTRA_TEMPLATE = "template";
1767
1768 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001769 if (!parent.isAdded()) return;
1770
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001771 final Bundle args = new Bundle();
1772 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1773
1774 final WarningEditorFragment dialog = new WarningEditorFragment();
1775 dialog.setArguments(args);
1776 dialog.setTargetFragment(parent, 0);
1777 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1778 }
1779
1780 @Override
1781 public Dialog onCreateDialog(Bundle savedInstanceState) {
1782 final Context context = getActivity();
1783 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1784 final NetworkPolicyEditor editor = target.mPolicyEditor;
1785
1786 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1787 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1788
1789 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1790 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1791
1792 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1793 final long warningBytes = editor.getPolicyWarningBytes(template);
1794 final long limitBytes = editor.getPolicyLimitBytes(template);
1795
1796 bytesPicker.setMinValue(0);
1797 if (limitBytes != LIMIT_DISABLED) {
1798 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1799 } else {
1800 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1801 }
1802 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1803 bytesPicker.setWrapSelectorWheel(false);
1804
1805 builder.setTitle(R.string.data_usage_warning_editor_title);
1806 builder.setView(view);
1807
1808 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1809 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001810 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001811 public void onClick(DialogInterface dialog, int which) {
1812 // clear focus to finish pending text edits
1813 bytesPicker.clearFocus();
1814
1815 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1816 editor.setPolicyWarningBytes(template, bytes);
1817 target.updatePolicy(false);
1818 }
1819 });
1820
1821 return builder.create();
1822 }
1823 }
1824
1825 /**
1826 * Dialog to edit {@link NetworkPolicy#limitBytes}.
1827 */
1828 public static class LimitEditorFragment extends DialogFragment {
1829 private static final String EXTRA_TEMPLATE = "template";
1830
1831 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001832 if (!parent.isAdded()) return;
1833
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001834 final Bundle args = new Bundle();
1835 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1836
1837 final LimitEditorFragment dialog = new LimitEditorFragment();
1838 dialog.setArguments(args);
1839 dialog.setTargetFragment(parent, 0);
1840 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
1841 }
1842
1843 @Override
1844 public Dialog onCreateDialog(Bundle savedInstanceState) {
1845 final Context context = getActivity();
1846 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1847 final NetworkPolicyEditor editor = target.mPolicyEditor;
1848
1849 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1850 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1851
1852 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1853 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1854
1855 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1856 final long warningBytes = editor.getPolicyWarningBytes(template);
1857 final long limitBytes = editor.getPolicyLimitBytes(template);
1858
1859 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09001860 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001861 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
1862 } else {
1863 bytesPicker.setMinValue(0);
1864 }
1865 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
1866 bytesPicker.setWrapSelectorWheel(false);
1867
1868 builder.setTitle(R.string.data_usage_limit_editor_title);
1869 builder.setView(view);
1870
1871 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1872 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001873 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001874 public void onClick(DialogInterface dialog, int which) {
1875 // clear focus to finish pending text edits
1876 bytesPicker.clearFocus();
1877
1878 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1879 editor.setPolicyLimitBytes(template, bytes);
1880 target.updatePolicy(false);
1881 }
1882 });
1883
1884 return builder.create();
1885 }
1886 }
1887 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07001888 * Dialog to request user confirmation before disabling data.
1889 */
1890 public static class ConfirmDataDisableFragment extends DialogFragment {
1891 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001892 if (!parent.isAdded()) return;
1893
Jeff Sharkey28130d92011-09-02 16:10:24 -07001894 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
1895 dialog.setTargetFragment(parent, 0);
1896 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
1897 }
1898
1899 @Override
1900 public Dialog onCreateDialog(Bundle savedInstanceState) {
1901 final Context context = getActivity();
1902
1903 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1904 builder.setMessage(R.string.data_usage_disable_mobile);
1905
1906 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001907 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07001908 public void onClick(DialogInterface dialog, int which) {
1909 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1910 if (target != null) {
1911 // TODO: extend to modify policy enabled flag.
1912 target.setMobileDataEnabled(false);
1913 }
1914 }
1915 });
1916 builder.setNegativeButton(android.R.string.cancel, null);
1917
1918 return builder.create();
1919 }
1920 }
1921
1922 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001923 * Dialog to request user confirmation before setting
Christopher Tate5a64c732012-09-07 13:35:31 -07001924 * {@link android.provider.Settings.Global#DATA_ROAMING}.
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001925 */
1926 public static class ConfirmDataRoamingFragment extends DialogFragment {
1927 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001928 if (!parent.isAdded()) return;
1929
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001930 final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment();
1931 dialog.setTargetFragment(parent, 0);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001932 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_ROAMING);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001933 }
1934
1935 @Override
1936 public Dialog onCreateDialog(Bundle savedInstanceState) {
1937 final Context context = getActivity();
1938
1939 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1940 builder.setTitle(R.string.roaming_reenable_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07001941 if (Utils.hasMultipleUsers(context)) {
1942 builder.setMessage(R.string.roaming_warning_multiuser);
1943 } else {
1944 builder.setMessage(R.string.roaming_warning);
1945 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001946
1947 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001948 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001949 public void onClick(DialogInterface dialog, int which) {
1950 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1951 if (target != null) {
1952 target.setDataRoaming(true);
1953 }
1954 }
1955 });
1956 builder.setNegativeButton(android.R.string.cancel, null);
1957
1958 return builder.create();
1959 }
1960 }
1961
1962 /**
1963 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001964 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001965 */
1966 public static class ConfirmRestrictFragment extends DialogFragment {
1967 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001968 if (!parent.isAdded()) return;
1969
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001970 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1971 dialog.setTargetFragment(parent, 0);
1972 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1973 }
1974
1975 @Override
1976 public Dialog onCreateDialog(Bundle savedInstanceState) {
1977 final Context context = getActivity();
1978
1979 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001980 builder.setTitle(R.string.data_usage_restrict_background_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07001981 if (Utils.hasMultipleUsers(context)) {
1982 builder.setMessage(R.string.data_usage_restrict_background_multiuser);
1983 } else {
1984 builder.setMessage(R.string.data_usage_restrict_background);
1985 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001986
1987 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001988 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001989 public void onClick(DialogInterface dialog, int which) {
1990 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1991 if (target != null) {
1992 target.setRestrictBackground(true);
1993 }
1994 }
1995 });
1996 builder.setNegativeButton(android.R.string.cancel, null);
1997
1998 return builder.create();
1999 }
2000 }
2001
2002 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002003 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
2004 * change has been denied, usually based on
2005 * {@link DataUsageSummary#hasLimitedNetworks()}.
2006 */
2007 public static class DeniedRestrictFragment extends DialogFragment {
2008 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002009 if (!parent.isAdded()) return;
2010
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002011 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
2012 dialog.setTargetFragment(parent, 0);
2013 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
2014 }
2015
2016 @Override
2017 public Dialog onCreateDialog(Bundle savedInstanceState) {
2018 final Context context = getActivity();
2019
2020 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2021 builder.setTitle(R.string.data_usage_app_restrict_background);
2022 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
2023 builder.setPositiveButton(android.R.string.ok, null);
2024
2025 return builder.create();
2026 }
2027 }
2028
2029 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002030 * Dialog to request user confirmation before setting
2031 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
2032 */
2033 public static class ConfirmAppRestrictFragment extends DialogFragment {
2034 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002035 if (!parent.isAdded()) return;
2036
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002037 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2038 dialog.setTargetFragment(parent, 0);
2039 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2040 }
2041
2042 @Override
2043 public Dialog onCreateDialog(Bundle savedInstanceState) {
2044 final Context context = getActivity();
2045
2046 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002047 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2048 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2049
2050 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002051 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002052 public void onClick(DialogInterface dialog, int which) {
2053 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2054 if (target != null) {
2055 target.setAppRestrictBackground(true);
2056 }
2057 }
2058 });
2059 builder.setNegativeButton(android.R.string.cancel, null);
2060
2061 return builder.create();
2062 }
2063 }
2064
2065 /**
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002066 * Dialog to inform user about changing auto-sync setting
2067 */
2068 public static class ConfirmAutoSyncChangeFragment extends DialogFragment {
Amith Yamasani665235f2012-06-07 19:58:34 -07002069 private static final String SAVE_ENABLING = "enabling";
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002070 private boolean mEnabling;
2071
2072 public static void show(DataUsageSummary parent, boolean enabling) {
2073 if (!parent.isAdded()) return;
2074
2075 final ConfirmAutoSyncChangeFragment dialog = new ConfirmAutoSyncChangeFragment();
2076 dialog.mEnabling = enabling;
2077 dialog.setTargetFragment(parent, 0);
2078 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_AUTO_SYNC_CHANGE);
2079 }
2080
2081 @Override
2082 public Dialog onCreateDialog(Bundle savedInstanceState) {
2083 final Context context = getActivity();
Amith Yamasani665235f2012-06-07 19:58:34 -07002084 if (savedInstanceState != null) {
2085 mEnabling = savedInstanceState.getBoolean(SAVE_ENABLING);
2086 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002087
2088 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2089 if (!mEnabling) {
2090 builder.setTitle(R.string.data_usage_auto_sync_off_dialog_title);
2091 builder.setMessage(R.string.data_usage_auto_sync_off_dialog);
2092 } else {
2093 builder.setTitle(R.string.data_usage_auto_sync_on_dialog_title);
2094 builder.setMessage(R.string.data_usage_auto_sync_on_dialog);
2095 }
2096
2097 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
2098 @Override
2099 public void onClick(DialogInterface dialog, int which) {
2100 ContentResolver.setMasterSyncAutomatically(mEnabling);
2101 }
2102 });
2103 builder.setNegativeButton(android.R.string.cancel, null);
2104
2105 return builder.create();
2106 }
Amith Yamasani665235f2012-06-07 19:58:34 -07002107
2108 @Override
2109 public void onSaveInstanceState(Bundle outState) {
2110 super.onSaveInstanceState(outState);
2111 outState.putBoolean(SAVE_ENABLING, mEnabling);
2112 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002113 }
2114
2115 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002116 * Compute default tab that should be selected, based on
2117 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2118 */
2119 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002120 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
2121 if (template == null) return null;
2122
2123 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002124 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002125 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002126 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002127 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002128 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002129 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002130 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002131 return TAB_WIFI;
2132 default:
2133 return null;
2134 }
2135 }
2136
2137 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002138 * Background task that loads {@link UidDetail}, binding to
2139 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002140 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002141 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2142 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002143 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002144 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002145
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002146 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002147 mProvider = checkNotNull(provider);
2148 mItem = checkNotNull(item);
2149 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002150 }
2151
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002152 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002153 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002154 final UidDetailTask existing = (UidDetailTask) target.getTag();
2155 if (existing != null) {
2156 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002157 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002158
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002159 final UidDetail cachedDetail = provider.getUidDetail(item.key, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002160 if (cachedDetail != null) {
2161 bindView(cachedDetail, target);
2162 } else {
2163 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2164 AsyncTask.THREAD_POOL_EXECUTOR));
2165 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002166 }
2167
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002168 private static void bindView(UidDetail detail, View target) {
2169 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2170 final TextView title = (TextView) target.findViewById(android.R.id.title);
2171
2172 if (detail != null) {
2173 icon.setImageDrawable(detail.icon);
2174 title.setText(detail.label);
2175 } else {
2176 icon.setImageDrawable(null);
2177 title.setText(null);
2178 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002179 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002180
2181 @Override
2182 protected void onPreExecute() {
2183 bindView(null, mTarget);
2184 }
2185
2186 @Override
2187 protected UidDetail doInBackground(Void... params) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002188 return mProvider.getUidDetail(mItem.key, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002189 }
2190
2191 @Override
2192 protected void onPostExecute(UidDetail result) {
2193 bindView(result, mTarget);
2194 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002195 }
2196
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002197 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002198 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002199 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002200 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002201 if (TEST_RADIOS) {
2202 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2203 }
2204
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002205 final ConnectivityManager conn = ConnectivityManager.from(context);
2206 final TelephonyManager tele = TelephonyManager.from(context);
2207
2208 // require both supported network and ready SIM
2209 return conn.isNetworkSupported(TYPE_MOBILE) && tele.getSimState() == SIM_STATE_READY;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002210 }
2211
2212 /**
2213 * Test if device has a mobile 4G data radio.
2214 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002215 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002216 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2217 return false;
2218 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002219 if (TEST_RADIOS) {
2220 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2221 }
2222
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002223 final ConnectivityManager conn = ConnectivityManager.from(context);
2224 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002225
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002226 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Wink Saville55434042012-06-14 12:33:43 -07002227 final boolean hasLte = (tele.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE)
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002228 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002229 return hasWimax || hasLte;
2230 }
2231
2232 /**
2233 * Test if device has a Wi-Fi data radio.
2234 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002235 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002236 if (TEST_RADIOS) {
2237 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2238 }
2239
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002240 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002241 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002242 }
2243
2244 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002245 * Test if device has an ethernet network connection.
2246 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002247 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002248 if (TEST_RADIOS) {
2249 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2250 }
2251
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002252 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002253 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2254
2255 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002256 if (mStatsSession != null) {
2257 try {
2258 ethernetBytes = mStatsSession.getSummaryForNetwork(
2259 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2260 .getTotalBytes();
2261 } catch (RemoteException e) {
2262 throw new RuntimeException(e);
2263 }
2264 } else {
2265 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002266 }
2267
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002268 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002269 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002270 }
2271
2272 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002273 * Inflate a {@link Preference} style layout, adding the given {@link View}
2274 * widget into {@link android.R.id#widget_frame}.
2275 */
2276 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2277 final View view = inflater.inflate(R.layout.preference, root, false);
2278 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2279 android.R.id.widget_frame);
2280 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2281 return view;
2282 }
2283
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002284 private static View inflateAppTitle(
2285 LayoutInflater inflater, ViewGroup root, CharSequence label) {
2286 final TextView view = (TextView) inflater.inflate(
2287 R.layout.data_usage_app_title, root, false);
2288 view.setText(label);
2289 return view;
2290 }
2291
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002292 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002293 * Test if any networks are currently limited.
2294 */
2295 private boolean hasLimitedNetworks() {
2296 return !buildLimitedNetworksList().isEmpty();
2297 }
2298
2299 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002300 * Build string describing currently limited networks, which defines when
2301 * background data is restricted.
2302 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002303 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002304 private CharSequence buildLimitedNetworksString() {
2305 final List<CharSequence> limited = buildLimitedNetworksList();
2306
2307 // handle case where no networks limited
2308 if (limited.isEmpty()) {
2309 limited.add(getText(R.string.data_usage_list_none));
2310 }
2311
2312 return TextUtils.join(limited);
2313 }
2314
2315 /**
2316 * Build list of currently limited networks, which defines when background
2317 * data is restricted.
2318 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002319 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002320 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002321 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002322
2323 // build combined list of all limited networks
2324 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002325
2326 final TelephonyManager tele = TelephonyManager.from(context);
2327 if (tele.getSimState() == SIM_STATE_READY) {
2328 final String subscriberId = getActiveSubscriberId(context);
2329 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2330 limited.add(getText(R.string.data_usage_list_mobile));
2331 }
2332 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2333 limited.add(getText(R.string.data_usage_tab_3g));
2334 }
2335 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2336 limited.add(getText(R.string.data_usage_tab_4g));
2337 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002338 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002339
2340 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002341 limited.add(getText(R.string.data_usage_tab_wifi));
2342 }
2343 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2344 limited.add(getText(R.string.data_usage_tab_ethernet));
2345 }
2346
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002347 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002348 }
2349
2350 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002351 * Inset both selector and divider {@link Drawable} on the given
2352 * {@link ListView} by the requested dimensions.
2353 */
2354 private static void insetListViewDrawables(ListView view, int insetSide) {
2355 final Drawable selector = view.getSelector();
2356 final Drawable divider = view.getDivider();
2357
2358 // fully unregister these drawables so callbacks can be maintained after
2359 // wrapping below.
2360 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2361 view.setSelector(stub);
2362 view.setDivider(stub);
2363
2364 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2365 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2366 }
2367
2368 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002369 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002370 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002371 */
2372 private static void setPreferenceTitle(View parent, int resId) {
2373 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2374 title.setText(resId);
2375 }
2376
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002377 /**
2378 * Set {@link android.R.id#summary} for a preference view inflated with
2379 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2380 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002381 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002382 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2383 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002384 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002385 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002386}