blob: ce68e3d590727624d79fc0445c21c7f10b838916 [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;
Zoltan Szatmary-Ban77c1d362014-12-12 18:48:03 +000065import android.content.pm.UserInfo;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070066import android.content.res.Resources;
Jeff Sharkeyb6548462014-07-21 15:38:06 -070067import android.content.res.TypedArray;
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;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700115import android.widget.ImageView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700116import android.widget.LinearLayout;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700117import android.widget.ListView;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700118import android.widget.NumberPicker;
Jeff Sharkey2412b0f2011-07-17 20:31:40 -0700119import android.widget.ProgressBar;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700120import android.widget.Spinner;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700121import android.widget.Switch;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700122import android.widget.TabHost;
123import android.widget.TabHost.OnTabChangeListener;
124import android.widget.TabHost.TabContentFactory;
125import android.widget.TabHost.TabSpec;
126import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700127import android.widget.TextView;
128
Wink Saville55434042012-06-14 12:33:43 -0700129import com.android.internal.telephony.PhoneConstants;
Jeff Sharkey5d706792011-09-08 18:57:17 -0700130import com.android.settings.drawable.InsetBoundsDrawable;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700131import com.android.settings.net.ChartData;
132import com.android.settings.net.ChartDataLoader;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700133import com.android.settings.net.DataUsageMeteredSettings;
Jeff Sharkeya662e492011-06-18 21:57:06 -0700134import com.android.settings.net.NetworkPolicyEditor;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700135import com.android.settings.net.SummaryForAllUidLoader;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700136import com.android.settings.net.UidDetail;
137import com.android.settings.net.UidDetailProvider;
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -0700138import com.android.settings.search.BaseSearchIndexProvider;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -0700139import com.android.settings.search.Indexable;
140import com.android.settings.search.SearchIndexableRaw;
PauloftheWest50e6eca2014-10-03 11:07:14 -0700141import com.android.settings.sim.SimSettings;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700142import com.android.settings.widget.ChartDataUsageView;
143import com.android.settings.widget.ChartDataUsageView.DataUsageChartListener;
PauloftheWestc80b7612014-11-25 04:40:45 -0800144import com.android.settings.widget.ChartNetworkSeriesView;
Zoltan Szatmary-Ban77c1d362014-12-12 18:48:03 +0000145
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700146import com.google.android.collect.Lists;
147
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700148import libcore.util.Objects;
149
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700150import java.util.ArrayList;
151import java.util.Collections;
PauloftheWest50e6eca2014-10-03 11:07:14 -0700152import java.util.HashMap;
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700153import java.util.List;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700154import java.util.Locale;
PauloftheWest50e6eca2014-10-03 11:07:14 -0700155import java.util.Map;
156import java.util.Set;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700157
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700158/**
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700159 * Panel showing data usage history across various networks, including options
160 * to inspect based on usage cycle and control through {@link NetworkPolicy}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700161 */
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700162public class DataUsageSummary extends HighlightingFragment implements Indexable {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700163 private static final String TAG = "DataUsage";
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800164 private static final boolean LOGD = false;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700165
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700166 // TODO: remove this testing code
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700167 private static final boolean TEST_ANIM = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700168 private static final boolean TEST_RADIOS = false;
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800169
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700170 private static final String TEST_RADIOS_PROP = "test.radios";
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800171 private static final String TEST_SUBSCRIBER_PROP = "test.subscriberid";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700172
Jeff Sharkey8a503642011-06-10 13:31:21 -0700173 private static final String TAB_3G = "3g";
174 private static final String TAB_4G = "4g";
175 private static final String TAB_MOBILE = "mobile";
176 private static final String TAB_WIFI = "wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700177 private static final String TAB_ETHERNET = "ethernet";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700178
Jeff Sharkey28130d92011-09-02 16:10:24 -0700179 private static final String TAG_CONFIRM_DATA_DISABLE = "confirmDataDisable";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700180 private static final String TAG_CONFIRM_LIMIT = "confirmLimit";
181 private static final String TAG_CYCLE_EDITOR = "cycleEditor";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700182 private static final String TAG_WARNING_EDITOR = "warningEditor";
183 private static final String TAG_LIMIT_EDITOR = "limitEditor";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700184 private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700185 private static final String TAG_DENIED_RESTRICT = "deniedRestrict";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700186 private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700187 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700188
Fabrice Di Meglio2169c882014-04-18 15:18:40 -0700189 private static final String DATA_USAGE_ENABLE_MOBILE_KEY = "data_usage_enable_mobile";
190 private static final String DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY =
191 "data_usage_disable_mobile_limit";
192 private static final String DATA_USAGE_CYCLE_KEY = "data_usage_cycle";
193
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700194 private static final int LOADER_CHART_DATA = 2;
195 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700196
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700197 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700198 private INetworkStatsService mStatsService;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700199 private NetworkPolicyManager mPolicyManager;
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700200 private TelephonyManager mTelephonyManager;
Wink Saville0183fb52014-11-22 10:11:39 -0800201 private SubscriptionManager mSubscriptionManager;
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700202
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700203 private INetworkStatsSession mStatsSession;
204
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700205 private static final String PREF_FILE = "data_usage";
206 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700207 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700208
209 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700210
Jeff Sharkey8a503642011-06-10 13:31:21 -0700211 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700212 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700213 private TabWidget mTabWidget;
214 private ListView mListView;
PauloftheWestc80b7612014-11-25 04:40:45 -0800215 private ChartNetworkSeriesView mSeries;
216 private ChartNetworkSeriesView mDetailedSeries;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700217 private DataUsageAdapter mAdapter;
218
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700219 /** Distance to inset content from sides, when needed. */
220 private int mInsetSide = 0;
221
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700222 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700223
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700224 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700225 private LinearLayout mNetworkSwitches;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700226 private boolean mDataEnabledSupported;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700227 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700228 private View mDataEnabledView;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700229 private boolean mDisableAtLimitSupported;
230 private Switch mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700231 private View mDisableAtLimitView;
232
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700233 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700234 private Spinner mCycleSpinner;
235 private CycleAdapter mCycleAdapter;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700236 private TextView mCycleSummary;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700237
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700238 private ChartDataUsageView mChart;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700239 private View mDisclaimer;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700240 private TextView mEmpty;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700241 private View mStupidPadding;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700242
243 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700244 private ImageView mAppIcon;
245 private ViewGroup mAppTitles;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700246 private TextView mAppTotal;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700247 private TextView mAppForeground;
248 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700249 private Button mAppSettings;
250
251 private LinearLayout mAppSwitches;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700252 private Switch mAppRestrict;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700253 private View mAppRestrictView;
254
Jeff Sharkey8a503642011-06-10 13:31:21 -0700255 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700256 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700257
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700258 private NetworkTemplate mTemplate;
259 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700260
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700261 private AppItem mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700262
263 private Intent mAppSettingsIntent;
264
Jeff Sharkeya662e492011-06-18 21:57:06 -0700265 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700266
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700267 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700268 private String mIntentTab = null;
269
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700270 private MenuItem mMenuRestrictBackground;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700271 private MenuItem mMenuShowWifi;
272 private MenuItem mMenuShowEthernet;
273 private MenuItem mMenuSimCards;
274 private MenuItem mMenuCellularNetworks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700275
Wink Savilleca756612014-11-08 10:47:12 -0800276 private List<SubscriptionInfo> mSubInfoList;
Wink Savilleb003a852014-10-23 10:16:26 -0700277 private Map<Integer,String> mMobileTagMap;
PauloftheWest50e6eca2014-10-03 11:07:14 -0700278
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700279 /** Flag used to ignore listeners during binding. */
280 private boolean mBinding;
281
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700282 private UidDetailProvider mUidDetailProvider;
283
Sanket Padawed819270f2015-01-14 11:03:33 -0800284 /**
285 * Local cache of data enabled for subId, used to work around delays.
286 */
287 private final Map<String, Boolean> mMobileDataEnabled = new HashMap<String, Boolean>();
288
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700289 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700290 public void onCreate(Bundle savedInstanceState) {
291 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700292 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700293
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700294 mNetworkService = INetworkManagementService.Stub.asInterface(
295 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700296 mStatsService = INetworkStatsService.Stub.asInterface(
297 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700298 mPolicyManager = NetworkPolicyManager.from(context);
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700299 mTelephonyManager = TelephonyManager.from(context);
Wink Saville0183fb52014-11-22 10:11:39 -0800300 mSubscriptionManager = SubscriptionManager.from(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700301
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700302 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700303
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700304 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700305 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700306
Wink Saville0183fb52014-11-22 10:11:39 -0800307 mSubInfoList = mSubscriptionManager.getActiveSubscriptionInfoList();
PauloftheWest50e6eca2014-10-03 11:07:14 -0700308 mMobileTagMap = initMobileTabTag(mSubInfoList);
309
Jaewan Kimffce9c12013-03-19 16:53:45 +0900310 try {
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700311 if (!mNetworkService.isBandwidthControlEnabled()) {
312 Log.w(TAG, "No bandwidth control; leaving");
313 getActivity().finish();
314 }
315 } catch (RemoteException e) {
316 Log.w(TAG, "No bandwidth control; leaving");
317 getActivity().finish();
318 }
319
320 try {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900321 mStatsSession = mStatsService.openSession();
322 } catch (RemoteException e) {
323 throw new RuntimeException(e);
324 }
325
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700326 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700327 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700328
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700329 // override preferences when no mobile radio
330 if (!hasReadyMobileRadio(context)) {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900331 mShowWifi = true;
332 mShowEthernet = true;
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700333 }
334
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700335 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700336 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700337
Jeff Sharkey8a503642011-06-10 13:31:21 -0700338 @Override
339 public View onCreateView(LayoutInflater inflater, ViewGroup container,
340 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700341
Jeff Sharkey8a503642011-06-10 13:31:21 -0700342 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700343 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
344
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700345 mUidDetailProvider = new UidDetailProvider(context);
346
Jeff Sharkey8a503642011-06-10 13:31:21 -0700347 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700348 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700349 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
350 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700351
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700352 // decide if we need to manually inset our content, or if we should rely
353 // on parent container for inset.
354 final boolean shouldInset = mListView.getScrollBarStyle()
355 == View.SCROLLBARS_OUTSIDE_OVERLAY;
Amith Yamasani56f51a82013-08-05 10:07:23 -0700356 mInsetSide = 0;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700357
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700358 // adjust padding around tabwidget as needed
Amith Yamasani56f51a82013-08-05 10:07:23 -0700359 prepareCustomPreferencesList(container, view, mListView, false);
Jeff Sharkey5d706792011-09-08 18:57:17 -0700360
Jeff Sharkey8a503642011-06-10 13:31:21 -0700361 mTabHost.setup();
362 mTabHost.setOnTabChangedListener(mTabListener);
363
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700364 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700365 mHeader.setClickable(true);
366
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700367 mListView.addHeaderView(new View(context), null, true);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700368 mListView.addHeaderView(mHeader, null, true);
369 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700370
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700371 if (mInsetSide > 0) {
372 // inset selector and divider drawables
373 insetListViewDrawables(mListView, mInsetSide);
Fabrice Di Megliob27223f2013-01-15 18:54:11 -0800374 mHeader.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700375 }
376
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700377 {
378 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700379 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
380 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700381 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700382
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700383 mDataEnabled = new Switch(inflater.getContext());
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700384 mDataEnabled.setClickable(false);
385 mDataEnabled.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700386 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700387 mDataEnabledView.setTag(R.id.preference_highlight_key,
388 DATA_USAGE_ENABLE_MOBILE_KEY);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700389 mDataEnabledView.setClickable(true);
390 mDataEnabledView.setFocusable(true);
391 mDataEnabledView.setOnClickListener(mDataEnabledListener);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700392 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700393
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700394 mDisableAtLimit = new Switch(inflater.getContext());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700395 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700396 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700397 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700398 mDisableAtLimitView.setTag(R.id.preference_highlight_key,
399 DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700400 mDisableAtLimitView.setClickable(true);
401 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700402 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
403 mNetworkSwitches.addView(mDisableAtLimitView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700404
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700405 mCycleView = inflater.inflate(R.layout.data_usage_cycles, mNetworkSwitches, false);
406 mCycleView.setTag(R.id.preference_highlight_key, DATA_USAGE_CYCLE_KEY);
407 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
408 mCycleAdapter = new CycleAdapter(context);
409 mCycleSpinner.setAdapter(mCycleAdapter);
410 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
411 mCycleSummary = (TextView) mCycleView.findViewById(R.id.cycle_summary);
412 mNetworkSwitches.addView(mCycleView);
PauloftheWestc80b7612014-11-25 04:40:45 -0800413 mSeries = (ChartNetworkSeriesView)view.findViewById(R.id.series);
414 mDetailedSeries = (ChartNetworkSeriesView)view.findViewById(R.id.detail_series);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700415 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700416
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700417 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700418 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800419 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700420
421 {
422 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700423 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700424 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
425 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700426 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
427 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700428 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700429
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700430 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700431
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700432 mAppRestrict = new Switch(inflater.getContext());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700433 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700434 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700435 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700436 mAppRestrictView.setClickable(true);
437 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700438 mAppRestrictView.setOnClickListener(mAppRestrictListener);
439 mAppSwitches.addView(mAppRestrictView);
440 }
441
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700442 mDisclaimer = mHeader.findViewById(R.id.disclaimer);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700443 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700444 mStupidPadding = mHeader.findViewById(R.id.stupid_padding);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700445
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +0100446 final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
447 mAdapter = new DataUsageAdapter(um, mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700448 mListView.setOnItemClickListener(mListListener);
449 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700450
451 return view;
452 }
453
454 @Override
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700455 public void onViewStateRestored(Bundle savedInstanceState) {
456 super.onViewStateRestored(savedInstanceState);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700457
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700458 // pick default tab based on incoming intent
459 final Intent intent = getActivity().getIntent();
460 mIntentTab = computeTabFromIntent(intent);
461
Jeff Sharkey8a503642011-06-10 13:31:21 -0700462 // this kicks off chain reaction which creates tabs, binds the body to
463 // selected network, and binds chart, cycles and detail list.
464 updateTabs();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700465 }
466
467 @Override
468 public void onResume() {
469 super.onResume();
470
471 getView().post(new Runnable() {
472 @Override
473 public void run() {
474 highlightViewIfNeeded();
475 }
476 });
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700477
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700478 // kick off background task to update stats
479 new AsyncTask<Void, Void, Void>() {
480 @Override
481 protected Void doInBackground(Void... params) {
482 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700483 // wait a few seconds before kicking off
484 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700485 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700486 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700487 } catch (RemoteException e) {
488 }
489 return null;
490 }
491
492 @Override
493 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700494 if (isAdded()) {
495 updateBody();
496 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700497 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700498 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700499 }
500
Jeff Sharkey8a503642011-06-10 13:31:21 -0700501 @Override
502 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
503 inflater.inflate(R.menu.data_usage, menu);
504 }
505
506 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700507 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700508 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700509 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700510 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700511
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700512 mMenuShowWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700513 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
514 mMenuShowWifi.setVisible(!appDetailMode);
515 } else {
516 mMenuShowWifi.setVisible(false);
517 }
518
519 mMenuShowEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
520 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
521 mMenuShowEthernet.setVisible(!appDetailMode);
522 } else {
523 mMenuShowEthernet.setVisible(false);
524 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700525
526 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700527 mMenuRestrictBackground.setVisible(
528 hasReadyMobileRadio(context) && isOwner && !appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700529
530 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
531 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700532 metered.setVisible(!appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700533 } else {
534 metered.setVisible(false);
535 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700536
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700537 // TODO: show when multiple sims available
538 mMenuSimCards = menu.findItem(R.id.data_usage_menu_sim_cards);
539 mMenuSimCards.setVisible(false);
540
541 mMenuCellularNetworks = menu.findItem(R.id.data_usage_menu_cellular_networks);
PauloftheWesta4b8fb32014-09-18 10:12:25 -0700542 mMenuCellularNetworks.setVisible(hasReadyMobileRadio(context)
543 && !appDetailMode && isOwner);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700544
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700545 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
546 String helpUrl;
547 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800548 HelpUtils.prepareHelpMenuItem(context, help, helpUrl);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700549 } else {
550 help.setVisible(false);
551 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700552
553 updateMenuTitles();
554 }
555
556 private void updateMenuTitles() {
557 if (mPolicyManager.getRestrictBackground()) {
558 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_allow_background);
559 } else {
560 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_restrict_background);
561 }
562
563 if (mShowWifi) {
564 mMenuShowWifi.setTitle(R.string.data_usage_menu_hide_wifi);
565 } else {
566 mMenuShowWifi.setTitle(R.string.data_usage_menu_show_wifi);
567 }
568
569 if (mShowEthernet) {
570 mMenuShowEthernet.setTitle(R.string.data_usage_menu_hide_ethernet);
571 } else {
572 mMenuShowEthernet.setTitle(R.string.data_usage_menu_show_ethernet);
573 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700574 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700575
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700576 @Override
577 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700578 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700579 case R.id.data_usage_menu_restrict_background: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700580 final boolean restrictBackground = !mPolicyManager.getRestrictBackground();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700581 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800582 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700583 } else {
584 // no confirmation to drop restriction
585 setRestrictBackground(false);
586 }
587 return true;
588 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700589 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700590 mShowWifi = !mShowWifi;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700591 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700592 updateMenuTitles();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700593 updateTabs();
594 return true;
595 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700596 case R.id.data_usage_menu_show_ethernet: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700597 mShowEthernet = !mShowEthernet;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700598 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700599 updateMenuTitles();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700600 updateTabs();
601 return true;
602 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700603 case R.id.data_usage_menu_sim_cards: {
604 // TODO: hook up to sim cards
605 return true;
606 }
607 case R.id.data_usage_menu_cellular_networks: {
608 final Intent intent = new Intent(Intent.ACTION_MAIN);
609 intent.setComponent(new ComponentName("com.android.phone",
610 "com.android.phone.MobileNetworkSettings"));
611 startActivity(intent);
612 return true;
613 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700614 case R.id.data_usage_menu_metered: {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800615 final SettingsActivity sa = (SettingsActivity) getActivity();
616 sa.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700617 R.string.data_usage_metered_title, null, this, 0);
618 return true;
619 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700620 }
621 return false;
622 }
623
Jeff Sharkey94a90952011-06-13 22:31:09 -0700624 @Override
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700625 public void onDestroy() {
Jeff Sharkey94a90952011-06-13 22:31:09 -0700626 mDataEnabledView = null;
627 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700628
629 mUidDetailProvider.clearCache();
630 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700631
632 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700633
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800634 super.onDestroy();
635 }
636
Jeff Sharkey8a503642011-06-10 13:31:21 -0700637 /**
Jeff Sharkey92811822012-05-04 11:47:29 -0700638 * Build and assign {@link LayoutTransition} to various containers. Should
639 * only be assigned after initial layout is complete.
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700640 */
Jeff Sharkey92811822012-05-04 11:47:29 -0700641 private void ensureLayoutTransitions() {
642 // skip when already setup
643 if (mChart.getLayoutTransition() != null) return;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700644
Jeff Sharkey92811822012-05-04 11:47:29 -0700645 mTabsContainer.setLayoutTransition(buildLayoutTransition());
646 mHeader.setLayoutTransition(buildLayoutTransition());
647 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700648
Jeff Sharkey92811822012-05-04 11:47:29 -0700649 final LayoutTransition chartTransition = buildLayoutTransition();
650 chartTransition.disableTransitionType(LayoutTransition.APPEARING);
651 chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
652 mChart.setLayoutTransition(chartTransition);
653 }
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700654
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700655 private static LayoutTransition buildLayoutTransition() {
656 final LayoutTransition transition = new LayoutTransition();
657 if (TEST_ANIM) {
658 transition.setDuration(1500);
659 }
660 transition.setAnimateParentHierarchy(false);
661 return transition;
662 }
663
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700664 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700665 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700666 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
667 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700668 */
669 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700670 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700671 mTabHost.clearAllTabs();
672
PauloftheWestc80b7612014-11-25 04:40:45 -0800673 for (int i = 0; i < mTelephonyManager.getSimCount(); i++) {
674 final SubscriptionInfo sir = Utils.findRecordBySlotId(context, i);
675 if (sir != null) {
676 addMobileTab(context, sir);
677 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700678 }
PauloftheWestc80b7612014-11-25 04:40:45 -0800679
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700680 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700681 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
682 }
PauloftheWestc80b7612014-11-25 04:40:45 -0800683
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700684 if (mShowEthernet && hasEthernet(context)) {
685 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
686 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700687
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700688 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700689 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
690 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
691 if (mIntentTab != null) {
692 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700693 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700694 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700695 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700696 mTabHost.setCurrentTabByTag(mIntentTab);
697 }
698 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700699 } else if (noTabs) {
700 // no usable tabs, so hide body
701 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700702 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700703 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700704 }
705 }
706
Jeff Sharkey8a503642011-06-10 13:31:21 -0700707 /**
708 * Factory that provide empty {@link View} to make {@link TabHost} happy.
709 */
710 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700711 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700712 public View createTabContent(String tag) {
713 return new View(mTabHost.getContext());
714 }
715 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700716
Jeff Sharkey8a503642011-06-10 13:31:21 -0700717 /**
718 * Build {@link TabSpec} with thin indicator, and empty content.
719 */
720 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700721 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
722 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700723 }
724
PauloftheWest50e6eca2014-10-03 11:07:14 -0700725 /**
726 * Build {@link TabSpec} with thin indicator, and empty content.
727 */
Stuart Scottb1531812014-11-04 14:52:23 -0800728 private TabSpec buildTabSpec(String tag, CharSequence title) {
PauloftheWest50e6eca2014-10-03 11:07:14 -0700729 return mTabHost.newTabSpec(tag).setIndicator(title).setContent(
730 mEmptyTabContent);
731 }
732
733
Jeff Sharkey8a503642011-06-10 13:31:21 -0700734 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700735 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700736 public void onTabChanged(String tabId) {
737 // user changed tab; update body
738 updateBody();
739 }
740 };
741
742 /**
743 * Update body content based on current tab. Loads
744 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
745 * binds them to visible controls.
746 */
747 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700748 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700749 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700750
Jeff Sharkeya662e492011-06-18 21:57:06 -0700751 final Context context = getActivity();
PauloftheWestc80b7612014-11-25 04:40:45 -0800752 final Resources resources = context.getResources();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700753 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700754 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700755
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700756 if (currentTab == null) {
757 Log.w(TAG, "no tab selected; hiding body");
758 mListView.setVisibility(View.GONE);
759 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700760 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700761 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700762 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700763
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700764 mCurrentTab = currentTab;
765
Jeff Sharkey8a503642011-06-10 13:31:21 -0700766 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
767
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700768 mDataEnabledSupported = isOwner;
769 mDisableAtLimitSupported = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700770
Wink Savillefcec91f2014-12-02 17:12:08 -0800771 // TODO: remove mobile tabs when SIM isn't ready probably by
772 // TODO: using SubscriptionManager.getActiveSubscriptionInfoList.
773 if (LOGD) Log.d(TAG, "updateBody() isMobileTab=" + isMobileTab(currentTab));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700774
PauloftheWest50e6eca2014-10-03 11:07:14 -0700775 if (isMobileTab(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800776 if (LOGD) Log.d(TAG, "updateBody() mobile tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700777 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
778 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
PauloftheWest50e6eca2014-10-03 11:07:14 -0700779 mDataEnabledSupported = isMobileDataAvailable(getSubId(currentTab));
Jeff Sharkey4e2d16c2014-12-03 13:44:26 -0800780
781 // Match mobile traffic for this subscriber, but normalize it to
782 // catch any other merged subscribers.
783 mTemplate = buildTemplateMobileAll(
784 getActiveSubscriberId(context, getSubId(currentTab)));
785 mTemplate = NetworkTemplate.normalize(mTemplate,
786 mTelephonyManager.getMergedSubscriberIds());
787
Jeff Sharkey8a503642011-06-10 13:31:21 -0700788 } else if (TAB_3G.equals(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800789 if (LOGD) Log.d(TAG, "updateBody() 3g tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700790 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
791 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
792 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700793 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700794
795 } else if (TAB_4G.equals(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800796 if (LOGD) Log.d(TAG, "updateBody() 4g tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700797 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
798 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
799 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700800 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700801
802 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700803 // wifi doesn't have any controls
Wink Savillefcec91f2014-12-02 17:12:08 -0800804 if (LOGD) Log.d(TAG, "updateBody() wifi tab");
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700805 mDataEnabledSupported = false;
806 mDisableAtLimitSupported = false;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700807 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700808
809 } else if (TAB_ETHERNET.equals(currentTab)) {
810 // ethernet doesn't have any controls
Wink Savillefcec91f2014-12-02 17:12:08 -0800811 if (LOGD) Log.d(TAG, "updateBody() ethernet tab");
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700812 mDataEnabledSupported = false;
813 mDisableAtLimitSupported = false;
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700814 mTemplate = buildTemplateEthernet();
815
816 } else {
Wink Savillefcec91f2014-12-02 17:12:08 -0800817 if (LOGD) Log.d(TAG, "updateBody() unknown tab");
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700818 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700819 }
820
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700821 // kick off loader for network history
822 // TODO: consider chaining two loaders together instead of reloading
823 // network history when showing app detail.
824 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700825 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700826
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700827 // detail mode can change visible menus, invalidate
828 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700829
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700830 mBinding = false;
PauloftheWestc80b7612014-11-25 04:40:45 -0800831
832 int seriesColor = resources.getColor(R.color.sim_noitification);
833 if (mCurrentTab != null && mCurrentTab.length() > TAB_MOBILE.length() ){
834 final int slotId = Integer.parseInt(mCurrentTab.substring(TAB_MOBILE.length(),
835 mCurrentTab.length()));
836 final SubscriptionInfo sir = com.android.settings.Utils.findRecordBySlotId(context,
837 slotId);
838
839 if (sir != null) {
840 seriesColor = sir.getIconTint();
841 }
842 }
843
Jeff Sharkey9c167e92015-01-26 15:15:59 -0700844 final int secondaryColor = Color.argb(127, Color.red(seriesColor), Color.green(seriesColor),
845 Color.blue(seriesColor));
846 mSeries.setChartColor(Color.BLACK, seriesColor, secondaryColor);
847 mDetailedSeries.setChartColor(Color.BLACK, seriesColor, secondaryColor);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700848 }
849
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700850 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700851 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700852 }
853
854 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700855 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700856 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700857 */
858 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700859 final Context context = getActivity();
860 final PackageManager pm = context.getPackageManager();
861 final LayoutInflater inflater = getActivity().getLayoutInflater();
862
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700863 if (isAppDetailMode()) {
864 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700865 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700866 } else {
867 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700868 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700869
870 // hide detail stats when not in detail mode
871 mChart.bindDetailNetworkStats(null);
872 return;
873 }
874
875 // remove warning/limit sweeps while in detail mode
876 mChart.bindNetworkPolicy(null);
877
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700878 // show icon and all labels appearing under this app
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700879 final int uid = mCurrentApp.key;
880 final UidDetail detail = mUidDetailProvider.getUidDetail(uid, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700881 mAppIcon.setImageDrawable(detail.icon);
882
883 mAppTitles.removeAllViews();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700884
885 View title = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700886 if (detail.detailLabels != null) {
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100887 final int n = detail.detailLabels.length;
888 for (int i = 0; i < n; ++i) {
889 CharSequence label = detail.detailLabels[i];
890 CharSequence contentDescription = detail.detailContentDescriptions[i];
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700891 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100892 TextView appTitle = (TextView) title.findViewById(R.id.app_title);
893 appTitle.setText(label);
894 appTitle.setContentDescription(contentDescription);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700895 mAppTitles.addView(title);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700896 }
897 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700898 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100899 TextView appTitle = (TextView) title.findViewById(R.id.app_title);
900 appTitle.setText(detail.label);
901 appTitle.setContentDescription(detail.contentDescription);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700902 mAppTitles.addView(title);
903 }
904
905 // Remember last slot for summary
906 if (title != null) {
907 mAppTotal = (TextView) title.findViewById(R.id.app_summary);
908 } else {
909 mAppTotal = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700910 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700911
912 // enable settings button when package provides it
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700913 final String[] packageNames = pm.getPackagesForUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700914 if (packageNames != null && packageNames.length > 0) {
915 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700916 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
917
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700918 // Search for match across all packages
919 boolean matchFound = false;
920 for (String packageName : packageNames) {
921 mAppSettingsIntent.setPackage(packageName);
922 if (pm.resolveActivity(mAppSettingsIntent, 0) != null) {
923 matchFound = true;
924 break;
925 }
926 }
927
Zoltan Szatmary-Banf20d39e2014-08-07 15:27:08 +0100928 mAppSettings.setOnClickListener(new OnClickListener() {
929 @Override
930 public void onClick(View v) {
931 if (!isAdded()) {
932 return;
933 }
934
935 // TODO: target towards entire UID instead of just first package
936 getActivity().startActivityAsUser(mAppSettingsIntent,
937 new UserHandle(UserHandle.getUserId(uid)));
938 }
939 });
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700940 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -0700941 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700942
943 } else {
944 mAppSettingsIntent = null;
Zoltan Szatmary-Banf20d39e2014-08-07 15:27:08 +0100945 mAppSettings.setOnClickListener(null);
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700946 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700947 }
948
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700949 updateDetailData();
950
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700951 if (UserHandle.isApp(uid) && !mPolicyManager.getRestrictBackground()
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700952 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700953 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800954 setPreferenceSummary(mAppRestrictView,
955 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700956
957 mAppRestrictView.setVisibility(View.VISIBLE);
958 mAppRestrict.setChecked(getAppRestrictBackground());
959
960 } else {
961 mAppRestrictView.setVisibility(View.GONE);
962 }
963 }
964
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700965 private void setPolicyWarningBytes(long warningBytes) {
966 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700967 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700968 updatePolicy(false);
969 }
970
971 private void setPolicyLimitBytes(long limitBytes) {
972 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700973 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700974 updatePolicy(false);
975 }
976
Wink Savilleb003a852014-10-23 10:16:26 -0700977 private boolean isMobileDataEnabled(int subId) {
Wink Savillea4f4d182014-12-05 15:34:46 -0800978 if (LOGD) Log.d(TAG, "isMobileDataEnabled:+ subId=" + subId);
PauloftheWest50e6eca2014-10-03 11:07:14 -0700979 boolean isEnable = false;
Sanket Padawed819270f2015-01-14 11:03:33 -0800980 if (mMobileDataEnabled.get(String.valueOf(subId)) != null) {
981 //TODO: deprecate and remove this once enabled flag is on policy
982 //Multiple Subscriptions, the value need to be reseted
983 isEnable = mMobileDataEnabled.get(String.valueOf(subId)).booleanValue();
Wink Savillea4f4d182014-12-05 15:34:46 -0800984 if (LOGD) {
985 Log.d(TAG, "isMobileDataEnabled: != null, subId=" + subId
986 + " isEnable=" + isEnable);
987 }
Sanket Padawed819270f2015-01-14 11:03:33 -0800988 mMobileDataEnabled.put(String.valueOf(subId), null);
Jeff Sharkey28130d92011-09-02 16:10:24 -0700989 } else {
Wink Savillea4f4d182014-12-05 15:34:46 -0800990 // SUB SELECT
991 isEnable = mTelephonyManager.getDataEnabled(subId);
992 if (LOGD) {
993 Log.d(TAG, "isMobileDataEnabled: == null, subId=" + subId
994 + " isEnable=" + isEnable);
995 }
Jeff Sharkey28130d92011-09-02 16:10:24 -0700996 }
PauloftheWest50e6eca2014-10-03 11:07:14 -0700997 return isEnable;
Jeff Sharkey28130d92011-09-02 16:10:24 -0700998 }
999
Sanket Padawe24f834d2015-01-08 11:23:11 -08001000 private void setMobileDataEnabled(int subId, boolean enabled) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001001 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
Sanket Padawe24f834d2015-01-08 11:23:11 -08001002 mTelephonyManager.setDataEnabled(subId, enabled);
Sanket Padawed819270f2015-01-14 11:03:33 -08001003 mMobileDataEnabled.put(String.valueOf(subId), enabled);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001004 updatePolicy(false);
1005 }
1006
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001007 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001008 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked()
1009 && ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001010 }
1011
1012 private boolean isBandwidthControlEnabled() {
1013 try {
1014 return mNetworkService.isBandwidthControlEnabled();
1015 } catch (RemoteException e) {
1016 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
1017 return false;
1018 }
1019 }
1020
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001021 public void setRestrictBackground(boolean restrictBackground) {
1022 mPolicyManager.setRestrictBackground(restrictBackground);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001023 updateMenuTitles();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001024 }
1025
1026 private boolean getAppRestrictBackground() {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001027 final int uid = mCurrentApp.key;
1028 final int uidPolicy = mPolicyManager.getUidPolicy(uid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001029 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
1030 }
1031
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001032 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001033 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001034 final int uid = mCurrentApp.key;
1035 mPolicyManager.setUidPolicy(
1036 uid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001037 mAppRestrict.setChecked(restrictBackground);
1038 }
1039
Jeff Sharkey8a503642011-06-10 13:31:21 -07001040 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001041 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
1042 * current {@link #mTemplate}.
1043 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001044 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001045 boolean dataEnabledVisible = mDataEnabledSupported;
1046 boolean disableAtLimitVisible = mDisableAtLimitSupported;
1047
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001048 if (isAppDetailMode()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001049 dataEnabledVisible = false;
1050 disableAtLimitVisible = false;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001051 }
1052
Jeff Sharkey28130d92011-09-02 16:10:24 -07001053 // TODO: move enabled state directly into policy
PauloftheWest50e6eca2014-10-03 11:07:14 -07001054 if (isMobileTab(mCurrentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001055 mBinding = true;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001056 mDataEnabled.setChecked(isMobileDataEnabled(getSubId(mCurrentTab)));
Jeff Sharkey28130d92011-09-02 16:10:24 -07001057 mBinding = false;
1058 }
1059
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001060 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001061 //SUB SELECT
1062 if (isNetworkPolicyModifiable(policy) && isMobileDataAvailable(getSubId(mCurrentTab))) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001063 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001064 if (!isAppDetailMode()) {
1065 mChart.bindNetworkPolicy(policy);
1066 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001067
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001068 } else {
1069 // controls are disabled; don't bind warning/limit sweeps
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001070 disableAtLimitVisible = false;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001071 mChart.bindNetworkPolicy(null);
1072 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001073
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001074 mDataEnabledView.setVisibility(dataEnabledVisible ? View.VISIBLE : View.GONE);
1075 mDisableAtLimitView.setVisibility(disableAtLimitVisible ? View.VISIBLE : View.GONE);
1076
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001077 if (refreshCycle) {
1078 // generate cycle list based on policy and available history
1079 updateCycleList(policy);
1080 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001081 }
1082
1083 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001084 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
1085 * and available {@link NetworkStatsHistory} data. Always selects the newest
1086 * item, updating the inspection range on {@link #mChart}.
1087 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001088 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001089 // stash away currently selected cycle to try restoring below
1090 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -07001091 mCycleAdapter.clear();
1092
1093 final Context context = mCycleSpinner.getContext();
1094
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001095 long historyStart = Long.MAX_VALUE;
1096 long historyEnd = Long.MIN_VALUE;
1097 if (mChartData != null) {
1098 historyStart = mChartData.network.getStart();
1099 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001100 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001101
Jeff Sharkey461842a2011-09-25 18:22:48 -07001102 final long now = System.currentTimeMillis();
1103 if (historyStart == Long.MAX_VALUE) historyStart = now;
1104 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001105
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001106 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001107 if (policy != null) {
1108 // find the next cycle boundary
1109 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001110
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001111 // walk backwards, generating all valid cycle ranges
1112 while (cycleEnd > historyStart) {
1113 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
1114 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
1115 + historyStart);
1116 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1117 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001118 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001119 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001120
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001121 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001122 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001123 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001124
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001125 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001126 // no policy defined cycles; show entry for each four-week period
1127 long cycleEnd = historyEnd;
1128 while (cycleEnd > historyStart) {
1129 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
1130 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1131 cycleEnd = cycleStart;
1132 }
1133
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001134 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001135 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001136
1137 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001138 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001139 final int position = mCycleAdapter.findNearestPosition(previousItem);
1140 mCycleSpinner.setSelection(position);
1141
1142 // only force-update cycle when changed; skipping preserves any
1143 // user-defined inspection region.
1144 final CycleItem selectedItem = mCycleAdapter.getItem(position);
1145 if (!Objects.equal(selectedItem, previousItem)) {
1146 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1147 } else {
1148 // but still kick off loader for detailed list
1149 updateDetailData();
1150 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001151 } else {
1152 updateDetailData();
1153 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001154 }
1155
Sanket Padawed819270f2015-01-14 11:03:33 -08001156 private void disableDataForOtherSubscriptions(SubscriptionInfo currentSir) {
1157 if (mSubInfoList != null) {
1158 for (SubscriptionInfo subInfo : mSubInfoList) {
1159 if (subInfo.getSubscriptionId() != currentSir.getSubscriptionId()) {
1160 setMobileDataEnabled(subInfo.getSubscriptionId(), false);
1161 }
1162 }
1163 }
1164 }
1165
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001166 private View.OnClickListener mDataEnabledListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001167 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001168 public void onClick(View v) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001169 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001170
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001171 final boolean dataEnabled = !mDataEnabled.isChecked();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001172 final String currentTab = mCurrentTab;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001173 if (isMobileTab(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001174 if (dataEnabled) {
PauloftheWestde338442014-10-29 14:46:44 -07001175 // If we are showing the Sim Card tile then we are a Multi-Sim device.
1176 if (Utils.showSimCardTile(getActivity())) {
1177 handleMultiSimDataDialog();
1178 } else {
Sanket Padawe24f834d2015-01-08 11:23:11 -08001179 setMobileDataEnabled(getSubId(currentTab), true);
PauloftheWestde338442014-10-29 14:46:44 -07001180 }
Jeff Sharkey28130d92011-09-02 16:10:24 -07001181 } else {
1182 // disabling data; show confirmation dialog which eventually
1183 // calls setMobileDataEnabled() once user confirms.
Sanket Padawe24f834d2015-01-08 11:23:11 -08001184 ConfirmDataDisableFragment.show(DataUsageSummary.this, getSubId(mCurrentTab));
Jeff Sharkey28130d92011-09-02 16:10:24 -07001185 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001186 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001187
Jeff Sharkey28130d92011-09-02 16:10:24 -07001188 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001189 }
1190 };
1191
PauloftheWestde338442014-10-29 14:46:44 -07001192 private void handleMultiSimDataDialog() {
PauloftheWestf0d84e22014-12-10 08:50:54 -08001193 final Context context = getActivity();
1194 final SubscriptionInfo currentSir = getCurrentTabSubInfo(context);
Sanket Padawe766ab602015-01-20 10:36:15 -08001195
1196 //If sim has not loaded after toggling data switch, return.
1197 if (currentSir == null) {
1198 return;
1199 }
1200
Wink Saville0183fb52014-11-22 10:11:39 -08001201 final SubscriptionInfo nextSir = mSubscriptionManager.getActiveSubscriptionInfo(
1202 mSubscriptionManager.getDefaultDataSubId());
PauloftheWestde338442014-10-29 14:46:44 -07001203
PauloftheWestf0d84e22014-12-10 08:50:54 -08001204 // If the device is single SIM or is enabling data on the active data SIM then forgo
1205 // the pop-up.
1206 if (!Utils.showSimCardTile(context) ||
Sanket Padawed819270f2015-01-14 11:03:33 -08001207 (nextSir != null && currentSir != null &&
1208 currentSir.getSubscriptionId() == nextSir.getSubscriptionId())) {
Sanket Padawe24f834d2015-01-08 11:23:11 -08001209 setMobileDataEnabled(currentSir.getSubscriptionId(), true);
Sanket Padawed819270f2015-01-14 11:03:33 -08001210 if (nextSir != null && currentSir != null &&
1211 currentSir.getSubscriptionId() == nextSir.getSubscriptionId()) {
1212 disableDataForOtherSubscriptions(currentSir);
1213 }
PauloftheWestde338442014-10-29 14:46:44 -07001214 updateBody();
1215 return;
1216 }
1217
PauloftheWestf0d84e22014-12-10 08:50:54 -08001218 final String previousName = (nextSir == null)
1219 ? context.getResources().getString(R.string.sim_selection_required_pref)
1220 : nextSir.getDisplayName().toString();
1221
PauloftheWestde338442014-10-29 14:46:44 -07001222 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
1223
1224 builder.setTitle(R.string.sim_change_data_title);
1225 builder.setMessage(getActivity().getResources().getString(R.string.sim_change_data_message,
PauloftheWestf0d84e22014-12-10 08:50:54 -08001226 currentSir.getDisplayName(), previousName));
PauloftheWestde338442014-10-29 14:46:44 -07001227
1228 builder.setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
1229 @Override
1230 public void onClick(DialogInterface dialog, int id) {
Wink Saville0183fb52014-11-22 10:11:39 -08001231 mSubscriptionManager.setDefaultDataSubId(currentSir.getSubscriptionId());
Sanket Padawe24f834d2015-01-08 11:23:11 -08001232 setMobileDataEnabled(currentSir.getSubscriptionId(), true);
Sanket Padawed819270f2015-01-14 11:03:33 -08001233 disableDataForOtherSubscriptions(currentSir);
PauloftheWestde338442014-10-29 14:46:44 -07001234 updateBody();
1235 }
1236 });
1237 builder.setNegativeButton(R.string.cancel, null);
1238
1239 builder.create().show();
1240 }
1241
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001242 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001243 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001244 public void onClick(View v) {
1245 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001246 if (disableAtLimit) {
1247 // enabling limit; show confirmation dialog which eventually
1248 // calls setPolicyLimitBytes() once user confirms.
1249 ConfirmLimitFragment.show(DataUsageSummary.this);
1250 } else {
1251 setPolicyLimitBytes(LIMIT_DISABLED);
1252 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001253 }
1254 };
1255
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001256 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001257 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001258 public void onClick(View v) {
1259 final boolean restrictBackground = !mAppRestrict.isChecked();
1260
1261 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001262 // enabling restriction; show confirmation dialog which
1263 // eventually calls setRestrictBackground() once user
1264 // confirms.
1265 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001266 } else {
1267 setAppRestrictBackground(false);
1268 }
1269 }
1270 };
1271
Jeff Sharkey8a503642011-06-10 13:31:21 -07001272 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001273 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001274 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001275 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001276 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001277
1278 // TODO: sigh, remove this hack once we understand 6450986
1279 if (mUidDetailProvider == null || app == null) return;
1280
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001281 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001282 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001283 }
1284 };
1285
1286 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001287 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001288 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1289 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1290 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001291 // show cycle editor; will eventually call setPolicyCycleDay()
1292 // when user finishes editing.
1293 CycleEditorFragment.show(DataUsageSummary.this);
1294
1295 // reset spinner to something other than "change cycle..."
1296 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001297
1298 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001299 if (LOGD) {
1300 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1301 + cycle.end + "]");
1302 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001303
1304 // update chart to show selected cycle, and update detail data
1305 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001306 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001307
1308 updateDetailData();
1309 }
1310 }
1311
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001312 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001313 public void onNothingSelected(AdapterView<?> parent) {
1314 // ignored
1315 }
1316 };
1317
1318 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001319 * Update details based on {@link #mChart} inspection range depending on
1320 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1321 * of applications data usage, and when {@link #isAppDetailMode()} update
1322 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001323 */
1324 private void updateDetailData() {
1325 if (LOGD) Log.d(TAG, "updateDetailData()");
1326
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001327 final long start = mChart.getInspectStart();
1328 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001329 final long now = System.currentTimeMillis();
1330
1331 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001332
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001333 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001334 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001335 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001336 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001337 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001338 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001339 final long foregroundBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001340 final long totalBytes = defaultBytes + foregroundBytes;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001341
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001342 if (mAppTotal != null) {
1343 mAppTotal.setText(Formatter.formatFileSize(context, totalBytes));
1344 }
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001345 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1346 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1347
1348 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001349 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001350
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001351 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001352
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001353 mCycleSummary.setVisibility(View.GONE);
1354
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001355 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001356 if (mChartData != null) {
1357 entry = mChartData.network.getValues(start, end, now, null);
1358 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001359
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001360 mCycleSummary.setVisibility(View.VISIBLE);
1361
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001362 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001363 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001364 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001365 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001366
1367 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1368 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001369 mCycleSummary.setText(totalPhrase);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001370
PauloftheWest50e6eca2014-10-03 11:07:14 -07001371 if (isMobileTab(mCurrentTab) || TAB_3G.equals(mCurrentTab)
László Dávid0b8bf4e2012-10-24 23:31:47 +02001372 || TAB_4G.equals(mCurrentTab)) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001373 if (isAppDetailMode()) {
1374 mDisclaimer.setVisibility(View.GONE);
1375 } else {
1376 mDisclaimer.setVisibility(View.VISIBLE);
1377 }
Jeff Sharkeye557c332012-04-13 16:04:07 -07001378 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001379 mDisclaimer.setVisibility(View.GONE);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001380 }
1381
Jeff Sharkey92811822012-05-04 11:47:29 -07001382 // initial layout is finished above, ensure we have transitions
1383 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001384 }
1385
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001386 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1387 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001388 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001389 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001390 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001391 }
1392
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001393 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001394 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1395 mChartData = data;
1396 mChart.bindNetworkStats(mChartData.network);
1397 mChart.bindDetailNetworkStats(mChartData.detail);
1398
1399 // calcuate policy cycles based on available data
1400 updatePolicy(true);
1401 updateAppDetail();
1402
1403 // force scroll to top of body when showing detail
1404 if (mChartData.detail != null) {
1405 mListView.smoothScrollToPosition(0);
1406 }
1407 }
1408
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001409 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001410 public void onLoaderReset(Loader<ChartData> loader) {
1411 mChartData = null;
1412 mChart.bindNetworkStats(null);
1413 mChart.bindDetailNetworkStats(null);
1414 }
1415 };
1416
1417 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001418 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001419 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001420 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001421 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001422 }
1423
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001424 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001425 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001426 final int[] restrictedUids = mPolicyManager.getUidsWithPolicy(
Jeff Sharkeye557c332012-04-13 16:04:07 -07001427 POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001428 mAdapter.bindStats(data, restrictedUids);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001429 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001430 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001431
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001432 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001433 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001434 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001435 updateEmptyVisible();
1436 }
1437
1438 private void updateEmptyVisible() {
1439 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1440 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001441 mStupidPadding.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001442 }
1443 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001444
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001445 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001446 final TelephonyManager tele = TelephonyManager.from(context);
1447 final String actualSubscriberId = tele.getSubscriberId();
Wink Savillefcec91f2014-12-02 17:12:08 -08001448 String retVal = SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
1449 if (LOGD) Log.d(TAG, "getActiveSubscriberId=" + retVal + " actualSubscriberId=" + actualSubscriberId);
1450 return retVal;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001451 }
1452
Wink Savilleb003a852014-10-23 10:16:26 -07001453 private static String getActiveSubscriberId(Context context, int subId) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07001454 final TelephonyManager tele = TelephonyManager.from(context);
Wink Savillefcec91f2014-12-02 17:12:08 -08001455 String retVal = tele.getSubscriberId(subId);
1456 if (LOGD) Log.d(TAG, "getActiveSubscriberId=" + retVal + " subId=" + subId);
1457 return retVal;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001458 }
1459
Jeff Sharkey8a503642011-06-10 13:31:21 -07001460 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001461 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001462 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001463 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001464 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001465
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001466 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001467 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001468 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001469 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001470
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001471 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001472 public void requestWarningEdit() {
1473 WarningEditorFragment.show(DataUsageSummary.this);
1474 }
1475
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001476 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001477 public void requestLimitEdit() {
1478 LimitEditorFragment.show(DataUsageSummary.this);
1479 }
1480 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001481
1482 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001483 * List item that reflects a specific data usage cycle.
1484 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001485 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001486 public CharSequence label;
1487 public long start;
1488 public long end;
1489
Jeff Sharkey8a503642011-06-10 13:31:21 -07001490 CycleItem(CharSequence label) {
1491 this.label = label;
1492 }
1493
1494 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001495 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001496 this.start = start;
1497 this.end = end;
1498 }
1499
Jeff Sharkey8a503642011-06-10 13:31:21 -07001500 @Override
1501 public String toString() {
1502 return label.toString();
1503 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001504
1505 @Override
1506 public boolean equals(Object o) {
1507 if (o instanceof CycleItem) {
1508 final CycleItem another = (CycleItem) o;
1509 return start == another.start && end == another.end;
1510 }
1511 return false;
1512 }
1513
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001514 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001515 public int compareTo(CycleItem another) {
1516 return Long.compare(start, another.start);
1517 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001518 }
1519
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001520 private static final StringBuilder sBuilder = new StringBuilder(50);
1521 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1522 sBuilder, Locale.getDefault());
1523
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001524 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001525 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001526
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001527 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001528 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001529 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1530 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001531 }
1532 }
1533
Jeff Sharkey8a503642011-06-10 13:31:21 -07001534 /**
1535 * Special-case data usage cycle that triggers dialog to change
1536 * {@link NetworkPolicy#cycleDay}.
1537 */
1538 public static class CycleChangeItem extends CycleItem {
1539 public CycleChangeItem(Context context) {
1540 super(context.getString(R.string.data_usage_change_cycle));
1541 }
1542 }
1543
1544 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001545 private boolean mChangePossible = false;
1546 private boolean mChangeVisible = false;
1547
1548 private final CycleChangeItem mChangeItem;
1549
Jeff Sharkey8a503642011-06-10 13:31:21 -07001550 public CycleAdapter(Context context) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001551 super(context, R.layout.data_usage_cycle_item);
1552 setDropDownViewResource(R.layout.data_usage_cycle_item_dropdown);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001553 mChangeItem = new CycleChangeItem(context);
1554 }
1555
1556 public void setChangePossible(boolean possible) {
1557 mChangePossible = possible;
1558 updateChange();
1559 }
1560
1561 public void setChangeVisible(boolean visible) {
1562 mChangeVisible = visible;
1563 updateChange();
1564 }
1565
1566 private void updateChange() {
1567 remove(mChangeItem);
1568 if (mChangePossible && mChangeVisible) {
1569 add(mChangeItem);
1570 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001571 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001572
1573 /**
1574 * Find position of {@link CycleItem} in this adapter which is nearest
1575 * the given {@link CycleItem}.
1576 */
1577 public int findNearestPosition(CycleItem target) {
1578 if (target != null) {
1579 final int count = getCount();
1580 for (int i = count - 1; i >= 0; i--) {
1581 final CycleItem item = getItem(i);
1582 if (item instanceof CycleChangeItem) {
1583 continue;
1584 } else if (item.compareTo(target) >= 0) {
1585 return i;
1586 }
1587 }
1588 }
1589 return 0;
1590 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001591 }
1592
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001593 public static class AppItem implements Comparable<AppItem>, Parcelable {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001594 public static final int CATEGORY_USER = 0;
1595 public static final int CATEGORY_APP_TITLE = 1;
1596 public static final int CATEGORY_APP = 2;
1597
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001598 public final int key;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001599 public boolean restricted;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001600 public int category;
1601
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001602 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001603 public long total;
1604
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001605 public AppItem() {
1606 this.key = 0;
1607 }
1608
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001609 public AppItem(int key) {
1610 this.key = key;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001611 }
1612
1613 public AppItem(Parcel parcel) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001614 key = parcel.readInt();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001615 uids = parcel.readSparseBooleanArray();
1616 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001617 }
1618
1619 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001620 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001621 }
1622
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001623 @Override
1624 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001625 dest.writeInt(key);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001626 dest.writeSparseBooleanArray(uids);
1627 dest.writeLong(total);
1628 }
1629
1630 @Override
1631 public int describeContents() {
1632 return 0;
1633 }
1634
1635 @Override
1636 public int compareTo(AppItem another) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001637 int comparison = Integer.compare(category, another.category);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001638 if (comparison == 0) {
1639 comparison = Long.compare(another.total, total);
1640 }
1641 return comparison;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001642 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001643
1644 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1645 @Override
1646 public AppItem createFromParcel(Parcel in) {
1647 return new AppItem(in);
1648 }
1649
1650 @Override
1651 public AppItem[] newArray(int size) {
1652 return new AppItem[size];
1653 }
1654 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001655 }
1656
Jeff Sharkey8a503642011-06-10 13:31:21 -07001657 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001658 * Adapter of applications, sorted by total usage descending.
1659 */
1660 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001661 private final UidDetailProvider mProvider;
1662 private final int mInsetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001663 private final UserManager mUm;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001664
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001665 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001666 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001667
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001668 public DataUsageAdapter(final UserManager userManager, UidDetailProvider provider, int insetSide) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001669 mProvider = checkNotNull(provider);
1670 mInsetSide = insetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001671 mUm = userManager;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001672 }
1673
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001674 /**
1675 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1676 */
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001677 public void bindStats(NetworkStats stats, int[] restrictedUids) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001678 mItems.clear();
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001679 mLargest = 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001680
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001681 final int currentUserId = ActivityManager.getCurrentUser();
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001682 final List<UserHandle> profiles = mUm.getUserProfiles();
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001683 final SparseArray<AppItem> knownItems = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001684
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001685 NetworkStats.Entry entry = null;
1686 final int size = stats != null ? stats.size() : 0;
1687 for (int i = 0; i < size; i++) {
1688 entry = stats.getValues(i, entry);
1689
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001690 // Decide how to collapse items together
1691 final int uid = entry.uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001692
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001693 final int collapseKey;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001694 final int category;
1695 final int userId = UserHandle.getUserId(uid);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001696 if (UserHandle.isApp(uid)) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001697 if (profiles.contains(new UserHandle(userId))) {
1698 if (userId != currentUserId) {
1699 // Add to a managed user item.
1700 final int managedKey = UidDetailProvider.buildKeyForUser(userId);
1701 accumulate(managedKey, knownItems, entry,
1702 AppItem.CATEGORY_USER);
1703 }
1704 // Add to app item.
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001705 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001706 category = AppItem.CATEGORY_APP;
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001707 } else {
Zoltan Szatmary-Ban77c1d362014-12-12 18:48:03 +00001708 // If it is a removed user add it to the removed users' key
1709 final UserInfo info = mUm.getUserInfo(userId);
1710 if (info == null) {
1711 collapseKey = UID_REMOVED;
1712 category = AppItem.CATEGORY_APP;
1713 } else {
1714 // Add to other user item.
1715 collapseKey = UidDetailProvider.buildKeyForUser(userId);
1716 category = AppItem.CATEGORY_USER;
1717 }
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001718 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001719 } else if (uid == UID_REMOVED || uid == UID_TETHERING) {
1720 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001721 category = AppItem.CATEGORY_APP;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001722 } else {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001723 collapseKey = android.os.Process.SYSTEM_UID;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001724 category = AppItem.CATEGORY_APP;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001725 }
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001726 accumulate(collapseKey, knownItems, entry, category);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001727 }
1728
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001729 final int restrictedUidsMax = restrictedUids.length;
1730 for (int i = 0; i < restrictedUidsMax; ++i) {
1731 final int uid = restrictedUids[i];
1732 // Only splice in restricted state for current user or managed users
1733 if (!profiles.contains(new UserHandle(UserHandle.getUserId(uid)))) {
1734 continue;
1735 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001736
1737 AppItem item = knownItems.get(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001738 if (item == null) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001739 item = new AppItem(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001740 item.total = -1;
1741 mItems.add(item);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001742 knownItems.put(item.key, item);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001743 }
1744 item.restricted = true;
1745 }
1746
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001747 if (!mItems.isEmpty()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001748 final AppItem title = new AppItem();
1749 title.category = AppItem.CATEGORY_APP_TITLE;
1750 mItems.add(title);
1751 }
1752
Jeff Sharkey8a503642011-06-10 13:31:21 -07001753 Collections.sort(mItems);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001754 notifyDataSetChanged();
1755 }
1756
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001757 /**
1758 * Accumulate data usage of a network stats entry for the item mapped by the collapse key.
1759 * Creates the item if needed.
1760 *
1761 * @param collapseKey the collapse key used to map the item.
1762 * @param knownItems collection of known (already existing) items.
1763 * @param entry the network stats entry to extract data usage from.
1764 * @param itemCategory the item is categorized on the list view by this category. Must be
1765 * either AppItem.APP_ITEM_CATEGORY or AppItem.MANAGED_USER_ITEM_CATEGORY
1766 */
1767 private void accumulate(int collapseKey, final SparseArray<AppItem> knownItems,
1768 NetworkStats.Entry entry, int itemCategory) {
1769 final int uid = entry.uid;
1770 AppItem item = knownItems.get(collapseKey);
1771 if (item == null) {
1772 item = new AppItem(collapseKey);
1773 item.category = itemCategory;
1774 mItems.add(item);
1775 knownItems.put(item.key, item);
1776 }
1777 item.addUid(uid);
1778 item.total += entry.rxBytes + entry.txBytes;
1779 if (mLargest < item.total) {
1780 mLargest = item.total;
1781 }
1782 }
1783
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001784 @Override
1785 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001786 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001787 }
1788
1789 @Override
1790 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001791 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001792 }
1793
1794 @Override
1795 public long getItemId(int position) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001796 return mItems.get(position).key;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001797 }
1798
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001799 /**
1800 * See {@link #getItemViewType} for the view types.
1801 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001802 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001803 public int getViewTypeCount() {
1804 return 2;
1805 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001806
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001807 /**
1808 * Returns 1 for separator items and 0 for anything else.
1809 */
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001810 @Override
1811 public int getItemViewType(int position) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001812 final AppItem item = mItems.get(position);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001813 if (item.category == AppItem.CATEGORY_APP_TITLE) {
1814 return 1;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001815 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001816 return 0;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001817 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001818 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001819
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001820 @Override
1821 public boolean areAllItemsEnabled() {
1822 return false;
1823 }
1824
1825 @Override
1826 public boolean isEnabled(int position) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001827 if (position > mItems.size()) {
1828 throw new ArrayIndexOutOfBoundsException();
1829 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001830 return getItemViewType(position) == 0;
1831 }
1832
1833 @Override
1834 public View getView(int position, View convertView, ViewGroup parent) {
1835 final AppItem item = mItems.get(position);
1836 if (getItemViewType(position) == 1) {
1837 if (convertView == null) {
1838 convertView = inflateCategoryHeader(LayoutInflater.from(parent.getContext()),
1839 parent);
1840 }
1841
1842 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
1843 title.setText(R.string.data_usage_app);
1844
1845 } else {
1846 if (convertView == null) {
1847 convertView = LayoutInflater.from(parent.getContext()).inflate(
1848 R.layout.data_usage_item, parent, false);
1849
1850 if (mInsetSide > 0) {
1851 convertView.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
1852 }
1853 }
1854
1855 final Context context = parent.getContext();
1856
1857 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
1858 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1859 android.R.id.progress);
1860
1861 // kick off async load of app details
1862 UidDetailTask.bindView(mProvider, item, convertView);
1863
1864 if (item.restricted && item.total <= 0) {
1865 text1.setText(R.string.data_usage_app_restricted);
1866 progress.setVisibility(View.GONE);
1867 } else {
1868 text1.setText(Formatter.formatFileSize(context, item.total));
1869 progress.setVisibility(View.VISIBLE);
1870 }
1871
1872 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1873 progress.setProgress(percentTotal);
1874 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001875
1876 return convertView;
1877 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001878 }
1879
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001880 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001881 * Empty {@link Fragment} that controls display of UID details in
1882 * {@link DataUsageSummary}.
1883 */
1884 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001885 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001886
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001887 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001888 if (!parent.isAdded()) return;
1889
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001890 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001891 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001892
1893 final AppDetailsFragment fragment = new AppDetailsFragment();
1894 fragment.setArguments(args);
1895 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001896 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1897 ft.add(fragment, TAG_APP_DETAILS);
1898 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001899 ft.setBreadCrumbTitle(
1900 parent.getResources().getString(R.string.data_usage_app_summary_title));
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001901 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001902 }
1903
1904 @Override
1905 public void onStart() {
1906 super.onStart();
1907 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001908 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001909 target.updateBody();
1910 }
1911
1912 @Override
1913 public void onStop() {
1914 super.onStop();
1915 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001916 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001917 target.updateBody();
1918 }
1919 }
1920
1921 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001922 * Dialog to request user confirmation before setting
1923 * {@link NetworkPolicy#limitBytes}.
1924 */
1925 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001926 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001927 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001928
1929 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001930 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001931
Jeff Sharkeyd0a55312014-08-08 10:25:24 -07001932 final NetworkPolicy policy = parent.mPolicyEditor.getPolicy(parent.mTemplate);
1933 if (policy == null) return;
1934
Jeff Sharkey461842a2011-09-25 18:22:48 -07001935 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001936 final CharSequence message;
Jeff Sharkeyd0a55312014-08-08 10:25:24 -07001937 final long minLimitBytes = (long) (policy.warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001938 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001939
1940 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001941 final String currentTab = parent.mCurrentTab;
1942 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001943 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001944 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001945 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001946 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001947 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001948 } else if (isMobileTab(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001949 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001950 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001951 } else {
1952 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001953 }
1954
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001955 final Bundle args = new Bundle();
1956 args.putCharSequence(EXTRA_MESSAGE, message);
1957 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1958
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001959 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1960 dialog.setArguments(args);
1961 dialog.setTargetFragment(parent, 0);
1962 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1963 }
1964
1965 @Override
1966 public Dialog onCreateDialog(Bundle savedInstanceState) {
1967 final Context context = getActivity();
1968
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001969 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001970 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1971
1972 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1973 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001974 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001975
1976 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001977 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001978 public void onClick(DialogInterface dialog, int which) {
1979 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1980 if (target != null) {
1981 target.setPolicyLimitBytes(limitBytes);
1982 }
1983 }
1984 });
1985
1986 return builder.create();
1987 }
1988 }
1989
1990 /**
1991 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1992 */
1993 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001994 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001995
1996 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001997 if (!parent.isAdded()) return;
1998
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001999 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002000 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002001
2002 final CycleEditorFragment dialog = new CycleEditorFragment();
2003 dialog.setArguments(args);
2004 dialog.setTargetFragment(parent, 0);
2005 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
2006 }
2007
2008 @Override
2009 public Dialog onCreateDialog(Bundle savedInstanceState) {
2010 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002011 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2012 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002013
2014 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2015 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2016
2017 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
2018 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
2019
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002020 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2021 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002022
2023 cycleDayPicker.setMinValue(1);
2024 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002025 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002026 cycleDayPicker.setWrapSelectorWheel(true);
2027
2028 builder.setTitle(R.string.data_usage_cycle_editor_title);
2029 builder.setView(view);
2030
2031 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2032 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002033 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002034 public void onClick(DialogInterface dialog, int which) {
Jeff Sharkeyd277de92013-03-25 15:11:25 -07002035 // clear focus to finish pending text edits
2036 cycleDayPicker.clearFocus();
2037
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002038 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08002039 final String cycleTimezone = new Time().timezone;
2040 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002041 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002042 }
2043 });
2044
2045 return builder.create();
2046 }
2047 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002048
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002049 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002050 * Dialog to edit {@link NetworkPolicy#warningBytes}.
2051 */
2052 public static class WarningEditorFragment extends DialogFragment {
2053 private static final String EXTRA_TEMPLATE = "template";
2054
2055 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002056 if (!parent.isAdded()) return;
2057
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002058 final Bundle args = new Bundle();
2059 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
2060
2061 final WarningEditorFragment dialog = new WarningEditorFragment();
2062 dialog.setArguments(args);
2063 dialog.setTargetFragment(parent, 0);
2064 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
2065 }
2066
2067 @Override
2068 public Dialog onCreateDialog(Bundle savedInstanceState) {
2069 final Context context = getActivity();
2070 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2071 final NetworkPolicyEditor editor = target.mPolicyEditor;
2072
2073 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2074 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2075
2076 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
2077 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
2078
2079 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2080 final long warningBytes = editor.getPolicyWarningBytes(template);
2081 final long limitBytes = editor.getPolicyLimitBytes(template);
2082
2083 bytesPicker.setMinValue(0);
2084 if (limitBytes != LIMIT_DISABLED) {
2085 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
2086 } else {
2087 bytesPicker.setMaxValue(Integer.MAX_VALUE);
2088 }
2089 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
2090 bytesPicker.setWrapSelectorWheel(false);
2091
2092 builder.setTitle(R.string.data_usage_warning_editor_title);
2093 builder.setView(view);
2094
2095 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2096 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002097 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002098 public void onClick(DialogInterface dialog, int which) {
2099 // clear focus to finish pending text edits
2100 bytesPicker.clearFocus();
2101
2102 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
2103 editor.setPolicyWarningBytes(template, bytes);
2104 target.updatePolicy(false);
2105 }
2106 });
2107
2108 return builder.create();
2109 }
2110 }
2111
2112 /**
2113 * Dialog to edit {@link NetworkPolicy#limitBytes}.
2114 */
2115 public static class LimitEditorFragment extends DialogFragment {
2116 private static final String EXTRA_TEMPLATE = "template";
2117
2118 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002119 if (!parent.isAdded()) return;
2120
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002121 final Bundle args = new Bundle();
2122 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
2123
2124 final LimitEditorFragment dialog = new LimitEditorFragment();
2125 dialog.setArguments(args);
2126 dialog.setTargetFragment(parent, 0);
2127 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
2128 }
2129
2130 @Override
2131 public Dialog onCreateDialog(Bundle savedInstanceState) {
2132 final Context context = getActivity();
2133 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2134 final NetworkPolicyEditor editor = target.mPolicyEditor;
2135
2136 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2137 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2138
2139 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
2140 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
2141
2142 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2143 final long warningBytes = editor.getPolicyWarningBytes(template);
2144 final long limitBytes = editor.getPolicyLimitBytes(template);
2145
2146 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09002147 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002148 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
2149 } else {
2150 bytesPicker.setMinValue(0);
2151 }
2152 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
2153 bytesPicker.setWrapSelectorWheel(false);
2154
2155 builder.setTitle(R.string.data_usage_limit_editor_title);
2156 builder.setView(view);
2157
2158 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2159 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002160 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002161 public void onClick(DialogInterface dialog, int which) {
2162 // clear focus to finish pending text edits
2163 bytesPicker.clearFocus();
2164
2165 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
2166 editor.setPolicyLimitBytes(template, bytes);
2167 target.updatePolicy(false);
2168 }
2169 });
2170
2171 return builder.create();
2172 }
2173 }
2174 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07002175 * Dialog to request user confirmation before disabling data.
2176 */
2177 public static class ConfirmDataDisableFragment extends DialogFragment {
Sanket Padawe24f834d2015-01-08 11:23:11 -08002178 static int mSubId;
2179 public static void show(DataUsageSummary parent, int subId) {
2180 mSubId = subId;
Jeff Sharkey461842a2011-09-25 18:22:48 -07002181 if (!parent.isAdded()) return;
2182
Jeff Sharkey28130d92011-09-02 16:10:24 -07002183 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
2184 dialog.setTargetFragment(parent, 0);
2185 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
2186 }
2187
2188 @Override
2189 public Dialog onCreateDialog(Bundle savedInstanceState) {
2190 final Context context = getActivity();
2191
2192 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2193 builder.setMessage(R.string.data_usage_disable_mobile);
2194
2195 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002196 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07002197 public void onClick(DialogInterface dialog, int which) {
2198 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2199 if (target != null) {
2200 // TODO: extend to modify policy enabled flag.
Sanket Padawe24f834d2015-01-08 11:23:11 -08002201 target.setMobileDataEnabled(mSubId, false);
Jeff Sharkey28130d92011-09-02 16:10:24 -07002202 }
2203 }
2204 });
2205 builder.setNegativeButton(android.R.string.cancel, null);
2206
2207 return builder.create();
2208 }
2209 }
2210
2211 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002212 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07002213 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002214 */
2215 public static class ConfirmRestrictFragment extends DialogFragment {
2216 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002217 if (!parent.isAdded()) return;
2218
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002219 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
2220 dialog.setTargetFragment(parent, 0);
2221 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
2222 }
2223
2224 @Override
2225 public Dialog onCreateDialog(Bundle savedInstanceState) {
2226 final Context context = getActivity();
2227
2228 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002229 builder.setTitle(R.string.data_usage_restrict_background_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07002230 if (Utils.hasMultipleUsers(context)) {
2231 builder.setMessage(R.string.data_usage_restrict_background_multiuser);
2232 } else {
2233 builder.setMessage(R.string.data_usage_restrict_background);
2234 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002235
2236 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002237 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002238 public void onClick(DialogInterface dialog, int which) {
2239 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2240 if (target != null) {
2241 target.setRestrictBackground(true);
2242 }
2243 }
2244 });
2245 builder.setNegativeButton(android.R.string.cancel, null);
2246
2247 return builder.create();
2248 }
2249 }
2250
2251 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002252 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
2253 * change has been denied, usually based on
2254 * {@link DataUsageSummary#hasLimitedNetworks()}.
2255 */
2256 public static class DeniedRestrictFragment extends DialogFragment {
2257 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002258 if (!parent.isAdded()) return;
2259
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002260 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
2261 dialog.setTargetFragment(parent, 0);
2262 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
2263 }
2264
2265 @Override
2266 public Dialog onCreateDialog(Bundle savedInstanceState) {
2267 final Context context = getActivity();
2268
2269 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2270 builder.setTitle(R.string.data_usage_app_restrict_background);
2271 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
2272 builder.setPositiveButton(android.R.string.ok, null);
2273
2274 return builder.create();
2275 }
2276 }
2277
2278 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002279 * Dialog to request user confirmation before setting
2280 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
2281 */
2282 public static class ConfirmAppRestrictFragment extends DialogFragment {
2283 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002284 if (!parent.isAdded()) return;
2285
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002286 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2287 dialog.setTargetFragment(parent, 0);
2288 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2289 }
2290
2291 @Override
2292 public Dialog onCreateDialog(Bundle savedInstanceState) {
2293 final Context context = getActivity();
2294
2295 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002296 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2297 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2298
2299 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002300 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002301 public void onClick(DialogInterface dialog, int which) {
2302 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2303 if (target != null) {
2304 target.setAppRestrictBackground(true);
2305 }
2306 }
2307 });
2308 builder.setNegativeButton(android.R.string.cancel, null);
2309
2310 return builder.create();
2311 }
2312 }
2313
2314 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002315 * Compute default tab that should be selected, based on
2316 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2317 */
2318 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002319 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002320 if (template == null) {
Wink Savilleb003a852014-10-23 10:16:26 -07002321 final int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
Wink Saville0183fb52014-11-22 10:11:39 -08002322 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Savilled72c0622014-12-11 10:10:05 -08002323 if (SubscriptionManager.isValidSubscriptionId(subId)) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07002324 return TAB_MOBILE + String.valueOf(subId);
2325 }
2326 return null;
2327 }
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002328
2329 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002330 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002331 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002332 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002333 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002334 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002335 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002336 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002337 return TAB_WIFI;
2338 default:
2339 return null;
2340 }
2341 }
2342
2343 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002344 * Background task that loads {@link UidDetail}, binding to
2345 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002346 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002347 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2348 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002349 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002350 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002351
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002352 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002353 mProvider = checkNotNull(provider);
2354 mItem = checkNotNull(item);
2355 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002356 }
2357
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002358 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002359 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002360 final UidDetailTask existing = (UidDetailTask) target.getTag();
2361 if (existing != null) {
2362 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002363 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002364
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002365 final UidDetail cachedDetail = provider.getUidDetail(item.key, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002366 if (cachedDetail != null) {
2367 bindView(cachedDetail, target);
2368 } else {
2369 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2370 AsyncTask.THREAD_POOL_EXECUTOR));
2371 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002372 }
2373
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002374 private static void bindView(UidDetail detail, View target) {
2375 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2376 final TextView title = (TextView) target.findViewById(android.R.id.title);
2377
2378 if (detail != null) {
2379 icon.setImageDrawable(detail.icon);
2380 title.setText(detail.label);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +01002381 title.setContentDescription(detail.contentDescription);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002382 } else {
2383 icon.setImageDrawable(null);
2384 title.setText(null);
2385 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002386 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002387
2388 @Override
2389 protected void onPreExecute() {
2390 bindView(null, mTarget);
2391 }
2392
2393 @Override
2394 protected UidDetail doInBackground(Void... params) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002395 return mProvider.getUidDetail(mItem.key, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002396 }
2397
2398 @Override
2399 protected void onPostExecute(UidDetail result) {
2400 bindView(result, mTarget);
2401 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002402 }
2403
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002404 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002405 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002406 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002407 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002408 if (TEST_RADIOS) {
2409 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2410 }
2411
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002412 final ConnectivityManager conn = ConnectivityManager.from(context);
2413 final TelephonyManager tele = TelephonyManager.from(context);
2414
Wink Saville0183fb52014-11-22 10:11:39 -08002415 final List<SubscriptionInfo> subInfoList =
2416 SubscriptionManager.from(context).getActiveSubscriptionInfoList();
2417 // No activated Subscriptions
PauloftheWest50e6eca2014-10-03 11:07:14 -07002418 if (subInfoList == null) {
Wink Savillefcec91f2014-12-02 17:12:08 -08002419 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subInfoList=null");
PauloftheWest50e6eca2014-10-03 11:07:14 -07002420 return false;
2421 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002422 // require both supported network and ready SIM
PauloftheWest50e6eca2014-10-03 11:07:14 -07002423 boolean isReady = true;
Wink Savilleca756612014-11-08 10:47:12 -08002424 for (SubscriptionInfo subInfo : subInfoList) {
Stuart Scott3ada2ec2014-10-31 14:11:24 -07002425 isReady = isReady & tele.getSimState(subInfo.getSimSlotIndex()) == SIM_STATE_READY;
Wink Savillefcec91f2014-12-02 17:12:08 -08002426 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subInfo=" + subInfo);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002427 }
Wink Savillefcec91f2014-12-02 17:12:08 -08002428 boolean retVal = conn.isNetworkSupported(TYPE_MOBILE) && isReady;
2429 if (LOGD) {
2430 Log.d(TAG, "hasReadyMobileRadio:"
2431 + " conn.isNetworkSupported(TYPE_MOBILE)="
2432 + conn.isNetworkSupported(TYPE_MOBILE)
2433 + " isReady=" + isReady);
2434 }
2435 return retVal;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002436 }
2437
2438 /*
2439 * TODO: consider adding to TelephonyManager or SubscritpionManager.
2440 */
Wink Savilleb003a852014-10-23 10:16:26 -07002441 public static boolean hasReadyMobileRadio(Context context, int subId) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07002442 if (TEST_RADIOS) {
2443 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2444 }
2445
2446 final ConnectivityManager conn = ConnectivityManager.from(context);
2447 final TelephonyManager tele = TelephonyManager.from(context);
2448 final int slotId = SubscriptionManager.getSlotId(subId);
2449 final boolean isReady = tele.getSimState(slotId) == SIM_STATE_READY;
2450
Wink Savillefcec91f2014-12-02 17:12:08 -08002451 boolean retVal = conn.isNetworkSupported(TYPE_MOBILE) && isReady;
2452 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subId=" + subId
2453 + " conn.isNetworkSupported(TYPE_MOBILE)=" + conn.isNetworkSupported(TYPE_MOBILE)
2454 + " isReady=" + isReady);
2455 return retVal;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002456 }
2457
2458 /**
2459 * Test if device has a mobile 4G data radio.
2460 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002461 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002462 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2463 return false;
2464 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002465 if (TEST_RADIOS) {
2466 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2467 }
2468
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002469 final ConnectivityManager conn = ConnectivityManager.from(context);
2470 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002471
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002472 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Wink Saville55434042012-06-14 12:33:43 -07002473 final boolean hasLte = (tele.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE)
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002474 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002475 return hasWimax || hasLte;
2476 }
2477
2478 /**
2479 * Test if device has a Wi-Fi data radio.
2480 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002481 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002482 if (TEST_RADIOS) {
2483 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2484 }
2485
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002486 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002487 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002488 }
2489
2490 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002491 * Test if device has an ethernet network connection.
2492 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002493 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002494 if (TEST_RADIOS) {
2495 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2496 }
2497
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002498 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002499 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2500
2501 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002502 if (mStatsSession != null) {
2503 try {
2504 ethernetBytes = mStatsSession.getSummaryForNetwork(
2505 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2506 .getTotalBytes();
2507 } catch (RemoteException e) {
2508 throw new RuntimeException(e);
2509 }
2510 } else {
2511 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002512 }
2513
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002514 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002515 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002516 }
2517
2518 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002519 * Inflate a {@link Preference} style layout, adding the given {@link View}
2520 * widget into {@link android.R.id#widget_frame}.
2521 */
2522 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2523 final View view = inflater.inflate(R.layout.preference, root, false);
2524 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2525 android.R.id.widget_frame);
2526 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2527 return view;
2528 }
2529
Jeff Sharkeyb6548462014-07-21 15:38:06 -07002530 private static View inflateCategoryHeader(LayoutInflater inflater, ViewGroup root) {
2531 final TypedArray a = inflater.getContext().obtainStyledAttributes(null,
2532 com.android.internal.R.styleable.Preference,
2533 com.android.internal.R.attr.preferenceCategoryStyle, 0);
2534 final int resId = a.getResourceId(com.android.internal.R.styleable.Preference_layout, 0);
2535 return inflater.inflate(resId, root, false);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002536 }
2537
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002538 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002539 * Test if any networks are currently limited.
2540 */
2541 private boolean hasLimitedNetworks() {
2542 return !buildLimitedNetworksList().isEmpty();
2543 }
2544
2545 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002546 * Build string describing currently limited networks, which defines when
2547 * background data is restricted.
2548 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002549 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002550 private CharSequence buildLimitedNetworksString() {
2551 final List<CharSequence> limited = buildLimitedNetworksList();
2552
2553 // handle case where no networks limited
2554 if (limited.isEmpty()) {
2555 limited.add(getText(R.string.data_usage_list_none));
2556 }
2557
2558 return TextUtils.join(limited);
2559 }
2560
2561 /**
2562 * Build list of currently limited networks, which defines when background
2563 * data is restricted.
2564 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002565 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002566 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002567 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002568
2569 // build combined list of all limited networks
2570 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002571
2572 final TelephonyManager tele = TelephonyManager.from(context);
2573 if (tele.getSimState() == SIM_STATE_READY) {
2574 final String subscriberId = getActiveSubscriberId(context);
2575 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2576 limited.add(getText(R.string.data_usage_list_mobile));
2577 }
2578 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2579 limited.add(getText(R.string.data_usage_tab_3g));
2580 }
2581 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2582 limited.add(getText(R.string.data_usage_tab_4g));
2583 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002584 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002585
2586 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002587 limited.add(getText(R.string.data_usage_tab_wifi));
2588 }
2589 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2590 limited.add(getText(R.string.data_usage_tab_ethernet));
2591 }
2592
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002593 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002594 }
2595
2596 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002597 * Inset both selector and divider {@link Drawable} on the given
2598 * {@link ListView} by the requested dimensions.
2599 */
2600 private static void insetListViewDrawables(ListView view, int insetSide) {
2601 final Drawable selector = view.getSelector();
2602 final Drawable divider = view.getDivider();
2603
2604 // fully unregister these drawables so callbacks can be maintained after
2605 // wrapping below.
2606 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2607 view.setSelector(stub);
2608 view.setDivider(stub);
2609
2610 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2611 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2612 }
2613
2614 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002615 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002616 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002617 */
2618 private static void setPreferenceTitle(View parent, int resId) {
2619 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2620 title.setText(resId);
2621 }
2622
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002623 /**
2624 * Set {@link android.R.id#summary} for a preference view inflated with
2625 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2626 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002627 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002628 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2629 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002630 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002631 }
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002632
2633 /**
2634 * For search
2635 */
2636 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -07002637 new BaseSearchIndexProvider() {
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002638 @Override
2639 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
2640 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
2641
2642 final Resources res = context.getResources();
2643
2644 // Add fragment title
2645 SearchIndexableRaw data = new SearchIndexableRaw(context);
2646 data.title = res.getString(R.string.data_usage_summary_title);
2647 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2648 result.add(data);
2649
2650 // Mobile data
2651 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002652 data.key = DATA_USAGE_ENABLE_MOBILE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002653 data.title = res.getString(R.string.data_usage_enable_mobile);
2654 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2655 result.add(data);
2656
2657 // Set mobile data limit
2658 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002659 data.key = DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002660 data.title = res.getString(R.string.data_usage_disable_mobile_limit);
2661 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2662 result.add(data);
2663
Fabrice Di Megliof2a52262014-04-17 17:20:27 -07002664 // Data usage cycle
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002665 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002666 data.key = DATA_USAGE_CYCLE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002667 data.title = res.getString(R.string.data_usage_cycle);
2668 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2669 result.add(data);
2670
2671 return result;
2672 }
2673 };
2674
PauloftheWestc80b7612014-11-25 04:40:45 -08002675 private void addMobileTab(Context context, SubscriptionInfo subInfo) {
2676 if (subInfo != null && mMobileTagMap != null) {
2677 if (hasReadyMobileRadio(context, subInfo.getSubscriptionId())) {
2678 mTabHost.addTab(buildTabSpec(mMobileTagMap.get(subInfo.getSubscriptionId()),
2679 subInfo.getDisplayName()));
PauloftheWest50e6eca2014-10-03 11:07:14 -07002680 }
Wink Savillefcec91f2014-12-02 17:12:08 -08002681 } else {
2682 if (LOGD) Log.d(TAG, "addMobileTab: subInfoList is null");
PauloftheWest50e6eca2014-10-03 11:07:14 -07002683 }
2684 }
2685
Wink Savilleca756612014-11-08 10:47:12 -08002686 private SubscriptionInfo getCurrentTabSubInfo(Context context) {
PauloftheWestde338442014-10-29 14:46:44 -07002687 if (mSubInfoList != null && mTabHost != null) {
2688 final int currentTagIndex = mTabHost.getCurrentTab();
2689 int i = 0;
Wink Savilleca756612014-11-08 10:47:12 -08002690 for (SubscriptionInfo subInfo : mSubInfoList) {
Stuart Scott3ada2ec2014-10-31 14:11:24 -07002691 if (hasReadyMobileRadio(context, subInfo.getSubscriptionId())) {
PauloftheWestde338442014-10-29 14:46:44 -07002692 if (i++ == currentTagIndex) {
2693 return subInfo;
2694 }
2695 }
2696 }
2697 }
2698 return null;
2699 }
2700
PauloftheWest50e6eca2014-10-03 11:07:14 -07002701 /**
2702 * Init a map with subId key and mobile tag name
2703 * @param subInfoList The subscription Info List
2704 * @return The map or null if no activated subscription
2705 */
Wink Savilleca756612014-11-08 10:47:12 -08002706 private Map<Integer, String> initMobileTabTag(List<SubscriptionInfo> subInfoList) {
Wink Savilleb003a852014-10-23 10:16:26 -07002707 Map<Integer, String> map = null;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002708 if (subInfoList != null) {
2709 String mobileTag;
Wink Savilleb003a852014-10-23 10:16:26 -07002710 map = new HashMap<Integer, String>();
Wink Savillefcec91f2014-12-02 17:12:08 -08002711 for (SubscriptionInfo subInfo : subInfoList) {
2712 mobileTag = TAB_MOBILE + String.valueOf(subInfo.getSubscriptionId());
2713 map.put(subInfo.getSubscriptionId(), mobileTag);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002714 }
2715 }
2716 return map;
2717 }
2718
2719 private static boolean isMobileTab(String currentTab) {
2720 return currentTab != null ? currentTab.contains(TAB_MOBILE) : false;
2721 }
2722
Wink Savilleb003a852014-10-23 10:16:26 -07002723 private int getSubId(String currentTab) {
Wink Saville0183fb52014-11-22 10:11:39 -08002724 if (mMobileTagMap != null) {
2725 Set<Integer> set = mMobileTagMap.keySet();
2726 for (Integer subId : set) {
2727 if (mMobileTagMap.get(subId).equals(currentTab)) {
2728 return subId;
2729 }
PauloftheWest50e6eca2014-10-03 11:07:14 -07002730 }
2731 }
2732 Log.e(TAG, "currentTab = " + currentTab + " non mobile tab called this function");
2733 return -1;
2734 }
2735
2736 //SUB SELECT
PauloftheWest98523c82014-10-28 08:30:05 -07002737 private boolean isMobileDataAvailable(long subId) {
Wink Savilleb003a852014-10-23 10:16:26 -07002738 int[] subIds = SubscriptionManager.getSubId(PhoneConstants.SUB1);
PauloftheWest98523c82014-10-28 08:30:05 -07002739 if (subIds != null && subIds[0] == subId) {
2740 return true;
2741 }
2742
2743 subIds = SubscriptionManager.getSubId(PhoneConstants.SUB2);
2744 if (subIds != null && subIds[0] == subId) {
2745 return true;
2746 }
2747
2748 subIds = SubscriptionManager.getSubId(PhoneConstants.SUB3);
2749 if (subIds != null && subIds[0] == subId) {
2750 return true;
2751 }
2752 return false;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002753 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002754}