blob: 38376e46b2a56e59bfdc11d79c2255782cf42451 [file] [log] [blame]
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070019import android.animation.LayoutTransition;
Jason Monk30695812015-11-17 09:01:08 -050020import android.app.Activity;
Jeff Sharkey38305fb2012-09-14 16:26:51 -070021import android.app.ActivityManager;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070022import android.app.AlertDialog;
23import android.app.Dialog;
24import android.app.DialogFragment;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070025import android.app.Fragment;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070026import android.app.FragmentTransaction;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070027import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkeyb6548462014-07-21 15:38:06 -070028import android.content.ComponentName;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070029import android.content.Context;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070030import android.content.DialogInterface;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -070031import android.content.Intent;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070032import android.content.Loader;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070033import android.content.SharedPreferences;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070034import android.content.pm.PackageManager;
Jason Monk4bb075b2015-03-17 10:06:58 -040035import android.content.pm.PackageManager.NameNotFoundException;
Zoltan Szatmary-Ban77c1d362014-12-12 18:48:03 +000036import android.content.pm.UserInfo;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070037import android.content.res.Resources;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070038import android.graphics.Color;
Jeff Sharkey5d706792011-09-08 18:57:17 -070039import android.graphics.drawable.ColorDrawable;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070040import android.graphics.drawable.Drawable;
Jason Monk30695812015-11-17 09:01:08 -050041import android.icu.impl.ICUResourceBundle;
42import android.icu.util.UResourceBundle;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070043import android.net.ConnectivityManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070044import android.net.INetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070045import android.net.INetworkStatsService;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070046import android.net.INetworkStatsSession;
Jeff Sharkey8a503642011-06-10 13:31:21 -070047import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070048import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070049import android.net.NetworkStats;
50import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070051import android.net.NetworkTemplate;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070052import android.net.TrafficStats;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070053import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070054import android.os.Bundle;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070055import android.os.INetworkManagementService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070056import android.os.RemoteException;
57import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070058import android.os.SystemProperties;
Dianne Hackbornbb06a422012-08-16 11:01:57 -070059import android.os.UserHandle;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +010060import android.os.UserManager;
Jason Monk39b46742015-09-10 15:52:51 -040061import android.support.v7.preference.Preference;
Wink Savilleca756612014-11-08 10:47:12 -080062import android.telephony.SubscriptionInfo;
PauloftheWest50e6eca2014-10-03 11:07:14 -070063import android.telephony.SubscriptionManager;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070064import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070065import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070066import android.text.format.DateUtils;
67import android.text.format.Formatter;
Jeff Sharkeye5223a02012-03-09 17:11:14 -080068import android.text.format.Time;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070069import android.util.Log;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070070import android.util.SparseArray;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070071import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -070072import android.view.Menu;
73import android.view.MenuInflater;
74import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070075import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070076import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070077import android.view.ViewGroup;
Jeff Sharkey8a503642011-06-10 13:31:21 -070078import android.widget.AdapterView;
79import android.widget.AdapterView.OnItemClickListener;
80import android.widget.AdapterView.OnItemSelectedListener;
81import android.widget.ArrayAdapter;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070082import android.widget.BaseAdapter;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070083import android.widget.Button;
Jason Monk20f464e2015-05-06 16:00:25 -040084import android.widget.FrameLayout;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070085import android.widget.ImageView;
Jeff Sharkey8a503642011-06-10 13:31:21 -070086import android.widget.LinearLayout;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070087import android.widget.ListView;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070088import android.widget.NumberPicker;
Jeff Sharkey2412b0f2011-07-17 20:31:40 -070089import android.widget.ProgressBar;
Jeff Sharkey8a503642011-06-10 13:31:21 -070090import android.widget.Spinner;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070091import android.widget.Switch;
Jeff Sharkey8a503642011-06-10 13:31:21 -070092import android.widget.TabHost;
93import android.widget.TabHost.OnTabChangeListener;
94import android.widget.TabHost.TabContentFactory;
95import android.widget.TabHost.TabSpec;
96import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070097import android.widget.TextView;
Jason Monk20f464e2015-05-06 16:00:25 -040098import android.widget.Toast;
Chris Wren8a963ba2015-03-20 10:29:14 -040099import com.android.internal.logging.MetricsLogger;
Wink Saville55434042012-06-14 12:33:43 -0700100import com.android.internal.telephony.PhoneConstants;
Jason Monk30695812015-11-17 09:01:08 -0500101import com.android.settings.dashboard.SummaryLoader;
Jason Monkc6858532015-12-16 12:58:39 -0500102import com.android.settings.dashboard.conditional.BackgroundDataCondition;
103import com.android.settings.dashboard.conditional.ConditionManager;
Jeff Sharkey5d706792011-09-08 18:57:17 -0700104import com.android.settings.drawable.InsetBoundsDrawable;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700105import com.android.settings.net.DataUsageMeteredSettings;
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -0700106import com.android.settings.search.BaseSearchIndexProvider;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -0700107import com.android.settings.search.Indexable;
108import com.android.settings.search.SearchIndexableRaw;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700109import com.android.settings.widget.ChartDataUsageView;
110import com.android.settings.widget.ChartDataUsageView.DataUsageChartListener;
PauloftheWestc80b7612014-11-25 04:40:45 -0800111import com.android.settings.widget.ChartNetworkSeriesView;
Wei Liude557d42015-07-13 12:01:42 -0700112import com.android.settingslib.AppItem;
Wei Liue0021c42015-07-07 15:37:11 -0700113import com.android.settingslib.NetworkPolicyEditor;
Wei Liude557d42015-07-13 12:01:42 -0700114import com.android.settingslib.net.ChartData;
115import com.android.settingslib.net.ChartDataLoader;
Jason Monk30695812015-11-17 09:01:08 -0500116import com.android.settingslib.net.MobileDataController;
Wei Liu8dffd152015-08-20 22:33:46 -0700117import com.android.settingslib.net.SummaryForAllUidLoader;
Wei Liu6e58d512015-08-24 17:29:04 -0700118import com.android.settingslib.net.UidDetail;
119import com.android.settingslib.net.UidDetailProvider;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700120import com.google.android.collect.Lists;
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700121import libcore.util.Objects;
122
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700123import java.util.ArrayList;
124import java.util.Collections;
PauloftheWest50e6eca2014-10-03 11:07:14 -0700125import java.util.HashMap;
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700126import java.util.List;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700127import java.util.Locale;
PauloftheWest50e6eca2014-10-03 11:07:14 -0700128import java.util.Map;
129import java.util.Set;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700130
Jason Monk39b46742015-09-10 15:52:51 -0400131import static android.net.ConnectivityManager.TYPE_ETHERNET;
132import static android.net.ConnectivityManager.TYPE_MOBILE;
133import static android.net.ConnectivityManager.TYPE_WIFI;
134import static android.net.ConnectivityManager.TYPE_WIMAX;
135import static android.net.NetworkPolicy.LIMIT_DISABLED;
136import static android.net.NetworkPolicy.WARNING_DISABLED;
137import static android.net.NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE;
138import static android.net.NetworkPolicyManager.POLICY_NONE;
139import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND;
140import static android.net.NetworkPolicyManager.computeLastCycleBoundary;
141import static android.net.NetworkPolicyManager.computeNextCycleBoundary;
142import static android.net.NetworkTemplate.MATCH_MOBILE_3G_LOWER;
143import static android.net.NetworkTemplate.MATCH_MOBILE_4G;
144import static android.net.NetworkTemplate.MATCH_MOBILE_ALL;
145import static android.net.NetworkTemplate.MATCH_WIFI;
146import static android.net.NetworkTemplate.buildTemplateEthernet;
147import static android.net.NetworkTemplate.buildTemplateMobile3gLower;
148import static android.net.NetworkTemplate.buildTemplateMobile4g;
149import static android.net.NetworkTemplate.buildTemplateMobileAll;
150import static android.net.NetworkTemplate.buildTemplateWifiWildcard;
151import static android.net.TrafficStats.GB_IN_BYTES;
152import static android.net.TrafficStats.MB_IN_BYTES;
153import static android.net.TrafficStats.UID_REMOVED;
154import static android.net.TrafficStats.UID_TETHERING;
155import static android.telephony.TelephonyManager.SIM_STATE_READY;
156import static android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
157import static android.text.format.DateUtils.FORMAT_SHOW_DATE;
158import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
159import static com.android.internal.util.Preconditions.checkNotNull;
160import static com.android.settings.Utils.prepareCustomPreferencesList;
161
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700162/**
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700163 * Panel showing data usage history across various networks, including options
164 * to inspect based on usage cycle and control through {@link NetworkPolicy}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700165 */
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700166public class DataUsageSummary extends HighlightingFragment implements Indexable {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700167 private static final String TAG = "DataUsage";
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800168 private static final boolean LOGD = false;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700169
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700170 // TODO: remove this testing code
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700171 private static final boolean TEST_ANIM = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700172 private static final boolean TEST_RADIOS = false;
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800173
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700174 private static final String TEST_RADIOS_PROP = "test.radios";
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800175 private static final String TEST_SUBSCRIBER_PROP = "test.subscriberid";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700176
Jeff Sharkey8a503642011-06-10 13:31:21 -0700177 private static final String TAB_3G = "3g";
178 private static final String TAB_4G = "4g";
179 private static final String TAB_MOBILE = "mobile";
180 private static final String TAB_WIFI = "wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700181 private static final String TAB_ETHERNET = "ethernet";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700182
Jeff Sharkey28130d92011-09-02 16:10:24 -0700183 private static final String TAG_CONFIRM_DATA_DISABLE = "confirmDataDisable";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700184 private static final String TAG_CONFIRM_LIMIT = "confirmLimit";
185 private static final String TAG_CYCLE_EDITOR = "cycleEditor";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700186 private static final String TAG_WARNING_EDITOR = "warningEditor";
187 private static final String TAG_LIMIT_EDITOR = "limitEditor";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700188 private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700189 private static final String TAG_DENIED_RESTRICT = "deniedRestrict";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700190 private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700191 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700192
Fabrice Di Meglio2169c882014-04-18 15:18:40 -0700193 private static final String DATA_USAGE_ENABLE_MOBILE_KEY = "data_usage_enable_mobile";
194 private static final String DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY =
195 "data_usage_disable_mobile_limit";
196 private static final String DATA_USAGE_CYCLE_KEY = "data_usage_cycle";
197
Jason Monk4bb075b2015-03-17 10:06:58 -0400198 public static final String EXTRA_SHOW_APP_IMMEDIATE_PKG = "showAppImmediatePkg";
199
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700200 private static final int LOADER_CHART_DATA = 2;
201 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700202
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700203 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700204 private INetworkStatsService mStatsService;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700205 private NetworkPolicyManager mPolicyManager;
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700206 private TelephonyManager mTelephonyManager;
Wink Saville0183fb52014-11-22 10:11:39 -0800207 private SubscriptionManager mSubscriptionManager;
Xiaohui Chen44879a32015-07-22 13:53:22 -0700208 private UserManager mUserManager;
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700209
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700210 private INetworkStatsSession mStatsSession;
211
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700212 private static final String PREF_FILE = "data_usage";
213 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700214 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700215
216 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700217
Jeff Sharkey8a503642011-06-10 13:31:21 -0700218 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700219 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700220 private TabWidget mTabWidget;
221 private ListView mListView;
PauloftheWestc80b7612014-11-25 04:40:45 -0800222 private ChartNetworkSeriesView mSeries;
223 private ChartNetworkSeriesView mDetailedSeries;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700224 private DataUsageAdapter mAdapter;
225
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700226 /** Distance to inset content from sides, when needed. */
227 private int mInsetSide = 0;
228
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700229 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700230
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700231 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700232 private LinearLayout mNetworkSwitches;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700233 private boolean mDataEnabledSupported;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700234 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700235 private View mDataEnabledView;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700236 private boolean mDisableAtLimitSupported;
237 private Switch mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700238 private View mDisableAtLimitView;
239
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700240 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700241 private Spinner mCycleSpinner;
242 private CycleAdapter mCycleAdapter;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700243 private TextView mCycleSummary;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700244
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700245 private ChartDataUsageView mChart;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700246 private View mDisclaimer;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700247 private TextView mEmpty;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700248 private View mStupidPadding;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700249
250 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700251 private ImageView mAppIcon;
252 private ViewGroup mAppTitles;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700253 private TextView mAppTotal;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700254 private TextView mAppForeground;
255 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700256 private Button mAppSettings;
257
258 private LinearLayout mAppSwitches;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700259 private Switch mAppRestrict;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700260 private View mAppRestrictView;
261
Jeff Sharkey8a503642011-06-10 13:31:21 -0700262 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700263 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700264
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700265 private NetworkTemplate mTemplate;
266 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700267
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700268 private AppItem mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700269
270 private Intent mAppSettingsIntent;
271
Jeff Sharkeya662e492011-06-18 21:57:06 -0700272 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700273
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700274 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700275 private String mIntentTab = null;
276
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700277 private MenuItem mMenuRestrictBackground;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700278 private MenuItem mMenuShowWifi;
279 private MenuItem mMenuShowEthernet;
280 private MenuItem mMenuSimCards;
281 private MenuItem mMenuCellularNetworks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700282
Wink Savilleca756612014-11-08 10:47:12 -0800283 private List<SubscriptionInfo> mSubInfoList;
Wink Savilleb003a852014-10-23 10:16:26 -0700284 private Map<Integer,String> mMobileTagMap;
PauloftheWest50e6eca2014-10-03 11:07:14 -0700285
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700286 /** Flag used to ignore listeners during binding. */
287 private boolean mBinding;
288
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700289 private UidDetailProvider mUidDetailProvider;
290
Jason Monk4bb075b2015-03-17 10:06:58 -0400291 // Indicates request to show app immediately rather than list.
292 private String mShowAppImmediatePkg;
293
Sanket Padawed819270f2015-01-14 11:03:33 -0800294 /**
295 * Local cache of data enabled for subId, used to work around delays.
296 */
297 private final Map<String, Boolean> mMobileDataEnabled = new HashMap<String, Boolean>();
298
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700299 @Override
Chris Wren8a963ba2015-03-20 10:29:14 -0400300 protected int getMetricsCategory() {
301 return MetricsLogger.DATA_USAGE_SUMMARY;
302 }
303
304 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700305 public void onCreate(Bundle savedInstanceState) {
306 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700307 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700308
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700309 mNetworkService = INetworkManagementService.Stub.asInterface(
310 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700311 mStatsService = INetworkStatsService.Stub.asInterface(
312 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700313 mPolicyManager = NetworkPolicyManager.from(context);
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700314 mTelephonyManager = TelephonyManager.from(context);
Wink Saville0183fb52014-11-22 10:11:39 -0800315 mSubscriptionManager = SubscriptionManager.from(context);
Xiaohui Chen44879a32015-07-22 13:53:22 -0700316 mUserManager = UserManager.get(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700317
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700318 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700319
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700320 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700321 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700322
Wink Saville0183fb52014-11-22 10:11:39 -0800323 mSubInfoList = mSubscriptionManager.getActiveSubscriptionInfoList();
PauloftheWest50e6eca2014-10-03 11:07:14 -0700324 mMobileTagMap = initMobileTabTag(mSubInfoList);
325
Jaewan Kimffce9c12013-03-19 16:53:45 +0900326 try {
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700327 if (!mNetworkService.isBandwidthControlEnabled()) {
328 Log.w(TAG, "No bandwidth control; leaving");
329 getActivity().finish();
330 }
331 } catch (RemoteException e) {
332 Log.w(TAG, "No bandwidth control; leaving");
333 getActivity().finish();
334 }
335
336 try {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900337 mStatsSession = mStatsService.openSession();
338 } catch (RemoteException e) {
339 throw new RuntimeException(e);
340 }
341
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700342 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700343 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700344
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700345 // override preferences when no mobile radio
346 if (!hasReadyMobileRadio(context)) {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900347 mShowWifi = true;
348 mShowEthernet = true;
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700349 }
350
Jason Monk4bb075b2015-03-17 10:06:58 -0400351 mUidDetailProvider = new UidDetailProvider(context);
352
353 Bundle arguments = getArguments();
354 if (arguments != null) {
355 mShowAppImmediatePkg = arguments.getString(EXTRA_SHOW_APP_IMMEDIATE_PKG);
356 }
357
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700358 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700359 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700360
Jeff Sharkey8a503642011-06-10 13:31:21 -0700361 @Override
362 public View onCreateView(LayoutInflater inflater, ViewGroup container,
363 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700364
Jeff Sharkey8a503642011-06-10 13:31:21 -0700365 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700366 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
367
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700368
Jeff Sharkey8a503642011-06-10 13:31:21 -0700369 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700370 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700371 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
372 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700373
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700374 // decide if we need to manually inset our content, or if we should rely
375 // on parent container for inset.
376 final boolean shouldInset = mListView.getScrollBarStyle()
377 == View.SCROLLBARS_OUTSIDE_OVERLAY;
Amith Yamasani56f51a82013-08-05 10:07:23 -0700378 mInsetSide = 0;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700379
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700380 // adjust padding around tabwidget as needed
Amith Yamasani56f51a82013-08-05 10:07:23 -0700381 prepareCustomPreferencesList(container, view, mListView, false);
Jeff Sharkey5d706792011-09-08 18:57:17 -0700382
Jeff Sharkey8a503642011-06-10 13:31:21 -0700383 mTabHost.setup();
384 mTabHost.setOnTabChangedListener(mTabListener);
385
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700386 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700387 mHeader.setClickable(true);
388
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700389 mListView.addHeaderView(new View(context), null, true);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700390 mListView.addHeaderView(mHeader, null, true);
391 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700392
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700393 if (mInsetSide > 0) {
394 // inset selector and divider drawables
395 insetListViewDrawables(mListView, mInsetSide);
Fabrice Di Megliob27223f2013-01-15 18:54:11 -0800396 mHeader.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700397 }
398
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700399 {
400 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700401 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
402 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700403 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700404
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700405 mDataEnabled = new Switch(inflater.getContext());
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700406 mDataEnabled.setClickable(false);
407 mDataEnabled.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700408 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700409 mDataEnabledView.setTag(R.id.preference_highlight_key,
410 DATA_USAGE_ENABLE_MOBILE_KEY);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700411 mDataEnabledView.setClickable(true);
412 mDataEnabledView.setFocusable(true);
413 mDataEnabledView.setOnClickListener(mDataEnabledListener);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700414 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700415
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700416 mDisableAtLimit = new Switch(inflater.getContext());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700417 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700418 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700419 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700420 mDisableAtLimitView.setTag(R.id.preference_highlight_key,
421 DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700422 mDisableAtLimitView.setClickable(true);
423 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700424 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
425 mNetworkSwitches.addView(mDisableAtLimitView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700426
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700427 mCycleView = inflater.inflate(R.layout.data_usage_cycles, mNetworkSwitches, false);
428 mCycleView.setTag(R.id.preference_highlight_key, DATA_USAGE_CYCLE_KEY);
429 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
430 mCycleAdapter = new CycleAdapter(context);
431 mCycleSpinner.setAdapter(mCycleAdapter);
432 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
433 mCycleSummary = (TextView) mCycleView.findViewById(R.id.cycle_summary);
434 mNetworkSwitches.addView(mCycleView);
PauloftheWestc80b7612014-11-25 04:40:45 -0800435 mSeries = (ChartNetworkSeriesView)view.findViewById(R.id.series);
436 mDetailedSeries = (ChartNetworkSeriesView)view.findViewById(R.id.detail_series);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700437 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700438
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700439 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700440 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800441 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700442
443 {
444 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700445 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700446 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
447 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700448 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
449 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700450 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700451
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700452 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700453
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700454 mAppRestrict = new Switch(inflater.getContext());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700455 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700456 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700457 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700458 mAppRestrictView.setClickable(true);
459 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700460 mAppRestrictView.setOnClickListener(mAppRestrictListener);
461 mAppSwitches.addView(mAppRestrictView);
462 }
463
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700464 mDisclaimer = mHeader.findViewById(R.id.disclaimer);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700465 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700466 mStupidPadding = mHeader.findViewById(R.id.stupid_padding);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700467
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +0100468 final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
469 mAdapter = new DataUsageAdapter(um, mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700470 mListView.setOnItemClickListener(mListListener);
471 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700472
Jason Monk20f464e2015-05-06 16:00:25 -0400473 showRequestedAppIfNeeded(view);
Jason Monk4bb075b2015-03-17 10:06:58 -0400474
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700475 return view;
476 }
477
Jason Monk20f464e2015-05-06 16:00:25 -0400478 private void showRequestedAppIfNeeded(View rootView) {
Jason Monk4bb075b2015-03-17 10:06:58 -0400479 if (mShowAppImmediatePkg == null) {
480 return;
481 }
482 try {
483 int uid = getActivity().getPackageManager().getPackageUid(mShowAppImmediatePkg,
484 UserHandle.myUserId());
485 AppItem app = new AppItem(uid);
486 app.addUid(uid);
487
488 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
489 // When we are going straight to an app then we are coming from App Info and want
490 // a header at the top.
Jason Monk20f464e2015-05-06 16:00:25 -0400491 FrameLayout pinnedHeader = (FrameLayout) rootView.findViewById(R.id.pinned_header);
Julia Reynoldse492ee02015-08-19 10:21:52 -0400492 AppHeader.createAppHeader(getActivity(), detail.icon, detail.label,
493 mShowAppImmediatePkg, uid, pinnedHeader);
494 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label, false);
Jason Monk4bb075b2015-03-17 10:06:58 -0400495 } catch (NameNotFoundException e) {
496 Log.w(TAG, "Could not find " + mShowAppImmediatePkg, e);
497 Toast.makeText(getActivity(), getString(R.string.unknown_app), Toast.LENGTH_LONG)
498 .show();
499 getActivity().finish();
500 }
501 }
502
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700503 @Override
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700504 public void onViewStateRestored(Bundle savedInstanceState) {
505 super.onViewStateRestored(savedInstanceState);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700506
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700507 // pick default tab based on incoming intent
508 final Intent intent = getActivity().getIntent();
509 mIntentTab = computeTabFromIntent(intent);
510
Jeff Sharkey8a503642011-06-10 13:31:21 -0700511 // this kicks off chain reaction which creates tabs, binds the body to
512 // selected network, and binds chart, cycles and detail list.
513 updateTabs();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700514 }
515
516 @Override
517 public void onResume() {
518 super.onResume();
519
520 getView().post(new Runnable() {
521 @Override
522 public void run() {
523 highlightViewIfNeeded();
524 }
525 });
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700526
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700527 // kick off background task to update stats
528 new AsyncTask<Void, Void, Void>() {
529 @Override
530 protected Void doInBackground(Void... params) {
531 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700532 // wait a few seconds before kicking off
533 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700534 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700535 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700536 } catch (RemoteException e) {
537 }
538 return null;
539 }
540
541 @Override
542 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700543 if (isAdded()) {
544 updateBody();
545 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700546 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700547 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700548 }
549
Jeff Sharkey8a503642011-06-10 13:31:21 -0700550 @Override
551 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
552 inflater.inflate(R.menu.data_usage, menu);
553 }
554
555 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700556 public void onPrepareOptionsMenu(Menu menu) {
Jason Monk886978f2015-11-10 14:54:18 -0500557 final Context context = getContext();
Jason Monke7641652015-11-10 15:09:34 -0500558 if (context == null) return;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700559 final boolean appDetailMode = isAppDetailMode();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700560 final boolean isAdmin = mUserManager.isAdminUser();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700561
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700562 mMenuShowWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700563 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
564 mMenuShowWifi.setVisible(!appDetailMode);
565 } else {
566 mMenuShowWifi.setVisible(false);
567 }
568
569 mMenuShowEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
570 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
571 mMenuShowEthernet.setVisible(!appDetailMode);
572 } else {
573 mMenuShowEthernet.setVisible(false);
574 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700575
576 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700577 mMenuRestrictBackground.setVisible(
Xiaohui Chen44879a32015-07-22 13:53:22 -0700578 hasReadyMobileRadio(context) && isAdmin && !appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700579
580 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
581 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700582 metered.setVisible(!appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700583 } else {
584 metered.setVisible(false);
585 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700586
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700587 // TODO: show when multiple sims available
588 mMenuSimCards = menu.findItem(R.id.data_usage_menu_sim_cards);
589 mMenuSimCards.setVisible(false);
590
591 mMenuCellularNetworks = menu.findItem(R.id.data_usage_menu_cellular_networks);
PauloftheWesta4b8fb32014-09-18 10:12:25 -0700592 mMenuCellularNetworks.setVisible(hasReadyMobileRadio(context)
Xiaohui Chen44879a32015-07-22 13:53:22 -0700593 && !appDetailMode && isAdmin);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700594
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700595 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
596 String helpUrl;
597 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
Jason Monk6e613472015-06-16 10:31:38 -0400598 HelpUtils.prepareHelpMenuItem(getActivity(), help, helpUrl, getClass().getName());
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700599 } else {
600 help.setVisible(false);
601 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700602
603 updateMenuTitles();
604 }
605
606 private void updateMenuTitles() {
607 if (mPolicyManager.getRestrictBackground()) {
608 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_allow_background);
609 } else {
610 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_restrict_background);
611 }
612
613 if (mShowWifi) {
614 mMenuShowWifi.setTitle(R.string.data_usage_menu_hide_wifi);
615 } else {
616 mMenuShowWifi.setTitle(R.string.data_usage_menu_show_wifi);
617 }
618
619 if (mShowEthernet) {
620 mMenuShowEthernet.setTitle(R.string.data_usage_menu_hide_ethernet);
621 } else {
622 mMenuShowEthernet.setTitle(R.string.data_usage_menu_show_ethernet);
623 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700624 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700625
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700626 @Override
627 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700628 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700629 case R.id.data_usage_menu_restrict_background: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700630 final boolean restrictBackground = !mPolicyManager.getRestrictBackground();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700631 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800632 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700633 } else {
634 // no confirmation to drop restriction
635 setRestrictBackground(false);
636 }
637 return true;
638 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700639 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700640 mShowWifi = !mShowWifi;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700641 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700642 updateMenuTitles();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700643 updateTabs();
644 return true;
645 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700646 case R.id.data_usage_menu_show_ethernet: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700647 mShowEthernet = !mShowEthernet;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700648 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700649 updateMenuTitles();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700650 updateTabs();
651 return true;
652 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700653 case R.id.data_usage_menu_sim_cards: {
654 // TODO: hook up to sim cards
655 return true;
656 }
657 case R.id.data_usage_menu_cellular_networks: {
658 final Intent intent = new Intent(Intent.ACTION_MAIN);
659 intent.setComponent(new ComponentName("com.android.phone",
660 "com.android.phone.MobileNetworkSettings"));
661 startActivity(intent);
662 return true;
663 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700664 case R.id.data_usage_menu_metered: {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800665 final SettingsActivity sa = (SettingsActivity) getActivity();
666 sa.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700667 R.string.data_usage_metered_title, null, this, 0);
668 return true;
669 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700670 }
671 return false;
672 }
673
Jeff Sharkey94a90952011-06-13 22:31:09 -0700674 @Override
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700675 public void onDestroy() {
Jeff Sharkey94a90952011-06-13 22:31:09 -0700676 mDataEnabledView = null;
677 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700678
679 mUidDetailProvider.clearCache();
680 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700681
682 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700683
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800684 super.onDestroy();
685 }
686
Jeff Sharkey8a503642011-06-10 13:31:21 -0700687 /**
Jeff Sharkey92811822012-05-04 11:47:29 -0700688 * Build and assign {@link LayoutTransition} to various containers. Should
689 * only be assigned after initial layout is complete.
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700690 */
Jeff Sharkey92811822012-05-04 11:47:29 -0700691 private void ensureLayoutTransitions() {
Jason Monk4bb075b2015-03-17 10:06:58 -0400692 if (mShowAppImmediatePkg != null) {
693 // If we are skipping right to showing an app, we don't care about transitions.
694 return;
695 }
Jeff Sharkey92811822012-05-04 11:47:29 -0700696 // skip when already setup
697 if (mChart.getLayoutTransition() != null) return;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700698
Jeff Sharkey92811822012-05-04 11:47:29 -0700699 mTabsContainer.setLayoutTransition(buildLayoutTransition());
700 mHeader.setLayoutTransition(buildLayoutTransition());
701 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700702
Jeff Sharkey92811822012-05-04 11:47:29 -0700703 final LayoutTransition chartTransition = buildLayoutTransition();
704 chartTransition.disableTransitionType(LayoutTransition.APPEARING);
705 chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
706 mChart.setLayoutTransition(chartTransition);
707 }
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700708
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700709 private static LayoutTransition buildLayoutTransition() {
710 final LayoutTransition transition = new LayoutTransition();
711 if (TEST_ANIM) {
712 transition.setDuration(1500);
713 }
714 transition.setAnimateParentHierarchy(false);
715 return transition;
716 }
717
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700718 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700719 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700720 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
721 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700722 */
723 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700724 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700725 mTabHost.clearAllTabs();
726
Sanket Padawe7e21fa52015-02-12 12:39:34 -0800727 int simCount = mTelephonyManager.getSimCount();
728
Stuart Scottf51d0062015-04-08 09:40:48 -0700729 List<SubscriptionInfo> sirs = mSubscriptionManager.getActiveSubscriptionInfoList();
730 if (sirs != null) {
731 for (SubscriptionInfo sir : sirs) {
Sanket Padawe7e21fa52015-02-12 12:39:34 -0800732 addMobileTab(context, sir, (simCount > 1));
PauloftheWestc80b7612014-11-25 04:40:45 -0800733 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700734 }
PauloftheWestc80b7612014-11-25 04:40:45 -0800735
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700736 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700737 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
738 }
PauloftheWestc80b7612014-11-25 04:40:45 -0800739
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700740 if (mShowEthernet && hasEthernet(context)) {
741 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
742 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700743
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700744 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700745 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
746 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
747 if (mIntentTab != null) {
748 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700749 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700750 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700751 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700752 mTabHost.setCurrentTabByTag(mIntentTab);
753 }
754 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700755 } else if (noTabs) {
756 // no usable tabs, so hide body
757 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700758 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700759 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700760 }
761 }
762
Jeff Sharkey8a503642011-06-10 13:31:21 -0700763 /**
764 * Factory that provide empty {@link View} to make {@link TabHost} happy.
765 */
766 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700767 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700768 public View createTabContent(String tag) {
769 return new View(mTabHost.getContext());
770 }
771 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700772
Jeff Sharkey8a503642011-06-10 13:31:21 -0700773 /**
774 * Build {@link TabSpec} with thin indicator, and empty content.
775 */
776 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700777 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
778 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700779 }
780
PauloftheWest50e6eca2014-10-03 11:07:14 -0700781 /**
782 * Build {@link TabSpec} with thin indicator, and empty content.
783 */
Stuart Scottb1531812014-11-04 14:52:23 -0800784 private TabSpec buildTabSpec(String tag, CharSequence title) {
PauloftheWest50e6eca2014-10-03 11:07:14 -0700785 return mTabHost.newTabSpec(tag).setIndicator(title).setContent(
786 mEmptyTabContent);
787 }
788
789
Jeff Sharkey8a503642011-06-10 13:31:21 -0700790 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700791 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700792 public void onTabChanged(String tabId) {
793 // user changed tab; update body
794 updateBody();
795 }
796 };
797
798 /**
799 * Update body content based on current tab. Loads
800 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
801 * binds them to visible controls.
802 */
803 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700804 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700805 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700806
Jeff Sharkeya662e492011-06-18 21:57:06 -0700807 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700808 final String currentTab = mTabHost.getCurrentTabTag();
Xiaohui Chen44879a32015-07-22 13:53:22 -0700809 final boolean isAdmin = mUserManager.isAdminUser();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700810
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700811 if (currentTab == null) {
812 Log.w(TAG, "no tab selected; hiding body");
813 mListView.setVisibility(View.GONE);
814 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700815 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700816 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700817 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700818
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700819 mCurrentTab = currentTab;
820
Jeff Sharkey8a503642011-06-10 13:31:21 -0700821 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
822
Xiaohui Chen44879a32015-07-22 13:53:22 -0700823 mDataEnabledSupported = isAdmin;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700824 mDisableAtLimitSupported = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700825
Wink Savillefcec91f2014-12-02 17:12:08 -0800826 // TODO: remove mobile tabs when SIM isn't ready probably by
827 // TODO: using SubscriptionManager.getActiveSubscriptionInfoList.
828 if (LOGD) Log.d(TAG, "updateBody() isMobileTab=" + isMobileTab(currentTab));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700829
PauloftheWest50e6eca2014-10-03 11:07:14 -0700830 if (isMobileTab(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800831 if (LOGD) Log.d(TAG, "updateBody() mobile tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700832 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
833 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
PauloftheWest50e6eca2014-10-03 11:07:14 -0700834 mDataEnabledSupported = isMobileDataAvailable(getSubId(currentTab));
Jeff Sharkey4e2d16c2014-12-03 13:44:26 -0800835
836 // Match mobile traffic for this subscriber, but normalize it to
837 // catch any other merged subscribers.
838 mTemplate = buildTemplateMobileAll(
839 getActiveSubscriberId(context, getSubId(currentTab)));
840 mTemplate = NetworkTemplate.normalize(mTemplate,
841 mTelephonyManager.getMergedSubscriberIds());
842
Jeff Sharkey8a503642011-06-10 13:31:21 -0700843 } else if (TAB_3G.equals(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800844 if (LOGD) Log.d(TAG, "updateBody() 3g tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700845 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
846 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
847 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700848 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700849
850 } else if (TAB_4G.equals(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800851 if (LOGD) Log.d(TAG, "updateBody() 4g tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700852 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
853 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
854 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700855 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700856
857 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700858 // wifi doesn't have any controls
Wink Savillefcec91f2014-12-02 17:12:08 -0800859 if (LOGD) Log.d(TAG, "updateBody() wifi tab");
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700860 mDataEnabledSupported = false;
861 mDisableAtLimitSupported = false;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700862 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700863
864 } else if (TAB_ETHERNET.equals(currentTab)) {
865 // ethernet doesn't have any controls
Wink Savillefcec91f2014-12-02 17:12:08 -0800866 if (LOGD) Log.d(TAG, "updateBody() ethernet tab");
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700867 mDataEnabledSupported = false;
868 mDisableAtLimitSupported = false;
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700869 mTemplate = buildTemplateEthernet();
870
871 } else {
Wink Savillefcec91f2014-12-02 17:12:08 -0800872 if (LOGD) Log.d(TAG, "updateBody() unknown tab");
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700873 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700874 }
875
Sanket Padawe716b1f82015-06-24 14:26:21 -0700876 mPolicyEditor.read();
877 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
878 if (policy != null) {
879 final long currentTime = System.currentTimeMillis();
880 final long start = computeLastCycleBoundary(currentTime, policy);
881 final long end = currentTime;
882 long totalBytes = 0;
883
884 try {
885 totalBytes = mStatsService.getNetworkTotalBytes(policy.template, start, end);
886 } catch (RuntimeException e) {
887 } catch (RemoteException e) {
888 }
889
890 if (policy.isOverLimit(totalBytes) && policy.lastLimitSnooze < start) {
891 setPreferenceSummary(mDataEnabledView,
892 getString(R.string.data_usage_cellular_data_summary));
893 } else {
894 final TextView summary = (TextView) mDataEnabledView
895 .findViewById(android.R.id.summary);
896 summary.setVisibility(View.GONE);
897 }
898 }
899
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700900 // kick off loader for network history
901 // TODO: consider chaining two loaders together instead of reloading
902 // network history when showing app detail.
903 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700904 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700905
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700906 // detail mode can change visible menus, invalidate
907 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700908
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700909 mBinding = false;
PauloftheWestc80b7612014-11-25 04:40:45 -0800910
Alan Viverette55eaa962015-03-18 18:10:52 -0700911 int seriesColor = context.getColor(R.color.sim_noitification);
PauloftheWestc80b7612014-11-25 04:40:45 -0800912 if (mCurrentTab != null && mCurrentTab.length() > TAB_MOBILE.length() ){
913 final int slotId = Integer.parseInt(mCurrentTab.substring(TAB_MOBILE.length(),
914 mCurrentTab.length()));
Stuart Scottf51d0062015-04-08 09:40:48 -0700915 final SubscriptionInfo sir = mSubscriptionManager
916 .getActiveSubscriptionInfoForSimSlotIndex(slotId);
PauloftheWestc80b7612014-11-25 04:40:45 -0800917
918 if (sir != null) {
919 seriesColor = sir.getIconTint();
920 }
921 }
922
Jeff Sharkey9c167e92015-01-26 15:15:59 -0700923 final int secondaryColor = Color.argb(127, Color.red(seriesColor), Color.green(seriesColor),
924 Color.blue(seriesColor));
925 mSeries.setChartColor(Color.BLACK, seriesColor, secondaryColor);
926 mDetailedSeries.setChartColor(Color.BLACK, seriesColor, secondaryColor);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700927 }
928
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700929 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700930 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700931 }
932
933 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700934 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700935 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700936 */
937 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700938 final Context context = getActivity();
939 final PackageManager pm = context.getPackageManager();
940 final LayoutInflater inflater = getActivity().getLayoutInflater();
941
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700942 if (isAppDetailMode()) {
943 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700944 mCycleAdapter.setChangeVisible(false);
Julia Reynolds1740ce42015-07-27 16:00:06 -0400945 mChart.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700946 } else {
947 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700948 mCycleAdapter.setChangeVisible(true);
Julia Reynolds1740ce42015-07-27 16:00:06 -0400949 mChart.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700950
951 // hide detail stats when not in detail mode
952 mChart.bindDetailNetworkStats(null);
953 return;
954 }
955
956 // remove warning/limit sweeps while in detail mode
957 mChart.bindNetworkPolicy(null);
958
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700959 // show icon and all labels appearing under this app
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700960 final int uid = mCurrentApp.key;
961 final UidDetail detail = mUidDetailProvider.getUidDetail(uid, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700962 mAppIcon.setImageDrawable(detail.icon);
963
964 mAppTitles.removeAllViews();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700965
966 View title = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700967 if (detail.detailLabels != null) {
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100968 final int n = detail.detailLabels.length;
969 for (int i = 0; i < n; ++i) {
970 CharSequence label = detail.detailLabels[i];
971 CharSequence contentDescription = detail.detailContentDescriptions[i];
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700972 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100973 TextView appTitle = (TextView) title.findViewById(R.id.app_title);
974 appTitle.setText(label);
975 appTitle.setContentDescription(contentDescription);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700976 mAppTitles.addView(title);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700977 }
978 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700979 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100980 TextView appTitle = (TextView) title.findViewById(R.id.app_title);
981 appTitle.setText(detail.label);
982 appTitle.setContentDescription(detail.contentDescription);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700983 mAppTitles.addView(title);
984 }
985
986 // Remember last slot for summary
987 if (title != null) {
988 mAppTotal = (TextView) title.findViewById(R.id.app_summary);
989 } else {
990 mAppTotal = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700991 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700992
993 // enable settings button when package provides it
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700994 final String[] packageNames = pm.getPackagesForUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700995 if (packageNames != null && packageNames.length > 0) {
996 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700997 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
998
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700999 // Search for match across all packages
1000 boolean matchFound = false;
1001 for (String packageName : packageNames) {
1002 mAppSettingsIntent.setPackage(packageName);
1003 if (pm.resolveActivity(mAppSettingsIntent, 0) != null) {
1004 matchFound = true;
1005 break;
1006 }
1007 }
1008
Zoltan Szatmary-Banf20d39e2014-08-07 15:27:08 +01001009 mAppSettings.setOnClickListener(new OnClickListener() {
1010 @Override
1011 public void onClick(View v) {
1012 if (!isAdded()) {
1013 return;
1014 }
1015
1016 // TODO: target towards entire UID instead of just first package
1017 getActivity().startActivityAsUser(mAppSettingsIntent,
1018 new UserHandle(UserHandle.getUserId(uid)));
1019 }
1020 });
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001021 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -07001022 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001023
1024 } else {
1025 mAppSettingsIntent = null;
Zoltan Szatmary-Banf20d39e2014-08-07 15:27:08 +01001026 mAppSettings.setOnClickListener(null);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001027 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001028 }
1029
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001030 updateDetailData();
1031
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001032 if (UserHandle.isApp(uid) && !mPolicyManager.getRestrictBackground()
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001033 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001034 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -08001035 setPreferenceSummary(mAppRestrictView,
1036 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001037
1038 mAppRestrictView.setVisibility(View.VISIBLE);
1039 mAppRestrict.setChecked(getAppRestrictBackground());
1040
1041 } else {
1042 mAppRestrictView.setVisibility(View.GONE);
1043 }
1044 }
1045
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001046 private void setPolicyWarningBytes(long warningBytes) {
1047 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -07001048 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001049 updatePolicy(false);
1050 }
1051
1052 private void setPolicyLimitBytes(long limitBytes) {
1053 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -07001054 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001055 updatePolicy(false);
1056 }
1057
Wink Savilleb003a852014-10-23 10:16:26 -07001058 private boolean isMobileDataEnabled(int subId) {
Wink Savillea4f4d182014-12-05 15:34:46 -08001059 if (LOGD) Log.d(TAG, "isMobileDataEnabled:+ subId=" + subId);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001060 boolean isEnable = false;
Sanket Padawed819270f2015-01-14 11:03:33 -08001061 if (mMobileDataEnabled.get(String.valueOf(subId)) != null) {
1062 //TODO: deprecate and remove this once enabled flag is on policy
1063 //Multiple Subscriptions, the value need to be reseted
1064 isEnable = mMobileDataEnabled.get(String.valueOf(subId)).booleanValue();
Wink Savillea4f4d182014-12-05 15:34:46 -08001065 if (LOGD) {
1066 Log.d(TAG, "isMobileDataEnabled: != null, subId=" + subId
1067 + " isEnable=" + isEnable);
1068 }
Sanket Padawed819270f2015-01-14 11:03:33 -08001069 mMobileDataEnabled.put(String.valueOf(subId), null);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001070 } else {
Wink Savillea4f4d182014-12-05 15:34:46 -08001071 // SUB SELECT
1072 isEnable = mTelephonyManager.getDataEnabled(subId);
1073 if (LOGD) {
1074 Log.d(TAG, "isMobileDataEnabled: == null, subId=" + subId
1075 + " isEnable=" + isEnable);
1076 }
Jeff Sharkey28130d92011-09-02 16:10:24 -07001077 }
PauloftheWest50e6eca2014-10-03 11:07:14 -07001078 return isEnable;
Jeff Sharkey28130d92011-09-02 16:10:24 -07001079 }
1080
Sanket Padawe24f834d2015-01-08 11:23:11 -08001081 private void setMobileDataEnabled(int subId, boolean enabled) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001082 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
Sanket Padawe24f834d2015-01-08 11:23:11 -08001083 mTelephonyManager.setDataEnabled(subId, enabled);
Sanket Padawed819270f2015-01-14 11:03:33 -08001084 mMobileDataEnabled.put(String.valueOf(subId), enabled);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001085 updatePolicy(false);
1086 }
1087
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001088 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001089 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked()
Xiaohui Chen44879a32015-07-22 13:53:22 -07001090 && mUserManager.isAdminUser();
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001091 }
1092
1093 private boolean isBandwidthControlEnabled() {
1094 try {
1095 return mNetworkService.isBandwidthControlEnabled();
1096 } catch (RemoteException e) {
1097 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
1098 return false;
1099 }
1100 }
1101
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001102 public void setRestrictBackground(boolean restrictBackground) {
1103 mPolicyManager.setRestrictBackground(restrictBackground);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001104 updateMenuTitles();
Jason Monkc6858532015-12-16 12:58:39 -05001105 ConditionManager.get(getContext()).getCondition(BackgroundDataCondition.class)
1106 .refreshState();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001107 }
1108
1109 private boolean getAppRestrictBackground() {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001110 final int uid = mCurrentApp.key;
1111 final int uidPolicy = mPolicyManager.getUidPolicy(uid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001112 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
1113 }
1114
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001115 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001116 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001117 final int uid = mCurrentApp.key;
1118 mPolicyManager.setUidPolicy(
1119 uid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001120 mAppRestrict.setChecked(restrictBackground);
1121 }
1122
Jeff Sharkey8a503642011-06-10 13:31:21 -07001123 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001124 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
1125 * current {@link #mTemplate}.
1126 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001127 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001128 boolean dataEnabledVisible = mDataEnabledSupported;
1129 boolean disableAtLimitVisible = mDisableAtLimitSupported;
1130
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001131 if (isAppDetailMode()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001132 dataEnabledVisible = false;
1133 disableAtLimitVisible = false;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001134 }
1135
Jeff Sharkey28130d92011-09-02 16:10:24 -07001136 // TODO: move enabled state directly into policy
PauloftheWest50e6eca2014-10-03 11:07:14 -07001137 if (isMobileTab(mCurrentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001138 mBinding = true;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001139 mDataEnabled.setChecked(isMobileDataEnabled(getSubId(mCurrentTab)));
Jeff Sharkey28130d92011-09-02 16:10:24 -07001140 mBinding = false;
1141 }
1142
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001143 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001144 //SUB SELECT
1145 if (isNetworkPolicyModifiable(policy) && isMobileDataAvailable(getSubId(mCurrentTab))) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001146 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001147 if (!isAppDetailMode()) {
1148 mChart.bindNetworkPolicy(policy);
1149 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001150
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001151 } else {
1152 // controls are disabled; don't bind warning/limit sweeps
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001153 disableAtLimitVisible = false;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001154 mChart.bindNetworkPolicy(null);
1155 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001156
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001157 mDataEnabledView.setVisibility(dataEnabledVisible ? View.VISIBLE : View.GONE);
1158 mDisableAtLimitView.setVisibility(disableAtLimitVisible ? View.VISIBLE : View.GONE);
1159
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001160 if (refreshCycle) {
1161 // generate cycle list based on policy and available history
1162 updateCycleList(policy);
1163 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001164 }
1165
1166 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001167 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
1168 * and available {@link NetworkStatsHistory} data. Always selects the newest
1169 * item, updating the inspection range on {@link #mChart}.
1170 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001171 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001172 // stash away currently selected cycle to try restoring below
1173 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -07001174 mCycleAdapter.clear();
1175
1176 final Context context = mCycleSpinner.getContext();
Jeff Sharkey02551ec2015-10-19 17:22:05 -07001177 NetworkStatsHistory.Entry entry = null;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001178
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001179 long historyStart = Long.MAX_VALUE;
1180 long historyEnd = Long.MIN_VALUE;
1181 if (mChartData != null) {
1182 historyStart = mChartData.network.getStart();
1183 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001184 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001185
Jeff Sharkey461842a2011-09-25 18:22:48 -07001186 final long now = System.currentTimeMillis();
1187 if (historyStart == Long.MAX_VALUE) historyStart = now;
1188 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001189
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001190 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001191 if (policy != null) {
1192 // find the next cycle boundary
1193 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001194
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001195 // walk backwards, generating all valid cycle ranges
1196 while (cycleEnd > historyStart) {
1197 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
1198 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
1199 + historyStart);
Jeff Sharkey02551ec2015-10-19 17:22:05 -07001200
1201 final boolean includeCycle;
1202 if (mChartData != null) {
1203 entry = mChartData.network.getValues(cycleStart, cycleEnd, entry);
1204 includeCycle = (entry.rxBytes + entry.txBytes) > 0;
1205 } else {
1206 includeCycle = true;
1207 }
1208
1209 if (includeCycle) {
1210 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1211 hasCycles = true;
1212 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001213 cycleEnd = cycleStart;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001214 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001215
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001216 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001217 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001218 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001219
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001220 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001221 // no policy defined cycles; show entry for each four-week period
1222 long cycleEnd = historyEnd;
1223 while (cycleEnd > historyStart) {
1224 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
Jeff Sharkey02551ec2015-10-19 17:22:05 -07001225
1226 final boolean includeCycle;
1227 if (mChartData != null) {
1228 entry = mChartData.network.getValues(cycleStart, cycleEnd, entry);
1229 includeCycle = (entry.rxBytes + entry.txBytes) > 0;
1230 } else {
1231 includeCycle = true;
1232 }
1233
1234 if (includeCycle) {
1235 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1236 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001237 cycleEnd = cycleStart;
1238 }
1239
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001240 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001241 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001242
1243 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001244 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001245 final int position = mCycleAdapter.findNearestPosition(previousItem);
1246 mCycleSpinner.setSelection(position);
1247
1248 // only force-update cycle when changed; skipping preserves any
1249 // user-defined inspection region.
1250 final CycleItem selectedItem = mCycleAdapter.getItem(position);
1251 if (!Objects.equal(selectedItem, previousItem)) {
1252 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1253 } else {
1254 // but still kick off loader for detailed list
1255 updateDetailData();
1256 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001257 } else {
1258 updateDetailData();
1259 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001260 }
1261
Sanket Padawed819270f2015-01-14 11:03:33 -08001262 private void disableDataForOtherSubscriptions(SubscriptionInfo currentSir) {
1263 if (mSubInfoList != null) {
1264 for (SubscriptionInfo subInfo : mSubInfoList) {
1265 if (subInfo.getSubscriptionId() != currentSir.getSubscriptionId()) {
1266 setMobileDataEnabled(subInfo.getSubscriptionId(), false);
1267 }
1268 }
1269 }
1270 }
1271
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001272 private View.OnClickListener mDataEnabledListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001273 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001274 public void onClick(View v) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001275 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001276
Chris Wren1b6ffba2015-05-08 17:10:01 -04001277 final boolean enabled = !mDataEnabled.isChecked();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001278 final String currentTab = mCurrentTab;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001279 if (isMobileTab(currentTab)) {
Chris Wren1b6ffba2015-05-08 17:10:01 -04001280 MetricsLogger.action(getContext(), MetricsLogger.ACTION_CELL_DATA_TOGGLE, enabled);
1281 if (enabled) {
PauloftheWestde338442014-10-29 14:46:44 -07001282 // If we are showing the Sim Card tile then we are a Multi-Sim device.
1283 if (Utils.showSimCardTile(getActivity())) {
1284 handleMultiSimDataDialog();
1285 } else {
Sanket Padawe24f834d2015-01-08 11:23:11 -08001286 setMobileDataEnabled(getSubId(currentTab), true);
PauloftheWestde338442014-10-29 14:46:44 -07001287 }
Jeff Sharkey28130d92011-09-02 16:10:24 -07001288 } else {
1289 // disabling data; show confirmation dialog which eventually
1290 // calls setMobileDataEnabled() once user confirms.
Sanket Padawe24f834d2015-01-08 11:23:11 -08001291 ConfirmDataDisableFragment.show(DataUsageSummary.this, getSubId(mCurrentTab));
Jeff Sharkey28130d92011-09-02 16:10:24 -07001292 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001293 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001294
Jeff Sharkey28130d92011-09-02 16:10:24 -07001295 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001296 }
1297 };
1298
PauloftheWestde338442014-10-29 14:46:44 -07001299 private void handleMultiSimDataDialog() {
PauloftheWestf0d84e22014-12-10 08:50:54 -08001300 final Context context = getActivity();
1301 final SubscriptionInfo currentSir = getCurrentTabSubInfo(context);
Sanket Padawe766ab602015-01-20 10:36:15 -08001302
1303 //If sim has not loaded after toggling data switch, return.
1304 if (currentSir == null) {
1305 return;
1306 }
1307
Stuart Scottf51d0062015-04-08 09:40:48 -07001308 final SubscriptionInfo nextSir = mSubscriptionManager.getDefaultDataSubscriptionInfo();
PauloftheWestde338442014-10-29 14:46:44 -07001309
PauloftheWestf0d84e22014-12-10 08:50:54 -08001310 // If the device is single SIM or is enabling data on the active data SIM then forgo
1311 // the pop-up.
1312 if (!Utils.showSimCardTile(context) ||
Sanket Padawed819270f2015-01-14 11:03:33 -08001313 (nextSir != null && currentSir != null &&
1314 currentSir.getSubscriptionId() == nextSir.getSubscriptionId())) {
Sanket Padawe24f834d2015-01-08 11:23:11 -08001315 setMobileDataEnabled(currentSir.getSubscriptionId(), true);
Sanket Padawed819270f2015-01-14 11:03:33 -08001316 if (nextSir != null && currentSir != null &&
1317 currentSir.getSubscriptionId() == nextSir.getSubscriptionId()) {
1318 disableDataForOtherSubscriptions(currentSir);
1319 }
PauloftheWestde338442014-10-29 14:46:44 -07001320 updateBody();
1321 return;
1322 }
1323
PauloftheWestf0d84e22014-12-10 08:50:54 -08001324 final String previousName = (nextSir == null)
1325 ? context.getResources().getString(R.string.sim_selection_required_pref)
1326 : nextSir.getDisplayName().toString();
1327
PauloftheWestde338442014-10-29 14:46:44 -07001328 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
1329
1330 builder.setTitle(R.string.sim_change_data_title);
1331 builder.setMessage(getActivity().getResources().getString(R.string.sim_change_data_message,
PauloftheWestf0d84e22014-12-10 08:50:54 -08001332 currentSir.getDisplayName(), previousName));
PauloftheWestde338442014-10-29 14:46:44 -07001333
1334 builder.setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
1335 @Override
1336 public void onClick(DialogInterface dialog, int id) {
Wink Saville0183fb52014-11-22 10:11:39 -08001337 mSubscriptionManager.setDefaultDataSubId(currentSir.getSubscriptionId());
Sanket Padawe24f834d2015-01-08 11:23:11 -08001338 setMobileDataEnabled(currentSir.getSubscriptionId(), true);
Sanket Padawed819270f2015-01-14 11:03:33 -08001339 disableDataForOtherSubscriptions(currentSir);
PauloftheWestde338442014-10-29 14:46:44 -07001340 updateBody();
1341 }
1342 });
1343 builder.setNegativeButton(R.string.cancel, null);
1344
1345 builder.create().show();
1346 }
1347
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001348 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001349 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001350 public void onClick(View v) {
1351 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001352 if (disableAtLimit) {
1353 // enabling limit; show confirmation dialog which eventually
1354 // calls setPolicyLimitBytes() once user confirms.
1355 ConfirmLimitFragment.show(DataUsageSummary.this);
1356 } else {
1357 setPolicyLimitBytes(LIMIT_DISABLED);
1358 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001359 }
1360 };
1361
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001362 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001363 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001364 public void onClick(View v) {
1365 final boolean restrictBackground = !mAppRestrict.isChecked();
1366
1367 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001368 // enabling restriction; show confirmation dialog which
1369 // eventually calls setRestrictBackground() once user
1370 // confirms.
1371 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001372 } else {
1373 setAppRestrictBackground(false);
1374 }
1375 }
1376 };
1377
Jeff Sharkey8a503642011-06-10 13:31:21 -07001378 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001379 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001380 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001381 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001382 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001383
1384 // TODO: sigh, remove this hack once we understand 6450986
1385 if (mUidDetailProvider == null || app == null) return;
1386
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001387 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001388 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001389 }
1390 };
1391
1392 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001393 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001394 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1395 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1396 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001397 // show cycle editor; will eventually call setPolicyCycleDay()
1398 // when user finishes editing.
1399 CycleEditorFragment.show(DataUsageSummary.this);
1400
1401 // reset spinner to something other than "change cycle..."
1402 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001403
1404 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001405 if (LOGD) {
1406 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1407 + cycle.end + "]");
1408 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001409
1410 // update chart to show selected cycle, and update detail data
1411 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001412 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001413
1414 updateDetailData();
1415 }
1416 }
1417
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001418 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001419 public void onNothingSelected(AdapterView<?> parent) {
1420 // ignored
1421 }
1422 };
1423
1424 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001425 * Update details based on {@link #mChart} inspection range depending on
1426 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1427 * of applications data usage, and when {@link #isAppDetailMode()} update
1428 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001429 */
1430 private void updateDetailData() {
1431 if (LOGD) Log.d(TAG, "updateDetailData()");
1432
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001433 final long start = mChart.getInspectStart();
1434 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001435 final long now = System.currentTimeMillis();
1436
1437 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001438
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001439 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001440 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001441 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001442 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001443 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001444 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001445 final long foregroundBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001446 final long totalBytes = defaultBytes + foregroundBytes;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001447
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001448 if (mAppTotal != null) {
1449 mAppTotal.setText(Formatter.formatFileSize(context, totalBytes));
1450 }
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001451 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1452 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1453
1454 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001455 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001456
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001457 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001458
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001459 mCycleSummary.setVisibility(View.GONE);
1460
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001461 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001462 if (mChartData != null) {
1463 entry = mChartData.network.getValues(start, end, now, null);
1464 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001465
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001466 mCycleSummary.setVisibility(View.VISIBLE);
1467
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001468 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001469 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001470 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001471 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001472
1473 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1474 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001475 mCycleSummary.setText(totalPhrase);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001476
PauloftheWest50e6eca2014-10-03 11:07:14 -07001477 if (isMobileTab(mCurrentTab) || TAB_3G.equals(mCurrentTab)
László Dávid0b8bf4e2012-10-24 23:31:47 +02001478 || TAB_4G.equals(mCurrentTab)) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001479 if (isAppDetailMode()) {
1480 mDisclaimer.setVisibility(View.GONE);
1481 } else {
1482 mDisclaimer.setVisibility(View.VISIBLE);
1483 }
Jeff Sharkeye557c332012-04-13 16:04:07 -07001484 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001485 mDisclaimer.setVisibility(View.GONE);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001486 }
1487
Jeff Sharkey92811822012-05-04 11:47:29 -07001488 // initial layout is finished above, ensure we have transitions
1489 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001490 }
1491
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001492 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1493 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001494 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001495 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001496 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001497 }
1498
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001499 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001500 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1501 mChartData = data;
1502 mChart.bindNetworkStats(mChartData.network);
1503 mChart.bindDetailNetworkStats(mChartData.detail);
1504
1505 // calcuate policy cycles based on available data
1506 updatePolicy(true);
1507 updateAppDetail();
1508
1509 // force scroll to top of body when showing detail
1510 if (mChartData.detail != null) {
1511 mListView.smoothScrollToPosition(0);
1512 }
1513 }
1514
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001515 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001516 public void onLoaderReset(Loader<ChartData> loader) {
1517 mChartData = null;
1518 mChart.bindNetworkStats(null);
1519 mChart.bindDetailNetworkStats(null);
1520 }
1521 };
1522
1523 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001524 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001525 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001526 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001527 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001528 }
1529
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001530 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001531 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001532 final int[] restrictedUids = mPolicyManager.getUidsWithPolicy(
Jeff Sharkeye557c332012-04-13 16:04:07 -07001533 POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001534 mAdapter.bindStats(data, restrictedUids);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001535 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001536 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001537
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001538 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001539 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001540 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001541 updateEmptyVisible();
1542 }
1543
1544 private void updateEmptyVisible() {
1545 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1546 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001547 mStupidPadding.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001548 }
1549 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001550
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001551 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001552 final TelephonyManager tele = TelephonyManager.from(context);
1553 final String actualSubscriberId = tele.getSubscriberId();
Wink Savillefcec91f2014-12-02 17:12:08 -08001554 String retVal = SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
1555 if (LOGD) Log.d(TAG, "getActiveSubscriberId=" + retVal + " actualSubscriberId=" + actualSubscriberId);
1556 return retVal;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001557 }
1558
Wink Savilleb003a852014-10-23 10:16:26 -07001559 private static String getActiveSubscriberId(Context context, int subId) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07001560 final TelephonyManager tele = TelephonyManager.from(context);
Wink Savillefcec91f2014-12-02 17:12:08 -08001561 String retVal = tele.getSubscriberId(subId);
1562 if (LOGD) Log.d(TAG, "getActiveSubscriberId=" + retVal + " subId=" + subId);
1563 return retVal;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001564 }
1565
Jeff Sharkey8a503642011-06-10 13:31:21 -07001566 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001567 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001568 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001569 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001570 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001571
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001572 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001573 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001574 setPolicyLimitBytes(mChart.getLimitBytes());
Sanket Padawe716b1f82015-06-24 14:26:21 -07001575 updateBody();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001576 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001577
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001578 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001579 public void requestWarningEdit() {
1580 WarningEditorFragment.show(DataUsageSummary.this);
1581 }
1582
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001583 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001584 public void requestLimitEdit() {
1585 LimitEditorFragment.show(DataUsageSummary.this);
1586 }
1587 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001588
1589 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001590 * List item that reflects a specific data usage cycle.
1591 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001592 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001593 public CharSequence label;
1594 public long start;
1595 public long end;
1596
Jeff Sharkey8a503642011-06-10 13:31:21 -07001597 CycleItem(CharSequence label) {
1598 this.label = label;
1599 }
1600
1601 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001602 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001603 this.start = start;
1604 this.end = end;
1605 }
1606
Jeff Sharkey8a503642011-06-10 13:31:21 -07001607 @Override
1608 public String toString() {
1609 return label.toString();
1610 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001611
1612 @Override
1613 public boolean equals(Object o) {
1614 if (o instanceof CycleItem) {
1615 final CycleItem another = (CycleItem) o;
1616 return start == another.start && end == another.end;
1617 }
1618 return false;
1619 }
1620
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001621 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001622 public int compareTo(CycleItem another) {
1623 return Long.compare(start, another.start);
1624 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001625 }
1626
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001627 private static final StringBuilder sBuilder = new StringBuilder(50);
1628 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1629 sBuilder, Locale.getDefault());
1630
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001631 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001632 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001633
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001634 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001635 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001636 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1637 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001638 }
1639 }
1640
Jeff Sharkey8a503642011-06-10 13:31:21 -07001641 /**
1642 * Special-case data usage cycle that triggers dialog to change
1643 * {@link NetworkPolicy#cycleDay}.
1644 */
1645 public static class CycleChangeItem extends CycleItem {
1646 public CycleChangeItem(Context context) {
1647 super(context.getString(R.string.data_usage_change_cycle));
1648 }
1649 }
1650
1651 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001652 private boolean mChangePossible = false;
1653 private boolean mChangeVisible = false;
1654
1655 private final CycleChangeItem mChangeItem;
1656
Jeff Sharkey8a503642011-06-10 13:31:21 -07001657 public CycleAdapter(Context context) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001658 super(context, R.layout.data_usage_cycle_item);
1659 setDropDownViewResource(R.layout.data_usage_cycle_item_dropdown);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001660 mChangeItem = new CycleChangeItem(context);
1661 }
1662
1663 public void setChangePossible(boolean possible) {
1664 mChangePossible = possible;
1665 updateChange();
1666 }
1667
1668 public void setChangeVisible(boolean visible) {
1669 mChangeVisible = visible;
1670 updateChange();
1671 }
1672
1673 private void updateChange() {
1674 remove(mChangeItem);
1675 if (mChangePossible && mChangeVisible) {
1676 add(mChangeItem);
1677 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001678 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001679
1680 /**
1681 * Find position of {@link CycleItem} in this adapter which is nearest
1682 * the given {@link CycleItem}.
1683 */
1684 public int findNearestPosition(CycleItem target) {
1685 if (target != null) {
1686 final int count = getCount();
1687 for (int i = count - 1; i >= 0; i--) {
1688 final CycleItem item = getItem(i);
1689 if (item instanceof CycleChangeItem) {
1690 continue;
1691 } else if (item.compareTo(target) >= 0) {
1692 return i;
1693 }
1694 }
1695 }
1696 return 0;
1697 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001698 }
1699
1700 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001701 * Adapter of applications, sorted by total usage descending.
1702 */
1703 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001704 private final UidDetailProvider mProvider;
1705 private final int mInsetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001706 private final UserManager mUm;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001707
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001708 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001709 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001710
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001711 public DataUsageAdapter(final UserManager userManager, UidDetailProvider provider, int insetSide) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001712 mProvider = checkNotNull(provider);
1713 mInsetSide = insetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001714 mUm = userManager;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001715 }
1716
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001717 /**
1718 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1719 */
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001720 public void bindStats(NetworkStats stats, int[] restrictedUids) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001721 mItems.clear();
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001722 mLargest = 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001723
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001724 final int currentUserId = ActivityManager.getCurrentUser();
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001725 final List<UserHandle> profiles = mUm.getUserProfiles();
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001726 final SparseArray<AppItem> knownItems = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001727
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001728 NetworkStats.Entry entry = null;
1729 final int size = stats != null ? stats.size() : 0;
1730 for (int i = 0; i < size; i++) {
1731 entry = stats.getValues(i, entry);
1732
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001733 // Decide how to collapse items together
1734 final int uid = entry.uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001735
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001736 final int collapseKey;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001737 final int category;
1738 final int userId = UserHandle.getUserId(uid);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001739 if (UserHandle.isApp(uid)) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001740 if (profiles.contains(new UserHandle(userId))) {
1741 if (userId != currentUserId) {
1742 // Add to a managed user item.
1743 final int managedKey = UidDetailProvider.buildKeyForUser(userId);
1744 accumulate(managedKey, knownItems, entry,
1745 AppItem.CATEGORY_USER);
1746 }
1747 // Add to app item.
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001748 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001749 category = AppItem.CATEGORY_APP;
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001750 } else {
Zoltan Szatmary-Ban77c1d362014-12-12 18:48:03 +00001751 // If it is a removed user add it to the removed users' key
1752 final UserInfo info = mUm.getUserInfo(userId);
1753 if (info == null) {
1754 collapseKey = UID_REMOVED;
1755 category = AppItem.CATEGORY_APP;
1756 } else {
1757 // Add to other user item.
1758 collapseKey = UidDetailProvider.buildKeyForUser(userId);
1759 category = AppItem.CATEGORY_USER;
1760 }
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001761 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001762 } else if (uid == UID_REMOVED || uid == UID_TETHERING) {
1763 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001764 category = AppItem.CATEGORY_APP;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001765 } else {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001766 collapseKey = android.os.Process.SYSTEM_UID;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001767 category = AppItem.CATEGORY_APP;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001768 }
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001769 accumulate(collapseKey, knownItems, entry, category);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001770 }
1771
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001772 final int restrictedUidsMax = restrictedUids.length;
1773 for (int i = 0; i < restrictedUidsMax; ++i) {
1774 final int uid = restrictedUids[i];
1775 // Only splice in restricted state for current user or managed users
1776 if (!profiles.contains(new UserHandle(UserHandle.getUserId(uid)))) {
1777 continue;
1778 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001779
1780 AppItem item = knownItems.get(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001781 if (item == null) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001782 item = new AppItem(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001783 item.total = -1;
1784 mItems.add(item);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001785 knownItems.put(item.key, item);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001786 }
1787 item.restricted = true;
1788 }
1789
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001790 if (!mItems.isEmpty()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001791 final AppItem title = new AppItem();
1792 title.category = AppItem.CATEGORY_APP_TITLE;
1793 mItems.add(title);
1794 }
1795
Jeff Sharkey8a503642011-06-10 13:31:21 -07001796 Collections.sort(mItems);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001797 notifyDataSetChanged();
1798 }
1799
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001800 /**
1801 * Accumulate data usage of a network stats entry for the item mapped by the collapse key.
1802 * Creates the item if needed.
1803 *
1804 * @param collapseKey the collapse key used to map the item.
1805 * @param knownItems collection of known (already existing) items.
1806 * @param entry the network stats entry to extract data usage from.
1807 * @param itemCategory the item is categorized on the list view by this category. Must be
1808 * either AppItem.APP_ITEM_CATEGORY or AppItem.MANAGED_USER_ITEM_CATEGORY
1809 */
1810 private void accumulate(int collapseKey, final SparseArray<AppItem> knownItems,
1811 NetworkStats.Entry entry, int itemCategory) {
1812 final int uid = entry.uid;
1813 AppItem item = knownItems.get(collapseKey);
1814 if (item == null) {
1815 item = new AppItem(collapseKey);
1816 item.category = itemCategory;
1817 mItems.add(item);
1818 knownItems.put(item.key, item);
1819 }
1820 item.addUid(uid);
1821 item.total += entry.rxBytes + entry.txBytes;
1822 if (mLargest < item.total) {
1823 mLargest = item.total;
1824 }
1825 }
1826
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001827 @Override
1828 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001829 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001830 }
1831
1832 @Override
1833 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001834 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001835 }
1836
1837 @Override
1838 public long getItemId(int position) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001839 return mItems.get(position).key;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001840 }
1841
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001842 /**
1843 * See {@link #getItemViewType} for the view types.
1844 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001845 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001846 public int getViewTypeCount() {
1847 return 2;
1848 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001849
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001850 /**
1851 * Returns 1 for separator items and 0 for anything else.
1852 */
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001853 @Override
1854 public int getItemViewType(int position) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001855 final AppItem item = mItems.get(position);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001856 if (item.category == AppItem.CATEGORY_APP_TITLE) {
1857 return 1;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001858 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001859 return 0;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001860 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001861 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001862
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001863 @Override
1864 public boolean areAllItemsEnabled() {
1865 return false;
1866 }
1867
1868 @Override
1869 public boolean isEnabled(int position) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001870 if (position > mItems.size()) {
1871 throw new ArrayIndexOutOfBoundsException();
1872 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001873 return getItemViewType(position) == 0;
1874 }
1875
1876 @Override
1877 public View getView(int position, View convertView, ViewGroup parent) {
1878 final AppItem item = mItems.get(position);
Jason Monk2cdafc62015-06-12 12:32:50 -04001879 LayoutInflater inflater = LayoutInflater.from(parent.getContext());
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001880 if (getItemViewType(position) == 1) {
1881 if (convertView == null) {
Jason Monk2cdafc62015-06-12 12:32:50 -04001882 convertView = Utils.inflateCategoryHeader(inflater, parent);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001883 }
1884
1885 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
1886 title.setText(R.string.data_usage_app);
1887
1888 } else {
1889 if (convertView == null) {
Jason Monk2cdafc62015-06-12 12:32:50 -04001890 convertView = inflater.inflate(R.layout.data_usage_item, parent, false);
1891 inflater.inflate(R.layout.widget_progress_bar,
1892 (ViewGroup) convertView.findViewById(android.R.id.widget_frame));
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001893
1894 if (mInsetSide > 0) {
1895 convertView.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
1896 }
1897 }
1898
1899 final Context context = parent.getContext();
1900
Jason Monk2cdafc62015-06-12 12:32:50 -04001901 final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001902 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1903 android.R.id.progress);
1904
1905 // kick off async load of app details
1906 UidDetailTask.bindView(mProvider, item, convertView);
1907
1908 if (item.restricted && item.total <= 0) {
Jason Monk2cdafc62015-06-12 12:32:50 -04001909 summary.setText(R.string.data_usage_app_restricted);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001910 progress.setVisibility(View.GONE);
1911 } else {
Jason Monk2cdafc62015-06-12 12:32:50 -04001912 summary.setText(Formatter.formatFileSize(context, item.total));
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001913 progress.setVisibility(View.VISIBLE);
1914 }
1915
1916 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1917 progress.setProgress(percentTotal);
1918 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001919
1920 return convertView;
1921 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001922 }
1923
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001924 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001925 * Empty {@link Fragment} that controls display of UID details in
1926 * {@link DataUsageSummary}.
1927 */
1928 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001929 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001930
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001931 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jason Monk4bb075b2015-03-17 10:06:58 -04001932 show(parent, app, label, true);
1933 }
1934
1935 public static void show(DataUsageSummary parent, AppItem app, CharSequence label,
1936 boolean addToBack) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001937 if (!parent.isAdded()) return;
1938
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001939 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001940 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001941
1942 final AppDetailsFragment fragment = new AppDetailsFragment();
1943 fragment.setArguments(args);
1944 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001945 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1946 ft.add(fragment, TAG_APP_DETAILS);
Jason Monk4bb075b2015-03-17 10:06:58 -04001947 if (addToBack) {
1948 ft.addToBackStack(TAG_APP_DETAILS);
1949 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001950 ft.setBreadCrumbTitle(
1951 parent.getResources().getString(R.string.data_usage_app_summary_title));
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001952 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001953 }
1954
1955 @Override
1956 public void onStart() {
1957 super.onStart();
1958 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001959 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001960 target.updateBody();
1961 }
1962
1963 @Override
1964 public void onStop() {
1965 super.onStop();
1966 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001967 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001968 target.updateBody();
1969 }
Julia Reynolds1740ce42015-07-27 16:00:06 -04001970
1971 @Override
1972 public boolean onOptionsItemSelected(MenuItem item) {
1973 switch (item.getItemId()) {
1974 case android.R.id.home:
1975 getFragmentManager().popBackStack();
1976 return true;
1977 }
1978 return super.onOptionsItemSelected(item);
1979 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001980 }
1981
1982 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001983 * Dialog to request user confirmation before setting
1984 * {@link NetworkPolicy#limitBytes}.
1985 */
1986 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001987 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001988 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001989
1990 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001991 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001992
Jeff Sharkeyd0a55312014-08-08 10:25:24 -07001993 final NetworkPolicy policy = parent.mPolicyEditor.getPolicy(parent.mTemplate);
1994 if (policy == null) return;
1995
Jeff Sharkey461842a2011-09-25 18:22:48 -07001996 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001997 final CharSequence message;
Jeff Sharkeyd0a55312014-08-08 10:25:24 -07001998 final long minLimitBytes = (long) (policy.warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001999 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002000
2001 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002002 final String currentTab = parent.mCurrentTab;
2003 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07002004 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07002005 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002006 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07002007 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07002008 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002009 } else if (isMobileTab(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07002010 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07002011 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07002012 } else {
2013 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002014 }
2015
Jeff Sharkey131f9d62011-08-17 17:08:19 -07002016 final Bundle args = new Bundle();
2017 args.putCharSequence(EXTRA_MESSAGE, message);
2018 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
2019
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002020 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
2021 dialog.setArguments(args);
2022 dialog.setTargetFragment(parent, 0);
2023 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
2024 }
2025
2026 @Override
2027 public Dialog onCreateDialog(Bundle savedInstanceState) {
2028 final Context context = getActivity();
2029
Jeff Sharkey131f9d62011-08-17 17:08:19 -07002030 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002031 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
2032
2033 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2034 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07002035 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002036
2037 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002038 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002039 public void onClick(DialogInterface dialog, int which) {
2040 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2041 if (target != null) {
2042 target.setPolicyLimitBytes(limitBytes);
2043 }
2044 }
2045 });
2046
2047 return builder.create();
2048 }
2049 }
2050
2051 /**
2052 * Dialog to edit {@link NetworkPolicy#cycleDay}.
2053 */
2054 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002055 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002056
2057 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002058 if (!parent.isAdded()) return;
2059
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002060 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002061 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002062
2063 final CycleEditorFragment dialog = new CycleEditorFragment();
2064 dialog.setArguments(args);
2065 dialog.setTargetFragment(parent, 0);
2066 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
2067 }
2068
2069 @Override
2070 public Dialog onCreateDialog(Bundle savedInstanceState) {
2071 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002072 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2073 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002074
2075 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2076 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2077
2078 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
2079 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
2080
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002081 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2082 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002083
2084 cycleDayPicker.setMinValue(1);
2085 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002086 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002087 cycleDayPicker.setWrapSelectorWheel(true);
2088
2089 builder.setTitle(R.string.data_usage_cycle_editor_title);
2090 builder.setView(view);
2091
2092 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2093 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002094 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002095 public void onClick(DialogInterface dialog, int which) {
Jeff Sharkeyd277de92013-03-25 15:11:25 -07002096 // clear focus to finish pending text edits
2097 cycleDayPicker.clearFocus();
2098
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002099 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08002100 final String cycleTimezone = new Time().timezone;
2101 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002102 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002103 }
2104 });
2105
2106 return builder.create();
2107 }
2108 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002109
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002110 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002111 * Dialog to edit {@link NetworkPolicy#warningBytes}.
2112 */
2113 public static class WarningEditorFragment extends DialogFragment {
2114 private static final String EXTRA_TEMPLATE = "template";
2115
2116 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002117 if (!parent.isAdded()) return;
2118
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002119 final Bundle args = new Bundle();
2120 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
2121
2122 final WarningEditorFragment dialog = new WarningEditorFragment();
2123 dialog.setArguments(args);
2124 dialog.setTargetFragment(parent, 0);
2125 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
2126 }
2127
2128 @Override
2129 public Dialog onCreateDialog(Bundle savedInstanceState) {
2130 final Context context = getActivity();
2131 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2132 final NetworkPolicyEditor editor = target.mPolicyEditor;
2133
2134 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2135 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2136
2137 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
2138 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
2139
2140 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2141 final long warningBytes = editor.getPolicyWarningBytes(template);
2142 final long limitBytes = editor.getPolicyLimitBytes(template);
2143
2144 bytesPicker.setMinValue(0);
2145 if (limitBytes != LIMIT_DISABLED) {
2146 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
2147 } else {
2148 bytesPicker.setMaxValue(Integer.MAX_VALUE);
2149 }
2150 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
2151 bytesPicker.setWrapSelectorWheel(false);
2152
2153 builder.setTitle(R.string.data_usage_warning_editor_title);
2154 builder.setView(view);
2155
2156 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2157 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002158 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002159 public void onClick(DialogInterface dialog, int which) {
2160 // clear focus to finish pending text edits
2161 bytesPicker.clearFocus();
2162
2163 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
2164 editor.setPolicyWarningBytes(template, bytes);
2165 target.updatePolicy(false);
2166 }
2167 });
2168
2169 return builder.create();
2170 }
2171 }
2172
2173 /**
2174 * Dialog to edit {@link NetworkPolicy#limitBytes}.
2175 */
2176 public static class LimitEditorFragment extends DialogFragment {
2177 private static final String EXTRA_TEMPLATE = "template";
2178
2179 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002180 if (!parent.isAdded()) return;
2181
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002182 final Bundle args = new Bundle();
2183 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
2184
2185 final LimitEditorFragment dialog = new LimitEditorFragment();
2186 dialog.setArguments(args);
2187 dialog.setTargetFragment(parent, 0);
2188 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
2189 }
2190
2191 @Override
2192 public Dialog onCreateDialog(Bundle savedInstanceState) {
2193 final Context context = getActivity();
2194 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2195 final NetworkPolicyEditor editor = target.mPolicyEditor;
2196
2197 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2198 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2199
2200 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
2201 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
2202
2203 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2204 final long warningBytes = editor.getPolicyWarningBytes(template);
2205 final long limitBytes = editor.getPolicyLimitBytes(template);
2206
2207 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09002208 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002209 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
2210 } else {
2211 bytesPicker.setMinValue(0);
2212 }
2213 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
2214 bytesPicker.setWrapSelectorWheel(false);
2215
2216 builder.setTitle(R.string.data_usage_limit_editor_title);
2217 builder.setView(view);
2218
2219 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2220 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002221 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002222 public void onClick(DialogInterface dialog, int which) {
2223 // clear focus to finish pending text edits
2224 bytesPicker.clearFocus();
2225
2226 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
2227 editor.setPolicyLimitBytes(template, bytes);
2228 target.updatePolicy(false);
2229 }
2230 });
2231
2232 return builder.create();
2233 }
2234 }
2235 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07002236 * Dialog to request user confirmation before disabling data.
2237 */
2238 public static class ConfirmDataDisableFragment extends DialogFragment {
Sanket Padawe24f834d2015-01-08 11:23:11 -08002239 static int mSubId;
2240 public static void show(DataUsageSummary parent, int subId) {
2241 mSubId = subId;
Jeff Sharkey461842a2011-09-25 18:22:48 -07002242 if (!parent.isAdded()) return;
2243
Jeff Sharkey28130d92011-09-02 16:10:24 -07002244 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
2245 dialog.setTargetFragment(parent, 0);
2246 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
2247 }
2248
2249 @Override
2250 public Dialog onCreateDialog(Bundle savedInstanceState) {
2251 final Context context = getActivity();
2252
2253 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2254 builder.setMessage(R.string.data_usage_disable_mobile);
2255
2256 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002257 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07002258 public void onClick(DialogInterface dialog, int which) {
2259 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2260 if (target != null) {
2261 // TODO: extend to modify policy enabled flag.
Sanket Padawe24f834d2015-01-08 11:23:11 -08002262 target.setMobileDataEnabled(mSubId, false);
Jeff Sharkey28130d92011-09-02 16:10:24 -07002263 }
2264 }
2265 });
2266 builder.setNegativeButton(android.R.string.cancel, null);
2267
2268 return builder.create();
2269 }
2270 }
2271
2272 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002273 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07002274 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002275 */
2276 public static class ConfirmRestrictFragment extends DialogFragment {
2277 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002278 if (!parent.isAdded()) return;
2279
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002280 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
2281 dialog.setTargetFragment(parent, 0);
2282 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
2283 }
2284
2285 @Override
2286 public Dialog onCreateDialog(Bundle savedInstanceState) {
2287 final Context context = getActivity();
2288
2289 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002290 builder.setTitle(R.string.data_usage_restrict_background_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07002291 if (Utils.hasMultipleUsers(context)) {
2292 builder.setMessage(R.string.data_usage_restrict_background_multiuser);
2293 } else {
2294 builder.setMessage(R.string.data_usage_restrict_background);
2295 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002296
2297 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002298 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002299 public void onClick(DialogInterface dialog, int which) {
2300 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2301 if (target != null) {
2302 target.setRestrictBackground(true);
2303 }
2304 }
2305 });
2306 builder.setNegativeButton(android.R.string.cancel, null);
2307
2308 return builder.create();
2309 }
2310 }
2311
2312 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002313 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
2314 * change has been denied, usually based on
2315 * {@link DataUsageSummary#hasLimitedNetworks()}.
2316 */
2317 public static class DeniedRestrictFragment extends DialogFragment {
2318 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002319 if (!parent.isAdded()) return;
2320
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002321 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
2322 dialog.setTargetFragment(parent, 0);
2323 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
2324 }
2325
2326 @Override
2327 public Dialog onCreateDialog(Bundle savedInstanceState) {
2328 final Context context = getActivity();
2329
2330 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2331 builder.setTitle(R.string.data_usage_app_restrict_background);
2332 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
2333 builder.setPositiveButton(android.R.string.ok, null);
2334
2335 return builder.create();
2336 }
2337 }
2338
2339 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002340 * Dialog to request user confirmation before setting
2341 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
2342 */
2343 public static class ConfirmAppRestrictFragment extends DialogFragment {
2344 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002345 if (!parent.isAdded()) return;
2346
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002347 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2348 dialog.setTargetFragment(parent, 0);
2349 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2350 }
2351
2352 @Override
2353 public Dialog onCreateDialog(Bundle savedInstanceState) {
2354 final Context context = getActivity();
2355
2356 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002357 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2358 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2359
2360 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002361 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002362 public void onClick(DialogInterface dialog, int which) {
2363 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2364 if (target != null) {
2365 target.setAppRestrictBackground(true);
2366 }
2367 }
2368 });
2369 builder.setNegativeButton(android.R.string.cancel, null);
2370
2371 return builder.create();
2372 }
2373 }
2374
2375 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002376 * Compute default tab that should be selected, based on
2377 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2378 */
2379 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002380 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002381 if (template == null) {
Wink Savilleb003a852014-10-23 10:16:26 -07002382 final int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
Wink Saville0183fb52014-11-22 10:11:39 -08002383 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Savilled72c0622014-12-11 10:10:05 -08002384 if (SubscriptionManager.isValidSubscriptionId(subId)) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07002385 return TAB_MOBILE + String.valueOf(subId);
2386 }
2387 return null;
2388 }
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002389
2390 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002391 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002392 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002393 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002394 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002395 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002396 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002397 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002398 return TAB_WIFI;
2399 default:
2400 return null;
2401 }
2402 }
2403
2404 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002405 * Background task that loads {@link UidDetail}, binding to
2406 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002407 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002408 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2409 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002410 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002411 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002412
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002413 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002414 mProvider = checkNotNull(provider);
2415 mItem = checkNotNull(item);
2416 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002417 }
2418
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002419 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002420 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002421 final UidDetailTask existing = (UidDetailTask) target.getTag();
2422 if (existing != null) {
2423 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002424 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002425
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002426 final UidDetail cachedDetail = provider.getUidDetail(item.key, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002427 if (cachedDetail != null) {
2428 bindView(cachedDetail, target);
2429 } else {
2430 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2431 AsyncTask.THREAD_POOL_EXECUTOR));
2432 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002433 }
2434
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002435 private static void bindView(UidDetail detail, View target) {
2436 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2437 final TextView title = (TextView) target.findViewById(android.R.id.title);
2438
2439 if (detail != null) {
2440 icon.setImageDrawable(detail.icon);
2441 title.setText(detail.label);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +01002442 title.setContentDescription(detail.contentDescription);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002443 } else {
2444 icon.setImageDrawable(null);
2445 title.setText(null);
2446 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002447 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002448
2449 @Override
2450 protected void onPreExecute() {
2451 bindView(null, mTarget);
2452 }
2453
2454 @Override
2455 protected UidDetail doInBackground(Void... params) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002456 return mProvider.getUidDetail(mItem.key, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002457 }
2458
2459 @Override
2460 protected void onPostExecute(UidDetail result) {
2461 bindView(result, mTarget);
2462 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002463 }
2464
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002465 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002466 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002467 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002468 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002469 if (TEST_RADIOS) {
2470 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2471 }
2472
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002473 final ConnectivityManager conn = ConnectivityManager.from(context);
2474 final TelephonyManager tele = TelephonyManager.from(context);
2475
Wink Saville0183fb52014-11-22 10:11:39 -08002476 final List<SubscriptionInfo> subInfoList =
2477 SubscriptionManager.from(context).getActiveSubscriptionInfoList();
2478 // No activated Subscriptions
PauloftheWest50e6eca2014-10-03 11:07:14 -07002479 if (subInfoList == null) {
Wink Savillefcec91f2014-12-02 17:12:08 -08002480 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subInfoList=null");
PauloftheWest50e6eca2014-10-03 11:07:14 -07002481 return false;
2482 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002483 // require both supported network and ready SIM
PauloftheWest50e6eca2014-10-03 11:07:14 -07002484 boolean isReady = true;
Wink Savilleca756612014-11-08 10:47:12 -08002485 for (SubscriptionInfo subInfo : subInfoList) {
Stuart Scott3ada2ec2014-10-31 14:11:24 -07002486 isReady = isReady & tele.getSimState(subInfo.getSimSlotIndex()) == SIM_STATE_READY;
Wink Savillefcec91f2014-12-02 17:12:08 -08002487 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subInfo=" + subInfo);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002488 }
Wink Savillefcec91f2014-12-02 17:12:08 -08002489 boolean retVal = conn.isNetworkSupported(TYPE_MOBILE) && isReady;
2490 if (LOGD) {
2491 Log.d(TAG, "hasReadyMobileRadio:"
2492 + " conn.isNetworkSupported(TYPE_MOBILE)="
2493 + conn.isNetworkSupported(TYPE_MOBILE)
2494 + " isReady=" + isReady);
2495 }
2496 return retVal;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002497 }
2498
2499 /*
2500 * TODO: consider adding to TelephonyManager or SubscritpionManager.
2501 */
Wink Savilleb003a852014-10-23 10:16:26 -07002502 public static boolean hasReadyMobileRadio(Context context, int subId) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07002503 if (TEST_RADIOS) {
2504 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2505 }
2506
2507 final ConnectivityManager conn = ConnectivityManager.from(context);
2508 final TelephonyManager tele = TelephonyManager.from(context);
2509 final int slotId = SubscriptionManager.getSlotId(subId);
2510 final boolean isReady = tele.getSimState(slotId) == SIM_STATE_READY;
2511
Wink Savillefcec91f2014-12-02 17:12:08 -08002512 boolean retVal = conn.isNetworkSupported(TYPE_MOBILE) && isReady;
2513 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subId=" + subId
2514 + " conn.isNetworkSupported(TYPE_MOBILE)=" + conn.isNetworkSupported(TYPE_MOBILE)
2515 + " isReady=" + isReady);
2516 return retVal;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002517 }
2518
2519 /**
2520 * Test if device has a mobile 4G data radio.
2521 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002522 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002523 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2524 return false;
2525 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002526 if (TEST_RADIOS) {
2527 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2528 }
2529
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002530 final ConnectivityManager conn = ConnectivityManager.from(context);
2531 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002532
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002533 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Wink Saville55434042012-06-14 12:33:43 -07002534 final boolean hasLte = (tele.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE)
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002535 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002536 return hasWimax || hasLte;
2537 }
2538
2539 /**
2540 * Test if device has a Wi-Fi data radio.
2541 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002542 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002543 if (TEST_RADIOS) {
2544 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2545 }
2546
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002547 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002548 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002549 }
2550
2551 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002552 * Test if device has an ethernet network connection.
2553 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002554 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002555 if (TEST_RADIOS) {
2556 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2557 }
2558
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002559 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002560 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2561
2562 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002563 if (mStatsSession != null) {
2564 try {
2565 ethernetBytes = mStatsSession.getSummaryForNetwork(
2566 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2567 .getTotalBytes();
2568 } catch (RemoteException e) {
2569 throw new RuntimeException(e);
2570 }
2571 } else {
2572 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002573 }
2574
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002575 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002576 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002577 }
2578
2579 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002580 * Inflate a {@link Preference} style layout, adding the given {@link View}
2581 * widget into {@link android.R.id#widget_frame}.
2582 */
2583 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2584 final View view = inflater.inflate(R.layout.preference, root, false);
2585 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2586 android.R.id.widget_frame);
2587 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2588 return view;
2589 }
2590
2591 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002592 * Test if any networks are currently limited.
2593 */
2594 private boolean hasLimitedNetworks() {
2595 return !buildLimitedNetworksList().isEmpty();
2596 }
2597
2598 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002599 * Build string describing currently limited networks, which defines when
2600 * background data is restricted.
2601 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002602 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002603 private CharSequence buildLimitedNetworksString() {
2604 final List<CharSequence> limited = buildLimitedNetworksList();
2605
2606 // handle case where no networks limited
2607 if (limited.isEmpty()) {
2608 limited.add(getText(R.string.data_usage_list_none));
2609 }
2610
Roozbeh Pournader4bbf75d2015-08-10 11:32:19 -07002611 final ICUResourceBundle icuBundle = (ICUResourceBundle) UResourceBundle.
2612 getBundleInstance(ICUResourceBundle.ICU_BASE_NAME);
2613 final String listMiddlePattern =
2614 icuBundle.getStringWithFallback("listPattern/standard/middle");
2615 // The returned pattern is something like "{0}, {1}", from which we want
2616 // to extract the ", " part.
2617 final int firstClosingBrace = listMiddlePattern.indexOf('}');
2618 final int lastOpeningBrace = listMiddlePattern.lastIndexOf('{');
2619 final CharSequence delimiter = listMiddlePattern.substring(
2620 firstClosingBrace+1, lastOpeningBrace);
2621
2622 return TextUtils.join(delimiter, limited);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002623 }
2624
2625 /**
2626 * Build list of currently limited networks, which defines when background
2627 * data is restricted.
2628 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002629 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002630 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002631 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002632
2633 // build combined list of all limited networks
2634 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002635
2636 final TelephonyManager tele = TelephonyManager.from(context);
2637 if (tele.getSimState() == SIM_STATE_READY) {
2638 final String subscriberId = getActiveSubscriberId(context);
2639 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2640 limited.add(getText(R.string.data_usage_list_mobile));
2641 }
2642 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2643 limited.add(getText(R.string.data_usage_tab_3g));
2644 }
2645 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2646 limited.add(getText(R.string.data_usage_tab_4g));
2647 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002648 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002649
2650 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002651 limited.add(getText(R.string.data_usage_tab_wifi));
2652 }
2653 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2654 limited.add(getText(R.string.data_usage_tab_ethernet));
2655 }
2656
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002657 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002658 }
2659
2660 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002661 * Inset both selector and divider {@link Drawable} on the given
2662 * {@link ListView} by the requested dimensions.
2663 */
2664 private static void insetListViewDrawables(ListView view, int insetSide) {
2665 final Drawable selector = view.getSelector();
2666 final Drawable divider = view.getDivider();
2667
2668 // fully unregister these drawables so callbacks can be maintained after
2669 // wrapping below.
2670 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2671 view.setSelector(stub);
2672 view.setDivider(stub);
2673
2674 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2675 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2676 }
2677
2678 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002679 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002680 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002681 */
2682 private static void setPreferenceTitle(View parent, int resId) {
2683 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2684 title.setText(resId);
2685 }
2686
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002687 /**
2688 * Set {@link android.R.id#summary} for a preference view inflated with
2689 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2690 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002691 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002692 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2693 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002694 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002695 }
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002696
Jason Monke634ff32015-11-17 13:52:47 -05002697 private void addMobileTab(Context context, SubscriptionInfo subInfo, boolean isMultiSim) {
2698 if (subInfo != null && mMobileTagMap != null) {
2699 if (hasReadyMobileRadio(context, subInfo.getSubscriptionId())) {
2700 if (isMultiSim) {
2701 mTabHost.addTab(buildTabSpec(mMobileTagMap.get(subInfo.getSubscriptionId()),
2702 subInfo.getDisplayName()));
2703 } else {
2704 mTabHost.addTab(buildTabSpec(mMobileTagMap.get(subInfo.getSubscriptionId()),
2705 R.string.data_usage_tab_mobile));
2706 }
2707 }
2708 } else {
2709 if (LOGD) Log.d(TAG, "addMobileTab: subInfoList is null");
2710 }
2711 }
2712
2713 private SubscriptionInfo getCurrentTabSubInfo(Context context) {
2714 if (mSubInfoList != null && mTabHost != null) {
2715 final int currentTagIndex = mTabHost.getCurrentTab();
2716 int i = 0;
2717 for (SubscriptionInfo subInfo : mSubInfoList) {
2718 if (hasReadyMobileRadio(context, subInfo.getSubscriptionId())) {
2719 if (i++ == currentTagIndex) {
2720 return subInfo;
2721 }
2722 }
2723 }
2724 }
2725 return null;
2726 }
2727
2728 /**
2729 * Init a map with subId key and mobile tag name
2730 * @param subInfoList The subscription Info List
2731 * @return The map or null if no activated subscription
2732 */
2733 private Map<Integer, String> initMobileTabTag(List<SubscriptionInfo> subInfoList) {
2734 Map<Integer, String> map = null;
2735 if (subInfoList != null) {
2736 String mobileTag;
2737 map = new HashMap<Integer, String>();
2738 for (SubscriptionInfo subInfo : subInfoList) {
2739 mobileTag = TAB_MOBILE + String.valueOf(subInfo.getSubscriptionId());
2740 map.put(subInfo.getSubscriptionId(), mobileTag);
2741 }
2742 }
2743 return map;
2744 }
2745
2746 private static boolean isMobileTab(String currentTab) {
2747 return currentTab != null ? currentTab.contains(TAB_MOBILE) : false;
2748 }
2749
2750 private int getSubId(String currentTab) {
2751 if (mMobileTagMap != null) {
2752 Set<Integer> set = mMobileTagMap.keySet();
2753 for (Integer subId : set) {
2754 if (mMobileTagMap.get(subId).equals(currentTab)) {
2755 return subId;
2756 }
2757 }
2758 }
2759 Log.e(TAG, "currentTab = " + currentTab + " non mobile tab called this function");
2760 return -1;
2761 }
2762
2763 private boolean isMobileDataAvailable(int subId) {
2764 return mSubscriptionManager.getActiveSubscriptionInfo(subId) != null;
2765 }
2766
2767 private static class SummaryProvider
2768 implements SummaryLoader.SummaryProvider {
2769
2770 private final Activity mActivity;
2771 private final SummaryLoader mSummaryLoader;
2772 private final MobileDataController mDataController;
2773
2774 public SummaryProvider(Activity activity, SummaryLoader summaryLoader) {
2775 mActivity = activity;
2776 mSummaryLoader = summaryLoader;
2777 mDataController = new MobileDataController(activity);
2778 }
2779
2780 @Override
2781 public void setListening(boolean listening) {
2782 if (listening) {
2783 MobileDataController.DataUsageInfo info = mDataController.getDataUsageInfo();
2784 String used;
2785 if (info == null) {
2786 used = Formatter.formatFileSize(mActivity, 0);
2787 } else if (info.limitLevel <= 0) {
2788 used = Formatter.formatFileSize(mActivity, info.usageLevel);
2789 } else {
2790 used = Utils.formatPercentage(info.usageLevel, info.limitLevel);
2791 }
2792 mSummaryLoader.setSummary(this,
2793 mActivity.getString(R.string.data_usage_summary_format, used));
2794 }
2795 }
2796 }
2797
2798 public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY
2799 = new SummaryLoader.SummaryProviderFactory() {
2800 @Override
2801 public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity,
2802 SummaryLoader summaryLoader) {
2803 return new SummaryProvider(activity, summaryLoader);
2804 }
2805 };
2806
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002807 /**
2808 * For search
2809 */
2810 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -07002811 new BaseSearchIndexProvider() {
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002812 @Override
2813 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
2814 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
2815
2816 final Resources res = context.getResources();
2817
2818 // Add fragment title
2819 SearchIndexableRaw data = new SearchIndexableRaw(context);
2820 data.title = res.getString(R.string.data_usage_summary_title);
2821 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2822 result.add(data);
2823
2824 // Mobile data
2825 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002826 data.key = DATA_USAGE_ENABLE_MOBILE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002827 data.title = res.getString(R.string.data_usage_enable_mobile);
2828 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2829 result.add(data);
2830
2831 // Set mobile data limit
2832 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002833 data.key = DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002834 data.title = res.getString(R.string.data_usage_disable_mobile_limit);
2835 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2836 result.add(data);
2837
Fabrice Di Megliof2a52262014-04-17 17:20:27 -07002838 // Data usage cycle
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002839 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002840 data.key = DATA_USAGE_CYCLE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002841 data.title = res.getString(R.string.data_usage_cycle);
2842 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2843 result.add(data);
2844
2845 return result;
2846 }
2847 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002848}