blob: 1c17ce5cbb461708b7351cdd7c33720573ba0506 [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
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700282 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700283 public void onCreate(Bundle savedInstanceState) {
284 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700285 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700286
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700287 mNetworkService = INetworkManagementService.Stub.asInterface(
288 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700289 mStatsService = INetworkStatsService.Stub.asInterface(
290 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700291 mPolicyManager = NetworkPolicyManager.from(context);
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700292 mTelephonyManager = TelephonyManager.from(context);
Wink Saville0183fb52014-11-22 10:11:39 -0800293 mSubscriptionManager = SubscriptionManager.from(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700294
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700295 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700296
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700297 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700298 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700299
Wink Saville0183fb52014-11-22 10:11:39 -0800300 mSubInfoList = mSubscriptionManager.getActiveSubscriptionInfoList();
PauloftheWest50e6eca2014-10-03 11:07:14 -0700301 mMobileTagMap = initMobileTabTag(mSubInfoList);
302
Jaewan Kimffce9c12013-03-19 16:53:45 +0900303 try {
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700304 if (!mNetworkService.isBandwidthControlEnabled()) {
305 Log.w(TAG, "No bandwidth control; leaving");
306 getActivity().finish();
307 }
308 } catch (RemoteException e) {
309 Log.w(TAG, "No bandwidth control; leaving");
310 getActivity().finish();
311 }
312
313 try {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900314 mStatsSession = mStatsService.openSession();
315 } catch (RemoteException e) {
316 throw new RuntimeException(e);
317 }
318
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700319 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700320 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700321
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700322 // override preferences when no mobile radio
323 if (!hasReadyMobileRadio(context)) {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900324 mShowWifi = true;
325 mShowEthernet = true;
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700326 }
327
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700328 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700329 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700330
Jeff Sharkey8a503642011-06-10 13:31:21 -0700331 @Override
332 public View onCreateView(LayoutInflater inflater, ViewGroup container,
333 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700334
Jeff Sharkey8a503642011-06-10 13:31:21 -0700335 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700336 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
337
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700338 mUidDetailProvider = new UidDetailProvider(context);
339
Jeff Sharkey8a503642011-06-10 13:31:21 -0700340 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700341 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700342 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
343 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700344
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700345 // decide if we need to manually inset our content, or if we should rely
346 // on parent container for inset.
347 final boolean shouldInset = mListView.getScrollBarStyle()
348 == View.SCROLLBARS_OUTSIDE_OVERLAY;
Amith Yamasani56f51a82013-08-05 10:07:23 -0700349 mInsetSide = 0;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700350
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700351 // adjust padding around tabwidget as needed
Amith Yamasani56f51a82013-08-05 10:07:23 -0700352 prepareCustomPreferencesList(container, view, mListView, false);
Jeff Sharkey5d706792011-09-08 18:57:17 -0700353
Jeff Sharkey8a503642011-06-10 13:31:21 -0700354 mTabHost.setup();
355 mTabHost.setOnTabChangedListener(mTabListener);
356
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700357 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700358 mHeader.setClickable(true);
359
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700360 mListView.addHeaderView(new View(context), null, true);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700361 mListView.addHeaderView(mHeader, null, true);
362 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700363
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700364 if (mInsetSide > 0) {
365 // inset selector and divider drawables
366 insetListViewDrawables(mListView, mInsetSide);
Fabrice Di Megliob27223f2013-01-15 18:54:11 -0800367 mHeader.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700368 }
369
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700370 {
371 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700372 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
373 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700374 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700375
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700376 mDataEnabled = new Switch(inflater.getContext());
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700377 mDataEnabled.setClickable(false);
378 mDataEnabled.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700379 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700380 mDataEnabledView.setTag(R.id.preference_highlight_key,
381 DATA_USAGE_ENABLE_MOBILE_KEY);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700382 mDataEnabledView.setClickable(true);
383 mDataEnabledView.setFocusable(true);
384 mDataEnabledView.setOnClickListener(mDataEnabledListener);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700385 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700386
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700387 mDisableAtLimit = new Switch(inflater.getContext());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700388 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700389 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700390 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700391 mDisableAtLimitView.setTag(R.id.preference_highlight_key,
392 DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700393 mDisableAtLimitView.setClickable(true);
394 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700395 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
396 mNetworkSwitches.addView(mDisableAtLimitView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700397
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700398 mCycleView = inflater.inflate(R.layout.data_usage_cycles, mNetworkSwitches, false);
399 mCycleView.setTag(R.id.preference_highlight_key, DATA_USAGE_CYCLE_KEY);
400 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
401 mCycleAdapter = new CycleAdapter(context);
402 mCycleSpinner.setAdapter(mCycleAdapter);
403 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
404 mCycleSummary = (TextView) mCycleView.findViewById(R.id.cycle_summary);
405 mNetworkSwitches.addView(mCycleView);
PauloftheWestc80b7612014-11-25 04:40:45 -0800406 mSeries = (ChartNetworkSeriesView)view.findViewById(R.id.series);
407 mDetailedSeries = (ChartNetworkSeriesView)view.findViewById(R.id.detail_series);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700408 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700409
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700410 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700411 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800412 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700413
414 {
415 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700416 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700417 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
418 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700419 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
420 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700421 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700422
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700423 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700424
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700425 mAppRestrict = new Switch(inflater.getContext());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700426 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700427 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700428 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700429 mAppRestrictView.setClickable(true);
430 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700431 mAppRestrictView.setOnClickListener(mAppRestrictListener);
432 mAppSwitches.addView(mAppRestrictView);
433 }
434
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700435 mDisclaimer = mHeader.findViewById(R.id.disclaimer);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700436 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700437 mStupidPadding = mHeader.findViewById(R.id.stupid_padding);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700438
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +0100439 final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
440 mAdapter = new DataUsageAdapter(um, mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700441 mListView.setOnItemClickListener(mListListener);
442 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700443
444 return view;
445 }
446
447 @Override
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700448 public void onViewStateRestored(Bundle savedInstanceState) {
449 super.onViewStateRestored(savedInstanceState);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700450
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700451 // pick default tab based on incoming intent
452 final Intent intent = getActivity().getIntent();
453 mIntentTab = computeTabFromIntent(intent);
454
Jeff Sharkey8a503642011-06-10 13:31:21 -0700455 // this kicks off chain reaction which creates tabs, binds the body to
456 // selected network, and binds chart, cycles and detail list.
457 updateTabs();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700458 }
459
460 @Override
461 public void onResume() {
462 super.onResume();
463
464 getView().post(new Runnable() {
465 @Override
466 public void run() {
467 highlightViewIfNeeded();
468 }
469 });
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700470
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700471 // kick off background task to update stats
472 new AsyncTask<Void, Void, Void>() {
473 @Override
474 protected Void doInBackground(Void... params) {
475 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700476 // wait a few seconds before kicking off
477 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700478 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700479 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700480 } catch (RemoteException e) {
481 }
482 return null;
483 }
484
485 @Override
486 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700487 if (isAdded()) {
488 updateBody();
489 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700490 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700491 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700492 }
493
Jeff Sharkey8a503642011-06-10 13:31:21 -0700494 @Override
495 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
496 inflater.inflate(R.menu.data_usage, menu);
497 }
498
499 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700500 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700501 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700502 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700503 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700504
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700505 mMenuShowWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700506 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
507 mMenuShowWifi.setVisible(!appDetailMode);
508 } else {
509 mMenuShowWifi.setVisible(false);
510 }
511
512 mMenuShowEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
513 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
514 mMenuShowEthernet.setVisible(!appDetailMode);
515 } else {
516 mMenuShowEthernet.setVisible(false);
517 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700518
519 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700520 mMenuRestrictBackground.setVisible(
521 hasReadyMobileRadio(context) && isOwner && !appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700522
523 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
524 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700525 metered.setVisible(!appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700526 } else {
527 metered.setVisible(false);
528 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700529
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700530 // TODO: show when multiple sims available
531 mMenuSimCards = menu.findItem(R.id.data_usage_menu_sim_cards);
532 mMenuSimCards.setVisible(false);
533
534 mMenuCellularNetworks = menu.findItem(R.id.data_usage_menu_cellular_networks);
PauloftheWesta4b8fb32014-09-18 10:12:25 -0700535 mMenuCellularNetworks.setVisible(hasReadyMobileRadio(context)
536 && !appDetailMode && isOwner);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700537
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700538 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
539 String helpUrl;
540 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800541 HelpUtils.prepareHelpMenuItem(context, help, helpUrl);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700542 } else {
543 help.setVisible(false);
544 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700545
546 updateMenuTitles();
547 }
548
549 private void updateMenuTitles() {
550 if (mPolicyManager.getRestrictBackground()) {
551 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_allow_background);
552 } else {
553 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_restrict_background);
554 }
555
556 if (mShowWifi) {
557 mMenuShowWifi.setTitle(R.string.data_usage_menu_hide_wifi);
558 } else {
559 mMenuShowWifi.setTitle(R.string.data_usage_menu_show_wifi);
560 }
561
562 if (mShowEthernet) {
563 mMenuShowEthernet.setTitle(R.string.data_usage_menu_hide_ethernet);
564 } else {
565 mMenuShowEthernet.setTitle(R.string.data_usage_menu_show_ethernet);
566 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700567 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700568
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700569 @Override
570 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700571 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700572 case R.id.data_usage_menu_restrict_background: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700573 final boolean restrictBackground = !mPolicyManager.getRestrictBackground();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700574 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800575 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700576 } else {
577 // no confirmation to drop restriction
578 setRestrictBackground(false);
579 }
580 return true;
581 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700582 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700583 mShowWifi = !mShowWifi;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700584 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700585 updateMenuTitles();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700586 updateTabs();
587 return true;
588 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700589 case R.id.data_usage_menu_show_ethernet: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700590 mShowEthernet = !mShowEthernet;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700591 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700592 updateMenuTitles();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700593 updateTabs();
594 return true;
595 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700596 case R.id.data_usage_menu_sim_cards: {
597 // TODO: hook up to sim cards
598 return true;
599 }
600 case R.id.data_usage_menu_cellular_networks: {
601 final Intent intent = new Intent(Intent.ACTION_MAIN);
602 intent.setComponent(new ComponentName("com.android.phone",
603 "com.android.phone.MobileNetworkSettings"));
604 startActivity(intent);
605 return true;
606 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700607 case R.id.data_usage_menu_metered: {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800608 final SettingsActivity sa = (SettingsActivity) getActivity();
609 sa.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700610 R.string.data_usage_metered_title, null, this, 0);
611 return true;
612 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700613 }
614 return false;
615 }
616
Jeff Sharkey94a90952011-06-13 22:31:09 -0700617 @Override
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700618 public void onDestroy() {
Jeff Sharkey94a90952011-06-13 22:31:09 -0700619 mDataEnabledView = null;
620 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700621
622 mUidDetailProvider.clearCache();
623 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700624
625 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700626
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800627 super.onDestroy();
628 }
629
Jeff Sharkey8a503642011-06-10 13:31:21 -0700630 /**
Jeff Sharkey92811822012-05-04 11:47:29 -0700631 * Build and assign {@link LayoutTransition} to various containers. Should
632 * only be assigned after initial layout is complete.
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700633 */
Jeff Sharkey92811822012-05-04 11:47:29 -0700634 private void ensureLayoutTransitions() {
635 // skip when already setup
636 if (mChart.getLayoutTransition() != null) return;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700637
Jeff Sharkey92811822012-05-04 11:47:29 -0700638 mTabsContainer.setLayoutTransition(buildLayoutTransition());
639 mHeader.setLayoutTransition(buildLayoutTransition());
640 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700641
Jeff Sharkey92811822012-05-04 11:47:29 -0700642 final LayoutTransition chartTransition = buildLayoutTransition();
643 chartTransition.disableTransitionType(LayoutTransition.APPEARING);
644 chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
645 mChart.setLayoutTransition(chartTransition);
646 }
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700647
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700648 private static LayoutTransition buildLayoutTransition() {
649 final LayoutTransition transition = new LayoutTransition();
650 if (TEST_ANIM) {
651 transition.setDuration(1500);
652 }
653 transition.setAnimateParentHierarchy(false);
654 return transition;
655 }
656
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700657 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700658 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700659 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
660 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700661 */
662 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700663 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700664 mTabHost.clearAllTabs();
665
PauloftheWestc80b7612014-11-25 04:40:45 -0800666 for (int i = 0; i < mTelephonyManager.getSimCount(); i++) {
667 final SubscriptionInfo sir = Utils.findRecordBySlotId(context, i);
668 if (sir != null) {
669 addMobileTab(context, sir);
670 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700671 }
PauloftheWestc80b7612014-11-25 04:40:45 -0800672
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700673 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700674 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
675 }
PauloftheWestc80b7612014-11-25 04:40:45 -0800676
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700677 if (mShowEthernet && hasEthernet(context)) {
678 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
679 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700680
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700681 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700682 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
683 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
684 if (mIntentTab != null) {
685 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700686 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700687 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700688 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700689 mTabHost.setCurrentTabByTag(mIntentTab);
690 }
691 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700692 } else if (noTabs) {
693 // no usable tabs, so hide body
694 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700695 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700696 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700697 }
698 }
699
Jeff Sharkey8a503642011-06-10 13:31:21 -0700700 /**
701 * Factory that provide empty {@link View} to make {@link TabHost} happy.
702 */
703 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700704 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700705 public View createTabContent(String tag) {
706 return new View(mTabHost.getContext());
707 }
708 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700709
Jeff Sharkey8a503642011-06-10 13:31:21 -0700710 /**
711 * Build {@link TabSpec} with thin indicator, and empty content.
712 */
713 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700714 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
715 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700716 }
717
PauloftheWest50e6eca2014-10-03 11:07:14 -0700718 /**
719 * Build {@link TabSpec} with thin indicator, and empty content.
720 */
Stuart Scottb1531812014-11-04 14:52:23 -0800721 private TabSpec buildTabSpec(String tag, CharSequence title) {
PauloftheWest50e6eca2014-10-03 11:07:14 -0700722 return mTabHost.newTabSpec(tag).setIndicator(title).setContent(
723 mEmptyTabContent);
724 }
725
726
Jeff Sharkey8a503642011-06-10 13:31:21 -0700727 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700728 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700729 public void onTabChanged(String tabId) {
730 // user changed tab; update body
731 updateBody();
732 }
733 };
734
735 /**
736 * Update body content based on current tab. Loads
737 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
738 * binds them to visible controls.
739 */
740 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700741 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700742 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700743
Jeff Sharkeya662e492011-06-18 21:57:06 -0700744 final Context context = getActivity();
PauloftheWestc80b7612014-11-25 04:40:45 -0800745 final Resources resources = context.getResources();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700746 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700747 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700748
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700749 if (currentTab == null) {
750 Log.w(TAG, "no tab selected; hiding body");
751 mListView.setVisibility(View.GONE);
752 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700753 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700754 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700755 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700756
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700757 mCurrentTab = currentTab;
758
Jeff Sharkey8a503642011-06-10 13:31:21 -0700759 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
760
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700761 mDataEnabledSupported = isOwner;
762 mDisableAtLimitSupported = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700763
Wink Savillefcec91f2014-12-02 17:12:08 -0800764 // TODO: remove mobile tabs when SIM isn't ready probably by
765 // TODO: using SubscriptionManager.getActiveSubscriptionInfoList.
766 if (LOGD) Log.d(TAG, "updateBody() isMobileTab=" + isMobileTab(currentTab));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700767
PauloftheWest50e6eca2014-10-03 11:07:14 -0700768 if (isMobileTab(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800769 if (LOGD) Log.d(TAG, "updateBody() mobile tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700770 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
771 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
PauloftheWest50e6eca2014-10-03 11:07:14 -0700772 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context,getSubId(currentTab)));
773 mDataEnabledSupported = isMobileDataAvailable(getSubId(currentTab));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700774 } else if (TAB_3G.equals(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800775 if (LOGD) Log.d(TAG, "updateBody() 3g tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700776 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
777 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
778 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700779 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700780
781 } else if (TAB_4G.equals(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800782 if (LOGD) Log.d(TAG, "updateBody() 4g tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700783 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
784 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
785 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700786 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700787
788 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700789 // wifi doesn't have any controls
Wink Savillefcec91f2014-12-02 17:12:08 -0800790 if (LOGD) Log.d(TAG, "updateBody() wifi tab");
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700791 mDataEnabledSupported = false;
792 mDisableAtLimitSupported = false;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700793 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700794
795 } else if (TAB_ETHERNET.equals(currentTab)) {
796 // ethernet doesn't have any controls
Wink Savillefcec91f2014-12-02 17:12:08 -0800797 if (LOGD) Log.d(TAG, "updateBody() ethernet tab");
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700798 mDataEnabledSupported = false;
799 mDisableAtLimitSupported = false;
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700800 mTemplate = buildTemplateEthernet();
801
802 } else {
Wink Savillefcec91f2014-12-02 17:12:08 -0800803 if (LOGD) Log.d(TAG, "updateBody() unknown tab");
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700804 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700805 }
806
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700807 // kick off loader for network history
808 // TODO: consider chaining two loaders together instead of reloading
809 // network history when showing app detail.
810 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700811 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700812
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700813 // detail mode can change visible menus, invalidate
814 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700815
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700816 mBinding = false;
PauloftheWestc80b7612014-11-25 04:40:45 -0800817
818 int seriesColor = resources.getColor(R.color.sim_noitification);
819 if (mCurrentTab != null && mCurrentTab.length() > TAB_MOBILE.length() ){
820 final int slotId = Integer.parseInt(mCurrentTab.substring(TAB_MOBILE.length(),
821 mCurrentTab.length()));
822 final SubscriptionInfo sir = com.android.settings.Utils.findRecordBySlotId(context,
823 slotId);
824
825 if (sir != null) {
826 seriesColor = sir.getIconTint();
827 }
828 }
829
830 mSeries.setChartColor(Color.BLACK, seriesColor, seriesColor);
831 mDetailedSeries.setChartColor(Color.BLACK, seriesColor, seriesColor);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700832 }
833
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700834 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700835 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700836 }
837
838 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700839 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700840 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700841 */
842 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700843 final Context context = getActivity();
844 final PackageManager pm = context.getPackageManager();
845 final LayoutInflater inflater = getActivity().getLayoutInflater();
846
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700847 if (isAppDetailMode()) {
848 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700849 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700850 } else {
851 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700852 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700853
854 // hide detail stats when not in detail mode
855 mChart.bindDetailNetworkStats(null);
856 return;
857 }
858
859 // remove warning/limit sweeps while in detail mode
860 mChart.bindNetworkPolicy(null);
861
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700862 // show icon and all labels appearing under this app
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700863 final int uid = mCurrentApp.key;
864 final UidDetail detail = mUidDetailProvider.getUidDetail(uid, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700865 mAppIcon.setImageDrawable(detail.icon);
866
867 mAppTitles.removeAllViews();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700868
869 View title = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700870 if (detail.detailLabels != null) {
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100871 final int n = detail.detailLabels.length;
872 for (int i = 0; i < n; ++i) {
873 CharSequence label = detail.detailLabels[i];
874 CharSequence contentDescription = detail.detailContentDescriptions[i];
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700875 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100876 TextView appTitle = (TextView) title.findViewById(R.id.app_title);
877 appTitle.setText(label);
878 appTitle.setContentDescription(contentDescription);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700879 mAppTitles.addView(title);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700880 }
881 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700882 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100883 TextView appTitle = (TextView) title.findViewById(R.id.app_title);
884 appTitle.setText(detail.label);
885 appTitle.setContentDescription(detail.contentDescription);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700886 mAppTitles.addView(title);
887 }
888
889 // Remember last slot for summary
890 if (title != null) {
891 mAppTotal = (TextView) title.findViewById(R.id.app_summary);
892 } else {
893 mAppTotal = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700894 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700895
896 // enable settings button when package provides it
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700897 final String[] packageNames = pm.getPackagesForUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700898 if (packageNames != null && packageNames.length > 0) {
899 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700900 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
901
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700902 // Search for match across all packages
903 boolean matchFound = false;
904 for (String packageName : packageNames) {
905 mAppSettingsIntent.setPackage(packageName);
906 if (pm.resolveActivity(mAppSettingsIntent, 0) != null) {
907 matchFound = true;
908 break;
909 }
910 }
911
Zoltan Szatmary-Banf20d39e2014-08-07 15:27:08 +0100912 mAppSettings.setOnClickListener(new OnClickListener() {
913 @Override
914 public void onClick(View v) {
915 if (!isAdded()) {
916 return;
917 }
918
919 // TODO: target towards entire UID instead of just first package
920 getActivity().startActivityAsUser(mAppSettingsIntent,
921 new UserHandle(UserHandle.getUserId(uid)));
922 }
923 });
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700924 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -0700925 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700926
927 } else {
928 mAppSettingsIntent = null;
Zoltan Szatmary-Banf20d39e2014-08-07 15:27:08 +0100929 mAppSettings.setOnClickListener(null);
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700930 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700931 }
932
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700933 updateDetailData();
934
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700935 if (UserHandle.isApp(uid) && !mPolicyManager.getRestrictBackground()
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700936 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700937 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800938 setPreferenceSummary(mAppRestrictView,
939 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700940
941 mAppRestrictView.setVisibility(View.VISIBLE);
942 mAppRestrict.setChecked(getAppRestrictBackground());
943
944 } else {
945 mAppRestrictView.setVisibility(View.GONE);
946 }
947 }
948
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700949 private void setPolicyWarningBytes(long warningBytes) {
950 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700951 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700952 updatePolicy(false);
953 }
954
955 private void setPolicyLimitBytes(long limitBytes) {
956 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700957 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700958 updatePolicy(false);
959 }
960
Jeff Sharkey28130d92011-09-02 16:10:24 -0700961 /**
962 * Local cache of value, used to work around delay when
963 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
964 */
965 private Boolean mMobileDataEnabled;
966
Wink Savilleb003a852014-10-23 10:16:26 -0700967 private boolean isMobileDataEnabled(int subId) {
PauloftheWest50e6eca2014-10-03 11:07:14 -0700968 boolean isEnable = false;
Jeff Sharkey28130d92011-09-02 16:10:24 -0700969 if (mMobileDataEnabled != null) {
970 // TODO: deprecate and remove this once enabled flag is on policy
PauloftheWest50e6eca2014-10-03 11:07:14 -0700971 // Multiple Subscriptions, the value need to be reseted
972 isEnable = mMobileDataEnabled.booleanValue();
973 mMobileDataEnabled = null;
Jeff Sharkey28130d92011-09-02 16:10:24 -0700974 } else {
PauloftheWest50e6eca2014-10-03 11:07:14 -0700975 //SUB SELECT
PauloftheWest98523c82014-10-28 08:30:05 -0700976 isEnable = mTelephonyManager.getDataEnabled()
Wink Saville0183fb52014-11-22 10:11:39 -0800977 && (subId == mSubscriptionManager.getDefaultDataSubId());
Jeff Sharkey28130d92011-09-02 16:10:24 -0700978 }
PauloftheWest50e6eca2014-10-03 11:07:14 -0700979 return isEnable;
Jeff Sharkey28130d92011-09-02 16:10:24 -0700980 }
981
982 private void setMobileDataEnabled(boolean enabled) {
983 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700984 mTelephonyManager.setDataEnabled(enabled);
Jeff Sharkey28130d92011-09-02 16:10:24 -0700985 mMobileDataEnabled = enabled;
986 updatePolicy(false);
987 }
988
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700989 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700990 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked()
991 && ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700992 }
993
994 private boolean isBandwidthControlEnabled() {
995 try {
996 return mNetworkService.isBandwidthControlEnabled();
997 } catch (RemoteException e) {
998 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
999 return false;
1000 }
1001 }
1002
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001003 public void setRestrictBackground(boolean restrictBackground) {
1004 mPolicyManager.setRestrictBackground(restrictBackground);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001005 updateMenuTitles();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001006 }
1007
1008 private boolean getAppRestrictBackground() {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001009 final int uid = mCurrentApp.key;
1010 final int uidPolicy = mPolicyManager.getUidPolicy(uid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001011 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
1012 }
1013
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001014 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001015 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001016 final int uid = mCurrentApp.key;
1017 mPolicyManager.setUidPolicy(
1018 uid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001019 mAppRestrict.setChecked(restrictBackground);
1020 }
1021
Jeff Sharkey8a503642011-06-10 13:31:21 -07001022 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001023 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
1024 * current {@link #mTemplate}.
1025 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001026 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001027 boolean dataEnabledVisible = mDataEnabledSupported;
1028 boolean disableAtLimitVisible = mDisableAtLimitSupported;
1029
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001030 if (isAppDetailMode()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001031 dataEnabledVisible = false;
1032 disableAtLimitVisible = false;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001033 }
1034
Jeff Sharkey28130d92011-09-02 16:10:24 -07001035 // TODO: move enabled state directly into policy
PauloftheWest50e6eca2014-10-03 11:07:14 -07001036 if (isMobileTab(mCurrentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001037 mBinding = true;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001038 mDataEnabled.setChecked(isMobileDataEnabled(getSubId(mCurrentTab)));
Jeff Sharkey28130d92011-09-02 16:10:24 -07001039 mBinding = false;
1040 }
1041
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001042 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001043 //SUB SELECT
1044 if (isNetworkPolicyModifiable(policy) && isMobileDataAvailable(getSubId(mCurrentTab))) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001045 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001046 if (!isAppDetailMode()) {
1047 mChart.bindNetworkPolicy(policy);
1048 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001049
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001050 } else {
1051 // controls are disabled; don't bind warning/limit sweeps
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001052 disableAtLimitVisible = false;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001053 mChart.bindNetworkPolicy(null);
1054 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001055
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001056 mDataEnabledView.setVisibility(dataEnabledVisible ? View.VISIBLE : View.GONE);
1057 mDisableAtLimitView.setVisibility(disableAtLimitVisible ? View.VISIBLE : View.GONE);
1058
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001059 if (refreshCycle) {
1060 // generate cycle list based on policy and available history
1061 updateCycleList(policy);
1062 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001063 }
1064
1065 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001066 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
1067 * and available {@link NetworkStatsHistory} data. Always selects the newest
1068 * item, updating the inspection range on {@link #mChart}.
1069 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001070 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001071 // stash away currently selected cycle to try restoring below
1072 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -07001073 mCycleAdapter.clear();
1074
1075 final Context context = mCycleSpinner.getContext();
1076
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001077 long historyStart = Long.MAX_VALUE;
1078 long historyEnd = Long.MIN_VALUE;
1079 if (mChartData != null) {
1080 historyStart = mChartData.network.getStart();
1081 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001082 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001083
Jeff Sharkey461842a2011-09-25 18:22:48 -07001084 final long now = System.currentTimeMillis();
1085 if (historyStart == Long.MAX_VALUE) historyStart = now;
1086 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001087
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001088 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001089 if (policy != null) {
1090 // find the next cycle boundary
1091 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001092
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001093 // walk backwards, generating all valid cycle ranges
1094 while (cycleEnd > historyStart) {
1095 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
1096 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
1097 + historyStart);
1098 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1099 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001100 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001101 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001102
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001103 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001104 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001105 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001106
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001107 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001108 // no policy defined cycles; show entry for each four-week period
1109 long cycleEnd = historyEnd;
1110 while (cycleEnd > historyStart) {
1111 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
1112 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1113 cycleEnd = cycleStart;
1114 }
1115
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001116 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001117 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001118
1119 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001120 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001121 final int position = mCycleAdapter.findNearestPosition(previousItem);
1122 mCycleSpinner.setSelection(position);
1123
1124 // only force-update cycle when changed; skipping preserves any
1125 // user-defined inspection region.
1126 final CycleItem selectedItem = mCycleAdapter.getItem(position);
1127 if (!Objects.equal(selectedItem, previousItem)) {
1128 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1129 } else {
1130 // but still kick off loader for detailed list
1131 updateDetailData();
1132 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001133 } else {
1134 updateDetailData();
1135 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001136 }
1137
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001138 private View.OnClickListener mDataEnabledListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001139 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001140 public void onClick(View v) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001141 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001142
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001143 final boolean dataEnabled = !mDataEnabled.isChecked();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001144 final String currentTab = mCurrentTab;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001145 if (isMobileTab(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001146 if (dataEnabled) {
PauloftheWestde338442014-10-29 14:46:44 -07001147 // If we are showing the Sim Card tile then we are a Multi-Sim device.
1148 if (Utils.showSimCardTile(getActivity())) {
1149 handleMultiSimDataDialog();
1150 } else {
1151 setMobileDataEnabled(true);
1152 }
Jeff Sharkey28130d92011-09-02 16:10:24 -07001153 } else {
1154 // disabling data; show confirmation dialog which eventually
1155 // calls setMobileDataEnabled() once user confirms.
1156 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1157 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001158 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001159
Jeff Sharkey28130d92011-09-02 16:10:24 -07001160 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001161 }
1162 };
1163
PauloftheWestde338442014-10-29 14:46:44 -07001164 private void handleMultiSimDataDialog() {
Wink Savilleca756612014-11-08 10:47:12 -08001165 final SubscriptionInfo currentSir = getCurrentTabSubInfo(getActivity());
Wink Saville0183fb52014-11-22 10:11:39 -08001166 final SubscriptionInfo nextSir = mSubscriptionManager.getActiveSubscriptionInfo(
1167 mSubscriptionManager.getDefaultDataSubId());
PauloftheWestde338442014-10-29 14:46:44 -07001168
Stuart Scott3ada2ec2014-10-31 14:11:24 -07001169 if (currentSir.getSubscriptionId() == nextSir.getSubscriptionId()) {
PauloftheWestde338442014-10-29 14:46:44 -07001170 setMobileDataEnabled(true);
1171 updateBody();
1172 return;
1173 }
1174
1175 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
1176
1177 builder.setTitle(R.string.sim_change_data_title);
1178 builder.setMessage(getActivity().getResources().getString(R.string.sim_change_data_message,
Stuart Scott3ada2ec2014-10-31 14:11:24 -07001179 currentSir.getDisplayName(), nextSir.getDisplayName()));
PauloftheWestde338442014-10-29 14:46:44 -07001180
1181 builder.setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
1182 @Override
1183 public void onClick(DialogInterface dialog, int id) {
Wink Saville0183fb52014-11-22 10:11:39 -08001184 mSubscriptionManager.setDefaultDataSubId(currentSir.getSubscriptionId());
PauloftheWestde338442014-10-29 14:46:44 -07001185 setMobileDataEnabled(true);
1186 updateBody();
1187 }
1188 });
1189 builder.setNegativeButton(R.string.cancel, null);
1190
1191 builder.create().show();
1192 }
1193
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001194 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001195 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001196 public void onClick(View v) {
1197 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001198 if (disableAtLimit) {
1199 // enabling limit; show confirmation dialog which eventually
1200 // calls setPolicyLimitBytes() once user confirms.
1201 ConfirmLimitFragment.show(DataUsageSummary.this);
1202 } else {
1203 setPolicyLimitBytes(LIMIT_DISABLED);
1204 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001205 }
1206 };
1207
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001208 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001209 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001210 public void onClick(View v) {
1211 final boolean restrictBackground = !mAppRestrict.isChecked();
1212
1213 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001214 // enabling restriction; show confirmation dialog which
1215 // eventually calls setRestrictBackground() once user
1216 // confirms.
1217 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001218 } else {
1219 setAppRestrictBackground(false);
1220 }
1221 }
1222 };
1223
Jeff Sharkey8a503642011-06-10 13:31:21 -07001224 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001225 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001226 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001227 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001228 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001229
1230 // TODO: sigh, remove this hack once we understand 6450986
1231 if (mUidDetailProvider == null || app == null) return;
1232
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001233 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001234 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001235 }
1236 };
1237
1238 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001239 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001240 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1241 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1242 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001243 // show cycle editor; will eventually call setPolicyCycleDay()
1244 // when user finishes editing.
1245 CycleEditorFragment.show(DataUsageSummary.this);
1246
1247 // reset spinner to something other than "change cycle..."
1248 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001249
1250 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001251 if (LOGD) {
1252 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1253 + cycle.end + "]");
1254 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001255
1256 // update chart to show selected cycle, and update detail data
1257 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001258 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001259
1260 updateDetailData();
1261 }
1262 }
1263
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001264 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001265 public void onNothingSelected(AdapterView<?> parent) {
1266 // ignored
1267 }
1268 };
1269
1270 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001271 * Update details based on {@link #mChart} inspection range depending on
1272 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1273 * of applications data usage, and when {@link #isAppDetailMode()} update
1274 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001275 */
1276 private void updateDetailData() {
1277 if (LOGD) Log.d(TAG, "updateDetailData()");
1278
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001279 final long start = mChart.getInspectStart();
1280 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001281 final long now = System.currentTimeMillis();
1282
1283 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001284
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001285 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001286 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001287 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001288 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001289 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001290 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001291 final long foregroundBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001292 final long totalBytes = defaultBytes + foregroundBytes;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001293
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001294 if (mAppTotal != null) {
1295 mAppTotal.setText(Formatter.formatFileSize(context, totalBytes));
1296 }
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001297 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1298 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1299
1300 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001301 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001302
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001303 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001304
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001305 mCycleSummary.setVisibility(View.GONE);
1306
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001307 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001308 if (mChartData != null) {
1309 entry = mChartData.network.getValues(start, end, now, null);
1310 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001311
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001312 mCycleSummary.setVisibility(View.VISIBLE);
1313
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001314 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001315 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001316 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001317 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001318
1319 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1320 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001321 mCycleSummary.setText(totalPhrase);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001322
PauloftheWest50e6eca2014-10-03 11:07:14 -07001323 if (isMobileTab(mCurrentTab) || TAB_3G.equals(mCurrentTab)
László Dávid0b8bf4e2012-10-24 23:31:47 +02001324 || TAB_4G.equals(mCurrentTab)) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001325 if (isAppDetailMode()) {
1326 mDisclaimer.setVisibility(View.GONE);
1327 } else {
1328 mDisclaimer.setVisibility(View.VISIBLE);
1329 }
Jeff Sharkeye557c332012-04-13 16:04:07 -07001330 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001331 mDisclaimer.setVisibility(View.GONE);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001332 }
1333
Jeff Sharkey92811822012-05-04 11:47:29 -07001334 // initial layout is finished above, ensure we have transitions
1335 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001336 }
1337
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001338 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1339 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001340 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001341 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001342 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001343 }
1344
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001345 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001346 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1347 mChartData = data;
1348 mChart.bindNetworkStats(mChartData.network);
1349 mChart.bindDetailNetworkStats(mChartData.detail);
1350
1351 // calcuate policy cycles based on available data
1352 updatePolicy(true);
1353 updateAppDetail();
1354
1355 // force scroll to top of body when showing detail
1356 if (mChartData.detail != null) {
1357 mListView.smoothScrollToPosition(0);
1358 }
1359 }
1360
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001361 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001362 public void onLoaderReset(Loader<ChartData> loader) {
1363 mChartData = null;
1364 mChart.bindNetworkStats(null);
1365 mChart.bindDetailNetworkStats(null);
1366 }
1367 };
1368
1369 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001370 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001371 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001372 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001373 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001374 }
1375
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001376 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001377 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001378 final int[] restrictedUids = mPolicyManager.getUidsWithPolicy(
Jeff Sharkeye557c332012-04-13 16:04:07 -07001379 POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001380 mAdapter.bindStats(data, restrictedUids);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001381 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001382 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001383
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001384 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001385 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001386 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001387 updateEmptyVisible();
1388 }
1389
1390 private void updateEmptyVisible() {
1391 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1392 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001393 mStupidPadding.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001394 }
1395 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001396
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001397 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001398 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001399 final Context context = getActivity();
Wink Savillefcec91f2014-12-02 17:12:08 -08001400 boolean retVal;
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001401 if (hasReadyMobileRadio(context)) {
1402 final TelephonyManager tele = TelephonyManager.from(context);
Wink Savillefcec91f2014-12-02 17:12:08 -08001403 retVal = mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001404 } else {
Wink Savillefcec91f2014-12-02 17:12:08 -08001405 retVal = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001406 }
Wink Savillefcec91f2014-12-02 17:12:08 -08001407 if (LOGD) Log.d(TAG, "isMobilePolicySplit: retVal=" + retVal);
1408 return retVal;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001409 }
1410
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001411 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001412 private void setMobilePolicySplit(boolean split) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001413 final Context context = getActivity();
1414 if (hasReadyMobileRadio(context)) {
1415 final TelephonyManager tele = TelephonyManager.from(context);
Wink Savillefcec91f2014-12-02 17:12:08 -08001416 if (LOGD) Log.d(TAG, "setMobilePolicySplit: split=" + split);
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001417 mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split);
1418 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001419 }
1420
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001421 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001422 final TelephonyManager tele = TelephonyManager.from(context);
1423 final String actualSubscriberId = tele.getSubscriberId();
Wink Savillefcec91f2014-12-02 17:12:08 -08001424 String retVal = SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
1425 if (LOGD) Log.d(TAG, "getActiveSubscriberId=" + retVal + " actualSubscriberId=" + actualSubscriberId);
1426 return retVal;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001427 }
1428
Wink Savilleb003a852014-10-23 10:16:26 -07001429 private static String getActiveSubscriberId(Context context, int subId) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07001430 final TelephonyManager tele = TelephonyManager.from(context);
Wink Savillefcec91f2014-12-02 17:12:08 -08001431 String retVal = tele.getSubscriberId(subId);
1432 if (LOGD) Log.d(TAG, "getActiveSubscriberId=" + retVal + " subId=" + subId);
1433 return retVal;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001434 }
1435
Jeff Sharkey8a503642011-06-10 13:31:21 -07001436 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001437 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001438 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001439 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001440 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001441
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001442 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001443 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001444 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001445 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001446
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001447 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001448 public void requestWarningEdit() {
1449 WarningEditorFragment.show(DataUsageSummary.this);
1450 }
1451
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001452 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001453 public void requestLimitEdit() {
1454 LimitEditorFragment.show(DataUsageSummary.this);
1455 }
1456 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001457
1458 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001459 * List item that reflects a specific data usage cycle.
1460 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001461 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001462 public CharSequence label;
1463 public long start;
1464 public long end;
1465
Jeff Sharkey8a503642011-06-10 13:31:21 -07001466 CycleItem(CharSequence label) {
1467 this.label = label;
1468 }
1469
1470 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001471 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001472 this.start = start;
1473 this.end = end;
1474 }
1475
Jeff Sharkey8a503642011-06-10 13:31:21 -07001476 @Override
1477 public String toString() {
1478 return label.toString();
1479 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001480
1481 @Override
1482 public boolean equals(Object o) {
1483 if (o instanceof CycleItem) {
1484 final CycleItem another = (CycleItem) o;
1485 return start == another.start && end == another.end;
1486 }
1487 return false;
1488 }
1489
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001490 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001491 public int compareTo(CycleItem another) {
1492 return Long.compare(start, another.start);
1493 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001494 }
1495
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001496 private static final StringBuilder sBuilder = new StringBuilder(50);
1497 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1498 sBuilder, Locale.getDefault());
1499
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001500 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001501 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001502
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001503 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001504 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001505 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1506 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001507 }
1508 }
1509
Jeff Sharkey8a503642011-06-10 13:31:21 -07001510 /**
1511 * Special-case data usage cycle that triggers dialog to change
1512 * {@link NetworkPolicy#cycleDay}.
1513 */
1514 public static class CycleChangeItem extends CycleItem {
1515 public CycleChangeItem(Context context) {
1516 super(context.getString(R.string.data_usage_change_cycle));
1517 }
1518 }
1519
1520 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001521 private boolean mChangePossible = false;
1522 private boolean mChangeVisible = false;
1523
1524 private final CycleChangeItem mChangeItem;
1525
Jeff Sharkey8a503642011-06-10 13:31:21 -07001526 public CycleAdapter(Context context) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001527 super(context, R.layout.data_usage_cycle_item);
1528 setDropDownViewResource(R.layout.data_usage_cycle_item_dropdown);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001529 mChangeItem = new CycleChangeItem(context);
1530 }
1531
1532 public void setChangePossible(boolean possible) {
1533 mChangePossible = possible;
1534 updateChange();
1535 }
1536
1537 public void setChangeVisible(boolean visible) {
1538 mChangeVisible = visible;
1539 updateChange();
1540 }
1541
1542 private void updateChange() {
1543 remove(mChangeItem);
1544 if (mChangePossible && mChangeVisible) {
1545 add(mChangeItem);
1546 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001547 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001548
1549 /**
1550 * Find position of {@link CycleItem} in this adapter which is nearest
1551 * the given {@link CycleItem}.
1552 */
1553 public int findNearestPosition(CycleItem target) {
1554 if (target != null) {
1555 final int count = getCount();
1556 for (int i = count - 1; i >= 0; i--) {
1557 final CycleItem item = getItem(i);
1558 if (item instanceof CycleChangeItem) {
1559 continue;
1560 } else if (item.compareTo(target) >= 0) {
1561 return i;
1562 }
1563 }
1564 }
1565 return 0;
1566 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001567 }
1568
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001569 public static class AppItem implements Comparable<AppItem>, Parcelable {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001570 public static final int CATEGORY_USER = 0;
1571 public static final int CATEGORY_APP_TITLE = 1;
1572 public static final int CATEGORY_APP = 2;
1573
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001574 public final int key;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001575 public boolean restricted;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001576 public int category;
1577
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001578 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001579 public long total;
1580
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001581 public AppItem() {
1582 this.key = 0;
1583 }
1584
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001585 public AppItem(int key) {
1586 this.key = key;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001587 }
1588
1589 public AppItem(Parcel parcel) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001590 key = parcel.readInt();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001591 uids = parcel.readSparseBooleanArray();
1592 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001593 }
1594
1595 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001596 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001597 }
1598
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001599 @Override
1600 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001601 dest.writeInt(key);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001602 dest.writeSparseBooleanArray(uids);
1603 dest.writeLong(total);
1604 }
1605
1606 @Override
1607 public int describeContents() {
1608 return 0;
1609 }
1610
1611 @Override
1612 public int compareTo(AppItem another) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001613 int comparison = Integer.compare(category, another.category);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001614 if (comparison == 0) {
1615 comparison = Long.compare(another.total, total);
1616 }
1617 return comparison;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001618 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001619
1620 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1621 @Override
1622 public AppItem createFromParcel(Parcel in) {
1623 return new AppItem(in);
1624 }
1625
1626 @Override
1627 public AppItem[] newArray(int size) {
1628 return new AppItem[size];
1629 }
1630 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001631 }
1632
Jeff Sharkey8a503642011-06-10 13:31:21 -07001633 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001634 * Adapter of applications, sorted by total usage descending.
1635 */
1636 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001637 private final UidDetailProvider mProvider;
1638 private final int mInsetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001639 private final UserManager mUm;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001640
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001641 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001642 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001643
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001644 public DataUsageAdapter(final UserManager userManager, UidDetailProvider provider, int insetSide) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001645 mProvider = checkNotNull(provider);
1646 mInsetSide = insetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001647 mUm = userManager;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001648 }
1649
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001650 /**
1651 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1652 */
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001653 public void bindStats(NetworkStats stats, int[] restrictedUids) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001654 mItems.clear();
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001655 mLargest = 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001656
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001657 final int currentUserId = ActivityManager.getCurrentUser();
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001658 final List<UserHandle> profiles = mUm.getUserProfiles();
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001659 final SparseArray<AppItem> knownItems = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001660
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001661 NetworkStats.Entry entry = null;
1662 final int size = stats != null ? stats.size() : 0;
1663 for (int i = 0; i < size; i++) {
1664 entry = stats.getValues(i, entry);
1665
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001666 // Decide how to collapse items together
1667 final int uid = entry.uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001668
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001669 final int collapseKey;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001670 final int category;
1671 final int userId = UserHandle.getUserId(uid);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001672 if (UserHandle.isApp(uid)) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001673 if (profiles.contains(new UserHandle(userId))) {
1674 if (userId != currentUserId) {
1675 // Add to a managed user item.
1676 final int managedKey = UidDetailProvider.buildKeyForUser(userId);
1677 accumulate(managedKey, knownItems, entry,
1678 AppItem.CATEGORY_USER);
1679 }
1680 // Add to app item.
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001681 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001682 category = AppItem.CATEGORY_APP;
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001683 } else {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001684 // Add to other user item.
1685 collapseKey = UidDetailProvider.buildKeyForUser(userId);
1686 category = AppItem.CATEGORY_USER;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001687 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001688 } else if (uid == UID_REMOVED || uid == UID_TETHERING) {
1689 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001690 category = AppItem.CATEGORY_APP;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001691 } else {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001692 collapseKey = android.os.Process.SYSTEM_UID;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001693 category = AppItem.CATEGORY_APP;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001694 }
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001695 accumulate(collapseKey, knownItems, entry, category);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001696 }
1697
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001698 final int restrictedUidsMax = restrictedUids.length;
1699 for (int i = 0; i < restrictedUidsMax; ++i) {
1700 final int uid = restrictedUids[i];
1701 // Only splice in restricted state for current user or managed users
1702 if (!profiles.contains(new UserHandle(UserHandle.getUserId(uid)))) {
1703 continue;
1704 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001705
1706 AppItem item = knownItems.get(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001707 if (item == null) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001708 item = new AppItem(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001709 item.total = -1;
1710 mItems.add(item);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001711 knownItems.put(item.key, item);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001712 }
1713 item.restricted = true;
1714 }
1715
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001716 if (!mItems.isEmpty()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001717 final AppItem title = new AppItem();
1718 title.category = AppItem.CATEGORY_APP_TITLE;
1719 mItems.add(title);
1720 }
1721
Jeff Sharkey8a503642011-06-10 13:31:21 -07001722 Collections.sort(mItems);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001723 notifyDataSetChanged();
1724 }
1725
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001726 /**
1727 * Accumulate data usage of a network stats entry for the item mapped by the collapse key.
1728 * Creates the item if needed.
1729 *
1730 * @param collapseKey the collapse key used to map the item.
1731 * @param knownItems collection of known (already existing) items.
1732 * @param entry the network stats entry to extract data usage from.
1733 * @param itemCategory the item is categorized on the list view by this category. Must be
1734 * either AppItem.APP_ITEM_CATEGORY or AppItem.MANAGED_USER_ITEM_CATEGORY
1735 */
1736 private void accumulate(int collapseKey, final SparseArray<AppItem> knownItems,
1737 NetworkStats.Entry entry, int itemCategory) {
1738 final int uid = entry.uid;
1739 AppItem item = knownItems.get(collapseKey);
1740 if (item == null) {
1741 item = new AppItem(collapseKey);
1742 item.category = itemCategory;
1743 mItems.add(item);
1744 knownItems.put(item.key, item);
1745 }
1746 item.addUid(uid);
1747 item.total += entry.rxBytes + entry.txBytes;
1748 if (mLargest < item.total) {
1749 mLargest = item.total;
1750 }
1751 }
1752
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001753 @Override
1754 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001755 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001756 }
1757
1758 @Override
1759 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001760 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001761 }
1762
1763 @Override
1764 public long getItemId(int position) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001765 return mItems.get(position).key;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001766 }
1767
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001768 /**
1769 * See {@link #getItemViewType} for the view types.
1770 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001771 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001772 public int getViewTypeCount() {
1773 return 2;
1774 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001775
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001776 /**
1777 * Returns 1 for separator items and 0 for anything else.
1778 */
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001779 @Override
1780 public int getItemViewType(int position) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001781 final AppItem item = mItems.get(position);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001782 if (item.category == AppItem.CATEGORY_APP_TITLE) {
1783 return 1;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001784 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001785 return 0;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001786 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001787 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001788
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001789 @Override
1790 public boolean areAllItemsEnabled() {
1791 return false;
1792 }
1793
1794 @Override
1795 public boolean isEnabled(int position) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001796 if (position > mItems.size()) {
1797 throw new ArrayIndexOutOfBoundsException();
1798 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001799 return getItemViewType(position) == 0;
1800 }
1801
1802 @Override
1803 public View getView(int position, View convertView, ViewGroup parent) {
1804 final AppItem item = mItems.get(position);
1805 if (getItemViewType(position) == 1) {
1806 if (convertView == null) {
1807 convertView = inflateCategoryHeader(LayoutInflater.from(parent.getContext()),
1808 parent);
1809 }
1810
1811 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
1812 title.setText(R.string.data_usage_app);
1813
1814 } else {
1815 if (convertView == null) {
1816 convertView = LayoutInflater.from(parent.getContext()).inflate(
1817 R.layout.data_usage_item, parent, false);
1818
1819 if (mInsetSide > 0) {
1820 convertView.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
1821 }
1822 }
1823
1824 final Context context = parent.getContext();
1825
1826 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
1827 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1828 android.R.id.progress);
1829
1830 // kick off async load of app details
1831 UidDetailTask.bindView(mProvider, item, convertView);
1832
1833 if (item.restricted && item.total <= 0) {
1834 text1.setText(R.string.data_usage_app_restricted);
1835 progress.setVisibility(View.GONE);
1836 } else {
1837 text1.setText(Formatter.formatFileSize(context, item.total));
1838 progress.setVisibility(View.VISIBLE);
1839 }
1840
1841 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1842 progress.setProgress(percentTotal);
1843 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001844
1845 return convertView;
1846 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001847 }
1848
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001849 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001850 * Empty {@link Fragment} that controls display of UID details in
1851 * {@link DataUsageSummary}.
1852 */
1853 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001854 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001855
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001856 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001857 if (!parent.isAdded()) return;
1858
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001859 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001860 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001861
1862 final AppDetailsFragment fragment = new AppDetailsFragment();
1863 fragment.setArguments(args);
1864 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001865 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1866 ft.add(fragment, TAG_APP_DETAILS);
1867 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001868 ft.setBreadCrumbTitle(
1869 parent.getResources().getString(R.string.data_usage_app_summary_title));
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001870 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001871 }
1872
1873 @Override
1874 public void onStart() {
1875 super.onStart();
1876 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001877 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001878 target.updateBody();
1879 }
1880
1881 @Override
1882 public void onStop() {
1883 super.onStop();
1884 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001885 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001886 target.updateBody();
1887 }
1888 }
1889
1890 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001891 * Dialog to request user confirmation before setting
1892 * {@link NetworkPolicy#limitBytes}.
1893 */
1894 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001895 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001896 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001897
1898 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001899 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001900
Jeff Sharkeyd0a55312014-08-08 10:25:24 -07001901 final NetworkPolicy policy = parent.mPolicyEditor.getPolicy(parent.mTemplate);
1902 if (policy == null) return;
1903
Jeff Sharkey461842a2011-09-25 18:22:48 -07001904 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001905 final CharSequence message;
Jeff Sharkeyd0a55312014-08-08 10:25:24 -07001906 final long minLimitBytes = (long) (policy.warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001907 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001908
1909 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001910 final String currentTab = parent.mCurrentTab;
1911 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001912 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001913 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001914 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001915 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001916 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001917 } else if (isMobileTab(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001918 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001919 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001920 } else {
1921 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001922 }
1923
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001924 final Bundle args = new Bundle();
1925 args.putCharSequence(EXTRA_MESSAGE, message);
1926 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1927
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001928 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1929 dialog.setArguments(args);
1930 dialog.setTargetFragment(parent, 0);
1931 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1932 }
1933
1934 @Override
1935 public Dialog onCreateDialog(Bundle savedInstanceState) {
1936 final Context context = getActivity();
1937
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001938 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001939 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1940
1941 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1942 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001943 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001944
1945 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001946 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001947 public void onClick(DialogInterface dialog, int which) {
1948 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1949 if (target != null) {
1950 target.setPolicyLimitBytes(limitBytes);
1951 }
1952 }
1953 });
1954
1955 return builder.create();
1956 }
1957 }
1958
1959 /**
1960 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1961 */
1962 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001963 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001964
1965 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001966 if (!parent.isAdded()) return;
1967
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001968 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001969 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001970
1971 final CycleEditorFragment dialog = new CycleEditorFragment();
1972 dialog.setArguments(args);
1973 dialog.setTargetFragment(parent, 0);
1974 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1975 }
1976
1977 @Override
1978 public Dialog onCreateDialog(Bundle savedInstanceState) {
1979 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001980 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1981 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001982
1983 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1984 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1985
1986 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1987 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1988
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001989 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1990 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001991
1992 cycleDayPicker.setMinValue(1);
1993 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001994 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001995 cycleDayPicker.setWrapSelectorWheel(true);
1996
1997 builder.setTitle(R.string.data_usage_cycle_editor_title);
1998 builder.setView(view);
1999
2000 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2001 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002002 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002003 public void onClick(DialogInterface dialog, int which) {
Jeff Sharkeyd277de92013-03-25 15:11:25 -07002004 // clear focus to finish pending text edits
2005 cycleDayPicker.clearFocus();
2006
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002007 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08002008 final String cycleTimezone = new Time().timezone;
2009 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002010 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002011 }
2012 });
2013
2014 return builder.create();
2015 }
2016 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002017
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002018 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002019 * Dialog to edit {@link NetworkPolicy#warningBytes}.
2020 */
2021 public static class WarningEditorFragment extends DialogFragment {
2022 private static final String EXTRA_TEMPLATE = "template";
2023
2024 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002025 if (!parent.isAdded()) return;
2026
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002027 final Bundle args = new Bundle();
2028 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
2029
2030 final WarningEditorFragment dialog = new WarningEditorFragment();
2031 dialog.setArguments(args);
2032 dialog.setTargetFragment(parent, 0);
2033 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
2034 }
2035
2036 @Override
2037 public Dialog onCreateDialog(Bundle savedInstanceState) {
2038 final Context context = getActivity();
2039 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2040 final NetworkPolicyEditor editor = target.mPolicyEditor;
2041
2042 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2043 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2044
2045 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
2046 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
2047
2048 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2049 final long warningBytes = editor.getPolicyWarningBytes(template);
2050 final long limitBytes = editor.getPolicyLimitBytes(template);
2051
2052 bytesPicker.setMinValue(0);
2053 if (limitBytes != LIMIT_DISABLED) {
2054 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
2055 } else {
2056 bytesPicker.setMaxValue(Integer.MAX_VALUE);
2057 }
2058 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
2059 bytesPicker.setWrapSelectorWheel(false);
2060
2061 builder.setTitle(R.string.data_usage_warning_editor_title);
2062 builder.setView(view);
2063
2064 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2065 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002066 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002067 public void onClick(DialogInterface dialog, int which) {
2068 // clear focus to finish pending text edits
2069 bytesPicker.clearFocus();
2070
2071 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
2072 editor.setPolicyWarningBytes(template, bytes);
2073 target.updatePolicy(false);
2074 }
2075 });
2076
2077 return builder.create();
2078 }
2079 }
2080
2081 /**
2082 * Dialog to edit {@link NetworkPolicy#limitBytes}.
2083 */
2084 public static class LimitEditorFragment extends DialogFragment {
2085 private static final String EXTRA_TEMPLATE = "template";
2086
2087 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002088 if (!parent.isAdded()) return;
2089
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002090 final Bundle args = new Bundle();
2091 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
2092
2093 final LimitEditorFragment dialog = new LimitEditorFragment();
2094 dialog.setArguments(args);
2095 dialog.setTargetFragment(parent, 0);
2096 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
2097 }
2098
2099 @Override
2100 public Dialog onCreateDialog(Bundle savedInstanceState) {
2101 final Context context = getActivity();
2102 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2103 final NetworkPolicyEditor editor = target.mPolicyEditor;
2104
2105 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2106 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2107
2108 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
2109 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
2110
2111 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2112 final long warningBytes = editor.getPolicyWarningBytes(template);
2113 final long limitBytes = editor.getPolicyLimitBytes(template);
2114
2115 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09002116 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002117 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
2118 } else {
2119 bytesPicker.setMinValue(0);
2120 }
2121 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
2122 bytesPicker.setWrapSelectorWheel(false);
2123
2124 builder.setTitle(R.string.data_usage_limit_editor_title);
2125 builder.setView(view);
2126
2127 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2128 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002129 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002130 public void onClick(DialogInterface dialog, int which) {
2131 // clear focus to finish pending text edits
2132 bytesPicker.clearFocus();
2133
2134 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
2135 editor.setPolicyLimitBytes(template, bytes);
2136 target.updatePolicy(false);
2137 }
2138 });
2139
2140 return builder.create();
2141 }
2142 }
2143 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07002144 * Dialog to request user confirmation before disabling data.
2145 */
2146 public static class ConfirmDataDisableFragment extends DialogFragment {
2147 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002148 if (!parent.isAdded()) return;
2149
Jeff Sharkey28130d92011-09-02 16:10:24 -07002150 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
2151 dialog.setTargetFragment(parent, 0);
2152 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
2153 }
2154
2155 @Override
2156 public Dialog onCreateDialog(Bundle savedInstanceState) {
2157 final Context context = getActivity();
2158
2159 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2160 builder.setMessage(R.string.data_usage_disable_mobile);
2161
2162 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002163 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07002164 public void onClick(DialogInterface dialog, int which) {
2165 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2166 if (target != null) {
2167 // TODO: extend to modify policy enabled flag.
2168 target.setMobileDataEnabled(false);
2169 }
2170 }
2171 });
2172 builder.setNegativeButton(android.R.string.cancel, null);
2173
2174 return builder.create();
2175 }
2176 }
2177
2178 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002179 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07002180 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002181 */
2182 public static class ConfirmRestrictFragment extends DialogFragment {
2183 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002184 if (!parent.isAdded()) return;
2185
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002186 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
2187 dialog.setTargetFragment(parent, 0);
2188 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
2189 }
2190
2191 @Override
2192 public Dialog onCreateDialog(Bundle savedInstanceState) {
2193 final Context context = getActivity();
2194
2195 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002196 builder.setTitle(R.string.data_usage_restrict_background_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07002197 if (Utils.hasMultipleUsers(context)) {
2198 builder.setMessage(R.string.data_usage_restrict_background_multiuser);
2199 } else {
2200 builder.setMessage(R.string.data_usage_restrict_background);
2201 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002202
2203 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002204 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002205 public void onClick(DialogInterface dialog, int which) {
2206 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2207 if (target != null) {
2208 target.setRestrictBackground(true);
2209 }
2210 }
2211 });
2212 builder.setNegativeButton(android.R.string.cancel, null);
2213
2214 return builder.create();
2215 }
2216 }
2217
2218 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002219 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
2220 * change has been denied, usually based on
2221 * {@link DataUsageSummary#hasLimitedNetworks()}.
2222 */
2223 public static class DeniedRestrictFragment extends DialogFragment {
2224 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002225 if (!parent.isAdded()) return;
2226
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002227 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
2228 dialog.setTargetFragment(parent, 0);
2229 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
2230 }
2231
2232 @Override
2233 public Dialog onCreateDialog(Bundle savedInstanceState) {
2234 final Context context = getActivity();
2235
2236 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2237 builder.setTitle(R.string.data_usage_app_restrict_background);
2238 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
2239 builder.setPositiveButton(android.R.string.ok, null);
2240
2241 return builder.create();
2242 }
2243 }
2244
2245 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002246 * Dialog to request user confirmation before setting
2247 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
2248 */
2249 public static class ConfirmAppRestrictFragment extends DialogFragment {
2250 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002251 if (!parent.isAdded()) return;
2252
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002253 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2254 dialog.setTargetFragment(parent, 0);
2255 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2256 }
2257
2258 @Override
2259 public Dialog onCreateDialog(Bundle savedInstanceState) {
2260 final Context context = getActivity();
2261
2262 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002263 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2264 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2265
2266 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002267 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002268 public void onClick(DialogInterface dialog, int which) {
2269 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2270 if (target != null) {
2271 target.setAppRestrictBackground(true);
2272 }
2273 }
2274 });
2275 builder.setNegativeButton(android.R.string.cancel, null);
2276
2277 return builder.create();
2278 }
2279 }
2280
2281 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002282 * Compute default tab that should be selected, based on
2283 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2284 */
2285 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002286 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002287 if (template == null) {
Wink Savilleb003a852014-10-23 10:16:26 -07002288 final int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
Wink Saville0183fb52014-11-22 10:11:39 -08002289 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002290 if (SubscriptionManager.isValidSubId(subId)) {
2291 return TAB_MOBILE + String.valueOf(subId);
2292 }
2293 return null;
2294 }
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002295
2296 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002297 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002298 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002299 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002300 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002301 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002302 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002303 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002304 return TAB_WIFI;
2305 default:
2306 return null;
2307 }
2308 }
2309
2310 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002311 * Background task that loads {@link UidDetail}, binding to
2312 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002313 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002314 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2315 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002316 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002317 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002318
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002319 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002320 mProvider = checkNotNull(provider);
2321 mItem = checkNotNull(item);
2322 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002323 }
2324
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002325 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002326 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002327 final UidDetailTask existing = (UidDetailTask) target.getTag();
2328 if (existing != null) {
2329 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002330 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002331
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002332 final UidDetail cachedDetail = provider.getUidDetail(item.key, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002333 if (cachedDetail != null) {
2334 bindView(cachedDetail, target);
2335 } else {
2336 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2337 AsyncTask.THREAD_POOL_EXECUTOR));
2338 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002339 }
2340
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002341 private static void bindView(UidDetail detail, View target) {
2342 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2343 final TextView title = (TextView) target.findViewById(android.R.id.title);
2344
2345 if (detail != null) {
2346 icon.setImageDrawable(detail.icon);
2347 title.setText(detail.label);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +01002348 title.setContentDescription(detail.contentDescription);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002349 } else {
2350 icon.setImageDrawable(null);
2351 title.setText(null);
2352 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002353 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002354
2355 @Override
2356 protected void onPreExecute() {
2357 bindView(null, mTarget);
2358 }
2359
2360 @Override
2361 protected UidDetail doInBackground(Void... params) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002362 return mProvider.getUidDetail(mItem.key, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002363 }
2364
2365 @Override
2366 protected void onPostExecute(UidDetail result) {
2367 bindView(result, mTarget);
2368 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002369 }
2370
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002371 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002372 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002373 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002374 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002375 if (TEST_RADIOS) {
2376 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2377 }
2378
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002379 final ConnectivityManager conn = ConnectivityManager.from(context);
2380 final TelephonyManager tele = TelephonyManager.from(context);
2381
Wink Saville0183fb52014-11-22 10:11:39 -08002382 final List<SubscriptionInfo> subInfoList =
2383 SubscriptionManager.from(context).getActiveSubscriptionInfoList();
2384 // No activated Subscriptions
PauloftheWest50e6eca2014-10-03 11:07:14 -07002385 if (subInfoList == null) {
Wink Savillefcec91f2014-12-02 17:12:08 -08002386 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subInfoList=null");
PauloftheWest50e6eca2014-10-03 11:07:14 -07002387 return false;
2388 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002389 // require both supported network and ready SIM
PauloftheWest50e6eca2014-10-03 11:07:14 -07002390 boolean isReady = true;
Wink Savilleca756612014-11-08 10:47:12 -08002391 for (SubscriptionInfo subInfo : subInfoList) {
Stuart Scott3ada2ec2014-10-31 14:11:24 -07002392 isReady = isReady & tele.getSimState(subInfo.getSimSlotIndex()) == SIM_STATE_READY;
Wink Savillefcec91f2014-12-02 17:12:08 -08002393 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subInfo=" + subInfo);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002394 }
Wink Savillefcec91f2014-12-02 17:12:08 -08002395 boolean retVal = conn.isNetworkSupported(TYPE_MOBILE) && isReady;
2396 if (LOGD) {
2397 Log.d(TAG, "hasReadyMobileRadio:"
2398 + " conn.isNetworkSupported(TYPE_MOBILE)="
2399 + conn.isNetworkSupported(TYPE_MOBILE)
2400 + " isReady=" + isReady);
2401 }
2402 return retVal;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002403 }
2404
2405 /*
2406 * TODO: consider adding to TelephonyManager or SubscritpionManager.
2407 */
Wink Savilleb003a852014-10-23 10:16:26 -07002408 public static boolean hasReadyMobileRadio(Context context, int subId) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07002409 if (TEST_RADIOS) {
2410 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2411 }
2412
2413 final ConnectivityManager conn = ConnectivityManager.from(context);
2414 final TelephonyManager tele = TelephonyManager.from(context);
2415 final int slotId = SubscriptionManager.getSlotId(subId);
2416 final boolean isReady = tele.getSimState(slotId) == SIM_STATE_READY;
2417
Wink Savillefcec91f2014-12-02 17:12:08 -08002418 boolean retVal = conn.isNetworkSupported(TYPE_MOBILE) && isReady;
2419 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subId=" + subId
2420 + " conn.isNetworkSupported(TYPE_MOBILE)=" + conn.isNetworkSupported(TYPE_MOBILE)
2421 + " isReady=" + isReady);
2422 return retVal;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002423 }
2424
2425 /**
2426 * Test if device has a mobile 4G data radio.
2427 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002428 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002429 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2430 return false;
2431 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002432 if (TEST_RADIOS) {
2433 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2434 }
2435
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002436 final ConnectivityManager conn = ConnectivityManager.from(context);
2437 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002438
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002439 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Wink Saville55434042012-06-14 12:33:43 -07002440 final boolean hasLte = (tele.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE)
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002441 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002442 return hasWimax || hasLte;
2443 }
2444
2445 /**
2446 * Test if device has a Wi-Fi data radio.
2447 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002448 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002449 if (TEST_RADIOS) {
2450 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2451 }
2452
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002453 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002454 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002455 }
2456
2457 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002458 * Test if device has an ethernet network connection.
2459 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002460 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002461 if (TEST_RADIOS) {
2462 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2463 }
2464
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002465 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002466 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2467
2468 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002469 if (mStatsSession != null) {
2470 try {
2471 ethernetBytes = mStatsSession.getSummaryForNetwork(
2472 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2473 .getTotalBytes();
2474 } catch (RemoteException e) {
2475 throw new RuntimeException(e);
2476 }
2477 } else {
2478 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002479 }
2480
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002481 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002482 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002483 }
2484
2485 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002486 * Inflate a {@link Preference} style layout, adding the given {@link View}
2487 * widget into {@link android.R.id#widget_frame}.
2488 */
2489 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2490 final View view = inflater.inflate(R.layout.preference, root, false);
2491 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2492 android.R.id.widget_frame);
2493 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2494 return view;
2495 }
2496
Jeff Sharkeyb6548462014-07-21 15:38:06 -07002497 private static View inflateCategoryHeader(LayoutInflater inflater, ViewGroup root) {
2498 final TypedArray a = inflater.getContext().obtainStyledAttributes(null,
2499 com.android.internal.R.styleable.Preference,
2500 com.android.internal.R.attr.preferenceCategoryStyle, 0);
2501 final int resId = a.getResourceId(com.android.internal.R.styleable.Preference_layout, 0);
2502 return inflater.inflate(resId, root, false);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002503 }
2504
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002505 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002506 * Test if any networks are currently limited.
2507 */
2508 private boolean hasLimitedNetworks() {
2509 return !buildLimitedNetworksList().isEmpty();
2510 }
2511
2512 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002513 * Build string describing currently limited networks, which defines when
2514 * background data is restricted.
2515 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002516 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002517 private CharSequence buildLimitedNetworksString() {
2518 final List<CharSequence> limited = buildLimitedNetworksList();
2519
2520 // handle case where no networks limited
2521 if (limited.isEmpty()) {
2522 limited.add(getText(R.string.data_usage_list_none));
2523 }
2524
2525 return TextUtils.join(limited);
2526 }
2527
2528 /**
2529 * Build list of currently limited networks, which defines when background
2530 * data is restricted.
2531 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002532 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002533 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002534 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002535
2536 // build combined list of all limited networks
2537 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002538
2539 final TelephonyManager tele = TelephonyManager.from(context);
2540 if (tele.getSimState() == SIM_STATE_READY) {
2541 final String subscriberId = getActiveSubscriberId(context);
2542 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2543 limited.add(getText(R.string.data_usage_list_mobile));
2544 }
2545 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2546 limited.add(getText(R.string.data_usage_tab_3g));
2547 }
2548 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2549 limited.add(getText(R.string.data_usage_tab_4g));
2550 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002551 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002552
2553 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002554 limited.add(getText(R.string.data_usage_tab_wifi));
2555 }
2556 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2557 limited.add(getText(R.string.data_usage_tab_ethernet));
2558 }
2559
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002560 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002561 }
2562
2563 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002564 * Inset both selector and divider {@link Drawable} on the given
2565 * {@link ListView} by the requested dimensions.
2566 */
2567 private static void insetListViewDrawables(ListView view, int insetSide) {
2568 final Drawable selector = view.getSelector();
2569 final Drawable divider = view.getDivider();
2570
2571 // fully unregister these drawables so callbacks can be maintained after
2572 // wrapping below.
2573 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2574 view.setSelector(stub);
2575 view.setDivider(stub);
2576
2577 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2578 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2579 }
2580
2581 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002582 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002583 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002584 */
2585 private static void setPreferenceTitle(View parent, int resId) {
2586 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2587 title.setText(resId);
2588 }
2589
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002590 /**
2591 * Set {@link android.R.id#summary} for a preference view inflated with
2592 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2593 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002594 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002595 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2596 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002597 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002598 }
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002599
2600 /**
2601 * For search
2602 */
2603 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -07002604 new BaseSearchIndexProvider() {
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002605 @Override
2606 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
2607 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
2608
2609 final Resources res = context.getResources();
2610
2611 // Add fragment title
2612 SearchIndexableRaw data = new SearchIndexableRaw(context);
2613 data.title = res.getString(R.string.data_usage_summary_title);
2614 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2615 result.add(data);
2616
2617 // Mobile data
2618 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002619 data.key = DATA_USAGE_ENABLE_MOBILE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002620 data.title = res.getString(R.string.data_usage_enable_mobile);
2621 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2622 result.add(data);
2623
2624 // Set mobile data limit
2625 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002626 data.key = DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002627 data.title = res.getString(R.string.data_usage_disable_mobile_limit);
2628 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2629 result.add(data);
2630
Fabrice Di Megliof2a52262014-04-17 17:20:27 -07002631 // Data usage cycle
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002632 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002633 data.key = DATA_USAGE_CYCLE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002634 data.title = res.getString(R.string.data_usage_cycle);
2635 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2636 result.add(data);
2637
2638 return result;
2639 }
2640 };
2641
PauloftheWestc80b7612014-11-25 04:40:45 -08002642 private void addMobileTab(Context context, SubscriptionInfo subInfo) {
2643 if (subInfo != null && mMobileTagMap != null) {
2644 if (hasReadyMobileRadio(context, subInfo.getSubscriptionId())) {
2645 mTabHost.addTab(buildTabSpec(mMobileTagMap.get(subInfo.getSubscriptionId()),
2646 subInfo.getDisplayName()));
PauloftheWest50e6eca2014-10-03 11:07:14 -07002647 }
Wink Savillefcec91f2014-12-02 17:12:08 -08002648 } else {
2649 if (LOGD) Log.d(TAG, "addMobileTab: subInfoList is null");
PauloftheWest50e6eca2014-10-03 11:07:14 -07002650 }
2651 }
2652
Wink Savilleca756612014-11-08 10:47:12 -08002653 private SubscriptionInfo getCurrentTabSubInfo(Context context) {
PauloftheWestde338442014-10-29 14:46:44 -07002654 if (mSubInfoList != null && mTabHost != null) {
2655 final int currentTagIndex = mTabHost.getCurrentTab();
2656 int i = 0;
Wink Savilleca756612014-11-08 10:47:12 -08002657 for (SubscriptionInfo subInfo : mSubInfoList) {
Stuart Scott3ada2ec2014-10-31 14:11:24 -07002658 if (hasReadyMobileRadio(context, subInfo.getSubscriptionId())) {
PauloftheWestde338442014-10-29 14:46:44 -07002659 if (i++ == currentTagIndex) {
2660 return subInfo;
2661 }
2662 }
2663 }
2664 }
2665 return null;
2666 }
2667
PauloftheWest50e6eca2014-10-03 11:07:14 -07002668 /**
2669 * Init a map with subId key and mobile tag name
2670 * @param subInfoList The subscription Info List
2671 * @return The map or null if no activated subscription
2672 */
Wink Savilleca756612014-11-08 10:47:12 -08002673 private Map<Integer, String> initMobileTabTag(List<SubscriptionInfo> subInfoList) {
Wink Savilleb003a852014-10-23 10:16:26 -07002674 Map<Integer, String> map = null;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002675 if (subInfoList != null) {
2676 String mobileTag;
Wink Savilleb003a852014-10-23 10:16:26 -07002677 map = new HashMap<Integer, String>();
Wink Savillefcec91f2014-12-02 17:12:08 -08002678 for (SubscriptionInfo subInfo : subInfoList) {
2679 mobileTag = TAB_MOBILE + String.valueOf(subInfo.getSubscriptionId());
2680 map.put(subInfo.getSubscriptionId(), mobileTag);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002681 }
2682 }
2683 return map;
2684 }
2685
2686 private static boolean isMobileTab(String currentTab) {
2687 return currentTab != null ? currentTab.contains(TAB_MOBILE) : false;
2688 }
2689
Wink Savilleb003a852014-10-23 10:16:26 -07002690 private int getSubId(String currentTab) {
Wink Saville0183fb52014-11-22 10:11:39 -08002691 if (mMobileTagMap != null) {
2692 Set<Integer> set = mMobileTagMap.keySet();
2693 for (Integer subId : set) {
2694 if (mMobileTagMap.get(subId).equals(currentTab)) {
2695 return subId;
2696 }
PauloftheWest50e6eca2014-10-03 11:07:14 -07002697 }
2698 }
2699 Log.e(TAG, "currentTab = " + currentTab + " non mobile tab called this function");
2700 return -1;
2701 }
2702
2703 //SUB SELECT
PauloftheWest98523c82014-10-28 08:30:05 -07002704 private boolean isMobileDataAvailable(long subId) {
Wink Savilleb003a852014-10-23 10:16:26 -07002705 int[] subIds = SubscriptionManager.getSubId(PhoneConstants.SUB1);
PauloftheWest98523c82014-10-28 08:30:05 -07002706 if (subIds != null && subIds[0] == subId) {
2707 return true;
2708 }
2709
2710 subIds = SubscriptionManager.getSubId(PhoneConstants.SUB2);
2711 if (subIds != null && subIds[0] == subId) {
2712 return true;
2713 }
2714
2715 subIds = SubscriptionManager.getSubId(PhoneConstants.SUB3);
2716 if (subIds != null && subIds[0] == subId) {
2717 return true;
2718 }
2719 return false;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002720 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002721}