blob: 7eec3dfff583fd164403ff68a5a439c799fb1c88 [file] [log] [blame]
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070019import static android.net.ConnectivityManager.TYPE_ETHERNET;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070020import static android.net.ConnectivityManager.TYPE_MOBILE;
Jeff Sharkeybdf98e82011-11-10 17:17:24 -080021import static android.net.ConnectivityManager.TYPE_WIFI;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070022import static android.net.ConnectivityManager.TYPE_WIMAX;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070023import static android.net.NetworkPolicy.LIMIT_DISABLED;
Jeff Sharkeya53188f2011-09-13 19:56:45 -070024import static android.net.NetworkPolicy.WARNING_DISABLED;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070025import static android.net.NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070026import static android.net.NetworkPolicyManager.POLICY_NONE;
27import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND;
Jeff Sharkey8a503642011-06-10 13:31:21 -070028import static android.net.NetworkPolicyManager.computeLastCycleBoundary;
29import static android.net.NetworkPolicyManager.computeNextCycleBoundary;
Jeff Sharkeya662e492011-06-18 21:57:06 -070030import static android.net.NetworkTemplate.MATCH_MOBILE_3G_LOWER;
31import static android.net.NetworkTemplate.MATCH_MOBILE_4G;
32import static android.net.NetworkTemplate.MATCH_MOBILE_ALL;
33import static android.net.NetworkTemplate.MATCH_WIFI;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070034import static android.net.NetworkTemplate.buildTemplateEthernet;
35import static android.net.NetworkTemplate.buildTemplateMobile3gLower;
36import static android.net.NetworkTemplate.buildTemplateMobile4g;
37import static android.net.NetworkTemplate.buildTemplateMobileAll;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070038import static android.net.NetworkTemplate.buildTemplateWifiWildcard;
Jeff Sharkey77dae912012-02-03 14:50:33 -080039import static android.net.TrafficStats.GB_IN_BYTES;
40import static android.net.TrafficStats.MB_IN_BYTES;
Jeff Sharkeya83a24f2011-09-16 01:52:39 -070041import static android.net.TrafficStats.UID_REMOVED;
42import static android.net.TrafficStats.UID_TETHERING;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070043import static android.telephony.TelephonyManager.SIM_STATE_READY;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070044import static android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
45import static android.text.format.DateUtils.FORMAT_SHOW_DATE;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070046import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -070047import static com.android.internal.util.Preconditions.checkNotNull;
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -070048import static com.android.settings.Utils.prepareCustomPreferencesList;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070049
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070050import android.animation.LayoutTransition;
Jeff Sharkey38305fb2012-09-14 16:26:51 -070051import android.app.ActivityManager;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070052import android.app.AlertDialog;
53import android.app.Dialog;
54import android.app.DialogFragment;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070055import android.app.Fragment;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070056import android.app.FragmentTransaction;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070057import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkeyb6548462014-07-21 15:38:06 -070058import android.content.ComponentName;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070059import android.content.Context;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070060import android.content.DialogInterface;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -070061import android.content.Intent;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070062import android.content.Loader;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070063import android.content.SharedPreferences;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070064import android.content.pm.PackageManager;
Jason Monk4bb075b2015-03-17 10:06:58 -040065import android.content.pm.PackageManager.NameNotFoundException;
Zoltan Szatmary-Ban77c1d362014-12-12 18:48:03 +000066import android.content.pm.UserInfo;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070067import android.content.res.Resources;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070068import android.graphics.Color;
Jeff Sharkey5d706792011-09-08 18:57:17 -070069import android.graphics.drawable.ColorDrawable;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070070import android.graphics.drawable.Drawable;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070071import android.net.ConnectivityManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070072import android.net.INetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070073import android.net.INetworkStatsService;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070074import android.net.INetworkStatsSession;
Jeff Sharkey8a503642011-06-10 13:31:21 -070075import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070076import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070077import android.net.NetworkStats;
78import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070079import android.net.NetworkTemplate;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070080import android.net.TrafficStats;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070081import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070082import android.os.Bundle;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070083import android.os.INetworkManagementService;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070084import android.os.Parcel;
85import android.os.Parcelable;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070086import android.os.RemoteException;
87import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070088import android.os.SystemProperties;
Dianne Hackbornbb06a422012-08-16 11:01:57 -070089import android.os.UserHandle;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +010090import android.os.UserManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070091import android.preference.Preference;
Wink Savilleca756612014-11-08 10:47:12 -080092import android.telephony.SubscriptionInfo;
PauloftheWest50e6eca2014-10-03 11:07:14 -070093import android.telephony.SubscriptionManager;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070094import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070095import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070096import android.text.format.DateUtils;
97import android.text.format.Formatter;
Jeff Sharkeye5223a02012-03-09 17:11:14 -080098import android.text.format.Time;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070099import android.util.Log;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700100import android.util.SparseArray;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700101import android.util.SparseBooleanArray;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700102import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700103import android.view.Menu;
104import android.view.MenuInflater;
105import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700106import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700107import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700108import android.view.ViewGroup;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700109import android.widget.AdapterView;
110import android.widget.AdapterView.OnItemClickListener;
111import android.widget.AdapterView.OnItemSelectedListener;
112import android.widget.ArrayAdapter;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700113import android.widget.BaseAdapter;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700114import android.widget.Button;
Jason Monk20f464e2015-05-06 16:00:25 -0400115import android.widget.FrameLayout;
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;
124import android.widget.TabHost.OnTabChangeListener;
125import android.widget.TabHost.TabContentFactory;
126import android.widget.TabHost.TabSpec;
127import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700128import android.widget.TextView;
Jason Monk20f464e2015-05-06 16:00:25 -0400129import android.widget.Toast;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700130
Chris Wren8a963ba2015-03-20 10:29:14 -0400131import com.android.internal.logging.MetricsLogger;
Wink Saville55434042012-06-14 12:33:43 -0700132import com.android.internal.telephony.PhoneConstants;
Jeff Sharkey5d706792011-09-08 18:57:17 -0700133import com.android.settings.drawable.InsetBoundsDrawable;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700134import com.android.settings.net.ChartData;
135import com.android.settings.net.ChartDataLoader;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700136import com.android.settings.net.DataUsageMeteredSettings;
Jeff Sharkeya662e492011-06-18 21:57:06 -0700137import com.android.settings.net.NetworkPolicyEditor;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700138import com.android.settings.net.SummaryForAllUidLoader;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700139import com.android.settings.net.UidDetail;
140import com.android.settings.net.UidDetailProvider;
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -0700141import com.android.settings.search.BaseSearchIndexProvider;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -0700142import com.android.settings.search.Indexable;
143import com.android.settings.search.SearchIndexableRaw;
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
Chris Wren8a963ba2015-03-20 10:29:14 -0400296 protected int getMetricsCategory() {
297 return MetricsLogger.DATA_USAGE_SUMMARY;
298 }
299
300 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700301 public void onCreate(Bundle savedInstanceState) {
302 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700303 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700304
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700305 mNetworkService = INetworkManagementService.Stub.asInterface(
306 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700307 mStatsService = INetworkStatsService.Stub.asInterface(
308 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700309 mPolicyManager = NetworkPolicyManager.from(context);
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700310 mTelephonyManager = TelephonyManager.from(context);
Wink Saville0183fb52014-11-22 10:11:39 -0800311 mSubscriptionManager = SubscriptionManager.from(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700312
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700313 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700314
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700315 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700316 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700317
Wink Saville0183fb52014-11-22 10:11:39 -0800318 mSubInfoList = mSubscriptionManager.getActiveSubscriptionInfoList();
PauloftheWest50e6eca2014-10-03 11:07:14 -0700319 mMobileTagMap = initMobileTabTag(mSubInfoList);
320
Jaewan Kimffce9c12013-03-19 16:53:45 +0900321 try {
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700322 if (!mNetworkService.isBandwidthControlEnabled()) {
323 Log.w(TAG, "No bandwidth control; leaving");
324 getActivity().finish();
325 }
326 } catch (RemoteException e) {
327 Log.w(TAG, "No bandwidth control; leaving");
328 getActivity().finish();
329 }
330
331 try {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900332 mStatsSession = mStatsService.openSession();
333 } catch (RemoteException e) {
334 throw new RuntimeException(e);
335 }
336
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700337 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700338 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700339
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700340 // override preferences when no mobile radio
341 if (!hasReadyMobileRadio(context)) {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900342 mShowWifi = true;
343 mShowEthernet = true;
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700344 }
345
Jason Monk4bb075b2015-03-17 10:06:58 -0400346 mUidDetailProvider = new UidDetailProvider(context);
347
348 Bundle arguments = getArguments();
349 if (arguments != null) {
350 mShowAppImmediatePkg = arguments.getString(EXTRA_SHOW_APP_IMMEDIATE_PKG);
351 }
352
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700353 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700354 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700355
Jeff Sharkey8a503642011-06-10 13:31:21 -0700356 @Override
357 public View onCreateView(LayoutInflater inflater, ViewGroup container,
358 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700359
Jeff Sharkey8a503642011-06-10 13:31:21 -0700360 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700361 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
362
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700363
Jeff Sharkey8a503642011-06-10 13:31:21 -0700364 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700365 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700366 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
367 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700368
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700369 // decide if we need to manually inset our content, or if we should rely
370 // on parent container for inset.
371 final boolean shouldInset = mListView.getScrollBarStyle()
372 == View.SCROLLBARS_OUTSIDE_OVERLAY;
Amith Yamasani56f51a82013-08-05 10:07:23 -0700373 mInsetSide = 0;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700374
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700375 // adjust padding around tabwidget as needed
Amith Yamasani56f51a82013-08-05 10:07:23 -0700376 prepareCustomPreferencesList(container, view, mListView, false);
Jeff Sharkey5d706792011-09-08 18:57:17 -0700377
Jeff Sharkey8a503642011-06-10 13:31:21 -0700378 mTabHost.setup();
379 mTabHost.setOnTabChangedListener(mTabListener);
380
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700381 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700382 mHeader.setClickable(true);
383
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700384 mListView.addHeaderView(new View(context), null, true);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700385 mListView.addHeaderView(mHeader, null, true);
386 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700387
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700388 if (mInsetSide > 0) {
389 // inset selector and divider drawables
390 insetListViewDrawables(mListView, mInsetSide);
Fabrice Di Megliob27223f2013-01-15 18:54:11 -0800391 mHeader.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700392 }
393
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700394 {
395 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700396 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
397 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700398 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700399
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700400 mDataEnabled = new Switch(inflater.getContext());
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700401 mDataEnabled.setClickable(false);
402 mDataEnabled.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700403 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700404 mDataEnabledView.setTag(R.id.preference_highlight_key,
405 DATA_USAGE_ENABLE_MOBILE_KEY);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700406 mDataEnabledView.setClickable(true);
407 mDataEnabledView.setFocusable(true);
408 mDataEnabledView.setOnClickListener(mDataEnabledListener);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700409 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700410
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700411 mDisableAtLimit = new Switch(inflater.getContext());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700412 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700413 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700414 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700415 mDisableAtLimitView.setTag(R.id.preference_highlight_key,
416 DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700417 mDisableAtLimitView.setClickable(true);
418 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700419 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
420 mNetworkSwitches.addView(mDisableAtLimitView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700421
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700422 mCycleView = inflater.inflate(R.layout.data_usage_cycles, mNetworkSwitches, false);
423 mCycleView.setTag(R.id.preference_highlight_key, DATA_USAGE_CYCLE_KEY);
424 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
425 mCycleAdapter = new CycleAdapter(context);
426 mCycleSpinner.setAdapter(mCycleAdapter);
427 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
428 mCycleSummary = (TextView) mCycleView.findViewById(R.id.cycle_summary);
429 mNetworkSwitches.addView(mCycleView);
PauloftheWestc80b7612014-11-25 04:40:45 -0800430 mSeries = (ChartNetworkSeriesView)view.findViewById(R.id.series);
431 mDetailedSeries = (ChartNetworkSeriesView)view.findViewById(R.id.detail_series);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700432 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700433
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700434 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700435 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800436 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700437
438 {
439 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700440 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700441 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
442 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700443 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
444 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700445 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700446
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700447 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700448
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700449 mAppRestrict = new Switch(inflater.getContext());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700450 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700451 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700452 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700453 mAppRestrictView.setClickable(true);
454 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700455 mAppRestrictView.setOnClickListener(mAppRestrictListener);
456 mAppSwitches.addView(mAppRestrictView);
457 }
458
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700459 mDisclaimer = mHeader.findViewById(R.id.disclaimer);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700460 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700461 mStupidPadding = mHeader.findViewById(R.id.stupid_padding);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700462
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +0100463 final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
464 mAdapter = new DataUsageAdapter(um, mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700465 mListView.setOnItemClickListener(mListListener);
466 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700467
Jason Monk20f464e2015-05-06 16:00:25 -0400468 showRequestedAppIfNeeded(view);
Jason Monk4bb075b2015-03-17 10:06:58 -0400469
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700470 return view;
471 }
472
Jason Monk20f464e2015-05-06 16:00:25 -0400473 private void showRequestedAppIfNeeded(View rootView) {
Jason Monk4bb075b2015-03-17 10:06:58 -0400474 if (mShowAppImmediatePkg == null) {
475 return;
476 }
477 try {
478 int uid = getActivity().getPackageManager().getPackageUid(mShowAppImmediatePkg,
479 UserHandle.myUserId());
480 AppItem app = new AppItem(uid);
481 app.addUid(uid);
482
483 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
484 // When we are going straight to an app then we are coming from App Info and want
485 // a header at the top.
Jason Monk20f464e2015-05-06 16:00:25 -0400486 FrameLayout pinnedHeader = (FrameLayout) rootView.findViewById(R.id.pinned_header);
487 AppHeader.createAppHeader(getActivity(), detail.icon, detail.label, null, pinnedHeader);
Jason Monk4bb075b2015-03-17 10:06:58 -0400488 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label, false);
489 } catch (NameNotFoundException e) {
490 Log.w(TAG, "Could not find " + mShowAppImmediatePkg, e);
491 Toast.makeText(getActivity(), getString(R.string.unknown_app), Toast.LENGTH_LONG)
492 .show();
493 getActivity().finish();
494 }
495 }
496
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700497 @Override
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700498 public void onViewStateRestored(Bundle savedInstanceState) {
499 super.onViewStateRestored(savedInstanceState);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700500
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700501 // pick default tab based on incoming intent
502 final Intent intent = getActivity().getIntent();
503 mIntentTab = computeTabFromIntent(intent);
504
Jeff Sharkey8a503642011-06-10 13:31:21 -0700505 // this kicks off chain reaction which creates tabs, binds the body to
506 // selected network, and binds chart, cycles and detail list.
507 updateTabs();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700508 }
509
510 @Override
511 public void onResume() {
512 super.onResume();
513
514 getView().post(new Runnable() {
515 @Override
516 public void run() {
517 highlightViewIfNeeded();
518 }
519 });
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700520
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700521 // kick off background task to update stats
522 new AsyncTask<Void, Void, Void>() {
523 @Override
524 protected Void doInBackground(Void... params) {
525 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700526 // wait a few seconds before kicking off
527 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700528 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700529 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700530 } catch (RemoteException e) {
531 }
532 return null;
533 }
534
535 @Override
536 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700537 if (isAdded()) {
538 updateBody();
539 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700540 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700541 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700542 }
543
Jeff Sharkey8a503642011-06-10 13:31:21 -0700544 @Override
545 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
546 inflater.inflate(R.menu.data_usage, menu);
547 }
548
549 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700550 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700551 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700552 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700553 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700554
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700555 mMenuShowWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700556 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
557 mMenuShowWifi.setVisible(!appDetailMode);
558 } else {
559 mMenuShowWifi.setVisible(false);
560 }
561
562 mMenuShowEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
563 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
564 mMenuShowEthernet.setVisible(!appDetailMode);
565 } else {
566 mMenuShowEthernet.setVisible(false);
567 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700568
569 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700570 mMenuRestrictBackground.setVisible(
571 hasReadyMobileRadio(context) && isOwner && !appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700572
573 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
574 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700575 metered.setVisible(!appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700576 } else {
577 metered.setVisible(false);
578 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700579
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700580 // TODO: show when multiple sims available
581 mMenuSimCards = menu.findItem(R.id.data_usage_menu_sim_cards);
582 mMenuSimCards.setVisible(false);
583
584 mMenuCellularNetworks = menu.findItem(R.id.data_usage_menu_cellular_networks);
PauloftheWesta4b8fb32014-09-18 10:12:25 -0700585 mMenuCellularNetworks.setVisible(hasReadyMobileRadio(context)
586 && !appDetailMode && isOwner);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700587
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700588 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
589 String helpUrl;
590 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800591 HelpUtils.prepareHelpMenuItem(context, help, helpUrl);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700592 } else {
593 help.setVisible(false);
594 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700595
596 updateMenuTitles();
597 }
598
599 private void updateMenuTitles() {
600 if (mPolicyManager.getRestrictBackground()) {
601 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_allow_background);
602 } else {
603 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_restrict_background);
604 }
605
606 if (mShowWifi) {
607 mMenuShowWifi.setTitle(R.string.data_usage_menu_hide_wifi);
608 } else {
609 mMenuShowWifi.setTitle(R.string.data_usage_menu_show_wifi);
610 }
611
612 if (mShowEthernet) {
613 mMenuShowEthernet.setTitle(R.string.data_usage_menu_hide_ethernet);
614 } else {
615 mMenuShowEthernet.setTitle(R.string.data_usage_menu_show_ethernet);
616 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700617 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700618
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700619 @Override
620 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700621 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700622 case R.id.data_usage_menu_restrict_background: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700623 final boolean restrictBackground = !mPolicyManager.getRestrictBackground();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700624 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800625 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700626 } else {
627 // no confirmation to drop restriction
628 setRestrictBackground(false);
629 }
630 return true;
631 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700632 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700633 mShowWifi = !mShowWifi;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700634 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700635 updateMenuTitles();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700636 updateTabs();
637 return true;
638 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700639 case R.id.data_usage_menu_show_ethernet: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700640 mShowEthernet = !mShowEthernet;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700641 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700642 updateMenuTitles();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700643 updateTabs();
644 return true;
645 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700646 case R.id.data_usage_menu_sim_cards: {
647 // TODO: hook up to sim cards
648 return true;
649 }
650 case R.id.data_usage_menu_cellular_networks: {
651 final Intent intent = new Intent(Intent.ACTION_MAIN);
652 intent.setComponent(new ComponentName("com.android.phone",
653 "com.android.phone.MobileNetworkSettings"));
654 startActivity(intent);
655 return true;
656 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700657 case R.id.data_usage_menu_metered: {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800658 final SettingsActivity sa = (SettingsActivity) getActivity();
659 sa.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700660 R.string.data_usage_metered_title, null, this, 0);
661 return true;
662 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700663 }
664 return false;
665 }
666
Jeff Sharkey94a90952011-06-13 22:31:09 -0700667 @Override
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700668 public void onDestroy() {
Jeff Sharkey94a90952011-06-13 22:31:09 -0700669 mDataEnabledView = null;
670 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700671
672 mUidDetailProvider.clearCache();
673 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700674
675 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700676
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800677 super.onDestroy();
678 }
679
Jeff Sharkey8a503642011-06-10 13:31:21 -0700680 /**
Jeff Sharkey92811822012-05-04 11:47:29 -0700681 * Build and assign {@link LayoutTransition} to various containers. Should
682 * only be assigned after initial layout is complete.
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700683 */
Jeff Sharkey92811822012-05-04 11:47:29 -0700684 private void ensureLayoutTransitions() {
Jason Monk4bb075b2015-03-17 10:06:58 -0400685 if (mShowAppImmediatePkg != null) {
686 // If we are skipping right to showing an app, we don't care about transitions.
687 return;
688 }
Jeff Sharkey92811822012-05-04 11:47:29 -0700689 // skip when already setup
690 if (mChart.getLayoutTransition() != null) return;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700691
Jeff Sharkey92811822012-05-04 11:47:29 -0700692 mTabsContainer.setLayoutTransition(buildLayoutTransition());
693 mHeader.setLayoutTransition(buildLayoutTransition());
694 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700695
Jeff Sharkey92811822012-05-04 11:47:29 -0700696 final LayoutTransition chartTransition = buildLayoutTransition();
697 chartTransition.disableTransitionType(LayoutTransition.APPEARING);
698 chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
699 mChart.setLayoutTransition(chartTransition);
700 }
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700701
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700702 private static LayoutTransition buildLayoutTransition() {
703 final LayoutTransition transition = new LayoutTransition();
704 if (TEST_ANIM) {
705 transition.setDuration(1500);
706 }
707 transition.setAnimateParentHierarchy(false);
708 return transition;
709 }
710
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700711 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700712 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700713 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
714 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700715 */
716 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700717 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700718 mTabHost.clearAllTabs();
719
Sanket Padawe7e21fa52015-02-12 12:39:34 -0800720 int simCount = mTelephonyManager.getSimCount();
721
Stuart Scottf51d0062015-04-08 09:40:48 -0700722 List<SubscriptionInfo> sirs = mSubscriptionManager.getActiveSubscriptionInfoList();
723 if (sirs != null) {
724 for (SubscriptionInfo sir : sirs) {
Sanket Padawe7e21fa52015-02-12 12:39:34 -0800725 addMobileTab(context, sir, (simCount > 1));
PauloftheWestc80b7612014-11-25 04:40:45 -0800726 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700727 }
PauloftheWestc80b7612014-11-25 04:40:45 -0800728
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700729 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700730 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
731 }
PauloftheWestc80b7612014-11-25 04:40:45 -0800732
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700733 if (mShowEthernet && hasEthernet(context)) {
734 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
735 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700736
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700737 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700738 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
739 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
740 if (mIntentTab != null) {
741 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700742 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700743 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700744 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700745 mTabHost.setCurrentTabByTag(mIntentTab);
746 }
747 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700748 } else if (noTabs) {
749 // no usable tabs, so hide body
750 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700751 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700752 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700753 }
754 }
755
Jeff Sharkey8a503642011-06-10 13:31:21 -0700756 /**
757 * Factory that provide empty {@link View} to make {@link TabHost} happy.
758 */
759 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700760 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700761 public View createTabContent(String tag) {
762 return new View(mTabHost.getContext());
763 }
764 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700765
Jeff Sharkey8a503642011-06-10 13:31:21 -0700766 /**
767 * Build {@link TabSpec} with thin indicator, and empty content.
768 */
769 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700770 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
771 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700772 }
773
PauloftheWest50e6eca2014-10-03 11:07:14 -0700774 /**
775 * Build {@link TabSpec} with thin indicator, and empty content.
776 */
Stuart Scottb1531812014-11-04 14:52:23 -0800777 private TabSpec buildTabSpec(String tag, CharSequence title) {
PauloftheWest50e6eca2014-10-03 11:07:14 -0700778 return mTabHost.newTabSpec(tag).setIndicator(title).setContent(
779 mEmptyTabContent);
780 }
781
782
Jeff Sharkey8a503642011-06-10 13:31:21 -0700783 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700784 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700785 public void onTabChanged(String tabId) {
786 // user changed tab; update body
787 updateBody();
788 }
789 };
790
791 /**
792 * Update body content based on current tab. Loads
793 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
794 * binds them to visible controls.
795 */
796 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700797 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700798 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700799
Jeff Sharkeya662e492011-06-18 21:57:06 -0700800 final Context context = getActivity();
PauloftheWestc80b7612014-11-25 04:40:45 -0800801 final Resources resources = context.getResources();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700802 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700803 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700804
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700805 if (currentTab == null) {
806 Log.w(TAG, "no tab selected; hiding body");
807 mListView.setVisibility(View.GONE);
808 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700809 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700810 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700811 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700812
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700813 mCurrentTab = currentTab;
814
Jeff Sharkey8a503642011-06-10 13:31:21 -0700815 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
816
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700817 mDataEnabledSupported = isOwner;
818 mDisableAtLimitSupported = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700819
Wink Savillefcec91f2014-12-02 17:12:08 -0800820 // TODO: remove mobile tabs when SIM isn't ready probably by
821 // TODO: using SubscriptionManager.getActiveSubscriptionInfoList.
822 if (LOGD) Log.d(TAG, "updateBody() isMobileTab=" + isMobileTab(currentTab));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700823
PauloftheWest50e6eca2014-10-03 11:07:14 -0700824 if (isMobileTab(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800825 if (LOGD) Log.d(TAG, "updateBody() mobile tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700826 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
827 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
PauloftheWest50e6eca2014-10-03 11:07:14 -0700828 mDataEnabledSupported = isMobileDataAvailable(getSubId(currentTab));
Jeff Sharkey4e2d16c2014-12-03 13:44:26 -0800829
830 // Match mobile traffic for this subscriber, but normalize it to
831 // catch any other merged subscribers.
832 mTemplate = buildTemplateMobileAll(
833 getActiveSubscriberId(context, getSubId(currentTab)));
834 mTemplate = NetworkTemplate.normalize(mTemplate,
835 mTelephonyManager.getMergedSubscriberIds());
836
Jeff Sharkey8a503642011-06-10 13:31:21 -0700837 } else if (TAB_3G.equals(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800838 if (LOGD) Log.d(TAG, "updateBody() 3g tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700839 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
840 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
841 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700842 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700843
844 } else if (TAB_4G.equals(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800845 if (LOGD) Log.d(TAG, "updateBody() 4g tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700846 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
847 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
848 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700849 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700850
851 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700852 // wifi doesn't have any controls
Wink Savillefcec91f2014-12-02 17:12:08 -0800853 if (LOGD) Log.d(TAG, "updateBody() wifi tab");
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700854 mDataEnabledSupported = false;
855 mDisableAtLimitSupported = false;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700856 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700857
858 } else if (TAB_ETHERNET.equals(currentTab)) {
859 // ethernet doesn't have any controls
Wink Savillefcec91f2014-12-02 17:12:08 -0800860 if (LOGD) Log.d(TAG, "updateBody() ethernet tab");
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700861 mDataEnabledSupported = false;
862 mDisableAtLimitSupported = false;
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700863 mTemplate = buildTemplateEthernet();
864
865 } else {
Wink Savillefcec91f2014-12-02 17:12:08 -0800866 if (LOGD) Log.d(TAG, "updateBody() unknown tab");
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700867 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700868 }
869
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700870 // kick off loader for network history
871 // TODO: consider chaining two loaders together instead of reloading
872 // network history when showing app detail.
873 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700874 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700875
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700876 // detail mode can change visible menus, invalidate
877 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700878
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700879 mBinding = false;
PauloftheWestc80b7612014-11-25 04:40:45 -0800880
Alan Viverette55eaa962015-03-18 18:10:52 -0700881 int seriesColor = context.getColor(R.color.sim_noitification);
PauloftheWestc80b7612014-11-25 04:40:45 -0800882 if (mCurrentTab != null && mCurrentTab.length() > TAB_MOBILE.length() ){
883 final int slotId = Integer.parseInt(mCurrentTab.substring(TAB_MOBILE.length(),
884 mCurrentTab.length()));
Stuart Scottf51d0062015-04-08 09:40:48 -0700885 final SubscriptionInfo sir = mSubscriptionManager
886 .getActiveSubscriptionInfoForSimSlotIndex(slotId);
PauloftheWestc80b7612014-11-25 04:40:45 -0800887
888 if (sir != null) {
889 seriesColor = sir.getIconTint();
890 }
891 }
892
Jeff Sharkey9c167e92015-01-26 15:15:59 -0700893 final int secondaryColor = Color.argb(127, Color.red(seriesColor), Color.green(seriesColor),
894 Color.blue(seriesColor));
895 mSeries.setChartColor(Color.BLACK, seriesColor, secondaryColor);
896 mDetailedSeries.setChartColor(Color.BLACK, seriesColor, secondaryColor);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700897 }
898
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700899 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700900 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700901 }
902
903 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700904 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700905 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700906 */
907 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700908 final Context context = getActivity();
909 final PackageManager pm = context.getPackageManager();
910 final LayoutInflater inflater = getActivity().getLayoutInflater();
911
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700912 if (isAppDetailMode()) {
913 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700914 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700915 } else {
916 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700917 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700918
919 // hide detail stats when not in detail mode
920 mChart.bindDetailNetworkStats(null);
921 return;
922 }
923
924 // remove warning/limit sweeps while in detail mode
925 mChart.bindNetworkPolicy(null);
926
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700927 // show icon and all labels appearing under this app
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700928 final int uid = mCurrentApp.key;
929 final UidDetail detail = mUidDetailProvider.getUidDetail(uid, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700930 mAppIcon.setImageDrawable(detail.icon);
931
932 mAppTitles.removeAllViews();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700933
934 View title = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700935 if (detail.detailLabels != null) {
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100936 final int n = detail.detailLabels.length;
937 for (int i = 0; i < n; ++i) {
938 CharSequence label = detail.detailLabels[i];
939 CharSequence contentDescription = detail.detailContentDescriptions[i];
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700940 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100941 TextView appTitle = (TextView) title.findViewById(R.id.app_title);
942 appTitle.setText(label);
943 appTitle.setContentDescription(contentDescription);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700944 mAppTitles.addView(title);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700945 }
946 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700947 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100948 TextView appTitle = (TextView) title.findViewById(R.id.app_title);
949 appTitle.setText(detail.label);
950 appTitle.setContentDescription(detail.contentDescription);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700951 mAppTitles.addView(title);
952 }
953
954 // Remember last slot for summary
955 if (title != null) {
956 mAppTotal = (TextView) title.findViewById(R.id.app_summary);
957 } else {
958 mAppTotal = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700959 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700960
961 // enable settings button when package provides it
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700962 final String[] packageNames = pm.getPackagesForUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700963 if (packageNames != null && packageNames.length > 0) {
964 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700965 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
966
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700967 // Search for match across all packages
968 boolean matchFound = false;
969 for (String packageName : packageNames) {
970 mAppSettingsIntent.setPackage(packageName);
971 if (pm.resolveActivity(mAppSettingsIntent, 0) != null) {
972 matchFound = true;
973 break;
974 }
975 }
976
Zoltan Szatmary-Banf20d39e2014-08-07 15:27:08 +0100977 mAppSettings.setOnClickListener(new OnClickListener() {
978 @Override
979 public void onClick(View v) {
980 if (!isAdded()) {
981 return;
982 }
983
984 // TODO: target towards entire UID instead of just first package
985 getActivity().startActivityAsUser(mAppSettingsIntent,
986 new UserHandle(UserHandle.getUserId(uid)));
987 }
988 });
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700989 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -0700990 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700991
992 } else {
993 mAppSettingsIntent = null;
Zoltan Szatmary-Banf20d39e2014-08-07 15:27:08 +0100994 mAppSettings.setOnClickListener(null);
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700995 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700996 }
997
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700998 updateDetailData();
999
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001000 if (UserHandle.isApp(uid) && !mPolicyManager.getRestrictBackground()
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001001 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001002 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -08001003 setPreferenceSummary(mAppRestrictView,
1004 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001005
1006 mAppRestrictView.setVisibility(View.VISIBLE);
1007 mAppRestrict.setChecked(getAppRestrictBackground());
1008
1009 } else {
1010 mAppRestrictView.setVisibility(View.GONE);
1011 }
1012 }
1013
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001014 private void setPolicyWarningBytes(long warningBytes) {
1015 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -07001016 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001017 updatePolicy(false);
1018 }
1019
1020 private void setPolicyLimitBytes(long limitBytes) {
1021 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -07001022 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001023 updatePolicy(false);
1024 }
1025
Wink Savilleb003a852014-10-23 10:16:26 -07001026 private boolean isMobileDataEnabled(int subId) {
Wink Savillea4f4d182014-12-05 15:34:46 -08001027 if (LOGD) Log.d(TAG, "isMobileDataEnabled:+ subId=" + subId);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001028 boolean isEnable = false;
Sanket Padawed819270f2015-01-14 11:03:33 -08001029 if (mMobileDataEnabled.get(String.valueOf(subId)) != null) {
1030 //TODO: deprecate and remove this once enabled flag is on policy
1031 //Multiple Subscriptions, the value need to be reseted
1032 isEnable = mMobileDataEnabled.get(String.valueOf(subId)).booleanValue();
Wink Savillea4f4d182014-12-05 15:34:46 -08001033 if (LOGD) {
1034 Log.d(TAG, "isMobileDataEnabled: != null, subId=" + subId
1035 + " isEnable=" + isEnable);
1036 }
Sanket Padawed819270f2015-01-14 11:03:33 -08001037 mMobileDataEnabled.put(String.valueOf(subId), null);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001038 } else {
Wink Savillea4f4d182014-12-05 15:34:46 -08001039 // SUB SELECT
1040 isEnable = mTelephonyManager.getDataEnabled(subId);
1041 if (LOGD) {
1042 Log.d(TAG, "isMobileDataEnabled: == null, subId=" + subId
1043 + " isEnable=" + isEnable);
1044 }
Jeff Sharkey28130d92011-09-02 16:10:24 -07001045 }
PauloftheWest50e6eca2014-10-03 11:07:14 -07001046 return isEnable;
Jeff Sharkey28130d92011-09-02 16:10:24 -07001047 }
1048
Sanket Padawe24f834d2015-01-08 11:23:11 -08001049 private void setMobileDataEnabled(int subId, boolean enabled) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001050 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
Sanket Padawe24f834d2015-01-08 11:23:11 -08001051 mTelephonyManager.setDataEnabled(subId, enabled);
Sanket Padawed819270f2015-01-14 11:03:33 -08001052 mMobileDataEnabled.put(String.valueOf(subId), enabled);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001053 updatePolicy(false);
1054 }
1055
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001056 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001057 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked()
1058 && ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001059 }
1060
1061 private boolean isBandwidthControlEnabled() {
1062 try {
1063 return mNetworkService.isBandwidthControlEnabled();
1064 } catch (RemoteException e) {
1065 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
1066 return false;
1067 }
1068 }
1069
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001070 public void setRestrictBackground(boolean restrictBackground) {
1071 mPolicyManager.setRestrictBackground(restrictBackground);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001072 updateMenuTitles();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001073 }
1074
1075 private boolean getAppRestrictBackground() {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001076 final int uid = mCurrentApp.key;
1077 final int uidPolicy = mPolicyManager.getUidPolicy(uid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001078 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
1079 }
1080
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001081 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001082 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001083 final int uid = mCurrentApp.key;
1084 mPolicyManager.setUidPolicy(
1085 uid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001086 mAppRestrict.setChecked(restrictBackground);
1087 }
1088
Jeff Sharkey8a503642011-06-10 13:31:21 -07001089 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001090 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
1091 * current {@link #mTemplate}.
1092 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001093 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001094 boolean dataEnabledVisible = mDataEnabledSupported;
1095 boolean disableAtLimitVisible = mDisableAtLimitSupported;
1096
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001097 if (isAppDetailMode()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001098 dataEnabledVisible = false;
1099 disableAtLimitVisible = false;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001100 }
1101
Jeff Sharkey28130d92011-09-02 16:10:24 -07001102 // TODO: move enabled state directly into policy
PauloftheWest50e6eca2014-10-03 11:07:14 -07001103 if (isMobileTab(mCurrentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001104 mBinding = true;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001105 mDataEnabled.setChecked(isMobileDataEnabled(getSubId(mCurrentTab)));
Jeff Sharkey28130d92011-09-02 16:10:24 -07001106 mBinding = false;
1107 }
1108
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001109 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001110 //SUB SELECT
1111 if (isNetworkPolicyModifiable(policy) && isMobileDataAvailable(getSubId(mCurrentTab))) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001112 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001113 if (!isAppDetailMode()) {
1114 mChart.bindNetworkPolicy(policy);
1115 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001116
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001117 } else {
1118 // controls are disabled; don't bind warning/limit sweeps
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001119 disableAtLimitVisible = false;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001120 mChart.bindNetworkPolicy(null);
1121 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001122
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001123 mDataEnabledView.setVisibility(dataEnabledVisible ? View.VISIBLE : View.GONE);
1124 mDisableAtLimitView.setVisibility(disableAtLimitVisible ? View.VISIBLE : View.GONE);
1125
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001126 if (refreshCycle) {
1127 // generate cycle list based on policy and available history
1128 updateCycleList(policy);
1129 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001130 }
1131
1132 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001133 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
1134 * and available {@link NetworkStatsHistory} data. Always selects the newest
1135 * item, updating the inspection range on {@link #mChart}.
1136 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001137 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001138 // stash away currently selected cycle to try restoring below
1139 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -07001140 mCycleAdapter.clear();
1141
1142 final Context context = mCycleSpinner.getContext();
1143
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001144 long historyStart = Long.MAX_VALUE;
1145 long historyEnd = Long.MIN_VALUE;
1146 if (mChartData != null) {
1147 historyStart = mChartData.network.getStart();
1148 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001149 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001150
Jeff Sharkey461842a2011-09-25 18:22:48 -07001151 final long now = System.currentTimeMillis();
1152 if (historyStart == Long.MAX_VALUE) historyStart = now;
1153 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001154
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001155 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001156 if (policy != null) {
1157 // find the next cycle boundary
1158 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001159
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001160 // walk backwards, generating all valid cycle ranges
1161 while (cycleEnd > historyStart) {
1162 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
1163 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
1164 + historyStart);
1165 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1166 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001167 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001168 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001169
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001170 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001171 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001172 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001173
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001174 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001175 // no policy defined cycles; show entry for each four-week period
1176 long cycleEnd = historyEnd;
1177 while (cycleEnd > historyStart) {
1178 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
1179 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1180 cycleEnd = cycleStart;
1181 }
1182
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001183 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001184 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001185
1186 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001187 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001188 final int position = mCycleAdapter.findNearestPosition(previousItem);
1189 mCycleSpinner.setSelection(position);
1190
1191 // only force-update cycle when changed; skipping preserves any
1192 // user-defined inspection region.
1193 final CycleItem selectedItem = mCycleAdapter.getItem(position);
1194 if (!Objects.equal(selectedItem, previousItem)) {
1195 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1196 } else {
1197 // but still kick off loader for detailed list
1198 updateDetailData();
1199 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001200 } else {
1201 updateDetailData();
1202 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001203 }
1204
Sanket Padawed819270f2015-01-14 11:03:33 -08001205 private void disableDataForOtherSubscriptions(SubscriptionInfo currentSir) {
1206 if (mSubInfoList != null) {
1207 for (SubscriptionInfo subInfo : mSubInfoList) {
1208 if (subInfo.getSubscriptionId() != currentSir.getSubscriptionId()) {
1209 setMobileDataEnabled(subInfo.getSubscriptionId(), false);
1210 }
1211 }
1212 }
1213 }
1214
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001215 private View.OnClickListener mDataEnabledListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001216 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001217 public void onClick(View v) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001218 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001219
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001220 final boolean dataEnabled = !mDataEnabled.isChecked();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001221 final String currentTab = mCurrentTab;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001222 if (isMobileTab(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001223 if (dataEnabled) {
PauloftheWestde338442014-10-29 14:46:44 -07001224 // If we are showing the Sim Card tile then we are a Multi-Sim device.
1225 if (Utils.showSimCardTile(getActivity())) {
1226 handleMultiSimDataDialog();
1227 } else {
Sanket Padawe24f834d2015-01-08 11:23:11 -08001228 setMobileDataEnabled(getSubId(currentTab), true);
PauloftheWestde338442014-10-29 14:46:44 -07001229 }
Jeff Sharkey28130d92011-09-02 16:10:24 -07001230 } else {
1231 // disabling data; show confirmation dialog which eventually
1232 // calls setMobileDataEnabled() once user confirms.
Sanket Padawe24f834d2015-01-08 11:23:11 -08001233 ConfirmDataDisableFragment.show(DataUsageSummary.this, getSubId(mCurrentTab));
Jeff Sharkey28130d92011-09-02 16:10:24 -07001234 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001235 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001236
Jeff Sharkey28130d92011-09-02 16:10:24 -07001237 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001238 }
1239 };
1240
PauloftheWestde338442014-10-29 14:46:44 -07001241 private void handleMultiSimDataDialog() {
PauloftheWestf0d84e22014-12-10 08:50:54 -08001242 final Context context = getActivity();
1243 final SubscriptionInfo currentSir = getCurrentTabSubInfo(context);
Sanket Padawe766ab602015-01-20 10:36:15 -08001244
1245 //If sim has not loaded after toggling data switch, return.
1246 if (currentSir == null) {
1247 return;
1248 }
1249
Stuart Scottf51d0062015-04-08 09:40:48 -07001250 final SubscriptionInfo nextSir = mSubscriptionManager.getDefaultDataSubscriptionInfo();
PauloftheWestde338442014-10-29 14:46:44 -07001251
PauloftheWestf0d84e22014-12-10 08:50:54 -08001252 // If the device is single SIM or is enabling data on the active data SIM then forgo
1253 // the pop-up.
1254 if (!Utils.showSimCardTile(context) ||
Sanket Padawed819270f2015-01-14 11:03:33 -08001255 (nextSir != null && currentSir != null &&
1256 currentSir.getSubscriptionId() == nextSir.getSubscriptionId())) {
Sanket Padawe24f834d2015-01-08 11:23:11 -08001257 setMobileDataEnabled(currentSir.getSubscriptionId(), true);
Sanket Padawed819270f2015-01-14 11:03:33 -08001258 if (nextSir != null && currentSir != null &&
1259 currentSir.getSubscriptionId() == nextSir.getSubscriptionId()) {
1260 disableDataForOtherSubscriptions(currentSir);
1261 }
PauloftheWestde338442014-10-29 14:46:44 -07001262 updateBody();
1263 return;
1264 }
1265
PauloftheWestf0d84e22014-12-10 08:50:54 -08001266 final String previousName = (nextSir == null)
1267 ? context.getResources().getString(R.string.sim_selection_required_pref)
1268 : nextSir.getDisplayName().toString();
1269
PauloftheWestde338442014-10-29 14:46:44 -07001270 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
1271
1272 builder.setTitle(R.string.sim_change_data_title);
1273 builder.setMessage(getActivity().getResources().getString(R.string.sim_change_data_message,
PauloftheWestf0d84e22014-12-10 08:50:54 -08001274 currentSir.getDisplayName(), previousName));
PauloftheWestde338442014-10-29 14:46:44 -07001275
1276 builder.setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
1277 @Override
1278 public void onClick(DialogInterface dialog, int id) {
Wink Saville0183fb52014-11-22 10:11:39 -08001279 mSubscriptionManager.setDefaultDataSubId(currentSir.getSubscriptionId());
Sanket Padawe24f834d2015-01-08 11:23:11 -08001280 setMobileDataEnabled(currentSir.getSubscriptionId(), true);
Sanket Padawed819270f2015-01-14 11:03:33 -08001281 disableDataForOtherSubscriptions(currentSir);
PauloftheWestde338442014-10-29 14:46:44 -07001282 updateBody();
1283 }
1284 });
1285 builder.setNegativeButton(R.string.cancel, null);
1286
1287 builder.create().show();
1288 }
1289
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001290 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001291 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001292 public void onClick(View v) {
1293 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001294 if (disableAtLimit) {
1295 // enabling limit; show confirmation dialog which eventually
1296 // calls setPolicyLimitBytes() once user confirms.
1297 ConfirmLimitFragment.show(DataUsageSummary.this);
1298 } else {
1299 setPolicyLimitBytes(LIMIT_DISABLED);
1300 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001301 }
1302 };
1303
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001304 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001305 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001306 public void onClick(View v) {
1307 final boolean restrictBackground = !mAppRestrict.isChecked();
1308
1309 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001310 // enabling restriction; show confirmation dialog which
1311 // eventually calls setRestrictBackground() once user
1312 // confirms.
1313 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001314 } else {
1315 setAppRestrictBackground(false);
1316 }
1317 }
1318 };
1319
Jeff Sharkey8a503642011-06-10 13:31:21 -07001320 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001321 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001322 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001323 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001324 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001325
1326 // TODO: sigh, remove this hack once we understand 6450986
1327 if (mUidDetailProvider == null || app == null) return;
1328
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001329 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001330 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001331 }
1332 };
1333
1334 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001335 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001336 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1337 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1338 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001339 // show cycle editor; will eventually call setPolicyCycleDay()
1340 // when user finishes editing.
1341 CycleEditorFragment.show(DataUsageSummary.this);
1342
1343 // reset spinner to something other than "change cycle..."
1344 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001345
1346 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001347 if (LOGD) {
1348 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1349 + cycle.end + "]");
1350 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001351
1352 // update chart to show selected cycle, and update detail data
1353 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001354 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001355
1356 updateDetailData();
1357 }
1358 }
1359
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001360 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001361 public void onNothingSelected(AdapterView<?> parent) {
1362 // ignored
1363 }
1364 };
1365
1366 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001367 * Update details based on {@link #mChart} inspection range depending on
1368 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1369 * of applications data usage, and when {@link #isAppDetailMode()} update
1370 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001371 */
1372 private void updateDetailData() {
1373 if (LOGD) Log.d(TAG, "updateDetailData()");
1374
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001375 final long start = mChart.getInspectStart();
1376 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001377 final long now = System.currentTimeMillis();
1378
1379 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001380
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001381 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001382 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001383 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001384 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001385 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001386 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001387 final long foregroundBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001388 final long totalBytes = defaultBytes + foregroundBytes;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001389
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001390 if (mAppTotal != null) {
1391 mAppTotal.setText(Formatter.formatFileSize(context, totalBytes));
1392 }
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001393 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1394 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1395
1396 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001397 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001398
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001399 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001400
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001401 mCycleSummary.setVisibility(View.GONE);
1402
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001403 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001404 if (mChartData != null) {
1405 entry = mChartData.network.getValues(start, end, now, null);
1406 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001407
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001408 mCycleSummary.setVisibility(View.VISIBLE);
1409
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001410 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001411 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001412 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001413 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001414
1415 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1416 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001417 mCycleSummary.setText(totalPhrase);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001418
PauloftheWest50e6eca2014-10-03 11:07:14 -07001419 if (isMobileTab(mCurrentTab) || TAB_3G.equals(mCurrentTab)
László Dávid0b8bf4e2012-10-24 23:31:47 +02001420 || TAB_4G.equals(mCurrentTab)) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001421 if (isAppDetailMode()) {
1422 mDisclaimer.setVisibility(View.GONE);
1423 } else {
1424 mDisclaimer.setVisibility(View.VISIBLE);
1425 }
Jeff Sharkeye557c332012-04-13 16:04:07 -07001426 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001427 mDisclaimer.setVisibility(View.GONE);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001428 }
1429
Jeff Sharkey92811822012-05-04 11:47:29 -07001430 // initial layout is finished above, ensure we have transitions
1431 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001432 }
1433
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001434 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1435 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001436 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001437 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001438 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001439 }
1440
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001441 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001442 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1443 mChartData = data;
1444 mChart.bindNetworkStats(mChartData.network);
1445 mChart.bindDetailNetworkStats(mChartData.detail);
1446
1447 // calcuate policy cycles based on available data
1448 updatePolicy(true);
1449 updateAppDetail();
1450
1451 // force scroll to top of body when showing detail
1452 if (mChartData.detail != null) {
1453 mListView.smoothScrollToPosition(0);
1454 }
1455 }
1456
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001457 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001458 public void onLoaderReset(Loader<ChartData> loader) {
1459 mChartData = null;
1460 mChart.bindNetworkStats(null);
1461 mChart.bindDetailNetworkStats(null);
1462 }
1463 };
1464
1465 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001466 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001467 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001468 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001469 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001470 }
1471
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001472 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001473 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001474 final int[] restrictedUids = mPolicyManager.getUidsWithPolicy(
Jeff Sharkeye557c332012-04-13 16:04:07 -07001475 POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001476 mAdapter.bindStats(data, restrictedUids);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001477 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001478 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001479
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001480 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001481 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001482 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001483 updateEmptyVisible();
1484 }
1485
1486 private void updateEmptyVisible() {
1487 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1488 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001489 mStupidPadding.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001490 }
1491 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001492
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001493 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001494 final TelephonyManager tele = TelephonyManager.from(context);
1495 final String actualSubscriberId = tele.getSubscriberId();
Wink Savillefcec91f2014-12-02 17:12:08 -08001496 String retVal = SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
1497 if (LOGD) Log.d(TAG, "getActiveSubscriberId=" + retVal + " actualSubscriberId=" + actualSubscriberId);
1498 return retVal;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001499 }
1500
Wink Savilleb003a852014-10-23 10:16:26 -07001501 private static String getActiveSubscriberId(Context context, int subId) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07001502 final TelephonyManager tele = TelephonyManager.from(context);
Wink Savillefcec91f2014-12-02 17:12:08 -08001503 String retVal = tele.getSubscriberId(subId);
1504 if (LOGD) Log.d(TAG, "getActiveSubscriberId=" + retVal + " subId=" + subId);
1505 return retVal;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001506 }
1507
Jeff Sharkey8a503642011-06-10 13:31:21 -07001508 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001509 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001510 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001511 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001512 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001513
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001514 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001515 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001516 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001517 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001518
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001519 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001520 public void requestWarningEdit() {
1521 WarningEditorFragment.show(DataUsageSummary.this);
1522 }
1523
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001524 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001525 public void requestLimitEdit() {
1526 LimitEditorFragment.show(DataUsageSummary.this);
1527 }
1528 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001529
1530 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001531 * List item that reflects a specific data usage cycle.
1532 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001533 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001534 public CharSequence label;
1535 public long start;
1536 public long end;
1537
Jeff Sharkey8a503642011-06-10 13:31:21 -07001538 CycleItem(CharSequence label) {
1539 this.label = label;
1540 }
1541
1542 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001543 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001544 this.start = start;
1545 this.end = end;
1546 }
1547
Jeff Sharkey8a503642011-06-10 13:31:21 -07001548 @Override
1549 public String toString() {
1550 return label.toString();
1551 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001552
1553 @Override
1554 public boolean equals(Object o) {
1555 if (o instanceof CycleItem) {
1556 final CycleItem another = (CycleItem) o;
1557 return start == another.start && end == another.end;
1558 }
1559 return false;
1560 }
1561
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001562 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001563 public int compareTo(CycleItem another) {
1564 return Long.compare(start, another.start);
1565 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001566 }
1567
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001568 private static final StringBuilder sBuilder = new StringBuilder(50);
1569 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1570 sBuilder, Locale.getDefault());
1571
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001572 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001573 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001574
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001575 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001576 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001577 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1578 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001579 }
1580 }
1581
Jeff Sharkey8a503642011-06-10 13:31:21 -07001582 /**
1583 * Special-case data usage cycle that triggers dialog to change
1584 * {@link NetworkPolicy#cycleDay}.
1585 */
1586 public static class CycleChangeItem extends CycleItem {
1587 public CycleChangeItem(Context context) {
1588 super(context.getString(R.string.data_usage_change_cycle));
1589 }
1590 }
1591
1592 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001593 private boolean mChangePossible = false;
1594 private boolean mChangeVisible = false;
1595
1596 private final CycleChangeItem mChangeItem;
1597
Jeff Sharkey8a503642011-06-10 13:31:21 -07001598 public CycleAdapter(Context context) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001599 super(context, R.layout.data_usage_cycle_item);
1600 setDropDownViewResource(R.layout.data_usage_cycle_item_dropdown);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001601 mChangeItem = new CycleChangeItem(context);
1602 }
1603
1604 public void setChangePossible(boolean possible) {
1605 mChangePossible = possible;
1606 updateChange();
1607 }
1608
1609 public void setChangeVisible(boolean visible) {
1610 mChangeVisible = visible;
1611 updateChange();
1612 }
1613
1614 private void updateChange() {
1615 remove(mChangeItem);
1616 if (mChangePossible && mChangeVisible) {
1617 add(mChangeItem);
1618 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001619 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001620
1621 /**
1622 * Find position of {@link CycleItem} in this adapter which is nearest
1623 * the given {@link CycleItem}.
1624 */
1625 public int findNearestPosition(CycleItem target) {
1626 if (target != null) {
1627 final int count = getCount();
1628 for (int i = count - 1; i >= 0; i--) {
1629 final CycleItem item = getItem(i);
1630 if (item instanceof CycleChangeItem) {
1631 continue;
1632 } else if (item.compareTo(target) >= 0) {
1633 return i;
1634 }
1635 }
1636 }
1637 return 0;
1638 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001639 }
1640
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001641 public static class AppItem implements Comparable<AppItem>, Parcelable {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001642 public static final int CATEGORY_USER = 0;
1643 public static final int CATEGORY_APP_TITLE = 1;
1644 public static final int CATEGORY_APP = 2;
1645
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001646 public final int key;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001647 public boolean restricted;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001648 public int category;
1649
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001650 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001651 public long total;
1652
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001653 public AppItem() {
1654 this.key = 0;
1655 }
1656
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001657 public AppItem(int key) {
1658 this.key = key;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001659 }
1660
1661 public AppItem(Parcel parcel) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001662 key = parcel.readInt();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001663 uids = parcel.readSparseBooleanArray();
1664 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001665 }
1666
1667 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001668 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001669 }
1670
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001671 @Override
1672 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001673 dest.writeInt(key);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001674 dest.writeSparseBooleanArray(uids);
1675 dest.writeLong(total);
1676 }
1677
1678 @Override
1679 public int describeContents() {
1680 return 0;
1681 }
1682
1683 @Override
1684 public int compareTo(AppItem another) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001685 int comparison = Integer.compare(category, another.category);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001686 if (comparison == 0) {
1687 comparison = Long.compare(another.total, total);
1688 }
1689 return comparison;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001690 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001691
1692 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1693 @Override
1694 public AppItem createFromParcel(Parcel in) {
1695 return new AppItem(in);
1696 }
1697
1698 @Override
1699 public AppItem[] newArray(int size) {
1700 return new AppItem[size];
1701 }
1702 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001703 }
1704
Jeff Sharkey8a503642011-06-10 13:31:21 -07001705 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001706 * Adapter of applications, sorted by total usage descending.
1707 */
1708 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001709 private final UidDetailProvider mProvider;
1710 private final int mInsetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001711 private final UserManager mUm;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001712
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001713 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001714 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001715
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001716 public DataUsageAdapter(final UserManager userManager, UidDetailProvider provider, int insetSide) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001717 mProvider = checkNotNull(provider);
1718 mInsetSide = insetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001719 mUm = userManager;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001720 }
1721
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001722 /**
1723 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1724 */
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001725 public void bindStats(NetworkStats stats, int[] restrictedUids) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001726 mItems.clear();
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001727 mLargest = 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001728
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001729 final int currentUserId = ActivityManager.getCurrentUser();
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001730 final List<UserHandle> profiles = mUm.getUserProfiles();
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001731 final SparseArray<AppItem> knownItems = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001732
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001733 NetworkStats.Entry entry = null;
1734 final int size = stats != null ? stats.size() : 0;
1735 for (int i = 0; i < size; i++) {
1736 entry = stats.getValues(i, entry);
1737
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001738 // Decide how to collapse items together
1739 final int uid = entry.uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001740
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001741 final int collapseKey;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001742 final int category;
1743 final int userId = UserHandle.getUserId(uid);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001744 if (UserHandle.isApp(uid)) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001745 if (profiles.contains(new UserHandle(userId))) {
1746 if (userId != currentUserId) {
1747 // Add to a managed user item.
1748 final int managedKey = UidDetailProvider.buildKeyForUser(userId);
1749 accumulate(managedKey, knownItems, entry,
1750 AppItem.CATEGORY_USER);
1751 }
1752 // Add to app item.
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001753 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001754 category = AppItem.CATEGORY_APP;
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001755 } else {
Zoltan Szatmary-Ban77c1d362014-12-12 18:48:03 +00001756 // If it is a removed user add it to the removed users' key
1757 final UserInfo info = mUm.getUserInfo(userId);
1758 if (info == null) {
1759 collapseKey = UID_REMOVED;
1760 category = AppItem.CATEGORY_APP;
1761 } else {
1762 // Add to other user item.
1763 collapseKey = UidDetailProvider.buildKeyForUser(userId);
1764 category = AppItem.CATEGORY_USER;
1765 }
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001766 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001767 } else if (uid == UID_REMOVED || uid == UID_TETHERING) {
1768 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001769 category = AppItem.CATEGORY_APP;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001770 } else {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001771 collapseKey = android.os.Process.SYSTEM_UID;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001772 category = AppItem.CATEGORY_APP;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001773 }
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001774 accumulate(collapseKey, knownItems, entry, category);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001775 }
1776
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001777 final int restrictedUidsMax = restrictedUids.length;
1778 for (int i = 0; i < restrictedUidsMax; ++i) {
1779 final int uid = restrictedUids[i];
1780 // Only splice in restricted state for current user or managed users
1781 if (!profiles.contains(new UserHandle(UserHandle.getUserId(uid)))) {
1782 continue;
1783 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001784
1785 AppItem item = knownItems.get(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001786 if (item == null) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001787 item = new AppItem(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001788 item.total = -1;
1789 mItems.add(item);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001790 knownItems.put(item.key, item);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001791 }
1792 item.restricted = true;
1793 }
1794
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001795 if (!mItems.isEmpty()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001796 final AppItem title = new AppItem();
1797 title.category = AppItem.CATEGORY_APP_TITLE;
1798 mItems.add(title);
1799 }
1800
Jeff Sharkey8a503642011-06-10 13:31:21 -07001801 Collections.sort(mItems);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001802 notifyDataSetChanged();
1803 }
1804
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001805 /**
1806 * Accumulate data usage of a network stats entry for the item mapped by the collapse key.
1807 * Creates the item if needed.
1808 *
1809 * @param collapseKey the collapse key used to map the item.
1810 * @param knownItems collection of known (already existing) items.
1811 * @param entry the network stats entry to extract data usage from.
1812 * @param itemCategory the item is categorized on the list view by this category. Must be
1813 * either AppItem.APP_ITEM_CATEGORY or AppItem.MANAGED_USER_ITEM_CATEGORY
1814 */
1815 private void accumulate(int collapseKey, final SparseArray<AppItem> knownItems,
1816 NetworkStats.Entry entry, int itemCategory) {
1817 final int uid = entry.uid;
1818 AppItem item = knownItems.get(collapseKey);
1819 if (item == null) {
1820 item = new AppItem(collapseKey);
1821 item.category = itemCategory;
1822 mItems.add(item);
1823 knownItems.put(item.key, item);
1824 }
1825 item.addUid(uid);
1826 item.total += entry.rxBytes + entry.txBytes;
1827 if (mLargest < item.total) {
1828 mLargest = item.total;
1829 }
1830 }
1831
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001832 @Override
1833 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001834 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001835 }
1836
1837 @Override
1838 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001839 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001840 }
1841
1842 @Override
1843 public long getItemId(int position) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001844 return mItems.get(position).key;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001845 }
1846
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001847 /**
1848 * See {@link #getItemViewType} for the view types.
1849 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001850 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001851 public int getViewTypeCount() {
1852 return 2;
1853 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001854
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001855 /**
1856 * Returns 1 for separator items and 0 for anything else.
1857 */
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001858 @Override
1859 public int getItemViewType(int position) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001860 final AppItem item = mItems.get(position);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001861 if (item.category == AppItem.CATEGORY_APP_TITLE) {
1862 return 1;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001863 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001864 return 0;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001865 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001866 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001867
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001868 @Override
1869 public boolean areAllItemsEnabled() {
1870 return false;
1871 }
1872
1873 @Override
1874 public boolean isEnabled(int position) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001875 if (position > mItems.size()) {
1876 throw new ArrayIndexOutOfBoundsException();
1877 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001878 return getItemViewType(position) == 0;
1879 }
1880
1881 @Override
1882 public View getView(int position, View convertView, ViewGroup parent) {
1883 final AppItem item = mItems.get(position);
1884 if (getItemViewType(position) == 1) {
1885 if (convertView == null) {
Zoltan Szatmary-Ban3af2e4c2014-12-19 17:17:23 +00001886 convertView = Utils.inflateCategoryHeader(LayoutInflater.from(
1887 parent.getContext()), parent);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001888 }
1889
1890 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
1891 title.setText(R.string.data_usage_app);
1892
1893 } else {
1894 if (convertView == null) {
1895 convertView = LayoutInflater.from(parent.getContext()).inflate(
1896 R.layout.data_usage_item, parent, false);
1897
1898 if (mInsetSide > 0) {
1899 convertView.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
1900 }
1901 }
1902
1903 final Context context = parent.getContext();
1904
1905 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
1906 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1907 android.R.id.progress);
1908
1909 // kick off async load of app details
1910 UidDetailTask.bindView(mProvider, item, convertView);
1911
1912 if (item.restricted && item.total <= 0) {
1913 text1.setText(R.string.data_usage_app_restricted);
1914 progress.setVisibility(View.GONE);
1915 } else {
1916 text1.setText(Formatter.formatFileSize(context, item.total));
1917 progress.setVisibility(View.VISIBLE);
1918 }
1919
1920 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1921 progress.setProgress(percentTotal);
1922 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001923
1924 return convertView;
1925 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001926 }
1927
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001928 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001929 * Empty {@link Fragment} that controls display of UID details in
1930 * {@link DataUsageSummary}.
1931 */
1932 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001933 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001934
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001935 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jason Monk4bb075b2015-03-17 10:06:58 -04001936 show(parent, app, label, true);
1937 }
1938
1939 public static void show(DataUsageSummary parent, AppItem app, CharSequence label,
1940 boolean addToBack) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001941 if (!parent.isAdded()) return;
1942
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001943 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001944 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001945
1946 final AppDetailsFragment fragment = new AppDetailsFragment();
1947 fragment.setArguments(args);
1948 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001949 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1950 ft.add(fragment, TAG_APP_DETAILS);
Jason Monk4bb075b2015-03-17 10:06:58 -04001951 if (addToBack) {
1952 ft.addToBackStack(TAG_APP_DETAILS);
1953 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001954 ft.setBreadCrumbTitle(
1955 parent.getResources().getString(R.string.data_usage_app_summary_title));
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001956 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001957 }
1958
1959 @Override
1960 public void onStart() {
1961 super.onStart();
1962 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001963 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001964 target.updateBody();
1965 }
1966
1967 @Override
1968 public void onStop() {
1969 super.onStop();
1970 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001971 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001972 target.updateBody();
1973 }
1974 }
1975
1976 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001977 * Dialog to request user confirmation before setting
1978 * {@link NetworkPolicy#limitBytes}.
1979 */
1980 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001981 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001982 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001983
1984 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001985 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001986
Jeff Sharkeyd0a55312014-08-08 10:25:24 -07001987 final NetworkPolicy policy = parent.mPolicyEditor.getPolicy(parent.mTemplate);
1988 if (policy == null) return;
1989
Jeff Sharkey461842a2011-09-25 18:22:48 -07001990 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001991 final CharSequence message;
Jeff Sharkeyd0a55312014-08-08 10:25:24 -07001992 final long minLimitBytes = (long) (policy.warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001993 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001994
1995 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001996 final String currentTab = parent.mCurrentTab;
1997 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001998 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001999 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002000 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07002001 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07002002 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002003 } else if (isMobileTab(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07002004 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07002005 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07002006 } else {
2007 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002008 }
2009
Jeff Sharkey131f9d62011-08-17 17:08:19 -07002010 final Bundle args = new Bundle();
2011 args.putCharSequence(EXTRA_MESSAGE, message);
2012 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
2013
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002014 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
2015 dialog.setArguments(args);
2016 dialog.setTargetFragment(parent, 0);
2017 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
2018 }
2019
2020 @Override
2021 public Dialog onCreateDialog(Bundle savedInstanceState) {
2022 final Context context = getActivity();
2023
Jeff Sharkey131f9d62011-08-17 17:08:19 -07002024 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002025 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
2026
2027 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2028 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07002029 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002030
2031 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002032 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002033 public void onClick(DialogInterface dialog, int which) {
2034 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2035 if (target != null) {
2036 target.setPolicyLimitBytes(limitBytes);
2037 }
2038 }
2039 });
2040
2041 return builder.create();
2042 }
2043 }
2044
2045 /**
2046 * Dialog to edit {@link NetworkPolicy#cycleDay}.
2047 */
2048 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002049 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002050
2051 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002052 if (!parent.isAdded()) return;
2053
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002054 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002055 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002056
2057 final CycleEditorFragment dialog = new CycleEditorFragment();
2058 dialog.setArguments(args);
2059 dialog.setTargetFragment(parent, 0);
2060 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
2061 }
2062
2063 @Override
2064 public Dialog onCreateDialog(Bundle savedInstanceState) {
2065 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002066 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2067 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002068
2069 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2070 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2071
2072 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
2073 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
2074
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002075 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2076 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002077
2078 cycleDayPicker.setMinValue(1);
2079 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002080 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002081 cycleDayPicker.setWrapSelectorWheel(true);
2082
2083 builder.setTitle(R.string.data_usage_cycle_editor_title);
2084 builder.setView(view);
2085
2086 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2087 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002088 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002089 public void onClick(DialogInterface dialog, int which) {
Jeff Sharkeyd277de92013-03-25 15:11:25 -07002090 // clear focus to finish pending text edits
2091 cycleDayPicker.clearFocus();
2092
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002093 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08002094 final String cycleTimezone = new Time().timezone;
2095 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002096 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002097 }
2098 });
2099
2100 return builder.create();
2101 }
2102 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002103
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002104 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002105 * Dialog to edit {@link NetworkPolicy#warningBytes}.
2106 */
2107 public static class WarningEditorFragment extends DialogFragment {
2108 private static final String EXTRA_TEMPLATE = "template";
2109
2110 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002111 if (!parent.isAdded()) return;
2112
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002113 final Bundle args = new Bundle();
2114 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
2115
2116 final WarningEditorFragment dialog = new WarningEditorFragment();
2117 dialog.setArguments(args);
2118 dialog.setTargetFragment(parent, 0);
2119 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
2120 }
2121
2122 @Override
2123 public Dialog onCreateDialog(Bundle savedInstanceState) {
2124 final Context context = getActivity();
2125 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2126 final NetworkPolicyEditor editor = target.mPolicyEditor;
2127
2128 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2129 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2130
2131 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
2132 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
2133
2134 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2135 final long warningBytes = editor.getPolicyWarningBytes(template);
2136 final long limitBytes = editor.getPolicyLimitBytes(template);
2137
2138 bytesPicker.setMinValue(0);
2139 if (limitBytes != LIMIT_DISABLED) {
2140 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
2141 } else {
2142 bytesPicker.setMaxValue(Integer.MAX_VALUE);
2143 }
2144 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
2145 bytesPicker.setWrapSelectorWheel(false);
2146
2147 builder.setTitle(R.string.data_usage_warning_editor_title);
2148 builder.setView(view);
2149
2150 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2151 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002152 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002153 public void onClick(DialogInterface dialog, int which) {
2154 // clear focus to finish pending text edits
2155 bytesPicker.clearFocus();
2156
2157 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
2158 editor.setPolicyWarningBytes(template, bytes);
2159 target.updatePolicy(false);
2160 }
2161 });
2162
2163 return builder.create();
2164 }
2165 }
2166
2167 /**
2168 * Dialog to edit {@link NetworkPolicy#limitBytes}.
2169 */
2170 public static class LimitEditorFragment extends DialogFragment {
2171 private static final String EXTRA_TEMPLATE = "template";
2172
2173 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002174 if (!parent.isAdded()) return;
2175
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002176 final Bundle args = new Bundle();
2177 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
2178
2179 final LimitEditorFragment dialog = new LimitEditorFragment();
2180 dialog.setArguments(args);
2181 dialog.setTargetFragment(parent, 0);
2182 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
2183 }
2184
2185 @Override
2186 public Dialog onCreateDialog(Bundle savedInstanceState) {
2187 final Context context = getActivity();
2188 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2189 final NetworkPolicyEditor editor = target.mPolicyEditor;
2190
2191 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2192 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2193
2194 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
2195 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
2196
2197 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2198 final long warningBytes = editor.getPolicyWarningBytes(template);
2199 final long limitBytes = editor.getPolicyLimitBytes(template);
2200
2201 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09002202 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002203 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
2204 } else {
2205 bytesPicker.setMinValue(0);
2206 }
2207 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
2208 bytesPicker.setWrapSelectorWheel(false);
2209
2210 builder.setTitle(R.string.data_usage_limit_editor_title);
2211 builder.setView(view);
2212
2213 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2214 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002215 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002216 public void onClick(DialogInterface dialog, int which) {
2217 // clear focus to finish pending text edits
2218 bytesPicker.clearFocus();
2219
2220 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
2221 editor.setPolicyLimitBytes(template, bytes);
2222 target.updatePolicy(false);
2223 }
2224 });
2225
2226 return builder.create();
2227 }
2228 }
2229 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07002230 * Dialog to request user confirmation before disabling data.
2231 */
2232 public static class ConfirmDataDisableFragment extends DialogFragment {
Sanket Padawe24f834d2015-01-08 11:23:11 -08002233 static int mSubId;
2234 public static void show(DataUsageSummary parent, int subId) {
2235 mSubId = subId;
Jeff Sharkey461842a2011-09-25 18:22:48 -07002236 if (!parent.isAdded()) return;
2237
Jeff Sharkey28130d92011-09-02 16:10:24 -07002238 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
2239 dialog.setTargetFragment(parent, 0);
2240 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
2241 }
2242
2243 @Override
2244 public Dialog onCreateDialog(Bundle savedInstanceState) {
2245 final Context context = getActivity();
2246
2247 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2248 builder.setMessage(R.string.data_usage_disable_mobile);
2249
2250 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002251 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07002252 public void onClick(DialogInterface dialog, int which) {
2253 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2254 if (target != null) {
2255 // TODO: extend to modify policy enabled flag.
Sanket Padawe24f834d2015-01-08 11:23:11 -08002256 target.setMobileDataEnabled(mSubId, false);
Jeff Sharkey28130d92011-09-02 16:10:24 -07002257 }
2258 }
2259 });
2260 builder.setNegativeButton(android.R.string.cancel, null);
2261
2262 return builder.create();
2263 }
2264 }
2265
2266 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002267 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07002268 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002269 */
2270 public static class ConfirmRestrictFragment extends DialogFragment {
2271 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002272 if (!parent.isAdded()) return;
2273
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002274 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
2275 dialog.setTargetFragment(parent, 0);
2276 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
2277 }
2278
2279 @Override
2280 public Dialog onCreateDialog(Bundle savedInstanceState) {
2281 final Context context = getActivity();
2282
2283 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002284 builder.setTitle(R.string.data_usage_restrict_background_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07002285 if (Utils.hasMultipleUsers(context)) {
2286 builder.setMessage(R.string.data_usage_restrict_background_multiuser);
2287 } else {
2288 builder.setMessage(R.string.data_usage_restrict_background);
2289 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002290
2291 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002292 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002293 public void onClick(DialogInterface dialog, int which) {
2294 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2295 if (target != null) {
2296 target.setRestrictBackground(true);
2297 }
2298 }
2299 });
2300 builder.setNegativeButton(android.R.string.cancel, null);
2301
2302 return builder.create();
2303 }
2304 }
2305
2306 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002307 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
2308 * change has been denied, usually based on
2309 * {@link DataUsageSummary#hasLimitedNetworks()}.
2310 */
2311 public static class DeniedRestrictFragment extends DialogFragment {
2312 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002313 if (!parent.isAdded()) return;
2314
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002315 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
2316 dialog.setTargetFragment(parent, 0);
2317 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
2318 }
2319
2320 @Override
2321 public Dialog onCreateDialog(Bundle savedInstanceState) {
2322 final Context context = getActivity();
2323
2324 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2325 builder.setTitle(R.string.data_usage_app_restrict_background);
2326 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
2327 builder.setPositiveButton(android.R.string.ok, null);
2328
2329 return builder.create();
2330 }
2331 }
2332
2333 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002334 * Dialog to request user confirmation before setting
2335 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
2336 */
2337 public static class ConfirmAppRestrictFragment extends DialogFragment {
2338 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002339 if (!parent.isAdded()) return;
2340
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002341 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2342 dialog.setTargetFragment(parent, 0);
2343 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2344 }
2345
2346 @Override
2347 public Dialog onCreateDialog(Bundle savedInstanceState) {
2348 final Context context = getActivity();
2349
2350 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002351 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2352 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2353
2354 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002355 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002356 public void onClick(DialogInterface dialog, int which) {
2357 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2358 if (target != null) {
2359 target.setAppRestrictBackground(true);
2360 }
2361 }
2362 });
2363 builder.setNegativeButton(android.R.string.cancel, null);
2364
2365 return builder.create();
2366 }
2367 }
2368
2369 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002370 * Compute default tab that should be selected, based on
2371 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2372 */
2373 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002374 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002375 if (template == null) {
Wink Savilleb003a852014-10-23 10:16:26 -07002376 final int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
Wink Saville0183fb52014-11-22 10:11:39 -08002377 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Savilled72c0622014-12-11 10:10:05 -08002378 if (SubscriptionManager.isValidSubscriptionId(subId)) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07002379 return TAB_MOBILE + String.valueOf(subId);
2380 }
2381 return null;
2382 }
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002383
2384 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002385 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002386 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002387 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002388 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002389 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002390 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002391 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002392 return TAB_WIFI;
2393 default:
2394 return null;
2395 }
2396 }
2397
2398 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002399 * Background task that loads {@link UidDetail}, binding to
2400 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002401 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002402 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2403 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002404 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002405 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002406
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002407 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002408 mProvider = checkNotNull(provider);
2409 mItem = checkNotNull(item);
2410 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002411 }
2412
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002413 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002414 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002415 final UidDetailTask existing = (UidDetailTask) target.getTag();
2416 if (existing != null) {
2417 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002418 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002419
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002420 final UidDetail cachedDetail = provider.getUidDetail(item.key, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002421 if (cachedDetail != null) {
2422 bindView(cachedDetail, target);
2423 } else {
2424 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2425 AsyncTask.THREAD_POOL_EXECUTOR));
2426 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002427 }
2428
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002429 private static void bindView(UidDetail detail, View target) {
2430 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2431 final TextView title = (TextView) target.findViewById(android.R.id.title);
2432
2433 if (detail != null) {
2434 icon.setImageDrawable(detail.icon);
2435 title.setText(detail.label);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +01002436 title.setContentDescription(detail.contentDescription);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002437 } else {
2438 icon.setImageDrawable(null);
2439 title.setText(null);
2440 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002441 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002442
2443 @Override
2444 protected void onPreExecute() {
2445 bindView(null, mTarget);
2446 }
2447
2448 @Override
2449 protected UidDetail doInBackground(Void... params) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002450 return mProvider.getUidDetail(mItem.key, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002451 }
2452
2453 @Override
2454 protected void onPostExecute(UidDetail result) {
2455 bindView(result, mTarget);
2456 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002457 }
2458
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002459 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002460 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002461 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002462 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002463 if (TEST_RADIOS) {
2464 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2465 }
2466
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002467 final ConnectivityManager conn = ConnectivityManager.from(context);
2468 final TelephonyManager tele = TelephonyManager.from(context);
2469
Wink Saville0183fb52014-11-22 10:11:39 -08002470 final List<SubscriptionInfo> subInfoList =
2471 SubscriptionManager.from(context).getActiveSubscriptionInfoList();
2472 // No activated Subscriptions
PauloftheWest50e6eca2014-10-03 11:07:14 -07002473 if (subInfoList == null) {
Wink Savillefcec91f2014-12-02 17:12:08 -08002474 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subInfoList=null");
PauloftheWest50e6eca2014-10-03 11:07:14 -07002475 return false;
2476 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002477 // require both supported network and ready SIM
PauloftheWest50e6eca2014-10-03 11:07:14 -07002478 boolean isReady = true;
Wink Savilleca756612014-11-08 10:47:12 -08002479 for (SubscriptionInfo subInfo : subInfoList) {
Stuart Scott3ada2ec2014-10-31 14:11:24 -07002480 isReady = isReady & tele.getSimState(subInfo.getSimSlotIndex()) == SIM_STATE_READY;
Wink Savillefcec91f2014-12-02 17:12:08 -08002481 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subInfo=" + subInfo);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002482 }
Wink Savillefcec91f2014-12-02 17:12:08 -08002483 boolean retVal = conn.isNetworkSupported(TYPE_MOBILE) && isReady;
2484 if (LOGD) {
2485 Log.d(TAG, "hasReadyMobileRadio:"
2486 + " conn.isNetworkSupported(TYPE_MOBILE)="
2487 + conn.isNetworkSupported(TYPE_MOBILE)
2488 + " isReady=" + isReady);
2489 }
2490 return retVal;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002491 }
2492
2493 /*
2494 * TODO: consider adding to TelephonyManager or SubscritpionManager.
2495 */
Wink Savilleb003a852014-10-23 10:16:26 -07002496 public static boolean hasReadyMobileRadio(Context context, int subId) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07002497 if (TEST_RADIOS) {
2498 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2499 }
2500
2501 final ConnectivityManager conn = ConnectivityManager.from(context);
2502 final TelephonyManager tele = TelephonyManager.from(context);
2503 final int slotId = SubscriptionManager.getSlotId(subId);
2504 final boolean isReady = tele.getSimState(slotId) == SIM_STATE_READY;
2505
Wink Savillefcec91f2014-12-02 17:12:08 -08002506 boolean retVal = conn.isNetworkSupported(TYPE_MOBILE) && isReady;
2507 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subId=" + subId
2508 + " conn.isNetworkSupported(TYPE_MOBILE)=" + conn.isNetworkSupported(TYPE_MOBILE)
2509 + " isReady=" + isReady);
2510 return retVal;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002511 }
2512
2513 /**
2514 * Test if device has a mobile 4G data radio.
2515 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002516 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002517 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2518 return false;
2519 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002520 if (TEST_RADIOS) {
2521 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2522 }
2523
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002524 final ConnectivityManager conn = ConnectivityManager.from(context);
2525 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002526
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002527 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Wink Saville55434042012-06-14 12:33:43 -07002528 final boolean hasLte = (tele.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE)
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002529 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002530 return hasWimax || hasLte;
2531 }
2532
2533 /**
2534 * Test if device has a Wi-Fi data radio.
2535 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002536 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002537 if (TEST_RADIOS) {
2538 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2539 }
2540
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002541 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002542 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002543 }
2544
2545 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002546 * Test if device has an ethernet network connection.
2547 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002548 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002549 if (TEST_RADIOS) {
2550 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2551 }
2552
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002553 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002554 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2555
2556 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002557 if (mStatsSession != null) {
2558 try {
2559 ethernetBytes = mStatsSession.getSummaryForNetwork(
2560 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2561 .getTotalBytes();
2562 } catch (RemoteException e) {
2563 throw new RuntimeException(e);
2564 }
2565 } else {
2566 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002567 }
2568
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002569 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002570 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002571 }
2572
2573 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002574 * Inflate a {@link Preference} style layout, adding the given {@link View}
2575 * widget into {@link android.R.id#widget_frame}.
2576 */
2577 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2578 final View view = inflater.inflate(R.layout.preference, root, false);
2579 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2580 android.R.id.widget_frame);
2581 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2582 return view;
2583 }
2584
2585 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002586 * Test if any networks are currently limited.
2587 */
2588 private boolean hasLimitedNetworks() {
2589 return !buildLimitedNetworksList().isEmpty();
2590 }
2591
2592 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002593 * Build string describing currently limited networks, which defines when
2594 * background data is restricted.
2595 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002596 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002597 private CharSequence buildLimitedNetworksString() {
2598 final List<CharSequence> limited = buildLimitedNetworksList();
2599
2600 // handle case where no networks limited
2601 if (limited.isEmpty()) {
2602 limited.add(getText(R.string.data_usage_list_none));
2603 }
2604
2605 return TextUtils.join(limited);
2606 }
2607
2608 /**
2609 * Build list of currently limited networks, which defines when background
2610 * data is restricted.
2611 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002612 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002613 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002614 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002615
2616 // build combined list of all limited networks
2617 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002618
2619 final TelephonyManager tele = TelephonyManager.from(context);
2620 if (tele.getSimState() == SIM_STATE_READY) {
2621 final String subscriberId = getActiveSubscriberId(context);
2622 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2623 limited.add(getText(R.string.data_usage_list_mobile));
2624 }
2625 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2626 limited.add(getText(R.string.data_usage_tab_3g));
2627 }
2628 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2629 limited.add(getText(R.string.data_usage_tab_4g));
2630 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002631 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002632
2633 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002634 limited.add(getText(R.string.data_usage_tab_wifi));
2635 }
2636 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2637 limited.add(getText(R.string.data_usage_tab_ethernet));
2638 }
2639
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002640 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002641 }
2642
2643 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002644 * Inset both selector and divider {@link Drawable} on the given
2645 * {@link ListView} by the requested dimensions.
2646 */
2647 private static void insetListViewDrawables(ListView view, int insetSide) {
2648 final Drawable selector = view.getSelector();
2649 final Drawable divider = view.getDivider();
2650
2651 // fully unregister these drawables so callbacks can be maintained after
2652 // wrapping below.
2653 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2654 view.setSelector(stub);
2655 view.setDivider(stub);
2656
2657 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2658 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2659 }
2660
2661 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002662 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002663 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002664 */
2665 private static void setPreferenceTitle(View parent, int resId) {
2666 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2667 title.setText(resId);
2668 }
2669
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002670 /**
2671 * Set {@link android.R.id#summary} for a preference view inflated with
2672 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2673 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002674 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002675 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2676 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002677 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002678 }
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002679
2680 /**
2681 * For search
2682 */
2683 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -07002684 new BaseSearchIndexProvider() {
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002685 @Override
2686 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
2687 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
2688
2689 final Resources res = context.getResources();
2690
2691 // Add fragment title
2692 SearchIndexableRaw data = new SearchIndexableRaw(context);
2693 data.title = res.getString(R.string.data_usage_summary_title);
2694 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2695 result.add(data);
2696
2697 // Mobile data
2698 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002699 data.key = DATA_USAGE_ENABLE_MOBILE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002700 data.title = res.getString(R.string.data_usage_enable_mobile);
2701 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2702 result.add(data);
2703
2704 // Set mobile data limit
2705 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002706 data.key = DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002707 data.title = res.getString(R.string.data_usage_disable_mobile_limit);
2708 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2709 result.add(data);
2710
Fabrice Di Megliof2a52262014-04-17 17:20:27 -07002711 // Data usage cycle
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002712 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002713 data.key = DATA_USAGE_CYCLE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002714 data.title = res.getString(R.string.data_usage_cycle);
2715 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2716 result.add(data);
2717
2718 return result;
2719 }
2720 };
2721
Sanket Padawe7e21fa52015-02-12 12:39:34 -08002722 private void addMobileTab(Context context, SubscriptionInfo subInfo, boolean isMultiSim) {
PauloftheWestc80b7612014-11-25 04:40:45 -08002723 if (subInfo != null && mMobileTagMap != null) {
2724 if (hasReadyMobileRadio(context, subInfo.getSubscriptionId())) {
Sanket Padawe7e21fa52015-02-12 12:39:34 -08002725 if (isMultiSim) {
2726 mTabHost.addTab(buildTabSpec(mMobileTagMap.get(subInfo.getSubscriptionId()),
2727 subInfo.getDisplayName()));
2728 } else {
2729 mTabHost.addTab(buildTabSpec(mMobileTagMap.get(subInfo.getSubscriptionId()),
2730 R.string.data_usage_tab_mobile));
2731 }
PauloftheWest50e6eca2014-10-03 11:07:14 -07002732 }
Wink Savillefcec91f2014-12-02 17:12:08 -08002733 } else {
2734 if (LOGD) Log.d(TAG, "addMobileTab: subInfoList is null");
PauloftheWest50e6eca2014-10-03 11:07:14 -07002735 }
2736 }
2737
Wink Savilleca756612014-11-08 10:47:12 -08002738 private SubscriptionInfo getCurrentTabSubInfo(Context context) {
PauloftheWestde338442014-10-29 14:46:44 -07002739 if (mSubInfoList != null && mTabHost != null) {
2740 final int currentTagIndex = mTabHost.getCurrentTab();
2741 int i = 0;
Wink Savilleca756612014-11-08 10:47:12 -08002742 for (SubscriptionInfo subInfo : mSubInfoList) {
Stuart Scott3ada2ec2014-10-31 14:11:24 -07002743 if (hasReadyMobileRadio(context, subInfo.getSubscriptionId())) {
PauloftheWestde338442014-10-29 14:46:44 -07002744 if (i++ == currentTagIndex) {
2745 return subInfo;
2746 }
2747 }
2748 }
2749 }
2750 return null;
2751 }
2752
PauloftheWest50e6eca2014-10-03 11:07:14 -07002753 /**
2754 * Init a map with subId key and mobile tag name
2755 * @param subInfoList The subscription Info List
2756 * @return The map or null if no activated subscription
2757 */
Wink Savilleca756612014-11-08 10:47:12 -08002758 private Map<Integer, String> initMobileTabTag(List<SubscriptionInfo> subInfoList) {
Wink Savilleb003a852014-10-23 10:16:26 -07002759 Map<Integer, String> map = null;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002760 if (subInfoList != null) {
2761 String mobileTag;
Wink Savilleb003a852014-10-23 10:16:26 -07002762 map = new HashMap<Integer, String>();
Wink Savillefcec91f2014-12-02 17:12:08 -08002763 for (SubscriptionInfo subInfo : subInfoList) {
2764 mobileTag = TAB_MOBILE + String.valueOf(subInfo.getSubscriptionId());
2765 map.put(subInfo.getSubscriptionId(), mobileTag);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002766 }
2767 }
2768 return map;
2769 }
2770
2771 private static boolean isMobileTab(String currentTab) {
2772 return currentTab != null ? currentTab.contains(TAB_MOBILE) : false;
2773 }
2774
Wink Savilleb003a852014-10-23 10:16:26 -07002775 private int getSubId(String currentTab) {
Wink Saville0183fb52014-11-22 10:11:39 -08002776 if (mMobileTagMap != null) {
2777 Set<Integer> set = mMobileTagMap.keySet();
2778 for (Integer subId : set) {
2779 if (mMobileTagMap.get(subId).equals(currentTab)) {
2780 return subId;
2781 }
PauloftheWest50e6eca2014-10-03 11:07:14 -07002782 }
2783 }
2784 Log.e(TAG, "currentTab = " + currentTab + " non mobile tab called this function");
2785 return -1;
2786 }
2787
Stuart Scottf51d0062015-04-08 09:40:48 -07002788 private boolean isMobileDataAvailable(int subId) {
2789 return mSubscriptionManager.getActiveSubscriptionInfo(subId) != null;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002790 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002791}