blob: b3f79730e8dcf8c7460af2b7883dbf6af1520556 [file] [log] [blame]
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070019import static android.net.ConnectivityManager.TYPE_ETHERNET;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070020import static android.net.ConnectivityManager.TYPE_MOBILE;
Jeff Sharkeybdf98e82011-11-10 17:17:24 -080021import static android.net.ConnectivityManager.TYPE_WIFI;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070022import static android.net.ConnectivityManager.TYPE_WIMAX;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070023import static android.net.NetworkPolicy.LIMIT_DISABLED;
Jeff Sharkeya53188f2011-09-13 19:56:45 -070024import static android.net.NetworkPolicy.WARNING_DISABLED;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070025import static android.net.NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070026import static android.net.NetworkPolicyManager.POLICY_NONE;
27import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND;
Jeff Sharkey8a503642011-06-10 13:31:21 -070028import static android.net.NetworkPolicyManager.computeLastCycleBoundary;
29import static android.net.NetworkPolicyManager.computeNextCycleBoundary;
Jeff Sharkeya662e492011-06-18 21:57:06 -070030import static android.net.NetworkTemplate.MATCH_MOBILE_3G_LOWER;
31import static android.net.NetworkTemplate.MATCH_MOBILE_4G;
32import static android.net.NetworkTemplate.MATCH_MOBILE_ALL;
33import static android.net.NetworkTemplate.MATCH_WIFI;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070034import static android.net.NetworkTemplate.buildTemplateEthernet;
35import static android.net.NetworkTemplate.buildTemplateMobile3gLower;
36import static android.net.NetworkTemplate.buildTemplateMobile4g;
37import static android.net.NetworkTemplate.buildTemplateMobileAll;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070038import static android.net.NetworkTemplate.buildTemplateWifiWildcard;
Jeff Sharkey77dae912012-02-03 14:50:33 -080039import static android.net.TrafficStats.GB_IN_BYTES;
40import static android.net.TrafficStats.MB_IN_BYTES;
Jeff Sharkeya83a24f2011-09-16 01:52:39 -070041import static android.net.TrafficStats.UID_REMOVED;
42import static android.net.TrafficStats.UID_TETHERING;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070043import static android.telephony.TelephonyManager.SIM_STATE_READY;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070044import static android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
45import static android.text.format.DateUtils.FORMAT_SHOW_DATE;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070046import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -070047import static com.android.internal.util.Preconditions.checkNotNull;
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -070048import static com.android.settings.Utils.prepareCustomPreferencesList;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070049
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070050import android.animation.LayoutTransition;
Jeff Sharkey38305fb2012-09-14 16:26:51 -070051import android.app.ActivityManager;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070052import android.app.AlertDialog;
53import android.app.Dialog;
54import android.app.DialogFragment;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070055import android.app.Fragment;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070056import android.app.FragmentTransaction;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070057import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkeyb6548462014-07-21 15:38:06 -070058import android.content.ComponentName;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070059import android.content.Context;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070060import android.content.DialogInterface;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -070061import android.content.Intent;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070062import android.content.Loader;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070063import android.content.SharedPreferences;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070064import android.content.pm.PackageManager;
Jason Monk4bb075b2015-03-17 10:06:58 -040065import android.content.pm.PackageManager.NameNotFoundException;
Zoltan Szatmary-Ban77c1d362014-12-12 18:48:03 +000066import android.content.pm.UserInfo;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070067import android.content.res.Resources;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070068import android.graphics.Color;
Jeff Sharkey5d706792011-09-08 18:57:17 -070069import android.graphics.drawable.ColorDrawable;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070070import android.graphics.drawable.Drawable;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070071import android.net.ConnectivityManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070072import android.net.INetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070073import android.net.INetworkStatsService;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070074import android.net.INetworkStatsSession;
Jeff Sharkey8a503642011-06-10 13:31:21 -070075import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070076import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070077import android.net.NetworkStats;
78import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070079import android.net.NetworkTemplate;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070080import android.net.TrafficStats;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070081import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070082import android.os.Bundle;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070083import android.os.INetworkManagementService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070084import android.os.RemoteException;
85import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070086import android.os.SystemProperties;
Dianne Hackbornbb06a422012-08-16 11:01:57 -070087import android.os.UserHandle;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +010088import android.os.UserManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070089import android.preference.Preference;
Wink Savilleca756612014-11-08 10:47:12 -080090import android.telephony.SubscriptionInfo;
PauloftheWest50e6eca2014-10-03 11:07:14 -070091import android.telephony.SubscriptionManager;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070092import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070093import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070094import android.text.format.DateUtils;
95import android.text.format.Formatter;
Jeff Sharkeye5223a02012-03-09 17:11:14 -080096import android.text.format.Time;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070097import android.util.Log;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070098import android.util.SparseArray;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070099import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700100import android.view.Menu;
101import android.view.MenuInflater;
102import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700103import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700104import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700105import android.view.ViewGroup;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700106import android.widget.AdapterView;
107import android.widget.AdapterView.OnItemClickListener;
108import android.widget.AdapterView.OnItemSelectedListener;
109import android.widget.ArrayAdapter;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700110import android.widget.BaseAdapter;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700111import android.widget.Button;
Jason Monk20f464e2015-05-06 16:00:25 -0400112import android.widget.FrameLayout;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700113import android.widget.ImageView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700114import android.widget.LinearLayout;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700115import android.widget.ListView;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700116import android.widget.NumberPicker;
Jeff Sharkey2412b0f2011-07-17 20:31:40 -0700117import android.widget.ProgressBar;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700118import android.widget.Spinner;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700119import android.widget.Switch;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700120import android.widget.TabHost;
121import android.widget.TabHost.OnTabChangeListener;
122import android.widget.TabHost.TabContentFactory;
123import android.widget.TabHost.TabSpec;
124import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700125import android.widget.TextView;
Jason Monk20f464e2015-05-06 16:00:25 -0400126import android.widget.Toast;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700127
Chris Wren8a963ba2015-03-20 10:29:14 -0400128import com.android.internal.logging.MetricsLogger;
Wink Saville55434042012-06-14 12:33:43 -0700129import com.android.internal.telephony.PhoneConstants;
Jeff Sharkey5d706792011-09-08 18:57:17 -0700130import com.android.settings.drawable.InsetBoundsDrawable;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700131import com.android.settings.net.DataUsageMeteredSettings;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700132import com.android.settings.net.SummaryForAllUidLoader;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700133import com.android.settings.net.UidDetail;
134import com.android.settings.net.UidDetailProvider;
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -0700135import com.android.settings.search.BaseSearchIndexProvider;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -0700136import com.android.settings.search.Indexable;
137import com.android.settings.search.SearchIndexableRaw;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700138import com.android.settings.widget.ChartDataUsageView;
139import com.android.settings.widget.ChartDataUsageView.DataUsageChartListener;
PauloftheWestc80b7612014-11-25 04:40:45 -0800140import com.android.settings.widget.ChartNetworkSeriesView;
Wei Liude557d42015-07-13 12:01:42 -0700141import com.android.settingslib.AppItem;
Wei Liue0021c42015-07-07 15:37:11 -0700142import com.android.settingslib.NetworkPolicyEditor;
Wei Liude557d42015-07-13 12:01:42 -0700143import com.android.settingslib.net.ChartData;
144import com.android.settingslib.net.ChartDataLoader;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700145import com.google.android.collect.Lists;
146
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700147import libcore.util.Objects;
148
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700149import java.util.ArrayList;
150import java.util.Collections;
PauloftheWest50e6eca2014-10-03 11:07:14 -0700151import java.util.HashMap;
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700152import java.util.List;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700153import java.util.Locale;
PauloftheWest50e6eca2014-10-03 11:07:14 -0700154import java.util.Map;
155import java.util.Set;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700156
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700157/**
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700158 * Panel showing data usage history across various networks, including options
159 * to inspect based on usage cycle and control through {@link NetworkPolicy}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700160 */
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700161public class DataUsageSummary extends HighlightingFragment implements Indexable {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700162 private static final String TAG = "DataUsage";
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800163 private static final boolean LOGD = false;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700164
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700165 // TODO: remove this testing code
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700166 private static final boolean TEST_ANIM = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700167 private static final boolean TEST_RADIOS = false;
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800168
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700169 private static final String TEST_RADIOS_PROP = "test.radios";
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800170 private static final String TEST_SUBSCRIBER_PROP = "test.subscriberid";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700171
Jeff Sharkey8a503642011-06-10 13:31:21 -0700172 private static final String TAB_3G = "3g";
173 private static final String TAB_4G = "4g";
174 private static final String TAB_MOBILE = "mobile";
175 private static final String TAB_WIFI = "wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700176 private static final String TAB_ETHERNET = "ethernet";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700177
Jeff Sharkey28130d92011-09-02 16:10:24 -0700178 private static final String TAG_CONFIRM_DATA_DISABLE = "confirmDataDisable";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700179 private static final String TAG_CONFIRM_LIMIT = "confirmLimit";
180 private static final String TAG_CYCLE_EDITOR = "cycleEditor";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700181 private static final String TAG_WARNING_EDITOR = "warningEditor";
182 private static final String TAG_LIMIT_EDITOR = "limitEditor";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700183 private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700184 private static final String TAG_DENIED_RESTRICT = "deniedRestrict";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700185 private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700186 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700187
Fabrice Di Meglio2169c882014-04-18 15:18:40 -0700188 private static final String DATA_USAGE_ENABLE_MOBILE_KEY = "data_usage_enable_mobile";
189 private static final String DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY =
190 "data_usage_disable_mobile_limit";
191 private static final String DATA_USAGE_CYCLE_KEY = "data_usage_cycle";
192
Jason Monk4bb075b2015-03-17 10:06:58 -0400193 public static final String EXTRA_SHOW_APP_IMMEDIATE_PKG = "showAppImmediatePkg";
194
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700195 private static final int LOADER_CHART_DATA = 2;
196 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700197
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700198 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700199 private INetworkStatsService mStatsService;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700200 private NetworkPolicyManager mPolicyManager;
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700201 private TelephonyManager mTelephonyManager;
Wink Saville0183fb52014-11-22 10:11:39 -0800202 private SubscriptionManager mSubscriptionManager;
Xiaohui Chen44879a32015-07-22 13:53:22 -0700203 private UserManager mUserManager;
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700204
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700205 private INetworkStatsSession mStatsSession;
206
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700207 private static final String PREF_FILE = "data_usage";
208 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700209 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700210
211 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700212
Jeff Sharkey8a503642011-06-10 13:31:21 -0700213 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700214 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700215 private TabWidget mTabWidget;
216 private ListView mListView;
PauloftheWestc80b7612014-11-25 04:40:45 -0800217 private ChartNetworkSeriesView mSeries;
218 private ChartNetworkSeriesView mDetailedSeries;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700219 private DataUsageAdapter mAdapter;
220
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700221 /** Distance to inset content from sides, when needed. */
222 private int mInsetSide = 0;
223
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700224 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700225
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700226 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700227 private LinearLayout mNetworkSwitches;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700228 private boolean mDataEnabledSupported;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700229 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700230 private View mDataEnabledView;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700231 private boolean mDisableAtLimitSupported;
232 private Switch mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700233 private View mDisableAtLimitView;
234
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700235 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700236 private Spinner mCycleSpinner;
237 private CycleAdapter mCycleAdapter;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700238 private TextView mCycleSummary;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700239
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700240 private ChartDataUsageView mChart;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700241 private View mDisclaimer;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700242 private TextView mEmpty;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700243 private View mStupidPadding;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700244
245 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700246 private ImageView mAppIcon;
247 private ViewGroup mAppTitles;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700248 private TextView mAppTotal;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700249 private TextView mAppForeground;
250 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700251 private Button mAppSettings;
252
253 private LinearLayout mAppSwitches;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700254 private Switch mAppRestrict;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700255 private View mAppRestrictView;
256
Jeff Sharkey8a503642011-06-10 13:31:21 -0700257 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700258 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700259
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700260 private NetworkTemplate mTemplate;
261 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700262
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700263 private AppItem mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700264
265 private Intent mAppSettingsIntent;
266
Jeff Sharkeya662e492011-06-18 21:57:06 -0700267 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700268
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700269 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700270 private String mIntentTab = null;
271
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700272 private MenuItem mMenuRestrictBackground;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700273 private MenuItem mMenuShowWifi;
274 private MenuItem mMenuShowEthernet;
275 private MenuItem mMenuSimCards;
276 private MenuItem mMenuCellularNetworks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700277
Wink Savilleca756612014-11-08 10:47:12 -0800278 private List<SubscriptionInfo> mSubInfoList;
Wink Savilleb003a852014-10-23 10:16:26 -0700279 private Map<Integer,String> mMobileTagMap;
PauloftheWest50e6eca2014-10-03 11:07:14 -0700280
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700281 /** Flag used to ignore listeners during binding. */
282 private boolean mBinding;
283
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700284 private UidDetailProvider mUidDetailProvider;
285
Jason Monk4bb075b2015-03-17 10:06:58 -0400286 // Indicates request to show app immediately rather than list.
287 private String mShowAppImmediatePkg;
288
Sanket Padawed819270f2015-01-14 11:03:33 -0800289 /**
290 * Local cache of data enabled for subId, used to work around delays.
291 */
292 private final Map<String, Boolean> mMobileDataEnabled = new HashMap<String, Boolean>();
293
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700294 @Override
Chris Wren8a963ba2015-03-20 10:29:14 -0400295 protected int getMetricsCategory() {
296 return MetricsLogger.DATA_USAGE_SUMMARY;
297 }
298
299 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700300 public void onCreate(Bundle savedInstanceState) {
301 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700302 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700303
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700304 mNetworkService = INetworkManagementService.Stub.asInterface(
305 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700306 mStatsService = INetworkStatsService.Stub.asInterface(
307 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700308 mPolicyManager = NetworkPolicyManager.from(context);
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700309 mTelephonyManager = TelephonyManager.from(context);
Wink Saville0183fb52014-11-22 10:11:39 -0800310 mSubscriptionManager = SubscriptionManager.from(context);
Xiaohui Chen44879a32015-07-22 13:53:22 -0700311 mUserManager = UserManager.get(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700312
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700313 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700314
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700315 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700316 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700317
Wink Saville0183fb52014-11-22 10:11:39 -0800318 mSubInfoList = mSubscriptionManager.getActiveSubscriptionInfoList();
PauloftheWest50e6eca2014-10-03 11:07:14 -0700319 mMobileTagMap = initMobileTabTag(mSubInfoList);
320
Jaewan Kimffce9c12013-03-19 16:53:45 +0900321 try {
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700322 if (!mNetworkService.isBandwidthControlEnabled()) {
323 Log.w(TAG, "No bandwidth control; leaving");
324 getActivity().finish();
325 }
326 } catch (RemoteException e) {
327 Log.w(TAG, "No bandwidth control; leaving");
328 getActivity().finish();
329 }
330
331 try {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900332 mStatsSession = mStatsService.openSession();
333 } catch (RemoteException e) {
334 throw new RuntimeException(e);
335 }
336
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700337 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700338 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700339
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700340 // override preferences when no mobile radio
341 if (!hasReadyMobileRadio(context)) {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900342 mShowWifi = true;
343 mShowEthernet = true;
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700344 }
345
Jason Monk4bb075b2015-03-17 10:06:58 -0400346 mUidDetailProvider = new UidDetailProvider(context);
347
348 Bundle arguments = getArguments();
349 if (arguments != null) {
350 mShowAppImmediatePkg = arguments.getString(EXTRA_SHOW_APP_IMMEDIATE_PKG);
351 }
352
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700353 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700354 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700355
Jeff Sharkey8a503642011-06-10 13:31:21 -0700356 @Override
357 public View onCreateView(LayoutInflater inflater, ViewGroup container,
358 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700359
Jeff Sharkey8a503642011-06-10 13:31:21 -0700360 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700361 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
362
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700363
Jeff Sharkey8a503642011-06-10 13:31:21 -0700364 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700365 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700366 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
367 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700368
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700369 // decide if we need to manually inset our content, or if we should rely
370 // on parent container for inset.
371 final boolean shouldInset = mListView.getScrollBarStyle()
372 == View.SCROLLBARS_OUTSIDE_OVERLAY;
Amith Yamasani56f51a82013-08-05 10:07:23 -0700373 mInsetSide = 0;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700374
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700375 // adjust padding around tabwidget as needed
Amith Yamasani56f51a82013-08-05 10:07:23 -0700376 prepareCustomPreferencesList(container, view, mListView, false);
Jeff Sharkey5d706792011-09-08 18:57:17 -0700377
Jeff Sharkey8a503642011-06-10 13:31:21 -0700378 mTabHost.setup();
379 mTabHost.setOnTabChangedListener(mTabListener);
380
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700381 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700382 mHeader.setClickable(true);
383
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700384 mListView.addHeaderView(new View(context), null, true);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700385 mListView.addHeaderView(mHeader, null, true);
386 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700387
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700388 if (mInsetSide > 0) {
389 // inset selector and divider drawables
390 insetListViewDrawables(mListView, mInsetSide);
Fabrice Di Megliob27223f2013-01-15 18:54:11 -0800391 mHeader.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700392 }
393
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700394 {
395 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700396 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
397 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700398 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700399
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700400 mDataEnabled = new Switch(inflater.getContext());
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700401 mDataEnabled.setClickable(false);
402 mDataEnabled.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700403 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700404 mDataEnabledView.setTag(R.id.preference_highlight_key,
405 DATA_USAGE_ENABLE_MOBILE_KEY);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700406 mDataEnabledView.setClickable(true);
407 mDataEnabledView.setFocusable(true);
408 mDataEnabledView.setOnClickListener(mDataEnabledListener);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700409 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700410
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700411 mDisableAtLimit = new Switch(inflater.getContext());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700412 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700413 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700414 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700415 mDisableAtLimitView.setTag(R.id.preference_highlight_key,
416 DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700417 mDisableAtLimitView.setClickable(true);
418 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700419 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
420 mNetworkSwitches.addView(mDisableAtLimitView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700421
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700422 mCycleView = inflater.inflate(R.layout.data_usage_cycles, mNetworkSwitches, false);
423 mCycleView.setTag(R.id.preference_highlight_key, DATA_USAGE_CYCLE_KEY);
424 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
425 mCycleAdapter = new CycleAdapter(context);
426 mCycleSpinner.setAdapter(mCycleAdapter);
427 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
428 mCycleSummary = (TextView) mCycleView.findViewById(R.id.cycle_summary);
429 mNetworkSwitches.addView(mCycleView);
PauloftheWestc80b7612014-11-25 04:40:45 -0800430 mSeries = (ChartNetworkSeriesView)view.findViewById(R.id.series);
431 mDetailedSeries = (ChartNetworkSeriesView)view.findViewById(R.id.detail_series);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700432 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700433
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700434 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700435 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800436 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700437
438 {
439 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700440 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700441 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
442 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700443 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
444 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700445 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700446
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700447 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700448
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700449 mAppRestrict = new Switch(inflater.getContext());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700450 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700451 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700452 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700453 mAppRestrictView.setClickable(true);
454 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700455 mAppRestrictView.setOnClickListener(mAppRestrictListener);
456 mAppSwitches.addView(mAppRestrictView);
457 }
458
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700459 mDisclaimer = mHeader.findViewById(R.id.disclaimer);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700460 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700461 mStupidPadding = mHeader.findViewById(R.id.stupid_padding);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700462
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +0100463 final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
464 mAdapter = new DataUsageAdapter(um, mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700465 mListView.setOnItemClickListener(mListListener);
466 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700467
Jason Monk20f464e2015-05-06 16:00:25 -0400468 showRequestedAppIfNeeded(view);
Jason Monk4bb075b2015-03-17 10:06:58 -0400469
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700470 return view;
471 }
472
Jason Monk20f464e2015-05-06 16:00:25 -0400473 private void showRequestedAppIfNeeded(View rootView) {
Jason Monk4bb075b2015-03-17 10:06:58 -0400474 if (mShowAppImmediatePkg == null) {
475 return;
476 }
477 try {
478 int uid = getActivity().getPackageManager().getPackageUid(mShowAppImmediatePkg,
479 UserHandle.myUserId());
480 AppItem app = new AppItem(uid);
481 app.addUid(uid);
482
483 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
484 // When we are going straight to an app then we are coming from App Info and want
485 // a header at the top.
Jason Monk20f464e2015-05-06 16:00:25 -0400486 FrameLayout pinnedHeader = (FrameLayout) rootView.findViewById(R.id.pinned_header);
487 AppHeader.createAppHeader(getActivity(), detail.icon, detail.label, null, pinnedHeader);
Julia Reynolds1740ce42015-07-27 16:00:06 -0400488 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label, true);
Jason Monk4bb075b2015-03-17 10:06:58 -0400489 } catch (NameNotFoundException e) {
490 Log.w(TAG, "Could not find " + mShowAppImmediatePkg, e);
491 Toast.makeText(getActivity(), getString(R.string.unknown_app), Toast.LENGTH_LONG)
492 .show();
493 getActivity().finish();
494 }
495 }
496
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700497 @Override
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700498 public void onViewStateRestored(Bundle savedInstanceState) {
499 super.onViewStateRestored(savedInstanceState);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700500
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700501 // pick default tab based on incoming intent
502 final Intent intent = getActivity().getIntent();
503 mIntentTab = computeTabFromIntent(intent);
504
Jeff Sharkey8a503642011-06-10 13:31:21 -0700505 // this kicks off chain reaction which creates tabs, binds the body to
506 // selected network, and binds chart, cycles and detail list.
507 updateTabs();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700508 }
509
510 @Override
511 public void onResume() {
512 super.onResume();
513
514 getView().post(new Runnable() {
515 @Override
516 public void run() {
517 highlightViewIfNeeded();
518 }
519 });
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700520
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700521 // kick off background task to update stats
522 new AsyncTask<Void, Void, Void>() {
523 @Override
524 protected Void doInBackground(Void... params) {
525 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700526 // wait a few seconds before kicking off
527 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700528 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700529 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700530 } catch (RemoteException e) {
531 }
532 return null;
533 }
534
535 @Override
536 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700537 if (isAdded()) {
538 updateBody();
539 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700540 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700541 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700542 }
543
Jeff Sharkey8a503642011-06-10 13:31:21 -0700544 @Override
545 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
546 inflater.inflate(R.menu.data_usage, menu);
547 }
548
549 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700550 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700551 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700552 final boolean appDetailMode = isAppDetailMode();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700553 final boolean isAdmin = mUserManager.isAdminUser();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700554
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700555 mMenuShowWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700556 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
557 mMenuShowWifi.setVisible(!appDetailMode);
558 } else {
559 mMenuShowWifi.setVisible(false);
560 }
561
562 mMenuShowEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
563 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
564 mMenuShowEthernet.setVisible(!appDetailMode);
565 } else {
566 mMenuShowEthernet.setVisible(false);
567 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700568
569 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700570 mMenuRestrictBackground.setVisible(
Xiaohui Chen44879a32015-07-22 13:53:22 -0700571 hasReadyMobileRadio(context) && isAdmin && !appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700572
573 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
574 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700575 metered.setVisible(!appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700576 } else {
577 metered.setVisible(false);
578 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700579
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700580 // TODO: show when multiple sims available
581 mMenuSimCards = menu.findItem(R.id.data_usage_menu_sim_cards);
582 mMenuSimCards.setVisible(false);
583
584 mMenuCellularNetworks = menu.findItem(R.id.data_usage_menu_cellular_networks);
PauloftheWesta4b8fb32014-09-18 10:12:25 -0700585 mMenuCellularNetworks.setVisible(hasReadyMobileRadio(context)
Xiaohui Chen44879a32015-07-22 13:53:22 -0700586 && !appDetailMode && isAdmin);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700587
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700588 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
589 String helpUrl;
590 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
Jason Monk6e613472015-06-16 10:31:38 -0400591 HelpUtils.prepareHelpMenuItem(getActivity(), help, helpUrl, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700592 } else {
593 help.setVisible(false);
594 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700595
596 updateMenuTitles();
597 }
598
599 private void updateMenuTitles() {
600 if (mPolicyManager.getRestrictBackground()) {
601 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_allow_background);
602 } else {
603 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_restrict_background);
604 }
605
606 if (mShowWifi) {
607 mMenuShowWifi.setTitle(R.string.data_usage_menu_hide_wifi);
608 } else {
609 mMenuShowWifi.setTitle(R.string.data_usage_menu_show_wifi);
610 }
611
612 if (mShowEthernet) {
613 mMenuShowEthernet.setTitle(R.string.data_usage_menu_hide_ethernet);
614 } else {
615 mMenuShowEthernet.setTitle(R.string.data_usage_menu_show_ethernet);
616 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700617 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700618
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700619 @Override
620 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700621 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700622 case R.id.data_usage_menu_restrict_background: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700623 final boolean restrictBackground = !mPolicyManager.getRestrictBackground();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700624 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800625 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700626 } else {
627 // no confirmation to drop restriction
628 setRestrictBackground(false);
629 }
630 return true;
631 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700632 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700633 mShowWifi = !mShowWifi;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700634 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700635 updateMenuTitles();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700636 updateTabs();
637 return true;
638 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700639 case R.id.data_usage_menu_show_ethernet: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700640 mShowEthernet = !mShowEthernet;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700641 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700642 updateMenuTitles();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700643 updateTabs();
644 return true;
645 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700646 case R.id.data_usage_menu_sim_cards: {
647 // TODO: hook up to sim cards
648 return true;
649 }
650 case R.id.data_usage_menu_cellular_networks: {
651 final Intent intent = new Intent(Intent.ACTION_MAIN);
652 intent.setComponent(new ComponentName("com.android.phone",
653 "com.android.phone.MobileNetworkSettings"));
654 startActivity(intent);
655 return true;
656 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700657 case R.id.data_usage_menu_metered: {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800658 final SettingsActivity sa = (SettingsActivity) getActivity();
659 sa.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700660 R.string.data_usage_metered_title, null, this, 0);
661 return true;
662 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700663 }
664 return false;
665 }
666
Jeff Sharkey94a90952011-06-13 22:31:09 -0700667 @Override
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700668 public void onDestroy() {
Jeff Sharkey94a90952011-06-13 22:31:09 -0700669 mDataEnabledView = null;
670 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700671
672 mUidDetailProvider.clearCache();
673 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700674
675 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700676
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800677 super.onDestroy();
678 }
679
Jeff Sharkey8a503642011-06-10 13:31:21 -0700680 /**
Jeff Sharkey92811822012-05-04 11:47:29 -0700681 * Build and assign {@link LayoutTransition} to various containers. Should
682 * only be assigned after initial layout is complete.
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700683 */
Jeff Sharkey92811822012-05-04 11:47:29 -0700684 private void ensureLayoutTransitions() {
Jason Monk4bb075b2015-03-17 10:06:58 -0400685 if (mShowAppImmediatePkg != null) {
686 // If we are skipping right to showing an app, we don't care about transitions.
687 return;
688 }
Jeff Sharkey92811822012-05-04 11:47:29 -0700689 // skip when already setup
690 if (mChart.getLayoutTransition() != null) return;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700691
Jeff Sharkey92811822012-05-04 11:47:29 -0700692 mTabsContainer.setLayoutTransition(buildLayoutTransition());
693 mHeader.setLayoutTransition(buildLayoutTransition());
694 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700695
Jeff Sharkey92811822012-05-04 11:47:29 -0700696 final LayoutTransition chartTransition = buildLayoutTransition();
697 chartTransition.disableTransitionType(LayoutTransition.APPEARING);
698 chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
699 mChart.setLayoutTransition(chartTransition);
700 }
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700701
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700702 private static LayoutTransition buildLayoutTransition() {
703 final LayoutTransition transition = new LayoutTransition();
704 if (TEST_ANIM) {
705 transition.setDuration(1500);
706 }
707 transition.setAnimateParentHierarchy(false);
708 return transition;
709 }
710
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700711 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700712 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700713 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
714 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700715 */
716 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700717 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700718 mTabHost.clearAllTabs();
719
Sanket Padawe7e21fa52015-02-12 12:39:34 -0800720 int simCount = mTelephonyManager.getSimCount();
721
Stuart Scottf51d0062015-04-08 09:40:48 -0700722 List<SubscriptionInfo> sirs = mSubscriptionManager.getActiveSubscriptionInfoList();
723 if (sirs != null) {
724 for (SubscriptionInfo sir : sirs) {
Sanket Padawe7e21fa52015-02-12 12:39:34 -0800725 addMobileTab(context, sir, (simCount > 1));
PauloftheWestc80b7612014-11-25 04:40:45 -0800726 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700727 }
PauloftheWestc80b7612014-11-25 04:40:45 -0800728
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700729 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700730 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
731 }
PauloftheWestc80b7612014-11-25 04:40:45 -0800732
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700733 if (mShowEthernet && hasEthernet(context)) {
734 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
735 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700736
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700737 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700738 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
739 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
740 if (mIntentTab != null) {
741 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700742 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700743 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700744 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700745 mTabHost.setCurrentTabByTag(mIntentTab);
746 }
747 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700748 } else if (noTabs) {
749 // no usable tabs, so hide body
750 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700751 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700752 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700753 }
754 }
755
Jeff Sharkey8a503642011-06-10 13:31:21 -0700756 /**
757 * Factory that provide empty {@link View} to make {@link TabHost} happy.
758 */
759 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700760 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700761 public View createTabContent(String tag) {
762 return new View(mTabHost.getContext());
763 }
764 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700765
Jeff Sharkey8a503642011-06-10 13:31:21 -0700766 /**
767 * Build {@link TabSpec} with thin indicator, and empty content.
768 */
769 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700770 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
771 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700772 }
773
PauloftheWest50e6eca2014-10-03 11:07:14 -0700774 /**
775 * Build {@link TabSpec} with thin indicator, and empty content.
776 */
Stuart Scottb1531812014-11-04 14:52:23 -0800777 private TabSpec buildTabSpec(String tag, CharSequence title) {
PauloftheWest50e6eca2014-10-03 11:07:14 -0700778 return mTabHost.newTabSpec(tag).setIndicator(title).setContent(
779 mEmptyTabContent);
780 }
781
782
Jeff Sharkey8a503642011-06-10 13:31:21 -0700783 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700784 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700785 public void onTabChanged(String tabId) {
786 // user changed tab; update body
787 updateBody();
788 }
789 };
790
791 /**
792 * Update body content based on current tab. Loads
793 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
794 * binds them to visible controls.
795 */
796 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700797 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700798 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700799
Jeff Sharkeya662e492011-06-18 21:57:06 -0700800 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700801 final String currentTab = mTabHost.getCurrentTabTag();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700802 final boolean isAdmin = mUserManager.isAdminUser();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700803
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700804 if (currentTab == null) {
805 Log.w(TAG, "no tab selected; hiding body");
806 mListView.setVisibility(View.GONE);
807 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700808 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700809 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700810 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700811
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700812 mCurrentTab = currentTab;
813
Jeff Sharkey8a503642011-06-10 13:31:21 -0700814 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
815
Xiaohui Chen44879a32015-07-22 13:53:22 -0700816 mDataEnabledSupported = isAdmin;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700817 mDisableAtLimitSupported = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700818
Wink Savillefcec91f2014-12-02 17:12:08 -0800819 // TODO: remove mobile tabs when SIM isn't ready probably by
820 // TODO: using SubscriptionManager.getActiveSubscriptionInfoList.
821 if (LOGD) Log.d(TAG, "updateBody() isMobileTab=" + isMobileTab(currentTab));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700822
PauloftheWest50e6eca2014-10-03 11:07:14 -0700823 if (isMobileTab(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800824 if (LOGD) Log.d(TAG, "updateBody() mobile tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700825 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
826 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
PauloftheWest50e6eca2014-10-03 11:07:14 -0700827 mDataEnabledSupported = isMobileDataAvailable(getSubId(currentTab));
Jeff Sharkey4e2d16c2014-12-03 13:44:26 -0800828
829 // Match mobile traffic for this subscriber, but normalize it to
830 // catch any other merged subscribers.
831 mTemplate = buildTemplateMobileAll(
832 getActiveSubscriberId(context, getSubId(currentTab)));
833 mTemplate = NetworkTemplate.normalize(mTemplate,
834 mTelephonyManager.getMergedSubscriberIds());
835
Jeff Sharkey8a503642011-06-10 13:31:21 -0700836 } else if (TAB_3G.equals(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800837 if (LOGD) Log.d(TAG, "updateBody() 3g tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700838 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
839 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
840 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700841 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700842
843 } else if (TAB_4G.equals(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800844 if (LOGD) Log.d(TAG, "updateBody() 4g tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700845 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
846 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
847 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700848 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700849
850 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700851 // wifi doesn't have any controls
Wink Savillefcec91f2014-12-02 17:12:08 -0800852 if (LOGD) Log.d(TAG, "updateBody() wifi tab");
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700853 mDataEnabledSupported = false;
854 mDisableAtLimitSupported = false;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700855 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700856
857 } else if (TAB_ETHERNET.equals(currentTab)) {
858 // ethernet doesn't have any controls
Wink Savillefcec91f2014-12-02 17:12:08 -0800859 if (LOGD) Log.d(TAG, "updateBody() ethernet tab");
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700860 mDataEnabledSupported = false;
861 mDisableAtLimitSupported = false;
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700862 mTemplate = buildTemplateEthernet();
863
864 } else {
Wink Savillefcec91f2014-12-02 17:12:08 -0800865 if (LOGD) Log.d(TAG, "updateBody() unknown tab");
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700866 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700867 }
868
Sanket Padawe716b1f82015-06-24 14:26:21 -0700869 mPolicyEditor.read();
870 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
871 if (policy != null) {
872 final long currentTime = System.currentTimeMillis();
873 final long start = computeLastCycleBoundary(currentTime, policy);
874 final long end = currentTime;
875 long totalBytes = 0;
876
877 try {
878 totalBytes = mStatsService.getNetworkTotalBytes(policy.template, start, end);
879 } catch (RuntimeException e) {
880 } catch (RemoteException e) {
881 }
882
883 if (policy.isOverLimit(totalBytes) && policy.lastLimitSnooze < start) {
884 setPreferenceSummary(mDataEnabledView,
885 getString(R.string.data_usage_cellular_data_summary));
886 } else {
887 final TextView summary = (TextView) mDataEnabledView
888 .findViewById(android.R.id.summary);
889 summary.setVisibility(View.GONE);
890 }
891 }
892
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700893 // kick off loader for network history
894 // TODO: consider chaining two loaders together instead of reloading
895 // network history when showing app detail.
896 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700897 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700898
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700899 // detail mode can change visible menus, invalidate
900 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700901
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700902 mBinding = false;
PauloftheWestc80b7612014-11-25 04:40:45 -0800903
Alan Viverette55eaa962015-03-18 18:10:52 -0700904 int seriesColor = context.getColor(R.color.sim_noitification);
PauloftheWestc80b7612014-11-25 04:40:45 -0800905 if (mCurrentTab != null && mCurrentTab.length() > TAB_MOBILE.length() ){
906 final int slotId = Integer.parseInt(mCurrentTab.substring(TAB_MOBILE.length(),
907 mCurrentTab.length()));
Stuart Scottf51d0062015-04-08 09:40:48 -0700908 final SubscriptionInfo sir = mSubscriptionManager
909 .getActiveSubscriptionInfoForSimSlotIndex(slotId);
PauloftheWestc80b7612014-11-25 04:40:45 -0800910
911 if (sir != null) {
912 seriesColor = sir.getIconTint();
913 }
914 }
915
Jeff Sharkey9c167e92015-01-26 15:15:59 -0700916 final int secondaryColor = Color.argb(127, Color.red(seriesColor), Color.green(seriesColor),
917 Color.blue(seriesColor));
918 mSeries.setChartColor(Color.BLACK, seriesColor, secondaryColor);
919 mDetailedSeries.setChartColor(Color.BLACK, seriesColor, secondaryColor);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700920 }
921
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700922 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700923 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700924 }
925
926 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700927 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700928 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700929 */
930 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700931 final Context context = getActivity();
932 final PackageManager pm = context.getPackageManager();
933 final LayoutInflater inflater = getActivity().getLayoutInflater();
934
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700935 if (isAppDetailMode()) {
936 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700937 mCycleAdapter.setChangeVisible(false);
Julia Reynolds1740ce42015-07-27 16:00:06 -0400938 mChart.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700939 } else {
940 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700941 mCycleAdapter.setChangeVisible(true);
Julia Reynolds1740ce42015-07-27 16:00:06 -0400942 mChart.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700943
944 // hide detail stats when not in detail mode
945 mChart.bindDetailNetworkStats(null);
946 return;
947 }
948
949 // remove warning/limit sweeps while in detail mode
950 mChart.bindNetworkPolicy(null);
951
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700952 // show icon and all labels appearing under this app
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700953 final int uid = mCurrentApp.key;
954 final UidDetail detail = mUidDetailProvider.getUidDetail(uid, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700955 mAppIcon.setImageDrawable(detail.icon);
956
957 mAppTitles.removeAllViews();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700958
959 View title = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700960 if (detail.detailLabels != null) {
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100961 final int n = detail.detailLabels.length;
962 for (int i = 0; i < n; ++i) {
963 CharSequence label = detail.detailLabels[i];
964 CharSequence contentDescription = detail.detailContentDescriptions[i];
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700965 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100966 TextView appTitle = (TextView) title.findViewById(R.id.app_title);
967 appTitle.setText(label);
968 appTitle.setContentDescription(contentDescription);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700969 mAppTitles.addView(title);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700970 }
971 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700972 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100973 TextView appTitle = (TextView) title.findViewById(R.id.app_title);
974 appTitle.setText(detail.label);
975 appTitle.setContentDescription(detail.contentDescription);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700976 mAppTitles.addView(title);
977 }
978
979 // Remember last slot for summary
980 if (title != null) {
981 mAppTotal = (TextView) title.findViewById(R.id.app_summary);
982 } else {
983 mAppTotal = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700984 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700985
986 // enable settings button when package provides it
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700987 final String[] packageNames = pm.getPackagesForUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700988 if (packageNames != null && packageNames.length > 0) {
989 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700990 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
991
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700992 // Search for match across all packages
993 boolean matchFound = false;
994 for (String packageName : packageNames) {
995 mAppSettingsIntent.setPackage(packageName);
996 if (pm.resolveActivity(mAppSettingsIntent, 0) != null) {
997 matchFound = true;
998 break;
999 }
1000 }
1001
Zoltan Szatmary-Banf20d39e2014-08-07 15:27:08 +01001002 mAppSettings.setOnClickListener(new OnClickListener() {
1003 @Override
1004 public void onClick(View v) {
1005 if (!isAdded()) {
1006 return;
1007 }
1008
1009 // TODO: target towards entire UID instead of just first package
1010 getActivity().startActivityAsUser(mAppSettingsIntent,
1011 new UserHandle(UserHandle.getUserId(uid)));
1012 }
1013 });
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001014 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -07001015 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001016
1017 } else {
1018 mAppSettingsIntent = null;
Zoltan Szatmary-Banf20d39e2014-08-07 15:27:08 +01001019 mAppSettings.setOnClickListener(null);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001020 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001021 }
1022
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001023 updateDetailData();
1024
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001025 if (UserHandle.isApp(uid) && !mPolicyManager.getRestrictBackground()
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001026 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001027 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -08001028 setPreferenceSummary(mAppRestrictView,
1029 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001030
1031 mAppRestrictView.setVisibility(View.VISIBLE);
1032 mAppRestrict.setChecked(getAppRestrictBackground());
1033
1034 } else {
1035 mAppRestrictView.setVisibility(View.GONE);
1036 }
1037 }
1038
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001039 private void setPolicyWarningBytes(long warningBytes) {
1040 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -07001041 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001042 updatePolicy(false);
1043 }
1044
1045 private void setPolicyLimitBytes(long limitBytes) {
1046 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -07001047 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001048 updatePolicy(false);
1049 }
1050
Wink Savilleb003a852014-10-23 10:16:26 -07001051 private boolean isMobileDataEnabled(int subId) {
Wink Savillea4f4d182014-12-05 15:34:46 -08001052 if (LOGD) Log.d(TAG, "isMobileDataEnabled:+ subId=" + subId);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001053 boolean isEnable = false;
Sanket Padawed819270f2015-01-14 11:03:33 -08001054 if (mMobileDataEnabled.get(String.valueOf(subId)) != null) {
1055 //TODO: deprecate and remove this once enabled flag is on policy
1056 //Multiple Subscriptions, the value need to be reseted
1057 isEnable = mMobileDataEnabled.get(String.valueOf(subId)).booleanValue();
Wink Savillea4f4d182014-12-05 15:34:46 -08001058 if (LOGD) {
1059 Log.d(TAG, "isMobileDataEnabled: != null, subId=" + subId
1060 + " isEnable=" + isEnable);
1061 }
Sanket Padawed819270f2015-01-14 11:03:33 -08001062 mMobileDataEnabled.put(String.valueOf(subId), null);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001063 } else {
Wink Savillea4f4d182014-12-05 15:34:46 -08001064 // SUB SELECT
1065 isEnable = mTelephonyManager.getDataEnabled(subId);
1066 if (LOGD) {
1067 Log.d(TAG, "isMobileDataEnabled: == null, subId=" + subId
1068 + " isEnable=" + isEnable);
1069 }
Jeff Sharkey28130d92011-09-02 16:10:24 -07001070 }
PauloftheWest50e6eca2014-10-03 11:07:14 -07001071 return isEnable;
Jeff Sharkey28130d92011-09-02 16:10:24 -07001072 }
1073
Sanket Padawe24f834d2015-01-08 11:23:11 -08001074 private void setMobileDataEnabled(int subId, boolean enabled) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001075 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
Sanket Padawe24f834d2015-01-08 11:23:11 -08001076 mTelephonyManager.setDataEnabled(subId, enabled);
Sanket Padawed819270f2015-01-14 11:03:33 -08001077 mMobileDataEnabled.put(String.valueOf(subId), enabled);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001078 updatePolicy(false);
1079 }
1080
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001081 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001082 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked()
Xiaohui Chen44879a32015-07-22 13:53:22 -07001083 && mUserManager.isAdminUser();
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001084 }
1085
1086 private boolean isBandwidthControlEnabled() {
1087 try {
1088 return mNetworkService.isBandwidthControlEnabled();
1089 } catch (RemoteException e) {
1090 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
1091 return false;
1092 }
1093 }
1094
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001095 public void setRestrictBackground(boolean restrictBackground) {
1096 mPolicyManager.setRestrictBackground(restrictBackground);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001097 updateMenuTitles();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001098 }
1099
1100 private boolean getAppRestrictBackground() {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001101 final int uid = mCurrentApp.key;
1102 final int uidPolicy = mPolicyManager.getUidPolicy(uid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001103 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
1104 }
1105
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001106 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001107 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001108 final int uid = mCurrentApp.key;
1109 mPolicyManager.setUidPolicy(
1110 uid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001111 mAppRestrict.setChecked(restrictBackground);
1112 }
1113
Jeff Sharkey8a503642011-06-10 13:31:21 -07001114 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001115 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
1116 * current {@link #mTemplate}.
1117 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001118 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001119 boolean dataEnabledVisible = mDataEnabledSupported;
1120 boolean disableAtLimitVisible = mDisableAtLimitSupported;
1121
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001122 if (isAppDetailMode()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001123 dataEnabledVisible = false;
1124 disableAtLimitVisible = false;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001125 }
1126
Jeff Sharkey28130d92011-09-02 16:10:24 -07001127 // TODO: move enabled state directly into policy
PauloftheWest50e6eca2014-10-03 11:07:14 -07001128 if (isMobileTab(mCurrentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001129 mBinding = true;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001130 mDataEnabled.setChecked(isMobileDataEnabled(getSubId(mCurrentTab)));
Jeff Sharkey28130d92011-09-02 16:10:24 -07001131 mBinding = false;
1132 }
1133
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001134 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001135 //SUB SELECT
1136 if (isNetworkPolicyModifiable(policy) && isMobileDataAvailable(getSubId(mCurrentTab))) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001137 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001138 if (!isAppDetailMode()) {
1139 mChart.bindNetworkPolicy(policy);
1140 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001141
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001142 } else {
1143 // controls are disabled; don't bind warning/limit sweeps
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001144 disableAtLimitVisible = false;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001145 mChart.bindNetworkPolicy(null);
1146 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001147
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001148 mDataEnabledView.setVisibility(dataEnabledVisible ? View.VISIBLE : View.GONE);
1149 mDisableAtLimitView.setVisibility(disableAtLimitVisible ? View.VISIBLE : View.GONE);
1150
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001151 if (refreshCycle) {
1152 // generate cycle list based on policy and available history
1153 updateCycleList(policy);
1154 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001155 }
1156
1157 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001158 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
1159 * and available {@link NetworkStatsHistory} data. Always selects the newest
1160 * item, updating the inspection range on {@link #mChart}.
1161 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001162 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001163 // stash away currently selected cycle to try restoring below
1164 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -07001165 mCycleAdapter.clear();
1166
1167 final Context context = mCycleSpinner.getContext();
1168
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001169 long historyStart = Long.MAX_VALUE;
1170 long historyEnd = Long.MIN_VALUE;
1171 if (mChartData != null) {
1172 historyStart = mChartData.network.getStart();
1173 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001174 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001175
Jeff Sharkey461842a2011-09-25 18:22:48 -07001176 final long now = System.currentTimeMillis();
1177 if (historyStart == Long.MAX_VALUE) historyStart = now;
1178 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001179
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001180 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001181 if (policy != null) {
1182 // find the next cycle boundary
1183 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001184
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001185 // walk backwards, generating all valid cycle ranges
1186 while (cycleEnd > historyStart) {
1187 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
1188 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
1189 + historyStart);
1190 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1191 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001192 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001193 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001194
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001195 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001196 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001197 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001198
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001199 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001200 // no policy defined cycles; show entry for each four-week period
1201 long cycleEnd = historyEnd;
1202 while (cycleEnd > historyStart) {
1203 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
1204 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1205 cycleEnd = cycleStart;
1206 }
1207
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001208 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001209 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001210
1211 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001212 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001213 final int position = mCycleAdapter.findNearestPosition(previousItem);
1214 mCycleSpinner.setSelection(position);
1215
1216 // only force-update cycle when changed; skipping preserves any
1217 // user-defined inspection region.
1218 final CycleItem selectedItem = mCycleAdapter.getItem(position);
1219 if (!Objects.equal(selectedItem, previousItem)) {
1220 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1221 } else {
1222 // but still kick off loader for detailed list
1223 updateDetailData();
1224 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001225 } else {
1226 updateDetailData();
1227 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001228 }
1229
Sanket Padawed819270f2015-01-14 11:03:33 -08001230 private void disableDataForOtherSubscriptions(SubscriptionInfo currentSir) {
1231 if (mSubInfoList != null) {
1232 for (SubscriptionInfo subInfo : mSubInfoList) {
1233 if (subInfo.getSubscriptionId() != currentSir.getSubscriptionId()) {
1234 setMobileDataEnabled(subInfo.getSubscriptionId(), false);
1235 }
1236 }
1237 }
1238 }
1239
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001240 private View.OnClickListener mDataEnabledListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001241 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001242 public void onClick(View v) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001243 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001244
Chris Wren1b6ffba2015-05-08 17:10:01 -04001245 final boolean enabled = !mDataEnabled.isChecked();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001246 final String currentTab = mCurrentTab;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001247 if (isMobileTab(currentTab)) {
Chris Wren1b6ffba2015-05-08 17:10:01 -04001248 MetricsLogger.action(getContext(), MetricsLogger.ACTION_CELL_DATA_TOGGLE, enabled);
1249 if (enabled) {
PauloftheWestde338442014-10-29 14:46:44 -07001250 // If we are showing the Sim Card tile then we are a Multi-Sim device.
1251 if (Utils.showSimCardTile(getActivity())) {
1252 handleMultiSimDataDialog();
1253 } else {
Sanket Padawe24f834d2015-01-08 11:23:11 -08001254 setMobileDataEnabled(getSubId(currentTab), true);
PauloftheWestde338442014-10-29 14:46:44 -07001255 }
Jeff Sharkey28130d92011-09-02 16:10:24 -07001256 } else {
1257 // disabling data; show confirmation dialog which eventually
1258 // calls setMobileDataEnabled() once user confirms.
Sanket Padawe24f834d2015-01-08 11:23:11 -08001259 ConfirmDataDisableFragment.show(DataUsageSummary.this, getSubId(mCurrentTab));
Jeff Sharkey28130d92011-09-02 16:10:24 -07001260 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001261 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001262
Jeff Sharkey28130d92011-09-02 16:10:24 -07001263 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001264 }
1265 };
1266
PauloftheWestde338442014-10-29 14:46:44 -07001267 private void handleMultiSimDataDialog() {
PauloftheWestf0d84e22014-12-10 08:50:54 -08001268 final Context context = getActivity();
1269 final SubscriptionInfo currentSir = getCurrentTabSubInfo(context);
Sanket Padawe766ab602015-01-20 10:36:15 -08001270
1271 //If sim has not loaded after toggling data switch, return.
1272 if (currentSir == null) {
1273 return;
1274 }
1275
Stuart Scottf51d0062015-04-08 09:40:48 -07001276 final SubscriptionInfo nextSir = mSubscriptionManager.getDefaultDataSubscriptionInfo();
PauloftheWestde338442014-10-29 14:46:44 -07001277
PauloftheWestf0d84e22014-12-10 08:50:54 -08001278 // If the device is single SIM or is enabling data on the active data SIM then forgo
1279 // the pop-up.
1280 if (!Utils.showSimCardTile(context) ||
Sanket Padawed819270f2015-01-14 11:03:33 -08001281 (nextSir != null && currentSir != null &&
1282 currentSir.getSubscriptionId() == nextSir.getSubscriptionId())) {
Sanket Padawe24f834d2015-01-08 11:23:11 -08001283 setMobileDataEnabled(currentSir.getSubscriptionId(), true);
Sanket Padawed819270f2015-01-14 11:03:33 -08001284 if (nextSir != null && currentSir != null &&
1285 currentSir.getSubscriptionId() == nextSir.getSubscriptionId()) {
1286 disableDataForOtherSubscriptions(currentSir);
1287 }
PauloftheWestde338442014-10-29 14:46:44 -07001288 updateBody();
1289 return;
1290 }
1291
PauloftheWestf0d84e22014-12-10 08:50:54 -08001292 final String previousName = (nextSir == null)
1293 ? context.getResources().getString(R.string.sim_selection_required_pref)
1294 : nextSir.getDisplayName().toString();
1295
PauloftheWestde338442014-10-29 14:46:44 -07001296 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
1297
1298 builder.setTitle(R.string.sim_change_data_title);
1299 builder.setMessage(getActivity().getResources().getString(R.string.sim_change_data_message,
PauloftheWestf0d84e22014-12-10 08:50:54 -08001300 currentSir.getDisplayName(), previousName));
PauloftheWestde338442014-10-29 14:46:44 -07001301
1302 builder.setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
1303 @Override
1304 public void onClick(DialogInterface dialog, int id) {
Wink Saville0183fb52014-11-22 10:11:39 -08001305 mSubscriptionManager.setDefaultDataSubId(currentSir.getSubscriptionId());
Sanket Padawe24f834d2015-01-08 11:23:11 -08001306 setMobileDataEnabled(currentSir.getSubscriptionId(), true);
Sanket Padawed819270f2015-01-14 11:03:33 -08001307 disableDataForOtherSubscriptions(currentSir);
PauloftheWestde338442014-10-29 14:46:44 -07001308 updateBody();
1309 }
1310 });
1311 builder.setNegativeButton(R.string.cancel, null);
1312
1313 builder.create().show();
1314 }
1315
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001316 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001317 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001318 public void onClick(View v) {
1319 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001320 if (disableAtLimit) {
1321 // enabling limit; show confirmation dialog which eventually
1322 // calls setPolicyLimitBytes() once user confirms.
1323 ConfirmLimitFragment.show(DataUsageSummary.this);
1324 } else {
1325 setPolicyLimitBytes(LIMIT_DISABLED);
1326 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001327 }
1328 };
1329
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001330 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001331 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001332 public void onClick(View v) {
1333 final boolean restrictBackground = !mAppRestrict.isChecked();
1334
1335 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001336 // enabling restriction; show confirmation dialog which
1337 // eventually calls setRestrictBackground() once user
1338 // confirms.
1339 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001340 } else {
1341 setAppRestrictBackground(false);
1342 }
1343 }
1344 };
1345
Jeff Sharkey8a503642011-06-10 13:31:21 -07001346 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001347 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001348 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001349 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001350 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001351
1352 // TODO: sigh, remove this hack once we understand 6450986
1353 if (mUidDetailProvider == null || app == null) return;
1354
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001355 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001356 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001357 }
1358 };
1359
1360 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001361 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001362 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1363 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1364 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001365 // show cycle editor; will eventually call setPolicyCycleDay()
1366 // when user finishes editing.
1367 CycleEditorFragment.show(DataUsageSummary.this);
1368
1369 // reset spinner to something other than "change cycle..."
1370 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001371
1372 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001373 if (LOGD) {
1374 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1375 + cycle.end + "]");
1376 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001377
1378 // update chart to show selected cycle, and update detail data
1379 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001380 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001381
1382 updateDetailData();
1383 }
1384 }
1385
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001386 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001387 public void onNothingSelected(AdapterView<?> parent) {
1388 // ignored
1389 }
1390 };
1391
1392 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001393 * Update details based on {@link #mChart} inspection range depending on
1394 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1395 * of applications data usage, and when {@link #isAppDetailMode()} update
1396 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001397 */
1398 private void updateDetailData() {
1399 if (LOGD) Log.d(TAG, "updateDetailData()");
1400
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001401 final long start = mChart.getInspectStart();
1402 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001403 final long now = System.currentTimeMillis();
1404
1405 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001406
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001407 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001408 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001409 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001410 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001411 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001412 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001413 final long foregroundBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001414 final long totalBytes = defaultBytes + foregroundBytes;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001415
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001416 if (mAppTotal != null) {
1417 mAppTotal.setText(Formatter.formatFileSize(context, totalBytes));
1418 }
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001419 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1420 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1421
1422 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001423 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001424
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001425 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001426
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001427 mCycleSummary.setVisibility(View.GONE);
1428
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001429 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001430 if (mChartData != null) {
1431 entry = mChartData.network.getValues(start, end, now, null);
1432 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001433
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001434 mCycleSummary.setVisibility(View.VISIBLE);
1435
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001436 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001437 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001438 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001439 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001440
1441 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1442 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001443 mCycleSummary.setText(totalPhrase);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001444
PauloftheWest50e6eca2014-10-03 11:07:14 -07001445 if (isMobileTab(mCurrentTab) || TAB_3G.equals(mCurrentTab)
László Dávid0b8bf4e2012-10-24 23:31:47 +02001446 || TAB_4G.equals(mCurrentTab)) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001447 if (isAppDetailMode()) {
1448 mDisclaimer.setVisibility(View.GONE);
1449 } else {
1450 mDisclaimer.setVisibility(View.VISIBLE);
1451 }
Jeff Sharkeye557c332012-04-13 16:04:07 -07001452 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001453 mDisclaimer.setVisibility(View.GONE);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001454 }
1455
Jeff Sharkey92811822012-05-04 11:47:29 -07001456 // initial layout is finished above, ensure we have transitions
1457 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001458 }
1459
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001460 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1461 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001462 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001463 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001464 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001465 }
1466
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001467 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001468 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1469 mChartData = data;
1470 mChart.bindNetworkStats(mChartData.network);
1471 mChart.bindDetailNetworkStats(mChartData.detail);
1472
1473 // calcuate policy cycles based on available data
1474 updatePolicy(true);
1475 updateAppDetail();
1476
1477 // force scroll to top of body when showing detail
1478 if (mChartData.detail != null) {
1479 mListView.smoothScrollToPosition(0);
1480 }
1481 }
1482
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001483 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001484 public void onLoaderReset(Loader<ChartData> loader) {
1485 mChartData = null;
1486 mChart.bindNetworkStats(null);
1487 mChart.bindDetailNetworkStats(null);
1488 }
1489 };
1490
1491 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001492 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001493 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001494 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001495 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001496 }
1497
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001498 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001499 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001500 final int[] restrictedUids = mPolicyManager.getUidsWithPolicy(
Jeff Sharkeye557c332012-04-13 16:04:07 -07001501 POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001502 mAdapter.bindStats(data, restrictedUids);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001503 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001504 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001505
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001506 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001507 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001508 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001509 updateEmptyVisible();
1510 }
1511
1512 private void updateEmptyVisible() {
1513 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1514 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001515 mStupidPadding.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001516 }
1517 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001518
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001519 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001520 final TelephonyManager tele = TelephonyManager.from(context);
1521 final String actualSubscriberId = tele.getSubscriberId();
Wink Savillefcec91f2014-12-02 17:12:08 -08001522 String retVal = SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
1523 if (LOGD) Log.d(TAG, "getActiveSubscriberId=" + retVal + " actualSubscriberId=" + actualSubscriberId);
1524 return retVal;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001525 }
1526
Wink Savilleb003a852014-10-23 10:16:26 -07001527 private static String getActiveSubscriberId(Context context, int subId) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07001528 final TelephonyManager tele = TelephonyManager.from(context);
Wink Savillefcec91f2014-12-02 17:12:08 -08001529 String retVal = tele.getSubscriberId(subId);
1530 if (LOGD) Log.d(TAG, "getActiveSubscriberId=" + retVal + " subId=" + subId);
1531 return retVal;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001532 }
1533
Jeff Sharkey8a503642011-06-10 13:31:21 -07001534 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001535 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001536 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001537 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001538 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001539
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001540 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001541 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001542 setPolicyLimitBytes(mChart.getLimitBytes());
Sanket Padawe716b1f82015-06-24 14:26:21 -07001543 updateBody();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001544 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001545
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001546 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001547 public void requestWarningEdit() {
1548 WarningEditorFragment.show(DataUsageSummary.this);
1549 }
1550
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001551 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001552 public void requestLimitEdit() {
1553 LimitEditorFragment.show(DataUsageSummary.this);
1554 }
1555 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001556
1557 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001558 * List item that reflects a specific data usage cycle.
1559 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001560 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001561 public CharSequence label;
1562 public long start;
1563 public long end;
1564
Jeff Sharkey8a503642011-06-10 13:31:21 -07001565 CycleItem(CharSequence label) {
1566 this.label = label;
1567 }
1568
1569 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001570 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001571 this.start = start;
1572 this.end = end;
1573 }
1574
Jeff Sharkey8a503642011-06-10 13:31:21 -07001575 @Override
1576 public String toString() {
1577 return label.toString();
1578 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001579
1580 @Override
1581 public boolean equals(Object o) {
1582 if (o instanceof CycleItem) {
1583 final CycleItem another = (CycleItem) o;
1584 return start == another.start && end == another.end;
1585 }
1586 return false;
1587 }
1588
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001589 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001590 public int compareTo(CycleItem another) {
1591 return Long.compare(start, another.start);
1592 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001593 }
1594
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001595 private static final StringBuilder sBuilder = new StringBuilder(50);
1596 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1597 sBuilder, Locale.getDefault());
1598
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001599 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001600 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001601
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001602 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001603 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001604 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1605 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001606 }
1607 }
1608
Jeff Sharkey8a503642011-06-10 13:31:21 -07001609 /**
1610 * Special-case data usage cycle that triggers dialog to change
1611 * {@link NetworkPolicy#cycleDay}.
1612 */
1613 public static class CycleChangeItem extends CycleItem {
1614 public CycleChangeItem(Context context) {
1615 super(context.getString(R.string.data_usage_change_cycle));
1616 }
1617 }
1618
1619 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001620 private boolean mChangePossible = false;
1621 private boolean mChangeVisible = false;
1622
1623 private final CycleChangeItem mChangeItem;
1624
Jeff Sharkey8a503642011-06-10 13:31:21 -07001625 public CycleAdapter(Context context) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001626 super(context, R.layout.data_usage_cycle_item);
1627 setDropDownViewResource(R.layout.data_usage_cycle_item_dropdown);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001628 mChangeItem = new CycleChangeItem(context);
1629 }
1630
1631 public void setChangePossible(boolean possible) {
1632 mChangePossible = possible;
1633 updateChange();
1634 }
1635
1636 public void setChangeVisible(boolean visible) {
1637 mChangeVisible = visible;
1638 updateChange();
1639 }
1640
1641 private void updateChange() {
1642 remove(mChangeItem);
1643 if (mChangePossible && mChangeVisible) {
1644 add(mChangeItem);
1645 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001646 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001647
1648 /**
1649 * Find position of {@link CycleItem} in this adapter which is nearest
1650 * the given {@link CycleItem}.
1651 */
1652 public int findNearestPosition(CycleItem target) {
1653 if (target != null) {
1654 final int count = getCount();
1655 for (int i = count - 1; i >= 0; i--) {
1656 final CycleItem item = getItem(i);
1657 if (item instanceof CycleChangeItem) {
1658 continue;
1659 } else if (item.compareTo(target) >= 0) {
1660 return i;
1661 }
1662 }
1663 }
1664 return 0;
1665 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001666 }
1667
1668 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001669 * Adapter of applications, sorted by total usage descending.
1670 */
1671 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001672 private final UidDetailProvider mProvider;
1673 private final int mInsetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001674 private final UserManager mUm;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001675
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001676 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001677 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001678
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001679 public DataUsageAdapter(final UserManager userManager, UidDetailProvider provider, int insetSide) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001680 mProvider = checkNotNull(provider);
1681 mInsetSide = insetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001682 mUm = userManager;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001683 }
1684
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001685 /**
1686 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1687 */
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001688 public void bindStats(NetworkStats stats, int[] restrictedUids) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001689 mItems.clear();
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001690 mLargest = 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001691
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001692 final int currentUserId = ActivityManager.getCurrentUser();
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001693 final List<UserHandle> profiles = mUm.getUserProfiles();
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001694 final SparseArray<AppItem> knownItems = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001695
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001696 NetworkStats.Entry entry = null;
1697 final int size = stats != null ? stats.size() : 0;
1698 for (int i = 0; i < size; i++) {
1699 entry = stats.getValues(i, entry);
1700
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001701 // Decide how to collapse items together
1702 final int uid = entry.uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001703
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001704 final int collapseKey;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001705 final int category;
1706 final int userId = UserHandle.getUserId(uid);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001707 if (UserHandle.isApp(uid)) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001708 if (profiles.contains(new UserHandle(userId))) {
1709 if (userId != currentUserId) {
1710 // Add to a managed user item.
1711 final int managedKey = UidDetailProvider.buildKeyForUser(userId);
1712 accumulate(managedKey, knownItems, entry,
1713 AppItem.CATEGORY_USER);
1714 }
1715 // Add to app item.
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001716 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001717 category = AppItem.CATEGORY_APP;
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001718 } else {
Zoltan Szatmary-Ban77c1d362014-12-12 18:48:03 +00001719 // If it is a removed user add it to the removed users' key
1720 final UserInfo info = mUm.getUserInfo(userId);
1721 if (info == null) {
1722 collapseKey = UID_REMOVED;
1723 category = AppItem.CATEGORY_APP;
1724 } else {
1725 // Add to other user item.
1726 collapseKey = UidDetailProvider.buildKeyForUser(userId);
1727 category = AppItem.CATEGORY_USER;
1728 }
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001729 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001730 } else if (uid == UID_REMOVED || uid == UID_TETHERING) {
1731 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001732 category = AppItem.CATEGORY_APP;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001733 } else {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001734 collapseKey = android.os.Process.SYSTEM_UID;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001735 category = AppItem.CATEGORY_APP;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001736 }
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001737 accumulate(collapseKey, knownItems, entry, category);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001738 }
1739
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001740 final int restrictedUidsMax = restrictedUids.length;
1741 for (int i = 0; i < restrictedUidsMax; ++i) {
1742 final int uid = restrictedUids[i];
1743 // Only splice in restricted state for current user or managed users
1744 if (!profiles.contains(new UserHandle(UserHandle.getUserId(uid)))) {
1745 continue;
1746 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001747
1748 AppItem item = knownItems.get(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001749 if (item == null) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001750 item = new AppItem(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001751 item.total = -1;
1752 mItems.add(item);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001753 knownItems.put(item.key, item);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001754 }
1755 item.restricted = true;
1756 }
1757
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001758 if (!mItems.isEmpty()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001759 final AppItem title = new AppItem();
1760 title.category = AppItem.CATEGORY_APP_TITLE;
1761 mItems.add(title);
1762 }
1763
Jeff Sharkey8a503642011-06-10 13:31:21 -07001764 Collections.sort(mItems);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001765 notifyDataSetChanged();
1766 }
1767
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001768 /**
1769 * Accumulate data usage of a network stats entry for the item mapped by the collapse key.
1770 * Creates the item if needed.
1771 *
1772 * @param collapseKey the collapse key used to map the item.
1773 * @param knownItems collection of known (already existing) items.
1774 * @param entry the network stats entry to extract data usage from.
1775 * @param itemCategory the item is categorized on the list view by this category. Must be
1776 * either AppItem.APP_ITEM_CATEGORY or AppItem.MANAGED_USER_ITEM_CATEGORY
1777 */
1778 private void accumulate(int collapseKey, final SparseArray<AppItem> knownItems,
1779 NetworkStats.Entry entry, int itemCategory) {
1780 final int uid = entry.uid;
1781 AppItem item = knownItems.get(collapseKey);
1782 if (item == null) {
1783 item = new AppItem(collapseKey);
1784 item.category = itemCategory;
1785 mItems.add(item);
1786 knownItems.put(item.key, item);
1787 }
1788 item.addUid(uid);
1789 item.total += entry.rxBytes + entry.txBytes;
1790 if (mLargest < item.total) {
1791 mLargest = item.total;
1792 }
1793 }
1794
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001795 @Override
1796 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001797 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001798 }
1799
1800 @Override
1801 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001802 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001803 }
1804
1805 @Override
1806 public long getItemId(int position) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001807 return mItems.get(position).key;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001808 }
1809
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001810 /**
1811 * See {@link #getItemViewType} for the view types.
1812 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001813 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001814 public int getViewTypeCount() {
1815 return 2;
1816 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001817
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001818 /**
1819 * Returns 1 for separator items and 0 for anything else.
1820 */
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001821 @Override
1822 public int getItemViewType(int position) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001823 final AppItem item = mItems.get(position);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001824 if (item.category == AppItem.CATEGORY_APP_TITLE) {
1825 return 1;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001826 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001827 return 0;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001828 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001829 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001830
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001831 @Override
1832 public boolean areAllItemsEnabled() {
1833 return false;
1834 }
1835
1836 @Override
1837 public boolean isEnabled(int position) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001838 if (position > mItems.size()) {
1839 throw new ArrayIndexOutOfBoundsException();
1840 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001841 return getItemViewType(position) == 0;
1842 }
1843
1844 @Override
1845 public View getView(int position, View convertView, ViewGroup parent) {
1846 final AppItem item = mItems.get(position);
Jason Monk2cdafc62015-06-12 12:32:50 -04001847 LayoutInflater inflater = LayoutInflater.from(parent.getContext());
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001848 if (getItemViewType(position) == 1) {
1849 if (convertView == null) {
Jason Monk2cdafc62015-06-12 12:32:50 -04001850 convertView = Utils.inflateCategoryHeader(inflater, parent);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001851 }
1852
1853 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
1854 title.setText(R.string.data_usage_app);
1855
1856 } else {
1857 if (convertView == null) {
Jason Monk2cdafc62015-06-12 12:32:50 -04001858 convertView = inflater.inflate(R.layout.data_usage_item, parent, false);
1859 inflater.inflate(R.layout.widget_progress_bar,
1860 (ViewGroup) convertView.findViewById(android.R.id.widget_frame));
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001861
1862 if (mInsetSide > 0) {
1863 convertView.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
1864 }
1865 }
1866
1867 final Context context = parent.getContext();
1868
Jason Monk2cdafc62015-06-12 12:32:50 -04001869 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001870 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1871 android.R.id.progress);
1872
1873 // kick off async load of app details
1874 UidDetailTask.bindView(mProvider, item, convertView);
1875
1876 if (item.restricted && item.total <= 0) {
Jason Monk2cdafc62015-06-12 12:32:50 -04001877 summary.setText(R.string.data_usage_app_restricted);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001878 progress.setVisibility(View.GONE);
1879 } else {
Jason Monk2cdafc62015-06-12 12:32:50 -04001880 summary.setText(Formatter.formatFileSize(context, item.total));
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001881 progress.setVisibility(View.VISIBLE);
1882 }
1883
1884 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1885 progress.setProgress(percentTotal);
1886 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001887
1888 return convertView;
1889 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001890 }
1891
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001892 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001893 * Empty {@link Fragment} that controls display of UID details in
1894 * {@link DataUsageSummary}.
1895 */
1896 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001897 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001898
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001899 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jason Monk4bb075b2015-03-17 10:06:58 -04001900 show(parent, app, label, true);
1901 }
1902
1903 public static void show(DataUsageSummary parent, AppItem app, CharSequence label,
1904 boolean addToBack) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001905 if (!parent.isAdded()) return;
1906
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001907 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001908 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001909
1910 final AppDetailsFragment fragment = new AppDetailsFragment();
1911 fragment.setArguments(args);
1912 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001913 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1914 ft.add(fragment, TAG_APP_DETAILS);
Jason Monk4bb075b2015-03-17 10:06:58 -04001915 if (addToBack) {
1916 ft.addToBackStack(TAG_APP_DETAILS);
1917 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001918 ft.setBreadCrumbTitle(
1919 parent.getResources().getString(R.string.data_usage_app_summary_title));
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001920 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001921 }
1922
1923 @Override
1924 public void onStart() {
1925 super.onStart();
1926 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001927 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001928 target.updateBody();
1929 }
1930
1931 @Override
1932 public void onStop() {
1933 super.onStop();
1934 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001935 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001936 target.updateBody();
1937 }
Julia Reynolds1740ce42015-07-27 16:00:06 -04001938
1939 @Override
1940 public boolean onOptionsItemSelected(MenuItem item) {
1941 switch (item.getItemId()) {
1942 case android.R.id.home:
1943 getFragmentManager().popBackStack();
1944 return true;
1945 }
1946 return super.onOptionsItemSelected(item);
1947 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001948 }
1949
1950 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001951 * Dialog to request user confirmation before setting
1952 * {@link NetworkPolicy#limitBytes}.
1953 */
1954 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001955 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001956 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001957
1958 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001959 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001960
Jeff Sharkeyd0a55312014-08-08 10:25:24 -07001961 final NetworkPolicy policy = parent.mPolicyEditor.getPolicy(parent.mTemplate);
1962 if (policy == null) return;
1963
Jeff Sharkey461842a2011-09-25 18:22:48 -07001964 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001965 final CharSequence message;
Jeff Sharkeyd0a55312014-08-08 10:25:24 -07001966 final long minLimitBytes = (long) (policy.warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001967 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001968
1969 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001970 final String currentTab = parent.mCurrentTab;
1971 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001972 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001973 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001974 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001975 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001976 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001977 } else if (isMobileTab(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001978 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001979 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001980 } else {
1981 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001982 }
1983
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001984 final Bundle args = new Bundle();
1985 args.putCharSequence(EXTRA_MESSAGE, message);
1986 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1987
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001988 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1989 dialog.setArguments(args);
1990 dialog.setTargetFragment(parent, 0);
1991 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1992 }
1993
1994 @Override
1995 public Dialog onCreateDialog(Bundle savedInstanceState) {
1996 final Context context = getActivity();
1997
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001998 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001999 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
2000
2001 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2002 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07002003 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002004
2005 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002006 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002007 public void onClick(DialogInterface dialog, int which) {
2008 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2009 if (target != null) {
2010 target.setPolicyLimitBytes(limitBytes);
2011 }
2012 }
2013 });
2014
2015 return builder.create();
2016 }
2017 }
2018
2019 /**
2020 * Dialog to edit {@link NetworkPolicy#cycleDay}.
2021 */
2022 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002023 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002024
2025 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002026 if (!parent.isAdded()) return;
2027
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002028 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002029 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002030
2031 final CycleEditorFragment dialog = new CycleEditorFragment();
2032 dialog.setArguments(args);
2033 dialog.setTargetFragment(parent, 0);
2034 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
2035 }
2036
2037 @Override
2038 public Dialog onCreateDialog(Bundle savedInstanceState) {
2039 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002040 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2041 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002042
2043 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2044 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2045
2046 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
2047 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
2048
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002049 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2050 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002051
2052 cycleDayPicker.setMinValue(1);
2053 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002054 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002055 cycleDayPicker.setWrapSelectorWheel(true);
2056
2057 builder.setTitle(R.string.data_usage_cycle_editor_title);
2058 builder.setView(view);
2059
2060 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2061 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002062 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002063 public void onClick(DialogInterface dialog, int which) {
Jeff Sharkeyd277de92013-03-25 15:11:25 -07002064 // clear focus to finish pending text edits
2065 cycleDayPicker.clearFocus();
2066
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002067 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08002068 final String cycleTimezone = new Time().timezone;
2069 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002070 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002071 }
2072 });
2073
2074 return builder.create();
2075 }
2076 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002077
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002078 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002079 * Dialog to edit {@link NetworkPolicy#warningBytes}.
2080 */
2081 public static class WarningEditorFragment extends DialogFragment {
2082 private static final String EXTRA_TEMPLATE = "template";
2083
2084 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002085 if (!parent.isAdded()) return;
2086
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002087 final Bundle args = new Bundle();
2088 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
2089
2090 final WarningEditorFragment dialog = new WarningEditorFragment();
2091 dialog.setArguments(args);
2092 dialog.setTargetFragment(parent, 0);
2093 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
2094 }
2095
2096 @Override
2097 public Dialog onCreateDialog(Bundle savedInstanceState) {
2098 final Context context = getActivity();
2099 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2100 final NetworkPolicyEditor editor = target.mPolicyEditor;
2101
2102 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2103 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2104
2105 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
2106 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
2107
2108 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2109 final long warningBytes = editor.getPolicyWarningBytes(template);
2110 final long limitBytes = editor.getPolicyLimitBytes(template);
2111
2112 bytesPicker.setMinValue(0);
2113 if (limitBytes != LIMIT_DISABLED) {
2114 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
2115 } else {
2116 bytesPicker.setMaxValue(Integer.MAX_VALUE);
2117 }
2118 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
2119 bytesPicker.setWrapSelectorWheel(false);
2120
2121 builder.setTitle(R.string.data_usage_warning_editor_title);
2122 builder.setView(view);
2123
2124 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2125 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002126 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002127 public void onClick(DialogInterface dialog, int which) {
2128 // clear focus to finish pending text edits
2129 bytesPicker.clearFocus();
2130
2131 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
2132 editor.setPolicyWarningBytes(template, bytes);
2133 target.updatePolicy(false);
2134 }
2135 });
2136
2137 return builder.create();
2138 }
2139 }
2140
2141 /**
2142 * Dialog to edit {@link NetworkPolicy#limitBytes}.
2143 */
2144 public static class LimitEditorFragment extends DialogFragment {
2145 private static final String EXTRA_TEMPLATE = "template";
2146
2147 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002148 if (!parent.isAdded()) return;
2149
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002150 final Bundle args = new Bundle();
2151 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
2152
2153 final LimitEditorFragment dialog = new LimitEditorFragment();
2154 dialog.setArguments(args);
2155 dialog.setTargetFragment(parent, 0);
2156 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
2157 }
2158
2159 @Override
2160 public Dialog onCreateDialog(Bundle savedInstanceState) {
2161 final Context context = getActivity();
2162 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2163 final NetworkPolicyEditor editor = target.mPolicyEditor;
2164
2165 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2166 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2167
2168 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
2169 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
2170
2171 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2172 final long warningBytes = editor.getPolicyWarningBytes(template);
2173 final long limitBytes = editor.getPolicyLimitBytes(template);
2174
2175 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09002176 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002177 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
2178 } else {
2179 bytesPicker.setMinValue(0);
2180 }
2181 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
2182 bytesPicker.setWrapSelectorWheel(false);
2183
2184 builder.setTitle(R.string.data_usage_limit_editor_title);
2185 builder.setView(view);
2186
2187 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2188 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002189 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002190 public void onClick(DialogInterface dialog, int which) {
2191 // clear focus to finish pending text edits
2192 bytesPicker.clearFocus();
2193
2194 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
2195 editor.setPolicyLimitBytes(template, bytes);
2196 target.updatePolicy(false);
2197 }
2198 });
2199
2200 return builder.create();
2201 }
2202 }
2203 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07002204 * Dialog to request user confirmation before disabling data.
2205 */
2206 public static class ConfirmDataDisableFragment extends DialogFragment {
Sanket Padawe24f834d2015-01-08 11:23:11 -08002207 static int mSubId;
2208 public static void show(DataUsageSummary parent, int subId) {
2209 mSubId = subId;
Jeff Sharkey461842a2011-09-25 18:22:48 -07002210 if (!parent.isAdded()) return;
2211
Jeff Sharkey28130d92011-09-02 16:10:24 -07002212 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
2213 dialog.setTargetFragment(parent, 0);
2214 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
2215 }
2216
2217 @Override
2218 public Dialog onCreateDialog(Bundle savedInstanceState) {
2219 final Context context = getActivity();
2220
2221 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2222 builder.setMessage(R.string.data_usage_disable_mobile);
2223
2224 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002225 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07002226 public void onClick(DialogInterface dialog, int which) {
2227 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2228 if (target != null) {
2229 // TODO: extend to modify policy enabled flag.
Sanket Padawe24f834d2015-01-08 11:23:11 -08002230 target.setMobileDataEnabled(mSubId, false);
Jeff Sharkey28130d92011-09-02 16:10:24 -07002231 }
2232 }
2233 });
2234 builder.setNegativeButton(android.R.string.cancel, null);
2235
2236 return builder.create();
2237 }
2238 }
2239
2240 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002241 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07002242 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002243 */
2244 public static class ConfirmRestrictFragment extends DialogFragment {
2245 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002246 if (!parent.isAdded()) return;
2247
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002248 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
2249 dialog.setTargetFragment(parent, 0);
2250 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
2251 }
2252
2253 @Override
2254 public Dialog onCreateDialog(Bundle savedInstanceState) {
2255 final Context context = getActivity();
2256
2257 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002258 builder.setTitle(R.string.data_usage_restrict_background_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07002259 if (Utils.hasMultipleUsers(context)) {
2260 builder.setMessage(R.string.data_usage_restrict_background_multiuser);
2261 } else {
2262 builder.setMessage(R.string.data_usage_restrict_background);
2263 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002264
2265 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002266 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002267 public void onClick(DialogInterface dialog, int which) {
2268 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2269 if (target != null) {
2270 target.setRestrictBackground(true);
2271 }
2272 }
2273 });
2274 builder.setNegativeButton(android.R.string.cancel, null);
2275
2276 return builder.create();
2277 }
2278 }
2279
2280 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002281 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
2282 * change has been denied, usually based on
2283 * {@link DataUsageSummary#hasLimitedNetworks()}.
2284 */
2285 public static class DeniedRestrictFragment extends DialogFragment {
2286 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002287 if (!parent.isAdded()) return;
2288
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002289 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
2290 dialog.setTargetFragment(parent, 0);
2291 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
2292 }
2293
2294 @Override
2295 public Dialog onCreateDialog(Bundle savedInstanceState) {
2296 final Context context = getActivity();
2297
2298 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2299 builder.setTitle(R.string.data_usage_app_restrict_background);
2300 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
2301 builder.setPositiveButton(android.R.string.ok, null);
2302
2303 return builder.create();
2304 }
2305 }
2306
2307 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002308 * Dialog to request user confirmation before setting
2309 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
2310 */
2311 public static class ConfirmAppRestrictFragment extends DialogFragment {
2312 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002313 if (!parent.isAdded()) return;
2314
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002315 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2316 dialog.setTargetFragment(parent, 0);
2317 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2318 }
2319
2320 @Override
2321 public Dialog onCreateDialog(Bundle savedInstanceState) {
2322 final Context context = getActivity();
2323
2324 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002325 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2326 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2327
2328 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002329 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002330 public void onClick(DialogInterface dialog, int which) {
2331 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2332 if (target != null) {
2333 target.setAppRestrictBackground(true);
2334 }
2335 }
2336 });
2337 builder.setNegativeButton(android.R.string.cancel, null);
2338
2339 return builder.create();
2340 }
2341 }
2342
2343 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002344 * Compute default tab that should be selected, based on
2345 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2346 */
2347 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002348 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002349 if (template == null) {
Wink Savilleb003a852014-10-23 10:16:26 -07002350 final int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
Wink Saville0183fb52014-11-22 10:11:39 -08002351 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Savilled72c0622014-12-11 10:10:05 -08002352 if (SubscriptionManager.isValidSubscriptionId(subId)) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07002353 return TAB_MOBILE + String.valueOf(subId);
2354 }
2355 return null;
2356 }
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002357
2358 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002359 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002360 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002361 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002362 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002363 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002364 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002365 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002366 return TAB_WIFI;
2367 default:
2368 return null;
2369 }
2370 }
2371
2372 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002373 * Background task that loads {@link UidDetail}, binding to
2374 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002375 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002376 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2377 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002378 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002379 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002380
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002381 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002382 mProvider = checkNotNull(provider);
2383 mItem = checkNotNull(item);
2384 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002385 }
2386
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002387 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002388 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002389 final UidDetailTask existing = (UidDetailTask) target.getTag();
2390 if (existing != null) {
2391 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002392 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002393
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002394 final UidDetail cachedDetail = provider.getUidDetail(item.key, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002395 if (cachedDetail != null) {
2396 bindView(cachedDetail, target);
2397 } else {
2398 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2399 AsyncTask.THREAD_POOL_EXECUTOR));
2400 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002401 }
2402
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002403 private static void bindView(UidDetail detail, View target) {
2404 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2405 final TextView title = (TextView) target.findViewById(android.R.id.title);
2406
2407 if (detail != null) {
2408 icon.setImageDrawable(detail.icon);
2409 title.setText(detail.label);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +01002410 title.setContentDescription(detail.contentDescription);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002411 } else {
2412 icon.setImageDrawable(null);
2413 title.setText(null);
2414 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002415 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002416
2417 @Override
2418 protected void onPreExecute() {
2419 bindView(null, mTarget);
2420 }
2421
2422 @Override
2423 protected UidDetail doInBackground(Void... params) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002424 return mProvider.getUidDetail(mItem.key, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002425 }
2426
2427 @Override
2428 protected void onPostExecute(UidDetail result) {
2429 bindView(result, mTarget);
2430 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002431 }
2432
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002433 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002434 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002435 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002436 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002437 if (TEST_RADIOS) {
2438 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2439 }
2440
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002441 final ConnectivityManager conn = ConnectivityManager.from(context);
2442 final TelephonyManager tele = TelephonyManager.from(context);
2443
Wink Saville0183fb52014-11-22 10:11:39 -08002444 final List<SubscriptionInfo> subInfoList =
2445 SubscriptionManager.from(context).getActiveSubscriptionInfoList();
2446 // No activated Subscriptions
PauloftheWest50e6eca2014-10-03 11:07:14 -07002447 if (subInfoList == null) {
Wink Savillefcec91f2014-12-02 17:12:08 -08002448 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subInfoList=null");
PauloftheWest50e6eca2014-10-03 11:07:14 -07002449 return false;
2450 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002451 // require both supported network and ready SIM
PauloftheWest50e6eca2014-10-03 11:07:14 -07002452 boolean isReady = true;
Wink Savilleca756612014-11-08 10:47:12 -08002453 for (SubscriptionInfo subInfo : subInfoList) {
Stuart Scott3ada2ec2014-10-31 14:11:24 -07002454 isReady = isReady & tele.getSimState(subInfo.getSimSlotIndex()) == SIM_STATE_READY;
Wink Savillefcec91f2014-12-02 17:12:08 -08002455 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subInfo=" + subInfo);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002456 }
Wink Savillefcec91f2014-12-02 17:12:08 -08002457 boolean retVal = conn.isNetworkSupported(TYPE_MOBILE) && isReady;
2458 if (LOGD) {
2459 Log.d(TAG, "hasReadyMobileRadio:"
2460 + " conn.isNetworkSupported(TYPE_MOBILE)="
2461 + conn.isNetworkSupported(TYPE_MOBILE)
2462 + " isReady=" + isReady);
2463 }
2464 return retVal;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002465 }
2466
2467 /*
2468 * TODO: consider adding to TelephonyManager or SubscritpionManager.
2469 */
Wink Savilleb003a852014-10-23 10:16:26 -07002470 public static boolean hasReadyMobileRadio(Context context, int subId) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07002471 if (TEST_RADIOS) {
2472 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2473 }
2474
2475 final ConnectivityManager conn = ConnectivityManager.from(context);
2476 final TelephonyManager tele = TelephonyManager.from(context);
2477 final int slotId = SubscriptionManager.getSlotId(subId);
2478 final boolean isReady = tele.getSimState(slotId) == SIM_STATE_READY;
2479
Wink Savillefcec91f2014-12-02 17:12:08 -08002480 boolean retVal = conn.isNetworkSupported(TYPE_MOBILE) && isReady;
2481 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subId=" + subId
2482 + " conn.isNetworkSupported(TYPE_MOBILE)=" + conn.isNetworkSupported(TYPE_MOBILE)
2483 + " isReady=" + isReady);
2484 return retVal;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002485 }
2486
2487 /**
2488 * Test if device has a mobile 4G data radio.
2489 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002490 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002491 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2492 return false;
2493 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002494 if (TEST_RADIOS) {
2495 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2496 }
2497
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002498 final ConnectivityManager conn = ConnectivityManager.from(context);
2499 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002500
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002501 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Wink Saville55434042012-06-14 12:33:43 -07002502 final boolean hasLte = (tele.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE)
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002503 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002504 return hasWimax || hasLte;
2505 }
2506
2507 /**
2508 * Test if device has a Wi-Fi data radio.
2509 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002510 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002511 if (TEST_RADIOS) {
2512 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2513 }
2514
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002515 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002516 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002517 }
2518
2519 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002520 * Test if device has an ethernet network connection.
2521 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002522 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002523 if (TEST_RADIOS) {
2524 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2525 }
2526
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002527 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002528 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2529
2530 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002531 if (mStatsSession != null) {
2532 try {
2533 ethernetBytes = mStatsSession.getSummaryForNetwork(
2534 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2535 .getTotalBytes();
2536 } catch (RemoteException e) {
2537 throw new RuntimeException(e);
2538 }
2539 } else {
2540 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002541 }
2542
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002543 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002544 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002545 }
2546
2547 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002548 * Inflate a {@link Preference} style layout, adding the given {@link View}
2549 * widget into {@link android.R.id#widget_frame}.
2550 */
2551 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2552 final View view = inflater.inflate(R.layout.preference, root, false);
2553 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2554 android.R.id.widget_frame);
2555 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2556 return view;
2557 }
2558
2559 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002560 * Test if any networks are currently limited.
2561 */
2562 private boolean hasLimitedNetworks() {
2563 return !buildLimitedNetworksList().isEmpty();
2564 }
2565
2566 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002567 * Build string describing currently limited networks, which defines when
2568 * background data is restricted.
2569 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002570 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002571 private CharSequence buildLimitedNetworksString() {
2572 final List<CharSequence> limited = buildLimitedNetworksList();
2573
2574 // handle case where no networks limited
2575 if (limited.isEmpty()) {
2576 limited.add(getText(R.string.data_usage_list_none));
2577 }
2578
2579 return TextUtils.join(limited);
2580 }
2581
2582 /**
2583 * Build list of currently limited networks, which defines when background
2584 * data is restricted.
2585 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002586 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002587 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002588 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002589
2590 // build combined list of all limited networks
2591 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002592
2593 final TelephonyManager tele = TelephonyManager.from(context);
2594 if (tele.getSimState() == SIM_STATE_READY) {
2595 final String subscriberId = getActiveSubscriberId(context);
2596 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2597 limited.add(getText(R.string.data_usage_list_mobile));
2598 }
2599 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2600 limited.add(getText(R.string.data_usage_tab_3g));
2601 }
2602 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2603 limited.add(getText(R.string.data_usage_tab_4g));
2604 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002605 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002606
2607 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002608 limited.add(getText(R.string.data_usage_tab_wifi));
2609 }
2610 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2611 limited.add(getText(R.string.data_usage_tab_ethernet));
2612 }
2613
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002614 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002615 }
2616
2617 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002618 * Inset both selector and divider {@link Drawable} on the given
2619 * {@link ListView} by the requested dimensions.
2620 */
2621 private static void insetListViewDrawables(ListView view, int insetSide) {
2622 final Drawable selector = view.getSelector();
2623 final Drawable divider = view.getDivider();
2624
2625 // fully unregister these drawables so callbacks can be maintained after
2626 // wrapping below.
2627 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2628 view.setSelector(stub);
2629 view.setDivider(stub);
2630
2631 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2632 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2633 }
2634
2635 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002636 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002637 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002638 */
2639 private static void setPreferenceTitle(View parent, int resId) {
2640 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2641 title.setText(resId);
2642 }
2643
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002644 /**
2645 * Set {@link android.R.id#summary} for a preference view inflated with
2646 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2647 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002648 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002649 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2650 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002651 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002652 }
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002653
2654 /**
2655 * For search
2656 */
2657 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -07002658 new BaseSearchIndexProvider() {
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002659 @Override
2660 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
2661 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
2662
2663 final Resources res = context.getResources();
2664
2665 // Add fragment title
2666 SearchIndexableRaw data = new SearchIndexableRaw(context);
2667 data.title = res.getString(R.string.data_usage_summary_title);
2668 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2669 result.add(data);
2670
2671 // Mobile data
2672 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002673 data.key = DATA_USAGE_ENABLE_MOBILE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002674 data.title = res.getString(R.string.data_usage_enable_mobile);
2675 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2676 result.add(data);
2677
2678 // Set mobile data limit
2679 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002680 data.key = DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002681 data.title = res.getString(R.string.data_usage_disable_mobile_limit);
2682 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2683 result.add(data);
2684
Fabrice Di Megliof2a52262014-04-17 17:20:27 -07002685 // Data usage cycle
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002686 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002687 data.key = DATA_USAGE_CYCLE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002688 data.title = res.getString(R.string.data_usage_cycle);
2689 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2690 result.add(data);
2691
2692 return result;
2693 }
2694 };
2695
Sanket Padawe7e21fa52015-02-12 12:39:34 -08002696 private void addMobileTab(Context context, SubscriptionInfo subInfo, boolean isMultiSim) {
PauloftheWestc80b7612014-11-25 04:40:45 -08002697 if (subInfo != null && mMobileTagMap != null) {
2698 if (hasReadyMobileRadio(context, subInfo.getSubscriptionId())) {
Sanket Padawe7e21fa52015-02-12 12:39:34 -08002699 if (isMultiSim) {
2700 mTabHost.addTab(buildTabSpec(mMobileTagMap.get(subInfo.getSubscriptionId()),
2701 subInfo.getDisplayName()));
2702 } else {
2703 mTabHost.addTab(buildTabSpec(mMobileTagMap.get(subInfo.getSubscriptionId()),
2704 R.string.data_usage_tab_mobile));
2705 }
PauloftheWest50e6eca2014-10-03 11:07:14 -07002706 }
Wink Savillefcec91f2014-12-02 17:12:08 -08002707 } else {
2708 if (LOGD) Log.d(TAG, "addMobileTab: subInfoList is null");
PauloftheWest50e6eca2014-10-03 11:07:14 -07002709 }
2710 }
2711
Wink Savilleca756612014-11-08 10:47:12 -08002712 private SubscriptionInfo getCurrentTabSubInfo(Context context) {
PauloftheWestde338442014-10-29 14:46:44 -07002713 if (mSubInfoList != null && mTabHost != null) {
2714 final int currentTagIndex = mTabHost.getCurrentTab();
2715 int i = 0;
Wink Savilleca756612014-11-08 10:47:12 -08002716 for (SubscriptionInfo subInfo : mSubInfoList) {
Stuart Scott3ada2ec2014-10-31 14:11:24 -07002717 if (hasReadyMobileRadio(context, subInfo.getSubscriptionId())) {
PauloftheWestde338442014-10-29 14:46:44 -07002718 if (i++ == currentTagIndex) {
2719 return subInfo;
2720 }
2721 }
2722 }
2723 }
2724 return null;
2725 }
2726
PauloftheWest50e6eca2014-10-03 11:07:14 -07002727 /**
2728 * Init a map with subId key and mobile tag name
2729 * @param subInfoList The subscription Info List
2730 * @return The map or null if no activated subscription
2731 */
Wink Savilleca756612014-11-08 10:47:12 -08002732 private Map<Integer, String> initMobileTabTag(List<SubscriptionInfo> subInfoList) {
Wink Savilleb003a852014-10-23 10:16:26 -07002733 Map<Integer, String> map = null;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002734 if (subInfoList != null) {
2735 String mobileTag;
Wink Savilleb003a852014-10-23 10:16:26 -07002736 map = new HashMap<Integer, String>();
Wink Savillefcec91f2014-12-02 17:12:08 -08002737 for (SubscriptionInfo subInfo : subInfoList) {
2738 mobileTag = TAB_MOBILE + String.valueOf(subInfo.getSubscriptionId());
2739 map.put(subInfo.getSubscriptionId(), mobileTag);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002740 }
2741 }
2742 return map;
2743 }
2744
2745 private static boolean isMobileTab(String currentTab) {
2746 return currentTab != null ? currentTab.contains(TAB_MOBILE) : false;
2747 }
2748
Wink Savilleb003a852014-10-23 10:16:26 -07002749 private int getSubId(String currentTab) {
Wink Saville0183fb52014-11-22 10:11:39 -08002750 if (mMobileTagMap != null) {
2751 Set<Integer> set = mMobileTagMap.keySet();
2752 for (Integer subId : set) {
2753 if (mMobileTagMap.get(subId).equals(currentTab)) {
2754 return subId;
2755 }
PauloftheWest50e6eca2014-10-03 11:07:14 -07002756 }
2757 }
2758 Log.e(TAG, "currentTab = " + currentTab + " non mobile tab called this function");
2759 return -1;
2760 }
2761
Stuart Scottf51d0062015-04-08 09:40:48 -07002762 private boolean isMobileDataAvailable(int subId) {
2763 return mSubscriptionManager.getActiveSubscriptionInfo(subId) != null;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002764 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002765}