blob: e9aaf0f7a2614295770fe42927bd7a72ce1b7e25 [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;
PauloftheWest50e6eca2014-10-03 11:07:14 -070091import android.telephony.SubInfoRecord;
92import 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;
198
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700199 private INetworkStatsSession mStatsSession;
200
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700201 private static final String PREF_FILE = "data_usage";
202 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700203 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700204
205 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700206
Jeff Sharkey8a503642011-06-10 13:31:21 -0700207 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700208 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700209 private TabWidget mTabWidget;
210 private ListView mListView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700211 private DataUsageAdapter mAdapter;
212
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700213 /** Distance to inset content from sides, when needed. */
214 private int mInsetSide = 0;
215
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700216 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700217
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700218 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700219 private LinearLayout mNetworkSwitches;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700220 private boolean mDataEnabledSupported;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700221 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700222 private View mDataEnabledView;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700223 private boolean mDisableAtLimitSupported;
224 private Switch mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700225 private View mDisableAtLimitView;
226
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700227 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700228 private Spinner mCycleSpinner;
229 private CycleAdapter mCycleAdapter;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700230 private TextView mCycleSummary;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700231
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700232 private ChartDataUsageView mChart;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700233 private View mDisclaimer;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700234 private TextView mEmpty;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700235 private View mStupidPadding;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700236
237 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700238 private ImageView mAppIcon;
239 private ViewGroup mAppTitles;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700240 private TextView mAppTotal;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700241 private TextView mAppForeground;
242 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700243 private Button mAppSettings;
244
245 private LinearLayout mAppSwitches;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700246 private Switch mAppRestrict;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700247 private View mAppRestrictView;
248
Jeff Sharkey8a503642011-06-10 13:31:21 -0700249 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700250 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700251
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700252 private NetworkTemplate mTemplate;
253 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700254
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700255 private AppItem mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700256
257 private Intent mAppSettingsIntent;
258
Jeff Sharkeya662e492011-06-18 21:57:06 -0700259 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700260
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700261 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700262 private String mIntentTab = null;
263
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700264 private MenuItem mMenuRestrictBackground;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700265 private MenuItem mMenuShowWifi;
266 private MenuItem mMenuShowEthernet;
267 private MenuItem mMenuSimCards;
268 private MenuItem mMenuCellularNetworks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700269
PauloftheWest50e6eca2014-10-03 11:07:14 -0700270 private List<SubInfoRecord> mSubInfoList;
271 private Map<Long,String> mMobileTagMap;
272
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700273 /** Flag used to ignore listeners during binding. */
274 private boolean mBinding;
275
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700276 private UidDetailProvider mUidDetailProvider;
277
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700278 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700279 public void onCreate(Bundle savedInstanceState) {
280 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700281 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700282
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700283 mNetworkService = INetworkManagementService.Stub.asInterface(
284 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700285 mStatsService = INetworkStatsService.Stub.asInterface(
286 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700287 mPolicyManager = NetworkPolicyManager.from(context);
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700288 mTelephonyManager = TelephonyManager.from(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700289
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700290 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700291
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700292 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700293 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700294
PauloftheWest50e6eca2014-10-03 11:07:14 -0700295 mSubInfoList = SimSettings.getSortedSubInfoList(getActivity());
296 mMobileTagMap = initMobileTabTag(mSubInfoList);
297
Jaewan Kimffce9c12013-03-19 16:53:45 +0900298 try {
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700299 if (!mNetworkService.isBandwidthControlEnabled()) {
300 Log.w(TAG, "No bandwidth control; leaving");
301 getActivity().finish();
302 }
303 } catch (RemoteException e) {
304 Log.w(TAG, "No bandwidth control; leaving");
305 getActivity().finish();
306 }
307
308 try {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900309 mStatsSession = mStatsService.openSession();
310 } catch (RemoteException e) {
311 throw new RuntimeException(e);
312 }
313
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700314 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700315 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700316
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700317 // override preferences when no mobile radio
318 if (!hasReadyMobileRadio(context)) {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900319 mShowWifi = true;
320 mShowEthernet = true;
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700321 }
322
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700323 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700324 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700325
Jeff Sharkey8a503642011-06-10 13:31:21 -0700326 @Override
327 public View onCreateView(LayoutInflater inflater, ViewGroup container,
328 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700329
Jeff Sharkey8a503642011-06-10 13:31:21 -0700330 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700331 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
332
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700333 mUidDetailProvider = new UidDetailProvider(context);
334
Jeff Sharkey8a503642011-06-10 13:31:21 -0700335 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700336 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700337 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
338 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700339
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700340 // decide if we need to manually inset our content, or if we should rely
341 // on parent container for inset.
342 final boolean shouldInset = mListView.getScrollBarStyle()
343 == View.SCROLLBARS_OUTSIDE_OVERLAY;
Amith Yamasani56f51a82013-08-05 10:07:23 -0700344 mInsetSide = 0;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700345
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700346 // adjust padding around tabwidget as needed
Amith Yamasani56f51a82013-08-05 10:07:23 -0700347 prepareCustomPreferencesList(container, view, mListView, false);
Jeff Sharkey5d706792011-09-08 18:57:17 -0700348
Jeff Sharkey8a503642011-06-10 13:31:21 -0700349 mTabHost.setup();
350 mTabHost.setOnTabChangedListener(mTabListener);
351
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700352 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700353 mHeader.setClickable(true);
354
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700355 mListView.addHeaderView(new View(context), null, true);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700356 mListView.addHeaderView(mHeader, null, true);
357 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700358
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700359 if (mInsetSide > 0) {
360 // inset selector and divider drawables
361 insetListViewDrawables(mListView, mInsetSide);
Fabrice Di Megliob27223f2013-01-15 18:54:11 -0800362 mHeader.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700363 }
364
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700365 {
366 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700367 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
368 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700369 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700370
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700371 mDataEnabled = new Switch(inflater.getContext());
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700372 mDataEnabled.setClickable(false);
373 mDataEnabled.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700374 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700375 mDataEnabledView.setTag(R.id.preference_highlight_key,
376 DATA_USAGE_ENABLE_MOBILE_KEY);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700377 mDataEnabledView.setClickable(true);
378 mDataEnabledView.setFocusable(true);
379 mDataEnabledView.setOnClickListener(mDataEnabledListener);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700380 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700381
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700382 mDisableAtLimit = new Switch(inflater.getContext());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700383 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700384 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700385 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700386 mDisableAtLimitView.setTag(R.id.preference_highlight_key,
387 DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700388 mDisableAtLimitView.setClickable(true);
389 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700390 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
391 mNetworkSwitches.addView(mDisableAtLimitView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700392
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700393 mCycleView = inflater.inflate(R.layout.data_usage_cycles, mNetworkSwitches, false);
394 mCycleView.setTag(R.id.preference_highlight_key, DATA_USAGE_CYCLE_KEY);
395 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
396 mCycleAdapter = new CycleAdapter(context);
397 mCycleSpinner.setAdapter(mCycleAdapter);
398 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
399 mCycleSummary = (TextView) mCycleView.findViewById(R.id.cycle_summary);
400 mNetworkSwitches.addView(mCycleView);
401 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700402
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700403 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700404 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800405 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700406
407 {
408 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700409 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700410 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
411 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700412 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
413 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700414 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700415
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700416 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700417
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700418 mAppRestrict = new Switch(inflater.getContext());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700419 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700420 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700421 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700422 mAppRestrictView.setClickable(true);
423 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700424 mAppRestrictView.setOnClickListener(mAppRestrictListener);
425 mAppSwitches.addView(mAppRestrictView);
426 }
427
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700428 mDisclaimer = mHeader.findViewById(R.id.disclaimer);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700429 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700430 mStupidPadding = mHeader.findViewById(R.id.stupid_padding);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700431
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +0100432 final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
433 mAdapter = new DataUsageAdapter(um, mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700434 mListView.setOnItemClickListener(mListListener);
435 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700436
437 return view;
438 }
439
440 @Override
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700441 public void onViewStateRestored(Bundle savedInstanceState) {
442 super.onViewStateRestored(savedInstanceState);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700443
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700444 // pick default tab based on incoming intent
445 final Intent intent = getActivity().getIntent();
446 mIntentTab = computeTabFromIntent(intent);
447
Jeff Sharkey8a503642011-06-10 13:31:21 -0700448 // this kicks off chain reaction which creates tabs, binds the body to
449 // selected network, and binds chart, cycles and detail list.
450 updateTabs();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700451 }
452
453 @Override
454 public void onResume() {
455 super.onResume();
456
457 getView().post(new Runnable() {
458 @Override
459 public void run() {
460 highlightViewIfNeeded();
461 }
462 });
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700463
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700464 // kick off background task to update stats
465 new AsyncTask<Void, Void, Void>() {
466 @Override
467 protected Void doInBackground(Void... params) {
468 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700469 // wait a few seconds before kicking off
470 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700471 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700472 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700473 } catch (RemoteException e) {
474 }
475 return null;
476 }
477
478 @Override
479 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700480 if (isAdded()) {
481 updateBody();
482 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700483 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700484 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700485 }
486
Jeff Sharkey8a503642011-06-10 13:31:21 -0700487 @Override
488 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
489 inflater.inflate(R.menu.data_usage, menu);
490 }
491
492 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700493 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700494 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700495 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700496 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700497
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700498 mMenuShowWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700499 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
500 mMenuShowWifi.setVisible(!appDetailMode);
501 } else {
502 mMenuShowWifi.setVisible(false);
503 }
504
505 mMenuShowEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
506 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
507 mMenuShowEthernet.setVisible(!appDetailMode);
508 } else {
509 mMenuShowEthernet.setVisible(false);
510 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700511
512 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700513 mMenuRestrictBackground.setVisible(
514 hasReadyMobileRadio(context) && isOwner && !appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700515
516 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
517 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700518 metered.setVisible(!appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700519 } else {
520 metered.setVisible(false);
521 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700522
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700523 // TODO: show when multiple sims available
524 mMenuSimCards = menu.findItem(R.id.data_usage_menu_sim_cards);
525 mMenuSimCards.setVisible(false);
526
527 mMenuCellularNetworks = menu.findItem(R.id.data_usage_menu_cellular_networks);
PauloftheWesta4b8fb32014-09-18 10:12:25 -0700528 mMenuCellularNetworks.setVisible(hasReadyMobileRadio(context)
529 && !appDetailMode && isOwner);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700530
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700531 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
532 String helpUrl;
533 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800534 HelpUtils.prepareHelpMenuItem(context, help, helpUrl);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700535 } else {
536 help.setVisible(false);
537 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700538
539 updateMenuTitles();
540 }
541
542 private void updateMenuTitles() {
543 if (mPolicyManager.getRestrictBackground()) {
544 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_allow_background);
545 } else {
546 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_restrict_background);
547 }
548
549 if (mShowWifi) {
550 mMenuShowWifi.setTitle(R.string.data_usage_menu_hide_wifi);
551 } else {
552 mMenuShowWifi.setTitle(R.string.data_usage_menu_show_wifi);
553 }
554
555 if (mShowEthernet) {
556 mMenuShowEthernet.setTitle(R.string.data_usage_menu_hide_ethernet);
557 } else {
558 mMenuShowEthernet.setTitle(R.string.data_usage_menu_show_ethernet);
559 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700560 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700561
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700562 @Override
563 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700564 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700565 case R.id.data_usage_menu_restrict_background: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700566 final boolean restrictBackground = !mPolicyManager.getRestrictBackground();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700567 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800568 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700569 } else {
570 // no confirmation to drop restriction
571 setRestrictBackground(false);
572 }
573 return true;
574 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700575 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700576 mShowWifi = !mShowWifi;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700577 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700578 updateMenuTitles();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700579 updateTabs();
580 return true;
581 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700582 case R.id.data_usage_menu_show_ethernet: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700583 mShowEthernet = !mShowEthernet;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700584 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700585 updateMenuTitles();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700586 updateTabs();
587 return true;
588 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700589 case R.id.data_usage_menu_sim_cards: {
590 // TODO: hook up to sim cards
591 return true;
592 }
593 case R.id.data_usage_menu_cellular_networks: {
594 final Intent intent = new Intent(Intent.ACTION_MAIN);
595 intent.setComponent(new ComponentName("com.android.phone",
596 "com.android.phone.MobileNetworkSettings"));
597 startActivity(intent);
598 return true;
599 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700600 case R.id.data_usage_menu_metered: {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800601 final SettingsActivity sa = (SettingsActivity) getActivity();
602 sa.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700603 R.string.data_usage_metered_title, null, this, 0);
604 return true;
605 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700606 }
607 return false;
608 }
609
Jeff Sharkey94a90952011-06-13 22:31:09 -0700610 @Override
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700611 public void onDestroy() {
Jeff Sharkey94a90952011-06-13 22:31:09 -0700612 mDataEnabledView = null;
613 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700614
615 mUidDetailProvider.clearCache();
616 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700617
618 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700619
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800620 super.onDestroy();
621 }
622
Jeff Sharkey8a503642011-06-10 13:31:21 -0700623 /**
Jeff Sharkey92811822012-05-04 11:47:29 -0700624 * Build and assign {@link LayoutTransition} to various containers. Should
625 * only be assigned after initial layout is complete.
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700626 */
Jeff Sharkey92811822012-05-04 11:47:29 -0700627 private void ensureLayoutTransitions() {
628 // skip when already setup
629 if (mChart.getLayoutTransition() != null) return;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700630
Jeff Sharkey92811822012-05-04 11:47:29 -0700631 mTabsContainer.setLayoutTransition(buildLayoutTransition());
632 mHeader.setLayoutTransition(buildLayoutTransition());
633 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700634
Jeff Sharkey92811822012-05-04 11:47:29 -0700635 final LayoutTransition chartTransition = buildLayoutTransition();
636 chartTransition.disableTransitionType(LayoutTransition.APPEARING);
637 chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
638 mChart.setLayoutTransition(chartTransition);
639 }
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700640
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700641 private static LayoutTransition buildLayoutTransition() {
642 final LayoutTransition transition = new LayoutTransition();
643 if (TEST_ANIM) {
644 transition.setDuration(1500);
645 }
646 transition.setAnimateParentHierarchy(false);
647 return transition;
648 }
649
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700650 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700651 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700652 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
653 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700654 */
655 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700656 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700657 mTabHost.clearAllTabs();
658
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700659 final boolean mobileSplit = isMobilePolicySplit();
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700660 if (mobileSplit && hasReadyMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700661 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
662 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700663 } else if (hasReadyMobileRadio(context)) {
PauloftheWest50e6eca2014-10-03 11:07:14 -0700664 addMobileTab(context, mSubInfoList);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700665 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700666 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700667 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
668 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700669 if (mShowEthernet && hasEthernet(context)) {
670 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
671 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700672
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700673 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700674 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
675 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
676 if (mIntentTab != null) {
677 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700678 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700679 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700680 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700681 mTabHost.setCurrentTabByTag(mIntentTab);
682 }
683 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700684 } else if (noTabs) {
685 // no usable tabs, so hide body
686 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700687 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700688 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700689 }
690 }
691
Jeff Sharkey8a503642011-06-10 13:31:21 -0700692 /**
693 * Factory that provide empty {@link View} to make {@link TabHost} happy.
694 */
695 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700696 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700697 public View createTabContent(String tag) {
698 return new View(mTabHost.getContext());
699 }
700 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700701
Jeff Sharkey8a503642011-06-10 13:31:21 -0700702 /**
703 * Build {@link TabSpec} with thin indicator, and empty content.
704 */
705 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700706 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
707 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700708 }
709
PauloftheWest50e6eca2014-10-03 11:07:14 -0700710 /**
711 * Build {@link TabSpec} with thin indicator, and empty content.
712 */
713 private TabSpec buildTabSpec(String tag, String title) {
714 return mTabHost.newTabSpec(tag).setIndicator(title).setContent(
715 mEmptyTabContent);
716 }
717
718
Jeff Sharkey8a503642011-06-10 13:31:21 -0700719 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700720 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700721 public void onTabChanged(String tabId) {
722 // user changed tab; update body
723 updateBody();
724 }
725 };
726
727 /**
728 * Update body content based on current tab. Loads
729 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
730 * binds them to visible controls.
731 */
732 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700733 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700734 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700735
Jeff Sharkeya662e492011-06-18 21:57:06 -0700736 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700737 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700738 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700739
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700740 if (currentTab == null) {
741 Log.w(TAG, "no tab selected; hiding body");
742 mListView.setVisibility(View.GONE);
743 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700744 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700745 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700746 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700747
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700748 mCurrentTab = currentTab;
749
Jeff Sharkey8a503642011-06-10 13:31:21 -0700750 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
751
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700752 mDataEnabledSupported = isOwner;
753 mDisableAtLimitSupported = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700754
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700755 // TODO: remove mobile tabs when SIM isn't ready
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700756
PauloftheWest50e6eca2014-10-03 11:07:14 -0700757 if (isMobileTab(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700758 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
759 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
PauloftheWest50e6eca2014-10-03 11:07:14 -0700760 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context,getSubId(currentTab)));
761 mDataEnabledSupported = isMobileDataAvailable(getSubId(currentTab));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700762 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700763 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
764 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
765 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700766 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700767
768 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700769 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
770 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
771 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700772 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700773
774 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700775 // wifi doesn't have any controls
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700776 mDataEnabledSupported = false;
777 mDisableAtLimitSupported = false;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700778 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700779
780 } else if (TAB_ETHERNET.equals(currentTab)) {
781 // ethernet doesn't have any controls
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700782 mDataEnabledSupported = false;
783 mDisableAtLimitSupported = false;
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700784 mTemplate = buildTemplateEthernet();
785
786 } else {
787 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700788 }
789
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700790 // kick off loader for network history
791 // TODO: consider chaining two loaders together instead of reloading
792 // network history when showing app detail.
793 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700794 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700795
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700796 // detail mode can change visible menus, invalidate
797 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700798
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700799 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700800 }
801
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700802 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700803 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700804 }
805
806 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700807 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700808 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700809 */
810 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700811 final Context context = getActivity();
812 final PackageManager pm = context.getPackageManager();
813 final LayoutInflater inflater = getActivity().getLayoutInflater();
814
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700815 if (isAppDetailMode()) {
816 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700817 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700818 } else {
819 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700820 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700821
822 // hide detail stats when not in detail mode
823 mChart.bindDetailNetworkStats(null);
824 return;
825 }
826
827 // remove warning/limit sweeps while in detail mode
828 mChart.bindNetworkPolicy(null);
829
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700830 // show icon and all labels appearing under this app
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700831 final int uid = mCurrentApp.key;
832 final UidDetail detail = mUidDetailProvider.getUidDetail(uid, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700833 mAppIcon.setImageDrawable(detail.icon);
834
835 mAppTitles.removeAllViews();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700836
837 View title = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700838 if (detail.detailLabels != null) {
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100839 final int n = detail.detailLabels.length;
840 for (int i = 0; i < n; ++i) {
841 CharSequence label = detail.detailLabels[i];
842 CharSequence contentDescription = detail.detailContentDescriptions[i];
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700843 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100844 TextView appTitle = (TextView) title.findViewById(R.id.app_title);
845 appTitle.setText(label);
846 appTitle.setContentDescription(contentDescription);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700847 mAppTitles.addView(title);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700848 }
849 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700850 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100851 TextView appTitle = (TextView) title.findViewById(R.id.app_title);
852 appTitle.setText(detail.label);
853 appTitle.setContentDescription(detail.contentDescription);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700854 mAppTitles.addView(title);
855 }
856
857 // Remember last slot for summary
858 if (title != null) {
859 mAppTotal = (TextView) title.findViewById(R.id.app_summary);
860 } else {
861 mAppTotal = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700862 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700863
864 // enable settings button when package provides it
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700865 final String[] packageNames = pm.getPackagesForUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700866 if (packageNames != null && packageNames.length > 0) {
867 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700868 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
869
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700870 // Search for match across all packages
871 boolean matchFound = false;
872 for (String packageName : packageNames) {
873 mAppSettingsIntent.setPackage(packageName);
874 if (pm.resolveActivity(mAppSettingsIntent, 0) != null) {
875 matchFound = true;
876 break;
877 }
878 }
879
Zoltan Szatmary-Banf20d39e2014-08-07 15:27:08 +0100880 mAppSettings.setOnClickListener(new OnClickListener() {
881 @Override
882 public void onClick(View v) {
883 if (!isAdded()) {
884 return;
885 }
886
887 // TODO: target towards entire UID instead of just first package
888 getActivity().startActivityAsUser(mAppSettingsIntent,
889 new UserHandle(UserHandle.getUserId(uid)));
890 }
891 });
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700892 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -0700893 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700894
895 } else {
896 mAppSettingsIntent = null;
Zoltan Szatmary-Banf20d39e2014-08-07 15:27:08 +0100897 mAppSettings.setOnClickListener(null);
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700898 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700899 }
900
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700901 updateDetailData();
902
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700903 if (UserHandle.isApp(uid) && !mPolicyManager.getRestrictBackground()
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700904 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700905 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800906 setPreferenceSummary(mAppRestrictView,
907 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700908
909 mAppRestrictView.setVisibility(View.VISIBLE);
910 mAppRestrict.setChecked(getAppRestrictBackground());
911
912 } else {
913 mAppRestrictView.setVisibility(View.GONE);
914 }
915 }
916
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700917 private void setPolicyWarningBytes(long warningBytes) {
918 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700919 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700920 updatePolicy(false);
921 }
922
923 private void setPolicyLimitBytes(long limitBytes) {
924 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700925 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700926 updatePolicy(false);
927 }
928
Jeff Sharkey28130d92011-09-02 16:10:24 -0700929 /**
930 * Local cache of value, used to work around delay when
931 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
932 */
933 private Boolean mMobileDataEnabled;
934
PauloftheWest50e6eca2014-10-03 11:07:14 -0700935 private boolean isMobileDataEnabled(long subId) {
936 boolean isEnable = false;
Jeff Sharkey28130d92011-09-02 16:10:24 -0700937 if (mMobileDataEnabled != null) {
938 // TODO: deprecate and remove this once enabled flag is on policy
PauloftheWest50e6eca2014-10-03 11:07:14 -0700939 // Multiple Subscriptions, the value need to be reseted
940 isEnable = mMobileDataEnabled.booleanValue();
941 mMobileDataEnabled = null;
Jeff Sharkey28130d92011-09-02 16:10:24 -0700942 } else {
PauloftheWest50e6eca2014-10-03 11:07:14 -0700943 //SUB SELECT
944 isEnable = mTelephonyManager.getDataEnabled() && isMobileDataAvailable(subId);
Jeff Sharkey28130d92011-09-02 16:10:24 -0700945 }
PauloftheWest50e6eca2014-10-03 11:07:14 -0700946 return isEnable;
Jeff Sharkey28130d92011-09-02 16:10:24 -0700947 }
948
949 private void setMobileDataEnabled(boolean enabled) {
950 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700951 mTelephonyManager.setDataEnabled(enabled);
Jeff Sharkey28130d92011-09-02 16:10:24 -0700952 mMobileDataEnabled = enabled;
953 updatePolicy(false);
954 }
955
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700956 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700957 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked()
958 && ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700959 }
960
961 private boolean isBandwidthControlEnabled() {
962 try {
963 return mNetworkService.isBandwidthControlEnabled();
964 } catch (RemoteException e) {
965 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
966 return false;
967 }
968 }
969
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700970 public void setRestrictBackground(boolean restrictBackground) {
971 mPolicyManager.setRestrictBackground(restrictBackground);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700972 updateMenuTitles();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700973 }
974
975 private boolean getAppRestrictBackground() {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700976 final int uid = mCurrentApp.key;
977 final int uidPolicy = mPolicyManager.getUidPolicy(uid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700978 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
979 }
980
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700981 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700982 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700983 final int uid = mCurrentApp.key;
984 mPolicyManager.setUidPolicy(
985 uid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700986 mAppRestrict.setChecked(restrictBackground);
987 }
988
Jeff Sharkey8a503642011-06-10 13:31:21 -0700989 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700990 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
991 * current {@link #mTemplate}.
992 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700993 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700994 boolean dataEnabledVisible = mDataEnabledSupported;
995 boolean disableAtLimitVisible = mDisableAtLimitSupported;
996
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700997 if (isAppDetailMode()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700998 dataEnabledVisible = false;
999 disableAtLimitVisible = false;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001000 }
1001
Jeff Sharkey28130d92011-09-02 16:10:24 -07001002 // TODO: move enabled state directly into policy
PauloftheWest50e6eca2014-10-03 11:07:14 -07001003 if (isMobileTab(mCurrentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001004 mBinding = true;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001005 mDataEnabled.setChecked(isMobileDataEnabled(getSubId(mCurrentTab)));
Jeff Sharkey28130d92011-09-02 16:10:24 -07001006 mBinding = false;
1007 }
1008
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001009 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001010 //SUB SELECT
1011 if (isNetworkPolicyModifiable(policy) && isMobileDataAvailable(getSubId(mCurrentTab))) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001012 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001013 if (!isAppDetailMode()) {
1014 mChart.bindNetworkPolicy(policy);
1015 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001016
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001017 } else {
1018 // controls are disabled; don't bind warning/limit sweeps
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001019 disableAtLimitVisible = false;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001020 mChart.bindNetworkPolicy(null);
1021 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001022
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001023 mDataEnabledView.setVisibility(dataEnabledVisible ? View.VISIBLE : View.GONE);
1024 mDisableAtLimitView.setVisibility(disableAtLimitVisible ? View.VISIBLE : View.GONE);
1025
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001026 if (refreshCycle) {
1027 // generate cycle list based on policy and available history
1028 updateCycleList(policy);
1029 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001030 }
1031
1032 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001033 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
1034 * and available {@link NetworkStatsHistory} data. Always selects the newest
1035 * item, updating the inspection range on {@link #mChart}.
1036 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001037 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001038 // stash away currently selected cycle to try restoring below
1039 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -07001040 mCycleAdapter.clear();
1041
1042 final Context context = mCycleSpinner.getContext();
1043
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001044 long historyStart = Long.MAX_VALUE;
1045 long historyEnd = Long.MIN_VALUE;
1046 if (mChartData != null) {
1047 historyStart = mChartData.network.getStart();
1048 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001049 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001050
Jeff Sharkey461842a2011-09-25 18:22:48 -07001051 final long now = System.currentTimeMillis();
1052 if (historyStart == Long.MAX_VALUE) historyStart = now;
1053 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001054
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001055 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001056 if (policy != null) {
1057 // find the next cycle boundary
1058 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001059
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001060 // walk backwards, generating all valid cycle ranges
1061 while (cycleEnd > historyStart) {
1062 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
1063 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
1064 + historyStart);
1065 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1066 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001067 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001068 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001069
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001070 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001071 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001072 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001073
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001074 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001075 // no policy defined cycles; show entry for each four-week period
1076 long cycleEnd = historyEnd;
1077 while (cycleEnd > historyStart) {
1078 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
1079 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1080 cycleEnd = cycleStart;
1081 }
1082
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001083 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001084 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001085
1086 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001087 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001088 final int position = mCycleAdapter.findNearestPosition(previousItem);
1089 mCycleSpinner.setSelection(position);
1090
1091 // only force-update cycle when changed; skipping preserves any
1092 // user-defined inspection region.
1093 final CycleItem selectedItem = mCycleAdapter.getItem(position);
1094 if (!Objects.equal(selectedItem, previousItem)) {
1095 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1096 } else {
1097 // but still kick off loader for detailed list
1098 updateDetailData();
1099 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001100 } else {
1101 updateDetailData();
1102 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001103 }
1104
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001105 private View.OnClickListener mDataEnabledListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001106 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001107 public void onClick(View v) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001108 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001109
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001110 final boolean dataEnabled = !mDataEnabled.isChecked();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001111 final String currentTab = mCurrentTab;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001112 if (isMobileTab(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001113 if (dataEnabled) {
1114 setMobileDataEnabled(true);
1115 } else {
1116 // disabling data; show confirmation dialog which eventually
1117 // calls setMobileDataEnabled() once user confirms.
1118 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1119 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001120 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001121
Jeff Sharkey28130d92011-09-02 16:10:24 -07001122 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001123 }
1124 };
1125
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001126 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001127 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001128 public void onClick(View v) {
1129 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001130 if (disableAtLimit) {
1131 // enabling limit; show confirmation dialog which eventually
1132 // calls setPolicyLimitBytes() once user confirms.
1133 ConfirmLimitFragment.show(DataUsageSummary.this);
1134 } else {
1135 setPolicyLimitBytes(LIMIT_DISABLED);
1136 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001137 }
1138 };
1139
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001140 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001141 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001142 public void onClick(View v) {
1143 final boolean restrictBackground = !mAppRestrict.isChecked();
1144
1145 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001146 // enabling restriction; show confirmation dialog which
1147 // eventually calls setRestrictBackground() once user
1148 // confirms.
1149 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001150 } else {
1151 setAppRestrictBackground(false);
1152 }
1153 }
1154 };
1155
Jeff Sharkey8a503642011-06-10 13:31:21 -07001156 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001157 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001158 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001159 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001160 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001161
1162 // TODO: sigh, remove this hack once we understand 6450986
1163 if (mUidDetailProvider == null || app == null) return;
1164
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001165 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001166 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001167 }
1168 };
1169
1170 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001171 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001172 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1173 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1174 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001175 // show cycle editor; will eventually call setPolicyCycleDay()
1176 // when user finishes editing.
1177 CycleEditorFragment.show(DataUsageSummary.this);
1178
1179 // reset spinner to something other than "change cycle..."
1180 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001181
1182 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001183 if (LOGD) {
1184 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1185 + cycle.end + "]");
1186 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001187
1188 // update chart to show selected cycle, and update detail data
1189 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001190 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001191
1192 updateDetailData();
1193 }
1194 }
1195
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001196 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001197 public void onNothingSelected(AdapterView<?> parent) {
1198 // ignored
1199 }
1200 };
1201
1202 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001203 * Update details based on {@link #mChart} inspection range depending on
1204 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1205 * of applications data usage, and when {@link #isAppDetailMode()} update
1206 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001207 */
1208 private void updateDetailData() {
1209 if (LOGD) Log.d(TAG, "updateDetailData()");
1210
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001211 final long start = mChart.getInspectStart();
1212 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001213 final long now = System.currentTimeMillis();
1214
1215 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001216
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001217 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001218 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001219 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001220 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001221 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001222 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001223 final long foregroundBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001224 final long totalBytes = defaultBytes + foregroundBytes;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001225
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001226 if (mAppTotal != null) {
1227 mAppTotal.setText(Formatter.formatFileSize(context, totalBytes));
1228 }
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001229 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1230 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1231
1232 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001233 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001234
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001235 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001236
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001237 mCycleSummary.setVisibility(View.GONE);
1238
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001239 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001240 if (mChartData != null) {
1241 entry = mChartData.network.getValues(start, end, now, null);
1242 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001243
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001244 mCycleSummary.setVisibility(View.VISIBLE);
1245
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001246 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001247 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001248 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001249 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001250
1251 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1252 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001253 mCycleSummary.setText(totalPhrase);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001254
PauloftheWest50e6eca2014-10-03 11:07:14 -07001255 if (isMobileTab(mCurrentTab) || TAB_3G.equals(mCurrentTab)
László Dávid0b8bf4e2012-10-24 23:31:47 +02001256 || TAB_4G.equals(mCurrentTab)) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001257 if (isAppDetailMode()) {
1258 mDisclaimer.setVisibility(View.GONE);
1259 } else {
1260 mDisclaimer.setVisibility(View.VISIBLE);
1261 }
Jeff Sharkeye557c332012-04-13 16:04:07 -07001262 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001263 mDisclaimer.setVisibility(View.GONE);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001264 }
1265
Jeff Sharkey92811822012-05-04 11:47:29 -07001266 // initial layout is finished above, ensure we have transitions
1267 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001268 }
1269
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001270 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1271 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001272 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001273 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001274 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001275 }
1276
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001277 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001278 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1279 mChartData = data;
1280 mChart.bindNetworkStats(mChartData.network);
1281 mChart.bindDetailNetworkStats(mChartData.detail);
1282
1283 // calcuate policy cycles based on available data
1284 updatePolicy(true);
1285 updateAppDetail();
1286
1287 // force scroll to top of body when showing detail
1288 if (mChartData.detail != null) {
1289 mListView.smoothScrollToPosition(0);
1290 }
1291 }
1292
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001293 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001294 public void onLoaderReset(Loader<ChartData> loader) {
1295 mChartData = null;
1296 mChart.bindNetworkStats(null);
1297 mChart.bindDetailNetworkStats(null);
1298 }
1299 };
1300
1301 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001302 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001303 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001304 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001305 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001306 }
1307
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001308 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001309 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001310 final int[] restrictedUids = mPolicyManager.getUidsWithPolicy(
Jeff Sharkeye557c332012-04-13 16:04:07 -07001311 POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001312 mAdapter.bindStats(data, restrictedUids);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001313 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001314 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001315
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001316 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001317 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001318 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001319 updateEmptyVisible();
1320 }
1321
1322 private void updateEmptyVisible() {
1323 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1324 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001325 mStupidPadding.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001326 }
1327 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001328
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001329 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001330 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001331 final Context context = getActivity();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001332 if (hasReadyMobileRadio(context)) {
1333 final TelephonyManager tele = TelephonyManager.from(context);
1334 return mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001335 } else {
1336 return false;
1337 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001338 }
1339
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001340 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001341 private void setMobilePolicySplit(boolean split) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001342 final Context context = getActivity();
1343 if (hasReadyMobileRadio(context)) {
1344 final TelephonyManager tele = TelephonyManager.from(context);
1345 mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split);
1346 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001347 }
1348
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001349 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001350 final TelephonyManager tele = TelephonyManager.from(context);
1351 final String actualSubscriberId = tele.getSubscriberId();
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -08001352 return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001353 }
1354
PauloftheWest50e6eca2014-10-03 11:07:14 -07001355 private static String getActiveSubscriberId(Context context, long subId) {
1356 final TelephonyManager tele = TelephonyManager.from(context);
1357 return tele.getSubscriberId(subId);
1358 }
1359
Jeff Sharkey8a503642011-06-10 13:31:21 -07001360 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001361 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001362 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001363 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001364 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001365
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001366 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001367 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001368 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001369 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001370
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001371 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001372 public void requestWarningEdit() {
1373 WarningEditorFragment.show(DataUsageSummary.this);
1374 }
1375
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001376 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001377 public void requestLimitEdit() {
1378 LimitEditorFragment.show(DataUsageSummary.this);
1379 }
1380 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001381
1382 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001383 * List item that reflects a specific data usage cycle.
1384 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001385 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001386 public CharSequence label;
1387 public long start;
1388 public long end;
1389
Jeff Sharkey8a503642011-06-10 13:31:21 -07001390 CycleItem(CharSequence label) {
1391 this.label = label;
1392 }
1393
1394 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001395 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001396 this.start = start;
1397 this.end = end;
1398 }
1399
Jeff Sharkey8a503642011-06-10 13:31:21 -07001400 @Override
1401 public String toString() {
1402 return label.toString();
1403 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001404
1405 @Override
1406 public boolean equals(Object o) {
1407 if (o instanceof CycleItem) {
1408 final CycleItem another = (CycleItem) o;
1409 return start == another.start && end == another.end;
1410 }
1411 return false;
1412 }
1413
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001414 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001415 public int compareTo(CycleItem another) {
1416 return Long.compare(start, another.start);
1417 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001418 }
1419
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001420 private static final StringBuilder sBuilder = new StringBuilder(50);
1421 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1422 sBuilder, Locale.getDefault());
1423
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001424 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001425 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001426
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001427 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001428 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001429 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1430 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001431 }
1432 }
1433
Jeff Sharkey8a503642011-06-10 13:31:21 -07001434 /**
1435 * Special-case data usage cycle that triggers dialog to change
1436 * {@link NetworkPolicy#cycleDay}.
1437 */
1438 public static class CycleChangeItem extends CycleItem {
1439 public CycleChangeItem(Context context) {
1440 super(context.getString(R.string.data_usage_change_cycle));
1441 }
1442 }
1443
1444 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001445 private boolean mChangePossible = false;
1446 private boolean mChangeVisible = false;
1447
1448 private final CycleChangeItem mChangeItem;
1449
Jeff Sharkey8a503642011-06-10 13:31:21 -07001450 public CycleAdapter(Context context) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001451 super(context, R.layout.data_usage_cycle_item);
1452 setDropDownViewResource(R.layout.data_usage_cycle_item_dropdown);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001453 mChangeItem = new CycleChangeItem(context);
1454 }
1455
1456 public void setChangePossible(boolean possible) {
1457 mChangePossible = possible;
1458 updateChange();
1459 }
1460
1461 public void setChangeVisible(boolean visible) {
1462 mChangeVisible = visible;
1463 updateChange();
1464 }
1465
1466 private void updateChange() {
1467 remove(mChangeItem);
1468 if (mChangePossible && mChangeVisible) {
1469 add(mChangeItem);
1470 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001471 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001472
1473 /**
1474 * Find position of {@link CycleItem} in this adapter which is nearest
1475 * the given {@link CycleItem}.
1476 */
1477 public int findNearestPosition(CycleItem target) {
1478 if (target != null) {
1479 final int count = getCount();
1480 for (int i = count - 1; i >= 0; i--) {
1481 final CycleItem item = getItem(i);
1482 if (item instanceof CycleChangeItem) {
1483 continue;
1484 } else if (item.compareTo(target) >= 0) {
1485 return i;
1486 }
1487 }
1488 }
1489 return 0;
1490 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001491 }
1492
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001493 public static class AppItem implements Comparable<AppItem>, Parcelable {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001494 public static final int CATEGORY_USER = 0;
1495 public static final int CATEGORY_APP_TITLE = 1;
1496 public static final int CATEGORY_APP = 2;
1497
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001498 public final int key;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001499 public boolean restricted;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001500 public int category;
1501
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001502 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001503 public long total;
1504
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001505 public AppItem() {
1506 this.key = 0;
1507 }
1508
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001509 public AppItem(int key) {
1510 this.key = key;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001511 }
1512
1513 public AppItem(Parcel parcel) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001514 key = parcel.readInt();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001515 uids = parcel.readSparseBooleanArray();
1516 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001517 }
1518
1519 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001520 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001521 }
1522
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001523 @Override
1524 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001525 dest.writeInt(key);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001526 dest.writeSparseBooleanArray(uids);
1527 dest.writeLong(total);
1528 }
1529
1530 @Override
1531 public int describeContents() {
1532 return 0;
1533 }
1534
1535 @Override
1536 public int compareTo(AppItem another) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001537 int comparison = Integer.compare(category, another.category);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001538 if (comparison == 0) {
1539 comparison = Long.compare(another.total, total);
1540 }
1541 return comparison;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001542 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001543
1544 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1545 @Override
1546 public AppItem createFromParcel(Parcel in) {
1547 return new AppItem(in);
1548 }
1549
1550 @Override
1551 public AppItem[] newArray(int size) {
1552 return new AppItem[size];
1553 }
1554 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001555 }
1556
Jeff Sharkey8a503642011-06-10 13:31:21 -07001557 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001558 * Adapter of applications, sorted by total usage descending.
1559 */
1560 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001561 private final UidDetailProvider mProvider;
1562 private final int mInsetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001563 private final UserManager mUm;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001564
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001565 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001566 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001567
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001568 public DataUsageAdapter(final UserManager userManager, UidDetailProvider provider, int insetSide) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001569 mProvider = checkNotNull(provider);
1570 mInsetSide = insetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001571 mUm = userManager;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001572 }
1573
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001574 /**
1575 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1576 */
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001577 public void bindStats(NetworkStats stats, int[] restrictedUids) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001578 mItems.clear();
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001579 mLargest = 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001580
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001581 final int currentUserId = ActivityManager.getCurrentUser();
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001582 final List<UserHandle> profiles = mUm.getUserProfiles();
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001583 final SparseArray<AppItem> knownItems = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001584
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001585 NetworkStats.Entry entry = null;
1586 final int size = stats != null ? stats.size() : 0;
1587 for (int i = 0; i < size; i++) {
1588 entry = stats.getValues(i, entry);
1589
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001590 // Decide how to collapse items together
1591 final int uid = entry.uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001592
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001593 final int collapseKey;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001594 final int category;
1595 final int userId = UserHandle.getUserId(uid);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001596 if (UserHandle.isApp(uid)) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001597 if (profiles.contains(new UserHandle(userId))) {
1598 if (userId != currentUserId) {
1599 // Add to a managed user item.
1600 final int managedKey = UidDetailProvider.buildKeyForUser(userId);
1601 accumulate(managedKey, knownItems, entry,
1602 AppItem.CATEGORY_USER);
1603 }
1604 // Add to app item.
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001605 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001606 category = AppItem.CATEGORY_APP;
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001607 } else {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001608 // Add to other user item.
1609 collapseKey = UidDetailProvider.buildKeyForUser(userId);
1610 category = AppItem.CATEGORY_USER;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001611 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001612 } else if (uid == UID_REMOVED || uid == UID_TETHERING) {
1613 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001614 category = AppItem.CATEGORY_APP;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001615 } else {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001616 collapseKey = android.os.Process.SYSTEM_UID;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001617 category = AppItem.CATEGORY_APP;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001618 }
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001619 accumulate(collapseKey, knownItems, entry, category);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001620 }
1621
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001622 final int restrictedUidsMax = restrictedUids.length;
1623 for (int i = 0; i < restrictedUidsMax; ++i) {
1624 final int uid = restrictedUids[i];
1625 // Only splice in restricted state for current user or managed users
1626 if (!profiles.contains(new UserHandle(UserHandle.getUserId(uid)))) {
1627 continue;
1628 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001629
1630 AppItem item = knownItems.get(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001631 if (item == null) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001632 item = new AppItem(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001633 item.total = -1;
1634 mItems.add(item);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001635 knownItems.put(item.key, item);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001636 }
1637 item.restricted = true;
1638 }
1639
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001640 if (!mItems.isEmpty()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001641 final AppItem title = new AppItem();
1642 title.category = AppItem.CATEGORY_APP_TITLE;
1643 mItems.add(title);
1644 }
1645
Jeff Sharkey8a503642011-06-10 13:31:21 -07001646 Collections.sort(mItems);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001647 notifyDataSetChanged();
1648 }
1649
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001650 /**
1651 * Accumulate data usage of a network stats entry for the item mapped by the collapse key.
1652 * Creates the item if needed.
1653 *
1654 * @param collapseKey the collapse key used to map the item.
1655 * @param knownItems collection of known (already existing) items.
1656 * @param entry the network stats entry to extract data usage from.
1657 * @param itemCategory the item is categorized on the list view by this category. Must be
1658 * either AppItem.APP_ITEM_CATEGORY or AppItem.MANAGED_USER_ITEM_CATEGORY
1659 */
1660 private void accumulate(int collapseKey, final SparseArray<AppItem> knownItems,
1661 NetworkStats.Entry entry, int itemCategory) {
1662 final int uid = entry.uid;
1663 AppItem item = knownItems.get(collapseKey);
1664 if (item == null) {
1665 item = new AppItem(collapseKey);
1666 item.category = itemCategory;
1667 mItems.add(item);
1668 knownItems.put(item.key, item);
1669 }
1670 item.addUid(uid);
1671 item.total += entry.rxBytes + entry.txBytes;
1672 if (mLargest < item.total) {
1673 mLargest = item.total;
1674 }
1675 }
1676
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001677 @Override
1678 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001679 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001680 }
1681
1682 @Override
1683 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001684 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001685 }
1686
1687 @Override
1688 public long getItemId(int position) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001689 return mItems.get(position).key;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001690 }
1691
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001692 /**
1693 * See {@link #getItemViewType} for the view types.
1694 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001695 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001696 public int getViewTypeCount() {
1697 return 2;
1698 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001699
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001700 /**
1701 * Returns 1 for separator items and 0 for anything else.
1702 */
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001703 @Override
1704 public int getItemViewType(int position) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001705 final AppItem item = mItems.get(position);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001706 if (item.category == AppItem.CATEGORY_APP_TITLE) {
1707 return 1;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001708 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001709 return 0;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001710 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001711 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001712
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001713 @Override
1714 public boolean areAllItemsEnabled() {
1715 return false;
1716 }
1717
1718 @Override
1719 public boolean isEnabled(int position) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001720 if (position > mItems.size()) {
1721 throw new ArrayIndexOutOfBoundsException();
1722 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001723 return getItemViewType(position) == 0;
1724 }
1725
1726 @Override
1727 public View getView(int position, View convertView, ViewGroup parent) {
1728 final AppItem item = mItems.get(position);
1729 if (getItemViewType(position) == 1) {
1730 if (convertView == null) {
1731 convertView = inflateCategoryHeader(LayoutInflater.from(parent.getContext()),
1732 parent);
1733 }
1734
1735 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
1736 title.setText(R.string.data_usage_app);
1737
1738 } else {
1739 if (convertView == null) {
1740 convertView = LayoutInflater.from(parent.getContext()).inflate(
1741 R.layout.data_usage_item, parent, false);
1742
1743 if (mInsetSide > 0) {
1744 convertView.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
1745 }
1746 }
1747
1748 final Context context = parent.getContext();
1749
1750 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
1751 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1752 android.R.id.progress);
1753
1754 // kick off async load of app details
1755 UidDetailTask.bindView(mProvider, item, convertView);
1756
1757 if (item.restricted && item.total <= 0) {
1758 text1.setText(R.string.data_usage_app_restricted);
1759 progress.setVisibility(View.GONE);
1760 } else {
1761 text1.setText(Formatter.formatFileSize(context, item.total));
1762 progress.setVisibility(View.VISIBLE);
1763 }
1764
1765 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1766 progress.setProgress(percentTotal);
1767 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001768
1769 return convertView;
1770 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001771 }
1772
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001773 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001774 * Empty {@link Fragment} that controls display of UID details in
1775 * {@link DataUsageSummary}.
1776 */
1777 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001778 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001779
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001780 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001781 if (!parent.isAdded()) return;
1782
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001783 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001784 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001785
1786 final AppDetailsFragment fragment = new AppDetailsFragment();
1787 fragment.setArguments(args);
1788 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001789 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1790 ft.add(fragment, TAG_APP_DETAILS);
1791 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001792 ft.setBreadCrumbTitle(
1793 parent.getResources().getString(R.string.data_usage_app_summary_title));
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001794 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001795 }
1796
1797 @Override
1798 public void onStart() {
1799 super.onStart();
1800 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001801 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001802 target.updateBody();
1803 }
1804
1805 @Override
1806 public void onStop() {
1807 super.onStop();
1808 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001809 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001810 target.updateBody();
1811 }
1812 }
1813
1814 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001815 * Dialog to request user confirmation before setting
1816 * {@link NetworkPolicy#limitBytes}.
1817 */
1818 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001819 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001820 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001821
1822 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001823 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001824
Jeff Sharkeyd0a55312014-08-08 10:25:24 -07001825 final NetworkPolicy policy = parent.mPolicyEditor.getPolicy(parent.mTemplate);
1826 if (policy == null) return;
1827
Jeff Sharkey461842a2011-09-25 18:22:48 -07001828 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001829 final CharSequence message;
Jeff Sharkeyd0a55312014-08-08 10:25:24 -07001830 final long minLimitBytes = (long) (policy.warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001831 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001832
1833 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001834 final String currentTab = parent.mCurrentTab;
1835 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001836 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001837 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001838 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001839 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001840 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001841 } else if (isMobileTab(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001842 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001843 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001844 } else {
1845 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001846 }
1847
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001848 final Bundle args = new Bundle();
1849 args.putCharSequence(EXTRA_MESSAGE, message);
1850 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1851
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001852 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1853 dialog.setArguments(args);
1854 dialog.setTargetFragment(parent, 0);
1855 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1856 }
1857
1858 @Override
1859 public Dialog onCreateDialog(Bundle savedInstanceState) {
1860 final Context context = getActivity();
1861
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001862 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001863 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1864
1865 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1866 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001867 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001868
1869 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001870 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001871 public void onClick(DialogInterface dialog, int which) {
1872 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1873 if (target != null) {
1874 target.setPolicyLimitBytes(limitBytes);
1875 }
1876 }
1877 });
1878
1879 return builder.create();
1880 }
1881 }
1882
1883 /**
1884 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1885 */
1886 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001887 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001888
1889 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001890 if (!parent.isAdded()) return;
1891
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001892 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001893 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001894
1895 final CycleEditorFragment dialog = new CycleEditorFragment();
1896 dialog.setArguments(args);
1897 dialog.setTargetFragment(parent, 0);
1898 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1899 }
1900
1901 @Override
1902 public Dialog onCreateDialog(Bundle savedInstanceState) {
1903 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001904 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1905 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001906
1907 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1908 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1909
1910 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1911 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1912
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001913 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1914 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001915
1916 cycleDayPicker.setMinValue(1);
1917 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001918 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001919 cycleDayPicker.setWrapSelectorWheel(true);
1920
1921 builder.setTitle(R.string.data_usage_cycle_editor_title);
1922 builder.setView(view);
1923
1924 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1925 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001926 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001927 public void onClick(DialogInterface dialog, int which) {
Jeff Sharkeyd277de92013-03-25 15:11:25 -07001928 // clear focus to finish pending text edits
1929 cycleDayPicker.clearFocus();
1930
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001931 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001932 final String cycleTimezone = new Time().timezone;
1933 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001934 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001935 }
1936 });
1937
1938 return builder.create();
1939 }
1940 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001941
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001942 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001943 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1944 */
1945 public static class WarningEditorFragment extends DialogFragment {
1946 private static final String EXTRA_TEMPLATE = "template";
1947
1948 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001949 if (!parent.isAdded()) return;
1950
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001951 final Bundle args = new Bundle();
1952 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1953
1954 final WarningEditorFragment dialog = new WarningEditorFragment();
1955 dialog.setArguments(args);
1956 dialog.setTargetFragment(parent, 0);
1957 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1958 }
1959
1960 @Override
1961 public Dialog onCreateDialog(Bundle savedInstanceState) {
1962 final Context context = getActivity();
1963 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1964 final NetworkPolicyEditor editor = target.mPolicyEditor;
1965
1966 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1967 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1968
1969 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1970 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1971
1972 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1973 final long warningBytes = editor.getPolicyWarningBytes(template);
1974 final long limitBytes = editor.getPolicyLimitBytes(template);
1975
1976 bytesPicker.setMinValue(0);
1977 if (limitBytes != LIMIT_DISABLED) {
1978 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1979 } else {
1980 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1981 }
1982 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1983 bytesPicker.setWrapSelectorWheel(false);
1984
1985 builder.setTitle(R.string.data_usage_warning_editor_title);
1986 builder.setView(view);
1987
1988 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1989 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001990 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001991 public void onClick(DialogInterface dialog, int which) {
1992 // clear focus to finish pending text edits
1993 bytesPicker.clearFocus();
1994
1995 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1996 editor.setPolicyWarningBytes(template, bytes);
1997 target.updatePolicy(false);
1998 }
1999 });
2000
2001 return builder.create();
2002 }
2003 }
2004
2005 /**
2006 * Dialog to edit {@link NetworkPolicy#limitBytes}.
2007 */
2008 public static class LimitEditorFragment extends DialogFragment {
2009 private static final String EXTRA_TEMPLATE = "template";
2010
2011 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002012 if (!parent.isAdded()) return;
2013
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002014 final Bundle args = new Bundle();
2015 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
2016
2017 final LimitEditorFragment dialog = new LimitEditorFragment();
2018 dialog.setArguments(args);
2019 dialog.setTargetFragment(parent, 0);
2020 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
2021 }
2022
2023 @Override
2024 public Dialog onCreateDialog(Bundle savedInstanceState) {
2025 final Context context = getActivity();
2026 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2027 final NetworkPolicyEditor editor = target.mPolicyEditor;
2028
2029 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2030 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2031
2032 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
2033 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
2034
2035 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2036 final long warningBytes = editor.getPolicyWarningBytes(template);
2037 final long limitBytes = editor.getPolicyLimitBytes(template);
2038
2039 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09002040 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002041 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
2042 } else {
2043 bytesPicker.setMinValue(0);
2044 }
2045 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
2046 bytesPicker.setWrapSelectorWheel(false);
2047
2048 builder.setTitle(R.string.data_usage_limit_editor_title);
2049 builder.setView(view);
2050
2051 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2052 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002053 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002054 public void onClick(DialogInterface dialog, int which) {
2055 // clear focus to finish pending text edits
2056 bytesPicker.clearFocus();
2057
2058 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
2059 editor.setPolicyLimitBytes(template, bytes);
2060 target.updatePolicy(false);
2061 }
2062 });
2063
2064 return builder.create();
2065 }
2066 }
2067 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07002068 * Dialog to request user confirmation before disabling data.
2069 */
2070 public static class ConfirmDataDisableFragment extends DialogFragment {
2071 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002072 if (!parent.isAdded()) return;
2073
Jeff Sharkey28130d92011-09-02 16:10:24 -07002074 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
2075 dialog.setTargetFragment(parent, 0);
2076 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
2077 }
2078
2079 @Override
2080 public Dialog onCreateDialog(Bundle savedInstanceState) {
2081 final Context context = getActivity();
2082
2083 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2084 builder.setMessage(R.string.data_usage_disable_mobile);
2085
2086 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002087 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07002088 public void onClick(DialogInterface dialog, int which) {
2089 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2090 if (target != null) {
2091 // TODO: extend to modify policy enabled flag.
2092 target.setMobileDataEnabled(false);
2093 }
2094 }
2095 });
2096 builder.setNegativeButton(android.R.string.cancel, null);
2097
2098 return builder.create();
2099 }
2100 }
2101
2102 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002103 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07002104 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002105 */
2106 public static class ConfirmRestrictFragment extends DialogFragment {
2107 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002108 if (!parent.isAdded()) return;
2109
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002110 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
2111 dialog.setTargetFragment(parent, 0);
2112 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
2113 }
2114
2115 @Override
2116 public Dialog onCreateDialog(Bundle savedInstanceState) {
2117 final Context context = getActivity();
2118
2119 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002120 builder.setTitle(R.string.data_usage_restrict_background_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07002121 if (Utils.hasMultipleUsers(context)) {
2122 builder.setMessage(R.string.data_usage_restrict_background_multiuser);
2123 } else {
2124 builder.setMessage(R.string.data_usage_restrict_background);
2125 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002126
2127 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002128 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002129 public void onClick(DialogInterface dialog, int which) {
2130 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2131 if (target != null) {
2132 target.setRestrictBackground(true);
2133 }
2134 }
2135 });
2136 builder.setNegativeButton(android.R.string.cancel, null);
2137
2138 return builder.create();
2139 }
2140 }
2141
2142 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002143 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
2144 * change has been denied, usually based on
2145 * {@link DataUsageSummary#hasLimitedNetworks()}.
2146 */
2147 public static class DeniedRestrictFragment extends DialogFragment {
2148 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002149 if (!parent.isAdded()) return;
2150
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002151 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
2152 dialog.setTargetFragment(parent, 0);
2153 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
2154 }
2155
2156 @Override
2157 public Dialog onCreateDialog(Bundle savedInstanceState) {
2158 final Context context = getActivity();
2159
2160 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2161 builder.setTitle(R.string.data_usage_app_restrict_background);
2162 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
2163 builder.setPositiveButton(android.R.string.ok, null);
2164
2165 return builder.create();
2166 }
2167 }
2168
2169 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002170 * Dialog to request user confirmation before setting
2171 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
2172 */
2173 public static class ConfirmAppRestrictFragment extends DialogFragment {
2174 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002175 if (!parent.isAdded()) return;
2176
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002177 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2178 dialog.setTargetFragment(parent, 0);
2179 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2180 }
2181
2182 @Override
2183 public Dialog onCreateDialog(Bundle savedInstanceState) {
2184 final Context context = getActivity();
2185
2186 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002187 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2188 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2189
2190 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002191 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002192 public void onClick(DialogInterface dialog, int which) {
2193 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2194 if (target != null) {
2195 target.setAppRestrictBackground(true);
2196 }
2197 }
2198 });
2199 builder.setNegativeButton(android.R.string.cancel, null);
2200
2201 return builder.create();
2202 }
2203 }
2204
2205 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002206 * Compute default tab that should be selected, based on
2207 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2208 */
2209 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002210 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002211 if (template == null) {
2212 final long subId = intent.getLongExtra(PhoneConstants.SUBSCRIPTION_KEY,
2213 SubscriptionManager.INVALID_SUB_ID);
2214 if (SubscriptionManager.isValidSubId(subId)) {
2215 return TAB_MOBILE + String.valueOf(subId);
2216 }
2217 return null;
2218 }
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002219
2220 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002221 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002222 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002223 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002224 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002225 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002226 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002227 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002228 return TAB_WIFI;
2229 default:
2230 return null;
2231 }
2232 }
2233
2234 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002235 * Background task that loads {@link UidDetail}, binding to
2236 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002237 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002238 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2239 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002240 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002241 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002242
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002243 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002244 mProvider = checkNotNull(provider);
2245 mItem = checkNotNull(item);
2246 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002247 }
2248
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002249 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002250 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002251 final UidDetailTask existing = (UidDetailTask) target.getTag();
2252 if (existing != null) {
2253 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002254 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002255
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002256 final UidDetail cachedDetail = provider.getUidDetail(item.key, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002257 if (cachedDetail != null) {
2258 bindView(cachedDetail, target);
2259 } else {
2260 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2261 AsyncTask.THREAD_POOL_EXECUTOR));
2262 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002263 }
2264
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002265 private static void bindView(UidDetail detail, View target) {
2266 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2267 final TextView title = (TextView) target.findViewById(android.R.id.title);
2268
2269 if (detail != null) {
2270 icon.setImageDrawable(detail.icon);
2271 title.setText(detail.label);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +01002272 title.setContentDescription(detail.contentDescription);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002273 } else {
2274 icon.setImageDrawable(null);
2275 title.setText(null);
2276 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002277 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002278
2279 @Override
2280 protected void onPreExecute() {
2281 bindView(null, mTarget);
2282 }
2283
2284 @Override
2285 protected UidDetail doInBackground(Void... params) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002286 return mProvider.getUidDetail(mItem.key, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002287 }
2288
2289 @Override
2290 protected void onPostExecute(UidDetail result) {
2291 bindView(result, mTarget);
2292 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002293 }
2294
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002295 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002296 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002297 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002298 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002299 if (TEST_RADIOS) {
2300 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2301 }
2302
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002303 final ConnectivityManager conn = ConnectivityManager.from(context);
2304 final TelephonyManager tele = TelephonyManager.from(context);
2305
PauloftheWest50e6eca2014-10-03 11:07:14 -07002306 final List<SubInfoRecord> subInfoList = SubscriptionManager.getActiveSubInfoList();
2307 // No activated Subscription
2308 if (subInfoList == null) {
2309 return false;
2310 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002311 // require both supported network and ready SIM
PauloftheWest50e6eca2014-10-03 11:07:14 -07002312 boolean isReady = true;
2313 for (SubInfoRecord subInfo : subInfoList) {
2314 isReady = isReady & tele.getSimState(subInfo.slotId) == SIM_STATE_READY;
2315 }
2316 return conn.isNetworkSupported(TYPE_MOBILE) && isReady;
2317 }
2318
2319 /*
2320 * TODO: consider adding to TelephonyManager or SubscritpionManager.
2321 */
2322 public static boolean hasReadyMobileRadio(Context context, long subId) {
2323 if (TEST_RADIOS) {
2324 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2325 }
2326
2327 final ConnectivityManager conn = ConnectivityManager.from(context);
2328 final TelephonyManager tele = TelephonyManager.from(context);
2329 final int slotId = SubscriptionManager.getSlotId(subId);
2330 final boolean isReady = tele.getSimState(slotId) == SIM_STATE_READY;
2331
2332 return conn.isNetworkSupported(TYPE_MOBILE) && isReady;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002333 }
2334
2335 /**
2336 * Test if device has a mobile 4G data radio.
2337 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002338 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002339 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2340 return false;
2341 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002342 if (TEST_RADIOS) {
2343 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2344 }
2345
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002346 final ConnectivityManager conn = ConnectivityManager.from(context);
2347 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002348
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002349 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Wink Saville55434042012-06-14 12:33:43 -07002350 final boolean hasLte = (tele.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE)
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002351 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002352 return hasWimax || hasLte;
2353 }
2354
2355 /**
2356 * Test if device has a Wi-Fi data radio.
2357 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002358 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002359 if (TEST_RADIOS) {
2360 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2361 }
2362
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002363 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002364 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002365 }
2366
2367 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002368 * Test if device has an ethernet network connection.
2369 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002370 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002371 if (TEST_RADIOS) {
2372 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2373 }
2374
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002375 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002376 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2377
2378 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002379 if (mStatsSession != null) {
2380 try {
2381 ethernetBytes = mStatsSession.getSummaryForNetwork(
2382 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2383 .getTotalBytes();
2384 } catch (RemoteException e) {
2385 throw new RuntimeException(e);
2386 }
2387 } else {
2388 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002389 }
2390
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002391 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002392 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002393 }
2394
2395 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002396 * Inflate a {@link Preference} style layout, adding the given {@link View}
2397 * widget into {@link android.R.id#widget_frame}.
2398 */
2399 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2400 final View view = inflater.inflate(R.layout.preference, root, false);
2401 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2402 android.R.id.widget_frame);
2403 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2404 return view;
2405 }
2406
Jeff Sharkeyb6548462014-07-21 15:38:06 -07002407 private static View inflateCategoryHeader(LayoutInflater inflater, ViewGroup root) {
2408 final TypedArray a = inflater.getContext().obtainStyledAttributes(null,
2409 com.android.internal.R.styleable.Preference,
2410 com.android.internal.R.attr.preferenceCategoryStyle, 0);
2411 final int resId = a.getResourceId(com.android.internal.R.styleable.Preference_layout, 0);
2412 return inflater.inflate(resId, root, false);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002413 }
2414
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002415 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002416 * Test if any networks are currently limited.
2417 */
2418 private boolean hasLimitedNetworks() {
2419 return !buildLimitedNetworksList().isEmpty();
2420 }
2421
2422 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002423 * Build string describing currently limited networks, which defines when
2424 * background data is restricted.
2425 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002426 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002427 private CharSequence buildLimitedNetworksString() {
2428 final List<CharSequence> limited = buildLimitedNetworksList();
2429
2430 // handle case where no networks limited
2431 if (limited.isEmpty()) {
2432 limited.add(getText(R.string.data_usage_list_none));
2433 }
2434
2435 return TextUtils.join(limited);
2436 }
2437
2438 /**
2439 * Build list of currently limited networks, which defines when background
2440 * data is restricted.
2441 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002442 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002443 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002444 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002445
2446 // build combined list of all limited networks
2447 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002448
2449 final TelephonyManager tele = TelephonyManager.from(context);
2450 if (tele.getSimState() == SIM_STATE_READY) {
2451 final String subscriberId = getActiveSubscriberId(context);
2452 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2453 limited.add(getText(R.string.data_usage_list_mobile));
2454 }
2455 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2456 limited.add(getText(R.string.data_usage_tab_3g));
2457 }
2458 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2459 limited.add(getText(R.string.data_usage_tab_4g));
2460 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002461 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002462
2463 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002464 limited.add(getText(R.string.data_usage_tab_wifi));
2465 }
2466 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2467 limited.add(getText(R.string.data_usage_tab_ethernet));
2468 }
2469
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002470 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002471 }
2472
2473 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002474 * Inset both selector and divider {@link Drawable} on the given
2475 * {@link ListView} by the requested dimensions.
2476 */
2477 private static void insetListViewDrawables(ListView view, int insetSide) {
2478 final Drawable selector = view.getSelector();
2479 final Drawable divider = view.getDivider();
2480
2481 // fully unregister these drawables so callbacks can be maintained after
2482 // wrapping below.
2483 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2484 view.setSelector(stub);
2485 view.setDivider(stub);
2486
2487 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2488 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2489 }
2490
2491 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002492 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002493 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002494 */
2495 private static void setPreferenceTitle(View parent, int resId) {
2496 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2497 title.setText(resId);
2498 }
2499
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002500 /**
2501 * Set {@link android.R.id#summary} for a preference view inflated with
2502 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2503 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002504 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002505 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2506 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002507 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002508 }
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002509
2510 /**
2511 * For search
2512 */
2513 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -07002514 new BaseSearchIndexProvider() {
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002515 @Override
2516 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
2517 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
2518
2519 final Resources res = context.getResources();
2520
2521 // Add fragment title
2522 SearchIndexableRaw data = new SearchIndexableRaw(context);
2523 data.title = res.getString(R.string.data_usage_summary_title);
2524 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2525 result.add(data);
2526
2527 // Mobile data
2528 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002529 data.key = DATA_USAGE_ENABLE_MOBILE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002530 data.title = res.getString(R.string.data_usage_enable_mobile);
2531 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2532 result.add(data);
2533
2534 // Set mobile data limit
2535 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002536 data.key = DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002537 data.title = res.getString(R.string.data_usage_disable_mobile_limit);
2538 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2539 result.add(data);
2540
Fabrice Di Megliof2a52262014-04-17 17:20:27 -07002541 // Data usage cycle
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002542 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002543 data.key = DATA_USAGE_CYCLE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002544 data.title = res.getString(R.string.data_usage_cycle);
2545 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2546 result.add(data);
2547
2548 return result;
2549 }
2550 };
2551
PauloftheWest50e6eca2014-10-03 11:07:14 -07002552 private void addMobileTab(Context context, List<SubInfoRecord> subInfoList) {
2553 if (subInfoList != null) {
2554 for (SubInfoRecord subInfo : mSubInfoList) {
2555 if (hasReadyMobileRadio(context,subInfo.subId)) {
2556 mTabHost.addTab(buildTabSpec(mMobileTagMap.get(subInfo.subId),
2557 subInfo.displayName));
2558 }
2559 }
2560 }
2561 }
2562
2563 /**
2564 * Init a map with subId key and mobile tag name
2565 * @param subInfoList The subscription Info List
2566 * @return The map or null if no activated subscription
2567 */
2568 private Map<Long, String> initMobileTabTag(List<SubInfoRecord> subInfoList) {
2569 Map<Long,String> map = null;
2570 if (subInfoList != null) {
2571 String mobileTag;
2572 map = new HashMap<Long, String>();
2573 for (SubInfoRecord subInfo : subInfoList) {
2574 mobileTag = TAB_MOBILE + String.valueOf(subInfo.subId);
2575 map.put(subInfo.subId,mobileTag);
2576 }
2577 }
2578 return map;
2579 }
2580
2581 private static boolean isMobileTab(String currentTab) {
2582 return currentTab != null ? currentTab.contains(TAB_MOBILE) : false;
2583 }
2584
2585 private long getSubId(String currentTab) {
2586 Set<Long> set = mMobileTagMap.keySet();
2587 for (Long subId : set) {
2588 if (mMobileTagMap.get(subId).equals(currentTab)) {
2589 return subId;
2590 }
2591 }
2592 Log.e(TAG, "currentTab = " + currentTab + " non mobile tab called this function");
2593 return -1;
2594 }
2595
2596 //SUB SELECT
2597 private boolean isMobileDataAvailable(long subId) {
2598 long[] subIds = SubscriptionManager.getSubId(PhoneConstants.SUB1);
2599 return subIds[0] == subId;
2600 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002601}