blob: 1d4181600fe28aa84c7f48f54f9725b988fcc7aa [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;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070056import android.app.FragmentTransaction;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070057import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070058import android.content.ContentResolver;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070059import android.content.Context;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070060import android.content.DialogInterface;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -070061import android.content.Intent;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070062import android.content.Loader;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070063import android.content.SharedPreferences;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070064import android.content.pm.PackageManager;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070065import android.content.res.Resources;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070066import android.graphics.Color;
Jeff Sharkey5d706792011-09-08 18:57:17 -070067import android.graphics.drawable.ColorDrawable;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070068import android.graphics.drawable.Drawable;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070069import android.net.ConnectivityManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070070import android.net.INetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070071import android.net.INetworkStatsService;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070072import android.net.INetworkStatsSession;
Jeff Sharkey8a503642011-06-10 13:31:21 -070073import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070074import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070075import android.net.NetworkStats;
76import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070077import android.net.NetworkTemplate;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070078import android.net.TrafficStats;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070079import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070080import android.os.Bundle;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070081import android.os.INetworkManagementService;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070082import android.os.Parcel;
83import android.os.Parcelable;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070084import android.os.RemoteException;
85import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070086import android.os.SystemProperties;
Dianne Hackbornbb06a422012-08-16 11:01:57 -070087import android.os.UserHandle;
Jeff Sharkey8a503642011-06-10 13:31:21 -070088import android.preference.Preference;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070089import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070090import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070091import android.text.format.DateUtils;
92import android.text.format.Formatter;
Jeff Sharkeye5223a02012-03-09 17:11:14 -080093import android.text.format.Time;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070094import android.util.Log;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070095import android.util.SparseArray;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070096import android.util.SparseBooleanArray;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070097import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -070098import android.view.Menu;
99import android.view.MenuInflater;
100import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700101import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700102import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700103import android.view.ViewGroup;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700104import android.widget.AdapterView;
105import android.widget.AdapterView.OnItemClickListener;
106import android.widget.AdapterView.OnItemSelectedListener;
107import android.widget.ArrayAdapter;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700108import android.widget.BaseAdapter;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700109import android.widget.Button;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700110import android.widget.CheckBox;
111import android.widget.CompoundButton;
112import android.widget.CompoundButton.OnCheckedChangeListener;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700113import android.widget.ImageView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700114import android.widget.LinearLayout;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700115import android.widget.ListView;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700116import android.widget.NumberPicker;
Jeff Sharkey2412b0f2011-07-17 20:31:40 -0700117import android.widget.ProgressBar;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700118import android.widget.Spinner;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700119import android.widget.Switch;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700120import android.widget.TabHost;
121import android.widget.TabHost.OnTabChangeListener;
122import android.widget.TabHost.TabContentFactory;
123import android.widget.TabHost.TabSpec;
124import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700125import android.widget.TextView;
126
Wink Saville55434042012-06-14 12:33:43 -0700127import com.android.internal.telephony.PhoneConstants;
Jeff Sharkey5d706792011-09-08 18:57:17 -0700128import com.android.settings.drawable.InsetBoundsDrawable;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700129import com.android.settings.net.ChartData;
130import com.android.settings.net.ChartDataLoader;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700131import com.android.settings.net.DataUsageMeteredSettings;
Jeff Sharkeya662e492011-06-18 21:57:06 -0700132import com.android.settings.net.NetworkPolicyEditor;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700133import com.android.settings.net.SummaryForAllUidLoader;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700134import com.android.settings.net.UidDetail;
135import com.android.settings.net.UidDetailProvider;
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -0700136import com.android.settings.search.BaseSearchIndexProvider;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -0700137import com.android.settings.search.Indexable;
138import com.android.settings.search.SearchIndexableRaw;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700139import com.android.settings.widget.ChartDataUsageView;
140import com.android.settings.widget.ChartDataUsageView.DataUsageChartListener;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700141import com.android.settings.widget.PieChartView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700142import com.google.android.collect.Lists;
143
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700144import libcore.util.Objects;
145
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700146import java.util.ArrayList;
147import java.util.Collections;
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700148import java.util.List;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700149import java.util.Locale;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700150
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700151/**
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700152 * Panel showing data usage history across various networks, including options
153 * to inspect based on usage cycle and control through {@link NetworkPolicy}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700154 */
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700155public class DataUsageSummary extends HighlightingFragment implements Indexable {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700156 private static final String TAG = "DataUsage";
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800157 private static final boolean LOGD = false;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700158
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700159 // TODO: remove this testing code
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700160 private static final boolean TEST_ANIM = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700161 private static final boolean TEST_RADIOS = false;
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800162
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700163 private static final String TEST_RADIOS_PROP = "test.radios";
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800164 private static final String TEST_SUBSCRIBER_PROP = "test.subscriberid";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700165
Jeff Sharkey8a503642011-06-10 13:31:21 -0700166 private static final String TAB_3G = "3g";
167 private static final String TAB_4G = "4g";
168 private static final String TAB_MOBILE = "mobile";
169 private static final String TAB_WIFI = "wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700170 private static final String TAB_ETHERNET = "ethernet";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700171
Jeff Sharkey28130d92011-09-02 16:10:24 -0700172 private static final String TAG_CONFIRM_DATA_DISABLE = "confirmDataDisable";
173 private static final String TAG_CONFIRM_DATA_ROAMING = "confirmDataRoaming";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700174 private static final String TAG_CONFIRM_LIMIT = "confirmLimit";
175 private static final String TAG_CYCLE_EDITOR = "cycleEditor";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700176 private static final String TAG_WARNING_EDITOR = "warningEditor";
177 private static final String TAG_LIMIT_EDITOR = "limitEditor";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700178 private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700179 private static final String TAG_DENIED_RESTRICT = "deniedRestrict";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700180 private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict";
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700181 private static final String TAG_CONFIRM_AUTO_SYNC_CHANGE = "confirmAutoSyncChange";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700182 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700183
Fabrice Di Meglio2169c882014-04-18 15:18:40 -0700184 private static final String DATA_USAGE_ENABLE_MOBILE_KEY = "data_usage_enable_mobile";
185 private static final String DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY =
186 "data_usage_disable_mobile_limit";
187 private static final String DATA_USAGE_CYCLE_KEY = "data_usage_cycle";
188
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700189 private static final int LOADER_CHART_DATA = 2;
190 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700191
Fabrice Di Meglio6ebcc362014-06-18 12:49:35 -0700192 private static final int FOREGROUND_BYTES_COLOR = 0xff009688;
193 private static final int DEFAULT_BYTES_COLOR = 0xffced7db;
194
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700195 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700196 private INetworkStatsService mStatsService;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700197 private NetworkPolicyManager mPolicyManager;
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700198 private TelephonyManager mTelephonyManager;
199
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700200
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700201 private INetworkStatsSession mStatsSession;
202
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700203 private static final String PREF_FILE = "data_usage";
204 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700205 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700206
207 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700208
Jeff Sharkey8a503642011-06-10 13:31:21 -0700209 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700210 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700211 private TabWidget mTabWidget;
212 private ListView mListView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700213 private DataUsageAdapter mAdapter;
214
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700215 /** Distance to inset content from sides, when needed. */
216 private int mInsetSide = 0;
217
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700218 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700219
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700220 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700221 private LinearLayout mNetworkSwitches;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700222 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700223 private View mDataEnabledView;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700224 private CheckBox mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700225 private View mDisableAtLimitView;
226
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700227 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700228 private Spinner mCycleSpinner;
229 private CycleAdapter mCycleAdapter;
230
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700231 private ChartDataUsageView mChart;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700232 private TextView mUsageSummary;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700233 private TextView mEmpty;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700234
235 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700236 private ImageView mAppIcon;
237 private ViewGroup mAppTitles;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700238 private PieChartView mAppPieChart;
239 private TextView mAppForeground;
240 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700241 private Button mAppSettings;
242
243 private LinearLayout mAppSwitches;
244 private CheckBox mAppRestrict;
245 private View mAppRestrictView;
246
Jeff Sharkey8a503642011-06-10 13:31:21 -0700247 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700248 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700249
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700250 private NetworkTemplate mTemplate;
251 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700252
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700253 private AppItem mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700254
255 private Intent mAppSettingsIntent;
256
Jeff Sharkeya662e492011-06-18 21:57:06 -0700257 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700258
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700259 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700260 private String mIntentTab = null;
261
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700262 private MenuItem mMenuDataRoaming;
263 private MenuItem mMenuRestrictBackground;
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700264 private MenuItem mMenuAutoSync;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700265
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700266 /** Flag used to ignore listeners during binding. */
267 private boolean mBinding;
268
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700269 private UidDetailProvider mUidDetailProvider;
270
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700271 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700272 public void onCreate(Bundle savedInstanceState) {
273 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700274 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700275
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700276 mNetworkService = INetworkManagementService.Stub.asInterface(
277 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700278 mStatsService = INetworkStatsService.Stub.asInterface(
279 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700280 mPolicyManager = NetworkPolicyManager.from(context);
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700281 mTelephonyManager = TelephonyManager.from(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700282
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700283 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700284
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700285 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700286 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700287
Jaewan Kimffce9c12013-03-19 16:53:45 +0900288 try {
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700289 if (!mNetworkService.isBandwidthControlEnabled()) {
290 Log.w(TAG, "No bandwidth control; leaving");
291 getActivity().finish();
292 }
293 } catch (RemoteException e) {
294 Log.w(TAG, "No bandwidth control; leaving");
295 getActivity().finish();
296 }
297
298 try {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900299 mStatsSession = mStatsService.openSession();
300 } catch (RemoteException e) {
301 throw new RuntimeException(e);
302 }
303
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700304 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700305 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700306
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700307 // override preferences when no mobile radio
308 if (!hasReadyMobileRadio(context)) {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900309 mShowWifi = true;
310 mShowEthernet = true;
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700311 }
312
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700313 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700314 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700315
Jeff Sharkey8a503642011-06-10 13:31:21 -0700316 @Override
317 public View onCreateView(LayoutInflater inflater, ViewGroup container,
318 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700319
Jeff Sharkey8a503642011-06-10 13:31:21 -0700320 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700321 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
322
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700323 mUidDetailProvider = new UidDetailProvider(context);
324
Jeff Sharkey8a503642011-06-10 13:31:21 -0700325 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700326 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700327 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
328 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700329
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700330 // decide if we need to manually inset our content, or if we should rely
331 // on parent container for inset.
332 final boolean shouldInset = mListView.getScrollBarStyle()
333 == View.SCROLLBARS_OUTSIDE_OVERLAY;
Amith Yamasani56f51a82013-08-05 10:07:23 -0700334 mInsetSide = 0;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700335
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700336 // adjust padding around tabwidget as needed
Amith Yamasani56f51a82013-08-05 10:07:23 -0700337 prepareCustomPreferencesList(container, view, mListView, false);
Jeff Sharkey5d706792011-09-08 18:57:17 -0700338
Jeff Sharkey8a503642011-06-10 13:31:21 -0700339 mTabHost.setup();
340 mTabHost.setOnTabChangedListener(mTabListener);
341
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700342 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700343 mHeader.setClickable(true);
344
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700345 mListView.addHeaderView(new View(context), null, true);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700346 mListView.addHeaderView(mHeader, null, true);
347 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700348
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700349 if (mInsetSide > 0) {
350 // inset selector and divider drawables
351 insetListViewDrawables(mListView, mInsetSide);
Fabrice Di Megliob27223f2013-01-15 18:54:11 -0800352 mHeader.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700353 }
354
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700355 {
356 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700357 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
358 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700359 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700360
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700361 mDataEnabled = new Switch(inflater.getContext());
362 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -0700363 mDataEnabledView.setTag(DATA_USAGE_ENABLE_MOBILE_KEY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700364 mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener);
365 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700366
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700367 mDisableAtLimit = new CheckBox(inflater.getContext());
368 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700369 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700370 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -0700371 mDisableAtLimitView.setTag(DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700372 mDisableAtLimitView.setClickable(true);
373 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700374 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
375 mNetworkSwitches.addView(mDisableAtLimitView);
376 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700377
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700378 // bind cycle dropdown
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700379 mCycleView = mHeader.findViewById(R.id.cycles);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -0700380 mCycleView.setTag(DATA_USAGE_CYCLE_KEY);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700381 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700382 mCycleAdapter = new CycleAdapter(context);
383 mCycleSpinner.setAdapter(mCycleAdapter);
384 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
385
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700386 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700387 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800388 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700389
390 {
391 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700392 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700393 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
394 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700395 mAppPieChart = (PieChartView) mAppDetail.findViewById(R.id.app_pie_chart);
396 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
397 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700398 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700399
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700400 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700401 mAppSettings.setOnClickListener(mAppSettingsListener);
402
403 mAppRestrict = new CheckBox(inflater.getContext());
404 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700405 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700406 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700407 mAppRestrictView.setClickable(true);
408 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700409 mAppRestrictView.setOnClickListener(mAppRestrictListener);
410 mAppSwitches.addView(mAppRestrictView);
411 }
412
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700413 mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700414 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700415
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700416 mAdapter = new DataUsageAdapter(mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700417 mListView.setOnItemClickListener(mListListener);
418 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700419
420 return view;
421 }
422
423 @Override
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700424 public void onViewStateRestored(Bundle savedInstanceState) {
425 super.onViewStateRestored(savedInstanceState);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700426
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700427 // pick default tab based on incoming intent
428 final Intent intent = getActivity().getIntent();
429 mIntentTab = computeTabFromIntent(intent);
430
Jeff Sharkey8a503642011-06-10 13:31:21 -0700431 // this kicks off chain reaction which creates tabs, binds the body to
432 // selected network, and binds chart, cycles and detail list.
433 updateTabs();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700434 }
435
436 @Override
437 public void onResume() {
438 super.onResume();
439
440 getView().post(new Runnable() {
441 @Override
442 public void run() {
443 highlightViewIfNeeded();
444 }
445 });
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700446
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700447 // kick off background task to update stats
448 new AsyncTask<Void, Void, Void>() {
449 @Override
450 protected Void doInBackground(Void... params) {
451 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700452 // wait a few seconds before kicking off
453 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700454 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700455 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700456 } catch (RemoteException e) {
457 }
458 return null;
459 }
460
461 @Override
462 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700463 if (isAdded()) {
464 updateBody();
465 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700466 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700467 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700468 }
469
Jeff Sharkey8a503642011-06-10 13:31:21 -0700470 @Override
471 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
472 inflater.inflate(R.menu.data_usage, menu);
473 }
474
475 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700476 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700477 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700478 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700479 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700480
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700481 mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700482 mMenuDataRoaming.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700483 mMenuDataRoaming.setChecked(getDataRoaming());
484
485 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700486 mMenuRestrictBackground.setVisible(
487 hasReadyMobileRadio(context) && isOwner && !appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700488 mMenuRestrictBackground.setChecked(mPolicyManager.getRestrictBackground());
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700489
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700490 mMenuAutoSync = menu.findItem(R.id.data_usage_menu_auto_sync);
491 mMenuAutoSync.setChecked(ContentResolver.getMasterSyncAutomatically());
Jeff Sharkeyfda48e32012-09-18 15:03:47 -0700492 mMenuAutoSync.setVisible(!appDetailMode);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700493
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700494 final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g);
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700495 split4g.setVisible(hasReadyMobile4gRadio(context) && isOwner && !appDetailMode);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700496 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700497
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700498 final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700499 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700500 showWifi.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700501 showWifi.setChecked(mShowWifi);
502 } else {
503 showWifi.setVisible(false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700504 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700505
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700506 final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700507 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700508 showEthernet.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700509 showEthernet.setChecked(mShowEthernet);
510 } else {
511 showEthernet.setVisible(false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700512 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700513
514 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
515 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700516 metered.setVisible(!appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700517 } else {
518 metered.setVisible(false);
519 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700520
521 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
522 String helpUrl;
523 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800524 HelpUtils.prepareHelpMenuItem(context, help, helpUrl);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700525 } else {
526 help.setVisible(false);
527 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700528 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700529
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700530 @Override
531 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700532 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700533 case R.id.data_usage_menu_roaming: {
534 final boolean dataRoaming = !item.isChecked();
535 if (dataRoaming) {
536 ConfirmDataRoamingFragment.show(this);
537 } else {
538 // no confirmation to disable roaming
539 setDataRoaming(false);
540 }
541 return true;
542 }
543 case R.id.data_usage_menu_restrict_background: {
544 final boolean restrictBackground = !item.isChecked();
545 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800546 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700547 } else {
548 // no confirmation to drop restriction
549 setRestrictBackground(false);
550 }
551 return true;
552 }
553 case R.id.data_usage_menu_split_4g: {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700554 final boolean mobileSplit = !item.isChecked();
Jeff Sharkeya662e492011-06-18 21:57:06 -0700555 setMobilePolicySplit(mobileSplit);
556 item.setChecked(isMobilePolicySplit());
Jeff Sharkey8a503642011-06-10 13:31:21 -0700557 updateTabs();
558 return true;
559 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700560 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700561 mShowWifi = !item.isChecked();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700562 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700563 item.setChecked(mShowWifi);
564 updateTabs();
565 return true;
566 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700567 case R.id.data_usage_menu_show_ethernet: {
568 mShowEthernet = !item.isChecked();
569 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
570 item.setChecked(mShowEthernet);
571 updateTabs();
572 return true;
573 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700574 case R.id.data_usage_menu_metered: {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800575 final SettingsActivity sa = (SettingsActivity) getActivity();
576 sa.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700577 R.string.data_usage_metered_title, null, this, 0);
578 return true;
579 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700580 case R.id.data_usage_menu_auto_sync: {
Guang Zhu167ba2a2012-10-19 17:55:05 -0700581 if (ActivityManager.isUserAMonkey()) {
582 Log.d("SyncState", "ignoring monkey's attempt to flip global sync state");
583 } else {
584 ConfirmAutoSyncChangeFragment.show(this, !item.isChecked());
585 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700586 return true;
587 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700588 }
589 return false;
590 }
591
Jeff Sharkey94a90952011-06-13 22:31:09 -0700592 @Override
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700593 public void onDestroy() {
Jeff Sharkey94a90952011-06-13 22:31:09 -0700594 mDataEnabledView = null;
595 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700596
597 mUidDetailProvider.clearCache();
598 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700599
600 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700601
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800602 super.onDestroy();
603 }
604
Jeff Sharkey8a503642011-06-10 13:31:21 -0700605 /**
Jeff Sharkey92811822012-05-04 11:47:29 -0700606 * Build and assign {@link LayoutTransition} to various containers. Should
607 * only be assigned after initial layout is complete.
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700608 */
Jeff Sharkey92811822012-05-04 11:47:29 -0700609 private void ensureLayoutTransitions() {
610 // skip when already setup
611 if (mChart.getLayoutTransition() != null) return;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700612
Jeff Sharkey92811822012-05-04 11:47:29 -0700613 mTabsContainer.setLayoutTransition(buildLayoutTransition());
614 mHeader.setLayoutTransition(buildLayoutTransition());
615 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700616
Jeff Sharkey92811822012-05-04 11:47:29 -0700617 final LayoutTransition chartTransition = buildLayoutTransition();
618 chartTransition.disableTransitionType(LayoutTransition.APPEARING);
619 chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
620 mChart.setLayoutTransition(chartTransition);
621 }
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700622
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700623 private static LayoutTransition buildLayoutTransition() {
624 final LayoutTransition transition = new LayoutTransition();
625 if (TEST_ANIM) {
626 transition.setDuration(1500);
627 }
628 transition.setAnimateParentHierarchy(false);
629 return transition;
630 }
631
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700632 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700633 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700634 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
635 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700636 */
637 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700638 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700639 mTabHost.clearAllTabs();
640
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700641 final boolean mobileSplit = isMobilePolicySplit();
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700642 if (mobileSplit && hasReadyMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700643 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
644 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700645 } else if (hasReadyMobileRadio(context)) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700646 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700647 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700648 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700649 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
650 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700651 if (mShowEthernet && hasEthernet(context)) {
652 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
653 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700654
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700655 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700656 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
657 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
658 if (mIntentTab != null) {
659 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700660 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700661 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700662 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700663 mTabHost.setCurrentTabByTag(mIntentTab);
664 }
665 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700666 } else if (noTabs) {
667 // no usable tabs, so hide body
668 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700669 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700670 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700671 }
672 }
673
Jeff Sharkey8a503642011-06-10 13:31:21 -0700674 /**
675 * Factory that provide empty {@link View} to make {@link TabHost} happy.
676 */
677 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700678 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700679 public View createTabContent(String tag) {
680 return new View(mTabHost.getContext());
681 }
682 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700683
Jeff Sharkey8a503642011-06-10 13:31:21 -0700684 /**
685 * Build {@link TabSpec} with thin indicator, and empty content.
686 */
687 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700688 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
689 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700690 }
691
692 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700693 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700694 public void onTabChanged(String tabId) {
695 // user changed tab; update body
696 updateBody();
697 }
698 };
699
700 /**
701 * Update body content based on current tab. Loads
702 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
703 * binds them to visible controls.
704 */
705 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700706 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700707 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700708
Jeff Sharkeya662e492011-06-18 21:57:06 -0700709 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700710 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700711 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700712
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700713 if (currentTab == null) {
714 Log.w(TAG, "no tab selected; hiding body");
715 mListView.setVisibility(View.GONE);
716 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700717 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700718 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700719 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700720
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700721 mCurrentTab = currentTab;
722
Jeff Sharkey8a503642011-06-10 13:31:21 -0700723 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
724
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700725 mDataEnabledView.setVisibility(isOwner ? View.VISIBLE : View.GONE);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700726
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700727 // TODO: remove mobile tabs when SIM isn't ready
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700728
Jeff Sharkey8a503642011-06-10 13:31:21 -0700729 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700730 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
731 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700732 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700733
734 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700735 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
736 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
737 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700738 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700739
740 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700741 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
742 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
743 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700744 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700745
746 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700747 // wifi doesn't have any controls
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700748 mDataEnabledView.setVisibility(View.GONE);
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700749 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700750 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700751
752 } else if (TAB_ETHERNET.equals(currentTab)) {
753 // ethernet doesn't have any controls
754 mDataEnabledView.setVisibility(View.GONE);
755 mDisableAtLimitView.setVisibility(View.GONE);
756 mTemplate = buildTemplateEthernet();
757
758 } else {
759 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700760 }
761
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700762 // kick off loader for network history
763 // TODO: consider chaining two loaders together instead of reloading
764 // network history when showing app detail.
765 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700766 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700767
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700768 // detail mode can change visible menus, invalidate
769 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700770
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700771 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700772 }
773
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700774 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700775 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700776 }
777
778 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700779 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700780 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700781 */
782 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700783 final Context context = getActivity();
784 final PackageManager pm = context.getPackageManager();
785 final LayoutInflater inflater = getActivity().getLayoutInflater();
786
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700787 if (isAppDetailMode()) {
788 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700789 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700790 } else {
791 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700792 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700793
794 // hide detail stats when not in detail mode
795 mChart.bindDetailNetworkStats(null);
796 return;
797 }
798
799 // remove warning/limit sweeps while in detail mode
800 mChart.bindNetworkPolicy(null);
801
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700802 // show icon and all labels appearing under this app
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700803 final int uid = mCurrentApp.key;
804 final UidDetail detail = mUidDetailProvider.getUidDetail(uid, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700805 mAppIcon.setImageDrawable(detail.icon);
806
807 mAppTitles.removeAllViews();
808 if (detail.detailLabels != null) {
809 for (CharSequence label : detail.detailLabels) {
810 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label));
811 }
812 } else {
813 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label));
814 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700815
816 // enable settings button when package provides it
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700817 final String[] packageNames = pm.getPackagesForUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700818 if (packageNames != null && packageNames.length > 0) {
819 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700820 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
821
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700822 // Search for match across all packages
823 boolean matchFound = false;
824 for (String packageName : packageNames) {
825 mAppSettingsIntent.setPackage(packageName);
826 if (pm.resolveActivity(mAppSettingsIntent, 0) != null) {
827 matchFound = true;
828 break;
829 }
830 }
831
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700832 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -0700833 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700834
835 } else {
836 mAppSettingsIntent = null;
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700837 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700838 }
839
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700840 updateDetailData();
841
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700842 if (UserHandle.isApp(uid) && !mPolicyManager.getRestrictBackground()
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700843 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700844 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800845 setPreferenceSummary(mAppRestrictView,
846 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700847
848 mAppRestrictView.setVisibility(View.VISIBLE);
849 mAppRestrict.setChecked(getAppRestrictBackground());
850
851 } else {
852 mAppRestrictView.setVisibility(View.GONE);
853 }
854 }
855
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700856 private void setPolicyWarningBytes(long warningBytes) {
857 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700858 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700859 updatePolicy(false);
860 }
861
862 private void setPolicyLimitBytes(long limitBytes) {
863 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700864 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700865 updatePolicy(false);
866 }
867
Jeff Sharkey28130d92011-09-02 16:10:24 -0700868 /**
869 * Local cache of value, used to work around delay when
870 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
871 */
872 private Boolean mMobileDataEnabled;
873
874 private boolean isMobileDataEnabled() {
875 if (mMobileDataEnabled != null) {
876 // TODO: deprecate and remove this once enabled flag is on policy
877 return mMobileDataEnabled;
878 } else {
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700879 return mTelephonyManager.getDataEnabled();
Jeff Sharkey28130d92011-09-02 16:10:24 -0700880 }
881 }
882
883 private void setMobileDataEnabled(boolean enabled) {
884 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700885 mTelephonyManager.setDataEnabled(enabled);
Jeff Sharkey28130d92011-09-02 16:10:24 -0700886 mMobileDataEnabled = enabled;
887 updatePolicy(false);
888 }
889
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700890 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700891 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked()
892 && ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700893 }
894
895 private boolean isBandwidthControlEnabled() {
896 try {
897 return mNetworkService.isBandwidthControlEnabled();
898 } catch (RemoteException e) {
899 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
900 return false;
901 }
902 }
903
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700904 private boolean getDataRoaming() {
905 final ContentResolver resolver = getActivity().getContentResolver();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800906 return android.provider.Settings.Global.getInt(resolver,
907 android.provider.Settings.Global.DATA_ROAMING, 0) != 0;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700908 }
909
910 private void setDataRoaming(boolean enabled) {
911 // TODO: teach telephony DataConnectionTracker to watch and apply
912 // updates when changed.
913 final ContentResolver resolver = getActivity().getContentResolver();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800914 android.provider.Settings.Global.putInt(resolver,
915 android.provider.Settings.Global.DATA_ROAMING, enabled ? 1 : 0);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700916 mMenuDataRoaming.setChecked(enabled);
917 }
918
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700919 public void setRestrictBackground(boolean restrictBackground) {
920 mPolicyManager.setRestrictBackground(restrictBackground);
921 mMenuRestrictBackground.setChecked(restrictBackground);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700922 }
923
924 private boolean getAppRestrictBackground() {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700925 final int uid = mCurrentApp.key;
926 final int uidPolicy = mPolicyManager.getUidPolicy(uid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700927 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
928 }
929
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700930 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700931 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700932 final int uid = mCurrentApp.key;
933 mPolicyManager.setUidPolicy(
934 uid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700935 mAppRestrict.setChecked(restrictBackground);
936 }
937
Jeff Sharkey8a503642011-06-10 13:31:21 -0700938 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700939 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
940 * current {@link #mTemplate}.
941 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700942 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700943 if (isAppDetailMode()) {
944 mNetworkSwitches.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700945 } else {
946 mNetworkSwitches.setVisibility(View.VISIBLE);
947 }
948
Jeff Sharkey28130d92011-09-02 16:10:24 -0700949 // TODO: move enabled state directly into policy
950 if (TAB_MOBILE.equals(mCurrentTab)) {
951 mBinding = true;
952 mDataEnabled.setChecked(isMobileDataEnabled());
953 mBinding = false;
954 }
955
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700956 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
957 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700958 mDisableAtLimitView.setVisibility(View.VISIBLE);
959 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700960 if (!isAppDetailMode()) {
961 mChart.bindNetworkPolicy(policy);
962 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700963
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700964 } else {
965 // controls are disabled; don't bind warning/limit sweeps
966 mDisableAtLimitView.setVisibility(View.GONE);
967 mChart.bindNetworkPolicy(null);
968 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700969
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700970 if (refreshCycle) {
971 // generate cycle list based on policy and available history
972 updateCycleList(policy);
973 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700974 }
975
976 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700977 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
978 * and available {@link NetworkStatsHistory} data. Always selects the newest
979 * item, updating the inspection range on {@link #mChart}.
980 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700981 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700982 // stash away currently selected cycle to try restoring below
983 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700984 mCycleAdapter.clear();
985
986 final Context context = mCycleSpinner.getContext();
987
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700988 long historyStart = Long.MAX_VALUE;
989 long historyEnd = Long.MIN_VALUE;
990 if (mChartData != null) {
991 historyStart = mChartData.network.getStart();
992 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700993 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700994
Jeff Sharkey461842a2011-09-25 18:22:48 -0700995 final long now = System.currentTimeMillis();
996 if (historyStart == Long.MAX_VALUE) historyStart = now;
997 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700998
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700999 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001000 if (policy != null) {
1001 // find the next cycle boundary
1002 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001003
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001004 // walk backwards, generating all valid cycle ranges
1005 while (cycleEnd > historyStart) {
1006 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
1007 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
1008 + historyStart);
1009 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1010 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001011 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001012 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001013
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001014 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001015 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001016 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001017
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001018 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001019 // no policy defined cycles; show entry for each four-week period
1020 long cycleEnd = historyEnd;
1021 while (cycleEnd > historyStart) {
1022 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
1023 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1024 cycleEnd = cycleStart;
1025 }
1026
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001027 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001028 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001029
1030 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001031 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001032 final int position = mCycleAdapter.findNearestPosition(previousItem);
1033 mCycleSpinner.setSelection(position);
1034
1035 // only force-update cycle when changed; skipping preserves any
1036 // user-defined inspection region.
1037 final CycleItem selectedItem = mCycleAdapter.getItem(position);
1038 if (!Objects.equal(selectedItem, previousItem)) {
1039 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1040 } else {
1041 // but still kick off loader for detailed list
1042 updateDetailData();
1043 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001044 } else {
1045 updateDetailData();
1046 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001047 }
1048
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001049 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001050 @Override
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001051 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
1052 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001053
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001054 final boolean dataEnabled = isChecked;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001055 final String currentTab = mCurrentTab;
1056 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001057 if (dataEnabled) {
1058 setMobileDataEnabled(true);
1059 } else {
1060 // disabling data; show confirmation dialog which eventually
1061 // calls setMobileDataEnabled() once user confirms.
1062 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1063 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001064 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001065
Jeff Sharkey28130d92011-09-02 16:10:24 -07001066 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001067 }
1068 };
1069
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001070 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001071 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001072 public void onClick(View v) {
1073 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001074 if (disableAtLimit) {
1075 // enabling limit; show confirmation dialog which eventually
1076 // calls setPolicyLimitBytes() once user confirms.
1077 ConfirmLimitFragment.show(DataUsageSummary.this);
1078 } else {
1079 setPolicyLimitBytes(LIMIT_DISABLED);
1080 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001081 }
1082 };
1083
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001084 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001085 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001086 public void onClick(View v) {
1087 final boolean restrictBackground = !mAppRestrict.isChecked();
1088
1089 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001090 // enabling restriction; show confirmation dialog which
1091 // eventually calls setRestrictBackground() once user
1092 // confirms.
1093 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001094 } else {
1095 setAppRestrictBackground(false);
1096 }
1097 }
1098 };
1099
1100 private OnClickListener mAppSettingsListener = new OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001101 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001102 public void onClick(View v) {
Jeff Sharkeyd92e0412012-04-24 11:35:43 -07001103 if (!isAdded()) return;
1104
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001105 // TODO: target torwards entire UID instead of just first package
1106 startActivity(mAppSettingsIntent);
1107 }
1108 };
1109
Jeff Sharkey8a503642011-06-10 13:31:21 -07001110 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001111 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001112 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001113 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001114 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001115
1116 // TODO: sigh, remove this hack once we understand 6450986
1117 if (mUidDetailProvider == null || app == null) return;
1118
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001119 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001120 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001121 }
1122 };
1123
1124 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001125 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001126 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1127 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1128 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001129 // show cycle editor; will eventually call setPolicyCycleDay()
1130 // when user finishes editing.
1131 CycleEditorFragment.show(DataUsageSummary.this);
1132
1133 // reset spinner to something other than "change cycle..."
1134 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001135
1136 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001137 if (LOGD) {
1138 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1139 + cycle.end + "]");
1140 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001141
1142 // update chart to show selected cycle, and update detail data
1143 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001144 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001145
1146 updateDetailData();
1147 }
1148 }
1149
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001150 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001151 public void onNothingSelected(AdapterView<?> parent) {
1152 // ignored
1153 }
1154 };
1155
1156 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001157 * Update details based on {@link #mChart} inspection range depending on
1158 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1159 * of applications data usage, and when {@link #isAppDetailMode()} update
1160 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001161 */
1162 private void updateDetailData() {
1163 if (LOGD) Log.d(TAG, "updateDetailData()");
1164
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001165 final long start = mChart.getInspectStart();
1166 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001167 final long now = System.currentTimeMillis();
1168
1169 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001170
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001171 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001172 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001173 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001174 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001175 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001176 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001177 final long foregroundBytes = entry.rxBytes + entry.txBytes;
1178
1179 mAppPieChart.setOriginAngle(175);
1180
1181 mAppPieChart.removeAllSlices();
Fabrice Di Meglio6ebcc362014-06-18 12:49:35 -07001182 mAppPieChart.addSlice(foregroundBytes, FOREGROUND_BYTES_COLOR);
1183 mAppPieChart.addSlice(defaultBytes, DEFAULT_BYTES_COLOR);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001184
1185 mAppPieChart.generatePath();
1186
1187 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1188 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1189
1190 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001191 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001192
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001193 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001194
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001195 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001196 if (mChartData != null) {
1197 entry = mChartData.network.getValues(start, end, now, null);
1198 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001199
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001200 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001201 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001202 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001203 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001204
1205 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1206 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001207 final String rangePhrase = formatDateRange(context, start, end);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001208
Jeff Sharkeye557c332012-04-13 16:04:07 -07001209 final int summaryRes;
László Dávid0b8bf4e2012-10-24 23:31:47 +02001210 if (TAB_MOBILE.equals(mCurrentTab) || TAB_3G.equals(mCurrentTab)
1211 || TAB_4G.equals(mCurrentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001212 summaryRes = R.string.data_usage_total_during_range_mobile;
1213 } else {
1214 summaryRes = R.string.data_usage_total_during_range;
1215 }
1216
1217 mUsageSummary.setText(getString(summaryRes, totalPhrase, rangePhrase));
Jeff Sharkey92811822012-05-04 11:47:29 -07001218
1219 // initial layout is finished above, ensure we have transitions
1220 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001221 }
1222
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001223 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1224 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001225 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001226 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001227 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001228 }
1229
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001230 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001231 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1232 mChartData = data;
1233 mChart.bindNetworkStats(mChartData.network);
1234 mChart.bindDetailNetworkStats(mChartData.detail);
1235
1236 // calcuate policy cycles based on available data
1237 updatePolicy(true);
1238 updateAppDetail();
1239
1240 // force scroll to top of body when showing detail
1241 if (mChartData.detail != null) {
1242 mListView.smoothScrollToPosition(0);
1243 }
1244 }
1245
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001246 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001247 public void onLoaderReset(Loader<ChartData> loader) {
1248 mChartData = null;
1249 mChart.bindNetworkStats(null);
1250 mChart.bindDetailNetworkStats(null);
1251 }
1252 };
1253
1254 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001255 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001256 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001257 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001258 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001259 }
1260
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001261 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001262 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001263 final int[] restrictedUids = mPolicyManager.getUidsWithPolicy(
Jeff Sharkeye557c332012-04-13 16:04:07 -07001264 POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001265 mAdapter.bindStats(data, restrictedUids);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001266 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001267 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001268
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001269 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001270 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001271 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001272 updateEmptyVisible();
1273 }
1274
1275 private void updateEmptyVisible() {
1276 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1277 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001278 }
1279 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001280
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001281 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001282 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001283 final Context context = getActivity();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001284 if (hasReadyMobileRadio(context)) {
1285 final TelephonyManager tele = TelephonyManager.from(context);
1286 return mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001287 } else {
1288 return false;
1289 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001290 }
1291
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001292 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001293 private void setMobilePolicySplit(boolean split) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001294 final Context context = getActivity();
1295 if (hasReadyMobileRadio(context)) {
1296 final TelephonyManager tele = TelephonyManager.from(context);
1297 mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split);
1298 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001299 }
1300
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001301 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001302 final TelephonyManager tele = TelephonyManager.from(context);
1303 final String actualSubscriberId = tele.getSubscriberId();
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -08001304 return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001305 }
1306
Jeff Sharkey8a503642011-06-10 13:31:21 -07001307 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001308 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001309 public void onInspectRangeChanged() {
1310 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
1311 updateDetailData();
1312 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001313
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001314 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001315 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001316 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001317 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001318
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001319 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001320 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001321 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001322 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001323
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001324 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001325 public void requestWarningEdit() {
1326 WarningEditorFragment.show(DataUsageSummary.this);
1327 }
1328
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001329 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001330 public void requestLimitEdit() {
1331 LimitEditorFragment.show(DataUsageSummary.this);
1332 }
1333 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001334
1335 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001336 * List item that reflects a specific data usage cycle.
1337 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001338 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001339 public CharSequence label;
1340 public long start;
1341 public long end;
1342
Jeff Sharkey8a503642011-06-10 13:31:21 -07001343 CycleItem(CharSequence label) {
1344 this.label = label;
1345 }
1346
1347 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001348 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001349 this.start = start;
1350 this.end = end;
1351 }
1352
Jeff Sharkey8a503642011-06-10 13:31:21 -07001353 @Override
1354 public String toString() {
1355 return label.toString();
1356 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001357
1358 @Override
1359 public boolean equals(Object o) {
1360 if (o instanceof CycleItem) {
1361 final CycleItem another = (CycleItem) o;
1362 return start == another.start && end == another.end;
1363 }
1364 return false;
1365 }
1366
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001367 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001368 public int compareTo(CycleItem another) {
1369 return Long.compare(start, another.start);
1370 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001371 }
1372
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001373 private static final StringBuilder sBuilder = new StringBuilder(50);
1374 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1375 sBuilder, Locale.getDefault());
1376
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001377 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001378 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001379
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001380 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001381 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001382 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1383 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001384 }
1385 }
1386
Jeff Sharkey8a503642011-06-10 13:31:21 -07001387 /**
1388 * Special-case data usage cycle that triggers dialog to change
1389 * {@link NetworkPolicy#cycleDay}.
1390 */
1391 public static class CycleChangeItem extends CycleItem {
1392 public CycleChangeItem(Context context) {
1393 super(context.getString(R.string.data_usage_change_cycle));
1394 }
1395 }
1396
1397 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001398 private boolean mChangePossible = false;
1399 private boolean mChangeVisible = false;
1400
1401 private final CycleChangeItem mChangeItem;
1402
Jeff Sharkey8a503642011-06-10 13:31:21 -07001403 public CycleAdapter(Context context) {
1404 super(context, android.R.layout.simple_spinner_item);
1405 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001406 mChangeItem = new CycleChangeItem(context);
1407 }
1408
1409 public void setChangePossible(boolean possible) {
1410 mChangePossible = possible;
1411 updateChange();
1412 }
1413
1414 public void setChangeVisible(boolean visible) {
1415 mChangeVisible = visible;
1416 updateChange();
1417 }
1418
1419 private void updateChange() {
1420 remove(mChangeItem);
1421 if (mChangePossible && mChangeVisible) {
1422 add(mChangeItem);
1423 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001424 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001425
1426 /**
1427 * Find position of {@link CycleItem} in this adapter which is nearest
1428 * the given {@link CycleItem}.
1429 */
1430 public int findNearestPosition(CycleItem target) {
1431 if (target != null) {
1432 final int count = getCount();
1433 for (int i = count - 1; i >= 0; i--) {
1434 final CycleItem item = getItem(i);
1435 if (item instanceof CycleChangeItem) {
1436 continue;
1437 } else if (item.compareTo(target) >= 0) {
1438 return i;
1439 }
1440 }
1441 }
1442 return 0;
1443 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001444 }
1445
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001446 public static class AppItem implements Comparable<AppItem>, Parcelable {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001447 public final int key;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001448 public boolean restricted;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001449 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001450 public long total;
1451
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001452 public AppItem(int key) {
1453 this.key = key;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001454 }
1455
1456 public AppItem(Parcel parcel) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001457 key = parcel.readInt();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001458 uids = parcel.readSparseBooleanArray();
1459 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001460 }
1461
1462 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001463 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001464 }
1465
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001466 @Override
1467 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001468 dest.writeInt(key);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001469 dest.writeSparseBooleanArray(uids);
1470 dest.writeLong(total);
1471 }
1472
1473 @Override
1474 public int describeContents() {
1475 return 0;
1476 }
1477
1478 @Override
1479 public int compareTo(AppItem another) {
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001480 return Long.compare(another.total, total);
1481 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001482
1483 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1484 @Override
1485 public AppItem createFromParcel(Parcel in) {
1486 return new AppItem(in);
1487 }
1488
1489 @Override
1490 public AppItem[] newArray(int size) {
1491 return new AppItem[size];
1492 }
1493 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001494 }
1495
Jeff Sharkey8a503642011-06-10 13:31:21 -07001496 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001497 * Adapter of applications, sorted by total usage descending.
1498 */
1499 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001500 private final UidDetailProvider mProvider;
1501 private final int mInsetSide;
1502
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001503 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001504 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001505
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001506 public DataUsageAdapter(UidDetailProvider provider, int insetSide) {
1507 mProvider = checkNotNull(provider);
1508 mInsetSide = insetSide;
1509 }
1510
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001511 /**
1512 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1513 */
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001514 public void bindStats(NetworkStats stats, int[] restrictedUids) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001515 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001516
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001517 final int currentUserId = ActivityManager.getCurrentUser();
1518 final SparseArray<AppItem> knownItems = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001519
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001520 NetworkStats.Entry entry = null;
1521 final int size = stats != null ? stats.size() : 0;
1522 for (int i = 0; i < size; i++) {
1523 entry = stats.getValues(i, entry);
1524
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001525 // Decide how to collapse items together
1526 final int uid = entry.uid;
1527 final int collapseKey;
1528 if (UserHandle.isApp(uid)) {
1529 if (UserHandle.getUserId(uid) == currentUserId) {
1530 collapseKey = uid;
1531 } else {
1532 collapseKey = UidDetailProvider.buildKeyForUser(UserHandle.getUserId(uid));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001533 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001534 } else if (uid == UID_REMOVED || uid == UID_TETHERING) {
1535 collapseKey = uid;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001536 } else {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001537 collapseKey = android.os.Process.SYSTEM_UID;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001538 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001539
1540 AppItem item = knownItems.get(collapseKey);
1541 if (item == null) {
1542 item = new AppItem(collapseKey);
1543 mItems.add(item);
1544 knownItems.put(item.key, item);
1545 }
1546 item.addUid(uid);
1547 item.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001548 }
1549
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001550 for (int uid : restrictedUids) {
1551 // Only splice in restricted state for current user
1552 if (UserHandle.getUserId(uid) != currentUserId) continue;
1553
1554 AppItem item = knownItems.get(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001555 if (item == null) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001556 item = new AppItem(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001557 item.total = -1;
1558 mItems.add(item);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001559 knownItems.put(item.key, item);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001560 }
1561 item.restricted = true;
1562 }
1563
Jeff Sharkey8a503642011-06-10 13:31:21 -07001564 Collections.sort(mItems);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001565 mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001566 notifyDataSetChanged();
1567 }
1568
1569 @Override
1570 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001571 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001572 }
1573
1574 @Override
1575 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001576 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001577 }
1578
1579 @Override
1580 public long getItemId(int position) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001581 return mItems.get(position).key;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001582 }
1583
1584 @Override
1585 public View getView(int position, View convertView, ViewGroup parent) {
1586 if (convertView == null) {
1587 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey5d706792011-09-08 18:57:17 -07001588 R.layout.data_usage_item, parent, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001589
1590 if (mInsetSide > 0) {
Fabrice Di Megliob27223f2013-01-15 18:54:11 -08001591 convertView.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001592 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001593 }
1594
1595 final Context context = parent.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001596
Jeff Sharkey28130d92011-09-02 16:10:24 -07001597 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001598 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1599 android.R.id.progress);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001600
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001601 // kick off async load of app details
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001602 final AppItem item = mItems.get(position);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001603 UidDetailTask.bindView(mProvider, item, convertView);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001604
Jeff Sharkeye557c332012-04-13 16:04:07 -07001605 if (item.restricted && item.total <= 0) {
1606 text1.setText(R.string.data_usage_app_restricted);
1607 progress.setVisibility(View.GONE);
1608 } else {
1609 text1.setText(Formatter.formatFileSize(context, item.total));
1610 progress.setVisibility(View.VISIBLE);
1611 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001612
1613 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1614 progress.setProgress(percentTotal);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001615
1616 return convertView;
1617 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001618 }
1619
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001620 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001621 * Empty {@link Fragment} that controls display of UID details in
1622 * {@link DataUsageSummary}.
1623 */
1624 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001625 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001626
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001627 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001628 if (!parent.isAdded()) return;
1629
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001630 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001631 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001632
1633 final AppDetailsFragment fragment = new AppDetailsFragment();
1634 fragment.setArguments(args);
1635 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001636 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1637 ft.add(fragment, TAG_APP_DETAILS);
1638 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001639 ft.setBreadCrumbTitle(label);
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001640 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001641 }
1642
1643 @Override
1644 public void onStart() {
1645 super.onStart();
1646 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001647 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001648 target.updateBody();
1649 }
1650
1651 @Override
1652 public void onStop() {
1653 super.onStop();
1654 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001655 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001656 target.updateBody();
1657 }
1658 }
1659
1660 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001661 * Dialog to request user confirmation before setting
1662 * {@link NetworkPolicy#limitBytes}.
1663 */
1664 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001665 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001666 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001667
1668 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001669 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001670
Jeff Sharkey461842a2011-09-25 18:22:48 -07001671 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001672 final CharSequence message;
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001673 final long minLimitBytes = (long) (
1674 parent.mPolicyEditor.getPolicy(parent.mTemplate).warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001675 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001676
1677 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001678 final String currentTab = parent.mCurrentTab;
1679 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001680 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001681 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001682 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001683 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001684 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001685 } else if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001686 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001687 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001688 } else {
1689 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001690 }
1691
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001692 final Bundle args = new Bundle();
1693 args.putCharSequence(EXTRA_MESSAGE, message);
1694 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1695
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001696 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1697 dialog.setArguments(args);
1698 dialog.setTargetFragment(parent, 0);
1699 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1700 }
1701
1702 @Override
1703 public Dialog onCreateDialog(Bundle savedInstanceState) {
1704 final Context context = getActivity();
1705
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001706 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001707 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1708
1709 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1710 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001711 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001712
1713 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001714 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001715 public void onClick(DialogInterface dialog, int which) {
1716 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1717 if (target != null) {
1718 target.setPolicyLimitBytes(limitBytes);
1719 }
1720 }
1721 });
1722
1723 return builder.create();
1724 }
1725 }
1726
1727 /**
1728 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1729 */
1730 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001731 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001732
1733 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001734 if (!parent.isAdded()) return;
1735
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001736 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001737 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001738
1739 final CycleEditorFragment dialog = new CycleEditorFragment();
1740 dialog.setArguments(args);
1741 dialog.setTargetFragment(parent, 0);
1742 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1743 }
1744
1745 @Override
1746 public Dialog onCreateDialog(Bundle savedInstanceState) {
1747 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001748 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1749 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001750
1751 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1752 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1753
1754 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1755 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1756
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001757 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1758 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001759
1760 cycleDayPicker.setMinValue(1);
1761 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001762 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001763 cycleDayPicker.setWrapSelectorWheel(true);
1764
1765 builder.setTitle(R.string.data_usage_cycle_editor_title);
1766 builder.setView(view);
1767
1768 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1769 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001770 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001771 public void onClick(DialogInterface dialog, int which) {
Jeff Sharkeyd277de92013-03-25 15:11:25 -07001772 // clear focus to finish pending text edits
1773 cycleDayPicker.clearFocus();
1774
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001775 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001776 final String cycleTimezone = new Time().timezone;
1777 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001778 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001779 }
1780 });
1781
1782 return builder.create();
1783 }
1784 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001785
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001786 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001787 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1788 */
1789 public static class WarningEditorFragment extends DialogFragment {
1790 private static final String EXTRA_TEMPLATE = "template";
1791
1792 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001793 if (!parent.isAdded()) return;
1794
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001795 final Bundle args = new Bundle();
1796 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1797
1798 final WarningEditorFragment dialog = new WarningEditorFragment();
1799 dialog.setArguments(args);
1800 dialog.setTargetFragment(parent, 0);
1801 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1802 }
1803
1804 @Override
1805 public Dialog onCreateDialog(Bundle savedInstanceState) {
1806 final Context context = getActivity();
1807 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1808 final NetworkPolicyEditor editor = target.mPolicyEditor;
1809
1810 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1811 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1812
1813 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1814 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1815
1816 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1817 final long warningBytes = editor.getPolicyWarningBytes(template);
1818 final long limitBytes = editor.getPolicyLimitBytes(template);
1819
1820 bytesPicker.setMinValue(0);
1821 if (limitBytes != LIMIT_DISABLED) {
1822 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1823 } else {
1824 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1825 }
1826 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1827 bytesPicker.setWrapSelectorWheel(false);
1828
1829 builder.setTitle(R.string.data_usage_warning_editor_title);
1830 builder.setView(view);
1831
1832 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1833 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001834 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001835 public void onClick(DialogInterface dialog, int which) {
1836 // clear focus to finish pending text edits
1837 bytesPicker.clearFocus();
1838
1839 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1840 editor.setPolicyWarningBytes(template, bytes);
1841 target.updatePolicy(false);
1842 }
1843 });
1844
1845 return builder.create();
1846 }
1847 }
1848
1849 /**
1850 * Dialog to edit {@link NetworkPolicy#limitBytes}.
1851 */
1852 public static class LimitEditorFragment extends DialogFragment {
1853 private static final String EXTRA_TEMPLATE = "template";
1854
1855 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001856 if (!parent.isAdded()) return;
1857
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001858 final Bundle args = new Bundle();
1859 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1860
1861 final LimitEditorFragment dialog = new LimitEditorFragment();
1862 dialog.setArguments(args);
1863 dialog.setTargetFragment(parent, 0);
1864 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
1865 }
1866
1867 @Override
1868 public Dialog onCreateDialog(Bundle savedInstanceState) {
1869 final Context context = getActivity();
1870 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1871 final NetworkPolicyEditor editor = target.mPolicyEditor;
1872
1873 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1874 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1875
1876 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1877 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1878
1879 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1880 final long warningBytes = editor.getPolicyWarningBytes(template);
1881 final long limitBytes = editor.getPolicyLimitBytes(template);
1882
1883 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09001884 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001885 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
1886 } else {
1887 bytesPicker.setMinValue(0);
1888 }
1889 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
1890 bytesPicker.setWrapSelectorWheel(false);
1891
1892 builder.setTitle(R.string.data_usage_limit_editor_title);
1893 builder.setView(view);
1894
1895 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1896 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001897 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001898 public void onClick(DialogInterface dialog, int which) {
1899 // clear focus to finish pending text edits
1900 bytesPicker.clearFocus();
1901
1902 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1903 editor.setPolicyLimitBytes(template, bytes);
1904 target.updatePolicy(false);
1905 }
1906 });
1907
1908 return builder.create();
1909 }
1910 }
1911 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07001912 * Dialog to request user confirmation before disabling data.
1913 */
1914 public static class ConfirmDataDisableFragment extends DialogFragment {
1915 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001916 if (!parent.isAdded()) return;
1917
Jeff Sharkey28130d92011-09-02 16:10:24 -07001918 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
1919 dialog.setTargetFragment(parent, 0);
1920 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
1921 }
1922
1923 @Override
1924 public Dialog onCreateDialog(Bundle savedInstanceState) {
1925 final Context context = getActivity();
1926
1927 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1928 builder.setMessage(R.string.data_usage_disable_mobile);
1929
1930 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001931 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07001932 public void onClick(DialogInterface dialog, int which) {
1933 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1934 if (target != null) {
1935 // TODO: extend to modify policy enabled flag.
1936 target.setMobileDataEnabled(false);
1937 }
1938 }
1939 });
1940 builder.setNegativeButton(android.R.string.cancel, null);
1941
1942 return builder.create();
1943 }
1944 }
1945
1946 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001947 * Dialog to request user confirmation before setting
Christopher Tate5a64c732012-09-07 13:35:31 -07001948 * {@link android.provider.Settings.Global#DATA_ROAMING}.
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001949 */
1950 public static class ConfirmDataRoamingFragment extends DialogFragment {
1951 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001952 if (!parent.isAdded()) return;
1953
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001954 final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment();
1955 dialog.setTargetFragment(parent, 0);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001956 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_ROAMING);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001957 }
1958
1959 @Override
1960 public Dialog onCreateDialog(Bundle savedInstanceState) {
1961 final Context context = getActivity();
1962
1963 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1964 builder.setTitle(R.string.roaming_reenable_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07001965 if (Utils.hasMultipleUsers(context)) {
1966 builder.setMessage(R.string.roaming_warning_multiuser);
1967 } else {
1968 builder.setMessage(R.string.roaming_warning);
1969 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001970
1971 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001972 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001973 public void onClick(DialogInterface dialog, int which) {
1974 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1975 if (target != null) {
1976 target.setDataRoaming(true);
1977 }
1978 }
1979 });
1980 builder.setNegativeButton(android.R.string.cancel, null);
1981
1982 return builder.create();
1983 }
1984 }
1985
1986 /**
1987 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001988 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001989 */
1990 public static class ConfirmRestrictFragment extends DialogFragment {
1991 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001992 if (!parent.isAdded()) return;
1993
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001994 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1995 dialog.setTargetFragment(parent, 0);
1996 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1997 }
1998
1999 @Override
2000 public Dialog onCreateDialog(Bundle savedInstanceState) {
2001 final Context context = getActivity();
2002
2003 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002004 builder.setTitle(R.string.data_usage_restrict_background_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07002005 if (Utils.hasMultipleUsers(context)) {
2006 builder.setMessage(R.string.data_usage_restrict_background_multiuser);
2007 } else {
2008 builder.setMessage(R.string.data_usage_restrict_background);
2009 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002010
2011 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002012 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002013 public void onClick(DialogInterface dialog, int which) {
2014 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2015 if (target != null) {
2016 target.setRestrictBackground(true);
2017 }
2018 }
2019 });
2020 builder.setNegativeButton(android.R.string.cancel, null);
2021
2022 return builder.create();
2023 }
2024 }
2025
2026 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002027 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
2028 * change has been denied, usually based on
2029 * {@link DataUsageSummary#hasLimitedNetworks()}.
2030 */
2031 public static class DeniedRestrictFragment extends DialogFragment {
2032 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002033 if (!parent.isAdded()) return;
2034
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002035 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
2036 dialog.setTargetFragment(parent, 0);
2037 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
2038 }
2039
2040 @Override
2041 public Dialog onCreateDialog(Bundle savedInstanceState) {
2042 final Context context = getActivity();
2043
2044 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2045 builder.setTitle(R.string.data_usage_app_restrict_background);
2046 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
2047 builder.setPositiveButton(android.R.string.ok, null);
2048
2049 return builder.create();
2050 }
2051 }
2052
2053 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002054 * Dialog to request user confirmation before setting
2055 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
2056 */
2057 public static class ConfirmAppRestrictFragment extends DialogFragment {
2058 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002059 if (!parent.isAdded()) return;
2060
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002061 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2062 dialog.setTargetFragment(parent, 0);
2063 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2064 }
2065
2066 @Override
2067 public Dialog onCreateDialog(Bundle savedInstanceState) {
2068 final Context context = getActivity();
2069
2070 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002071 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2072 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2073
2074 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002075 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002076 public void onClick(DialogInterface dialog, int which) {
2077 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2078 if (target != null) {
2079 target.setAppRestrictBackground(true);
2080 }
2081 }
2082 });
2083 builder.setNegativeButton(android.R.string.cancel, null);
2084
2085 return builder.create();
2086 }
2087 }
2088
2089 /**
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002090 * Dialog to inform user about changing auto-sync setting
2091 */
2092 public static class ConfirmAutoSyncChangeFragment extends DialogFragment {
Amith Yamasani665235f2012-06-07 19:58:34 -07002093 private static final String SAVE_ENABLING = "enabling";
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002094 private boolean mEnabling;
2095
2096 public static void show(DataUsageSummary parent, boolean enabling) {
2097 if (!parent.isAdded()) return;
2098
2099 final ConfirmAutoSyncChangeFragment dialog = new ConfirmAutoSyncChangeFragment();
2100 dialog.mEnabling = enabling;
2101 dialog.setTargetFragment(parent, 0);
2102 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_AUTO_SYNC_CHANGE);
2103 }
2104
2105 @Override
2106 public Dialog onCreateDialog(Bundle savedInstanceState) {
2107 final Context context = getActivity();
Amith Yamasani665235f2012-06-07 19:58:34 -07002108 if (savedInstanceState != null) {
2109 mEnabling = savedInstanceState.getBoolean(SAVE_ENABLING);
2110 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002111
2112 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2113 if (!mEnabling) {
2114 builder.setTitle(R.string.data_usage_auto_sync_off_dialog_title);
2115 builder.setMessage(R.string.data_usage_auto_sync_off_dialog);
2116 } else {
2117 builder.setTitle(R.string.data_usage_auto_sync_on_dialog_title);
2118 builder.setMessage(R.string.data_usage_auto_sync_on_dialog);
2119 }
2120
2121 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
2122 @Override
2123 public void onClick(DialogInterface dialog, int which) {
2124 ContentResolver.setMasterSyncAutomatically(mEnabling);
2125 }
2126 });
2127 builder.setNegativeButton(android.R.string.cancel, null);
2128
2129 return builder.create();
2130 }
Amith Yamasani665235f2012-06-07 19:58:34 -07002131
2132 @Override
2133 public void onSaveInstanceState(Bundle outState) {
2134 super.onSaveInstanceState(outState);
2135 outState.putBoolean(SAVE_ENABLING, mEnabling);
2136 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002137 }
2138
2139 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002140 * Compute default tab that should be selected, based on
2141 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2142 */
2143 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002144 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
2145 if (template == null) return null;
2146
2147 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002148 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002149 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002150 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002151 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002152 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002153 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002154 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002155 return TAB_WIFI;
2156 default:
2157 return null;
2158 }
2159 }
2160
2161 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002162 * Background task that loads {@link UidDetail}, binding to
2163 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002164 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002165 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2166 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002167 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002168 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002169
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002170 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002171 mProvider = checkNotNull(provider);
2172 mItem = checkNotNull(item);
2173 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002174 }
2175
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002176 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002177 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002178 final UidDetailTask existing = (UidDetailTask) target.getTag();
2179 if (existing != null) {
2180 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002181 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002182
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002183 final UidDetail cachedDetail = provider.getUidDetail(item.key, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002184 if (cachedDetail != null) {
2185 bindView(cachedDetail, target);
2186 } else {
2187 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2188 AsyncTask.THREAD_POOL_EXECUTOR));
2189 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002190 }
2191
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002192 private static void bindView(UidDetail detail, View target) {
2193 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2194 final TextView title = (TextView) target.findViewById(android.R.id.title);
2195
2196 if (detail != null) {
2197 icon.setImageDrawable(detail.icon);
2198 title.setText(detail.label);
2199 } else {
2200 icon.setImageDrawable(null);
2201 title.setText(null);
2202 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002203 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002204
2205 @Override
2206 protected void onPreExecute() {
2207 bindView(null, mTarget);
2208 }
2209
2210 @Override
2211 protected UidDetail doInBackground(Void... params) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002212 return mProvider.getUidDetail(mItem.key, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002213 }
2214
2215 @Override
2216 protected void onPostExecute(UidDetail result) {
2217 bindView(result, mTarget);
2218 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002219 }
2220
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002221 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002222 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002223 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002224 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002225 if (TEST_RADIOS) {
2226 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2227 }
2228
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002229 final ConnectivityManager conn = ConnectivityManager.from(context);
2230 final TelephonyManager tele = TelephonyManager.from(context);
2231
2232 // require both supported network and ready SIM
2233 return conn.isNetworkSupported(TYPE_MOBILE) && tele.getSimState() == SIM_STATE_READY;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002234 }
2235
2236 /**
2237 * Test if device has a mobile 4G data radio.
2238 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002239 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002240 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2241 return false;
2242 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002243 if (TEST_RADIOS) {
2244 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2245 }
2246
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002247 final ConnectivityManager conn = ConnectivityManager.from(context);
2248 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002249
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002250 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Wink Saville55434042012-06-14 12:33:43 -07002251 final boolean hasLte = (tele.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE)
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002252 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002253 return hasWimax || hasLte;
2254 }
2255
2256 /**
2257 * Test if device has a Wi-Fi data radio.
2258 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002259 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002260 if (TEST_RADIOS) {
2261 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2262 }
2263
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002264 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002265 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002266 }
2267
2268 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002269 * Test if device has an ethernet network connection.
2270 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002271 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002272 if (TEST_RADIOS) {
2273 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2274 }
2275
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002276 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002277 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2278
2279 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002280 if (mStatsSession != null) {
2281 try {
2282 ethernetBytes = mStatsSession.getSummaryForNetwork(
2283 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2284 .getTotalBytes();
2285 } catch (RemoteException e) {
2286 throw new RuntimeException(e);
2287 }
2288 } else {
2289 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002290 }
2291
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002292 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002293 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002294 }
2295
2296 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002297 * Inflate a {@link Preference} style layout, adding the given {@link View}
2298 * widget into {@link android.R.id#widget_frame}.
2299 */
2300 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2301 final View view = inflater.inflate(R.layout.preference, root, false);
2302 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2303 android.R.id.widget_frame);
2304 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2305 return view;
2306 }
2307
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002308 private static View inflateAppTitle(
2309 LayoutInflater inflater, ViewGroup root, CharSequence label) {
2310 final TextView view = (TextView) inflater.inflate(
2311 R.layout.data_usage_app_title, root, false);
2312 view.setText(label);
2313 return view;
2314 }
2315
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002316 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002317 * Test if any networks are currently limited.
2318 */
2319 private boolean hasLimitedNetworks() {
2320 return !buildLimitedNetworksList().isEmpty();
2321 }
2322
2323 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002324 * Build string describing currently limited networks, which defines when
2325 * background data is restricted.
2326 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002327 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002328 private CharSequence buildLimitedNetworksString() {
2329 final List<CharSequence> limited = buildLimitedNetworksList();
2330
2331 // handle case where no networks limited
2332 if (limited.isEmpty()) {
2333 limited.add(getText(R.string.data_usage_list_none));
2334 }
2335
2336 return TextUtils.join(limited);
2337 }
2338
2339 /**
2340 * Build list of currently limited networks, which defines when background
2341 * data is restricted.
2342 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002343 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002344 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002345 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002346
2347 // build combined list of all limited networks
2348 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002349
2350 final TelephonyManager tele = TelephonyManager.from(context);
2351 if (tele.getSimState() == SIM_STATE_READY) {
2352 final String subscriberId = getActiveSubscriberId(context);
2353 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2354 limited.add(getText(R.string.data_usage_list_mobile));
2355 }
2356 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2357 limited.add(getText(R.string.data_usage_tab_3g));
2358 }
2359 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2360 limited.add(getText(R.string.data_usage_tab_4g));
2361 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002362 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002363
2364 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002365 limited.add(getText(R.string.data_usage_tab_wifi));
2366 }
2367 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2368 limited.add(getText(R.string.data_usage_tab_ethernet));
2369 }
2370
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002371 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002372 }
2373
2374 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002375 * Inset both selector and divider {@link Drawable} on the given
2376 * {@link ListView} by the requested dimensions.
2377 */
2378 private static void insetListViewDrawables(ListView view, int insetSide) {
2379 final Drawable selector = view.getSelector();
2380 final Drawable divider = view.getDivider();
2381
2382 // fully unregister these drawables so callbacks can be maintained after
2383 // wrapping below.
2384 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2385 view.setSelector(stub);
2386 view.setDivider(stub);
2387
2388 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2389 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2390 }
2391
2392 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002393 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002394 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002395 */
2396 private static void setPreferenceTitle(View parent, int resId) {
2397 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2398 title.setText(resId);
2399 }
2400
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002401 /**
2402 * Set {@link android.R.id#summary} for a preference view inflated with
2403 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2404 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002405 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002406 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2407 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002408 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002409 }
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002410
2411 /**
2412 * For search
2413 */
2414 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -07002415 new BaseSearchIndexProvider() {
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002416 @Override
2417 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
2418 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
2419
2420 final Resources res = context.getResources();
2421
2422 // Add fragment title
2423 SearchIndexableRaw data = new SearchIndexableRaw(context);
2424 data.title = res.getString(R.string.data_usage_summary_title);
2425 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2426 result.add(data);
2427
2428 // Mobile data
2429 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002430 data.key = DATA_USAGE_ENABLE_MOBILE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002431 data.title = res.getString(R.string.data_usage_enable_mobile);
2432 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2433 result.add(data);
2434
2435 // Set mobile data limit
2436 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002437 data.key = DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002438 data.title = res.getString(R.string.data_usage_disable_mobile_limit);
2439 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2440 result.add(data);
2441
Fabrice Di Megliof2a52262014-04-17 17:20:27 -07002442 // Data usage cycle
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002443 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002444 data.key = DATA_USAGE_CYCLE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002445 data.title = res.getString(R.string.data_usage_cycle);
2446 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2447 result.add(data);
2448
2449 return result;
2450 }
2451 };
2452
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002453}