blob: 387331b44e25709ee8762b225d4f309a60ac8eae [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;
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -0700132import com.android.settings.search.BaseSearchIndexProvider;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -0700133import com.android.settings.search.Indexable;
134import com.android.settings.search.SearchIndexableRaw;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700135import com.android.settings.widget.ChartDataUsageView;
136import com.android.settings.widget.ChartDataUsageView.DataUsageChartListener;
PauloftheWestc80b7612014-11-25 04:40:45 -0800137import com.android.settings.widget.ChartNetworkSeriesView;
Wei Liude557d42015-07-13 12:01:42 -0700138import com.android.settingslib.AppItem;
Wei Liue0021c42015-07-07 15:37:11 -0700139import com.android.settingslib.NetworkPolicyEditor;
Wei Liude557d42015-07-13 12:01:42 -0700140import com.android.settingslib.net.ChartData;
141import com.android.settingslib.net.ChartDataLoader;
Wei Liu8dffd152015-08-20 22:33:46 -0700142import com.android.settingslib.net.SummaryForAllUidLoader;
Wei Liu6e58d512015-08-24 17:29:04 -0700143import com.android.settingslib.net.UidDetail;
144import com.android.settingslib.net.UidDetailProvider;
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;
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700203
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700204 private INetworkStatsSession mStatsSession;
205
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700206 private static final String PREF_FILE = "data_usage";
207 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700208 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700209
210 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700211
Jeff Sharkey8a503642011-06-10 13:31:21 -0700212 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700213 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700214 private TabWidget mTabWidget;
215 private ListView mListView;
PauloftheWestc80b7612014-11-25 04:40:45 -0800216 private ChartNetworkSeriesView mSeries;
217 private ChartNetworkSeriesView mDetailedSeries;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700218 private DataUsageAdapter mAdapter;
219
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700220 /** Distance to inset content from sides, when needed. */
221 private int mInsetSide = 0;
222
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700223 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700224
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700225 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700226 private LinearLayout mNetworkSwitches;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700227 private boolean mDataEnabledSupported;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700228 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700229 private View mDataEnabledView;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700230 private boolean mDisableAtLimitSupported;
231 private Switch mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700232 private View mDisableAtLimitView;
233
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700234 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700235 private Spinner mCycleSpinner;
236 private CycleAdapter mCycleAdapter;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700237 private TextView mCycleSummary;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700238
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700239 private ChartDataUsageView mChart;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700240 private View mDisclaimer;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700241 private TextView mEmpty;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700242 private View mStupidPadding;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700243
244 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700245 private ImageView mAppIcon;
246 private ViewGroup mAppTitles;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700247 private TextView mAppTotal;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700248 private TextView mAppForeground;
249 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700250 private Button mAppSettings;
251
252 private LinearLayout mAppSwitches;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700253 private Switch mAppRestrict;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700254 private View mAppRestrictView;
255
Jeff Sharkey8a503642011-06-10 13:31:21 -0700256 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700257 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700258
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700259 private NetworkTemplate mTemplate;
260 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700261
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700262 private AppItem mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700263
264 private Intent mAppSettingsIntent;
265
Jeff Sharkeya662e492011-06-18 21:57:06 -0700266 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700267
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700268 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700269 private String mIntentTab = null;
270
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700271 private MenuItem mMenuRestrictBackground;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700272 private MenuItem mMenuShowWifi;
273 private MenuItem mMenuShowEthernet;
274 private MenuItem mMenuSimCards;
275 private MenuItem mMenuCellularNetworks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700276
Wink Savilleca756612014-11-08 10:47:12 -0800277 private List<SubscriptionInfo> mSubInfoList;
Wink Savilleb003a852014-10-23 10:16:26 -0700278 private Map<Integer,String> mMobileTagMap;
PauloftheWest50e6eca2014-10-03 11:07:14 -0700279
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700280 /** Flag used to ignore listeners during binding. */
281 private boolean mBinding;
282
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700283 private UidDetailProvider mUidDetailProvider;
284
Jason Monk4bb075b2015-03-17 10:06:58 -0400285 // Indicates request to show app immediately rather than list.
286 private String mShowAppImmediatePkg;
287
Sanket Padawed819270f2015-01-14 11:03:33 -0800288 /**
289 * Local cache of data enabled for subId, used to work around delays.
290 */
291 private final Map<String, Boolean> mMobileDataEnabled = new HashMap<String, Boolean>();
292
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700293 @Override
Chris Wren8a963ba2015-03-20 10:29:14 -0400294 protected int getMetricsCategory() {
295 return MetricsLogger.DATA_USAGE_SUMMARY;
296 }
297
298 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700299 public void onCreate(Bundle savedInstanceState) {
300 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700301 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700302
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700303 mNetworkService = INetworkManagementService.Stub.asInterface(
304 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700305 mStatsService = INetworkStatsService.Stub.asInterface(
306 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700307 mPolicyManager = NetworkPolicyManager.from(context);
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700308 mTelephonyManager = TelephonyManager.from(context);
Wink Saville0183fb52014-11-22 10:11:39 -0800309 mSubscriptionManager = SubscriptionManager.from(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700310
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700311 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700312
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700313 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700314 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700315
Wink Saville0183fb52014-11-22 10:11:39 -0800316 mSubInfoList = mSubscriptionManager.getActiveSubscriptionInfoList();
PauloftheWest50e6eca2014-10-03 11:07:14 -0700317 mMobileTagMap = initMobileTabTag(mSubInfoList);
318
Jaewan Kimffce9c12013-03-19 16:53:45 +0900319 try {
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700320 if (!mNetworkService.isBandwidthControlEnabled()) {
321 Log.w(TAG, "No bandwidth control; leaving");
322 getActivity().finish();
323 }
324 } catch (RemoteException e) {
325 Log.w(TAG, "No bandwidth control; leaving");
326 getActivity().finish();
327 }
328
329 try {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900330 mStatsSession = mStatsService.openSession();
331 } catch (RemoteException e) {
332 throw new RuntimeException(e);
333 }
334
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700335 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700336 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700337
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700338 // override preferences when no mobile radio
339 if (!hasReadyMobileRadio(context)) {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900340 mShowWifi = true;
341 mShowEthernet = true;
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700342 }
343
Jason Monk4bb075b2015-03-17 10:06:58 -0400344 mUidDetailProvider = new UidDetailProvider(context);
345
346 Bundle arguments = getArguments();
347 if (arguments != null) {
348 mShowAppImmediatePkg = arguments.getString(EXTRA_SHOW_APP_IMMEDIATE_PKG);
349 }
350
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700351 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700352 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700353
Jeff Sharkey8a503642011-06-10 13:31:21 -0700354 @Override
355 public View onCreateView(LayoutInflater inflater, ViewGroup container,
356 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700357
Jeff Sharkey8a503642011-06-10 13:31:21 -0700358 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700359 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
360
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700361
Jeff Sharkey8a503642011-06-10 13:31:21 -0700362 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700363 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700364 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
365 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700366
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700367 // decide if we need to manually inset our content, or if we should rely
368 // on parent container for inset.
369 final boolean shouldInset = mListView.getScrollBarStyle()
370 == View.SCROLLBARS_OUTSIDE_OVERLAY;
Amith Yamasani56f51a82013-08-05 10:07:23 -0700371 mInsetSide = 0;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700372
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700373 // adjust padding around tabwidget as needed
Amith Yamasani56f51a82013-08-05 10:07:23 -0700374 prepareCustomPreferencesList(container, view, mListView, false);
Jeff Sharkey5d706792011-09-08 18:57:17 -0700375
Jeff Sharkey8a503642011-06-10 13:31:21 -0700376 mTabHost.setup();
377 mTabHost.setOnTabChangedListener(mTabListener);
378
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700379 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700380 mHeader.setClickable(true);
381
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700382 mListView.addHeaderView(new View(context), null, true);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700383 mListView.addHeaderView(mHeader, null, true);
384 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700385
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700386 if (mInsetSide > 0) {
387 // inset selector and divider drawables
388 insetListViewDrawables(mListView, mInsetSide);
Fabrice Di Megliob27223f2013-01-15 18:54:11 -0800389 mHeader.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700390 }
391
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700392 {
393 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700394 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
395 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700396 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700397
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700398 mDataEnabled = new Switch(inflater.getContext());
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700399 mDataEnabled.setClickable(false);
400 mDataEnabled.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700401 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700402 mDataEnabledView.setTag(R.id.preference_highlight_key,
403 DATA_USAGE_ENABLE_MOBILE_KEY);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700404 mDataEnabledView.setClickable(true);
405 mDataEnabledView.setFocusable(true);
406 mDataEnabledView.setOnClickListener(mDataEnabledListener);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700407 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700408
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700409 mDisableAtLimit = new Switch(inflater.getContext());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700410 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700411 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700412 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700413 mDisableAtLimitView.setTag(R.id.preference_highlight_key,
414 DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700415 mDisableAtLimitView.setClickable(true);
416 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700417 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
418 mNetworkSwitches.addView(mDisableAtLimitView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700419
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700420 mCycleView = inflater.inflate(R.layout.data_usage_cycles, mNetworkSwitches, false);
421 mCycleView.setTag(R.id.preference_highlight_key, DATA_USAGE_CYCLE_KEY);
422 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
423 mCycleAdapter = new CycleAdapter(context);
424 mCycleSpinner.setAdapter(mCycleAdapter);
425 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
426 mCycleSummary = (TextView) mCycleView.findViewById(R.id.cycle_summary);
427 mNetworkSwitches.addView(mCycleView);
PauloftheWestc80b7612014-11-25 04:40:45 -0800428 mSeries = (ChartNetworkSeriesView)view.findViewById(R.id.series);
429 mDetailedSeries = (ChartNetworkSeriesView)view.findViewById(R.id.detail_series);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700430 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700431
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700432 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700433 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800434 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700435
436 {
437 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700438 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700439 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
440 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700441 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
442 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700443 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700444
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700445 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700446
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700447 mAppRestrict = new Switch(inflater.getContext());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700448 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700449 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700450 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700451 mAppRestrictView.setClickable(true);
452 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700453 mAppRestrictView.setOnClickListener(mAppRestrictListener);
454 mAppSwitches.addView(mAppRestrictView);
455 }
456
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700457 mDisclaimer = mHeader.findViewById(R.id.disclaimer);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700458 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700459 mStupidPadding = mHeader.findViewById(R.id.stupid_padding);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700460
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +0100461 final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
462 mAdapter = new DataUsageAdapter(um, mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700463 mListView.setOnItemClickListener(mListListener);
464 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700465
Jason Monk20f464e2015-05-06 16:00:25 -0400466 showRequestedAppIfNeeded(view);
Jason Monk4bb075b2015-03-17 10:06:58 -0400467
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700468 return view;
469 }
470
Jason Monk20f464e2015-05-06 16:00:25 -0400471 private void showRequestedAppIfNeeded(View rootView) {
Jason Monk4bb075b2015-03-17 10:06:58 -0400472 if (mShowAppImmediatePkg == null) {
473 return;
474 }
475 try {
476 int uid = getActivity().getPackageManager().getPackageUid(mShowAppImmediatePkg,
477 UserHandle.myUserId());
478 AppItem app = new AppItem(uid);
479 app.addUid(uid);
480
481 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
482 // When we are going straight to an app then we are coming from App Info and want
483 // a header at the top.
Jason Monk20f464e2015-05-06 16:00:25 -0400484 FrameLayout pinnedHeader = (FrameLayout) rootView.findViewById(R.id.pinned_header);
485 AppHeader.createAppHeader(getActivity(), detail.icon, detail.label, null, pinnedHeader);
Julia Reynolds1740ce42015-07-27 16:00:06 -0400486 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label, true);
Jason Monk4bb075b2015-03-17 10:06:58 -0400487 } catch (NameNotFoundException e) {
488 Log.w(TAG, "Could not find " + mShowAppImmediatePkg, e);
489 Toast.makeText(getActivity(), getString(R.string.unknown_app), Toast.LENGTH_LONG)
490 .show();
491 getActivity().finish();
492 }
493 }
494
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700495 @Override
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700496 public void onViewStateRestored(Bundle savedInstanceState) {
497 super.onViewStateRestored(savedInstanceState);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700498
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700499 // pick default tab based on incoming intent
500 final Intent intent = getActivity().getIntent();
501 mIntentTab = computeTabFromIntent(intent);
502
Jeff Sharkey8a503642011-06-10 13:31:21 -0700503 // this kicks off chain reaction which creates tabs, binds the body to
504 // selected network, and binds chart, cycles and detail list.
505 updateTabs();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700506 }
507
508 @Override
509 public void onResume() {
510 super.onResume();
511
512 getView().post(new Runnable() {
513 @Override
514 public void run() {
515 highlightViewIfNeeded();
516 }
517 });
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700518
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700519 // kick off background task to update stats
520 new AsyncTask<Void, Void, Void>() {
521 @Override
522 protected Void doInBackground(Void... params) {
523 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700524 // wait a few seconds before kicking off
525 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700526 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700527 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700528 } catch (RemoteException e) {
529 }
530 return null;
531 }
532
533 @Override
534 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700535 if (isAdded()) {
536 updateBody();
537 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700538 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700539 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700540 }
541
Jeff Sharkey8a503642011-06-10 13:31:21 -0700542 @Override
543 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
544 inflater.inflate(R.menu.data_usage, menu);
545 }
546
547 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700548 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700549 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700550 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700551 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700552
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700553 mMenuShowWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700554 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
555 mMenuShowWifi.setVisible(!appDetailMode);
556 } else {
557 mMenuShowWifi.setVisible(false);
558 }
559
560 mMenuShowEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
561 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
562 mMenuShowEthernet.setVisible(!appDetailMode);
563 } else {
564 mMenuShowEthernet.setVisible(false);
565 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700566
567 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700568 mMenuRestrictBackground.setVisible(
569 hasReadyMobileRadio(context) && isOwner && !appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700570
571 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
572 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700573 metered.setVisible(!appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700574 } else {
575 metered.setVisible(false);
576 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700577
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700578 // TODO: show when multiple sims available
579 mMenuSimCards = menu.findItem(R.id.data_usage_menu_sim_cards);
580 mMenuSimCards.setVisible(false);
581
582 mMenuCellularNetworks = menu.findItem(R.id.data_usage_menu_cellular_networks);
PauloftheWesta4b8fb32014-09-18 10:12:25 -0700583 mMenuCellularNetworks.setVisible(hasReadyMobileRadio(context)
584 && !appDetailMode && isOwner);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700585
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700586 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
587 String helpUrl;
588 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
Jason Monk6e613472015-06-16 10:31:38 -0400589 HelpUtils.prepareHelpMenuItem(getActivity(), help, helpUrl, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700590 } else {
591 help.setVisible(false);
592 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700593
594 updateMenuTitles();
595 }
596
597 private void updateMenuTitles() {
598 if (mPolicyManager.getRestrictBackground()) {
599 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_allow_background);
600 } else {
601 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_restrict_background);
602 }
603
604 if (mShowWifi) {
605 mMenuShowWifi.setTitle(R.string.data_usage_menu_hide_wifi);
606 } else {
607 mMenuShowWifi.setTitle(R.string.data_usage_menu_show_wifi);
608 }
609
610 if (mShowEthernet) {
611 mMenuShowEthernet.setTitle(R.string.data_usage_menu_hide_ethernet);
612 } else {
613 mMenuShowEthernet.setTitle(R.string.data_usage_menu_show_ethernet);
614 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700615 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700616
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700617 @Override
618 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700619 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700620 case R.id.data_usage_menu_restrict_background: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700621 final boolean restrictBackground = !mPolicyManager.getRestrictBackground();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700622 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800623 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700624 } else {
625 // no confirmation to drop restriction
626 setRestrictBackground(false);
627 }
628 return true;
629 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700630 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700631 mShowWifi = !mShowWifi;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700632 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700633 updateMenuTitles();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700634 updateTabs();
635 return true;
636 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700637 case R.id.data_usage_menu_show_ethernet: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700638 mShowEthernet = !mShowEthernet;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700639 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700640 updateMenuTitles();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700641 updateTabs();
642 return true;
643 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700644 case R.id.data_usage_menu_sim_cards: {
645 // TODO: hook up to sim cards
646 return true;
647 }
648 case R.id.data_usage_menu_cellular_networks: {
649 final Intent intent = new Intent(Intent.ACTION_MAIN);
650 intent.setComponent(new ComponentName("com.android.phone",
651 "com.android.phone.MobileNetworkSettings"));
652 startActivity(intent);
653 return true;
654 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700655 case R.id.data_usage_menu_metered: {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800656 final SettingsActivity sa = (SettingsActivity) getActivity();
657 sa.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700658 R.string.data_usage_metered_title, null, this, 0);
659 return true;
660 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700661 }
662 return false;
663 }
664
Jeff Sharkey94a90952011-06-13 22:31:09 -0700665 @Override
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700666 public void onDestroy() {
Jeff Sharkey94a90952011-06-13 22:31:09 -0700667 mDataEnabledView = null;
668 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700669
670 mUidDetailProvider.clearCache();
671 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700672
673 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700674
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800675 super.onDestroy();
676 }
677
Jeff Sharkey8a503642011-06-10 13:31:21 -0700678 /**
Jeff Sharkey92811822012-05-04 11:47:29 -0700679 * Build and assign {@link LayoutTransition} to various containers. Should
680 * only be assigned after initial layout is complete.
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700681 */
Jeff Sharkey92811822012-05-04 11:47:29 -0700682 private void ensureLayoutTransitions() {
Jason Monk4bb075b2015-03-17 10:06:58 -0400683 if (mShowAppImmediatePkg != null) {
684 // If we are skipping right to showing an app, we don't care about transitions.
685 return;
686 }
Jeff Sharkey92811822012-05-04 11:47:29 -0700687 // skip when already setup
688 if (mChart.getLayoutTransition() != null) return;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700689
Jeff Sharkey92811822012-05-04 11:47:29 -0700690 mTabsContainer.setLayoutTransition(buildLayoutTransition());
691 mHeader.setLayoutTransition(buildLayoutTransition());
692 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700693
Jeff Sharkey92811822012-05-04 11:47:29 -0700694 final LayoutTransition chartTransition = buildLayoutTransition();
695 chartTransition.disableTransitionType(LayoutTransition.APPEARING);
696 chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
697 mChart.setLayoutTransition(chartTransition);
698 }
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700699
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700700 private static LayoutTransition buildLayoutTransition() {
701 final LayoutTransition transition = new LayoutTransition();
702 if (TEST_ANIM) {
703 transition.setDuration(1500);
704 }
705 transition.setAnimateParentHierarchy(false);
706 return transition;
707 }
708
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700709 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700710 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700711 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
712 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700713 */
714 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700715 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700716 mTabHost.clearAllTabs();
717
Sanket Padawe7e21fa52015-02-12 12:39:34 -0800718 int simCount = mTelephonyManager.getSimCount();
719
Stuart Scottf51d0062015-04-08 09:40:48 -0700720 List<SubscriptionInfo> sirs = mSubscriptionManager.getActiveSubscriptionInfoList();
721 if (sirs != null) {
722 for (SubscriptionInfo sir : sirs) {
Sanket Padawe7e21fa52015-02-12 12:39:34 -0800723 addMobileTab(context, sir, (simCount > 1));
PauloftheWestc80b7612014-11-25 04:40:45 -0800724 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700725 }
PauloftheWestc80b7612014-11-25 04:40:45 -0800726
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700727 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700728 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
729 }
PauloftheWestc80b7612014-11-25 04:40:45 -0800730
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700731 if (mShowEthernet && hasEthernet(context)) {
732 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
733 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700734
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700735 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700736 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
737 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
738 if (mIntentTab != null) {
739 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700740 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700741 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700742 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700743 mTabHost.setCurrentTabByTag(mIntentTab);
744 }
745 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700746 } else if (noTabs) {
747 // no usable tabs, so hide body
748 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700749 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700750 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700751 }
752 }
753
Jeff Sharkey8a503642011-06-10 13:31:21 -0700754 /**
755 * Factory that provide empty {@link View} to make {@link TabHost} happy.
756 */
757 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700758 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700759 public View createTabContent(String tag) {
760 return new View(mTabHost.getContext());
761 }
762 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700763
Jeff Sharkey8a503642011-06-10 13:31:21 -0700764 /**
765 * Build {@link TabSpec} with thin indicator, and empty content.
766 */
767 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700768 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
769 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700770 }
771
PauloftheWest50e6eca2014-10-03 11:07:14 -0700772 /**
773 * Build {@link TabSpec} with thin indicator, and empty content.
774 */
Stuart Scottb1531812014-11-04 14:52:23 -0800775 private TabSpec buildTabSpec(String tag, CharSequence title) {
PauloftheWest50e6eca2014-10-03 11:07:14 -0700776 return mTabHost.newTabSpec(tag).setIndicator(title).setContent(
777 mEmptyTabContent);
778 }
779
780
Jeff Sharkey8a503642011-06-10 13:31:21 -0700781 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700782 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700783 public void onTabChanged(String tabId) {
784 // user changed tab; update body
785 updateBody();
786 }
787 };
788
789 /**
790 * Update body content based on current tab. Loads
791 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
792 * binds them to visible controls.
793 */
794 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700795 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700796 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700797
Jeff Sharkeya662e492011-06-18 21:57:06 -0700798 final Context context = getActivity();
PauloftheWestc80b7612014-11-25 04:40:45 -0800799 final Resources resources = context.getResources();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700800 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700801 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700802
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700803 if (currentTab == null) {
804 Log.w(TAG, "no tab selected; hiding body");
805 mListView.setVisibility(View.GONE);
806 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700807 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700808 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700809 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700810
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700811 mCurrentTab = currentTab;
812
Jeff Sharkey8a503642011-06-10 13:31:21 -0700813 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
814
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700815 mDataEnabledSupported = isOwner;
816 mDisableAtLimitSupported = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700817
Wink Savillefcec91f2014-12-02 17:12:08 -0800818 // TODO: remove mobile tabs when SIM isn't ready probably by
819 // TODO: using SubscriptionManager.getActiveSubscriptionInfoList.
820 if (LOGD) Log.d(TAG, "updateBody() isMobileTab=" + isMobileTab(currentTab));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700821
PauloftheWest50e6eca2014-10-03 11:07:14 -0700822 if (isMobileTab(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800823 if (LOGD) Log.d(TAG, "updateBody() mobile tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700824 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
825 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
PauloftheWest50e6eca2014-10-03 11:07:14 -0700826 mDataEnabledSupported = isMobileDataAvailable(getSubId(currentTab));
Jeff Sharkey4e2d16c2014-12-03 13:44:26 -0800827
828 // Match mobile traffic for this subscriber, but normalize it to
829 // catch any other merged subscribers.
830 mTemplate = buildTemplateMobileAll(
831 getActiveSubscriberId(context, getSubId(currentTab)));
832 mTemplate = NetworkTemplate.normalize(mTemplate,
833 mTelephonyManager.getMergedSubscriberIds());
834
Jeff Sharkey8a503642011-06-10 13:31:21 -0700835 } else if (TAB_3G.equals(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800836 if (LOGD) Log.d(TAG, "updateBody() 3g tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700837 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
838 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
839 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700840 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700841
842 } else if (TAB_4G.equals(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800843 if (LOGD) Log.d(TAG, "updateBody() 4g tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700844 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
845 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
846 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700847 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700848
849 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700850 // wifi doesn't have any controls
Wink Savillefcec91f2014-12-02 17:12:08 -0800851 if (LOGD) Log.d(TAG, "updateBody() wifi tab");
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700852 mDataEnabledSupported = false;
853 mDisableAtLimitSupported = false;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700854 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700855
856 } else if (TAB_ETHERNET.equals(currentTab)) {
857 // ethernet doesn't have any controls
Wink Savillefcec91f2014-12-02 17:12:08 -0800858 if (LOGD) Log.d(TAG, "updateBody() ethernet tab");
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700859 mDataEnabledSupported = false;
860 mDisableAtLimitSupported = false;
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700861 mTemplate = buildTemplateEthernet();
862
863 } else {
Wink Savillefcec91f2014-12-02 17:12:08 -0800864 if (LOGD) Log.d(TAG, "updateBody() unknown tab");
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700865 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700866 }
867
Sanket Padawe716b1f82015-06-24 14:26:21 -0700868 mPolicyEditor.read();
869 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
870 if (policy != null) {
871 final long currentTime = System.currentTimeMillis();
872 final long start = computeLastCycleBoundary(currentTime, policy);
873 final long end = currentTime;
874 long totalBytes = 0;
875
876 try {
877 totalBytes = mStatsService.getNetworkTotalBytes(policy.template, start, end);
878 } catch (RuntimeException e) {
879 } catch (RemoteException e) {
880 }
881
882 if (policy.isOverLimit(totalBytes) && policy.lastLimitSnooze < start) {
883 setPreferenceSummary(mDataEnabledView,
884 getString(R.string.data_usage_cellular_data_summary));
885 } else {
886 final TextView summary = (TextView) mDataEnabledView
887 .findViewById(android.R.id.summary);
888 summary.setVisibility(View.GONE);
889 }
890 }
891
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700892 // kick off loader for network history
893 // TODO: consider chaining two loaders together instead of reloading
894 // network history when showing app detail.
895 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700896 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700897
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700898 // detail mode can change visible menus, invalidate
899 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700900
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700901 mBinding = false;
PauloftheWestc80b7612014-11-25 04:40:45 -0800902
Alan Viverette55eaa962015-03-18 18:10:52 -0700903 int seriesColor = context.getColor(R.color.sim_noitification);
PauloftheWestc80b7612014-11-25 04:40:45 -0800904 if (mCurrentTab != null && mCurrentTab.length() > TAB_MOBILE.length() ){
905 final int slotId = Integer.parseInt(mCurrentTab.substring(TAB_MOBILE.length(),
906 mCurrentTab.length()));
Stuart Scottf51d0062015-04-08 09:40:48 -0700907 final SubscriptionInfo sir = mSubscriptionManager
908 .getActiveSubscriptionInfoForSimSlotIndex(slotId);
PauloftheWestc80b7612014-11-25 04:40:45 -0800909
910 if (sir != null) {
911 seriesColor = sir.getIconTint();
912 }
913 }
914
Jeff Sharkey9c167e92015-01-26 15:15:59 -0700915 final int secondaryColor = Color.argb(127, Color.red(seriesColor), Color.green(seriesColor),
916 Color.blue(seriesColor));
917 mSeries.setChartColor(Color.BLACK, seriesColor, secondaryColor);
918 mDetailedSeries.setChartColor(Color.BLACK, seriesColor, secondaryColor);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700919 }
920
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700921 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700922 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700923 }
924
925 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700926 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700927 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700928 */
929 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700930 final Context context = getActivity();
931 final PackageManager pm = context.getPackageManager();
932 final LayoutInflater inflater = getActivity().getLayoutInflater();
933
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700934 if (isAppDetailMode()) {
935 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700936 mCycleAdapter.setChangeVisible(false);
Julia Reynolds1740ce42015-07-27 16:00:06 -0400937 mChart.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700938 } else {
939 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700940 mCycleAdapter.setChangeVisible(true);
Julia Reynolds1740ce42015-07-27 16:00:06 -0400941 mChart.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700942
943 // hide detail stats when not in detail mode
944 mChart.bindDetailNetworkStats(null);
945 return;
946 }
947
948 // remove warning/limit sweeps while in detail mode
949 mChart.bindNetworkPolicy(null);
950
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700951 // show icon and all labels appearing under this app
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700952 final int uid = mCurrentApp.key;
953 final UidDetail detail = mUidDetailProvider.getUidDetail(uid, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700954 mAppIcon.setImageDrawable(detail.icon);
955
956 mAppTitles.removeAllViews();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700957
958 View title = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700959 if (detail.detailLabels != null) {
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100960 final int n = detail.detailLabels.length;
961 for (int i = 0; i < n; ++i) {
962 CharSequence label = detail.detailLabels[i];
963 CharSequence contentDescription = detail.detailContentDescriptions[i];
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700964 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100965 TextView appTitle = (TextView) title.findViewById(R.id.app_title);
966 appTitle.setText(label);
967 appTitle.setContentDescription(contentDescription);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700968 mAppTitles.addView(title);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700969 }
970 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700971 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100972 TextView appTitle = (TextView) title.findViewById(R.id.app_title);
973 appTitle.setText(detail.label);
974 appTitle.setContentDescription(detail.contentDescription);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700975 mAppTitles.addView(title);
976 }
977
978 // Remember last slot for summary
979 if (title != null) {
980 mAppTotal = (TextView) title.findViewById(R.id.app_summary);
981 } else {
982 mAppTotal = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700983 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700984
985 // enable settings button when package provides it
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700986 final String[] packageNames = pm.getPackagesForUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700987 if (packageNames != null && packageNames.length > 0) {
988 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700989 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
990
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700991 // Search for match across all packages
992 boolean matchFound = false;
993 for (String packageName : packageNames) {
994 mAppSettingsIntent.setPackage(packageName);
995 if (pm.resolveActivity(mAppSettingsIntent, 0) != null) {
996 matchFound = true;
997 break;
998 }
999 }
1000
Zoltan Szatmary-Banf20d39e2014-08-07 15:27:08 +01001001 mAppSettings.setOnClickListener(new OnClickListener() {
1002 @Override
1003 public void onClick(View v) {
1004 if (!isAdded()) {
1005 return;
1006 }
1007
1008 // TODO: target towards entire UID instead of just first package
1009 getActivity().startActivityAsUser(mAppSettingsIntent,
1010 new UserHandle(UserHandle.getUserId(uid)));
1011 }
1012 });
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001013 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -07001014 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001015
1016 } else {
1017 mAppSettingsIntent = null;
Zoltan Szatmary-Banf20d39e2014-08-07 15:27:08 +01001018 mAppSettings.setOnClickListener(null);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001019 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001020 }
1021
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001022 updateDetailData();
1023
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001024 if (UserHandle.isApp(uid) && !mPolicyManager.getRestrictBackground()
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001025 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001026 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -08001027 setPreferenceSummary(mAppRestrictView,
1028 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001029
1030 mAppRestrictView.setVisibility(View.VISIBLE);
1031 mAppRestrict.setChecked(getAppRestrictBackground());
1032
1033 } else {
1034 mAppRestrictView.setVisibility(View.GONE);
1035 }
1036 }
1037
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001038 private void setPolicyWarningBytes(long warningBytes) {
1039 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -07001040 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001041 updatePolicy(false);
1042 }
1043
1044 private void setPolicyLimitBytes(long limitBytes) {
1045 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -07001046 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001047 updatePolicy(false);
1048 }
1049
Wink Savilleb003a852014-10-23 10:16:26 -07001050 private boolean isMobileDataEnabled(int subId) {
Wink Savillea4f4d182014-12-05 15:34:46 -08001051 if (LOGD) Log.d(TAG, "isMobileDataEnabled:+ subId=" + subId);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001052 boolean isEnable = false;
Sanket Padawed819270f2015-01-14 11:03:33 -08001053 if (mMobileDataEnabled.get(String.valueOf(subId)) != null) {
1054 //TODO: deprecate and remove this once enabled flag is on policy
1055 //Multiple Subscriptions, the value need to be reseted
1056 isEnable = mMobileDataEnabled.get(String.valueOf(subId)).booleanValue();
Wink Savillea4f4d182014-12-05 15:34:46 -08001057 if (LOGD) {
1058 Log.d(TAG, "isMobileDataEnabled: != null, subId=" + subId
1059 + " isEnable=" + isEnable);
1060 }
Sanket Padawed819270f2015-01-14 11:03:33 -08001061 mMobileDataEnabled.put(String.valueOf(subId), null);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001062 } else {
Wink Savillea4f4d182014-12-05 15:34:46 -08001063 // SUB SELECT
1064 isEnable = mTelephonyManager.getDataEnabled(subId);
1065 if (LOGD) {
1066 Log.d(TAG, "isMobileDataEnabled: == null, subId=" + subId
1067 + " isEnable=" + isEnable);
1068 }
Jeff Sharkey28130d92011-09-02 16:10:24 -07001069 }
PauloftheWest50e6eca2014-10-03 11:07:14 -07001070 return isEnable;
Jeff Sharkey28130d92011-09-02 16:10:24 -07001071 }
1072
Sanket Padawe24f834d2015-01-08 11:23:11 -08001073 private void setMobileDataEnabled(int subId, boolean enabled) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001074 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
Sanket Padawe24f834d2015-01-08 11:23:11 -08001075 mTelephonyManager.setDataEnabled(subId, enabled);
Sanket Padawed819270f2015-01-14 11:03:33 -08001076 mMobileDataEnabled.put(String.valueOf(subId), enabled);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001077 updatePolicy(false);
1078 }
1079
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001080 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001081 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked()
1082 && ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001083 }
1084
1085 private boolean isBandwidthControlEnabled() {
1086 try {
1087 return mNetworkService.isBandwidthControlEnabled();
1088 } catch (RemoteException e) {
1089 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
1090 return false;
1091 }
1092 }
1093
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001094 public void setRestrictBackground(boolean restrictBackground) {
1095 mPolicyManager.setRestrictBackground(restrictBackground);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001096 updateMenuTitles();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001097 }
1098
1099 private boolean getAppRestrictBackground() {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001100 final int uid = mCurrentApp.key;
1101 final int uidPolicy = mPolicyManager.getUidPolicy(uid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001102 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
1103 }
1104
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001105 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001106 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001107 final int uid = mCurrentApp.key;
1108 mPolicyManager.setUidPolicy(
1109 uid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001110 mAppRestrict.setChecked(restrictBackground);
1111 }
1112
Jeff Sharkey8a503642011-06-10 13:31:21 -07001113 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001114 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
1115 * current {@link #mTemplate}.
1116 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001117 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001118 boolean dataEnabledVisible = mDataEnabledSupported;
1119 boolean disableAtLimitVisible = mDisableAtLimitSupported;
1120
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001121 if (isAppDetailMode()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001122 dataEnabledVisible = false;
1123 disableAtLimitVisible = false;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001124 }
1125
Jeff Sharkey28130d92011-09-02 16:10:24 -07001126 // TODO: move enabled state directly into policy
PauloftheWest50e6eca2014-10-03 11:07:14 -07001127 if (isMobileTab(mCurrentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001128 mBinding = true;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001129 mDataEnabled.setChecked(isMobileDataEnabled(getSubId(mCurrentTab)));
Jeff Sharkey28130d92011-09-02 16:10:24 -07001130 mBinding = false;
1131 }
1132
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001133 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001134 //SUB SELECT
1135 if (isNetworkPolicyModifiable(policy) && isMobileDataAvailable(getSubId(mCurrentTab))) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001136 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001137 if (!isAppDetailMode()) {
1138 mChart.bindNetworkPolicy(policy);
1139 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001140
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001141 } else {
1142 // controls are disabled; don't bind warning/limit sweeps
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001143 disableAtLimitVisible = false;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001144 mChart.bindNetworkPolicy(null);
1145 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001146
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001147 mDataEnabledView.setVisibility(dataEnabledVisible ? View.VISIBLE : View.GONE);
1148 mDisableAtLimitView.setVisibility(disableAtLimitVisible ? View.VISIBLE : View.GONE);
1149
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001150 if (refreshCycle) {
1151 // generate cycle list based on policy and available history
1152 updateCycleList(policy);
1153 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001154 }
1155
1156 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001157 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
1158 * and available {@link NetworkStatsHistory} data. Always selects the newest
1159 * item, updating the inspection range on {@link #mChart}.
1160 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001161 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001162 // stash away currently selected cycle to try restoring below
1163 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -07001164 mCycleAdapter.clear();
1165
1166 final Context context = mCycleSpinner.getContext();
1167
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001168 long historyStart = Long.MAX_VALUE;
1169 long historyEnd = Long.MIN_VALUE;
1170 if (mChartData != null) {
1171 historyStart = mChartData.network.getStart();
1172 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001173 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001174
Jeff Sharkey461842a2011-09-25 18:22:48 -07001175 final long now = System.currentTimeMillis();
1176 if (historyStart == Long.MAX_VALUE) historyStart = now;
1177 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001178
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001179 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001180 if (policy != null) {
1181 // find the next cycle boundary
1182 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001183
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001184 // walk backwards, generating all valid cycle ranges
1185 while (cycleEnd > historyStart) {
1186 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
1187 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
1188 + historyStart);
1189 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1190 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001191 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001192 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001193
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001194 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001195 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001196 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001197
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001198 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001199 // no policy defined cycles; show entry for each four-week period
1200 long cycleEnd = historyEnd;
1201 while (cycleEnd > historyStart) {
1202 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
1203 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1204 cycleEnd = cycleStart;
1205 }
1206
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001207 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001208 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001209
1210 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001211 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001212 final int position = mCycleAdapter.findNearestPosition(previousItem);
1213 mCycleSpinner.setSelection(position);
1214
1215 // only force-update cycle when changed; skipping preserves any
1216 // user-defined inspection region.
1217 final CycleItem selectedItem = mCycleAdapter.getItem(position);
1218 if (!Objects.equal(selectedItem, previousItem)) {
1219 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1220 } else {
1221 // but still kick off loader for detailed list
1222 updateDetailData();
1223 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001224 } else {
1225 updateDetailData();
1226 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001227 }
1228
Sanket Padawed819270f2015-01-14 11:03:33 -08001229 private void disableDataForOtherSubscriptions(SubscriptionInfo currentSir) {
1230 if (mSubInfoList != null) {
1231 for (SubscriptionInfo subInfo : mSubInfoList) {
1232 if (subInfo.getSubscriptionId() != currentSir.getSubscriptionId()) {
1233 setMobileDataEnabled(subInfo.getSubscriptionId(), false);
1234 }
1235 }
1236 }
1237 }
1238
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001239 private View.OnClickListener mDataEnabledListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001240 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001241 public void onClick(View v) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001242 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001243
Chris Wren1b6ffba2015-05-08 17:10:01 -04001244 final boolean enabled = !mDataEnabled.isChecked();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001245 final String currentTab = mCurrentTab;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001246 if (isMobileTab(currentTab)) {
Chris Wren1b6ffba2015-05-08 17:10:01 -04001247 MetricsLogger.action(getContext(), MetricsLogger.ACTION_CELL_DATA_TOGGLE, enabled);
1248 if (enabled) {
PauloftheWestde338442014-10-29 14:46:44 -07001249 // If we are showing the Sim Card tile then we are a Multi-Sim device.
1250 if (Utils.showSimCardTile(getActivity())) {
1251 handleMultiSimDataDialog();
1252 } else {
Sanket Padawe24f834d2015-01-08 11:23:11 -08001253 setMobileDataEnabled(getSubId(currentTab), true);
PauloftheWestde338442014-10-29 14:46:44 -07001254 }
Jeff Sharkey28130d92011-09-02 16:10:24 -07001255 } else {
1256 // disabling data; show confirmation dialog which eventually
1257 // calls setMobileDataEnabled() once user confirms.
Sanket Padawe24f834d2015-01-08 11:23:11 -08001258 ConfirmDataDisableFragment.show(DataUsageSummary.this, getSubId(mCurrentTab));
Jeff Sharkey28130d92011-09-02 16:10:24 -07001259 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001260 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001261
Jeff Sharkey28130d92011-09-02 16:10:24 -07001262 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001263 }
1264 };
1265
PauloftheWestde338442014-10-29 14:46:44 -07001266 private void handleMultiSimDataDialog() {
PauloftheWestf0d84e22014-12-10 08:50:54 -08001267 final Context context = getActivity();
1268 final SubscriptionInfo currentSir = getCurrentTabSubInfo(context);
Sanket Padawe766ab602015-01-20 10:36:15 -08001269
1270 //If sim has not loaded after toggling data switch, return.
1271 if (currentSir == null) {
1272 return;
1273 }
1274
Stuart Scottf51d0062015-04-08 09:40:48 -07001275 final SubscriptionInfo nextSir = mSubscriptionManager.getDefaultDataSubscriptionInfo();
PauloftheWestde338442014-10-29 14:46:44 -07001276
PauloftheWestf0d84e22014-12-10 08:50:54 -08001277 // If the device is single SIM or is enabling data on the active data SIM then forgo
1278 // the pop-up.
1279 if (!Utils.showSimCardTile(context) ||
Sanket Padawed819270f2015-01-14 11:03:33 -08001280 (nextSir != null && currentSir != null &&
1281 currentSir.getSubscriptionId() == nextSir.getSubscriptionId())) {
Sanket Padawe24f834d2015-01-08 11:23:11 -08001282 setMobileDataEnabled(currentSir.getSubscriptionId(), true);
Sanket Padawed819270f2015-01-14 11:03:33 -08001283 if (nextSir != null && currentSir != null &&
1284 currentSir.getSubscriptionId() == nextSir.getSubscriptionId()) {
1285 disableDataForOtherSubscriptions(currentSir);
1286 }
PauloftheWestde338442014-10-29 14:46:44 -07001287 updateBody();
1288 return;
1289 }
1290
PauloftheWestf0d84e22014-12-10 08:50:54 -08001291 final String previousName = (nextSir == null)
1292 ? context.getResources().getString(R.string.sim_selection_required_pref)
1293 : nextSir.getDisplayName().toString();
1294
PauloftheWestde338442014-10-29 14:46:44 -07001295 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
1296
1297 builder.setTitle(R.string.sim_change_data_title);
1298 builder.setMessage(getActivity().getResources().getString(R.string.sim_change_data_message,
PauloftheWestf0d84e22014-12-10 08:50:54 -08001299 currentSir.getDisplayName(), previousName));
PauloftheWestde338442014-10-29 14:46:44 -07001300
1301 builder.setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
1302 @Override
1303 public void onClick(DialogInterface dialog, int id) {
Wink Saville0183fb52014-11-22 10:11:39 -08001304 mSubscriptionManager.setDefaultDataSubId(currentSir.getSubscriptionId());
Sanket Padawe24f834d2015-01-08 11:23:11 -08001305 setMobileDataEnabled(currentSir.getSubscriptionId(), true);
Sanket Padawed819270f2015-01-14 11:03:33 -08001306 disableDataForOtherSubscriptions(currentSir);
PauloftheWestde338442014-10-29 14:46:44 -07001307 updateBody();
1308 }
1309 });
1310 builder.setNegativeButton(R.string.cancel, null);
1311
1312 builder.create().show();
1313 }
1314
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001315 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001316 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001317 public void onClick(View v) {
1318 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001319 if (disableAtLimit) {
1320 // enabling limit; show confirmation dialog which eventually
1321 // calls setPolicyLimitBytes() once user confirms.
1322 ConfirmLimitFragment.show(DataUsageSummary.this);
1323 } else {
1324 setPolicyLimitBytes(LIMIT_DISABLED);
1325 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001326 }
1327 };
1328
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001329 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001330 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001331 public void onClick(View v) {
1332 final boolean restrictBackground = !mAppRestrict.isChecked();
1333
1334 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001335 // enabling restriction; show confirmation dialog which
1336 // eventually calls setRestrictBackground() once user
1337 // confirms.
1338 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001339 } else {
1340 setAppRestrictBackground(false);
1341 }
1342 }
1343 };
1344
Jeff Sharkey8a503642011-06-10 13:31:21 -07001345 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001346 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001347 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001348 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001349 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001350
1351 // TODO: sigh, remove this hack once we understand 6450986
1352 if (mUidDetailProvider == null || app == null) return;
1353
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001354 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001355 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001356 }
1357 };
1358
1359 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001360 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001361 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1362 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1363 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001364 // show cycle editor; will eventually call setPolicyCycleDay()
1365 // when user finishes editing.
1366 CycleEditorFragment.show(DataUsageSummary.this);
1367
1368 // reset spinner to something other than "change cycle..."
1369 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001370
1371 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001372 if (LOGD) {
1373 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1374 + cycle.end + "]");
1375 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001376
1377 // update chart to show selected cycle, and update detail data
1378 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001379 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001380
1381 updateDetailData();
1382 }
1383 }
1384
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001385 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001386 public void onNothingSelected(AdapterView<?> parent) {
1387 // ignored
1388 }
1389 };
1390
1391 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001392 * Update details based on {@link #mChart} inspection range depending on
1393 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1394 * of applications data usage, and when {@link #isAppDetailMode()} update
1395 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001396 */
1397 private void updateDetailData() {
1398 if (LOGD) Log.d(TAG, "updateDetailData()");
1399
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001400 final long start = mChart.getInspectStart();
1401 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001402 final long now = System.currentTimeMillis();
1403
1404 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001405
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001406 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001407 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001408 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001409 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001410 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001411 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001412 final long foregroundBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001413 final long totalBytes = defaultBytes + foregroundBytes;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001414
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001415 if (mAppTotal != null) {
1416 mAppTotal.setText(Formatter.formatFileSize(context, totalBytes));
1417 }
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001418 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1419 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1420
1421 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001422 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001423
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001424 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001425
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001426 mCycleSummary.setVisibility(View.GONE);
1427
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001428 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001429 if (mChartData != null) {
1430 entry = mChartData.network.getValues(start, end, now, null);
1431 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001432
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001433 mCycleSummary.setVisibility(View.VISIBLE);
1434
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001435 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001436 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001437 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001438 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001439
1440 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1441 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001442 mCycleSummary.setText(totalPhrase);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001443
PauloftheWest50e6eca2014-10-03 11:07:14 -07001444 if (isMobileTab(mCurrentTab) || TAB_3G.equals(mCurrentTab)
László Dávid0b8bf4e2012-10-24 23:31:47 +02001445 || TAB_4G.equals(mCurrentTab)) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001446 if (isAppDetailMode()) {
1447 mDisclaimer.setVisibility(View.GONE);
1448 } else {
1449 mDisclaimer.setVisibility(View.VISIBLE);
1450 }
Jeff Sharkeye557c332012-04-13 16:04:07 -07001451 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001452 mDisclaimer.setVisibility(View.GONE);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001453 }
1454
Jeff Sharkey92811822012-05-04 11:47:29 -07001455 // initial layout is finished above, ensure we have transitions
1456 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001457 }
1458
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001459 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1460 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001461 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001462 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001463 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001464 }
1465
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001466 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001467 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1468 mChartData = data;
1469 mChart.bindNetworkStats(mChartData.network);
1470 mChart.bindDetailNetworkStats(mChartData.detail);
1471
1472 // calcuate policy cycles based on available data
1473 updatePolicy(true);
1474 updateAppDetail();
1475
1476 // force scroll to top of body when showing detail
1477 if (mChartData.detail != null) {
1478 mListView.smoothScrollToPosition(0);
1479 }
1480 }
1481
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001482 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001483 public void onLoaderReset(Loader<ChartData> loader) {
1484 mChartData = null;
1485 mChart.bindNetworkStats(null);
1486 mChart.bindDetailNetworkStats(null);
1487 }
1488 };
1489
1490 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001491 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001492 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001493 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001494 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001495 }
1496
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001497 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001498 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001499 final int[] restrictedUids = mPolicyManager.getUidsWithPolicy(
Jeff Sharkeye557c332012-04-13 16:04:07 -07001500 POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001501 mAdapter.bindStats(data, restrictedUids);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001502 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001503 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001504
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001505 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001506 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001507 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001508 updateEmptyVisible();
1509 }
1510
1511 private void updateEmptyVisible() {
1512 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1513 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001514 mStupidPadding.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001515 }
1516 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001517
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001518 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001519 final TelephonyManager tele = TelephonyManager.from(context);
1520 final String actualSubscriberId = tele.getSubscriberId();
Wink Savillefcec91f2014-12-02 17:12:08 -08001521 String retVal = SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
1522 if (LOGD) Log.d(TAG, "getActiveSubscriberId=" + retVal + " actualSubscriberId=" + actualSubscriberId);
1523 return retVal;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001524 }
1525
Wink Savilleb003a852014-10-23 10:16:26 -07001526 private static String getActiveSubscriberId(Context context, int subId) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07001527 final TelephonyManager tele = TelephonyManager.from(context);
Wink Savillefcec91f2014-12-02 17:12:08 -08001528 String retVal = tele.getSubscriberId(subId);
1529 if (LOGD) Log.d(TAG, "getActiveSubscriberId=" + retVal + " subId=" + subId);
1530 return retVal;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001531 }
1532
Jeff Sharkey8a503642011-06-10 13:31:21 -07001533 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001534 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001535 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001536 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001537 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001538
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001539 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001540 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001541 setPolicyLimitBytes(mChart.getLimitBytes());
Sanket Padawe716b1f82015-06-24 14:26:21 -07001542 updateBody();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001543 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001544
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001545 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001546 public void requestWarningEdit() {
1547 WarningEditorFragment.show(DataUsageSummary.this);
1548 }
1549
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001550 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001551 public void requestLimitEdit() {
1552 LimitEditorFragment.show(DataUsageSummary.this);
1553 }
1554 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001555
1556 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001557 * List item that reflects a specific data usage cycle.
1558 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001559 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001560 public CharSequence label;
1561 public long start;
1562 public long end;
1563
Jeff Sharkey8a503642011-06-10 13:31:21 -07001564 CycleItem(CharSequence label) {
1565 this.label = label;
1566 }
1567
1568 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001569 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001570 this.start = start;
1571 this.end = end;
1572 }
1573
Jeff Sharkey8a503642011-06-10 13:31:21 -07001574 @Override
1575 public String toString() {
1576 return label.toString();
1577 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001578
1579 @Override
1580 public boolean equals(Object o) {
1581 if (o instanceof CycleItem) {
1582 final CycleItem another = (CycleItem) o;
1583 return start == another.start && end == another.end;
1584 }
1585 return false;
1586 }
1587
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001588 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001589 public int compareTo(CycleItem another) {
1590 return Long.compare(start, another.start);
1591 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001592 }
1593
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001594 private static final StringBuilder sBuilder = new StringBuilder(50);
1595 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1596 sBuilder, Locale.getDefault());
1597
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001598 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001599 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001600
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001601 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001602 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001603 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1604 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001605 }
1606 }
1607
Jeff Sharkey8a503642011-06-10 13:31:21 -07001608 /**
1609 * Special-case data usage cycle that triggers dialog to change
1610 * {@link NetworkPolicy#cycleDay}.
1611 */
1612 public static class CycleChangeItem extends CycleItem {
1613 public CycleChangeItem(Context context) {
1614 super(context.getString(R.string.data_usage_change_cycle));
1615 }
1616 }
1617
1618 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001619 private boolean mChangePossible = false;
1620 private boolean mChangeVisible = false;
1621
1622 private final CycleChangeItem mChangeItem;
1623
Jeff Sharkey8a503642011-06-10 13:31:21 -07001624 public CycleAdapter(Context context) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001625 super(context, R.layout.data_usage_cycle_item);
1626 setDropDownViewResource(R.layout.data_usage_cycle_item_dropdown);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001627 mChangeItem = new CycleChangeItem(context);
1628 }
1629
1630 public void setChangePossible(boolean possible) {
1631 mChangePossible = possible;
1632 updateChange();
1633 }
1634
1635 public void setChangeVisible(boolean visible) {
1636 mChangeVisible = visible;
1637 updateChange();
1638 }
1639
1640 private void updateChange() {
1641 remove(mChangeItem);
1642 if (mChangePossible && mChangeVisible) {
1643 add(mChangeItem);
1644 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001645 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001646
1647 /**
1648 * Find position of {@link CycleItem} in this adapter which is nearest
1649 * the given {@link CycleItem}.
1650 */
1651 public int findNearestPosition(CycleItem target) {
1652 if (target != null) {
1653 final int count = getCount();
1654 for (int i = count - 1; i >= 0; i--) {
1655 final CycleItem item = getItem(i);
1656 if (item instanceof CycleChangeItem) {
1657 continue;
1658 } else if (item.compareTo(target) >= 0) {
1659 return i;
1660 }
1661 }
1662 }
1663 return 0;
1664 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001665 }
1666
1667 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001668 * Adapter of applications, sorted by total usage descending.
1669 */
1670 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001671 private final UidDetailProvider mProvider;
1672 private final int mInsetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001673 private final UserManager mUm;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001674
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001675 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001676 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001677
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001678 public DataUsageAdapter(final UserManager userManager, UidDetailProvider provider, int insetSide) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001679 mProvider = checkNotNull(provider);
1680 mInsetSide = insetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001681 mUm = userManager;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001682 }
1683
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001684 /**
1685 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1686 */
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001687 public void bindStats(NetworkStats stats, int[] restrictedUids) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001688 mItems.clear();
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001689 mLargest = 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001690
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001691 final int currentUserId = ActivityManager.getCurrentUser();
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001692 final List<UserHandle> profiles = mUm.getUserProfiles();
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001693 final SparseArray<AppItem> knownItems = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001694
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001695 NetworkStats.Entry entry = null;
1696 final int size = stats != null ? stats.size() : 0;
1697 for (int i = 0; i < size; i++) {
1698 entry = stats.getValues(i, entry);
1699
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001700 // Decide how to collapse items together
1701 final int uid = entry.uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001702
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001703 final int collapseKey;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001704 final int category;
1705 final int userId = UserHandle.getUserId(uid);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001706 if (UserHandle.isApp(uid)) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001707 if (profiles.contains(new UserHandle(userId))) {
1708 if (userId != currentUserId) {
1709 // Add to a managed user item.
1710 final int managedKey = UidDetailProvider.buildKeyForUser(userId);
1711 accumulate(managedKey, knownItems, entry,
1712 AppItem.CATEGORY_USER);
1713 }
1714 // Add to app item.
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001715 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001716 category = AppItem.CATEGORY_APP;
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001717 } else {
Zoltan Szatmary-Ban77c1d362014-12-12 18:48:03 +00001718 // If it is a removed user add it to the removed users' key
1719 final UserInfo info = mUm.getUserInfo(userId);
1720 if (info == null) {
1721 collapseKey = UID_REMOVED;
1722 category = AppItem.CATEGORY_APP;
1723 } else {
1724 // Add to other user item.
1725 collapseKey = UidDetailProvider.buildKeyForUser(userId);
1726 category = AppItem.CATEGORY_USER;
1727 }
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001728 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001729 } else if (uid == UID_REMOVED || uid == UID_TETHERING) {
1730 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001731 category = AppItem.CATEGORY_APP;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001732 } else {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001733 collapseKey = android.os.Process.SYSTEM_UID;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001734 category = AppItem.CATEGORY_APP;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001735 }
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001736 accumulate(collapseKey, knownItems, entry, category);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001737 }
1738
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001739 final int restrictedUidsMax = restrictedUids.length;
1740 for (int i = 0; i < restrictedUidsMax; ++i) {
1741 final int uid = restrictedUids[i];
1742 // Only splice in restricted state for current user or managed users
1743 if (!profiles.contains(new UserHandle(UserHandle.getUserId(uid)))) {
1744 continue;
1745 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001746
1747 AppItem item = knownItems.get(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001748 if (item == null) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001749 item = new AppItem(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001750 item.total = -1;
1751 mItems.add(item);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001752 knownItems.put(item.key, item);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001753 }
1754 item.restricted = true;
1755 }
1756
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001757 if (!mItems.isEmpty()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001758 final AppItem title = new AppItem();
1759 title.category = AppItem.CATEGORY_APP_TITLE;
1760 mItems.add(title);
1761 }
1762
Jeff Sharkey8a503642011-06-10 13:31:21 -07001763 Collections.sort(mItems);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001764 notifyDataSetChanged();
1765 }
1766
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001767 /**
1768 * Accumulate data usage of a network stats entry for the item mapped by the collapse key.
1769 * Creates the item if needed.
1770 *
1771 * @param collapseKey the collapse key used to map the item.
1772 * @param knownItems collection of known (already existing) items.
1773 * @param entry the network stats entry to extract data usage from.
1774 * @param itemCategory the item is categorized on the list view by this category. Must be
1775 * either AppItem.APP_ITEM_CATEGORY or AppItem.MANAGED_USER_ITEM_CATEGORY
1776 */
1777 private void accumulate(int collapseKey, final SparseArray<AppItem> knownItems,
1778 NetworkStats.Entry entry, int itemCategory) {
1779 final int uid = entry.uid;
1780 AppItem item = knownItems.get(collapseKey);
1781 if (item == null) {
1782 item = new AppItem(collapseKey);
1783 item.category = itemCategory;
1784 mItems.add(item);
1785 knownItems.put(item.key, item);
1786 }
1787 item.addUid(uid);
1788 item.total += entry.rxBytes + entry.txBytes;
1789 if (mLargest < item.total) {
1790 mLargest = item.total;
1791 }
1792 }
1793
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001794 @Override
1795 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001796 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001797 }
1798
1799 @Override
1800 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001801 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001802 }
1803
1804 @Override
1805 public long getItemId(int position) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001806 return mItems.get(position).key;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001807 }
1808
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001809 /**
1810 * See {@link #getItemViewType} for the view types.
1811 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001812 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001813 public int getViewTypeCount() {
1814 return 2;
1815 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001816
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001817 /**
1818 * Returns 1 for separator items and 0 for anything else.
1819 */
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001820 @Override
1821 public int getItemViewType(int position) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001822 final AppItem item = mItems.get(position);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001823 if (item.category == AppItem.CATEGORY_APP_TITLE) {
1824 return 1;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001825 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001826 return 0;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001827 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001828 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001829
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001830 @Override
1831 public boolean areAllItemsEnabled() {
1832 return false;
1833 }
1834
1835 @Override
1836 public boolean isEnabled(int position) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001837 if (position > mItems.size()) {
1838 throw new ArrayIndexOutOfBoundsException();
1839 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001840 return getItemViewType(position) == 0;
1841 }
1842
1843 @Override
1844 public View getView(int position, View convertView, ViewGroup parent) {
1845 final AppItem item = mItems.get(position);
Jason Monk2cdafc62015-06-12 12:32:50 -04001846 LayoutInflater inflater = LayoutInflater.from(parent.getContext());
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001847 if (getItemViewType(position) == 1) {
1848 if (convertView == null) {
Jason Monk2cdafc62015-06-12 12:32:50 -04001849 convertView = Utils.inflateCategoryHeader(inflater, parent);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001850 }
1851
1852 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
1853 title.setText(R.string.data_usage_app);
1854
1855 } else {
1856 if (convertView == null) {
Jason Monk2cdafc62015-06-12 12:32:50 -04001857 convertView = inflater.inflate(R.layout.data_usage_item, parent, false);
1858 inflater.inflate(R.layout.widget_progress_bar,
1859 (ViewGroup) convertView.findViewById(android.R.id.widget_frame));
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001860
1861 if (mInsetSide > 0) {
1862 convertView.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
1863 }
1864 }
1865
1866 final Context context = parent.getContext();
1867
Jason Monk2cdafc62015-06-12 12:32:50 -04001868 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001869 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1870 android.R.id.progress);
1871
1872 // kick off async load of app details
1873 UidDetailTask.bindView(mProvider, item, convertView);
1874
1875 if (item.restricted && item.total <= 0) {
Jason Monk2cdafc62015-06-12 12:32:50 -04001876 summary.setText(R.string.data_usage_app_restricted);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001877 progress.setVisibility(View.GONE);
1878 } else {
Jason Monk2cdafc62015-06-12 12:32:50 -04001879 summary.setText(Formatter.formatFileSize(context, item.total));
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001880 progress.setVisibility(View.VISIBLE);
1881 }
1882
1883 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1884 progress.setProgress(percentTotal);
1885 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001886
1887 return convertView;
1888 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001889 }
1890
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001891 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001892 * Empty {@link Fragment} that controls display of UID details in
1893 * {@link DataUsageSummary}.
1894 */
1895 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001896 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001897
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001898 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jason Monk4bb075b2015-03-17 10:06:58 -04001899 show(parent, app, label, true);
1900 }
1901
1902 public static void show(DataUsageSummary parent, AppItem app, CharSequence label,
1903 boolean addToBack) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001904 if (!parent.isAdded()) return;
1905
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001906 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001907 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001908
1909 final AppDetailsFragment fragment = new AppDetailsFragment();
1910 fragment.setArguments(args);
1911 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001912 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1913 ft.add(fragment, TAG_APP_DETAILS);
Jason Monk4bb075b2015-03-17 10:06:58 -04001914 if (addToBack) {
1915 ft.addToBackStack(TAG_APP_DETAILS);
1916 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001917 ft.setBreadCrumbTitle(
1918 parent.getResources().getString(R.string.data_usage_app_summary_title));
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001919 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001920 }
1921
1922 @Override
1923 public void onStart() {
1924 super.onStart();
1925 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001926 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001927 target.updateBody();
1928 }
1929
1930 @Override
1931 public void onStop() {
1932 super.onStop();
1933 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001934 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001935 target.updateBody();
1936 }
Julia Reynolds1740ce42015-07-27 16:00:06 -04001937
1938 @Override
1939 public boolean onOptionsItemSelected(MenuItem item) {
1940 switch (item.getItemId()) {
1941 case android.R.id.home:
1942 getFragmentManager().popBackStack();
1943 return true;
1944 }
1945 return super.onOptionsItemSelected(item);
1946 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001947 }
1948
1949 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001950 * Dialog to request user confirmation before setting
1951 * {@link NetworkPolicy#limitBytes}.
1952 */
1953 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001954 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001955 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001956
1957 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001958 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001959
Jeff Sharkeyd0a55312014-08-08 10:25:24 -07001960 final NetworkPolicy policy = parent.mPolicyEditor.getPolicy(parent.mTemplate);
1961 if (policy == null) return;
1962
Jeff Sharkey461842a2011-09-25 18:22:48 -07001963 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001964 final CharSequence message;
Jeff Sharkeyd0a55312014-08-08 10:25:24 -07001965 final long minLimitBytes = (long) (policy.warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001966 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001967
1968 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001969 final String currentTab = parent.mCurrentTab;
1970 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001971 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001972 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001973 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001974 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001975 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001976 } else if (isMobileTab(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001977 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001978 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001979 } else {
1980 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001981 }
1982
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001983 final Bundle args = new Bundle();
1984 args.putCharSequence(EXTRA_MESSAGE, message);
1985 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1986
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001987 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1988 dialog.setArguments(args);
1989 dialog.setTargetFragment(parent, 0);
1990 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1991 }
1992
1993 @Override
1994 public Dialog onCreateDialog(Bundle savedInstanceState) {
1995 final Context context = getActivity();
1996
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001997 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001998 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1999
2000 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2001 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07002002 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002003
2004 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002005 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002006 public void onClick(DialogInterface dialog, int which) {
2007 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2008 if (target != null) {
2009 target.setPolicyLimitBytes(limitBytes);
2010 }
2011 }
2012 });
2013
2014 return builder.create();
2015 }
2016 }
2017
2018 /**
2019 * Dialog to edit {@link NetworkPolicy#cycleDay}.
2020 */
2021 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002022 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002023
2024 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002025 if (!parent.isAdded()) return;
2026
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002027 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002028 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002029
2030 final CycleEditorFragment dialog = new CycleEditorFragment();
2031 dialog.setArguments(args);
2032 dialog.setTargetFragment(parent, 0);
2033 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
2034 }
2035
2036 @Override
2037 public Dialog onCreateDialog(Bundle savedInstanceState) {
2038 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002039 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2040 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002041
2042 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2043 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2044
2045 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
2046 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
2047
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002048 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2049 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002050
2051 cycleDayPicker.setMinValue(1);
2052 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002053 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002054 cycleDayPicker.setWrapSelectorWheel(true);
2055
2056 builder.setTitle(R.string.data_usage_cycle_editor_title);
2057 builder.setView(view);
2058
2059 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2060 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002061 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002062 public void onClick(DialogInterface dialog, int which) {
Jeff Sharkeyd277de92013-03-25 15:11:25 -07002063 // clear focus to finish pending text edits
2064 cycleDayPicker.clearFocus();
2065
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002066 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08002067 final String cycleTimezone = new Time().timezone;
2068 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002069 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002070 }
2071 });
2072
2073 return builder.create();
2074 }
2075 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002076
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002077 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002078 * Dialog to edit {@link NetworkPolicy#warningBytes}.
2079 */
2080 public static class WarningEditorFragment extends DialogFragment {
2081 private static final String EXTRA_TEMPLATE = "template";
2082
2083 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002084 if (!parent.isAdded()) return;
2085
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002086 final Bundle args = new Bundle();
2087 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
2088
2089 final WarningEditorFragment dialog = new WarningEditorFragment();
2090 dialog.setArguments(args);
2091 dialog.setTargetFragment(parent, 0);
2092 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
2093 }
2094
2095 @Override
2096 public Dialog onCreateDialog(Bundle savedInstanceState) {
2097 final Context context = getActivity();
2098 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2099 final NetworkPolicyEditor editor = target.mPolicyEditor;
2100
2101 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2102 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2103
2104 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
2105 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
2106
2107 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2108 final long warningBytes = editor.getPolicyWarningBytes(template);
2109 final long limitBytes = editor.getPolicyLimitBytes(template);
2110
2111 bytesPicker.setMinValue(0);
2112 if (limitBytes != LIMIT_DISABLED) {
2113 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
2114 } else {
2115 bytesPicker.setMaxValue(Integer.MAX_VALUE);
2116 }
2117 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
2118 bytesPicker.setWrapSelectorWheel(false);
2119
2120 builder.setTitle(R.string.data_usage_warning_editor_title);
2121 builder.setView(view);
2122
2123 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2124 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002125 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002126 public void onClick(DialogInterface dialog, int which) {
2127 // clear focus to finish pending text edits
2128 bytesPicker.clearFocus();
2129
2130 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
2131 editor.setPolicyWarningBytes(template, bytes);
2132 target.updatePolicy(false);
2133 }
2134 });
2135
2136 return builder.create();
2137 }
2138 }
2139
2140 /**
2141 * Dialog to edit {@link NetworkPolicy#limitBytes}.
2142 */
2143 public static class LimitEditorFragment extends DialogFragment {
2144 private static final String EXTRA_TEMPLATE = "template";
2145
2146 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002147 if (!parent.isAdded()) return;
2148
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002149 final Bundle args = new Bundle();
2150 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
2151
2152 final LimitEditorFragment dialog = new LimitEditorFragment();
2153 dialog.setArguments(args);
2154 dialog.setTargetFragment(parent, 0);
2155 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
2156 }
2157
2158 @Override
2159 public Dialog onCreateDialog(Bundle savedInstanceState) {
2160 final Context context = getActivity();
2161 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2162 final NetworkPolicyEditor editor = target.mPolicyEditor;
2163
2164 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2165 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2166
2167 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
2168 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
2169
2170 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2171 final long warningBytes = editor.getPolicyWarningBytes(template);
2172 final long limitBytes = editor.getPolicyLimitBytes(template);
2173
2174 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09002175 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002176 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
2177 } else {
2178 bytesPicker.setMinValue(0);
2179 }
2180 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
2181 bytesPicker.setWrapSelectorWheel(false);
2182
2183 builder.setTitle(R.string.data_usage_limit_editor_title);
2184 builder.setView(view);
2185
2186 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2187 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002188 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002189 public void onClick(DialogInterface dialog, int which) {
2190 // clear focus to finish pending text edits
2191 bytesPicker.clearFocus();
2192
2193 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
2194 editor.setPolicyLimitBytes(template, bytes);
2195 target.updatePolicy(false);
2196 }
2197 });
2198
2199 return builder.create();
2200 }
2201 }
2202 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07002203 * Dialog to request user confirmation before disabling data.
2204 */
2205 public static class ConfirmDataDisableFragment extends DialogFragment {
Sanket Padawe24f834d2015-01-08 11:23:11 -08002206 static int mSubId;
2207 public static void show(DataUsageSummary parent, int subId) {
2208 mSubId = subId;
Jeff Sharkey461842a2011-09-25 18:22:48 -07002209 if (!parent.isAdded()) return;
2210
Jeff Sharkey28130d92011-09-02 16:10:24 -07002211 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
2212 dialog.setTargetFragment(parent, 0);
2213 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
2214 }
2215
2216 @Override
2217 public Dialog onCreateDialog(Bundle savedInstanceState) {
2218 final Context context = getActivity();
2219
2220 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2221 builder.setMessage(R.string.data_usage_disable_mobile);
2222
2223 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002224 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07002225 public void onClick(DialogInterface dialog, int which) {
2226 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2227 if (target != null) {
2228 // TODO: extend to modify policy enabled flag.
Sanket Padawe24f834d2015-01-08 11:23:11 -08002229 target.setMobileDataEnabled(mSubId, false);
Jeff Sharkey28130d92011-09-02 16:10:24 -07002230 }
2231 }
2232 });
2233 builder.setNegativeButton(android.R.string.cancel, null);
2234
2235 return builder.create();
2236 }
2237 }
2238
2239 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002240 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07002241 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002242 */
2243 public static class ConfirmRestrictFragment extends DialogFragment {
2244 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002245 if (!parent.isAdded()) return;
2246
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002247 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
2248 dialog.setTargetFragment(parent, 0);
2249 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
2250 }
2251
2252 @Override
2253 public Dialog onCreateDialog(Bundle savedInstanceState) {
2254 final Context context = getActivity();
2255
2256 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002257 builder.setTitle(R.string.data_usage_restrict_background_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07002258 if (Utils.hasMultipleUsers(context)) {
2259 builder.setMessage(R.string.data_usage_restrict_background_multiuser);
2260 } else {
2261 builder.setMessage(R.string.data_usage_restrict_background);
2262 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002263
2264 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002265 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002266 public void onClick(DialogInterface dialog, int which) {
2267 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2268 if (target != null) {
2269 target.setRestrictBackground(true);
2270 }
2271 }
2272 });
2273 builder.setNegativeButton(android.R.string.cancel, null);
2274
2275 return builder.create();
2276 }
2277 }
2278
2279 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002280 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
2281 * change has been denied, usually based on
2282 * {@link DataUsageSummary#hasLimitedNetworks()}.
2283 */
2284 public static class DeniedRestrictFragment extends DialogFragment {
2285 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002286 if (!parent.isAdded()) return;
2287
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002288 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
2289 dialog.setTargetFragment(parent, 0);
2290 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
2291 }
2292
2293 @Override
2294 public Dialog onCreateDialog(Bundle savedInstanceState) {
2295 final Context context = getActivity();
2296
2297 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2298 builder.setTitle(R.string.data_usage_app_restrict_background);
2299 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
2300 builder.setPositiveButton(android.R.string.ok, null);
2301
2302 return builder.create();
2303 }
2304 }
2305
2306 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002307 * Dialog to request user confirmation before setting
2308 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
2309 */
2310 public static class ConfirmAppRestrictFragment extends DialogFragment {
2311 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002312 if (!parent.isAdded()) return;
2313
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002314 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2315 dialog.setTargetFragment(parent, 0);
2316 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2317 }
2318
2319 @Override
2320 public Dialog onCreateDialog(Bundle savedInstanceState) {
2321 final Context context = getActivity();
2322
2323 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002324 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2325 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2326
2327 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002328 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002329 public void onClick(DialogInterface dialog, int which) {
2330 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2331 if (target != null) {
2332 target.setAppRestrictBackground(true);
2333 }
2334 }
2335 });
2336 builder.setNegativeButton(android.R.string.cancel, null);
2337
2338 return builder.create();
2339 }
2340 }
2341
2342 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002343 * Compute default tab that should be selected, based on
2344 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2345 */
2346 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002347 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002348 if (template == null) {
Wink Savilleb003a852014-10-23 10:16:26 -07002349 final int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
Wink Saville0183fb52014-11-22 10:11:39 -08002350 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Savilled72c0622014-12-11 10:10:05 -08002351 if (SubscriptionManager.isValidSubscriptionId(subId)) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07002352 return TAB_MOBILE + String.valueOf(subId);
2353 }
2354 return null;
2355 }
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002356
2357 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002358 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002359 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002360 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002361 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002362 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002363 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002364 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002365 return TAB_WIFI;
2366 default:
2367 return null;
2368 }
2369 }
2370
2371 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002372 * Background task that loads {@link UidDetail}, binding to
2373 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002374 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002375 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2376 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002377 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002378 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002379
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002380 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002381 mProvider = checkNotNull(provider);
2382 mItem = checkNotNull(item);
2383 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002384 }
2385
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002386 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002387 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002388 final UidDetailTask existing = (UidDetailTask) target.getTag();
2389 if (existing != null) {
2390 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002391 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002392
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002393 final UidDetail cachedDetail = provider.getUidDetail(item.key, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002394 if (cachedDetail != null) {
2395 bindView(cachedDetail, target);
2396 } else {
2397 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2398 AsyncTask.THREAD_POOL_EXECUTOR));
2399 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002400 }
2401
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002402 private static void bindView(UidDetail detail, View target) {
2403 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2404 final TextView title = (TextView) target.findViewById(android.R.id.title);
2405
2406 if (detail != null) {
2407 icon.setImageDrawable(detail.icon);
2408 title.setText(detail.label);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +01002409 title.setContentDescription(detail.contentDescription);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002410 } else {
2411 icon.setImageDrawable(null);
2412 title.setText(null);
2413 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002414 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002415
2416 @Override
2417 protected void onPreExecute() {
2418 bindView(null, mTarget);
2419 }
2420
2421 @Override
2422 protected UidDetail doInBackground(Void... params) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002423 return mProvider.getUidDetail(mItem.key, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002424 }
2425
2426 @Override
2427 protected void onPostExecute(UidDetail result) {
2428 bindView(result, mTarget);
2429 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002430 }
2431
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002432 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002433 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002434 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002435 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002436 if (TEST_RADIOS) {
2437 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2438 }
2439
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002440 final ConnectivityManager conn = ConnectivityManager.from(context);
2441 final TelephonyManager tele = TelephonyManager.from(context);
2442
Wink Saville0183fb52014-11-22 10:11:39 -08002443 final List<SubscriptionInfo> subInfoList =
2444 SubscriptionManager.from(context).getActiveSubscriptionInfoList();
2445 // No activated Subscriptions
PauloftheWest50e6eca2014-10-03 11:07:14 -07002446 if (subInfoList == null) {
Wink Savillefcec91f2014-12-02 17:12:08 -08002447 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subInfoList=null");
PauloftheWest50e6eca2014-10-03 11:07:14 -07002448 return false;
2449 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002450 // require both supported network and ready SIM
PauloftheWest50e6eca2014-10-03 11:07:14 -07002451 boolean isReady = true;
Wink Savilleca756612014-11-08 10:47:12 -08002452 for (SubscriptionInfo subInfo : subInfoList) {
Stuart Scott3ada2ec2014-10-31 14:11:24 -07002453 isReady = isReady & tele.getSimState(subInfo.getSimSlotIndex()) == SIM_STATE_READY;
Wink Savillefcec91f2014-12-02 17:12:08 -08002454 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subInfo=" + subInfo);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002455 }
Wink Savillefcec91f2014-12-02 17:12:08 -08002456 boolean retVal = conn.isNetworkSupported(TYPE_MOBILE) && isReady;
2457 if (LOGD) {
2458 Log.d(TAG, "hasReadyMobileRadio:"
2459 + " conn.isNetworkSupported(TYPE_MOBILE)="
2460 + conn.isNetworkSupported(TYPE_MOBILE)
2461 + " isReady=" + isReady);
2462 }
2463 return retVal;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002464 }
2465
2466 /*
2467 * TODO: consider adding to TelephonyManager or SubscritpionManager.
2468 */
Wink Savilleb003a852014-10-23 10:16:26 -07002469 public static boolean hasReadyMobileRadio(Context context, int subId) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07002470 if (TEST_RADIOS) {
2471 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2472 }
2473
2474 final ConnectivityManager conn = ConnectivityManager.from(context);
2475 final TelephonyManager tele = TelephonyManager.from(context);
2476 final int slotId = SubscriptionManager.getSlotId(subId);
2477 final boolean isReady = tele.getSimState(slotId) == SIM_STATE_READY;
2478
Wink Savillefcec91f2014-12-02 17:12:08 -08002479 boolean retVal = conn.isNetworkSupported(TYPE_MOBILE) && isReady;
2480 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subId=" + subId
2481 + " conn.isNetworkSupported(TYPE_MOBILE)=" + conn.isNetworkSupported(TYPE_MOBILE)
2482 + " isReady=" + isReady);
2483 return retVal;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002484 }
2485
2486 /**
2487 * Test if device has a mobile 4G data radio.
2488 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002489 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002490 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2491 return false;
2492 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002493 if (TEST_RADIOS) {
2494 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2495 }
2496
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002497 final ConnectivityManager conn = ConnectivityManager.from(context);
2498 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002499
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002500 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Wink Saville55434042012-06-14 12:33:43 -07002501 final boolean hasLte = (tele.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE)
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002502 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002503 return hasWimax || hasLte;
2504 }
2505
2506 /**
2507 * Test if device has a Wi-Fi data radio.
2508 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002509 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002510 if (TEST_RADIOS) {
2511 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2512 }
2513
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002514 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002515 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002516 }
2517
2518 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002519 * Test if device has an ethernet network connection.
2520 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002521 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002522 if (TEST_RADIOS) {
2523 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2524 }
2525
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002526 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002527 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2528
2529 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002530 if (mStatsSession != null) {
2531 try {
2532 ethernetBytes = mStatsSession.getSummaryForNetwork(
2533 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2534 .getTotalBytes();
2535 } catch (RemoteException e) {
2536 throw new RuntimeException(e);
2537 }
2538 } else {
2539 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002540 }
2541
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002542 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002543 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002544 }
2545
2546 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002547 * Inflate a {@link Preference} style layout, adding the given {@link View}
2548 * widget into {@link android.R.id#widget_frame}.
2549 */
2550 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2551 final View view = inflater.inflate(R.layout.preference, root, false);
2552 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2553 android.R.id.widget_frame);
2554 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2555 return view;
2556 }
2557
2558 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002559 * Test if any networks are currently limited.
2560 */
2561 private boolean hasLimitedNetworks() {
2562 return !buildLimitedNetworksList().isEmpty();
2563 }
2564
2565 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002566 * Build string describing currently limited networks, which defines when
2567 * background data is restricted.
2568 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002569 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002570 private CharSequence buildLimitedNetworksString() {
2571 final List<CharSequence> limited = buildLimitedNetworksList();
2572
2573 // handle case where no networks limited
2574 if (limited.isEmpty()) {
2575 limited.add(getText(R.string.data_usage_list_none));
2576 }
2577
2578 return TextUtils.join(limited);
2579 }
2580
2581 /**
2582 * Build list of currently limited networks, which defines when background
2583 * data is restricted.
2584 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002585 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002586 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002587 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002588
2589 // build combined list of all limited networks
2590 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002591
2592 final TelephonyManager tele = TelephonyManager.from(context);
2593 if (tele.getSimState() == SIM_STATE_READY) {
2594 final String subscriberId = getActiveSubscriberId(context);
2595 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2596 limited.add(getText(R.string.data_usage_list_mobile));
2597 }
2598 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2599 limited.add(getText(R.string.data_usage_tab_3g));
2600 }
2601 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2602 limited.add(getText(R.string.data_usage_tab_4g));
2603 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002604 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002605
2606 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002607 limited.add(getText(R.string.data_usage_tab_wifi));
2608 }
2609 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2610 limited.add(getText(R.string.data_usage_tab_ethernet));
2611 }
2612
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002613 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002614 }
2615
2616 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002617 * Inset both selector and divider {@link Drawable} on the given
2618 * {@link ListView} by the requested dimensions.
2619 */
2620 private static void insetListViewDrawables(ListView view, int insetSide) {
2621 final Drawable selector = view.getSelector();
2622 final Drawable divider = view.getDivider();
2623
2624 // fully unregister these drawables so callbacks can be maintained after
2625 // wrapping below.
2626 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2627 view.setSelector(stub);
2628 view.setDivider(stub);
2629
2630 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2631 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2632 }
2633
2634 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002635 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002636 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002637 */
2638 private static void setPreferenceTitle(View parent, int resId) {
2639 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2640 title.setText(resId);
2641 }
2642
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002643 /**
2644 * Set {@link android.R.id#summary} for a preference view inflated with
2645 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2646 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002647 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002648 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2649 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002650 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002651 }
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002652
2653 /**
2654 * For search
2655 */
2656 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -07002657 new BaseSearchIndexProvider() {
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002658 @Override
2659 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
2660 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
2661
2662 final Resources res = context.getResources();
2663
2664 // Add fragment title
2665 SearchIndexableRaw data = new SearchIndexableRaw(context);
2666 data.title = res.getString(R.string.data_usage_summary_title);
2667 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2668 result.add(data);
2669
2670 // Mobile data
2671 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002672 data.key = DATA_USAGE_ENABLE_MOBILE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002673 data.title = res.getString(R.string.data_usage_enable_mobile);
2674 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2675 result.add(data);
2676
2677 // Set mobile data limit
2678 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002679 data.key = DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002680 data.title = res.getString(R.string.data_usage_disable_mobile_limit);
2681 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2682 result.add(data);
2683
Fabrice Di Megliof2a52262014-04-17 17:20:27 -07002684 // Data usage cycle
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002685 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002686 data.key = DATA_USAGE_CYCLE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002687 data.title = res.getString(R.string.data_usage_cycle);
2688 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2689 result.add(data);
2690
2691 return result;
2692 }
2693 };
2694
Sanket Padawe7e21fa52015-02-12 12:39:34 -08002695 private void addMobileTab(Context context, SubscriptionInfo subInfo, boolean isMultiSim) {
PauloftheWestc80b7612014-11-25 04:40:45 -08002696 if (subInfo != null && mMobileTagMap != null) {
2697 if (hasReadyMobileRadio(context, subInfo.getSubscriptionId())) {
Sanket Padawe7e21fa52015-02-12 12:39:34 -08002698 if (isMultiSim) {
2699 mTabHost.addTab(buildTabSpec(mMobileTagMap.get(subInfo.getSubscriptionId()),
2700 subInfo.getDisplayName()));
2701 } else {
2702 mTabHost.addTab(buildTabSpec(mMobileTagMap.get(subInfo.getSubscriptionId()),
2703 R.string.data_usage_tab_mobile));
2704 }
PauloftheWest50e6eca2014-10-03 11:07:14 -07002705 }
Wink Savillefcec91f2014-12-02 17:12:08 -08002706 } else {
2707 if (LOGD) Log.d(TAG, "addMobileTab: subInfoList is null");
PauloftheWest50e6eca2014-10-03 11:07:14 -07002708 }
2709 }
2710
Wink Savilleca756612014-11-08 10:47:12 -08002711 private SubscriptionInfo getCurrentTabSubInfo(Context context) {
PauloftheWestde338442014-10-29 14:46:44 -07002712 if (mSubInfoList != null && mTabHost != null) {
2713 final int currentTagIndex = mTabHost.getCurrentTab();
2714 int i = 0;
Wink Savilleca756612014-11-08 10:47:12 -08002715 for (SubscriptionInfo subInfo : mSubInfoList) {
Stuart Scott3ada2ec2014-10-31 14:11:24 -07002716 if (hasReadyMobileRadio(context, subInfo.getSubscriptionId())) {
PauloftheWestde338442014-10-29 14:46:44 -07002717 if (i++ == currentTagIndex) {
2718 return subInfo;
2719 }
2720 }
2721 }
2722 }
2723 return null;
2724 }
2725
PauloftheWest50e6eca2014-10-03 11:07:14 -07002726 /**
2727 * Init a map with subId key and mobile tag name
2728 * @param subInfoList The subscription Info List
2729 * @return The map or null if no activated subscription
2730 */
Wink Savilleca756612014-11-08 10:47:12 -08002731 private Map<Integer, String> initMobileTabTag(List<SubscriptionInfo> subInfoList) {
Wink Savilleb003a852014-10-23 10:16:26 -07002732 Map<Integer, String> map = null;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002733 if (subInfoList != null) {
2734 String mobileTag;
Wink Savilleb003a852014-10-23 10:16:26 -07002735 map = new HashMap<Integer, String>();
Wink Savillefcec91f2014-12-02 17:12:08 -08002736 for (SubscriptionInfo subInfo : subInfoList) {
2737 mobileTag = TAB_MOBILE + String.valueOf(subInfo.getSubscriptionId());
2738 map.put(subInfo.getSubscriptionId(), mobileTag);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002739 }
2740 }
2741 return map;
2742 }
2743
2744 private static boolean isMobileTab(String currentTab) {
2745 return currentTab != null ? currentTab.contains(TAB_MOBILE) : false;
2746 }
2747
Wink Savilleb003a852014-10-23 10:16:26 -07002748 private int getSubId(String currentTab) {
Wink Saville0183fb52014-11-22 10:11:39 -08002749 if (mMobileTagMap != null) {
2750 Set<Integer> set = mMobileTagMap.keySet();
2751 for (Integer subId : set) {
2752 if (mMobileTagMap.get(subId).equals(currentTab)) {
2753 return subId;
2754 }
PauloftheWest50e6eca2014-10-03 11:07:14 -07002755 }
2756 }
2757 Log.e(TAG, "currentTab = " + currentTab + " non mobile tab called this function");
2758 return -1;
2759 }
2760
Stuart Scottf51d0062015-04-08 09:40:48 -07002761 private boolean isMobileDataAvailable(int subId) {
2762 return mSubscriptionManager.getActiveSubscriptionInfo(subId) != null;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002763 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002764}