blob: 0d21066a910da866d020b9b1535775ee8ce5e18f [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
Wink Saville55434042012-06-14 12:33:43 -0700130import com.android.internal.telephony.PhoneConstants;
Jeff Sharkey5d706792011-09-08 18:57:17 -0700131import com.android.settings.drawable.InsetBoundsDrawable;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700132import com.android.settings.net.ChartData;
133import com.android.settings.net.ChartDataLoader;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700134import com.android.settings.net.DataUsageMeteredSettings;
Jeff Sharkeya662e492011-06-18 21:57:06 -0700135import com.android.settings.net.NetworkPolicyEditor;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700136import com.android.settings.net.SummaryForAllUidLoader;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700137import com.android.settings.net.UidDetail;
138import com.android.settings.net.UidDetailProvider;
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -0700139import com.android.settings.search.BaseSearchIndexProvider;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -0700140import com.android.settings.search.Indexable;
141import com.android.settings.search.SearchIndexableRaw;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700142import com.android.settings.widget.ChartDataUsageView;
143import com.android.settings.widget.ChartDataUsageView.DataUsageChartListener;
PauloftheWestc80b7612014-11-25 04:40:45 -0800144import com.android.settings.widget.ChartNetworkSeriesView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700145import com.google.android.collect.Lists;
146
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700147import libcore.util.Objects;
148
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700149import java.util.ArrayList;
150import java.util.Collections;
PauloftheWest50e6eca2014-10-03 11:07:14 -0700151import java.util.HashMap;
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700152import java.util.List;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700153import java.util.Locale;
PauloftheWest50e6eca2014-10-03 11:07:14 -0700154import java.util.Map;
155import java.util.Set;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700156
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700157/**
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700158 * Panel showing data usage history across various networks, including options
159 * to inspect based on usage cycle and control through {@link NetworkPolicy}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700160 */
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700161public class DataUsageSummary extends HighlightingFragment implements Indexable {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700162 private static final String TAG = "DataUsage";
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800163 private static final boolean LOGD = false;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700164
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700165 // TODO: remove this testing code
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700166 private static final boolean TEST_ANIM = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700167 private static final boolean TEST_RADIOS = false;
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800168
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700169 private static final String TEST_RADIOS_PROP = "test.radios";
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800170 private static final String TEST_SUBSCRIBER_PROP = "test.subscriberid";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700171
Jeff Sharkey8a503642011-06-10 13:31:21 -0700172 private static final String TAB_3G = "3g";
173 private static final String TAB_4G = "4g";
174 private static final String TAB_MOBILE = "mobile";
175 private static final String TAB_WIFI = "wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700176 private static final String TAB_ETHERNET = "ethernet";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700177
Jeff Sharkey28130d92011-09-02 16:10:24 -0700178 private static final String TAG_CONFIRM_DATA_DISABLE = "confirmDataDisable";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700179 private static final String TAG_CONFIRM_LIMIT = "confirmLimit";
180 private static final String TAG_CYCLE_EDITOR = "cycleEditor";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700181 private static final String TAG_WARNING_EDITOR = "warningEditor";
182 private static final String TAG_LIMIT_EDITOR = "limitEditor";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700183 private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700184 private static final String TAG_DENIED_RESTRICT = "deniedRestrict";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700185 private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700186 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700187
Fabrice Di Meglio2169c882014-04-18 15:18:40 -0700188 private static final String DATA_USAGE_ENABLE_MOBILE_KEY = "data_usage_enable_mobile";
189 private static final String DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY =
190 "data_usage_disable_mobile_limit";
191 private static final String DATA_USAGE_CYCLE_KEY = "data_usage_cycle";
192
Jason Monk4bb075b2015-03-17 10:06:58 -0400193 public static final String EXTRA_SHOW_APP_IMMEDIATE_PKG = "showAppImmediatePkg";
194
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700195 private static final int LOADER_CHART_DATA = 2;
196 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700197
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700198 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700199 private INetworkStatsService mStatsService;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700200 private NetworkPolicyManager mPolicyManager;
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700201 private TelephonyManager mTelephonyManager;
Wink Saville0183fb52014-11-22 10:11:39 -0800202 private SubscriptionManager mSubscriptionManager;
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700203
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700204 private INetworkStatsSession mStatsSession;
205
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700206 private static final String PREF_FILE = "data_usage";
207 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700208 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700209
210 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700211
Jeff Sharkey8a503642011-06-10 13:31:21 -0700212 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700213 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700214 private TabWidget mTabWidget;
215 private ListView mListView;
PauloftheWestc80b7612014-11-25 04:40:45 -0800216 private ChartNetworkSeriesView mSeries;
217 private ChartNetworkSeriesView mDetailedSeries;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700218 private DataUsageAdapter mAdapter;
219
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700220 /** Distance to inset content from sides, when needed. */
221 private int mInsetSide = 0;
222
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700223 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700224
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700225 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700226 private LinearLayout mNetworkSwitches;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700227 private boolean mDataEnabledSupported;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700228 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700229 private View mDataEnabledView;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700230 private boolean mDisableAtLimitSupported;
231 private Switch mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700232 private View mDisableAtLimitView;
233
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700234 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700235 private Spinner mCycleSpinner;
236 private CycleAdapter mCycleAdapter;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700237 private TextView mCycleSummary;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700238
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700239 private ChartDataUsageView mChart;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700240 private View mDisclaimer;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700241 private TextView mEmpty;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700242 private View mStupidPadding;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700243
244 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700245 private ImageView mAppIcon;
246 private ViewGroup mAppTitles;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700247 private TextView mAppTotal;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700248 private TextView mAppForeground;
249 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700250 private Button mAppSettings;
251
252 private LinearLayout mAppSwitches;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700253 private Switch mAppRestrict;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700254 private View mAppRestrictView;
255
Jeff Sharkey8a503642011-06-10 13:31:21 -0700256 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700257 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700258
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700259 private NetworkTemplate mTemplate;
260 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700261
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700262 private AppItem mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700263
264 private Intent mAppSettingsIntent;
265
Jeff Sharkeya662e492011-06-18 21:57:06 -0700266 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700267
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700268 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700269 private String mIntentTab = null;
270
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700271 private MenuItem mMenuRestrictBackground;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700272 private MenuItem mMenuShowWifi;
273 private MenuItem mMenuShowEthernet;
274 private MenuItem mMenuSimCards;
275 private MenuItem mMenuCellularNetworks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700276
Wink Savilleca756612014-11-08 10:47:12 -0800277 private List<SubscriptionInfo> mSubInfoList;
Wink Savilleb003a852014-10-23 10:16:26 -0700278 private Map<Integer,String> mMobileTagMap;
PauloftheWest50e6eca2014-10-03 11:07:14 -0700279
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700280 /** Flag used to ignore listeners during binding. */
281 private boolean mBinding;
282
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700283 private UidDetailProvider mUidDetailProvider;
284
Jason Monk4bb075b2015-03-17 10:06:58 -0400285 // Indicates request to show app immediately rather than list.
286 private String mShowAppImmediatePkg;
287
Sanket Padawed819270f2015-01-14 11:03:33 -0800288 /**
289 * Local cache of data enabled for subId, used to work around delays.
290 */
291 private final Map<String, Boolean> mMobileDataEnabled = new HashMap<String, Boolean>();
292
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700293 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700294 public void onCreate(Bundle savedInstanceState) {
295 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700296 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700297
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700298 mNetworkService = INetworkManagementService.Stub.asInterface(
299 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700300 mStatsService = INetworkStatsService.Stub.asInterface(
301 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700302 mPolicyManager = NetworkPolicyManager.from(context);
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700303 mTelephonyManager = TelephonyManager.from(context);
Wink Saville0183fb52014-11-22 10:11:39 -0800304 mSubscriptionManager = SubscriptionManager.from(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700305
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700306 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700307
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700308 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700309 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700310
Wink Saville0183fb52014-11-22 10:11:39 -0800311 mSubInfoList = mSubscriptionManager.getActiveSubscriptionInfoList();
PauloftheWest50e6eca2014-10-03 11:07:14 -0700312 mMobileTagMap = initMobileTabTag(mSubInfoList);
313
Jaewan Kimffce9c12013-03-19 16:53:45 +0900314 try {
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700315 if (!mNetworkService.isBandwidthControlEnabled()) {
316 Log.w(TAG, "No bandwidth control; leaving");
317 getActivity().finish();
318 }
319 } catch (RemoteException e) {
320 Log.w(TAG, "No bandwidth control; leaving");
321 getActivity().finish();
322 }
323
324 try {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900325 mStatsSession = mStatsService.openSession();
326 } catch (RemoteException e) {
327 throw new RuntimeException(e);
328 }
329
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700330 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700331 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700332
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700333 // override preferences when no mobile radio
334 if (!hasReadyMobileRadio(context)) {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900335 mShowWifi = true;
336 mShowEthernet = true;
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700337 }
338
Jason Monk4bb075b2015-03-17 10:06:58 -0400339 mUidDetailProvider = new UidDetailProvider(context);
340
341 Bundle arguments = getArguments();
342 if (arguments != null) {
343 mShowAppImmediatePkg = arguments.getString(EXTRA_SHOW_APP_IMMEDIATE_PKG);
344 }
345
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700346 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700347 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700348
Jeff Sharkey8a503642011-06-10 13:31:21 -0700349 @Override
350 public View onCreateView(LayoutInflater inflater, ViewGroup container,
351 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700352
Jeff Sharkey8a503642011-06-10 13:31:21 -0700353 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700354 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
355
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700356
Jeff Sharkey8a503642011-06-10 13:31:21 -0700357 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700358 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700359 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
360 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700361
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700362 // decide if we need to manually inset our content, or if we should rely
363 // on parent container for inset.
364 final boolean shouldInset = mListView.getScrollBarStyle()
365 == View.SCROLLBARS_OUTSIDE_OVERLAY;
Amith Yamasani56f51a82013-08-05 10:07:23 -0700366 mInsetSide = 0;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700367
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700368 // adjust padding around tabwidget as needed
Amith Yamasani56f51a82013-08-05 10:07:23 -0700369 prepareCustomPreferencesList(container, view, mListView, false);
Jeff Sharkey5d706792011-09-08 18:57:17 -0700370
Jeff Sharkey8a503642011-06-10 13:31:21 -0700371 mTabHost.setup();
372 mTabHost.setOnTabChangedListener(mTabListener);
373
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700374 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700375 mHeader.setClickable(true);
376
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700377 mListView.addHeaderView(new View(context), null, true);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700378 mListView.addHeaderView(mHeader, null, true);
379 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700380
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700381 if (mInsetSide > 0) {
382 // inset selector and divider drawables
383 insetListViewDrawables(mListView, mInsetSide);
Fabrice Di Megliob27223f2013-01-15 18:54:11 -0800384 mHeader.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700385 }
386
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700387 {
388 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700389 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
390 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700391 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700392
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700393 mDataEnabled = new Switch(inflater.getContext());
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700394 mDataEnabled.setClickable(false);
395 mDataEnabled.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700396 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700397 mDataEnabledView.setTag(R.id.preference_highlight_key,
398 DATA_USAGE_ENABLE_MOBILE_KEY);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700399 mDataEnabledView.setClickable(true);
400 mDataEnabledView.setFocusable(true);
401 mDataEnabledView.setOnClickListener(mDataEnabledListener);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700402 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700403
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700404 mDisableAtLimit = new Switch(inflater.getContext());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700405 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700406 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700407 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700408 mDisableAtLimitView.setTag(R.id.preference_highlight_key,
409 DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700410 mDisableAtLimitView.setClickable(true);
411 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700412 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
413 mNetworkSwitches.addView(mDisableAtLimitView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700414
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700415 mCycleView = inflater.inflate(R.layout.data_usage_cycles, mNetworkSwitches, false);
416 mCycleView.setTag(R.id.preference_highlight_key, DATA_USAGE_CYCLE_KEY);
417 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
418 mCycleAdapter = new CycleAdapter(context);
419 mCycleSpinner.setAdapter(mCycleAdapter);
420 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
421 mCycleSummary = (TextView) mCycleView.findViewById(R.id.cycle_summary);
422 mNetworkSwitches.addView(mCycleView);
PauloftheWestc80b7612014-11-25 04:40:45 -0800423 mSeries = (ChartNetworkSeriesView)view.findViewById(R.id.series);
424 mDetailedSeries = (ChartNetworkSeriesView)view.findViewById(R.id.detail_series);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700425 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700426
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700427 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700428 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800429 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700430
431 {
432 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700433 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700434 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
435 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700436 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
437 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700438 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700439
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700440 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700441
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700442 mAppRestrict = new Switch(inflater.getContext());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700443 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700444 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700445 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700446 mAppRestrictView.setClickable(true);
447 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700448 mAppRestrictView.setOnClickListener(mAppRestrictListener);
449 mAppSwitches.addView(mAppRestrictView);
450 }
451
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700452 mDisclaimer = mHeader.findViewById(R.id.disclaimer);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700453 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700454 mStupidPadding = mHeader.findViewById(R.id.stupid_padding);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700455
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +0100456 final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
457 mAdapter = new DataUsageAdapter(um, mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700458 mListView.setOnItemClickListener(mListListener);
459 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700460
Jason Monk4bb075b2015-03-17 10:06:58 -0400461 showRequestedAppIfNeeded();
462
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700463 return view;
464 }
465
Jason Monk4bb075b2015-03-17 10:06:58 -0400466 private void showRequestedAppIfNeeded() {
467 if (mShowAppImmediatePkg == null) {
468 return;
469 }
470 try {
471 int uid = getActivity().getPackageManager().getPackageUid(mShowAppImmediatePkg,
472 UserHandle.myUserId());
473 AppItem app = new AppItem(uid);
474 app.addUid(uid);
475
476 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
477 // When we are going straight to an app then we are coming from App Info and want
478 // a header at the top.
479 AppHeader.createAppHeader(getActivity(), detail.icon, detail.label, null);
480 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label, false);
481 } catch (NameNotFoundException e) {
482 Log.w(TAG, "Could not find " + mShowAppImmediatePkg, e);
483 Toast.makeText(getActivity(), getString(R.string.unknown_app), Toast.LENGTH_LONG)
484 .show();
485 getActivity().finish();
486 }
487 }
488
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700489 @Override
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700490 public void onViewStateRestored(Bundle savedInstanceState) {
491 super.onViewStateRestored(savedInstanceState);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700492
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700493 // pick default tab based on incoming intent
494 final Intent intent = getActivity().getIntent();
495 mIntentTab = computeTabFromIntent(intent);
496
Jeff Sharkey8a503642011-06-10 13:31:21 -0700497 // this kicks off chain reaction which creates tabs, binds the body to
498 // selected network, and binds chart, cycles and detail list.
499 updateTabs();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700500 }
501
502 @Override
503 public void onResume() {
504 super.onResume();
505
506 getView().post(new Runnable() {
507 @Override
508 public void run() {
509 highlightViewIfNeeded();
510 }
511 });
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700512
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700513 // kick off background task to update stats
514 new AsyncTask<Void, Void, Void>() {
515 @Override
516 protected Void doInBackground(Void... params) {
517 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700518 // wait a few seconds before kicking off
519 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700520 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700521 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700522 } catch (RemoteException e) {
523 }
524 return null;
525 }
526
527 @Override
528 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700529 if (isAdded()) {
530 updateBody();
531 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700532 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700533 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700534 }
535
Jeff Sharkey8a503642011-06-10 13:31:21 -0700536 @Override
537 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
538 inflater.inflate(R.menu.data_usage, menu);
539 }
540
541 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700542 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700543 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700544 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700545 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700546
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700547 mMenuShowWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700548 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
549 mMenuShowWifi.setVisible(!appDetailMode);
550 } else {
551 mMenuShowWifi.setVisible(false);
552 }
553
554 mMenuShowEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
555 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
556 mMenuShowEthernet.setVisible(!appDetailMode);
557 } else {
558 mMenuShowEthernet.setVisible(false);
559 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700560
561 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700562 mMenuRestrictBackground.setVisible(
563 hasReadyMobileRadio(context) && isOwner && !appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700564
565 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
566 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700567 metered.setVisible(!appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700568 } else {
569 metered.setVisible(false);
570 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700571
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700572 // TODO: show when multiple sims available
573 mMenuSimCards = menu.findItem(R.id.data_usage_menu_sim_cards);
574 mMenuSimCards.setVisible(false);
575
576 mMenuCellularNetworks = menu.findItem(R.id.data_usage_menu_cellular_networks);
PauloftheWesta4b8fb32014-09-18 10:12:25 -0700577 mMenuCellularNetworks.setVisible(hasReadyMobileRadio(context)
578 && !appDetailMode && isOwner);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700579
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700580 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
581 String helpUrl;
582 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800583 HelpUtils.prepareHelpMenuItem(context, help, helpUrl);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700584 } else {
585 help.setVisible(false);
586 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700587
588 updateMenuTitles();
589 }
590
591 private void updateMenuTitles() {
592 if (mPolicyManager.getRestrictBackground()) {
593 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_allow_background);
594 } else {
595 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_restrict_background);
596 }
597
598 if (mShowWifi) {
599 mMenuShowWifi.setTitle(R.string.data_usage_menu_hide_wifi);
600 } else {
601 mMenuShowWifi.setTitle(R.string.data_usage_menu_show_wifi);
602 }
603
604 if (mShowEthernet) {
605 mMenuShowEthernet.setTitle(R.string.data_usage_menu_hide_ethernet);
606 } else {
607 mMenuShowEthernet.setTitle(R.string.data_usage_menu_show_ethernet);
608 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700609 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700610
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700611 @Override
612 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700613 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700614 case R.id.data_usage_menu_restrict_background: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700615 final boolean restrictBackground = !mPolicyManager.getRestrictBackground();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700616 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800617 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700618 } else {
619 // no confirmation to drop restriction
620 setRestrictBackground(false);
621 }
622 return true;
623 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700624 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700625 mShowWifi = !mShowWifi;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700626 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700627 updateMenuTitles();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700628 updateTabs();
629 return true;
630 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700631 case R.id.data_usage_menu_show_ethernet: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700632 mShowEthernet = !mShowEthernet;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700633 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700634 updateMenuTitles();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700635 updateTabs();
636 return true;
637 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700638 case R.id.data_usage_menu_sim_cards: {
639 // TODO: hook up to sim cards
640 return true;
641 }
642 case R.id.data_usage_menu_cellular_networks: {
643 final Intent intent = new Intent(Intent.ACTION_MAIN);
644 intent.setComponent(new ComponentName("com.android.phone",
645 "com.android.phone.MobileNetworkSettings"));
646 startActivity(intent);
647 return true;
648 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700649 case R.id.data_usage_menu_metered: {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800650 final SettingsActivity sa = (SettingsActivity) getActivity();
651 sa.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700652 R.string.data_usage_metered_title, null, this, 0);
653 return true;
654 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700655 }
656 return false;
657 }
658
Jeff Sharkey94a90952011-06-13 22:31:09 -0700659 @Override
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700660 public void onDestroy() {
Jeff Sharkey94a90952011-06-13 22:31:09 -0700661 mDataEnabledView = null;
662 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700663
664 mUidDetailProvider.clearCache();
665 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700666
667 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700668
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800669 super.onDestroy();
670 }
671
Jeff Sharkey8a503642011-06-10 13:31:21 -0700672 /**
Jeff Sharkey92811822012-05-04 11:47:29 -0700673 * Build and assign {@link LayoutTransition} to various containers. Should
674 * only be assigned after initial layout is complete.
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700675 */
Jeff Sharkey92811822012-05-04 11:47:29 -0700676 private void ensureLayoutTransitions() {
Jason Monk4bb075b2015-03-17 10:06:58 -0400677 if (mShowAppImmediatePkg != null) {
678 // If we are skipping right to showing an app, we don't care about transitions.
679 return;
680 }
Jeff Sharkey92811822012-05-04 11:47:29 -0700681 // skip when already setup
682 if (mChart.getLayoutTransition() != null) return;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700683
Jeff Sharkey92811822012-05-04 11:47:29 -0700684 mTabsContainer.setLayoutTransition(buildLayoutTransition());
685 mHeader.setLayoutTransition(buildLayoutTransition());
686 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700687
Jeff Sharkey92811822012-05-04 11:47:29 -0700688 final LayoutTransition chartTransition = buildLayoutTransition();
689 chartTransition.disableTransitionType(LayoutTransition.APPEARING);
690 chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
691 mChart.setLayoutTransition(chartTransition);
692 }
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700693
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700694 private static LayoutTransition buildLayoutTransition() {
695 final LayoutTransition transition = new LayoutTransition();
696 if (TEST_ANIM) {
697 transition.setDuration(1500);
698 }
699 transition.setAnimateParentHierarchy(false);
700 return transition;
701 }
702
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700703 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700704 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700705 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
706 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700707 */
708 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700709 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700710 mTabHost.clearAllTabs();
711
Sanket Padawe7e21fa52015-02-12 12:39:34 -0800712 int simCount = mTelephonyManager.getSimCount();
713
714 for (int i = 0; i < simCount; i++) {
PauloftheWestc80b7612014-11-25 04:40:45 -0800715 final SubscriptionInfo sir = Utils.findRecordBySlotId(context, i);
716 if (sir != null) {
Sanket Padawe7e21fa52015-02-12 12:39:34 -0800717 addMobileTab(context, sir, (simCount > 1));
PauloftheWestc80b7612014-11-25 04:40:45 -0800718 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700719 }
PauloftheWestc80b7612014-11-25 04:40:45 -0800720
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700721 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700722 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
723 }
PauloftheWestc80b7612014-11-25 04:40:45 -0800724
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700725 if (mShowEthernet && hasEthernet(context)) {
726 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
727 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700728
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700729 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700730 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
731 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
732 if (mIntentTab != null) {
733 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700734 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700735 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700736 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700737 mTabHost.setCurrentTabByTag(mIntentTab);
738 }
739 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700740 } else if (noTabs) {
741 // no usable tabs, so hide body
742 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700743 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700744 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700745 }
746 }
747
Jeff Sharkey8a503642011-06-10 13:31:21 -0700748 /**
749 * Factory that provide empty {@link View} to make {@link TabHost} happy.
750 */
751 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700752 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700753 public View createTabContent(String tag) {
754 return new View(mTabHost.getContext());
755 }
756 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700757
Jeff Sharkey8a503642011-06-10 13:31:21 -0700758 /**
759 * Build {@link TabSpec} with thin indicator, and empty content.
760 */
761 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700762 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
763 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700764 }
765
PauloftheWest50e6eca2014-10-03 11:07:14 -0700766 /**
767 * Build {@link TabSpec} with thin indicator, and empty content.
768 */
Stuart Scottb1531812014-11-04 14:52:23 -0800769 private TabSpec buildTabSpec(String tag, CharSequence title) {
PauloftheWest50e6eca2014-10-03 11:07:14 -0700770 return mTabHost.newTabSpec(tag).setIndicator(title).setContent(
771 mEmptyTabContent);
772 }
773
774
Jeff Sharkey8a503642011-06-10 13:31:21 -0700775 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700776 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700777 public void onTabChanged(String tabId) {
778 // user changed tab; update body
779 updateBody();
780 }
781 };
782
783 /**
784 * Update body content based on current tab. Loads
785 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
786 * binds them to visible controls.
787 */
788 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700789 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700790 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700791
Jeff Sharkeya662e492011-06-18 21:57:06 -0700792 final Context context = getActivity();
PauloftheWestc80b7612014-11-25 04:40:45 -0800793 final Resources resources = context.getResources();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700794 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700795 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700796
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700797 if (currentTab == null) {
798 Log.w(TAG, "no tab selected; hiding body");
799 mListView.setVisibility(View.GONE);
800 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700801 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700802 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700803 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700804
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700805 mCurrentTab = currentTab;
806
Jeff Sharkey8a503642011-06-10 13:31:21 -0700807 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
808
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700809 mDataEnabledSupported = isOwner;
810 mDisableAtLimitSupported = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700811
Wink Savillefcec91f2014-12-02 17:12:08 -0800812 // TODO: remove mobile tabs when SIM isn't ready probably by
813 // TODO: using SubscriptionManager.getActiveSubscriptionInfoList.
814 if (LOGD) Log.d(TAG, "updateBody() isMobileTab=" + isMobileTab(currentTab));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700815
PauloftheWest50e6eca2014-10-03 11:07:14 -0700816 if (isMobileTab(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800817 if (LOGD) Log.d(TAG, "updateBody() mobile tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700818 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
819 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
PauloftheWest50e6eca2014-10-03 11:07:14 -0700820 mDataEnabledSupported = isMobileDataAvailable(getSubId(currentTab));
Jeff Sharkey4e2d16c2014-12-03 13:44:26 -0800821
822 // Match mobile traffic for this subscriber, but normalize it to
823 // catch any other merged subscribers.
824 mTemplate = buildTemplateMobileAll(
825 getActiveSubscriberId(context, getSubId(currentTab)));
826 mTemplate = NetworkTemplate.normalize(mTemplate,
827 mTelephonyManager.getMergedSubscriberIds());
828
Jeff Sharkey8a503642011-06-10 13:31:21 -0700829 } else if (TAB_3G.equals(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800830 if (LOGD) Log.d(TAG, "updateBody() 3g tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700831 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
832 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
833 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700834 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700835
836 } else if (TAB_4G.equals(currentTab)) {
Wink Savillefcec91f2014-12-02 17:12:08 -0800837 if (LOGD) Log.d(TAG, "updateBody() 4g tab");
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700838 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
839 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
840 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700841 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700842
843 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700844 // wifi doesn't have any controls
Wink Savillefcec91f2014-12-02 17:12:08 -0800845 if (LOGD) Log.d(TAG, "updateBody() wifi tab");
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700846 mDataEnabledSupported = false;
847 mDisableAtLimitSupported = false;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700848 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700849
850 } else if (TAB_ETHERNET.equals(currentTab)) {
851 // ethernet doesn't have any controls
Wink Savillefcec91f2014-12-02 17:12:08 -0800852 if (LOGD) Log.d(TAG, "updateBody() ethernet tab");
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700853 mDataEnabledSupported = false;
854 mDisableAtLimitSupported = false;
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700855 mTemplate = buildTemplateEthernet();
856
857 } else {
Wink Savillefcec91f2014-12-02 17:12:08 -0800858 if (LOGD) Log.d(TAG, "updateBody() unknown tab");
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700859 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700860 }
861
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700862 // kick off loader for network history
863 // TODO: consider chaining two loaders together instead of reloading
864 // network history when showing app detail.
865 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700866 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700867
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700868 // detail mode can change visible menus, invalidate
869 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700870
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700871 mBinding = false;
PauloftheWestc80b7612014-11-25 04:40:45 -0800872
Alan Viverette55eaa962015-03-18 18:10:52 -0700873 int seriesColor = context.getColor(R.color.sim_noitification);
PauloftheWestc80b7612014-11-25 04:40:45 -0800874 if (mCurrentTab != null && mCurrentTab.length() > TAB_MOBILE.length() ){
875 final int slotId = Integer.parseInt(mCurrentTab.substring(TAB_MOBILE.length(),
876 mCurrentTab.length()));
877 final SubscriptionInfo sir = com.android.settings.Utils.findRecordBySlotId(context,
878 slotId);
879
880 if (sir != null) {
881 seriesColor = sir.getIconTint();
882 }
883 }
884
Jeff Sharkey9c167e92015-01-26 15:15:59 -0700885 final int secondaryColor = Color.argb(127, Color.red(seriesColor), Color.green(seriesColor),
886 Color.blue(seriesColor));
887 mSeries.setChartColor(Color.BLACK, seriesColor, secondaryColor);
888 mDetailedSeries.setChartColor(Color.BLACK, seriesColor, secondaryColor);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700889 }
890
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700891 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700892 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700893 }
894
895 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700896 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700897 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700898 */
899 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700900 final Context context = getActivity();
901 final PackageManager pm = context.getPackageManager();
902 final LayoutInflater inflater = getActivity().getLayoutInflater();
903
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700904 if (isAppDetailMode()) {
905 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700906 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700907 } else {
908 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700909 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700910
911 // hide detail stats when not in detail mode
912 mChart.bindDetailNetworkStats(null);
913 return;
914 }
915
916 // remove warning/limit sweeps while in detail mode
917 mChart.bindNetworkPolicy(null);
918
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700919 // show icon and all labels appearing under this app
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700920 final int uid = mCurrentApp.key;
921 final UidDetail detail = mUidDetailProvider.getUidDetail(uid, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700922 mAppIcon.setImageDrawable(detail.icon);
923
924 mAppTitles.removeAllViews();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700925
926 View title = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700927 if (detail.detailLabels != null) {
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100928 final int n = detail.detailLabels.length;
929 for (int i = 0; i < n; ++i) {
930 CharSequence label = detail.detailLabels[i];
931 CharSequence contentDescription = detail.detailContentDescriptions[i];
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700932 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100933 TextView appTitle = (TextView) title.findViewById(R.id.app_title);
934 appTitle.setText(label);
935 appTitle.setContentDescription(contentDescription);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700936 mAppTitles.addView(title);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700937 }
938 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700939 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100940 TextView appTitle = (TextView) title.findViewById(R.id.app_title);
941 appTitle.setText(detail.label);
942 appTitle.setContentDescription(detail.contentDescription);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700943 mAppTitles.addView(title);
944 }
945
946 // Remember last slot for summary
947 if (title != null) {
948 mAppTotal = (TextView) title.findViewById(R.id.app_summary);
949 } else {
950 mAppTotal = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700951 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700952
953 // enable settings button when package provides it
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700954 final String[] packageNames = pm.getPackagesForUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700955 if (packageNames != null && packageNames.length > 0) {
956 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700957 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
958
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700959 // Search for match across all packages
960 boolean matchFound = false;
961 for (String packageName : packageNames) {
962 mAppSettingsIntent.setPackage(packageName);
963 if (pm.resolveActivity(mAppSettingsIntent, 0) != null) {
964 matchFound = true;
965 break;
966 }
967 }
968
Zoltan Szatmary-Banf20d39e2014-08-07 15:27:08 +0100969 mAppSettings.setOnClickListener(new OnClickListener() {
970 @Override
971 public void onClick(View v) {
972 if (!isAdded()) {
973 return;
974 }
975
976 // TODO: target towards entire UID instead of just first package
977 getActivity().startActivityAsUser(mAppSettingsIntent,
978 new UserHandle(UserHandle.getUserId(uid)));
979 }
980 });
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700981 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -0700982 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700983
984 } else {
985 mAppSettingsIntent = null;
Zoltan Szatmary-Banf20d39e2014-08-07 15:27:08 +0100986 mAppSettings.setOnClickListener(null);
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700987 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700988 }
989
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700990 updateDetailData();
991
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700992 if (UserHandle.isApp(uid) && !mPolicyManager.getRestrictBackground()
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700993 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700994 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800995 setPreferenceSummary(mAppRestrictView,
996 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700997
998 mAppRestrictView.setVisibility(View.VISIBLE);
999 mAppRestrict.setChecked(getAppRestrictBackground());
1000
1001 } else {
1002 mAppRestrictView.setVisibility(View.GONE);
1003 }
1004 }
1005
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001006 private void setPolicyWarningBytes(long warningBytes) {
1007 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -07001008 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001009 updatePolicy(false);
1010 }
1011
1012 private void setPolicyLimitBytes(long limitBytes) {
1013 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -07001014 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001015 updatePolicy(false);
1016 }
1017
Wink Savilleb003a852014-10-23 10:16:26 -07001018 private boolean isMobileDataEnabled(int subId) {
Wink Savillea4f4d182014-12-05 15:34:46 -08001019 if (LOGD) Log.d(TAG, "isMobileDataEnabled:+ subId=" + subId);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001020 boolean isEnable = false;
Sanket Padawed819270f2015-01-14 11:03:33 -08001021 if (mMobileDataEnabled.get(String.valueOf(subId)) != null) {
1022 //TODO: deprecate and remove this once enabled flag is on policy
1023 //Multiple Subscriptions, the value need to be reseted
1024 isEnable = mMobileDataEnabled.get(String.valueOf(subId)).booleanValue();
Wink Savillea4f4d182014-12-05 15:34:46 -08001025 if (LOGD) {
1026 Log.d(TAG, "isMobileDataEnabled: != null, subId=" + subId
1027 + " isEnable=" + isEnable);
1028 }
Sanket Padawed819270f2015-01-14 11:03:33 -08001029 mMobileDataEnabled.put(String.valueOf(subId), null);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001030 } else {
Wink Savillea4f4d182014-12-05 15:34:46 -08001031 // SUB SELECT
1032 isEnable = mTelephonyManager.getDataEnabled(subId);
1033 if (LOGD) {
1034 Log.d(TAG, "isMobileDataEnabled: == null, subId=" + subId
1035 + " isEnable=" + isEnable);
1036 }
Jeff Sharkey28130d92011-09-02 16:10:24 -07001037 }
PauloftheWest50e6eca2014-10-03 11:07:14 -07001038 return isEnable;
Jeff Sharkey28130d92011-09-02 16:10:24 -07001039 }
1040
Sanket Padawe24f834d2015-01-08 11:23:11 -08001041 private void setMobileDataEnabled(int subId, boolean enabled) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001042 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
Sanket Padawe24f834d2015-01-08 11:23:11 -08001043 mTelephonyManager.setDataEnabled(subId, enabled);
Sanket Padawed819270f2015-01-14 11:03:33 -08001044 mMobileDataEnabled.put(String.valueOf(subId), enabled);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001045 updatePolicy(false);
1046 }
1047
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001048 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001049 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked()
1050 && ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001051 }
1052
1053 private boolean isBandwidthControlEnabled() {
1054 try {
1055 return mNetworkService.isBandwidthControlEnabled();
1056 } catch (RemoteException e) {
1057 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
1058 return false;
1059 }
1060 }
1061
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001062 public void setRestrictBackground(boolean restrictBackground) {
1063 mPolicyManager.setRestrictBackground(restrictBackground);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001064 updateMenuTitles();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001065 }
1066
1067 private boolean getAppRestrictBackground() {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001068 final int uid = mCurrentApp.key;
1069 final int uidPolicy = mPolicyManager.getUidPolicy(uid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001070 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
1071 }
1072
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001073 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001074 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001075 final int uid = mCurrentApp.key;
1076 mPolicyManager.setUidPolicy(
1077 uid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001078 mAppRestrict.setChecked(restrictBackground);
1079 }
1080
Jeff Sharkey8a503642011-06-10 13:31:21 -07001081 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001082 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
1083 * current {@link #mTemplate}.
1084 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001085 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001086 boolean dataEnabledVisible = mDataEnabledSupported;
1087 boolean disableAtLimitVisible = mDisableAtLimitSupported;
1088
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001089 if (isAppDetailMode()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001090 dataEnabledVisible = false;
1091 disableAtLimitVisible = false;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001092 }
1093
Jeff Sharkey28130d92011-09-02 16:10:24 -07001094 // TODO: move enabled state directly into policy
PauloftheWest50e6eca2014-10-03 11:07:14 -07001095 if (isMobileTab(mCurrentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001096 mBinding = true;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001097 mDataEnabled.setChecked(isMobileDataEnabled(getSubId(mCurrentTab)));
Jeff Sharkey28130d92011-09-02 16:10:24 -07001098 mBinding = false;
1099 }
1100
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001101 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001102 //SUB SELECT
1103 if (isNetworkPolicyModifiable(policy) && isMobileDataAvailable(getSubId(mCurrentTab))) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001104 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001105 if (!isAppDetailMode()) {
1106 mChart.bindNetworkPolicy(policy);
1107 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001108
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001109 } else {
1110 // controls are disabled; don't bind warning/limit sweeps
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001111 disableAtLimitVisible = false;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001112 mChart.bindNetworkPolicy(null);
1113 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001114
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001115 mDataEnabledView.setVisibility(dataEnabledVisible ? View.VISIBLE : View.GONE);
1116 mDisableAtLimitView.setVisibility(disableAtLimitVisible ? View.VISIBLE : View.GONE);
1117
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001118 if (refreshCycle) {
1119 // generate cycle list based on policy and available history
1120 updateCycleList(policy);
1121 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001122 }
1123
1124 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001125 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
1126 * and available {@link NetworkStatsHistory} data. Always selects the newest
1127 * item, updating the inspection range on {@link #mChart}.
1128 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001129 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001130 // stash away currently selected cycle to try restoring below
1131 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -07001132 mCycleAdapter.clear();
1133
1134 final Context context = mCycleSpinner.getContext();
1135
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001136 long historyStart = Long.MAX_VALUE;
1137 long historyEnd = Long.MIN_VALUE;
1138 if (mChartData != null) {
1139 historyStart = mChartData.network.getStart();
1140 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001141 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001142
Jeff Sharkey461842a2011-09-25 18:22:48 -07001143 final long now = System.currentTimeMillis();
1144 if (historyStart == Long.MAX_VALUE) historyStart = now;
1145 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001146
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001147 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001148 if (policy != null) {
1149 // find the next cycle boundary
1150 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001151
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001152 // walk backwards, generating all valid cycle ranges
1153 while (cycleEnd > historyStart) {
1154 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
1155 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
1156 + historyStart);
1157 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1158 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001159 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001160 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001161
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001162 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001163 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001164 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001165
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001166 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001167 // no policy defined cycles; show entry for each four-week period
1168 long cycleEnd = historyEnd;
1169 while (cycleEnd > historyStart) {
1170 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
1171 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1172 cycleEnd = cycleStart;
1173 }
1174
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001175 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001176 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001177
1178 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001179 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001180 final int position = mCycleAdapter.findNearestPosition(previousItem);
1181 mCycleSpinner.setSelection(position);
1182
1183 // only force-update cycle when changed; skipping preserves any
1184 // user-defined inspection region.
1185 final CycleItem selectedItem = mCycleAdapter.getItem(position);
1186 if (!Objects.equal(selectedItem, previousItem)) {
1187 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1188 } else {
1189 // but still kick off loader for detailed list
1190 updateDetailData();
1191 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001192 } else {
1193 updateDetailData();
1194 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001195 }
1196
Sanket Padawed819270f2015-01-14 11:03:33 -08001197 private void disableDataForOtherSubscriptions(SubscriptionInfo currentSir) {
1198 if (mSubInfoList != null) {
1199 for (SubscriptionInfo subInfo : mSubInfoList) {
1200 if (subInfo.getSubscriptionId() != currentSir.getSubscriptionId()) {
1201 setMobileDataEnabled(subInfo.getSubscriptionId(), false);
1202 }
1203 }
1204 }
1205 }
1206
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001207 private View.OnClickListener mDataEnabledListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001208 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001209 public void onClick(View v) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001210 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001211
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001212 final boolean dataEnabled = !mDataEnabled.isChecked();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001213 final String currentTab = mCurrentTab;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001214 if (isMobileTab(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001215 if (dataEnabled) {
PauloftheWestde338442014-10-29 14:46:44 -07001216 // If we are showing the Sim Card tile then we are a Multi-Sim device.
1217 if (Utils.showSimCardTile(getActivity())) {
1218 handleMultiSimDataDialog();
1219 } else {
Sanket Padawe24f834d2015-01-08 11:23:11 -08001220 setMobileDataEnabled(getSubId(currentTab), true);
PauloftheWestde338442014-10-29 14:46:44 -07001221 }
Jeff Sharkey28130d92011-09-02 16:10:24 -07001222 } else {
1223 // disabling data; show confirmation dialog which eventually
1224 // calls setMobileDataEnabled() once user confirms.
Sanket Padawe24f834d2015-01-08 11:23:11 -08001225 ConfirmDataDisableFragment.show(DataUsageSummary.this, getSubId(mCurrentTab));
Jeff Sharkey28130d92011-09-02 16:10:24 -07001226 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001227 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001228
Jeff Sharkey28130d92011-09-02 16:10:24 -07001229 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001230 }
1231 };
1232
PauloftheWestde338442014-10-29 14:46:44 -07001233 private void handleMultiSimDataDialog() {
PauloftheWestf0d84e22014-12-10 08:50:54 -08001234 final Context context = getActivity();
1235 final SubscriptionInfo currentSir = getCurrentTabSubInfo(context);
Sanket Padawe766ab602015-01-20 10:36:15 -08001236
1237 //If sim has not loaded after toggling data switch, return.
1238 if (currentSir == null) {
1239 return;
1240 }
1241
Wink Saville0183fb52014-11-22 10:11:39 -08001242 final SubscriptionInfo nextSir = mSubscriptionManager.getActiveSubscriptionInfo(
1243 mSubscriptionManager.getDefaultDataSubId());
PauloftheWestde338442014-10-29 14:46:44 -07001244
PauloftheWestf0d84e22014-12-10 08:50:54 -08001245 // If the device is single SIM or is enabling data on the active data SIM then forgo
1246 // the pop-up.
1247 if (!Utils.showSimCardTile(context) ||
Sanket Padawed819270f2015-01-14 11:03:33 -08001248 (nextSir != null && currentSir != null &&
1249 currentSir.getSubscriptionId() == nextSir.getSubscriptionId())) {
Sanket Padawe24f834d2015-01-08 11:23:11 -08001250 setMobileDataEnabled(currentSir.getSubscriptionId(), true);
Sanket Padawed819270f2015-01-14 11:03:33 -08001251 if (nextSir != null && currentSir != null &&
1252 currentSir.getSubscriptionId() == nextSir.getSubscriptionId()) {
1253 disableDataForOtherSubscriptions(currentSir);
1254 }
PauloftheWestde338442014-10-29 14:46:44 -07001255 updateBody();
1256 return;
1257 }
1258
PauloftheWestf0d84e22014-12-10 08:50:54 -08001259 final String previousName = (nextSir == null)
1260 ? context.getResources().getString(R.string.sim_selection_required_pref)
1261 : nextSir.getDisplayName().toString();
1262
PauloftheWestde338442014-10-29 14:46:44 -07001263 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
1264
1265 builder.setTitle(R.string.sim_change_data_title);
1266 builder.setMessage(getActivity().getResources().getString(R.string.sim_change_data_message,
PauloftheWestf0d84e22014-12-10 08:50:54 -08001267 currentSir.getDisplayName(), previousName));
PauloftheWestde338442014-10-29 14:46:44 -07001268
1269 builder.setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
1270 @Override
1271 public void onClick(DialogInterface dialog, int id) {
Wink Saville0183fb52014-11-22 10:11:39 -08001272 mSubscriptionManager.setDefaultDataSubId(currentSir.getSubscriptionId());
Sanket Padawe24f834d2015-01-08 11:23:11 -08001273 setMobileDataEnabled(currentSir.getSubscriptionId(), true);
Sanket Padawed819270f2015-01-14 11:03:33 -08001274 disableDataForOtherSubscriptions(currentSir);
PauloftheWestde338442014-10-29 14:46:44 -07001275 updateBody();
1276 }
1277 });
1278 builder.setNegativeButton(R.string.cancel, null);
1279
1280 builder.create().show();
1281 }
1282
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001283 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001284 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001285 public void onClick(View v) {
1286 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001287 if (disableAtLimit) {
1288 // enabling limit; show confirmation dialog which eventually
1289 // calls setPolicyLimitBytes() once user confirms.
1290 ConfirmLimitFragment.show(DataUsageSummary.this);
1291 } else {
1292 setPolicyLimitBytes(LIMIT_DISABLED);
1293 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001294 }
1295 };
1296
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001297 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001298 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001299 public void onClick(View v) {
1300 final boolean restrictBackground = !mAppRestrict.isChecked();
1301
1302 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001303 // enabling restriction; show confirmation dialog which
1304 // eventually calls setRestrictBackground() once user
1305 // confirms.
1306 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001307 } else {
1308 setAppRestrictBackground(false);
1309 }
1310 }
1311 };
1312
Jeff Sharkey8a503642011-06-10 13:31:21 -07001313 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001314 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001315 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001316 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001317 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001318
1319 // TODO: sigh, remove this hack once we understand 6450986
1320 if (mUidDetailProvider == null || app == null) return;
1321
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001322 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001323 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001324 }
1325 };
1326
1327 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001328 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001329 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1330 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1331 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001332 // show cycle editor; will eventually call setPolicyCycleDay()
1333 // when user finishes editing.
1334 CycleEditorFragment.show(DataUsageSummary.this);
1335
1336 // reset spinner to something other than "change cycle..."
1337 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001338
1339 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001340 if (LOGD) {
1341 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1342 + cycle.end + "]");
1343 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001344
1345 // update chart to show selected cycle, and update detail data
1346 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001347 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001348
1349 updateDetailData();
1350 }
1351 }
1352
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001353 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001354 public void onNothingSelected(AdapterView<?> parent) {
1355 // ignored
1356 }
1357 };
1358
1359 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001360 * Update details based on {@link #mChart} inspection range depending on
1361 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1362 * of applications data usage, and when {@link #isAppDetailMode()} update
1363 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001364 */
1365 private void updateDetailData() {
1366 if (LOGD) Log.d(TAG, "updateDetailData()");
1367
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001368 final long start = mChart.getInspectStart();
1369 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001370 final long now = System.currentTimeMillis();
1371
1372 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001373
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001374 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001375 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001376 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001377 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001378 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001379 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001380 final long foregroundBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001381 final long totalBytes = defaultBytes + foregroundBytes;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001382
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001383 if (mAppTotal != null) {
1384 mAppTotal.setText(Formatter.formatFileSize(context, totalBytes));
1385 }
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001386 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1387 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1388
1389 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001390 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001391
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001392 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001393
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001394 mCycleSummary.setVisibility(View.GONE);
1395
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001396 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001397 if (mChartData != null) {
1398 entry = mChartData.network.getValues(start, end, now, null);
1399 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001400
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001401 mCycleSummary.setVisibility(View.VISIBLE);
1402
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001403 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001404 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001405 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001406 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001407
1408 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1409 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001410 mCycleSummary.setText(totalPhrase);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001411
PauloftheWest50e6eca2014-10-03 11:07:14 -07001412 if (isMobileTab(mCurrentTab) || TAB_3G.equals(mCurrentTab)
László Dávid0b8bf4e2012-10-24 23:31:47 +02001413 || TAB_4G.equals(mCurrentTab)) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001414 if (isAppDetailMode()) {
1415 mDisclaimer.setVisibility(View.GONE);
1416 } else {
1417 mDisclaimer.setVisibility(View.VISIBLE);
1418 }
Jeff Sharkeye557c332012-04-13 16:04:07 -07001419 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001420 mDisclaimer.setVisibility(View.GONE);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001421 }
1422
Jeff Sharkey92811822012-05-04 11:47:29 -07001423 // initial layout is finished above, ensure we have transitions
1424 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001425 }
1426
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001427 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1428 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001429 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001430 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001431 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001432 }
1433
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001434 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001435 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1436 mChartData = data;
1437 mChart.bindNetworkStats(mChartData.network);
1438 mChart.bindDetailNetworkStats(mChartData.detail);
1439
1440 // calcuate policy cycles based on available data
1441 updatePolicy(true);
1442 updateAppDetail();
1443
1444 // force scroll to top of body when showing detail
1445 if (mChartData.detail != null) {
1446 mListView.smoothScrollToPosition(0);
1447 }
1448 }
1449
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001450 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001451 public void onLoaderReset(Loader<ChartData> loader) {
1452 mChartData = null;
1453 mChart.bindNetworkStats(null);
1454 mChart.bindDetailNetworkStats(null);
1455 }
1456 };
1457
1458 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001459 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001460 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001461 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001462 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001463 }
1464
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001465 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001466 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001467 final int[] restrictedUids = mPolicyManager.getUidsWithPolicy(
Jeff Sharkeye557c332012-04-13 16:04:07 -07001468 POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001469 mAdapter.bindStats(data, restrictedUids);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001470 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001471 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001472
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001473 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001474 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001475 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001476 updateEmptyVisible();
1477 }
1478
1479 private void updateEmptyVisible() {
1480 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1481 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001482 mStupidPadding.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001483 }
1484 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001485
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001486 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001487 final TelephonyManager tele = TelephonyManager.from(context);
1488 final String actualSubscriberId = tele.getSubscriberId();
Wink Savillefcec91f2014-12-02 17:12:08 -08001489 String retVal = SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
1490 if (LOGD) Log.d(TAG, "getActiveSubscriberId=" + retVal + " actualSubscriberId=" + actualSubscriberId);
1491 return retVal;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001492 }
1493
Wink Savilleb003a852014-10-23 10:16:26 -07001494 private static String getActiveSubscriberId(Context context, int subId) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07001495 final TelephonyManager tele = TelephonyManager.from(context);
Wink Savillefcec91f2014-12-02 17:12:08 -08001496 String retVal = tele.getSubscriberId(subId);
1497 if (LOGD) Log.d(TAG, "getActiveSubscriberId=" + retVal + " subId=" + subId);
1498 return retVal;
PauloftheWest50e6eca2014-10-03 11:07:14 -07001499 }
1500
Jeff Sharkey8a503642011-06-10 13:31:21 -07001501 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001502 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001503 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001504 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001505 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001506
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001507 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001508 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001509 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001510 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001511
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001512 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001513 public void requestWarningEdit() {
1514 WarningEditorFragment.show(DataUsageSummary.this);
1515 }
1516
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001517 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001518 public void requestLimitEdit() {
1519 LimitEditorFragment.show(DataUsageSummary.this);
1520 }
1521 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001522
1523 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001524 * List item that reflects a specific data usage cycle.
1525 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001526 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001527 public CharSequence label;
1528 public long start;
1529 public long end;
1530
Jeff Sharkey8a503642011-06-10 13:31:21 -07001531 CycleItem(CharSequence label) {
1532 this.label = label;
1533 }
1534
1535 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001536 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001537 this.start = start;
1538 this.end = end;
1539 }
1540
Jeff Sharkey8a503642011-06-10 13:31:21 -07001541 @Override
1542 public String toString() {
1543 return label.toString();
1544 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001545
1546 @Override
1547 public boolean equals(Object o) {
1548 if (o instanceof CycleItem) {
1549 final CycleItem another = (CycleItem) o;
1550 return start == another.start && end == another.end;
1551 }
1552 return false;
1553 }
1554
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001555 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001556 public int compareTo(CycleItem another) {
1557 return Long.compare(start, another.start);
1558 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001559 }
1560
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001561 private static final StringBuilder sBuilder = new StringBuilder(50);
1562 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1563 sBuilder, Locale.getDefault());
1564
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001565 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001566 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001567
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001568 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001569 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001570 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1571 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001572 }
1573 }
1574
Jeff Sharkey8a503642011-06-10 13:31:21 -07001575 /**
1576 * Special-case data usage cycle that triggers dialog to change
1577 * {@link NetworkPolicy#cycleDay}.
1578 */
1579 public static class CycleChangeItem extends CycleItem {
1580 public CycleChangeItem(Context context) {
1581 super(context.getString(R.string.data_usage_change_cycle));
1582 }
1583 }
1584
1585 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001586 private boolean mChangePossible = false;
1587 private boolean mChangeVisible = false;
1588
1589 private final CycleChangeItem mChangeItem;
1590
Jeff Sharkey8a503642011-06-10 13:31:21 -07001591 public CycleAdapter(Context context) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001592 super(context, R.layout.data_usage_cycle_item);
1593 setDropDownViewResource(R.layout.data_usage_cycle_item_dropdown);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001594 mChangeItem = new CycleChangeItem(context);
1595 }
1596
1597 public void setChangePossible(boolean possible) {
1598 mChangePossible = possible;
1599 updateChange();
1600 }
1601
1602 public void setChangeVisible(boolean visible) {
1603 mChangeVisible = visible;
1604 updateChange();
1605 }
1606
1607 private void updateChange() {
1608 remove(mChangeItem);
1609 if (mChangePossible && mChangeVisible) {
1610 add(mChangeItem);
1611 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001612 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001613
1614 /**
1615 * Find position of {@link CycleItem} in this adapter which is nearest
1616 * the given {@link CycleItem}.
1617 */
1618 public int findNearestPosition(CycleItem target) {
1619 if (target != null) {
1620 final int count = getCount();
1621 for (int i = count - 1; i >= 0; i--) {
1622 final CycleItem item = getItem(i);
1623 if (item instanceof CycleChangeItem) {
1624 continue;
1625 } else if (item.compareTo(target) >= 0) {
1626 return i;
1627 }
1628 }
1629 }
1630 return 0;
1631 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001632 }
1633
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001634 public static class AppItem implements Comparable<AppItem>, Parcelable {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001635 public static final int CATEGORY_USER = 0;
1636 public static final int CATEGORY_APP_TITLE = 1;
1637 public static final int CATEGORY_APP = 2;
1638
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001639 public final int key;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001640 public boolean restricted;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001641 public int category;
1642
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001643 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001644 public long total;
1645
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001646 public AppItem() {
1647 this.key = 0;
1648 }
1649
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001650 public AppItem(int key) {
1651 this.key = key;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001652 }
1653
1654 public AppItem(Parcel parcel) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001655 key = parcel.readInt();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001656 uids = parcel.readSparseBooleanArray();
1657 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001658 }
1659
1660 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001661 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001662 }
1663
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001664 @Override
1665 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001666 dest.writeInt(key);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001667 dest.writeSparseBooleanArray(uids);
1668 dest.writeLong(total);
1669 }
1670
1671 @Override
1672 public int describeContents() {
1673 return 0;
1674 }
1675
1676 @Override
1677 public int compareTo(AppItem another) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001678 int comparison = Integer.compare(category, another.category);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001679 if (comparison == 0) {
1680 comparison = Long.compare(another.total, total);
1681 }
1682 return comparison;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001683 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001684
1685 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1686 @Override
1687 public AppItem createFromParcel(Parcel in) {
1688 return new AppItem(in);
1689 }
1690
1691 @Override
1692 public AppItem[] newArray(int size) {
1693 return new AppItem[size];
1694 }
1695 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001696 }
1697
Jeff Sharkey8a503642011-06-10 13:31:21 -07001698 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001699 * Adapter of applications, sorted by total usage descending.
1700 */
1701 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001702 private final UidDetailProvider mProvider;
1703 private final int mInsetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001704 private final UserManager mUm;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001705
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001706 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001707 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001708
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001709 public DataUsageAdapter(final UserManager userManager, UidDetailProvider provider, int insetSide) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001710 mProvider = checkNotNull(provider);
1711 mInsetSide = insetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001712 mUm = userManager;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001713 }
1714
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001715 /**
1716 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1717 */
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001718 public void bindStats(NetworkStats stats, int[] restrictedUids) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001719 mItems.clear();
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001720 mLargest = 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001721
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001722 final int currentUserId = ActivityManager.getCurrentUser();
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001723 final List<UserHandle> profiles = mUm.getUserProfiles();
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001724 final SparseArray<AppItem> knownItems = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001725
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001726 NetworkStats.Entry entry = null;
1727 final int size = stats != null ? stats.size() : 0;
1728 for (int i = 0; i < size; i++) {
1729 entry = stats.getValues(i, entry);
1730
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001731 // Decide how to collapse items together
1732 final int uid = entry.uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001733
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001734 final int collapseKey;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001735 final int category;
1736 final int userId = UserHandle.getUserId(uid);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001737 if (UserHandle.isApp(uid)) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001738 if (profiles.contains(new UserHandle(userId))) {
1739 if (userId != currentUserId) {
1740 // Add to a managed user item.
1741 final int managedKey = UidDetailProvider.buildKeyForUser(userId);
1742 accumulate(managedKey, knownItems, entry,
1743 AppItem.CATEGORY_USER);
1744 }
1745 // Add to app item.
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001746 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001747 category = AppItem.CATEGORY_APP;
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001748 } else {
Zoltan Szatmary-Ban77c1d362014-12-12 18:48:03 +00001749 // If it is a removed user add it to the removed users' key
1750 final UserInfo info = mUm.getUserInfo(userId);
1751 if (info == null) {
1752 collapseKey = UID_REMOVED;
1753 category = AppItem.CATEGORY_APP;
1754 } else {
1755 // Add to other user item.
1756 collapseKey = UidDetailProvider.buildKeyForUser(userId);
1757 category = AppItem.CATEGORY_USER;
1758 }
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001759 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001760 } else if (uid == UID_REMOVED || uid == UID_TETHERING) {
1761 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001762 category = AppItem.CATEGORY_APP;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001763 } else {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001764 collapseKey = android.os.Process.SYSTEM_UID;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001765 category = AppItem.CATEGORY_APP;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001766 }
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001767 accumulate(collapseKey, knownItems, entry, category);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001768 }
1769
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001770 final int restrictedUidsMax = restrictedUids.length;
1771 for (int i = 0; i < restrictedUidsMax; ++i) {
1772 final int uid = restrictedUids[i];
1773 // Only splice in restricted state for current user or managed users
1774 if (!profiles.contains(new UserHandle(UserHandle.getUserId(uid)))) {
1775 continue;
1776 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001777
1778 AppItem item = knownItems.get(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001779 if (item == null) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001780 item = new AppItem(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001781 item.total = -1;
1782 mItems.add(item);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001783 knownItems.put(item.key, item);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001784 }
1785 item.restricted = true;
1786 }
1787
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001788 if (!mItems.isEmpty()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001789 final AppItem title = new AppItem();
1790 title.category = AppItem.CATEGORY_APP_TITLE;
1791 mItems.add(title);
1792 }
1793
Jeff Sharkey8a503642011-06-10 13:31:21 -07001794 Collections.sort(mItems);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001795 notifyDataSetChanged();
1796 }
1797
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001798 /**
1799 * Accumulate data usage of a network stats entry for the item mapped by the collapse key.
1800 * Creates the item if needed.
1801 *
1802 * @param collapseKey the collapse key used to map the item.
1803 * @param knownItems collection of known (already existing) items.
1804 * @param entry the network stats entry to extract data usage from.
1805 * @param itemCategory the item is categorized on the list view by this category. Must be
1806 * either AppItem.APP_ITEM_CATEGORY or AppItem.MANAGED_USER_ITEM_CATEGORY
1807 */
1808 private void accumulate(int collapseKey, final SparseArray<AppItem> knownItems,
1809 NetworkStats.Entry entry, int itemCategory) {
1810 final int uid = entry.uid;
1811 AppItem item = knownItems.get(collapseKey);
1812 if (item == null) {
1813 item = new AppItem(collapseKey);
1814 item.category = itemCategory;
1815 mItems.add(item);
1816 knownItems.put(item.key, item);
1817 }
1818 item.addUid(uid);
1819 item.total += entry.rxBytes + entry.txBytes;
1820 if (mLargest < item.total) {
1821 mLargest = item.total;
1822 }
1823 }
1824
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001825 @Override
1826 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001827 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001828 }
1829
1830 @Override
1831 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001832 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001833 }
1834
1835 @Override
1836 public long getItemId(int position) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001837 return mItems.get(position).key;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001838 }
1839
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001840 /**
1841 * See {@link #getItemViewType} for the view types.
1842 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001843 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001844 public int getViewTypeCount() {
1845 return 2;
1846 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001847
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001848 /**
1849 * Returns 1 for separator items and 0 for anything else.
1850 */
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001851 @Override
1852 public int getItemViewType(int position) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001853 final AppItem item = mItems.get(position);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001854 if (item.category == AppItem.CATEGORY_APP_TITLE) {
1855 return 1;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001856 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001857 return 0;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001858 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001859 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001860
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001861 @Override
1862 public boolean areAllItemsEnabled() {
1863 return false;
1864 }
1865
1866 @Override
1867 public boolean isEnabled(int position) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001868 if (position > mItems.size()) {
1869 throw new ArrayIndexOutOfBoundsException();
1870 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001871 return getItemViewType(position) == 0;
1872 }
1873
1874 @Override
1875 public View getView(int position, View convertView, ViewGroup parent) {
1876 final AppItem item = mItems.get(position);
1877 if (getItemViewType(position) == 1) {
1878 if (convertView == null) {
Zoltan Szatmary-Ban3af2e4c2014-12-19 17:17:23 +00001879 convertView = Utils.inflateCategoryHeader(LayoutInflater.from(
1880 parent.getContext()), parent);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001881 }
1882
1883 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
1884 title.setText(R.string.data_usage_app);
1885
1886 } else {
1887 if (convertView == null) {
1888 convertView = LayoutInflater.from(parent.getContext()).inflate(
1889 R.layout.data_usage_item, parent, false);
1890
1891 if (mInsetSide > 0) {
1892 convertView.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
1893 }
1894 }
1895
1896 final Context context = parent.getContext();
1897
1898 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
1899 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1900 android.R.id.progress);
1901
1902 // kick off async load of app details
1903 UidDetailTask.bindView(mProvider, item, convertView);
1904
1905 if (item.restricted && item.total <= 0) {
1906 text1.setText(R.string.data_usage_app_restricted);
1907 progress.setVisibility(View.GONE);
1908 } else {
1909 text1.setText(Formatter.formatFileSize(context, item.total));
1910 progress.setVisibility(View.VISIBLE);
1911 }
1912
1913 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1914 progress.setProgress(percentTotal);
1915 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001916
1917 return convertView;
1918 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001919 }
1920
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001921 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001922 * Empty {@link Fragment} that controls display of UID details in
1923 * {@link DataUsageSummary}.
1924 */
1925 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001926 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001927
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001928 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jason Monk4bb075b2015-03-17 10:06:58 -04001929 show(parent, app, label, true);
1930 }
1931
1932 public static void show(DataUsageSummary parent, AppItem app, CharSequence label,
1933 boolean addToBack) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001934 if (!parent.isAdded()) return;
1935
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001936 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001937 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001938
1939 final AppDetailsFragment fragment = new AppDetailsFragment();
1940 fragment.setArguments(args);
1941 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001942 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1943 ft.add(fragment, TAG_APP_DETAILS);
Jason Monk4bb075b2015-03-17 10:06:58 -04001944 if (addToBack) {
1945 ft.addToBackStack(TAG_APP_DETAILS);
1946 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001947 ft.setBreadCrumbTitle(
1948 parent.getResources().getString(R.string.data_usage_app_summary_title));
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001949 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001950 }
1951
1952 @Override
1953 public void onStart() {
1954 super.onStart();
1955 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001956 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001957 target.updateBody();
1958 }
1959
1960 @Override
1961 public void onStop() {
1962 super.onStop();
1963 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001964 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001965 target.updateBody();
1966 }
1967 }
1968
1969 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001970 * Dialog to request user confirmation before setting
1971 * {@link NetworkPolicy#limitBytes}.
1972 */
1973 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001974 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001975 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001976
1977 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001978 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001979
Jeff Sharkeyd0a55312014-08-08 10:25:24 -07001980 final NetworkPolicy policy = parent.mPolicyEditor.getPolicy(parent.mTemplate);
1981 if (policy == null) return;
1982
Jeff Sharkey461842a2011-09-25 18:22:48 -07001983 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001984 final CharSequence message;
Jeff Sharkeyd0a55312014-08-08 10:25:24 -07001985 final long minLimitBytes = (long) (policy.warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001986 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001987
1988 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001989 final String currentTab = parent.mCurrentTab;
1990 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001991 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001992 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001993 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001994 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001995 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
PauloftheWest50e6eca2014-10-03 11:07:14 -07001996 } else if (isMobileTab(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001997 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001998 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001999 } else {
2000 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002001 }
2002
Jeff Sharkey131f9d62011-08-17 17:08:19 -07002003 final Bundle args = new Bundle();
2004 args.putCharSequence(EXTRA_MESSAGE, message);
2005 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
2006
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002007 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
2008 dialog.setArguments(args);
2009 dialog.setTargetFragment(parent, 0);
2010 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
2011 }
2012
2013 @Override
2014 public Dialog onCreateDialog(Bundle savedInstanceState) {
2015 final Context context = getActivity();
2016
Jeff Sharkey131f9d62011-08-17 17:08:19 -07002017 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002018 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
2019
2020 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2021 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07002022 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002023
2024 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002025 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002026 public void onClick(DialogInterface dialog, int which) {
2027 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2028 if (target != null) {
2029 target.setPolicyLimitBytes(limitBytes);
2030 }
2031 }
2032 });
2033
2034 return builder.create();
2035 }
2036 }
2037
2038 /**
2039 * Dialog to edit {@link NetworkPolicy#cycleDay}.
2040 */
2041 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002042 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002043
2044 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002045 if (!parent.isAdded()) return;
2046
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002047 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002048 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002049
2050 final CycleEditorFragment dialog = new CycleEditorFragment();
2051 dialog.setArguments(args);
2052 dialog.setTargetFragment(parent, 0);
2053 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
2054 }
2055
2056 @Override
2057 public Dialog onCreateDialog(Bundle savedInstanceState) {
2058 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002059 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2060 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002061
2062 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2063 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2064
2065 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
2066 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
2067
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002068 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2069 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002070
2071 cycleDayPicker.setMinValue(1);
2072 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002073 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002074 cycleDayPicker.setWrapSelectorWheel(true);
2075
2076 builder.setTitle(R.string.data_usage_cycle_editor_title);
2077 builder.setView(view);
2078
2079 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2080 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002081 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002082 public void onClick(DialogInterface dialog, int which) {
Jeff Sharkeyd277de92013-03-25 15:11:25 -07002083 // clear focus to finish pending text edits
2084 cycleDayPicker.clearFocus();
2085
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002086 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08002087 final String cycleTimezone = new Time().timezone;
2088 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002089 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07002090 }
2091 });
2092
2093 return builder.create();
2094 }
2095 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002096
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002097 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002098 * Dialog to edit {@link NetworkPolicy#warningBytes}.
2099 */
2100 public static class WarningEditorFragment extends DialogFragment {
2101 private static final String EXTRA_TEMPLATE = "template";
2102
2103 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002104 if (!parent.isAdded()) return;
2105
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002106 final Bundle args = new Bundle();
2107 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
2108
2109 final WarningEditorFragment dialog = new WarningEditorFragment();
2110 dialog.setArguments(args);
2111 dialog.setTargetFragment(parent, 0);
2112 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
2113 }
2114
2115 @Override
2116 public Dialog onCreateDialog(Bundle savedInstanceState) {
2117 final Context context = getActivity();
2118 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2119 final NetworkPolicyEditor editor = target.mPolicyEditor;
2120
2121 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2122 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2123
2124 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
2125 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
2126
2127 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2128 final long warningBytes = editor.getPolicyWarningBytes(template);
2129 final long limitBytes = editor.getPolicyLimitBytes(template);
2130
2131 bytesPicker.setMinValue(0);
2132 if (limitBytes != LIMIT_DISABLED) {
2133 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
2134 } else {
2135 bytesPicker.setMaxValue(Integer.MAX_VALUE);
2136 }
2137 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
2138 bytesPicker.setWrapSelectorWheel(false);
2139
2140 builder.setTitle(R.string.data_usage_warning_editor_title);
2141 builder.setView(view);
2142
2143 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2144 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002145 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002146 public void onClick(DialogInterface dialog, int which) {
2147 // clear focus to finish pending text edits
2148 bytesPicker.clearFocus();
2149
2150 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
2151 editor.setPolicyWarningBytes(template, bytes);
2152 target.updatePolicy(false);
2153 }
2154 });
2155
2156 return builder.create();
2157 }
2158 }
2159
2160 /**
2161 * Dialog to edit {@link NetworkPolicy#limitBytes}.
2162 */
2163 public static class LimitEditorFragment extends DialogFragment {
2164 private static final String EXTRA_TEMPLATE = "template";
2165
2166 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002167 if (!parent.isAdded()) return;
2168
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002169 final Bundle args = new Bundle();
2170 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
2171
2172 final LimitEditorFragment dialog = new LimitEditorFragment();
2173 dialog.setArguments(args);
2174 dialog.setTargetFragment(parent, 0);
2175 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
2176 }
2177
2178 @Override
2179 public Dialog onCreateDialog(Bundle savedInstanceState) {
2180 final Context context = getActivity();
2181 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2182 final NetworkPolicyEditor editor = target.mPolicyEditor;
2183
2184 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2185 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
2186
2187 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
2188 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
2189
2190 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2191 final long warningBytes = editor.getPolicyWarningBytes(template);
2192 final long limitBytes = editor.getPolicyLimitBytes(template);
2193
2194 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09002195 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002196 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
2197 } else {
2198 bytesPicker.setMinValue(0);
2199 }
2200 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
2201 bytesPicker.setWrapSelectorWheel(false);
2202
2203 builder.setTitle(R.string.data_usage_limit_editor_title);
2204 builder.setView(view);
2205
2206 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2207 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002208 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002209 public void onClick(DialogInterface dialog, int which) {
2210 // clear focus to finish pending text edits
2211 bytesPicker.clearFocus();
2212
2213 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
2214 editor.setPolicyLimitBytes(template, bytes);
2215 target.updatePolicy(false);
2216 }
2217 });
2218
2219 return builder.create();
2220 }
2221 }
2222 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07002223 * Dialog to request user confirmation before disabling data.
2224 */
2225 public static class ConfirmDataDisableFragment extends DialogFragment {
Sanket Padawe24f834d2015-01-08 11:23:11 -08002226 static int mSubId;
2227 public static void show(DataUsageSummary parent, int subId) {
2228 mSubId = subId;
Jeff Sharkey461842a2011-09-25 18:22:48 -07002229 if (!parent.isAdded()) return;
2230
Jeff Sharkey28130d92011-09-02 16:10:24 -07002231 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
2232 dialog.setTargetFragment(parent, 0);
2233 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
2234 }
2235
2236 @Override
2237 public Dialog onCreateDialog(Bundle savedInstanceState) {
2238 final Context context = getActivity();
2239
2240 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2241 builder.setMessage(R.string.data_usage_disable_mobile);
2242
2243 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002244 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07002245 public void onClick(DialogInterface dialog, int which) {
2246 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2247 if (target != null) {
2248 // TODO: extend to modify policy enabled flag.
Sanket Padawe24f834d2015-01-08 11:23:11 -08002249 target.setMobileDataEnabled(mSubId, false);
Jeff Sharkey28130d92011-09-02 16:10:24 -07002250 }
2251 }
2252 });
2253 builder.setNegativeButton(android.R.string.cancel, null);
2254
2255 return builder.create();
2256 }
2257 }
2258
2259 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002260 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07002261 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002262 */
2263 public static class ConfirmRestrictFragment extends DialogFragment {
2264 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002265 if (!parent.isAdded()) return;
2266
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002267 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
2268 dialog.setTargetFragment(parent, 0);
2269 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
2270 }
2271
2272 @Override
2273 public Dialog onCreateDialog(Bundle savedInstanceState) {
2274 final Context context = getActivity();
2275
2276 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002277 builder.setTitle(R.string.data_usage_restrict_background_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07002278 if (Utils.hasMultipleUsers(context)) {
2279 builder.setMessage(R.string.data_usage_restrict_background_multiuser);
2280 } else {
2281 builder.setMessage(R.string.data_usage_restrict_background);
2282 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002283
2284 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002285 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002286 public void onClick(DialogInterface dialog, int which) {
2287 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2288 if (target != null) {
2289 target.setRestrictBackground(true);
2290 }
2291 }
2292 });
2293 builder.setNegativeButton(android.R.string.cancel, null);
2294
2295 return builder.create();
2296 }
2297 }
2298
2299 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002300 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
2301 * change has been denied, usually based on
2302 * {@link DataUsageSummary#hasLimitedNetworks()}.
2303 */
2304 public static class DeniedRestrictFragment extends DialogFragment {
2305 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002306 if (!parent.isAdded()) return;
2307
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002308 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
2309 dialog.setTargetFragment(parent, 0);
2310 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
2311 }
2312
2313 @Override
2314 public Dialog onCreateDialog(Bundle savedInstanceState) {
2315 final Context context = getActivity();
2316
2317 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2318 builder.setTitle(R.string.data_usage_app_restrict_background);
2319 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
2320 builder.setPositiveButton(android.R.string.ok, null);
2321
2322 return builder.create();
2323 }
2324 }
2325
2326 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002327 * Dialog to request user confirmation before setting
2328 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
2329 */
2330 public static class ConfirmAppRestrictFragment extends DialogFragment {
2331 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002332 if (!parent.isAdded()) return;
2333
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002334 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2335 dialog.setTargetFragment(parent, 0);
2336 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2337 }
2338
2339 @Override
2340 public Dialog onCreateDialog(Bundle savedInstanceState) {
2341 final Context context = getActivity();
2342
2343 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002344 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2345 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2346
2347 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002348 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002349 public void onClick(DialogInterface dialog, int which) {
2350 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2351 if (target != null) {
2352 target.setAppRestrictBackground(true);
2353 }
2354 }
2355 });
2356 builder.setNegativeButton(android.R.string.cancel, null);
2357
2358 return builder.create();
2359 }
2360 }
2361
2362 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002363 * Compute default tab that should be selected, based on
2364 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2365 */
2366 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002367 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002368 if (template == null) {
Wink Savilleb003a852014-10-23 10:16:26 -07002369 final int subId = intent.getIntExtra(PhoneConstants.SUBSCRIPTION_KEY,
Wink Saville0183fb52014-11-22 10:11:39 -08002370 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Savilled72c0622014-12-11 10:10:05 -08002371 if (SubscriptionManager.isValidSubscriptionId(subId)) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07002372 return TAB_MOBILE + String.valueOf(subId);
2373 }
2374 return null;
2375 }
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002376
2377 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002378 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002379 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002380 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002381 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002382 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002383 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002384 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002385 return TAB_WIFI;
2386 default:
2387 return null;
2388 }
2389 }
2390
2391 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002392 * Background task that loads {@link UidDetail}, binding to
2393 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002394 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002395 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2396 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002397 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002398 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002399
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002400 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002401 mProvider = checkNotNull(provider);
2402 mItem = checkNotNull(item);
2403 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002404 }
2405
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002406 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002407 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002408 final UidDetailTask existing = (UidDetailTask) target.getTag();
2409 if (existing != null) {
2410 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002411 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002412
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002413 final UidDetail cachedDetail = provider.getUidDetail(item.key, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002414 if (cachedDetail != null) {
2415 bindView(cachedDetail, target);
2416 } else {
2417 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2418 AsyncTask.THREAD_POOL_EXECUTOR));
2419 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002420 }
2421
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002422 private static void bindView(UidDetail detail, View target) {
2423 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2424 final TextView title = (TextView) target.findViewById(android.R.id.title);
2425
2426 if (detail != null) {
2427 icon.setImageDrawable(detail.icon);
2428 title.setText(detail.label);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +01002429 title.setContentDescription(detail.contentDescription);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002430 } else {
2431 icon.setImageDrawable(null);
2432 title.setText(null);
2433 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002434 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002435
2436 @Override
2437 protected void onPreExecute() {
2438 bindView(null, mTarget);
2439 }
2440
2441 @Override
2442 protected UidDetail doInBackground(Void... params) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002443 return mProvider.getUidDetail(mItem.key, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002444 }
2445
2446 @Override
2447 protected void onPostExecute(UidDetail result) {
2448 bindView(result, mTarget);
2449 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002450 }
2451
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002452 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002453 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002454 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002455 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002456 if (TEST_RADIOS) {
2457 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2458 }
2459
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002460 final ConnectivityManager conn = ConnectivityManager.from(context);
2461 final TelephonyManager tele = TelephonyManager.from(context);
2462
Wink Saville0183fb52014-11-22 10:11:39 -08002463 final List<SubscriptionInfo> subInfoList =
2464 SubscriptionManager.from(context).getActiveSubscriptionInfoList();
2465 // No activated Subscriptions
PauloftheWest50e6eca2014-10-03 11:07:14 -07002466 if (subInfoList == null) {
Wink Savillefcec91f2014-12-02 17:12:08 -08002467 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subInfoList=null");
PauloftheWest50e6eca2014-10-03 11:07:14 -07002468 return false;
2469 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002470 // require both supported network and ready SIM
PauloftheWest50e6eca2014-10-03 11:07:14 -07002471 boolean isReady = true;
Wink Savilleca756612014-11-08 10:47:12 -08002472 for (SubscriptionInfo subInfo : subInfoList) {
Stuart Scott3ada2ec2014-10-31 14:11:24 -07002473 isReady = isReady & tele.getSimState(subInfo.getSimSlotIndex()) == SIM_STATE_READY;
Wink Savillefcec91f2014-12-02 17:12:08 -08002474 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subInfo=" + subInfo);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002475 }
Wink Savillefcec91f2014-12-02 17:12:08 -08002476 boolean retVal = conn.isNetworkSupported(TYPE_MOBILE) && isReady;
2477 if (LOGD) {
2478 Log.d(TAG, "hasReadyMobileRadio:"
2479 + " conn.isNetworkSupported(TYPE_MOBILE)="
2480 + conn.isNetworkSupported(TYPE_MOBILE)
2481 + " isReady=" + isReady);
2482 }
2483 return retVal;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002484 }
2485
2486 /*
2487 * TODO: consider adding to TelephonyManager or SubscritpionManager.
2488 */
Wink Savilleb003a852014-10-23 10:16:26 -07002489 public static boolean hasReadyMobileRadio(Context context, int subId) {
PauloftheWest50e6eca2014-10-03 11:07:14 -07002490 if (TEST_RADIOS) {
2491 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2492 }
2493
2494 final ConnectivityManager conn = ConnectivityManager.from(context);
2495 final TelephonyManager tele = TelephonyManager.from(context);
2496 final int slotId = SubscriptionManager.getSlotId(subId);
2497 final boolean isReady = tele.getSimState(slotId) == SIM_STATE_READY;
2498
Wink Savillefcec91f2014-12-02 17:12:08 -08002499 boolean retVal = conn.isNetworkSupported(TYPE_MOBILE) && isReady;
2500 if (LOGD) Log.d(TAG, "hasReadyMobileRadio: subId=" + subId
2501 + " conn.isNetworkSupported(TYPE_MOBILE)=" + conn.isNetworkSupported(TYPE_MOBILE)
2502 + " isReady=" + isReady);
2503 return retVal;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002504 }
2505
2506 /**
2507 * Test if device has a mobile 4G data radio.
2508 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002509 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002510 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2511 return false;
2512 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002513 if (TEST_RADIOS) {
2514 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2515 }
2516
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002517 final ConnectivityManager conn = ConnectivityManager.from(context);
2518 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002519
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002520 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Wink Saville55434042012-06-14 12:33:43 -07002521 final boolean hasLte = (tele.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE)
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002522 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002523 return hasWimax || hasLte;
2524 }
2525
2526 /**
2527 * Test if device has a Wi-Fi data radio.
2528 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002529 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002530 if (TEST_RADIOS) {
2531 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2532 }
2533
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002534 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002535 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002536 }
2537
2538 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002539 * Test if device has an ethernet network connection.
2540 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002541 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002542 if (TEST_RADIOS) {
2543 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2544 }
2545
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002546 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002547 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2548
2549 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002550 if (mStatsSession != null) {
2551 try {
2552 ethernetBytes = mStatsSession.getSummaryForNetwork(
2553 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2554 .getTotalBytes();
2555 } catch (RemoteException e) {
2556 throw new RuntimeException(e);
2557 }
2558 } else {
2559 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002560 }
2561
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002562 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002563 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002564 }
2565
2566 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002567 * Inflate a {@link Preference} style layout, adding the given {@link View}
2568 * widget into {@link android.R.id#widget_frame}.
2569 */
2570 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2571 final View view = inflater.inflate(R.layout.preference, root, false);
2572 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2573 android.R.id.widget_frame);
2574 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2575 return view;
2576 }
2577
2578 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002579 * Test if any networks are currently limited.
2580 */
2581 private boolean hasLimitedNetworks() {
2582 return !buildLimitedNetworksList().isEmpty();
2583 }
2584
2585 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002586 * Build string describing currently limited networks, which defines when
2587 * background data is restricted.
2588 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002589 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002590 private CharSequence buildLimitedNetworksString() {
2591 final List<CharSequence> limited = buildLimitedNetworksList();
2592
2593 // handle case where no networks limited
2594 if (limited.isEmpty()) {
2595 limited.add(getText(R.string.data_usage_list_none));
2596 }
2597
2598 return TextUtils.join(limited);
2599 }
2600
2601 /**
2602 * Build list of currently limited networks, which defines when background
2603 * data is restricted.
2604 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002605 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002606 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002607 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002608
2609 // build combined list of all limited networks
2610 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002611
2612 final TelephonyManager tele = TelephonyManager.from(context);
2613 if (tele.getSimState() == SIM_STATE_READY) {
2614 final String subscriberId = getActiveSubscriberId(context);
2615 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2616 limited.add(getText(R.string.data_usage_list_mobile));
2617 }
2618 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2619 limited.add(getText(R.string.data_usage_tab_3g));
2620 }
2621 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2622 limited.add(getText(R.string.data_usage_tab_4g));
2623 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002624 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002625
2626 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002627 limited.add(getText(R.string.data_usage_tab_wifi));
2628 }
2629 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2630 limited.add(getText(R.string.data_usage_tab_ethernet));
2631 }
2632
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002633 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002634 }
2635
2636 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002637 * Inset both selector and divider {@link Drawable} on the given
2638 * {@link ListView} by the requested dimensions.
2639 */
2640 private static void insetListViewDrawables(ListView view, int insetSide) {
2641 final Drawable selector = view.getSelector();
2642 final Drawable divider = view.getDivider();
2643
2644 // fully unregister these drawables so callbacks can be maintained after
2645 // wrapping below.
2646 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2647 view.setSelector(stub);
2648 view.setDivider(stub);
2649
2650 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2651 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2652 }
2653
2654 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002655 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002656 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002657 */
2658 private static void setPreferenceTitle(View parent, int resId) {
2659 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2660 title.setText(resId);
2661 }
2662
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002663 /**
2664 * Set {@link android.R.id#summary} for a preference view inflated with
2665 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2666 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002667 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002668 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2669 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002670 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002671 }
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002672
2673 /**
2674 * For search
2675 */
2676 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -07002677 new BaseSearchIndexProvider() {
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002678 @Override
2679 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
2680 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
2681
2682 final Resources res = context.getResources();
2683
2684 // Add fragment title
2685 SearchIndexableRaw data = new SearchIndexableRaw(context);
2686 data.title = res.getString(R.string.data_usage_summary_title);
2687 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2688 result.add(data);
2689
2690 // Mobile data
2691 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002692 data.key = DATA_USAGE_ENABLE_MOBILE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002693 data.title = res.getString(R.string.data_usage_enable_mobile);
2694 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2695 result.add(data);
2696
2697 // Set mobile data limit
2698 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002699 data.key = DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002700 data.title = res.getString(R.string.data_usage_disable_mobile_limit);
2701 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2702 result.add(data);
2703
Fabrice Di Megliof2a52262014-04-17 17:20:27 -07002704 // Data usage cycle
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002705 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002706 data.key = DATA_USAGE_CYCLE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002707 data.title = res.getString(R.string.data_usage_cycle);
2708 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2709 result.add(data);
2710
2711 return result;
2712 }
2713 };
2714
Sanket Padawe7e21fa52015-02-12 12:39:34 -08002715 private void addMobileTab(Context context, SubscriptionInfo subInfo, boolean isMultiSim) {
PauloftheWestc80b7612014-11-25 04:40:45 -08002716 if (subInfo != null && mMobileTagMap != null) {
2717 if (hasReadyMobileRadio(context, subInfo.getSubscriptionId())) {
Sanket Padawe7e21fa52015-02-12 12:39:34 -08002718 if (isMultiSim) {
2719 mTabHost.addTab(buildTabSpec(mMobileTagMap.get(subInfo.getSubscriptionId()),
2720 subInfo.getDisplayName()));
2721 } else {
2722 mTabHost.addTab(buildTabSpec(mMobileTagMap.get(subInfo.getSubscriptionId()),
2723 R.string.data_usage_tab_mobile));
2724 }
PauloftheWest50e6eca2014-10-03 11:07:14 -07002725 }
Wink Savillefcec91f2014-12-02 17:12:08 -08002726 } else {
2727 if (LOGD) Log.d(TAG, "addMobileTab: subInfoList is null");
PauloftheWest50e6eca2014-10-03 11:07:14 -07002728 }
2729 }
2730
Wink Savilleca756612014-11-08 10:47:12 -08002731 private SubscriptionInfo getCurrentTabSubInfo(Context context) {
PauloftheWestde338442014-10-29 14:46:44 -07002732 if (mSubInfoList != null && mTabHost != null) {
2733 final int currentTagIndex = mTabHost.getCurrentTab();
2734 int i = 0;
Wink Savilleca756612014-11-08 10:47:12 -08002735 for (SubscriptionInfo subInfo : mSubInfoList) {
Stuart Scott3ada2ec2014-10-31 14:11:24 -07002736 if (hasReadyMobileRadio(context, subInfo.getSubscriptionId())) {
PauloftheWestde338442014-10-29 14:46:44 -07002737 if (i++ == currentTagIndex) {
2738 return subInfo;
2739 }
2740 }
2741 }
2742 }
2743 return null;
2744 }
2745
PauloftheWest50e6eca2014-10-03 11:07:14 -07002746 /**
2747 * Init a map with subId key and mobile tag name
2748 * @param subInfoList The subscription Info List
2749 * @return The map or null if no activated subscription
2750 */
Wink Savilleca756612014-11-08 10:47:12 -08002751 private Map<Integer, String> initMobileTabTag(List<SubscriptionInfo> subInfoList) {
Wink Savilleb003a852014-10-23 10:16:26 -07002752 Map<Integer, String> map = null;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002753 if (subInfoList != null) {
2754 String mobileTag;
Wink Savilleb003a852014-10-23 10:16:26 -07002755 map = new HashMap<Integer, String>();
Wink Savillefcec91f2014-12-02 17:12:08 -08002756 for (SubscriptionInfo subInfo : subInfoList) {
2757 mobileTag = TAB_MOBILE + String.valueOf(subInfo.getSubscriptionId());
2758 map.put(subInfo.getSubscriptionId(), mobileTag);
PauloftheWest50e6eca2014-10-03 11:07:14 -07002759 }
2760 }
2761 return map;
2762 }
2763
2764 private static boolean isMobileTab(String currentTab) {
2765 return currentTab != null ? currentTab.contains(TAB_MOBILE) : false;
2766 }
2767
Wink Savilleb003a852014-10-23 10:16:26 -07002768 private int getSubId(String currentTab) {
Wink Saville0183fb52014-11-22 10:11:39 -08002769 if (mMobileTagMap != null) {
2770 Set<Integer> set = mMobileTagMap.keySet();
2771 for (Integer subId : set) {
2772 if (mMobileTagMap.get(subId).equals(currentTab)) {
2773 return subId;
2774 }
PauloftheWest50e6eca2014-10-03 11:07:14 -07002775 }
2776 }
2777 Log.e(TAG, "currentTab = " + currentTab + " non mobile tab called this function");
2778 return -1;
2779 }
2780
2781 //SUB SELECT
PauloftheWest98523c82014-10-28 08:30:05 -07002782 private boolean isMobileDataAvailable(long subId) {
Wink Savilleb003a852014-10-23 10:16:26 -07002783 int[] subIds = SubscriptionManager.getSubId(PhoneConstants.SUB1);
PauloftheWest98523c82014-10-28 08:30:05 -07002784 if (subIds != null && subIds[0] == subId) {
2785 return true;
2786 }
2787
2788 subIds = SubscriptionManager.getSubId(PhoneConstants.SUB2);
2789 if (subIds != null && subIds[0] == subId) {
2790 return true;
2791 }
2792
2793 subIds = SubscriptionManager.getSubId(PhoneConstants.SUB3);
2794 if (subIds != null && subIds[0] == subId) {
2795 return true;
2796 }
2797 return false;
PauloftheWest50e6eca2014-10-03 11:07:14 -07002798 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002799}