blob: f7a0618e6fb86a529f22b112de1dae16d118ef80 [file] [log] [blame]
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070019import static android.net.ConnectivityManager.TYPE_ETHERNET;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070020import static android.net.ConnectivityManager.TYPE_MOBILE;
Jeff Sharkeybdf98e82011-11-10 17:17:24 -080021import static android.net.ConnectivityManager.TYPE_WIFI;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070022import static android.net.ConnectivityManager.TYPE_WIMAX;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070023import static android.net.NetworkPolicy.LIMIT_DISABLED;
Jeff Sharkeya53188f2011-09-13 19:56:45 -070024import static android.net.NetworkPolicy.WARNING_DISABLED;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070025import static android.net.NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070026import static android.net.NetworkPolicyManager.POLICY_NONE;
27import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND;
Jeff Sharkey8a503642011-06-10 13:31:21 -070028import static android.net.NetworkPolicyManager.computeLastCycleBoundary;
29import static android.net.NetworkPolicyManager.computeNextCycleBoundary;
Jeff Sharkeya662e492011-06-18 21:57:06 -070030import static android.net.NetworkTemplate.MATCH_MOBILE_3G_LOWER;
31import static android.net.NetworkTemplate.MATCH_MOBILE_4G;
32import static android.net.NetworkTemplate.MATCH_MOBILE_ALL;
33import static android.net.NetworkTemplate.MATCH_WIFI;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070034import static android.net.NetworkTemplate.buildTemplateEthernet;
35import static android.net.NetworkTemplate.buildTemplateMobile3gLower;
36import static android.net.NetworkTemplate.buildTemplateMobile4g;
37import static android.net.NetworkTemplate.buildTemplateMobileAll;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070038import static android.net.NetworkTemplate.buildTemplateWifiWildcard;
Jeff Sharkey77dae912012-02-03 14:50:33 -080039import static android.net.TrafficStats.GB_IN_BYTES;
40import static android.net.TrafficStats.MB_IN_BYTES;
Jeff Sharkeya83a24f2011-09-16 01:52:39 -070041import static android.net.TrafficStats.UID_REMOVED;
42import static android.net.TrafficStats.UID_TETHERING;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070043import static android.telephony.TelephonyManager.SIM_STATE_READY;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070044import static android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
45import static android.text.format.DateUtils.FORMAT_SHOW_DATE;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070046import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -070047import static com.android.internal.util.Preconditions.checkNotNull;
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -070048import static com.android.settings.Utils.prepareCustomPreferencesList;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070049
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070050import android.animation.LayoutTransition;
Jeff Sharkey38305fb2012-09-14 16:26:51 -070051import android.app.ActivityManager;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070052import android.app.AlertDialog;
53import android.app.Dialog;
54import android.app.DialogFragment;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070055import android.app.Fragment;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070056import android.app.FragmentTransaction;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070057import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkeyb6548462014-07-21 15:38:06 -070058import android.content.ComponentName;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070059import android.content.Context;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070060import android.content.DialogInterface;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -070061import android.content.Intent;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070062import android.content.Loader;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070063import android.content.SharedPreferences;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070064import android.content.pm.PackageManager;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070065import android.content.res.Resources;
Jeff Sharkeyb6548462014-07-21 15:38:06 -070066import android.content.res.TypedArray;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070067import android.graphics.Color;
Jeff Sharkey5d706792011-09-08 18:57:17 -070068import android.graphics.drawable.ColorDrawable;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070069import android.graphics.drawable.Drawable;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070070import android.net.ConnectivityManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070071import android.net.INetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070072import android.net.INetworkStatsService;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070073import android.net.INetworkStatsSession;
Jeff Sharkey8a503642011-06-10 13:31:21 -070074import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070075import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070076import android.net.NetworkStats;
77import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070078import android.net.NetworkTemplate;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070079import android.net.TrafficStats;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070080import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070081import android.os.Bundle;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070082import android.os.INetworkManagementService;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070083import android.os.Parcel;
84import android.os.Parcelable;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070085import android.os.RemoteException;
86import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070087import android.os.SystemProperties;
Dianne Hackbornbb06a422012-08-16 11:01:57 -070088import android.os.UserHandle;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +010089import android.os.UserManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070090import android.preference.Preference;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070091import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070092import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070093import android.text.format.DateUtils;
94import android.text.format.Formatter;
Jeff Sharkeye5223a02012-03-09 17:11:14 -080095import android.text.format.Time;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070096import android.util.Log;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070097import android.util.SparseArray;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070098import android.util.SparseBooleanArray;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070099import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700100import android.view.Menu;
101import android.view.MenuInflater;
102import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700103import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700104import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700105import android.view.ViewGroup;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700106import android.widget.AdapterView;
107import android.widget.AdapterView.OnItemClickListener;
108import android.widget.AdapterView.OnItemSelectedListener;
109import android.widget.ArrayAdapter;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700110import android.widget.BaseAdapter;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700111import android.widget.Button;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700112import android.widget.ImageView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700113import android.widget.LinearLayout;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700114import android.widget.ListView;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700115import android.widget.NumberPicker;
Jeff Sharkey2412b0f2011-07-17 20:31:40 -0700116import android.widget.ProgressBar;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700117import android.widget.Spinner;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700118import android.widget.Switch;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700119import android.widget.TabHost;
120import android.widget.TabHost.OnTabChangeListener;
121import android.widget.TabHost.TabContentFactory;
122import android.widget.TabHost.TabSpec;
123import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700124import android.widget.TextView;
125
Wink Saville55434042012-06-14 12:33:43 -0700126import com.android.internal.telephony.PhoneConstants;
Jeff Sharkey5d706792011-09-08 18:57:17 -0700127import com.android.settings.drawable.InsetBoundsDrawable;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700128import com.android.settings.net.ChartData;
129import com.android.settings.net.ChartDataLoader;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700130import com.android.settings.net.DataUsageMeteredSettings;
Jeff Sharkeya662e492011-06-18 21:57:06 -0700131import com.android.settings.net.NetworkPolicyEditor;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700132import com.android.settings.net.SummaryForAllUidLoader;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700133import com.android.settings.net.UidDetail;
134import com.android.settings.net.UidDetailProvider;
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -0700135import com.android.settings.search.BaseSearchIndexProvider;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -0700136import com.android.settings.search.Indexable;
137import com.android.settings.search.SearchIndexableRaw;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700138import com.android.settings.widget.ChartDataUsageView;
139import com.android.settings.widget.ChartDataUsageView.DataUsageChartListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700140import com.google.android.collect.Lists;
141
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700142import libcore.util.Objects;
143
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700144import java.util.ArrayList;
145import java.util.Collections;
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700146import java.util.List;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700147import java.util.Locale;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700148
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700149/**
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700150 * Panel showing data usage history across various networks, including options
151 * to inspect based on usage cycle and control through {@link NetworkPolicy}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700152 */
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700153public class DataUsageSummary extends HighlightingFragment implements Indexable {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700154 private static final String TAG = "DataUsage";
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800155 private static final boolean LOGD = false;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700156
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700157 // TODO: remove this testing code
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700158 private static final boolean TEST_ANIM = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700159 private static final boolean TEST_RADIOS = false;
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800160
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700161 private static final String TEST_RADIOS_PROP = "test.radios";
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800162 private static final String TEST_SUBSCRIBER_PROP = "test.subscriberid";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700163
Jeff Sharkey8a503642011-06-10 13:31:21 -0700164 private static final String TAB_3G = "3g";
165 private static final String TAB_4G = "4g";
166 private static final String TAB_MOBILE = "mobile";
167 private static final String TAB_WIFI = "wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700168 private static final String TAB_ETHERNET = "ethernet";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700169
Jeff Sharkey28130d92011-09-02 16:10:24 -0700170 private static final String TAG_CONFIRM_DATA_DISABLE = "confirmDataDisable";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700171 private static final String TAG_CONFIRM_LIMIT = "confirmLimit";
172 private static final String TAG_CYCLE_EDITOR = "cycleEditor";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700173 private static final String TAG_WARNING_EDITOR = "warningEditor";
174 private static final String TAG_LIMIT_EDITOR = "limitEditor";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700175 private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700176 private static final String TAG_DENIED_RESTRICT = "deniedRestrict";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700177 private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700178 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700179
Fabrice Di Meglio2169c882014-04-18 15:18:40 -0700180 private static final String DATA_USAGE_ENABLE_MOBILE_KEY = "data_usage_enable_mobile";
181 private static final String DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY =
182 "data_usage_disable_mobile_limit";
183 private static final String DATA_USAGE_CYCLE_KEY = "data_usage_cycle";
184
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700185 private static final int LOADER_CHART_DATA = 2;
186 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700187
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700188 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700189 private INetworkStatsService mStatsService;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700190 private NetworkPolicyManager mPolicyManager;
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700191 private TelephonyManager mTelephonyManager;
192
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700193 private INetworkStatsSession mStatsSession;
194
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700195 private static final String PREF_FILE = "data_usage";
196 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700197 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700198
199 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700200
Jeff Sharkey8a503642011-06-10 13:31:21 -0700201 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700202 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700203 private TabWidget mTabWidget;
204 private ListView mListView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700205 private DataUsageAdapter mAdapter;
206
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700207 /** Distance to inset content from sides, when needed. */
208 private int mInsetSide = 0;
209
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700210 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700211
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700212 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700213 private LinearLayout mNetworkSwitches;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700214 private boolean mDataEnabledSupported;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700215 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700216 private View mDataEnabledView;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700217 private boolean mDisableAtLimitSupported;
218 private Switch mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700219 private View mDisableAtLimitView;
220
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700221 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700222 private Spinner mCycleSpinner;
223 private CycleAdapter mCycleAdapter;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700224 private TextView mCycleSummary;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700225
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700226 private ChartDataUsageView mChart;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700227 private View mDisclaimer;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700228 private TextView mEmpty;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700229 private View mStupidPadding;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700230
231 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700232 private ImageView mAppIcon;
233 private ViewGroup mAppTitles;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700234 private TextView mAppTotal;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700235 private TextView mAppForeground;
236 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700237 private Button mAppSettings;
238
239 private LinearLayout mAppSwitches;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700240 private Switch mAppRestrict;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700241 private View mAppRestrictView;
242
Jeff Sharkey8a503642011-06-10 13:31:21 -0700243 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700244 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700245
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700246 private NetworkTemplate mTemplate;
247 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700248
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700249 private AppItem mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700250
251 private Intent mAppSettingsIntent;
252
Jeff Sharkeya662e492011-06-18 21:57:06 -0700253 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700254
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700255 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700256 private String mIntentTab = null;
257
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700258 private MenuItem mMenuRestrictBackground;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700259 private MenuItem mMenuShowWifi;
260 private MenuItem mMenuShowEthernet;
261 private MenuItem mMenuSimCards;
262 private MenuItem mMenuCellularNetworks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700263
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700264 /** Flag used to ignore listeners during binding. */
265 private boolean mBinding;
266
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700267 private UidDetailProvider mUidDetailProvider;
268
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700269 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700270 public void onCreate(Bundle savedInstanceState) {
271 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700272 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700273
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700274 mNetworkService = INetworkManagementService.Stub.asInterface(
275 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700276 mStatsService = INetworkStatsService.Stub.asInterface(
277 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700278 mPolicyManager = NetworkPolicyManager.from(context);
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700279 mTelephonyManager = TelephonyManager.from(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700280
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700281 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700282
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700283 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700284 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700285
Jaewan Kimffce9c12013-03-19 16:53:45 +0900286 try {
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700287 if (!mNetworkService.isBandwidthControlEnabled()) {
288 Log.w(TAG, "No bandwidth control; leaving");
289 getActivity().finish();
290 }
291 } catch (RemoteException e) {
292 Log.w(TAG, "No bandwidth control; leaving");
293 getActivity().finish();
294 }
295
296 try {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900297 mStatsSession = mStatsService.openSession();
298 } catch (RemoteException e) {
299 throw new RuntimeException(e);
300 }
301
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700302 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700303 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700304
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700305 // override preferences when no mobile radio
306 if (!hasReadyMobileRadio(context)) {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900307 mShowWifi = true;
308 mShowEthernet = true;
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700309 }
310
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700311 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700312 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700313
Jeff Sharkey8a503642011-06-10 13:31:21 -0700314 @Override
315 public View onCreateView(LayoutInflater inflater, ViewGroup container,
316 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700317
Jeff Sharkey8a503642011-06-10 13:31:21 -0700318 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700319 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
320
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700321 mUidDetailProvider = new UidDetailProvider(context);
322
Jeff Sharkey8a503642011-06-10 13:31:21 -0700323 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700324 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700325 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
326 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700327
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700328 // decide if we need to manually inset our content, or if we should rely
329 // on parent container for inset.
330 final boolean shouldInset = mListView.getScrollBarStyle()
331 == View.SCROLLBARS_OUTSIDE_OVERLAY;
Amith Yamasani56f51a82013-08-05 10:07:23 -0700332 mInsetSide = 0;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700333
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700334 // adjust padding around tabwidget as needed
Amith Yamasani56f51a82013-08-05 10:07:23 -0700335 prepareCustomPreferencesList(container, view, mListView, false);
Jeff Sharkey5d706792011-09-08 18:57:17 -0700336
Jeff Sharkey8a503642011-06-10 13:31:21 -0700337 mTabHost.setup();
338 mTabHost.setOnTabChangedListener(mTabListener);
339
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700340 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700341 mHeader.setClickable(true);
342
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700343 mListView.addHeaderView(new View(context), null, true);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700344 mListView.addHeaderView(mHeader, null, true);
345 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700346
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700347 if (mInsetSide > 0) {
348 // inset selector and divider drawables
349 insetListViewDrawables(mListView, mInsetSide);
Fabrice Di Megliob27223f2013-01-15 18:54:11 -0800350 mHeader.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700351 }
352
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700353 {
354 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700355 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
356 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700357 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700358
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700359 mDataEnabled = new Switch(inflater.getContext());
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700360 mDataEnabled.setClickable(false);
361 mDataEnabled.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700362 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700363 mDataEnabledView.setTag(R.id.preference_highlight_key,
364 DATA_USAGE_ENABLE_MOBILE_KEY);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700365 mDataEnabledView.setClickable(true);
366 mDataEnabledView.setFocusable(true);
367 mDataEnabledView.setOnClickListener(mDataEnabledListener);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700368 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700369
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700370 mDisableAtLimit = new Switch(inflater.getContext());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700371 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700372 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700373 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700374 mDisableAtLimitView.setTag(R.id.preference_highlight_key,
375 DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700376 mDisableAtLimitView.setClickable(true);
377 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700378 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
379 mNetworkSwitches.addView(mDisableAtLimitView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700380
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700381 mCycleView = inflater.inflate(R.layout.data_usage_cycles, mNetworkSwitches, false);
382 mCycleView.setTag(R.id.preference_highlight_key, DATA_USAGE_CYCLE_KEY);
383 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
384 mCycleAdapter = new CycleAdapter(context);
385 mCycleSpinner.setAdapter(mCycleAdapter);
386 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
387 mCycleSummary = (TextView) mCycleView.findViewById(R.id.cycle_summary);
388 mNetworkSwitches.addView(mCycleView);
389 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700390
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700391 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700392 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800393 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700394
395 {
396 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700397 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700398 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
399 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700400 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
401 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700402 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700403
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700404 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700405
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700406 mAppRestrict = new Switch(inflater.getContext());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700407 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700408 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700409 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700410 mAppRestrictView.setClickable(true);
411 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700412 mAppRestrictView.setOnClickListener(mAppRestrictListener);
413 mAppSwitches.addView(mAppRestrictView);
414 }
415
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700416 mDisclaimer = mHeader.findViewById(R.id.disclaimer);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700417 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700418 mStupidPadding = mHeader.findViewById(R.id.stupid_padding);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700419
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +0100420 final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
421 mAdapter = new DataUsageAdapter(um, mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700422 mListView.setOnItemClickListener(mListListener);
423 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700424
425 return view;
426 }
427
428 @Override
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700429 public void onViewStateRestored(Bundle savedInstanceState) {
430 super.onViewStateRestored(savedInstanceState);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700431
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700432 // pick default tab based on incoming intent
433 final Intent intent = getActivity().getIntent();
434 mIntentTab = computeTabFromIntent(intent);
435
Jeff Sharkey8a503642011-06-10 13:31:21 -0700436 // this kicks off chain reaction which creates tabs, binds the body to
437 // selected network, and binds chart, cycles and detail list.
438 updateTabs();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700439 }
440
441 @Override
442 public void onResume() {
443 super.onResume();
444
445 getView().post(new Runnable() {
446 @Override
447 public void run() {
448 highlightViewIfNeeded();
449 }
450 });
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700451
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700452 // kick off background task to update stats
453 new AsyncTask<Void, Void, Void>() {
454 @Override
455 protected Void doInBackground(Void... params) {
456 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700457 // wait a few seconds before kicking off
458 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700459 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700460 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700461 } catch (RemoteException e) {
462 }
463 return null;
464 }
465
466 @Override
467 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700468 if (isAdded()) {
469 updateBody();
470 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700471 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700472 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700473 }
474
Jeff Sharkey8a503642011-06-10 13:31:21 -0700475 @Override
476 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
477 inflater.inflate(R.menu.data_usage, menu);
478 }
479
480 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700481 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700482 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700483 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700484 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700485
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700486 mMenuShowWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700487 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
488 mMenuShowWifi.setVisible(!appDetailMode);
489 } else {
490 mMenuShowWifi.setVisible(false);
491 }
492
493 mMenuShowEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
494 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
495 mMenuShowEthernet.setVisible(!appDetailMode);
496 } else {
497 mMenuShowEthernet.setVisible(false);
498 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700499
500 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700501 mMenuRestrictBackground.setVisible(
502 hasReadyMobileRadio(context) && isOwner && !appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700503
504 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
505 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700506 metered.setVisible(!appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700507 } else {
508 metered.setVisible(false);
509 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700510
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700511 // TODO: show when multiple sims available
512 mMenuSimCards = menu.findItem(R.id.data_usage_menu_sim_cards);
513 mMenuSimCards.setVisible(false);
514
515 mMenuCellularNetworks = menu.findItem(R.id.data_usage_menu_cellular_networks);
516 if (hasReadyMobileRadio(context)) {
517 mMenuCellularNetworks.setVisible(!appDetailMode);
518 } else {
519 mMenuCellularNetworks.setVisible(false);
520 }
521
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700522 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
523 String helpUrl;
524 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800525 HelpUtils.prepareHelpMenuItem(context, help, helpUrl);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700526 } else {
527 help.setVisible(false);
528 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700529
530 updateMenuTitles();
531 }
532
533 private void updateMenuTitles() {
534 if (mPolicyManager.getRestrictBackground()) {
535 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_allow_background);
536 } else {
537 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_restrict_background);
538 }
539
540 if (mShowWifi) {
541 mMenuShowWifi.setTitle(R.string.data_usage_menu_hide_wifi);
542 } else {
543 mMenuShowWifi.setTitle(R.string.data_usage_menu_show_wifi);
544 }
545
546 if (mShowEthernet) {
547 mMenuShowEthernet.setTitle(R.string.data_usage_menu_hide_ethernet);
548 } else {
549 mMenuShowEthernet.setTitle(R.string.data_usage_menu_show_ethernet);
550 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700551 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700552
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700553 @Override
554 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700555 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700556 case R.id.data_usage_menu_restrict_background: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700557 final boolean restrictBackground = !mPolicyManager.getRestrictBackground();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700558 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800559 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700560 } else {
561 // no confirmation to drop restriction
562 setRestrictBackground(false);
563 }
564 return true;
565 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700566 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700567 mShowWifi = !mShowWifi;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700568 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700569 updateMenuTitles();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700570 updateTabs();
571 return true;
572 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700573 case R.id.data_usage_menu_show_ethernet: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700574 mShowEthernet = !mShowEthernet;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700575 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700576 updateMenuTitles();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700577 updateTabs();
578 return true;
579 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700580 case R.id.data_usage_menu_sim_cards: {
581 // TODO: hook up to sim cards
582 return true;
583 }
584 case R.id.data_usage_menu_cellular_networks: {
585 final Intent intent = new Intent(Intent.ACTION_MAIN);
586 intent.setComponent(new ComponentName("com.android.phone",
587 "com.android.phone.MobileNetworkSettings"));
588 startActivity(intent);
589 return true;
590 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700591 case R.id.data_usage_menu_metered: {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800592 final SettingsActivity sa = (SettingsActivity) getActivity();
593 sa.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700594 R.string.data_usage_metered_title, null, this, 0);
595 return true;
596 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700597 }
598 return false;
599 }
600
Jeff Sharkey94a90952011-06-13 22:31:09 -0700601 @Override
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700602 public void onDestroy() {
Jeff Sharkey94a90952011-06-13 22:31:09 -0700603 mDataEnabledView = null;
604 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700605
606 mUidDetailProvider.clearCache();
607 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700608
609 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700610
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800611 super.onDestroy();
612 }
613
Jeff Sharkey8a503642011-06-10 13:31:21 -0700614 /**
Jeff Sharkey92811822012-05-04 11:47:29 -0700615 * Build and assign {@link LayoutTransition} to various containers. Should
616 * only be assigned after initial layout is complete.
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700617 */
Jeff Sharkey92811822012-05-04 11:47:29 -0700618 private void ensureLayoutTransitions() {
619 // skip when already setup
620 if (mChart.getLayoutTransition() != null) return;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700621
Jeff Sharkey92811822012-05-04 11:47:29 -0700622 mTabsContainer.setLayoutTransition(buildLayoutTransition());
623 mHeader.setLayoutTransition(buildLayoutTransition());
624 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700625
Jeff Sharkey92811822012-05-04 11:47:29 -0700626 final LayoutTransition chartTransition = buildLayoutTransition();
627 chartTransition.disableTransitionType(LayoutTransition.APPEARING);
628 chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
629 mChart.setLayoutTransition(chartTransition);
630 }
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700631
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700632 private static LayoutTransition buildLayoutTransition() {
633 final LayoutTransition transition = new LayoutTransition();
634 if (TEST_ANIM) {
635 transition.setDuration(1500);
636 }
637 transition.setAnimateParentHierarchy(false);
638 return transition;
639 }
640
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700641 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700642 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700643 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
644 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700645 */
646 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700647 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700648 mTabHost.clearAllTabs();
649
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700650 final boolean mobileSplit = isMobilePolicySplit();
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700651 if (mobileSplit && hasReadyMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700652 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
653 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700654 } else if (hasReadyMobileRadio(context)) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700655 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700656 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700657 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700658 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
659 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700660 if (mShowEthernet && hasEthernet(context)) {
661 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
662 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700663
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700664 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700665 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
666 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
667 if (mIntentTab != null) {
668 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700669 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700670 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700671 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700672 mTabHost.setCurrentTabByTag(mIntentTab);
673 }
674 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700675 } else if (noTabs) {
676 // no usable tabs, so hide body
677 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700678 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700679 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700680 }
681 }
682
Jeff Sharkey8a503642011-06-10 13:31:21 -0700683 /**
684 * Factory that provide empty {@link View} to make {@link TabHost} happy.
685 */
686 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700687 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700688 public View createTabContent(String tag) {
689 return new View(mTabHost.getContext());
690 }
691 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700692
Jeff Sharkey8a503642011-06-10 13:31:21 -0700693 /**
694 * Build {@link TabSpec} with thin indicator, and empty content.
695 */
696 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700697 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
698 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700699 }
700
701 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700702 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700703 public void onTabChanged(String tabId) {
704 // user changed tab; update body
705 updateBody();
706 }
707 };
708
709 /**
710 * Update body content based on current tab. Loads
711 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
712 * binds them to visible controls.
713 */
714 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700715 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700716 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700717
Jeff Sharkeya662e492011-06-18 21:57:06 -0700718 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700719 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700720 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700721
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700722 if (currentTab == null) {
723 Log.w(TAG, "no tab selected; hiding body");
724 mListView.setVisibility(View.GONE);
725 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700726 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700727 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700728 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700729
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700730 mCurrentTab = currentTab;
731
Jeff Sharkey8a503642011-06-10 13:31:21 -0700732 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
733
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700734 mDataEnabledSupported = isOwner;
735 mDisableAtLimitSupported = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700736
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700737 // TODO: remove mobile tabs when SIM isn't ready
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700738
Jeff Sharkey8a503642011-06-10 13:31:21 -0700739 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700740 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
741 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700742 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700743
744 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700745 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
746 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
747 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700748 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700749
750 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700751 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
752 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
753 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700754 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700755
756 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700757 // wifi doesn't have any controls
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700758 mDataEnabledSupported = false;
759 mDisableAtLimitSupported = false;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700760 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700761
762 } else if (TAB_ETHERNET.equals(currentTab)) {
763 // ethernet doesn't have any controls
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700764 mDataEnabledSupported = false;
765 mDisableAtLimitSupported = false;
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700766 mTemplate = buildTemplateEthernet();
767
768 } else {
769 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700770 }
771
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700772 // kick off loader for network history
773 // TODO: consider chaining two loaders together instead of reloading
774 // network history when showing app detail.
775 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700776 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700777
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700778 // detail mode can change visible menus, invalidate
779 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700780
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700781 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700782 }
783
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700784 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700785 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700786 }
787
788 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700789 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700790 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700791 */
792 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700793 final Context context = getActivity();
794 final PackageManager pm = context.getPackageManager();
795 final LayoutInflater inflater = getActivity().getLayoutInflater();
796
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700797 if (isAppDetailMode()) {
798 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700799 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700800 } else {
801 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700802 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700803
804 // hide detail stats when not in detail mode
805 mChart.bindDetailNetworkStats(null);
806 return;
807 }
808
809 // remove warning/limit sweeps while in detail mode
810 mChart.bindNetworkPolicy(null);
811
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700812 // show icon and all labels appearing under this app
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700813 final int uid = mCurrentApp.key;
814 final UidDetail detail = mUidDetailProvider.getUidDetail(uid, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700815 mAppIcon.setImageDrawable(detail.icon);
816
817 mAppTitles.removeAllViews();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700818
819 View title = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700820 if (detail.detailLabels != null) {
821 for (CharSequence label : detail.detailLabels) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700822 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
823 ((TextView) title.findViewById(R.id.app_title)).setText(label);
824 mAppTitles.addView(title);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700825 }
826 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700827 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
828 ((TextView) title.findViewById(R.id.app_title)).setText(detail.label);
829 mAppTitles.addView(title);
830 }
831
832 // Remember last slot for summary
833 if (title != null) {
834 mAppTotal = (TextView) title.findViewById(R.id.app_summary);
835 } else {
836 mAppTotal = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700837 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700838
839 // enable settings button when package provides it
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700840 final String[] packageNames = pm.getPackagesForUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700841 if (packageNames != null && packageNames.length > 0) {
842 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700843 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
844
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700845 // Search for match across all packages
846 boolean matchFound = false;
847 for (String packageName : packageNames) {
848 mAppSettingsIntent.setPackage(packageName);
849 if (pm.resolveActivity(mAppSettingsIntent, 0) != null) {
850 matchFound = true;
851 break;
852 }
853 }
854
Zoltan Szatmary-Banf20d39e2014-08-07 15:27:08 +0100855 mAppSettings.setOnClickListener(new OnClickListener() {
856 @Override
857 public void onClick(View v) {
858 if (!isAdded()) {
859 return;
860 }
861
862 // TODO: target towards entire UID instead of just first package
863 getActivity().startActivityAsUser(mAppSettingsIntent,
864 new UserHandle(UserHandle.getUserId(uid)));
865 }
866 });
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700867 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -0700868 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700869
870 } else {
871 mAppSettingsIntent = null;
Zoltan Szatmary-Banf20d39e2014-08-07 15:27:08 +0100872 mAppSettings.setOnClickListener(null);
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700873 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700874 }
875
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700876 updateDetailData();
877
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700878 if (UserHandle.isApp(uid) && !mPolicyManager.getRestrictBackground()
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700879 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700880 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800881 setPreferenceSummary(mAppRestrictView,
882 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700883
884 mAppRestrictView.setVisibility(View.VISIBLE);
885 mAppRestrict.setChecked(getAppRestrictBackground());
886
887 } else {
888 mAppRestrictView.setVisibility(View.GONE);
889 }
890 }
891
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700892 private void setPolicyWarningBytes(long warningBytes) {
893 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700894 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700895 updatePolicy(false);
896 }
897
898 private void setPolicyLimitBytes(long limitBytes) {
899 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700900 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700901 updatePolicy(false);
902 }
903
Jeff Sharkey28130d92011-09-02 16:10:24 -0700904 /**
905 * Local cache of value, used to work around delay when
906 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
907 */
908 private Boolean mMobileDataEnabled;
909
910 private boolean isMobileDataEnabled() {
911 if (mMobileDataEnabled != null) {
912 // TODO: deprecate and remove this once enabled flag is on policy
913 return mMobileDataEnabled;
914 } else {
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700915 return mTelephonyManager.getDataEnabled();
Jeff Sharkey28130d92011-09-02 16:10:24 -0700916 }
917 }
918
919 private void setMobileDataEnabled(boolean enabled) {
920 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700921 mTelephonyManager.setDataEnabled(enabled);
Jeff Sharkey28130d92011-09-02 16:10:24 -0700922 mMobileDataEnabled = enabled;
923 updatePolicy(false);
924 }
925
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700926 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700927 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked()
928 && ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700929 }
930
931 private boolean isBandwidthControlEnabled() {
932 try {
933 return mNetworkService.isBandwidthControlEnabled();
934 } catch (RemoteException e) {
935 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
936 return false;
937 }
938 }
939
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700940 public void setRestrictBackground(boolean restrictBackground) {
941 mPolicyManager.setRestrictBackground(restrictBackground);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700942 updateMenuTitles();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700943 }
944
945 private boolean getAppRestrictBackground() {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700946 final int uid = mCurrentApp.key;
947 final int uidPolicy = mPolicyManager.getUidPolicy(uid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700948 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
949 }
950
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700951 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700952 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700953 final int uid = mCurrentApp.key;
954 mPolicyManager.setUidPolicy(
955 uid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700956 mAppRestrict.setChecked(restrictBackground);
957 }
958
Jeff Sharkey8a503642011-06-10 13:31:21 -0700959 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700960 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
961 * current {@link #mTemplate}.
962 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700963 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700964 boolean dataEnabledVisible = mDataEnabledSupported;
965 boolean disableAtLimitVisible = mDisableAtLimitSupported;
966
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700967 if (isAppDetailMode()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700968 dataEnabledVisible = false;
969 disableAtLimitVisible = false;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700970 }
971
Jeff Sharkey28130d92011-09-02 16:10:24 -0700972 // TODO: move enabled state directly into policy
973 if (TAB_MOBILE.equals(mCurrentTab)) {
974 mBinding = true;
975 mDataEnabled.setChecked(isMobileDataEnabled());
976 mBinding = false;
977 }
978
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700979 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
980 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700981 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700982 if (!isAppDetailMode()) {
983 mChart.bindNetworkPolicy(policy);
984 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700985
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700986 } else {
987 // controls are disabled; don't bind warning/limit sweeps
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700988 disableAtLimitVisible = false;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700989 mChart.bindNetworkPolicy(null);
990 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700991
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700992 mDataEnabledView.setVisibility(dataEnabledVisible ? View.VISIBLE : View.GONE);
993 mDisableAtLimitView.setVisibility(disableAtLimitVisible ? View.VISIBLE : View.GONE);
994
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700995 if (refreshCycle) {
996 // generate cycle list based on policy and available history
997 updateCycleList(policy);
998 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700999 }
1000
1001 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001002 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
1003 * and available {@link NetworkStatsHistory} data. Always selects the newest
1004 * item, updating the inspection range on {@link #mChart}.
1005 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001006 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001007 // stash away currently selected cycle to try restoring below
1008 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -07001009 mCycleAdapter.clear();
1010
1011 final Context context = mCycleSpinner.getContext();
1012
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001013 long historyStart = Long.MAX_VALUE;
1014 long historyEnd = Long.MIN_VALUE;
1015 if (mChartData != null) {
1016 historyStart = mChartData.network.getStart();
1017 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001018 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001019
Jeff Sharkey461842a2011-09-25 18:22:48 -07001020 final long now = System.currentTimeMillis();
1021 if (historyStart == Long.MAX_VALUE) historyStart = now;
1022 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001023
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001024 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001025 if (policy != null) {
1026 // find the next cycle boundary
1027 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001028
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001029 // walk backwards, generating all valid cycle ranges
1030 while (cycleEnd > historyStart) {
1031 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
1032 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
1033 + historyStart);
1034 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1035 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001036 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001037 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001038
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001039 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001040 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001041 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001042
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001043 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001044 // no policy defined cycles; show entry for each four-week period
1045 long cycleEnd = historyEnd;
1046 while (cycleEnd > historyStart) {
1047 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
1048 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1049 cycleEnd = cycleStart;
1050 }
1051
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001052 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001053 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001054
1055 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001056 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001057 final int position = mCycleAdapter.findNearestPosition(previousItem);
1058 mCycleSpinner.setSelection(position);
1059
1060 // only force-update cycle when changed; skipping preserves any
1061 // user-defined inspection region.
1062 final CycleItem selectedItem = mCycleAdapter.getItem(position);
1063 if (!Objects.equal(selectedItem, previousItem)) {
1064 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1065 } else {
1066 // but still kick off loader for detailed list
1067 updateDetailData();
1068 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001069 } else {
1070 updateDetailData();
1071 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001072 }
1073
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001074 private View.OnClickListener mDataEnabledListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001075 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001076 public void onClick(View v) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001077 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001078
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001079 final boolean dataEnabled = !mDataEnabled.isChecked();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001080 final String currentTab = mCurrentTab;
1081 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001082 if (dataEnabled) {
1083 setMobileDataEnabled(true);
1084 } else {
1085 // disabling data; show confirmation dialog which eventually
1086 // calls setMobileDataEnabled() once user confirms.
1087 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1088 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001089 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001090
Jeff Sharkey28130d92011-09-02 16:10:24 -07001091 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001092 }
1093 };
1094
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001095 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001096 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001097 public void onClick(View v) {
1098 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001099 if (disableAtLimit) {
1100 // enabling limit; show confirmation dialog which eventually
1101 // calls setPolicyLimitBytes() once user confirms.
1102 ConfirmLimitFragment.show(DataUsageSummary.this);
1103 } else {
1104 setPolicyLimitBytes(LIMIT_DISABLED);
1105 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001106 }
1107 };
1108
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001109 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001110 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001111 public void onClick(View v) {
1112 final boolean restrictBackground = !mAppRestrict.isChecked();
1113
1114 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001115 // enabling restriction; show confirmation dialog which
1116 // eventually calls setRestrictBackground() once user
1117 // confirms.
1118 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001119 } else {
1120 setAppRestrictBackground(false);
1121 }
1122 }
1123 };
1124
Jeff Sharkey8a503642011-06-10 13:31:21 -07001125 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001126 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001127 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001128 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001129 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001130
1131 // TODO: sigh, remove this hack once we understand 6450986
1132 if (mUidDetailProvider == null || app == null) return;
1133
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001134 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001135 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001136 }
1137 };
1138
1139 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001140 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001141 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1142 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1143 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001144 // show cycle editor; will eventually call setPolicyCycleDay()
1145 // when user finishes editing.
1146 CycleEditorFragment.show(DataUsageSummary.this);
1147
1148 // reset spinner to something other than "change cycle..."
1149 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001150
1151 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001152 if (LOGD) {
1153 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1154 + cycle.end + "]");
1155 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001156
1157 // update chart to show selected cycle, and update detail data
1158 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001159 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001160
1161 updateDetailData();
1162 }
1163 }
1164
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001165 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001166 public void onNothingSelected(AdapterView<?> parent) {
1167 // ignored
1168 }
1169 };
1170
1171 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001172 * Update details based on {@link #mChart} inspection range depending on
1173 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1174 * of applications data usage, and when {@link #isAppDetailMode()} update
1175 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001176 */
1177 private void updateDetailData() {
1178 if (LOGD) Log.d(TAG, "updateDetailData()");
1179
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001180 final long start = mChart.getInspectStart();
1181 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001182 final long now = System.currentTimeMillis();
1183
1184 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001185
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001186 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001187 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001188 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001189 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001190 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001191 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001192 final long foregroundBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001193 final long totalBytes = defaultBytes + foregroundBytes;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001194
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001195 if (mAppTotal != null) {
1196 mAppTotal.setText(Formatter.formatFileSize(context, totalBytes));
1197 }
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001198 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1199 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1200
1201 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001202 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001203
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001204 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001205
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001206 mCycleSummary.setVisibility(View.GONE);
1207
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001208 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001209 if (mChartData != null) {
1210 entry = mChartData.network.getValues(start, end, now, null);
1211 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001212
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001213 mCycleSummary.setVisibility(View.VISIBLE);
1214
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001215 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001216 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001217 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001218 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001219
1220 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1221 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001222 mCycleSummary.setText(totalPhrase);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001223
László Dávid0b8bf4e2012-10-24 23:31:47 +02001224 if (TAB_MOBILE.equals(mCurrentTab) || TAB_3G.equals(mCurrentTab)
1225 || TAB_4G.equals(mCurrentTab)) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001226 if (isAppDetailMode()) {
1227 mDisclaimer.setVisibility(View.GONE);
1228 } else {
1229 mDisclaimer.setVisibility(View.VISIBLE);
1230 }
Jeff Sharkeye557c332012-04-13 16:04:07 -07001231 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001232 mDisclaimer.setVisibility(View.GONE);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001233 }
1234
Jeff Sharkey92811822012-05-04 11:47:29 -07001235 // initial layout is finished above, ensure we have transitions
1236 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001237 }
1238
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001239 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1240 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001241 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001242 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001243 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001244 }
1245
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001246 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001247 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1248 mChartData = data;
1249 mChart.bindNetworkStats(mChartData.network);
1250 mChart.bindDetailNetworkStats(mChartData.detail);
1251
1252 // calcuate policy cycles based on available data
1253 updatePolicy(true);
1254 updateAppDetail();
1255
1256 // force scroll to top of body when showing detail
1257 if (mChartData.detail != null) {
1258 mListView.smoothScrollToPosition(0);
1259 }
1260 }
1261
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001262 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001263 public void onLoaderReset(Loader<ChartData> loader) {
1264 mChartData = null;
1265 mChart.bindNetworkStats(null);
1266 mChart.bindDetailNetworkStats(null);
1267 }
1268 };
1269
1270 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001271 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001272 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001273 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001274 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001275 }
1276
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001277 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001278 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001279 final int[] restrictedUids = mPolicyManager.getUidsWithPolicy(
Jeff Sharkeye557c332012-04-13 16:04:07 -07001280 POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001281 mAdapter.bindStats(data, restrictedUids);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001282 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001283 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001284
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001285 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001286 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001287 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001288 updateEmptyVisible();
1289 }
1290
1291 private void updateEmptyVisible() {
1292 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1293 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001294 mStupidPadding.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001295 }
1296 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001297
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001298 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001299 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001300 final Context context = getActivity();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001301 if (hasReadyMobileRadio(context)) {
1302 final TelephonyManager tele = TelephonyManager.from(context);
1303 return mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001304 } else {
1305 return false;
1306 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001307 }
1308
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001309 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001310 private void setMobilePolicySplit(boolean split) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001311 final Context context = getActivity();
1312 if (hasReadyMobileRadio(context)) {
1313 final TelephonyManager tele = TelephonyManager.from(context);
1314 mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split);
1315 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001316 }
1317
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001318 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001319 final TelephonyManager tele = TelephonyManager.from(context);
1320 final String actualSubscriberId = tele.getSubscriberId();
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -08001321 return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001322 }
1323
Jeff Sharkey8a503642011-06-10 13:31:21 -07001324 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001325 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001326 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001327 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001328 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001329
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001330 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001331 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001332 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001333 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001334
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001335 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001336 public void requestWarningEdit() {
1337 WarningEditorFragment.show(DataUsageSummary.this);
1338 }
1339
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001340 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001341 public void requestLimitEdit() {
1342 LimitEditorFragment.show(DataUsageSummary.this);
1343 }
1344 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001345
1346 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001347 * List item that reflects a specific data usage cycle.
1348 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001349 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001350 public CharSequence label;
1351 public long start;
1352 public long end;
1353
Jeff Sharkey8a503642011-06-10 13:31:21 -07001354 CycleItem(CharSequence label) {
1355 this.label = label;
1356 }
1357
1358 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001359 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001360 this.start = start;
1361 this.end = end;
1362 }
1363
Jeff Sharkey8a503642011-06-10 13:31:21 -07001364 @Override
1365 public String toString() {
1366 return label.toString();
1367 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001368
1369 @Override
1370 public boolean equals(Object o) {
1371 if (o instanceof CycleItem) {
1372 final CycleItem another = (CycleItem) o;
1373 return start == another.start && end == another.end;
1374 }
1375 return false;
1376 }
1377
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001378 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001379 public int compareTo(CycleItem another) {
1380 return Long.compare(start, another.start);
1381 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001382 }
1383
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001384 private static final StringBuilder sBuilder = new StringBuilder(50);
1385 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1386 sBuilder, Locale.getDefault());
1387
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001388 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001389 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001390
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001391 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001392 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001393 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1394 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001395 }
1396 }
1397
Jeff Sharkey8a503642011-06-10 13:31:21 -07001398 /**
1399 * Special-case data usage cycle that triggers dialog to change
1400 * {@link NetworkPolicy#cycleDay}.
1401 */
1402 public static class CycleChangeItem extends CycleItem {
1403 public CycleChangeItem(Context context) {
1404 super(context.getString(R.string.data_usage_change_cycle));
1405 }
1406 }
1407
1408 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001409 private boolean mChangePossible = false;
1410 private boolean mChangeVisible = false;
1411
1412 private final CycleChangeItem mChangeItem;
1413
Jeff Sharkey8a503642011-06-10 13:31:21 -07001414 public CycleAdapter(Context context) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001415 super(context, R.layout.data_usage_cycle_item);
1416 setDropDownViewResource(R.layout.data_usage_cycle_item_dropdown);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001417 mChangeItem = new CycleChangeItem(context);
1418 }
1419
1420 public void setChangePossible(boolean possible) {
1421 mChangePossible = possible;
1422 updateChange();
1423 }
1424
1425 public void setChangeVisible(boolean visible) {
1426 mChangeVisible = visible;
1427 updateChange();
1428 }
1429
1430 private void updateChange() {
1431 remove(mChangeItem);
1432 if (mChangePossible && mChangeVisible) {
1433 add(mChangeItem);
1434 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001435 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001436
1437 /**
1438 * Find position of {@link CycleItem} in this adapter which is nearest
1439 * the given {@link CycleItem}.
1440 */
1441 public int findNearestPosition(CycleItem target) {
1442 if (target != null) {
1443 final int count = getCount();
1444 for (int i = count - 1; i >= 0; i--) {
1445 final CycleItem item = getItem(i);
1446 if (item instanceof CycleChangeItem) {
1447 continue;
1448 } else if (item.compareTo(target) >= 0) {
1449 return i;
1450 }
1451 }
1452 }
1453 return 0;
1454 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001455 }
1456
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001457 public static class AppItem implements Comparable<AppItem>, Parcelable {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001458 public static final int CATEGORY_USER = 0;
1459 public static final int CATEGORY_APP_TITLE = 1;
1460 public static final int CATEGORY_APP = 2;
1461
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001462 public final int key;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001463 public boolean restricted;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001464 public int category;
1465
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001466 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001467 public long total;
1468
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001469 public AppItem() {
1470 this.key = 0;
1471 }
1472
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001473 public AppItem(int key) {
1474 this.key = key;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001475 }
1476
1477 public AppItem(Parcel parcel) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001478 key = parcel.readInt();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001479 uids = parcel.readSparseBooleanArray();
1480 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001481 }
1482
1483 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001484 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001485 }
1486
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001487 @Override
1488 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001489 dest.writeInt(key);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001490 dest.writeSparseBooleanArray(uids);
1491 dest.writeLong(total);
1492 }
1493
1494 @Override
1495 public int describeContents() {
1496 return 0;
1497 }
1498
1499 @Override
1500 public int compareTo(AppItem another) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001501 int comparison = Integer.compare(category, another.category);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001502 if (comparison == 0) {
1503 comparison = Long.compare(another.total, total);
1504 }
1505 return comparison;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001506 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001507
1508 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1509 @Override
1510 public AppItem createFromParcel(Parcel in) {
1511 return new AppItem(in);
1512 }
1513
1514 @Override
1515 public AppItem[] newArray(int size) {
1516 return new AppItem[size];
1517 }
1518 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001519 }
1520
Jeff Sharkey8a503642011-06-10 13:31:21 -07001521 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001522 * Adapter of applications, sorted by total usage descending.
1523 */
1524 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001525 private final UidDetailProvider mProvider;
1526 private final int mInsetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001527 private final UserManager mUm;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001528
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001529 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001530 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001531
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001532 public DataUsageAdapter(final UserManager userManager, UidDetailProvider provider, int insetSide) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001533 mProvider = checkNotNull(provider);
1534 mInsetSide = insetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001535 mUm = userManager;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001536 }
1537
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001538 /**
1539 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1540 */
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001541 public void bindStats(NetworkStats stats, int[] restrictedUids) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001542 mItems.clear();
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001543 mLargest = 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001544
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001545 final int currentUserId = ActivityManager.getCurrentUser();
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001546 final List<UserHandle> profiles = mUm.getUserProfiles();
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001547 final SparseArray<AppItem> knownItems = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001548
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001549 NetworkStats.Entry entry = null;
1550 final int size = stats != null ? stats.size() : 0;
1551 for (int i = 0; i < size; i++) {
1552 entry = stats.getValues(i, entry);
1553
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001554 // Decide how to collapse items together
1555 final int uid = entry.uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001556
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001557 final int collapseKey;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001558 final int category;
1559 final int userId = UserHandle.getUserId(uid);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001560 if (UserHandle.isApp(uid)) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001561 if (profiles.contains(new UserHandle(userId))) {
1562 if (userId != currentUserId) {
1563 // Add to a managed user item.
1564 final int managedKey = UidDetailProvider.buildKeyForUser(userId);
1565 accumulate(managedKey, knownItems, entry,
1566 AppItem.CATEGORY_USER);
1567 }
1568 // Add to app item.
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001569 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001570 category = AppItem.CATEGORY_APP;
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001571 } else {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001572 // Add to other user item.
1573 collapseKey = UidDetailProvider.buildKeyForUser(userId);
1574 category = AppItem.CATEGORY_USER;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001575 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001576 } else if (uid == UID_REMOVED || uid == UID_TETHERING) {
1577 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001578 category = AppItem.CATEGORY_APP;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001579 } else {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001580 collapseKey = android.os.Process.SYSTEM_UID;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001581 category = AppItem.CATEGORY_APP;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001582 }
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001583 accumulate(collapseKey, knownItems, entry, category);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001584 }
1585
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001586 final int restrictedUidsMax = restrictedUids.length;
1587 for (int i = 0; i < restrictedUidsMax; ++i) {
1588 final int uid = restrictedUids[i];
1589 // Only splice in restricted state for current user or managed users
1590 if (!profiles.contains(new UserHandle(UserHandle.getUserId(uid)))) {
1591 continue;
1592 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001593
1594 AppItem item = knownItems.get(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001595 if (item == null) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001596 item = new AppItem(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001597 item.total = -1;
1598 mItems.add(item);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001599 knownItems.put(item.key, item);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001600 }
1601 item.restricted = true;
1602 }
1603
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001604 if (!mItems.isEmpty()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001605 final AppItem title = new AppItem();
1606 title.category = AppItem.CATEGORY_APP_TITLE;
1607 mItems.add(title);
1608 }
1609
Jeff Sharkey8a503642011-06-10 13:31:21 -07001610 Collections.sort(mItems);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001611 notifyDataSetChanged();
1612 }
1613
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001614 /**
1615 * Accumulate data usage of a network stats entry for the item mapped by the collapse key.
1616 * Creates the item if needed.
1617 *
1618 * @param collapseKey the collapse key used to map the item.
1619 * @param knownItems collection of known (already existing) items.
1620 * @param entry the network stats entry to extract data usage from.
1621 * @param itemCategory the item is categorized on the list view by this category. Must be
1622 * either AppItem.APP_ITEM_CATEGORY or AppItem.MANAGED_USER_ITEM_CATEGORY
1623 */
1624 private void accumulate(int collapseKey, final SparseArray<AppItem> knownItems,
1625 NetworkStats.Entry entry, int itemCategory) {
1626 final int uid = entry.uid;
1627 AppItem item = knownItems.get(collapseKey);
1628 if (item == null) {
1629 item = new AppItem(collapseKey);
1630 item.category = itemCategory;
1631 mItems.add(item);
1632 knownItems.put(item.key, item);
1633 }
1634 item.addUid(uid);
1635 item.total += entry.rxBytes + entry.txBytes;
1636 if (mLargest < item.total) {
1637 mLargest = item.total;
1638 }
1639 }
1640
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001641 @Override
1642 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001643 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001644 }
1645
1646 @Override
1647 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001648 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001649 }
1650
1651 @Override
1652 public long getItemId(int position) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001653 return mItems.get(position).key;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001654 }
1655
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001656 /**
1657 * See {@link #getItemViewType} for the view types.
1658 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001659 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001660 public int getViewTypeCount() {
1661 return 2;
1662 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001663
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001664 /**
1665 * Returns 1 for separator items and 0 for anything else.
1666 */
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001667 @Override
1668 public int getItemViewType(int position) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001669 final AppItem item = mItems.get(position);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001670 if (item.category == AppItem.CATEGORY_APP_TITLE) {
1671 return 1;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001672 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001673 return 0;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001674 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001675 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001676
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001677 @Override
1678 public boolean areAllItemsEnabled() {
1679 return false;
1680 }
1681
1682 @Override
1683 public boolean isEnabled(int position) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001684 if (position > mItems.size()) {
1685 throw new ArrayIndexOutOfBoundsException();
1686 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001687 return getItemViewType(position) == 0;
1688 }
1689
1690 @Override
1691 public View getView(int position, View convertView, ViewGroup parent) {
1692 final AppItem item = mItems.get(position);
1693 if (getItemViewType(position) == 1) {
1694 if (convertView == null) {
1695 convertView = inflateCategoryHeader(LayoutInflater.from(parent.getContext()),
1696 parent);
1697 }
1698
1699 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
1700 title.setText(R.string.data_usage_app);
1701
1702 } else {
1703 if (convertView == null) {
1704 convertView = LayoutInflater.from(parent.getContext()).inflate(
1705 R.layout.data_usage_item, parent, false);
1706
1707 if (mInsetSide > 0) {
1708 convertView.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
1709 }
1710 }
1711
1712 final Context context = parent.getContext();
1713
1714 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
1715 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1716 android.R.id.progress);
1717
1718 // kick off async load of app details
1719 UidDetailTask.bindView(mProvider, item, convertView);
1720
1721 if (item.restricted && item.total <= 0) {
1722 text1.setText(R.string.data_usage_app_restricted);
1723 progress.setVisibility(View.GONE);
1724 } else {
1725 text1.setText(Formatter.formatFileSize(context, item.total));
1726 progress.setVisibility(View.VISIBLE);
1727 }
1728
1729 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1730 progress.setProgress(percentTotal);
1731 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001732
1733 return convertView;
1734 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001735 }
1736
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001737 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001738 * Empty {@link Fragment} that controls display of UID details in
1739 * {@link DataUsageSummary}.
1740 */
1741 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001742 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001743
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001744 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001745 if (!parent.isAdded()) return;
1746
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001747 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001748 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001749
1750 final AppDetailsFragment fragment = new AppDetailsFragment();
1751 fragment.setArguments(args);
1752 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001753 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1754 ft.add(fragment, TAG_APP_DETAILS);
1755 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001756 ft.setBreadCrumbTitle(
1757 parent.getResources().getString(R.string.data_usage_app_summary_title));
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001758 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001759 }
1760
1761 @Override
1762 public void onStart() {
1763 super.onStart();
1764 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001765 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001766 target.updateBody();
1767 }
1768
1769 @Override
1770 public void onStop() {
1771 super.onStop();
1772 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001773 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001774 target.updateBody();
1775 }
1776 }
1777
1778 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001779 * Dialog to request user confirmation before setting
1780 * {@link NetworkPolicy#limitBytes}.
1781 */
1782 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001783 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001784 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001785
1786 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001787 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001788
Jeff Sharkey461842a2011-09-25 18:22:48 -07001789 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001790 final CharSequence message;
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001791 final long minLimitBytes = (long) (
1792 parent.mPolicyEditor.getPolicy(parent.mTemplate).warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001793 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001794
1795 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001796 final String currentTab = parent.mCurrentTab;
1797 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001798 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001799 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001800 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001801 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001802 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001803 } else if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001804 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001805 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001806 } else {
1807 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001808 }
1809
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001810 final Bundle args = new Bundle();
1811 args.putCharSequence(EXTRA_MESSAGE, message);
1812 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1813
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001814 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1815 dialog.setArguments(args);
1816 dialog.setTargetFragment(parent, 0);
1817 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1818 }
1819
1820 @Override
1821 public Dialog onCreateDialog(Bundle savedInstanceState) {
1822 final Context context = getActivity();
1823
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001824 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001825 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1826
1827 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1828 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001829 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001830
1831 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001832 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001833 public void onClick(DialogInterface dialog, int which) {
1834 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1835 if (target != null) {
1836 target.setPolicyLimitBytes(limitBytes);
1837 }
1838 }
1839 });
1840
1841 return builder.create();
1842 }
1843 }
1844
1845 /**
1846 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1847 */
1848 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001849 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001850
1851 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001852 if (!parent.isAdded()) return;
1853
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001854 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001855 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001856
1857 final CycleEditorFragment dialog = new CycleEditorFragment();
1858 dialog.setArguments(args);
1859 dialog.setTargetFragment(parent, 0);
1860 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1861 }
1862
1863 @Override
1864 public Dialog onCreateDialog(Bundle savedInstanceState) {
1865 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001866 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1867 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001868
1869 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1870 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1871
1872 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1873 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1874
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001875 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1876 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001877
1878 cycleDayPicker.setMinValue(1);
1879 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001880 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001881 cycleDayPicker.setWrapSelectorWheel(true);
1882
1883 builder.setTitle(R.string.data_usage_cycle_editor_title);
1884 builder.setView(view);
1885
1886 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1887 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001888 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001889 public void onClick(DialogInterface dialog, int which) {
Jeff Sharkeyd277de92013-03-25 15:11:25 -07001890 // clear focus to finish pending text edits
1891 cycleDayPicker.clearFocus();
1892
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001893 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001894 final String cycleTimezone = new Time().timezone;
1895 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001896 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001897 }
1898 });
1899
1900 return builder.create();
1901 }
1902 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001903
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001904 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001905 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1906 */
1907 public static class WarningEditorFragment extends DialogFragment {
1908 private static final String EXTRA_TEMPLATE = "template";
1909
1910 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001911 if (!parent.isAdded()) return;
1912
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001913 final Bundle args = new Bundle();
1914 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1915
1916 final WarningEditorFragment dialog = new WarningEditorFragment();
1917 dialog.setArguments(args);
1918 dialog.setTargetFragment(parent, 0);
1919 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1920 }
1921
1922 @Override
1923 public Dialog onCreateDialog(Bundle savedInstanceState) {
1924 final Context context = getActivity();
1925 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1926 final NetworkPolicyEditor editor = target.mPolicyEditor;
1927
1928 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1929 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1930
1931 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1932 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1933
1934 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1935 final long warningBytes = editor.getPolicyWarningBytes(template);
1936 final long limitBytes = editor.getPolicyLimitBytes(template);
1937
1938 bytesPicker.setMinValue(0);
1939 if (limitBytes != LIMIT_DISABLED) {
1940 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1941 } else {
1942 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1943 }
1944 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1945 bytesPicker.setWrapSelectorWheel(false);
1946
1947 builder.setTitle(R.string.data_usage_warning_editor_title);
1948 builder.setView(view);
1949
1950 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1951 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001952 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001953 public void onClick(DialogInterface dialog, int which) {
1954 // clear focus to finish pending text edits
1955 bytesPicker.clearFocus();
1956
1957 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1958 editor.setPolicyWarningBytes(template, bytes);
1959 target.updatePolicy(false);
1960 }
1961 });
1962
1963 return builder.create();
1964 }
1965 }
1966
1967 /**
1968 * Dialog to edit {@link NetworkPolicy#limitBytes}.
1969 */
1970 public static class LimitEditorFragment extends DialogFragment {
1971 private static final String EXTRA_TEMPLATE = "template";
1972
1973 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001974 if (!parent.isAdded()) return;
1975
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001976 final Bundle args = new Bundle();
1977 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1978
1979 final LimitEditorFragment dialog = new LimitEditorFragment();
1980 dialog.setArguments(args);
1981 dialog.setTargetFragment(parent, 0);
1982 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
1983 }
1984
1985 @Override
1986 public Dialog onCreateDialog(Bundle savedInstanceState) {
1987 final Context context = getActivity();
1988 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1989 final NetworkPolicyEditor editor = target.mPolicyEditor;
1990
1991 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1992 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1993
1994 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1995 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1996
1997 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1998 final long warningBytes = editor.getPolicyWarningBytes(template);
1999 final long limitBytes = editor.getPolicyLimitBytes(template);
2000
2001 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09002002 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002003 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
2004 } else {
2005 bytesPicker.setMinValue(0);
2006 }
2007 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
2008 bytesPicker.setWrapSelectorWheel(false);
2009
2010 builder.setTitle(R.string.data_usage_limit_editor_title);
2011 builder.setView(view);
2012
2013 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2014 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002015 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002016 public void onClick(DialogInterface dialog, int which) {
2017 // clear focus to finish pending text edits
2018 bytesPicker.clearFocus();
2019
2020 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
2021 editor.setPolicyLimitBytes(template, bytes);
2022 target.updatePolicy(false);
2023 }
2024 });
2025
2026 return builder.create();
2027 }
2028 }
2029 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07002030 * Dialog to request user confirmation before disabling data.
2031 */
2032 public static class ConfirmDataDisableFragment extends DialogFragment {
2033 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002034 if (!parent.isAdded()) return;
2035
Jeff Sharkey28130d92011-09-02 16:10:24 -07002036 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
2037 dialog.setTargetFragment(parent, 0);
2038 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
2039 }
2040
2041 @Override
2042 public Dialog onCreateDialog(Bundle savedInstanceState) {
2043 final Context context = getActivity();
2044
2045 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2046 builder.setMessage(R.string.data_usage_disable_mobile);
2047
2048 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002049 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07002050 public void onClick(DialogInterface dialog, int which) {
2051 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2052 if (target != null) {
2053 // TODO: extend to modify policy enabled flag.
2054 target.setMobileDataEnabled(false);
2055 }
2056 }
2057 });
2058 builder.setNegativeButton(android.R.string.cancel, null);
2059
2060 return builder.create();
2061 }
2062 }
2063
2064 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002065 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07002066 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002067 */
2068 public static class ConfirmRestrictFragment extends DialogFragment {
2069 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002070 if (!parent.isAdded()) return;
2071
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002072 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
2073 dialog.setTargetFragment(parent, 0);
2074 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
2075 }
2076
2077 @Override
2078 public Dialog onCreateDialog(Bundle savedInstanceState) {
2079 final Context context = getActivity();
2080
2081 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002082 builder.setTitle(R.string.data_usage_restrict_background_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07002083 if (Utils.hasMultipleUsers(context)) {
2084 builder.setMessage(R.string.data_usage_restrict_background_multiuser);
2085 } else {
2086 builder.setMessage(R.string.data_usage_restrict_background);
2087 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002088
2089 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002090 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002091 public void onClick(DialogInterface dialog, int which) {
2092 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2093 if (target != null) {
2094 target.setRestrictBackground(true);
2095 }
2096 }
2097 });
2098 builder.setNegativeButton(android.R.string.cancel, null);
2099
2100 return builder.create();
2101 }
2102 }
2103
2104 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002105 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
2106 * change has been denied, usually based on
2107 * {@link DataUsageSummary#hasLimitedNetworks()}.
2108 */
2109 public static class DeniedRestrictFragment extends DialogFragment {
2110 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002111 if (!parent.isAdded()) return;
2112
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002113 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
2114 dialog.setTargetFragment(parent, 0);
2115 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
2116 }
2117
2118 @Override
2119 public Dialog onCreateDialog(Bundle savedInstanceState) {
2120 final Context context = getActivity();
2121
2122 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2123 builder.setTitle(R.string.data_usage_app_restrict_background);
2124 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
2125 builder.setPositiveButton(android.R.string.ok, null);
2126
2127 return builder.create();
2128 }
2129 }
2130
2131 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002132 * Dialog to request user confirmation before setting
2133 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
2134 */
2135 public static class ConfirmAppRestrictFragment extends DialogFragment {
2136 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002137 if (!parent.isAdded()) return;
2138
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002139 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2140 dialog.setTargetFragment(parent, 0);
2141 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2142 }
2143
2144 @Override
2145 public Dialog onCreateDialog(Bundle savedInstanceState) {
2146 final Context context = getActivity();
2147
2148 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002149 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2150 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2151
2152 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002153 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002154 public void onClick(DialogInterface dialog, int which) {
2155 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2156 if (target != null) {
2157 target.setAppRestrictBackground(true);
2158 }
2159 }
2160 });
2161 builder.setNegativeButton(android.R.string.cancel, null);
2162
2163 return builder.create();
2164 }
2165 }
2166
2167 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002168 * Compute default tab that should be selected, based on
2169 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2170 */
2171 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002172 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
2173 if (template == null) return null;
2174
2175 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002176 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002177 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002178 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002179 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002180 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002181 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002182 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002183 return TAB_WIFI;
2184 default:
2185 return null;
2186 }
2187 }
2188
2189 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002190 * Background task that loads {@link UidDetail}, binding to
2191 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002192 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002193 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2194 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002195 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002196 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002197
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002198 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002199 mProvider = checkNotNull(provider);
2200 mItem = checkNotNull(item);
2201 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002202 }
2203
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002204 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002205 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002206 final UidDetailTask existing = (UidDetailTask) target.getTag();
2207 if (existing != null) {
2208 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002209 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002210
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002211 final UidDetail cachedDetail = provider.getUidDetail(item.key, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002212 if (cachedDetail != null) {
2213 bindView(cachedDetail, target);
2214 } else {
2215 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2216 AsyncTask.THREAD_POOL_EXECUTOR));
2217 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002218 }
2219
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002220 private static void bindView(UidDetail detail, View target) {
2221 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2222 final TextView title = (TextView) target.findViewById(android.R.id.title);
2223
2224 if (detail != null) {
2225 icon.setImageDrawable(detail.icon);
2226 title.setText(detail.label);
2227 } else {
2228 icon.setImageDrawable(null);
2229 title.setText(null);
2230 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002231 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002232
2233 @Override
2234 protected void onPreExecute() {
2235 bindView(null, mTarget);
2236 }
2237
2238 @Override
2239 protected UidDetail doInBackground(Void... params) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002240 return mProvider.getUidDetail(mItem.key, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002241 }
2242
2243 @Override
2244 protected void onPostExecute(UidDetail result) {
2245 bindView(result, mTarget);
2246 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002247 }
2248
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002249 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002250 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002251 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002252 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002253 if (TEST_RADIOS) {
2254 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2255 }
2256
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002257 final ConnectivityManager conn = ConnectivityManager.from(context);
2258 final TelephonyManager tele = TelephonyManager.from(context);
2259
2260 // require both supported network and ready SIM
2261 return conn.isNetworkSupported(TYPE_MOBILE) && tele.getSimState() == SIM_STATE_READY;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002262 }
2263
2264 /**
2265 * Test if device has a mobile 4G data radio.
2266 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002267 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002268 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2269 return false;
2270 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002271 if (TEST_RADIOS) {
2272 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2273 }
2274
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002275 final ConnectivityManager conn = ConnectivityManager.from(context);
2276 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002277
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002278 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Wink Saville55434042012-06-14 12:33:43 -07002279 final boolean hasLte = (tele.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE)
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002280 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002281 return hasWimax || hasLte;
2282 }
2283
2284 /**
2285 * Test if device has a Wi-Fi data radio.
2286 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002287 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002288 if (TEST_RADIOS) {
2289 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2290 }
2291
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002292 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002293 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002294 }
2295
2296 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002297 * Test if device has an ethernet network connection.
2298 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002299 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002300 if (TEST_RADIOS) {
2301 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2302 }
2303
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002304 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002305 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2306
2307 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002308 if (mStatsSession != null) {
2309 try {
2310 ethernetBytes = mStatsSession.getSummaryForNetwork(
2311 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2312 .getTotalBytes();
2313 } catch (RemoteException e) {
2314 throw new RuntimeException(e);
2315 }
2316 } else {
2317 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002318 }
2319
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002320 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002321 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002322 }
2323
2324 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002325 * Inflate a {@link Preference} style layout, adding the given {@link View}
2326 * widget into {@link android.R.id#widget_frame}.
2327 */
2328 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2329 final View view = inflater.inflate(R.layout.preference, root, false);
2330 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2331 android.R.id.widget_frame);
2332 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2333 return view;
2334 }
2335
Jeff Sharkeyb6548462014-07-21 15:38:06 -07002336 private static View inflateCategoryHeader(LayoutInflater inflater, ViewGroup root) {
2337 final TypedArray a = inflater.getContext().obtainStyledAttributes(null,
2338 com.android.internal.R.styleable.Preference,
2339 com.android.internal.R.attr.preferenceCategoryStyle, 0);
2340 final int resId = a.getResourceId(com.android.internal.R.styleable.Preference_layout, 0);
2341 return inflater.inflate(resId, root, false);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002342 }
2343
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002344 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002345 * Test if any networks are currently limited.
2346 */
2347 private boolean hasLimitedNetworks() {
2348 return !buildLimitedNetworksList().isEmpty();
2349 }
2350
2351 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002352 * Build string describing currently limited networks, which defines when
2353 * background data is restricted.
2354 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002355 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002356 private CharSequence buildLimitedNetworksString() {
2357 final List<CharSequence> limited = buildLimitedNetworksList();
2358
2359 // handle case where no networks limited
2360 if (limited.isEmpty()) {
2361 limited.add(getText(R.string.data_usage_list_none));
2362 }
2363
2364 return TextUtils.join(limited);
2365 }
2366
2367 /**
2368 * Build list of currently limited networks, which defines when background
2369 * data is restricted.
2370 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002371 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002372 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002373 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002374
2375 // build combined list of all limited networks
2376 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002377
2378 final TelephonyManager tele = TelephonyManager.from(context);
2379 if (tele.getSimState() == SIM_STATE_READY) {
2380 final String subscriberId = getActiveSubscriberId(context);
2381 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2382 limited.add(getText(R.string.data_usage_list_mobile));
2383 }
2384 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2385 limited.add(getText(R.string.data_usage_tab_3g));
2386 }
2387 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2388 limited.add(getText(R.string.data_usage_tab_4g));
2389 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002390 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002391
2392 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002393 limited.add(getText(R.string.data_usage_tab_wifi));
2394 }
2395 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2396 limited.add(getText(R.string.data_usage_tab_ethernet));
2397 }
2398
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002399 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002400 }
2401
2402 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002403 * Inset both selector and divider {@link Drawable} on the given
2404 * {@link ListView} by the requested dimensions.
2405 */
2406 private static void insetListViewDrawables(ListView view, int insetSide) {
2407 final Drawable selector = view.getSelector();
2408 final Drawable divider = view.getDivider();
2409
2410 // fully unregister these drawables so callbacks can be maintained after
2411 // wrapping below.
2412 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2413 view.setSelector(stub);
2414 view.setDivider(stub);
2415
2416 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2417 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2418 }
2419
2420 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002421 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002422 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002423 */
2424 private static void setPreferenceTitle(View parent, int resId) {
2425 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2426 title.setText(resId);
2427 }
2428
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002429 /**
2430 * Set {@link android.R.id#summary} for a preference view inflated with
2431 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2432 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002433 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002434 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2435 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002436 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002437 }
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002438
2439 /**
2440 * For search
2441 */
2442 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -07002443 new BaseSearchIndexProvider() {
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002444 @Override
2445 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
2446 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
2447
2448 final Resources res = context.getResources();
2449
2450 // Add fragment title
2451 SearchIndexableRaw data = new SearchIndexableRaw(context);
2452 data.title = res.getString(R.string.data_usage_summary_title);
2453 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2454 result.add(data);
2455
2456 // Mobile data
2457 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002458 data.key = DATA_USAGE_ENABLE_MOBILE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002459 data.title = res.getString(R.string.data_usage_enable_mobile);
2460 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2461 result.add(data);
2462
2463 // Set mobile data limit
2464 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002465 data.key = DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002466 data.title = res.getString(R.string.data_usage_disable_mobile_limit);
2467 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2468 result.add(data);
2469
Fabrice Di Megliof2a52262014-04-17 17:20:27 -07002470 // Data usage cycle
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002471 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002472 data.key = DATA_USAGE_CYCLE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002473 data.title = res.getString(R.string.data_usage_cycle);
2474 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2475 result.add(data);
2476
2477 return result;
2478 }
2479 };
2480
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002481}