blob: 27a486cc0d1be7b2c9feea533ff05529ddc99f22 [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;
Jason Monk4bb075b2015-03-17 10:06:58 -040065import android.content.pm.PackageManager.NameNotFoundException;
Zoltan Szatmary-Ban77c1d362014-12-12 18:48:03 +000066import android.content.pm.UserInfo;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070067import android.content.res.Resources;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070068import android.graphics.Color;
Jeff Sharkey5d706792011-09-08 18:57:17 -070069import android.graphics.drawable.ColorDrawable;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070070import android.graphics.drawable.Drawable;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070071import android.net.ConnectivityManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070072import android.net.INetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070073import android.net.INetworkStatsService;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070074import android.net.INetworkStatsSession;
Jeff Sharkey8a503642011-06-10 13:31:21 -070075import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070076import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070077import android.net.NetworkStats;
78import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070079import android.net.NetworkTemplate;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070080import android.net.TrafficStats;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070081import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070082import android.os.Bundle;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070083import android.os.INetworkManagementService;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070084import android.os.Parcel;
85import android.os.Parcelable;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070086import android.os.RemoteException;
87import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070088import android.os.SystemProperties;
Dianne Hackbornbb06a422012-08-16 11:01:57 -070089import android.os.UserHandle;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +010090import android.os.UserManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070091import android.preference.Preference;
Wink Savilleca756612014-11-08 10:47:12 -080092import android.telephony.SubscriptionInfo;
PauloftheWest50e6eca2014-10-03 11:07:14 -070093import android.telephony.SubscriptionManager;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070094import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070095import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070096import android.text.format.DateUtils;
97import android.text.format.Formatter;
Jeff Sharkeye5223a02012-03-09 17:11:14 -080098import android.text.format.Time;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070099import android.util.Log;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700100import android.util.SparseArray;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700101import android.util.SparseBooleanArray;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700102import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700103import android.view.Menu;
104import android.view.MenuInflater;
105import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700106import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700107import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700108import android.view.ViewGroup;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700109import android.widget.AdapterView;
110import android.widget.AdapterView.OnItemClickListener;
111import android.widget.AdapterView.OnItemSelectedListener;
112import android.widget.ArrayAdapter;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700113import android.widget.BaseAdapter;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700114import android.widget.Button;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700115import android.widget.ImageView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700116import android.widget.LinearLayout;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700117import android.widget.ListView;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700118import android.widget.NumberPicker;
Jeff Sharkey2412b0f2011-07-17 20:31:40 -0700119import android.widget.ProgressBar;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700120import android.widget.Spinner;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700121import android.widget.Switch;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700122import android.widget.TabHost;
Jason Monk4bb075b2015-03-17 10:06:58 -0400123import android.widget.Toast;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700124import android.widget.TabHost.OnTabChangeListener;
125import android.widget.TabHost.TabContentFactory;
126import android.widget.TabHost.TabSpec;
127import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700128import android.widget.TextView;
129
Chris Wren8a963ba2015-03-20 10:29:14 -0400130import com.android.internal.logging.MetricsLogger;
Wink Saville55434042012-06-14 12:33:43 -0700131import com.android.internal.telephony.PhoneConstants;
Jeff Sharkey5d706792011-09-08 18:57:17 -0700132import com.android.settings.drawable.InsetBoundsDrawable;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700133import com.android.settings.net.ChartData;
134import com.android.settings.net.ChartDataLoader;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700135import com.android.settings.net.DataUsageMeteredSettings;
Jeff Sharkeya662e492011-06-18 21:57:06 -0700136import com.android.settings.net.NetworkPolicyEditor;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700137import com.android.settings.net.SummaryForAllUidLoader;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700138import com.android.settings.net.UidDetail;
139import com.android.settings.net.UidDetailProvider;
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -0700140import com.android.settings.search.BaseSearchIndexProvider;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -0700141import com.android.settings.search.Indexable;
142import com.android.settings.search.SearchIndexableRaw;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700143import com.android.settings.widget.ChartDataUsageView;
144import com.android.settings.widget.ChartDataUsageView.DataUsageChartListener;
PauloftheWestc80b7612014-11-25 04:40:45 -0800145import com.android.settings.widget.ChartNetworkSeriesView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700146import com.google.android.collect.Lists;
147
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700148import libcore.util.Objects;
149
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700150import java.util.ArrayList;
151import java.util.Collections;
PauloftheWest50e6eca2014-10-03 11:07:14 -0700152import java.util.HashMap;
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700153import java.util.List;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700154import java.util.Locale;
PauloftheWest50e6eca2014-10-03 11:07:14 -0700155import java.util.Map;
156import java.util.Set;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700157
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700158/**
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700159 * Panel showing data usage history across various networks, including options
160 * to inspect based on usage cycle and control through {@link NetworkPolicy}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700161 */
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700162public class DataUsageSummary extends HighlightingFragment implements Indexable {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700163 private static final String TAG = "DataUsage";
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800164 private static final boolean LOGD = false;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700165
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700166 // TODO: remove this testing code
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700167 private static final boolean TEST_ANIM = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700168 private static final boolean TEST_RADIOS = false;
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800169
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700170 private static final String TEST_RADIOS_PROP = "test.radios";
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800171 private static final String TEST_SUBSCRIBER_PROP = "test.subscriberid";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700172
Jeff Sharkey8a503642011-06-10 13:31:21 -0700173 private static final String TAB_3G = "3g";
174 private static final String TAB_4G = "4g";
175 private static final String TAB_MOBILE = "mobile";
176 private static final String TAB_WIFI = "wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700177 private static final String TAB_ETHERNET = "ethernet";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700178
Jeff Sharkey28130d92011-09-02 16:10:24 -0700179 private static final String TAG_CONFIRM_DATA_DISABLE = "confirmDataDisable";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700180 private static final String TAG_CONFIRM_LIMIT = "confirmLimit";
181 private static final String TAG_CYCLE_EDITOR = "cycleEditor";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700182 private static final String TAG_WARNING_EDITOR = "warningEditor";
183 private static final String TAG_LIMIT_EDITOR = "limitEditor";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700184 private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700185 private static final String TAG_DENIED_RESTRICT = "deniedRestrict";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700186 private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700187 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700188
Fabrice Di Meglio2169c882014-04-18 15:18:40 -0700189 private static final String DATA_USAGE_ENABLE_MOBILE_KEY = "data_usage_enable_mobile";
190 private static final String DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY =
191 "data_usage_disable_mobile_limit";
192 private static final String DATA_USAGE_CYCLE_KEY = "data_usage_cycle";
193
Jason Monk4bb075b2015-03-17 10:06:58 -0400194 public static final String EXTRA_SHOW_APP_IMMEDIATE_PKG = "showAppImmediatePkg";
195
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700196 private static final int LOADER_CHART_DATA = 2;
197 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700198
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700199 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700200 private INetworkStatsService mStatsService;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700201 private NetworkPolicyManager mPolicyManager;
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700202 private TelephonyManager mTelephonyManager;
Wink Saville0183fb52014-11-22 10:11:39 -0800203 private SubscriptionManager mSubscriptionManager;
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700204
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700205 private INetworkStatsSession mStatsSession;
206
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700207 private static final String PREF_FILE = "data_usage";
208 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700209 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700210
211 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700212
Jeff Sharkey8a503642011-06-10 13:31:21 -0700213 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700214 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700215 private TabWidget mTabWidget;
216 private ListView mListView;
PauloftheWestc80b7612014-11-25 04:40:45 -0800217 private ChartNetworkSeriesView mSeries;
218 private ChartNetworkSeriesView mDetailedSeries;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700219 private DataUsageAdapter mAdapter;
220
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700221 /** Distance to inset content from sides, when needed. */
222 private int mInsetSide = 0;
223
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700224 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700225
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700226 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700227 private LinearLayout mNetworkSwitches;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700228 private boolean mDataEnabledSupported;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700229 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700230 private View mDataEnabledView;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700231 private boolean mDisableAtLimitSupported;
232 private Switch mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700233 private View mDisableAtLimitView;
234
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700235 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700236 private Spinner mCycleSpinner;
237 private CycleAdapter mCycleAdapter;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700238 private TextView mCycleSummary;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700239
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700240 private ChartDataUsageView mChart;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700241 private View mDisclaimer;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700242 private TextView mEmpty;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700243 private View mStupidPadding;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700244
245 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700246 private ImageView mAppIcon;
247 private ViewGroup mAppTitles;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700248 private TextView mAppTotal;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700249 private TextView mAppForeground;
250 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700251 private Button mAppSettings;
252
253 private LinearLayout mAppSwitches;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700254 private Switch mAppRestrict;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700255 private View mAppRestrictView;
256
Jeff Sharkey8a503642011-06-10 13:31:21 -0700257 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700258 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700259
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700260 private NetworkTemplate mTemplate;
261 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700262
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700263 private AppItem mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700264
265 private Intent mAppSettingsIntent;
266
Jeff Sharkeya662e492011-06-18 21:57:06 -0700267 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700268
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700269 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700270 private String mIntentTab = null;
271
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700272 private MenuItem mMenuRestrictBackground;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700273 private MenuItem mMenuShowWifi;
274 private MenuItem mMenuShowEthernet;
275 private MenuItem mMenuSimCards;
276 private MenuItem mMenuCellularNetworks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700277
Wink Savilleca756612014-11-08 10:47:12 -0800278 private List<SubscriptionInfo> mSubInfoList;
Wink Savilleb003a852014-10-23 10:16:26 -0700279 private Map<Integer,String> mMobileTagMap;
PauloftheWest50e6eca2014-10-03 11:07:14 -0700280
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700281 /** Flag used to ignore listeners during binding. */
282 private boolean mBinding;
283
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700284 private UidDetailProvider mUidDetailProvider;
285
Jason Monk4bb075b2015-03-17 10:06:58 -0400286 // Indicates request to show app immediately rather than list.
287 private String mShowAppImmediatePkg;
288
Sanket Padawed819270f2015-01-14 11:03:33 -0800289 /**
290 * Local cache of data enabled for subId, used to work around delays.
291 */
292 private final Map<String, Boolean> mMobileDataEnabled = new HashMap<String, Boolean>();
293
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700294 @Override
Chris Wren8a963ba2015-03-20 10:29:14 -0400295 protected int getMetricsCategory() {
296 return MetricsLogger.DATA_USAGE_SUMMARY;
297 }
298
299 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700300 public void onCreate(Bundle savedInstanceState) {
301 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700302 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700303
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700304 mNetworkService = INetworkManagementService.Stub.asInterface(
305 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700306 mStatsService = INetworkStatsService.Stub.asInterface(
307 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700308 mPolicyManager = NetworkPolicyManager.from(context);
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700309 mTelephonyManager = TelephonyManager.from(context);
Wink Saville0183fb52014-11-22 10:11:39 -0800310 mSubscriptionManager = SubscriptionManager.from(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700311
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700312 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700313
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700314 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700315 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700316
Wink Saville0183fb52014-11-22 10:11:39 -0800317 mSubInfoList = mSubscriptionManager.getActiveSubscriptionInfoList();
PauloftheWest50e6eca2014-10-03 11:07:14 -0700318 mMobileTagMap = initMobileTabTag(mSubInfoList);
319
Jaewan Kimffce9c12013-03-19 16:53:45 +0900320 try {
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700321 if (!mNetworkService.isBandwidthControlEnabled()) {
322 Log.w(TAG, "No bandwidth control; leaving");
323 getActivity().finish();
324 }
325 } catch (RemoteException e) {
326 Log.w(TAG, "No bandwidth control; leaving");
327 getActivity().finish();
328 }
329
330 try {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900331 mStatsSession = mStatsService.openSession();
332 } catch (RemoteException e) {
333 throw new RuntimeException(e);
334 }
335
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700336 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700337 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700338
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700339 // override preferences when no mobile radio
340 if (!hasReadyMobileRadio(context)) {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900341 mShowWifi = true;
342 mShowEthernet = true;
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700343 }
344
Jason Monk4bb075b2015-03-17 10:06:58 -0400345 mUidDetailProvider = new UidDetailProvider(context);
346
347 Bundle arguments = getArguments();
348 if (arguments != null) {
349 mShowAppImmediatePkg = arguments.getString(EXTRA_SHOW_APP_IMMEDIATE_PKG);
350 }
351
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700352 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700353 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700354
Jeff Sharkey8a503642011-06-10 13:31:21 -0700355 @Override
356 public View onCreateView(LayoutInflater inflater, ViewGroup container,
357 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700358
Jeff Sharkey8a503642011-06-10 13:31:21 -0700359 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700360 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
361
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700362
Jeff Sharkey8a503642011-06-10 13:31:21 -0700363 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700364 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700365 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
366 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700367
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700368 // decide if we need to manually inset our content, or if we should rely
369 // on parent container for inset.
370 final boolean shouldInset = mListView.getScrollBarStyle()
371 == View.SCROLLBARS_OUTSIDE_OVERLAY;
Amith Yamasani56f51a82013-08-05 10:07:23 -0700372 mInsetSide = 0;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700373
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700374 // adjust padding around tabwidget as needed
Amith Yamasani56f51a82013-08-05 10:07:23 -0700375 prepareCustomPreferencesList(container, view, mListView, false);
Jeff Sharkey5d706792011-09-08 18:57:17 -0700376
Jeff Sharkey8a503642011-06-10 13:31:21 -0700377 mTabHost.setup();
378 mTabHost.setOnTabChangedListener(mTabListener);
379
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700380 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700381 mHeader.setClickable(true);
382
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700383 mListView.addHeaderView(new View(context), null, true);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700384 mListView.addHeaderView(mHeader, null, true);
385 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700386
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700387 if (mInsetSide > 0) {
388 // inset selector and divider drawables
389 insetListViewDrawables(mListView, mInsetSide);
Fabrice Di Megliob27223f2013-01-15 18:54:11 -0800390 mHeader.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700391 }
392
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700393 {
394 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700395 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
396 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700397 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700398
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700399 mDataEnabled = new Switch(inflater.getContext());
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700400 mDataEnabled.setClickable(false);
401 mDataEnabled.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700402 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700403 mDataEnabledView.setTag(R.id.preference_highlight_key,
404 DATA_USAGE_ENABLE_MOBILE_KEY);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700405 mDataEnabledView.setClickable(true);
406 mDataEnabledView.setFocusable(true);
407 mDataEnabledView.setOnClickListener(mDataEnabledListener);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700408 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700409
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700410 mDisableAtLimit = new Switch(inflater.getContext());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700411 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700412 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700413 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700414 mDisableAtLimitView.setTag(R.id.preference_highlight_key,
415 DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700416 mDisableAtLimitView.setClickable(true);
417 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700418 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
419 mNetworkSwitches.addView(mDisableAtLimitView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700420
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700421 mCycleView = inflater.inflate(R.layout.data_usage_cycles, mNetworkSwitches, false);
422 mCycleView.setTag(R.id.preference_highlight_key, DATA_USAGE_CYCLE_KEY);
423 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
424 mCycleAdapter = new CycleAdapter(context);
425 mCycleSpinner.setAdapter(mCycleAdapter);
426 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
427 mCycleSummary = (TextView) mCycleView.findViewById(R.id.cycle_summary);
428 mNetworkSwitches.addView(mCycleView);
PauloftheWestc80b7612014-11-25 04:40:45 -0800429 mSeries = (ChartNetworkSeriesView)view.findViewById(R.id.series);
430 mDetailedSeries = (ChartNetworkSeriesView)view.findViewById(R.id.detail_series);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700431 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700432
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700433 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700434 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800435 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700436
437 {
438 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700439 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700440 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
441 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700442 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
443 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700444 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700445
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700446 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700447
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700448 mAppRestrict = new Switch(inflater.getContext());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700449 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700450 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700451 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700452 mAppRestrictView.setClickable(true);
453 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700454 mAppRestrictView.setOnClickListener(mAppRestrictListener);
455 mAppSwitches.addView(mAppRestrictView);
456 }
457
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700458 mDisclaimer = mHeader.findViewById(R.id.disclaimer);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700459 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700460 mStupidPadding = mHeader.findViewById(R.id.stupid_padding);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700461
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +0100462 final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
463 mAdapter = new DataUsageAdapter(um, mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700464 mListView.setOnItemClickListener(mListListener);
465 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700466
Jason Monk4bb075b2015-03-17 10:06:58 -0400467 showRequestedAppIfNeeded();
468
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700469 return view;
470 }
471
Jason Monk4bb075b2015-03-17 10:06:58 -0400472 private void showRequestedAppIfNeeded() {
473 if (mShowAppImmediatePkg == null) {
474 return;
475 }
476 try {
477 int uid = getActivity().getPackageManager().getPackageUid(mShowAppImmediatePkg,
478 UserHandle.myUserId());
479 AppItem app = new AppItem(uid);
480 app.addUid(uid);
481
482 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
483 // When we are going straight to an app then we are coming from App Info and want
484 // a header at the top.
485 AppHeader.createAppHeader(getActivity(), detail.icon, detail.label, null);
486 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label, false);
487 } catch (NameNotFoundException e) {
488 Log.w(TAG, "Could not find " + mShowAppImmediatePkg, e);
489 Toast.makeText(getActivity(), getString(R.string.unknown_app), Toast.LENGTH_LONG)
490 .show();
491 getActivity().finish();
492 }
493 }
494
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700495 @Override
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700496 public void onViewStateRestored(Bundle savedInstanceState) {
497 super.onViewStateRestored(savedInstanceState);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700498
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700499 // pick default tab based on incoming intent
500 final Intent intent = getActivity().getIntent();
501 mIntentTab = computeTabFromIntent(intent);
502
Jeff Sharkey8a503642011-06-10 13:31:21 -0700503 // this kicks off chain reaction which creates tabs, binds the body to
504 // selected network, and binds chart, cycles and detail list.
505 updateTabs();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700506 }
507
508 @Override
509 public void onResume() {
510 super.onResume();
511
512 getView().post(new Runnable() {
513 @Override
514 public void run() {
515 highlightViewIfNeeded();
516 }
517 });
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700518
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700519 // kick off background task to update stats
520 new AsyncTask<Void, Void, Void>() {
521 @Override
522 protected Void doInBackground(Void... params) {
523 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700524 // wait a few seconds before kicking off
525 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700526 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700527 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700528 } catch (RemoteException e) {
529 }
530 return null;
531 }
532
533 @Override
534 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700535 if (isAdded()) {
536 updateBody();
537 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700538 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700539 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700540 }
541
Jeff Sharkey8a503642011-06-10 13:31:21 -0700542 @Override
543 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
544 inflater.inflate(R.menu.data_usage, menu);
545 }
546
547 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700548 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700549 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700550 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700551 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700552
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700553 mMenuShowWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700554 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
555 mMenuShowWifi.setVisible(!appDetailMode);
556 } else {
557 mMenuShowWifi.setVisible(false);
558 }
559
560 mMenuShowEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
561 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
562 mMenuShowEthernet.setVisible(!appDetailMode);
563 } else {
564 mMenuShowEthernet.setVisible(false);
565 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700566
567 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700568 mMenuRestrictBackground.setVisible(
569 hasReadyMobileRadio(context) && isOwner && !appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700570
571 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
572 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700573 metered.setVisible(!appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700574 } else {
575 metered.setVisible(false);
576 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700577
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700578 // TODO: show when multiple sims available
579 mMenuSimCards = menu.findItem(R.id.data_usage_menu_sim_cards);
580 mMenuSimCards.setVisible(false);
581
582 mMenuCellularNetworks = menu.findItem(R.id.data_usage_menu_cellular_networks);
PauloftheWesta4b8fb32014-09-18 10:12:25 -0700583 mMenuCellularNetworks.setVisible(hasReadyMobileRadio(context)
584 && !appDetailMode && isOwner);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700585
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700586 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
587 String helpUrl;
588 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800589 HelpUtils.prepareHelpMenuItem(context, help, helpUrl);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700590 } else {
591 help.setVisible(false);
592 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700593
594 updateMenuTitles();
595 }
596
597 private void updateMenuTitles() {
598 if (mPolicyManager.getRestrictBackground()) {
599 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_allow_background);
600 } else {
601 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_restrict_background);
602 }
603
604 if (mShowWifi) {
605 mMenuShowWifi.setTitle(R.string.data_usage_menu_hide_wifi);
606 } else {
607 mMenuShowWifi.setTitle(R.string.data_usage_menu_show_wifi);
608 }
609
610 if (mShowEthernet) {
611 mMenuShowEthernet.setTitle(R.string.data_usage_menu_hide_ethernet);
612 } else {
613 mMenuShowEthernet.setTitle(R.string.data_usage_menu_show_ethernet);
614 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700615 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700616
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700617 @Override
618 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700619 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700620 case R.id.data_usage_menu_restrict_background: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700621 final boolean restrictBackground = !mPolicyManager.getRestrictBackground();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700622 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800623 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700624 } else {
625 // no confirmation to drop restriction
626 setRestrictBackground(false);
627 }
628 return true;
629 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700630 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700631 mShowWifi = !mShowWifi;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700632 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700633 updateMenuTitles();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700634 updateTabs();
635 return true;
636 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700637 case R.id.data_usage_menu_show_ethernet: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700638 mShowEthernet = !mShowEthernet;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700639 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700640 updateMenuTitles();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700641 updateTabs();
642 return true;
643 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700644 case R.id.data_usage_menu_sim_cards: {
645 // TODO: hook up to sim cards
646 return true;
647 }
648 case R.id.data_usage_menu_cellular_networks: {
649 final Intent intent = new Intent(Intent.ACTION_MAIN);
650 intent.setComponent(new ComponentName("com.android.phone",
651 "com.android.phone.MobileNetworkSettings"));
652 startActivity(intent);
653 return true;
654 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700655 case R.id.data_usage_menu_metered: {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800656 final SettingsActivity sa = (SettingsActivity) getActivity();
657 sa.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700658 R.string.data_usage_metered_title, null, this, 0);
659 return true;
660 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700661 }
662 return false;
663 }
664
Jeff Sharkey94a90952011-06-13 22:31:09 -0700665 @Override
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700666 public void onDestroy() {
Jeff Sharkey94a90952011-06-13 22:31:09 -0700667 mDataEnabledView = null;
668 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700669
670 mUidDetailProvider.clearCache();
671 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700672
673 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700674
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800675 super.onDestroy();
676 }
677
Jeff Sharkey8a503642011-06-10 13:31:21 -0700678 /**
Jeff Sharkey92811822012-05-04 11:47:29 -0700679 * Build and assign {@link LayoutTransition} to various containers. Should
680 * only be assigned after initial layout is complete.
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700681 */
Jeff Sharkey92811822012-05-04 11:47:29 -0700682 private void ensureLayoutTransitions() {
Jason Monk4bb075b2015-03-17 10:06:58 -0400683 if (mShowAppImmediatePkg != null) {
684 // If we are skipping right to showing an app, we don't care about transitions.
685 return;
686 }
Jeff Sharkey92811822012-05-04 11:47:29 -0700687 // skip when already setup
688 if (mChart.getLayoutTransition() != null) return;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700689
Jeff Sharkey92811822012-05-04 11:47:29 -0700690 mTabsContainer.setLayoutTransition(buildLayoutTransition());
691 mHeader.setLayoutTransition(buildLayoutTransition());
692 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700693
Jeff Sharkey92811822012-05-04 11:47:29 -0700694 final LayoutTransition chartTransition = buildLayoutTransition();
695 chartTransition.disableTransitionType(LayoutTransition.APPEARING);
696 chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
697 mChart.setLayoutTransition(chartTransition);
698 }
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700699
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700700 private static LayoutTransition buildLayoutTransition() {
701 final LayoutTransition transition = new LayoutTransition();
702 if (TEST_ANIM) {
703 transition.setDuration(1500);
704 }
705 transition.setAnimateParentHierarchy(false);
706 return transition;
707 }
708
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700709 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700710 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700711 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
712 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700713 */
714 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700715 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700716 mTabHost.clearAllTabs();
717
Sanket Padawe7e21fa52015-02-12 12:39:34 -0800718 int simCount = mTelephonyManager.getSimCount();
719
Stuart Scottf51d0062015-04-08 09:40:48 -0700720 List<SubscriptionInfo> sirs = mSubscriptionManager.getActiveSubscriptionInfoList();
721 if (sirs != null) {
722 for (SubscriptionInfo sir : sirs) {
Sanket Padawe7e21fa52015-02-12 12:39:34 -0800723 addMobileTab(context, sir, (simCount > 1));
PauloftheWestc80b7612014-11-25 04:40:45 -0800724 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700725 }
PauloftheWestc80b7612014-11-25 04:40:45 -0800726
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700727 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700728 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
729 }
PauloftheWestc80b7612014-11-25 04:40:45 -0800730
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700731 if (mShowEthernet && hasEthernet(context)) {
732 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
733 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700734
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700735 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700736 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
737 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
738 if (mIntentTab != null) {
739 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700740 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700741 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700742 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700743 mTabHost.setCurrentTabByTag(mIntentTab);
744 }
745 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700746 } else if (noTabs) {
747 // no usable tabs, so hide body
748 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700749 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700750 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700751 }
752 }
753
Jeff Sharkey8a503642011-06-10 13:31:21 -0700754 /**
755 * Factory that provide empty {@link View} to make {@link TabHost} happy.
756 */
757 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700758 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700759 public View createTabContent(String tag) {
760 return new View(mTabHost.getContext());
761 }
762 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700763
Jeff Sharkey8a503642011-06-10 13:31:21 -0700764 /**
765 * Build {@link TabSpec} with thin indicator, and empty content.
766 */
767 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700768 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
769 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700770 }
771
PauloftheWest50e6eca2014-10-03 11:07:14 -0700772 /**
773 * Build {@link TabSpec} with thin indicator, and empty content.
774 */
Stuart Scottb1531812014-11-04 14:52:23 -0800775 private TabSpec buildTabSpec(String tag, CharSequence title) {
PauloftheWest50e6eca2014-10-03 11:07:14 -0700776 return mTabHost.newTabSpec(tag).setIndicator(title).setContent(
777 mEmptyTabContent);
778 }
779
780
Jeff Sharkey8a503642011-06-10 13:31:21 -0700781 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700782 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700783 public void onTabChanged(String tabId) {
784 // user changed tab; update body
785 updateBody();
786 }
787 };
788
789 /**
790 * Update body content based on current tab. Loads
791 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
792 * binds them to visible controls.
793 */
794 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700795 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700796 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700797
Jeff Sharkeya662e492011-06-18 21:57:06 -0700798 final Context context = getActivity();
PauloftheWestc80b7612014-11-25 04:40:45 -0800799 final Resources resources = context.getResources();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700800 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700801 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700802
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700803 if (currentTab == null) {
804 Log.w(TAG, "no tab selected; hiding body");
805 mListView.setVisibility(View.GONE);
806 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700807 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700808 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700809 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700810
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700811 mCurrentTab = currentTab;
812
Jeff Sharkey8a503642011-06-10 13:31:21 -0700813 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
814
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700815 mDataEnabledSupported = isOwner;
816 mDisableAtLimitSupported = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700817
Wink Savillefcec91f2014-12-02 17:12:08 -0800818 // TODO: remove mobile tabs when SIM isn't ready probably by
819 // TODO: using SubscriptionManager.getActiveSubscriptionInfoList.
820 if (LOGD) Log.d(TAG, "updateBody() isMobileTab=" + isMobileTab(currentTab));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700821
PauloftheWest50e6eca2014-10-03 11:07:14 -0700822 if (isMobileTab(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800823 if (LOGD) Log.d(TAG, "updateBody() mobile tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700824 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
825 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
PauloftheWest50e6eca2014-10-03 11:07:14 -0700826 mDataEnabledSupported = isMobileDataAvailable(getSubId(currentTab));
Jeff Sharkey4e2d16c2014-12-03 13:44:26 -0800827
828 // Match mobile traffic for this subscriber, but normalize it to
829 // catch any other merged subscribers.
830 mTemplate = buildTemplateMobileAll(
831 getActiveSubscriberId(context, getSubId(currentTab)));
832 mTemplate = NetworkTemplate.normalize(mTemplate,
833 mTelephonyManager.getMergedSubscriberIds());
834
Jeff Sharkey8a503642011-06-10 13:31:21 -0700835 } else if (TAB_3G.equals(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800836 if (LOGD) Log.d(TAG, "updateBody() 3g tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700837 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
838 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
839 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700840 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700841
842 } else if (TAB_4G.equals(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800843 if (LOGD) Log.d(TAG, "updateBody() 4g tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700844 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
845 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
846 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700847 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700848
849 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700850 // wifi doesn't have any controls
Wink Savillefcec91f2014-12-02 17:12:08 -0800851 if (LOGD) Log.d(TAG, "updateBody() wifi tab");
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700852 mDataEnabledSupported = false;
853 mDisableAtLimitSupported = false;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700854 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700855
856 } else if (TAB_ETHERNET.equals(currentTab)) {
857 // ethernet doesn't have any controls
Wink Savillefcec91f2014-12-02 17:12:08 -0800858 if (LOGD) Log.d(TAG, "updateBody() ethernet tab");
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700859 mDataEnabledSupported = false;
860 mDisableAtLimitSupported = false;
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700861 mTemplate = buildTemplateEthernet();
862
863 } else {
Wink Savillefcec91f2014-12-02 17:12:08 -0800864 if (LOGD) Log.d(TAG, "updateBody() unknown tab");
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700865 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700866 }
867
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700868 // kick off loader for network history
869 // TODO: consider chaining two loaders together instead of reloading
870 // network history when showing app detail.
871 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700872 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700873
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700874 // detail mode can change visible menus, invalidate
875 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700876
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700877 mBinding = false;
PauloftheWestc80b7612014-11-25 04:40:45 -0800878
Alan Viverette55eaa962015-03-18 18:10:52 -0700879 int seriesColor = context.getColor(R.color.sim_noitification);
PauloftheWestc80b7612014-11-25 04:40:45 -0800880 if (mCurrentTab != null && mCurrentTab.length() > TAB_MOBILE.length() ){
881 final int slotId = Integer.parseInt(mCurrentTab.substring(TAB_MOBILE.length(),
882 mCurrentTab.length()));
Stuart Scottf51d0062015-04-08 09:40:48 -0700883 final SubscriptionInfo sir = mSubscriptionManager
884 .getActiveSubscriptionInfoForSimSlotIndex(slotId);
PauloftheWestc80b7612014-11-25 04:40:45 -0800885
886 if (sir != null) {
887 seriesColor = sir.getIconTint();
888 }
889 }
890
Jeff Sharkey9c167e92015-01-26 15:15:59 -0700891 final int secondaryColor = Color.argb(127, Color.red(seriesColor), Color.green(seriesColor),
892 Color.blue(seriesColor));
893 mSeries.setChartColor(Color.BLACK, seriesColor, secondaryColor);
894 mDetailedSeries.setChartColor(Color.BLACK, seriesColor, secondaryColor);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700895 }
896
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700897 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700898 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700899 }
900
901 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700902 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700903 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700904 */
905 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700906 final Context context = getActivity();
907 final PackageManager pm = context.getPackageManager();
908 final LayoutInflater inflater = getActivity().getLayoutInflater();
909
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700910 if (isAppDetailMode()) {
911 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700912 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700913 } else {
914 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700915 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700916
917 // hide detail stats when not in detail mode
918 mChart.bindDetailNetworkStats(null);
919 return;
920 }
921
922 // remove warning/limit sweeps while in detail mode
923 mChart.bindNetworkPolicy(null);
924
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700925 // show icon and all labels appearing under this app
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700926 final int uid = mCurrentApp.key;
927 final UidDetail detail = mUidDetailProvider.getUidDetail(uid, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700928 mAppIcon.setImageDrawable(detail.icon);
929
930 mAppTitles.removeAllViews();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700931
932 View title = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700933 if (detail.detailLabels != null) {
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100934 final int n = detail.detailLabels.length;
935 for (int i = 0; i < n; ++i) {
936 CharSequence label = detail.detailLabels[i];
937 CharSequence contentDescription = detail.detailContentDescriptions[i];
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700938 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100939 TextView appTitle = (TextView) title.findViewById(R.id.app_title);
940 appTitle.setText(label);
941 appTitle.setContentDescription(contentDescription);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700942 mAppTitles.addView(title);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700943 }
944 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700945 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100946 TextView appTitle = (TextView) title.findViewById(R.id.app_title);
947 appTitle.setText(detail.label);
948 appTitle.setContentDescription(detail.contentDescription);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700949 mAppTitles.addView(title);
950 }
951
952 // Remember last slot for summary
953 if (title != null) {
954 mAppTotal = (TextView) title.findViewById(R.id.app_summary);
955 } else {
956 mAppTotal = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700957 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700958
959 // enable settings button when package provides it
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700960 final String[] packageNames = pm.getPackagesForUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700961 if (packageNames != null && packageNames.length > 0) {
962 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700963 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
964
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700965 // Search for match across all packages
966 boolean matchFound = false;
967 for (String packageName : packageNames) {
968 mAppSettingsIntent.setPackage(packageName);
969 if (pm.resolveActivity(mAppSettingsIntent, 0) != null) {
970 matchFound = true;
971 break;
972 }
973 }
974
Zoltan Szatmary-Banf20d39e2014-08-07 15:27:08 +0100975 mAppSettings.setOnClickListener(new OnClickListener() {
976 @Override
977 public void onClick(View v) {
978 if (!isAdded()) {
979 return;
980 }
981
982 // TODO: target towards entire UID instead of just first package
983 getActivity().startActivityAsUser(mAppSettingsIntent,
984 new UserHandle(UserHandle.getUserId(uid)));
985 }
986 });
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700987 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -0700988 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700989
990 } else {
991 mAppSettingsIntent = null;
Zoltan Szatmary-Banf20d39e2014-08-07 15:27:08 +0100992 mAppSettings.setOnClickListener(null);
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700993 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700994 }
995
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700996 updateDetailData();
997
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700998 if (UserHandle.isApp(uid) && !mPolicyManager.getRestrictBackground()
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700999 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001000 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -08001001 setPreferenceSummary(mAppRestrictView,
1002 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001003
1004 mAppRestrictView.setVisibility(View.VISIBLE);
1005 mAppRestrict.setChecked(getAppRestrictBackground());
1006
1007 } else {
1008 mAppRestrictView.setVisibility(View.GONE);
1009 }
1010 }
1011
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001012 private void setPolicyWarningBytes(long warningBytes) {
1013 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -07001014 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001015 updatePolicy(false);
1016 }
1017
1018 private void setPolicyLimitBytes(long limitBytes) {
1019 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -07001020 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001021 updatePolicy(false);
1022 }
1023
Wink Savilleb003a852014-10-23 10:16:26 -07001024 private boolean isMobileDataEnabled(int subId) {
Wink Savillea4f4d182014-12-05 15:34:46 -08001025 if (LOGD) Log.d(TAG, "isMobileDataEnabled:+ subId=" + subId);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001026 boolean isEnable = false;
Sanket Padawed819270f2015-01-14 11:03:33 -08001027 if (mMobileDataEnabled.get(String.valueOf(subId)) != null) {
1028 //TODO: deprecate and remove this once enabled flag is on policy
1029 //Multiple Subscriptions, the value need to be reseted
1030 isEnable = mMobileDataEnabled.get(String.valueOf(subId)).booleanValue();
Wink Savillea4f4d182014-12-05 15:34:46 -08001031 if (LOGD) {
1032 Log.d(TAG, "isMobileDataEnabled: != null, subId=" + subId
1033 + " isEnable=" + isEnable);
1034 }
Sanket Padawed819270f2015-01-14 11:03:33 -08001035 mMobileDataEnabled.put(String.valueOf(subId), null);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001036 } else {
Wink Savillea4f4d182014-12-05 15:34:46 -08001037 // SUB SELECT
1038 isEnable = mTelephonyManager.getDataEnabled(subId);
1039 if (LOGD) {
1040 Log.d(TAG, "isMobileDataEnabled: == null, subId=" + subId
1041 + " isEnable=" + isEnable);
1042 }
Jeff Sharkey28130d92011-09-02 16:10:24 -07001043 }
PauloftheWest50e6eca2014-10-03 11:07:14 -07001044 return isEnable;
Jeff Sharkey28130d92011-09-02 16:10:24 -07001045 }
1046
Sanket Padawe24f834d2015-01-08 11:23:11 -08001047 private void setMobileDataEnabled(int subId, boolean enabled) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001048 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
Sanket Padawe24f834d2015-01-08 11:23:11 -08001049 mTelephonyManager.setDataEnabled(subId, enabled);
Sanket Padawed819270f2015-01-14 11:03:33 -08001050 mMobileDataEnabled.put(String.valueOf(subId), enabled);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001051 updatePolicy(false);
1052 }
1053
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001054 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001055 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked()
1056 && ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001057 }
1058
1059 private boolean isBandwidthControlEnabled() {
1060 try {
1061 return mNetworkService.isBandwidthControlEnabled();
1062 } catch (RemoteException e) {
1063 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
1064 return false;
1065 }
1066 }
1067
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001068 public void setRestrictBackground(boolean restrictBackground) {
1069 mPolicyManager.setRestrictBackground(restrictBackground);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001070 updateMenuTitles();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001071 }
1072
1073 private boolean getAppRestrictBackground() {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001074 final int uid = mCurrentApp.key;
1075 final int uidPolicy = mPolicyManager.getUidPolicy(uid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001076 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
1077 }
1078
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001079 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001080 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001081 final int uid = mCurrentApp.key;
1082 mPolicyManager.setUidPolicy(
1083 uid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001084 mAppRestrict.setChecked(restrictBackground);
1085 }
1086
Jeff Sharkey8a503642011-06-10 13:31:21 -07001087 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001088 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
1089 * current {@link #mTemplate}.
1090 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001091 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001092 boolean dataEnabledVisible = mDataEnabledSupported;
1093 boolean disableAtLimitVisible = mDisableAtLimitSupported;
1094
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001095 if (isAppDetailMode()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001096 dataEnabledVisible = false;
1097 disableAtLimitVisible = false;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001098 }
1099
Jeff Sharkey28130d92011-09-02 16:10:24 -07001100 // TODO: move enabled state directly into policy
PauloftheWest50e6eca2014-10-03 11:07:14 -07001101 if (isMobileTab(mCurrentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001102 mBinding = true;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001103 mDataEnabled.setChecked(isMobileDataEnabled(getSubId(mCurrentTab)));
Jeff Sharkey28130d92011-09-02 16:10:24 -07001104 mBinding = false;
1105 }
1106
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001107 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001108 //SUB SELECT
1109 if (isNetworkPolicyModifiable(policy) && isMobileDataAvailable(getSubId(mCurrentTab))) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001110 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001111 if (!isAppDetailMode()) {
1112 mChart.bindNetworkPolicy(policy);
1113 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001114
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001115 } else {
1116 // controls are disabled; don't bind warning/limit sweeps
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001117 disableAtLimitVisible = false;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001118 mChart.bindNetworkPolicy(null);
1119 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001120
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001121 mDataEnabledView.setVisibility(dataEnabledVisible ? View.VISIBLE : View.GONE);
1122 mDisableAtLimitView.setVisibility(disableAtLimitVisible ? View.VISIBLE : View.GONE);
1123
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001124 if (refreshCycle) {
1125 // generate cycle list based on policy and available history
1126 updateCycleList(policy);
1127 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001128 }
1129
1130 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001131 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
1132 * and available {@link NetworkStatsHistory} data. Always selects the newest
1133 * item, updating the inspection range on {@link #mChart}.
1134 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001135 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001136 // stash away currently selected cycle to try restoring below
1137 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -07001138 mCycleAdapter.clear();
1139
1140 final Context context = mCycleSpinner.getContext();
1141
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001142 long historyStart = Long.MAX_VALUE;
1143 long historyEnd = Long.MIN_VALUE;
1144 if (mChartData != null) {
1145 historyStart = mChartData.network.getStart();
1146 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001147 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001148
Jeff Sharkey461842a2011-09-25 18:22:48 -07001149 final long now = System.currentTimeMillis();
1150 if (historyStart == Long.MAX_VALUE) historyStart = now;
1151 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001152
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001153 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001154 if (policy != null) {
1155 // find the next cycle boundary
1156 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001157
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001158 // walk backwards, generating all valid cycle ranges
1159 while (cycleEnd > historyStart) {
1160 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
1161 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
1162 + historyStart);
1163 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1164 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001165 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001166 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001167
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001168 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001169 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001170 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001171
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001172 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001173 // no policy defined cycles; show entry for each four-week period
1174 long cycleEnd = historyEnd;
1175 while (cycleEnd > historyStart) {
1176 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
1177 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1178 cycleEnd = cycleStart;
1179 }
1180
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001181 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001182 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001183
1184 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001185 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001186 final int position = mCycleAdapter.findNearestPosition(previousItem);
1187 mCycleSpinner.setSelection(position);
1188
1189 // only force-update cycle when changed; skipping preserves any
1190 // user-defined inspection region.
1191 final CycleItem selectedItem = mCycleAdapter.getItem(position);
1192 if (!Objects.equal(selectedItem, previousItem)) {
1193 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1194 } else {
1195 // but still kick off loader for detailed list
1196 updateDetailData();
1197 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001198 } else {
1199 updateDetailData();
1200 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001201 }
1202
Sanket Padawed819270f2015-01-14 11:03:33 -08001203 private void disableDataForOtherSubscriptions(SubscriptionInfo currentSir) {
1204 if (mSubInfoList != null) {
1205 for (SubscriptionInfo subInfo : mSubInfoList) {
1206 if (subInfo.getSubscriptionId() != currentSir.getSubscriptionId()) {
1207 setMobileDataEnabled(subInfo.getSubscriptionId(), false);
1208 }
1209 }
1210 }
1211 }
1212
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001213 private View.OnClickListener mDataEnabledListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001214 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001215 public void onClick(View v) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001216 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001217
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001218 final boolean dataEnabled = !mDataEnabled.isChecked();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001219 final String currentTab = mCurrentTab;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001220 if (isMobileTab(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001221 if (dataEnabled) {
PauloftheWestde338442014-10-29 14:46:44 -07001222 // If we are showing the Sim Card tile then we are a Multi-Sim device.
1223 if (Utils.showSimCardTile(getActivity())) {
1224 handleMultiSimDataDialog();
1225 } else {
Sanket Padawe24f834d2015-01-08 11:23:11 -08001226 setMobileDataEnabled(getSubId(currentTab), true);
PauloftheWestde338442014-10-29 14:46:44 -07001227 }
Jeff Sharkey28130d92011-09-02 16:10:24 -07001228 } else {
1229 // disabling data; show confirmation dialog which eventually
1230 // calls setMobileDataEnabled() once user confirms.
Sanket Padawe24f834d2015-01-08 11:23:11 -08001231 ConfirmDataDisableFragment.show(DataUsageSummary.this, getSubId(mCurrentTab));
Jeff Sharkey28130d92011-09-02 16:10:24 -07001232 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001233 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001234
Jeff Sharkey28130d92011-09-02 16:10:24 -07001235 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001236 }
1237 };
1238
PauloftheWestde338442014-10-29 14:46:44 -07001239 private void handleMultiSimDataDialog() {
PauloftheWestf0d84e22014-12-10 08:50:54 -08001240 final Context context = getActivity();
1241 final SubscriptionInfo currentSir = getCurrentTabSubInfo(context);
Sanket Padawe766ab602015-01-20 10:36:15 -08001242
1243 //If sim has not loaded after toggling data switch, return.
1244 if (currentSir == null) {
1245 return;
1246 }
1247
Stuart Scottf51d0062015-04-08 09:40:48 -07001248 final SubscriptionInfo nextSir = mSubscriptionManager.getDefaultDataSubscriptionInfo();
PauloftheWestde338442014-10-29 14:46:44 -07001249
PauloftheWestf0d84e22014-12-10 08:50:54 -08001250 // If the device is single SIM or is enabling data on the active data SIM then forgo
1251 // the pop-up.
1252 if (!Utils.showSimCardTile(context) ||
Sanket Padawed819270f2015-01-14 11:03:33 -08001253 (nextSir != null && currentSir != null &&
1254 currentSir.getSubscriptionId() == nextSir.getSubscriptionId())) {
Sanket Padawe24f834d2015-01-08 11:23:11 -08001255 setMobileDataEnabled(currentSir.getSubscriptionId(), true);
Sanket Padawed819270f2015-01-14 11:03:33 -08001256 if (nextSir != null && currentSir != null &&
1257 currentSir.getSubscriptionId() == nextSir.getSubscriptionId()) {
1258 disableDataForOtherSubscriptions(currentSir);
1259 }
PauloftheWestde338442014-10-29 14:46:44 -07001260 updateBody();
1261 return;
1262 }
1263
PauloftheWestf0d84e22014-12-10 08:50:54 -08001264 final String previousName = (nextSir == null)
1265 ? context.getResources().getString(R.string.sim_selection_required_pref)
1266 : nextSir.getDisplayName().toString();
1267
PauloftheWestde338442014-10-29 14:46:44 -07001268 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
1269
1270 builder.setTitle(R.string.sim_change_data_title);
1271 builder.setMessage(getActivity().getResources().getString(R.string.sim_change_data_message,
PauloftheWestf0d84e22014-12-10 08:50:54 -08001272 currentSir.getDisplayName(), previousName));
PauloftheWestde338442014-10-29 14:46:44 -07001273
1274 builder.setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
1275 @Override
1276 public void onClick(DialogInterface dialog, int id) {
Wink Saville0183fb52014-11-22 10:11:39 -08001277 mSubscriptionManager.setDefaultDataSubId(currentSir.getSubscriptionId());
Sanket Padawe24f834d2015-01-08 11:23:11 -08001278 setMobileDataEnabled(currentSir.getSubscriptionId(), true);
Sanket Padawed819270f2015-01-14 11:03:33 -08001279 disableDataForOtherSubscriptions(currentSir);
PauloftheWestde338442014-10-29 14:46:44 -07001280 updateBody();
1281 }
1282 });
1283 builder.setNegativeButton(R.string.cancel, null);
1284
1285 builder.create().show();
1286 }
1287
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001288 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001289 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001290 public void onClick(View v) {
1291 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001292 if (disableAtLimit) {
1293 // enabling limit; show confirmation dialog which eventually
1294 // calls setPolicyLimitBytes() once user confirms.
1295 ConfirmLimitFragment.show(DataUsageSummary.this);
1296 } else {
1297 setPolicyLimitBytes(LIMIT_DISABLED);
1298 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001299 }
1300 };
1301
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001302 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001303 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001304 public void onClick(View v) {
1305 final boolean restrictBackground = !mAppRestrict.isChecked();
1306
1307 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001308 // enabling restriction; show confirmation dialog which
1309 // eventually calls setRestrictBackground() once user
1310 // confirms.
1311 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001312 } else {
1313 setAppRestrictBackground(false);
1314 }
1315 }
1316 };
1317
Jeff Sharkey8a503642011-06-10 13:31:21 -07001318 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001319 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001320 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001321 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001322 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001323
1324 // TODO: sigh, remove this hack once we understand 6450986
1325 if (mUidDetailProvider == null || app == null) return;
1326
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001327 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001328 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001329 }
1330 };
1331
1332 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001333 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001334 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1335 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1336 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001337 // show cycle editor; will eventually call setPolicyCycleDay()
1338 // when user finishes editing.
1339 CycleEditorFragment.show(DataUsageSummary.this);
1340
1341 // reset spinner to something other than "change cycle..."
1342 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001343
1344 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001345 if (LOGD) {
1346 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1347 + cycle.end + "]");
1348 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001349
1350 // update chart to show selected cycle, and update detail data
1351 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001352 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001353
1354 updateDetailData();
1355 }
1356 }
1357
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001358 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001359 public void onNothingSelected(AdapterView<?> parent) {
1360 // ignored
1361 }
1362 };
1363
1364 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001365 * Update details based on {@link #mChart} inspection range depending on
1366 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1367 * of applications data usage, and when {@link #isAppDetailMode()} update
1368 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001369 */
1370 private void updateDetailData() {
1371 if (LOGD) Log.d(TAG, "updateDetailData()");
1372
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001373 final long start = mChart.getInspectStart();
1374 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001375 final long now = System.currentTimeMillis();
1376
1377 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001378
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001379 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001380 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001381 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001382 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001383 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001384 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001385 final long foregroundBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001386 final long totalBytes = defaultBytes + foregroundBytes;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001387
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001388 if (mAppTotal != null) {
1389 mAppTotal.setText(Formatter.formatFileSize(context, totalBytes));
1390 }
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001391 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1392 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1393
1394 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001395 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001396
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001397 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001398
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001399 mCycleSummary.setVisibility(View.GONE);
1400
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001401 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001402 if (mChartData != null) {
1403 entry = mChartData.network.getValues(start, end, now, null);
1404 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001405
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001406 mCycleSummary.setVisibility(View.VISIBLE);
1407
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001408 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001409 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001410 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001411 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001412
1413 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1414 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001415 mCycleSummary.setText(totalPhrase);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001416
PauloftheWest50e6eca2014-10-03 11:07:14 -07001417 if (isMobileTab(mCurrentTab) || TAB_3G.equals(mCurrentTab)
László Dávid0b8bf4e2012-10-24 23:31:47 +02001418 || TAB_4G.equals(mCurrentTab)) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001419 if (isAppDetailMode()) {
1420 mDisclaimer.setVisibility(View.GONE);
1421 } else {
1422 mDisclaimer.setVisibility(View.VISIBLE);
1423 }
Jeff Sharkeye557c332012-04-13 16:04:07 -07001424 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001425 mDisclaimer.setVisibility(View.GONE);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001426 }
1427
Jeff Sharkey92811822012-05-04 11:47:29 -07001428 // initial layout is finished above, ensure we have transitions
1429 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001430 }
1431
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001432 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1433 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001434 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001435 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001436 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001437 }
1438
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001439 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001440 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1441 mChartData = data;
1442 mChart.bindNetworkStats(mChartData.network);
1443 mChart.bindDetailNetworkStats(mChartData.detail);
1444
1445 // calcuate policy cycles based on available data
1446 updatePolicy(true);
1447 updateAppDetail();
1448
1449 // force scroll to top of body when showing detail
1450 if (mChartData.detail != null) {
1451 mListView.smoothScrollToPosition(0);
1452 }
1453 }
1454
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001455 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001456 public void onLoaderReset(Loader<ChartData> loader) {
1457 mChartData = null;
1458 mChart.bindNetworkStats(null);
1459 mChart.bindDetailNetworkStats(null);
1460 }
1461 };
1462
1463 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001464 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001465 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001466 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001467 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001468 }
1469
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001470 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001471 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001472 final int[] restrictedUids = mPolicyManager.getUidsWithPolicy(
Jeff Sharkeye557c332012-04-13 16:04:07 -07001473 POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001474 mAdapter.bindStats(data, restrictedUids);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001475 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001476 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001477
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001478 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001479 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001480 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001481 updateEmptyVisible();
1482 }
1483
1484 private void updateEmptyVisible() {
1485 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1486 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001487 mStupidPadding.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001488 }
1489 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001490
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001491 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001492 final TelephonyManager tele = TelephonyManager.from(context);
1493 final String actualSubscriberId = tele.getSubscriberId();
Wink Savillefcec91f2014-12-02 17:12:08 -08001494 String retVal = SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
1495 if (LOGD) Log.d(TAG, "getActiveSubscriberId=" + retVal + " actualSubscriberId=" + actualSubscriberId);
1496 return retVal;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001497 }
1498
Wink Savilleb003a852014-10-23 10:16:26 -07001499 private static String getActiveSubscriberId(Context context, int subId) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07001500 final TelephonyManager tele = TelephonyManager.from(context);
Wink Savillefcec91f2014-12-02 17:12:08 -08001501 String retVal = tele.getSubscriberId(subId);
1502 if (LOGD) Log.d(TAG, "getActiveSubscriberId=" + retVal + " subId=" + subId);
1503 return retVal;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001504 }
1505
Jeff Sharkey8a503642011-06-10 13:31:21 -07001506 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001507 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001508 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001509 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001510 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001511
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001512 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001513 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001514 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001515 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001516
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001517 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001518 public void requestWarningEdit() {
1519 WarningEditorFragment.show(DataUsageSummary.this);
1520 }
1521
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001522 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001523 public void requestLimitEdit() {
1524 LimitEditorFragment.show(DataUsageSummary.this);
1525 }
1526 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001527
1528 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001529 * List item that reflects a specific data usage cycle.
1530 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001531 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001532 public CharSequence label;
1533 public long start;
1534 public long end;
1535
Jeff Sharkey8a503642011-06-10 13:31:21 -07001536 CycleItem(CharSequence label) {
1537 this.label = label;
1538 }
1539
1540 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001541 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001542 this.start = start;
1543 this.end = end;
1544 }
1545
Jeff Sharkey8a503642011-06-10 13:31:21 -07001546 @Override
1547 public String toString() {
1548 return label.toString();
1549 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001550
1551 @Override
1552 public boolean equals(Object o) {
1553 if (o instanceof CycleItem) {
1554 final CycleItem another = (CycleItem) o;
1555 return start == another.start && end == another.end;
1556 }
1557 return false;
1558 }
1559
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001560 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001561 public int compareTo(CycleItem another) {
1562 return Long.compare(start, another.start);
1563 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001564 }
1565
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001566 private static final StringBuilder sBuilder = new StringBuilder(50);
1567 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1568 sBuilder, Locale.getDefault());
1569
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001570 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001571 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001572
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001573 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001574 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001575 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1576 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001577 }
1578 }
1579
Jeff Sharkey8a503642011-06-10 13:31:21 -07001580 /**
1581 * Special-case data usage cycle that triggers dialog to change
1582 * {@link NetworkPolicy#cycleDay}.
1583 */
1584 public static class CycleChangeItem extends CycleItem {
1585 public CycleChangeItem(Context context) {
1586 super(context.getString(R.string.data_usage_change_cycle));
1587 }
1588 }
1589
1590 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001591 private boolean mChangePossible = false;
1592 private boolean mChangeVisible = false;
1593
1594 private final CycleChangeItem mChangeItem;
1595
Jeff Sharkey8a503642011-06-10 13:31:21 -07001596 public CycleAdapter(Context context) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001597 super(context, R.layout.data_usage_cycle_item);
1598 setDropDownViewResource(R.layout.data_usage_cycle_item_dropdown);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001599 mChangeItem = new CycleChangeItem(context);
1600 }
1601
1602 public void setChangePossible(boolean possible) {
1603 mChangePossible = possible;
1604 updateChange();
1605 }
1606
1607 public void setChangeVisible(boolean visible) {
1608 mChangeVisible = visible;
1609 updateChange();
1610 }
1611
1612 private void updateChange() {
1613 remove(mChangeItem);
1614 if (mChangePossible && mChangeVisible) {
1615 add(mChangeItem);
1616 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001617 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001618
1619 /**
1620 * Find position of {@link CycleItem} in this adapter which is nearest
1621 * the given {@link CycleItem}.
1622 */
1623 public int findNearestPosition(CycleItem target) {
1624 if (target != null) {
1625 final int count = getCount();
1626 for (int i = count - 1; i >= 0; i--) {
1627 final CycleItem item = getItem(i);
1628 if (item instanceof CycleChangeItem) {
1629 continue;
1630 } else if (item.compareTo(target) >= 0) {
1631 return i;
1632 }
1633 }
1634 }
1635 return 0;
1636 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001637 }
1638
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001639 public static class AppItem implements Comparable<AppItem>, Parcelable {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001640 public static final int CATEGORY_USER = 0;
1641 public static final int CATEGORY_APP_TITLE = 1;
1642 public static final int CATEGORY_APP = 2;
1643
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001644 public final int key;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001645 public boolean restricted;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001646 public int category;
1647
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001648 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001649 public long total;
1650
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001651 public AppItem() {
1652 this.key = 0;
1653 }
1654
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001655 public AppItem(int key) {
1656 this.key = key;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001657 }
1658
1659 public AppItem(Parcel parcel) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001660 key = parcel.readInt();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001661 uids = parcel.readSparseBooleanArray();
1662 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001663 }
1664
1665 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001666 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001667 }
1668
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001669 @Override
1670 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001671 dest.writeInt(key);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001672 dest.writeSparseBooleanArray(uids);
1673 dest.writeLong(total);
1674 }
1675
1676 @Override
1677 public int describeContents() {
1678 return 0;
1679 }
1680
1681 @Override
1682 public int compareTo(AppItem another) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001683 int comparison = Integer.compare(category, another.category);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001684 if (comparison == 0) {
1685 comparison = Long.compare(another.total, total);
1686 }
1687 return comparison;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001688 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001689
1690 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1691 @Override
1692 public AppItem createFromParcel(Parcel in) {
1693 return new AppItem(in);
1694 }
1695
1696 @Override
1697 public AppItem[] newArray(int size) {
1698 return new AppItem[size];
1699 }
1700 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001701 }
1702
Jeff Sharkey8a503642011-06-10 13:31:21 -07001703 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001704 * Adapter of applications, sorted by total usage descending.
1705 */
1706 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001707 private final UidDetailProvider mProvider;
1708 private final int mInsetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001709 private final UserManager mUm;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001710
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001711 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001712 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001713
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001714 public DataUsageAdapter(final UserManager userManager, UidDetailProvider provider, int insetSide) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001715 mProvider = checkNotNull(provider);
1716 mInsetSide = insetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001717 mUm = userManager;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001718 }
1719
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001720 /**
1721 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1722 */
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001723 public void bindStats(NetworkStats stats, int[] restrictedUids) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001724 mItems.clear();
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001725 mLargest = 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001726
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001727 final int currentUserId = ActivityManager.getCurrentUser();
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001728 final List<UserHandle> profiles = mUm.getUserProfiles();
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001729 final SparseArray<AppItem> knownItems = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001730
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001731 NetworkStats.Entry entry = null;
1732 final int size = stats != null ? stats.size() : 0;
1733 for (int i = 0; i < size; i++) {
1734 entry = stats.getValues(i, entry);
1735
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001736 // Decide how to collapse items together
1737 final int uid = entry.uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001738
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001739 final int collapseKey;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001740 final int category;
1741 final int userId = UserHandle.getUserId(uid);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001742 if (UserHandle.isApp(uid)) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001743 if (profiles.contains(new UserHandle(userId))) {
1744 if (userId != currentUserId) {
1745 // Add to a managed user item.
1746 final int managedKey = UidDetailProvider.buildKeyForUser(userId);
1747 accumulate(managedKey, knownItems, entry,
1748 AppItem.CATEGORY_USER);
1749 }
1750 // Add to app item.
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001751 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001752 category = AppItem.CATEGORY_APP;
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001753 } else {
Zoltan Szatmary-Ban77c1d362014-12-12 18:48:03 +00001754 // If it is a removed user add it to the removed users' key
1755 final UserInfo info = mUm.getUserInfo(userId);
1756 if (info == null) {
1757 collapseKey = UID_REMOVED;
1758 category = AppItem.CATEGORY_APP;
1759 } else {
1760 // Add to other user item.
1761 collapseKey = UidDetailProvider.buildKeyForUser(userId);
1762 category = AppItem.CATEGORY_USER;
1763 }
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001764 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001765 } else if (uid == UID_REMOVED || uid == UID_TETHERING) {
1766 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001767 category = AppItem.CATEGORY_APP;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001768 } else {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001769 collapseKey = android.os.Process.SYSTEM_UID;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001770 category = AppItem.CATEGORY_APP;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001771 }
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001772 accumulate(collapseKey, knownItems, entry, category);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001773 }
1774
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001775 final int restrictedUidsMax = restrictedUids.length;
1776 for (int i = 0; i < restrictedUidsMax; ++i) {
1777 final int uid = restrictedUids[i];
1778 // Only splice in restricted state for current user or managed users
1779 if (!profiles.contains(new UserHandle(UserHandle.getUserId(uid)))) {
1780 continue;
1781 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001782
1783 AppItem item = knownItems.get(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001784 if (item == null) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001785 item = new AppItem(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001786 item.total = -1;
1787 mItems.add(item);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001788 knownItems.put(item.key, item);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001789 }
1790 item.restricted = true;
1791 }
1792
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001793 if (!mItems.isEmpty()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001794 final AppItem title = new AppItem();
1795 title.category = AppItem.CATEGORY_APP_TITLE;
1796 mItems.add(title);
1797 }
1798
Jeff Sharkey8a503642011-06-10 13:31:21 -07001799 Collections.sort(mItems);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001800 notifyDataSetChanged();
1801 }
1802
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001803 /**
1804 * Accumulate data usage of a network stats entry for the item mapped by the collapse key.
1805 * Creates the item if needed.
1806 *
1807 * @param collapseKey the collapse key used to map the item.
1808 * @param knownItems collection of known (already existing) items.
1809 * @param entry the network stats entry to extract data usage from.
1810 * @param itemCategory the item is categorized on the list view by this category. Must be
1811 * either AppItem.APP_ITEM_CATEGORY or AppItem.MANAGED_USER_ITEM_CATEGORY
1812 */
1813 private void accumulate(int collapseKey, final SparseArray<AppItem> knownItems,
1814 NetworkStats.Entry entry, int itemCategory) {
1815 final int uid = entry.uid;
1816 AppItem item = knownItems.get(collapseKey);
1817 if (item == null) {
1818 item = new AppItem(collapseKey);
1819 item.category = itemCategory;
1820 mItems.add(item);
1821 knownItems.put(item.key, item);
1822 }
1823 item.addUid(uid);
1824 item.total += entry.rxBytes + entry.txBytes;
1825 if (mLargest < item.total) {
1826 mLargest = item.total;
1827 }
1828 }
1829
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001830 @Override
1831 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001832 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001833 }
1834
1835 @Override
1836 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001837 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001838 }
1839
1840 @Override
1841 public long getItemId(int position) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001842 return mItems.get(position).key;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001843 }
1844
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001845 /**
1846 * See {@link #getItemViewType} for the view types.
1847 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001848 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001849 public int getViewTypeCount() {
1850 return 2;
1851 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001852
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001853 /**
1854 * Returns 1 for separator items and 0 for anything else.
1855 */
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001856 @Override
1857 public int getItemViewType(int position) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001858 final AppItem item = mItems.get(position);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001859 if (item.category == AppItem.CATEGORY_APP_TITLE) {
1860 return 1;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001861 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001862 return 0;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001863 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001864 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001865
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001866 @Override
1867 public boolean areAllItemsEnabled() {
1868 return false;
1869 }
1870
1871 @Override
1872 public boolean isEnabled(int position) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001873 if (position > mItems.size()) {
1874 throw new ArrayIndexOutOfBoundsException();
1875 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001876 return getItemViewType(position) == 0;
1877 }
1878
1879 @Override
1880 public View getView(int position, View convertView, ViewGroup parent) {
1881 final AppItem item = mItems.get(position);
1882 if (getItemViewType(position) == 1) {
1883 if (convertView == null) {
Zoltan Szatmary-Ban3af2e4c2014-12-19 17:17:23 +00001884 convertView = Utils.inflateCategoryHeader(LayoutInflater.from(
1885 parent.getContext()), parent);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001886 }
1887
1888 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
1889 title.setText(R.string.data_usage_app);
1890
1891 } else {
1892 if (convertView == null) {
1893 convertView = LayoutInflater.from(parent.getContext()).inflate(
1894 R.layout.data_usage_item, parent, false);
1895
1896 if (mInsetSide > 0) {
1897 convertView.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
1898 }
1899 }
1900
1901 final Context context = parent.getContext();
1902
1903 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
1904 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1905 android.R.id.progress);
1906
1907 // kick off async load of app details
1908 UidDetailTask.bindView(mProvider, item, convertView);
1909
1910 if (item.restricted && item.total <= 0) {
1911 text1.setText(R.string.data_usage_app_restricted);
1912 progress.setVisibility(View.GONE);
1913 } else {
1914 text1.setText(Formatter.formatFileSize(context, item.total));
1915 progress.setVisibility(View.VISIBLE);
1916 }
1917
1918 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1919 progress.setProgress(percentTotal);
1920 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001921
1922 return convertView;
1923 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001924 }
1925
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001926 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001927 * Empty {@link Fragment} that controls display of UID details in
1928 * {@link DataUsageSummary}.
1929 */
1930 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001931 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001932
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001933 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jason Monk4bb075b2015-03-17 10:06:58 -04001934 show(parent, app, label, true);
1935 }
1936
1937 public static void show(DataUsageSummary parent, AppItem app, CharSequence label,
1938 boolean addToBack) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001939 if (!parent.isAdded()) return;
1940
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001941 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001942 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001943
1944 final AppDetailsFragment fragment = new AppDetailsFragment();
1945 fragment.setArguments(args);
1946 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001947 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1948 ft.add(fragment, TAG_APP_DETAILS);
Jason Monk4bb075b2015-03-17 10:06:58 -04001949 if (addToBack) {
1950 ft.addToBackStack(TAG_APP_DETAILS);
1951 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001952 ft.setBreadCrumbTitle(
1953 parent.getResources().getString(R.string.data_usage_app_summary_title));
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001954 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001955 }
1956
1957 @Override
1958 public void onStart() {
1959 super.onStart();
1960 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001961 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001962 target.updateBody();
1963 }
1964
1965 @Override
1966 public void onStop() {
1967 super.onStop();
1968 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001969 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001970 target.updateBody();
1971 }
1972 }
1973
1974 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001975 * Dialog to request user confirmation before setting
1976 * {@link NetworkPolicy#limitBytes}.
1977 */
1978 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001979 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001980 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001981
1982 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001983 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001984
Jeff Sharkeyd0a55312014-08-08 10:25:24 -07001985 final NetworkPolicy policy = parent.mPolicyEditor.getPolicy(parent.mTemplate);
1986 if (policy == null) return;
1987
Jeff Sharkey461842a2011-09-25 18:22:48 -07001988 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001989 final CharSequence message;
Jeff Sharkeyd0a55312014-08-08 10:25:24 -07001990 final long minLimitBytes = (long) (policy.warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001991 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001992
1993 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001994 final String currentTab = parent.mCurrentTab;
1995 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001996 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001997 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001998 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001999 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07002000 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002001 } else if (isMobileTab(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07002002 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07002003 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07002004 } else {
2005 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002006 }
2007
Jeff Sharkey131f9d62011-08-17 17:08:19 -07002008 final Bundle args = new Bundle();
2009 args.putCharSequence(EXTRA_MESSAGE, message);
2010 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
2011
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002012 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
2013 dialog.setArguments(args);
2014 dialog.setTargetFragment(parent, 0);
2015 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
2016 }
2017
2018 @Override
2019 public Dialog onCreateDialog(Bundle savedInstanceState) {
2020 final Context context = getActivity();
2021
Jeff Sharkey131f9d62011-08-17 17:08:19 -07002022 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002023 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
2024
2025 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2026 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07002027 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002028
2029 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002030 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002031 public void onClick(DialogInterface dialog, int which) {
2032 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2033 if (target != null) {
2034 target.setPolicyLimitBytes(limitBytes);
2035 }
2036 }
2037 });
2038
2039 return builder.create();
2040 }
2041 }
2042
2043 /**
2044 * Dialog to edit {@link NetworkPolicy#cycleDay}.
2045 */
2046 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002047 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002048
2049 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002050 if (!parent.isAdded()) return;
2051
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002052 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002053 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002054
2055 final CycleEditorFragment dialog = new CycleEditorFragment();
2056 dialog.setArguments(args);
2057 dialog.setTargetFragment(parent, 0);
2058 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
2059 }
2060
2061 @Override
2062 public Dialog onCreateDialog(Bundle savedInstanceState) {
2063 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002064 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2065 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002066
2067 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2068 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2069
2070 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
2071 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
2072
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002073 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2074 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002075
2076 cycleDayPicker.setMinValue(1);
2077 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002078 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002079 cycleDayPicker.setWrapSelectorWheel(true);
2080
2081 builder.setTitle(R.string.data_usage_cycle_editor_title);
2082 builder.setView(view);
2083
2084 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2085 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002086 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002087 public void onClick(DialogInterface dialog, int which) {
Jeff Sharkeyd277de92013-03-25 15:11:25 -07002088 // clear focus to finish pending text edits
2089 cycleDayPicker.clearFocus();
2090
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002091 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08002092 final String cycleTimezone = new Time().timezone;
2093 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002094 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002095 }
2096 });
2097
2098 return builder.create();
2099 }
2100 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002101
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002102 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002103 * Dialog to edit {@link NetworkPolicy#warningBytes}.
2104 */
2105 public static class WarningEditorFragment extends DialogFragment {
2106 private static final String EXTRA_TEMPLATE = "template";
2107
2108 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002109 if (!parent.isAdded()) return;
2110
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002111 final Bundle args = new Bundle();
2112 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
2113
2114 final WarningEditorFragment dialog = new WarningEditorFragment();
2115 dialog.setArguments(args);
2116 dialog.setTargetFragment(parent, 0);
2117 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
2118 }
2119
2120 @Override
2121 public Dialog onCreateDialog(Bundle savedInstanceState) {
2122 final Context context = getActivity();
2123 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2124 final NetworkPolicyEditor editor = target.mPolicyEditor;
2125
2126 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2127 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2128
2129 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
2130 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
2131
2132 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2133 final long warningBytes = editor.getPolicyWarningBytes(template);
2134 final long limitBytes = editor.getPolicyLimitBytes(template);
2135
2136 bytesPicker.setMinValue(0);
2137 if (limitBytes != LIMIT_DISABLED) {
2138 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
2139 } else {
2140 bytesPicker.setMaxValue(Integer.MAX_VALUE);
2141 }
2142 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
2143 bytesPicker.setWrapSelectorWheel(false);
2144
2145 builder.setTitle(R.string.data_usage_warning_editor_title);
2146 builder.setView(view);
2147
2148 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2149 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002150 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002151 public void onClick(DialogInterface dialog, int which) {
2152 // clear focus to finish pending text edits
2153 bytesPicker.clearFocus();
2154
2155 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
2156 editor.setPolicyWarningBytes(template, bytes);
2157 target.updatePolicy(false);
2158 }
2159 });
2160
2161 return builder.create();
2162 }
2163 }
2164
2165 /**
2166 * Dialog to edit {@link NetworkPolicy#limitBytes}.
2167 */
2168 public static class LimitEditorFragment extends DialogFragment {
2169 private static final String EXTRA_TEMPLATE = "template";
2170
2171 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002172 if (!parent.isAdded()) return;
2173
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002174 final Bundle args = new Bundle();
2175 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
2176
2177 final LimitEditorFragment dialog = new LimitEditorFragment();
2178 dialog.setArguments(args);
2179 dialog.setTargetFragment(parent, 0);
2180 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
2181 }
2182
2183 @Override
2184 public Dialog onCreateDialog(Bundle savedInstanceState) {
2185 final Context context = getActivity();
2186 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2187 final NetworkPolicyEditor editor = target.mPolicyEditor;
2188
2189 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2190 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2191
2192 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
2193 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
2194
2195 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2196 final long warningBytes = editor.getPolicyWarningBytes(template);
2197 final long limitBytes = editor.getPolicyLimitBytes(template);
2198
2199 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09002200 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002201 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
2202 } else {
2203 bytesPicker.setMinValue(0);
2204 }
2205 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
2206 bytesPicker.setWrapSelectorWheel(false);
2207
2208 builder.setTitle(R.string.data_usage_limit_editor_title);
2209 builder.setView(view);
2210
2211 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2212 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002213 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002214 public void onClick(DialogInterface dialog, int which) {
2215 // clear focus to finish pending text edits
2216 bytesPicker.clearFocus();
2217
2218 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
2219 editor.setPolicyLimitBytes(template, bytes);
2220 target.updatePolicy(false);
2221 }
2222 });
2223
2224 return builder.create();
2225 }
2226 }
2227 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07002228 * Dialog to request user confirmation before disabling data.
2229 */
2230 public static class ConfirmDataDisableFragment extends DialogFragment {
Sanket Padawe24f834d2015-01-08 11:23:11 -08002231 static int mSubId;
2232 public static void show(DataUsageSummary parent, int subId) {
2233 mSubId = subId;
Jeff Sharkey461842a2011-09-25 18:22:48 -07002234 if (!parent.isAdded()) return;
2235
Jeff Sharkey28130d92011-09-02 16:10:24 -07002236 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
2237 dialog.setTargetFragment(parent, 0);
2238 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
2239 }
2240
2241 @Override
2242 public Dialog onCreateDialog(Bundle savedInstanceState) {
2243 final Context context = getActivity();
2244
2245 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2246 builder.setMessage(R.string.data_usage_disable_mobile);
2247
2248 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002249 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07002250 public void onClick(DialogInterface dialog, int which) {
2251 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2252 if (target != null) {
2253 // TODO: extend to modify policy enabled flag.
Sanket Padawe24f834d2015-01-08 11:23:11 -08002254 target.setMobileDataEnabled(mSubId, false);
Jeff Sharkey28130d92011-09-02 16:10:24 -07002255 }
2256 }
2257 });
2258 builder.setNegativeButton(android.R.string.cancel, null);
2259
2260 return builder.create();
2261 }
2262 }
2263
2264 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002265 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07002266 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002267 */
2268 public static class ConfirmRestrictFragment extends DialogFragment {
2269 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002270 if (!parent.isAdded()) return;
2271
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002272 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
2273 dialog.setTargetFragment(parent, 0);
2274 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
2275 }
2276
2277 @Override
2278 public Dialog onCreateDialog(Bundle savedInstanceState) {
2279 final Context context = getActivity();
2280
2281 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002282 builder.setTitle(R.string.data_usage_restrict_background_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07002283 if (Utils.hasMultipleUsers(context)) {
2284 builder.setMessage(R.string.data_usage_restrict_background_multiuser);
2285 } else {
2286 builder.setMessage(R.string.data_usage_restrict_background);
2287 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002288
2289 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002290 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002291 public void onClick(DialogInterface dialog, int which) {
2292 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2293 if (target != null) {
2294 target.setRestrictBackground(true);
2295 }
2296 }
2297 });
2298 builder.setNegativeButton(android.R.string.cancel, null);
2299
2300 return builder.create();
2301 }
2302 }
2303
2304 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002305 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
2306 * change has been denied, usually based on
2307 * {@link DataUsageSummary#hasLimitedNetworks()}.
2308 */
2309 public static class DeniedRestrictFragment extends DialogFragment {
2310 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002311 if (!parent.isAdded()) return;
2312
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002313 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
2314 dialog.setTargetFragment(parent, 0);
2315 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
2316 }
2317
2318 @Override
2319 public Dialog onCreateDialog(Bundle savedInstanceState) {
2320 final Context context = getActivity();
2321
2322 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2323 builder.setTitle(R.string.data_usage_app_restrict_background);
2324 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
2325 builder.setPositiveButton(android.R.string.ok, null);
2326
2327 return builder.create();
2328 }
2329 }
2330
2331 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002332 * Dialog to request user confirmation before setting
2333 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
2334 */
2335 public static class ConfirmAppRestrictFragment extends DialogFragment {
2336 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002337 if (!parent.isAdded()) return;
2338
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002339 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2340 dialog.setTargetFragment(parent, 0);
2341 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2342 }
2343
2344 @Override
2345 public Dialog onCreateDialog(Bundle savedInstanceState) {
2346 final Context context = getActivity();
2347
2348 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002349 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2350 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2351
2352 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002353 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002354 public void onClick(DialogInterface dialog, int which) {
2355 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2356 if (target != null) {
2357 target.setAppRestrictBackground(true);
2358 }
2359 }
2360 });
2361 builder.setNegativeButton(android.R.string.cancel, null);
2362
2363 return builder.create();
2364 }
2365 }
2366
2367 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002368 * Compute default tab that should be selected, based on
2369 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2370 */
2371 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002372 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002373 if (template == null) {
Wink Savilleb003a852014-10-23 10:16:26 -07002374 final int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
Wink Saville0183fb52014-11-22 10:11:39 -08002375 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Savilled72c0622014-12-11 10:10:05 -08002376 if (SubscriptionManager.isValidSubscriptionId(subId)) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07002377 return TAB_MOBILE + String.valueOf(subId);
2378 }
2379 return null;
2380 }
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002381
2382 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002383 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002384 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002385 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002386 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002387 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002388 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002389 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002390 return TAB_WIFI;
2391 default:
2392 return null;
2393 }
2394 }
2395
2396 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002397 * Background task that loads {@link UidDetail}, binding to
2398 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002399 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002400 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2401 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002402 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002403 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002404
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002405 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002406 mProvider = checkNotNull(provider);
2407 mItem = checkNotNull(item);
2408 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002409 }
2410
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002411 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002412 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002413 final UidDetailTask existing = (UidDetailTask) target.getTag();
2414 if (existing != null) {
2415 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002416 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002417
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002418 final UidDetail cachedDetail = provider.getUidDetail(item.key, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002419 if (cachedDetail != null) {
2420 bindView(cachedDetail, target);
2421 } else {
2422 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2423 AsyncTask.THREAD_POOL_EXECUTOR));
2424 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002425 }
2426
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002427 private static void bindView(UidDetail detail, View target) {
2428 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2429 final TextView title = (TextView) target.findViewById(android.R.id.title);
2430
2431 if (detail != null) {
2432 icon.setImageDrawable(detail.icon);
2433 title.setText(detail.label);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +01002434 title.setContentDescription(detail.contentDescription);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002435 } else {
2436 icon.setImageDrawable(null);
2437 title.setText(null);
2438 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002439 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002440
2441 @Override
2442 protected void onPreExecute() {
2443 bindView(null, mTarget);
2444 }
2445
2446 @Override
2447 protected UidDetail doInBackground(Void... params) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002448 return mProvider.getUidDetail(mItem.key, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002449 }
2450
2451 @Override
2452 protected void onPostExecute(UidDetail result) {
2453 bindView(result, mTarget);
2454 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002455 }
2456
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002457 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002458 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002459 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002460 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002461 if (TEST_RADIOS) {
2462 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2463 }
2464
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002465 final ConnectivityManager conn = ConnectivityManager.from(context);
2466 final TelephonyManager tele = TelephonyManager.from(context);
2467
Wink Saville0183fb52014-11-22 10:11:39 -08002468 final List<SubscriptionInfo> subInfoList =
2469 SubscriptionManager.from(context).getActiveSubscriptionInfoList();
2470 // No activated Subscriptions
PauloftheWest50e6eca2014-10-03 11:07:14 -07002471 if (subInfoList == null) {
Wink Savillefcec91f2014-12-02 17:12:08 -08002472 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subInfoList=null");
PauloftheWest50e6eca2014-10-03 11:07:14 -07002473 return false;
2474 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002475 // require both supported network and ready SIM
PauloftheWest50e6eca2014-10-03 11:07:14 -07002476 boolean isReady = true;
Wink Savilleca756612014-11-08 10:47:12 -08002477 for (SubscriptionInfo subInfo : subInfoList) {
Stuart Scott3ada2ec2014-10-31 14:11:24 -07002478 isReady = isReady & tele.getSimState(subInfo.getSimSlotIndex()) == SIM_STATE_READY;
Wink Savillefcec91f2014-12-02 17:12:08 -08002479 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subInfo=" + subInfo);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002480 }
Wink Savillefcec91f2014-12-02 17:12:08 -08002481 boolean retVal = conn.isNetworkSupported(TYPE_MOBILE) && isReady;
2482 if (LOGD) {
2483 Log.d(TAG, "hasReadyMobileRadio:"
2484 + " conn.isNetworkSupported(TYPE_MOBILE)="
2485 + conn.isNetworkSupported(TYPE_MOBILE)
2486 + " isReady=" + isReady);
2487 }
2488 return retVal;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002489 }
2490
2491 /*
2492 * TODO: consider adding to TelephonyManager or SubscritpionManager.
2493 */
Wink Savilleb003a852014-10-23 10:16:26 -07002494 public static boolean hasReadyMobileRadio(Context context, int subId) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07002495 if (TEST_RADIOS) {
2496 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2497 }
2498
2499 final ConnectivityManager conn = ConnectivityManager.from(context);
2500 final TelephonyManager tele = TelephonyManager.from(context);
2501 final int slotId = SubscriptionManager.getSlotId(subId);
2502 final boolean isReady = tele.getSimState(slotId) == SIM_STATE_READY;
2503
Wink Savillefcec91f2014-12-02 17:12:08 -08002504 boolean retVal = conn.isNetworkSupported(TYPE_MOBILE) && isReady;
2505 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subId=" + subId
2506 + " conn.isNetworkSupported(TYPE_MOBILE)=" + conn.isNetworkSupported(TYPE_MOBILE)
2507 + " isReady=" + isReady);
2508 return retVal;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002509 }
2510
2511 /**
2512 * Test if device has a mobile 4G data radio.
2513 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002514 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002515 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2516 return false;
2517 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002518 if (TEST_RADIOS) {
2519 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2520 }
2521
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002522 final ConnectivityManager conn = ConnectivityManager.from(context);
2523 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002524
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002525 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Wink Saville55434042012-06-14 12:33:43 -07002526 final boolean hasLte = (tele.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE)
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002527 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002528 return hasWimax || hasLte;
2529 }
2530
2531 /**
2532 * Test if device has a Wi-Fi data radio.
2533 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002534 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002535 if (TEST_RADIOS) {
2536 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2537 }
2538
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002539 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002540 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002541 }
2542
2543 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002544 * Test if device has an ethernet network connection.
2545 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002546 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002547 if (TEST_RADIOS) {
2548 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2549 }
2550
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002551 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002552 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2553
2554 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002555 if (mStatsSession != null) {
2556 try {
2557 ethernetBytes = mStatsSession.getSummaryForNetwork(
2558 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2559 .getTotalBytes();
2560 } catch (RemoteException e) {
2561 throw new RuntimeException(e);
2562 }
2563 } else {
2564 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002565 }
2566
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002567 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002568 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002569 }
2570
2571 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002572 * Inflate a {@link Preference} style layout, adding the given {@link View}
2573 * widget into {@link android.R.id#widget_frame}.
2574 */
2575 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2576 final View view = inflater.inflate(R.layout.preference, root, false);
2577 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2578 android.R.id.widget_frame);
2579 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2580 return view;
2581 }
2582
2583 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002584 * Test if any networks are currently limited.
2585 */
2586 private boolean hasLimitedNetworks() {
2587 return !buildLimitedNetworksList().isEmpty();
2588 }
2589
2590 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002591 * Build string describing currently limited networks, which defines when
2592 * background data is restricted.
2593 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002594 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002595 private CharSequence buildLimitedNetworksString() {
2596 final List<CharSequence> limited = buildLimitedNetworksList();
2597
2598 // handle case where no networks limited
2599 if (limited.isEmpty()) {
2600 limited.add(getText(R.string.data_usage_list_none));
2601 }
2602
2603 return TextUtils.join(limited);
2604 }
2605
2606 /**
2607 * Build list of currently limited networks, which defines when background
2608 * data is restricted.
2609 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002610 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002611 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002612 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002613
2614 // build combined list of all limited networks
2615 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002616
2617 final TelephonyManager tele = TelephonyManager.from(context);
2618 if (tele.getSimState() == SIM_STATE_READY) {
2619 final String subscriberId = getActiveSubscriberId(context);
2620 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2621 limited.add(getText(R.string.data_usage_list_mobile));
2622 }
2623 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2624 limited.add(getText(R.string.data_usage_tab_3g));
2625 }
2626 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2627 limited.add(getText(R.string.data_usage_tab_4g));
2628 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002629 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002630
2631 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002632 limited.add(getText(R.string.data_usage_tab_wifi));
2633 }
2634 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2635 limited.add(getText(R.string.data_usage_tab_ethernet));
2636 }
2637
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002638 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002639 }
2640
2641 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002642 * Inset both selector and divider {@link Drawable} on the given
2643 * {@link ListView} by the requested dimensions.
2644 */
2645 private static void insetListViewDrawables(ListView view, int insetSide) {
2646 final Drawable selector = view.getSelector();
2647 final Drawable divider = view.getDivider();
2648
2649 // fully unregister these drawables so callbacks can be maintained after
2650 // wrapping below.
2651 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2652 view.setSelector(stub);
2653 view.setDivider(stub);
2654
2655 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2656 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2657 }
2658
2659 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002660 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002661 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002662 */
2663 private static void setPreferenceTitle(View parent, int resId) {
2664 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2665 title.setText(resId);
2666 }
2667
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002668 /**
2669 * Set {@link android.R.id#summary} for a preference view inflated with
2670 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2671 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002672 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002673 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2674 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002675 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002676 }
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002677
2678 /**
2679 * For search
2680 */
2681 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -07002682 new BaseSearchIndexProvider() {
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002683 @Override
2684 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
2685 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
2686
2687 final Resources res = context.getResources();
2688
2689 // Add fragment title
2690 SearchIndexableRaw data = new SearchIndexableRaw(context);
2691 data.title = res.getString(R.string.data_usage_summary_title);
2692 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2693 result.add(data);
2694
2695 // Mobile data
2696 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002697 data.key = DATA_USAGE_ENABLE_MOBILE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002698 data.title = res.getString(R.string.data_usage_enable_mobile);
2699 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2700 result.add(data);
2701
2702 // Set mobile data limit
2703 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002704 data.key = DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002705 data.title = res.getString(R.string.data_usage_disable_mobile_limit);
2706 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2707 result.add(data);
2708
Fabrice Di Megliof2a52262014-04-17 17:20:27 -07002709 // Data usage cycle
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002710 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002711 data.key = DATA_USAGE_CYCLE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002712 data.title = res.getString(R.string.data_usage_cycle);
2713 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2714 result.add(data);
2715
2716 return result;
2717 }
2718 };
2719
Sanket Padawe7e21fa52015-02-12 12:39:34 -08002720 private void addMobileTab(Context context, SubscriptionInfo subInfo, boolean isMultiSim) {
PauloftheWestc80b7612014-11-25 04:40:45 -08002721 if (subInfo != null && mMobileTagMap != null) {
2722 if (hasReadyMobileRadio(context, subInfo.getSubscriptionId())) {
Sanket Padawe7e21fa52015-02-12 12:39:34 -08002723 if (isMultiSim) {
2724 mTabHost.addTab(buildTabSpec(mMobileTagMap.get(subInfo.getSubscriptionId()),
2725 subInfo.getDisplayName()));
2726 } else {
2727 mTabHost.addTab(buildTabSpec(mMobileTagMap.get(subInfo.getSubscriptionId()),
2728 R.string.data_usage_tab_mobile));
2729 }
PauloftheWest50e6eca2014-10-03 11:07:14 -07002730 }
Wink Savillefcec91f2014-12-02 17:12:08 -08002731 } else {
2732 if (LOGD) Log.d(TAG, "addMobileTab: subInfoList is null");
PauloftheWest50e6eca2014-10-03 11:07:14 -07002733 }
2734 }
2735
Wink Savilleca756612014-11-08 10:47:12 -08002736 private SubscriptionInfo getCurrentTabSubInfo(Context context) {
PauloftheWestde338442014-10-29 14:46:44 -07002737 if (mSubInfoList != null && mTabHost != null) {
2738 final int currentTagIndex = mTabHost.getCurrentTab();
2739 int i = 0;
Wink Savilleca756612014-11-08 10:47:12 -08002740 for (SubscriptionInfo subInfo : mSubInfoList) {
Stuart Scott3ada2ec2014-10-31 14:11:24 -07002741 if (hasReadyMobileRadio(context, subInfo.getSubscriptionId())) {
PauloftheWestde338442014-10-29 14:46:44 -07002742 if (i++ == currentTagIndex) {
2743 return subInfo;
2744 }
2745 }
2746 }
2747 }
2748 return null;
2749 }
2750
PauloftheWest50e6eca2014-10-03 11:07:14 -07002751 /**
2752 * Init a map with subId key and mobile tag name
2753 * @param subInfoList The subscription Info List
2754 * @return The map or null if no activated subscription
2755 */
Wink Savilleca756612014-11-08 10:47:12 -08002756 private Map<Integer, String> initMobileTabTag(List<SubscriptionInfo> subInfoList) {
Wink Savilleb003a852014-10-23 10:16:26 -07002757 Map<Integer, String> map = null;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002758 if (subInfoList != null) {
2759 String mobileTag;
Wink Savilleb003a852014-10-23 10:16:26 -07002760 map = new HashMap<Integer, String>();
Wink Savillefcec91f2014-12-02 17:12:08 -08002761 for (SubscriptionInfo subInfo : subInfoList) {
2762 mobileTag = TAB_MOBILE + String.valueOf(subInfo.getSubscriptionId());
2763 map.put(subInfo.getSubscriptionId(), mobileTag);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002764 }
2765 }
2766 return map;
2767 }
2768
2769 private static boolean isMobileTab(String currentTab) {
2770 return currentTab != null ? currentTab.contains(TAB_MOBILE) : false;
2771 }
2772
Wink Savilleb003a852014-10-23 10:16:26 -07002773 private int getSubId(String currentTab) {
Wink Saville0183fb52014-11-22 10:11:39 -08002774 if (mMobileTagMap != null) {
2775 Set<Integer> set = mMobileTagMap.keySet();
2776 for (Integer subId : set) {
2777 if (mMobileTagMap.get(subId).equals(currentTab)) {
2778 return subId;
2779 }
PauloftheWest50e6eca2014-10-03 11:07:14 -07002780 }
2781 }
2782 Log.e(TAG, "currentTab = " + currentTab + " non mobile tab called this function");
2783 return -1;
2784 }
2785
Stuart Scottf51d0062015-04-08 09:40:48 -07002786 private boolean isMobileDataAvailable(int subId) {
2787 return mSubscriptionManager.getActiveSubscriptionInfo(subId) != null;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002788 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002789}