blob: 8ca62fbc3ab97d2111a4e6b6cf82e5847908b6e8 [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;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070065import android.content.res.Resources;
Jeff Sharkeyb6548462014-07-21 15:38:06 -070066import android.content.res.TypedArray;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070067import android.graphics.Color;
Jeff Sharkey5d706792011-09-08 18:57:17 -070068import android.graphics.drawable.ColorDrawable;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070069import android.graphics.drawable.Drawable;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070070import android.net.ConnectivityManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070071import android.net.INetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070072import android.net.INetworkStatsService;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070073import android.net.INetworkStatsSession;
Jeff Sharkey8a503642011-06-10 13:31:21 -070074import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070075import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070076import android.net.NetworkStats;
77import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070078import android.net.NetworkTemplate;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070079import android.net.TrafficStats;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070080import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070081import android.os.Bundle;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070082import android.os.INetworkManagementService;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070083import android.os.Parcel;
84import android.os.Parcelable;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070085import android.os.RemoteException;
86import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070087import android.os.SystemProperties;
Dianne Hackbornbb06a422012-08-16 11:01:57 -070088import android.os.UserHandle;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +010089import android.os.UserManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070090import android.preference.Preference;
Wink Savilleca756612014-11-08 10:47:12 -080091import android.telephony.SubscriptionInfo;
PauloftheWest50e6eca2014-10-03 11:07:14 -070092import android.telephony.SubscriptionManager;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070093import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070094import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070095import android.text.format.DateUtils;
96import android.text.format.Formatter;
Jeff Sharkeye5223a02012-03-09 17:11:14 -080097import android.text.format.Time;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070098import android.util.Log;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070099import android.util.SparseArray;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700100import android.util.SparseBooleanArray;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700101import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700102import android.view.Menu;
103import android.view.MenuInflater;
104import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700105import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700106import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700107import android.view.ViewGroup;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700108import android.widget.AdapterView;
109import android.widget.AdapterView.OnItemClickListener;
110import android.widget.AdapterView.OnItemSelectedListener;
111import android.widget.ArrayAdapter;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700112import android.widget.BaseAdapter;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700113import android.widget.Button;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700114import android.widget.ImageView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700115import android.widget.LinearLayout;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700116import android.widget.ListView;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700117import android.widget.NumberPicker;
Jeff Sharkey2412b0f2011-07-17 20:31:40 -0700118import android.widget.ProgressBar;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700119import android.widget.Spinner;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700120import android.widget.Switch;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700121import android.widget.TabHost;
122import android.widget.TabHost.OnTabChangeListener;
123import android.widget.TabHost.TabContentFactory;
124import android.widget.TabHost.TabSpec;
125import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700126import android.widget.TextView;
127
Wink Saville55434042012-06-14 12:33:43 -0700128import com.android.internal.telephony.PhoneConstants;
Jeff Sharkey5d706792011-09-08 18:57:17 -0700129import com.android.settings.drawable.InsetBoundsDrawable;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700130import com.android.settings.net.ChartData;
131import com.android.settings.net.ChartDataLoader;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700132import com.android.settings.net.DataUsageMeteredSettings;
Jeff Sharkeya662e492011-06-18 21:57:06 -0700133import com.android.settings.net.NetworkPolicyEditor;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700134import com.android.settings.net.SummaryForAllUidLoader;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700135import com.android.settings.net.UidDetail;
136import com.android.settings.net.UidDetailProvider;
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -0700137import com.android.settings.search.BaseSearchIndexProvider;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -0700138import com.android.settings.search.Indexable;
139import com.android.settings.search.SearchIndexableRaw;
PauloftheWest50e6eca2014-10-03 11:07:14 -0700140import com.android.settings.sim.SimSettings;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700141import com.android.settings.widget.ChartDataUsageView;
142import com.android.settings.widget.ChartDataUsageView.DataUsageChartListener;
PauloftheWestc80b7612014-11-25 04:40:45 -0800143import com.android.settings.widget.ChartNetworkSeriesView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700144import com.google.android.collect.Lists;
145
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700146import libcore.util.Objects;
147
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700148import java.util.ArrayList;
149import java.util.Collections;
PauloftheWest50e6eca2014-10-03 11:07:14 -0700150import java.util.HashMap;
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700151import java.util.List;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700152import java.util.Locale;
PauloftheWest50e6eca2014-10-03 11:07:14 -0700153import java.util.Map;
154import java.util.Set;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700155
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700156/**
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700157 * Panel showing data usage history across various networks, including options
158 * to inspect based on usage cycle and control through {@link NetworkPolicy}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700159 */
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700160public class DataUsageSummary extends HighlightingFragment implements Indexable {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700161 private static final String TAG = "DataUsage";
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800162 private static final boolean LOGD = false;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700163
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700164 // TODO: remove this testing code
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700165 private static final boolean TEST_ANIM = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700166 private static final boolean TEST_RADIOS = false;
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800167
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700168 private static final String TEST_RADIOS_PROP = "test.radios";
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800169 private static final String TEST_SUBSCRIBER_PROP = "test.subscriberid";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700170
Jeff Sharkey8a503642011-06-10 13:31:21 -0700171 private static final String TAB_3G = "3g";
172 private static final String TAB_4G = "4g";
173 private static final String TAB_MOBILE = "mobile";
174 private static final String TAB_WIFI = "wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700175 private static final String TAB_ETHERNET = "ethernet";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700176
Jeff Sharkey28130d92011-09-02 16:10:24 -0700177 private static final String TAG_CONFIRM_DATA_DISABLE = "confirmDataDisable";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700178 private static final String TAG_CONFIRM_LIMIT = "confirmLimit";
179 private static final String TAG_CYCLE_EDITOR = "cycleEditor";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700180 private static final String TAG_WARNING_EDITOR = "warningEditor";
181 private static final String TAG_LIMIT_EDITOR = "limitEditor";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700182 private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700183 private static final String TAG_DENIED_RESTRICT = "deniedRestrict";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700184 private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700185 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700186
Fabrice Di Meglio2169c882014-04-18 15:18:40 -0700187 private static final String DATA_USAGE_ENABLE_MOBILE_KEY = "data_usage_enable_mobile";
188 private static final String DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY =
189 "data_usage_disable_mobile_limit";
190 private static final String DATA_USAGE_CYCLE_KEY = "data_usage_cycle";
191
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700192 private static final int LOADER_CHART_DATA = 2;
193 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700194
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700195 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700196 private INetworkStatsService mStatsService;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700197 private NetworkPolicyManager mPolicyManager;
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700198 private TelephonyManager mTelephonyManager;
Wink Saville0183fb52014-11-22 10:11:39 -0800199 private SubscriptionManager mSubscriptionManager;
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700200
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700201 private INetworkStatsSession mStatsSession;
202
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700203 private static final String PREF_FILE = "data_usage";
204 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700205 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700206
207 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700208
Jeff Sharkey8a503642011-06-10 13:31:21 -0700209 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700210 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700211 private TabWidget mTabWidget;
212 private ListView mListView;
PauloftheWestc80b7612014-11-25 04:40:45 -0800213 private ChartNetworkSeriesView mSeries;
214 private ChartNetworkSeriesView mDetailedSeries;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700215 private DataUsageAdapter mAdapter;
216
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700217 /** Distance to inset content from sides, when needed. */
218 private int mInsetSide = 0;
219
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700220 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700221
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700222 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700223 private LinearLayout mNetworkSwitches;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700224 private boolean mDataEnabledSupported;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700225 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700226 private View mDataEnabledView;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700227 private boolean mDisableAtLimitSupported;
228 private Switch mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700229 private View mDisableAtLimitView;
230
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700231 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700232 private Spinner mCycleSpinner;
233 private CycleAdapter mCycleAdapter;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700234 private TextView mCycleSummary;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700235
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700236 private ChartDataUsageView mChart;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700237 private View mDisclaimer;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700238 private TextView mEmpty;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700239 private View mStupidPadding;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700240
241 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700242 private ImageView mAppIcon;
243 private ViewGroup mAppTitles;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700244 private TextView mAppTotal;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700245 private TextView mAppForeground;
246 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700247 private Button mAppSettings;
248
249 private LinearLayout mAppSwitches;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700250 private Switch mAppRestrict;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700251 private View mAppRestrictView;
252
Jeff Sharkey8a503642011-06-10 13:31:21 -0700253 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700254 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700255
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700256 private NetworkTemplate mTemplate;
257 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700258
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700259 private AppItem mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700260
261 private Intent mAppSettingsIntent;
262
Jeff Sharkeya662e492011-06-18 21:57:06 -0700263 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700264
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700265 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700266 private String mIntentTab = null;
267
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700268 private MenuItem mMenuRestrictBackground;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700269 private MenuItem mMenuShowWifi;
270 private MenuItem mMenuShowEthernet;
271 private MenuItem mMenuSimCards;
272 private MenuItem mMenuCellularNetworks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700273
Wink Savilleca756612014-11-08 10:47:12 -0800274 private List<SubscriptionInfo> mSubInfoList;
Wink Savilleb003a852014-10-23 10:16:26 -0700275 private Map<Integer,String> mMobileTagMap;
PauloftheWest50e6eca2014-10-03 11:07:14 -0700276
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700277 /** Flag used to ignore listeners during binding. */
278 private boolean mBinding;
279
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700280 private UidDetailProvider mUidDetailProvider;
281
Sanket Padawed819270f2015-01-14 11:03:33 -0800282 /**
283 * Local cache of data enabled for subId, used to work around delays.
284 */
285 private final Map<String, Boolean> mMobileDataEnabled = new HashMap<String, Boolean>();
286
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700287 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700288 public void onCreate(Bundle savedInstanceState) {
289 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700290 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700291
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700292 mNetworkService = INetworkManagementService.Stub.asInterface(
293 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700294 mStatsService = INetworkStatsService.Stub.asInterface(
295 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700296 mPolicyManager = NetworkPolicyManager.from(context);
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700297 mTelephonyManager = TelephonyManager.from(context);
Wink Saville0183fb52014-11-22 10:11:39 -0800298 mSubscriptionManager = SubscriptionManager.from(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700299
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700300 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700301
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700302 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700303 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700304
Wink Saville0183fb52014-11-22 10:11:39 -0800305 mSubInfoList = mSubscriptionManager.getActiveSubscriptionInfoList();
PauloftheWest50e6eca2014-10-03 11:07:14 -0700306 mMobileTagMap = initMobileTabTag(mSubInfoList);
307
Jaewan Kimffce9c12013-03-19 16:53:45 +0900308 try {
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700309 if (!mNetworkService.isBandwidthControlEnabled()) {
310 Log.w(TAG, "No bandwidth control; leaving");
311 getActivity().finish();
312 }
313 } catch (RemoteException e) {
314 Log.w(TAG, "No bandwidth control; leaving");
315 getActivity().finish();
316 }
317
318 try {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900319 mStatsSession = mStatsService.openSession();
320 } catch (RemoteException e) {
321 throw new RuntimeException(e);
322 }
323
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700324 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700325 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700326
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700327 // override preferences when no mobile radio
328 if (!hasReadyMobileRadio(context)) {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900329 mShowWifi = true;
330 mShowEthernet = true;
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700331 }
332
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700333 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700334 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700335
Jeff Sharkey8a503642011-06-10 13:31:21 -0700336 @Override
337 public View onCreateView(LayoutInflater inflater, ViewGroup container,
338 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700339
Jeff Sharkey8a503642011-06-10 13:31:21 -0700340 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700341 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
342
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700343 mUidDetailProvider = new UidDetailProvider(context);
344
Jeff Sharkey8a503642011-06-10 13:31:21 -0700345 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700346 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700347 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
348 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700349
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700350 // decide if we need to manually inset our content, or if we should rely
351 // on parent container for inset.
352 final boolean shouldInset = mListView.getScrollBarStyle()
353 == View.SCROLLBARS_OUTSIDE_OVERLAY;
Amith Yamasani56f51a82013-08-05 10:07:23 -0700354 mInsetSide = 0;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700355
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700356 // adjust padding around tabwidget as needed
Amith Yamasani56f51a82013-08-05 10:07:23 -0700357 prepareCustomPreferencesList(container, view, mListView, false);
Jeff Sharkey5d706792011-09-08 18:57:17 -0700358
Jeff Sharkey8a503642011-06-10 13:31:21 -0700359 mTabHost.setup();
360 mTabHost.setOnTabChangedListener(mTabListener);
361
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700362 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700363 mHeader.setClickable(true);
364
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700365 mListView.addHeaderView(new View(context), null, true);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700366 mListView.addHeaderView(mHeader, null, true);
367 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700368
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700369 if (mInsetSide > 0) {
370 // inset selector and divider drawables
371 insetListViewDrawables(mListView, mInsetSide);
Fabrice Di Megliob27223f2013-01-15 18:54:11 -0800372 mHeader.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700373 }
374
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700375 {
376 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700377 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
378 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700379 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700380
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700381 mDataEnabled = new Switch(inflater.getContext());
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700382 mDataEnabled.setClickable(false);
383 mDataEnabled.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700384 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700385 mDataEnabledView.setTag(R.id.preference_highlight_key,
386 DATA_USAGE_ENABLE_MOBILE_KEY);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700387 mDataEnabledView.setClickable(true);
388 mDataEnabledView.setFocusable(true);
389 mDataEnabledView.setOnClickListener(mDataEnabledListener);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700390 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700391
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700392 mDisableAtLimit = new Switch(inflater.getContext());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700393 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700394 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700395 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700396 mDisableAtLimitView.setTag(R.id.preference_highlight_key,
397 DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700398 mDisableAtLimitView.setClickable(true);
399 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700400 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
401 mNetworkSwitches.addView(mDisableAtLimitView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700402
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700403 mCycleView = inflater.inflate(R.layout.data_usage_cycles, mNetworkSwitches, false);
404 mCycleView.setTag(R.id.preference_highlight_key, DATA_USAGE_CYCLE_KEY);
405 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
406 mCycleAdapter = new CycleAdapter(context);
407 mCycleSpinner.setAdapter(mCycleAdapter);
408 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
409 mCycleSummary = (TextView) mCycleView.findViewById(R.id.cycle_summary);
410 mNetworkSwitches.addView(mCycleView);
PauloftheWestc80b7612014-11-25 04:40:45 -0800411 mSeries = (ChartNetworkSeriesView)view.findViewById(R.id.series);
412 mDetailedSeries = (ChartNetworkSeriesView)view.findViewById(R.id.detail_series);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700413 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700414
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700415 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700416 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800417 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700418
419 {
420 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700421 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700422 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
423 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700424 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
425 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700426 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700427
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700428 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700429
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700430 mAppRestrict = new Switch(inflater.getContext());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700431 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700432 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700433 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700434 mAppRestrictView.setClickable(true);
435 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700436 mAppRestrictView.setOnClickListener(mAppRestrictListener);
437 mAppSwitches.addView(mAppRestrictView);
438 }
439
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700440 mDisclaimer = mHeader.findViewById(R.id.disclaimer);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700441 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700442 mStupidPadding = mHeader.findViewById(R.id.stupid_padding);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700443
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +0100444 final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
445 mAdapter = new DataUsageAdapter(um, mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700446 mListView.setOnItemClickListener(mListListener);
447 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700448
449 return view;
450 }
451
452 @Override
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700453 public void onViewStateRestored(Bundle savedInstanceState) {
454 super.onViewStateRestored(savedInstanceState);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700455
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700456 // pick default tab based on incoming intent
457 final Intent intent = getActivity().getIntent();
458 mIntentTab = computeTabFromIntent(intent);
459
Jeff Sharkey8a503642011-06-10 13:31:21 -0700460 // this kicks off chain reaction which creates tabs, binds the body to
461 // selected network, and binds chart, cycles and detail list.
462 updateTabs();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700463 }
464
465 @Override
466 public void onResume() {
467 super.onResume();
468
469 getView().post(new Runnable() {
470 @Override
471 public void run() {
472 highlightViewIfNeeded();
473 }
474 });
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700475
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700476 // kick off background task to update stats
477 new AsyncTask<Void, Void, Void>() {
478 @Override
479 protected Void doInBackground(Void... params) {
480 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700481 // wait a few seconds before kicking off
482 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700483 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700484 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700485 } catch (RemoteException e) {
486 }
487 return null;
488 }
489
490 @Override
491 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700492 if (isAdded()) {
493 updateBody();
494 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700495 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700496 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700497 }
498
Jeff Sharkey8a503642011-06-10 13:31:21 -0700499 @Override
500 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
501 inflater.inflate(R.menu.data_usage, menu);
502 }
503
504 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700505 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700506 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700507 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700508 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700509
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700510 mMenuShowWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700511 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
512 mMenuShowWifi.setVisible(!appDetailMode);
513 } else {
514 mMenuShowWifi.setVisible(false);
515 }
516
517 mMenuShowEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
518 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
519 mMenuShowEthernet.setVisible(!appDetailMode);
520 } else {
521 mMenuShowEthernet.setVisible(false);
522 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700523
524 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700525 mMenuRestrictBackground.setVisible(
526 hasReadyMobileRadio(context) && isOwner && !appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700527
528 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
529 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700530 metered.setVisible(!appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700531 } else {
532 metered.setVisible(false);
533 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700534
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700535 // TODO: show when multiple sims available
536 mMenuSimCards = menu.findItem(R.id.data_usage_menu_sim_cards);
537 mMenuSimCards.setVisible(false);
538
539 mMenuCellularNetworks = menu.findItem(R.id.data_usage_menu_cellular_networks);
PauloftheWesta4b8fb32014-09-18 10:12:25 -0700540 mMenuCellularNetworks.setVisible(hasReadyMobileRadio(context)
541 && !appDetailMode && isOwner);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700542
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700543 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
544 String helpUrl;
545 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800546 HelpUtils.prepareHelpMenuItem(context, help, helpUrl);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700547 } else {
548 help.setVisible(false);
549 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700550
551 updateMenuTitles();
552 }
553
554 private void updateMenuTitles() {
555 if (mPolicyManager.getRestrictBackground()) {
556 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_allow_background);
557 } else {
558 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_restrict_background);
559 }
560
561 if (mShowWifi) {
562 mMenuShowWifi.setTitle(R.string.data_usage_menu_hide_wifi);
563 } else {
564 mMenuShowWifi.setTitle(R.string.data_usage_menu_show_wifi);
565 }
566
567 if (mShowEthernet) {
568 mMenuShowEthernet.setTitle(R.string.data_usage_menu_hide_ethernet);
569 } else {
570 mMenuShowEthernet.setTitle(R.string.data_usage_menu_show_ethernet);
571 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700572 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700573
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700574 @Override
575 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700576 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700577 case R.id.data_usage_menu_restrict_background: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700578 final boolean restrictBackground = !mPolicyManager.getRestrictBackground();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700579 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800580 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700581 } else {
582 // no confirmation to drop restriction
583 setRestrictBackground(false);
584 }
585 return true;
586 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700587 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700588 mShowWifi = !mShowWifi;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700589 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700590 updateMenuTitles();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700591 updateTabs();
592 return true;
593 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700594 case R.id.data_usage_menu_show_ethernet: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700595 mShowEthernet = !mShowEthernet;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700596 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700597 updateMenuTitles();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700598 updateTabs();
599 return true;
600 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700601 case R.id.data_usage_menu_sim_cards: {
602 // TODO: hook up to sim cards
603 return true;
604 }
605 case R.id.data_usage_menu_cellular_networks: {
606 final Intent intent = new Intent(Intent.ACTION_MAIN);
607 intent.setComponent(new ComponentName("com.android.phone",
608 "com.android.phone.MobileNetworkSettings"));
609 startActivity(intent);
610 return true;
611 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700612 case R.id.data_usage_menu_metered: {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800613 final SettingsActivity sa = (SettingsActivity) getActivity();
614 sa.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700615 R.string.data_usage_metered_title, null, this, 0);
616 return true;
617 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700618 }
619 return false;
620 }
621
Jeff Sharkey94a90952011-06-13 22:31:09 -0700622 @Override
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700623 public void onDestroy() {
Jeff Sharkey94a90952011-06-13 22:31:09 -0700624 mDataEnabledView = null;
625 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700626
627 mUidDetailProvider.clearCache();
628 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700629
630 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700631
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800632 super.onDestroy();
633 }
634
Jeff Sharkey8a503642011-06-10 13:31:21 -0700635 /**
Jeff Sharkey92811822012-05-04 11:47:29 -0700636 * Build and assign {@link LayoutTransition} to various containers. Should
637 * only be assigned after initial layout is complete.
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700638 */
Jeff Sharkey92811822012-05-04 11:47:29 -0700639 private void ensureLayoutTransitions() {
640 // skip when already setup
641 if (mChart.getLayoutTransition() != null) return;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700642
Jeff Sharkey92811822012-05-04 11:47:29 -0700643 mTabsContainer.setLayoutTransition(buildLayoutTransition());
644 mHeader.setLayoutTransition(buildLayoutTransition());
645 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700646
Jeff Sharkey92811822012-05-04 11:47:29 -0700647 final LayoutTransition chartTransition = buildLayoutTransition();
648 chartTransition.disableTransitionType(LayoutTransition.APPEARING);
649 chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
650 mChart.setLayoutTransition(chartTransition);
651 }
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700652
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700653 private static LayoutTransition buildLayoutTransition() {
654 final LayoutTransition transition = new LayoutTransition();
655 if (TEST_ANIM) {
656 transition.setDuration(1500);
657 }
658 transition.setAnimateParentHierarchy(false);
659 return transition;
660 }
661
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700662 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700663 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700664 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
665 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700666 */
667 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700668 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700669 mTabHost.clearAllTabs();
670
PauloftheWestc80b7612014-11-25 04:40:45 -0800671 for (int i = 0; i < mTelephonyManager.getSimCount(); i++) {
672 final SubscriptionInfo sir = Utils.findRecordBySlotId(context, i);
673 if (sir != null) {
674 addMobileTab(context, sir);
675 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700676 }
PauloftheWestc80b7612014-11-25 04:40:45 -0800677
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700678 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700679 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
680 }
PauloftheWestc80b7612014-11-25 04:40:45 -0800681
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700682 if (mShowEthernet && hasEthernet(context)) {
683 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
684 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700685
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700686 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700687 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
688 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
689 if (mIntentTab != null) {
690 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700691 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700692 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700693 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700694 mTabHost.setCurrentTabByTag(mIntentTab);
695 }
696 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700697 } else if (noTabs) {
698 // no usable tabs, so hide body
699 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700700 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700701 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700702 }
703 }
704
Jeff Sharkey8a503642011-06-10 13:31:21 -0700705 /**
706 * Factory that provide empty {@link View} to make {@link TabHost} happy.
707 */
708 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700709 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700710 public View createTabContent(String tag) {
711 return new View(mTabHost.getContext());
712 }
713 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700714
Jeff Sharkey8a503642011-06-10 13:31:21 -0700715 /**
716 * Build {@link TabSpec} with thin indicator, and empty content.
717 */
718 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700719 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
720 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700721 }
722
PauloftheWest50e6eca2014-10-03 11:07:14 -0700723 /**
724 * Build {@link TabSpec} with thin indicator, and empty content.
725 */
Stuart Scottb1531812014-11-04 14:52:23 -0800726 private TabSpec buildTabSpec(String tag, CharSequence title) {
PauloftheWest50e6eca2014-10-03 11:07:14 -0700727 return mTabHost.newTabSpec(tag).setIndicator(title).setContent(
728 mEmptyTabContent);
729 }
730
731
Jeff Sharkey8a503642011-06-10 13:31:21 -0700732 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700733 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700734 public void onTabChanged(String tabId) {
735 // user changed tab; update body
736 updateBody();
737 }
738 };
739
740 /**
741 * Update body content based on current tab. Loads
742 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
743 * binds them to visible controls.
744 */
745 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700746 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700747 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700748
Jeff Sharkeya662e492011-06-18 21:57:06 -0700749 final Context context = getActivity();
PauloftheWestc80b7612014-11-25 04:40:45 -0800750 final Resources resources = context.getResources();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700751 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700752 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700753
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700754 if (currentTab == null) {
755 Log.w(TAG, "no tab selected; hiding body");
756 mListView.setVisibility(View.GONE);
757 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700758 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700759 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700760 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700761
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700762 mCurrentTab = currentTab;
763
Jeff Sharkey8a503642011-06-10 13:31:21 -0700764 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
765
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700766 mDataEnabledSupported = isOwner;
767 mDisableAtLimitSupported = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700768
Wink Savillefcec91f2014-12-02 17:12:08 -0800769 // TODO: remove mobile tabs when SIM isn't ready probably by
770 // TODO: using SubscriptionManager.getActiveSubscriptionInfoList.
771 if (LOGD) Log.d(TAG, "updateBody() isMobileTab=" + isMobileTab(currentTab));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700772
PauloftheWest50e6eca2014-10-03 11:07:14 -0700773 if (isMobileTab(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800774 if (LOGD) Log.d(TAG, "updateBody() mobile tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700775 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
776 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
PauloftheWest50e6eca2014-10-03 11:07:14 -0700777 mDataEnabledSupported = isMobileDataAvailable(getSubId(currentTab));
Jeff Sharkey4e2d16c2014-12-03 13:44:26 -0800778
779 // Match mobile traffic for this subscriber, but normalize it to
780 // catch any other merged subscribers.
781 mTemplate = buildTemplateMobileAll(
782 getActiveSubscriberId(context, getSubId(currentTab)));
783 mTemplate = NetworkTemplate.normalize(mTemplate,
784 mTelephonyManager.getMergedSubscriberIds());
785
Jeff Sharkey8a503642011-06-10 13:31:21 -0700786 } else if (TAB_3G.equals(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800787 if (LOGD) Log.d(TAG, "updateBody() 3g tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700788 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
789 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
790 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700791 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700792
793 } else if (TAB_4G.equals(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800794 if (LOGD) Log.d(TAG, "updateBody() 4g tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700795 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
796 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
797 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700798 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700799
800 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700801 // wifi doesn't have any controls
Wink Savillefcec91f2014-12-02 17:12:08 -0800802 if (LOGD) Log.d(TAG, "updateBody() wifi tab");
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700803 mDataEnabledSupported = false;
804 mDisableAtLimitSupported = false;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700805 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700806
807 } else if (TAB_ETHERNET.equals(currentTab)) {
808 // ethernet doesn't have any controls
Wink Savillefcec91f2014-12-02 17:12:08 -0800809 if (LOGD) Log.d(TAG, "updateBody() ethernet tab");
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700810 mDataEnabledSupported = false;
811 mDisableAtLimitSupported = false;
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700812 mTemplate = buildTemplateEthernet();
813
814 } else {
Wink Savillefcec91f2014-12-02 17:12:08 -0800815 if (LOGD) Log.d(TAG, "updateBody() unknown tab");
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700816 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700817 }
818
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700819 // kick off loader for network history
820 // TODO: consider chaining two loaders together instead of reloading
821 // network history when showing app detail.
822 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700823 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700824
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700825 // detail mode can change visible menus, invalidate
826 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700827
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700828 mBinding = false;
PauloftheWestc80b7612014-11-25 04:40:45 -0800829
830 int seriesColor = resources.getColor(R.color.sim_noitification);
831 if (mCurrentTab != null && mCurrentTab.length() > TAB_MOBILE.length() ){
832 final int slotId = Integer.parseInt(mCurrentTab.substring(TAB_MOBILE.length(),
833 mCurrentTab.length()));
834 final SubscriptionInfo sir = com.android.settings.Utils.findRecordBySlotId(context,
835 slotId);
836
837 if (sir != null) {
838 seriesColor = sir.getIconTint();
839 }
840 }
841
842 mSeries.setChartColor(Color.BLACK, seriesColor, seriesColor);
843 mDetailedSeries.setChartColor(Color.BLACK, seriesColor, seriesColor);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700844 }
845
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700846 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700847 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700848 }
849
850 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700851 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700852 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700853 */
854 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700855 final Context context = getActivity();
856 final PackageManager pm = context.getPackageManager();
857 final LayoutInflater inflater = getActivity().getLayoutInflater();
858
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700859 if (isAppDetailMode()) {
860 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700861 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700862 } else {
863 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700864 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700865
866 // hide detail stats when not in detail mode
867 mChart.bindDetailNetworkStats(null);
868 return;
869 }
870
871 // remove warning/limit sweeps while in detail mode
872 mChart.bindNetworkPolicy(null);
873
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700874 // show icon and all labels appearing under this app
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700875 final int uid = mCurrentApp.key;
876 final UidDetail detail = mUidDetailProvider.getUidDetail(uid, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700877 mAppIcon.setImageDrawable(detail.icon);
878
879 mAppTitles.removeAllViews();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700880
881 View title = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700882 if (detail.detailLabels != null) {
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100883 final int n = detail.detailLabels.length;
884 for (int i = 0; i < n; ++i) {
885 CharSequence label = detail.detailLabels[i];
886 CharSequence contentDescription = detail.detailContentDescriptions[i];
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700887 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100888 TextView appTitle = (TextView) title.findViewById(R.id.app_title);
889 appTitle.setText(label);
890 appTitle.setContentDescription(contentDescription);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700891 mAppTitles.addView(title);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700892 }
893 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700894 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100895 TextView appTitle = (TextView) title.findViewById(R.id.app_title);
896 appTitle.setText(detail.label);
897 appTitle.setContentDescription(detail.contentDescription);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700898 mAppTitles.addView(title);
899 }
900
901 // Remember last slot for summary
902 if (title != null) {
903 mAppTotal = (TextView) title.findViewById(R.id.app_summary);
904 } else {
905 mAppTotal = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700906 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700907
908 // enable settings button when package provides it
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700909 final String[] packageNames = pm.getPackagesForUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700910 if (packageNames != null && packageNames.length > 0) {
911 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700912 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
913
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700914 // Search for match across all packages
915 boolean matchFound = false;
916 for (String packageName : packageNames) {
917 mAppSettingsIntent.setPackage(packageName);
918 if (pm.resolveActivity(mAppSettingsIntent, 0) != null) {
919 matchFound = true;
920 break;
921 }
922 }
923
Zoltan Szatmary-Banf20d39e2014-08-07 15:27:08 +0100924 mAppSettings.setOnClickListener(new OnClickListener() {
925 @Override
926 public void onClick(View v) {
927 if (!isAdded()) {
928 return;
929 }
930
931 // TODO: target towards entire UID instead of just first package
932 getActivity().startActivityAsUser(mAppSettingsIntent,
933 new UserHandle(UserHandle.getUserId(uid)));
934 }
935 });
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700936 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -0700937 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700938
939 } else {
940 mAppSettingsIntent = null;
Zoltan Szatmary-Banf20d39e2014-08-07 15:27:08 +0100941 mAppSettings.setOnClickListener(null);
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700942 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700943 }
944
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700945 updateDetailData();
946
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700947 if (UserHandle.isApp(uid) && !mPolicyManager.getRestrictBackground()
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700948 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700949 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800950 setPreferenceSummary(mAppRestrictView,
951 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700952
953 mAppRestrictView.setVisibility(View.VISIBLE);
954 mAppRestrict.setChecked(getAppRestrictBackground());
955
956 } else {
957 mAppRestrictView.setVisibility(View.GONE);
958 }
959 }
960
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700961 private void setPolicyWarningBytes(long warningBytes) {
962 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700963 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700964 updatePolicy(false);
965 }
966
967 private void setPolicyLimitBytes(long limitBytes) {
968 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700969 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700970 updatePolicy(false);
971 }
972
Wink Savilleb003a852014-10-23 10:16:26 -0700973 private boolean isMobileDataEnabled(int subId) {
Wink Savillea4f4d182014-12-05 15:34:46 -0800974 if (LOGD) Log.d(TAG, "isMobileDataEnabled:+ subId=" + subId);
PauloftheWest50e6eca2014-10-03 11:07:14 -0700975 boolean isEnable = false;
Sanket Padawed819270f2015-01-14 11:03:33 -0800976 if (mMobileDataEnabled.get(String.valueOf(subId)) != null) {
977 //TODO: deprecate and remove this once enabled flag is on policy
978 //Multiple Subscriptions, the value need to be reseted
979 isEnable = mMobileDataEnabled.get(String.valueOf(subId)).booleanValue();
Wink Savillea4f4d182014-12-05 15:34:46 -0800980 if (LOGD) {
981 Log.d(TAG, "isMobileDataEnabled: != null, subId=" + subId
982 + " isEnable=" + isEnable);
983 }
Sanket Padawed819270f2015-01-14 11:03:33 -0800984 mMobileDataEnabled.put(String.valueOf(subId), null);
Jeff Sharkey28130d92011-09-02 16:10:24 -0700985 } else {
Wink Savillea4f4d182014-12-05 15:34:46 -0800986 // SUB SELECT
987 isEnable = mTelephonyManager.getDataEnabled(subId);
988 if (LOGD) {
989 Log.d(TAG, "isMobileDataEnabled: == null, subId=" + subId
990 + " isEnable=" + isEnable);
991 }
Jeff Sharkey28130d92011-09-02 16:10:24 -0700992 }
PauloftheWest50e6eca2014-10-03 11:07:14 -0700993 return isEnable;
Jeff Sharkey28130d92011-09-02 16:10:24 -0700994 }
995
Sanket Padawe24f834d2015-01-08 11:23:11 -0800996 private void setMobileDataEnabled(int subId, boolean enabled) {
Jeff Sharkey28130d92011-09-02 16:10:24 -0700997 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
Sanket Padawe24f834d2015-01-08 11:23:11 -0800998 mTelephonyManager.setDataEnabled(subId, enabled);
Sanket Padawed819270f2015-01-14 11:03:33 -0800999 mMobileDataEnabled.put(String.valueOf(subId), enabled);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001000 updatePolicy(false);
1001 }
1002
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001003 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001004 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked()
1005 && ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001006 }
1007
1008 private boolean isBandwidthControlEnabled() {
1009 try {
1010 return mNetworkService.isBandwidthControlEnabled();
1011 } catch (RemoteException e) {
1012 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
1013 return false;
1014 }
1015 }
1016
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001017 public void setRestrictBackground(boolean restrictBackground) {
1018 mPolicyManager.setRestrictBackground(restrictBackground);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001019 updateMenuTitles();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001020 }
1021
1022 private boolean getAppRestrictBackground() {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001023 final int uid = mCurrentApp.key;
1024 final int uidPolicy = mPolicyManager.getUidPolicy(uid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001025 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
1026 }
1027
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001028 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001029 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001030 final int uid = mCurrentApp.key;
1031 mPolicyManager.setUidPolicy(
1032 uid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001033 mAppRestrict.setChecked(restrictBackground);
1034 }
1035
Jeff Sharkey8a503642011-06-10 13:31:21 -07001036 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001037 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
1038 * current {@link #mTemplate}.
1039 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001040 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001041 boolean dataEnabledVisible = mDataEnabledSupported;
1042 boolean disableAtLimitVisible = mDisableAtLimitSupported;
1043
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001044 if (isAppDetailMode()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001045 dataEnabledVisible = false;
1046 disableAtLimitVisible = false;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001047 }
1048
Jeff Sharkey28130d92011-09-02 16:10:24 -07001049 // TODO: move enabled state directly into policy
PauloftheWest50e6eca2014-10-03 11:07:14 -07001050 if (isMobileTab(mCurrentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001051 mBinding = true;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001052 mDataEnabled.setChecked(isMobileDataEnabled(getSubId(mCurrentTab)));
Jeff Sharkey28130d92011-09-02 16:10:24 -07001053 mBinding = false;
1054 }
1055
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001056 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001057 //SUB SELECT
1058 if (isNetworkPolicyModifiable(policy) && isMobileDataAvailable(getSubId(mCurrentTab))) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001059 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001060 if (!isAppDetailMode()) {
1061 mChart.bindNetworkPolicy(policy);
1062 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001063
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001064 } else {
1065 // controls are disabled; don't bind warning/limit sweeps
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001066 disableAtLimitVisible = false;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001067 mChart.bindNetworkPolicy(null);
1068 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001069
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001070 mDataEnabledView.setVisibility(dataEnabledVisible ? View.VISIBLE : View.GONE);
1071 mDisableAtLimitView.setVisibility(disableAtLimitVisible ? View.VISIBLE : View.GONE);
1072
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001073 if (refreshCycle) {
1074 // generate cycle list based on policy and available history
1075 updateCycleList(policy);
1076 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001077 }
1078
1079 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001080 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
1081 * and available {@link NetworkStatsHistory} data. Always selects the newest
1082 * item, updating the inspection range on {@link #mChart}.
1083 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001084 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001085 // stash away currently selected cycle to try restoring below
1086 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -07001087 mCycleAdapter.clear();
1088
1089 final Context context = mCycleSpinner.getContext();
1090
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001091 long historyStart = Long.MAX_VALUE;
1092 long historyEnd = Long.MIN_VALUE;
1093 if (mChartData != null) {
1094 historyStart = mChartData.network.getStart();
1095 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001096 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001097
Jeff Sharkey461842a2011-09-25 18:22:48 -07001098 final long now = System.currentTimeMillis();
1099 if (historyStart == Long.MAX_VALUE) historyStart = now;
1100 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001101
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001102 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001103 if (policy != null) {
1104 // find the next cycle boundary
1105 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001106
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001107 // walk backwards, generating all valid cycle ranges
1108 while (cycleEnd > historyStart) {
1109 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
1110 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
1111 + historyStart);
1112 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1113 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001114 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001115 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001116
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001117 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001118 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001119 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001120
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001121 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001122 // no policy defined cycles; show entry for each four-week period
1123 long cycleEnd = historyEnd;
1124 while (cycleEnd > historyStart) {
1125 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
1126 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1127 cycleEnd = cycleStart;
1128 }
1129
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001130 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001131 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001132
1133 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001134 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001135 final int position = mCycleAdapter.findNearestPosition(previousItem);
1136 mCycleSpinner.setSelection(position);
1137
1138 // only force-update cycle when changed; skipping preserves any
1139 // user-defined inspection region.
1140 final CycleItem selectedItem = mCycleAdapter.getItem(position);
1141 if (!Objects.equal(selectedItem, previousItem)) {
1142 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1143 } else {
1144 // but still kick off loader for detailed list
1145 updateDetailData();
1146 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001147 } else {
1148 updateDetailData();
1149 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001150 }
1151
Sanket Padawed819270f2015-01-14 11:03:33 -08001152 private void disableDataForOtherSubscriptions(SubscriptionInfo currentSir) {
1153 if (mSubInfoList != null) {
1154 for (SubscriptionInfo subInfo : mSubInfoList) {
1155 if (subInfo.getSubscriptionId() != currentSir.getSubscriptionId()) {
1156 setMobileDataEnabled(subInfo.getSubscriptionId(), false);
1157 }
1158 }
1159 }
1160 }
1161
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001162 private View.OnClickListener mDataEnabledListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001163 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001164 public void onClick(View v) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001165 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001166
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001167 final boolean dataEnabled = !mDataEnabled.isChecked();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001168 final String currentTab = mCurrentTab;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001169 if (isMobileTab(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001170 if (dataEnabled) {
PauloftheWestde338442014-10-29 14:46:44 -07001171 // If we are showing the Sim Card tile then we are a Multi-Sim device.
1172 if (Utils.showSimCardTile(getActivity())) {
1173 handleMultiSimDataDialog();
1174 } else {
Sanket Padawe24f834d2015-01-08 11:23:11 -08001175 setMobileDataEnabled(getSubId(currentTab), true);
PauloftheWestde338442014-10-29 14:46:44 -07001176 }
Jeff Sharkey28130d92011-09-02 16:10:24 -07001177 } else {
1178 // disabling data; show confirmation dialog which eventually
1179 // calls setMobileDataEnabled() once user confirms.
Sanket Padawe24f834d2015-01-08 11:23:11 -08001180 ConfirmDataDisableFragment.show(DataUsageSummary.this, getSubId(mCurrentTab));
Jeff Sharkey28130d92011-09-02 16:10:24 -07001181 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001182 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001183
Jeff Sharkey28130d92011-09-02 16:10:24 -07001184 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001185 }
1186 };
1187
PauloftheWestde338442014-10-29 14:46:44 -07001188 private void handleMultiSimDataDialog() {
PauloftheWestf0d84e22014-12-10 08:50:54 -08001189 final Context context = getActivity();
1190 final SubscriptionInfo currentSir = getCurrentTabSubInfo(context);
Sanket Padawe766ab602015-01-20 10:36:15 -08001191
1192 //If sim has not loaded after toggling data switch, return.
1193 if (currentSir == null) {
1194 return;
1195 }
1196
Wink Saville0183fb52014-11-22 10:11:39 -08001197 final SubscriptionInfo nextSir = mSubscriptionManager.getActiveSubscriptionInfo(
1198 mSubscriptionManager.getDefaultDataSubId());
PauloftheWestde338442014-10-29 14:46:44 -07001199
PauloftheWestf0d84e22014-12-10 08:50:54 -08001200 // If the device is single SIM or is enabling data on the active data SIM then forgo
1201 // the pop-up.
1202 if (!Utils.showSimCardTile(context) ||
Sanket Padawed819270f2015-01-14 11:03:33 -08001203 (nextSir != null && currentSir != null &&
1204 currentSir.getSubscriptionId() == nextSir.getSubscriptionId())) {
Sanket Padawe24f834d2015-01-08 11:23:11 -08001205 setMobileDataEnabled(currentSir.getSubscriptionId(), true);
Sanket Padawed819270f2015-01-14 11:03:33 -08001206 if (nextSir != null && currentSir != null &&
1207 currentSir.getSubscriptionId() == nextSir.getSubscriptionId()) {
1208 disableDataForOtherSubscriptions(currentSir);
1209 }
PauloftheWestde338442014-10-29 14:46:44 -07001210 updateBody();
1211 return;
1212 }
1213
PauloftheWestf0d84e22014-12-10 08:50:54 -08001214 final String previousName = (nextSir == null)
1215 ? context.getResources().getString(R.string.sim_selection_required_pref)
1216 : nextSir.getDisplayName().toString();
1217
PauloftheWestde338442014-10-29 14:46:44 -07001218 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
1219
1220 builder.setTitle(R.string.sim_change_data_title);
1221 builder.setMessage(getActivity().getResources().getString(R.string.sim_change_data_message,
PauloftheWestf0d84e22014-12-10 08:50:54 -08001222 currentSir.getDisplayName(), previousName));
PauloftheWestde338442014-10-29 14:46:44 -07001223
1224 builder.setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
1225 @Override
1226 public void onClick(DialogInterface dialog, int id) {
Wink Saville0183fb52014-11-22 10:11:39 -08001227 mSubscriptionManager.setDefaultDataSubId(currentSir.getSubscriptionId());
Sanket Padawe24f834d2015-01-08 11:23:11 -08001228 setMobileDataEnabled(currentSir.getSubscriptionId(), true);
Sanket Padawed819270f2015-01-14 11:03:33 -08001229 disableDataForOtherSubscriptions(currentSir);
PauloftheWestde338442014-10-29 14:46:44 -07001230 updateBody();
1231 }
1232 });
1233 builder.setNegativeButton(R.string.cancel, null);
1234
1235 builder.create().show();
1236 }
1237
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001238 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001239 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001240 public void onClick(View v) {
1241 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001242 if (disableAtLimit) {
1243 // enabling limit; show confirmation dialog which eventually
1244 // calls setPolicyLimitBytes() once user confirms.
1245 ConfirmLimitFragment.show(DataUsageSummary.this);
1246 } else {
1247 setPolicyLimitBytes(LIMIT_DISABLED);
1248 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001249 }
1250 };
1251
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001252 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001253 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001254 public void onClick(View v) {
1255 final boolean restrictBackground = !mAppRestrict.isChecked();
1256
1257 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001258 // enabling restriction; show confirmation dialog which
1259 // eventually calls setRestrictBackground() once user
1260 // confirms.
1261 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001262 } else {
1263 setAppRestrictBackground(false);
1264 }
1265 }
1266 };
1267
Jeff Sharkey8a503642011-06-10 13:31:21 -07001268 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001269 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001270 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001271 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001272 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001273
1274 // TODO: sigh, remove this hack once we understand 6450986
1275 if (mUidDetailProvider == null || app == null) return;
1276
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001277 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001278 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001279 }
1280 };
1281
1282 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001283 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001284 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1285 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1286 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001287 // show cycle editor; will eventually call setPolicyCycleDay()
1288 // when user finishes editing.
1289 CycleEditorFragment.show(DataUsageSummary.this);
1290
1291 // reset spinner to something other than "change cycle..."
1292 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001293
1294 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001295 if (LOGD) {
1296 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1297 + cycle.end + "]");
1298 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001299
1300 // update chart to show selected cycle, and update detail data
1301 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001302 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001303
1304 updateDetailData();
1305 }
1306 }
1307
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001308 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001309 public void onNothingSelected(AdapterView<?> parent) {
1310 // ignored
1311 }
1312 };
1313
1314 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001315 * Update details based on {@link #mChart} inspection range depending on
1316 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1317 * of applications data usage, and when {@link #isAppDetailMode()} update
1318 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001319 */
1320 private void updateDetailData() {
1321 if (LOGD) Log.d(TAG, "updateDetailData()");
1322
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001323 final long start = mChart.getInspectStart();
1324 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001325 final long now = System.currentTimeMillis();
1326
1327 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001328
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001329 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001330 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001331 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001332 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001333 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001334 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001335 final long foregroundBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001336 final long totalBytes = defaultBytes + foregroundBytes;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001337
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001338 if (mAppTotal != null) {
1339 mAppTotal.setText(Formatter.formatFileSize(context, totalBytes));
1340 }
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001341 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1342 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1343
1344 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001345 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001346
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001347 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001348
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001349 mCycleSummary.setVisibility(View.GONE);
1350
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001351 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001352 if (mChartData != null) {
1353 entry = mChartData.network.getValues(start, end, now, null);
1354 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001355
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001356 mCycleSummary.setVisibility(View.VISIBLE);
1357
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001358 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001359 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001360 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001361 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001362
1363 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1364 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001365 mCycleSummary.setText(totalPhrase);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001366
PauloftheWest50e6eca2014-10-03 11:07:14 -07001367 if (isMobileTab(mCurrentTab) || TAB_3G.equals(mCurrentTab)
László Dávid0b8bf4e2012-10-24 23:31:47 +02001368 || TAB_4G.equals(mCurrentTab)) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001369 if (isAppDetailMode()) {
1370 mDisclaimer.setVisibility(View.GONE);
1371 } else {
1372 mDisclaimer.setVisibility(View.VISIBLE);
1373 }
Jeff Sharkeye557c332012-04-13 16:04:07 -07001374 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001375 mDisclaimer.setVisibility(View.GONE);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001376 }
1377
Jeff Sharkey92811822012-05-04 11:47:29 -07001378 // initial layout is finished above, ensure we have transitions
1379 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001380 }
1381
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001382 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1383 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001384 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001385 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001386 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001387 }
1388
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001389 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001390 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1391 mChartData = data;
1392 mChart.bindNetworkStats(mChartData.network);
1393 mChart.bindDetailNetworkStats(mChartData.detail);
1394
1395 // calcuate policy cycles based on available data
1396 updatePolicy(true);
1397 updateAppDetail();
1398
1399 // force scroll to top of body when showing detail
1400 if (mChartData.detail != null) {
1401 mListView.smoothScrollToPosition(0);
1402 }
1403 }
1404
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001405 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001406 public void onLoaderReset(Loader<ChartData> loader) {
1407 mChartData = null;
1408 mChart.bindNetworkStats(null);
1409 mChart.bindDetailNetworkStats(null);
1410 }
1411 };
1412
1413 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001414 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001415 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001416 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001417 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001418 }
1419
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001420 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001421 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001422 final int[] restrictedUids = mPolicyManager.getUidsWithPolicy(
Jeff Sharkeye557c332012-04-13 16:04:07 -07001423 POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001424 mAdapter.bindStats(data, restrictedUids);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001425 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001426 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001427
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001428 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001429 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001430 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001431 updateEmptyVisible();
1432 }
1433
1434 private void updateEmptyVisible() {
1435 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1436 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001437 mStupidPadding.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001438 }
1439 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001440
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001441 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001442 final TelephonyManager tele = TelephonyManager.from(context);
1443 final String actualSubscriberId = tele.getSubscriberId();
Wink Savillefcec91f2014-12-02 17:12:08 -08001444 String retVal = SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
1445 if (LOGD) Log.d(TAG, "getActiveSubscriberId=" + retVal + " actualSubscriberId=" + actualSubscriberId);
1446 return retVal;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001447 }
1448
Wink Savilleb003a852014-10-23 10:16:26 -07001449 private static String getActiveSubscriberId(Context context, int subId) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07001450 final TelephonyManager tele = TelephonyManager.from(context);
Wink Savillefcec91f2014-12-02 17:12:08 -08001451 String retVal = tele.getSubscriberId(subId);
1452 if (LOGD) Log.d(TAG, "getActiveSubscriberId=" + retVal + " subId=" + subId);
1453 return retVal;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001454 }
1455
Jeff Sharkey8a503642011-06-10 13:31:21 -07001456 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001457 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001458 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001459 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001460 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001461
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001462 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001463 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001464 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001465 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001466
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001467 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001468 public void requestWarningEdit() {
1469 WarningEditorFragment.show(DataUsageSummary.this);
1470 }
1471
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001472 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001473 public void requestLimitEdit() {
1474 LimitEditorFragment.show(DataUsageSummary.this);
1475 }
1476 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001477
1478 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001479 * List item that reflects a specific data usage cycle.
1480 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001481 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001482 public CharSequence label;
1483 public long start;
1484 public long end;
1485
Jeff Sharkey8a503642011-06-10 13:31:21 -07001486 CycleItem(CharSequence label) {
1487 this.label = label;
1488 }
1489
1490 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001491 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001492 this.start = start;
1493 this.end = end;
1494 }
1495
Jeff Sharkey8a503642011-06-10 13:31:21 -07001496 @Override
1497 public String toString() {
1498 return label.toString();
1499 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001500
1501 @Override
1502 public boolean equals(Object o) {
1503 if (o instanceof CycleItem) {
1504 final CycleItem another = (CycleItem) o;
1505 return start == another.start && end == another.end;
1506 }
1507 return false;
1508 }
1509
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001510 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001511 public int compareTo(CycleItem another) {
1512 return Long.compare(start, another.start);
1513 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001514 }
1515
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001516 private static final StringBuilder sBuilder = new StringBuilder(50);
1517 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1518 sBuilder, Locale.getDefault());
1519
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001520 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001521 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001522
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001523 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001524 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001525 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1526 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001527 }
1528 }
1529
Jeff Sharkey8a503642011-06-10 13:31:21 -07001530 /**
1531 * Special-case data usage cycle that triggers dialog to change
1532 * {@link NetworkPolicy#cycleDay}.
1533 */
1534 public static class CycleChangeItem extends CycleItem {
1535 public CycleChangeItem(Context context) {
1536 super(context.getString(R.string.data_usage_change_cycle));
1537 }
1538 }
1539
1540 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001541 private boolean mChangePossible = false;
1542 private boolean mChangeVisible = false;
1543
1544 private final CycleChangeItem mChangeItem;
1545
Jeff Sharkey8a503642011-06-10 13:31:21 -07001546 public CycleAdapter(Context context) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001547 super(context, R.layout.data_usage_cycle_item);
1548 setDropDownViewResource(R.layout.data_usage_cycle_item_dropdown);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001549 mChangeItem = new CycleChangeItem(context);
1550 }
1551
1552 public void setChangePossible(boolean possible) {
1553 mChangePossible = possible;
1554 updateChange();
1555 }
1556
1557 public void setChangeVisible(boolean visible) {
1558 mChangeVisible = visible;
1559 updateChange();
1560 }
1561
1562 private void updateChange() {
1563 remove(mChangeItem);
1564 if (mChangePossible && mChangeVisible) {
1565 add(mChangeItem);
1566 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001567 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001568
1569 /**
1570 * Find position of {@link CycleItem} in this adapter which is nearest
1571 * the given {@link CycleItem}.
1572 */
1573 public int findNearestPosition(CycleItem target) {
1574 if (target != null) {
1575 final int count = getCount();
1576 for (int i = count - 1; i >= 0; i--) {
1577 final CycleItem item = getItem(i);
1578 if (item instanceof CycleChangeItem) {
1579 continue;
1580 } else if (item.compareTo(target) >= 0) {
1581 return i;
1582 }
1583 }
1584 }
1585 return 0;
1586 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001587 }
1588
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001589 public static class AppItem implements Comparable<AppItem>, Parcelable {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001590 public static final int CATEGORY_USER = 0;
1591 public static final int CATEGORY_APP_TITLE = 1;
1592 public static final int CATEGORY_APP = 2;
1593
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001594 public final int key;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001595 public boolean restricted;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001596 public int category;
1597
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001598 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001599 public long total;
1600
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001601 public AppItem() {
1602 this.key = 0;
1603 }
1604
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001605 public AppItem(int key) {
1606 this.key = key;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001607 }
1608
1609 public AppItem(Parcel parcel) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001610 key = parcel.readInt();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001611 uids = parcel.readSparseBooleanArray();
1612 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001613 }
1614
1615 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001616 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001617 }
1618
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001619 @Override
1620 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001621 dest.writeInt(key);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001622 dest.writeSparseBooleanArray(uids);
1623 dest.writeLong(total);
1624 }
1625
1626 @Override
1627 public int describeContents() {
1628 return 0;
1629 }
1630
1631 @Override
1632 public int compareTo(AppItem another) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001633 int comparison = Integer.compare(category, another.category);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001634 if (comparison == 0) {
1635 comparison = Long.compare(another.total, total);
1636 }
1637 return comparison;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001638 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001639
1640 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1641 @Override
1642 public AppItem createFromParcel(Parcel in) {
1643 return new AppItem(in);
1644 }
1645
1646 @Override
1647 public AppItem[] newArray(int size) {
1648 return new AppItem[size];
1649 }
1650 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001651 }
1652
Jeff Sharkey8a503642011-06-10 13:31:21 -07001653 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001654 * Adapter of applications, sorted by total usage descending.
1655 */
1656 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001657 private final UidDetailProvider mProvider;
1658 private final int mInsetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001659 private final UserManager mUm;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001660
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001661 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001662 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001663
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001664 public DataUsageAdapter(final UserManager userManager, UidDetailProvider provider, int insetSide) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001665 mProvider = checkNotNull(provider);
1666 mInsetSide = insetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001667 mUm = userManager;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001668 }
1669
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001670 /**
1671 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1672 */
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001673 public void bindStats(NetworkStats stats, int[] restrictedUids) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001674 mItems.clear();
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001675 mLargest = 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001676
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001677 final int currentUserId = ActivityManager.getCurrentUser();
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001678 final List<UserHandle> profiles = mUm.getUserProfiles();
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001679 final SparseArray<AppItem> knownItems = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001680
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001681 NetworkStats.Entry entry = null;
1682 final int size = stats != null ? stats.size() : 0;
1683 for (int i = 0; i < size; i++) {
1684 entry = stats.getValues(i, entry);
1685
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001686 // Decide how to collapse items together
1687 final int uid = entry.uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001688
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001689 final int collapseKey;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001690 final int category;
1691 final int userId = UserHandle.getUserId(uid);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001692 if (UserHandle.isApp(uid)) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001693 if (profiles.contains(new UserHandle(userId))) {
1694 if (userId != currentUserId) {
1695 // Add to a managed user item.
1696 final int managedKey = UidDetailProvider.buildKeyForUser(userId);
1697 accumulate(managedKey, knownItems, entry,
1698 AppItem.CATEGORY_USER);
1699 }
1700 // Add to app item.
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001701 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001702 category = AppItem.CATEGORY_APP;
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001703 } else {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001704 // Add to other user item.
1705 collapseKey = UidDetailProvider.buildKeyForUser(userId);
1706 category = AppItem.CATEGORY_USER;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001707 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001708 } else if (uid == UID_REMOVED || uid == UID_TETHERING) {
1709 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001710 category = AppItem.CATEGORY_APP;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001711 } else {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001712 collapseKey = android.os.Process.SYSTEM_UID;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001713 category = AppItem.CATEGORY_APP;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001714 }
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001715 accumulate(collapseKey, knownItems, entry, category);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001716 }
1717
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001718 final int restrictedUidsMax = restrictedUids.length;
1719 for (int i = 0; i < restrictedUidsMax; ++i) {
1720 final int uid = restrictedUids[i];
1721 // Only splice in restricted state for current user or managed users
1722 if (!profiles.contains(new UserHandle(UserHandle.getUserId(uid)))) {
1723 continue;
1724 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001725
1726 AppItem item = knownItems.get(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001727 if (item == null) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001728 item = new AppItem(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001729 item.total = -1;
1730 mItems.add(item);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001731 knownItems.put(item.key, item);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001732 }
1733 item.restricted = true;
1734 }
1735
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001736 if (!mItems.isEmpty()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001737 final AppItem title = new AppItem();
1738 title.category = AppItem.CATEGORY_APP_TITLE;
1739 mItems.add(title);
1740 }
1741
Jeff Sharkey8a503642011-06-10 13:31:21 -07001742 Collections.sort(mItems);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001743 notifyDataSetChanged();
1744 }
1745
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001746 /**
1747 * Accumulate data usage of a network stats entry for the item mapped by the collapse key.
1748 * Creates the item if needed.
1749 *
1750 * @param collapseKey the collapse key used to map the item.
1751 * @param knownItems collection of known (already existing) items.
1752 * @param entry the network stats entry to extract data usage from.
1753 * @param itemCategory the item is categorized on the list view by this category. Must be
1754 * either AppItem.APP_ITEM_CATEGORY or AppItem.MANAGED_USER_ITEM_CATEGORY
1755 */
1756 private void accumulate(int collapseKey, final SparseArray<AppItem> knownItems,
1757 NetworkStats.Entry entry, int itemCategory) {
1758 final int uid = entry.uid;
1759 AppItem item = knownItems.get(collapseKey);
1760 if (item == null) {
1761 item = new AppItem(collapseKey);
1762 item.category = itemCategory;
1763 mItems.add(item);
1764 knownItems.put(item.key, item);
1765 }
1766 item.addUid(uid);
1767 item.total += entry.rxBytes + entry.txBytes;
1768 if (mLargest < item.total) {
1769 mLargest = item.total;
1770 }
1771 }
1772
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001773 @Override
1774 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001775 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001776 }
1777
1778 @Override
1779 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001780 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001781 }
1782
1783 @Override
1784 public long getItemId(int position) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001785 return mItems.get(position).key;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001786 }
1787
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001788 /**
1789 * See {@link #getItemViewType} for the view types.
1790 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001791 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001792 public int getViewTypeCount() {
1793 return 2;
1794 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001795
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001796 /**
1797 * Returns 1 for separator items and 0 for anything else.
1798 */
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001799 @Override
1800 public int getItemViewType(int position) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001801 final AppItem item = mItems.get(position);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001802 if (item.category == AppItem.CATEGORY_APP_TITLE) {
1803 return 1;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001804 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001805 return 0;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001806 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001807 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001808
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001809 @Override
1810 public boolean areAllItemsEnabled() {
1811 return false;
1812 }
1813
1814 @Override
1815 public boolean isEnabled(int position) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001816 if (position > mItems.size()) {
1817 throw new ArrayIndexOutOfBoundsException();
1818 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001819 return getItemViewType(position) == 0;
1820 }
1821
1822 @Override
1823 public View getView(int position, View convertView, ViewGroup parent) {
1824 final AppItem item = mItems.get(position);
1825 if (getItemViewType(position) == 1) {
1826 if (convertView == null) {
1827 convertView = inflateCategoryHeader(LayoutInflater.from(parent.getContext()),
1828 parent);
1829 }
1830
1831 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
1832 title.setText(R.string.data_usage_app);
1833
1834 } else {
1835 if (convertView == null) {
1836 convertView = LayoutInflater.from(parent.getContext()).inflate(
1837 R.layout.data_usage_item, parent, false);
1838
1839 if (mInsetSide > 0) {
1840 convertView.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
1841 }
1842 }
1843
1844 final Context context = parent.getContext();
1845
1846 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
1847 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1848 android.R.id.progress);
1849
1850 // kick off async load of app details
1851 UidDetailTask.bindView(mProvider, item, convertView);
1852
1853 if (item.restricted && item.total <= 0) {
1854 text1.setText(R.string.data_usage_app_restricted);
1855 progress.setVisibility(View.GONE);
1856 } else {
1857 text1.setText(Formatter.formatFileSize(context, item.total));
1858 progress.setVisibility(View.VISIBLE);
1859 }
1860
1861 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1862 progress.setProgress(percentTotal);
1863 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001864
1865 return convertView;
1866 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001867 }
1868
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001869 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001870 * Empty {@link Fragment} that controls display of UID details in
1871 * {@link DataUsageSummary}.
1872 */
1873 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001874 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001875
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001876 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001877 if (!parent.isAdded()) return;
1878
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001879 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001880 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001881
1882 final AppDetailsFragment fragment = new AppDetailsFragment();
1883 fragment.setArguments(args);
1884 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001885 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1886 ft.add(fragment, TAG_APP_DETAILS);
1887 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001888 ft.setBreadCrumbTitle(
1889 parent.getResources().getString(R.string.data_usage_app_summary_title));
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001890 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001891 }
1892
1893 @Override
1894 public void onStart() {
1895 super.onStart();
1896 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001897 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001898 target.updateBody();
1899 }
1900
1901 @Override
1902 public void onStop() {
1903 super.onStop();
1904 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001905 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001906 target.updateBody();
1907 }
1908 }
1909
1910 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001911 * Dialog to request user confirmation before setting
1912 * {@link NetworkPolicy#limitBytes}.
1913 */
1914 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001915 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001916 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001917
1918 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001919 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001920
Jeff Sharkeyd0a55312014-08-08 10:25:24 -07001921 final NetworkPolicy policy = parent.mPolicyEditor.getPolicy(parent.mTemplate);
1922 if (policy == null) return;
1923
Jeff Sharkey461842a2011-09-25 18:22:48 -07001924 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001925 final CharSequence message;
Jeff Sharkeyd0a55312014-08-08 10:25:24 -07001926 final long minLimitBytes = (long) (policy.warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001927 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001928
1929 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001930 final String currentTab = parent.mCurrentTab;
1931 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001932 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001933 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001934 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001935 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001936 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001937 } else if (isMobileTab(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001938 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001939 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001940 } else {
1941 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001942 }
1943
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001944 final Bundle args = new Bundle();
1945 args.putCharSequence(EXTRA_MESSAGE, message);
1946 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1947
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001948 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1949 dialog.setArguments(args);
1950 dialog.setTargetFragment(parent, 0);
1951 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1952 }
1953
1954 @Override
1955 public Dialog onCreateDialog(Bundle savedInstanceState) {
1956 final Context context = getActivity();
1957
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001958 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001959 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1960
1961 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1962 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001963 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001964
1965 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001966 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001967 public void onClick(DialogInterface dialog, int which) {
1968 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1969 if (target != null) {
1970 target.setPolicyLimitBytes(limitBytes);
1971 }
1972 }
1973 });
1974
1975 return builder.create();
1976 }
1977 }
1978
1979 /**
1980 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1981 */
1982 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001983 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001984
1985 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001986 if (!parent.isAdded()) return;
1987
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001988 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001989 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001990
1991 final CycleEditorFragment dialog = new CycleEditorFragment();
1992 dialog.setArguments(args);
1993 dialog.setTargetFragment(parent, 0);
1994 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1995 }
1996
1997 @Override
1998 public Dialog onCreateDialog(Bundle savedInstanceState) {
1999 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002000 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2001 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002002
2003 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2004 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2005
2006 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
2007 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
2008
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002009 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2010 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002011
2012 cycleDayPicker.setMinValue(1);
2013 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002014 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002015 cycleDayPicker.setWrapSelectorWheel(true);
2016
2017 builder.setTitle(R.string.data_usage_cycle_editor_title);
2018 builder.setView(view);
2019
2020 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2021 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002022 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002023 public void onClick(DialogInterface dialog, int which) {
Jeff Sharkeyd277de92013-03-25 15:11:25 -07002024 // clear focus to finish pending text edits
2025 cycleDayPicker.clearFocus();
2026
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002027 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08002028 final String cycleTimezone = new Time().timezone;
2029 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002030 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002031 }
2032 });
2033
2034 return builder.create();
2035 }
2036 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002037
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002038 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002039 * Dialog to edit {@link NetworkPolicy#warningBytes}.
2040 */
2041 public static class WarningEditorFragment extends DialogFragment {
2042 private static final String EXTRA_TEMPLATE = "template";
2043
2044 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002045 if (!parent.isAdded()) return;
2046
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002047 final Bundle args = new Bundle();
2048 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
2049
2050 final WarningEditorFragment dialog = new WarningEditorFragment();
2051 dialog.setArguments(args);
2052 dialog.setTargetFragment(parent, 0);
2053 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
2054 }
2055
2056 @Override
2057 public Dialog onCreateDialog(Bundle savedInstanceState) {
2058 final Context context = getActivity();
2059 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2060 final NetworkPolicyEditor editor = target.mPolicyEditor;
2061
2062 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2063 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2064
2065 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
2066 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
2067
2068 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2069 final long warningBytes = editor.getPolicyWarningBytes(template);
2070 final long limitBytes = editor.getPolicyLimitBytes(template);
2071
2072 bytesPicker.setMinValue(0);
2073 if (limitBytes != LIMIT_DISABLED) {
2074 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
2075 } else {
2076 bytesPicker.setMaxValue(Integer.MAX_VALUE);
2077 }
2078 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
2079 bytesPicker.setWrapSelectorWheel(false);
2080
2081 builder.setTitle(R.string.data_usage_warning_editor_title);
2082 builder.setView(view);
2083
2084 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2085 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002086 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002087 public void onClick(DialogInterface dialog, int which) {
2088 // clear focus to finish pending text edits
2089 bytesPicker.clearFocus();
2090
2091 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
2092 editor.setPolicyWarningBytes(template, bytes);
2093 target.updatePolicy(false);
2094 }
2095 });
2096
2097 return builder.create();
2098 }
2099 }
2100
2101 /**
2102 * Dialog to edit {@link NetworkPolicy#limitBytes}.
2103 */
2104 public static class LimitEditorFragment extends DialogFragment {
2105 private static final String EXTRA_TEMPLATE = "template";
2106
2107 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002108 if (!parent.isAdded()) return;
2109
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002110 final Bundle args = new Bundle();
2111 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
2112
2113 final LimitEditorFragment dialog = new LimitEditorFragment();
2114 dialog.setArguments(args);
2115 dialog.setTargetFragment(parent, 0);
2116 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
2117 }
2118
2119 @Override
2120 public Dialog onCreateDialog(Bundle savedInstanceState) {
2121 final Context context = getActivity();
2122 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2123 final NetworkPolicyEditor editor = target.mPolicyEditor;
2124
2125 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2126 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2127
2128 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
2129 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
2130
2131 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2132 final long warningBytes = editor.getPolicyWarningBytes(template);
2133 final long limitBytes = editor.getPolicyLimitBytes(template);
2134
2135 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09002136 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002137 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
2138 } else {
2139 bytesPicker.setMinValue(0);
2140 }
2141 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
2142 bytesPicker.setWrapSelectorWheel(false);
2143
2144 builder.setTitle(R.string.data_usage_limit_editor_title);
2145 builder.setView(view);
2146
2147 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2148 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002149 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002150 public void onClick(DialogInterface dialog, int which) {
2151 // clear focus to finish pending text edits
2152 bytesPicker.clearFocus();
2153
2154 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
2155 editor.setPolicyLimitBytes(template, bytes);
2156 target.updatePolicy(false);
2157 }
2158 });
2159
2160 return builder.create();
2161 }
2162 }
2163 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07002164 * Dialog to request user confirmation before disabling data.
2165 */
2166 public static class ConfirmDataDisableFragment extends DialogFragment {
Sanket Padawe24f834d2015-01-08 11:23:11 -08002167 static int mSubId;
2168 public static void show(DataUsageSummary parent, int subId) {
2169 mSubId = subId;
Jeff Sharkey461842a2011-09-25 18:22:48 -07002170 if (!parent.isAdded()) return;
2171
Jeff Sharkey28130d92011-09-02 16:10:24 -07002172 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
2173 dialog.setTargetFragment(parent, 0);
2174 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
2175 }
2176
2177 @Override
2178 public Dialog onCreateDialog(Bundle savedInstanceState) {
2179 final Context context = getActivity();
2180
2181 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2182 builder.setMessage(R.string.data_usage_disable_mobile);
2183
2184 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002185 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07002186 public void onClick(DialogInterface dialog, int which) {
2187 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2188 if (target != null) {
2189 // TODO: extend to modify policy enabled flag.
Sanket Padawe24f834d2015-01-08 11:23:11 -08002190 target.setMobileDataEnabled(mSubId, false);
Jeff Sharkey28130d92011-09-02 16:10:24 -07002191 }
2192 }
2193 });
2194 builder.setNegativeButton(android.R.string.cancel, null);
2195
2196 return builder.create();
2197 }
2198 }
2199
2200 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002201 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07002202 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002203 */
2204 public static class ConfirmRestrictFragment extends DialogFragment {
2205 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002206 if (!parent.isAdded()) return;
2207
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002208 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
2209 dialog.setTargetFragment(parent, 0);
2210 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
2211 }
2212
2213 @Override
2214 public Dialog onCreateDialog(Bundle savedInstanceState) {
2215 final Context context = getActivity();
2216
2217 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002218 builder.setTitle(R.string.data_usage_restrict_background_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07002219 if (Utils.hasMultipleUsers(context)) {
2220 builder.setMessage(R.string.data_usage_restrict_background_multiuser);
2221 } else {
2222 builder.setMessage(R.string.data_usage_restrict_background);
2223 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002224
2225 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002226 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002227 public void onClick(DialogInterface dialog, int which) {
2228 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2229 if (target != null) {
2230 target.setRestrictBackground(true);
2231 }
2232 }
2233 });
2234 builder.setNegativeButton(android.R.string.cancel, null);
2235
2236 return builder.create();
2237 }
2238 }
2239
2240 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002241 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
2242 * change has been denied, usually based on
2243 * {@link DataUsageSummary#hasLimitedNetworks()}.
2244 */
2245 public static class DeniedRestrictFragment extends DialogFragment {
2246 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002247 if (!parent.isAdded()) return;
2248
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002249 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
2250 dialog.setTargetFragment(parent, 0);
2251 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
2252 }
2253
2254 @Override
2255 public Dialog onCreateDialog(Bundle savedInstanceState) {
2256 final Context context = getActivity();
2257
2258 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2259 builder.setTitle(R.string.data_usage_app_restrict_background);
2260 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
2261 builder.setPositiveButton(android.R.string.ok, null);
2262
2263 return builder.create();
2264 }
2265 }
2266
2267 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002268 * Dialog to request user confirmation before setting
2269 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
2270 */
2271 public static class ConfirmAppRestrictFragment extends DialogFragment {
2272 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002273 if (!parent.isAdded()) return;
2274
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002275 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2276 dialog.setTargetFragment(parent, 0);
2277 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2278 }
2279
2280 @Override
2281 public Dialog onCreateDialog(Bundle savedInstanceState) {
2282 final Context context = getActivity();
2283
2284 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002285 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2286 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2287
2288 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002289 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002290 public void onClick(DialogInterface dialog, int which) {
2291 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2292 if (target != null) {
2293 target.setAppRestrictBackground(true);
2294 }
2295 }
2296 });
2297 builder.setNegativeButton(android.R.string.cancel, null);
2298
2299 return builder.create();
2300 }
2301 }
2302
2303 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002304 * Compute default tab that should be selected, based on
2305 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2306 */
2307 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002308 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002309 if (template == null) {
Wink Savilleb003a852014-10-23 10:16:26 -07002310 final int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
Wink Saville0183fb52014-11-22 10:11:39 -08002311 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Savilled72c0622014-12-11 10:10:05 -08002312 if (SubscriptionManager.isValidSubscriptionId(subId)) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07002313 return TAB_MOBILE + String.valueOf(subId);
2314 }
2315 return null;
2316 }
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002317
2318 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002319 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002320 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002321 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002322 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002323 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002324 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002325 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002326 return TAB_WIFI;
2327 default:
2328 return null;
2329 }
2330 }
2331
2332 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002333 * Background task that loads {@link UidDetail}, binding to
2334 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002335 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002336 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2337 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002338 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002339 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002340
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002341 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002342 mProvider = checkNotNull(provider);
2343 mItem = checkNotNull(item);
2344 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002345 }
2346
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002347 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002348 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002349 final UidDetailTask existing = (UidDetailTask) target.getTag();
2350 if (existing != null) {
2351 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002352 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002353
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002354 final UidDetail cachedDetail = provider.getUidDetail(item.key, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002355 if (cachedDetail != null) {
2356 bindView(cachedDetail, target);
2357 } else {
2358 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2359 AsyncTask.THREAD_POOL_EXECUTOR));
2360 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002361 }
2362
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002363 private static void bindView(UidDetail detail, View target) {
2364 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2365 final TextView title = (TextView) target.findViewById(android.R.id.title);
2366
2367 if (detail != null) {
2368 icon.setImageDrawable(detail.icon);
2369 title.setText(detail.label);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +01002370 title.setContentDescription(detail.contentDescription);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002371 } else {
2372 icon.setImageDrawable(null);
2373 title.setText(null);
2374 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002375 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002376
2377 @Override
2378 protected void onPreExecute() {
2379 bindView(null, mTarget);
2380 }
2381
2382 @Override
2383 protected UidDetail doInBackground(Void... params) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002384 return mProvider.getUidDetail(mItem.key, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002385 }
2386
2387 @Override
2388 protected void onPostExecute(UidDetail result) {
2389 bindView(result, mTarget);
2390 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002391 }
2392
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002393 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002394 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002395 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002396 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002397 if (TEST_RADIOS) {
2398 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2399 }
2400
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002401 final ConnectivityManager conn = ConnectivityManager.from(context);
2402 final TelephonyManager tele = TelephonyManager.from(context);
2403
Wink Saville0183fb52014-11-22 10:11:39 -08002404 final List<SubscriptionInfo> subInfoList =
2405 SubscriptionManager.from(context).getActiveSubscriptionInfoList();
2406 // No activated Subscriptions
PauloftheWest50e6eca2014-10-03 11:07:14 -07002407 if (subInfoList == null) {
Wink Savillefcec91f2014-12-02 17:12:08 -08002408 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subInfoList=null");
PauloftheWest50e6eca2014-10-03 11:07:14 -07002409 return false;
2410 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002411 // require both supported network and ready SIM
PauloftheWest50e6eca2014-10-03 11:07:14 -07002412 boolean isReady = true;
Wink Savilleca756612014-11-08 10:47:12 -08002413 for (SubscriptionInfo subInfo : subInfoList) {
Stuart Scott3ada2ec2014-10-31 14:11:24 -07002414 isReady = isReady & tele.getSimState(subInfo.getSimSlotIndex()) == SIM_STATE_READY;
Wink Savillefcec91f2014-12-02 17:12:08 -08002415 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subInfo=" + subInfo);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002416 }
Wink Savillefcec91f2014-12-02 17:12:08 -08002417 boolean retVal = conn.isNetworkSupported(TYPE_MOBILE) && isReady;
2418 if (LOGD) {
2419 Log.d(TAG, "hasReadyMobileRadio:"
2420 + " conn.isNetworkSupported(TYPE_MOBILE)="
2421 + conn.isNetworkSupported(TYPE_MOBILE)
2422 + " isReady=" + isReady);
2423 }
2424 return retVal;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002425 }
2426
2427 /*
2428 * TODO: consider adding to TelephonyManager or SubscritpionManager.
2429 */
Wink Savilleb003a852014-10-23 10:16:26 -07002430 public static boolean hasReadyMobileRadio(Context context, int subId) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07002431 if (TEST_RADIOS) {
2432 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2433 }
2434
2435 final ConnectivityManager conn = ConnectivityManager.from(context);
2436 final TelephonyManager tele = TelephonyManager.from(context);
2437 final int slotId = SubscriptionManager.getSlotId(subId);
2438 final boolean isReady = tele.getSimState(slotId) == SIM_STATE_READY;
2439
Wink Savillefcec91f2014-12-02 17:12:08 -08002440 boolean retVal = conn.isNetworkSupported(TYPE_MOBILE) && isReady;
2441 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subId=" + subId
2442 + " conn.isNetworkSupported(TYPE_MOBILE)=" + conn.isNetworkSupported(TYPE_MOBILE)
2443 + " isReady=" + isReady);
2444 return retVal;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002445 }
2446
2447 /**
2448 * Test if device has a mobile 4G data radio.
2449 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002450 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002451 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2452 return false;
2453 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002454 if (TEST_RADIOS) {
2455 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2456 }
2457
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002458 final ConnectivityManager conn = ConnectivityManager.from(context);
2459 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002460
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002461 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Wink Saville55434042012-06-14 12:33:43 -07002462 final boolean hasLte = (tele.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE)
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002463 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002464 return hasWimax || hasLte;
2465 }
2466
2467 /**
2468 * Test if device has a Wi-Fi data radio.
2469 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002470 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002471 if (TEST_RADIOS) {
2472 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2473 }
2474
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002475 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002476 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002477 }
2478
2479 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002480 * Test if device has an ethernet network connection.
2481 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002482 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002483 if (TEST_RADIOS) {
2484 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2485 }
2486
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002487 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002488 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2489
2490 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002491 if (mStatsSession != null) {
2492 try {
2493 ethernetBytes = mStatsSession.getSummaryForNetwork(
2494 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2495 .getTotalBytes();
2496 } catch (RemoteException e) {
2497 throw new RuntimeException(e);
2498 }
2499 } else {
2500 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002501 }
2502
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002503 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002504 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002505 }
2506
2507 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002508 * Inflate a {@link Preference} style layout, adding the given {@link View}
2509 * widget into {@link android.R.id#widget_frame}.
2510 */
2511 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2512 final View view = inflater.inflate(R.layout.preference, root, false);
2513 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2514 android.R.id.widget_frame);
2515 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2516 return view;
2517 }
2518
Jeff Sharkeyb6548462014-07-21 15:38:06 -07002519 private static View inflateCategoryHeader(LayoutInflater inflater, ViewGroup root) {
2520 final TypedArray a = inflater.getContext().obtainStyledAttributes(null,
2521 com.android.internal.R.styleable.Preference,
2522 com.android.internal.R.attr.preferenceCategoryStyle, 0);
2523 final int resId = a.getResourceId(com.android.internal.R.styleable.Preference_layout, 0);
2524 return inflater.inflate(resId, root, false);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002525 }
2526
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002527 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002528 * Test if any networks are currently limited.
2529 */
2530 private boolean hasLimitedNetworks() {
2531 return !buildLimitedNetworksList().isEmpty();
2532 }
2533
2534 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002535 * Build string describing currently limited networks, which defines when
2536 * background data is restricted.
2537 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002538 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002539 private CharSequence buildLimitedNetworksString() {
2540 final List<CharSequence> limited = buildLimitedNetworksList();
2541
2542 // handle case where no networks limited
2543 if (limited.isEmpty()) {
2544 limited.add(getText(R.string.data_usage_list_none));
2545 }
2546
2547 return TextUtils.join(limited);
2548 }
2549
2550 /**
2551 * Build list of currently limited networks, which defines when background
2552 * data is restricted.
2553 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002554 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002555 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002556 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002557
2558 // build combined list of all limited networks
2559 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002560
2561 final TelephonyManager tele = TelephonyManager.from(context);
2562 if (tele.getSimState() == SIM_STATE_READY) {
2563 final String subscriberId = getActiveSubscriberId(context);
2564 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2565 limited.add(getText(R.string.data_usage_list_mobile));
2566 }
2567 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2568 limited.add(getText(R.string.data_usage_tab_3g));
2569 }
2570 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2571 limited.add(getText(R.string.data_usage_tab_4g));
2572 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002573 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002574
2575 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002576 limited.add(getText(R.string.data_usage_tab_wifi));
2577 }
2578 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2579 limited.add(getText(R.string.data_usage_tab_ethernet));
2580 }
2581
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002582 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002583 }
2584
2585 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002586 * Inset both selector and divider {@link Drawable} on the given
2587 * {@link ListView} by the requested dimensions.
2588 */
2589 private static void insetListViewDrawables(ListView view, int insetSide) {
2590 final Drawable selector = view.getSelector();
2591 final Drawable divider = view.getDivider();
2592
2593 // fully unregister these drawables so callbacks can be maintained after
2594 // wrapping below.
2595 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2596 view.setSelector(stub);
2597 view.setDivider(stub);
2598
2599 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2600 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2601 }
2602
2603 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002604 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002605 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002606 */
2607 private static void setPreferenceTitle(View parent, int resId) {
2608 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2609 title.setText(resId);
2610 }
2611
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002612 /**
2613 * Set {@link android.R.id#summary} for a preference view inflated with
2614 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2615 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002616 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002617 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2618 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002619 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002620 }
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002621
2622 /**
2623 * For search
2624 */
2625 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -07002626 new BaseSearchIndexProvider() {
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002627 @Override
2628 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
2629 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
2630
2631 final Resources res = context.getResources();
2632
2633 // Add fragment title
2634 SearchIndexableRaw data = new SearchIndexableRaw(context);
2635 data.title = res.getString(R.string.data_usage_summary_title);
2636 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2637 result.add(data);
2638
2639 // Mobile data
2640 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002641 data.key = DATA_USAGE_ENABLE_MOBILE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002642 data.title = res.getString(R.string.data_usage_enable_mobile);
2643 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2644 result.add(data);
2645
2646 // Set mobile data limit
2647 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002648 data.key = DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002649 data.title = res.getString(R.string.data_usage_disable_mobile_limit);
2650 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2651 result.add(data);
2652
Fabrice Di Megliof2a52262014-04-17 17:20:27 -07002653 // Data usage cycle
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002654 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002655 data.key = DATA_USAGE_CYCLE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002656 data.title = res.getString(R.string.data_usage_cycle);
2657 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2658 result.add(data);
2659
2660 return result;
2661 }
2662 };
2663
PauloftheWestc80b7612014-11-25 04:40:45 -08002664 private void addMobileTab(Context context, SubscriptionInfo subInfo) {
2665 if (subInfo != null && mMobileTagMap != null) {
2666 if (hasReadyMobileRadio(context, subInfo.getSubscriptionId())) {
2667 mTabHost.addTab(buildTabSpec(mMobileTagMap.get(subInfo.getSubscriptionId()),
2668 subInfo.getDisplayName()));
PauloftheWest50e6eca2014-10-03 11:07:14 -07002669 }
Wink Savillefcec91f2014-12-02 17:12:08 -08002670 } else {
2671 if (LOGD) Log.d(TAG, "addMobileTab: subInfoList is null");
PauloftheWest50e6eca2014-10-03 11:07:14 -07002672 }
2673 }
2674
Wink Savilleca756612014-11-08 10:47:12 -08002675 private SubscriptionInfo getCurrentTabSubInfo(Context context) {
PauloftheWestde338442014-10-29 14:46:44 -07002676 if (mSubInfoList != null && mTabHost != null) {
2677 final int currentTagIndex = mTabHost.getCurrentTab();
2678 int i = 0;
Wink Savilleca756612014-11-08 10:47:12 -08002679 for (SubscriptionInfo subInfo : mSubInfoList) {
Stuart Scott3ada2ec2014-10-31 14:11:24 -07002680 if (hasReadyMobileRadio(context, subInfo.getSubscriptionId())) {
PauloftheWestde338442014-10-29 14:46:44 -07002681 if (i++ == currentTagIndex) {
2682 return subInfo;
2683 }
2684 }
2685 }
2686 }
2687 return null;
2688 }
2689
PauloftheWest50e6eca2014-10-03 11:07:14 -07002690 /**
2691 * Init a map with subId key and mobile tag name
2692 * @param subInfoList The subscription Info List
2693 * @return The map or null if no activated subscription
2694 */
Wink Savilleca756612014-11-08 10:47:12 -08002695 private Map<Integer, String> initMobileTabTag(List<SubscriptionInfo> subInfoList) {
Wink Savilleb003a852014-10-23 10:16:26 -07002696 Map<Integer, String> map = null;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002697 if (subInfoList != null) {
2698 String mobileTag;
Wink Savilleb003a852014-10-23 10:16:26 -07002699 map = new HashMap<Integer, String>();
Wink Savillefcec91f2014-12-02 17:12:08 -08002700 for (SubscriptionInfo subInfo : subInfoList) {
2701 mobileTag = TAB_MOBILE + String.valueOf(subInfo.getSubscriptionId());
2702 map.put(subInfo.getSubscriptionId(), mobileTag);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002703 }
2704 }
2705 return map;
2706 }
2707
2708 private static boolean isMobileTab(String currentTab) {
2709 return currentTab != null ? currentTab.contains(TAB_MOBILE) : false;
2710 }
2711
Wink Savilleb003a852014-10-23 10:16:26 -07002712 private int getSubId(String currentTab) {
Wink Saville0183fb52014-11-22 10:11:39 -08002713 if (mMobileTagMap != null) {
2714 Set<Integer> set = mMobileTagMap.keySet();
2715 for (Integer subId : set) {
2716 if (mMobileTagMap.get(subId).equals(currentTab)) {
2717 return subId;
2718 }
PauloftheWest50e6eca2014-10-03 11:07:14 -07002719 }
2720 }
2721 Log.e(TAG, "currentTab = " + currentTab + " non mobile tab called this function");
2722 return -1;
2723 }
2724
2725 //SUB SELECT
PauloftheWest98523c82014-10-28 08:30:05 -07002726 private boolean isMobileDataAvailable(long subId) {
Wink Savilleb003a852014-10-23 10:16:26 -07002727 int[] subIds = SubscriptionManager.getSubId(PhoneConstants.SUB1);
PauloftheWest98523c82014-10-28 08:30:05 -07002728 if (subIds != null && subIds[0] == subId) {
2729 return true;
2730 }
2731
2732 subIds = SubscriptionManager.getSubId(PhoneConstants.SUB2);
2733 if (subIds != null && subIds[0] == subId) {
2734 return true;
2735 }
2736
2737 subIds = SubscriptionManager.getSubId(PhoneConstants.SUB3);
2738 if (subIds != null && subIds[0] == subId) {
2739 return true;
2740 }
2741 return false;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002742 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002743}