blob: c8691e16cb414a4ddca70ebe1e25ee2fceb6ff7e [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 Sharkeyb6548462014-07-21 15:38:06 -070058import android.content.ComponentName;
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 Sharkeyb6548462014-07-21 15:38:06 -070066import android.content.res.TypedArray;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070067import android.graphics.Color;
Jeff Sharkey5d706792011-09-08 18:57:17 -070068import android.graphics.drawable.ColorDrawable;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070069import android.graphics.drawable.Drawable;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070070import android.net.ConnectivityManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070071import android.net.INetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070072import android.net.INetworkStatsService;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070073import android.net.INetworkStatsSession;
Jeff Sharkey8a503642011-06-10 13:31:21 -070074import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070075import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070076import android.net.NetworkStats;
77import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070078import android.net.NetworkTemplate;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070079import android.net.TrafficStats;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070080import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070081import android.os.Bundle;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070082import android.os.INetworkManagementService;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070083import android.os.Parcel;
84import android.os.Parcelable;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070085import android.os.RemoteException;
86import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070087import android.os.SystemProperties;
Dianne Hackbornbb06a422012-08-16 11:01:57 -070088import android.os.UserHandle;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +010089import android.os.UserManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070090import android.preference.Preference;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070091import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070092import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070093import android.text.format.DateUtils;
94import android.text.format.Formatter;
Jeff Sharkeye5223a02012-03-09 17:11:14 -080095import android.text.format.Time;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070096import android.util.Log;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070097import android.util.SparseArray;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070098import android.util.SparseBooleanArray;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070099import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700100import android.view.Menu;
101import android.view.MenuInflater;
102import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700103import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700104import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700105import android.view.ViewGroup;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700106import android.widget.AdapterView;
107import android.widget.AdapterView.OnItemClickListener;
108import android.widget.AdapterView.OnItemSelectedListener;
109import android.widget.ArrayAdapter;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700110import android.widget.BaseAdapter;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700111import android.widget.Button;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700112import android.widget.ImageView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700113import android.widget.LinearLayout;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700114import android.widget.ListView;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700115import android.widget.NumberPicker;
Jeff Sharkey2412b0f2011-07-17 20:31:40 -0700116import android.widget.ProgressBar;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700117import android.widget.Spinner;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700118import android.widget.Switch;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700119import android.widget.TabHost;
120import android.widget.TabHost.OnTabChangeListener;
121import android.widget.TabHost.TabContentFactory;
122import android.widget.TabHost.TabSpec;
123import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700124import android.widget.TextView;
125
Wink Saville55434042012-06-14 12:33:43 -0700126import com.android.internal.telephony.PhoneConstants;
Jeff Sharkey5d706792011-09-08 18:57:17 -0700127import com.android.settings.drawable.InsetBoundsDrawable;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700128import com.android.settings.net.ChartData;
129import com.android.settings.net.ChartDataLoader;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700130import com.android.settings.net.DataUsageMeteredSettings;
Jeff Sharkeya662e492011-06-18 21:57:06 -0700131import com.android.settings.net.NetworkPolicyEditor;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700132import com.android.settings.net.SummaryForAllUidLoader;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700133import com.android.settings.net.UidDetail;
134import com.android.settings.net.UidDetailProvider;
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -0700135import com.android.settings.search.BaseSearchIndexProvider;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -0700136import com.android.settings.search.Indexable;
137import com.android.settings.search.SearchIndexableRaw;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700138import com.android.settings.widget.ChartDataUsageView;
139import com.android.settings.widget.ChartDataUsageView.DataUsageChartListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700140import com.google.android.collect.Lists;
141
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700142import libcore.util.Objects;
143
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700144import java.util.ArrayList;
145import java.util.Collections;
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700146import java.util.List;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700147import java.util.Locale;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700148
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700149/**
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700150 * Panel showing data usage history across various networks, including options
151 * to inspect based on usage cycle and control through {@link NetworkPolicy}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700152 */
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700153public class DataUsageSummary extends HighlightingFragment implements Indexable {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700154 private static final String TAG = "DataUsage";
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800155 private static final boolean LOGD = false;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700156
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700157 // TODO: remove this testing code
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700158 private static final boolean TEST_ANIM = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700159 private static final boolean TEST_RADIOS = false;
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800160
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700161 private static final String TEST_RADIOS_PROP = "test.radios";
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800162 private static final String TEST_SUBSCRIBER_PROP = "test.subscriberid";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700163
Jeff Sharkey8a503642011-06-10 13:31:21 -0700164 private static final String TAB_3G = "3g";
165 private static final String TAB_4G = "4g";
166 private static final String TAB_MOBILE = "mobile";
167 private static final String TAB_WIFI = "wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700168 private static final String TAB_ETHERNET = "ethernet";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700169
Jeff Sharkey28130d92011-09-02 16:10:24 -0700170 private static final String TAG_CONFIRM_DATA_DISABLE = "confirmDataDisable";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700171 private static final String TAG_CONFIRM_LIMIT = "confirmLimit";
172 private static final String TAG_CYCLE_EDITOR = "cycleEditor";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700173 private static final String TAG_WARNING_EDITOR = "warningEditor";
174 private static final String TAG_LIMIT_EDITOR = "limitEditor";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700175 private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700176 private static final String TAG_DENIED_RESTRICT = "deniedRestrict";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700177 private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700178 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700179
Fabrice Di Meglio2169c882014-04-18 15:18:40 -0700180 private static final String DATA_USAGE_ENABLE_MOBILE_KEY = "data_usage_enable_mobile";
181 private static final String DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY =
182 "data_usage_disable_mobile_limit";
183 private static final String DATA_USAGE_CYCLE_KEY = "data_usage_cycle";
184
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700185 private static final int LOADER_CHART_DATA = 2;
186 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700187
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700188 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700189 private INetworkStatsService mStatsService;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700190 private NetworkPolicyManager mPolicyManager;
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700191 private TelephonyManager mTelephonyManager;
192
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700193 private INetworkStatsSession mStatsSession;
194
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700195 private static final String PREF_FILE = "data_usage";
196 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700197 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700198
199 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700200
Jeff Sharkey8a503642011-06-10 13:31:21 -0700201 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700202 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700203 private TabWidget mTabWidget;
204 private ListView mListView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700205 private DataUsageAdapter mAdapter;
206
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700207 /** Distance to inset content from sides, when needed. */
208 private int mInsetSide = 0;
209
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700210 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700211
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700212 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700213 private LinearLayout mNetworkSwitches;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700214 private boolean mDataEnabledSupported;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700215 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700216 private View mDataEnabledView;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700217 private boolean mDisableAtLimitSupported;
218 private Switch mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700219 private View mDisableAtLimitView;
220
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700221 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700222 private Spinner mCycleSpinner;
223 private CycleAdapter mCycleAdapter;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700224 private TextView mCycleSummary;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700225
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700226 private ChartDataUsageView mChart;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700227 private View mDisclaimer;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700228 private TextView mEmpty;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700229 private View mStupidPadding;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700230
231 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700232 private ImageView mAppIcon;
233 private ViewGroup mAppTitles;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700234 private TextView mAppTotal;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700235 private TextView mAppForeground;
236 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700237 private Button mAppSettings;
238
239 private LinearLayout mAppSwitches;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700240 private Switch mAppRestrict;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700241 private View mAppRestrictView;
242
Jeff Sharkey8a503642011-06-10 13:31:21 -0700243 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700244 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700245
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700246 private NetworkTemplate mTemplate;
247 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700248
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700249 private AppItem mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700250
251 private Intent mAppSettingsIntent;
252
Jeff Sharkeya662e492011-06-18 21:57:06 -0700253 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700254
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700255 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700256 private String mIntentTab = null;
257
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700258 private MenuItem mMenuRestrictBackground;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700259 private MenuItem mMenuShowWifi;
260 private MenuItem mMenuShowEthernet;
261 private MenuItem mMenuSimCards;
262 private MenuItem mMenuCellularNetworks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700263
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700264 /** Flag used to ignore listeners during binding. */
265 private boolean mBinding;
266
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700267 private UidDetailProvider mUidDetailProvider;
268
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700269 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700270 public void onCreate(Bundle savedInstanceState) {
271 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700272 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700273
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700274 mNetworkService = INetworkManagementService.Stub.asInterface(
275 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700276 mStatsService = INetworkStatsService.Stub.asInterface(
277 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700278 mPolicyManager = NetworkPolicyManager.from(context);
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700279 mTelephonyManager = TelephonyManager.from(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700280
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700281 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700282
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700283 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700284 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700285
Jaewan Kimffce9c12013-03-19 16:53:45 +0900286 try {
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700287 if (!mNetworkService.isBandwidthControlEnabled()) {
288 Log.w(TAG, "No bandwidth control; leaving");
289 getActivity().finish();
290 }
291 } catch (RemoteException e) {
292 Log.w(TAG, "No bandwidth control; leaving");
293 getActivity().finish();
294 }
295
296 try {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900297 mStatsSession = mStatsService.openSession();
298 } catch (RemoteException e) {
299 throw new RuntimeException(e);
300 }
301
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700302 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700303 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700304
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700305 // override preferences when no mobile radio
306 if (!hasReadyMobileRadio(context)) {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900307 mShowWifi = true;
308 mShowEthernet = true;
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700309 }
310
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700311 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700312 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700313
Jeff Sharkey8a503642011-06-10 13:31:21 -0700314 @Override
315 public View onCreateView(LayoutInflater inflater, ViewGroup container,
316 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700317
Jeff Sharkey8a503642011-06-10 13:31:21 -0700318 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700319 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
320
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700321 mUidDetailProvider = new UidDetailProvider(context);
322
Jeff Sharkey8a503642011-06-10 13:31:21 -0700323 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700324 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700325 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
326 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700327
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700328 // decide if we need to manually inset our content, or if we should rely
329 // on parent container for inset.
330 final boolean shouldInset = mListView.getScrollBarStyle()
331 == View.SCROLLBARS_OUTSIDE_OVERLAY;
Amith Yamasani56f51a82013-08-05 10:07:23 -0700332 mInsetSide = 0;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700333
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700334 // adjust padding around tabwidget as needed
Amith Yamasani56f51a82013-08-05 10:07:23 -0700335 prepareCustomPreferencesList(container, view, mListView, false);
Jeff Sharkey5d706792011-09-08 18:57:17 -0700336
Jeff Sharkey8a503642011-06-10 13:31:21 -0700337 mTabHost.setup();
338 mTabHost.setOnTabChangedListener(mTabListener);
339
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700340 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700341 mHeader.setClickable(true);
342
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700343 mListView.addHeaderView(new View(context), null, true);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700344 mListView.addHeaderView(mHeader, null, true);
345 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700346
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700347 if (mInsetSide > 0) {
348 // inset selector and divider drawables
349 insetListViewDrawables(mListView, mInsetSide);
Fabrice Di Megliob27223f2013-01-15 18:54:11 -0800350 mHeader.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700351 }
352
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700353 {
354 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700355 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
356 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700357 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700358
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700359 mDataEnabled = new Switch(inflater.getContext());
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700360 mDataEnabled.setClickable(false);
361 mDataEnabled.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700362 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 Sharkeyb6548462014-07-21 15:38:06 -0700365 mDataEnabledView.setClickable(true);
366 mDataEnabledView.setFocusable(true);
367 mDataEnabledView.setOnClickListener(mDataEnabledListener);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700368 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700369
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700370 mDisableAtLimit = new Switch(inflater.getContext());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700371 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700372 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700373 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700374 mDisableAtLimitView.setTag(R.id.preference_highlight_key,
375 DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700376 mDisableAtLimitView.setClickable(true);
377 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700378 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
379 mNetworkSwitches.addView(mDisableAtLimitView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700380
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700381 mCycleView = inflater.inflate(R.layout.data_usage_cycles, mNetworkSwitches, false);
382 mCycleView.setTag(R.id.preference_highlight_key, DATA_USAGE_CYCLE_KEY);
383 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
384 mCycleAdapter = new CycleAdapter(context);
385 mCycleSpinner.setAdapter(mCycleAdapter);
386 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
387 mCycleSummary = (TextView) mCycleView.findViewById(R.id.cycle_summary);
388 mNetworkSwitches.addView(mCycleView);
389 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700390
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700391 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700392 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800393 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700394
395 {
396 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700397 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700398 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
399 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700400 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
401 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700402 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700403
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700404 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700405 mAppSettings.setOnClickListener(mAppSettingsListener);
406
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700407 mAppRestrict = new Switch(inflater.getContext());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700408 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700409 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700410 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700411 mAppRestrictView.setClickable(true);
412 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700413 mAppRestrictView.setOnClickListener(mAppRestrictListener);
414 mAppSwitches.addView(mAppRestrictView);
415 }
416
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700417 mDisclaimer = mHeader.findViewById(R.id.disclaimer);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700418 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700419 mStupidPadding = mHeader.findViewById(R.id.stupid_padding);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700420
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +0100421 final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
422 mAdapter = new DataUsageAdapter(um, mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700423 mListView.setOnItemClickListener(mListListener);
424 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700425
426 return view;
427 }
428
429 @Override
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700430 public void onViewStateRestored(Bundle savedInstanceState) {
431 super.onViewStateRestored(savedInstanceState);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700432
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700433 // pick default tab based on incoming intent
434 final Intent intent = getActivity().getIntent();
435 mIntentTab = computeTabFromIntent(intent);
436
Jeff Sharkey8a503642011-06-10 13:31:21 -0700437 // this kicks off chain reaction which creates tabs, binds the body to
438 // selected network, and binds chart, cycles and detail list.
439 updateTabs();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700440 }
441
442 @Override
443 public void onResume() {
444 super.onResume();
445
446 getView().post(new Runnable() {
447 @Override
448 public void run() {
449 highlightViewIfNeeded();
450 }
451 });
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700452
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700453 // kick off background task to update stats
454 new AsyncTask<Void, Void, Void>() {
455 @Override
456 protected Void doInBackground(Void... params) {
457 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700458 // wait a few seconds before kicking off
459 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700460 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700461 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700462 } catch (RemoteException e) {
463 }
464 return null;
465 }
466
467 @Override
468 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700469 if (isAdded()) {
470 updateBody();
471 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700472 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700473 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700474 }
475
Jeff Sharkey8a503642011-06-10 13:31:21 -0700476 @Override
477 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
478 inflater.inflate(R.menu.data_usage, menu);
479 }
480
481 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700482 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700483 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700484 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700485 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700486
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700487 mMenuShowWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700488 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
489 mMenuShowWifi.setVisible(!appDetailMode);
490 } else {
491 mMenuShowWifi.setVisible(false);
492 }
493
494 mMenuShowEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
495 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
496 mMenuShowEthernet.setVisible(!appDetailMode);
497 } else {
498 mMenuShowEthernet.setVisible(false);
499 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700500
501 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700502 mMenuRestrictBackground.setVisible(
503 hasReadyMobileRadio(context) && isOwner && !appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700504
505 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
506 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700507 metered.setVisible(!appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700508 } else {
509 metered.setVisible(false);
510 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700511
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700512 // TODO: show when multiple sims available
513 mMenuSimCards = menu.findItem(R.id.data_usage_menu_sim_cards);
514 mMenuSimCards.setVisible(false);
515
516 mMenuCellularNetworks = menu.findItem(R.id.data_usage_menu_cellular_networks);
517 if (hasReadyMobileRadio(context)) {
518 mMenuCellularNetworks.setVisible(!appDetailMode);
519 } else {
520 mMenuCellularNetworks.setVisible(false);
521 }
522
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700523 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 Sharkeyb6548462014-07-21 15:38:06 -0700530
531 updateMenuTitles();
532 }
533
534 private void updateMenuTitles() {
535 if (mPolicyManager.getRestrictBackground()) {
536 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_allow_background);
537 } else {
538 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_restrict_background);
539 }
540
541 if (mShowWifi) {
542 mMenuShowWifi.setTitle(R.string.data_usage_menu_hide_wifi);
543 } else {
544 mMenuShowWifi.setTitle(R.string.data_usage_menu_show_wifi);
545 }
546
547 if (mShowEthernet) {
548 mMenuShowEthernet.setTitle(R.string.data_usage_menu_hide_ethernet);
549 } else {
550 mMenuShowEthernet.setTitle(R.string.data_usage_menu_show_ethernet);
551 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700552 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700553
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700554 @Override
555 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700556 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700557 case R.id.data_usage_menu_restrict_background: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700558 final boolean restrictBackground = !mPolicyManager.getRestrictBackground();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700559 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800560 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700561 } else {
562 // no confirmation to drop restriction
563 setRestrictBackground(false);
564 }
565 return true;
566 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700567 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700568 mShowWifi = !mShowWifi;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700569 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700570 updateMenuTitles();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700571 updateTabs();
572 return true;
573 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700574 case R.id.data_usage_menu_show_ethernet: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700575 mShowEthernet = !mShowEthernet;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700576 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700577 updateMenuTitles();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700578 updateTabs();
579 return true;
580 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700581 case R.id.data_usage_menu_sim_cards: {
582 // TODO: hook up to sim cards
583 return true;
584 }
585 case R.id.data_usage_menu_cellular_networks: {
586 final Intent intent = new Intent(Intent.ACTION_MAIN);
587 intent.setComponent(new ComponentName("com.android.phone",
588 "com.android.phone.MobileNetworkSettings"));
589 startActivity(intent);
590 return true;
591 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700592 case R.id.data_usage_menu_metered: {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800593 final SettingsActivity sa = (SettingsActivity) getActivity();
594 sa.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700595 R.string.data_usage_metered_title, null, this, 0);
596 return true;
597 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700598 }
599 return false;
600 }
601
Jeff Sharkey94a90952011-06-13 22:31:09 -0700602 @Override
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700603 public void onDestroy() {
Jeff Sharkey94a90952011-06-13 22:31:09 -0700604 mDataEnabledView = null;
605 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700606
607 mUidDetailProvider.clearCache();
608 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700609
610 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700611
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800612 super.onDestroy();
613 }
614
Jeff Sharkey8a503642011-06-10 13:31:21 -0700615 /**
Jeff Sharkey92811822012-05-04 11:47:29 -0700616 * Build and assign {@link LayoutTransition} to various containers. Should
617 * only be assigned after initial layout is complete.
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700618 */
Jeff Sharkey92811822012-05-04 11:47:29 -0700619 private void ensureLayoutTransitions() {
620 // skip when already setup
621 if (mChart.getLayoutTransition() != null) return;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700622
Jeff Sharkey92811822012-05-04 11:47:29 -0700623 mTabsContainer.setLayoutTransition(buildLayoutTransition());
624 mHeader.setLayoutTransition(buildLayoutTransition());
625 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700626
Jeff Sharkey92811822012-05-04 11:47:29 -0700627 final LayoutTransition chartTransition = buildLayoutTransition();
628 chartTransition.disableTransitionType(LayoutTransition.APPEARING);
629 chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
630 mChart.setLayoutTransition(chartTransition);
631 }
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700632
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700633 private static LayoutTransition buildLayoutTransition() {
634 final LayoutTransition transition = new LayoutTransition();
635 if (TEST_ANIM) {
636 transition.setDuration(1500);
637 }
638 transition.setAnimateParentHierarchy(false);
639 return transition;
640 }
641
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700642 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700643 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700644 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
645 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700646 */
647 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700648 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700649 mTabHost.clearAllTabs();
650
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700651 final boolean mobileSplit = isMobilePolicySplit();
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700652 if (mobileSplit && hasReadyMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700653 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
654 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700655 } else if (hasReadyMobileRadio(context)) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700656 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700657 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700658 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700659 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
660 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700661 if (mShowEthernet && hasEthernet(context)) {
662 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
663 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700664
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700665 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700666 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
667 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
668 if (mIntentTab != null) {
669 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700670 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700671 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700672 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700673 mTabHost.setCurrentTabByTag(mIntentTab);
674 }
675 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700676 } else if (noTabs) {
677 // no usable tabs, so hide body
678 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700679 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700680 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700681 }
682 }
683
Jeff Sharkey8a503642011-06-10 13:31:21 -0700684 /**
685 * Factory that provide empty {@link View} to make {@link TabHost} happy.
686 */
687 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700688 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700689 public View createTabContent(String tag) {
690 return new View(mTabHost.getContext());
691 }
692 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700693
Jeff Sharkey8a503642011-06-10 13:31:21 -0700694 /**
695 * Build {@link TabSpec} with thin indicator, and empty content.
696 */
697 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700698 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
699 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700700 }
701
702 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700703 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700704 public void onTabChanged(String tabId) {
705 // user changed tab; update body
706 updateBody();
707 }
708 };
709
710 /**
711 * Update body content based on current tab. Loads
712 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
713 * binds them to visible controls.
714 */
715 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700716 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700717 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700718
Jeff Sharkeya662e492011-06-18 21:57:06 -0700719 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700720 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700721 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700722
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700723 if (currentTab == null) {
724 Log.w(TAG, "no tab selected; hiding body");
725 mListView.setVisibility(View.GONE);
726 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700727 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700728 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700729 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700730
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700731 mCurrentTab = currentTab;
732
Jeff Sharkey8a503642011-06-10 13:31:21 -0700733 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
734
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700735 mDataEnabledSupported = isOwner;
736 mDisableAtLimitSupported = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700737
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700738 // TODO: remove mobile tabs when SIM isn't ready
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700739
Jeff Sharkey8a503642011-06-10 13:31:21 -0700740 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700741 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
742 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700743 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700744
745 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700746 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
747 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
748 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700749 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700750
751 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700752 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
753 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
754 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700755 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700756
757 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700758 // wifi doesn't have any controls
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700759 mDataEnabledSupported = false;
760 mDisableAtLimitSupported = false;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700761 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700762
763 } else if (TAB_ETHERNET.equals(currentTab)) {
764 // ethernet doesn't have any controls
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700765 mDataEnabledSupported = false;
766 mDisableAtLimitSupported = false;
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700767 mTemplate = buildTemplateEthernet();
768
769 } else {
770 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700771 }
772
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700773 // kick off loader for network history
774 // TODO: consider chaining two loaders together instead of reloading
775 // network history when showing app detail.
776 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700777 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700778
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700779 // detail mode can change visible menus, invalidate
780 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700781
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700782 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700783 }
784
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700785 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700786 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700787 }
788
789 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700790 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700791 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700792 */
793 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700794 final Context context = getActivity();
795 final PackageManager pm = context.getPackageManager();
796 final LayoutInflater inflater = getActivity().getLayoutInflater();
797
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700798 if (isAppDetailMode()) {
799 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700800 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700801 } else {
802 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700803 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700804
805 // hide detail stats when not in detail mode
806 mChart.bindDetailNetworkStats(null);
807 return;
808 }
809
810 // remove warning/limit sweeps while in detail mode
811 mChart.bindNetworkPolicy(null);
812
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700813 // show icon and all labels appearing under this app
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700814 final int uid = mCurrentApp.key;
815 final UidDetail detail = mUidDetailProvider.getUidDetail(uid, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700816 mAppIcon.setImageDrawable(detail.icon);
817
818 mAppTitles.removeAllViews();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700819
820 View title = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700821 if (detail.detailLabels != null) {
822 for (CharSequence label : detail.detailLabels) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700823 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
824 ((TextView) title.findViewById(R.id.app_title)).setText(label);
825 mAppTitles.addView(title);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700826 }
827 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700828 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
829 ((TextView) title.findViewById(R.id.app_title)).setText(detail.label);
830 mAppTitles.addView(title);
831 }
832
833 // Remember last slot for summary
834 if (title != null) {
835 mAppTotal = (TextView) title.findViewById(R.id.app_summary);
836 } else {
837 mAppTotal = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700838 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700839
840 // enable settings button when package provides it
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700841 final String[] packageNames = pm.getPackagesForUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700842 if (packageNames != null && packageNames.length > 0) {
843 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700844 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
845
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700846 // Search for match across all packages
847 boolean matchFound = false;
848 for (String packageName : packageNames) {
849 mAppSettingsIntent.setPackage(packageName);
850 if (pm.resolveActivity(mAppSettingsIntent, 0) != null) {
851 matchFound = true;
852 break;
853 }
854 }
855
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700856 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -0700857 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700858
859 } else {
860 mAppSettingsIntent = null;
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700861 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700862 }
863
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700864 updateDetailData();
865
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700866 if (UserHandle.isApp(uid) && !mPolicyManager.getRestrictBackground()
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700867 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700868 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800869 setPreferenceSummary(mAppRestrictView,
870 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700871
872 mAppRestrictView.setVisibility(View.VISIBLE);
873 mAppRestrict.setChecked(getAppRestrictBackground());
874
875 } else {
876 mAppRestrictView.setVisibility(View.GONE);
877 }
878 }
879
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700880 private void setPolicyWarningBytes(long warningBytes) {
881 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700882 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700883 updatePolicy(false);
884 }
885
886 private void setPolicyLimitBytes(long limitBytes) {
887 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700888 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700889 updatePolicy(false);
890 }
891
Jeff Sharkey28130d92011-09-02 16:10:24 -0700892 /**
893 * Local cache of value, used to work around delay when
894 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
895 */
896 private Boolean mMobileDataEnabled;
897
898 private boolean isMobileDataEnabled() {
899 if (mMobileDataEnabled != null) {
900 // TODO: deprecate and remove this once enabled flag is on policy
901 return mMobileDataEnabled;
902 } else {
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700903 return mTelephonyManager.getDataEnabled();
Jeff Sharkey28130d92011-09-02 16:10:24 -0700904 }
905 }
906
907 private void setMobileDataEnabled(boolean enabled) {
908 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700909 mTelephonyManager.setDataEnabled(enabled);
Jeff Sharkey28130d92011-09-02 16:10:24 -0700910 mMobileDataEnabled = enabled;
911 updatePolicy(false);
912 }
913
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700914 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700915 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked()
916 && ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700917 }
918
919 private boolean isBandwidthControlEnabled() {
920 try {
921 return mNetworkService.isBandwidthControlEnabled();
922 } catch (RemoteException e) {
923 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
924 return false;
925 }
926 }
927
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700928 public void setRestrictBackground(boolean restrictBackground) {
929 mPolicyManager.setRestrictBackground(restrictBackground);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700930 updateMenuTitles();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700931 }
932
933 private boolean getAppRestrictBackground() {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700934 final int uid = mCurrentApp.key;
935 final int uidPolicy = mPolicyManager.getUidPolicy(uid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700936 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
937 }
938
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700939 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700940 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700941 final int uid = mCurrentApp.key;
942 mPolicyManager.setUidPolicy(
943 uid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700944 mAppRestrict.setChecked(restrictBackground);
945 }
946
Jeff Sharkey8a503642011-06-10 13:31:21 -0700947 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700948 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
949 * current {@link #mTemplate}.
950 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700951 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700952 boolean dataEnabledVisible = mDataEnabledSupported;
953 boolean disableAtLimitVisible = mDisableAtLimitSupported;
954
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700955 if (isAppDetailMode()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700956 dataEnabledVisible = false;
957 disableAtLimitVisible = false;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700958 }
959
Jeff Sharkey28130d92011-09-02 16:10:24 -0700960 // TODO: move enabled state directly into policy
961 if (TAB_MOBILE.equals(mCurrentTab)) {
962 mBinding = true;
963 mDataEnabled.setChecked(isMobileDataEnabled());
964 mBinding = false;
965 }
966
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700967 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
968 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700969 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700970 if (!isAppDetailMode()) {
971 mChart.bindNetworkPolicy(policy);
972 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700973
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700974 } else {
975 // controls are disabled; don't bind warning/limit sweeps
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700976 disableAtLimitVisible = false;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700977 mChart.bindNetworkPolicy(null);
978 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700979
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700980 mDataEnabledView.setVisibility(dataEnabledVisible ? View.VISIBLE : View.GONE);
981 mDisableAtLimitView.setVisibility(disableAtLimitVisible ? View.VISIBLE : View.GONE);
982
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700983 if (refreshCycle) {
984 // generate cycle list based on policy and available history
985 updateCycleList(policy);
986 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700987 }
988
989 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700990 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
991 * and available {@link NetworkStatsHistory} data. Always selects the newest
992 * item, updating the inspection range on {@link #mChart}.
993 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700994 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700995 // stash away currently selected cycle to try restoring below
996 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700997 mCycleAdapter.clear();
998
999 final Context context = mCycleSpinner.getContext();
1000
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001001 long historyStart = Long.MAX_VALUE;
1002 long historyEnd = Long.MIN_VALUE;
1003 if (mChartData != null) {
1004 historyStart = mChartData.network.getStart();
1005 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001006 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001007
Jeff Sharkey461842a2011-09-25 18:22:48 -07001008 final long now = System.currentTimeMillis();
1009 if (historyStart == Long.MAX_VALUE) historyStart = now;
1010 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001011
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001012 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001013 if (policy != null) {
1014 // find the next cycle boundary
1015 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001016
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001017 // walk backwards, generating all valid cycle ranges
1018 while (cycleEnd > historyStart) {
1019 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
1020 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
1021 + historyStart);
1022 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1023 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001024 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001025 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001026
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001027 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001028 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001029 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001030
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001031 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001032 // no policy defined cycles; show entry for each four-week period
1033 long cycleEnd = historyEnd;
1034 while (cycleEnd > historyStart) {
1035 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
1036 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1037 cycleEnd = cycleStart;
1038 }
1039
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001040 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001041 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001042
1043 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001044 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001045 final int position = mCycleAdapter.findNearestPosition(previousItem);
1046 mCycleSpinner.setSelection(position);
1047
1048 // only force-update cycle when changed; skipping preserves any
1049 // user-defined inspection region.
1050 final CycleItem selectedItem = mCycleAdapter.getItem(position);
1051 if (!Objects.equal(selectedItem, previousItem)) {
1052 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1053 } else {
1054 // but still kick off loader for detailed list
1055 updateDetailData();
1056 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001057 } else {
1058 updateDetailData();
1059 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001060 }
1061
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001062 private View.OnClickListener mDataEnabledListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001063 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001064 public void onClick(View v) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001065 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001066
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001067 final boolean dataEnabled = !mDataEnabled.isChecked();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001068 final String currentTab = mCurrentTab;
1069 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001070 if (dataEnabled) {
1071 setMobileDataEnabled(true);
1072 } else {
1073 // disabling data; show confirmation dialog which eventually
1074 // calls setMobileDataEnabled() once user confirms.
1075 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1076 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001077 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001078
Jeff Sharkey28130d92011-09-02 16:10:24 -07001079 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001080 }
1081 };
1082
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001083 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001084 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001085 public void onClick(View v) {
1086 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001087 if (disableAtLimit) {
1088 // enabling limit; show confirmation dialog which eventually
1089 // calls setPolicyLimitBytes() once user confirms.
1090 ConfirmLimitFragment.show(DataUsageSummary.this);
1091 } else {
1092 setPolicyLimitBytes(LIMIT_DISABLED);
1093 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001094 }
1095 };
1096
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001097 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001098 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001099 public void onClick(View v) {
1100 final boolean restrictBackground = !mAppRestrict.isChecked();
1101
1102 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001103 // enabling restriction; show confirmation dialog which
1104 // eventually calls setRestrictBackground() once user
1105 // confirms.
1106 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001107 } else {
1108 setAppRestrictBackground(false);
1109 }
1110 }
1111 };
1112
1113 private OnClickListener mAppSettingsListener = new OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001114 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001115 public void onClick(View v) {
Jeff Sharkeyd92e0412012-04-24 11:35:43 -07001116 if (!isAdded()) return;
1117
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001118 // TODO: target torwards entire UID instead of just first package
1119 startActivity(mAppSettingsIntent);
1120 }
1121 };
1122
Jeff Sharkey8a503642011-06-10 13:31:21 -07001123 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001124 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001125 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001126 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001127 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001128
1129 // TODO: sigh, remove this hack once we understand 6450986
1130 if (mUidDetailProvider == null || app == null) return;
1131
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001132 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001133 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001134 }
1135 };
1136
1137 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001138 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001139 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1140 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1141 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001142 // show cycle editor; will eventually call setPolicyCycleDay()
1143 // when user finishes editing.
1144 CycleEditorFragment.show(DataUsageSummary.this);
1145
1146 // reset spinner to something other than "change cycle..."
1147 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001148
1149 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001150 if (LOGD) {
1151 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1152 + cycle.end + "]");
1153 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001154
1155 // update chart to show selected cycle, and update detail data
1156 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001157 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001158
1159 updateDetailData();
1160 }
1161 }
1162
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001163 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001164 public void onNothingSelected(AdapterView<?> parent) {
1165 // ignored
1166 }
1167 };
1168
1169 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001170 * Update details based on {@link #mChart} inspection range depending on
1171 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1172 * of applications data usage, and when {@link #isAppDetailMode()} update
1173 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001174 */
1175 private void updateDetailData() {
1176 if (LOGD) Log.d(TAG, "updateDetailData()");
1177
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001178 final long start = mChart.getInspectStart();
1179 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001180 final long now = System.currentTimeMillis();
1181
1182 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001183
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001184 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001185 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001186 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001187 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001188 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001189 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001190 final long foregroundBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001191 final long totalBytes = defaultBytes + foregroundBytes;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001192
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001193 if (mAppTotal != null) {
1194 mAppTotal.setText(Formatter.formatFileSize(context, totalBytes));
1195 }
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001196 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1197 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1198
1199 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001200 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001201
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001202 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001203
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001204 mCycleSummary.setVisibility(View.GONE);
1205
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001206 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001207 if (mChartData != null) {
1208 entry = mChartData.network.getValues(start, end, now, null);
1209 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001210
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001211 mCycleSummary.setVisibility(View.VISIBLE);
1212
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001213 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001214 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001215 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001216 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001217
1218 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1219 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001220 mCycleSummary.setText(totalPhrase);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001221
László Dávid0b8bf4e2012-10-24 23:31:47 +02001222 if (TAB_MOBILE.equals(mCurrentTab) || TAB_3G.equals(mCurrentTab)
1223 || TAB_4G.equals(mCurrentTab)) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001224 if (isAppDetailMode()) {
1225 mDisclaimer.setVisibility(View.GONE);
1226 } else {
1227 mDisclaimer.setVisibility(View.VISIBLE);
1228 }
Jeff Sharkeye557c332012-04-13 16:04:07 -07001229 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001230 mDisclaimer.setVisibility(View.GONE);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001231 }
1232
Jeff Sharkey92811822012-05-04 11:47:29 -07001233 // initial layout is finished above, ensure we have transitions
1234 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001235 }
1236
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001237 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1238 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001239 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001240 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001241 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001242 }
1243
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001244 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001245 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1246 mChartData = data;
1247 mChart.bindNetworkStats(mChartData.network);
1248 mChart.bindDetailNetworkStats(mChartData.detail);
1249
1250 // calcuate policy cycles based on available data
1251 updatePolicy(true);
1252 updateAppDetail();
1253
1254 // force scroll to top of body when showing detail
1255 if (mChartData.detail != null) {
1256 mListView.smoothScrollToPosition(0);
1257 }
1258 }
1259
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001260 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001261 public void onLoaderReset(Loader<ChartData> loader) {
1262 mChartData = null;
1263 mChart.bindNetworkStats(null);
1264 mChart.bindDetailNetworkStats(null);
1265 }
1266 };
1267
1268 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001269 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001270 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001271 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001272 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001273 }
1274
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001275 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001276 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001277 final int[] restrictedUids = mPolicyManager.getUidsWithPolicy(
Jeff Sharkeye557c332012-04-13 16:04:07 -07001278 POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001279 mAdapter.bindStats(data, restrictedUids);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001280 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001281 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001282
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001283 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001284 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001285 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001286 updateEmptyVisible();
1287 }
1288
1289 private void updateEmptyVisible() {
1290 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1291 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001292 mStupidPadding.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001293 }
1294 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001295
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001296 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001297 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001298 final Context context = getActivity();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001299 if (hasReadyMobileRadio(context)) {
1300 final TelephonyManager tele = TelephonyManager.from(context);
1301 return mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001302 } else {
1303 return false;
1304 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001305 }
1306
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001307 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001308 private void setMobilePolicySplit(boolean split) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001309 final Context context = getActivity();
1310 if (hasReadyMobileRadio(context)) {
1311 final TelephonyManager tele = TelephonyManager.from(context);
1312 mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split);
1313 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001314 }
1315
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001316 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001317 final TelephonyManager tele = TelephonyManager.from(context);
1318 final String actualSubscriberId = tele.getSubscriberId();
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -08001319 return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001320 }
1321
Jeff Sharkey8a503642011-06-10 13:31:21 -07001322 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001323 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001324 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001325 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001326 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001327
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001328 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001329 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001330 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001331 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001332
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001333 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001334 public void requestWarningEdit() {
1335 WarningEditorFragment.show(DataUsageSummary.this);
1336 }
1337
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001338 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001339 public void requestLimitEdit() {
1340 LimitEditorFragment.show(DataUsageSummary.this);
1341 }
1342 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001343
1344 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001345 * List item that reflects a specific data usage cycle.
1346 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001347 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001348 public CharSequence label;
1349 public long start;
1350 public long end;
1351
Jeff Sharkey8a503642011-06-10 13:31:21 -07001352 CycleItem(CharSequence label) {
1353 this.label = label;
1354 }
1355
1356 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001357 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001358 this.start = start;
1359 this.end = end;
1360 }
1361
Jeff Sharkey8a503642011-06-10 13:31:21 -07001362 @Override
1363 public String toString() {
1364 return label.toString();
1365 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001366
1367 @Override
1368 public boolean equals(Object o) {
1369 if (o instanceof CycleItem) {
1370 final CycleItem another = (CycleItem) o;
1371 return start == another.start && end == another.end;
1372 }
1373 return false;
1374 }
1375
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001376 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001377 public int compareTo(CycleItem another) {
1378 return Long.compare(start, another.start);
1379 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001380 }
1381
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001382 private static final StringBuilder sBuilder = new StringBuilder(50);
1383 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1384 sBuilder, Locale.getDefault());
1385
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001386 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001387 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001388
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001389 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001390 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001391 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1392 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001393 }
1394 }
1395
Jeff Sharkey8a503642011-06-10 13:31:21 -07001396 /**
1397 * Special-case data usage cycle that triggers dialog to change
1398 * {@link NetworkPolicy#cycleDay}.
1399 */
1400 public static class CycleChangeItem extends CycleItem {
1401 public CycleChangeItem(Context context) {
1402 super(context.getString(R.string.data_usage_change_cycle));
1403 }
1404 }
1405
1406 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001407 private boolean mChangePossible = false;
1408 private boolean mChangeVisible = false;
1409
1410 private final CycleChangeItem mChangeItem;
1411
Jeff Sharkey8a503642011-06-10 13:31:21 -07001412 public CycleAdapter(Context context) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001413 super(context, R.layout.data_usage_cycle_item);
1414 setDropDownViewResource(R.layout.data_usage_cycle_item_dropdown);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001415 mChangeItem = new CycleChangeItem(context);
1416 }
1417
1418 public void setChangePossible(boolean possible) {
1419 mChangePossible = possible;
1420 updateChange();
1421 }
1422
1423 public void setChangeVisible(boolean visible) {
1424 mChangeVisible = visible;
1425 updateChange();
1426 }
1427
1428 private void updateChange() {
1429 remove(mChangeItem);
1430 if (mChangePossible && mChangeVisible) {
1431 add(mChangeItem);
1432 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001433 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001434
1435 /**
1436 * Find position of {@link CycleItem} in this adapter which is nearest
1437 * the given {@link CycleItem}.
1438 */
1439 public int findNearestPosition(CycleItem target) {
1440 if (target != null) {
1441 final int count = getCount();
1442 for (int i = count - 1; i >= 0; i--) {
1443 final CycleItem item = getItem(i);
1444 if (item instanceof CycleChangeItem) {
1445 continue;
1446 } else if (item.compareTo(target) >= 0) {
1447 return i;
1448 }
1449 }
1450 }
1451 return 0;
1452 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001453 }
1454
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001455 public static class AppItem implements Comparable<AppItem>, Parcelable {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001456 public static final int CATEGORY_USER = 0;
1457 public static final int CATEGORY_APP_TITLE = 1;
1458 public static final int CATEGORY_APP = 2;
1459
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001460 public final int key;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001461 public boolean restricted;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001462 public int category;
1463
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001464 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001465 public long total;
1466
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001467 public AppItem() {
1468 this.key = 0;
1469 }
1470
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001471 public AppItem(int key) {
1472 this.key = key;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001473 }
1474
1475 public AppItem(Parcel parcel) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001476 key = parcel.readInt();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001477 uids = parcel.readSparseBooleanArray();
1478 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001479 }
1480
1481 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001482 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001483 }
1484
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001485 @Override
1486 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001487 dest.writeInt(key);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001488 dest.writeSparseBooleanArray(uids);
1489 dest.writeLong(total);
1490 }
1491
1492 @Override
1493 public int describeContents() {
1494 return 0;
1495 }
1496
1497 @Override
1498 public int compareTo(AppItem another) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001499 int comparison = Integer.compare(category, another.category);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001500 if (comparison == 0) {
1501 comparison = Long.compare(another.total, total);
1502 }
1503 return comparison;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001504 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001505
1506 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1507 @Override
1508 public AppItem createFromParcel(Parcel in) {
1509 return new AppItem(in);
1510 }
1511
1512 @Override
1513 public AppItem[] newArray(int size) {
1514 return new AppItem[size];
1515 }
1516 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001517 }
1518
Jeff Sharkey8a503642011-06-10 13:31:21 -07001519 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001520 * Adapter of applications, sorted by total usage descending.
1521 */
1522 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001523 private final UidDetailProvider mProvider;
1524 private final int mInsetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001525 private final UserManager mUm;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001526
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001527 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001528 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001529
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001530 public DataUsageAdapter(final UserManager userManager, UidDetailProvider provider, int insetSide) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001531 mProvider = checkNotNull(provider);
1532 mInsetSide = insetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001533 mUm = userManager;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001534 }
1535
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001536 /**
1537 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1538 */
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001539 public void bindStats(NetworkStats stats, int[] restrictedUids) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001540 mItems.clear();
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001541 mLargest = 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001542
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001543 final int currentUserId = ActivityManager.getCurrentUser();
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001544 final List<UserHandle> profiles = mUm.getUserProfiles();
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001545 final SparseArray<AppItem> knownItems = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001546
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001547 NetworkStats.Entry entry = null;
1548 final int size = stats != null ? stats.size() : 0;
1549 for (int i = 0; i < size; i++) {
1550 entry = stats.getValues(i, entry);
1551
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001552 // Decide how to collapse items together
1553 final int uid = entry.uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001554
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001555 final int collapseKey;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001556 final int category;
1557 final int userId = UserHandle.getUserId(uid);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001558 if (UserHandle.isApp(uid)) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001559 if (profiles.contains(new UserHandle(userId))) {
1560 if (userId != currentUserId) {
1561 // Add to a managed user item.
1562 final int managedKey = UidDetailProvider.buildKeyForUser(userId);
1563 accumulate(managedKey, knownItems, entry,
1564 AppItem.CATEGORY_USER);
1565 }
1566 // Add to app item.
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001567 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001568 category = AppItem.CATEGORY_APP;
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001569 } else {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001570 // Add to other user item.
1571 collapseKey = UidDetailProvider.buildKeyForUser(userId);
1572 category = AppItem.CATEGORY_USER;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001573 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001574 } else if (uid == UID_REMOVED || uid == UID_TETHERING) {
1575 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001576 category = AppItem.CATEGORY_APP;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001577 } else {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001578 collapseKey = android.os.Process.SYSTEM_UID;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001579 category = AppItem.CATEGORY_APP;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001580 }
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001581 accumulate(collapseKey, knownItems, entry, category);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001582 }
1583
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001584 final int restrictedUidsMax = restrictedUids.length;
1585 for (int i = 0; i < restrictedUidsMax; ++i) {
1586 final int uid = restrictedUids[i];
1587 // Only splice in restricted state for current user or managed users
1588 if (!profiles.contains(new UserHandle(UserHandle.getUserId(uid)))) {
1589 continue;
1590 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001591
1592 AppItem item = knownItems.get(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001593 if (item == null) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001594 item = new AppItem(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001595 item.total = -1;
1596 mItems.add(item);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001597 knownItems.put(item.key, item);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001598 }
1599 item.restricted = true;
1600 }
1601
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001602 if (!mItems.isEmpty()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001603 final AppItem title = new AppItem();
1604 title.category = AppItem.CATEGORY_APP_TITLE;
1605 mItems.add(title);
1606 }
1607
Jeff Sharkey8a503642011-06-10 13:31:21 -07001608 Collections.sort(mItems);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001609 notifyDataSetChanged();
1610 }
1611
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001612 /**
1613 * Accumulate data usage of a network stats entry for the item mapped by the collapse key.
1614 * Creates the item if needed.
1615 *
1616 * @param collapseKey the collapse key used to map the item.
1617 * @param knownItems collection of known (already existing) items.
1618 * @param entry the network stats entry to extract data usage from.
1619 * @param itemCategory the item is categorized on the list view by this category. Must be
1620 * either AppItem.APP_ITEM_CATEGORY or AppItem.MANAGED_USER_ITEM_CATEGORY
1621 */
1622 private void accumulate(int collapseKey, final SparseArray<AppItem> knownItems,
1623 NetworkStats.Entry entry, int itemCategory) {
1624 final int uid = entry.uid;
1625 AppItem item = knownItems.get(collapseKey);
1626 if (item == null) {
1627 item = new AppItem(collapseKey);
1628 item.category = itemCategory;
1629 mItems.add(item);
1630 knownItems.put(item.key, item);
1631 }
1632 item.addUid(uid);
1633 item.total += entry.rxBytes + entry.txBytes;
1634 if (mLargest < item.total) {
1635 mLargest = item.total;
1636 }
1637 }
1638
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001639 @Override
1640 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001641 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001642 }
1643
1644 @Override
1645 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001646 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001647 }
1648
1649 @Override
1650 public long getItemId(int position) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001651 return mItems.get(position).key;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001652 }
1653
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001654 /**
1655 * See {@link #getItemViewType} for the view types.
1656 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001657 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001658 public int getViewTypeCount() {
1659 return 2;
1660 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001661
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001662 /**
1663 * Returns 1 for separator items and 0 for anything else.
1664 */
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001665 @Override
1666 public int getItemViewType(int position) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001667 final AppItem item = mItems.get(position);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001668 if (item.category == AppItem.CATEGORY_APP_TITLE) {
1669 return 1;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001670 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001671 return 0;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001672 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001673 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001674
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001675 @Override
1676 public boolean areAllItemsEnabled() {
1677 return false;
1678 }
1679
1680 @Override
1681 public boolean isEnabled(int position) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001682 if (position > mItems.size()) {
1683 throw new ArrayIndexOutOfBoundsException();
1684 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001685 return getItemViewType(position) == 0;
1686 }
1687
1688 @Override
1689 public View getView(int position, View convertView, ViewGroup parent) {
1690 final AppItem item = mItems.get(position);
1691 if (getItemViewType(position) == 1) {
1692 if (convertView == null) {
1693 convertView = inflateCategoryHeader(LayoutInflater.from(parent.getContext()),
1694 parent);
1695 }
1696
1697 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
1698 title.setText(R.string.data_usage_app);
1699
1700 } else {
1701 if (convertView == null) {
1702 convertView = LayoutInflater.from(parent.getContext()).inflate(
1703 R.layout.data_usage_item, parent, false);
1704
1705 if (mInsetSide > 0) {
1706 convertView.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
1707 }
1708 }
1709
1710 final Context context = parent.getContext();
1711
1712 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
1713 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1714 android.R.id.progress);
1715
1716 // kick off async load of app details
1717 UidDetailTask.bindView(mProvider, item, convertView);
1718
1719 if (item.restricted && item.total <= 0) {
1720 text1.setText(R.string.data_usage_app_restricted);
1721 progress.setVisibility(View.GONE);
1722 } else {
1723 text1.setText(Formatter.formatFileSize(context, item.total));
1724 progress.setVisibility(View.VISIBLE);
1725 }
1726
1727 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1728 progress.setProgress(percentTotal);
1729 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001730
1731 return convertView;
1732 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001733 }
1734
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001735 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001736 * Empty {@link Fragment} that controls display of UID details in
1737 * {@link DataUsageSummary}.
1738 */
1739 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001740 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001741
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001742 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001743 if (!parent.isAdded()) return;
1744
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001745 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001746 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001747
1748 final AppDetailsFragment fragment = new AppDetailsFragment();
1749 fragment.setArguments(args);
1750 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001751 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1752 ft.add(fragment, TAG_APP_DETAILS);
1753 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001754 ft.setBreadCrumbTitle(
1755 parent.getResources().getString(R.string.data_usage_app_summary_title));
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001756 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001757 }
1758
1759 @Override
1760 public void onStart() {
1761 super.onStart();
1762 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001763 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001764 target.updateBody();
1765 }
1766
1767 @Override
1768 public void onStop() {
1769 super.onStop();
1770 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001771 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001772 target.updateBody();
1773 }
1774 }
1775
1776 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001777 * Dialog to request user confirmation before setting
1778 * {@link NetworkPolicy#limitBytes}.
1779 */
1780 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001781 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001782 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001783
1784 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001785 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001786
Jeff Sharkey461842a2011-09-25 18:22:48 -07001787 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001788 final CharSequence message;
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001789 final long minLimitBytes = (long) (
1790 parent.mPolicyEditor.getPolicy(parent.mTemplate).warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001791 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001792
1793 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001794 final String currentTab = parent.mCurrentTab;
1795 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001796 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001797 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001798 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001799 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001800 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001801 } else if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001802 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001803 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001804 } else {
1805 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001806 }
1807
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001808 final Bundle args = new Bundle();
1809 args.putCharSequence(EXTRA_MESSAGE, message);
1810 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1811
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001812 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1813 dialog.setArguments(args);
1814 dialog.setTargetFragment(parent, 0);
1815 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1816 }
1817
1818 @Override
1819 public Dialog onCreateDialog(Bundle savedInstanceState) {
1820 final Context context = getActivity();
1821
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001822 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001823 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1824
1825 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1826 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001827 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001828
1829 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001830 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001831 public void onClick(DialogInterface dialog, int which) {
1832 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1833 if (target != null) {
1834 target.setPolicyLimitBytes(limitBytes);
1835 }
1836 }
1837 });
1838
1839 return builder.create();
1840 }
1841 }
1842
1843 /**
1844 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1845 */
1846 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001847 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001848
1849 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001850 if (!parent.isAdded()) return;
1851
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001852 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001853 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001854
1855 final CycleEditorFragment dialog = new CycleEditorFragment();
1856 dialog.setArguments(args);
1857 dialog.setTargetFragment(parent, 0);
1858 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1859 }
1860
1861 @Override
1862 public Dialog onCreateDialog(Bundle savedInstanceState) {
1863 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001864 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1865 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001866
1867 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1868 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1869
1870 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1871 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1872
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001873 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1874 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001875
1876 cycleDayPicker.setMinValue(1);
1877 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001878 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001879 cycleDayPicker.setWrapSelectorWheel(true);
1880
1881 builder.setTitle(R.string.data_usage_cycle_editor_title);
1882 builder.setView(view);
1883
1884 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1885 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001886 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001887 public void onClick(DialogInterface dialog, int which) {
Jeff Sharkeyd277de92013-03-25 15:11:25 -07001888 // clear focus to finish pending text edits
1889 cycleDayPicker.clearFocus();
1890
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001891 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001892 final String cycleTimezone = new Time().timezone;
1893 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001894 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001895 }
1896 });
1897
1898 return builder.create();
1899 }
1900 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001901
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001902 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001903 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1904 */
1905 public static class WarningEditorFragment extends DialogFragment {
1906 private static final String EXTRA_TEMPLATE = "template";
1907
1908 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001909 if (!parent.isAdded()) return;
1910
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001911 final Bundle args = new Bundle();
1912 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1913
1914 final WarningEditorFragment dialog = new WarningEditorFragment();
1915 dialog.setArguments(args);
1916 dialog.setTargetFragment(parent, 0);
1917 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1918 }
1919
1920 @Override
1921 public Dialog onCreateDialog(Bundle savedInstanceState) {
1922 final Context context = getActivity();
1923 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1924 final NetworkPolicyEditor editor = target.mPolicyEditor;
1925
1926 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1927 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1928
1929 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1930 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1931
1932 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1933 final long warningBytes = editor.getPolicyWarningBytes(template);
1934 final long limitBytes = editor.getPolicyLimitBytes(template);
1935
1936 bytesPicker.setMinValue(0);
1937 if (limitBytes != LIMIT_DISABLED) {
1938 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1939 } else {
1940 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1941 }
1942 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1943 bytesPicker.setWrapSelectorWheel(false);
1944
1945 builder.setTitle(R.string.data_usage_warning_editor_title);
1946 builder.setView(view);
1947
1948 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1949 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001950 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001951 public void onClick(DialogInterface dialog, int which) {
1952 // clear focus to finish pending text edits
1953 bytesPicker.clearFocus();
1954
1955 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1956 editor.setPolicyWarningBytes(template, bytes);
1957 target.updatePolicy(false);
1958 }
1959 });
1960
1961 return builder.create();
1962 }
1963 }
1964
1965 /**
1966 * Dialog to edit {@link NetworkPolicy#limitBytes}.
1967 */
1968 public static class LimitEditorFragment extends DialogFragment {
1969 private static final String EXTRA_TEMPLATE = "template";
1970
1971 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001972 if (!parent.isAdded()) return;
1973
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001974 final Bundle args = new Bundle();
1975 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1976
1977 final LimitEditorFragment dialog = new LimitEditorFragment();
1978 dialog.setArguments(args);
1979 dialog.setTargetFragment(parent, 0);
1980 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
1981 }
1982
1983 @Override
1984 public Dialog onCreateDialog(Bundle savedInstanceState) {
1985 final Context context = getActivity();
1986 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1987 final NetworkPolicyEditor editor = target.mPolicyEditor;
1988
1989 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1990 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1991
1992 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1993 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1994
1995 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1996 final long warningBytes = editor.getPolicyWarningBytes(template);
1997 final long limitBytes = editor.getPolicyLimitBytes(template);
1998
1999 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09002000 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002001 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
2002 } else {
2003 bytesPicker.setMinValue(0);
2004 }
2005 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
2006 bytesPicker.setWrapSelectorWheel(false);
2007
2008 builder.setTitle(R.string.data_usage_limit_editor_title);
2009 builder.setView(view);
2010
2011 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2012 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002013 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002014 public void onClick(DialogInterface dialog, int which) {
2015 // clear focus to finish pending text edits
2016 bytesPicker.clearFocus();
2017
2018 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
2019 editor.setPolicyLimitBytes(template, bytes);
2020 target.updatePolicy(false);
2021 }
2022 });
2023
2024 return builder.create();
2025 }
2026 }
2027 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07002028 * Dialog to request user confirmation before disabling data.
2029 */
2030 public static class ConfirmDataDisableFragment extends DialogFragment {
2031 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002032 if (!parent.isAdded()) return;
2033
Jeff Sharkey28130d92011-09-02 16:10:24 -07002034 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
2035 dialog.setTargetFragment(parent, 0);
2036 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
2037 }
2038
2039 @Override
2040 public Dialog onCreateDialog(Bundle savedInstanceState) {
2041 final Context context = getActivity();
2042
2043 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2044 builder.setMessage(R.string.data_usage_disable_mobile);
2045
2046 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002047 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07002048 public void onClick(DialogInterface dialog, int which) {
2049 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2050 if (target != null) {
2051 // TODO: extend to modify policy enabled flag.
2052 target.setMobileDataEnabled(false);
2053 }
2054 }
2055 });
2056 builder.setNegativeButton(android.R.string.cancel, null);
2057
2058 return builder.create();
2059 }
2060 }
2061
2062 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002063 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07002064 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002065 */
2066 public static class ConfirmRestrictFragment extends DialogFragment {
2067 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002068 if (!parent.isAdded()) return;
2069
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002070 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
2071 dialog.setTargetFragment(parent, 0);
2072 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
2073 }
2074
2075 @Override
2076 public Dialog onCreateDialog(Bundle savedInstanceState) {
2077 final Context context = getActivity();
2078
2079 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002080 builder.setTitle(R.string.data_usage_restrict_background_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07002081 if (Utils.hasMultipleUsers(context)) {
2082 builder.setMessage(R.string.data_usage_restrict_background_multiuser);
2083 } else {
2084 builder.setMessage(R.string.data_usage_restrict_background);
2085 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002086
2087 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002088 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002089 public void onClick(DialogInterface dialog, int which) {
2090 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2091 if (target != null) {
2092 target.setRestrictBackground(true);
2093 }
2094 }
2095 });
2096 builder.setNegativeButton(android.R.string.cancel, null);
2097
2098 return builder.create();
2099 }
2100 }
2101
2102 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002103 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
2104 * change has been denied, usually based on
2105 * {@link DataUsageSummary#hasLimitedNetworks()}.
2106 */
2107 public static class DeniedRestrictFragment extends DialogFragment {
2108 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002109 if (!parent.isAdded()) return;
2110
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002111 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
2112 dialog.setTargetFragment(parent, 0);
2113 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
2114 }
2115
2116 @Override
2117 public Dialog onCreateDialog(Bundle savedInstanceState) {
2118 final Context context = getActivity();
2119
2120 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2121 builder.setTitle(R.string.data_usage_app_restrict_background);
2122 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
2123 builder.setPositiveButton(android.R.string.ok, null);
2124
2125 return builder.create();
2126 }
2127 }
2128
2129 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002130 * Dialog to request user confirmation before setting
2131 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
2132 */
2133 public static class ConfirmAppRestrictFragment extends DialogFragment {
2134 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002135 if (!parent.isAdded()) return;
2136
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002137 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2138 dialog.setTargetFragment(parent, 0);
2139 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2140 }
2141
2142 @Override
2143 public Dialog onCreateDialog(Bundle savedInstanceState) {
2144 final Context context = getActivity();
2145
2146 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002147 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2148 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2149
2150 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002151 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002152 public void onClick(DialogInterface dialog, int which) {
2153 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2154 if (target != null) {
2155 target.setAppRestrictBackground(true);
2156 }
2157 }
2158 });
2159 builder.setNegativeButton(android.R.string.cancel, null);
2160
2161 return builder.create();
2162 }
2163 }
2164
2165 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002166 * Compute default tab that should be selected, based on
2167 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2168 */
2169 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002170 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
2171 if (template == null) return null;
2172
2173 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002174 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002175 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002176 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002177 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002178 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002179 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002180 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002181 return TAB_WIFI;
2182 default:
2183 return null;
2184 }
2185 }
2186
2187 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002188 * Background task that loads {@link UidDetail}, binding to
2189 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002190 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002191 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2192 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002193 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002194 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002195
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002196 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002197 mProvider = checkNotNull(provider);
2198 mItem = checkNotNull(item);
2199 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002200 }
2201
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002202 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002203 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002204 final UidDetailTask existing = (UidDetailTask) target.getTag();
2205 if (existing != null) {
2206 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002207 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002208
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002209 final UidDetail cachedDetail = provider.getUidDetail(item.key, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002210 if (cachedDetail != null) {
2211 bindView(cachedDetail, target);
2212 } else {
2213 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2214 AsyncTask.THREAD_POOL_EXECUTOR));
2215 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002216 }
2217
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002218 private static void bindView(UidDetail detail, View target) {
2219 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2220 final TextView title = (TextView) target.findViewById(android.R.id.title);
2221
2222 if (detail != null) {
2223 icon.setImageDrawable(detail.icon);
2224 title.setText(detail.label);
2225 } else {
2226 icon.setImageDrawable(null);
2227 title.setText(null);
2228 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002229 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002230
2231 @Override
2232 protected void onPreExecute() {
2233 bindView(null, mTarget);
2234 }
2235
2236 @Override
2237 protected UidDetail doInBackground(Void... params) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002238 return mProvider.getUidDetail(mItem.key, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002239 }
2240
2241 @Override
2242 protected void onPostExecute(UidDetail result) {
2243 bindView(result, mTarget);
2244 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002245 }
2246
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002247 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002248 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002249 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002250 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002251 if (TEST_RADIOS) {
2252 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2253 }
2254
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002255 final ConnectivityManager conn = ConnectivityManager.from(context);
2256 final TelephonyManager tele = TelephonyManager.from(context);
2257
2258 // require both supported network and ready SIM
2259 return conn.isNetworkSupported(TYPE_MOBILE) && tele.getSimState() == SIM_STATE_READY;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002260 }
2261
2262 /**
2263 * Test if device has a mobile 4G data radio.
2264 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002265 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002266 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2267 return false;
2268 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002269 if (TEST_RADIOS) {
2270 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2271 }
2272
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002273 final ConnectivityManager conn = ConnectivityManager.from(context);
2274 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002275
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002276 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Wink Saville55434042012-06-14 12:33:43 -07002277 final boolean hasLte = (tele.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE)
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002278 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002279 return hasWimax || hasLte;
2280 }
2281
2282 /**
2283 * Test if device has a Wi-Fi data radio.
2284 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002285 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002286 if (TEST_RADIOS) {
2287 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2288 }
2289
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002290 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002291 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002292 }
2293
2294 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002295 * Test if device has an ethernet network connection.
2296 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002297 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002298 if (TEST_RADIOS) {
2299 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2300 }
2301
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002302 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002303 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2304
2305 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002306 if (mStatsSession != null) {
2307 try {
2308 ethernetBytes = mStatsSession.getSummaryForNetwork(
2309 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2310 .getTotalBytes();
2311 } catch (RemoteException e) {
2312 throw new RuntimeException(e);
2313 }
2314 } else {
2315 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002316 }
2317
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002318 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002319 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002320 }
2321
2322 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002323 * Inflate a {@link Preference} style layout, adding the given {@link View}
2324 * widget into {@link android.R.id#widget_frame}.
2325 */
2326 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2327 final View view = inflater.inflate(R.layout.preference, root, false);
2328 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2329 android.R.id.widget_frame);
2330 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2331 return view;
2332 }
2333
Jeff Sharkeyb6548462014-07-21 15:38:06 -07002334 private static View inflateCategoryHeader(LayoutInflater inflater, ViewGroup root) {
2335 final TypedArray a = inflater.getContext().obtainStyledAttributes(null,
2336 com.android.internal.R.styleable.Preference,
2337 com.android.internal.R.attr.preferenceCategoryStyle, 0);
2338 final int resId = a.getResourceId(com.android.internal.R.styleable.Preference_layout, 0);
2339 return inflater.inflate(resId, root, false);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002340 }
2341
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002342 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002343 * Test if any networks are currently limited.
2344 */
2345 private boolean hasLimitedNetworks() {
2346 return !buildLimitedNetworksList().isEmpty();
2347 }
2348
2349 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002350 * Build string describing currently limited networks, which defines when
2351 * background data is restricted.
2352 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002353 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002354 private CharSequence buildLimitedNetworksString() {
2355 final List<CharSequence> limited = buildLimitedNetworksList();
2356
2357 // handle case where no networks limited
2358 if (limited.isEmpty()) {
2359 limited.add(getText(R.string.data_usage_list_none));
2360 }
2361
2362 return TextUtils.join(limited);
2363 }
2364
2365 /**
2366 * Build list of currently limited networks, which defines when background
2367 * data is restricted.
2368 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002369 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002370 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002371 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002372
2373 // build combined list of all limited networks
2374 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002375
2376 final TelephonyManager tele = TelephonyManager.from(context);
2377 if (tele.getSimState() == SIM_STATE_READY) {
2378 final String subscriberId = getActiveSubscriberId(context);
2379 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2380 limited.add(getText(R.string.data_usage_list_mobile));
2381 }
2382 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2383 limited.add(getText(R.string.data_usage_tab_3g));
2384 }
2385 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2386 limited.add(getText(R.string.data_usage_tab_4g));
2387 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002388 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002389
2390 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002391 limited.add(getText(R.string.data_usage_tab_wifi));
2392 }
2393 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2394 limited.add(getText(R.string.data_usage_tab_ethernet));
2395 }
2396
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002397 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002398 }
2399
2400 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002401 * Inset both selector and divider {@link Drawable} on the given
2402 * {@link ListView} by the requested dimensions.
2403 */
2404 private static void insetListViewDrawables(ListView view, int insetSide) {
2405 final Drawable selector = view.getSelector();
2406 final Drawable divider = view.getDivider();
2407
2408 // fully unregister these drawables so callbacks can be maintained after
2409 // wrapping below.
2410 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2411 view.setSelector(stub);
2412 view.setDivider(stub);
2413
2414 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2415 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2416 }
2417
2418 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002419 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002420 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002421 */
2422 private static void setPreferenceTitle(View parent, int resId) {
2423 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2424 title.setText(resId);
2425 }
2426
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002427 /**
2428 * Set {@link android.R.id#summary} for a preference view inflated with
2429 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2430 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002431 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002432 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2433 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002434 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002435 }
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002436
2437 /**
2438 * For search
2439 */
2440 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -07002441 new BaseSearchIndexProvider() {
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002442 @Override
2443 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
2444 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
2445
2446 final Resources res = context.getResources();
2447
2448 // Add fragment title
2449 SearchIndexableRaw data = new SearchIndexableRaw(context);
2450 data.title = res.getString(R.string.data_usage_summary_title);
2451 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2452 result.add(data);
2453
2454 // Mobile data
2455 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002456 data.key = DATA_USAGE_ENABLE_MOBILE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002457 data.title = res.getString(R.string.data_usage_enable_mobile);
2458 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2459 result.add(data);
2460
2461 // Set mobile data limit
2462 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002463 data.key = DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002464 data.title = res.getString(R.string.data_usage_disable_mobile_limit);
2465 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2466 result.add(data);
2467
Fabrice Di Megliof2a52262014-04-17 17:20:27 -07002468 // Data usage cycle
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002469 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002470 data.key = DATA_USAGE_CYCLE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002471 data.title = res.getString(R.string.data_usage_cycle);
2472 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2473 result.add(data);
2474
2475 return result;
2476 }
2477 };
2478
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002479}