blob: a0a237d4a137c3df64e651fdb7c20c655605496a [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;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700143import com.google.android.collect.Lists;
144
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700145import libcore.util.Objects;
146
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700147import java.util.ArrayList;
148import java.util.Collections;
PauloftheWest50e6eca2014-10-03 11:07:14 -0700149import java.util.HashMap;
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700150import java.util.List;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700151import java.util.Locale;
PauloftheWest50e6eca2014-10-03 11:07:14 -0700152import java.util.Map;
153import java.util.Set;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700154
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700155/**
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700156 * Panel showing data usage history across various networks, including options
157 * to inspect based on usage cycle and control through {@link NetworkPolicy}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700158 */
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700159public class DataUsageSummary extends HighlightingFragment implements Indexable {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700160 private static final String TAG = "DataUsage";
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800161 private static final boolean LOGD = false;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700162
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700163 // TODO: remove this testing code
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700164 private static final boolean TEST_ANIM = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700165 private static final boolean TEST_RADIOS = false;
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800166
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700167 private static final String TEST_RADIOS_PROP = "test.radios";
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800168 private static final String TEST_SUBSCRIBER_PROP = "test.subscriberid";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700169
Jeff Sharkey8a503642011-06-10 13:31:21 -0700170 private static final String TAB_3G = "3g";
171 private static final String TAB_4G = "4g";
172 private static final String TAB_MOBILE = "mobile";
173 private static final String TAB_WIFI = "wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700174 private static final String TAB_ETHERNET = "ethernet";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700175
Jeff Sharkey28130d92011-09-02 16:10:24 -0700176 private static final String TAG_CONFIRM_DATA_DISABLE = "confirmDataDisable";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700177 private static final String TAG_CONFIRM_LIMIT = "confirmLimit";
178 private static final String TAG_CYCLE_EDITOR = "cycleEditor";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700179 private static final String TAG_WARNING_EDITOR = "warningEditor";
180 private static final String TAG_LIMIT_EDITOR = "limitEditor";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700181 private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700182 private static final String TAG_DENIED_RESTRICT = "deniedRestrict";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700183 private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700184 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700185
Fabrice Di Meglio2169c882014-04-18 15:18:40 -0700186 private static final String DATA_USAGE_ENABLE_MOBILE_KEY = "data_usage_enable_mobile";
187 private static final String DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY =
188 "data_usage_disable_mobile_limit";
189 private static final String DATA_USAGE_CYCLE_KEY = "data_usage_cycle";
190
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700191 private static final int LOADER_CHART_DATA = 2;
192 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700193
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700194 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700195 private INetworkStatsService mStatsService;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700196 private NetworkPolicyManager mPolicyManager;
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700197 private TelephonyManager mTelephonyManager;
Wink Saville0183fb52014-11-22 10:11:39 -0800198 private SubscriptionManager mSubscriptionManager;
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700199
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700200 private INetworkStatsSession mStatsSession;
201
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700202 private static final String PREF_FILE = "data_usage";
203 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700204 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700205
206 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700207
Jeff Sharkey8a503642011-06-10 13:31:21 -0700208 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700209 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700210 private TabWidget mTabWidget;
211 private ListView mListView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700212 private DataUsageAdapter mAdapter;
213
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700214 /** Distance to inset content from sides, when needed. */
215 private int mInsetSide = 0;
216
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700217 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700218
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700219 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700220 private LinearLayout mNetworkSwitches;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700221 private boolean mDataEnabledSupported;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700222 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700223 private View mDataEnabledView;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700224 private boolean mDisableAtLimitSupported;
225 private Switch mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700226 private View mDisableAtLimitView;
227
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700228 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700229 private Spinner mCycleSpinner;
230 private CycleAdapter mCycleAdapter;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700231 private TextView mCycleSummary;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700232
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700233 private ChartDataUsageView mChart;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700234 private View mDisclaimer;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700235 private TextView mEmpty;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700236 private View mStupidPadding;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700237
238 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700239 private ImageView mAppIcon;
240 private ViewGroup mAppTitles;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700241 private TextView mAppTotal;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700242 private TextView mAppForeground;
243 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700244 private Button mAppSettings;
245
246 private LinearLayout mAppSwitches;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700247 private Switch mAppRestrict;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700248 private View mAppRestrictView;
249
Jeff Sharkey8a503642011-06-10 13:31:21 -0700250 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700251 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700252
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700253 private NetworkTemplate mTemplate;
254 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700255
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700256 private AppItem mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700257
258 private Intent mAppSettingsIntent;
259
Jeff Sharkeya662e492011-06-18 21:57:06 -0700260 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700261
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700262 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700263 private String mIntentTab = null;
264
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700265 private MenuItem mMenuRestrictBackground;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700266 private MenuItem mMenuShowWifi;
267 private MenuItem mMenuShowEthernet;
268 private MenuItem mMenuSimCards;
269 private MenuItem mMenuCellularNetworks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700270
Wink Savilleca756612014-11-08 10:47:12 -0800271 private List<SubscriptionInfo> mSubInfoList;
Wink Savilleb003a852014-10-23 10:16:26 -0700272 private Map<Integer,String> mMobileTagMap;
PauloftheWest50e6eca2014-10-03 11:07:14 -0700273
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700274 /** Flag used to ignore listeners during binding. */
275 private boolean mBinding;
276
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700277 private UidDetailProvider mUidDetailProvider;
278
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700279 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700280 public void onCreate(Bundle savedInstanceState) {
281 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700282 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700283
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700284 mNetworkService = INetworkManagementService.Stub.asInterface(
285 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700286 mStatsService = INetworkStatsService.Stub.asInterface(
287 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700288 mPolicyManager = NetworkPolicyManager.from(context);
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700289 mTelephonyManager = TelephonyManager.from(context);
Wink Saville0183fb52014-11-22 10:11:39 -0800290 mSubscriptionManager = SubscriptionManager.from(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700291
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700292 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700293
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700294 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700295 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700296
Wink Saville0183fb52014-11-22 10:11:39 -0800297 mSubInfoList = mSubscriptionManager.getActiveSubscriptionInfoList();
PauloftheWest50e6eca2014-10-03 11:07:14 -0700298 mMobileTagMap = initMobileTabTag(mSubInfoList);
299
Jaewan Kimffce9c12013-03-19 16:53:45 +0900300 try {
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700301 if (!mNetworkService.isBandwidthControlEnabled()) {
302 Log.w(TAG, "No bandwidth control; leaving");
303 getActivity().finish();
304 }
305 } catch (RemoteException e) {
306 Log.w(TAG, "No bandwidth control; leaving");
307 getActivity().finish();
308 }
309
310 try {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900311 mStatsSession = mStatsService.openSession();
312 } catch (RemoteException e) {
313 throw new RuntimeException(e);
314 }
315
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700316 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700317 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700318
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700319 // override preferences when no mobile radio
320 if (!hasReadyMobileRadio(context)) {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900321 mShowWifi = true;
322 mShowEthernet = true;
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700323 }
324
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700325 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700326 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700327
Jeff Sharkey8a503642011-06-10 13:31:21 -0700328 @Override
329 public View onCreateView(LayoutInflater inflater, ViewGroup container,
330 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700331
Jeff Sharkey8a503642011-06-10 13:31:21 -0700332 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700333 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
334
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700335 mUidDetailProvider = new UidDetailProvider(context);
336
Jeff Sharkey8a503642011-06-10 13:31:21 -0700337 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700338 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700339 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
340 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700341
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700342 // decide if we need to manually inset our content, or if we should rely
343 // on parent container for inset.
344 final boolean shouldInset = mListView.getScrollBarStyle()
345 == View.SCROLLBARS_OUTSIDE_OVERLAY;
Amith Yamasani56f51a82013-08-05 10:07:23 -0700346 mInsetSide = 0;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700347
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700348 // adjust padding around tabwidget as needed
Amith Yamasani56f51a82013-08-05 10:07:23 -0700349 prepareCustomPreferencesList(container, view, mListView, false);
Jeff Sharkey5d706792011-09-08 18:57:17 -0700350
Jeff Sharkey8a503642011-06-10 13:31:21 -0700351 mTabHost.setup();
352 mTabHost.setOnTabChangedListener(mTabListener);
353
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700354 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700355 mHeader.setClickable(true);
356
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700357 mListView.addHeaderView(new View(context), null, true);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700358 mListView.addHeaderView(mHeader, null, true);
359 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700360
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700361 if (mInsetSide > 0) {
362 // inset selector and divider drawables
363 insetListViewDrawables(mListView, mInsetSide);
Fabrice Di Megliob27223f2013-01-15 18:54:11 -0800364 mHeader.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700365 }
366
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700367 {
368 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700369 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
370 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700371 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700372
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700373 mDataEnabled = new Switch(inflater.getContext());
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700374 mDataEnabled.setClickable(false);
375 mDataEnabled.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700376 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700377 mDataEnabledView.setTag(R.id.preference_highlight_key,
378 DATA_USAGE_ENABLE_MOBILE_KEY);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700379 mDataEnabledView.setClickable(true);
380 mDataEnabledView.setFocusable(true);
381 mDataEnabledView.setOnClickListener(mDataEnabledListener);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700382 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700383
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700384 mDisableAtLimit = new Switch(inflater.getContext());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700385 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700386 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700387 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700388 mDisableAtLimitView.setTag(R.id.preference_highlight_key,
389 DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700390 mDisableAtLimitView.setClickable(true);
391 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700392 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
393 mNetworkSwitches.addView(mDisableAtLimitView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700394
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700395 mCycleView = inflater.inflate(R.layout.data_usage_cycles, mNetworkSwitches, false);
396 mCycleView.setTag(R.id.preference_highlight_key, DATA_USAGE_CYCLE_KEY);
397 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
398 mCycleAdapter = new CycleAdapter(context);
399 mCycleSpinner.setAdapter(mCycleAdapter);
400 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
401 mCycleSummary = (TextView) mCycleView.findViewById(R.id.cycle_summary);
402 mNetworkSwitches.addView(mCycleView);
403 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700404
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700405 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700406 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800407 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700408
409 {
410 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700411 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700412 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
413 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700414 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
415 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700416 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700417
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700418 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700419
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700420 mAppRestrict = new Switch(inflater.getContext());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700421 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700422 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700423 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700424 mAppRestrictView.setClickable(true);
425 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700426 mAppRestrictView.setOnClickListener(mAppRestrictListener);
427 mAppSwitches.addView(mAppRestrictView);
428 }
429
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700430 mDisclaimer = mHeader.findViewById(R.id.disclaimer);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700431 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700432 mStupidPadding = mHeader.findViewById(R.id.stupid_padding);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700433
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +0100434 final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
435 mAdapter = new DataUsageAdapter(um, mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700436 mListView.setOnItemClickListener(mListListener);
437 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700438
439 return view;
440 }
441
442 @Override
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700443 public void onViewStateRestored(Bundle savedInstanceState) {
444 super.onViewStateRestored(savedInstanceState);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700445
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700446 // pick default tab based on incoming intent
447 final Intent intent = getActivity().getIntent();
448 mIntentTab = computeTabFromIntent(intent);
449
Jeff Sharkey8a503642011-06-10 13:31:21 -0700450 // this kicks off chain reaction which creates tabs, binds the body to
451 // selected network, and binds chart, cycles and detail list.
452 updateTabs();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700453 }
454
455 @Override
456 public void onResume() {
457 super.onResume();
458
459 getView().post(new Runnable() {
460 @Override
461 public void run() {
462 highlightViewIfNeeded();
463 }
464 });
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700465
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700466 // kick off background task to update stats
467 new AsyncTask<Void, Void, Void>() {
468 @Override
469 protected Void doInBackground(Void... params) {
470 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700471 // wait a few seconds before kicking off
472 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700473 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700474 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700475 } catch (RemoteException e) {
476 }
477 return null;
478 }
479
480 @Override
481 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700482 if (isAdded()) {
483 updateBody();
484 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700485 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700486 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700487 }
488
Jeff Sharkey8a503642011-06-10 13:31:21 -0700489 @Override
490 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
491 inflater.inflate(R.menu.data_usage, menu);
492 }
493
494 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700495 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700496 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700497 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700498 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700499
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700500 mMenuShowWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700501 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
502 mMenuShowWifi.setVisible(!appDetailMode);
503 } else {
504 mMenuShowWifi.setVisible(false);
505 }
506
507 mMenuShowEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
508 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
509 mMenuShowEthernet.setVisible(!appDetailMode);
510 } else {
511 mMenuShowEthernet.setVisible(false);
512 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700513
514 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700515 mMenuRestrictBackground.setVisible(
516 hasReadyMobileRadio(context) && isOwner && !appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700517
518 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
519 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700520 metered.setVisible(!appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700521 } else {
522 metered.setVisible(false);
523 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700524
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700525 // TODO: show when multiple sims available
526 mMenuSimCards = menu.findItem(R.id.data_usage_menu_sim_cards);
527 mMenuSimCards.setVisible(false);
528
529 mMenuCellularNetworks = menu.findItem(R.id.data_usage_menu_cellular_networks);
PauloftheWesta4b8fb32014-09-18 10:12:25 -0700530 mMenuCellularNetworks.setVisible(hasReadyMobileRadio(context)
531 && !appDetailMode && isOwner);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700532
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700533 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
534 String helpUrl;
535 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800536 HelpUtils.prepareHelpMenuItem(context, help, helpUrl);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700537 } else {
538 help.setVisible(false);
539 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700540
541 updateMenuTitles();
542 }
543
544 private void updateMenuTitles() {
545 if (mPolicyManager.getRestrictBackground()) {
546 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_allow_background);
547 } else {
548 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_restrict_background);
549 }
550
551 if (mShowWifi) {
552 mMenuShowWifi.setTitle(R.string.data_usage_menu_hide_wifi);
553 } else {
554 mMenuShowWifi.setTitle(R.string.data_usage_menu_show_wifi);
555 }
556
557 if (mShowEthernet) {
558 mMenuShowEthernet.setTitle(R.string.data_usage_menu_hide_ethernet);
559 } else {
560 mMenuShowEthernet.setTitle(R.string.data_usage_menu_show_ethernet);
561 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700562 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700563
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700564 @Override
565 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700566 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700567 case R.id.data_usage_menu_restrict_background: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700568 final boolean restrictBackground = !mPolicyManager.getRestrictBackground();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700569 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800570 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700571 } else {
572 // no confirmation to drop restriction
573 setRestrictBackground(false);
574 }
575 return true;
576 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700577 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700578 mShowWifi = !mShowWifi;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700579 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700580 updateMenuTitles();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700581 updateTabs();
582 return true;
583 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700584 case R.id.data_usage_menu_show_ethernet: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700585 mShowEthernet = !mShowEthernet;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700586 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700587 updateMenuTitles();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700588 updateTabs();
589 return true;
590 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700591 case R.id.data_usage_menu_sim_cards: {
592 // TODO: hook up to sim cards
593 return true;
594 }
595 case R.id.data_usage_menu_cellular_networks: {
596 final Intent intent = new Intent(Intent.ACTION_MAIN);
597 intent.setComponent(new ComponentName("com.android.phone",
598 "com.android.phone.MobileNetworkSettings"));
599 startActivity(intent);
600 return true;
601 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700602 case R.id.data_usage_menu_metered: {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800603 final SettingsActivity sa = (SettingsActivity) getActivity();
604 sa.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700605 R.string.data_usage_metered_title, null, this, 0);
606 return true;
607 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700608 }
609 return false;
610 }
611
Jeff Sharkey94a90952011-06-13 22:31:09 -0700612 @Override
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700613 public void onDestroy() {
Jeff Sharkey94a90952011-06-13 22:31:09 -0700614 mDataEnabledView = null;
615 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700616
617 mUidDetailProvider.clearCache();
618 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700619
620 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700621
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800622 super.onDestroy();
623 }
624
Jeff Sharkey8a503642011-06-10 13:31:21 -0700625 /**
Jeff Sharkey92811822012-05-04 11:47:29 -0700626 * Build and assign {@link LayoutTransition} to various containers. Should
627 * only be assigned after initial layout is complete.
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700628 */
Jeff Sharkey92811822012-05-04 11:47:29 -0700629 private void ensureLayoutTransitions() {
630 // skip when already setup
631 if (mChart.getLayoutTransition() != null) return;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700632
Jeff Sharkey92811822012-05-04 11:47:29 -0700633 mTabsContainer.setLayoutTransition(buildLayoutTransition());
634 mHeader.setLayoutTransition(buildLayoutTransition());
635 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700636
Jeff Sharkey92811822012-05-04 11:47:29 -0700637 final LayoutTransition chartTransition = buildLayoutTransition();
638 chartTransition.disableTransitionType(LayoutTransition.APPEARING);
639 chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
640 mChart.setLayoutTransition(chartTransition);
641 }
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700642
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700643 private static LayoutTransition buildLayoutTransition() {
644 final LayoutTransition transition = new LayoutTransition();
645 if (TEST_ANIM) {
646 transition.setDuration(1500);
647 }
648 transition.setAnimateParentHierarchy(false);
649 return transition;
650 }
651
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700652 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700653 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700654 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
655 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700656 */
657 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700658 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700659 mTabHost.clearAllTabs();
660
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700661 final boolean mobileSplit = isMobilePolicySplit();
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700662 if (mobileSplit && hasReadyMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700663 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
664 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700665 } else if (hasReadyMobileRadio(context)) {
PauloftheWest50e6eca2014-10-03 11:07:14 -0700666 addMobileTab(context, mSubInfoList);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700667 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700668 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700669 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
670 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700671 if (mShowEthernet && hasEthernet(context)) {
672 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
673 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700674
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700675 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700676 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
677 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
678 if (mIntentTab != null) {
679 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700680 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700681 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700682 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700683 mTabHost.setCurrentTabByTag(mIntentTab);
684 }
685 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700686 } else if (noTabs) {
687 // no usable tabs, so hide body
688 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700689 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700690 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700691 }
692 }
693
Jeff Sharkey8a503642011-06-10 13:31:21 -0700694 /**
695 * Factory that provide empty {@link View} to make {@link TabHost} happy.
696 */
697 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700698 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700699 public View createTabContent(String tag) {
700 return new View(mTabHost.getContext());
701 }
702 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700703
Jeff Sharkey8a503642011-06-10 13:31:21 -0700704 /**
705 * Build {@link TabSpec} with thin indicator, and empty content.
706 */
707 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700708 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
709 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700710 }
711
PauloftheWest50e6eca2014-10-03 11:07:14 -0700712 /**
713 * Build {@link TabSpec} with thin indicator, and empty content.
714 */
Stuart Scottb1531812014-11-04 14:52:23 -0800715 private TabSpec buildTabSpec(String tag, CharSequence title) {
PauloftheWest50e6eca2014-10-03 11:07:14 -0700716 return mTabHost.newTabSpec(tag).setIndicator(title).setContent(
717 mEmptyTabContent);
718 }
719
720
Jeff Sharkey8a503642011-06-10 13:31:21 -0700721 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700722 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700723 public void onTabChanged(String tabId) {
724 // user changed tab; update body
725 updateBody();
726 }
727 };
728
729 /**
730 * Update body content based on current tab. Loads
731 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
732 * binds them to visible controls.
733 */
734 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700735 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700736 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700737
Jeff Sharkeya662e492011-06-18 21:57:06 -0700738 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700739 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700740 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700741
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700742 if (currentTab == null) {
743 Log.w(TAG, "no tab selected; hiding body");
744 mListView.setVisibility(View.GONE);
745 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700746 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700747 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700748 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700749
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700750 mCurrentTab = currentTab;
751
Jeff Sharkey8a503642011-06-10 13:31:21 -0700752 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
753
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700754 mDataEnabledSupported = isOwner;
755 mDisableAtLimitSupported = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700756
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700757 // TODO: remove mobile tabs when SIM isn't ready
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700758
PauloftheWest50e6eca2014-10-03 11:07:14 -0700759 if (isMobileTab(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700760 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
761 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
PauloftheWest50e6eca2014-10-03 11:07:14 -0700762 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context,getSubId(currentTab)));
763 mDataEnabledSupported = isMobileDataAvailable(getSubId(currentTab));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700764 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700765 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
766 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
767 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700768 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700769
770 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700771 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
772 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
773 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700774 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700775
776 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700777 // wifi doesn't have any controls
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700778 mDataEnabledSupported = false;
779 mDisableAtLimitSupported = false;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700780 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700781
782 } else if (TAB_ETHERNET.equals(currentTab)) {
783 // ethernet doesn't have any controls
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700784 mDataEnabledSupported = false;
785 mDisableAtLimitSupported = false;
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700786 mTemplate = buildTemplateEthernet();
787
788 } else {
789 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700790 }
791
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700792 // kick off loader for network history
793 // TODO: consider chaining two loaders together instead of reloading
794 // network history when showing app detail.
795 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700796 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700797
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700798 // detail mode can change visible menus, invalidate
799 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700800
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700801 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700802 }
803
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700804 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700805 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700806 }
807
808 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700809 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700810 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700811 */
812 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700813 final Context context = getActivity();
814 final PackageManager pm = context.getPackageManager();
815 final LayoutInflater inflater = getActivity().getLayoutInflater();
816
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700817 if (isAppDetailMode()) {
818 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700819 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700820 } else {
821 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700822 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700823
824 // hide detail stats when not in detail mode
825 mChart.bindDetailNetworkStats(null);
826 return;
827 }
828
829 // remove warning/limit sweeps while in detail mode
830 mChart.bindNetworkPolicy(null);
831
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700832 // show icon and all labels appearing under this app
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700833 final int uid = mCurrentApp.key;
834 final UidDetail detail = mUidDetailProvider.getUidDetail(uid, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700835 mAppIcon.setImageDrawable(detail.icon);
836
837 mAppTitles.removeAllViews();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700838
839 View title = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700840 if (detail.detailLabels != null) {
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100841 final int n = detail.detailLabels.length;
842 for (int i = 0; i < n; ++i) {
843 CharSequence label = detail.detailLabels[i];
844 CharSequence contentDescription = detail.detailContentDescriptions[i];
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700845 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100846 TextView appTitle = (TextView) title.findViewById(R.id.app_title);
847 appTitle.setText(label);
848 appTitle.setContentDescription(contentDescription);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700849 mAppTitles.addView(title);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700850 }
851 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700852 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100853 TextView appTitle = (TextView) title.findViewById(R.id.app_title);
854 appTitle.setText(detail.label);
855 appTitle.setContentDescription(detail.contentDescription);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700856 mAppTitles.addView(title);
857 }
858
859 // Remember last slot for summary
860 if (title != null) {
861 mAppTotal = (TextView) title.findViewById(R.id.app_summary);
862 } else {
863 mAppTotal = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700864 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700865
866 // enable settings button when package provides it
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700867 final String[] packageNames = pm.getPackagesForUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700868 if (packageNames != null && packageNames.length > 0) {
869 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700870 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
871
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700872 // Search for match across all packages
873 boolean matchFound = false;
874 for (String packageName : packageNames) {
875 mAppSettingsIntent.setPackage(packageName);
876 if (pm.resolveActivity(mAppSettingsIntent, 0) != null) {
877 matchFound = true;
878 break;
879 }
880 }
881
Zoltan Szatmary-Banf20d39e2014-08-07 15:27:08 +0100882 mAppSettings.setOnClickListener(new OnClickListener() {
883 @Override
884 public void onClick(View v) {
885 if (!isAdded()) {
886 return;
887 }
888
889 // TODO: target towards entire UID instead of just first package
890 getActivity().startActivityAsUser(mAppSettingsIntent,
891 new UserHandle(UserHandle.getUserId(uid)));
892 }
893 });
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700894 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -0700895 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700896
897 } else {
898 mAppSettingsIntent = null;
Zoltan Szatmary-Banf20d39e2014-08-07 15:27:08 +0100899 mAppSettings.setOnClickListener(null);
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700900 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700901 }
902
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700903 updateDetailData();
904
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700905 if (UserHandle.isApp(uid) && !mPolicyManager.getRestrictBackground()
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700906 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700907 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800908 setPreferenceSummary(mAppRestrictView,
909 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700910
911 mAppRestrictView.setVisibility(View.VISIBLE);
912 mAppRestrict.setChecked(getAppRestrictBackground());
913
914 } else {
915 mAppRestrictView.setVisibility(View.GONE);
916 }
917 }
918
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700919 private void setPolicyWarningBytes(long warningBytes) {
920 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700921 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700922 updatePolicy(false);
923 }
924
925 private void setPolicyLimitBytes(long limitBytes) {
926 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700927 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700928 updatePolicy(false);
929 }
930
Jeff Sharkey28130d92011-09-02 16:10:24 -0700931 /**
932 * Local cache of value, used to work around delay when
933 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
934 */
935 private Boolean mMobileDataEnabled;
936
Wink Savilleb003a852014-10-23 10:16:26 -0700937 private boolean isMobileDataEnabled(int subId) {
PauloftheWest50e6eca2014-10-03 11:07:14 -0700938 boolean isEnable = false;
Jeff Sharkey28130d92011-09-02 16:10:24 -0700939 if (mMobileDataEnabled != null) {
940 // TODO: deprecate and remove this once enabled flag is on policy
PauloftheWest50e6eca2014-10-03 11:07:14 -0700941 // Multiple Subscriptions, the value need to be reseted
942 isEnable = mMobileDataEnabled.booleanValue();
943 mMobileDataEnabled = null;
Jeff Sharkey28130d92011-09-02 16:10:24 -0700944 } else {
PauloftheWest50e6eca2014-10-03 11:07:14 -0700945 //SUB SELECT
PauloftheWest98523c82014-10-28 08:30:05 -0700946 isEnable = mTelephonyManager.getDataEnabled()
Wink Saville0183fb52014-11-22 10:11:39 -0800947 && (subId == mSubscriptionManager.getDefaultDataSubId());
Jeff Sharkey28130d92011-09-02 16:10:24 -0700948 }
PauloftheWest50e6eca2014-10-03 11:07:14 -0700949 return isEnable;
Jeff Sharkey28130d92011-09-02 16:10:24 -0700950 }
951
952 private void setMobileDataEnabled(boolean enabled) {
953 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700954 mTelephonyManager.setDataEnabled(enabled);
Jeff Sharkey28130d92011-09-02 16:10:24 -0700955 mMobileDataEnabled = enabled;
956 updatePolicy(false);
957 }
958
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700959 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700960 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked()
961 && ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700962 }
963
964 private boolean isBandwidthControlEnabled() {
965 try {
966 return mNetworkService.isBandwidthControlEnabled();
967 } catch (RemoteException e) {
968 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
969 return false;
970 }
971 }
972
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700973 public void setRestrictBackground(boolean restrictBackground) {
974 mPolicyManager.setRestrictBackground(restrictBackground);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700975 updateMenuTitles();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700976 }
977
978 private boolean getAppRestrictBackground() {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700979 final int uid = mCurrentApp.key;
980 final int uidPolicy = mPolicyManager.getUidPolicy(uid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700981 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
982 }
983
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700984 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700985 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700986 final int uid = mCurrentApp.key;
987 mPolicyManager.setUidPolicy(
988 uid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700989 mAppRestrict.setChecked(restrictBackground);
990 }
991
Jeff Sharkey8a503642011-06-10 13:31:21 -0700992 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700993 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
994 * current {@link #mTemplate}.
995 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700996 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700997 boolean dataEnabledVisible = mDataEnabledSupported;
998 boolean disableAtLimitVisible = mDisableAtLimitSupported;
999
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001000 if (isAppDetailMode()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001001 dataEnabledVisible = false;
1002 disableAtLimitVisible = false;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001003 }
1004
Jeff Sharkey28130d92011-09-02 16:10:24 -07001005 // TODO: move enabled state directly into policy
PauloftheWest50e6eca2014-10-03 11:07:14 -07001006 if (isMobileTab(mCurrentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001007 mBinding = true;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001008 mDataEnabled.setChecked(isMobileDataEnabled(getSubId(mCurrentTab)));
Jeff Sharkey28130d92011-09-02 16:10:24 -07001009 mBinding = false;
1010 }
1011
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001012 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001013 //SUB SELECT
1014 if (isNetworkPolicyModifiable(policy) && isMobileDataAvailable(getSubId(mCurrentTab))) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001015 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001016 if (!isAppDetailMode()) {
1017 mChart.bindNetworkPolicy(policy);
1018 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001019
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001020 } else {
1021 // controls are disabled; don't bind warning/limit sweeps
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001022 disableAtLimitVisible = false;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001023 mChart.bindNetworkPolicy(null);
1024 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001025
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001026 mDataEnabledView.setVisibility(dataEnabledVisible ? View.VISIBLE : View.GONE);
1027 mDisableAtLimitView.setVisibility(disableAtLimitVisible ? View.VISIBLE : View.GONE);
1028
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001029 if (refreshCycle) {
1030 // generate cycle list based on policy and available history
1031 updateCycleList(policy);
1032 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001033 }
1034
1035 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001036 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
1037 * and available {@link NetworkStatsHistory} data. Always selects the newest
1038 * item, updating the inspection range on {@link #mChart}.
1039 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001040 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001041 // stash away currently selected cycle to try restoring below
1042 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -07001043 mCycleAdapter.clear();
1044
1045 final Context context = mCycleSpinner.getContext();
1046
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001047 long historyStart = Long.MAX_VALUE;
1048 long historyEnd = Long.MIN_VALUE;
1049 if (mChartData != null) {
1050 historyStart = mChartData.network.getStart();
1051 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001052 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001053
Jeff Sharkey461842a2011-09-25 18:22:48 -07001054 final long now = System.currentTimeMillis();
1055 if (historyStart == Long.MAX_VALUE) historyStart = now;
1056 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001057
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001058 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001059 if (policy != null) {
1060 // find the next cycle boundary
1061 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001062
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001063 // walk backwards, generating all valid cycle ranges
1064 while (cycleEnd > historyStart) {
1065 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
1066 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
1067 + historyStart);
1068 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1069 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001070 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001071 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001072
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001073 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001074 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001075 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001076
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001077 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001078 // no policy defined cycles; show entry for each four-week period
1079 long cycleEnd = historyEnd;
1080 while (cycleEnd > historyStart) {
1081 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
1082 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1083 cycleEnd = cycleStart;
1084 }
1085
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001086 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001087 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001088
1089 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001090 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001091 final int position = mCycleAdapter.findNearestPosition(previousItem);
1092 mCycleSpinner.setSelection(position);
1093
1094 // only force-update cycle when changed; skipping preserves any
1095 // user-defined inspection region.
1096 final CycleItem selectedItem = mCycleAdapter.getItem(position);
1097 if (!Objects.equal(selectedItem, previousItem)) {
1098 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1099 } else {
1100 // but still kick off loader for detailed list
1101 updateDetailData();
1102 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001103 } else {
1104 updateDetailData();
1105 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001106 }
1107
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001108 private View.OnClickListener mDataEnabledListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001109 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001110 public void onClick(View v) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001111 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001112
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001113 final boolean dataEnabled = !mDataEnabled.isChecked();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001114 final String currentTab = mCurrentTab;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001115 if (isMobileTab(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001116 if (dataEnabled) {
PauloftheWestde338442014-10-29 14:46:44 -07001117 // If we are showing the Sim Card tile then we are a Multi-Sim device.
1118 if (Utils.showSimCardTile(getActivity())) {
1119 handleMultiSimDataDialog();
1120 } else {
1121 setMobileDataEnabled(true);
1122 }
Jeff Sharkey28130d92011-09-02 16:10:24 -07001123 } else {
1124 // disabling data; show confirmation dialog which eventually
1125 // calls setMobileDataEnabled() once user confirms.
1126 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1127 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001128 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001129
Jeff Sharkey28130d92011-09-02 16:10:24 -07001130 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001131 }
1132 };
1133
PauloftheWestde338442014-10-29 14:46:44 -07001134 private void handleMultiSimDataDialog() {
Wink Savilleca756612014-11-08 10:47:12 -08001135 final SubscriptionInfo currentSir = getCurrentTabSubInfo(getActivity());
Wink Saville0183fb52014-11-22 10:11:39 -08001136 final SubscriptionInfo nextSir = mSubscriptionManager.getActiveSubscriptionInfo(
1137 mSubscriptionManager.getDefaultDataSubId());
PauloftheWestde338442014-10-29 14:46:44 -07001138
Stuart Scott3ada2ec2014-10-31 14:11:24 -07001139 if (currentSir.getSubscriptionId() == nextSir.getSubscriptionId()) {
PauloftheWestde338442014-10-29 14:46:44 -07001140 setMobileDataEnabled(true);
1141 updateBody();
1142 return;
1143 }
1144
1145 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
1146
1147 builder.setTitle(R.string.sim_change_data_title);
1148 builder.setMessage(getActivity().getResources().getString(R.string.sim_change_data_message,
Stuart Scott3ada2ec2014-10-31 14:11:24 -07001149 currentSir.getDisplayName(), nextSir.getDisplayName()));
PauloftheWestde338442014-10-29 14:46:44 -07001150
1151 builder.setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
1152 @Override
1153 public void onClick(DialogInterface dialog, int id) {
Wink Saville0183fb52014-11-22 10:11:39 -08001154 mSubscriptionManager.setDefaultDataSubId(currentSir.getSubscriptionId());
PauloftheWestde338442014-10-29 14:46:44 -07001155 setMobileDataEnabled(true);
1156 updateBody();
1157 }
1158 });
1159 builder.setNegativeButton(R.string.cancel, null);
1160
1161 builder.create().show();
1162 }
1163
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001164 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001165 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001166 public void onClick(View v) {
1167 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001168 if (disableAtLimit) {
1169 // enabling limit; show confirmation dialog which eventually
1170 // calls setPolicyLimitBytes() once user confirms.
1171 ConfirmLimitFragment.show(DataUsageSummary.this);
1172 } else {
1173 setPolicyLimitBytes(LIMIT_DISABLED);
1174 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001175 }
1176 };
1177
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001178 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001179 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001180 public void onClick(View v) {
1181 final boolean restrictBackground = !mAppRestrict.isChecked();
1182
1183 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001184 // enabling restriction; show confirmation dialog which
1185 // eventually calls setRestrictBackground() once user
1186 // confirms.
1187 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001188 } else {
1189 setAppRestrictBackground(false);
1190 }
1191 }
1192 };
1193
Jeff Sharkey8a503642011-06-10 13:31:21 -07001194 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001195 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001196 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001197 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001198 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001199
1200 // TODO: sigh, remove this hack once we understand 6450986
1201 if (mUidDetailProvider == null || app == null) return;
1202
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001203 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001204 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001205 }
1206 };
1207
1208 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001209 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001210 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1211 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1212 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001213 // show cycle editor; will eventually call setPolicyCycleDay()
1214 // when user finishes editing.
1215 CycleEditorFragment.show(DataUsageSummary.this);
1216
1217 // reset spinner to something other than "change cycle..."
1218 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001219
1220 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001221 if (LOGD) {
1222 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1223 + cycle.end + "]");
1224 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001225
1226 // update chart to show selected cycle, and update detail data
1227 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001228 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001229
1230 updateDetailData();
1231 }
1232 }
1233
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001234 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001235 public void onNothingSelected(AdapterView<?> parent) {
1236 // ignored
1237 }
1238 };
1239
1240 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001241 * Update details based on {@link #mChart} inspection range depending on
1242 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1243 * of applications data usage, and when {@link #isAppDetailMode()} update
1244 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001245 */
1246 private void updateDetailData() {
1247 if (LOGD) Log.d(TAG, "updateDetailData()");
1248
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001249 final long start = mChart.getInspectStart();
1250 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001251 final long now = System.currentTimeMillis();
1252
1253 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001254
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001255 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001256 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001257 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001258 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001259 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001260 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001261 final long foregroundBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001262 final long totalBytes = defaultBytes + foregroundBytes;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001263
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001264 if (mAppTotal != null) {
1265 mAppTotal.setText(Formatter.formatFileSize(context, totalBytes));
1266 }
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001267 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1268 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1269
1270 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001271 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001272
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001273 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001274
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001275 mCycleSummary.setVisibility(View.GONE);
1276
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001277 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001278 if (mChartData != null) {
1279 entry = mChartData.network.getValues(start, end, now, null);
1280 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001281
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001282 mCycleSummary.setVisibility(View.VISIBLE);
1283
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001284 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001285 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001286 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001287 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001288
1289 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1290 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001291 mCycleSummary.setText(totalPhrase);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001292
PauloftheWest50e6eca2014-10-03 11:07:14 -07001293 if (isMobileTab(mCurrentTab) || TAB_3G.equals(mCurrentTab)
László Dávid0b8bf4e2012-10-24 23:31:47 +02001294 || TAB_4G.equals(mCurrentTab)) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001295 if (isAppDetailMode()) {
1296 mDisclaimer.setVisibility(View.GONE);
1297 } else {
1298 mDisclaimer.setVisibility(View.VISIBLE);
1299 }
Jeff Sharkeye557c332012-04-13 16:04:07 -07001300 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001301 mDisclaimer.setVisibility(View.GONE);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001302 }
1303
Jeff Sharkey92811822012-05-04 11:47:29 -07001304 // initial layout is finished above, ensure we have transitions
1305 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001306 }
1307
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001308 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1309 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001310 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001311 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001312 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001313 }
1314
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001315 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001316 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1317 mChartData = data;
1318 mChart.bindNetworkStats(mChartData.network);
1319 mChart.bindDetailNetworkStats(mChartData.detail);
1320
1321 // calcuate policy cycles based on available data
1322 updatePolicy(true);
1323 updateAppDetail();
1324
1325 // force scroll to top of body when showing detail
1326 if (mChartData.detail != null) {
1327 mListView.smoothScrollToPosition(0);
1328 }
1329 }
1330
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001331 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001332 public void onLoaderReset(Loader<ChartData> loader) {
1333 mChartData = null;
1334 mChart.bindNetworkStats(null);
1335 mChart.bindDetailNetworkStats(null);
1336 }
1337 };
1338
1339 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001340 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001341 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001342 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001343 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001344 }
1345
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001346 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001347 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001348 final int[] restrictedUids = mPolicyManager.getUidsWithPolicy(
Jeff Sharkeye557c332012-04-13 16:04:07 -07001349 POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001350 mAdapter.bindStats(data, restrictedUids);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001351 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001352 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001353
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001354 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001355 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001356 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001357 updateEmptyVisible();
1358 }
1359
1360 private void updateEmptyVisible() {
1361 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1362 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001363 mStupidPadding.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001364 }
1365 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001366
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001367 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001368 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001369 final Context context = getActivity();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001370 if (hasReadyMobileRadio(context)) {
1371 final TelephonyManager tele = TelephonyManager.from(context);
1372 return mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001373 } else {
1374 return false;
1375 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001376 }
1377
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001378 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001379 private void setMobilePolicySplit(boolean split) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001380 final Context context = getActivity();
1381 if (hasReadyMobileRadio(context)) {
1382 final TelephonyManager tele = TelephonyManager.from(context);
1383 mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split);
1384 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001385 }
1386
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001387 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001388 final TelephonyManager tele = TelephonyManager.from(context);
1389 final String actualSubscriberId = tele.getSubscriberId();
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -08001390 return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001391 }
1392
Wink Savilleb003a852014-10-23 10:16:26 -07001393 private static String getActiveSubscriberId(Context context, int subId) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07001394 final TelephonyManager tele = TelephonyManager.from(context);
1395 return tele.getSubscriberId(subId);
1396 }
1397
Jeff Sharkey8a503642011-06-10 13:31:21 -07001398 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001399 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001400 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001401 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001402 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001403
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001404 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001405 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001406 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001407 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001408
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001409 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001410 public void requestWarningEdit() {
1411 WarningEditorFragment.show(DataUsageSummary.this);
1412 }
1413
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001414 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001415 public void requestLimitEdit() {
1416 LimitEditorFragment.show(DataUsageSummary.this);
1417 }
1418 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001419
1420 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001421 * List item that reflects a specific data usage cycle.
1422 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001423 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001424 public CharSequence label;
1425 public long start;
1426 public long end;
1427
Jeff Sharkey8a503642011-06-10 13:31:21 -07001428 CycleItem(CharSequence label) {
1429 this.label = label;
1430 }
1431
1432 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001433 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001434 this.start = start;
1435 this.end = end;
1436 }
1437
Jeff Sharkey8a503642011-06-10 13:31:21 -07001438 @Override
1439 public String toString() {
1440 return label.toString();
1441 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001442
1443 @Override
1444 public boolean equals(Object o) {
1445 if (o instanceof CycleItem) {
1446 final CycleItem another = (CycleItem) o;
1447 return start == another.start && end == another.end;
1448 }
1449 return false;
1450 }
1451
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001452 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001453 public int compareTo(CycleItem another) {
1454 return Long.compare(start, another.start);
1455 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001456 }
1457
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001458 private static final StringBuilder sBuilder = new StringBuilder(50);
1459 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1460 sBuilder, Locale.getDefault());
1461
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001462 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001463 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001464
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001465 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001466 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001467 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1468 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001469 }
1470 }
1471
Jeff Sharkey8a503642011-06-10 13:31:21 -07001472 /**
1473 * Special-case data usage cycle that triggers dialog to change
1474 * {@link NetworkPolicy#cycleDay}.
1475 */
1476 public static class CycleChangeItem extends CycleItem {
1477 public CycleChangeItem(Context context) {
1478 super(context.getString(R.string.data_usage_change_cycle));
1479 }
1480 }
1481
1482 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001483 private boolean mChangePossible = false;
1484 private boolean mChangeVisible = false;
1485
1486 private final CycleChangeItem mChangeItem;
1487
Jeff Sharkey8a503642011-06-10 13:31:21 -07001488 public CycleAdapter(Context context) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001489 super(context, R.layout.data_usage_cycle_item);
1490 setDropDownViewResource(R.layout.data_usage_cycle_item_dropdown);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001491 mChangeItem = new CycleChangeItem(context);
1492 }
1493
1494 public void setChangePossible(boolean possible) {
1495 mChangePossible = possible;
1496 updateChange();
1497 }
1498
1499 public void setChangeVisible(boolean visible) {
1500 mChangeVisible = visible;
1501 updateChange();
1502 }
1503
1504 private void updateChange() {
1505 remove(mChangeItem);
1506 if (mChangePossible && mChangeVisible) {
1507 add(mChangeItem);
1508 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001509 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001510
1511 /**
1512 * Find position of {@link CycleItem} in this adapter which is nearest
1513 * the given {@link CycleItem}.
1514 */
1515 public int findNearestPosition(CycleItem target) {
1516 if (target != null) {
1517 final int count = getCount();
1518 for (int i = count - 1; i >= 0; i--) {
1519 final CycleItem item = getItem(i);
1520 if (item instanceof CycleChangeItem) {
1521 continue;
1522 } else if (item.compareTo(target) >= 0) {
1523 return i;
1524 }
1525 }
1526 }
1527 return 0;
1528 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001529 }
1530
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001531 public static class AppItem implements Comparable<AppItem>, Parcelable {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001532 public static final int CATEGORY_USER = 0;
1533 public static final int CATEGORY_APP_TITLE = 1;
1534 public static final int CATEGORY_APP = 2;
1535
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001536 public final int key;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001537 public boolean restricted;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001538 public int category;
1539
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001540 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001541 public long total;
1542
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001543 public AppItem() {
1544 this.key = 0;
1545 }
1546
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001547 public AppItem(int key) {
1548 this.key = key;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001549 }
1550
1551 public AppItem(Parcel parcel) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001552 key = parcel.readInt();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001553 uids = parcel.readSparseBooleanArray();
1554 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001555 }
1556
1557 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001558 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001559 }
1560
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001561 @Override
1562 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001563 dest.writeInt(key);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001564 dest.writeSparseBooleanArray(uids);
1565 dest.writeLong(total);
1566 }
1567
1568 @Override
1569 public int describeContents() {
1570 return 0;
1571 }
1572
1573 @Override
1574 public int compareTo(AppItem another) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001575 int comparison = Integer.compare(category, another.category);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001576 if (comparison == 0) {
1577 comparison = Long.compare(another.total, total);
1578 }
1579 return comparison;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001580 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001581
1582 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1583 @Override
1584 public AppItem createFromParcel(Parcel in) {
1585 return new AppItem(in);
1586 }
1587
1588 @Override
1589 public AppItem[] newArray(int size) {
1590 return new AppItem[size];
1591 }
1592 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001593 }
1594
Jeff Sharkey8a503642011-06-10 13:31:21 -07001595 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001596 * Adapter of applications, sorted by total usage descending.
1597 */
1598 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001599 private final UidDetailProvider mProvider;
1600 private final int mInsetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001601 private final UserManager mUm;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001602
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001603 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001604 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001605
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001606 public DataUsageAdapter(final UserManager userManager, UidDetailProvider provider, int insetSide) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001607 mProvider = checkNotNull(provider);
1608 mInsetSide = insetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001609 mUm = userManager;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001610 }
1611
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001612 /**
1613 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1614 */
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001615 public void bindStats(NetworkStats stats, int[] restrictedUids) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001616 mItems.clear();
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001617 mLargest = 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001618
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001619 final int currentUserId = ActivityManager.getCurrentUser();
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001620 final List<UserHandle> profiles = mUm.getUserProfiles();
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001621 final SparseArray<AppItem> knownItems = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001622
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001623 NetworkStats.Entry entry = null;
1624 final int size = stats != null ? stats.size() : 0;
1625 for (int i = 0; i < size; i++) {
1626 entry = stats.getValues(i, entry);
1627
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001628 // Decide how to collapse items together
1629 final int uid = entry.uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001630
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001631 final int collapseKey;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001632 final int category;
1633 final int userId = UserHandle.getUserId(uid);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001634 if (UserHandle.isApp(uid)) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001635 if (profiles.contains(new UserHandle(userId))) {
1636 if (userId != currentUserId) {
1637 // Add to a managed user item.
1638 final int managedKey = UidDetailProvider.buildKeyForUser(userId);
1639 accumulate(managedKey, knownItems, entry,
1640 AppItem.CATEGORY_USER);
1641 }
1642 // Add to app item.
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001643 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001644 category = AppItem.CATEGORY_APP;
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001645 } else {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001646 // Add to other user item.
1647 collapseKey = UidDetailProvider.buildKeyForUser(userId);
1648 category = AppItem.CATEGORY_USER;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001649 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001650 } else if (uid == UID_REMOVED || uid == UID_TETHERING) {
1651 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001652 category = AppItem.CATEGORY_APP;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001653 } else {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001654 collapseKey = android.os.Process.SYSTEM_UID;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001655 category = AppItem.CATEGORY_APP;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001656 }
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001657 accumulate(collapseKey, knownItems, entry, category);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001658 }
1659
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001660 final int restrictedUidsMax = restrictedUids.length;
1661 for (int i = 0; i < restrictedUidsMax; ++i) {
1662 final int uid = restrictedUids[i];
1663 // Only splice in restricted state for current user or managed users
1664 if (!profiles.contains(new UserHandle(UserHandle.getUserId(uid)))) {
1665 continue;
1666 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001667
1668 AppItem item = knownItems.get(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001669 if (item == null) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001670 item = new AppItem(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001671 item.total = -1;
1672 mItems.add(item);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001673 knownItems.put(item.key, item);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001674 }
1675 item.restricted = true;
1676 }
1677
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001678 if (!mItems.isEmpty()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001679 final AppItem title = new AppItem();
1680 title.category = AppItem.CATEGORY_APP_TITLE;
1681 mItems.add(title);
1682 }
1683
Jeff Sharkey8a503642011-06-10 13:31:21 -07001684 Collections.sort(mItems);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001685 notifyDataSetChanged();
1686 }
1687
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001688 /**
1689 * Accumulate data usage of a network stats entry for the item mapped by the collapse key.
1690 * Creates the item if needed.
1691 *
1692 * @param collapseKey the collapse key used to map the item.
1693 * @param knownItems collection of known (already existing) items.
1694 * @param entry the network stats entry to extract data usage from.
1695 * @param itemCategory the item is categorized on the list view by this category. Must be
1696 * either AppItem.APP_ITEM_CATEGORY or AppItem.MANAGED_USER_ITEM_CATEGORY
1697 */
1698 private void accumulate(int collapseKey, final SparseArray<AppItem> knownItems,
1699 NetworkStats.Entry entry, int itemCategory) {
1700 final int uid = entry.uid;
1701 AppItem item = knownItems.get(collapseKey);
1702 if (item == null) {
1703 item = new AppItem(collapseKey);
1704 item.category = itemCategory;
1705 mItems.add(item);
1706 knownItems.put(item.key, item);
1707 }
1708 item.addUid(uid);
1709 item.total += entry.rxBytes + entry.txBytes;
1710 if (mLargest < item.total) {
1711 mLargest = item.total;
1712 }
1713 }
1714
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001715 @Override
1716 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001717 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001718 }
1719
1720 @Override
1721 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001722 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001723 }
1724
1725 @Override
1726 public long getItemId(int position) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001727 return mItems.get(position).key;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001728 }
1729
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001730 /**
1731 * See {@link #getItemViewType} for the view types.
1732 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001733 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001734 public int getViewTypeCount() {
1735 return 2;
1736 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001737
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001738 /**
1739 * Returns 1 for separator items and 0 for anything else.
1740 */
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001741 @Override
1742 public int getItemViewType(int position) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001743 final AppItem item = mItems.get(position);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001744 if (item.category == AppItem.CATEGORY_APP_TITLE) {
1745 return 1;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001746 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001747 return 0;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001748 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001749 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001750
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001751 @Override
1752 public boolean areAllItemsEnabled() {
1753 return false;
1754 }
1755
1756 @Override
1757 public boolean isEnabled(int position) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001758 if (position > mItems.size()) {
1759 throw new ArrayIndexOutOfBoundsException();
1760 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001761 return getItemViewType(position) == 0;
1762 }
1763
1764 @Override
1765 public View getView(int position, View convertView, ViewGroup parent) {
1766 final AppItem item = mItems.get(position);
1767 if (getItemViewType(position) == 1) {
1768 if (convertView == null) {
1769 convertView = inflateCategoryHeader(LayoutInflater.from(parent.getContext()),
1770 parent);
1771 }
1772
1773 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
1774 title.setText(R.string.data_usage_app);
1775
1776 } else {
1777 if (convertView == null) {
1778 convertView = LayoutInflater.from(parent.getContext()).inflate(
1779 R.layout.data_usage_item, parent, false);
1780
1781 if (mInsetSide > 0) {
1782 convertView.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
1783 }
1784 }
1785
1786 final Context context = parent.getContext();
1787
1788 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
1789 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1790 android.R.id.progress);
1791
1792 // kick off async load of app details
1793 UidDetailTask.bindView(mProvider, item, convertView);
1794
1795 if (item.restricted && item.total <= 0) {
1796 text1.setText(R.string.data_usage_app_restricted);
1797 progress.setVisibility(View.GONE);
1798 } else {
1799 text1.setText(Formatter.formatFileSize(context, item.total));
1800 progress.setVisibility(View.VISIBLE);
1801 }
1802
1803 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1804 progress.setProgress(percentTotal);
1805 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001806
1807 return convertView;
1808 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001809 }
1810
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001811 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001812 * Empty {@link Fragment} that controls display of UID details in
1813 * {@link DataUsageSummary}.
1814 */
1815 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001816 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001817
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001818 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001819 if (!parent.isAdded()) return;
1820
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001821 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001822 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001823
1824 final AppDetailsFragment fragment = new AppDetailsFragment();
1825 fragment.setArguments(args);
1826 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001827 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1828 ft.add(fragment, TAG_APP_DETAILS);
1829 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001830 ft.setBreadCrumbTitle(
1831 parent.getResources().getString(R.string.data_usage_app_summary_title));
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001832 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001833 }
1834
1835 @Override
1836 public void onStart() {
1837 super.onStart();
1838 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001839 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001840 target.updateBody();
1841 }
1842
1843 @Override
1844 public void onStop() {
1845 super.onStop();
1846 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001847 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001848 target.updateBody();
1849 }
1850 }
1851
1852 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001853 * Dialog to request user confirmation before setting
1854 * {@link NetworkPolicy#limitBytes}.
1855 */
1856 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001857 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001858 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001859
1860 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001861 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001862
Jeff Sharkeyd0a55312014-08-08 10:25:24 -07001863 final NetworkPolicy policy = parent.mPolicyEditor.getPolicy(parent.mTemplate);
1864 if (policy == null) return;
1865
Jeff Sharkey461842a2011-09-25 18:22:48 -07001866 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001867 final CharSequence message;
Jeff Sharkeyd0a55312014-08-08 10:25:24 -07001868 final long minLimitBytes = (long) (policy.warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001869 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001870
1871 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001872 final String currentTab = parent.mCurrentTab;
1873 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001874 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001875 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001876 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001877 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001878 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001879 } else if (isMobileTab(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001880 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001881 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001882 } else {
1883 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001884 }
1885
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001886 final Bundle args = new Bundle();
1887 args.putCharSequence(EXTRA_MESSAGE, message);
1888 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1889
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001890 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1891 dialog.setArguments(args);
1892 dialog.setTargetFragment(parent, 0);
1893 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1894 }
1895
1896 @Override
1897 public Dialog onCreateDialog(Bundle savedInstanceState) {
1898 final Context context = getActivity();
1899
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001900 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001901 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1902
1903 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1904 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001905 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001906
1907 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001908 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001909 public void onClick(DialogInterface dialog, int which) {
1910 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1911 if (target != null) {
1912 target.setPolicyLimitBytes(limitBytes);
1913 }
1914 }
1915 });
1916
1917 return builder.create();
1918 }
1919 }
1920
1921 /**
1922 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1923 */
1924 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001925 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001926
1927 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001928 if (!parent.isAdded()) return;
1929
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001930 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001931 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001932
1933 final CycleEditorFragment dialog = new CycleEditorFragment();
1934 dialog.setArguments(args);
1935 dialog.setTargetFragment(parent, 0);
1936 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1937 }
1938
1939 @Override
1940 public Dialog onCreateDialog(Bundle savedInstanceState) {
1941 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001942 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1943 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001944
1945 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1946 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1947
1948 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1949 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1950
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001951 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1952 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001953
1954 cycleDayPicker.setMinValue(1);
1955 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001956 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001957 cycleDayPicker.setWrapSelectorWheel(true);
1958
1959 builder.setTitle(R.string.data_usage_cycle_editor_title);
1960 builder.setView(view);
1961
1962 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1963 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001964 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001965 public void onClick(DialogInterface dialog, int which) {
Jeff Sharkeyd277de92013-03-25 15:11:25 -07001966 // clear focus to finish pending text edits
1967 cycleDayPicker.clearFocus();
1968
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001969 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001970 final String cycleTimezone = new Time().timezone;
1971 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001972 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001973 }
1974 });
1975
1976 return builder.create();
1977 }
1978 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001979
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001980 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001981 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1982 */
1983 public static class WarningEditorFragment extends DialogFragment {
1984 private static final String EXTRA_TEMPLATE = "template";
1985
1986 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001987 if (!parent.isAdded()) return;
1988
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001989 final Bundle args = new Bundle();
1990 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1991
1992 final WarningEditorFragment dialog = new WarningEditorFragment();
1993 dialog.setArguments(args);
1994 dialog.setTargetFragment(parent, 0);
1995 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1996 }
1997
1998 @Override
1999 public Dialog onCreateDialog(Bundle savedInstanceState) {
2000 final Context context = getActivity();
2001 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2002 final NetworkPolicyEditor editor = target.mPolicyEditor;
2003
2004 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2005 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2006
2007 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
2008 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
2009
2010 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2011 final long warningBytes = editor.getPolicyWarningBytes(template);
2012 final long limitBytes = editor.getPolicyLimitBytes(template);
2013
2014 bytesPicker.setMinValue(0);
2015 if (limitBytes != LIMIT_DISABLED) {
2016 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
2017 } else {
2018 bytesPicker.setMaxValue(Integer.MAX_VALUE);
2019 }
2020 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
2021 bytesPicker.setWrapSelectorWheel(false);
2022
2023 builder.setTitle(R.string.data_usage_warning_editor_title);
2024 builder.setView(view);
2025
2026 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2027 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002028 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002029 public void onClick(DialogInterface dialog, int which) {
2030 // clear focus to finish pending text edits
2031 bytesPicker.clearFocus();
2032
2033 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
2034 editor.setPolicyWarningBytes(template, bytes);
2035 target.updatePolicy(false);
2036 }
2037 });
2038
2039 return builder.create();
2040 }
2041 }
2042
2043 /**
2044 * Dialog to edit {@link NetworkPolicy#limitBytes}.
2045 */
2046 public static class LimitEditorFragment extends DialogFragment {
2047 private static final String EXTRA_TEMPLATE = "template";
2048
2049 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002050 if (!parent.isAdded()) return;
2051
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002052 final Bundle args = new Bundle();
2053 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
2054
2055 final LimitEditorFragment dialog = new LimitEditorFragment();
2056 dialog.setArguments(args);
2057 dialog.setTargetFragment(parent, 0);
2058 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
2059 }
2060
2061 @Override
2062 public Dialog onCreateDialog(Bundle savedInstanceState) {
2063 final Context context = getActivity();
2064 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2065 final NetworkPolicyEditor editor = target.mPolicyEditor;
2066
2067 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2068 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2069
2070 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
2071 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
2072
2073 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2074 final long warningBytes = editor.getPolicyWarningBytes(template);
2075 final long limitBytes = editor.getPolicyLimitBytes(template);
2076
2077 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09002078 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002079 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
2080 } else {
2081 bytesPicker.setMinValue(0);
2082 }
2083 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
2084 bytesPicker.setWrapSelectorWheel(false);
2085
2086 builder.setTitle(R.string.data_usage_limit_editor_title);
2087 builder.setView(view);
2088
2089 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2090 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002091 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002092 public void onClick(DialogInterface dialog, int which) {
2093 // clear focus to finish pending text edits
2094 bytesPicker.clearFocus();
2095
2096 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
2097 editor.setPolicyLimitBytes(template, bytes);
2098 target.updatePolicy(false);
2099 }
2100 });
2101
2102 return builder.create();
2103 }
2104 }
2105 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07002106 * Dialog to request user confirmation before disabling data.
2107 */
2108 public static class ConfirmDataDisableFragment extends DialogFragment {
2109 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002110 if (!parent.isAdded()) return;
2111
Jeff Sharkey28130d92011-09-02 16:10:24 -07002112 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
2113 dialog.setTargetFragment(parent, 0);
2114 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
2115 }
2116
2117 @Override
2118 public Dialog onCreateDialog(Bundle savedInstanceState) {
2119 final Context context = getActivity();
2120
2121 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2122 builder.setMessage(R.string.data_usage_disable_mobile);
2123
2124 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002125 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07002126 public void onClick(DialogInterface dialog, int which) {
2127 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2128 if (target != null) {
2129 // TODO: extend to modify policy enabled flag.
2130 target.setMobileDataEnabled(false);
2131 }
2132 }
2133 });
2134 builder.setNegativeButton(android.R.string.cancel, null);
2135
2136 return builder.create();
2137 }
2138 }
2139
2140 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002141 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07002142 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002143 */
2144 public static class ConfirmRestrictFragment extends DialogFragment {
2145 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002146 if (!parent.isAdded()) return;
2147
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002148 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
2149 dialog.setTargetFragment(parent, 0);
2150 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
2151 }
2152
2153 @Override
2154 public Dialog onCreateDialog(Bundle savedInstanceState) {
2155 final Context context = getActivity();
2156
2157 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002158 builder.setTitle(R.string.data_usage_restrict_background_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07002159 if (Utils.hasMultipleUsers(context)) {
2160 builder.setMessage(R.string.data_usage_restrict_background_multiuser);
2161 } else {
2162 builder.setMessage(R.string.data_usage_restrict_background);
2163 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002164
2165 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002166 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002167 public void onClick(DialogInterface dialog, int which) {
2168 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2169 if (target != null) {
2170 target.setRestrictBackground(true);
2171 }
2172 }
2173 });
2174 builder.setNegativeButton(android.R.string.cancel, null);
2175
2176 return builder.create();
2177 }
2178 }
2179
2180 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002181 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
2182 * change has been denied, usually based on
2183 * {@link DataUsageSummary#hasLimitedNetworks()}.
2184 */
2185 public static class DeniedRestrictFragment extends DialogFragment {
2186 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002187 if (!parent.isAdded()) return;
2188
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002189 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
2190 dialog.setTargetFragment(parent, 0);
2191 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
2192 }
2193
2194 @Override
2195 public Dialog onCreateDialog(Bundle savedInstanceState) {
2196 final Context context = getActivity();
2197
2198 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2199 builder.setTitle(R.string.data_usage_app_restrict_background);
2200 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
2201 builder.setPositiveButton(android.R.string.ok, null);
2202
2203 return builder.create();
2204 }
2205 }
2206
2207 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002208 * Dialog to request user confirmation before setting
2209 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
2210 */
2211 public static class ConfirmAppRestrictFragment extends DialogFragment {
2212 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002213 if (!parent.isAdded()) return;
2214
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002215 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2216 dialog.setTargetFragment(parent, 0);
2217 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2218 }
2219
2220 @Override
2221 public Dialog onCreateDialog(Bundle savedInstanceState) {
2222 final Context context = getActivity();
2223
2224 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002225 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2226 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2227
2228 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002229 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002230 public void onClick(DialogInterface dialog, int which) {
2231 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2232 if (target != null) {
2233 target.setAppRestrictBackground(true);
2234 }
2235 }
2236 });
2237 builder.setNegativeButton(android.R.string.cancel, null);
2238
2239 return builder.create();
2240 }
2241 }
2242
2243 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002244 * Compute default tab that should be selected, based on
2245 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2246 */
2247 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002248 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002249 if (template == null) {
Wink Savilleb003a852014-10-23 10:16:26 -07002250 final int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
Wink Saville0183fb52014-11-22 10:11:39 -08002251 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002252 if (SubscriptionManager.isValidSubId(subId)) {
2253 return TAB_MOBILE + String.valueOf(subId);
2254 }
2255 return null;
2256 }
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002257
2258 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002259 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002260 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002261 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002262 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002263 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002264 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002265 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002266 return TAB_WIFI;
2267 default:
2268 return null;
2269 }
2270 }
2271
2272 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002273 * Background task that loads {@link UidDetail}, binding to
2274 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002275 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002276 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2277 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002278 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002279 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002280
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002281 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002282 mProvider = checkNotNull(provider);
2283 mItem = checkNotNull(item);
2284 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002285 }
2286
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002287 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002288 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002289 final UidDetailTask existing = (UidDetailTask) target.getTag();
2290 if (existing != null) {
2291 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002292 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002293
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002294 final UidDetail cachedDetail = provider.getUidDetail(item.key, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002295 if (cachedDetail != null) {
2296 bindView(cachedDetail, target);
2297 } else {
2298 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2299 AsyncTask.THREAD_POOL_EXECUTOR));
2300 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002301 }
2302
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002303 private static void bindView(UidDetail detail, View target) {
2304 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2305 final TextView title = (TextView) target.findViewById(android.R.id.title);
2306
2307 if (detail != null) {
2308 icon.setImageDrawable(detail.icon);
2309 title.setText(detail.label);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +01002310 title.setContentDescription(detail.contentDescription);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002311 } else {
2312 icon.setImageDrawable(null);
2313 title.setText(null);
2314 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002315 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002316
2317 @Override
2318 protected void onPreExecute() {
2319 bindView(null, mTarget);
2320 }
2321
2322 @Override
2323 protected UidDetail doInBackground(Void... params) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002324 return mProvider.getUidDetail(mItem.key, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002325 }
2326
2327 @Override
2328 protected void onPostExecute(UidDetail result) {
2329 bindView(result, mTarget);
2330 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002331 }
2332
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002333 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002334 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002335 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002336 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002337 if (TEST_RADIOS) {
2338 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2339 }
2340
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002341 final ConnectivityManager conn = ConnectivityManager.from(context);
2342 final TelephonyManager tele = TelephonyManager.from(context);
2343
Wink Saville0183fb52014-11-22 10:11:39 -08002344 final List<SubscriptionInfo> subInfoList =
2345 SubscriptionManager.from(context).getActiveSubscriptionInfoList();
2346 // No activated Subscriptions
PauloftheWest50e6eca2014-10-03 11:07:14 -07002347 if (subInfoList == null) {
2348 return false;
2349 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002350 // require both supported network and ready SIM
PauloftheWest50e6eca2014-10-03 11:07:14 -07002351 boolean isReady = true;
Wink Savilleca756612014-11-08 10:47:12 -08002352 for (SubscriptionInfo subInfo : subInfoList) {
Stuart Scott3ada2ec2014-10-31 14:11:24 -07002353 isReady = isReady & tele.getSimState(subInfo.getSimSlotIndex()) == SIM_STATE_READY;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002354 }
2355 return conn.isNetworkSupported(TYPE_MOBILE) && isReady;
2356 }
2357
2358 /*
2359 * TODO: consider adding to TelephonyManager or SubscritpionManager.
2360 */
Wink Savilleb003a852014-10-23 10:16:26 -07002361 public static boolean hasReadyMobileRadio(Context context, int subId) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07002362 if (TEST_RADIOS) {
2363 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2364 }
2365
2366 final ConnectivityManager conn = ConnectivityManager.from(context);
2367 final TelephonyManager tele = TelephonyManager.from(context);
2368 final int slotId = SubscriptionManager.getSlotId(subId);
2369 final boolean isReady = tele.getSimState(slotId) == SIM_STATE_READY;
2370
2371 return conn.isNetworkSupported(TYPE_MOBILE) && isReady;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002372 }
2373
2374 /**
2375 * Test if device has a mobile 4G data radio.
2376 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002377 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002378 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2379 return false;
2380 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002381 if (TEST_RADIOS) {
2382 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2383 }
2384
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002385 final ConnectivityManager conn = ConnectivityManager.from(context);
2386 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002387
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002388 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Wink Saville55434042012-06-14 12:33:43 -07002389 final boolean hasLte = (tele.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE)
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002390 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002391 return hasWimax || hasLte;
2392 }
2393
2394 /**
2395 * Test if device has a Wi-Fi data radio.
2396 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002397 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002398 if (TEST_RADIOS) {
2399 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2400 }
2401
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002402 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002403 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002404 }
2405
2406 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002407 * Test if device has an ethernet network connection.
2408 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002409 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002410 if (TEST_RADIOS) {
2411 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2412 }
2413
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002414 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002415 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2416
2417 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002418 if (mStatsSession != null) {
2419 try {
2420 ethernetBytes = mStatsSession.getSummaryForNetwork(
2421 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2422 .getTotalBytes();
2423 } catch (RemoteException e) {
2424 throw new RuntimeException(e);
2425 }
2426 } else {
2427 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002428 }
2429
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002430 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002431 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002432 }
2433
2434 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002435 * Inflate a {@link Preference} style layout, adding the given {@link View}
2436 * widget into {@link android.R.id#widget_frame}.
2437 */
2438 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2439 final View view = inflater.inflate(R.layout.preference, root, false);
2440 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2441 android.R.id.widget_frame);
2442 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2443 return view;
2444 }
2445
Jeff Sharkeyb6548462014-07-21 15:38:06 -07002446 private static View inflateCategoryHeader(LayoutInflater inflater, ViewGroup root) {
2447 final TypedArray a = inflater.getContext().obtainStyledAttributes(null,
2448 com.android.internal.R.styleable.Preference,
2449 com.android.internal.R.attr.preferenceCategoryStyle, 0);
2450 final int resId = a.getResourceId(com.android.internal.R.styleable.Preference_layout, 0);
2451 return inflater.inflate(resId, root, false);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002452 }
2453
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002454 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002455 * Test if any networks are currently limited.
2456 */
2457 private boolean hasLimitedNetworks() {
2458 return !buildLimitedNetworksList().isEmpty();
2459 }
2460
2461 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002462 * Build string describing currently limited networks, which defines when
2463 * background data is restricted.
2464 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002465 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002466 private CharSequence buildLimitedNetworksString() {
2467 final List<CharSequence> limited = buildLimitedNetworksList();
2468
2469 // handle case where no networks limited
2470 if (limited.isEmpty()) {
2471 limited.add(getText(R.string.data_usage_list_none));
2472 }
2473
2474 return TextUtils.join(limited);
2475 }
2476
2477 /**
2478 * Build list of currently limited networks, which defines when background
2479 * data is restricted.
2480 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002481 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002482 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002483 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002484
2485 // build combined list of all limited networks
2486 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002487
2488 final TelephonyManager tele = TelephonyManager.from(context);
2489 if (tele.getSimState() == SIM_STATE_READY) {
2490 final String subscriberId = getActiveSubscriberId(context);
2491 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2492 limited.add(getText(R.string.data_usage_list_mobile));
2493 }
2494 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2495 limited.add(getText(R.string.data_usage_tab_3g));
2496 }
2497 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2498 limited.add(getText(R.string.data_usage_tab_4g));
2499 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002500 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002501
2502 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002503 limited.add(getText(R.string.data_usage_tab_wifi));
2504 }
2505 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2506 limited.add(getText(R.string.data_usage_tab_ethernet));
2507 }
2508
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002509 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002510 }
2511
2512 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002513 * Inset both selector and divider {@link Drawable} on the given
2514 * {@link ListView} by the requested dimensions.
2515 */
2516 private static void insetListViewDrawables(ListView view, int insetSide) {
2517 final Drawable selector = view.getSelector();
2518 final Drawable divider = view.getDivider();
2519
2520 // fully unregister these drawables so callbacks can be maintained after
2521 // wrapping below.
2522 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2523 view.setSelector(stub);
2524 view.setDivider(stub);
2525
2526 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2527 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2528 }
2529
2530 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002531 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002532 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002533 */
2534 private static void setPreferenceTitle(View parent, int resId) {
2535 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2536 title.setText(resId);
2537 }
2538
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002539 /**
2540 * Set {@link android.R.id#summary} for a preference view inflated with
2541 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2542 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002543 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002544 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2545 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002546 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002547 }
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002548
2549 /**
2550 * For search
2551 */
2552 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -07002553 new BaseSearchIndexProvider() {
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002554 @Override
2555 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
2556 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
2557
2558 final Resources res = context.getResources();
2559
2560 // Add fragment title
2561 SearchIndexableRaw data = new SearchIndexableRaw(context);
2562 data.title = res.getString(R.string.data_usage_summary_title);
2563 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2564 result.add(data);
2565
2566 // Mobile data
2567 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002568 data.key = DATA_USAGE_ENABLE_MOBILE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002569 data.title = res.getString(R.string.data_usage_enable_mobile);
2570 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2571 result.add(data);
2572
2573 // Set mobile data limit
2574 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002575 data.key = DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002576 data.title = res.getString(R.string.data_usage_disable_mobile_limit);
2577 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2578 result.add(data);
2579
Fabrice Di Megliof2a52262014-04-17 17:20:27 -07002580 // Data usage cycle
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002581 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002582 data.key = DATA_USAGE_CYCLE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002583 data.title = res.getString(R.string.data_usage_cycle);
2584 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2585 result.add(data);
2586
2587 return result;
2588 }
2589 };
2590
Wink Savilleca756612014-11-08 10:47:12 -08002591 private void addMobileTab(Context context, List<SubscriptionInfo> subInfoList) {
Wink Saville0183fb52014-11-22 10:11:39 -08002592 if (subInfoList != null && mMobileTagMap != null) {
Wink Savilleca756612014-11-08 10:47:12 -08002593 for (SubscriptionInfo subInfo : mSubInfoList) {
Stuart Scott3ada2ec2014-10-31 14:11:24 -07002594 if (hasReadyMobileRadio(context, subInfo.getSubscriptionId())) {
2595 mTabHost.addTab(buildTabSpec(mMobileTagMap.get(subInfo.getSubscriptionId()),
Stuart Scottb1531812014-11-04 14:52:23 -08002596 subInfo.getDisplayName()));
PauloftheWest50e6eca2014-10-03 11:07:14 -07002597 }
2598 }
2599 }
2600 }
2601
Wink Savilleca756612014-11-08 10:47:12 -08002602 private SubscriptionInfo getCurrentTabSubInfo(Context context) {
PauloftheWestde338442014-10-29 14:46:44 -07002603 if (mSubInfoList != null && mTabHost != null) {
2604 final int currentTagIndex = mTabHost.getCurrentTab();
2605 int i = 0;
Wink Savilleca756612014-11-08 10:47:12 -08002606 for (SubscriptionInfo subInfo : mSubInfoList) {
Stuart Scott3ada2ec2014-10-31 14:11:24 -07002607 if (hasReadyMobileRadio(context, subInfo.getSubscriptionId())) {
PauloftheWestde338442014-10-29 14:46:44 -07002608 if (i++ == currentTagIndex) {
2609 return subInfo;
2610 }
2611 }
2612 }
2613 }
2614 return null;
2615 }
2616
PauloftheWest50e6eca2014-10-03 11:07:14 -07002617 /**
2618 * Init a map with subId key and mobile tag name
2619 * @param subInfoList The subscription Info List
2620 * @return The map or null if no activated subscription
2621 */
Wink Savilleca756612014-11-08 10:47:12 -08002622 private Map<Integer, String> initMobileTabTag(List<SubscriptionInfo> subInfoList) {
Wink Savilleb003a852014-10-23 10:16:26 -07002623 Map<Integer, String> map = null;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002624 if (subInfoList != null) {
2625 String mobileTag;
Wink Savilleb003a852014-10-23 10:16:26 -07002626 map = new HashMap<Integer, String>();
Wink Savilleca756612014-11-08 10:47:12 -08002627 for (SubscriptionInfo subInfo : subInfoList) {
Stuart Scott3ada2ec2014-10-31 14:11:24 -07002628 mobileTag = TAB_MOBILE + String.valueOf(subInfo.getSubscriptionId());
2629 map.put(subInfo.getSubscriptionId(), mobileTag);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002630 }
2631 }
2632 return map;
2633 }
2634
2635 private static boolean isMobileTab(String currentTab) {
2636 return currentTab != null ? currentTab.contains(TAB_MOBILE) : false;
2637 }
2638
Wink Savilleb003a852014-10-23 10:16:26 -07002639 private int getSubId(String currentTab) {
Wink Saville0183fb52014-11-22 10:11:39 -08002640 if (mMobileTagMap != null) {
2641 Set<Integer> set = mMobileTagMap.keySet();
2642 for (Integer subId : set) {
2643 if (mMobileTagMap.get(subId).equals(currentTab)) {
2644 return subId;
2645 }
PauloftheWest50e6eca2014-10-03 11:07:14 -07002646 }
2647 }
2648 Log.e(TAG, "currentTab = " + currentTab + " non mobile tab called this function");
2649 return -1;
2650 }
2651
2652 //SUB SELECT
PauloftheWest98523c82014-10-28 08:30:05 -07002653 private boolean isMobileDataAvailable(long subId) {
Wink Savilleb003a852014-10-23 10:16:26 -07002654 int[] subIds = SubscriptionManager.getSubId(PhoneConstants.SUB1);
PauloftheWest98523c82014-10-28 08:30:05 -07002655 if (subIds != null && subIds[0] == subId) {
2656 return true;
2657 }
2658
2659 subIds = SubscriptionManager.getSubId(PhoneConstants.SUB2);
2660 if (subIds != null && subIds[0] == subId) {
2661 return true;
2662 }
2663
2664 subIds = SubscriptionManager.getSubId(PhoneConstants.SUB3);
2665 if (subIds != null && subIds[0] == subId) {
2666 return true;
2667 }
2668 return false;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002669 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002670}