blob: 872de1acd2fd97fc87b9304a7270216f05e6e67c [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);
PauloftheWesta4b8fb32014-09-18 10:12:25 -0700516 mMenuCellularNetworks.setVisible(hasReadyMobileRadio(context)
517 && !appDetailMode && isOwner);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700518
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700519 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
520 String helpUrl;
521 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800522 HelpUtils.prepareHelpMenuItem(context, help, helpUrl);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700523 } else {
524 help.setVisible(false);
525 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700526
527 updateMenuTitles();
528 }
529
530 private void updateMenuTitles() {
531 if (mPolicyManager.getRestrictBackground()) {
532 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_allow_background);
533 } else {
534 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_restrict_background);
535 }
536
537 if (mShowWifi) {
538 mMenuShowWifi.setTitle(R.string.data_usage_menu_hide_wifi);
539 } else {
540 mMenuShowWifi.setTitle(R.string.data_usage_menu_show_wifi);
541 }
542
543 if (mShowEthernet) {
544 mMenuShowEthernet.setTitle(R.string.data_usage_menu_hide_ethernet);
545 } else {
546 mMenuShowEthernet.setTitle(R.string.data_usage_menu_show_ethernet);
547 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700548 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700549
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700550 @Override
551 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700552 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700553 case R.id.data_usage_menu_restrict_background: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700554 final boolean restrictBackground = !mPolicyManager.getRestrictBackground();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700555 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800556 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700557 } else {
558 // no confirmation to drop restriction
559 setRestrictBackground(false);
560 }
561 return true;
562 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700563 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700564 mShowWifi = !mShowWifi;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700565 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700566 updateMenuTitles();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700567 updateTabs();
568 return true;
569 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700570 case R.id.data_usage_menu_show_ethernet: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700571 mShowEthernet = !mShowEthernet;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700572 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700573 updateMenuTitles();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700574 updateTabs();
575 return true;
576 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700577 case R.id.data_usage_menu_sim_cards: {
578 // TODO: hook up to sim cards
579 return true;
580 }
581 case R.id.data_usage_menu_cellular_networks: {
582 final Intent intent = new Intent(Intent.ACTION_MAIN);
583 intent.setComponent(new ComponentName("com.android.phone",
584 "com.android.phone.MobileNetworkSettings"));
585 startActivity(intent);
586 return true;
587 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700588 case R.id.data_usage_menu_metered: {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800589 final SettingsActivity sa = (SettingsActivity) getActivity();
590 sa.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700591 R.string.data_usage_metered_title, null, this, 0);
592 return true;
593 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700594 }
595 return false;
596 }
597
Jeff Sharkey94a90952011-06-13 22:31:09 -0700598 @Override
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700599 public void onDestroy() {
Jeff Sharkey94a90952011-06-13 22:31:09 -0700600 mDataEnabledView = null;
601 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700602
603 mUidDetailProvider.clearCache();
604 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700605
606 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700607
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800608 super.onDestroy();
609 }
610
Jeff Sharkey8a503642011-06-10 13:31:21 -0700611 /**
Jeff Sharkey92811822012-05-04 11:47:29 -0700612 * Build and assign {@link LayoutTransition} to various containers. Should
613 * only be assigned after initial layout is complete.
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700614 */
Jeff Sharkey92811822012-05-04 11:47:29 -0700615 private void ensureLayoutTransitions() {
616 // skip when already setup
617 if (mChart.getLayoutTransition() != null) return;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700618
Jeff Sharkey92811822012-05-04 11:47:29 -0700619 mTabsContainer.setLayoutTransition(buildLayoutTransition());
620 mHeader.setLayoutTransition(buildLayoutTransition());
621 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700622
Jeff Sharkey92811822012-05-04 11:47:29 -0700623 final LayoutTransition chartTransition = buildLayoutTransition();
624 chartTransition.disableTransitionType(LayoutTransition.APPEARING);
625 chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
626 mChart.setLayoutTransition(chartTransition);
627 }
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700628
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700629 private static LayoutTransition buildLayoutTransition() {
630 final LayoutTransition transition = new LayoutTransition();
631 if (TEST_ANIM) {
632 transition.setDuration(1500);
633 }
634 transition.setAnimateParentHierarchy(false);
635 return transition;
636 }
637
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700638 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700639 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700640 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
641 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700642 */
643 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700644 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700645 mTabHost.clearAllTabs();
646
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700647 final boolean mobileSplit = isMobilePolicySplit();
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700648 if (mobileSplit && hasReadyMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700649 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
650 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700651 } else if (hasReadyMobileRadio(context)) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700652 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700653 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700654 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700655 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
656 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700657 if (mShowEthernet && hasEthernet(context)) {
658 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
659 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700660
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700661 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700662 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
663 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
664 if (mIntentTab != null) {
665 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700666 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700667 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700668 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700669 mTabHost.setCurrentTabByTag(mIntentTab);
670 }
671 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700672 } else if (noTabs) {
673 // no usable tabs, so hide body
674 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700675 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700676 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700677 }
678 }
679
Jeff Sharkey8a503642011-06-10 13:31:21 -0700680 /**
681 * Factory that provide empty {@link View} to make {@link TabHost} happy.
682 */
683 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700684 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700685 public View createTabContent(String tag) {
686 return new View(mTabHost.getContext());
687 }
688 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700689
Jeff Sharkey8a503642011-06-10 13:31:21 -0700690 /**
691 * Build {@link TabSpec} with thin indicator, and empty content.
692 */
693 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700694 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
695 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700696 }
697
698 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700699 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700700 public void onTabChanged(String tabId) {
701 // user changed tab; update body
702 updateBody();
703 }
704 };
705
706 /**
707 * Update body content based on current tab. Loads
708 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
709 * binds them to visible controls.
710 */
711 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700712 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700713 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700714
Jeff Sharkeya662e492011-06-18 21:57:06 -0700715 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700716 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700717 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700718
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700719 if (currentTab == null) {
720 Log.w(TAG, "no tab selected; hiding body");
721 mListView.setVisibility(View.GONE);
722 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700723 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700724 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700725 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700726
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700727 mCurrentTab = currentTab;
728
Jeff Sharkey8a503642011-06-10 13:31:21 -0700729 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
730
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700731 mDataEnabledSupported = isOwner;
732 mDisableAtLimitSupported = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700733
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700734 // TODO: remove mobile tabs when SIM isn't ready
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700735
Jeff Sharkey8a503642011-06-10 13:31:21 -0700736 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700737 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
738 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700739 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700740
741 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700742 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
743 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
744 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700745 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700746
747 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700748 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
749 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
750 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700751 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700752
753 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700754 // wifi doesn't have any controls
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700755 mDataEnabledSupported = false;
756 mDisableAtLimitSupported = false;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700757 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700758
759 } else if (TAB_ETHERNET.equals(currentTab)) {
760 // ethernet doesn't have any controls
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700761 mDataEnabledSupported = false;
762 mDisableAtLimitSupported = false;
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700763 mTemplate = buildTemplateEthernet();
764
765 } else {
766 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700767 }
768
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700769 // kick off loader for network history
770 // TODO: consider chaining two loaders together instead of reloading
771 // network history when showing app detail.
772 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700773 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700774
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700775 // detail mode can change visible menus, invalidate
776 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700777
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700778 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700779 }
780
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700781 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700782 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700783 }
784
785 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700786 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700787 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700788 */
789 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700790 final Context context = getActivity();
791 final PackageManager pm = context.getPackageManager();
792 final LayoutInflater inflater = getActivity().getLayoutInflater();
793
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700794 if (isAppDetailMode()) {
795 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700796 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700797 } else {
798 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700799 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700800
801 // hide detail stats when not in detail mode
802 mChart.bindDetailNetworkStats(null);
803 return;
804 }
805
806 // remove warning/limit sweeps while in detail mode
807 mChart.bindNetworkPolicy(null);
808
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700809 // show icon and all labels appearing under this app
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700810 final int uid = mCurrentApp.key;
811 final UidDetail detail = mUidDetailProvider.getUidDetail(uid, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700812 mAppIcon.setImageDrawable(detail.icon);
813
814 mAppTitles.removeAllViews();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700815
816 View title = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700817 if (detail.detailLabels != null) {
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100818 final int n = detail.detailLabels.length;
819 for (int i = 0; i < n; ++i) {
820 CharSequence label = detail.detailLabels[i];
821 CharSequence contentDescription = detail.detailContentDescriptions[i];
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700822 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100823 TextView appTitle = (TextView) title.findViewById(R.id.app_title);
824 appTitle.setText(label);
825 appTitle.setContentDescription(contentDescription);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700826 mAppTitles.addView(title);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700827 }
828 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700829 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +0100830 TextView appTitle = (TextView) title.findViewById(R.id.app_title);
831 appTitle.setText(detail.label);
832 appTitle.setContentDescription(detail.contentDescription);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700833 mAppTitles.addView(title);
834 }
835
836 // Remember last slot for summary
837 if (title != null) {
838 mAppTotal = (TextView) title.findViewById(R.id.app_summary);
839 } else {
840 mAppTotal = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700841 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700842
843 // enable settings button when package provides it
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700844 final String[] packageNames = pm.getPackagesForUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700845 if (packageNames != null && packageNames.length > 0) {
846 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700847 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
848
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700849 // Search for match across all packages
850 boolean matchFound = false;
851 for (String packageName : packageNames) {
852 mAppSettingsIntent.setPackage(packageName);
853 if (pm.resolveActivity(mAppSettingsIntent, 0) != null) {
854 matchFound = true;
855 break;
856 }
857 }
858
Zoltan Szatmary-Banf20d39e2014-08-07 15:27:08 +0100859 mAppSettings.setOnClickListener(new OnClickListener() {
860 @Override
861 public void onClick(View v) {
862 if (!isAdded()) {
863 return;
864 }
865
866 // TODO: target towards entire UID instead of just first package
867 getActivity().startActivityAsUser(mAppSettingsIntent,
868 new UserHandle(UserHandle.getUserId(uid)));
869 }
870 });
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700871 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -0700872 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700873
874 } else {
875 mAppSettingsIntent = null;
Zoltan Szatmary-Banf20d39e2014-08-07 15:27:08 +0100876 mAppSettings.setOnClickListener(null);
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700877 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700878 }
879
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700880 updateDetailData();
881
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700882 if (UserHandle.isApp(uid) && !mPolicyManager.getRestrictBackground()
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700883 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700884 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800885 setPreferenceSummary(mAppRestrictView,
886 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700887
888 mAppRestrictView.setVisibility(View.VISIBLE);
889 mAppRestrict.setChecked(getAppRestrictBackground());
890
891 } else {
892 mAppRestrictView.setVisibility(View.GONE);
893 }
894 }
895
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700896 private void setPolicyWarningBytes(long warningBytes) {
897 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700898 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700899 updatePolicy(false);
900 }
901
902 private void setPolicyLimitBytes(long limitBytes) {
903 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700904 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700905 updatePolicy(false);
906 }
907
Jeff Sharkey28130d92011-09-02 16:10:24 -0700908 /**
909 * Local cache of value, used to work around delay when
910 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
911 */
912 private Boolean mMobileDataEnabled;
913
914 private boolean isMobileDataEnabled() {
915 if (mMobileDataEnabled != null) {
916 // TODO: deprecate and remove this once enabled flag is on policy
917 return mMobileDataEnabled;
918 } else {
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700919 return mTelephonyManager.getDataEnabled();
Jeff Sharkey28130d92011-09-02 16:10:24 -0700920 }
921 }
922
923 private void setMobileDataEnabled(boolean enabled) {
924 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700925 mTelephonyManager.setDataEnabled(enabled);
Jeff Sharkey28130d92011-09-02 16:10:24 -0700926 mMobileDataEnabled = enabled;
927 updatePolicy(false);
928 }
929
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700930 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700931 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked()
932 && ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700933 }
934
935 private boolean isBandwidthControlEnabled() {
936 try {
937 return mNetworkService.isBandwidthControlEnabled();
938 } catch (RemoteException e) {
939 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
940 return false;
941 }
942 }
943
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700944 public void setRestrictBackground(boolean restrictBackground) {
945 mPolicyManager.setRestrictBackground(restrictBackground);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700946 updateMenuTitles();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700947 }
948
949 private boolean getAppRestrictBackground() {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700950 final int uid = mCurrentApp.key;
951 final int uidPolicy = mPolicyManager.getUidPolicy(uid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700952 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
953 }
954
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700955 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700956 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700957 final int uid = mCurrentApp.key;
958 mPolicyManager.setUidPolicy(
959 uid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700960 mAppRestrict.setChecked(restrictBackground);
961 }
962
Jeff Sharkey8a503642011-06-10 13:31:21 -0700963 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700964 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
965 * current {@link #mTemplate}.
966 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700967 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700968 boolean dataEnabledVisible = mDataEnabledSupported;
969 boolean disableAtLimitVisible = mDisableAtLimitSupported;
970
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700971 if (isAppDetailMode()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700972 dataEnabledVisible = false;
973 disableAtLimitVisible = false;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700974 }
975
Jeff Sharkey28130d92011-09-02 16:10:24 -0700976 // TODO: move enabled state directly into policy
977 if (TAB_MOBILE.equals(mCurrentTab)) {
978 mBinding = true;
979 mDataEnabled.setChecked(isMobileDataEnabled());
980 mBinding = false;
981 }
982
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700983 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
984 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700985 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700986 if (!isAppDetailMode()) {
987 mChart.bindNetworkPolicy(policy);
988 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700989
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700990 } else {
991 // controls are disabled; don't bind warning/limit sweeps
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700992 disableAtLimitVisible = false;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700993 mChart.bindNetworkPolicy(null);
994 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700995
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700996 mDataEnabledView.setVisibility(dataEnabledVisible ? View.VISIBLE : View.GONE);
997 mDisableAtLimitView.setVisibility(disableAtLimitVisible ? View.VISIBLE : View.GONE);
998
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700999 if (refreshCycle) {
1000 // generate cycle list based on policy and available history
1001 updateCycleList(policy);
1002 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001003 }
1004
1005 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001006 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
1007 * and available {@link NetworkStatsHistory} data. Always selects the newest
1008 * item, updating the inspection range on {@link #mChart}.
1009 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001010 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001011 // stash away currently selected cycle to try restoring below
1012 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -07001013 mCycleAdapter.clear();
1014
1015 final Context context = mCycleSpinner.getContext();
1016
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001017 long historyStart = Long.MAX_VALUE;
1018 long historyEnd = Long.MIN_VALUE;
1019 if (mChartData != null) {
1020 historyStart = mChartData.network.getStart();
1021 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001022 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001023
Jeff Sharkey461842a2011-09-25 18:22:48 -07001024 final long now = System.currentTimeMillis();
1025 if (historyStart == Long.MAX_VALUE) historyStart = now;
1026 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001027
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001028 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001029 if (policy != null) {
1030 // find the next cycle boundary
1031 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001032
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001033 // walk backwards, generating all valid cycle ranges
1034 while (cycleEnd > historyStart) {
1035 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
1036 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
1037 + historyStart);
1038 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1039 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001040 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001041 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001042
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001043 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001044 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001045 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001046
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001047 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001048 // no policy defined cycles; show entry for each four-week period
1049 long cycleEnd = historyEnd;
1050 while (cycleEnd > historyStart) {
1051 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
1052 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1053 cycleEnd = cycleStart;
1054 }
1055
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001056 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001057 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001058
1059 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001060 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001061 final int position = mCycleAdapter.findNearestPosition(previousItem);
1062 mCycleSpinner.setSelection(position);
1063
1064 // only force-update cycle when changed; skipping preserves any
1065 // user-defined inspection region.
1066 final CycleItem selectedItem = mCycleAdapter.getItem(position);
1067 if (!Objects.equal(selectedItem, previousItem)) {
1068 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1069 } else {
1070 // but still kick off loader for detailed list
1071 updateDetailData();
1072 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001073 } else {
1074 updateDetailData();
1075 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001076 }
1077
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001078 private View.OnClickListener mDataEnabledListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001079 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001080 public void onClick(View v) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001081 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001082
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001083 final boolean dataEnabled = !mDataEnabled.isChecked();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001084 final String currentTab = mCurrentTab;
1085 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001086 if (dataEnabled) {
1087 setMobileDataEnabled(true);
1088 } else {
1089 // disabling data; show confirmation dialog which eventually
1090 // calls setMobileDataEnabled() once user confirms.
1091 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1092 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001093 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001094
Jeff Sharkey28130d92011-09-02 16:10:24 -07001095 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001096 }
1097 };
1098
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001099 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001100 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001101 public void onClick(View v) {
1102 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001103 if (disableAtLimit) {
1104 // enabling limit; show confirmation dialog which eventually
1105 // calls setPolicyLimitBytes() once user confirms.
1106 ConfirmLimitFragment.show(DataUsageSummary.this);
1107 } else {
1108 setPolicyLimitBytes(LIMIT_DISABLED);
1109 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001110 }
1111 };
1112
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001113 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001114 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001115 public void onClick(View v) {
1116 final boolean restrictBackground = !mAppRestrict.isChecked();
1117
1118 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001119 // enabling restriction; show confirmation dialog which
1120 // eventually calls setRestrictBackground() once user
1121 // confirms.
1122 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001123 } else {
1124 setAppRestrictBackground(false);
1125 }
1126 }
1127 };
1128
Jeff Sharkey8a503642011-06-10 13:31:21 -07001129 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001130 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001131 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001132 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001133 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001134
1135 // TODO: sigh, remove this hack once we understand 6450986
1136 if (mUidDetailProvider == null || app == null) return;
1137
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001138 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001139 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001140 }
1141 };
1142
1143 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001144 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001145 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1146 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1147 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001148 // show cycle editor; will eventually call setPolicyCycleDay()
1149 // when user finishes editing.
1150 CycleEditorFragment.show(DataUsageSummary.this);
1151
1152 // reset spinner to something other than "change cycle..."
1153 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001154
1155 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001156 if (LOGD) {
1157 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1158 + cycle.end + "]");
1159 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001160
1161 // update chart to show selected cycle, and update detail data
1162 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001163 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001164
1165 updateDetailData();
1166 }
1167 }
1168
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001169 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001170 public void onNothingSelected(AdapterView<?> parent) {
1171 // ignored
1172 }
1173 };
1174
1175 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001176 * Update details based on {@link #mChart} inspection range depending on
1177 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1178 * of applications data usage, and when {@link #isAppDetailMode()} update
1179 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001180 */
1181 private void updateDetailData() {
1182 if (LOGD) Log.d(TAG, "updateDetailData()");
1183
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001184 final long start = mChart.getInspectStart();
1185 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001186 final long now = System.currentTimeMillis();
1187
1188 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001189
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001190 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001191 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001192 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001193 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001194 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001195 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001196 final long foregroundBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001197 final long totalBytes = defaultBytes + foregroundBytes;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001198
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001199 if (mAppTotal != null) {
1200 mAppTotal.setText(Formatter.formatFileSize(context, totalBytes));
1201 }
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001202 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1203 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1204
1205 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001206 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001207
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001208 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001209
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001210 mCycleSummary.setVisibility(View.GONE);
1211
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001212 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001213 if (mChartData != null) {
1214 entry = mChartData.network.getValues(start, end, now, null);
1215 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001216
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001217 mCycleSummary.setVisibility(View.VISIBLE);
1218
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001219 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001220 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001221 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001222 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001223
1224 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1225 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001226 mCycleSummary.setText(totalPhrase);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001227
László Dávid0b8bf4e2012-10-24 23:31:47 +02001228 if (TAB_MOBILE.equals(mCurrentTab) || TAB_3G.equals(mCurrentTab)
1229 || TAB_4G.equals(mCurrentTab)) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001230 if (isAppDetailMode()) {
1231 mDisclaimer.setVisibility(View.GONE);
1232 } else {
1233 mDisclaimer.setVisibility(View.VISIBLE);
1234 }
Jeff Sharkeye557c332012-04-13 16:04:07 -07001235 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001236 mDisclaimer.setVisibility(View.GONE);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001237 }
1238
Jeff Sharkey92811822012-05-04 11:47:29 -07001239 // initial layout is finished above, ensure we have transitions
1240 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001241 }
1242
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001243 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1244 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001245 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001246 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001247 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001248 }
1249
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001250 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001251 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1252 mChartData = data;
1253 mChart.bindNetworkStats(mChartData.network);
1254 mChart.bindDetailNetworkStats(mChartData.detail);
1255
1256 // calcuate policy cycles based on available data
1257 updatePolicy(true);
1258 updateAppDetail();
1259
1260 // force scroll to top of body when showing detail
1261 if (mChartData.detail != null) {
1262 mListView.smoothScrollToPosition(0);
1263 }
1264 }
1265
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001266 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001267 public void onLoaderReset(Loader<ChartData> loader) {
1268 mChartData = null;
1269 mChart.bindNetworkStats(null);
1270 mChart.bindDetailNetworkStats(null);
1271 }
1272 };
1273
1274 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001275 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001276 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001277 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001278 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001279 }
1280
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001281 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001282 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001283 final int[] restrictedUids = mPolicyManager.getUidsWithPolicy(
Jeff Sharkeye557c332012-04-13 16:04:07 -07001284 POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001285 mAdapter.bindStats(data, restrictedUids);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001286 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001287 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001288
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001289 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001290 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001291 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001292 updateEmptyVisible();
1293 }
1294
1295 private void updateEmptyVisible() {
1296 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1297 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001298 mStupidPadding.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001299 }
1300 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001301
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001302 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001303 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001304 final Context context = getActivity();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001305 if (hasReadyMobileRadio(context)) {
1306 final TelephonyManager tele = TelephonyManager.from(context);
1307 return mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001308 } else {
1309 return false;
1310 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001311 }
1312
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001313 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001314 private void setMobilePolicySplit(boolean split) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001315 final Context context = getActivity();
1316 if (hasReadyMobileRadio(context)) {
1317 final TelephonyManager tele = TelephonyManager.from(context);
1318 mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split);
1319 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001320 }
1321
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001322 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001323 final TelephonyManager tele = TelephonyManager.from(context);
1324 final String actualSubscriberId = tele.getSubscriberId();
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -08001325 return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001326 }
1327
Jeff Sharkey8a503642011-06-10 13:31:21 -07001328 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001329 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001330 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001331 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001332 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001333
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001334 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001335 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001336 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001337 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001338
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001339 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001340 public void requestWarningEdit() {
1341 WarningEditorFragment.show(DataUsageSummary.this);
1342 }
1343
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001344 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001345 public void requestLimitEdit() {
1346 LimitEditorFragment.show(DataUsageSummary.this);
1347 }
1348 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001349
1350 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001351 * List item that reflects a specific data usage cycle.
1352 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001353 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001354 public CharSequence label;
1355 public long start;
1356 public long end;
1357
Jeff Sharkey8a503642011-06-10 13:31:21 -07001358 CycleItem(CharSequence label) {
1359 this.label = label;
1360 }
1361
1362 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001363 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001364 this.start = start;
1365 this.end = end;
1366 }
1367
Jeff Sharkey8a503642011-06-10 13:31:21 -07001368 @Override
1369 public String toString() {
1370 return label.toString();
1371 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001372
1373 @Override
1374 public boolean equals(Object o) {
1375 if (o instanceof CycleItem) {
1376 final CycleItem another = (CycleItem) o;
1377 return start == another.start && end == another.end;
1378 }
1379 return false;
1380 }
1381
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001382 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001383 public int compareTo(CycleItem another) {
1384 return Long.compare(start, another.start);
1385 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001386 }
1387
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001388 private static final StringBuilder sBuilder = new StringBuilder(50);
1389 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1390 sBuilder, Locale.getDefault());
1391
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001392 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001393 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001394
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001395 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001396 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001397 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1398 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001399 }
1400 }
1401
Jeff Sharkey8a503642011-06-10 13:31:21 -07001402 /**
1403 * Special-case data usage cycle that triggers dialog to change
1404 * {@link NetworkPolicy#cycleDay}.
1405 */
1406 public static class CycleChangeItem extends CycleItem {
1407 public CycleChangeItem(Context context) {
1408 super(context.getString(R.string.data_usage_change_cycle));
1409 }
1410 }
1411
1412 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001413 private boolean mChangePossible = false;
1414 private boolean mChangeVisible = false;
1415
1416 private final CycleChangeItem mChangeItem;
1417
Jeff Sharkey8a503642011-06-10 13:31:21 -07001418 public CycleAdapter(Context context) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001419 super(context, R.layout.data_usage_cycle_item);
1420 setDropDownViewResource(R.layout.data_usage_cycle_item_dropdown);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001421 mChangeItem = new CycleChangeItem(context);
1422 }
1423
1424 public void setChangePossible(boolean possible) {
1425 mChangePossible = possible;
1426 updateChange();
1427 }
1428
1429 public void setChangeVisible(boolean visible) {
1430 mChangeVisible = visible;
1431 updateChange();
1432 }
1433
1434 private void updateChange() {
1435 remove(mChangeItem);
1436 if (mChangePossible && mChangeVisible) {
1437 add(mChangeItem);
1438 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001439 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001440
1441 /**
1442 * Find position of {@link CycleItem} in this adapter which is nearest
1443 * the given {@link CycleItem}.
1444 */
1445 public int findNearestPosition(CycleItem target) {
1446 if (target != null) {
1447 final int count = getCount();
1448 for (int i = count - 1; i >= 0; i--) {
1449 final CycleItem item = getItem(i);
1450 if (item instanceof CycleChangeItem) {
1451 continue;
1452 } else if (item.compareTo(target) >= 0) {
1453 return i;
1454 }
1455 }
1456 }
1457 return 0;
1458 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001459 }
1460
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001461 public static class AppItem implements Comparable<AppItem>, Parcelable {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001462 public static final int CATEGORY_USER = 0;
1463 public static final int CATEGORY_APP_TITLE = 1;
1464 public static final int CATEGORY_APP = 2;
1465
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001466 public final int key;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001467 public boolean restricted;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001468 public int category;
1469
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001470 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001471 public long total;
1472
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001473 public AppItem() {
1474 this.key = 0;
1475 }
1476
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001477 public AppItem(int key) {
1478 this.key = key;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001479 }
1480
1481 public AppItem(Parcel parcel) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001482 key = parcel.readInt();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001483 uids = parcel.readSparseBooleanArray();
1484 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001485 }
1486
1487 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001488 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001489 }
1490
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001491 @Override
1492 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001493 dest.writeInt(key);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001494 dest.writeSparseBooleanArray(uids);
1495 dest.writeLong(total);
1496 }
1497
1498 @Override
1499 public int describeContents() {
1500 return 0;
1501 }
1502
1503 @Override
1504 public int compareTo(AppItem another) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001505 int comparison = Integer.compare(category, another.category);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001506 if (comparison == 0) {
1507 comparison = Long.compare(another.total, total);
1508 }
1509 return comparison;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001510 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001511
1512 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1513 @Override
1514 public AppItem createFromParcel(Parcel in) {
1515 return new AppItem(in);
1516 }
1517
1518 @Override
1519 public AppItem[] newArray(int size) {
1520 return new AppItem[size];
1521 }
1522 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001523 }
1524
Jeff Sharkey8a503642011-06-10 13:31:21 -07001525 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001526 * Adapter of applications, sorted by total usage descending.
1527 */
1528 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001529 private final UidDetailProvider mProvider;
1530 private final int mInsetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001531 private final UserManager mUm;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001532
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001533 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001534 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001535
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001536 public DataUsageAdapter(final UserManager userManager, UidDetailProvider provider, int insetSide) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001537 mProvider = checkNotNull(provider);
1538 mInsetSide = insetSide;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001539 mUm = userManager;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001540 }
1541
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001542 /**
1543 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1544 */
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001545 public void bindStats(NetworkStats stats, int[] restrictedUids) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001546 mItems.clear();
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001547 mLargest = 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001548
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001549 final int currentUserId = ActivityManager.getCurrentUser();
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001550 final List<UserHandle> profiles = mUm.getUserProfiles();
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001551 final SparseArray<AppItem> knownItems = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001552
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001553 NetworkStats.Entry entry = null;
1554 final int size = stats != null ? stats.size() : 0;
1555 for (int i = 0; i < size; i++) {
1556 entry = stats.getValues(i, entry);
1557
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001558 // Decide how to collapse items together
1559 final int uid = entry.uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001560
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001561 final int collapseKey;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001562 final int category;
1563 final int userId = UserHandle.getUserId(uid);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001564 if (UserHandle.isApp(uid)) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001565 if (profiles.contains(new UserHandle(userId))) {
1566 if (userId != currentUserId) {
1567 // Add to a managed user item.
1568 final int managedKey = UidDetailProvider.buildKeyForUser(userId);
1569 accumulate(managedKey, knownItems, entry,
1570 AppItem.CATEGORY_USER);
1571 }
1572 // Add to app item.
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001573 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001574 category = AppItem.CATEGORY_APP;
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001575 } else {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001576 // Add to other user item.
1577 collapseKey = UidDetailProvider.buildKeyForUser(userId);
1578 category = AppItem.CATEGORY_USER;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001579 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001580 } else if (uid == UID_REMOVED || uid == UID_TETHERING) {
1581 collapseKey = uid;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001582 category = AppItem.CATEGORY_APP;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001583 } else {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001584 collapseKey = android.os.Process.SYSTEM_UID;
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001585 category = AppItem.CATEGORY_APP;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001586 }
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001587 accumulate(collapseKey, knownItems, entry, category);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001588 }
1589
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001590 final int restrictedUidsMax = restrictedUids.length;
1591 for (int i = 0; i < restrictedUidsMax; ++i) {
1592 final int uid = restrictedUids[i];
1593 // Only splice in restricted state for current user or managed users
1594 if (!profiles.contains(new UserHandle(UserHandle.getUserId(uid)))) {
1595 continue;
1596 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001597
1598 AppItem item = knownItems.get(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001599 if (item == null) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001600 item = new AppItem(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001601 item.total = -1;
1602 mItems.add(item);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001603 knownItems.put(item.key, item);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001604 }
1605 item.restricted = true;
1606 }
1607
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001608 if (!mItems.isEmpty()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001609 final AppItem title = new AppItem();
1610 title.category = AppItem.CATEGORY_APP_TITLE;
1611 mItems.add(title);
1612 }
1613
Jeff Sharkey8a503642011-06-10 13:31:21 -07001614 Collections.sort(mItems);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001615 notifyDataSetChanged();
1616 }
1617
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001618 /**
1619 * Accumulate data usage of a network stats entry for the item mapped by the collapse key.
1620 * Creates the item if needed.
1621 *
1622 * @param collapseKey the collapse key used to map the item.
1623 * @param knownItems collection of known (already existing) items.
1624 * @param entry the network stats entry to extract data usage from.
1625 * @param itemCategory the item is categorized on the list view by this category. Must be
1626 * either AppItem.APP_ITEM_CATEGORY or AppItem.MANAGED_USER_ITEM_CATEGORY
1627 */
1628 private void accumulate(int collapseKey, final SparseArray<AppItem> knownItems,
1629 NetworkStats.Entry entry, int itemCategory) {
1630 final int uid = entry.uid;
1631 AppItem item = knownItems.get(collapseKey);
1632 if (item == null) {
1633 item = new AppItem(collapseKey);
1634 item.category = itemCategory;
1635 mItems.add(item);
1636 knownItems.put(item.key, item);
1637 }
1638 item.addUid(uid);
1639 item.total += entry.rxBytes + entry.txBytes;
1640 if (mLargest < item.total) {
1641 mLargest = item.total;
1642 }
1643 }
1644
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001645 @Override
1646 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001647 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001648 }
1649
1650 @Override
1651 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001652 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001653 }
1654
1655 @Override
1656 public long getItemId(int position) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001657 return mItems.get(position).key;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001658 }
1659
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001660 /**
1661 * See {@link #getItemViewType} for the view types.
1662 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001663 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001664 public int getViewTypeCount() {
1665 return 2;
1666 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001667
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001668 /**
1669 * Returns 1 for separator items and 0 for anything else.
1670 */
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001671 @Override
1672 public int getItemViewType(int position) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001673 final AppItem item = mItems.get(position);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001674 if (item.category == AppItem.CATEGORY_APP_TITLE) {
1675 return 1;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001676 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001677 return 0;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001678 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001679 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001680
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001681 @Override
1682 public boolean areAllItemsEnabled() {
1683 return false;
1684 }
1685
1686 @Override
1687 public boolean isEnabled(int position) {
Zoltan Szatmary-Ban0924f192014-07-21 16:42:50 +01001688 if (position > mItems.size()) {
1689 throw new ArrayIndexOutOfBoundsException();
1690 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001691 return getItemViewType(position) == 0;
1692 }
1693
1694 @Override
1695 public View getView(int position, View convertView, ViewGroup parent) {
1696 final AppItem item = mItems.get(position);
1697 if (getItemViewType(position) == 1) {
1698 if (convertView == null) {
1699 convertView = inflateCategoryHeader(LayoutInflater.from(parent.getContext()),
1700 parent);
1701 }
1702
1703 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
1704 title.setText(R.string.data_usage_app);
1705
1706 } else {
1707 if (convertView == null) {
1708 convertView = LayoutInflater.from(parent.getContext()).inflate(
1709 R.layout.data_usage_item, parent, false);
1710
1711 if (mInsetSide > 0) {
1712 convertView.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
1713 }
1714 }
1715
1716 final Context context = parent.getContext();
1717
1718 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
1719 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1720 android.R.id.progress);
1721
1722 // kick off async load of app details
1723 UidDetailTask.bindView(mProvider, item, convertView);
1724
1725 if (item.restricted && item.total <= 0) {
1726 text1.setText(R.string.data_usage_app_restricted);
1727 progress.setVisibility(View.GONE);
1728 } else {
1729 text1.setText(Formatter.formatFileSize(context, item.total));
1730 progress.setVisibility(View.VISIBLE);
1731 }
1732
1733 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1734 progress.setProgress(percentTotal);
1735 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001736
1737 return convertView;
1738 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001739 }
1740
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001741 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001742 * Empty {@link Fragment} that controls display of UID details in
1743 * {@link DataUsageSummary}.
1744 */
1745 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001746 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001747
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001748 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001749 if (!parent.isAdded()) return;
1750
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001751 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001752 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001753
1754 final AppDetailsFragment fragment = new AppDetailsFragment();
1755 fragment.setArguments(args);
1756 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001757 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1758 ft.add(fragment, TAG_APP_DETAILS);
1759 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001760 ft.setBreadCrumbTitle(
1761 parent.getResources().getString(R.string.data_usage_app_summary_title));
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001762 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001763 }
1764
1765 @Override
1766 public void onStart() {
1767 super.onStart();
1768 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001769 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001770 target.updateBody();
1771 }
1772
1773 @Override
1774 public void onStop() {
1775 super.onStop();
1776 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001777 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001778 target.updateBody();
1779 }
1780 }
1781
1782 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001783 * Dialog to request user confirmation before setting
1784 * {@link NetworkPolicy#limitBytes}.
1785 */
1786 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001787 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001788 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001789
1790 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001791 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001792
Jeff Sharkeyd0a55312014-08-08 10:25:24 -07001793 final NetworkPolicy policy = parent.mPolicyEditor.getPolicy(parent.mTemplate);
1794 if (policy == null) return;
1795
Jeff Sharkey461842a2011-09-25 18:22:48 -07001796 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001797 final CharSequence message;
Jeff Sharkeyd0a55312014-08-08 10:25:24 -07001798 final long minLimitBytes = (long) (policy.warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001799 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001800
1801 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001802 final String currentTab = parent.mCurrentTab;
1803 if (TAB_3G.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 Sharkey9549e9f2011-07-14 20:01:13 -07001806 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001807 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001808 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001809 } else if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001810 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001811 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001812 } else {
1813 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001814 }
1815
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001816 final Bundle args = new Bundle();
1817 args.putCharSequence(EXTRA_MESSAGE, message);
1818 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1819
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001820 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1821 dialog.setArguments(args);
1822 dialog.setTargetFragment(parent, 0);
1823 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1824 }
1825
1826 @Override
1827 public Dialog onCreateDialog(Bundle savedInstanceState) {
1828 final Context context = getActivity();
1829
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001830 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001831 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1832
1833 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1834 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001835 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001836
1837 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001838 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001839 public void onClick(DialogInterface dialog, int which) {
1840 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1841 if (target != null) {
1842 target.setPolicyLimitBytes(limitBytes);
1843 }
1844 }
1845 });
1846
1847 return builder.create();
1848 }
1849 }
1850
1851 /**
1852 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1853 */
1854 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001855 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001856
1857 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001858 if (!parent.isAdded()) return;
1859
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001860 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001861 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001862
1863 final CycleEditorFragment dialog = new CycleEditorFragment();
1864 dialog.setArguments(args);
1865 dialog.setTargetFragment(parent, 0);
1866 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1867 }
1868
1869 @Override
1870 public Dialog onCreateDialog(Bundle savedInstanceState) {
1871 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001872 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1873 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001874
1875 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1876 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1877
1878 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1879 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1880
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001881 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1882 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001883
1884 cycleDayPicker.setMinValue(1);
1885 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001886 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001887 cycleDayPicker.setWrapSelectorWheel(true);
1888
1889 builder.setTitle(R.string.data_usage_cycle_editor_title);
1890 builder.setView(view);
1891
1892 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1893 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001894 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001895 public void onClick(DialogInterface dialog, int which) {
Jeff Sharkeyd277de92013-03-25 15:11:25 -07001896 // clear focus to finish pending text edits
1897 cycleDayPicker.clearFocus();
1898
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001899 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001900 final String cycleTimezone = new Time().timezone;
1901 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001902 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001903 }
1904 });
1905
1906 return builder.create();
1907 }
1908 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001909
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001910 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001911 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1912 */
1913 public static class WarningEditorFragment extends DialogFragment {
1914 private static final String EXTRA_TEMPLATE = "template";
1915
1916 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001917 if (!parent.isAdded()) return;
1918
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001919 final Bundle args = new Bundle();
1920 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1921
1922 final WarningEditorFragment dialog = new WarningEditorFragment();
1923 dialog.setArguments(args);
1924 dialog.setTargetFragment(parent, 0);
1925 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1926 }
1927
1928 @Override
1929 public Dialog onCreateDialog(Bundle savedInstanceState) {
1930 final Context context = getActivity();
1931 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1932 final NetworkPolicyEditor editor = target.mPolicyEditor;
1933
1934 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1935 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1936
1937 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1938 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1939
1940 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1941 final long warningBytes = editor.getPolicyWarningBytes(template);
1942 final long limitBytes = editor.getPolicyLimitBytes(template);
1943
1944 bytesPicker.setMinValue(0);
1945 if (limitBytes != LIMIT_DISABLED) {
1946 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1947 } else {
1948 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1949 }
1950 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1951 bytesPicker.setWrapSelectorWheel(false);
1952
1953 builder.setTitle(R.string.data_usage_warning_editor_title);
1954 builder.setView(view);
1955
1956 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1957 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001958 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001959 public void onClick(DialogInterface dialog, int which) {
1960 // clear focus to finish pending text edits
1961 bytesPicker.clearFocus();
1962
1963 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1964 editor.setPolicyWarningBytes(template, bytes);
1965 target.updatePolicy(false);
1966 }
1967 });
1968
1969 return builder.create();
1970 }
1971 }
1972
1973 /**
1974 * Dialog to edit {@link NetworkPolicy#limitBytes}.
1975 */
1976 public static class LimitEditorFragment extends DialogFragment {
1977 private static final String EXTRA_TEMPLATE = "template";
1978
1979 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001980 if (!parent.isAdded()) return;
1981
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001982 final Bundle args = new Bundle();
1983 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1984
1985 final LimitEditorFragment dialog = new LimitEditorFragment();
1986 dialog.setArguments(args);
1987 dialog.setTargetFragment(parent, 0);
1988 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
1989 }
1990
1991 @Override
1992 public Dialog onCreateDialog(Bundle savedInstanceState) {
1993 final Context context = getActivity();
1994 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1995 final NetworkPolicyEditor editor = target.mPolicyEditor;
1996
1997 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1998 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1999
2000 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
2001 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
2002
2003 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
2004 final long warningBytes = editor.getPolicyWarningBytes(template);
2005 final long limitBytes = editor.getPolicyLimitBytes(template);
2006
2007 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09002008 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002009 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
2010 } else {
2011 bytesPicker.setMinValue(0);
2012 }
2013 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
2014 bytesPicker.setWrapSelectorWheel(false);
2015
2016 builder.setTitle(R.string.data_usage_limit_editor_title);
2017 builder.setView(view);
2018
2019 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
2020 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002021 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002022 public void onClick(DialogInterface dialog, int which) {
2023 // clear focus to finish pending text edits
2024 bytesPicker.clearFocus();
2025
2026 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
2027 editor.setPolicyLimitBytes(template, bytes);
2028 target.updatePolicy(false);
2029 }
2030 });
2031
2032 return builder.create();
2033 }
2034 }
2035 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07002036 * Dialog to request user confirmation before disabling data.
2037 */
2038 public static class ConfirmDataDisableFragment extends DialogFragment {
2039 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002040 if (!parent.isAdded()) return;
2041
Jeff Sharkey28130d92011-09-02 16:10:24 -07002042 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
2043 dialog.setTargetFragment(parent, 0);
2044 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
2045 }
2046
2047 @Override
2048 public Dialog onCreateDialog(Bundle savedInstanceState) {
2049 final Context context = getActivity();
2050
2051 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2052 builder.setMessage(R.string.data_usage_disable_mobile);
2053
2054 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002055 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07002056 public void onClick(DialogInterface dialog, int which) {
2057 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2058 if (target != null) {
2059 // TODO: extend to modify policy enabled flag.
2060 target.setMobileDataEnabled(false);
2061 }
2062 }
2063 });
2064 builder.setNegativeButton(android.R.string.cancel, null);
2065
2066 return builder.create();
2067 }
2068 }
2069
2070 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002071 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07002072 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002073 */
2074 public static class ConfirmRestrictFragment extends DialogFragment {
2075 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002076 if (!parent.isAdded()) return;
2077
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002078 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
2079 dialog.setTargetFragment(parent, 0);
2080 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
2081 }
2082
2083 @Override
2084 public Dialog onCreateDialog(Bundle savedInstanceState) {
2085 final Context context = getActivity();
2086
2087 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002088 builder.setTitle(R.string.data_usage_restrict_background_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07002089 if (Utils.hasMultipleUsers(context)) {
2090 builder.setMessage(R.string.data_usage_restrict_background_multiuser);
2091 } else {
2092 builder.setMessage(R.string.data_usage_restrict_background);
2093 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002094
2095 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002096 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002097 public void onClick(DialogInterface dialog, int which) {
2098 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2099 if (target != null) {
2100 target.setRestrictBackground(true);
2101 }
2102 }
2103 });
2104 builder.setNegativeButton(android.R.string.cancel, null);
2105
2106 return builder.create();
2107 }
2108 }
2109
2110 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002111 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
2112 * change has been denied, usually based on
2113 * {@link DataUsageSummary#hasLimitedNetworks()}.
2114 */
2115 public static class DeniedRestrictFragment extends DialogFragment {
2116 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002117 if (!parent.isAdded()) return;
2118
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002119 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
2120 dialog.setTargetFragment(parent, 0);
2121 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
2122 }
2123
2124 @Override
2125 public Dialog onCreateDialog(Bundle savedInstanceState) {
2126 final Context context = getActivity();
2127
2128 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2129 builder.setTitle(R.string.data_usage_app_restrict_background);
2130 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
2131 builder.setPositiveButton(android.R.string.ok, null);
2132
2133 return builder.create();
2134 }
2135 }
2136
2137 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002138 * Dialog to request user confirmation before setting
2139 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
2140 */
2141 public static class ConfirmAppRestrictFragment extends DialogFragment {
2142 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002143 if (!parent.isAdded()) return;
2144
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002145 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2146 dialog.setTargetFragment(parent, 0);
2147 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2148 }
2149
2150 @Override
2151 public Dialog onCreateDialog(Bundle savedInstanceState) {
2152 final Context context = getActivity();
2153
2154 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002155 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2156 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2157
2158 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002159 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002160 public void onClick(DialogInterface dialog, int which) {
2161 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2162 if (target != null) {
2163 target.setAppRestrictBackground(true);
2164 }
2165 }
2166 });
2167 builder.setNegativeButton(android.R.string.cancel, null);
2168
2169 return builder.create();
2170 }
2171 }
2172
2173 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002174 * Compute default tab that should be selected, based on
2175 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2176 */
2177 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002178 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
2179 if (template == null) return null;
2180
2181 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002182 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002183 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002184 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002185 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002186 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002187 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002188 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002189 return TAB_WIFI;
2190 default:
2191 return null;
2192 }
2193 }
2194
2195 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002196 * Background task that loads {@link UidDetail}, binding to
2197 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002198 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002199 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2200 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002201 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002202 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002203
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002204 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002205 mProvider = checkNotNull(provider);
2206 mItem = checkNotNull(item);
2207 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002208 }
2209
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002210 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002211 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002212 final UidDetailTask existing = (UidDetailTask) target.getTag();
2213 if (existing != null) {
2214 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002215 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002216
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002217 final UidDetail cachedDetail = provider.getUidDetail(item.key, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002218 if (cachedDetail != null) {
2219 bindView(cachedDetail, target);
2220 } else {
2221 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2222 AsyncTask.THREAD_POOL_EXECUTOR));
2223 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002224 }
2225
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002226 private static void bindView(UidDetail detail, View target) {
2227 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2228 final TextView title = (TextView) target.findViewById(android.R.id.title);
2229
2230 if (detail != null) {
2231 icon.setImageDrawable(detail.icon);
2232 title.setText(detail.label);
Zoltan Szatmary-Banebb36ec2014-07-23 11:02:46 +01002233 title.setContentDescription(detail.contentDescription);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002234 } else {
2235 icon.setImageDrawable(null);
2236 title.setText(null);
2237 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002238 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002239
2240 @Override
2241 protected void onPreExecute() {
2242 bindView(null, mTarget);
2243 }
2244
2245 @Override
2246 protected UidDetail doInBackground(Void... params) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002247 return mProvider.getUidDetail(mItem.key, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002248 }
2249
2250 @Override
2251 protected void onPostExecute(UidDetail result) {
2252 bindView(result, mTarget);
2253 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002254 }
2255
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002256 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002257 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002258 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002259 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002260 if (TEST_RADIOS) {
2261 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2262 }
2263
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002264 final ConnectivityManager conn = ConnectivityManager.from(context);
2265 final TelephonyManager tele = TelephonyManager.from(context);
2266
2267 // require both supported network and ready SIM
2268 return conn.isNetworkSupported(TYPE_MOBILE) && tele.getSimState() == SIM_STATE_READY;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002269 }
2270
2271 /**
2272 * Test if device has a mobile 4G data radio.
2273 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002274 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002275 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2276 return false;
2277 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002278 if (TEST_RADIOS) {
2279 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2280 }
2281
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002282 final ConnectivityManager conn = ConnectivityManager.from(context);
2283 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002284
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002285 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Wink Saville55434042012-06-14 12:33:43 -07002286 final boolean hasLte = (tele.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE)
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002287 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002288 return hasWimax || hasLte;
2289 }
2290
2291 /**
2292 * Test if device has a Wi-Fi data radio.
2293 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002294 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002295 if (TEST_RADIOS) {
2296 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2297 }
2298
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002299 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002300 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002301 }
2302
2303 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002304 * Test if device has an ethernet network connection.
2305 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002306 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002307 if (TEST_RADIOS) {
2308 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2309 }
2310
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002311 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002312 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2313
2314 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002315 if (mStatsSession != null) {
2316 try {
2317 ethernetBytes = mStatsSession.getSummaryForNetwork(
2318 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2319 .getTotalBytes();
2320 } catch (RemoteException e) {
2321 throw new RuntimeException(e);
2322 }
2323 } else {
2324 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002325 }
2326
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002327 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002328 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002329 }
2330
2331 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002332 * Inflate a {@link Preference} style layout, adding the given {@link View}
2333 * widget into {@link android.R.id#widget_frame}.
2334 */
2335 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2336 final View view = inflater.inflate(R.layout.preference, root, false);
2337 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2338 android.R.id.widget_frame);
2339 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2340 return view;
2341 }
2342
Jeff Sharkeyb6548462014-07-21 15:38:06 -07002343 private static View inflateCategoryHeader(LayoutInflater inflater, ViewGroup root) {
2344 final TypedArray a = inflater.getContext().obtainStyledAttributes(null,
2345 com.android.internal.R.styleable.Preference,
2346 com.android.internal.R.attr.preferenceCategoryStyle, 0);
2347 final int resId = a.getResourceId(com.android.internal.R.styleable.Preference_layout, 0);
2348 return inflater.inflate(resId, root, false);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002349 }
2350
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002351 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002352 * Test if any networks are currently limited.
2353 */
2354 private boolean hasLimitedNetworks() {
2355 return !buildLimitedNetworksList().isEmpty();
2356 }
2357
2358 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002359 * Build string describing currently limited networks, which defines when
2360 * background data is restricted.
2361 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002362 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002363 private CharSequence buildLimitedNetworksString() {
2364 final List<CharSequence> limited = buildLimitedNetworksList();
2365
2366 // handle case where no networks limited
2367 if (limited.isEmpty()) {
2368 limited.add(getText(R.string.data_usage_list_none));
2369 }
2370
2371 return TextUtils.join(limited);
2372 }
2373
2374 /**
2375 * Build list of currently limited networks, which defines when background
2376 * data is restricted.
2377 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002378 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002379 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002380 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002381
2382 // build combined list of all limited networks
2383 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002384
2385 final TelephonyManager tele = TelephonyManager.from(context);
2386 if (tele.getSimState() == SIM_STATE_READY) {
2387 final String subscriberId = getActiveSubscriberId(context);
2388 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2389 limited.add(getText(R.string.data_usage_list_mobile));
2390 }
2391 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2392 limited.add(getText(R.string.data_usage_tab_3g));
2393 }
2394 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2395 limited.add(getText(R.string.data_usage_tab_4g));
2396 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002397 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002398
2399 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002400 limited.add(getText(R.string.data_usage_tab_wifi));
2401 }
2402 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2403 limited.add(getText(R.string.data_usage_tab_ethernet));
2404 }
2405
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002406 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002407 }
2408
2409 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002410 * Inset both selector and divider {@link Drawable} on the given
2411 * {@link ListView} by the requested dimensions.
2412 */
2413 private static void insetListViewDrawables(ListView view, int insetSide) {
2414 final Drawable selector = view.getSelector();
2415 final Drawable divider = view.getDivider();
2416
2417 // fully unregister these drawables so callbacks can be maintained after
2418 // wrapping below.
2419 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2420 view.setSelector(stub);
2421 view.setDivider(stub);
2422
2423 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2424 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2425 }
2426
2427 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002428 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002429 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002430 */
2431 private static void setPreferenceTitle(View parent, int resId) {
2432 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2433 title.setText(resId);
2434 }
2435
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002436 /**
2437 * Set {@link android.R.id#summary} for a preference view inflated with
2438 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2439 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002440 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002441 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2442 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002443 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002444 }
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002445
2446 /**
2447 * For search
2448 */
2449 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -07002450 new BaseSearchIndexProvider() {
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002451 @Override
2452 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
2453 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
2454
2455 final Resources res = context.getResources();
2456
2457 // Add fragment title
2458 SearchIndexableRaw data = new SearchIndexableRaw(context);
2459 data.title = res.getString(R.string.data_usage_summary_title);
2460 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2461 result.add(data);
2462
2463 // Mobile data
2464 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002465 data.key = DATA_USAGE_ENABLE_MOBILE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002466 data.title = res.getString(R.string.data_usage_enable_mobile);
2467 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2468 result.add(data);
2469
2470 // Set mobile data limit
2471 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002472 data.key = DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002473 data.title = res.getString(R.string.data_usage_disable_mobile_limit);
2474 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2475 result.add(data);
2476
Fabrice Di Megliof2a52262014-04-17 17:20:27 -07002477 // Data usage cycle
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002478 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002479 data.key = DATA_USAGE_CYCLE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002480 data.title = res.getString(R.string.data_usage_cycle);
2481 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2482 result.add(data);
2483
2484 return result;
2485 }
2486 };
2487
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002488}