blob: 6ee0ef056fd6fcb75905548c8195fab1eaa103c3 [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;
Wink Savilleb003a852014-10-23 10:16:26 -0700271 private Map<Integer,String> mMobileTagMap;
PauloftheWest50e6eca2014-10-03 11:07:14 -0700272
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
Wink Savilleb003a852014-10-23 10:16:26 -0700935 private boolean isMobileDataEnabled(int subId) {
PauloftheWest50e6eca2014-10-03 11:07:14 -0700936 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
PauloftheWest98523c82014-10-28 08:30:05 -0700944 isEnable = mTelephonyManager.getDataEnabled()
945 && (subId == SubscriptionManager.getDefaultDataSubId());
Jeff Sharkey28130d92011-09-02 16:10:24 -0700946 }
PauloftheWest50e6eca2014-10-03 11:07:14 -0700947 return isEnable;
Jeff Sharkey28130d92011-09-02 16:10:24 -0700948 }
949
950 private void setMobileDataEnabled(boolean enabled) {
951 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700952 mTelephonyManager.setDataEnabled(enabled);
Jeff Sharkey28130d92011-09-02 16:10:24 -0700953 mMobileDataEnabled = enabled;
954 updatePolicy(false);
955 }
956
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700957 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700958 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked()
959 && ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700960 }
961
962 private boolean isBandwidthControlEnabled() {
963 try {
964 return mNetworkService.isBandwidthControlEnabled();
965 } catch (RemoteException e) {
966 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
967 return false;
968 }
969 }
970
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700971 public void setRestrictBackground(boolean restrictBackground) {
972 mPolicyManager.setRestrictBackground(restrictBackground);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700973 updateMenuTitles();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700974 }
975
976 private boolean getAppRestrictBackground() {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700977 final int uid = mCurrentApp.key;
978 final int uidPolicy = mPolicyManager.getUidPolicy(uid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700979 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
980 }
981
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700982 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700983 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700984 final int uid = mCurrentApp.key;
985 mPolicyManager.setUidPolicy(
986 uid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700987 mAppRestrict.setChecked(restrictBackground);
988 }
989
Jeff Sharkey8a503642011-06-10 13:31:21 -0700990 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700991 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
992 * current {@link #mTemplate}.
993 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700994 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700995 boolean dataEnabledVisible = mDataEnabledSupported;
996 boolean disableAtLimitVisible = mDisableAtLimitSupported;
997
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700998 if (isAppDetailMode()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700999 dataEnabledVisible = false;
1000 disableAtLimitVisible = false;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001001 }
1002
Jeff Sharkey28130d92011-09-02 16:10:24 -07001003 // TODO: move enabled state directly into policy
PauloftheWest50e6eca2014-10-03 11:07:14 -07001004 if (isMobileTab(mCurrentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001005 mBinding = true;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001006 mDataEnabled.setChecked(isMobileDataEnabled(getSubId(mCurrentTab)));
Jeff Sharkey28130d92011-09-02 16:10:24 -07001007 mBinding = false;
1008 }
1009
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001010 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001011 //SUB SELECT
1012 if (isNetworkPolicyModifiable(policy) && isMobileDataAvailable(getSubId(mCurrentTab))) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001013 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001014 if (!isAppDetailMode()) {
1015 mChart.bindNetworkPolicy(policy);
1016 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001017
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001018 } else {
1019 // controls are disabled; don't bind warning/limit sweeps
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001020 disableAtLimitVisible = false;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001021 mChart.bindNetworkPolicy(null);
1022 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001023
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001024 mDataEnabledView.setVisibility(dataEnabledVisible ? View.VISIBLE : View.GONE);
1025 mDisableAtLimitView.setVisibility(disableAtLimitVisible ? View.VISIBLE : View.GONE);
1026
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001027 if (refreshCycle) {
1028 // generate cycle list based on policy and available history
1029 updateCycleList(policy);
1030 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001031 }
1032
1033 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001034 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
1035 * and available {@link NetworkStatsHistory} data. Always selects the newest
1036 * item, updating the inspection range on {@link #mChart}.
1037 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001038 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001039 // stash away currently selected cycle to try restoring below
1040 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -07001041 mCycleAdapter.clear();
1042
1043 final Context context = mCycleSpinner.getContext();
1044
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001045 long historyStart = Long.MAX_VALUE;
1046 long historyEnd = Long.MIN_VALUE;
1047 if (mChartData != null) {
1048 historyStart = mChartData.network.getStart();
1049 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001050 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001051
Jeff Sharkey461842a2011-09-25 18:22:48 -07001052 final long now = System.currentTimeMillis();
1053 if (historyStart == Long.MAX_VALUE) historyStart = now;
1054 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001055
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001056 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001057 if (policy != null) {
1058 // find the next cycle boundary
1059 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001060
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001061 // walk backwards, generating all valid cycle ranges
1062 while (cycleEnd > historyStart) {
1063 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
1064 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
1065 + historyStart);
1066 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1067 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001068 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001069 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001070
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001071 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001072 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001073 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001074
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001075 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001076 // no policy defined cycles; show entry for each four-week period
1077 long cycleEnd = historyEnd;
1078 while (cycleEnd > historyStart) {
1079 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
1080 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1081 cycleEnd = cycleStart;
1082 }
1083
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001084 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001085 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001086
1087 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001088 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001089 final int position = mCycleAdapter.findNearestPosition(previousItem);
1090 mCycleSpinner.setSelection(position);
1091
1092 // only force-update cycle when changed; skipping preserves any
1093 // user-defined inspection region.
1094 final CycleItem selectedItem = mCycleAdapter.getItem(position);
1095 if (!Objects.equal(selectedItem, previousItem)) {
1096 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1097 } else {
1098 // but still kick off loader for detailed list
1099 updateDetailData();
1100 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001101 } else {
1102 updateDetailData();
1103 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001104 }
1105
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001106 private View.OnClickListener mDataEnabledListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001107 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001108 public void onClick(View v) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001109 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001110
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001111 final boolean dataEnabled = !mDataEnabled.isChecked();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001112 final String currentTab = mCurrentTab;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001113 if (isMobileTab(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001114 if (dataEnabled) {
1115 setMobileDataEnabled(true);
1116 } else {
1117 // disabling data; show confirmation dialog which eventually
1118 // calls setMobileDataEnabled() once user confirms.
1119 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1120 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001121 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001122
Jeff Sharkey28130d92011-09-02 16:10:24 -07001123 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001124 }
1125 };
1126
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001127 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001128 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001129 public void onClick(View v) {
1130 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001131 if (disableAtLimit) {
1132 // enabling limit; show confirmation dialog which eventually
1133 // calls setPolicyLimitBytes() once user confirms.
1134 ConfirmLimitFragment.show(DataUsageSummary.this);
1135 } else {
1136 setPolicyLimitBytes(LIMIT_DISABLED);
1137 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001138 }
1139 };
1140
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001141 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001142 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001143 public void onClick(View v) {
1144 final boolean restrictBackground = !mAppRestrict.isChecked();
1145
1146 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001147 // enabling restriction; show confirmation dialog which
1148 // eventually calls setRestrictBackground() once user
1149 // confirms.
1150 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001151 } else {
1152 setAppRestrictBackground(false);
1153 }
1154 }
1155 };
1156
Jeff Sharkey8a503642011-06-10 13:31:21 -07001157 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001158 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001159 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001160 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001161 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001162
1163 // TODO: sigh, remove this hack once we understand 6450986
1164 if (mUidDetailProvider == null || app == null) return;
1165
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001166 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001167 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001168 }
1169 };
1170
1171 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001172 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001173 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1174 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1175 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001176 // show cycle editor; will eventually call setPolicyCycleDay()
1177 // when user finishes editing.
1178 CycleEditorFragment.show(DataUsageSummary.this);
1179
1180 // reset spinner to something other than "change cycle..."
1181 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001182
1183 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001184 if (LOGD) {
1185 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1186 + cycle.end + "]");
1187 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001188
1189 // update chart to show selected cycle, and update detail data
1190 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001191 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001192
1193 updateDetailData();
1194 }
1195 }
1196
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001197 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001198 public void onNothingSelected(AdapterView<?> parent) {
1199 // ignored
1200 }
1201 };
1202
1203 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001204 * Update details based on {@link #mChart} inspection range depending on
1205 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1206 * of applications data usage, and when {@link #isAppDetailMode()} update
1207 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001208 */
1209 private void updateDetailData() {
1210 if (LOGD) Log.d(TAG, "updateDetailData()");
1211
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001212 final long start = mChart.getInspectStart();
1213 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001214 final long now = System.currentTimeMillis();
1215
1216 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001217
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001218 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001219 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001220 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001221 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001222 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001223 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001224 final long foregroundBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001225 final long totalBytes = defaultBytes + foregroundBytes;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001226
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001227 if (mAppTotal != null) {
1228 mAppTotal.setText(Formatter.formatFileSize(context, totalBytes));
1229 }
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001230 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1231 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1232
1233 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001234 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001235
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001236 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001237
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001238 mCycleSummary.setVisibility(View.GONE);
1239
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001240 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001241 if (mChartData != null) {
1242 entry = mChartData.network.getValues(start, end, now, null);
1243 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001244
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001245 mCycleSummary.setVisibility(View.VISIBLE);
1246
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001247 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001248 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001249 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001250 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001251
1252 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1253 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001254 mCycleSummary.setText(totalPhrase);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001255
PauloftheWest50e6eca2014-10-03 11:07:14 -07001256 if (isMobileTab(mCurrentTab) || TAB_3G.equals(mCurrentTab)
László Dávid0b8bf4e2012-10-24 23:31:47 +02001257 || TAB_4G.equals(mCurrentTab)) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001258 if (isAppDetailMode()) {
1259 mDisclaimer.setVisibility(View.GONE);
1260 } else {
1261 mDisclaimer.setVisibility(View.VISIBLE);
1262 }
Jeff Sharkeye557c332012-04-13 16:04:07 -07001263 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001264 mDisclaimer.setVisibility(View.GONE);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001265 }
1266
Jeff Sharkey92811822012-05-04 11:47:29 -07001267 // initial layout is finished above, ensure we have transitions
1268 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001269 }
1270
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001271 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1272 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001273 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001274 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001275 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001276 }
1277
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001278 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001279 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1280 mChartData = data;
1281 mChart.bindNetworkStats(mChartData.network);
1282 mChart.bindDetailNetworkStats(mChartData.detail);
1283
1284 // calcuate policy cycles based on available data
1285 updatePolicy(true);
1286 updateAppDetail();
1287
1288 // force scroll to top of body when showing detail
1289 if (mChartData.detail != null) {
1290 mListView.smoothScrollToPosition(0);
1291 }
1292 }
1293
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001294 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001295 public void onLoaderReset(Loader<ChartData> loader) {
1296 mChartData = null;
1297 mChart.bindNetworkStats(null);
1298 mChart.bindDetailNetworkStats(null);
1299 }
1300 };
1301
1302 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001303 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001304 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001305 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001306 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001307 }
1308
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001309 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001310 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001311 final int[] restrictedUids = mPolicyManager.getUidsWithPolicy(
Jeff Sharkeye557c332012-04-13 16:04:07 -07001312 POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001313 mAdapter.bindStats(data, restrictedUids);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001314 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001315 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001316
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001317 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001318 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001319 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001320 updateEmptyVisible();
1321 }
1322
1323 private void updateEmptyVisible() {
1324 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1325 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001326 mStupidPadding.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001327 }
1328 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001329
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001330 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001331 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001332 final Context context = getActivity();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001333 if (hasReadyMobileRadio(context)) {
1334 final TelephonyManager tele = TelephonyManager.from(context);
1335 return mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001336 } else {
1337 return false;
1338 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001339 }
1340
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001341 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001342 private void setMobilePolicySplit(boolean split) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001343 final Context context = getActivity();
1344 if (hasReadyMobileRadio(context)) {
1345 final TelephonyManager tele = TelephonyManager.from(context);
1346 mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split);
1347 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001348 }
1349
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001350 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001351 final TelephonyManager tele = TelephonyManager.from(context);
1352 final String actualSubscriberId = tele.getSubscriberId();
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -08001353 return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001354 }
1355
Wink Savilleb003a852014-10-23 10:16:26 -07001356 private static String getActiveSubscriberId(Context context, int subId) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07001357 final TelephonyManager tele = TelephonyManager.from(context);
1358 return tele.getSubscriberId(subId);
1359 }
1360
Jeff Sharkey8a503642011-06-10 13:31:21 -07001361 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001362 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001363 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001364 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001365 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001366
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001367 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001368 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001369 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001370 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001371
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001372 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001373 public void requestWarningEdit() {
1374 WarningEditorFragment.show(DataUsageSummary.this);
1375 }
1376
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001377 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001378 public void requestLimitEdit() {
1379 LimitEditorFragment.show(DataUsageSummary.this);
1380 }
1381 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001382
1383 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001384 * List item that reflects a specific data usage cycle.
1385 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001386 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001387 public CharSequence label;
1388 public long start;
1389 public long end;
1390
Jeff Sharkey8a503642011-06-10 13:31:21 -07001391 CycleItem(CharSequence label) {
1392 this.label = label;
1393 }
1394
1395 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001396 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001397 this.start = start;
1398 this.end = end;
1399 }
1400
Jeff Sharkey8a503642011-06-10 13:31:21 -07001401 @Override
1402 public String toString() {
1403 return label.toString();
1404 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001405
1406 @Override
1407 public boolean equals(Object o) {
1408 if (o instanceof CycleItem) {
1409 final CycleItem another = (CycleItem) o;
1410 return start == another.start && end == another.end;
1411 }
1412 return false;
1413 }
1414
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001415 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001416 public int compareTo(CycleItem another) {
1417 return Long.compare(start, another.start);
1418 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001419 }
1420
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001421 private static final StringBuilder sBuilder = new StringBuilder(50);
1422 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1423 sBuilder, Locale.getDefault());
1424
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001425 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001426 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001427
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001428 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001429 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001430 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1431 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001432 }
1433 }
1434
Jeff Sharkey8a503642011-06-10 13:31:21 -07001435 /**
1436 * Special-case data usage cycle that triggers dialog to change
1437 * {@link NetworkPolicy#cycleDay}.
1438 */
1439 public static class CycleChangeItem extends CycleItem {
1440 public CycleChangeItem(Context context) {
1441 super(context.getString(R.string.data_usage_change_cycle));
1442 }
1443 }
1444
1445 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001446 private boolean mChangePossible = false;
1447 private boolean mChangeVisible = false;
1448
1449 private final CycleChangeItem mChangeItem;
1450
Jeff Sharkey8a503642011-06-10 13:31:21 -07001451 public CycleAdapter(Context context) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001452 super(context, R.layout.data_usage_cycle_item);
1453 setDropDownViewResource(R.layout.data_usage_cycle_item_dropdown);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001454 mChangeItem = new CycleChangeItem(context);
1455 }
1456
1457 public void setChangePossible(boolean possible) {
1458 mChangePossible = possible;
1459 updateChange();
1460 }
1461
1462 public void setChangeVisible(boolean visible) {
1463 mChangeVisible = visible;
1464 updateChange();
1465 }
1466
1467 private void updateChange() {
1468 remove(mChangeItem);
1469 if (mChangePossible && mChangeVisible) {
1470 add(mChangeItem);
1471 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001472 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001473
1474 /**
1475 * Find position of {@link CycleItem} in this adapter which is nearest
1476 * the given {@link CycleItem}.
1477 */
1478 public int findNearestPosition(CycleItem target) {
1479 if (target != null) {
1480 final int count = getCount();
1481 for (int i = count - 1; i >= 0; i--) {
1482 final CycleItem item = getItem(i);
1483 if (item instanceof CycleChangeItem) {
1484 continue;
1485 } else if (item.compareTo(target) >= 0) {
1486 return i;
1487 }
1488 }
1489 }
1490 return 0;
1491 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001492 }
1493
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001494 public static class AppItem implements Comparable<AppItem>, Parcelable {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001495 public static final int CATEGORY_USER = 0;
1496 public static final int CATEGORY_APP_TITLE = 1;
1497 public static final int CATEGORY_APP = 2;
1498
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001499 public final int key;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001500 public boolean restricted;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001501 public int category;
1502
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001503 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001504 public long total;
1505
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001506 public AppItem() {
1507 this.key = 0;
1508 }
1509
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001510 public AppItem(int key) {
1511 this.key = key;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001512 }
1513
1514 public AppItem(Parcel parcel) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001515 key = parcel.readInt();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001516 uids = parcel.readSparseBooleanArray();
1517 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001518 }
1519
1520 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001521 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001522 }
1523
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001524 @Override
1525 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001526 dest.writeInt(key);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001527 dest.writeSparseBooleanArray(uids);
1528 dest.writeLong(total);
1529 }
1530
1531 @Override
1532 public int describeContents() {
1533 return 0;
1534 }
1535
1536 @Override
1537 public int compareTo(AppItem another) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001538 int comparison = Integer.compare(category, another.category);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001539 if (comparison == 0) {
1540 comparison = Long.compare(another.total, total);
1541 }
1542 return comparison;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001543 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001544
1545 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1546 @Override
1547 public AppItem createFromParcel(Parcel in) {
1548 return new AppItem(in);
1549 }
1550
1551 @Override
1552 public AppItem[] newArray(int size) {
1553 return new AppItem[size];
1554 }
1555 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001556 }
1557
Jeff Sharkey8a503642011-06-10 13:31:21 -07001558 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001559 * Adapter of applications, sorted by total usage descending.
1560 */
1561 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001562 private final UidDetailProvider mProvider;
1563 private final int mInsetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001564 private final UserManager mUm;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001565
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001566 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001567 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001568
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001569 public DataUsageAdapter(final UserManager userManager, UidDetailProvider provider, int insetSide) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001570 mProvider = checkNotNull(provider);
1571 mInsetSide = insetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001572 mUm = userManager;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001573 }
1574
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001575 /**
1576 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1577 */
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001578 public void bindStats(NetworkStats stats, int[] restrictedUids) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001579 mItems.clear();
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001580 mLargest = 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001581
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001582 final int currentUserId = ActivityManager.getCurrentUser();
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001583 final List<UserHandle> profiles = mUm.getUserProfiles();
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001584 final SparseArray<AppItem> knownItems = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001585
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001586 NetworkStats.Entry entry = null;
1587 final int size = stats != null ? stats.size() : 0;
1588 for (int i = 0; i < size; i++) {
1589 entry = stats.getValues(i, entry);
1590
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001591 // Decide how to collapse items together
1592 final int uid = entry.uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001593
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001594 final int collapseKey;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001595 final int category;
1596 final int userId = UserHandle.getUserId(uid);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001597 if (UserHandle.isApp(uid)) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001598 if (profiles.contains(new UserHandle(userId))) {
1599 if (userId != currentUserId) {
1600 // Add to a managed user item.
1601 final int managedKey = UidDetailProvider.buildKeyForUser(userId);
1602 accumulate(managedKey, knownItems, entry,
1603 AppItem.CATEGORY_USER);
1604 }
1605 // Add to app item.
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001606 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001607 category = AppItem.CATEGORY_APP;
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001608 } else {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001609 // Add to other user item.
1610 collapseKey = UidDetailProvider.buildKeyForUser(userId);
1611 category = AppItem.CATEGORY_USER;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001612 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001613 } else if (uid == UID_REMOVED || uid == UID_TETHERING) {
1614 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001615 category = AppItem.CATEGORY_APP;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001616 } else {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001617 collapseKey = android.os.Process.SYSTEM_UID;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001618 category = AppItem.CATEGORY_APP;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001619 }
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001620 accumulate(collapseKey, knownItems, entry, category);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001621 }
1622
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001623 final int restrictedUidsMax = restrictedUids.length;
1624 for (int i = 0; i < restrictedUidsMax; ++i) {
1625 final int uid = restrictedUids[i];
1626 // Only splice in restricted state for current user or managed users
1627 if (!profiles.contains(new UserHandle(UserHandle.getUserId(uid)))) {
1628 continue;
1629 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001630
1631 AppItem item = knownItems.get(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001632 if (item == null) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001633 item = new AppItem(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001634 item.total = -1;
1635 mItems.add(item);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001636 knownItems.put(item.key, item);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001637 }
1638 item.restricted = true;
1639 }
1640
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001641 if (!mItems.isEmpty()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001642 final AppItem title = new AppItem();
1643 title.category = AppItem.CATEGORY_APP_TITLE;
1644 mItems.add(title);
1645 }
1646
Jeff Sharkey8a503642011-06-10 13:31:21 -07001647 Collections.sort(mItems);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001648 notifyDataSetChanged();
1649 }
1650
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001651 /**
1652 * Accumulate data usage of a network stats entry for the item mapped by the collapse key.
1653 * Creates the item if needed.
1654 *
1655 * @param collapseKey the collapse key used to map the item.
1656 * @param knownItems collection of known (already existing) items.
1657 * @param entry the network stats entry to extract data usage from.
1658 * @param itemCategory the item is categorized on the list view by this category. Must be
1659 * either AppItem.APP_ITEM_CATEGORY or AppItem.MANAGED_USER_ITEM_CATEGORY
1660 */
1661 private void accumulate(int collapseKey, final SparseArray<AppItem> knownItems,
1662 NetworkStats.Entry entry, int itemCategory) {
1663 final int uid = entry.uid;
1664 AppItem item = knownItems.get(collapseKey);
1665 if (item == null) {
1666 item = new AppItem(collapseKey);
1667 item.category = itemCategory;
1668 mItems.add(item);
1669 knownItems.put(item.key, item);
1670 }
1671 item.addUid(uid);
1672 item.total += entry.rxBytes + entry.txBytes;
1673 if (mLargest < item.total) {
1674 mLargest = item.total;
1675 }
1676 }
1677
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001678 @Override
1679 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001680 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001681 }
1682
1683 @Override
1684 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001685 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001686 }
1687
1688 @Override
1689 public long getItemId(int position) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001690 return mItems.get(position).key;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001691 }
1692
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001693 /**
1694 * See {@link #getItemViewType} for the view types.
1695 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001696 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001697 public int getViewTypeCount() {
1698 return 2;
1699 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001700
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001701 /**
1702 * Returns 1 for separator items and 0 for anything else.
1703 */
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001704 @Override
1705 public int getItemViewType(int position) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001706 final AppItem item = mItems.get(position);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001707 if (item.category == AppItem.CATEGORY_APP_TITLE) {
1708 return 1;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001709 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001710 return 0;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001711 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001712 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001713
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001714 @Override
1715 public boolean areAllItemsEnabled() {
1716 return false;
1717 }
1718
1719 @Override
1720 public boolean isEnabled(int position) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001721 if (position > mItems.size()) {
1722 throw new ArrayIndexOutOfBoundsException();
1723 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001724 return getItemViewType(position) == 0;
1725 }
1726
1727 @Override
1728 public View getView(int position, View convertView, ViewGroup parent) {
1729 final AppItem item = mItems.get(position);
1730 if (getItemViewType(position) == 1) {
1731 if (convertView == null) {
1732 convertView = inflateCategoryHeader(LayoutInflater.from(parent.getContext()),
1733 parent);
1734 }
1735
1736 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
1737 title.setText(R.string.data_usage_app);
1738
1739 } else {
1740 if (convertView == null) {
1741 convertView = LayoutInflater.from(parent.getContext()).inflate(
1742 R.layout.data_usage_item, parent, false);
1743
1744 if (mInsetSide > 0) {
1745 convertView.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
1746 }
1747 }
1748
1749 final Context context = parent.getContext();
1750
1751 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
1752 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1753 android.R.id.progress);
1754
1755 // kick off async load of app details
1756 UidDetailTask.bindView(mProvider, item, convertView);
1757
1758 if (item.restricted && item.total <= 0) {
1759 text1.setText(R.string.data_usage_app_restricted);
1760 progress.setVisibility(View.GONE);
1761 } else {
1762 text1.setText(Formatter.formatFileSize(context, item.total));
1763 progress.setVisibility(View.VISIBLE);
1764 }
1765
1766 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1767 progress.setProgress(percentTotal);
1768 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001769
1770 return convertView;
1771 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001772 }
1773
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001774 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001775 * Empty {@link Fragment} that controls display of UID details in
1776 * {@link DataUsageSummary}.
1777 */
1778 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001779 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001780
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001781 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001782 if (!parent.isAdded()) return;
1783
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001784 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001785 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001786
1787 final AppDetailsFragment fragment = new AppDetailsFragment();
1788 fragment.setArguments(args);
1789 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001790 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1791 ft.add(fragment, TAG_APP_DETAILS);
1792 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001793 ft.setBreadCrumbTitle(
1794 parent.getResources().getString(R.string.data_usage_app_summary_title));
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001795 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001796 }
1797
1798 @Override
1799 public void onStart() {
1800 super.onStart();
1801 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001802 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001803 target.updateBody();
1804 }
1805
1806 @Override
1807 public void onStop() {
1808 super.onStop();
1809 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001810 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001811 target.updateBody();
1812 }
1813 }
1814
1815 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001816 * Dialog to request user confirmation before setting
1817 * {@link NetworkPolicy#limitBytes}.
1818 */
1819 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001820 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001821 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001822
1823 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001824 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001825
Jeff Sharkeyd0a55312014-08-08 10:25:24 -07001826 final NetworkPolicy policy = parent.mPolicyEditor.getPolicy(parent.mTemplate);
1827 if (policy == null) return;
1828
Jeff Sharkey461842a2011-09-25 18:22:48 -07001829 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001830 final CharSequence message;
Jeff Sharkeyd0a55312014-08-08 10:25:24 -07001831 final long minLimitBytes = (long) (policy.warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001832 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001833
1834 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001835 final String currentTab = parent.mCurrentTab;
1836 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001837 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001838 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001839 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001840 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001841 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001842 } else if (isMobileTab(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001843 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001844 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001845 } else {
1846 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001847 }
1848
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001849 final Bundle args = new Bundle();
1850 args.putCharSequence(EXTRA_MESSAGE, message);
1851 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1852
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001853 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1854 dialog.setArguments(args);
1855 dialog.setTargetFragment(parent, 0);
1856 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1857 }
1858
1859 @Override
1860 public Dialog onCreateDialog(Bundle savedInstanceState) {
1861 final Context context = getActivity();
1862
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001863 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001864 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1865
1866 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1867 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001868 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001869
1870 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001871 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001872 public void onClick(DialogInterface dialog, int which) {
1873 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1874 if (target != null) {
1875 target.setPolicyLimitBytes(limitBytes);
1876 }
1877 }
1878 });
1879
1880 return builder.create();
1881 }
1882 }
1883
1884 /**
1885 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1886 */
1887 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001888 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001889
1890 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001891 if (!parent.isAdded()) return;
1892
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001893 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001894 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001895
1896 final CycleEditorFragment dialog = new CycleEditorFragment();
1897 dialog.setArguments(args);
1898 dialog.setTargetFragment(parent, 0);
1899 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1900 }
1901
1902 @Override
1903 public Dialog onCreateDialog(Bundle savedInstanceState) {
1904 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001905 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1906 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001907
1908 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1909 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1910
1911 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1912 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1913
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001914 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1915 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001916
1917 cycleDayPicker.setMinValue(1);
1918 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001919 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001920 cycleDayPicker.setWrapSelectorWheel(true);
1921
1922 builder.setTitle(R.string.data_usage_cycle_editor_title);
1923 builder.setView(view);
1924
1925 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1926 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001927 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001928 public void onClick(DialogInterface dialog, int which) {
Jeff Sharkeyd277de92013-03-25 15:11:25 -07001929 // clear focus to finish pending text edits
1930 cycleDayPicker.clearFocus();
1931
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001932 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001933 final String cycleTimezone = new Time().timezone;
1934 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001935 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001936 }
1937 });
1938
1939 return builder.create();
1940 }
1941 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001942
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001943 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001944 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1945 */
1946 public static class WarningEditorFragment extends DialogFragment {
1947 private static final String EXTRA_TEMPLATE = "template";
1948
1949 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001950 if (!parent.isAdded()) return;
1951
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001952 final Bundle args = new Bundle();
1953 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1954
1955 final WarningEditorFragment dialog = new WarningEditorFragment();
1956 dialog.setArguments(args);
1957 dialog.setTargetFragment(parent, 0);
1958 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1959 }
1960
1961 @Override
1962 public Dialog onCreateDialog(Bundle savedInstanceState) {
1963 final Context context = getActivity();
1964 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1965 final NetworkPolicyEditor editor = target.mPolicyEditor;
1966
1967 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1968 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1969
1970 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1971 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1972
1973 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1974 final long warningBytes = editor.getPolicyWarningBytes(template);
1975 final long limitBytes = editor.getPolicyLimitBytes(template);
1976
1977 bytesPicker.setMinValue(0);
1978 if (limitBytes != LIMIT_DISABLED) {
1979 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1980 } else {
1981 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1982 }
1983 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1984 bytesPicker.setWrapSelectorWheel(false);
1985
1986 builder.setTitle(R.string.data_usage_warning_editor_title);
1987 builder.setView(view);
1988
1989 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1990 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001991 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001992 public void onClick(DialogInterface dialog, int which) {
1993 // clear focus to finish pending text edits
1994 bytesPicker.clearFocus();
1995
1996 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1997 editor.setPolicyWarningBytes(template, bytes);
1998 target.updatePolicy(false);
1999 }
2000 });
2001
2002 return builder.create();
2003 }
2004 }
2005
2006 /**
2007 * Dialog to edit {@link NetworkPolicy#limitBytes}.
2008 */
2009 public static class LimitEditorFragment extends DialogFragment {
2010 private static final String EXTRA_TEMPLATE = "template";
2011
2012 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002013 if (!parent.isAdded()) return;
2014
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002015 final Bundle args = new Bundle();
2016 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
2017
2018 final LimitEditorFragment dialog = new LimitEditorFragment();
2019 dialog.setArguments(args);
2020 dialog.setTargetFragment(parent, 0);
2021 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
2022 }
2023
2024 @Override
2025 public Dialog onCreateDialog(Bundle savedInstanceState) {
2026 final Context context = getActivity();
2027 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2028 final NetworkPolicyEditor editor = target.mPolicyEditor;
2029
2030 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2031 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2032
2033 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
2034 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
2035
2036 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2037 final long warningBytes = editor.getPolicyWarningBytes(template);
2038 final long limitBytes = editor.getPolicyLimitBytes(template);
2039
2040 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09002041 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002042 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
2043 } else {
2044 bytesPicker.setMinValue(0);
2045 }
2046 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
2047 bytesPicker.setWrapSelectorWheel(false);
2048
2049 builder.setTitle(R.string.data_usage_limit_editor_title);
2050 builder.setView(view);
2051
2052 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2053 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002054 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002055 public void onClick(DialogInterface dialog, int which) {
2056 // clear focus to finish pending text edits
2057 bytesPicker.clearFocus();
2058
2059 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
2060 editor.setPolicyLimitBytes(template, bytes);
2061 target.updatePolicy(false);
2062 }
2063 });
2064
2065 return builder.create();
2066 }
2067 }
2068 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07002069 * Dialog to request user confirmation before disabling data.
2070 */
2071 public static class ConfirmDataDisableFragment extends DialogFragment {
2072 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002073 if (!parent.isAdded()) return;
2074
Jeff Sharkey28130d92011-09-02 16:10:24 -07002075 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
2076 dialog.setTargetFragment(parent, 0);
2077 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
2078 }
2079
2080 @Override
2081 public Dialog onCreateDialog(Bundle savedInstanceState) {
2082 final Context context = getActivity();
2083
2084 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2085 builder.setMessage(R.string.data_usage_disable_mobile);
2086
2087 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002088 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07002089 public void onClick(DialogInterface dialog, int which) {
2090 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2091 if (target != null) {
2092 // TODO: extend to modify policy enabled flag.
2093 target.setMobileDataEnabled(false);
2094 }
2095 }
2096 });
2097 builder.setNegativeButton(android.R.string.cancel, null);
2098
2099 return builder.create();
2100 }
2101 }
2102
2103 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002104 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07002105 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002106 */
2107 public static class ConfirmRestrictFragment extends DialogFragment {
2108 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002109 if (!parent.isAdded()) return;
2110
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002111 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
2112 dialog.setTargetFragment(parent, 0);
2113 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
2114 }
2115
2116 @Override
2117 public Dialog onCreateDialog(Bundle savedInstanceState) {
2118 final Context context = getActivity();
2119
2120 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002121 builder.setTitle(R.string.data_usage_restrict_background_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07002122 if (Utils.hasMultipleUsers(context)) {
2123 builder.setMessage(R.string.data_usage_restrict_background_multiuser);
2124 } else {
2125 builder.setMessage(R.string.data_usage_restrict_background);
2126 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002127
2128 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002129 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002130 public void onClick(DialogInterface dialog, int which) {
2131 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2132 if (target != null) {
2133 target.setRestrictBackground(true);
2134 }
2135 }
2136 });
2137 builder.setNegativeButton(android.R.string.cancel, null);
2138
2139 return builder.create();
2140 }
2141 }
2142
2143 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002144 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
2145 * change has been denied, usually based on
2146 * {@link DataUsageSummary#hasLimitedNetworks()}.
2147 */
2148 public static class DeniedRestrictFragment extends DialogFragment {
2149 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002150 if (!parent.isAdded()) return;
2151
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002152 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
2153 dialog.setTargetFragment(parent, 0);
2154 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
2155 }
2156
2157 @Override
2158 public Dialog onCreateDialog(Bundle savedInstanceState) {
2159 final Context context = getActivity();
2160
2161 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2162 builder.setTitle(R.string.data_usage_app_restrict_background);
2163 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
2164 builder.setPositiveButton(android.R.string.ok, null);
2165
2166 return builder.create();
2167 }
2168 }
2169
2170 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002171 * Dialog to request user confirmation before setting
2172 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
2173 */
2174 public static class ConfirmAppRestrictFragment extends DialogFragment {
2175 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002176 if (!parent.isAdded()) return;
2177
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002178 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2179 dialog.setTargetFragment(parent, 0);
2180 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2181 }
2182
2183 @Override
2184 public Dialog onCreateDialog(Bundle savedInstanceState) {
2185 final Context context = getActivity();
2186
2187 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002188 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2189 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2190
2191 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002192 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002193 public void onClick(DialogInterface dialog, int which) {
2194 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2195 if (target != null) {
2196 target.setAppRestrictBackground(true);
2197 }
2198 }
2199 });
2200 builder.setNegativeButton(android.R.string.cancel, null);
2201
2202 return builder.create();
2203 }
2204 }
2205
2206 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002207 * Compute default tab that should be selected, based on
2208 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2209 */
2210 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002211 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002212 if (template == null) {
Wink Savilleb003a852014-10-23 10:16:26 -07002213 final int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
PauloftheWest50e6eca2014-10-03 11:07:14 -07002214 SubscriptionManager.INVALID_SUB_ID);
2215 if (SubscriptionManager.isValidSubId(subId)) {
2216 return TAB_MOBILE + String.valueOf(subId);
2217 }
2218 return null;
2219 }
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002220
2221 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002222 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002223 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002224 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002225 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002226 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002227 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002228 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002229 return TAB_WIFI;
2230 default:
2231 return null;
2232 }
2233 }
2234
2235 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002236 * Background task that loads {@link UidDetail}, binding to
2237 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002238 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002239 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2240 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002241 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002242 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002243
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002244 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002245 mProvider = checkNotNull(provider);
2246 mItem = checkNotNull(item);
2247 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002248 }
2249
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002250 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002251 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002252 final UidDetailTask existing = (UidDetailTask) target.getTag();
2253 if (existing != null) {
2254 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002255 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002256
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002257 final UidDetail cachedDetail = provider.getUidDetail(item.key, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002258 if (cachedDetail != null) {
2259 bindView(cachedDetail, target);
2260 } else {
2261 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2262 AsyncTask.THREAD_POOL_EXECUTOR));
2263 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002264 }
2265
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002266 private static void bindView(UidDetail detail, View target) {
2267 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2268 final TextView title = (TextView) target.findViewById(android.R.id.title);
2269
2270 if (detail != null) {
2271 icon.setImageDrawable(detail.icon);
2272 title.setText(detail.label);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +01002273 title.setContentDescription(detail.contentDescription);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002274 } else {
2275 icon.setImageDrawable(null);
2276 title.setText(null);
2277 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002278 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002279
2280 @Override
2281 protected void onPreExecute() {
2282 bindView(null, mTarget);
2283 }
2284
2285 @Override
2286 protected UidDetail doInBackground(Void... params) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002287 return mProvider.getUidDetail(mItem.key, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002288 }
2289
2290 @Override
2291 protected void onPostExecute(UidDetail result) {
2292 bindView(result, mTarget);
2293 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002294 }
2295
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002296 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002297 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002298 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002299 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002300 if (TEST_RADIOS) {
2301 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2302 }
2303
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002304 final ConnectivityManager conn = ConnectivityManager.from(context);
2305 final TelephonyManager tele = TelephonyManager.from(context);
2306
PauloftheWest50e6eca2014-10-03 11:07:14 -07002307 final List<SubInfoRecord> subInfoList = SubscriptionManager.getActiveSubInfoList();
2308 // No activated Subscription
2309 if (subInfoList == null) {
2310 return false;
2311 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002312 // require both supported network and ready SIM
PauloftheWest50e6eca2014-10-03 11:07:14 -07002313 boolean isReady = true;
2314 for (SubInfoRecord subInfo : subInfoList) {
2315 isReady = isReady & tele.getSimState(subInfo.slotId) == SIM_STATE_READY;
2316 }
2317 return conn.isNetworkSupported(TYPE_MOBILE) && isReady;
2318 }
2319
2320 /*
2321 * TODO: consider adding to TelephonyManager or SubscritpionManager.
2322 */
Wink Savilleb003a852014-10-23 10:16:26 -07002323 public static boolean hasReadyMobileRadio(Context context, int subId) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07002324 if (TEST_RADIOS) {
2325 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2326 }
2327
2328 final ConnectivityManager conn = ConnectivityManager.from(context);
2329 final TelephonyManager tele = TelephonyManager.from(context);
2330 final int slotId = SubscriptionManager.getSlotId(subId);
2331 final boolean isReady = tele.getSimState(slotId) == SIM_STATE_READY;
2332
2333 return conn.isNetworkSupported(TYPE_MOBILE) && isReady;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002334 }
2335
2336 /**
2337 * Test if device has a mobile 4G data radio.
2338 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002339 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002340 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2341 return false;
2342 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002343 if (TEST_RADIOS) {
2344 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2345 }
2346
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002347 final ConnectivityManager conn = ConnectivityManager.from(context);
2348 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002349
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002350 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Wink Saville55434042012-06-14 12:33:43 -07002351 final boolean hasLte = (tele.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE)
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002352 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002353 return hasWimax || hasLte;
2354 }
2355
2356 /**
2357 * Test if device has a Wi-Fi data radio.
2358 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002359 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002360 if (TEST_RADIOS) {
2361 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2362 }
2363
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002364 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002365 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002366 }
2367
2368 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002369 * Test if device has an ethernet network connection.
2370 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002371 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002372 if (TEST_RADIOS) {
2373 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2374 }
2375
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002376 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002377 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2378
2379 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002380 if (mStatsSession != null) {
2381 try {
2382 ethernetBytes = mStatsSession.getSummaryForNetwork(
2383 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2384 .getTotalBytes();
2385 } catch (RemoteException e) {
2386 throw new RuntimeException(e);
2387 }
2388 } else {
2389 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002390 }
2391
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002392 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002393 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002394 }
2395
2396 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002397 * Inflate a {@link Preference} style layout, adding the given {@link View}
2398 * widget into {@link android.R.id#widget_frame}.
2399 */
2400 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2401 final View view = inflater.inflate(R.layout.preference, root, false);
2402 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2403 android.R.id.widget_frame);
2404 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2405 return view;
2406 }
2407
Jeff Sharkeyb6548462014-07-21 15:38:06 -07002408 private static View inflateCategoryHeader(LayoutInflater inflater, ViewGroup root) {
2409 final TypedArray a = inflater.getContext().obtainStyledAttributes(null,
2410 com.android.internal.R.styleable.Preference,
2411 com.android.internal.R.attr.preferenceCategoryStyle, 0);
2412 final int resId = a.getResourceId(com.android.internal.R.styleable.Preference_layout, 0);
2413 return inflater.inflate(resId, root, false);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002414 }
2415
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002416 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002417 * Test if any networks are currently limited.
2418 */
2419 private boolean hasLimitedNetworks() {
2420 return !buildLimitedNetworksList().isEmpty();
2421 }
2422
2423 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002424 * Build string describing currently limited networks, which defines when
2425 * background data is restricted.
2426 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002427 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002428 private CharSequence buildLimitedNetworksString() {
2429 final List<CharSequence> limited = buildLimitedNetworksList();
2430
2431 // handle case where no networks limited
2432 if (limited.isEmpty()) {
2433 limited.add(getText(R.string.data_usage_list_none));
2434 }
2435
2436 return TextUtils.join(limited);
2437 }
2438
2439 /**
2440 * Build list of currently limited networks, which defines when background
2441 * data is restricted.
2442 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002443 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002444 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002445 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002446
2447 // build combined list of all limited networks
2448 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002449
2450 final TelephonyManager tele = TelephonyManager.from(context);
2451 if (tele.getSimState() == SIM_STATE_READY) {
2452 final String subscriberId = getActiveSubscriberId(context);
2453 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2454 limited.add(getText(R.string.data_usage_list_mobile));
2455 }
2456 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2457 limited.add(getText(R.string.data_usage_tab_3g));
2458 }
2459 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2460 limited.add(getText(R.string.data_usage_tab_4g));
2461 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002462 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002463
2464 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002465 limited.add(getText(R.string.data_usage_tab_wifi));
2466 }
2467 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2468 limited.add(getText(R.string.data_usage_tab_ethernet));
2469 }
2470
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002471 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002472 }
2473
2474 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002475 * Inset both selector and divider {@link Drawable} on the given
2476 * {@link ListView} by the requested dimensions.
2477 */
2478 private static void insetListViewDrawables(ListView view, int insetSide) {
2479 final Drawable selector = view.getSelector();
2480 final Drawable divider = view.getDivider();
2481
2482 // fully unregister these drawables so callbacks can be maintained after
2483 // wrapping below.
2484 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2485 view.setSelector(stub);
2486 view.setDivider(stub);
2487
2488 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2489 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2490 }
2491
2492 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002493 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002494 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002495 */
2496 private static void setPreferenceTitle(View parent, int resId) {
2497 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2498 title.setText(resId);
2499 }
2500
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002501 /**
2502 * Set {@link android.R.id#summary} for a preference view inflated with
2503 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2504 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002505 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002506 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2507 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002508 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002509 }
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002510
2511 /**
2512 * For search
2513 */
2514 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -07002515 new BaseSearchIndexProvider() {
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002516 @Override
2517 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
2518 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
2519
2520 final Resources res = context.getResources();
2521
2522 // Add fragment title
2523 SearchIndexableRaw data = new SearchIndexableRaw(context);
2524 data.title = res.getString(R.string.data_usage_summary_title);
2525 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2526 result.add(data);
2527
2528 // Mobile data
2529 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002530 data.key = DATA_USAGE_ENABLE_MOBILE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002531 data.title = res.getString(R.string.data_usage_enable_mobile);
2532 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2533 result.add(data);
2534
2535 // Set mobile data limit
2536 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002537 data.key = DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002538 data.title = res.getString(R.string.data_usage_disable_mobile_limit);
2539 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2540 result.add(data);
2541
Fabrice Di Megliof2a52262014-04-17 17:20:27 -07002542 // Data usage cycle
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002543 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002544 data.key = DATA_USAGE_CYCLE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002545 data.title = res.getString(R.string.data_usage_cycle);
2546 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2547 result.add(data);
2548
2549 return result;
2550 }
2551 };
2552
PauloftheWest50e6eca2014-10-03 11:07:14 -07002553 private void addMobileTab(Context context, List<SubInfoRecord> subInfoList) {
2554 if (subInfoList != null) {
2555 for (SubInfoRecord subInfo : mSubInfoList) {
2556 if (hasReadyMobileRadio(context,subInfo.subId)) {
2557 mTabHost.addTab(buildTabSpec(mMobileTagMap.get(subInfo.subId),
2558 subInfo.displayName));
2559 }
2560 }
2561 }
2562 }
2563
2564 /**
2565 * Init a map with subId key and mobile tag name
2566 * @param subInfoList The subscription Info List
2567 * @return The map or null if no activated subscription
2568 */
Wink Savilleb003a852014-10-23 10:16:26 -07002569 private Map<Integer, String> initMobileTabTag(List<SubInfoRecord> subInfoList) {
2570 Map<Integer, String> map = null;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002571 if (subInfoList != null) {
2572 String mobileTag;
Wink Savilleb003a852014-10-23 10:16:26 -07002573 map = new HashMap<Integer, String>();
PauloftheWest50e6eca2014-10-03 11:07:14 -07002574 for (SubInfoRecord subInfo : subInfoList) {
2575 mobileTag = TAB_MOBILE + String.valueOf(subInfo.subId);
2576 map.put(subInfo.subId,mobileTag);
2577 }
2578 }
2579 return map;
2580 }
2581
2582 private static boolean isMobileTab(String currentTab) {
2583 return currentTab != null ? currentTab.contains(TAB_MOBILE) : false;
2584 }
2585
Wink Savilleb003a852014-10-23 10:16:26 -07002586 private int getSubId(String currentTab) {
2587 Set<Integer> set = mMobileTagMap.keySet();
2588 for (Integer subId : set) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07002589 if (mMobileTagMap.get(subId).equals(currentTab)) {
2590 return subId;
2591 }
2592 }
2593 Log.e(TAG, "currentTab = " + currentTab + " non mobile tab called this function");
2594 return -1;
2595 }
2596
2597 //SUB SELECT
PauloftheWest98523c82014-10-28 08:30:05 -07002598 private boolean isMobileDataAvailable(long subId) {
Wink Savilleb003a852014-10-23 10:16:26 -07002599 int[] subIds = SubscriptionManager.getSubId(PhoneConstants.SUB1);
PauloftheWest98523c82014-10-28 08:30:05 -07002600 if (subIds != null && subIds[0] == subId) {
2601 return true;
2602 }
2603
2604 subIds = SubscriptionManager.getSubId(PhoneConstants.SUB2);
2605 if (subIds != null && subIds[0] == subId) {
2606 return true;
2607 }
2608
2609 subIds = SubscriptionManager.getSubId(PhoneConstants.SUB3);
2610 if (subIds != null && subIds[0] == subId) {
2611 return true;
2612 }
2613 return false;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002614 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002615}