blob: bcd05bdef37e50761aceed184ee93d4fa4aba895 [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 Sharkeyf54f4352011-06-23 22:15:54 -070019import android.animation.LayoutTransition;
Jeff Sharkey38305fb2012-09-14 16:26:51 -070020import android.app.ActivityManager;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070021import android.app.AlertDialog;
22import android.app.Dialog;
23import android.app.DialogFragment;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070024import android.app.Fragment;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070025import android.app.FragmentTransaction;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070026import android.app.LoaderManager.LoaderCallbacks;
Jason Monk39b46742015-09-10 15:52:51 -040027import android.icu.impl.ICUResourceBundle;
28import android.icu.util.UResourceBundle;
Jeff Sharkeyb6548462014-07-21 15:38:06 -070029import android.content.ComponentName;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070030import android.content.Context;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070031import android.content.DialogInterface;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -070032import android.content.Intent;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070033import android.content.Loader;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070034import android.content.SharedPreferences;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070035import android.content.pm.PackageManager;
Jason Monk4bb075b2015-03-17 10:06:58 -040036import android.content.pm.PackageManager.NameNotFoundException;
Zoltan Szatmary-Ban77c1d362014-12-12 18:48:03 +000037import android.content.pm.UserInfo;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070038import android.content.res.Resources;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070039import android.graphics.Color;
Jeff Sharkey5d706792011-09-08 18:57:17 -070040import android.graphics.drawable.ColorDrawable;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070041import android.graphics.drawable.Drawable;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070042import android.net.ConnectivityManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070043import android.net.INetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070044import android.net.INetworkStatsService;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070045import android.net.INetworkStatsSession;
Jeff Sharkey8a503642011-06-10 13:31:21 -070046import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070047import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070048import android.net.NetworkStats;
49import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070050import android.net.NetworkTemplate;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070051import android.net.TrafficStats;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070052import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070053import android.os.Bundle;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070054import android.os.INetworkManagementService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070055import android.os.RemoteException;
56import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070057import android.os.SystemProperties;
Dianne Hackbornbb06a422012-08-16 11:01:57 -070058import android.os.UserHandle;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +010059import android.os.UserManager;
Jason Monk39b46742015-09-10 15:52:51 -040060import android.support.v7.preference.Preference;
Wink Savilleca756612014-11-08 10:47:12 -080061import android.telephony.SubscriptionInfo;
PauloftheWest50e6eca2014-10-03 11:07:14 -070062import android.telephony.SubscriptionManager;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070063import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070064import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070065import android.text.format.DateUtils;
66import android.text.format.Formatter;
Jeff Sharkeye5223a02012-03-09 17:11:14 -080067import android.text.format.Time;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070068import android.util.Log;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070069import android.util.SparseArray;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070070import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -070071import android.view.Menu;
72import android.view.MenuInflater;
73import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070074import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070075import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070076import android.view.ViewGroup;
Jeff Sharkey8a503642011-06-10 13:31:21 -070077import android.widget.AdapterView;
78import android.widget.AdapterView.OnItemClickListener;
79import android.widget.AdapterView.OnItemSelectedListener;
80import android.widget.ArrayAdapter;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070081import android.widget.BaseAdapter;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070082import android.widget.Button;
Jason Monk20f464e2015-05-06 16:00:25 -040083import android.widget.FrameLayout;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070084import android.widget.ImageView;
Jeff Sharkey8a503642011-06-10 13:31:21 -070085import android.widget.LinearLayout;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070086import android.widget.ListView;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070087import android.widget.NumberPicker;
Jeff Sharkey2412b0f2011-07-17 20:31:40 -070088import android.widget.ProgressBar;
Jeff Sharkey8a503642011-06-10 13:31:21 -070089import android.widget.Spinner;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070090import android.widget.Switch;
Jeff Sharkey8a503642011-06-10 13:31:21 -070091import android.widget.TabHost;
92import android.widget.TabHost.OnTabChangeListener;
93import android.widget.TabHost.TabContentFactory;
94import android.widget.TabHost.TabSpec;
95import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070096import android.widget.TextView;
Jason Monk20f464e2015-05-06 16:00:25 -040097import android.widget.Toast;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070098
Chris Wren8a963ba2015-03-20 10:29:14 -040099import com.android.internal.logging.MetricsLogger;
Wink Saville55434042012-06-14 12:33:43 -0700100import com.android.internal.telephony.PhoneConstants;
Jeff Sharkey5d706792011-09-08 18:57:17 -0700101import com.android.settings.drawable.InsetBoundsDrawable;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700102import com.android.settings.net.DataUsageMeteredSettings;
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -0700103import com.android.settings.search.BaseSearchIndexProvider;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -0700104import com.android.settings.search.Indexable;
105import com.android.settings.search.SearchIndexableRaw;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700106import com.android.settings.widget.ChartDataUsageView;
107import com.android.settings.widget.ChartDataUsageView.DataUsageChartListener;
PauloftheWestc80b7612014-11-25 04:40:45 -0800108import com.android.settings.widget.ChartNetworkSeriesView;
Wei Liude557d42015-07-13 12:01:42 -0700109import com.android.settingslib.AppItem;
Wei Liue0021c42015-07-07 15:37:11 -0700110import com.android.settingslib.NetworkPolicyEditor;
Wei Liude557d42015-07-13 12:01:42 -0700111import com.android.settingslib.net.ChartData;
112import com.android.settingslib.net.ChartDataLoader;
Wei Liu8dffd152015-08-20 22:33:46 -0700113import com.android.settingslib.net.SummaryForAllUidLoader;
Wei Liu6e58d512015-08-24 17:29:04 -0700114import com.android.settingslib.net.UidDetail;
115import com.android.settingslib.net.UidDetailProvider;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700116import com.google.android.collect.Lists;
117
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700118import libcore.util.Objects;
119
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700120import java.util.ArrayList;
121import java.util.Collections;
PauloftheWest50e6eca2014-10-03 11:07:14 -0700122import java.util.HashMap;
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700123import java.util.List;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700124import java.util.Locale;
PauloftheWest50e6eca2014-10-03 11:07:14 -0700125import java.util.Map;
126import java.util.Set;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700127
Jason Monk39b46742015-09-10 15:52:51 -0400128import static android.net.ConnectivityManager.TYPE_ETHERNET;
129import static android.net.ConnectivityManager.TYPE_MOBILE;
130import static android.net.ConnectivityManager.TYPE_WIFI;
131import static android.net.ConnectivityManager.TYPE_WIMAX;
132import static android.net.NetworkPolicy.LIMIT_DISABLED;
133import static android.net.NetworkPolicy.WARNING_DISABLED;
134import static android.net.NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE;
135import static android.net.NetworkPolicyManager.POLICY_NONE;
136import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND;
137import static android.net.NetworkPolicyManager.computeLastCycleBoundary;
138import static android.net.NetworkPolicyManager.computeNextCycleBoundary;
139import static android.net.NetworkTemplate.MATCH_MOBILE_3G_LOWER;
140import static android.net.NetworkTemplate.MATCH_MOBILE_4G;
141import static android.net.NetworkTemplate.MATCH_MOBILE_ALL;
142import static android.net.NetworkTemplate.MATCH_WIFI;
143import static android.net.NetworkTemplate.buildTemplateEthernet;
144import static android.net.NetworkTemplate.buildTemplateMobile3gLower;
145import static android.net.NetworkTemplate.buildTemplateMobile4g;
146import static android.net.NetworkTemplate.buildTemplateMobileAll;
147import static android.net.NetworkTemplate.buildTemplateWifiWildcard;
148import static android.net.TrafficStats.GB_IN_BYTES;
149import static android.net.TrafficStats.MB_IN_BYTES;
150import static android.net.TrafficStats.UID_REMOVED;
151import static android.net.TrafficStats.UID_TETHERING;
152import static android.telephony.TelephonyManager.SIM_STATE_READY;
153import static android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
154import static android.text.format.DateUtils.FORMAT_SHOW_DATE;
155import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
156import static com.android.internal.util.Preconditions.checkNotNull;
157import static com.android.settings.Utils.prepareCustomPreferencesList;
158
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700159/**
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700160 * Panel showing data usage history across various networks, including options
161 * to inspect based on usage cycle and control through {@link NetworkPolicy}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700162 */
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700163public class DataUsageSummary extends HighlightingFragment implements Indexable {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700164 private static final String TAG = "DataUsage";
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800165 private static final boolean LOGD = false;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700166
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700167 // TODO: remove this testing code
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700168 private static final boolean TEST_ANIM = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700169 private static final boolean TEST_RADIOS = false;
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800170
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700171 private static final String TEST_RADIOS_PROP = "test.radios";
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800172 private static final String TEST_SUBSCRIBER_PROP = "test.subscriberid";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700173
Jeff Sharkey8a503642011-06-10 13:31:21 -0700174 private static final String TAB_3G = "3g";
175 private static final String TAB_4G = "4g";
176 private static final String TAB_MOBILE = "mobile";
177 private static final String TAB_WIFI = "wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700178 private static final String TAB_ETHERNET = "ethernet";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700179
Jeff Sharkey28130d92011-09-02 16:10:24 -0700180 private static final String TAG_CONFIRM_DATA_DISABLE = "confirmDataDisable";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700181 private static final String TAG_CONFIRM_LIMIT = "confirmLimit";
182 private static final String TAG_CYCLE_EDITOR = "cycleEditor";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700183 private static final String TAG_WARNING_EDITOR = "warningEditor";
184 private static final String TAG_LIMIT_EDITOR = "limitEditor";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700185 private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700186 private static final String TAG_DENIED_RESTRICT = "deniedRestrict";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700187 private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700188 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700189
Fabrice Di Meglio2169c882014-04-18 15:18:40 -0700190 private static final String DATA_USAGE_ENABLE_MOBILE_KEY = "data_usage_enable_mobile";
191 private static final String DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY =
192 "data_usage_disable_mobile_limit";
193 private static final String DATA_USAGE_CYCLE_KEY = "data_usage_cycle";
194
Jason Monk4bb075b2015-03-17 10:06:58 -0400195 public static final String EXTRA_SHOW_APP_IMMEDIATE_PKG = "showAppImmediatePkg";
196
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700197 private static final int LOADER_CHART_DATA = 2;
198 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700199
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700200 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700201 private INetworkStatsService mStatsService;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700202 private NetworkPolicyManager mPolicyManager;
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700203 private TelephonyManager mTelephonyManager;
Wink Saville0183fb52014-11-22 10:11:39 -0800204 private SubscriptionManager mSubscriptionManager;
Xiaohui Chen44879a32015-07-22 13:53:22 -0700205 private UserManager mUserManager;
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700206
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700207 private INetworkStatsSession mStatsSession;
208
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700209 private static final String PREF_FILE = "data_usage";
210 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700211 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700212
213 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700214
Jeff Sharkey8a503642011-06-10 13:31:21 -0700215 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700216 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700217 private TabWidget mTabWidget;
218 private ListView mListView;
PauloftheWestc80b7612014-11-25 04:40:45 -0800219 private ChartNetworkSeriesView mSeries;
220 private ChartNetworkSeriesView mDetailedSeries;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700221 private DataUsageAdapter mAdapter;
222
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700223 /** Distance to inset content from sides, when needed. */
224 private int mInsetSide = 0;
225
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700226 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700227
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700228 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700229 private LinearLayout mNetworkSwitches;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700230 private boolean mDataEnabledSupported;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700231 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700232 private View mDataEnabledView;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700233 private boolean mDisableAtLimitSupported;
234 private Switch mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700235 private View mDisableAtLimitView;
236
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700237 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700238 private Spinner mCycleSpinner;
239 private CycleAdapter mCycleAdapter;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700240 private TextView mCycleSummary;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700241
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700242 private ChartDataUsageView mChart;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700243 private View mDisclaimer;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700244 private TextView mEmpty;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700245 private View mStupidPadding;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700246
247 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700248 private ImageView mAppIcon;
249 private ViewGroup mAppTitles;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700250 private TextView mAppTotal;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700251 private TextView mAppForeground;
252 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700253 private Button mAppSettings;
254
255 private LinearLayout mAppSwitches;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700256 private Switch mAppRestrict;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700257 private View mAppRestrictView;
258
Jeff Sharkey8a503642011-06-10 13:31:21 -0700259 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700260 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700261
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700262 private NetworkTemplate mTemplate;
263 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700264
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700265 private AppItem mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700266
267 private Intent mAppSettingsIntent;
268
Jeff Sharkeya662e492011-06-18 21:57:06 -0700269 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700270
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700271 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700272 private String mIntentTab = null;
273
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700274 private MenuItem mMenuRestrictBackground;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700275 private MenuItem mMenuShowWifi;
276 private MenuItem mMenuShowEthernet;
277 private MenuItem mMenuSimCards;
278 private MenuItem mMenuCellularNetworks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700279
Wink Savilleca756612014-11-08 10:47:12 -0800280 private List<SubscriptionInfo> mSubInfoList;
Wink Savilleb003a852014-10-23 10:16:26 -0700281 private Map<Integer,String> mMobileTagMap;
PauloftheWest50e6eca2014-10-03 11:07:14 -0700282
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700283 /** Flag used to ignore listeners during binding. */
284 private boolean mBinding;
285
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700286 private UidDetailProvider mUidDetailProvider;
287
Jason Monk4bb075b2015-03-17 10:06:58 -0400288 // Indicates request to show app immediately rather than list.
289 private String mShowAppImmediatePkg;
290
Sanket Padawed819270f2015-01-14 11:03:33 -0800291 /**
292 * Local cache of data enabled for subId, used to work around delays.
293 */
294 private final Map<String, Boolean> mMobileDataEnabled = new HashMap<String, Boolean>();
295
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700296 @Override
Chris Wren8a963ba2015-03-20 10:29:14 -0400297 protected int getMetricsCategory() {
298 return MetricsLogger.DATA_USAGE_SUMMARY;
299 }
300
301 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700302 public void onCreate(Bundle savedInstanceState) {
303 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700304 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700305
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700306 mNetworkService = INetworkManagementService.Stub.asInterface(
307 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700308 mStatsService = INetworkStatsService.Stub.asInterface(
309 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700310 mPolicyManager = NetworkPolicyManager.from(context);
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700311 mTelephonyManager = TelephonyManager.from(context);
Wink Saville0183fb52014-11-22 10:11:39 -0800312 mSubscriptionManager = SubscriptionManager.from(context);
Xiaohui Chen44879a32015-07-22 13:53:22 -0700313 mUserManager = UserManager.get(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700314
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700315 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700316
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700317 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700318 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700319
Wink Saville0183fb52014-11-22 10:11:39 -0800320 mSubInfoList = mSubscriptionManager.getActiveSubscriptionInfoList();
PauloftheWest50e6eca2014-10-03 11:07:14 -0700321 mMobileTagMap = initMobileTabTag(mSubInfoList);
322
Jaewan Kimffce9c12013-03-19 16:53:45 +0900323 try {
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700324 if (!mNetworkService.isBandwidthControlEnabled()) {
325 Log.w(TAG, "No bandwidth control; leaving");
326 getActivity().finish();
327 }
328 } catch (RemoteException e) {
329 Log.w(TAG, "No bandwidth control; leaving");
330 getActivity().finish();
331 }
332
333 try {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900334 mStatsSession = mStatsService.openSession();
335 } catch (RemoteException e) {
336 throw new RuntimeException(e);
337 }
338
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700339 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700340 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700341
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700342 // override preferences when no mobile radio
343 if (!hasReadyMobileRadio(context)) {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900344 mShowWifi = true;
345 mShowEthernet = true;
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700346 }
347
Jason Monk4bb075b2015-03-17 10:06:58 -0400348 mUidDetailProvider = new UidDetailProvider(context);
349
350 Bundle arguments = getArguments();
351 if (arguments != null) {
352 mShowAppImmediatePkg = arguments.getString(EXTRA_SHOW_APP_IMMEDIATE_PKG);
353 }
354
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700355 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700356 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700357
Jeff Sharkey8a503642011-06-10 13:31:21 -0700358 @Override
359 public View onCreateView(LayoutInflater inflater, ViewGroup container,
360 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700361
Jeff Sharkey8a503642011-06-10 13:31:21 -0700362 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700363 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
364
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700365
Jeff Sharkey8a503642011-06-10 13:31:21 -0700366 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700367 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700368 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
369 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700370
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700371 // decide if we need to manually inset our content, or if we should rely
372 // on parent container for inset.
373 final boolean shouldInset = mListView.getScrollBarStyle()
374 == View.SCROLLBARS_OUTSIDE_OVERLAY;
Amith Yamasani56f51a82013-08-05 10:07:23 -0700375 mInsetSide = 0;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700376
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700377 // adjust padding around tabwidget as needed
Amith Yamasani56f51a82013-08-05 10:07:23 -0700378 prepareCustomPreferencesList(container, view, mListView, false);
Jeff Sharkey5d706792011-09-08 18:57:17 -0700379
Jeff Sharkey8a503642011-06-10 13:31:21 -0700380 mTabHost.setup();
381 mTabHost.setOnTabChangedListener(mTabListener);
382
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700383 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700384 mHeader.setClickable(true);
385
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700386 mListView.addHeaderView(new View(context), null, true);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700387 mListView.addHeaderView(mHeader, null, true);
388 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700389
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700390 if (mInsetSide > 0) {
391 // inset selector and divider drawables
392 insetListViewDrawables(mListView, mInsetSide);
Fabrice Di Megliob27223f2013-01-15 18:54:11 -0800393 mHeader.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700394 }
395
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700396 {
397 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700398 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
399 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700400 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700401
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700402 mDataEnabled = new Switch(inflater.getContext());
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700403 mDataEnabled.setClickable(false);
404 mDataEnabled.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700405 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700406 mDataEnabledView.setTag(R.id.preference_highlight_key,
407 DATA_USAGE_ENABLE_MOBILE_KEY);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700408 mDataEnabledView.setClickable(true);
409 mDataEnabledView.setFocusable(true);
410 mDataEnabledView.setOnClickListener(mDataEnabledListener);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700411 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700412
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700413 mDisableAtLimit = new Switch(inflater.getContext());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700414 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700415 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700416 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700417 mDisableAtLimitView.setTag(R.id.preference_highlight_key,
418 DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700419 mDisableAtLimitView.setClickable(true);
420 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700421 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
422 mNetworkSwitches.addView(mDisableAtLimitView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700423
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700424 mCycleView = inflater.inflate(R.layout.data_usage_cycles, mNetworkSwitches, false);
425 mCycleView.setTag(R.id.preference_highlight_key, DATA_USAGE_CYCLE_KEY);
426 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
427 mCycleAdapter = new CycleAdapter(context);
428 mCycleSpinner.setAdapter(mCycleAdapter);
429 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
430 mCycleSummary = (TextView) mCycleView.findViewById(R.id.cycle_summary);
431 mNetworkSwitches.addView(mCycleView);
PauloftheWestc80b7612014-11-25 04:40:45 -0800432 mSeries = (ChartNetworkSeriesView)view.findViewById(R.id.series);
433 mDetailedSeries = (ChartNetworkSeriesView)view.findViewById(R.id.detail_series);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700434 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700435
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700436 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700437 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800438 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700439
440 {
441 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700442 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700443 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
444 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700445 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
446 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700447 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700448
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700449 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700450
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700451 mAppRestrict = new Switch(inflater.getContext());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700452 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700453 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700454 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700455 mAppRestrictView.setClickable(true);
456 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700457 mAppRestrictView.setOnClickListener(mAppRestrictListener);
458 mAppSwitches.addView(mAppRestrictView);
459 }
460
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700461 mDisclaimer = mHeader.findViewById(R.id.disclaimer);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700462 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700463 mStupidPadding = mHeader.findViewById(R.id.stupid_padding);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700464
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +0100465 final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
466 mAdapter = new DataUsageAdapter(um, mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700467 mListView.setOnItemClickListener(mListListener);
468 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700469
Jason Monk20f464e2015-05-06 16:00:25 -0400470 showRequestedAppIfNeeded(view);
Jason Monk4bb075b2015-03-17 10:06:58 -0400471
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700472 return view;
473 }
474
Jason Monk20f464e2015-05-06 16:00:25 -0400475 private void showRequestedAppIfNeeded(View rootView) {
Jason Monk4bb075b2015-03-17 10:06:58 -0400476 if (mShowAppImmediatePkg == null) {
477 return;
478 }
479 try {
480 int uid = getActivity().getPackageManager().getPackageUid(mShowAppImmediatePkg,
481 UserHandle.myUserId());
482 AppItem app = new AppItem(uid);
483 app.addUid(uid);
484
485 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
486 // When we are going straight to an app then we are coming from App Info and want
487 // a header at the top.
Jason Monk20f464e2015-05-06 16:00:25 -0400488 FrameLayout pinnedHeader = (FrameLayout) rootView.findViewById(R.id.pinned_header);
Julia Reynoldse492ee02015-08-19 10:21:52 -0400489 AppHeader.createAppHeader(getActivity(), detail.icon, detail.label,
490 mShowAppImmediatePkg, uid, pinnedHeader);
491 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label, false);
Jason Monk4bb075b2015-03-17 10:06:58 -0400492 } catch (NameNotFoundException e) {
493 Log.w(TAG, "Could not find " + mShowAppImmediatePkg, e);
494 Toast.makeText(getActivity(), getString(R.string.unknown_app), Toast.LENGTH_LONG)
495 .show();
496 getActivity().finish();
497 }
498 }
499
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700500 @Override
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700501 public void onViewStateRestored(Bundle savedInstanceState) {
502 super.onViewStateRestored(savedInstanceState);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700503
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700504 // pick default tab based on incoming intent
505 final Intent intent = getActivity().getIntent();
506 mIntentTab = computeTabFromIntent(intent);
507
Jeff Sharkey8a503642011-06-10 13:31:21 -0700508 // this kicks off chain reaction which creates tabs, binds the body to
509 // selected network, and binds chart, cycles and detail list.
510 updateTabs();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700511 }
512
513 @Override
514 public void onResume() {
515 super.onResume();
516
517 getView().post(new Runnable() {
518 @Override
519 public void run() {
520 highlightViewIfNeeded();
521 }
522 });
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700523
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700524 // kick off background task to update stats
525 new AsyncTask<Void, Void, Void>() {
526 @Override
527 protected Void doInBackground(Void... params) {
528 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700529 // wait a few seconds before kicking off
530 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700531 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700532 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700533 } catch (RemoteException e) {
534 }
535 return null;
536 }
537
538 @Override
539 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700540 if (isAdded()) {
541 updateBody();
542 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700543 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700544 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700545 }
546
Jeff Sharkey8a503642011-06-10 13:31:21 -0700547 @Override
548 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
549 inflater.inflate(R.menu.data_usage, menu);
550 }
551
552 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700553 public void onPrepareOptionsMenu(Menu menu) {
Jason Monk886978f2015-11-10 14:54:18 -0500554 final Context context = getContext();
Jason Monke7641652015-11-10 15:09:34 -0500555 if (context == null) return;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700556 final boolean appDetailMode = isAppDetailMode();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700557 final boolean isAdmin = mUserManager.isAdminUser();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700558
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700559 mMenuShowWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700560 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
561 mMenuShowWifi.setVisible(!appDetailMode);
562 } else {
563 mMenuShowWifi.setVisible(false);
564 }
565
566 mMenuShowEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
567 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
568 mMenuShowEthernet.setVisible(!appDetailMode);
569 } else {
570 mMenuShowEthernet.setVisible(false);
571 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700572
573 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700574 mMenuRestrictBackground.setVisible(
Xiaohui Chen44879a32015-07-22 13:53:22 -0700575 hasReadyMobileRadio(context) && isAdmin && !appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700576
577 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
578 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700579 metered.setVisible(!appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700580 } else {
581 metered.setVisible(false);
582 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700583
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700584 // TODO: show when multiple sims available
585 mMenuSimCards = menu.findItem(R.id.data_usage_menu_sim_cards);
586 mMenuSimCards.setVisible(false);
587
588 mMenuCellularNetworks = menu.findItem(R.id.data_usage_menu_cellular_networks);
PauloftheWesta4b8fb32014-09-18 10:12:25 -0700589 mMenuCellularNetworks.setVisible(hasReadyMobileRadio(context)
Xiaohui Chen44879a32015-07-22 13:53:22 -0700590 && !appDetailMode && isAdmin);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700591
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700592 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
593 String helpUrl;
594 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
Jason Monk6e613472015-06-16 10:31:38 -0400595 HelpUtils.prepareHelpMenuItem(getActivity(), help, helpUrl, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700596 } else {
597 help.setVisible(false);
598 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700599
600 updateMenuTitles();
601 }
602
603 private void updateMenuTitles() {
604 if (mPolicyManager.getRestrictBackground()) {
605 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_allow_background);
606 } else {
607 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_restrict_background);
608 }
609
610 if (mShowWifi) {
611 mMenuShowWifi.setTitle(R.string.data_usage_menu_hide_wifi);
612 } else {
613 mMenuShowWifi.setTitle(R.string.data_usage_menu_show_wifi);
614 }
615
616 if (mShowEthernet) {
617 mMenuShowEthernet.setTitle(R.string.data_usage_menu_hide_ethernet);
618 } else {
619 mMenuShowEthernet.setTitle(R.string.data_usage_menu_show_ethernet);
620 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700621 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700622
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700623 @Override
624 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700625 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700626 case R.id.data_usage_menu_restrict_background: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700627 final boolean restrictBackground = !mPolicyManager.getRestrictBackground();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700628 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800629 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700630 } else {
631 // no confirmation to drop restriction
632 setRestrictBackground(false);
633 }
634 return true;
635 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700636 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700637 mShowWifi = !mShowWifi;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700638 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700639 updateMenuTitles();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700640 updateTabs();
641 return true;
642 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700643 case R.id.data_usage_menu_show_ethernet: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700644 mShowEthernet = !mShowEthernet;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700645 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700646 updateMenuTitles();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700647 updateTabs();
648 return true;
649 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700650 case R.id.data_usage_menu_sim_cards: {
651 // TODO: hook up to sim cards
652 return true;
653 }
654 case R.id.data_usage_menu_cellular_networks: {
655 final Intent intent = new Intent(Intent.ACTION_MAIN);
656 intent.setComponent(new ComponentName("com.android.phone",
657 "com.android.phone.MobileNetworkSettings"));
658 startActivity(intent);
659 return true;
660 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700661 case R.id.data_usage_menu_metered: {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800662 final SettingsActivity sa = (SettingsActivity) getActivity();
663 sa.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700664 R.string.data_usage_metered_title, null, this, 0);
665 return true;
666 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700667 }
668 return false;
669 }
670
Jeff Sharkey94a90952011-06-13 22:31:09 -0700671 @Override
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700672 public void onDestroy() {
Jeff Sharkey94a90952011-06-13 22:31:09 -0700673 mDataEnabledView = null;
674 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700675
676 mUidDetailProvider.clearCache();
677 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700678
679 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700680
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800681 super.onDestroy();
682 }
683
Jeff Sharkey8a503642011-06-10 13:31:21 -0700684 /**
Jeff Sharkey92811822012-05-04 11:47:29 -0700685 * Build and assign {@link LayoutTransition} to various containers. Should
686 * only be assigned after initial layout is complete.
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700687 */
Jeff Sharkey92811822012-05-04 11:47:29 -0700688 private void ensureLayoutTransitions() {
Jason Monk4bb075b2015-03-17 10:06:58 -0400689 if (mShowAppImmediatePkg != null) {
690 // If we are skipping right to showing an app, we don't care about transitions.
691 return;
692 }
Jeff Sharkey92811822012-05-04 11:47:29 -0700693 // skip when already setup
694 if (mChart.getLayoutTransition() != null) return;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700695
Jeff Sharkey92811822012-05-04 11:47:29 -0700696 mTabsContainer.setLayoutTransition(buildLayoutTransition());
697 mHeader.setLayoutTransition(buildLayoutTransition());
698 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700699
Jeff Sharkey92811822012-05-04 11:47:29 -0700700 final LayoutTransition chartTransition = buildLayoutTransition();
701 chartTransition.disableTransitionType(LayoutTransition.APPEARING);
702 chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
703 mChart.setLayoutTransition(chartTransition);
704 }
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700705
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700706 private static LayoutTransition buildLayoutTransition() {
707 final LayoutTransition transition = new LayoutTransition();
708 if (TEST_ANIM) {
709 transition.setDuration(1500);
710 }
711 transition.setAnimateParentHierarchy(false);
712 return transition;
713 }
714
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700715 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700716 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700717 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
718 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700719 */
720 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700721 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700722 mTabHost.clearAllTabs();
723
Sanket Padawe7e21fa52015-02-12 12:39:34 -0800724 int simCount = mTelephonyManager.getSimCount();
725
Stuart Scottf51d0062015-04-08 09:40:48 -0700726 List<SubscriptionInfo> sirs = mSubscriptionManager.getActiveSubscriptionInfoList();
727 if (sirs != null) {
728 for (SubscriptionInfo sir : sirs) {
Sanket Padawe7e21fa52015-02-12 12:39:34 -0800729 addMobileTab(context, sir, (simCount > 1));
PauloftheWestc80b7612014-11-25 04:40:45 -0800730 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700731 }
PauloftheWestc80b7612014-11-25 04:40:45 -0800732
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700733 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700734 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
735 }
PauloftheWestc80b7612014-11-25 04:40:45 -0800736
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700737 if (mShowEthernet && hasEthernet(context)) {
738 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
739 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700740
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700741 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700742 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
743 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
744 if (mIntentTab != null) {
745 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700746 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700747 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700748 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700749 mTabHost.setCurrentTabByTag(mIntentTab);
750 }
751 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700752 } else if (noTabs) {
753 // no usable tabs, so hide body
754 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700755 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700756 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700757 }
758 }
759
Jeff Sharkey8a503642011-06-10 13:31:21 -0700760 /**
761 * Factory that provide empty {@link View} to make {@link TabHost} happy.
762 */
763 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700764 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700765 public View createTabContent(String tag) {
766 return new View(mTabHost.getContext());
767 }
768 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700769
Jeff Sharkey8a503642011-06-10 13:31:21 -0700770 /**
771 * Build {@link TabSpec} with thin indicator, and empty content.
772 */
773 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700774 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
775 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700776 }
777
PauloftheWest50e6eca2014-10-03 11:07:14 -0700778 /**
779 * Build {@link TabSpec} with thin indicator, and empty content.
780 */
Stuart Scottb1531812014-11-04 14:52:23 -0800781 private TabSpec buildTabSpec(String tag, CharSequence title) {
PauloftheWest50e6eca2014-10-03 11:07:14 -0700782 return mTabHost.newTabSpec(tag).setIndicator(title).setContent(
783 mEmptyTabContent);
784 }
785
786
Jeff Sharkey8a503642011-06-10 13:31:21 -0700787 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700788 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700789 public void onTabChanged(String tabId) {
790 // user changed tab; update body
791 updateBody();
792 }
793 };
794
795 /**
796 * Update body content based on current tab. Loads
797 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
798 * binds them to visible controls.
799 */
800 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700801 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700802 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700803
Jeff Sharkeya662e492011-06-18 21:57:06 -0700804 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700805 final String currentTab = mTabHost.getCurrentTabTag();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700806 final boolean isAdmin = mUserManager.isAdminUser();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700807
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700808 if (currentTab == null) {
809 Log.w(TAG, "no tab selected; hiding body");
810 mListView.setVisibility(View.GONE);
811 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700812 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700813 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700814 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700815
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700816 mCurrentTab = currentTab;
817
Jeff Sharkey8a503642011-06-10 13:31:21 -0700818 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
819
Xiaohui Chen44879a32015-07-22 13:53:22 -0700820 mDataEnabledSupported = isAdmin;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700821 mDisableAtLimitSupported = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700822
Wink Savillefcec91f2014-12-02 17:12:08 -0800823 // TODO: remove mobile tabs when SIM isn't ready probably by
824 // TODO: using SubscriptionManager.getActiveSubscriptionInfoList.
825 if (LOGD) Log.d(TAG, "updateBody() isMobileTab=" + isMobileTab(currentTab));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700826
PauloftheWest50e6eca2014-10-03 11:07:14 -0700827 if (isMobileTab(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800828 if (LOGD) Log.d(TAG, "updateBody() mobile tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700829 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
830 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
PauloftheWest50e6eca2014-10-03 11:07:14 -0700831 mDataEnabledSupported = isMobileDataAvailable(getSubId(currentTab));
Jeff Sharkey4e2d16c2014-12-03 13:44:26 -0800832
833 // Match mobile traffic for this subscriber, but normalize it to
834 // catch any other merged subscribers.
835 mTemplate = buildTemplateMobileAll(
836 getActiveSubscriberId(context, getSubId(currentTab)));
837 mTemplate = NetworkTemplate.normalize(mTemplate,
838 mTelephonyManager.getMergedSubscriberIds());
839
Jeff Sharkey8a503642011-06-10 13:31:21 -0700840 } else if (TAB_3G.equals(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800841 if (LOGD) Log.d(TAG, "updateBody() 3g tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700842 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
843 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
844 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700845 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700846
847 } else if (TAB_4G.equals(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800848 if (LOGD) Log.d(TAG, "updateBody() 4g tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700849 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
850 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
851 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700852 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700853
854 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700855 // wifi doesn't have any controls
Wink Savillefcec91f2014-12-02 17:12:08 -0800856 if (LOGD) Log.d(TAG, "updateBody() wifi tab");
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700857 mDataEnabledSupported = false;
858 mDisableAtLimitSupported = false;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700859 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700860
861 } else if (TAB_ETHERNET.equals(currentTab)) {
862 // ethernet doesn't have any controls
Wink Savillefcec91f2014-12-02 17:12:08 -0800863 if (LOGD) Log.d(TAG, "updateBody() ethernet tab");
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700864 mDataEnabledSupported = false;
865 mDisableAtLimitSupported = false;
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700866 mTemplate = buildTemplateEthernet();
867
868 } else {
Wink Savillefcec91f2014-12-02 17:12:08 -0800869 if (LOGD) Log.d(TAG, "updateBody() unknown tab");
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700870 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700871 }
872
Sanket Padawe716b1f82015-06-24 14:26:21 -0700873 mPolicyEditor.read();
874 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
875 if (policy != null) {
876 final long currentTime = System.currentTimeMillis();
877 final long start = computeLastCycleBoundary(currentTime, policy);
878 final long end = currentTime;
879 long totalBytes = 0;
880
881 try {
882 totalBytes = mStatsService.getNetworkTotalBytes(policy.template, start, end);
883 } catch (RuntimeException e) {
884 } catch (RemoteException e) {
885 }
886
887 if (policy.isOverLimit(totalBytes) && policy.lastLimitSnooze < start) {
888 setPreferenceSummary(mDataEnabledView,
889 getString(R.string.data_usage_cellular_data_summary));
890 } else {
891 final TextView summary = (TextView) mDataEnabledView
892 .findViewById(android.R.id.summary);
893 summary.setVisibility(View.GONE);
894 }
895 }
896
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700897 // kick off loader for network history
898 // TODO: consider chaining two loaders together instead of reloading
899 // network history when showing app detail.
900 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700901 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700902
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700903 // detail mode can change visible menus, invalidate
904 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700905
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700906 mBinding = false;
PauloftheWestc80b7612014-11-25 04:40:45 -0800907
Alan Viverette55eaa962015-03-18 18:10:52 -0700908 int seriesColor = context.getColor(R.color.sim_noitification);
PauloftheWestc80b7612014-11-25 04:40:45 -0800909 if (mCurrentTab != null && mCurrentTab.length() > TAB_MOBILE.length() ){
910 final int slotId = Integer.parseInt(mCurrentTab.substring(TAB_MOBILE.length(),
911 mCurrentTab.length()));
Stuart Scottf51d0062015-04-08 09:40:48 -0700912 final SubscriptionInfo sir = mSubscriptionManager
913 .getActiveSubscriptionInfoForSimSlotIndex(slotId);
PauloftheWestc80b7612014-11-25 04:40:45 -0800914
915 if (sir != null) {
916 seriesColor = sir.getIconTint();
917 }
918 }
919
Jeff Sharkey9c167e92015-01-26 15:15:59 -0700920 final int secondaryColor = Color.argb(127, Color.red(seriesColor), Color.green(seriesColor),
921 Color.blue(seriesColor));
922 mSeries.setChartColor(Color.BLACK, seriesColor, secondaryColor);
923 mDetailedSeries.setChartColor(Color.BLACK, seriesColor, secondaryColor);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700924 }
925
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700926 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700927 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700928 }
929
930 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700931 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700932 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700933 */
934 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700935 final Context context = getActivity();
936 final PackageManager pm = context.getPackageManager();
937 final LayoutInflater inflater = getActivity().getLayoutInflater();
938
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700939 if (isAppDetailMode()) {
940 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700941 mCycleAdapter.setChangeVisible(false);
Julia Reynolds1740ce42015-07-27 16:00:06 -0400942 mChart.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700943 } else {
944 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700945 mCycleAdapter.setChangeVisible(true);
Julia Reynolds1740ce42015-07-27 16:00:06 -0400946 mChart.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700947
948 // hide detail stats when not in detail mode
949 mChart.bindDetailNetworkStats(null);
950 return;
951 }
952
953 // remove warning/limit sweeps while in detail mode
954 mChart.bindNetworkPolicy(null);
955
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700956 // show icon and all labels appearing under this app
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700957 final int uid = mCurrentApp.key;
958 final UidDetail detail = mUidDetailProvider.getUidDetail(uid, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700959 mAppIcon.setImageDrawable(detail.icon);
960
961 mAppTitles.removeAllViews();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700962
963 View title = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700964 if (detail.detailLabels != null) {
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100965 final int n = detail.detailLabels.length;
966 for (int i = 0; i < n; ++i) {
967 CharSequence label = detail.detailLabels[i];
968 CharSequence contentDescription = detail.detailContentDescriptions[i];
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700969 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100970 TextView appTitle = (TextView) title.findViewById(R.id.app_title);
971 appTitle.setText(label);
972 appTitle.setContentDescription(contentDescription);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700973 mAppTitles.addView(title);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700974 }
975 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700976 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100977 TextView appTitle = (TextView) title.findViewById(R.id.app_title);
978 appTitle.setText(detail.label);
979 appTitle.setContentDescription(detail.contentDescription);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700980 mAppTitles.addView(title);
981 }
982
983 // Remember last slot for summary
984 if (title != null) {
985 mAppTotal = (TextView) title.findViewById(R.id.app_summary);
986 } else {
987 mAppTotal = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700988 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700989
990 // enable settings button when package provides it
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700991 final String[] packageNames = pm.getPackagesForUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700992 if (packageNames != null && packageNames.length > 0) {
993 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700994 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
995
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700996 // Search for match across all packages
997 boolean matchFound = false;
998 for (String packageName : packageNames) {
999 mAppSettingsIntent.setPackage(packageName);
1000 if (pm.resolveActivity(mAppSettingsIntent, 0) != null) {
1001 matchFound = true;
1002 break;
1003 }
1004 }
1005
Zoltan Szatmary-Banf20d39e2014-08-07 15:27:08 +01001006 mAppSettings.setOnClickListener(new OnClickListener() {
1007 @Override
1008 public void onClick(View v) {
1009 if (!isAdded()) {
1010 return;
1011 }
1012
1013 // TODO: target towards entire UID instead of just first package
1014 getActivity().startActivityAsUser(mAppSettingsIntent,
1015 new UserHandle(UserHandle.getUserId(uid)));
1016 }
1017 });
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001018 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -07001019 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001020
1021 } else {
1022 mAppSettingsIntent = null;
Zoltan Szatmary-Banf20d39e2014-08-07 15:27:08 +01001023 mAppSettings.setOnClickListener(null);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001024 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001025 }
1026
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001027 updateDetailData();
1028
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001029 if (UserHandle.isApp(uid) && !mPolicyManager.getRestrictBackground()
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001030 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001031 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -08001032 setPreferenceSummary(mAppRestrictView,
1033 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001034
1035 mAppRestrictView.setVisibility(View.VISIBLE);
1036 mAppRestrict.setChecked(getAppRestrictBackground());
1037
1038 } else {
1039 mAppRestrictView.setVisibility(View.GONE);
1040 }
1041 }
1042
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001043 private void setPolicyWarningBytes(long warningBytes) {
1044 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -07001045 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001046 updatePolicy(false);
1047 }
1048
1049 private void setPolicyLimitBytes(long limitBytes) {
1050 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -07001051 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001052 updatePolicy(false);
1053 }
1054
Wink Savilleb003a852014-10-23 10:16:26 -07001055 private boolean isMobileDataEnabled(int subId) {
Wink Savillea4f4d182014-12-05 15:34:46 -08001056 if (LOGD) Log.d(TAG, "isMobileDataEnabled:+ subId=" + subId);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001057 boolean isEnable = false;
Sanket Padawed819270f2015-01-14 11:03:33 -08001058 if (mMobileDataEnabled.get(String.valueOf(subId)) != null) {
1059 //TODO: deprecate and remove this once enabled flag is on policy
1060 //Multiple Subscriptions, the value need to be reseted
1061 isEnable = mMobileDataEnabled.get(String.valueOf(subId)).booleanValue();
Wink Savillea4f4d182014-12-05 15:34:46 -08001062 if (LOGD) {
1063 Log.d(TAG, "isMobileDataEnabled: != null, subId=" + subId
1064 + " isEnable=" + isEnable);
1065 }
Sanket Padawed819270f2015-01-14 11:03:33 -08001066 mMobileDataEnabled.put(String.valueOf(subId), null);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001067 } else {
Wink Savillea4f4d182014-12-05 15:34:46 -08001068 // SUB SELECT
1069 isEnable = mTelephonyManager.getDataEnabled(subId);
1070 if (LOGD) {
1071 Log.d(TAG, "isMobileDataEnabled: == null, subId=" + subId
1072 + " isEnable=" + isEnable);
1073 }
Jeff Sharkey28130d92011-09-02 16:10:24 -07001074 }
PauloftheWest50e6eca2014-10-03 11:07:14 -07001075 return isEnable;
Jeff Sharkey28130d92011-09-02 16:10:24 -07001076 }
1077
Sanket Padawe24f834d2015-01-08 11:23:11 -08001078 private void setMobileDataEnabled(int subId, boolean enabled) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001079 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
Sanket Padawe24f834d2015-01-08 11:23:11 -08001080 mTelephonyManager.setDataEnabled(subId, enabled);
Sanket Padawed819270f2015-01-14 11:03:33 -08001081 mMobileDataEnabled.put(String.valueOf(subId), enabled);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001082 updatePolicy(false);
1083 }
1084
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001085 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001086 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked()
Xiaohui Chen44879a32015-07-22 13:53:22 -07001087 && mUserManager.isAdminUser();
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001088 }
1089
1090 private boolean isBandwidthControlEnabled() {
1091 try {
1092 return mNetworkService.isBandwidthControlEnabled();
1093 } catch (RemoteException e) {
1094 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
1095 return false;
1096 }
1097 }
1098
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001099 public void setRestrictBackground(boolean restrictBackground) {
1100 mPolicyManager.setRestrictBackground(restrictBackground);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001101 updateMenuTitles();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001102 }
1103
1104 private boolean getAppRestrictBackground() {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001105 final int uid = mCurrentApp.key;
1106 final int uidPolicy = mPolicyManager.getUidPolicy(uid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001107 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
1108 }
1109
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001110 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001111 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001112 final int uid = mCurrentApp.key;
1113 mPolicyManager.setUidPolicy(
1114 uid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001115 mAppRestrict.setChecked(restrictBackground);
1116 }
1117
Jeff Sharkey8a503642011-06-10 13:31:21 -07001118 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001119 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
1120 * current {@link #mTemplate}.
1121 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001122 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001123 boolean dataEnabledVisible = mDataEnabledSupported;
1124 boolean disableAtLimitVisible = mDisableAtLimitSupported;
1125
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001126 if (isAppDetailMode()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001127 dataEnabledVisible = false;
1128 disableAtLimitVisible = false;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001129 }
1130
Jeff Sharkey28130d92011-09-02 16:10:24 -07001131 // TODO: move enabled state directly into policy
PauloftheWest50e6eca2014-10-03 11:07:14 -07001132 if (isMobileTab(mCurrentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001133 mBinding = true;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001134 mDataEnabled.setChecked(isMobileDataEnabled(getSubId(mCurrentTab)));
Jeff Sharkey28130d92011-09-02 16:10:24 -07001135 mBinding = false;
1136 }
1137
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001138 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001139 //SUB SELECT
1140 if (isNetworkPolicyModifiable(policy) && isMobileDataAvailable(getSubId(mCurrentTab))) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001141 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001142 if (!isAppDetailMode()) {
1143 mChart.bindNetworkPolicy(policy);
1144 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001145
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001146 } else {
1147 // controls are disabled; don't bind warning/limit sweeps
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001148 disableAtLimitVisible = false;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001149 mChart.bindNetworkPolicy(null);
1150 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001151
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001152 mDataEnabledView.setVisibility(dataEnabledVisible ? View.VISIBLE : View.GONE);
1153 mDisableAtLimitView.setVisibility(disableAtLimitVisible ? View.VISIBLE : View.GONE);
1154
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001155 if (refreshCycle) {
1156 // generate cycle list based on policy and available history
1157 updateCycleList(policy);
1158 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001159 }
1160
1161 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001162 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
1163 * and available {@link NetworkStatsHistory} data. Always selects the newest
1164 * item, updating the inspection range on {@link #mChart}.
1165 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001166 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001167 // stash away currently selected cycle to try restoring below
1168 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -07001169 mCycleAdapter.clear();
1170
1171 final Context context = mCycleSpinner.getContext();
Jeff Sharkey02551ec2015-10-19 17:22:05 -07001172 NetworkStatsHistory.Entry entry = null;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001173
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001174 long historyStart = Long.MAX_VALUE;
1175 long historyEnd = Long.MIN_VALUE;
1176 if (mChartData != null) {
1177 historyStart = mChartData.network.getStart();
1178 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001179 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001180
Jeff Sharkey461842a2011-09-25 18:22:48 -07001181 final long now = System.currentTimeMillis();
1182 if (historyStart == Long.MAX_VALUE) historyStart = now;
1183 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001184
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001185 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001186 if (policy != null) {
1187 // find the next cycle boundary
1188 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001189
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001190 // walk backwards, generating all valid cycle ranges
1191 while (cycleEnd > historyStart) {
1192 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
1193 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
1194 + historyStart);
Jeff Sharkey02551ec2015-10-19 17:22:05 -07001195
1196 final boolean includeCycle;
1197 if (mChartData != null) {
1198 entry = mChartData.network.getValues(cycleStart, cycleEnd, entry);
1199 includeCycle = (entry.rxBytes + entry.txBytes) > 0;
1200 } else {
1201 includeCycle = true;
1202 }
1203
1204 if (includeCycle) {
1205 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1206 hasCycles = true;
1207 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001208 cycleEnd = cycleStart;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001209 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001210
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001211 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001212 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001213 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001214
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001215 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001216 // no policy defined cycles; show entry for each four-week period
1217 long cycleEnd = historyEnd;
1218 while (cycleEnd > historyStart) {
1219 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
Jeff Sharkey02551ec2015-10-19 17:22:05 -07001220
1221 final boolean includeCycle;
1222 if (mChartData != null) {
1223 entry = mChartData.network.getValues(cycleStart, cycleEnd, entry);
1224 includeCycle = (entry.rxBytes + entry.txBytes) > 0;
1225 } else {
1226 includeCycle = true;
1227 }
1228
1229 if (includeCycle) {
1230 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1231 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001232 cycleEnd = cycleStart;
1233 }
1234
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001235 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001236 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001237
1238 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001239 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001240 final int position = mCycleAdapter.findNearestPosition(previousItem);
1241 mCycleSpinner.setSelection(position);
1242
1243 // only force-update cycle when changed; skipping preserves any
1244 // user-defined inspection region.
1245 final CycleItem selectedItem = mCycleAdapter.getItem(position);
1246 if (!Objects.equal(selectedItem, previousItem)) {
1247 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1248 } else {
1249 // but still kick off loader for detailed list
1250 updateDetailData();
1251 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001252 } else {
1253 updateDetailData();
1254 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001255 }
1256
Sanket Padawed819270f2015-01-14 11:03:33 -08001257 private void disableDataForOtherSubscriptions(SubscriptionInfo currentSir) {
1258 if (mSubInfoList != null) {
1259 for (SubscriptionInfo subInfo : mSubInfoList) {
1260 if (subInfo.getSubscriptionId() != currentSir.getSubscriptionId()) {
1261 setMobileDataEnabled(subInfo.getSubscriptionId(), false);
1262 }
1263 }
1264 }
1265 }
1266
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001267 private View.OnClickListener mDataEnabledListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001268 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001269 public void onClick(View v) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001270 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001271
Chris Wren1b6ffba2015-05-08 17:10:01 -04001272 final boolean enabled = !mDataEnabled.isChecked();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001273 final String currentTab = mCurrentTab;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001274 if (isMobileTab(currentTab)) {
Chris Wren1b6ffba2015-05-08 17:10:01 -04001275 MetricsLogger.action(getContext(), MetricsLogger.ACTION_CELL_DATA_TOGGLE, enabled);
1276 if (enabled) {
PauloftheWestde338442014-10-29 14:46:44 -07001277 // If we are showing the Sim Card tile then we are a Multi-Sim device.
1278 if (Utils.showSimCardTile(getActivity())) {
1279 handleMultiSimDataDialog();
1280 } else {
Sanket Padawe24f834d2015-01-08 11:23:11 -08001281 setMobileDataEnabled(getSubId(currentTab), true);
PauloftheWestde338442014-10-29 14:46:44 -07001282 }
Jeff Sharkey28130d92011-09-02 16:10:24 -07001283 } else {
1284 // disabling data; show confirmation dialog which eventually
1285 // calls setMobileDataEnabled() once user confirms.
Sanket Padawe24f834d2015-01-08 11:23:11 -08001286 ConfirmDataDisableFragment.show(DataUsageSummary.this, getSubId(mCurrentTab));
Jeff Sharkey28130d92011-09-02 16:10:24 -07001287 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001288 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001289
Jeff Sharkey28130d92011-09-02 16:10:24 -07001290 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001291 }
1292 };
1293
PauloftheWestde338442014-10-29 14:46:44 -07001294 private void handleMultiSimDataDialog() {
PauloftheWestf0d84e22014-12-10 08:50:54 -08001295 final Context context = getActivity();
1296 final SubscriptionInfo currentSir = getCurrentTabSubInfo(context);
Sanket Padawe766ab602015-01-20 10:36:15 -08001297
1298 //If sim has not loaded after toggling data switch, return.
1299 if (currentSir == null) {
1300 return;
1301 }
1302
Stuart Scottf51d0062015-04-08 09:40:48 -07001303 final SubscriptionInfo nextSir = mSubscriptionManager.getDefaultDataSubscriptionInfo();
PauloftheWestde338442014-10-29 14:46:44 -07001304
PauloftheWestf0d84e22014-12-10 08:50:54 -08001305 // If the device is single SIM or is enabling data on the active data SIM then forgo
1306 // the pop-up.
1307 if (!Utils.showSimCardTile(context) ||
Sanket Padawed819270f2015-01-14 11:03:33 -08001308 (nextSir != null && currentSir != null &&
1309 currentSir.getSubscriptionId() == nextSir.getSubscriptionId())) {
Sanket Padawe24f834d2015-01-08 11:23:11 -08001310 setMobileDataEnabled(currentSir.getSubscriptionId(), true);
Sanket Padawed819270f2015-01-14 11:03:33 -08001311 if (nextSir != null && currentSir != null &&
1312 currentSir.getSubscriptionId() == nextSir.getSubscriptionId()) {
1313 disableDataForOtherSubscriptions(currentSir);
1314 }
PauloftheWestde338442014-10-29 14:46:44 -07001315 updateBody();
1316 return;
1317 }
1318
PauloftheWestf0d84e22014-12-10 08:50:54 -08001319 final String previousName = (nextSir == null)
1320 ? context.getResources().getString(R.string.sim_selection_required_pref)
1321 : nextSir.getDisplayName().toString();
1322
PauloftheWestde338442014-10-29 14:46:44 -07001323 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
1324
1325 builder.setTitle(R.string.sim_change_data_title);
1326 builder.setMessage(getActivity().getResources().getString(R.string.sim_change_data_message,
PauloftheWestf0d84e22014-12-10 08:50:54 -08001327 currentSir.getDisplayName(), previousName));
PauloftheWestde338442014-10-29 14:46:44 -07001328
1329 builder.setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
1330 @Override
1331 public void onClick(DialogInterface dialog, int id) {
Wink Saville0183fb52014-11-22 10:11:39 -08001332 mSubscriptionManager.setDefaultDataSubId(currentSir.getSubscriptionId());
Sanket Padawe24f834d2015-01-08 11:23:11 -08001333 setMobileDataEnabled(currentSir.getSubscriptionId(), true);
Sanket Padawed819270f2015-01-14 11:03:33 -08001334 disableDataForOtherSubscriptions(currentSir);
PauloftheWestde338442014-10-29 14:46:44 -07001335 updateBody();
1336 }
1337 });
1338 builder.setNegativeButton(R.string.cancel, null);
1339
1340 builder.create().show();
1341 }
1342
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001343 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001344 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001345 public void onClick(View v) {
1346 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001347 if (disableAtLimit) {
1348 // enabling limit; show confirmation dialog which eventually
1349 // calls setPolicyLimitBytes() once user confirms.
1350 ConfirmLimitFragment.show(DataUsageSummary.this);
1351 } else {
1352 setPolicyLimitBytes(LIMIT_DISABLED);
1353 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001354 }
1355 };
1356
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001357 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001358 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001359 public void onClick(View v) {
1360 final boolean restrictBackground = !mAppRestrict.isChecked();
1361
1362 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001363 // enabling restriction; show confirmation dialog which
1364 // eventually calls setRestrictBackground() once user
1365 // confirms.
1366 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001367 } else {
1368 setAppRestrictBackground(false);
1369 }
1370 }
1371 };
1372
Jeff Sharkey8a503642011-06-10 13:31:21 -07001373 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001374 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001375 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001376 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001377 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001378
1379 // TODO: sigh, remove this hack once we understand 6450986
1380 if (mUidDetailProvider == null || app == null) return;
1381
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001382 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001383 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001384 }
1385 };
1386
1387 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001388 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001389 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1390 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1391 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001392 // show cycle editor; will eventually call setPolicyCycleDay()
1393 // when user finishes editing.
1394 CycleEditorFragment.show(DataUsageSummary.this);
1395
1396 // reset spinner to something other than "change cycle..."
1397 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001398
1399 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001400 if (LOGD) {
1401 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1402 + cycle.end + "]");
1403 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001404
1405 // update chart to show selected cycle, and update detail data
1406 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001407 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001408
1409 updateDetailData();
1410 }
1411 }
1412
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001413 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001414 public void onNothingSelected(AdapterView<?> parent) {
1415 // ignored
1416 }
1417 };
1418
1419 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001420 * Update details based on {@link #mChart} inspection range depending on
1421 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1422 * of applications data usage, and when {@link #isAppDetailMode()} update
1423 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001424 */
1425 private void updateDetailData() {
1426 if (LOGD) Log.d(TAG, "updateDetailData()");
1427
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001428 final long start = mChart.getInspectStart();
1429 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001430 final long now = System.currentTimeMillis();
1431
1432 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001433
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001434 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001435 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001436 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001437 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001438 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001439 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001440 final long foregroundBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001441 final long totalBytes = defaultBytes + foregroundBytes;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001442
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001443 if (mAppTotal != null) {
1444 mAppTotal.setText(Formatter.formatFileSize(context, totalBytes));
1445 }
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001446 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1447 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1448
1449 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001450 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001451
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001452 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001453
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001454 mCycleSummary.setVisibility(View.GONE);
1455
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001456 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001457 if (mChartData != null) {
1458 entry = mChartData.network.getValues(start, end, now, null);
1459 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001460
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001461 mCycleSummary.setVisibility(View.VISIBLE);
1462
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001463 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001464 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001465 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001466 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001467
1468 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1469 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001470 mCycleSummary.setText(totalPhrase);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001471
PauloftheWest50e6eca2014-10-03 11:07:14 -07001472 if (isMobileTab(mCurrentTab) || TAB_3G.equals(mCurrentTab)
László Dávid0b8bf4e2012-10-24 23:31:47 +02001473 || TAB_4G.equals(mCurrentTab)) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001474 if (isAppDetailMode()) {
1475 mDisclaimer.setVisibility(View.GONE);
1476 } else {
1477 mDisclaimer.setVisibility(View.VISIBLE);
1478 }
Jeff Sharkeye557c332012-04-13 16:04:07 -07001479 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001480 mDisclaimer.setVisibility(View.GONE);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001481 }
1482
Jeff Sharkey92811822012-05-04 11:47:29 -07001483 // initial layout is finished above, ensure we have transitions
1484 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001485 }
1486
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001487 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1488 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001489 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001490 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001491 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001492 }
1493
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001494 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001495 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1496 mChartData = data;
1497 mChart.bindNetworkStats(mChartData.network);
1498 mChart.bindDetailNetworkStats(mChartData.detail);
1499
1500 // calcuate policy cycles based on available data
1501 updatePolicy(true);
1502 updateAppDetail();
1503
1504 // force scroll to top of body when showing detail
1505 if (mChartData.detail != null) {
1506 mListView.smoothScrollToPosition(0);
1507 }
1508 }
1509
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001510 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001511 public void onLoaderReset(Loader<ChartData> loader) {
1512 mChartData = null;
1513 mChart.bindNetworkStats(null);
1514 mChart.bindDetailNetworkStats(null);
1515 }
1516 };
1517
1518 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001519 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001520 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001521 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001522 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001523 }
1524
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001525 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001526 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001527 final int[] restrictedUids = mPolicyManager.getUidsWithPolicy(
Jeff Sharkeye557c332012-04-13 16:04:07 -07001528 POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001529 mAdapter.bindStats(data, restrictedUids);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001530 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001531 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001532
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001533 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001534 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001535 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001536 updateEmptyVisible();
1537 }
1538
1539 private void updateEmptyVisible() {
1540 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1541 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001542 mStupidPadding.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001543 }
1544 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001545
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001546 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001547 final TelephonyManager tele = TelephonyManager.from(context);
1548 final String actualSubscriberId = tele.getSubscriberId();
Wink Savillefcec91f2014-12-02 17:12:08 -08001549 String retVal = SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
1550 if (LOGD) Log.d(TAG, "getActiveSubscriberId=" + retVal + " actualSubscriberId=" + actualSubscriberId);
1551 return retVal;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001552 }
1553
Wink Savilleb003a852014-10-23 10:16:26 -07001554 private static String getActiveSubscriberId(Context context, int subId) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07001555 final TelephonyManager tele = TelephonyManager.from(context);
Wink Savillefcec91f2014-12-02 17:12:08 -08001556 String retVal = tele.getSubscriberId(subId);
1557 if (LOGD) Log.d(TAG, "getActiveSubscriberId=" + retVal + " subId=" + subId);
1558 return retVal;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001559 }
1560
Jeff Sharkey8a503642011-06-10 13:31:21 -07001561 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001562 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001563 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001564 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001565 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001566
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001567 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001568 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001569 setPolicyLimitBytes(mChart.getLimitBytes());
Sanket Padawe716b1f82015-06-24 14:26:21 -07001570 updateBody();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001571 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001572
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001573 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001574 public void requestWarningEdit() {
1575 WarningEditorFragment.show(DataUsageSummary.this);
1576 }
1577
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001578 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001579 public void requestLimitEdit() {
1580 LimitEditorFragment.show(DataUsageSummary.this);
1581 }
1582 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001583
1584 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001585 * List item that reflects a specific data usage cycle.
1586 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001587 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001588 public CharSequence label;
1589 public long start;
1590 public long end;
1591
Jeff Sharkey8a503642011-06-10 13:31:21 -07001592 CycleItem(CharSequence label) {
1593 this.label = label;
1594 }
1595
1596 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001597 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001598 this.start = start;
1599 this.end = end;
1600 }
1601
Jeff Sharkey8a503642011-06-10 13:31:21 -07001602 @Override
1603 public String toString() {
1604 return label.toString();
1605 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001606
1607 @Override
1608 public boolean equals(Object o) {
1609 if (o instanceof CycleItem) {
1610 final CycleItem another = (CycleItem) o;
1611 return start == another.start && end == another.end;
1612 }
1613 return false;
1614 }
1615
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001616 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001617 public int compareTo(CycleItem another) {
1618 return Long.compare(start, another.start);
1619 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001620 }
1621
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001622 private static final StringBuilder sBuilder = new StringBuilder(50);
1623 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1624 sBuilder, Locale.getDefault());
1625
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001626 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001627 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001628
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001629 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001630 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001631 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1632 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001633 }
1634 }
1635
Jeff Sharkey8a503642011-06-10 13:31:21 -07001636 /**
1637 * Special-case data usage cycle that triggers dialog to change
1638 * {@link NetworkPolicy#cycleDay}.
1639 */
1640 public static class CycleChangeItem extends CycleItem {
1641 public CycleChangeItem(Context context) {
1642 super(context.getString(R.string.data_usage_change_cycle));
1643 }
1644 }
1645
1646 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001647 private boolean mChangePossible = false;
1648 private boolean mChangeVisible = false;
1649
1650 private final CycleChangeItem mChangeItem;
1651
Jeff Sharkey8a503642011-06-10 13:31:21 -07001652 public CycleAdapter(Context context) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001653 super(context, R.layout.data_usage_cycle_item);
1654 setDropDownViewResource(R.layout.data_usage_cycle_item_dropdown);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001655 mChangeItem = new CycleChangeItem(context);
1656 }
1657
1658 public void setChangePossible(boolean possible) {
1659 mChangePossible = possible;
1660 updateChange();
1661 }
1662
1663 public void setChangeVisible(boolean visible) {
1664 mChangeVisible = visible;
1665 updateChange();
1666 }
1667
1668 private void updateChange() {
1669 remove(mChangeItem);
1670 if (mChangePossible && mChangeVisible) {
1671 add(mChangeItem);
1672 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001673 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001674
1675 /**
1676 * Find position of {@link CycleItem} in this adapter which is nearest
1677 * the given {@link CycleItem}.
1678 */
1679 public int findNearestPosition(CycleItem target) {
1680 if (target != null) {
1681 final int count = getCount();
1682 for (int i = count - 1; i >= 0; i--) {
1683 final CycleItem item = getItem(i);
1684 if (item instanceof CycleChangeItem) {
1685 continue;
1686 } else if (item.compareTo(target) >= 0) {
1687 return i;
1688 }
1689 }
1690 }
1691 return 0;
1692 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001693 }
1694
1695 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001696 * Adapter of applications, sorted by total usage descending.
1697 */
1698 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001699 private final UidDetailProvider mProvider;
1700 private final int mInsetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001701 private final UserManager mUm;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001702
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001703 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001704 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001705
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001706 public DataUsageAdapter(final UserManager userManager, UidDetailProvider provider, int insetSide) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001707 mProvider = checkNotNull(provider);
1708 mInsetSide = insetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001709 mUm = userManager;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001710 }
1711
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001712 /**
1713 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1714 */
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001715 public void bindStats(NetworkStats stats, int[] restrictedUids) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001716 mItems.clear();
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001717 mLargest = 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001718
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001719 final int currentUserId = ActivityManager.getCurrentUser();
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001720 final List<UserHandle> profiles = mUm.getUserProfiles();
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001721 final SparseArray<AppItem> knownItems = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001722
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001723 NetworkStats.Entry entry = null;
1724 final int size = stats != null ? stats.size() : 0;
1725 for (int i = 0; i < size; i++) {
1726 entry = stats.getValues(i, entry);
1727
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001728 // Decide how to collapse items together
1729 final int uid = entry.uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001730
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001731 final int collapseKey;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001732 final int category;
1733 final int userId = UserHandle.getUserId(uid);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001734 if (UserHandle.isApp(uid)) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001735 if (profiles.contains(new UserHandle(userId))) {
1736 if (userId != currentUserId) {
1737 // Add to a managed user item.
1738 final int managedKey = UidDetailProvider.buildKeyForUser(userId);
1739 accumulate(managedKey, knownItems, entry,
1740 AppItem.CATEGORY_USER);
1741 }
1742 // Add to app item.
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001743 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001744 category = AppItem.CATEGORY_APP;
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001745 } else {
Zoltan Szatmary-Ban77c1d362014-12-12 18:48:03 +00001746 // If it is a removed user add it to the removed users' key
1747 final UserInfo info = mUm.getUserInfo(userId);
1748 if (info == null) {
1749 collapseKey = UID_REMOVED;
1750 category = AppItem.CATEGORY_APP;
1751 } else {
1752 // Add to other user item.
1753 collapseKey = UidDetailProvider.buildKeyForUser(userId);
1754 category = AppItem.CATEGORY_USER;
1755 }
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001756 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001757 } else if (uid == UID_REMOVED || uid == UID_TETHERING) {
1758 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001759 category = AppItem.CATEGORY_APP;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001760 } else {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001761 collapseKey = android.os.Process.SYSTEM_UID;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001762 category = AppItem.CATEGORY_APP;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001763 }
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001764 accumulate(collapseKey, knownItems, entry, category);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001765 }
1766
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001767 final int restrictedUidsMax = restrictedUids.length;
1768 for (int i = 0; i < restrictedUidsMax; ++i) {
1769 final int uid = restrictedUids[i];
1770 // Only splice in restricted state for current user or managed users
1771 if (!profiles.contains(new UserHandle(UserHandle.getUserId(uid)))) {
1772 continue;
1773 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001774
1775 AppItem item = knownItems.get(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001776 if (item == null) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001777 item = new AppItem(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001778 item.total = -1;
1779 mItems.add(item);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001780 knownItems.put(item.key, item);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001781 }
1782 item.restricted = true;
1783 }
1784
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001785 if (!mItems.isEmpty()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001786 final AppItem title = new AppItem();
1787 title.category = AppItem.CATEGORY_APP_TITLE;
1788 mItems.add(title);
1789 }
1790
Jeff Sharkey8a503642011-06-10 13:31:21 -07001791 Collections.sort(mItems);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001792 notifyDataSetChanged();
1793 }
1794
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001795 /**
1796 * Accumulate data usage of a network stats entry for the item mapped by the collapse key.
1797 * Creates the item if needed.
1798 *
1799 * @param collapseKey the collapse key used to map the item.
1800 * @param knownItems collection of known (already existing) items.
1801 * @param entry the network stats entry to extract data usage from.
1802 * @param itemCategory the item is categorized on the list view by this category. Must be
1803 * either AppItem.APP_ITEM_CATEGORY or AppItem.MANAGED_USER_ITEM_CATEGORY
1804 */
1805 private void accumulate(int collapseKey, final SparseArray<AppItem> knownItems,
1806 NetworkStats.Entry entry, int itemCategory) {
1807 final int uid = entry.uid;
1808 AppItem item = knownItems.get(collapseKey);
1809 if (item == null) {
1810 item = new AppItem(collapseKey);
1811 item.category = itemCategory;
1812 mItems.add(item);
1813 knownItems.put(item.key, item);
1814 }
1815 item.addUid(uid);
1816 item.total += entry.rxBytes + entry.txBytes;
1817 if (mLargest < item.total) {
1818 mLargest = item.total;
1819 }
1820 }
1821
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001822 @Override
1823 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001824 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001825 }
1826
1827 @Override
1828 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001829 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001830 }
1831
1832 @Override
1833 public long getItemId(int position) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001834 return mItems.get(position).key;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001835 }
1836
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001837 /**
1838 * See {@link #getItemViewType} for the view types.
1839 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001840 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001841 public int getViewTypeCount() {
1842 return 2;
1843 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001844
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001845 /**
1846 * Returns 1 for separator items and 0 for anything else.
1847 */
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001848 @Override
1849 public int getItemViewType(int position) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001850 final AppItem item = mItems.get(position);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001851 if (item.category == AppItem.CATEGORY_APP_TITLE) {
1852 return 1;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001853 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001854 return 0;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001855 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001856 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001857
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001858 @Override
1859 public boolean areAllItemsEnabled() {
1860 return false;
1861 }
1862
1863 @Override
1864 public boolean isEnabled(int position) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001865 if (position > mItems.size()) {
1866 throw new ArrayIndexOutOfBoundsException();
1867 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001868 return getItemViewType(position) == 0;
1869 }
1870
1871 @Override
1872 public View getView(int position, View convertView, ViewGroup parent) {
1873 final AppItem item = mItems.get(position);
Jason Monk2cdafc62015-06-12 12:32:50 -04001874 LayoutInflater inflater = LayoutInflater.from(parent.getContext());
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001875 if (getItemViewType(position) == 1) {
1876 if (convertView == null) {
Jason Monk2cdafc62015-06-12 12:32:50 -04001877 convertView = Utils.inflateCategoryHeader(inflater, parent);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001878 }
1879
1880 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
1881 title.setText(R.string.data_usage_app);
1882
1883 } else {
1884 if (convertView == null) {
Jason Monk2cdafc62015-06-12 12:32:50 -04001885 convertView = inflater.inflate(R.layout.data_usage_item, parent, false);
1886 inflater.inflate(R.layout.widget_progress_bar,
1887 (ViewGroup) convertView.findViewById(android.R.id.widget_frame));
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001888
1889 if (mInsetSide > 0) {
1890 convertView.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
1891 }
1892 }
1893
1894 final Context context = parent.getContext();
1895
Jason Monk2cdafc62015-06-12 12:32:50 -04001896 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001897 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1898 android.R.id.progress);
1899
1900 // kick off async load of app details
1901 UidDetailTask.bindView(mProvider, item, convertView);
1902
1903 if (item.restricted && item.total <= 0) {
Jason Monk2cdafc62015-06-12 12:32:50 -04001904 summary.setText(R.string.data_usage_app_restricted);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001905 progress.setVisibility(View.GONE);
1906 } else {
Jason Monk2cdafc62015-06-12 12:32:50 -04001907 summary.setText(Formatter.formatFileSize(context, item.total));
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001908 progress.setVisibility(View.VISIBLE);
1909 }
1910
1911 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1912 progress.setProgress(percentTotal);
1913 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001914
1915 return convertView;
1916 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001917 }
1918
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001919 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001920 * Empty {@link Fragment} that controls display of UID details in
1921 * {@link DataUsageSummary}.
1922 */
1923 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001924 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001925
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001926 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jason Monk4bb075b2015-03-17 10:06:58 -04001927 show(parent, app, label, true);
1928 }
1929
1930 public static void show(DataUsageSummary parent, AppItem app, CharSequence label,
1931 boolean addToBack) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001932 if (!parent.isAdded()) return;
1933
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001934 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001935 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001936
1937 final AppDetailsFragment fragment = new AppDetailsFragment();
1938 fragment.setArguments(args);
1939 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001940 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1941 ft.add(fragment, TAG_APP_DETAILS);
Jason Monk4bb075b2015-03-17 10:06:58 -04001942 if (addToBack) {
1943 ft.addToBackStack(TAG_APP_DETAILS);
1944 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001945 ft.setBreadCrumbTitle(
1946 parent.getResources().getString(R.string.data_usage_app_summary_title));
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001947 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001948 }
1949
1950 @Override
1951 public void onStart() {
1952 super.onStart();
1953 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001954 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001955 target.updateBody();
1956 }
1957
1958 @Override
1959 public void onStop() {
1960 super.onStop();
1961 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001962 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001963 target.updateBody();
1964 }
Julia Reynolds1740ce42015-07-27 16:00:06 -04001965
1966 @Override
1967 public boolean onOptionsItemSelected(MenuItem item) {
1968 switch (item.getItemId()) {
1969 case android.R.id.home:
1970 getFragmentManager().popBackStack();
1971 return true;
1972 }
1973 return super.onOptionsItemSelected(item);
1974 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001975 }
1976
1977 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001978 * Dialog to request user confirmation before setting
1979 * {@link NetworkPolicy#limitBytes}.
1980 */
1981 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001982 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001983 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001984
1985 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001986 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001987
Jeff Sharkeyd0a55312014-08-08 10:25:24 -07001988 final NetworkPolicy policy = parent.mPolicyEditor.getPolicy(parent.mTemplate);
1989 if (policy == null) return;
1990
Jeff Sharkey461842a2011-09-25 18:22:48 -07001991 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001992 final CharSequence message;
Jeff Sharkeyd0a55312014-08-08 10:25:24 -07001993 final long minLimitBytes = (long) (policy.warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001994 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001995
1996 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001997 final String currentTab = parent.mCurrentTab;
1998 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001999 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07002000 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002001 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07002002 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07002003 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002004 } else if (isMobileTab(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07002005 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07002006 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07002007 } else {
2008 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002009 }
2010
Jeff Sharkey131f9d62011-08-17 17:08:19 -07002011 final Bundle args = new Bundle();
2012 args.putCharSequence(EXTRA_MESSAGE, message);
2013 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
2014
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002015 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
2016 dialog.setArguments(args);
2017 dialog.setTargetFragment(parent, 0);
2018 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
2019 }
2020
2021 @Override
2022 public Dialog onCreateDialog(Bundle savedInstanceState) {
2023 final Context context = getActivity();
2024
Jeff Sharkey131f9d62011-08-17 17:08:19 -07002025 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002026 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
2027
2028 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2029 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07002030 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002031
2032 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002033 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002034 public void onClick(DialogInterface dialog, int which) {
2035 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2036 if (target != null) {
2037 target.setPolicyLimitBytes(limitBytes);
2038 }
2039 }
2040 });
2041
2042 return builder.create();
2043 }
2044 }
2045
2046 /**
2047 * Dialog to edit {@link NetworkPolicy#cycleDay}.
2048 */
2049 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002050 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002051
2052 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002053 if (!parent.isAdded()) return;
2054
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002055 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002056 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002057
2058 final CycleEditorFragment dialog = new CycleEditorFragment();
2059 dialog.setArguments(args);
2060 dialog.setTargetFragment(parent, 0);
2061 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
2062 }
2063
2064 @Override
2065 public Dialog onCreateDialog(Bundle savedInstanceState) {
2066 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002067 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2068 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002069
2070 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2071 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2072
2073 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
2074 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
2075
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002076 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2077 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002078
2079 cycleDayPicker.setMinValue(1);
2080 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002081 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002082 cycleDayPicker.setWrapSelectorWheel(true);
2083
2084 builder.setTitle(R.string.data_usage_cycle_editor_title);
2085 builder.setView(view);
2086
2087 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2088 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002089 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002090 public void onClick(DialogInterface dialog, int which) {
Jeff Sharkeyd277de92013-03-25 15:11:25 -07002091 // clear focus to finish pending text edits
2092 cycleDayPicker.clearFocus();
2093
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002094 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08002095 final String cycleTimezone = new Time().timezone;
2096 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002097 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002098 }
2099 });
2100
2101 return builder.create();
2102 }
2103 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002104
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002105 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002106 * Dialog to edit {@link NetworkPolicy#warningBytes}.
2107 */
2108 public static class WarningEditorFragment extends DialogFragment {
2109 private static final String EXTRA_TEMPLATE = "template";
2110
2111 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002112 if (!parent.isAdded()) return;
2113
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002114 final Bundle args = new Bundle();
2115 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
2116
2117 final WarningEditorFragment dialog = new WarningEditorFragment();
2118 dialog.setArguments(args);
2119 dialog.setTargetFragment(parent, 0);
2120 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
2121 }
2122
2123 @Override
2124 public Dialog onCreateDialog(Bundle savedInstanceState) {
2125 final Context context = getActivity();
2126 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2127 final NetworkPolicyEditor editor = target.mPolicyEditor;
2128
2129 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2130 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2131
2132 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
2133 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
2134
2135 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2136 final long warningBytes = editor.getPolicyWarningBytes(template);
2137 final long limitBytes = editor.getPolicyLimitBytes(template);
2138
2139 bytesPicker.setMinValue(0);
2140 if (limitBytes != LIMIT_DISABLED) {
2141 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
2142 } else {
2143 bytesPicker.setMaxValue(Integer.MAX_VALUE);
2144 }
2145 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
2146 bytesPicker.setWrapSelectorWheel(false);
2147
2148 builder.setTitle(R.string.data_usage_warning_editor_title);
2149 builder.setView(view);
2150
2151 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2152 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002153 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002154 public void onClick(DialogInterface dialog, int which) {
2155 // clear focus to finish pending text edits
2156 bytesPicker.clearFocus();
2157
2158 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
2159 editor.setPolicyWarningBytes(template, bytes);
2160 target.updatePolicy(false);
2161 }
2162 });
2163
2164 return builder.create();
2165 }
2166 }
2167
2168 /**
2169 * Dialog to edit {@link NetworkPolicy#limitBytes}.
2170 */
2171 public static class LimitEditorFragment extends DialogFragment {
2172 private static final String EXTRA_TEMPLATE = "template";
2173
2174 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002175 if (!parent.isAdded()) return;
2176
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002177 final Bundle args = new Bundle();
2178 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
2179
2180 final LimitEditorFragment dialog = new LimitEditorFragment();
2181 dialog.setArguments(args);
2182 dialog.setTargetFragment(parent, 0);
2183 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
2184 }
2185
2186 @Override
2187 public Dialog onCreateDialog(Bundle savedInstanceState) {
2188 final Context context = getActivity();
2189 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2190 final NetworkPolicyEditor editor = target.mPolicyEditor;
2191
2192 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2193 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2194
2195 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
2196 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
2197
2198 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2199 final long warningBytes = editor.getPolicyWarningBytes(template);
2200 final long limitBytes = editor.getPolicyLimitBytes(template);
2201
2202 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09002203 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002204 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
2205 } else {
2206 bytesPicker.setMinValue(0);
2207 }
2208 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
2209 bytesPicker.setWrapSelectorWheel(false);
2210
2211 builder.setTitle(R.string.data_usage_limit_editor_title);
2212 builder.setView(view);
2213
2214 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2215 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002216 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002217 public void onClick(DialogInterface dialog, int which) {
2218 // clear focus to finish pending text edits
2219 bytesPicker.clearFocus();
2220
2221 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
2222 editor.setPolicyLimitBytes(template, bytes);
2223 target.updatePolicy(false);
2224 }
2225 });
2226
2227 return builder.create();
2228 }
2229 }
2230 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07002231 * Dialog to request user confirmation before disabling data.
2232 */
2233 public static class ConfirmDataDisableFragment extends DialogFragment {
Sanket Padawe24f834d2015-01-08 11:23:11 -08002234 static int mSubId;
2235 public static void show(DataUsageSummary parent, int subId) {
2236 mSubId = subId;
Jeff Sharkey461842a2011-09-25 18:22:48 -07002237 if (!parent.isAdded()) return;
2238
Jeff Sharkey28130d92011-09-02 16:10:24 -07002239 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
2240 dialog.setTargetFragment(parent, 0);
2241 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
2242 }
2243
2244 @Override
2245 public Dialog onCreateDialog(Bundle savedInstanceState) {
2246 final Context context = getActivity();
2247
2248 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2249 builder.setMessage(R.string.data_usage_disable_mobile);
2250
2251 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002252 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07002253 public void onClick(DialogInterface dialog, int which) {
2254 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2255 if (target != null) {
2256 // TODO: extend to modify policy enabled flag.
Sanket Padawe24f834d2015-01-08 11:23:11 -08002257 target.setMobileDataEnabled(mSubId, false);
Jeff Sharkey28130d92011-09-02 16:10:24 -07002258 }
2259 }
2260 });
2261 builder.setNegativeButton(android.R.string.cancel, null);
2262
2263 return builder.create();
2264 }
2265 }
2266
2267 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002268 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07002269 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002270 */
2271 public static class ConfirmRestrictFragment extends DialogFragment {
2272 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002273 if (!parent.isAdded()) return;
2274
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002275 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
2276 dialog.setTargetFragment(parent, 0);
2277 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
2278 }
2279
2280 @Override
2281 public Dialog onCreateDialog(Bundle savedInstanceState) {
2282 final Context context = getActivity();
2283
2284 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002285 builder.setTitle(R.string.data_usage_restrict_background_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07002286 if (Utils.hasMultipleUsers(context)) {
2287 builder.setMessage(R.string.data_usage_restrict_background_multiuser);
2288 } else {
2289 builder.setMessage(R.string.data_usage_restrict_background);
2290 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002291
2292 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002293 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002294 public void onClick(DialogInterface dialog, int which) {
2295 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2296 if (target != null) {
2297 target.setRestrictBackground(true);
2298 }
2299 }
2300 });
2301 builder.setNegativeButton(android.R.string.cancel, null);
2302
2303 return builder.create();
2304 }
2305 }
2306
2307 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002308 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
2309 * change has been denied, usually based on
2310 * {@link DataUsageSummary#hasLimitedNetworks()}.
2311 */
2312 public static class DeniedRestrictFragment extends DialogFragment {
2313 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002314 if (!parent.isAdded()) return;
2315
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002316 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
2317 dialog.setTargetFragment(parent, 0);
2318 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
2319 }
2320
2321 @Override
2322 public Dialog onCreateDialog(Bundle savedInstanceState) {
2323 final Context context = getActivity();
2324
2325 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2326 builder.setTitle(R.string.data_usage_app_restrict_background);
2327 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
2328 builder.setPositiveButton(android.R.string.ok, null);
2329
2330 return builder.create();
2331 }
2332 }
2333
2334 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002335 * Dialog to request user confirmation before setting
2336 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
2337 */
2338 public static class ConfirmAppRestrictFragment extends DialogFragment {
2339 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002340 if (!parent.isAdded()) return;
2341
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002342 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2343 dialog.setTargetFragment(parent, 0);
2344 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2345 }
2346
2347 @Override
2348 public Dialog onCreateDialog(Bundle savedInstanceState) {
2349 final Context context = getActivity();
2350
2351 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002352 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2353 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2354
2355 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002356 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002357 public void onClick(DialogInterface dialog, int which) {
2358 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2359 if (target != null) {
2360 target.setAppRestrictBackground(true);
2361 }
2362 }
2363 });
2364 builder.setNegativeButton(android.R.string.cancel, null);
2365
2366 return builder.create();
2367 }
2368 }
2369
2370 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002371 * Compute default tab that should be selected, based on
2372 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2373 */
2374 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002375 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002376 if (template == null) {
Wink Savilleb003a852014-10-23 10:16:26 -07002377 final int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
Wink Saville0183fb52014-11-22 10:11:39 -08002378 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Savilled72c0622014-12-11 10:10:05 -08002379 if (SubscriptionManager.isValidSubscriptionId(subId)) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07002380 return TAB_MOBILE + String.valueOf(subId);
2381 }
2382 return null;
2383 }
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002384
2385 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002386 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002387 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002388 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002389 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002390 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002391 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002392 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002393 return TAB_WIFI;
2394 default:
2395 return null;
2396 }
2397 }
2398
2399 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002400 * Background task that loads {@link UidDetail}, binding to
2401 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002402 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002403 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2404 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002405 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002406 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002407
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002408 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002409 mProvider = checkNotNull(provider);
2410 mItem = checkNotNull(item);
2411 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002412 }
2413
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002414 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002415 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002416 final UidDetailTask existing = (UidDetailTask) target.getTag();
2417 if (existing != null) {
2418 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002419 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002420
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002421 final UidDetail cachedDetail = provider.getUidDetail(item.key, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002422 if (cachedDetail != null) {
2423 bindView(cachedDetail, target);
2424 } else {
2425 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2426 AsyncTask.THREAD_POOL_EXECUTOR));
2427 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002428 }
2429
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002430 private static void bindView(UidDetail detail, View target) {
2431 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2432 final TextView title = (TextView) target.findViewById(android.R.id.title);
2433
2434 if (detail != null) {
2435 icon.setImageDrawable(detail.icon);
2436 title.setText(detail.label);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +01002437 title.setContentDescription(detail.contentDescription);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002438 } else {
2439 icon.setImageDrawable(null);
2440 title.setText(null);
2441 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002442 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002443
2444 @Override
2445 protected void onPreExecute() {
2446 bindView(null, mTarget);
2447 }
2448
2449 @Override
2450 protected UidDetail doInBackground(Void... params) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002451 return mProvider.getUidDetail(mItem.key, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002452 }
2453
2454 @Override
2455 protected void onPostExecute(UidDetail result) {
2456 bindView(result, mTarget);
2457 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002458 }
2459
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002460 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002461 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002462 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002463 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002464 if (TEST_RADIOS) {
2465 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2466 }
2467
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002468 final ConnectivityManager conn = ConnectivityManager.from(context);
2469 final TelephonyManager tele = TelephonyManager.from(context);
2470
Wink Saville0183fb52014-11-22 10:11:39 -08002471 final List<SubscriptionInfo> subInfoList =
2472 SubscriptionManager.from(context).getActiveSubscriptionInfoList();
2473 // No activated Subscriptions
PauloftheWest50e6eca2014-10-03 11:07:14 -07002474 if (subInfoList == null) {
Wink Savillefcec91f2014-12-02 17:12:08 -08002475 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subInfoList=null");
PauloftheWest50e6eca2014-10-03 11:07:14 -07002476 return false;
2477 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002478 // require both supported network and ready SIM
PauloftheWest50e6eca2014-10-03 11:07:14 -07002479 boolean isReady = true;
Wink Savilleca756612014-11-08 10:47:12 -08002480 for (SubscriptionInfo subInfo : subInfoList) {
Stuart Scott3ada2ec2014-10-31 14:11:24 -07002481 isReady = isReady & tele.getSimState(subInfo.getSimSlotIndex()) == SIM_STATE_READY;
Wink Savillefcec91f2014-12-02 17:12:08 -08002482 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subInfo=" + subInfo);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002483 }
Wink Savillefcec91f2014-12-02 17:12:08 -08002484 boolean retVal = conn.isNetworkSupported(TYPE_MOBILE) && isReady;
2485 if (LOGD) {
2486 Log.d(TAG, "hasReadyMobileRadio:"
2487 + " conn.isNetworkSupported(TYPE_MOBILE)="
2488 + conn.isNetworkSupported(TYPE_MOBILE)
2489 + " isReady=" + isReady);
2490 }
2491 return retVal;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002492 }
2493
2494 /*
2495 * TODO: consider adding to TelephonyManager or SubscritpionManager.
2496 */
Wink Savilleb003a852014-10-23 10:16:26 -07002497 public static boolean hasReadyMobileRadio(Context context, int subId) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07002498 if (TEST_RADIOS) {
2499 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2500 }
2501
2502 final ConnectivityManager conn = ConnectivityManager.from(context);
2503 final TelephonyManager tele = TelephonyManager.from(context);
2504 final int slotId = SubscriptionManager.getSlotId(subId);
2505 final boolean isReady = tele.getSimState(slotId) == SIM_STATE_READY;
2506
Wink Savillefcec91f2014-12-02 17:12:08 -08002507 boolean retVal = conn.isNetworkSupported(TYPE_MOBILE) && isReady;
2508 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subId=" + subId
2509 + " conn.isNetworkSupported(TYPE_MOBILE)=" + conn.isNetworkSupported(TYPE_MOBILE)
2510 + " isReady=" + isReady);
2511 return retVal;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002512 }
2513
2514 /**
2515 * Test if device has a mobile 4G data radio.
2516 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002517 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002518 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2519 return false;
2520 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002521 if (TEST_RADIOS) {
2522 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2523 }
2524
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002525 final ConnectivityManager conn = ConnectivityManager.from(context);
2526 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002527
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002528 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Wink Saville55434042012-06-14 12:33:43 -07002529 final boolean hasLte = (tele.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE)
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002530 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002531 return hasWimax || hasLte;
2532 }
2533
2534 /**
2535 * Test if device has a Wi-Fi data radio.
2536 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002537 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002538 if (TEST_RADIOS) {
2539 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2540 }
2541
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002542 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002543 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002544 }
2545
2546 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002547 * Test if device has an ethernet network connection.
2548 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002549 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002550 if (TEST_RADIOS) {
2551 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2552 }
2553
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002554 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002555 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2556
2557 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002558 if (mStatsSession != null) {
2559 try {
2560 ethernetBytes = mStatsSession.getSummaryForNetwork(
2561 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2562 .getTotalBytes();
2563 } catch (RemoteException e) {
2564 throw new RuntimeException(e);
2565 }
2566 } else {
2567 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002568 }
2569
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002570 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002571 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002572 }
2573
2574 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002575 * Inflate a {@link Preference} style layout, adding the given {@link View}
2576 * widget into {@link android.R.id#widget_frame}.
2577 */
2578 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2579 final View view = inflater.inflate(R.layout.preference, root, false);
2580 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2581 android.R.id.widget_frame);
2582 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2583 return view;
2584 }
2585
2586 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002587 * Test if any networks are currently limited.
2588 */
2589 private boolean hasLimitedNetworks() {
2590 return !buildLimitedNetworksList().isEmpty();
2591 }
2592
2593 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002594 * Build string describing currently limited networks, which defines when
2595 * background data is restricted.
2596 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002597 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002598 private CharSequence buildLimitedNetworksString() {
2599 final List<CharSequence> limited = buildLimitedNetworksList();
2600
2601 // handle case where no networks limited
2602 if (limited.isEmpty()) {
2603 limited.add(getText(R.string.data_usage_list_none));
2604 }
2605
Roozbeh Pournader4bbf75d2015-08-10 11:32:19 -07002606 final ICUResourceBundle icuBundle = (ICUResourceBundle) UResourceBundle.
2607 getBundleInstance(ICUResourceBundle.ICU_BASE_NAME);
2608 final String listMiddlePattern =
2609 icuBundle.getStringWithFallback("listPattern/standard/middle");
2610 // The returned pattern is something like "{0}, {1}", from which we want
2611 // to extract the ", " part.
2612 final int firstClosingBrace = listMiddlePattern.indexOf('}');
2613 final int lastOpeningBrace = listMiddlePattern.lastIndexOf('{');
2614 final CharSequence delimiter = listMiddlePattern.substring(
2615 firstClosingBrace+1, lastOpeningBrace);
2616
2617 return TextUtils.join(delimiter, limited);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002618 }
2619
2620 /**
2621 * Build list of currently limited networks, which defines when background
2622 * data is restricted.
2623 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002624 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002625 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002626 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002627
2628 // build combined list of all limited networks
2629 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002630
2631 final TelephonyManager tele = TelephonyManager.from(context);
2632 if (tele.getSimState() == SIM_STATE_READY) {
2633 final String subscriberId = getActiveSubscriberId(context);
2634 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2635 limited.add(getText(R.string.data_usage_list_mobile));
2636 }
2637 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2638 limited.add(getText(R.string.data_usage_tab_3g));
2639 }
2640 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2641 limited.add(getText(R.string.data_usage_tab_4g));
2642 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002643 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002644
2645 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002646 limited.add(getText(R.string.data_usage_tab_wifi));
2647 }
2648 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2649 limited.add(getText(R.string.data_usage_tab_ethernet));
2650 }
2651
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002652 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002653 }
2654
2655 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002656 * Inset both selector and divider {@link Drawable} on the given
2657 * {@link ListView} by the requested dimensions.
2658 */
2659 private static void insetListViewDrawables(ListView view, int insetSide) {
2660 final Drawable selector = view.getSelector();
2661 final Drawable divider = view.getDivider();
2662
2663 // fully unregister these drawables so callbacks can be maintained after
2664 // wrapping below.
2665 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2666 view.setSelector(stub);
2667 view.setDivider(stub);
2668
2669 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2670 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2671 }
2672
2673 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002674 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002675 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002676 */
2677 private static void setPreferenceTitle(View parent, int resId) {
2678 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2679 title.setText(resId);
2680 }
2681
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002682 /**
2683 * Set {@link android.R.id#summary} for a preference view inflated with
2684 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2685 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002686 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002687 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2688 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002689 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002690 }
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002691
2692 /**
2693 * For search
2694 */
2695 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -07002696 new BaseSearchIndexProvider() {
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002697 @Override
2698 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
2699 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
2700
2701 final Resources res = context.getResources();
2702
2703 // Add fragment title
2704 SearchIndexableRaw data = new SearchIndexableRaw(context);
2705 data.title = res.getString(R.string.data_usage_summary_title);
2706 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2707 result.add(data);
2708
2709 // Mobile data
2710 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002711 data.key = DATA_USAGE_ENABLE_MOBILE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002712 data.title = res.getString(R.string.data_usage_enable_mobile);
2713 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2714 result.add(data);
2715
2716 // Set mobile data limit
2717 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002718 data.key = DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002719 data.title = res.getString(R.string.data_usage_disable_mobile_limit);
2720 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2721 result.add(data);
2722
Fabrice Di Megliof2a52262014-04-17 17:20:27 -07002723 // Data usage cycle
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002724 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002725 data.key = DATA_USAGE_CYCLE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002726 data.title = res.getString(R.string.data_usage_cycle);
2727 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2728 result.add(data);
2729
2730 return result;
2731 }
2732 };
2733
Sanket Padawe7e21fa52015-02-12 12:39:34 -08002734 private void addMobileTab(Context context, SubscriptionInfo subInfo, boolean isMultiSim) {
PauloftheWestc80b7612014-11-25 04:40:45 -08002735 if (subInfo != null && mMobileTagMap != null) {
2736 if (hasReadyMobileRadio(context, subInfo.getSubscriptionId())) {
Sanket Padawe7e21fa52015-02-12 12:39:34 -08002737 if (isMultiSim) {
2738 mTabHost.addTab(buildTabSpec(mMobileTagMap.get(subInfo.getSubscriptionId()),
2739 subInfo.getDisplayName()));
2740 } else {
2741 mTabHost.addTab(buildTabSpec(mMobileTagMap.get(subInfo.getSubscriptionId()),
2742 R.string.data_usage_tab_mobile));
2743 }
PauloftheWest50e6eca2014-10-03 11:07:14 -07002744 }
Wink Savillefcec91f2014-12-02 17:12:08 -08002745 } else {
2746 if (LOGD) Log.d(TAG, "addMobileTab: subInfoList is null");
PauloftheWest50e6eca2014-10-03 11:07:14 -07002747 }
2748 }
2749
Wink Savilleca756612014-11-08 10:47:12 -08002750 private SubscriptionInfo getCurrentTabSubInfo(Context context) {
PauloftheWestde338442014-10-29 14:46:44 -07002751 if (mSubInfoList != null && mTabHost != null) {
2752 final int currentTagIndex = mTabHost.getCurrentTab();
2753 int i = 0;
Wink Savilleca756612014-11-08 10:47:12 -08002754 for (SubscriptionInfo subInfo : mSubInfoList) {
Stuart Scott3ada2ec2014-10-31 14:11:24 -07002755 if (hasReadyMobileRadio(context, subInfo.getSubscriptionId())) {
PauloftheWestde338442014-10-29 14:46:44 -07002756 if (i++ == currentTagIndex) {
2757 return subInfo;
2758 }
2759 }
2760 }
2761 }
2762 return null;
2763 }
2764
PauloftheWest50e6eca2014-10-03 11:07:14 -07002765 /**
2766 * Init a map with subId key and mobile tag name
2767 * @param subInfoList The subscription Info List
2768 * @return The map or null if no activated subscription
2769 */
Wink Savilleca756612014-11-08 10:47:12 -08002770 private Map<Integer, String> initMobileTabTag(List<SubscriptionInfo> subInfoList) {
Wink Savilleb003a852014-10-23 10:16:26 -07002771 Map<Integer, String> map = null;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002772 if (subInfoList != null) {
2773 String mobileTag;
Wink Savilleb003a852014-10-23 10:16:26 -07002774 map = new HashMap<Integer, String>();
Wink Savillefcec91f2014-12-02 17:12:08 -08002775 for (SubscriptionInfo subInfo : subInfoList) {
2776 mobileTag = TAB_MOBILE + String.valueOf(subInfo.getSubscriptionId());
2777 map.put(subInfo.getSubscriptionId(), mobileTag);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002778 }
2779 }
2780 return map;
2781 }
2782
2783 private static boolean isMobileTab(String currentTab) {
2784 return currentTab != null ? currentTab.contains(TAB_MOBILE) : false;
2785 }
2786
Wink Savilleb003a852014-10-23 10:16:26 -07002787 private int getSubId(String currentTab) {
Wink Saville0183fb52014-11-22 10:11:39 -08002788 if (mMobileTagMap != null) {
2789 Set<Integer> set = mMobileTagMap.keySet();
2790 for (Integer subId : set) {
2791 if (mMobileTagMap.get(subId).equals(currentTab)) {
2792 return subId;
2793 }
PauloftheWest50e6eca2014-10-03 11:07:14 -07002794 }
2795 }
2796 Log.e(TAG, "currentTab = " + currentTab + " non mobile tab called this function");
2797 return -1;
2798 }
2799
Stuart Scottf51d0062015-04-08 09:40:48 -07002800 private boolean isMobileDataAvailable(int subId) {
2801 return mSubscriptionManager.getActiveSubscriptionInfo(subId) != null;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002802 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002803}