blob: dbef6cb7851526490f5cec6f754980f9332f9f0b [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 Meglioc70b7f62014-06-19 11:29:26 -0700363 mDataEnabledView.setTag(R.id.preference_highlight_key,
364 DATA_USAGE_ENABLE_MOBILE_KEY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700365 mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener);
366 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700367
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700368 mDisableAtLimit = new CheckBox(inflater.getContext());
369 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700370 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700371 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700372 mDisableAtLimitView.setTag(R.id.preference_highlight_key,
373 DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700374 mDisableAtLimitView.setClickable(true);
375 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700376 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
377 mNetworkSwitches.addView(mDisableAtLimitView);
378 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700379
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700380 // bind cycle dropdown
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700381 mCycleView = mHeader.findViewById(R.id.cycles);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700382 mCycleView.setTag(R.id.preference_highlight_key, DATA_USAGE_CYCLE_KEY);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700383 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700384 mCycleAdapter = new CycleAdapter(context);
385 mCycleSpinner.setAdapter(mCycleAdapter);
386 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
387
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700388 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700389 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800390 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700391
392 {
393 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700394 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700395 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
396 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700397 mAppPieChart = (PieChartView) mAppDetail.findViewById(R.id.app_pie_chart);
398 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
399 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700400 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700401
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700402 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700403 mAppSettings.setOnClickListener(mAppSettingsListener);
404
405 mAppRestrict = new CheckBox(inflater.getContext());
406 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700407 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700408 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700409 mAppRestrictView.setClickable(true);
410 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700411 mAppRestrictView.setOnClickListener(mAppRestrictListener);
412 mAppSwitches.addView(mAppRestrictView);
413 }
414
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700415 mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700416 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700417
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700418 mAdapter = new DataUsageAdapter(mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700419 mListView.setOnItemClickListener(mListListener);
420 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700421
422 return view;
423 }
424
425 @Override
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700426 public void onViewStateRestored(Bundle savedInstanceState) {
427 super.onViewStateRestored(savedInstanceState);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700428
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700429 // pick default tab based on incoming intent
430 final Intent intent = getActivity().getIntent();
431 mIntentTab = computeTabFromIntent(intent);
432
Jeff Sharkey8a503642011-06-10 13:31:21 -0700433 // this kicks off chain reaction which creates tabs, binds the body to
434 // selected network, and binds chart, cycles and detail list.
435 updateTabs();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700436 }
437
438 @Override
439 public void onResume() {
440 super.onResume();
441
442 getView().post(new Runnable() {
443 @Override
444 public void run() {
445 highlightViewIfNeeded();
446 }
447 });
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700448
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700449 // kick off background task to update stats
450 new AsyncTask<Void, Void, Void>() {
451 @Override
452 protected Void doInBackground(Void... params) {
453 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700454 // wait a few seconds before kicking off
455 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700456 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700457 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700458 } catch (RemoteException e) {
459 }
460 return null;
461 }
462
463 @Override
464 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700465 if (isAdded()) {
466 updateBody();
467 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700468 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700469 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700470 }
471
Jeff Sharkey8a503642011-06-10 13:31:21 -0700472 @Override
473 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
474 inflater.inflate(R.menu.data_usage, menu);
475 }
476
477 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700478 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700479 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700480 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700481 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700482
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700483 mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700484 mMenuDataRoaming.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700485 mMenuDataRoaming.setChecked(getDataRoaming());
486
487 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700488 mMenuRestrictBackground.setVisible(
489 hasReadyMobileRadio(context) && isOwner && !appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700490 mMenuRestrictBackground.setChecked(mPolicyManager.getRestrictBackground());
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700491
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700492 mMenuAutoSync = menu.findItem(R.id.data_usage_menu_auto_sync);
493 mMenuAutoSync.setChecked(ContentResolver.getMasterSyncAutomatically());
Jeff Sharkeyfda48e32012-09-18 15:03:47 -0700494 mMenuAutoSync.setVisible(!appDetailMode);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700495
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700496 final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g);
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700497 split4g.setVisible(hasReadyMobile4gRadio(context) && isOwner && !appDetailMode);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700498 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700499
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700500 final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700501 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700502 showWifi.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700503 showWifi.setChecked(mShowWifi);
504 } else {
505 showWifi.setVisible(false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700506 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700507
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700508 final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700509 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700510 showEthernet.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700511 showEthernet.setChecked(mShowEthernet);
512 } else {
513 showEthernet.setVisible(false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700514 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700515
516 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
517 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700518 metered.setVisible(!appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700519 } else {
520 metered.setVisible(false);
521 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700522
523 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
524 String helpUrl;
525 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800526 HelpUtils.prepareHelpMenuItem(context, help, helpUrl);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700527 } else {
528 help.setVisible(false);
529 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700530 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700531
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700532 @Override
533 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700534 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700535 case R.id.data_usage_menu_roaming: {
536 final boolean dataRoaming = !item.isChecked();
537 if (dataRoaming) {
538 ConfirmDataRoamingFragment.show(this);
539 } else {
540 // no confirmation to disable roaming
541 setDataRoaming(false);
542 }
543 return true;
544 }
545 case R.id.data_usage_menu_restrict_background: {
546 final boolean restrictBackground = !item.isChecked();
547 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800548 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700549 } else {
550 // no confirmation to drop restriction
551 setRestrictBackground(false);
552 }
553 return true;
554 }
555 case R.id.data_usage_menu_split_4g: {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700556 final boolean mobileSplit = !item.isChecked();
Jeff Sharkeya662e492011-06-18 21:57:06 -0700557 setMobilePolicySplit(mobileSplit);
558 item.setChecked(isMobilePolicySplit());
Jeff Sharkey8a503642011-06-10 13:31:21 -0700559 updateTabs();
560 return true;
561 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700562 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700563 mShowWifi = !item.isChecked();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700564 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700565 item.setChecked(mShowWifi);
566 updateTabs();
567 return true;
568 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700569 case R.id.data_usage_menu_show_ethernet: {
570 mShowEthernet = !item.isChecked();
571 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
572 item.setChecked(mShowEthernet);
573 updateTabs();
574 return true;
575 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700576 case R.id.data_usage_menu_metered: {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800577 final SettingsActivity sa = (SettingsActivity) getActivity();
578 sa.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700579 R.string.data_usage_metered_title, null, this, 0);
580 return true;
581 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700582 case R.id.data_usage_menu_auto_sync: {
Guang Zhu167ba2a2012-10-19 17:55:05 -0700583 if (ActivityManager.isUserAMonkey()) {
584 Log.d("SyncState", "ignoring monkey's attempt to flip global sync state");
585 } else {
586 ConfirmAutoSyncChangeFragment.show(this, !item.isChecked());
587 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700588 return true;
589 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700590 }
591 return false;
592 }
593
Jeff Sharkey94a90952011-06-13 22:31:09 -0700594 @Override
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700595 public void onDestroy() {
Jeff Sharkey94a90952011-06-13 22:31:09 -0700596 mDataEnabledView = null;
597 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700598
599 mUidDetailProvider.clearCache();
600 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700601
602 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700603
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800604 super.onDestroy();
605 }
606
Jeff Sharkey8a503642011-06-10 13:31:21 -0700607 /**
Jeff Sharkey92811822012-05-04 11:47:29 -0700608 * Build and assign {@link LayoutTransition} to various containers. Should
609 * only be assigned after initial layout is complete.
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700610 */
Jeff Sharkey92811822012-05-04 11:47:29 -0700611 private void ensureLayoutTransitions() {
612 // skip when already setup
613 if (mChart.getLayoutTransition() != null) return;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700614
Jeff Sharkey92811822012-05-04 11:47:29 -0700615 mTabsContainer.setLayoutTransition(buildLayoutTransition());
616 mHeader.setLayoutTransition(buildLayoutTransition());
617 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700618
Jeff Sharkey92811822012-05-04 11:47:29 -0700619 final LayoutTransition chartTransition = buildLayoutTransition();
620 chartTransition.disableTransitionType(LayoutTransition.APPEARING);
621 chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
622 mChart.setLayoutTransition(chartTransition);
623 }
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700624
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700625 private static LayoutTransition buildLayoutTransition() {
626 final LayoutTransition transition = new LayoutTransition();
627 if (TEST_ANIM) {
628 transition.setDuration(1500);
629 }
630 transition.setAnimateParentHierarchy(false);
631 return transition;
632 }
633
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700634 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700635 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700636 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
637 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700638 */
639 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700640 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700641 mTabHost.clearAllTabs();
642
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700643 final boolean mobileSplit = isMobilePolicySplit();
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700644 if (mobileSplit && hasReadyMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700645 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
646 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700647 } else if (hasReadyMobileRadio(context)) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700648 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700649 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700650 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700651 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
652 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700653 if (mShowEthernet && hasEthernet(context)) {
654 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
655 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700656
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700657 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700658 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
659 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
660 if (mIntentTab != null) {
661 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700662 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700663 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700664 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700665 mTabHost.setCurrentTabByTag(mIntentTab);
666 }
667 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700668 } else if (noTabs) {
669 // no usable tabs, so hide body
670 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700671 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700672 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700673 }
674 }
675
Jeff Sharkey8a503642011-06-10 13:31:21 -0700676 /**
677 * Factory that provide empty {@link View} to make {@link TabHost} happy.
678 */
679 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700680 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700681 public View createTabContent(String tag) {
682 return new View(mTabHost.getContext());
683 }
684 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700685
Jeff Sharkey8a503642011-06-10 13:31:21 -0700686 /**
687 * Build {@link TabSpec} with thin indicator, and empty content.
688 */
689 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700690 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
691 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700692 }
693
694 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700695 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700696 public void onTabChanged(String tabId) {
697 // user changed tab; update body
698 updateBody();
699 }
700 };
701
702 /**
703 * Update body content based on current tab. Loads
704 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
705 * binds them to visible controls.
706 */
707 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700708 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700709 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700710
Jeff Sharkeya662e492011-06-18 21:57:06 -0700711 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700712 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700713 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700714
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700715 if (currentTab == null) {
716 Log.w(TAG, "no tab selected; hiding body");
717 mListView.setVisibility(View.GONE);
718 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700719 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700720 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700721 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700722
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700723 mCurrentTab = currentTab;
724
Jeff Sharkey8a503642011-06-10 13:31:21 -0700725 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
726
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700727 mDataEnabledView.setVisibility(isOwner ? View.VISIBLE : View.GONE);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700728
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700729 // TODO: remove mobile tabs when SIM isn't ready
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700730
Jeff Sharkey8a503642011-06-10 13:31:21 -0700731 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700732 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
733 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700734 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700735
736 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700737 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
738 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
739 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700740 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700741
742 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700743 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
744 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
745 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700746 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700747
748 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700749 // wifi doesn't have any controls
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700750 mDataEnabledView.setVisibility(View.GONE);
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700751 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700752 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700753
754 } else if (TAB_ETHERNET.equals(currentTab)) {
755 // ethernet doesn't have any controls
756 mDataEnabledView.setVisibility(View.GONE);
757 mDisableAtLimitView.setVisibility(View.GONE);
758 mTemplate = buildTemplateEthernet();
759
760 } else {
761 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700762 }
763
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700764 // kick off loader for network history
765 // TODO: consider chaining two loaders together instead of reloading
766 // network history when showing app detail.
767 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700768 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700769
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700770 // detail mode can change visible menus, invalidate
771 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700772
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700773 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700774 }
775
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700776 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700777 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700778 }
779
780 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700781 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700782 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700783 */
784 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700785 final Context context = getActivity();
786 final PackageManager pm = context.getPackageManager();
787 final LayoutInflater inflater = getActivity().getLayoutInflater();
788
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700789 if (isAppDetailMode()) {
790 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700791 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700792 } else {
793 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700794 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700795
796 // hide detail stats when not in detail mode
797 mChart.bindDetailNetworkStats(null);
798 return;
799 }
800
801 // remove warning/limit sweeps while in detail mode
802 mChart.bindNetworkPolicy(null);
803
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700804 // show icon and all labels appearing under this app
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700805 final int uid = mCurrentApp.key;
806 final UidDetail detail = mUidDetailProvider.getUidDetail(uid, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700807 mAppIcon.setImageDrawable(detail.icon);
808
809 mAppTitles.removeAllViews();
810 if (detail.detailLabels != null) {
811 for (CharSequence label : detail.detailLabels) {
812 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label));
813 }
814 } else {
815 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label));
816 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700817
818 // enable settings button when package provides it
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700819 final String[] packageNames = pm.getPackagesForUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700820 if (packageNames != null && packageNames.length > 0) {
821 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700822 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
823
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700824 // Search for match across all packages
825 boolean matchFound = false;
826 for (String packageName : packageNames) {
827 mAppSettingsIntent.setPackage(packageName);
828 if (pm.resolveActivity(mAppSettingsIntent, 0) != null) {
829 matchFound = true;
830 break;
831 }
832 }
833
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700834 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -0700835 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700836
837 } else {
838 mAppSettingsIntent = null;
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700839 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700840 }
841
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700842 updateDetailData();
843
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700844 if (UserHandle.isApp(uid) && !mPolicyManager.getRestrictBackground()
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700845 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700846 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800847 setPreferenceSummary(mAppRestrictView,
848 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700849
850 mAppRestrictView.setVisibility(View.VISIBLE);
851 mAppRestrict.setChecked(getAppRestrictBackground());
852
853 } else {
854 mAppRestrictView.setVisibility(View.GONE);
855 }
856 }
857
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700858 private void setPolicyWarningBytes(long warningBytes) {
859 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700860 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700861 updatePolicy(false);
862 }
863
864 private void setPolicyLimitBytes(long limitBytes) {
865 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700866 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700867 updatePolicy(false);
868 }
869
Jeff Sharkey28130d92011-09-02 16:10:24 -0700870 /**
871 * Local cache of value, used to work around delay when
872 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
873 */
874 private Boolean mMobileDataEnabled;
875
876 private boolean isMobileDataEnabled() {
877 if (mMobileDataEnabled != null) {
878 // TODO: deprecate and remove this once enabled flag is on policy
879 return mMobileDataEnabled;
880 } else {
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700881 return mTelephonyManager.getDataEnabled();
Jeff Sharkey28130d92011-09-02 16:10:24 -0700882 }
883 }
884
885 private void setMobileDataEnabled(boolean enabled) {
886 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700887 mTelephonyManager.setDataEnabled(enabled);
Jeff Sharkey28130d92011-09-02 16:10:24 -0700888 mMobileDataEnabled = enabled;
889 updatePolicy(false);
890 }
891
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700892 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700893 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked()
894 && ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700895 }
896
897 private boolean isBandwidthControlEnabled() {
898 try {
899 return mNetworkService.isBandwidthControlEnabled();
900 } catch (RemoteException e) {
901 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
902 return false;
903 }
904 }
905
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700906 private boolean getDataRoaming() {
907 final ContentResolver resolver = getActivity().getContentResolver();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800908 return android.provider.Settings.Global.getInt(resolver,
909 android.provider.Settings.Global.DATA_ROAMING, 0) != 0;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700910 }
911
912 private void setDataRoaming(boolean enabled) {
913 // TODO: teach telephony DataConnectionTracker to watch and apply
914 // updates when changed.
915 final ContentResolver resolver = getActivity().getContentResolver();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800916 android.provider.Settings.Global.putInt(resolver,
917 android.provider.Settings.Global.DATA_ROAMING, enabled ? 1 : 0);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700918 mMenuDataRoaming.setChecked(enabled);
919 }
920
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700921 public void setRestrictBackground(boolean restrictBackground) {
922 mPolicyManager.setRestrictBackground(restrictBackground);
923 mMenuRestrictBackground.setChecked(restrictBackground);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700924 }
925
926 private boolean getAppRestrictBackground() {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700927 final int uid = mCurrentApp.key;
928 final int uidPolicy = mPolicyManager.getUidPolicy(uid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700929 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
930 }
931
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700932 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700933 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700934 final int uid = mCurrentApp.key;
935 mPolicyManager.setUidPolicy(
936 uid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700937 mAppRestrict.setChecked(restrictBackground);
938 }
939
Jeff Sharkey8a503642011-06-10 13:31:21 -0700940 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700941 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
942 * current {@link #mTemplate}.
943 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700944 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700945 if (isAppDetailMode()) {
946 mNetworkSwitches.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700947 } else {
948 mNetworkSwitches.setVisibility(View.VISIBLE);
949 }
950
Jeff Sharkey28130d92011-09-02 16:10:24 -0700951 // TODO: move enabled state directly into policy
952 if (TAB_MOBILE.equals(mCurrentTab)) {
953 mBinding = true;
954 mDataEnabled.setChecked(isMobileDataEnabled());
955 mBinding = false;
956 }
957
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700958 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
959 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700960 mDisableAtLimitView.setVisibility(View.VISIBLE);
961 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700962 if (!isAppDetailMode()) {
963 mChart.bindNetworkPolicy(policy);
964 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700965
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700966 } else {
967 // controls are disabled; don't bind warning/limit sweeps
968 mDisableAtLimitView.setVisibility(View.GONE);
969 mChart.bindNetworkPolicy(null);
970 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700971
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700972 if (refreshCycle) {
973 // generate cycle list based on policy and available history
974 updateCycleList(policy);
975 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700976 }
977
978 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700979 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
980 * and available {@link NetworkStatsHistory} data. Always selects the newest
981 * item, updating the inspection range on {@link #mChart}.
982 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700983 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700984 // stash away currently selected cycle to try restoring below
985 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700986 mCycleAdapter.clear();
987
988 final Context context = mCycleSpinner.getContext();
989
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700990 long historyStart = Long.MAX_VALUE;
991 long historyEnd = Long.MIN_VALUE;
992 if (mChartData != null) {
993 historyStart = mChartData.network.getStart();
994 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700995 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700996
Jeff Sharkey461842a2011-09-25 18:22:48 -0700997 final long now = System.currentTimeMillis();
998 if (historyStart == Long.MAX_VALUE) historyStart = now;
999 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001000
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001001 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001002 if (policy != null) {
1003 // find the next cycle boundary
1004 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001005
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001006 // walk backwards, generating all valid cycle ranges
1007 while (cycleEnd > historyStart) {
1008 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
1009 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
1010 + historyStart);
1011 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1012 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001013 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001014 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001015
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001016 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001017 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001018 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001019
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001020 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001021 // no policy defined cycles; show entry for each four-week period
1022 long cycleEnd = historyEnd;
1023 while (cycleEnd > historyStart) {
1024 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
1025 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1026 cycleEnd = cycleStart;
1027 }
1028
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001029 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001030 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001031
1032 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001033 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001034 final int position = mCycleAdapter.findNearestPosition(previousItem);
1035 mCycleSpinner.setSelection(position);
1036
1037 // only force-update cycle when changed; skipping preserves any
1038 // user-defined inspection region.
1039 final CycleItem selectedItem = mCycleAdapter.getItem(position);
1040 if (!Objects.equal(selectedItem, previousItem)) {
1041 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1042 } else {
1043 // but still kick off loader for detailed list
1044 updateDetailData();
1045 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001046 } else {
1047 updateDetailData();
1048 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001049 }
1050
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001051 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001052 @Override
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001053 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
1054 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001055
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001056 final boolean dataEnabled = isChecked;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001057 final String currentTab = mCurrentTab;
1058 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001059 if (dataEnabled) {
1060 setMobileDataEnabled(true);
1061 } else {
1062 // disabling data; show confirmation dialog which eventually
1063 // calls setMobileDataEnabled() once user confirms.
1064 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1065 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001066 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001067
Jeff Sharkey28130d92011-09-02 16:10:24 -07001068 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001069 }
1070 };
1071
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001072 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001073 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001074 public void onClick(View v) {
1075 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001076 if (disableAtLimit) {
1077 // enabling limit; show confirmation dialog which eventually
1078 // calls setPolicyLimitBytes() once user confirms.
1079 ConfirmLimitFragment.show(DataUsageSummary.this);
1080 } else {
1081 setPolicyLimitBytes(LIMIT_DISABLED);
1082 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001083 }
1084 };
1085
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001086 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001087 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001088 public void onClick(View v) {
1089 final boolean restrictBackground = !mAppRestrict.isChecked();
1090
1091 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001092 // enabling restriction; show confirmation dialog which
1093 // eventually calls setRestrictBackground() once user
1094 // confirms.
1095 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001096 } else {
1097 setAppRestrictBackground(false);
1098 }
1099 }
1100 };
1101
1102 private OnClickListener mAppSettingsListener = new OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001103 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001104 public void onClick(View v) {
Jeff Sharkeyd92e0412012-04-24 11:35:43 -07001105 if (!isAdded()) return;
1106
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001107 // TODO: target torwards entire UID instead of just first package
1108 startActivity(mAppSettingsIntent);
1109 }
1110 };
1111
Jeff Sharkey8a503642011-06-10 13:31:21 -07001112 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001113 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001114 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001115 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001116 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001117
1118 // TODO: sigh, remove this hack once we understand 6450986
1119 if (mUidDetailProvider == null || app == null) return;
1120
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001121 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001122 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001123 }
1124 };
1125
1126 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001127 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001128 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1129 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1130 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001131 // show cycle editor; will eventually call setPolicyCycleDay()
1132 // when user finishes editing.
1133 CycleEditorFragment.show(DataUsageSummary.this);
1134
1135 // reset spinner to something other than "change cycle..."
1136 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001137
1138 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001139 if (LOGD) {
1140 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1141 + cycle.end + "]");
1142 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001143
1144 // update chart to show selected cycle, and update detail data
1145 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001146 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001147
1148 updateDetailData();
1149 }
1150 }
1151
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001152 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001153 public void onNothingSelected(AdapterView<?> parent) {
1154 // ignored
1155 }
1156 };
1157
1158 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001159 * Update details based on {@link #mChart} inspection range depending on
1160 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1161 * of applications data usage, and when {@link #isAppDetailMode()} update
1162 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001163 */
1164 private void updateDetailData() {
1165 if (LOGD) Log.d(TAG, "updateDetailData()");
1166
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001167 final long start = mChart.getInspectStart();
1168 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001169 final long now = System.currentTimeMillis();
1170
1171 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001172
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001173 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001174 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001175 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001176 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001177 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001178 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001179 final long foregroundBytes = entry.rxBytes + entry.txBytes;
1180
1181 mAppPieChart.setOriginAngle(175);
1182
1183 mAppPieChart.removeAllSlices();
Fabrice Di Meglio6ebcc362014-06-18 12:49:35 -07001184 mAppPieChart.addSlice(foregroundBytes, FOREGROUND_BYTES_COLOR);
1185 mAppPieChart.addSlice(defaultBytes, DEFAULT_BYTES_COLOR);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001186
1187 mAppPieChart.generatePath();
1188
1189 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1190 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1191
1192 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001193 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001194
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001195 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001196
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001197 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001198 if (mChartData != null) {
1199 entry = mChartData.network.getValues(start, end, now, null);
1200 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001201
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001202 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001203 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001204 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001205 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001206
1207 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1208 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001209 final String rangePhrase = formatDateRange(context, start, end);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001210
Jeff Sharkeye557c332012-04-13 16:04:07 -07001211 final int summaryRes;
László Dávid0b8bf4e2012-10-24 23:31:47 +02001212 if (TAB_MOBILE.equals(mCurrentTab) || TAB_3G.equals(mCurrentTab)
1213 || TAB_4G.equals(mCurrentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001214 summaryRes = R.string.data_usage_total_during_range_mobile;
1215 } else {
1216 summaryRes = R.string.data_usage_total_during_range;
1217 }
1218
1219 mUsageSummary.setText(getString(summaryRes, totalPhrase, rangePhrase));
Jeff Sharkey92811822012-05-04 11:47:29 -07001220
1221 // initial layout is finished above, ensure we have transitions
1222 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001223 }
1224
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001225 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1226 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001227 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001228 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001229 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001230 }
1231
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001232 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001233 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1234 mChartData = data;
1235 mChart.bindNetworkStats(mChartData.network);
1236 mChart.bindDetailNetworkStats(mChartData.detail);
1237
1238 // calcuate policy cycles based on available data
1239 updatePolicy(true);
1240 updateAppDetail();
1241
1242 // force scroll to top of body when showing detail
1243 if (mChartData.detail != null) {
1244 mListView.smoothScrollToPosition(0);
1245 }
1246 }
1247
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001248 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001249 public void onLoaderReset(Loader<ChartData> loader) {
1250 mChartData = null;
1251 mChart.bindNetworkStats(null);
1252 mChart.bindDetailNetworkStats(null);
1253 }
1254 };
1255
1256 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001257 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001258 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001259 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001260 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001261 }
1262
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001263 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001264 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001265 final int[] restrictedUids = mPolicyManager.getUidsWithPolicy(
Jeff Sharkeye557c332012-04-13 16:04:07 -07001266 POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001267 mAdapter.bindStats(data, restrictedUids);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001268 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001269 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001270
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001271 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001272 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001273 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001274 updateEmptyVisible();
1275 }
1276
1277 private void updateEmptyVisible() {
1278 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1279 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001280 }
1281 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001282
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001283 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001284 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001285 final Context context = getActivity();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001286 if (hasReadyMobileRadio(context)) {
1287 final TelephonyManager tele = TelephonyManager.from(context);
1288 return mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001289 } else {
1290 return false;
1291 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001292 }
1293
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001294 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001295 private void setMobilePolicySplit(boolean split) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001296 final Context context = getActivity();
1297 if (hasReadyMobileRadio(context)) {
1298 final TelephonyManager tele = TelephonyManager.from(context);
1299 mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split);
1300 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001301 }
1302
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001303 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001304 final TelephonyManager tele = TelephonyManager.from(context);
1305 final String actualSubscriberId = tele.getSubscriberId();
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -08001306 return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001307 }
1308
Jeff Sharkey8a503642011-06-10 13:31:21 -07001309 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001310 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001311 public void onInspectRangeChanged() {
1312 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
1313 updateDetailData();
1314 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001315
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001316 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001317 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001318 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001319 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001320
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001321 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001322 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001323 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001324 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001325
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001326 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001327 public void requestWarningEdit() {
1328 WarningEditorFragment.show(DataUsageSummary.this);
1329 }
1330
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001331 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001332 public void requestLimitEdit() {
1333 LimitEditorFragment.show(DataUsageSummary.this);
1334 }
1335 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001336
1337 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001338 * List item that reflects a specific data usage cycle.
1339 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001340 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001341 public CharSequence label;
1342 public long start;
1343 public long end;
1344
Jeff Sharkey8a503642011-06-10 13:31:21 -07001345 CycleItem(CharSequence label) {
1346 this.label = label;
1347 }
1348
1349 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001350 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001351 this.start = start;
1352 this.end = end;
1353 }
1354
Jeff Sharkey8a503642011-06-10 13:31:21 -07001355 @Override
1356 public String toString() {
1357 return label.toString();
1358 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001359
1360 @Override
1361 public boolean equals(Object o) {
1362 if (o instanceof CycleItem) {
1363 final CycleItem another = (CycleItem) o;
1364 return start == another.start && end == another.end;
1365 }
1366 return false;
1367 }
1368
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001369 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001370 public int compareTo(CycleItem another) {
1371 return Long.compare(start, another.start);
1372 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001373 }
1374
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001375 private static final StringBuilder sBuilder = new StringBuilder(50);
1376 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1377 sBuilder, Locale.getDefault());
1378
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001379 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001380 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001381
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001382 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001383 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001384 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1385 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001386 }
1387 }
1388
Jeff Sharkey8a503642011-06-10 13:31:21 -07001389 /**
1390 * Special-case data usage cycle that triggers dialog to change
1391 * {@link NetworkPolicy#cycleDay}.
1392 */
1393 public static class CycleChangeItem extends CycleItem {
1394 public CycleChangeItem(Context context) {
1395 super(context.getString(R.string.data_usage_change_cycle));
1396 }
1397 }
1398
1399 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001400 private boolean mChangePossible = false;
1401 private boolean mChangeVisible = false;
1402
1403 private final CycleChangeItem mChangeItem;
1404
Jeff Sharkey8a503642011-06-10 13:31:21 -07001405 public CycleAdapter(Context context) {
1406 super(context, android.R.layout.simple_spinner_item);
1407 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001408 mChangeItem = new CycleChangeItem(context);
1409 }
1410
1411 public void setChangePossible(boolean possible) {
1412 mChangePossible = possible;
1413 updateChange();
1414 }
1415
1416 public void setChangeVisible(boolean visible) {
1417 mChangeVisible = visible;
1418 updateChange();
1419 }
1420
1421 private void updateChange() {
1422 remove(mChangeItem);
1423 if (mChangePossible && mChangeVisible) {
1424 add(mChangeItem);
1425 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001426 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001427
1428 /**
1429 * Find position of {@link CycleItem} in this adapter which is nearest
1430 * the given {@link CycleItem}.
1431 */
1432 public int findNearestPosition(CycleItem target) {
1433 if (target != null) {
1434 final int count = getCount();
1435 for (int i = count - 1; i >= 0; i--) {
1436 final CycleItem item = getItem(i);
1437 if (item instanceof CycleChangeItem) {
1438 continue;
1439 } else if (item.compareTo(target) >= 0) {
1440 return i;
1441 }
1442 }
1443 }
1444 return 0;
1445 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001446 }
1447
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001448 public static class AppItem implements Comparable<AppItem>, Parcelable {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001449 public final int key;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001450 public boolean restricted;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001451 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001452 public long total;
1453
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001454 public AppItem(int key) {
1455 this.key = key;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001456 }
1457
1458 public AppItem(Parcel parcel) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001459 key = parcel.readInt();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001460 uids = parcel.readSparseBooleanArray();
1461 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001462 }
1463
1464 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001465 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001466 }
1467
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001468 @Override
1469 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001470 dest.writeInt(key);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001471 dest.writeSparseBooleanArray(uids);
1472 dest.writeLong(total);
1473 }
1474
1475 @Override
1476 public int describeContents() {
1477 return 0;
1478 }
1479
1480 @Override
1481 public int compareTo(AppItem another) {
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001482 return Long.compare(another.total, total);
1483 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001484
1485 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1486 @Override
1487 public AppItem createFromParcel(Parcel in) {
1488 return new AppItem(in);
1489 }
1490
1491 @Override
1492 public AppItem[] newArray(int size) {
1493 return new AppItem[size];
1494 }
1495 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001496 }
1497
Jeff Sharkey8a503642011-06-10 13:31:21 -07001498 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001499 * Adapter of applications, sorted by total usage descending.
1500 */
1501 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001502 private final UidDetailProvider mProvider;
1503 private final int mInsetSide;
1504
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001505 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001506 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001507
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001508 public DataUsageAdapter(UidDetailProvider provider, int insetSide) {
1509 mProvider = checkNotNull(provider);
1510 mInsetSide = insetSide;
1511 }
1512
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001513 /**
1514 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1515 */
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001516 public void bindStats(NetworkStats stats, int[] restrictedUids) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001517 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001518
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001519 final int currentUserId = ActivityManager.getCurrentUser();
1520 final SparseArray<AppItem> knownItems = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001521
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001522 NetworkStats.Entry entry = null;
1523 final int size = stats != null ? stats.size() : 0;
1524 for (int i = 0; i < size; i++) {
1525 entry = stats.getValues(i, entry);
1526
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001527 // Decide how to collapse items together
1528 final int uid = entry.uid;
1529 final int collapseKey;
1530 if (UserHandle.isApp(uid)) {
1531 if (UserHandle.getUserId(uid) == currentUserId) {
1532 collapseKey = uid;
1533 } else {
1534 collapseKey = UidDetailProvider.buildKeyForUser(UserHandle.getUserId(uid));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001535 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001536 } else if (uid == UID_REMOVED || uid == UID_TETHERING) {
1537 collapseKey = uid;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001538 } else {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001539 collapseKey = android.os.Process.SYSTEM_UID;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001540 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001541
1542 AppItem item = knownItems.get(collapseKey);
1543 if (item == null) {
1544 item = new AppItem(collapseKey);
1545 mItems.add(item);
1546 knownItems.put(item.key, item);
1547 }
1548 item.addUid(uid);
1549 item.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001550 }
1551
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001552 for (int uid : restrictedUids) {
1553 // Only splice in restricted state for current user
1554 if (UserHandle.getUserId(uid) != currentUserId) continue;
1555
1556 AppItem item = knownItems.get(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001557 if (item == null) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001558 item = new AppItem(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001559 item.total = -1;
1560 mItems.add(item);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001561 knownItems.put(item.key, item);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001562 }
1563 item.restricted = true;
1564 }
1565
Jeff Sharkey8a503642011-06-10 13:31:21 -07001566 Collections.sort(mItems);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001567 mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001568 notifyDataSetChanged();
1569 }
1570
1571 @Override
1572 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001573 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001574 }
1575
1576 @Override
1577 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001578 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001579 }
1580
1581 @Override
1582 public long getItemId(int position) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001583 return mItems.get(position).key;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001584 }
1585
1586 @Override
1587 public View getView(int position, View convertView, ViewGroup parent) {
1588 if (convertView == null) {
1589 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey5d706792011-09-08 18:57:17 -07001590 R.layout.data_usage_item, parent, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001591
1592 if (mInsetSide > 0) {
Fabrice Di Megliob27223f2013-01-15 18:54:11 -08001593 convertView.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001594 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001595 }
1596
1597 final Context context = parent.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001598
Jeff Sharkey28130d92011-09-02 16:10:24 -07001599 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001600 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1601 android.R.id.progress);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001602
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001603 // kick off async load of app details
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001604 final AppItem item = mItems.get(position);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001605 UidDetailTask.bindView(mProvider, item, convertView);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001606
Jeff Sharkeye557c332012-04-13 16:04:07 -07001607 if (item.restricted && item.total <= 0) {
1608 text1.setText(R.string.data_usage_app_restricted);
1609 progress.setVisibility(View.GONE);
1610 } else {
1611 text1.setText(Formatter.formatFileSize(context, item.total));
1612 progress.setVisibility(View.VISIBLE);
1613 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001614
1615 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1616 progress.setProgress(percentTotal);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001617
1618 return convertView;
1619 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001620 }
1621
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001622 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001623 * Empty {@link Fragment} that controls display of UID details in
1624 * {@link DataUsageSummary}.
1625 */
1626 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001627 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001628
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001629 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001630 if (!parent.isAdded()) return;
1631
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001632 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001633 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001634
1635 final AppDetailsFragment fragment = new AppDetailsFragment();
1636 fragment.setArguments(args);
1637 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001638 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1639 ft.add(fragment, TAG_APP_DETAILS);
1640 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001641 ft.setBreadCrumbTitle(label);
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001642 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001643 }
1644
1645 @Override
1646 public void onStart() {
1647 super.onStart();
1648 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001649 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001650 target.updateBody();
1651 }
1652
1653 @Override
1654 public void onStop() {
1655 super.onStop();
1656 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001657 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001658 target.updateBody();
1659 }
1660 }
1661
1662 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001663 * Dialog to request user confirmation before setting
1664 * {@link NetworkPolicy#limitBytes}.
1665 */
1666 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001667 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001668 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001669
1670 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001671 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001672
Jeff Sharkey461842a2011-09-25 18:22:48 -07001673 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001674 final CharSequence message;
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001675 final long minLimitBytes = (long) (
1676 parent.mPolicyEditor.getPolicy(parent.mTemplate).warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001677 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001678
1679 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001680 final String currentTab = parent.mCurrentTab;
1681 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001682 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001683 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001684 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001685 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001686 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001687 } else if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001688 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001689 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001690 } else {
1691 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001692 }
1693
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001694 final Bundle args = new Bundle();
1695 args.putCharSequence(EXTRA_MESSAGE, message);
1696 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1697
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001698 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1699 dialog.setArguments(args);
1700 dialog.setTargetFragment(parent, 0);
1701 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1702 }
1703
1704 @Override
1705 public Dialog onCreateDialog(Bundle savedInstanceState) {
1706 final Context context = getActivity();
1707
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001708 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001709 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1710
1711 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1712 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001713 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001714
1715 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001716 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001717 public void onClick(DialogInterface dialog, int which) {
1718 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1719 if (target != null) {
1720 target.setPolicyLimitBytes(limitBytes);
1721 }
1722 }
1723 });
1724
1725 return builder.create();
1726 }
1727 }
1728
1729 /**
1730 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1731 */
1732 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001733 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001734
1735 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001736 if (!parent.isAdded()) return;
1737
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001738 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001739 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001740
1741 final CycleEditorFragment dialog = new CycleEditorFragment();
1742 dialog.setArguments(args);
1743 dialog.setTargetFragment(parent, 0);
1744 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1745 }
1746
1747 @Override
1748 public Dialog onCreateDialog(Bundle savedInstanceState) {
1749 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001750 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1751 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001752
1753 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1754 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1755
1756 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1757 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1758
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001759 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1760 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001761
1762 cycleDayPicker.setMinValue(1);
1763 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001764 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001765 cycleDayPicker.setWrapSelectorWheel(true);
1766
1767 builder.setTitle(R.string.data_usage_cycle_editor_title);
1768 builder.setView(view);
1769
1770 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1771 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001772 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001773 public void onClick(DialogInterface dialog, int which) {
Jeff Sharkeyd277de92013-03-25 15:11:25 -07001774 // clear focus to finish pending text edits
1775 cycleDayPicker.clearFocus();
1776
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001777 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001778 final String cycleTimezone = new Time().timezone;
1779 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001780 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001781 }
1782 });
1783
1784 return builder.create();
1785 }
1786 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001787
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001788 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001789 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1790 */
1791 public static class WarningEditorFragment extends DialogFragment {
1792 private static final String EXTRA_TEMPLATE = "template";
1793
1794 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001795 if (!parent.isAdded()) return;
1796
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001797 final Bundle args = new Bundle();
1798 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1799
1800 final WarningEditorFragment dialog = new WarningEditorFragment();
1801 dialog.setArguments(args);
1802 dialog.setTargetFragment(parent, 0);
1803 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1804 }
1805
1806 @Override
1807 public Dialog onCreateDialog(Bundle savedInstanceState) {
1808 final Context context = getActivity();
1809 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1810 final NetworkPolicyEditor editor = target.mPolicyEditor;
1811
1812 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1813 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1814
1815 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1816 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1817
1818 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1819 final long warningBytes = editor.getPolicyWarningBytes(template);
1820 final long limitBytes = editor.getPolicyLimitBytes(template);
1821
1822 bytesPicker.setMinValue(0);
1823 if (limitBytes != LIMIT_DISABLED) {
1824 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1825 } else {
1826 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1827 }
1828 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1829 bytesPicker.setWrapSelectorWheel(false);
1830
1831 builder.setTitle(R.string.data_usage_warning_editor_title);
1832 builder.setView(view);
1833
1834 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1835 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001836 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001837 public void onClick(DialogInterface dialog, int which) {
1838 // clear focus to finish pending text edits
1839 bytesPicker.clearFocus();
1840
1841 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1842 editor.setPolicyWarningBytes(template, bytes);
1843 target.updatePolicy(false);
1844 }
1845 });
1846
1847 return builder.create();
1848 }
1849 }
1850
1851 /**
1852 * Dialog to edit {@link NetworkPolicy#limitBytes}.
1853 */
1854 public static class LimitEditorFragment extends DialogFragment {
1855 private static final String EXTRA_TEMPLATE = "template";
1856
1857 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001858 if (!parent.isAdded()) return;
1859
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001860 final Bundle args = new Bundle();
1861 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1862
1863 final LimitEditorFragment dialog = new LimitEditorFragment();
1864 dialog.setArguments(args);
1865 dialog.setTargetFragment(parent, 0);
1866 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
1867 }
1868
1869 @Override
1870 public Dialog onCreateDialog(Bundle savedInstanceState) {
1871 final Context context = getActivity();
1872 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1873 final NetworkPolicyEditor editor = target.mPolicyEditor;
1874
1875 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1876 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1877
1878 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1879 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1880
1881 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1882 final long warningBytes = editor.getPolicyWarningBytes(template);
1883 final long limitBytes = editor.getPolicyLimitBytes(template);
1884
1885 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09001886 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001887 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
1888 } else {
1889 bytesPicker.setMinValue(0);
1890 }
1891 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
1892 bytesPicker.setWrapSelectorWheel(false);
1893
1894 builder.setTitle(R.string.data_usage_limit_editor_title);
1895 builder.setView(view);
1896
1897 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1898 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001899 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001900 public void onClick(DialogInterface dialog, int which) {
1901 // clear focus to finish pending text edits
1902 bytesPicker.clearFocus();
1903
1904 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1905 editor.setPolicyLimitBytes(template, bytes);
1906 target.updatePolicy(false);
1907 }
1908 });
1909
1910 return builder.create();
1911 }
1912 }
1913 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07001914 * Dialog to request user confirmation before disabling data.
1915 */
1916 public static class ConfirmDataDisableFragment extends DialogFragment {
1917 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001918 if (!parent.isAdded()) return;
1919
Jeff Sharkey28130d92011-09-02 16:10:24 -07001920 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
1921 dialog.setTargetFragment(parent, 0);
1922 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
1923 }
1924
1925 @Override
1926 public Dialog onCreateDialog(Bundle savedInstanceState) {
1927 final Context context = getActivity();
1928
1929 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1930 builder.setMessage(R.string.data_usage_disable_mobile);
1931
1932 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001933 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07001934 public void onClick(DialogInterface dialog, int which) {
1935 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1936 if (target != null) {
1937 // TODO: extend to modify policy enabled flag.
1938 target.setMobileDataEnabled(false);
1939 }
1940 }
1941 });
1942 builder.setNegativeButton(android.R.string.cancel, null);
1943
1944 return builder.create();
1945 }
1946 }
1947
1948 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001949 * Dialog to request user confirmation before setting
Christopher Tate5a64c732012-09-07 13:35:31 -07001950 * {@link android.provider.Settings.Global#DATA_ROAMING}.
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001951 */
1952 public static class ConfirmDataRoamingFragment extends DialogFragment {
1953 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001954 if (!parent.isAdded()) return;
1955
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001956 final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment();
1957 dialog.setTargetFragment(parent, 0);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001958 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_ROAMING);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001959 }
1960
1961 @Override
1962 public Dialog onCreateDialog(Bundle savedInstanceState) {
1963 final Context context = getActivity();
1964
1965 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1966 builder.setTitle(R.string.roaming_reenable_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07001967 if (Utils.hasMultipleUsers(context)) {
1968 builder.setMessage(R.string.roaming_warning_multiuser);
1969 } else {
1970 builder.setMessage(R.string.roaming_warning);
1971 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001972
1973 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001974 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001975 public void onClick(DialogInterface dialog, int which) {
1976 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1977 if (target != null) {
1978 target.setDataRoaming(true);
1979 }
1980 }
1981 });
1982 builder.setNegativeButton(android.R.string.cancel, null);
1983
1984 return builder.create();
1985 }
1986 }
1987
1988 /**
1989 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001990 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001991 */
1992 public static class ConfirmRestrictFragment extends DialogFragment {
1993 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001994 if (!parent.isAdded()) return;
1995
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001996 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1997 dialog.setTargetFragment(parent, 0);
1998 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1999 }
2000
2001 @Override
2002 public Dialog onCreateDialog(Bundle savedInstanceState) {
2003 final Context context = getActivity();
2004
2005 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002006 builder.setTitle(R.string.data_usage_restrict_background_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07002007 if (Utils.hasMultipleUsers(context)) {
2008 builder.setMessage(R.string.data_usage_restrict_background_multiuser);
2009 } else {
2010 builder.setMessage(R.string.data_usage_restrict_background);
2011 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002012
2013 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002014 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002015 public void onClick(DialogInterface dialog, int which) {
2016 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2017 if (target != null) {
2018 target.setRestrictBackground(true);
2019 }
2020 }
2021 });
2022 builder.setNegativeButton(android.R.string.cancel, null);
2023
2024 return builder.create();
2025 }
2026 }
2027
2028 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002029 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
2030 * change has been denied, usually based on
2031 * {@link DataUsageSummary#hasLimitedNetworks()}.
2032 */
2033 public static class DeniedRestrictFragment extends DialogFragment {
2034 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002035 if (!parent.isAdded()) return;
2036
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002037 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
2038 dialog.setTargetFragment(parent, 0);
2039 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
2040 }
2041
2042 @Override
2043 public Dialog onCreateDialog(Bundle savedInstanceState) {
2044 final Context context = getActivity();
2045
2046 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2047 builder.setTitle(R.string.data_usage_app_restrict_background);
2048 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
2049 builder.setPositiveButton(android.R.string.ok, null);
2050
2051 return builder.create();
2052 }
2053 }
2054
2055 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002056 * Dialog to request user confirmation before setting
2057 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
2058 */
2059 public static class ConfirmAppRestrictFragment extends DialogFragment {
2060 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002061 if (!parent.isAdded()) return;
2062
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002063 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2064 dialog.setTargetFragment(parent, 0);
2065 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2066 }
2067
2068 @Override
2069 public Dialog onCreateDialog(Bundle savedInstanceState) {
2070 final Context context = getActivity();
2071
2072 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002073 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2074 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2075
2076 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002077 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002078 public void onClick(DialogInterface dialog, int which) {
2079 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2080 if (target != null) {
2081 target.setAppRestrictBackground(true);
2082 }
2083 }
2084 });
2085 builder.setNegativeButton(android.R.string.cancel, null);
2086
2087 return builder.create();
2088 }
2089 }
2090
2091 /**
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002092 * Dialog to inform user about changing auto-sync setting
2093 */
2094 public static class ConfirmAutoSyncChangeFragment extends DialogFragment {
Amith Yamasani665235f2012-06-07 19:58:34 -07002095 private static final String SAVE_ENABLING = "enabling";
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002096 private boolean mEnabling;
2097
2098 public static void show(DataUsageSummary parent, boolean enabling) {
2099 if (!parent.isAdded()) return;
2100
2101 final ConfirmAutoSyncChangeFragment dialog = new ConfirmAutoSyncChangeFragment();
2102 dialog.mEnabling = enabling;
2103 dialog.setTargetFragment(parent, 0);
2104 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_AUTO_SYNC_CHANGE);
2105 }
2106
2107 @Override
2108 public Dialog onCreateDialog(Bundle savedInstanceState) {
2109 final Context context = getActivity();
Amith Yamasani665235f2012-06-07 19:58:34 -07002110 if (savedInstanceState != null) {
2111 mEnabling = savedInstanceState.getBoolean(SAVE_ENABLING);
2112 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002113
2114 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2115 if (!mEnabling) {
2116 builder.setTitle(R.string.data_usage_auto_sync_off_dialog_title);
2117 builder.setMessage(R.string.data_usage_auto_sync_off_dialog);
2118 } else {
2119 builder.setTitle(R.string.data_usage_auto_sync_on_dialog_title);
2120 builder.setMessage(R.string.data_usage_auto_sync_on_dialog);
2121 }
2122
2123 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
2124 @Override
2125 public void onClick(DialogInterface dialog, int which) {
2126 ContentResolver.setMasterSyncAutomatically(mEnabling);
2127 }
2128 });
2129 builder.setNegativeButton(android.R.string.cancel, null);
2130
2131 return builder.create();
2132 }
Amith Yamasani665235f2012-06-07 19:58:34 -07002133
2134 @Override
2135 public void onSaveInstanceState(Bundle outState) {
2136 super.onSaveInstanceState(outState);
2137 outState.putBoolean(SAVE_ENABLING, mEnabling);
2138 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002139 }
2140
2141 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002142 * Compute default tab that should be selected, based on
2143 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2144 */
2145 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002146 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
2147 if (template == null) return null;
2148
2149 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002150 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002151 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002152 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002153 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002154 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002155 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002156 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002157 return TAB_WIFI;
2158 default:
2159 return null;
2160 }
2161 }
2162
2163 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002164 * Background task that loads {@link UidDetail}, binding to
2165 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002166 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002167 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2168 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002169 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002170 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002171
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002172 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002173 mProvider = checkNotNull(provider);
2174 mItem = checkNotNull(item);
2175 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002176 }
2177
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002178 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002179 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002180 final UidDetailTask existing = (UidDetailTask) target.getTag();
2181 if (existing != null) {
2182 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002183 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002184
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002185 final UidDetail cachedDetail = provider.getUidDetail(item.key, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002186 if (cachedDetail != null) {
2187 bindView(cachedDetail, target);
2188 } else {
2189 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2190 AsyncTask.THREAD_POOL_EXECUTOR));
2191 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002192 }
2193
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002194 private static void bindView(UidDetail detail, View target) {
2195 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2196 final TextView title = (TextView) target.findViewById(android.R.id.title);
2197
2198 if (detail != null) {
2199 icon.setImageDrawable(detail.icon);
2200 title.setText(detail.label);
2201 } else {
2202 icon.setImageDrawable(null);
2203 title.setText(null);
2204 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002205 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002206
2207 @Override
2208 protected void onPreExecute() {
2209 bindView(null, mTarget);
2210 }
2211
2212 @Override
2213 protected UidDetail doInBackground(Void... params) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002214 return mProvider.getUidDetail(mItem.key, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002215 }
2216
2217 @Override
2218 protected void onPostExecute(UidDetail result) {
2219 bindView(result, mTarget);
2220 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002221 }
2222
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002223 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002224 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002225 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002226 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002227 if (TEST_RADIOS) {
2228 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2229 }
2230
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002231 final ConnectivityManager conn = ConnectivityManager.from(context);
2232 final TelephonyManager tele = TelephonyManager.from(context);
2233
2234 // require both supported network and ready SIM
2235 return conn.isNetworkSupported(TYPE_MOBILE) && tele.getSimState() == SIM_STATE_READY;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002236 }
2237
2238 /**
2239 * Test if device has a mobile 4G data radio.
2240 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002241 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002242 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2243 return false;
2244 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002245 if (TEST_RADIOS) {
2246 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2247 }
2248
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002249 final ConnectivityManager conn = ConnectivityManager.from(context);
2250 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002251
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002252 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Wink Saville55434042012-06-14 12:33:43 -07002253 final boolean hasLte = (tele.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE)
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002254 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002255 return hasWimax || hasLte;
2256 }
2257
2258 /**
2259 * Test if device has a Wi-Fi data radio.
2260 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002261 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002262 if (TEST_RADIOS) {
2263 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2264 }
2265
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002266 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002267 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002268 }
2269
2270 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002271 * Test if device has an ethernet network connection.
2272 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002273 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002274 if (TEST_RADIOS) {
2275 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2276 }
2277
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002278 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002279 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2280
2281 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002282 if (mStatsSession != null) {
2283 try {
2284 ethernetBytes = mStatsSession.getSummaryForNetwork(
2285 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2286 .getTotalBytes();
2287 } catch (RemoteException e) {
2288 throw new RuntimeException(e);
2289 }
2290 } else {
2291 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002292 }
2293
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002294 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002295 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002296 }
2297
2298 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002299 * Inflate a {@link Preference} style layout, adding the given {@link View}
2300 * widget into {@link android.R.id#widget_frame}.
2301 */
2302 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2303 final View view = inflater.inflate(R.layout.preference, root, false);
2304 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2305 android.R.id.widget_frame);
2306 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2307 return view;
2308 }
2309
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002310 private static View inflateAppTitle(
2311 LayoutInflater inflater, ViewGroup root, CharSequence label) {
2312 final TextView view = (TextView) inflater.inflate(
2313 R.layout.data_usage_app_title, root, false);
2314 view.setText(label);
2315 return view;
2316 }
2317
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002318 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002319 * Test if any networks are currently limited.
2320 */
2321 private boolean hasLimitedNetworks() {
2322 return !buildLimitedNetworksList().isEmpty();
2323 }
2324
2325 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002326 * Build string describing currently limited networks, which defines when
2327 * background data is restricted.
2328 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002329 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002330 private CharSequence buildLimitedNetworksString() {
2331 final List<CharSequence> limited = buildLimitedNetworksList();
2332
2333 // handle case where no networks limited
2334 if (limited.isEmpty()) {
2335 limited.add(getText(R.string.data_usage_list_none));
2336 }
2337
2338 return TextUtils.join(limited);
2339 }
2340
2341 /**
2342 * Build list of currently limited networks, which defines when background
2343 * data is restricted.
2344 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002345 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002346 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002347 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002348
2349 // build combined list of all limited networks
2350 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002351
2352 final TelephonyManager tele = TelephonyManager.from(context);
2353 if (tele.getSimState() == SIM_STATE_READY) {
2354 final String subscriberId = getActiveSubscriberId(context);
2355 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2356 limited.add(getText(R.string.data_usage_list_mobile));
2357 }
2358 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2359 limited.add(getText(R.string.data_usage_tab_3g));
2360 }
2361 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2362 limited.add(getText(R.string.data_usage_tab_4g));
2363 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002364 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002365
2366 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002367 limited.add(getText(R.string.data_usage_tab_wifi));
2368 }
2369 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2370 limited.add(getText(R.string.data_usage_tab_ethernet));
2371 }
2372
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002373 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002374 }
2375
2376 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002377 * Inset both selector and divider {@link Drawable} on the given
2378 * {@link ListView} by the requested dimensions.
2379 */
2380 private static void insetListViewDrawables(ListView view, int insetSide) {
2381 final Drawable selector = view.getSelector();
2382 final Drawable divider = view.getDivider();
2383
2384 // fully unregister these drawables so callbacks can be maintained after
2385 // wrapping below.
2386 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2387 view.setSelector(stub);
2388 view.setDivider(stub);
2389
2390 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2391 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2392 }
2393
2394 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002395 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002396 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002397 */
2398 private static void setPreferenceTitle(View parent, int resId) {
2399 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2400 title.setText(resId);
2401 }
2402
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002403 /**
2404 * Set {@link android.R.id#summary} for a preference view inflated with
2405 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2406 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002407 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002408 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2409 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002410 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002411 }
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002412
2413 /**
2414 * For search
2415 */
2416 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -07002417 new BaseSearchIndexProvider() {
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002418 @Override
2419 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
2420 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
2421
2422 final Resources res = context.getResources();
2423
2424 // Add fragment title
2425 SearchIndexableRaw data = new SearchIndexableRaw(context);
2426 data.title = res.getString(R.string.data_usage_summary_title);
2427 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2428 result.add(data);
2429
2430 // Mobile data
2431 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002432 data.key = DATA_USAGE_ENABLE_MOBILE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002433 data.title = res.getString(R.string.data_usage_enable_mobile);
2434 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2435 result.add(data);
2436
2437 // Set mobile data limit
2438 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002439 data.key = DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002440 data.title = res.getString(R.string.data_usage_disable_mobile_limit);
2441 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2442 result.add(data);
2443
Fabrice Di Megliof2a52262014-04-17 17:20:27 -07002444 // Data usage cycle
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002445 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002446 data.key = DATA_USAGE_CYCLE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002447 data.title = res.getString(R.string.data_usage_cycle);
2448 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2449 result.add(data);
2450
2451 return result;
2452 }
2453 };
2454
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002455}