blob: 3ab1d239e266efbe9d4763298262db853f471e91 [file] [log] [blame]
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070019import static android.net.ConnectivityManager.TYPE_ETHERNET;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070020import static android.net.ConnectivityManager.TYPE_MOBILE;
Jeff Sharkeybdf98e82011-11-10 17:17:24 -080021import static android.net.ConnectivityManager.TYPE_WIFI;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070022import static android.net.ConnectivityManager.TYPE_WIMAX;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070023import static android.net.NetworkPolicy.LIMIT_DISABLED;
Jeff Sharkeya53188f2011-09-13 19:56:45 -070024import static android.net.NetworkPolicy.WARNING_DISABLED;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070025import static android.net.NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070026import static android.net.NetworkPolicyManager.POLICY_NONE;
27import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND;
Jeff Sharkey8a503642011-06-10 13:31:21 -070028import static android.net.NetworkPolicyManager.computeLastCycleBoundary;
29import static android.net.NetworkPolicyManager.computeNextCycleBoundary;
Jeff Sharkeya662e492011-06-18 21:57:06 -070030import static android.net.NetworkTemplate.MATCH_MOBILE_3G_LOWER;
31import static android.net.NetworkTemplate.MATCH_MOBILE_4G;
32import static android.net.NetworkTemplate.MATCH_MOBILE_ALL;
33import static android.net.NetworkTemplate.MATCH_WIFI;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070034import static android.net.NetworkTemplate.buildTemplateEthernet;
35import static android.net.NetworkTemplate.buildTemplateMobile3gLower;
36import static android.net.NetworkTemplate.buildTemplateMobile4g;
37import static android.net.NetworkTemplate.buildTemplateMobileAll;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070038import static android.net.NetworkTemplate.buildTemplateWifiWildcard;
Jeff Sharkey77dae912012-02-03 14:50:33 -080039import static android.net.TrafficStats.GB_IN_BYTES;
40import static android.net.TrafficStats.MB_IN_BYTES;
Jeff Sharkeya83a24f2011-09-16 01:52:39 -070041import static android.net.TrafficStats.UID_REMOVED;
42import static android.net.TrafficStats.UID_TETHERING;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070043import static android.telephony.TelephonyManager.SIM_STATE_READY;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070044import static android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
45import static android.text.format.DateUtils.FORMAT_SHOW_DATE;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070046import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -070047import static com.android.internal.util.Preconditions.checkNotNull;
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -070048import static com.android.settings.Utils.prepareCustomPreferencesList;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070049
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070050import android.animation.LayoutTransition;
Jeff Sharkey38305fb2012-09-14 16:26:51 -070051import android.app.ActivityManager;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070052import android.app.AlertDialog;
53import android.app.Dialog;
54import android.app.DialogFragment;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070055import android.app.Fragment;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070056import android.app.FragmentTransaction;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070057import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkeyb6548462014-07-21 15:38:06 -070058import android.content.ComponentName;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070059import android.content.Context;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070060import android.content.DialogInterface;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -070061import android.content.Intent;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070062import android.content.Loader;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070063import android.content.SharedPreferences;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070064import android.content.pm.PackageManager;
Jason Monk4bb075b2015-03-17 10:06:58 -040065import android.content.pm.PackageManager.NameNotFoundException;
Zoltan Szatmary-Ban77c1d362014-12-12 18:48:03 +000066import android.content.pm.UserInfo;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070067import android.content.res.Resources;
Jeff Sharkeyb6548462014-07-21 15:38:06 -070068import android.content.res.TypedArray;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070069import android.graphics.Color;
Jeff Sharkey5d706792011-09-08 18:57:17 -070070import android.graphics.drawable.ColorDrawable;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070071import android.graphics.drawable.Drawable;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070072import android.net.ConnectivityManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070073import android.net.INetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070074import android.net.INetworkStatsService;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070075import android.net.INetworkStatsSession;
Jeff Sharkey8a503642011-06-10 13:31:21 -070076import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070077import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070078import android.net.NetworkStats;
79import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070080import android.net.NetworkTemplate;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070081import android.net.TrafficStats;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070082import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070083import android.os.Bundle;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070084import android.os.INetworkManagementService;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070085import android.os.Parcel;
86import android.os.Parcelable;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070087import android.os.RemoteException;
88import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070089import android.os.SystemProperties;
Dianne Hackbornbb06a422012-08-16 11:01:57 -070090import android.os.UserHandle;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +010091import android.os.UserManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070092import android.preference.Preference;
Wink Savilleca756612014-11-08 10:47:12 -080093import android.telephony.SubscriptionInfo;
PauloftheWest50e6eca2014-10-03 11:07:14 -070094import android.telephony.SubscriptionManager;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070095import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070096import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070097import android.text.format.DateUtils;
98import android.text.format.Formatter;
Jeff Sharkeye5223a02012-03-09 17:11:14 -080099import android.text.format.Time;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700100import android.util.Log;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700101import android.util.SparseArray;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700102import android.util.SparseBooleanArray;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700103import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700104import android.view.Menu;
105import android.view.MenuInflater;
106import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700107import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700108import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700109import android.view.ViewGroup;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700110import android.widget.AdapterView;
111import android.widget.AdapterView.OnItemClickListener;
112import android.widget.AdapterView.OnItemSelectedListener;
113import android.widget.ArrayAdapter;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700114import android.widget.BaseAdapter;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700115import android.widget.Button;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700116import android.widget.ImageView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700117import android.widget.LinearLayout;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700118import android.widget.ListView;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700119import android.widget.NumberPicker;
Jeff Sharkey2412b0f2011-07-17 20:31:40 -0700120import android.widget.ProgressBar;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700121import android.widget.Spinner;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700122import android.widget.Switch;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700123import android.widget.TabHost;
Jason Monk4bb075b2015-03-17 10:06:58 -0400124import android.widget.Toast;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700125import android.widget.TabHost.OnTabChangeListener;
126import android.widget.TabHost.TabContentFactory;
127import android.widget.TabHost.TabSpec;
128import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700129import android.widget.TextView;
130
Wink Saville55434042012-06-14 12:33:43 -0700131import com.android.internal.telephony.PhoneConstants;
Jeff Sharkey5d706792011-09-08 18:57:17 -0700132import com.android.settings.drawable.InsetBoundsDrawable;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700133import com.android.settings.net.ChartData;
134import com.android.settings.net.ChartDataLoader;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700135import com.android.settings.net.DataUsageMeteredSettings;
Jeff Sharkeya662e492011-06-18 21:57:06 -0700136import com.android.settings.net.NetworkPolicyEditor;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700137import com.android.settings.net.SummaryForAllUidLoader;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700138import com.android.settings.net.UidDetail;
139import com.android.settings.net.UidDetailProvider;
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -0700140import com.android.settings.search.BaseSearchIndexProvider;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -0700141import com.android.settings.search.Indexable;
142import com.android.settings.search.SearchIndexableRaw;
PauloftheWest50e6eca2014-10-03 11:07:14 -0700143import com.android.settings.sim.SimSettings;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700144import com.android.settings.widget.ChartDataUsageView;
145import com.android.settings.widget.ChartDataUsageView.DataUsageChartListener;
PauloftheWestc80b7612014-11-25 04:40:45 -0800146import com.android.settings.widget.ChartNetworkSeriesView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700147import com.google.android.collect.Lists;
148
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700149import libcore.util.Objects;
150
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700151import java.util.ArrayList;
152import java.util.Collections;
PauloftheWest50e6eca2014-10-03 11:07:14 -0700153import java.util.HashMap;
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700154import java.util.List;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700155import java.util.Locale;
PauloftheWest50e6eca2014-10-03 11:07:14 -0700156import java.util.Map;
157import java.util.Set;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700158
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700159/**
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700160 * Panel showing data usage history across various networks, including options
161 * to inspect based on usage cycle and control through {@link NetworkPolicy}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700162 */
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700163public class DataUsageSummary extends HighlightingFragment implements Indexable {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700164 private static final String TAG = "DataUsage";
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800165 private static final boolean LOGD = false;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700166
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700167 // TODO: remove this testing code
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700168 private static final boolean TEST_ANIM = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700169 private static final boolean TEST_RADIOS = false;
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800170
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700171 private static final String TEST_RADIOS_PROP = "test.radios";
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800172 private static final String TEST_SUBSCRIBER_PROP = "test.subscriberid";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700173
Jeff Sharkey8a503642011-06-10 13:31:21 -0700174 private static final String TAB_3G = "3g";
175 private static final String TAB_4G = "4g";
176 private static final String TAB_MOBILE = "mobile";
177 private static final String TAB_WIFI = "wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700178 private static final String TAB_ETHERNET = "ethernet";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700179
Jeff Sharkey28130d92011-09-02 16:10:24 -0700180 private static final String TAG_CONFIRM_DATA_DISABLE = "confirmDataDisable";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700181 private static final String TAG_CONFIRM_LIMIT = "confirmLimit";
182 private static final String TAG_CYCLE_EDITOR = "cycleEditor";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700183 private static final String TAG_WARNING_EDITOR = "warningEditor";
184 private static final String TAG_LIMIT_EDITOR = "limitEditor";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700185 private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700186 private static final String TAG_DENIED_RESTRICT = "deniedRestrict";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700187 private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700188 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700189
Fabrice Di Meglio2169c882014-04-18 15:18:40 -0700190 private static final String DATA_USAGE_ENABLE_MOBILE_KEY = "data_usage_enable_mobile";
191 private static final String DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY =
192 "data_usage_disable_mobile_limit";
193 private static final String DATA_USAGE_CYCLE_KEY = "data_usage_cycle";
194
Jason Monk4bb075b2015-03-17 10:06:58 -0400195 public static final String EXTRA_SHOW_APP_IMMEDIATE_PKG = "showAppImmediatePkg";
196
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700197 private static final int LOADER_CHART_DATA = 2;
198 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700199
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700200 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700201 private INetworkStatsService mStatsService;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700202 private NetworkPolicyManager mPolicyManager;
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700203 private TelephonyManager mTelephonyManager;
Wink Saville0183fb52014-11-22 10:11:39 -0800204 private SubscriptionManager mSubscriptionManager;
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700205
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700206 private INetworkStatsSession mStatsSession;
207
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700208 private static final String PREF_FILE = "data_usage";
209 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700210 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700211
212 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700213
Jeff Sharkey8a503642011-06-10 13:31:21 -0700214 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700215 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700216 private TabWidget mTabWidget;
217 private ListView mListView;
PauloftheWestc80b7612014-11-25 04:40:45 -0800218 private ChartNetworkSeriesView mSeries;
219 private ChartNetworkSeriesView mDetailedSeries;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700220 private DataUsageAdapter mAdapter;
221
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700222 /** Distance to inset content from sides, when needed. */
223 private int mInsetSide = 0;
224
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700225 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700226
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700227 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700228 private LinearLayout mNetworkSwitches;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700229 private boolean mDataEnabledSupported;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700230 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700231 private View mDataEnabledView;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700232 private boolean mDisableAtLimitSupported;
233 private Switch mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700234 private View mDisableAtLimitView;
235
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700236 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700237 private Spinner mCycleSpinner;
238 private CycleAdapter mCycleAdapter;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700239 private TextView mCycleSummary;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700240
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700241 private ChartDataUsageView mChart;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700242 private View mDisclaimer;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700243 private TextView mEmpty;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700244 private View mStupidPadding;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700245
246 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700247 private ImageView mAppIcon;
248 private ViewGroup mAppTitles;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700249 private TextView mAppTotal;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700250 private TextView mAppForeground;
251 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700252 private Button mAppSettings;
253
254 private LinearLayout mAppSwitches;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700255 private Switch mAppRestrict;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700256 private View mAppRestrictView;
257
Jeff Sharkey8a503642011-06-10 13:31:21 -0700258 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700259 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700260
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700261 private NetworkTemplate mTemplate;
262 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700263
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700264 private AppItem mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700265
266 private Intent mAppSettingsIntent;
267
Jeff Sharkeya662e492011-06-18 21:57:06 -0700268 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700269
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700270 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700271 private String mIntentTab = null;
272
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700273 private MenuItem mMenuRestrictBackground;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700274 private MenuItem mMenuShowWifi;
275 private MenuItem mMenuShowEthernet;
276 private MenuItem mMenuSimCards;
277 private MenuItem mMenuCellularNetworks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700278
Wink Savilleca756612014-11-08 10:47:12 -0800279 private List<SubscriptionInfo> mSubInfoList;
Wink Savilleb003a852014-10-23 10:16:26 -0700280 private Map<Integer,String> mMobileTagMap;
PauloftheWest50e6eca2014-10-03 11:07:14 -0700281
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700282 /** Flag used to ignore listeners during binding. */
283 private boolean mBinding;
284
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700285 private UidDetailProvider mUidDetailProvider;
286
Jason Monk4bb075b2015-03-17 10:06:58 -0400287 // Indicates request to show app immediately rather than list.
288 private String mShowAppImmediatePkg;
289
Sanket Padawed819270f2015-01-14 11:03:33 -0800290 /**
291 * Local cache of data enabled for subId, used to work around delays.
292 */
293 private final Map<String, Boolean> mMobileDataEnabled = new HashMap<String, Boolean>();
294
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700295 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700296 public void onCreate(Bundle savedInstanceState) {
297 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700298 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700299
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700300 mNetworkService = INetworkManagementService.Stub.asInterface(
301 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700302 mStatsService = INetworkStatsService.Stub.asInterface(
303 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700304 mPolicyManager = NetworkPolicyManager.from(context);
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700305 mTelephonyManager = TelephonyManager.from(context);
Wink Saville0183fb52014-11-22 10:11:39 -0800306 mSubscriptionManager = SubscriptionManager.from(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700307
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700308 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700309
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700310 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700311 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700312
Wink Saville0183fb52014-11-22 10:11:39 -0800313 mSubInfoList = mSubscriptionManager.getActiveSubscriptionInfoList();
PauloftheWest50e6eca2014-10-03 11:07:14 -0700314 mMobileTagMap = initMobileTabTag(mSubInfoList);
315
Jaewan Kimffce9c12013-03-19 16:53:45 +0900316 try {
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700317 if (!mNetworkService.isBandwidthControlEnabled()) {
318 Log.w(TAG, "No bandwidth control; leaving");
319 getActivity().finish();
320 }
321 } catch (RemoteException e) {
322 Log.w(TAG, "No bandwidth control; leaving");
323 getActivity().finish();
324 }
325
326 try {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900327 mStatsSession = mStatsService.openSession();
328 } catch (RemoteException e) {
329 throw new RuntimeException(e);
330 }
331
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700332 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700333 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700334
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700335 // override preferences when no mobile radio
336 if (!hasReadyMobileRadio(context)) {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900337 mShowWifi = true;
338 mShowEthernet = true;
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700339 }
340
Jason Monk4bb075b2015-03-17 10:06:58 -0400341 mUidDetailProvider = new UidDetailProvider(context);
342
343 Bundle arguments = getArguments();
344 if (arguments != null) {
345 mShowAppImmediatePkg = arguments.getString(EXTRA_SHOW_APP_IMMEDIATE_PKG);
346 }
347
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700348 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700349 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700350
Jeff Sharkey8a503642011-06-10 13:31:21 -0700351 @Override
352 public View onCreateView(LayoutInflater inflater, ViewGroup container,
353 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700354
Jeff Sharkey8a503642011-06-10 13:31:21 -0700355 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700356 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
357
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700358
Jeff Sharkey8a503642011-06-10 13:31:21 -0700359 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700360 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700361 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
362 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700363
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700364 // decide if we need to manually inset our content, or if we should rely
365 // on parent container for inset.
366 final boolean shouldInset = mListView.getScrollBarStyle()
367 == View.SCROLLBARS_OUTSIDE_OVERLAY;
Amith Yamasani56f51a82013-08-05 10:07:23 -0700368 mInsetSide = 0;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700369
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700370 // adjust padding around tabwidget as needed
Amith Yamasani56f51a82013-08-05 10:07:23 -0700371 prepareCustomPreferencesList(container, view, mListView, false);
Jeff Sharkey5d706792011-09-08 18:57:17 -0700372
Jeff Sharkey8a503642011-06-10 13:31:21 -0700373 mTabHost.setup();
374 mTabHost.setOnTabChangedListener(mTabListener);
375
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700376 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700377 mHeader.setClickable(true);
378
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700379 mListView.addHeaderView(new View(context), null, true);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700380 mListView.addHeaderView(mHeader, null, true);
381 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700382
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700383 if (mInsetSide > 0) {
384 // inset selector and divider drawables
385 insetListViewDrawables(mListView, mInsetSide);
Fabrice Di Megliob27223f2013-01-15 18:54:11 -0800386 mHeader.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700387 }
388
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700389 {
390 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700391 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
392 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700393 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700394
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700395 mDataEnabled = new Switch(inflater.getContext());
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700396 mDataEnabled.setClickable(false);
397 mDataEnabled.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700398 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700399 mDataEnabledView.setTag(R.id.preference_highlight_key,
400 DATA_USAGE_ENABLE_MOBILE_KEY);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700401 mDataEnabledView.setClickable(true);
402 mDataEnabledView.setFocusable(true);
403 mDataEnabledView.setOnClickListener(mDataEnabledListener);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700404 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700405
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700406 mDisableAtLimit = new Switch(inflater.getContext());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700407 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700408 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700409 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700410 mDisableAtLimitView.setTag(R.id.preference_highlight_key,
411 DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700412 mDisableAtLimitView.setClickable(true);
413 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700414 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
415 mNetworkSwitches.addView(mDisableAtLimitView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700416
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700417 mCycleView = inflater.inflate(R.layout.data_usage_cycles, mNetworkSwitches, false);
418 mCycleView.setTag(R.id.preference_highlight_key, DATA_USAGE_CYCLE_KEY);
419 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
420 mCycleAdapter = new CycleAdapter(context);
421 mCycleSpinner.setAdapter(mCycleAdapter);
422 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
423 mCycleSummary = (TextView) mCycleView.findViewById(R.id.cycle_summary);
424 mNetworkSwitches.addView(mCycleView);
PauloftheWestc80b7612014-11-25 04:40:45 -0800425 mSeries = (ChartNetworkSeriesView)view.findViewById(R.id.series);
426 mDetailedSeries = (ChartNetworkSeriesView)view.findViewById(R.id.detail_series);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700427 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700428
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700429 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700430 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800431 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700432
433 {
434 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700435 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700436 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
437 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700438 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
439 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700440 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700441
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700442 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700443
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700444 mAppRestrict = new Switch(inflater.getContext());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700445 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700446 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700447 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700448 mAppRestrictView.setClickable(true);
449 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700450 mAppRestrictView.setOnClickListener(mAppRestrictListener);
451 mAppSwitches.addView(mAppRestrictView);
452 }
453
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700454 mDisclaimer = mHeader.findViewById(R.id.disclaimer);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700455 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700456 mStupidPadding = mHeader.findViewById(R.id.stupid_padding);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700457
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +0100458 final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
459 mAdapter = new DataUsageAdapter(um, mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700460 mListView.setOnItemClickListener(mListListener);
461 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700462
Jason Monk4bb075b2015-03-17 10:06:58 -0400463 showRequestedAppIfNeeded();
464
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700465 return view;
466 }
467
Jason Monk4bb075b2015-03-17 10:06:58 -0400468 private void showRequestedAppIfNeeded() {
469 if (mShowAppImmediatePkg == null) {
470 return;
471 }
472 try {
473 int uid = getActivity().getPackageManager().getPackageUid(mShowAppImmediatePkg,
474 UserHandle.myUserId());
475 AppItem app = new AppItem(uid);
476 app.addUid(uid);
477
478 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
479 // When we are going straight to an app then we are coming from App Info and want
480 // a header at the top.
481 AppHeader.createAppHeader(getActivity(), detail.icon, detail.label, null);
482 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label, false);
483 } catch (NameNotFoundException e) {
484 Log.w(TAG, "Could not find " + mShowAppImmediatePkg, e);
485 Toast.makeText(getActivity(), getString(R.string.unknown_app), Toast.LENGTH_LONG)
486 .show();
487 getActivity().finish();
488 }
489 }
490
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700491 @Override
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700492 public void onViewStateRestored(Bundle savedInstanceState) {
493 super.onViewStateRestored(savedInstanceState);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700494
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700495 // pick default tab based on incoming intent
496 final Intent intent = getActivity().getIntent();
497 mIntentTab = computeTabFromIntent(intent);
498
Jeff Sharkey8a503642011-06-10 13:31:21 -0700499 // this kicks off chain reaction which creates tabs, binds the body to
500 // selected network, and binds chart, cycles and detail list.
501 updateTabs();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700502 }
503
504 @Override
505 public void onResume() {
506 super.onResume();
507
508 getView().post(new Runnable() {
509 @Override
510 public void run() {
511 highlightViewIfNeeded();
512 }
513 });
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700514
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700515 // kick off background task to update stats
516 new AsyncTask<Void, Void, Void>() {
517 @Override
518 protected Void doInBackground(Void... params) {
519 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700520 // wait a few seconds before kicking off
521 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700522 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700523 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700524 } catch (RemoteException e) {
525 }
526 return null;
527 }
528
529 @Override
530 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700531 if (isAdded()) {
532 updateBody();
533 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700534 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700535 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700536 }
537
Jeff Sharkey8a503642011-06-10 13:31:21 -0700538 @Override
539 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
540 inflater.inflate(R.menu.data_usage, menu);
541 }
542
543 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700544 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700545 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700546 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700547 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700548
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700549 mMenuShowWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700550 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
551 mMenuShowWifi.setVisible(!appDetailMode);
552 } else {
553 mMenuShowWifi.setVisible(false);
554 }
555
556 mMenuShowEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
557 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
558 mMenuShowEthernet.setVisible(!appDetailMode);
559 } else {
560 mMenuShowEthernet.setVisible(false);
561 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700562
563 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700564 mMenuRestrictBackground.setVisible(
565 hasReadyMobileRadio(context) && isOwner && !appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700566
567 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
568 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700569 metered.setVisible(!appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700570 } else {
571 metered.setVisible(false);
572 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700573
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700574 // TODO: show when multiple sims available
575 mMenuSimCards = menu.findItem(R.id.data_usage_menu_sim_cards);
576 mMenuSimCards.setVisible(false);
577
578 mMenuCellularNetworks = menu.findItem(R.id.data_usage_menu_cellular_networks);
PauloftheWesta4b8fb32014-09-18 10:12:25 -0700579 mMenuCellularNetworks.setVisible(hasReadyMobileRadio(context)
580 && !appDetailMode && isOwner);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700581
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700582 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
583 String helpUrl;
584 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800585 HelpUtils.prepareHelpMenuItem(context, help, helpUrl);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700586 } else {
587 help.setVisible(false);
588 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700589
590 updateMenuTitles();
591 }
592
593 private void updateMenuTitles() {
594 if (mPolicyManager.getRestrictBackground()) {
595 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_allow_background);
596 } else {
597 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_restrict_background);
598 }
599
600 if (mShowWifi) {
601 mMenuShowWifi.setTitle(R.string.data_usage_menu_hide_wifi);
602 } else {
603 mMenuShowWifi.setTitle(R.string.data_usage_menu_show_wifi);
604 }
605
606 if (mShowEthernet) {
607 mMenuShowEthernet.setTitle(R.string.data_usage_menu_hide_ethernet);
608 } else {
609 mMenuShowEthernet.setTitle(R.string.data_usage_menu_show_ethernet);
610 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700611 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700612
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700613 @Override
614 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700615 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700616 case R.id.data_usage_menu_restrict_background: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700617 final boolean restrictBackground = !mPolicyManager.getRestrictBackground();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700618 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800619 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700620 } else {
621 // no confirmation to drop restriction
622 setRestrictBackground(false);
623 }
624 return true;
625 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700626 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700627 mShowWifi = !mShowWifi;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700628 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700629 updateMenuTitles();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700630 updateTabs();
631 return true;
632 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700633 case R.id.data_usage_menu_show_ethernet: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700634 mShowEthernet = !mShowEthernet;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700635 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700636 updateMenuTitles();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700637 updateTabs();
638 return true;
639 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700640 case R.id.data_usage_menu_sim_cards: {
641 // TODO: hook up to sim cards
642 return true;
643 }
644 case R.id.data_usage_menu_cellular_networks: {
645 final Intent intent = new Intent(Intent.ACTION_MAIN);
646 intent.setComponent(new ComponentName("com.android.phone",
647 "com.android.phone.MobileNetworkSettings"));
648 startActivity(intent);
649 return true;
650 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700651 case R.id.data_usage_menu_metered: {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800652 final SettingsActivity sa = (SettingsActivity) getActivity();
653 sa.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700654 R.string.data_usage_metered_title, null, this, 0);
655 return true;
656 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700657 }
658 return false;
659 }
660
Jeff Sharkey94a90952011-06-13 22:31:09 -0700661 @Override
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700662 public void onDestroy() {
Jeff Sharkey94a90952011-06-13 22:31:09 -0700663 mDataEnabledView = null;
664 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700665
666 mUidDetailProvider.clearCache();
667 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700668
669 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700670
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800671 super.onDestroy();
672 }
673
Jeff Sharkey8a503642011-06-10 13:31:21 -0700674 /**
Jeff Sharkey92811822012-05-04 11:47:29 -0700675 * Build and assign {@link LayoutTransition} to various containers. Should
676 * only be assigned after initial layout is complete.
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700677 */
Jeff Sharkey92811822012-05-04 11:47:29 -0700678 private void ensureLayoutTransitions() {
Jason Monk4bb075b2015-03-17 10:06:58 -0400679 if (mShowAppImmediatePkg != null) {
680 // If we are skipping right to showing an app, we don't care about transitions.
681 return;
682 }
Jeff Sharkey92811822012-05-04 11:47:29 -0700683 // skip when already setup
684 if (mChart.getLayoutTransition() != null) return;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700685
Jeff Sharkey92811822012-05-04 11:47:29 -0700686 mTabsContainer.setLayoutTransition(buildLayoutTransition());
687 mHeader.setLayoutTransition(buildLayoutTransition());
688 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700689
Jeff Sharkey92811822012-05-04 11:47:29 -0700690 final LayoutTransition chartTransition = buildLayoutTransition();
691 chartTransition.disableTransitionType(LayoutTransition.APPEARING);
692 chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
693 mChart.setLayoutTransition(chartTransition);
694 }
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700695
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700696 private static LayoutTransition buildLayoutTransition() {
697 final LayoutTransition transition = new LayoutTransition();
698 if (TEST_ANIM) {
699 transition.setDuration(1500);
700 }
701 transition.setAnimateParentHierarchy(false);
702 return transition;
703 }
704
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700705 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700706 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700707 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
708 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700709 */
710 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700711 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700712 mTabHost.clearAllTabs();
713
Sanket Padawe7e21fa52015-02-12 12:39:34 -0800714 int simCount = mTelephonyManager.getSimCount();
715
716 for (int i = 0; i < simCount; i++) {
PauloftheWestc80b7612014-11-25 04:40:45 -0800717 final SubscriptionInfo sir = Utils.findRecordBySlotId(context, i);
718 if (sir != null) {
Sanket Padawe7e21fa52015-02-12 12:39:34 -0800719 addMobileTab(context, sir, (simCount > 1));
PauloftheWestc80b7612014-11-25 04:40:45 -0800720 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700721 }
PauloftheWestc80b7612014-11-25 04:40:45 -0800722
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700723 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700724 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
725 }
PauloftheWestc80b7612014-11-25 04:40:45 -0800726
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700727 if (mShowEthernet && hasEthernet(context)) {
728 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
729 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700730
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700731 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700732 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
733 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
734 if (mIntentTab != null) {
735 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700736 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700737 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700738 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700739 mTabHost.setCurrentTabByTag(mIntentTab);
740 }
741 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700742 } else if (noTabs) {
743 // no usable tabs, so hide body
744 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700745 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700746 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700747 }
748 }
749
Jeff Sharkey8a503642011-06-10 13:31:21 -0700750 /**
751 * Factory that provide empty {@link View} to make {@link TabHost} happy.
752 */
753 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700754 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700755 public View createTabContent(String tag) {
756 return new View(mTabHost.getContext());
757 }
758 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700759
Jeff Sharkey8a503642011-06-10 13:31:21 -0700760 /**
761 * Build {@link TabSpec} with thin indicator, and empty content.
762 */
763 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700764 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
765 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700766 }
767
PauloftheWest50e6eca2014-10-03 11:07:14 -0700768 /**
769 * Build {@link TabSpec} with thin indicator, and empty content.
770 */
Stuart Scottb1531812014-11-04 14:52:23 -0800771 private TabSpec buildTabSpec(String tag, CharSequence title) {
PauloftheWest50e6eca2014-10-03 11:07:14 -0700772 return mTabHost.newTabSpec(tag).setIndicator(title).setContent(
773 mEmptyTabContent);
774 }
775
776
Jeff Sharkey8a503642011-06-10 13:31:21 -0700777 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700778 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700779 public void onTabChanged(String tabId) {
780 // user changed tab; update body
781 updateBody();
782 }
783 };
784
785 /**
786 * Update body content based on current tab. Loads
787 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
788 * binds them to visible controls.
789 */
790 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700791 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700792 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700793
Jeff Sharkeya662e492011-06-18 21:57:06 -0700794 final Context context = getActivity();
PauloftheWestc80b7612014-11-25 04:40:45 -0800795 final Resources resources = context.getResources();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700796 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700797 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700798
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700799 if (currentTab == null) {
800 Log.w(TAG, "no tab selected; hiding body");
801 mListView.setVisibility(View.GONE);
802 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700803 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700804 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700805 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700806
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700807 mCurrentTab = currentTab;
808
Jeff Sharkey8a503642011-06-10 13:31:21 -0700809 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
810
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700811 mDataEnabledSupported = isOwner;
812 mDisableAtLimitSupported = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700813
Wink Savillefcec91f2014-12-02 17:12:08 -0800814 // TODO: remove mobile tabs when SIM isn't ready probably by
815 // TODO: using SubscriptionManager.getActiveSubscriptionInfoList.
816 if (LOGD) Log.d(TAG, "updateBody() isMobileTab=" + isMobileTab(currentTab));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700817
PauloftheWest50e6eca2014-10-03 11:07:14 -0700818 if (isMobileTab(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800819 if (LOGD) Log.d(TAG, "updateBody() mobile tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700820 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
821 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
PauloftheWest50e6eca2014-10-03 11:07:14 -0700822 mDataEnabledSupported = isMobileDataAvailable(getSubId(currentTab));
Jeff Sharkey4e2d16c2014-12-03 13:44:26 -0800823
824 // Match mobile traffic for this subscriber, but normalize it to
825 // catch any other merged subscribers.
826 mTemplate = buildTemplateMobileAll(
827 getActiveSubscriberId(context, getSubId(currentTab)));
828 mTemplate = NetworkTemplate.normalize(mTemplate,
829 mTelephonyManager.getMergedSubscriberIds());
830
Jeff Sharkey8a503642011-06-10 13:31:21 -0700831 } else if (TAB_3G.equals(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800832 if (LOGD) Log.d(TAG, "updateBody() 3g tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700833 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
834 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
835 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700836 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700837
838 } else if (TAB_4G.equals(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800839 if (LOGD) Log.d(TAG, "updateBody() 4g tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700840 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
841 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
842 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700843 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700844
845 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700846 // wifi doesn't have any controls
Wink Savillefcec91f2014-12-02 17:12:08 -0800847 if (LOGD) Log.d(TAG, "updateBody() wifi tab");
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700848 mDataEnabledSupported = false;
849 mDisableAtLimitSupported = false;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700850 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700851
852 } else if (TAB_ETHERNET.equals(currentTab)) {
853 // ethernet doesn't have any controls
Wink Savillefcec91f2014-12-02 17:12:08 -0800854 if (LOGD) Log.d(TAG, "updateBody() ethernet tab");
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700855 mDataEnabledSupported = false;
856 mDisableAtLimitSupported = false;
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700857 mTemplate = buildTemplateEthernet();
858
859 } else {
Wink Savillefcec91f2014-12-02 17:12:08 -0800860 if (LOGD) Log.d(TAG, "updateBody() unknown tab");
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700861 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700862 }
863
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700864 // kick off loader for network history
865 // TODO: consider chaining two loaders together instead of reloading
866 // network history when showing app detail.
867 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700868 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700869
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700870 // detail mode can change visible menus, invalidate
871 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700872
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700873 mBinding = false;
PauloftheWestc80b7612014-11-25 04:40:45 -0800874
875 int seriesColor = resources.getColor(R.color.sim_noitification);
876 if (mCurrentTab != null && mCurrentTab.length() > TAB_MOBILE.length() ){
877 final int slotId = Integer.parseInt(mCurrentTab.substring(TAB_MOBILE.length(),
878 mCurrentTab.length()));
879 final SubscriptionInfo sir = com.android.settings.Utils.findRecordBySlotId(context,
880 slotId);
881
882 if (sir != null) {
883 seriesColor = sir.getIconTint();
884 }
885 }
886
Jeff Sharkey9c167e92015-01-26 15:15:59 -0700887 final int secondaryColor = Color.argb(127, Color.red(seriesColor), Color.green(seriesColor),
888 Color.blue(seriesColor));
889 mSeries.setChartColor(Color.BLACK, seriesColor, secondaryColor);
890 mDetailedSeries.setChartColor(Color.BLACK, seriesColor, secondaryColor);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700891 }
892
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700893 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700894 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700895 }
896
897 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700898 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700899 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700900 */
901 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700902 final Context context = getActivity();
903 final PackageManager pm = context.getPackageManager();
904 final LayoutInflater inflater = getActivity().getLayoutInflater();
905
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700906 if (isAppDetailMode()) {
907 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700908 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700909 } else {
910 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700911 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700912
913 // hide detail stats when not in detail mode
914 mChart.bindDetailNetworkStats(null);
915 return;
916 }
917
918 // remove warning/limit sweeps while in detail mode
919 mChart.bindNetworkPolicy(null);
920
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700921 // show icon and all labels appearing under this app
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700922 final int uid = mCurrentApp.key;
923 final UidDetail detail = mUidDetailProvider.getUidDetail(uid, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700924 mAppIcon.setImageDrawable(detail.icon);
925
926 mAppTitles.removeAllViews();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700927
928 View title = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700929 if (detail.detailLabels != null) {
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100930 final int n = detail.detailLabels.length;
931 for (int i = 0; i < n; ++i) {
932 CharSequence label = detail.detailLabels[i];
933 CharSequence contentDescription = detail.detailContentDescriptions[i];
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700934 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100935 TextView appTitle = (TextView) title.findViewById(R.id.app_title);
936 appTitle.setText(label);
937 appTitle.setContentDescription(contentDescription);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700938 mAppTitles.addView(title);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700939 }
940 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700941 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100942 TextView appTitle = (TextView) title.findViewById(R.id.app_title);
943 appTitle.setText(detail.label);
944 appTitle.setContentDescription(detail.contentDescription);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700945 mAppTitles.addView(title);
946 }
947
948 // Remember last slot for summary
949 if (title != null) {
950 mAppTotal = (TextView) title.findViewById(R.id.app_summary);
951 } else {
952 mAppTotal = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700953 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700954
955 // enable settings button when package provides it
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700956 final String[] packageNames = pm.getPackagesForUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700957 if (packageNames != null && packageNames.length > 0) {
958 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700959 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
960
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700961 // Search for match across all packages
962 boolean matchFound = false;
963 for (String packageName : packageNames) {
964 mAppSettingsIntent.setPackage(packageName);
965 if (pm.resolveActivity(mAppSettingsIntent, 0) != null) {
966 matchFound = true;
967 break;
968 }
969 }
970
Zoltan Szatmary-Banf20d39e2014-08-07 15:27:08 +0100971 mAppSettings.setOnClickListener(new OnClickListener() {
972 @Override
973 public void onClick(View v) {
974 if (!isAdded()) {
975 return;
976 }
977
978 // TODO: target towards entire UID instead of just first package
979 getActivity().startActivityAsUser(mAppSettingsIntent,
980 new UserHandle(UserHandle.getUserId(uid)));
981 }
982 });
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700983 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -0700984 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700985
986 } else {
987 mAppSettingsIntent = null;
Zoltan Szatmary-Banf20d39e2014-08-07 15:27:08 +0100988 mAppSettings.setOnClickListener(null);
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700989 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700990 }
991
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700992 updateDetailData();
993
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700994 if (UserHandle.isApp(uid) && !mPolicyManager.getRestrictBackground()
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700995 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700996 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800997 setPreferenceSummary(mAppRestrictView,
998 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700999
1000 mAppRestrictView.setVisibility(View.VISIBLE);
1001 mAppRestrict.setChecked(getAppRestrictBackground());
1002
1003 } else {
1004 mAppRestrictView.setVisibility(View.GONE);
1005 }
1006 }
1007
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001008 private void setPolicyWarningBytes(long warningBytes) {
1009 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -07001010 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001011 updatePolicy(false);
1012 }
1013
1014 private void setPolicyLimitBytes(long limitBytes) {
1015 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -07001016 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001017 updatePolicy(false);
1018 }
1019
Wink Savilleb003a852014-10-23 10:16:26 -07001020 private boolean isMobileDataEnabled(int subId) {
Wink Savillea4f4d182014-12-05 15:34:46 -08001021 if (LOGD) Log.d(TAG, "isMobileDataEnabled:+ subId=" + subId);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001022 boolean isEnable = false;
Sanket Padawed819270f2015-01-14 11:03:33 -08001023 if (mMobileDataEnabled.get(String.valueOf(subId)) != null) {
1024 //TODO: deprecate and remove this once enabled flag is on policy
1025 //Multiple Subscriptions, the value need to be reseted
1026 isEnable = mMobileDataEnabled.get(String.valueOf(subId)).booleanValue();
Wink Savillea4f4d182014-12-05 15:34:46 -08001027 if (LOGD) {
1028 Log.d(TAG, "isMobileDataEnabled: != null, subId=" + subId
1029 + " isEnable=" + isEnable);
1030 }
Sanket Padawed819270f2015-01-14 11:03:33 -08001031 mMobileDataEnabled.put(String.valueOf(subId), null);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001032 } else {
Wink Savillea4f4d182014-12-05 15:34:46 -08001033 // SUB SELECT
1034 isEnable = mTelephonyManager.getDataEnabled(subId);
1035 if (LOGD) {
1036 Log.d(TAG, "isMobileDataEnabled: == null, subId=" + subId
1037 + " isEnable=" + isEnable);
1038 }
Jeff Sharkey28130d92011-09-02 16:10:24 -07001039 }
PauloftheWest50e6eca2014-10-03 11:07:14 -07001040 return isEnable;
Jeff Sharkey28130d92011-09-02 16:10:24 -07001041 }
1042
Sanket Padawe24f834d2015-01-08 11:23:11 -08001043 private void setMobileDataEnabled(int subId, boolean enabled) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001044 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
Sanket Padawe24f834d2015-01-08 11:23:11 -08001045 mTelephonyManager.setDataEnabled(subId, enabled);
Sanket Padawed819270f2015-01-14 11:03:33 -08001046 mMobileDataEnabled.put(String.valueOf(subId), enabled);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001047 updatePolicy(false);
1048 }
1049
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001050 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001051 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked()
1052 && ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001053 }
1054
1055 private boolean isBandwidthControlEnabled() {
1056 try {
1057 return mNetworkService.isBandwidthControlEnabled();
1058 } catch (RemoteException e) {
1059 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
1060 return false;
1061 }
1062 }
1063
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001064 public void setRestrictBackground(boolean restrictBackground) {
1065 mPolicyManager.setRestrictBackground(restrictBackground);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001066 updateMenuTitles();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001067 }
1068
1069 private boolean getAppRestrictBackground() {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001070 final int uid = mCurrentApp.key;
1071 final int uidPolicy = mPolicyManager.getUidPolicy(uid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001072 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
1073 }
1074
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001075 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001076 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001077 final int uid = mCurrentApp.key;
1078 mPolicyManager.setUidPolicy(
1079 uid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001080 mAppRestrict.setChecked(restrictBackground);
1081 }
1082
Jeff Sharkey8a503642011-06-10 13:31:21 -07001083 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001084 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
1085 * current {@link #mTemplate}.
1086 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001087 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001088 boolean dataEnabledVisible = mDataEnabledSupported;
1089 boolean disableAtLimitVisible = mDisableAtLimitSupported;
1090
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001091 if (isAppDetailMode()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001092 dataEnabledVisible = false;
1093 disableAtLimitVisible = false;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001094 }
1095
Jeff Sharkey28130d92011-09-02 16:10:24 -07001096 // TODO: move enabled state directly into policy
PauloftheWest50e6eca2014-10-03 11:07:14 -07001097 if (isMobileTab(mCurrentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001098 mBinding = true;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001099 mDataEnabled.setChecked(isMobileDataEnabled(getSubId(mCurrentTab)));
Jeff Sharkey28130d92011-09-02 16:10:24 -07001100 mBinding = false;
1101 }
1102
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001103 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001104 //SUB SELECT
1105 if (isNetworkPolicyModifiable(policy) && isMobileDataAvailable(getSubId(mCurrentTab))) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001106 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001107 if (!isAppDetailMode()) {
1108 mChart.bindNetworkPolicy(policy);
1109 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001110
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001111 } else {
1112 // controls are disabled; don't bind warning/limit sweeps
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001113 disableAtLimitVisible = false;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001114 mChart.bindNetworkPolicy(null);
1115 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001116
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001117 mDataEnabledView.setVisibility(dataEnabledVisible ? View.VISIBLE : View.GONE);
1118 mDisableAtLimitView.setVisibility(disableAtLimitVisible ? View.VISIBLE : View.GONE);
1119
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001120 if (refreshCycle) {
1121 // generate cycle list based on policy and available history
1122 updateCycleList(policy);
1123 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001124 }
1125
1126 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001127 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
1128 * and available {@link NetworkStatsHistory} data. Always selects the newest
1129 * item, updating the inspection range on {@link #mChart}.
1130 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001131 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001132 // stash away currently selected cycle to try restoring below
1133 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -07001134 mCycleAdapter.clear();
1135
1136 final Context context = mCycleSpinner.getContext();
1137
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001138 long historyStart = Long.MAX_VALUE;
1139 long historyEnd = Long.MIN_VALUE;
1140 if (mChartData != null) {
1141 historyStart = mChartData.network.getStart();
1142 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001143 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001144
Jeff Sharkey461842a2011-09-25 18:22:48 -07001145 final long now = System.currentTimeMillis();
1146 if (historyStart == Long.MAX_VALUE) historyStart = now;
1147 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001148
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001149 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001150 if (policy != null) {
1151 // find the next cycle boundary
1152 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001153
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001154 // walk backwards, generating all valid cycle ranges
1155 while (cycleEnd > historyStart) {
1156 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
1157 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
1158 + historyStart);
1159 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1160 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001161 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001162 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001163
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001164 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001165 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001166 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001167
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001168 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001169 // no policy defined cycles; show entry for each four-week period
1170 long cycleEnd = historyEnd;
1171 while (cycleEnd > historyStart) {
1172 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
1173 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1174 cycleEnd = cycleStart;
1175 }
1176
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001177 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001178 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001179
1180 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001181 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001182 final int position = mCycleAdapter.findNearestPosition(previousItem);
1183 mCycleSpinner.setSelection(position);
1184
1185 // only force-update cycle when changed; skipping preserves any
1186 // user-defined inspection region.
1187 final CycleItem selectedItem = mCycleAdapter.getItem(position);
1188 if (!Objects.equal(selectedItem, previousItem)) {
1189 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1190 } else {
1191 // but still kick off loader for detailed list
1192 updateDetailData();
1193 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001194 } else {
1195 updateDetailData();
1196 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001197 }
1198
Sanket Padawed819270f2015-01-14 11:03:33 -08001199 private void disableDataForOtherSubscriptions(SubscriptionInfo currentSir) {
1200 if (mSubInfoList != null) {
1201 for (SubscriptionInfo subInfo : mSubInfoList) {
1202 if (subInfo.getSubscriptionId() != currentSir.getSubscriptionId()) {
1203 setMobileDataEnabled(subInfo.getSubscriptionId(), false);
1204 }
1205 }
1206 }
1207 }
1208
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001209 private View.OnClickListener mDataEnabledListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001210 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001211 public void onClick(View v) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001212 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001213
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001214 final boolean dataEnabled = !mDataEnabled.isChecked();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001215 final String currentTab = mCurrentTab;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001216 if (isMobileTab(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001217 if (dataEnabled) {
PauloftheWestde338442014-10-29 14:46:44 -07001218 // If we are showing the Sim Card tile then we are a Multi-Sim device.
1219 if (Utils.showSimCardTile(getActivity())) {
1220 handleMultiSimDataDialog();
1221 } else {
Sanket Padawe24f834d2015-01-08 11:23:11 -08001222 setMobileDataEnabled(getSubId(currentTab), true);
PauloftheWestde338442014-10-29 14:46:44 -07001223 }
Jeff Sharkey28130d92011-09-02 16:10:24 -07001224 } else {
1225 // disabling data; show confirmation dialog which eventually
1226 // calls setMobileDataEnabled() once user confirms.
Sanket Padawe24f834d2015-01-08 11:23:11 -08001227 ConfirmDataDisableFragment.show(DataUsageSummary.this, getSubId(mCurrentTab));
Jeff Sharkey28130d92011-09-02 16:10:24 -07001228 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001229 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001230
Jeff Sharkey28130d92011-09-02 16:10:24 -07001231 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001232 }
1233 };
1234
PauloftheWestde338442014-10-29 14:46:44 -07001235 private void handleMultiSimDataDialog() {
PauloftheWestf0d84e22014-12-10 08:50:54 -08001236 final Context context = getActivity();
1237 final SubscriptionInfo currentSir = getCurrentTabSubInfo(context);
Sanket Padawe766ab602015-01-20 10:36:15 -08001238
1239 //If sim has not loaded after toggling data switch, return.
1240 if (currentSir == null) {
1241 return;
1242 }
1243
Wink Saville0183fb52014-11-22 10:11:39 -08001244 final SubscriptionInfo nextSir = mSubscriptionManager.getActiveSubscriptionInfo(
1245 mSubscriptionManager.getDefaultDataSubId());
PauloftheWestde338442014-10-29 14:46:44 -07001246
PauloftheWestf0d84e22014-12-10 08:50:54 -08001247 // If the device is single SIM or is enabling data on the active data SIM then forgo
1248 // the pop-up.
1249 if (!Utils.showSimCardTile(context) ||
Sanket Padawed819270f2015-01-14 11:03:33 -08001250 (nextSir != null && currentSir != null &&
1251 currentSir.getSubscriptionId() == nextSir.getSubscriptionId())) {
Sanket Padawe24f834d2015-01-08 11:23:11 -08001252 setMobileDataEnabled(currentSir.getSubscriptionId(), true);
Sanket Padawed819270f2015-01-14 11:03:33 -08001253 if (nextSir != null && currentSir != null &&
1254 currentSir.getSubscriptionId() == nextSir.getSubscriptionId()) {
1255 disableDataForOtherSubscriptions(currentSir);
1256 }
PauloftheWestde338442014-10-29 14:46:44 -07001257 updateBody();
1258 return;
1259 }
1260
PauloftheWestf0d84e22014-12-10 08:50:54 -08001261 final String previousName = (nextSir == null)
1262 ? context.getResources().getString(R.string.sim_selection_required_pref)
1263 : nextSir.getDisplayName().toString();
1264
PauloftheWestde338442014-10-29 14:46:44 -07001265 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
1266
1267 builder.setTitle(R.string.sim_change_data_title);
1268 builder.setMessage(getActivity().getResources().getString(R.string.sim_change_data_message,
PauloftheWestf0d84e22014-12-10 08:50:54 -08001269 currentSir.getDisplayName(), previousName));
PauloftheWestde338442014-10-29 14:46:44 -07001270
1271 builder.setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
1272 @Override
1273 public void onClick(DialogInterface dialog, int id) {
Wink Saville0183fb52014-11-22 10:11:39 -08001274 mSubscriptionManager.setDefaultDataSubId(currentSir.getSubscriptionId());
Sanket Padawe24f834d2015-01-08 11:23:11 -08001275 setMobileDataEnabled(currentSir.getSubscriptionId(), true);
Sanket Padawed819270f2015-01-14 11:03:33 -08001276 disableDataForOtherSubscriptions(currentSir);
PauloftheWestde338442014-10-29 14:46:44 -07001277 updateBody();
1278 }
1279 });
1280 builder.setNegativeButton(R.string.cancel, null);
1281
1282 builder.create().show();
1283 }
1284
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001285 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001286 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001287 public void onClick(View v) {
1288 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001289 if (disableAtLimit) {
1290 // enabling limit; show confirmation dialog which eventually
1291 // calls setPolicyLimitBytes() once user confirms.
1292 ConfirmLimitFragment.show(DataUsageSummary.this);
1293 } else {
1294 setPolicyLimitBytes(LIMIT_DISABLED);
1295 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001296 }
1297 };
1298
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001299 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001300 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001301 public void onClick(View v) {
1302 final boolean restrictBackground = !mAppRestrict.isChecked();
1303
1304 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001305 // enabling restriction; show confirmation dialog which
1306 // eventually calls setRestrictBackground() once user
1307 // confirms.
1308 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001309 } else {
1310 setAppRestrictBackground(false);
1311 }
1312 }
1313 };
1314
Jeff Sharkey8a503642011-06-10 13:31:21 -07001315 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001316 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001317 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001318 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001319 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001320
1321 // TODO: sigh, remove this hack once we understand 6450986
1322 if (mUidDetailProvider == null || app == null) return;
1323
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001324 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001325 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001326 }
1327 };
1328
1329 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001330 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001331 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1332 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1333 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001334 // show cycle editor; will eventually call setPolicyCycleDay()
1335 // when user finishes editing.
1336 CycleEditorFragment.show(DataUsageSummary.this);
1337
1338 // reset spinner to something other than "change cycle..."
1339 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001340
1341 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001342 if (LOGD) {
1343 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1344 + cycle.end + "]");
1345 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001346
1347 // update chart to show selected cycle, and update detail data
1348 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001349 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001350
1351 updateDetailData();
1352 }
1353 }
1354
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001355 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001356 public void onNothingSelected(AdapterView<?> parent) {
1357 // ignored
1358 }
1359 };
1360
1361 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001362 * Update details based on {@link #mChart} inspection range depending on
1363 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1364 * of applications data usage, and when {@link #isAppDetailMode()} update
1365 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001366 */
1367 private void updateDetailData() {
1368 if (LOGD) Log.d(TAG, "updateDetailData()");
1369
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001370 final long start = mChart.getInspectStart();
1371 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001372 final long now = System.currentTimeMillis();
1373
1374 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001375
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001376 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001377 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001378 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001379 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001380 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001381 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001382 final long foregroundBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001383 final long totalBytes = defaultBytes + foregroundBytes;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001384
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001385 if (mAppTotal != null) {
1386 mAppTotal.setText(Formatter.formatFileSize(context, totalBytes));
1387 }
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001388 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1389 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1390
1391 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001392 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001393
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001394 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001395
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001396 mCycleSummary.setVisibility(View.GONE);
1397
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001398 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001399 if (mChartData != null) {
1400 entry = mChartData.network.getValues(start, end, now, null);
1401 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001402
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001403 mCycleSummary.setVisibility(View.VISIBLE);
1404
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001405 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001406 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001407 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001408 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001409
1410 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1411 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001412 mCycleSummary.setText(totalPhrase);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001413
PauloftheWest50e6eca2014-10-03 11:07:14 -07001414 if (isMobileTab(mCurrentTab) || TAB_3G.equals(mCurrentTab)
László Dávid0b8bf4e2012-10-24 23:31:47 +02001415 || TAB_4G.equals(mCurrentTab)) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001416 if (isAppDetailMode()) {
1417 mDisclaimer.setVisibility(View.GONE);
1418 } else {
1419 mDisclaimer.setVisibility(View.VISIBLE);
1420 }
Jeff Sharkeye557c332012-04-13 16:04:07 -07001421 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001422 mDisclaimer.setVisibility(View.GONE);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001423 }
1424
Jeff Sharkey92811822012-05-04 11:47:29 -07001425 // initial layout is finished above, ensure we have transitions
1426 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001427 }
1428
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001429 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1430 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001431 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001432 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001433 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001434 }
1435
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001436 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001437 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1438 mChartData = data;
1439 mChart.bindNetworkStats(mChartData.network);
1440 mChart.bindDetailNetworkStats(mChartData.detail);
1441
1442 // calcuate policy cycles based on available data
1443 updatePolicy(true);
1444 updateAppDetail();
1445
1446 // force scroll to top of body when showing detail
1447 if (mChartData.detail != null) {
1448 mListView.smoothScrollToPosition(0);
1449 }
1450 }
1451
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001452 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001453 public void onLoaderReset(Loader<ChartData> loader) {
1454 mChartData = null;
1455 mChart.bindNetworkStats(null);
1456 mChart.bindDetailNetworkStats(null);
1457 }
1458 };
1459
1460 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001461 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001462 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001463 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001464 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001465 }
1466
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001467 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001468 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001469 final int[] restrictedUids = mPolicyManager.getUidsWithPolicy(
Jeff Sharkeye557c332012-04-13 16:04:07 -07001470 POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001471 mAdapter.bindStats(data, restrictedUids);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001472 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001473 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001474
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001475 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001476 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001477 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001478 updateEmptyVisible();
1479 }
1480
1481 private void updateEmptyVisible() {
1482 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1483 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001484 mStupidPadding.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001485 }
1486 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001487
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001488 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001489 final TelephonyManager tele = TelephonyManager.from(context);
1490 final String actualSubscriberId = tele.getSubscriberId();
Wink Savillefcec91f2014-12-02 17:12:08 -08001491 String retVal = SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
1492 if (LOGD) Log.d(TAG, "getActiveSubscriberId=" + retVal + " actualSubscriberId=" + actualSubscriberId);
1493 return retVal;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001494 }
1495
Wink Savilleb003a852014-10-23 10:16:26 -07001496 private static String getActiveSubscriberId(Context context, int subId) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07001497 final TelephonyManager tele = TelephonyManager.from(context);
Wink Savillefcec91f2014-12-02 17:12:08 -08001498 String retVal = tele.getSubscriberId(subId);
1499 if (LOGD) Log.d(TAG, "getActiveSubscriberId=" + retVal + " subId=" + subId);
1500 return retVal;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001501 }
1502
Jeff Sharkey8a503642011-06-10 13:31:21 -07001503 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001504 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001505 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001506 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001507 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001508
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001509 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001510 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001511 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001512 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001513
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001514 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001515 public void requestWarningEdit() {
1516 WarningEditorFragment.show(DataUsageSummary.this);
1517 }
1518
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001519 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001520 public void requestLimitEdit() {
1521 LimitEditorFragment.show(DataUsageSummary.this);
1522 }
1523 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001524
1525 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001526 * List item that reflects a specific data usage cycle.
1527 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001528 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001529 public CharSequence label;
1530 public long start;
1531 public long end;
1532
Jeff Sharkey8a503642011-06-10 13:31:21 -07001533 CycleItem(CharSequence label) {
1534 this.label = label;
1535 }
1536
1537 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001538 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001539 this.start = start;
1540 this.end = end;
1541 }
1542
Jeff Sharkey8a503642011-06-10 13:31:21 -07001543 @Override
1544 public String toString() {
1545 return label.toString();
1546 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001547
1548 @Override
1549 public boolean equals(Object o) {
1550 if (o instanceof CycleItem) {
1551 final CycleItem another = (CycleItem) o;
1552 return start == another.start && end == another.end;
1553 }
1554 return false;
1555 }
1556
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001557 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001558 public int compareTo(CycleItem another) {
1559 return Long.compare(start, another.start);
1560 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001561 }
1562
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001563 private static final StringBuilder sBuilder = new StringBuilder(50);
1564 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1565 sBuilder, Locale.getDefault());
1566
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001567 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001568 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001569
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001570 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001571 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001572 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1573 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001574 }
1575 }
1576
Jeff Sharkey8a503642011-06-10 13:31:21 -07001577 /**
1578 * Special-case data usage cycle that triggers dialog to change
1579 * {@link NetworkPolicy#cycleDay}.
1580 */
1581 public static class CycleChangeItem extends CycleItem {
1582 public CycleChangeItem(Context context) {
1583 super(context.getString(R.string.data_usage_change_cycle));
1584 }
1585 }
1586
1587 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001588 private boolean mChangePossible = false;
1589 private boolean mChangeVisible = false;
1590
1591 private final CycleChangeItem mChangeItem;
1592
Jeff Sharkey8a503642011-06-10 13:31:21 -07001593 public CycleAdapter(Context context) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001594 super(context, R.layout.data_usage_cycle_item);
1595 setDropDownViewResource(R.layout.data_usage_cycle_item_dropdown);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001596 mChangeItem = new CycleChangeItem(context);
1597 }
1598
1599 public void setChangePossible(boolean possible) {
1600 mChangePossible = possible;
1601 updateChange();
1602 }
1603
1604 public void setChangeVisible(boolean visible) {
1605 mChangeVisible = visible;
1606 updateChange();
1607 }
1608
1609 private void updateChange() {
1610 remove(mChangeItem);
1611 if (mChangePossible && mChangeVisible) {
1612 add(mChangeItem);
1613 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001614 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001615
1616 /**
1617 * Find position of {@link CycleItem} in this adapter which is nearest
1618 * the given {@link CycleItem}.
1619 */
1620 public int findNearestPosition(CycleItem target) {
1621 if (target != null) {
1622 final int count = getCount();
1623 for (int i = count - 1; i >= 0; i--) {
1624 final CycleItem item = getItem(i);
1625 if (item instanceof CycleChangeItem) {
1626 continue;
1627 } else if (item.compareTo(target) >= 0) {
1628 return i;
1629 }
1630 }
1631 }
1632 return 0;
1633 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001634 }
1635
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001636 public static class AppItem implements Comparable<AppItem>, Parcelable {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001637 public static final int CATEGORY_USER = 0;
1638 public static final int CATEGORY_APP_TITLE = 1;
1639 public static final int CATEGORY_APP = 2;
1640
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001641 public final int key;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001642 public boolean restricted;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001643 public int category;
1644
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001645 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001646 public long total;
1647
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001648 public AppItem() {
1649 this.key = 0;
1650 }
1651
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001652 public AppItem(int key) {
1653 this.key = key;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001654 }
1655
1656 public AppItem(Parcel parcel) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001657 key = parcel.readInt();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001658 uids = parcel.readSparseBooleanArray();
1659 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001660 }
1661
1662 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001663 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001664 }
1665
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001666 @Override
1667 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001668 dest.writeInt(key);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001669 dest.writeSparseBooleanArray(uids);
1670 dest.writeLong(total);
1671 }
1672
1673 @Override
1674 public int describeContents() {
1675 return 0;
1676 }
1677
1678 @Override
1679 public int compareTo(AppItem another) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001680 int comparison = Integer.compare(category, another.category);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001681 if (comparison == 0) {
1682 comparison = Long.compare(another.total, total);
1683 }
1684 return comparison;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001685 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001686
1687 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1688 @Override
1689 public AppItem createFromParcel(Parcel in) {
1690 return new AppItem(in);
1691 }
1692
1693 @Override
1694 public AppItem[] newArray(int size) {
1695 return new AppItem[size];
1696 }
1697 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001698 }
1699
Jeff Sharkey8a503642011-06-10 13:31:21 -07001700 /**
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);
1879 if (getItemViewType(position) == 1) {
1880 if (convertView == null) {
Zoltan Szatmary-Ban3af2e4c2014-12-19 17:17:23 +00001881 convertView = Utils.inflateCategoryHeader(LayoutInflater.from(
1882 parent.getContext()), 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) {
1890 convertView = LayoutInflater.from(parent.getContext()).inflate(
1891 R.layout.data_usage_item, parent, false);
1892
1893 if (mInsetSide > 0) {
1894 convertView.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
1895 }
1896 }
1897
1898 final Context context = parent.getContext();
1899
1900 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
1901 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1902 android.R.id.progress);
1903
1904 // kick off async load of app details
1905 UidDetailTask.bindView(mProvider, item, convertView);
1906
1907 if (item.restricted && item.total <= 0) {
1908 text1.setText(R.string.data_usage_app_restricted);
1909 progress.setVisibility(View.GONE);
1910 } else {
1911 text1.setText(Formatter.formatFileSize(context, item.total));
1912 progress.setVisibility(View.VISIBLE);
1913 }
1914
1915 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1916 progress.setProgress(percentTotal);
1917 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001918
1919 return convertView;
1920 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001921 }
1922
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001923 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001924 * Empty {@link Fragment} that controls display of UID details in
1925 * {@link DataUsageSummary}.
1926 */
1927 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001928 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001929
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001930 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jason Monk4bb075b2015-03-17 10:06:58 -04001931 show(parent, app, label, true);
1932 }
1933
1934 public static void show(DataUsageSummary parent, AppItem app, CharSequence label,
1935 boolean addToBack) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001936 if (!parent.isAdded()) return;
1937
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001938 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001939 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001940
1941 final AppDetailsFragment fragment = new AppDetailsFragment();
1942 fragment.setArguments(args);
1943 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001944 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1945 ft.add(fragment, TAG_APP_DETAILS);
Jason Monk4bb075b2015-03-17 10:06:58 -04001946 if (addToBack) {
1947 ft.addToBackStack(TAG_APP_DETAILS);
1948 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001949 ft.setBreadCrumbTitle(
1950 parent.getResources().getString(R.string.data_usage_app_summary_title));
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001951 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001952 }
1953
1954 @Override
1955 public void onStart() {
1956 super.onStart();
1957 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001958 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001959 target.updateBody();
1960 }
1961
1962 @Override
1963 public void onStop() {
1964 super.onStop();
1965 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001966 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001967 target.updateBody();
1968 }
1969 }
1970
1971 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001972 * Dialog to request user confirmation before setting
1973 * {@link NetworkPolicy#limitBytes}.
1974 */
1975 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001976 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001977 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001978
1979 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001980 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001981
Jeff Sharkeyd0a55312014-08-08 10:25:24 -07001982 final NetworkPolicy policy = parent.mPolicyEditor.getPolicy(parent.mTemplate);
1983 if (policy == null) return;
1984
Jeff Sharkey461842a2011-09-25 18:22:48 -07001985 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001986 final CharSequence message;
Jeff Sharkeyd0a55312014-08-08 10:25:24 -07001987 final long minLimitBytes = (long) (policy.warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001988 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001989
1990 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001991 final String currentTab = parent.mCurrentTab;
1992 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001993 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001994 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001995 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001996 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001997 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001998 } else if (isMobileTab(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001999 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07002000 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07002001 } else {
2002 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002003 }
2004
Jeff Sharkey131f9d62011-08-17 17:08:19 -07002005 final Bundle args = new Bundle();
2006 args.putCharSequence(EXTRA_MESSAGE, message);
2007 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
2008
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002009 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
2010 dialog.setArguments(args);
2011 dialog.setTargetFragment(parent, 0);
2012 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
2013 }
2014
2015 @Override
2016 public Dialog onCreateDialog(Bundle savedInstanceState) {
2017 final Context context = getActivity();
2018
Jeff Sharkey131f9d62011-08-17 17:08:19 -07002019 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002020 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
2021
2022 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2023 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07002024 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002025
2026 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002027 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002028 public void onClick(DialogInterface dialog, int which) {
2029 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2030 if (target != null) {
2031 target.setPolicyLimitBytes(limitBytes);
2032 }
2033 }
2034 });
2035
2036 return builder.create();
2037 }
2038 }
2039
2040 /**
2041 * Dialog to edit {@link NetworkPolicy#cycleDay}.
2042 */
2043 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002044 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002045
2046 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002047 if (!parent.isAdded()) return;
2048
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002049 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002050 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002051
2052 final CycleEditorFragment dialog = new CycleEditorFragment();
2053 dialog.setArguments(args);
2054 dialog.setTargetFragment(parent, 0);
2055 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
2056 }
2057
2058 @Override
2059 public Dialog onCreateDialog(Bundle savedInstanceState) {
2060 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002061 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2062 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002063
2064 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2065 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2066
2067 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
2068 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
2069
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002070 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2071 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002072
2073 cycleDayPicker.setMinValue(1);
2074 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002075 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002076 cycleDayPicker.setWrapSelectorWheel(true);
2077
2078 builder.setTitle(R.string.data_usage_cycle_editor_title);
2079 builder.setView(view);
2080
2081 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2082 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002083 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002084 public void onClick(DialogInterface dialog, int which) {
Jeff Sharkeyd277de92013-03-25 15:11:25 -07002085 // clear focus to finish pending text edits
2086 cycleDayPicker.clearFocus();
2087
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002088 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08002089 final String cycleTimezone = new Time().timezone;
2090 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002091 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002092 }
2093 });
2094
2095 return builder.create();
2096 }
2097 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002098
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002099 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002100 * Dialog to edit {@link NetworkPolicy#warningBytes}.
2101 */
2102 public static class WarningEditorFragment extends DialogFragment {
2103 private static final String EXTRA_TEMPLATE = "template";
2104
2105 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002106 if (!parent.isAdded()) return;
2107
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002108 final Bundle args = new Bundle();
2109 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
2110
2111 final WarningEditorFragment dialog = new WarningEditorFragment();
2112 dialog.setArguments(args);
2113 dialog.setTargetFragment(parent, 0);
2114 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
2115 }
2116
2117 @Override
2118 public Dialog onCreateDialog(Bundle savedInstanceState) {
2119 final Context context = getActivity();
2120 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2121 final NetworkPolicyEditor editor = target.mPolicyEditor;
2122
2123 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2124 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2125
2126 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
2127 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
2128
2129 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2130 final long warningBytes = editor.getPolicyWarningBytes(template);
2131 final long limitBytes = editor.getPolicyLimitBytes(template);
2132
2133 bytesPicker.setMinValue(0);
2134 if (limitBytes != LIMIT_DISABLED) {
2135 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
2136 } else {
2137 bytesPicker.setMaxValue(Integer.MAX_VALUE);
2138 }
2139 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
2140 bytesPicker.setWrapSelectorWheel(false);
2141
2142 builder.setTitle(R.string.data_usage_warning_editor_title);
2143 builder.setView(view);
2144
2145 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2146 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002147 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002148 public void onClick(DialogInterface dialog, int which) {
2149 // clear focus to finish pending text edits
2150 bytesPicker.clearFocus();
2151
2152 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
2153 editor.setPolicyWarningBytes(template, bytes);
2154 target.updatePolicy(false);
2155 }
2156 });
2157
2158 return builder.create();
2159 }
2160 }
2161
2162 /**
2163 * Dialog to edit {@link NetworkPolicy#limitBytes}.
2164 */
2165 public static class LimitEditorFragment extends DialogFragment {
2166 private static final String EXTRA_TEMPLATE = "template";
2167
2168 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002169 if (!parent.isAdded()) return;
2170
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002171 final Bundle args = new Bundle();
2172 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
2173
2174 final LimitEditorFragment dialog = new LimitEditorFragment();
2175 dialog.setArguments(args);
2176 dialog.setTargetFragment(parent, 0);
2177 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
2178 }
2179
2180 @Override
2181 public Dialog onCreateDialog(Bundle savedInstanceState) {
2182 final Context context = getActivity();
2183 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2184 final NetworkPolicyEditor editor = target.mPolicyEditor;
2185
2186 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2187 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2188
2189 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
2190 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
2191
2192 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2193 final long warningBytes = editor.getPolicyWarningBytes(template);
2194 final long limitBytes = editor.getPolicyLimitBytes(template);
2195
2196 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09002197 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002198 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
2199 } else {
2200 bytesPicker.setMinValue(0);
2201 }
2202 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
2203 bytesPicker.setWrapSelectorWheel(false);
2204
2205 builder.setTitle(R.string.data_usage_limit_editor_title);
2206 builder.setView(view);
2207
2208 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2209 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002210 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002211 public void onClick(DialogInterface dialog, int which) {
2212 // clear focus to finish pending text edits
2213 bytesPicker.clearFocus();
2214
2215 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
2216 editor.setPolicyLimitBytes(template, bytes);
2217 target.updatePolicy(false);
2218 }
2219 });
2220
2221 return builder.create();
2222 }
2223 }
2224 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07002225 * Dialog to request user confirmation before disabling data.
2226 */
2227 public static class ConfirmDataDisableFragment extends DialogFragment {
Sanket Padawe24f834d2015-01-08 11:23:11 -08002228 static int mSubId;
2229 public static void show(DataUsageSummary parent, int subId) {
2230 mSubId = subId;
Jeff Sharkey461842a2011-09-25 18:22:48 -07002231 if (!parent.isAdded()) return;
2232
Jeff Sharkey28130d92011-09-02 16:10:24 -07002233 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
2234 dialog.setTargetFragment(parent, 0);
2235 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
2236 }
2237
2238 @Override
2239 public Dialog onCreateDialog(Bundle savedInstanceState) {
2240 final Context context = getActivity();
2241
2242 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2243 builder.setMessage(R.string.data_usage_disable_mobile);
2244
2245 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002246 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07002247 public void onClick(DialogInterface dialog, int which) {
2248 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2249 if (target != null) {
2250 // TODO: extend to modify policy enabled flag.
Sanket Padawe24f834d2015-01-08 11:23:11 -08002251 target.setMobileDataEnabled(mSubId, false);
Jeff Sharkey28130d92011-09-02 16:10:24 -07002252 }
2253 }
2254 });
2255 builder.setNegativeButton(android.R.string.cancel, null);
2256
2257 return builder.create();
2258 }
2259 }
2260
2261 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002262 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07002263 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002264 */
2265 public static class ConfirmRestrictFragment extends DialogFragment {
2266 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002267 if (!parent.isAdded()) return;
2268
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002269 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
2270 dialog.setTargetFragment(parent, 0);
2271 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
2272 }
2273
2274 @Override
2275 public Dialog onCreateDialog(Bundle savedInstanceState) {
2276 final Context context = getActivity();
2277
2278 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002279 builder.setTitle(R.string.data_usage_restrict_background_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07002280 if (Utils.hasMultipleUsers(context)) {
2281 builder.setMessage(R.string.data_usage_restrict_background_multiuser);
2282 } else {
2283 builder.setMessage(R.string.data_usage_restrict_background);
2284 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002285
2286 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002287 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002288 public void onClick(DialogInterface dialog, int which) {
2289 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2290 if (target != null) {
2291 target.setRestrictBackground(true);
2292 }
2293 }
2294 });
2295 builder.setNegativeButton(android.R.string.cancel, null);
2296
2297 return builder.create();
2298 }
2299 }
2300
2301 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002302 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
2303 * change has been denied, usually based on
2304 * {@link DataUsageSummary#hasLimitedNetworks()}.
2305 */
2306 public static class DeniedRestrictFragment extends DialogFragment {
2307 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002308 if (!parent.isAdded()) return;
2309
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002310 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
2311 dialog.setTargetFragment(parent, 0);
2312 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
2313 }
2314
2315 @Override
2316 public Dialog onCreateDialog(Bundle savedInstanceState) {
2317 final Context context = getActivity();
2318
2319 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2320 builder.setTitle(R.string.data_usage_app_restrict_background);
2321 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
2322 builder.setPositiveButton(android.R.string.ok, null);
2323
2324 return builder.create();
2325 }
2326 }
2327
2328 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002329 * Dialog to request user confirmation before setting
2330 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
2331 */
2332 public static class ConfirmAppRestrictFragment extends DialogFragment {
2333 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002334 if (!parent.isAdded()) return;
2335
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002336 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2337 dialog.setTargetFragment(parent, 0);
2338 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2339 }
2340
2341 @Override
2342 public Dialog onCreateDialog(Bundle savedInstanceState) {
2343 final Context context = getActivity();
2344
2345 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002346 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2347 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2348
2349 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002350 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002351 public void onClick(DialogInterface dialog, int which) {
2352 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2353 if (target != null) {
2354 target.setAppRestrictBackground(true);
2355 }
2356 }
2357 });
2358 builder.setNegativeButton(android.R.string.cancel, null);
2359
2360 return builder.create();
2361 }
2362 }
2363
2364 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002365 * Compute default tab that should be selected, based on
2366 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2367 */
2368 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002369 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002370 if (template == null) {
Wink Savilleb003a852014-10-23 10:16:26 -07002371 final int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
Wink Saville0183fb52014-11-22 10:11:39 -08002372 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Savilled72c0622014-12-11 10:10:05 -08002373 if (SubscriptionManager.isValidSubscriptionId(subId)) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07002374 return TAB_MOBILE + String.valueOf(subId);
2375 }
2376 return null;
2377 }
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002378
2379 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002380 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002381 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002382 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002383 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002384 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002385 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002386 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002387 return TAB_WIFI;
2388 default:
2389 return null;
2390 }
2391 }
2392
2393 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002394 * Background task that loads {@link UidDetail}, binding to
2395 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002396 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002397 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2398 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002399 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002400 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002401
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002402 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002403 mProvider = checkNotNull(provider);
2404 mItem = checkNotNull(item);
2405 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002406 }
2407
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002408 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002409 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002410 final UidDetailTask existing = (UidDetailTask) target.getTag();
2411 if (existing != null) {
2412 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002413 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002414
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002415 final UidDetail cachedDetail = provider.getUidDetail(item.key, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002416 if (cachedDetail != null) {
2417 bindView(cachedDetail, target);
2418 } else {
2419 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2420 AsyncTask.THREAD_POOL_EXECUTOR));
2421 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002422 }
2423
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002424 private static void bindView(UidDetail detail, View target) {
2425 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2426 final TextView title = (TextView) target.findViewById(android.R.id.title);
2427
2428 if (detail != null) {
2429 icon.setImageDrawable(detail.icon);
2430 title.setText(detail.label);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +01002431 title.setContentDescription(detail.contentDescription);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002432 } else {
2433 icon.setImageDrawable(null);
2434 title.setText(null);
2435 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002436 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002437
2438 @Override
2439 protected void onPreExecute() {
2440 bindView(null, mTarget);
2441 }
2442
2443 @Override
2444 protected UidDetail doInBackground(Void... params) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002445 return mProvider.getUidDetail(mItem.key, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002446 }
2447
2448 @Override
2449 protected void onPostExecute(UidDetail result) {
2450 bindView(result, mTarget);
2451 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002452 }
2453
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002454 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002455 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002456 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002457 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002458 if (TEST_RADIOS) {
2459 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2460 }
2461
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002462 final ConnectivityManager conn = ConnectivityManager.from(context);
2463 final TelephonyManager tele = TelephonyManager.from(context);
2464
Wink Saville0183fb52014-11-22 10:11:39 -08002465 final List<SubscriptionInfo> subInfoList =
2466 SubscriptionManager.from(context).getActiveSubscriptionInfoList();
2467 // No activated Subscriptions
PauloftheWest50e6eca2014-10-03 11:07:14 -07002468 if (subInfoList == null) {
Wink Savillefcec91f2014-12-02 17:12:08 -08002469 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subInfoList=null");
PauloftheWest50e6eca2014-10-03 11:07:14 -07002470 return false;
2471 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002472 // require both supported network and ready SIM
PauloftheWest50e6eca2014-10-03 11:07:14 -07002473 boolean isReady = true;
Wink Savilleca756612014-11-08 10:47:12 -08002474 for (SubscriptionInfo subInfo : subInfoList) {
Stuart Scott3ada2ec2014-10-31 14:11:24 -07002475 isReady = isReady & tele.getSimState(subInfo.getSimSlotIndex()) == SIM_STATE_READY;
Wink Savillefcec91f2014-12-02 17:12:08 -08002476 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subInfo=" + subInfo);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002477 }
Wink Savillefcec91f2014-12-02 17:12:08 -08002478 boolean retVal = conn.isNetworkSupported(TYPE_MOBILE) && isReady;
2479 if (LOGD) {
2480 Log.d(TAG, "hasReadyMobileRadio:"
2481 + " conn.isNetworkSupported(TYPE_MOBILE)="
2482 + conn.isNetworkSupported(TYPE_MOBILE)
2483 + " isReady=" + isReady);
2484 }
2485 return retVal;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002486 }
2487
2488 /*
2489 * TODO: consider adding to TelephonyManager or SubscritpionManager.
2490 */
Wink Savilleb003a852014-10-23 10:16:26 -07002491 public static boolean hasReadyMobileRadio(Context context, int subId) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07002492 if (TEST_RADIOS) {
2493 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2494 }
2495
2496 final ConnectivityManager conn = ConnectivityManager.from(context);
2497 final TelephonyManager tele = TelephonyManager.from(context);
2498 final int slotId = SubscriptionManager.getSlotId(subId);
2499 final boolean isReady = tele.getSimState(slotId) == SIM_STATE_READY;
2500
Wink Savillefcec91f2014-12-02 17:12:08 -08002501 boolean retVal = conn.isNetworkSupported(TYPE_MOBILE) && isReady;
2502 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subId=" + subId
2503 + " conn.isNetworkSupported(TYPE_MOBILE)=" + conn.isNetworkSupported(TYPE_MOBILE)
2504 + " isReady=" + isReady);
2505 return retVal;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002506 }
2507
2508 /**
2509 * Test if device has a mobile 4G data radio.
2510 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002511 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002512 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2513 return false;
2514 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002515 if (TEST_RADIOS) {
2516 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2517 }
2518
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002519 final ConnectivityManager conn = ConnectivityManager.from(context);
2520 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002521
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002522 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Wink Saville55434042012-06-14 12:33:43 -07002523 final boolean hasLte = (tele.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE)
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002524 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002525 return hasWimax || hasLte;
2526 }
2527
2528 /**
2529 * Test if device has a Wi-Fi data radio.
2530 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002531 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002532 if (TEST_RADIOS) {
2533 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2534 }
2535
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002536 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002537 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002538 }
2539
2540 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002541 * Test if device has an ethernet network connection.
2542 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002543 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002544 if (TEST_RADIOS) {
2545 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2546 }
2547
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002548 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002549 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2550
2551 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002552 if (mStatsSession != null) {
2553 try {
2554 ethernetBytes = mStatsSession.getSummaryForNetwork(
2555 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2556 .getTotalBytes();
2557 } catch (RemoteException e) {
2558 throw new RuntimeException(e);
2559 }
2560 } else {
2561 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002562 }
2563
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002564 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002565 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002566 }
2567
2568 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002569 * Inflate a {@link Preference} style layout, adding the given {@link View}
2570 * widget into {@link android.R.id#widget_frame}.
2571 */
2572 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2573 final View view = inflater.inflate(R.layout.preference, root, false);
2574 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2575 android.R.id.widget_frame);
2576 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2577 return view;
2578 }
2579
2580 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002581 * Test if any networks are currently limited.
2582 */
2583 private boolean hasLimitedNetworks() {
2584 return !buildLimitedNetworksList().isEmpty();
2585 }
2586
2587 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002588 * Build string describing currently limited networks, which defines when
2589 * background data is restricted.
2590 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002591 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002592 private CharSequence buildLimitedNetworksString() {
2593 final List<CharSequence> limited = buildLimitedNetworksList();
2594
2595 // handle case where no networks limited
2596 if (limited.isEmpty()) {
2597 limited.add(getText(R.string.data_usage_list_none));
2598 }
2599
2600 return TextUtils.join(limited);
2601 }
2602
2603 /**
2604 * Build list of currently limited networks, which defines when background
2605 * data is restricted.
2606 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002607 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002608 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002609 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002610
2611 // build combined list of all limited networks
2612 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002613
2614 final TelephonyManager tele = TelephonyManager.from(context);
2615 if (tele.getSimState() == SIM_STATE_READY) {
2616 final String subscriberId = getActiveSubscriberId(context);
2617 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2618 limited.add(getText(R.string.data_usage_list_mobile));
2619 }
2620 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2621 limited.add(getText(R.string.data_usage_tab_3g));
2622 }
2623 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2624 limited.add(getText(R.string.data_usage_tab_4g));
2625 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002626 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002627
2628 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002629 limited.add(getText(R.string.data_usage_tab_wifi));
2630 }
2631 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2632 limited.add(getText(R.string.data_usage_tab_ethernet));
2633 }
2634
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002635 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002636 }
2637
2638 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002639 * Inset both selector and divider {@link Drawable} on the given
2640 * {@link ListView} by the requested dimensions.
2641 */
2642 private static void insetListViewDrawables(ListView view, int insetSide) {
2643 final Drawable selector = view.getSelector();
2644 final Drawable divider = view.getDivider();
2645
2646 // fully unregister these drawables so callbacks can be maintained after
2647 // wrapping below.
2648 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2649 view.setSelector(stub);
2650 view.setDivider(stub);
2651
2652 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2653 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2654 }
2655
2656 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002657 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002658 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002659 */
2660 private static void setPreferenceTitle(View parent, int resId) {
2661 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2662 title.setText(resId);
2663 }
2664
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002665 /**
2666 * Set {@link android.R.id#summary} for a preference view inflated with
2667 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2668 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002669 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002670 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2671 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002672 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002673 }
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002674
2675 /**
2676 * For search
2677 */
2678 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -07002679 new BaseSearchIndexProvider() {
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002680 @Override
2681 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
2682 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
2683
2684 final Resources res = context.getResources();
2685
2686 // Add fragment title
2687 SearchIndexableRaw data = new SearchIndexableRaw(context);
2688 data.title = res.getString(R.string.data_usage_summary_title);
2689 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2690 result.add(data);
2691
2692 // Mobile data
2693 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002694 data.key = DATA_USAGE_ENABLE_MOBILE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002695 data.title = res.getString(R.string.data_usage_enable_mobile);
2696 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2697 result.add(data);
2698
2699 // Set mobile data limit
2700 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002701 data.key = DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002702 data.title = res.getString(R.string.data_usage_disable_mobile_limit);
2703 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2704 result.add(data);
2705
Fabrice Di Megliof2a52262014-04-17 17:20:27 -07002706 // Data usage cycle
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002707 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002708 data.key = DATA_USAGE_CYCLE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002709 data.title = res.getString(R.string.data_usage_cycle);
2710 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2711 result.add(data);
2712
2713 return result;
2714 }
2715 };
2716
Sanket Padawe7e21fa52015-02-12 12:39:34 -08002717 private void addMobileTab(Context context, SubscriptionInfo subInfo, boolean isMultiSim) {
PauloftheWestc80b7612014-11-25 04:40:45 -08002718 if (subInfo != null && mMobileTagMap != null) {
2719 if (hasReadyMobileRadio(context, subInfo.getSubscriptionId())) {
Sanket Padawe7e21fa52015-02-12 12:39:34 -08002720 if (isMultiSim) {
2721 mTabHost.addTab(buildTabSpec(mMobileTagMap.get(subInfo.getSubscriptionId()),
2722 subInfo.getDisplayName()));
2723 } else {
2724 mTabHost.addTab(buildTabSpec(mMobileTagMap.get(subInfo.getSubscriptionId()),
2725 R.string.data_usage_tab_mobile));
2726 }
PauloftheWest50e6eca2014-10-03 11:07:14 -07002727 }
Wink Savillefcec91f2014-12-02 17:12:08 -08002728 } else {
2729 if (LOGD) Log.d(TAG, "addMobileTab: subInfoList is null");
PauloftheWest50e6eca2014-10-03 11:07:14 -07002730 }
2731 }
2732
Wink Savilleca756612014-11-08 10:47:12 -08002733 private SubscriptionInfo getCurrentTabSubInfo(Context context) {
PauloftheWestde338442014-10-29 14:46:44 -07002734 if (mSubInfoList != null && mTabHost != null) {
2735 final int currentTagIndex = mTabHost.getCurrentTab();
2736 int i = 0;
Wink Savilleca756612014-11-08 10:47:12 -08002737 for (SubscriptionInfo subInfo : mSubInfoList) {
Stuart Scott3ada2ec2014-10-31 14:11:24 -07002738 if (hasReadyMobileRadio(context, subInfo.getSubscriptionId())) {
PauloftheWestde338442014-10-29 14:46:44 -07002739 if (i++ == currentTagIndex) {
2740 return subInfo;
2741 }
2742 }
2743 }
2744 }
2745 return null;
2746 }
2747
PauloftheWest50e6eca2014-10-03 11:07:14 -07002748 /**
2749 * Init a map with subId key and mobile tag name
2750 * @param subInfoList The subscription Info List
2751 * @return The map or null if no activated subscription
2752 */
Wink Savilleca756612014-11-08 10:47:12 -08002753 private Map<Integer, String> initMobileTabTag(List<SubscriptionInfo> subInfoList) {
Wink Savilleb003a852014-10-23 10:16:26 -07002754 Map<Integer, String> map = null;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002755 if (subInfoList != null) {
2756 String mobileTag;
Wink Savilleb003a852014-10-23 10:16:26 -07002757 map = new HashMap<Integer, String>();
Wink Savillefcec91f2014-12-02 17:12:08 -08002758 for (SubscriptionInfo subInfo : subInfoList) {
2759 mobileTag = TAB_MOBILE + String.valueOf(subInfo.getSubscriptionId());
2760 map.put(subInfo.getSubscriptionId(), mobileTag);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002761 }
2762 }
2763 return map;
2764 }
2765
2766 private static boolean isMobileTab(String currentTab) {
2767 return currentTab != null ? currentTab.contains(TAB_MOBILE) : false;
2768 }
2769
Wink Savilleb003a852014-10-23 10:16:26 -07002770 private int getSubId(String currentTab) {
Wink Saville0183fb52014-11-22 10:11:39 -08002771 if (mMobileTagMap != null) {
2772 Set<Integer> set = mMobileTagMap.keySet();
2773 for (Integer subId : set) {
2774 if (mMobileTagMap.get(subId).equals(currentTab)) {
2775 return subId;
2776 }
PauloftheWest50e6eca2014-10-03 11:07:14 -07002777 }
2778 }
2779 Log.e(TAG, "currentTab = " + currentTab + " non mobile tab called this function");
2780 return -1;
2781 }
2782
2783 //SUB SELECT
PauloftheWest98523c82014-10-28 08:30:05 -07002784 private boolean isMobileDataAvailable(long subId) {
Wink Savilleb003a852014-10-23 10:16:26 -07002785 int[] subIds = SubscriptionManager.getSubId(PhoneConstants.SUB1);
PauloftheWest98523c82014-10-28 08:30:05 -07002786 if (subIds != null && subIds[0] == subId) {
2787 return true;
2788 }
2789
2790 subIds = SubscriptionManager.getSubId(PhoneConstants.SUB2);
2791 if (subIds != null && subIds[0] == subId) {
2792 return true;
2793 }
2794
2795 subIds = SubscriptionManager.getSubId(PhoneConstants.SUB3);
2796 if (subIds != null && subIds[0] == subId) {
2797 return true;
2798 }
2799 return false;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002800 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002801}