blob: 6ad1b9c26a8beee53b895596e6dd38c9bff99e1b [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) {
PauloftheWestde338442014-10-29 14:46:44 -07001115 // If we are showing the Sim Card tile then we are a Multi-Sim device.
1116 if (Utils.showSimCardTile(getActivity())) {
1117 handleMultiSimDataDialog();
1118 } else {
1119 setMobileDataEnabled(true);
1120 }
Jeff Sharkey28130d92011-09-02 16:10:24 -07001121 } else {
1122 // disabling data; show confirmation dialog which eventually
1123 // calls setMobileDataEnabled() once user confirms.
1124 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1125 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001126 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001127
Jeff Sharkey28130d92011-09-02 16:10:24 -07001128 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001129 }
1130 };
1131
PauloftheWestde338442014-10-29 14:46:44 -07001132 private void handleMultiSimDataDialog() {
1133 final SubInfoRecord currentSir = getCurrentTabSubInfo(getActivity());
1134 final SubInfoRecord nextSir = SubscriptionManager.getSubInfoForSubscriber(
1135 SubscriptionManager.getDefaultDataSubId());
1136
1137 if (currentSir.subId == nextSir.subId) {
1138 setMobileDataEnabled(true);
1139 updateBody();
1140 return;
1141 }
1142
1143 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
1144
1145 builder.setTitle(R.string.sim_change_data_title);
1146 builder.setMessage(getActivity().getResources().getString(R.string.sim_change_data_message,
1147 currentSir.displayName, nextSir.displayName));
1148
1149 builder.setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
1150 @Override
1151 public void onClick(DialogInterface dialog, int id) {
1152 SubscriptionManager.setDefaultDataSubId(currentSir.subId);
1153 setMobileDataEnabled(true);
1154 updateBody();
1155 }
1156 });
1157 builder.setNegativeButton(R.string.cancel, null);
1158
1159 builder.create().show();
1160 }
1161
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001162 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001163 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001164 public void onClick(View v) {
1165 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001166 if (disableAtLimit) {
1167 // enabling limit; show confirmation dialog which eventually
1168 // calls setPolicyLimitBytes() once user confirms.
1169 ConfirmLimitFragment.show(DataUsageSummary.this);
1170 } else {
1171 setPolicyLimitBytes(LIMIT_DISABLED);
1172 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001173 }
1174 };
1175
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001176 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001177 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001178 public void onClick(View v) {
1179 final boolean restrictBackground = !mAppRestrict.isChecked();
1180
1181 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001182 // enabling restriction; show confirmation dialog which
1183 // eventually calls setRestrictBackground() once user
1184 // confirms.
1185 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001186 } else {
1187 setAppRestrictBackground(false);
1188 }
1189 }
1190 };
1191
Jeff Sharkey8a503642011-06-10 13:31:21 -07001192 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001193 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001194 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001195 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001196 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001197
1198 // TODO: sigh, remove this hack once we understand 6450986
1199 if (mUidDetailProvider == null || app == null) return;
1200
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001201 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001202 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001203 }
1204 };
1205
1206 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001207 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001208 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1209 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1210 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001211 // show cycle editor; will eventually call setPolicyCycleDay()
1212 // when user finishes editing.
1213 CycleEditorFragment.show(DataUsageSummary.this);
1214
1215 // reset spinner to something other than "change cycle..."
1216 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001217
1218 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001219 if (LOGD) {
1220 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1221 + cycle.end + "]");
1222 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001223
1224 // update chart to show selected cycle, and update detail data
1225 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001226 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001227
1228 updateDetailData();
1229 }
1230 }
1231
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001232 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001233 public void onNothingSelected(AdapterView<?> parent) {
1234 // ignored
1235 }
1236 };
1237
1238 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001239 * Update details based on {@link #mChart} inspection range depending on
1240 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1241 * of applications data usage, and when {@link #isAppDetailMode()} update
1242 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001243 */
1244 private void updateDetailData() {
1245 if (LOGD) Log.d(TAG, "updateDetailData()");
1246
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001247 final long start = mChart.getInspectStart();
1248 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001249 final long now = System.currentTimeMillis();
1250
1251 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001252
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001253 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001254 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001255 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001256 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001257 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001258 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001259 final long foregroundBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001260 final long totalBytes = defaultBytes + foregroundBytes;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001261
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001262 if (mAppTotal != null) {
1263 mAppTotal.setText(Formatter.formatFileSize(context, totalBytes));
1264 }
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001265 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1266 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1267
1268 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001269 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001270
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001271 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001272
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001273 mCycleSummary.setVisibility(View.GONE);
1274
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001275 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001276 if (mChartData != null) {
1277 entry = mChartData.network.getValues(start, end, now, null);
1278 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001279
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001280 mCycleSummary.setVisibility(View.VISIBLE);
1281
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001282 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001283 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001284 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001285 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001286
1287 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1288 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001289 mCycleSummary.setText(totalPhrase);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001290
PauloftheWest50e6eca2014-10-03 11:07:14 -07001291 if (isMobileTab(mCurrentTab) || TAB_3G.equals(mCurrentTab)
László Dávid0b8bf4e2012-10-24 23:31:47 +02001292 || TAB_4G.equals(mCurrentTab)) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001293 if (isAppDetailMode()) {
1294 mDisclaimer.setVisibility(View.GONE);
1295 } else {
1296 mDisclaimer.setVisibility(View.VISIBLE);
1297 }
Jeff Sharkeye557c332012-04-13 16:04:07 -07001298 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001299 mDisclaimer.setVisibility(View.GONE);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001300 }
1301
Jeff Sharkey92811822012-05-04 11:47:29 -07001302 // initial layout is finished above, ensure we have transitions
1303 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001304 }
1305
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001306 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1307 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001308 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001309 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001310 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001311 }
1312
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001313 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001314 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1315 mChartData = data;
1316 mChart.bindNetworkStats(mChartData.network);
1317 mChart.bindDetailNetworkStats(mChartData.detail);
1318
1319 // calcuate policy cycles based on available data
1320 updatePolicy(true);
1321 updateAppDetail();
1322
1323 // force scroll to top of body when showing detail
1324 if (mChartData.detail != null) {
1325 mListView.smoothScrollToPosition(0);
1326 }
1327 }
1328
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001329 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001330 public void onLoaderReset(Loader<ChartData> loader) {
1331 mChartData = null;
1332 mChart.bindNetworkStats(null);
1333 mChart.bindDetailNetworkStats(null);
1334 }
1335 };
1336
1337 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001338 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001339 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001340 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001341 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001342 }
1343
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001344 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001345 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001346 final int[] restrictedUids = mPolicyManager.getUidsWithPolicy(
Jeff Sharkeye557c332012-04-13 16:04:07 -07001347 POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001348 mAdapter.bindStats(data, restrictedUids);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001349 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001350 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001351
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001352 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001353 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001354 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001355 updateEmptyVisible();
1356 }
1357
1358 private void updateEmptyVisible() {
1359 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1360 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001361 mStupidPadding.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001362 }
1363 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001364
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001365 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001366 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001367 final Context context = getActivity();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001368 if (hasReadyMobileRadio(context)) {
1369 final TelephonyManager tele = TelephonyManager.from(context);
1370 return mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001371 } else {
1372 return false;
1373 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001374 }
1375
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001376 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001377 private void setMobilePolicySplit(boolean split) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001378 final Context context = getActivity();
1379 if (hasReadyMobileRadio(context)) {
1380 final TelephonyManager tele = TelephonyManager.from(context);
1381 mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split);
1382 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001383 }
1384
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001385 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001386 final TelephonyManager tele = TelephonyManager.from(context);
1387 final String actualSubscriberId = tele.getSubscriberId();
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -08001388 return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001389 }
1390
Wink Savilleb003a852014-10-23 10:16:26 -07001391 private static String getActiveSubscriberId(Context context, int subId) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07001392 final TelephonyManager tele = TelephonyManager.from(context);
1393 return tele.getSubscriberId(subId);
1394 }
1395
Jeff Sharkey8a503642011-06-10 13:31:21 -07001396 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001397 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001398 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001399 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001400 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001401
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001402 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001403 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001404 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001405 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001406
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001407 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001408 public void requestWarningEdit() {
1409 WarningEditorFragment.show(DataUsageSummary.this);
1410 }
1411
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001412 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001413 public void requestLimitEdit() {
1414 LimitEditorFragment.show(DataUsageSummary.this);
1415 }
1416 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001417
1418 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001419 * List item that reflects a specific data usage cycle.
1420 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001421 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001422 public CharSequence label;
1423 public long start;
1424 public long end;
1425
Jeff Sharkey8a503642011-06-10 13:31:21 -07001426 CycleItem(CharSequence label) {
1427 this.label = label;
1428 }
1429
1430 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001431 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001432 this.start = start;
1433 this.end = end;
1434 }
1435
Jeff Sharkey8a503642011-06-10 13:31:21 -07001436 @Override
1437 public String toString() {
1438 return label.toString();
1439 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001440
1441 @Override
1442 public boolean equals(Object o) {
1443 if (o instanceof CycleItem) {
1444 final CycleItem another = (CycleItem) o;
1445 return start == another.start && end == another.end;
1446 }
1447 return false;
1448 }
1449
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001450 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001451 public int compareTo(CycleItem another) {
1452 return Long.compare(start, another.start);
1453 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001454 }
1455
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001456 private static final StringBuilder sBuilder = new StringBuilder(50);
1457 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1458 sBuilder, Locale.getDefault());
1459
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001460 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001461 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001462
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001463 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001464 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001465 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1466 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001467 }
1468 }
1469
Jeff Sharkey8a503642011-06-10 13:31:21 -07001470 /**
1471 * Special-case data usage cycle that triggers dialog to change
1472 * {@link NetworkPolicy#cycleDay}.
1473 */
1474 public static class CycleChangeItem extends CycleItem {
1475 public CycleChangeItem(Context context) {
1476 super(context.getString(R.string.data_usage_change_cycle));
1477 }
1478 }
1479
1480 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001481 private boolean mChangePossible = false;
1482 private boolean mChangeVisible = false;
1483
1484 private final CycleChangeItem mChangeItem;
1485
Jeff Sharkey8a503642011-06-10 13:31:21 -07001486 public CycleAdapter(Context context) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001487 super(context, R.layout.data_usage_cycle_item);
1488 setDropDownViewResource(R.layout.data_usage_cycle_item_dropdown);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001489 mChangeItem = new CycleChangeItem(context);
1490 }
1491
1492 public void setChangePossible(boolean possible) {
1493 mChangePossible = possible;
1494 updateChange();
1495 }
1496
1497 public void setChangeVisible(boolean visible) {
1498 mChangeVisible = visible;
1499 updateChange();
1500 }
1501
1502 private void updateChange() {
1503 remove(mChangeItem);
1504 if (mChangePossible && mChangeVisible) {
1505 add(mChangeItem);
1506 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001507 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001508
1509 /**
1510 * Find position of {@link CycleItem} in this adapter which is nearest
1511 * the given {@link CycleItem}.
1512 */
1513 public int findNearestPosition(CycleItem target) {
1514 if (target != null) {
1515 final int count = getCount();
1516 for (int i = count - 1; i >= 0; i--) {
1517 final CycleItem item = getItem(i);
1518 if (item instanceof CycleChangeItem) {
1519 continue;
1520 } else if (item.compareTo(target) >= 0) {
1521 return i;
1522 }
1523 }
1524 }
1525 return 0;
1526 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001527 }
1528
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001529 public static class AppItem implements Comparable<AppItem>, Parcelable {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001530 public static final int CATEGORY_USER = 0;
1531 public static final int CATEGORY_APP_TITLE = 1;
1532 public static final int CATEGORY_APP = 2;
1533
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001534 public final int key;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001535 public boolean restricted;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001536 public int category;
1537
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001538 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001539 public long total;
1540
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001541 public AppItem() {
1542 this.key = 0;
1543 }
1544
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001545 public AppItem(int key) {
1546 this.key = key;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001547 }
1548
1549 public AppItem(Parcel parcel) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001550 key = parcel.readInt();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001551 uids = parcel.readSparseBooleanArray();
1552 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001553 }
1554
1555 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001556 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001557 }
1558
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001559 @Override
1560 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001561 dest.writeInt(key);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001562 dest.writeSparseBooleanArray(uids);
1563 dest.writeLong(total);
1564 }
1565
1566 @Override
1567 public int describeContents() {
1568 return 0;
1569 }
1570
1571 @Override
1572 public int compareTo(AppItem another) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001573 int comparison = Integer.compare(category, another.category);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001574 if (comparison == 0) {
1575 comparison = Long.compare(another.total, total);
1576 }
1577 return comparison;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001578 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001579
1580 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1581 @Override
1582 public AppItem createFromParcel(Parcel in) {
1583 return new AppItem(in);
1584 }
1585
1586 @Override
1587 public AppItem[] newArray(int size) {
1588 return new AppItem[size];
1589 }
1590 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001591 }
1592
Jeff Sharkey8a503642011-06-10 13:31:21 -07001593 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001594 * Adapter of applications, sorted by total usage descending.
1595 */
1596 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001597 private final UidDetailProvider mProvider;
1598 private final int mInsetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001599 private final UserManager mUm;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001600
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001601 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001602 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001603
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001604 public DataUsageAdapter(final UserManager userManager, UidDetailProvider provider, int insetSide) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001605 mProvider = checkNotNull(provider);
1606 mInsetSide = insetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001607 mUm = userManager;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001608 }
1609
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001610 /**
1611 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1612 */
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001613 public void bindStats(NetworkStats stats, int[] restrictedUids) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001614 mItems.clear();
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001615 mLargest = 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001616
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001617 final int currentUserId = ActivityManager.getCurrentUser();
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001618 final List<UserHandle> profiles = mUm.getUserProfiles();
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001619 final SparseArray<AppItem> knownItems = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001620
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001621 NetworkStats.Entry entry = null;
1622 final int size = stats != null ? stats.size() : 0;
1623 for (int i = 0; i < size; i++) {
1624 entry = stats.getValues(i, entry);
1625
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001626 // Decide how to collapse items together
1627 final int uid = entry.uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001628
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001629 final int collapseKey;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001630 final int category;
1631 final int userId = UserHandle.getUserId(uid);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001632 if (UserHandle.isApp(uid)) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001633 if (profiles.contains(new UserHandle(userId))) {
1634 if (userId != currentUserId) {
1635 // Add to a managed user item.
1636 final int managedKey = UidDetailProvider.buildKeyForUser(userId);
1637 accumulate(managedKey, knownItems, entry,
1638 AppItem.CATEGORY_USER);
1639 }
1640 // Add to app item.
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001641 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001642 category = AppItem.CATEGORY_APP;
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001643 } else {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001644 // Add to other user item.
1645 collapseKey = UidDetailProvider.buildKeyForUser(userId);
1646 category = AppItem.CATEGORY_USER;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001647 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001648 } else if (uid == UID_REMOVED || uid == UID_TETHERING) {
1649 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001650 category = AppItem.CATEGORY_APP;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001651 } else {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001652 collapseKey = android.os.Process.SYSTEM_UID;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001653 category = AppItem.CATEGORY_APP;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001654 }
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001655 accumulate(collapseKey, knownItems, entry, category);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001656 }
1657
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001658 final int restrictedUidsMax = restrictedUids.length;
1659 for (int i = 0; i < restrictedUidsMax; ++i) {
1660 final int uid = restrictedUids[i];
1661 // Only splice in restricted state for current user or managed users
1662 if (!profiles.contains(new UserHandle(UserHandle.getUserId(uid)))) {
1663 continue;
1664 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001665
1666 AppItem item = knownItems.get(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001667 if (item == null) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001668 item = new AppItem(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001669 item.total = -1;
1670 mItems.add(item);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001671 knownItems.put(item.key, item);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001672 }
1673 item.restricted = true;
1674 }
1675
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001676 if (!mItems.isEmpty()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001677 final AppItem title = new AppItem();
1678 title.category = AppItem.CATEGORY_APP_TITLE;
1679 mItems.add(title);
1680 }
1681
Jeff Sharkey8a503642011-06-10 13:31:21 -07001682 Collections.sort(mItems);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001683 notifyDataSetChanged();
1684 }
1685
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001686 /**
1687 * Accumulate data usage of a network stats entry for the item mapped by the collapse key.
1688 * Creates the item if needed.
1689 *
1690 * @param collapseKey the collapse key used to map the item.
1691 * @param knownItems collection of known (already existing) items.
1692 * @param entry the network stats entry to extract data usage from.
1693 * @param itemCategory the item is categorized on the list view by this category. Must be
1694 * either AppItem.APP_ITEM_CATEGORY or AppItem.MANAGED_USER_ITEM_CATEGORY
1695 */
1696 private void accumulate(int collapseKey, final SparseArray<AppItem> knownItems,
1697 NetworkStats.Entry entry, int itemCategory) {
1698 final int uid = entry.uid;
1699 AppItem item = knownItems.get(collapseKey);
1700 if (item == null) {
1701 item = new AppItem(collapseKey);
1702 item.category = itemCategory;
1703 mItems.add(item);
1704 knownItems.put(item.key, item);
1705 }
1706 item.addUid(uid);
1707 item.total += entry.rxBytes + entry.txBytes;
1708 if (mLargest < item.total) {
1709 mLargest = item.total;
1710 }
1711 }
1712
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001713 @Override
1714 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001715 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001716 }
1717
1718 @Override
1719 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001720 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001721 }
1722
1723 @Override
1724 public long getItemId(int position) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001725 return mItems.get(position).key;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001726 }
1727
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001728 /**
1729 * See {@link #getItemViewType} for the view types.
1730 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001731 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001732 public int getViewTypeCount() {
1733 return 2;
1734 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001735
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001736 /**
1737 * Returns 1 for separator items and 0 for anything else.
1738 */
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001739 @Override
1740 public int getItemViewType(int position) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001741 final AppItem item = mItems.get(position);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001742 if (item.category == AppItem.CATEGORY_APP_TITLE) {
1743 return 1;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001744 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001745 return 0;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001746 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001747 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001748
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001749 @Override
1750 public boolean areAllItemsEnabled() {
1751 return false;
1752 }
1753
1754 @Override
1755 public boolean isEnabled(int position) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001756 if (position > mItems.size()) {
1757 throw new ArrayIndexOutOfBoundsException();
1758 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001759 return getItemViewType(position) == 0;
1760 }
1761
1762 @Override
1763 public View getView(int position, View convertView, ViewGroup parent) {
1764 final AppItem item = mItems.get(position);
1765 if (getItemViewType(position) == 1) {
1766 if (convertView == null) {
1767 convertView = inflateCategoryHeader(LayoutInflater.from(parent.getContext()),
1768 parent);
1769 }
1770
1771 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
1772 title.setText(R.string.data_usage_app);
1773
1774 } else {
1775 if (convertView == null) {
1776 convertView = LayoutInflater.from(parent.getContext()).inflate(
1777 R.layout.data_usage_item, parent, false);
1778
1779 if (mInsetSide > 0) {
1780 convertView.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
1781 }
1782 }
1783
1784 final Context context = parent.getContext();
1785
1786 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
1787 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1788 android.R.id.progress);
1789
1790 // kick off async load of app details
1791 UidDetailTask.bindView(mProvider, item, convertView);
1792
1793 if (item.restricted && item.total <= 0) {
1794 text1.setText(R.string.data_usage_app_restricted);
1795 progress.setVisibility(View.GONE);
1796 } else {
1797 text1.setText(Formatter.formatFileSize(context, item.total));
1798 progress.setVisibility(View.VISIBLE);
1799 }
1800
1801 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1802 progress.setProgress(percentTotal);
1803 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001804
1805 return convertView;
1806 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001807 }
1808
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001809 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001810 * Empty {@link Fragment} that controls display of UID details in
1811 * {@link DataUsageSummary}.
1812 */
1813 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001814 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001815
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001816 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001817 if (!parent.isAdded()) return;
1818
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001819 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001820 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001821
1822 final AppDetailsFragment fragment = new AppDetailsFragment();
1823 fragment.setArguments(args);
1824 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001825 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1826 ft.add(fragment, TAG_APP_DETAILS);
1827 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001828 ft.setBreadCrumbTitle(
1829 parent.getResources().getString(R.string.data_usage_app_summary_title));
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001830 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001831 }
1832
1833 @Override
1834 public void onStart() {
1835 super.onStart();
1836 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001837 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001838 target.updateBody();
1839 }
1840
1841 @Override
1842 public void onStop() {
1843 super.onStop();
1844 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001845 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001846 target.updateBody();
1847 }
1848 }
1849
1850 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001851 * Dialog to request user confirmation before setting
1852 * {@link NetworkPolicy#limitBytes}.
1853 */
1854 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001855 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001856 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001857
1858 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001859 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001860
Jeff Sharkeyd0a55312014-08-08 10:25:24 -07001861 final NetworkPolicy policy = parent.mPolicyEditor.getPolicy(parent.mTemplate);
1862 if (policy == null) return;
1863
Jeff Sharkey461842a2011-09-25 18:22:48 -07001864 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001865 final CharSequence message;
Jeff Sharkeyd0a55312014-08-08 10:25:24 -07001866 final long minLimitBytes = (long) (policy.warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001867 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001868
1869 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001870 final String currentTab = parent.mCurrentTab;
1871 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001872 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001873 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001874 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001875 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001876 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001877 } else if (isMobileTab(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001878 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001879 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001880 } else {
1881 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001882 }
1883
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001884 final Bundle args = new Bundle();
1885 args.putCharSequence(EXTRA_MESSAGE, message);
1886 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1887
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001888 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1889 dialog.setArguments(args);
1890 dialog.setTargetFragment(parent, 0);
1891 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1892 }
1893
1894 @Override
1895 public Dialog onCreateDialog(Bundle savedInstanceState) {
1896 final Context context = getActivity();
1897
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001898 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001899 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1900
1901 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1902 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001903 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001904
1905 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001906 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001907 public void onClick(DialogInterface dialog, int which) {
1908 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1909 if (target != null) {
1910 target.setPolicyLimitBytes(limitBytes);
1911 }
1912 }
1913 });
1914
1915 return builder.create();
1916 }
1917 }
1918
1919 /**
1920 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1921 */
1922 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001923 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001924
1925 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001926 if (!parent.isAdded()) return;
1927
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001928 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001929 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001930
1931 final CycleEditorFragment dialog = new CycleEditorFragment();
1932 dialog.setArguments(args);
1933 dialog.setTargetFragment(parent, 0);
1934 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1935 }
1936
1937 @Override
1938 public Dialog onCreateDialog(Bundle savedInstanceState) {
1939 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001940 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1941 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001942
1943 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1944 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1945
1946 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1947 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1948
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001949 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1950 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001951
1952 cycleDayPicker.setMinValue(1);
1953 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001954 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001955 cycleDayPicker.setWrapSelectorWheel(true);
1956
1957 builder.setTitle(R.string.data_usage_cycle_editor_title);
1958 builder.setView(view);
1959
1960 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1961 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001962 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001963 public void onClick(DialogInterface dialog, int which) {
Jeff Sharkeyd277de92013-03-25 15:11:25 -07001964 // clear focus to finish pending text edits
1965 cycleDayPicker.clearFocus();
1966
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001967 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001968 final String cycleTimezone = new Time().timezone;
1969 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001970 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001971 }
1972 });
1973
1974 return builder.create();
1975 }
1976 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001977
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001978 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001979 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1980 */
1981 public static class WarningEditorFragment extends DialogFragment {
1982 private static final String EXTRA_TEMPLATE = "template";
1983
1984 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001985 if (!parent.isAdded()) return;
1986
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001987 final Bundle args = new Bundle();
1988 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1989
1990 final WarningEditorFragment dialog = new WarningEditorFragment();
1991 dialog.setArguments(args);
1992 dialog.setTargetFragment(parent, 0);
1993 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1994 }
1995
1996 @Override
1997 public Dialog onCreateDialog(Bundle savedInstanceState) {
1998 final Context context = getActivity();
1999 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2000 final NetworkPolicyEditor editor = target.mPolicyEditor;
2001
2002 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2003 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2004
2005 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
2006 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
2007
2008 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2009 final long warningBytes = editor.getPolicyWarningBytes(template);
2010 final long limitBytes = editor.getPolicyLimitBytes(template);
2011
2012 bytesPicker.setMinValue(0);
2013 if (limitBytes != LIMIT_DISABLED) {
2014 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
2015 } else {
2016 bytesPicker.setMaxValue(Integer.MAX_VALUE);
2017 }
2018 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
2019 bytesPicker.setWrapSelectorWheel(false);
2020
2021 builder.setTitle(R.string.data_usage_warning_editor_title);
2022 builder.setView(view);
2023
2024 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2025 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002026 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002027 public void onClick(DialogInterface dialog, int which) {
2028 // clear focus to finish pending text edits
2029 bytesPicker.clearFocus();
2030
2031 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
2032 editor.setPolicyWarningBytes(template, bytes);
2033 target.updatePolicy(false);
2034 }
2035 });
2036
2037 return builder.create();
2038 }
2039 }
2040
2041 /**
2042 * Dialog to edit {@link NetworkPolicy#limitBytes}.
2043 */
2044 public static class LimitEditorFragment extends DialogFragment {
2045 private static final String EXTRA_TEMPLATE = "template";
2046
2047 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002048 if (!parent.isAdded()) return;
2049
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002050 final Bundle args = new Bundle();
2051 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
2052
2053 final LimitEditorFragment dialog = new LimitEditorFragment();
2054 dialog.setArguments(args);
2055 dialog.setTargetFragment(parent, 0);
2056 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
2057 }
2058
2059 @Override
2060 public Dialog onCreateDialog(Bundle savedInstanceState) {
2061 final Context context = getActivity();
2062 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2063 final NetworkPolicyEditor editor = target.mPolicyEditor;
2064
2065 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2066 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2067
2068 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
2069 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
2070
2071 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2072 final long warningBytes = editor.getPolicyWarningBytes(template);
2073 final long limitBytes = editor.getPolicyLimitBytes(template);
2074
2075 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09002076 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002077 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
2078 } else {
2079 bytesPicker.setMinValue(0);
2080 }
2081 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
2082 bytesPicker.setWrapSelectorWheel(false);
2083
2084 builder.setTitle(R.string.data_usage_limit_editor_title);
2085 builder.setView(view);
2086
2087 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2088 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002089 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002090 public void onClick(DialogInterface dialog, int which) {
2091 // clear focus to finish pending text edits
2092 bytesPicker.clearFocus();
2093
2094 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
2095 editor.setPolicyLimitBytes(template, bytes);
2096 target.updatePolicy(false);
2097 }
2098 });
2099
2100 return builder.create();
2101 }
2102 }
2103 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07002104 * Dialog to request user confirmation before disabling data.
2105 */
2106 public static class ConfirmDataDisableFragment extends DialogFragment {
2107 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002108 if (!parent.isAdded()) return;
2109
Jeff Sharkey28130d92011-09-02 16:10:24 -07002110 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
2111 dialog.setTargetFragment(parent, 0);
2112 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
2113 }
2114
2115 @Override
2116 public Dialog onCreateDialog(Bundle savedInstanceState) {
2117 final Context context = getActivity();
2118
2119 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2120 builder.setMessage(R.string.data_usage_disable_mobile);
2121
2122 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002123 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07002124 public void onClick(DialogInterface dialog, int which) {
2125 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2126 if (target != null) {
2127 // TODO: extend to modify policy enabled flag.
2128 target.setMobileDataEnabled(false);
2129 }
2130 }
2131 });
2132 builder.setNegativeButton(android.R.string.cancel, null);
2133
2134 return builder.create();
2135 }
2136 }
2137
2138 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002139 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07002140 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002141 */
2142 public static class ConfirmRestrictFragment extends DialogFragment {
2143 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002144 if (!parent.isAdded()) return;
2145
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002146 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
2147 dialog.setTargetFragment(parent, 0);
2148 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
2149 }
2150
2151 @Override
2152 public Dialog onCreateDialog(Bundle savedInstanceState) {
2153 final Context context = getActivity();
2154
2155 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002156 builder.setTitle(R.string.data_usage_restrict_background_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07002157 if (Utils.hasMultipleUsers(context)) {
2158 builder.setMessage(R.string.data_usage_restrict_background_multiuser);
2159 } else {
2160 builder.setMessage(R.string.data_usage_restrict_background);
2161 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002162
2163 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002164 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002165 public void onClick(DialogInterface dialog, int which) {
2166 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2167 if (target != null) {
2168 target.setRestrictBackground(true);
2169 }
2170 }
2171 });
2172 builder.setNegativeButton(android.R.string.cancel, null);
2173
2174 return builder.create();
2175 }
2176 }
2177
2178 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002179 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
2180 * change has been denied, usually based on
2181 * {@link DataUsageSummary#hasLimitedNetworks()}.
2182 */
2183 public static class DeniedRestrictFragment extends DialogFragment {
2184 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002185 if (!parent.isAdded()) return;
2186
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002187 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
2188 dialog.setTargetFragment(parent, 0);
2189 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
2190 }
2191
2192 @Override
2193 public Dialog onCreateDialog(Bundle savedInstanceState) {
2194 final Context context = getActivity();
2195
2196 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2197 builder.setTitle(R.string.data_usage_app_restrict_background);
2198 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
2199 builder.setPositiveButton(android.R.string.ok, null);
2200
2201 return builder.create();
2202 }
2203 }
2204
2205 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002206 * Dialog to request user confirmation before setting
2207 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
2208 */
2209 public static class ConfirmAppRestrictFragment extends DialogFragment {
2210 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002211 if (!parent.isAdded()) return;
2212
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002213 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2214 dialog.setTargetFragment(parent, 0);
2215 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2216 }
2217
2218 @Override
2219 public Dialog onCreateDialog(Bundle savedInstanceState) {
2220 final Context context = getActivity();
2221
2222 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002223 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2224 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2225
2226 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002227 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002228 public void onClick(DialogInterface dialog, int which) {
2229 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2230 if (target != null) {
2231 target.setAppRestrictBackground(true);
2232 }
2233 }
2234 });
2235 builder.setNegativeButton(android.R.string.cancel, null);
2236
2237 return builder.create();
2238 }
2239 }
2240
2241 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002242 * Compute default tab that should be selected, based on
2243 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2244 */
2245 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002246 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002247 if (template == null) {
Wink Savilleb003a852014-10-23 10:16:26 -07002248 final int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
PauloftheWest50e6eca2014-10-03 11:07:14 -07002249 SubscriptionManager.INVALID_SUB_ID);
2250 if (SubscriptionManager.isValidSubId(subId)) {
2251 return TAB_MOBILE + String.valueOf(subId);
2252 }
2253 return null;
2254 }
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002255
2256 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002257 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002258 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002259 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002260 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002261 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002262 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002263 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002264 return TAB_WIFI;
2265 default:
2266 return null;
2267 }
2268 }
2269
2270 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002271 * Background task that loads {@link UidDetail}, binding to
2272 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002273 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002274 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2275 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002276 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002277 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002278
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002279 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002280 mProvider = checkNotNull(provider);
2281 mItem = checkNotNull(item);
2282 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002283 }
2284
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002285 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002286 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002287 final UidDetailTask existing = (UidDetailTask) target.getTag();
2288 if (existing != null) {
2289 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002290 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002291
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002292 final UidDetail cachedDetail = provider.getUidDetail(item.key, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002293 if (cachedDetail != null) {
2294 bindView(cachedDetail, target);
2295 } else {
2296 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2297 AsyncTask.THREAD_POOL_EXECUTOR));
2298 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002299 }
2300
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002301 private static void bindView(UidDetail detail, View target) {
2302 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2303 final TextView title = (TextView) target.findViewById(android.R.id.title);
2304
2305 if (detail != null) {
2306 icon.setImageDrawable(detail.icon);
2307 title.setText(detail.label);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +01002308 title.setContentDescription(detail.contentDescription);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002309 } else {
2310 icon.setImageDrawable(null);
2311 title.setText(null);
2312 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002313 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002314
2315 @Override
2316 protected void onPreExecute() {
2317 bindView(null, mTarget);
2318 }
2319
2320 @Override
2321 protected UidDetail doInBackground(Void... params) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002322 return mProvider.getUidDetail(mItem.key, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002323 }
2324
2325 @Override
2326 protected void onPostExecute(UidDetail result) {
2327 bindView(result, mTarget);
2328 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002329 }
2330
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002331 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002332 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002333 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002334 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002335 if (TEST_RADIOS) {
2336 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2337 }
2338
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002339 final ConnectivityManager conn = ConnectivityManager.from(context);
2340 final TelephonyManager tele = TelephonyManager.from(context);
2341
PauloftheWest50e6eca2014-10-03 11:07:14 -07002342 final List<SubInfoRecord> subInfoList = SubscriptionManager.getActiveSubInfoList();
2343 // No activated Subscription
2344 if (subInfoList == null) {
2345 return false;
2346 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002347 // require both supported network and ready SIM
PauloftheWest50e6eca2014-10-03 11:07:14 -07002348 boolean isReady = true;
2349 for (SubInfoRecord subInfo : subInfoList) {
2350 isReady = isReady & tele.getSimState(subInfo.slotId) == SIM_STATE_READY;
2351 }
2352 return conn.isNetworkSupported(TYPE_MOBILE) && isReady;
2353 }
2354
2355 /*
2356 * TODO: consider adding to TelephonyManager or SubscritpionManager.
2357 */
Wink Savilleb003a852014-10-23 10:16:26 -07002358 public static boolean hasReadyMobileRadio(Context context, int subId) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07002359 if (TEST_RADIOS) {
2360 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2361 }
2362
2363 final ConnectivityManager conn = ConnectivityManager.from(context);
2364 final TelephonyManager tele = TelephonyManager.from(context);
2365 final int slotId = SubscriptionManager.getSlotId(subId);
2366 final boolean isReady = tele.getSimState(slotId) == SIM_STATE_READY;
2367
2368 return conn.isNetworkSupported(TYPE_MOBILE) && isReady;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002369 }
2370
2371 /**
2372 * Test if device has a mobile 4G data radio.
2373 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002374 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002375 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2376 return false;
2377 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002378 if (TEST_RADIOS) {
2379 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2380 }
2381
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002382 final ConnectivityManager conn = ConnectivityManager.from(context);
2383 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002384
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002385 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Wink Saville55434042012-06-14 12:33:43 -07002386 final boolean hasLte = (tele.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE)
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002387 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002388 return hasWimax || hasLte;
2389 }
2390
2391 /**
2392 * Test if device has a Wi-Fi data radio.
2393 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002394 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002395 if (TEST_RADIOS) {
2396 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2397 }
2398
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002399 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002400 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002401 }
2402
2403 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002404 * Test if device has an ethernet network connection.
2405 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002406 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002407 if (TEST_RADIOS) {
2408 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2409 }
2410
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002411 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002412 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2413
2414 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002415 if (mStatsSession != null) {
2416 try {
2417 ethernetBytes = mStatsSession.getSummaryForNetwork(
2418 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2419 .getTotalBytes();
2420 } catch (RemoteException e) {
2421 throw new RuntimeException(e);
2422 }
2423 } else {
2424 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002425 }
2426
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002427 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002428 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002429 }
2430
2431 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002432 * Inflate a {@link Preference} style layout, adding the given {@link View}
2433 * widget into {@link android.R.id#widget_frame}.
2434 */
2435 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2436 final View view = inflater.inflate(R.layout.preference, root, false);
2437 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2438 android.R.id.widget_frame);
2439 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2440 return view;
2441 }
2442
Jeff Sharkeyb6548462014-07-21 15:38:06 -07002443 private static View inflateCategoryHeader(LayoutInflater inflater, ViewGroup root) {
2444 final TypedArray a = inflater.getContext().obtainStyledAttributes(null,
2445 com.android.internal.R.styleable.Preference,
2446 com.android.internal.R.attr.preferenceCategoryStyle, 0);
2447 final int resId = a.getResourceId(com.android.internal.R.styleable.Preference_layout, 0);
2448 return inflater.inflate(resId, root, false);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002449 }
2450
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002451 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002452 * Test if any networks are currently limited.
2453 */
2454 private boolean hasLimitedNetworks() {
2455 return !buildLimitedNetworksList().isEmpty();
2456 }
2457
2458 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002459 * Build string describing currently limited networks, which defines when
2460 * background data is restricted.
2461 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002462 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002463 private CharSequence buildLimitedNetworksString() {
2464 final List<CharSequence> limited = buildLimitedNetworksList();
2465
2466 // handle case where no networks limited
2467 if (limited.isEmpty()) {
2468 limited.add(getText(R.string.data_usage_list_none));
2469 }
2470
2471 return TextUtils.join(limited);
2472 }
2473
2474 /**
2475 * Build list of currently limited networks, which defines when background
2476 * data is restricted.
2477 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002478 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002479 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002480 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002481
2482 // build combined list of all limited networks
2483 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002484
2485 final TelephonyManager tele = TelephonyManager.from(context);
2486 if (tele.getSimState() == SIM_STATE_READY) {
2487 final String subscriberId = getActiveSubscriberId(context);
2488 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2489 limited.add(getText(R.string.data_usage_list_mobile));
2490 }
2491 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2492 limited.add(getText(R.string.data_usage_tab_3g));
2493 }
2494 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2495 limited.add(getText(R.string.data_usage_tab_4g));
2496 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002497 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002498
2499 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002500 limited.add(getText(R.string.data_usage_tab_wifi));
2501 }
2502 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2503 limited.add(getText(R.string.data_usage_tab_ethernet));
2504 }
2505
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002506 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002507 }
2508
2509 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002510 * Inset both selector and divider {@link Drawable} on the given
2511 * {@link ListView} by the requested dimensions.
2512 */
2513 private static void insetListViewDrawables(ListView view, int insetSide) {
2514 final Drawable selector = view.getSelector();
2515 final Drawable divider = view.getDivider();
2516
2517 // fully unregister these drawables so callbacks can be maintained after
2518 // wrapping below.
2519 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2520 view.setSelector(stub);
2521 view.setDivider(stub);
2522
2523 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2524 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2525 }
2526
2527 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002528 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002529 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002530 */
2531 private static void setPreferenceTitle(View parent, int resId) {
2532 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2533 title.setText(resId);
2534 }
2535
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002536 /**
2537 * Set {@link android.R.id#summary} for a preference view inflated with
2538 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2539 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002540 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002541 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2542 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002543 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002544 }
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002545
2546 /**
2547 * For search
2548 */
2549 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -07002550 new BaseSearchIndexProvider() {
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002551 @Override
2552 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
2553 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
2554
2555 final Resources res = context.getResources();
2556
2557 // Add fragment title
2558 SearchIndexableRaw data = new SearchIndexableRaw(context);
2559 data.title = res.getString(R.string.data_usage_summary_title);
2560 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2561 result.add(data);
2562
2563 // Mobile data
2564 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002565 data.key = DATA_USAGE_ENABLE_MOBILE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002566 data.title = res.getString(R.string.data_usage_enable_mobile);
2567 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2568 result.add(data);
2569
2570 // Set mobile data limit
2571 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002572 data.key = DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002573 data.title = res.getString(R.string.data_usage_disable_mobile_limit);
2574 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2575 result.add(data);
2576
Fabrice Di Megliof2a52262014-04-17 17:20:27 -07002577 // Data usage cycle
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002578 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002579 data.key = DATA_USAGE_CYCLE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002580 data.title = res.getString(R.string.data_usage_cycle);
2581 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2582 result.add(data);
2583
2584 return result;
2585 }
2586 };
2587
PauloftheWest50e6eca2014-10-03 11:07:14 -07002588 private void addMobileTab(Context context, List<SubInfoRecord> subInfoList) {
2589 if (subInfoList != null) {
2590 for (SubInfoRecord subInfo : mSubInfoList) {
PauloftheWestde338442014-10-29 14:46:44 -07002591 if (hasReadyMobileRadio(context, subInfo.subId)) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07002592 mTabHost.addTab(buildTabSpec(mMobileTagMap.get(subInfo.subId),
2593 subInfo.displayName));
2594 }
2595 }
2596 }
2597 }
2598
PauloftheWestde338442014-10-29 14:46:44 -07002599 private SubInfoRecord getCurrentTabSubInfo(Context context) {
2600 if (mSubInfoList != null && mTabHost != null) {
2601 final int currentTagIndex = mTabHost.getCurrentTab();
2602 int i = 0;
2603 for (SubInfoRecord subInfo : mSubInfoList) {
2604 if (hasReadyMobileRadio(context, subInfo.subId)) {
2605 if (i++ == currentTagIndex) {
2606 return subInfo;
2607 }
2608 }
2609 }
2610 }
2611 return null;
2612 }
2613
PauloftheWest50e6eca2014-10-03 11:07:14 -07002614 /**
2615 * Init a map with subId key and mobile tag name
2616 * @param subInfoList The subscription Info List
2617 * @return The map or null if no activated subscription
2618 */
Wink Savilleb003a852014-10-23 10:16:26 -07002619 private Map<Integer, String> initMobileTabTag(List<SubInfoRecord> subInfoList) {
2620 Map<Integer, String> map = null;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002621 if (subInfoList != null) {
2622 String mobileTag;
Wink Savilleb003a852014-10-23 10:16:26 -07002623 map = new HashMap<Integer, String>();
PauloftheWest50e6eca2014-10-03 11:07:14 -07002624 for (SubInfoRecord subInfo : subInfoList) {
2625 mobileTag = TAB_MOBILE + String.valueOf(subInfo.subId);
2626 map.put(subInfo.subId,mobileTag);
2627 }
2628 }
2629 return map;
2630 }
2631
2632 private static boolean isMobileTab(String currentTab) {
2633 return currentTab != null ? currentTab.contains(TAB_MOBILE) : false;
2634 }
2635
Wink Savilleb003a852014-10-23 10:16:26 -07002636 private int getSubId(String currentTab) {
2637 Set<Integer> set = mMobileTagMap.keySet();
2638 for (Integer subId : set) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07002639 if (mMobileTagMap.get(subId).equals(currentTab)) {
2640 return subId;
2641 }
2642 }
2643 Log.e(TAG, "currentTab = " + currentTab + " non mobile tab called this function");
2644 return -1;
2645 }
2646
2647 //SUB SELECT
PauloftheWest98523c82014-10-28 08:30:05 -07002648 private boolean isMobileDataAvailable(long subId) {
Wink Savilleb003a852014-10-23 10:16:26 -07002649 int[] subIds = SubscriptionManager.getSubId(PhoneConstants.SUB1);
PauloftheWest98523c82014-10-28 08:30:05 -07002650 if (subIds != null && subIds[0] == subId) {
2651 return true;
2652 }
2653
2654 subIds = SubscriptionManager.getSubId(PhoneConstants.SUB2);
2655 if (subIds != null && subIds[0] == subId) {
2656 return true;
2657 }
2658
2659 subIds = SubscriptionManager.getSubId(PhoneConstants.SUB3);
2660 if (subIds != null && subIds[0] == subId) {
2661 return true;
2662 }
2663 return false;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002664 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002665}