blob: f8f285fbbd4c0d684237acbc0b392984a1a5cdca [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 Sharkey9fab0da2011-07-09 17:52:31 -070059import android.content.ContentResolver;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070060import android.content.Context;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070061import android.content.DialogInterface;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -070062import android.content.Intent;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070063import android.content.Loader;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070064import android.content.SharedPreferences;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070065import android.content.pm.PackageManager;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070066import android.content.res.Resources;
Jeff Sharkeyb6548462014-07-21 15:38:06 -070067import android.content.res.TypedArray;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070068import android.graphics.Color;
Jeff Sharkey5d706792011-09-08 18:57:17 -070069import android.graphics.drawable.ColorDrawable;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070070import android.graphics.drawable.Drawable;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070071import android.net.ConnectivityManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070072import android.net.INetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070073import android.net.INetworkStatsService;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070074import android.net.INetworkStatsSession;
Jeff Sharkey8a503642011-06-10 13:31:21 -070075import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070076import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070077import android.net.NetworkStats;
78import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070079import android.net.NetworkTemplate;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070080import android.net.TrafficStats;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070081import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070082import android.os.Bundle;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070083import android.os.INetworkManagementService;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070084import android.os.Parcel;
85import android.os.Parcelable;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070086import android.os.RemoteException;
87import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070088import android.os.SystemProperties;
Dianne Hackbornbb06a422012-08-16 11:01:57 -070089import android.os.UserHandle;
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";
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700178 private static final String TAG_CONFIRM_AUTO_SYNC_CHANGE = "confirmAutoSyncChange";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700179 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700180
Fabrice Di Meglio2169c882014-04-18 15:18:40 -0700181 private static final String DATA_USAGE_ENABLE_MOBILE_KEY = "data_usage_enable_mobile";
182 private static final String DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY =
183 "data_usage_disable_mobile_limit";
184 private static final String DATA_USAGE_CYCLE_KEY = "data_usage_cycle";
185
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700186 private static final int LOADER_CHART_DATA = 2;
187 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700188
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700189 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700190 private INetworkStatsService mStatsService;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700191 private NetworkPolicyManager mPolicyManager;
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700192 private TelephonyManager mTelephonyManager;
193
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700194 private INetworkStatsSession mStatsSession;
195
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700196 private static final String PREF_FILE = "data_usage";
197 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700198 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700199
200 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700201
Jeff Sharkey8a503642011-06-10 13:31:21 -0700202 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700203 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700204 private TabWidget mTabWidget;
205 private ListView mListView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700206 private DataUsageAdapter mAdapter;
207
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700208 /** Distance to inset content from sides, when needed. */
209 private int mInsetSide = 0;
210
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700211 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700212
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700213 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700214 private LinearLayout mNetworkSwitches;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700215 private boolean mDataEnabledSupported;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700216 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700217 private View mDataEnabledView;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700218 private boolean mDisableAtLimitSupported;
219 private Switch mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700220 private View mDisableAtLimitView;
221
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700222 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700223 private Spinner mCycleSpinner;
224 private CycleAdapter mCycleAdapter;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700225 private TextView mCycleSummary;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700226
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700227 private ChartDataUsageView mChart;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700228 private View mDisclaimer;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700229 private TextView mEmpty;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700230 private View mStupidPadding;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700231
232 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700233 private ImageView mAppIcon;
234 private ViewGroup mAppTitles;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700235 private TextView mAppTotal;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700236 private TextView mAppForeground;
237 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700238 private Button mAppSettings;
239
240 private LinearLayout mAppSwitches;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700241 private Switch mAppRestrict;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700242 private View mAppRestrictView;
243
Jeff Sharkey8a503642011-06-10 13:31:21 -0700244 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700245 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700246
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700247 private NetworkTemplate mTemplate;
248 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700249
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700250 private AppItem mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700251
252 private Intent mAppSettingsIntent;
253
Jeff Sharkeya662e492011-06-18 21:57:06 -0700254 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700255
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700256 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700257 private String mIntentTab = null;
258
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700259 private MenuItem mMenuRestrictBackground;
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700260 private MenuItem mMenuShowWifi;
261 private MenuItem mMenuShowEthernet;
262 private MenuItem mMenuSimCards;
263 private MenuItem mMenuCellularNetworks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700264
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700265 /** Flag used to ignore listeners during binding. */
266 private boolean mBinding;
267
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700268 private UidDetailProvider mUidDetailProvider;
269
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700270 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700271 public void onCreate(Bundle savedInstanceState) {
272 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700273 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700274
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700275 mNetworkService = INetworkManagementService.Stub.asInterface(
276 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700277 mStatsService = INetworkStatsService.Stub.asInterface(
278 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700279 mPolicyManager = NetworkPolicyManager.from(context);
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700280 mTelephonyManager = TelephonyManager.from(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700281
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700282 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700283
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700284 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700285 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700286
Jaewan Kimffce9c12013-03-19 16:53:45 +0900287 try {
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700288 if (!mNetworkService.isBandwidthControlEnabled()) {
289 Log.w(TAG, "No bandwidth control; leaving");
290 getActivity().finish();
291 }
292 } catch (RemoteException e) {
293 Log.w(TAG, "No bandwidth control; leaving");
294 getActivity().finish();
295 }
296
297 try {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900298 mStatsSession = mStatsService.openSession();
299 } catch (RemoteException e) {
300 throw new RuntimeException(e);
301 }
302
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700303 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700304 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700305
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700306 // override preferences when no mobile radio
307 if (!hasReadyMobileRadio(context)) {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900308 mShowWifi = true;
309 mShowEthernet = true;
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700310 }
311
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700312 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700313 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700314
Jeff Sharkey8a503642011-06-10 13:31:21 -0700315 @Override
316 public View onCreateView(LayoutInflater inflater, ViewGroup container,
317 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700318
Jeff Sharkey8a503642011-06-10 13:31:21 -0700319 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700320 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
321
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700322 mUidDetailProvider = new UidDetailProvider(context);
323
Jeff Sharkey8a503642011-06-10 13:31:21 -0700324 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700325 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700326 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
327 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700328
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700329 // decide if we need to manually inset our content, or if we should rely
330 // on parent container for inset.
331 final boolean shouldInset = mListView.getScrollBarStyle()
332 == View.SCROLLBARS_OUTSIDE_OVERLAY;
Amith Yamasani56f51a82013-08-05 10:07:23 -0700333 mInsetSide = 0;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700334
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700335 // adjust padding around tabwidget as needed
Amith Yamasani56f51a82013-08-05 10:07:23 -0700336 prepareCustomPreferencesList(container, view, mListView, false);
Jeff Sharkey5d706792011-09-08 18:57:17 -0700337
Jeff Sharkey8a503642011-06-10 13:31:21 -0700338 mTabHost.setup();
339 mTabHost.setOnTabChangedListener(mTabListener);
340
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700341 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700342 mHeader.setClickable(true);
343
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700344 mListView.addHeaderView(new View(context), null, true);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700345 mListView.addHeaderView(mHeader, null, true);
346 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700347
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700348 if (mInsetSide > 0) {
349 // inset selector and divider drawables
350 insetListViewDrawables(mListView, mInsetSide);
Fabrice Di Megliob27223f2013-01-15 18:54:11 -0800351 mHeader.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700352 }
353
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700354 {
355 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700356 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
357 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700358 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700359
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700360 mDataEnabled = new Switch(inflater.getContext());
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700361 mDataEnabled.setClickable(false);
362 mDataEnabled.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700363 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700364 mDataEnabledView.setTag(R.id.preference_highlight_key,
365 DATA_USAGE_ENABLE_MOBILE_KEY);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700366 mDataEnabledView.setClickable(true);
367 mDataEnabledView.setFocusable(true);
368 mDataEnabledView.setOnClickListener(mDataEnabledListener);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700369 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700370
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700371 mDisableAtLimit = new Switch(inflater.getContext());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700372 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700373 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700374 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Fabrice Di Meglioc70b7f62014-06-19 11:29:26 -0700375 mDisableAtLimitView.setTag(R.id.preference_highlight_key,
376 DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700377 mDisableAtLimitView.setClickable(true);
378 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700379 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
380 mNetworkSwitches.addView(mDisableAtLimitView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700381
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700382 mCycleView = inflater.inflate(R.layout.data_usage_cycles, mNetworkSwitches, false);
383 mCycleView.setTag(R.id.preference_highlight_key, DATA_USAGE_CYCLE_KEY);
384 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
385 mCycleAdapter = new CycleAdapter(context);
386 mCycleSpinner.setAdapter(mCycleAdapter);
387 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
388 mCycleSummary = (TextView) mCycleView.findViewById(R.id.cycle_summary);
389 mNetworkSwitches.addView(mCycleView);
390 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700391
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700392 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700393 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800394 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700395
396 {
397 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700398 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700399 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
400 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700401 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
402 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700403 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700404
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700405 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700406 mAppSettings.setOnClickListener(mAppSettingsListener);
407
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700408 mAppRestrict = new Switch(inflater.getContext());
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700409 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700410 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700411 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700412 mAppRestrictView.setClickable(true);
413 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700414 mAppRestrictView.setOnClickListener(mAppRestrictListener);
415 mAppSwitches.addView(mAppRestrictView);
416 }
417
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700418 mDisclaimer = mHeader.findViewById(R.id.disclaimer);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700419 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700420 mStupidPadding = mHeader.findViewById(R.id.stupid_padding);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700421
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700422 mAdapter = new DataUsageAdapter(mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700423 mListView.setOnItemClickListener(mListListener);
424 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700425
426 return view;
427 }
428
429 @Override
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700430 public void onViewStateRestored(Bundle savedInstanceState) {
431 super.onViewStateRestored(savedInstanceState);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700432
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700433 // pick default tab based on incoming intent
434 final Intent intent = getActivity().getIntent();
435 mIntentTab = computeTabFromIntent(intent);
436
Jeff Sharkey8a503642011-06-10 13:31:21 -0700437 // this kicks off chain reaction which creates tabs, binds the body to
438 // selected network, and binds chart, cycles and detail list.
439 updateTabs();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700440 }
441
442 @Override
443 public void onResume() {
444 super.onResume();
445
446 getView().post(new Runnable() {
447 @Override
448 public void run() {
449 highlightViewIfNeeded();
450 }
451 });
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700452
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700453 // kick off background task to update stats
454 new AsyncTask<Void, Void, Void>() {
455 @Override
456 protected Void doInBackground(Void... params) {
457 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700458 // wait a few seconds before kicking off
459 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700460 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700461 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700462 } catch (RemoteException e) {
463 }
464 return null;
465 }
466
467 @Override
468 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700469 if (isAdded()) {
470 updateBody();
471 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700472 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700473 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700474 }
475
Jeff Sharkey8a503642011-06-10 13:31:21 -0700476 @Override
477 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
478 inflater.inflate(R.menu.data_usage, menu);
479 }
480
481 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700482 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700483 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700484 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700485 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700486
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700487 mMenuShowWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
488 // TODO: Define behavior of this sync button. See: http://b/16076571
489 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
490 mMenuShowWifi.setVisible(!appDetailMode);
491 } else {
492 mMenuShowWifi.setVisible(false);
493 }
494
495 mMenuShowEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
496 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
497 mMenuShowEthernet.setVisible(!appDetailMode);
498 } else {
499 mMenuShowEthernet.setVisible(false);
500 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700501
502 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700503 mMenuRestrictBackground.setVisible(
504 hasReadyMobileRadio(context) && isOwner && !appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700505
506 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
507 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700508 metered.setVisible(!appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700509 } else {
510 metered.setVisible(false);
511 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700512
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700513 // TODO: show when multiple sims available
514 mMenuSimCards = menu.findItem(R.id.data_usage_menu_sim_cards);
515 mMenuSimCards.setVisible(false);
516
517 mMenuCellularNetworks = menu.findItem(R.id.data_usage_menu_cellular_networks);
518 if (hasReadyMobileRadio(context)) {
519 mMenuCellularNetworks.setVisible(!appDetailMode);
520 } else {
521 mMenuCellularNetworks.setVisible(false);
522 }
523
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700524 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
525 String helpUrl;
526 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800527 HelpUtils.prepareHelpMenuItem(context, help, helpUrl);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700528 } else {
529 help.setVisible(false);
530 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700531
532 updateMenuTitles();
533 }
534
535 private void updateMenuTitles() {
536 if (mPolicyManager.getRestrictBackground()) {
537 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_allow_background);
538 } else {
539 mMenuRestrictBackground.setTitle(R.string.data_usage_menu_restrict_background);
540 }
541
542 if (mShowWifi) {
543 mMenuShowWifi.setTitle(R.string.data_usage_menu_hide_wifi);
544 } else {
545 mMenuShowWifi.setTitle(R.string.data_usage_menu_show_wifi);
546 }
547
548 if (mShowEthernet) {
549 mMenuShowEthernet.setTitle(R.string.data_usage_menu_hide_ethernet);
550 } else {
551 mMenuShowEthernet.setTitle(R.string.data_usage_menu_show_ethernet);
552 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700553 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700554
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700555 @Override
556 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700557 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700558 case R.id.data_usage_menu_restrict_background: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700559 final boolean restrictBackground = !mPolicyManager.getRestrictBackground();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700560 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800561 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700562 } else {
563 // no confirmation to drop restriction
564 setRestrictBackground(false);
565 }
566 return true;
567 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700568 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700569 mShowWifi = !mShowWifi;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700570 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700571 updateMenuTitles();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700572 updateTabs();
573 return true;
574 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700575 case R.id.data_usage_menu_show_ethernet: {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700576 mShowEthernet = !mShowEthernet;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700577 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700578 updateMenuTitles();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700579 updateTabs();
580 return true;
581 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700582 case R.id.data_usage_menu_sim_cards: {
583 // TODO: hook up to sim cards
584 return true;
585 }
586 case R.id.data_usage_menu_cellular_networks: {
587 final Intent intent = new Intent(Intent.ACTION_MAIN);
588 intent.setComponent(new ComponentName("com.android.phone",
589 "com.android.phone.MobileNetworkSettings"));
590 startActivity(intent);
591 return true;
592 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700593 case R.id.data_usage_menu_metered: {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800594 final SettingsActivity sa = (SettingsActivity) getActivity();
595 sa.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700596 R.string.data_usage_metered_title, null, this, 0);
597 return true;
598 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700599 }
600 return false;
601 }
602
Jeff Sharkey94a90952011-06-13 22:31:09 -0700603 @Override
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700604 public void onDestroy() {
Jeff Sharkey94a90952011-06-13 22:31:09 -0700605 mDataEnabledView = null;
606 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700607
608 mUidDetailProvider.clearCache();
609 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700610
611 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700612
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800613 super.onDestroy();
614 }
615
Jeff Sharkey8a503642011-06-10 13:31:21 -0700616 /**
Jeff Sharkey92811822012-05-04 11:47:29 -0700617 * Build and assign {@link LayoutTransition} to various containers. Should
618 * only be assigned after initial layout is complete.
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700619 */
Jeff Sharkey92811822012-05-04 11:47:29 -0700620 private void ensureLayoutTransitions() {
621 // skip when already setup
622 if (mChart.getLayoutTransition() != null) return;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700623
Jeff Sharkey92811822012-05-04 11:47:29 -0700624 mTabsContainer.setLayoutTransition(buildLayoutTransition());
625 mHeader.setLayoutTransition(buildLayoutTransition());
626 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700627
Jeff Sharkey92811822012-05-04 11:47:29 -0700628 final LayoutTransition chartTransition = buildLayoutTransition();
629 chartTransition.disableTransitionType(LayoutTransition.APPEARING);
630 chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
631 mChart.setLayoutTransition(chartTransition);
632 }
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700633
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700634 private static LayoutTransition buildLayoutTransition() {
635 final LayoutTransition transition = new LayoutTransition();
636 if (TEST_ANIM) {
637 transition.setDuration(1500);
638 }
639 transition.setAnimateParentHierarchy(false);
640 return transition;
641 }
642
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700643 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700644 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700645 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
646 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700647 */
648 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700649 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700650 mTabHost.clearAllTabs();
651
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700652 final boolean mobileSplit = isMobilePolicySplit();
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700653 if (mobileSplit && hasReadyMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700654 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
655 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700656 } else if (hasReadyMobileRadio(context)) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700657 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700658 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700659 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700660 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
661 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700662 if (mShowEthernet && hasEthernet(context)) {
663 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
664 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700665
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700666 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700667 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
668 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
669 if (mIntentTab != null) {
670 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700671 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700672 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700673 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700674 mTabHost.setCurrentTabByTag(mIntentTab);
675 }
676 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700677 } else if (noTabs) {
678 // no usable tabs, so hide body
679 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700680 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700681 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700682 }
683 }
684
Jeff Sharkey8a503642011-06-10 13:31:21 -0700685 /**
686 * Factory that provide empty {@link View} to make {@link TabHost} happy.
687 */
688 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700689 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700690 public View createTabContent(String tag) {
691 return new View(mTabHost.getContext());
692 }
693 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700694
Jeff Sharkey8a503642011-06-10 13:31:21 -0700695 /**
696 * Build {@link TabSpec} with thin indicator, and empty content.
697 */
698 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700699 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
700 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700701 }
702
703 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700704 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700705 public void onTabChanged(String tabId) {
706 // user changed tab; update body
707 updateBody();
708 }
709 };
710
711 /**
712 * Update body content based on current tab. Loads
713 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
714 * binds them to visible controls.
715 */
716 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700717 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700718 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700719
Jeff Sharkeya662e492011-06-18 21:57:06 -0700720 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700721 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700722 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700723
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700724 if (currentTab == null) {
725 Log.w(TAG, "no tab selected; hiding body");
726 mListView.setVisibility(View.GONE);
727 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700728 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700729 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700730 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700731
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700732 mCurrentTab = currentTab;
733
Jeff Sharkey8a503642011-06-10 13:31:21 -0700734 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
735
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700736 mDataEnabledSupported = isOwner;
737 mDisableAtLimitSupported = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700738
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700739 // TODO: remove mobile tabs when SIM isn't ready
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700740
Jeff Sharkey8a503642011-06-10 13:31:21 -0700741 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700742 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
743 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700744 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700745
746 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700747 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
748 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
749 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700750 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700751
752 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700753 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
754 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
755 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700756 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700757
758 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700759 // wifi doesn't have any controls
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700760 mDataEnabledSupported = false;
761 mDisableAtLimitSupported = false;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700762 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700763
764 } else if (TAB_ETHERNET.equals(currentTab)) {
765 // ethernet doesn't have any controls
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700766 mDataEnabledSupported = false;
767 mDisableAtLimitSupported = false;
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700768 mTemplate = buildTemplateEthernet();
769
770 } else {
771 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700772 }
773
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700774 // kick off loader for network history
775 // TODO: consider chaining two loaders together instead of reloading
776 // network history when showing app detail.
777 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700778 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700779
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700780 // detail mode can change visible menus, invalidate
781 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700782
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700783 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700784 }
785
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700786 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700787 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700788 }
789
790 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700791 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700792 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700793 */
794 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700795 final Context context = getActivity();
796 final PackageManager pm = context.getPackageManager();
797 final LayoutInflater inflater = getActivity().getLayoutInflater();
798
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700799 if (isAppDetailMode()) {
800 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700801 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700802 } else {
803 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700804 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700805
806 // hide detail stats when not in detail mode
807 mChart.bindDetailNetworkStats(null);
808 return;
809 }
810
811 // remove warning/limit sweeps while in detail mode
812 mChart.bindNetworkPolicy(null);
813
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700814 // show icon and all labels appearing under this app
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700815 final int uid = mCurrentApp.key;
816 final UidDetail detail = mUidDetailProvider.getUidDetail(uid, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700817 mAppIcon.setImageDrawable(detail.icon);
818
819 mAppTitles.removeAllViews();
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700820
821 View title = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700822 if (detail.detailLabels != null) {
823 for (CharSequence label : detail.detailLabels) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700824 title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false);
825 ((TextView) title.findViewById(R.id.app_title)).setText(label);
826 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);
830 ((TextView) title.findViewById(R.id.app_title)).setText(detail.label);
831 mAppTitles.addView(title);
832 }
833
834 // Remember last slot for summary
835 if (title != null) {
836 mAppTotal = (TextView) title.findViewById(R.id.app_summary);
837 } else {
838 mAppTotal = null;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700839 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700840
841 // enable settings button when package provides it
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700842 final String[] packageNames = pm.getPackagesForUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700843 if (packageNames != null && packageNames.length > 0) {
844 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700845 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
846
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700847 // Search for match across all packages
848 boolean matchFound = false;
849 for (String packageName : packageNames) {
850 mAppSettingsIntent.setPackage(packageName);
851 if (pm.resolveActivity(mAppSettingsIntent, 0) != null) {
852 matchFound = true;
853 break;
854 }
855 }
856
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700857 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -0700858 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700859
860 } else {
861 mAppSettingsIntent = null;
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700862 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700863 }
864
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700865 updateDetailData();
866
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700867 if (UserHandle.isApp(uid) && !mPolicyManager.getRestrictBackground()
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700868 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700869 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800870 setPreferenceSummary(mAppRestrictView,
871 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700872
873 mAppRestrictView.setVisibility(View.VISIBLE);
874 mAppRestrict.setChecked(getAppRestrictBackground());
875
876 } else {
877 mAppRestrictView.setVisibility(View.GONE);
878 }
879 }
880
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700881 private void setPolicyWarningBytes(long warningBytes) {
882 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700883 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700884 updatePolicy(false);
885 }
886
887 private void setPolicyLimitBytes(long limitBytes) {
888 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700889 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700890 updatePolicy(false);
891 }
892
Jeff Sharkey28130d92011-09-02 16:10:24 -0700893 /**
894 * Local cache of value, used to work around delay when
895 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
896 */
897 private Boolean mMobileDataEnabled;
898
899 private boolean isMobileDataEnabled() {
900 if (mMobileDataEnabled != null) {
901 // TODO: deprecate and remove this once enabled flag is on policy
902 return mMobileDataEnabled;
903 } else {
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700904 return mTelephonyManager.getDataEnabled();
Jeff Sharkey28130d92011-09-02 16:10:24 -0700905 }
906 }
907
908 private void setMobileDataEnabled(boolean enabled) {
909 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700910 mTelephonyManager.setDataEnabled(enabled);
Jeff Sharkey28130d92011-09-02 16:10:24 -0700911 mMobileDataEnabled = enabled;
912 updatePolicy(false);
913 }
914
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700915 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700916 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked()
917 && ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700918 }
919
920 private boolean isBandwidthControlEnabled() {
921 try {
922 return mNetworkService.isBandwidthControlEnabled();
923 } catch (RemoteException e) {
924 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
925 return false;
926 }
927 }
928
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700929 public void setRestrictBackground(boolean restrictBackground) {
930 mPolicyManager.setRestrictBackground(restrictBackground);
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700931 updateMenuTitles();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700932 }
933
934 private boolean getAppRestrictBackground() {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700935 final int uid = mCurrentApp.key;
936 final int uidPolicy = mPolicyManager.getUidPolicy(uid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700937 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
938 }
939
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700940 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700941 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700942 final int uid = mCurrentApp.key;
943 mPolicyManager.setUidPolicy(
944 uid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700945 mAppRestrict.setChecked(restrictBackground);
946 }
947
Jeff Sharkey8a503642011-06-10 13:31:21 -0700948 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700949 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
950 * current {@link #mTemplate}.
951 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700952 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700953 boolean dataEnabledVisible = mDataEnabledSupported;
954 boolean disableAtLimitVisible = mDisableAtLimitSupported;
955
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700956 if (isAppDetailMode()) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700957 dataEnabledVisible = false;
958 disableAtLimitVisible = false;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700959 }
960
Jeff Sharkey28130d92011-09-02 16:10:24 -0700961 // TODO: move enabled state directly into policy
962 if (TAB_MOBILE.equals(mCurrentTab)) {
963 mBinding = true;
964 mDataEnabled.setChecked(isMobileDataEnabled());
965 mBinding = false;
966 }
967
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700968 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
969 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700970 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700971 if (!isAppDetailMode()) {
972 mChart.bindNetworkPolicy(policy);
973 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700974
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700975 } else {
976 // controls are disabled; don't bind warning/limit sweeps
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700977 disableAtLimitVisible = false;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700978 mChart.bindNetworkPolicy(null);
979 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700980
Jeff Sharkeyb6548462014-07-21 15:38:06 -0700981 mDataEnabledView.setVisibility(dataEnabledVisible ? View.VISIBLE : View.GONE);
982 mDisableAtLimitView.setVisibility(disableAtLimitVisible ? View.VISIBLE : View.GONE);
983
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700984 if (refreshCycle) {
985 // generate cycle list based on policy and available history
986 updateCycleList(policy);
987 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700988 }
989
990 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700991 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
992 * and available {@link NetworkStatsHistory} data. Always selects the newest
993 * item, updating the inspection range on {@link #mChart}.
994 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700995 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700996 // stash away currently selected cycle to try restoring below
997 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700998 mCycleAdapter.clear();
999
1000 final Context context = mCycleSpinner.getContext();
1001
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001002 long historyStart = Long.MAX_VALUE;
1003 long historyEnd = Long.MIN_VALUE;
1004 if (mChartData != null) {
1005 historyStart = mChartData.network.getStart();
1006 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001007 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001008
Jeff Sharkey461842a2011-09-25 18:22:48 -07001009 final long now = System.currentTimeMillis();
1010 if (historyStart == Long.MAX_VALUE) historyStart = now;
1011 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001012
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001013 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001014 if (policy != null) {
1015 // find the next cycle boundary
1016 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001017
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001018 // walk backwards, generating all valid cycle ranges
1019 while (cycleEnd > historyStart) {
1020 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
1021 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
1022 + historyStart);
1023 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1024 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001025 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001026 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001027
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001028 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001029 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001030 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001031
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001032 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001033 // no policy defined cycles; show entry for each four-week period
1034 long cycleEnd = historyEnd;
1035 while (cycleEnd > historyStart) {
1036 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
1037 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1038 cycleEnd = cycleStart;
1039 }
1040
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001041 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001042 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001043
1044 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001045 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001046 final int position = mCycleAdapter.findNearestPosition(previousItem);
1047 mCycleSpinner.setSelection(position);
1048
1049 // only force-update cycle when changed; skipping preserves any
1050 // user-defined inspection region.
1051 final CycleItem selectedItem = mCycleAdapter.getItem(position);
1052 if (!Objects.equal(selectedItem, previousItem)) {
1053 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1054 } else {
1055 // but still kick off loader for detailed list
1056 updateDetailData();
1057 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001058 } else {
1059 updateDetailData();
1060 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001061 }
1062
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001063 private View.OnClickListener mDataEnabledListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001064 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001065 public void onClick(View v) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001066 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001067
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001068 final boolean dataEnabled = !mDataEnabled.isChecked();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001069 final String currentTab = mCurrentTab;
1070 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001071 if (dataEnabled) {
1072 setMobileDataEnabled(true);
1073 } else {
1074 // disabling data; show confirmation dialog which eventually
1075 // calls setMobileDataEnabled() once user confirms.
1076 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1077 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001078 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001079
Jeff Sharkey28130d92011-09-02 16:10:24 -07001080 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001081 }
1082 };
1083
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001084 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001085 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001086 public void onClick(View v) {
1087 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001088 if (disableAtLimit) {
1089 // enabling limit; show confirmation dialog which eventually
1090 // calls setPolicyLimitBytes() once user confirms.
1091 ConfirmLimitFragment.show(DataUsageSummary.this);
1092 } else {
1093 setPolicyLimitBytes(LIMIT_DISABLED);
1094 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001095 }
1096 };
1097
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001098 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001099 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001100 public void onClick(View v) {
1101 final boolean restrictBackground = !mAppRestrict.isChecked();
1102
1103 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001104 // enabling restriction; show confirmation dialog which
1105 // eventually calls setRestrictBackground() once user
1106 // confirms.
1107 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001108 } else {
1109 setAppRestrictBackground(false);
1110 }
1111 }
1112 };
1113
1114 private OnClickListener mAppSettingsListener = new OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001115 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001116 public void onClick(View v) {
Jeff Sharkeyd92e0412012-04-24 11:35:43 -07001117 if (!isAdded()) return;
1118
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001119 // TODO: target torwards entire UID instead of just first package
1120 startActivity(mAppSettingsIntent);
1121 }
1122 };
1123
Jeff Sharkey8a503642011-06-10 13:31:21 -07001124 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001125 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001126 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001127 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001128 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001129
1130 // TODO: sigh, remove this hack once we understand 6450986
1131 if (mUidDetailProvider == null || app == null) return;
1132
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001133 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001134 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001135 }
1136 };
1137
1138 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001139 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001140 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1141 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1142 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001143 // show cycle editor; will eventually call setPolicyCycleDay()
1144 // when user finishes editing.
1145 CycleEditorFragment.show(DataUsageSummary.this);
1146
1147 // reset spinner to something other than "change cycle..."
1148 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001149
1150 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001151 if (LOGD) {
1152 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1153 + cycle.end + "]");
1154 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001155
1156 // update chart to show selected cycle, and update detail data
1157 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001158 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001159
1160 updateDetailData();
1161 }
1162 }
1163
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001164 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001165 public void onNothingSelected(AdapterView<?> parent) {
1166 // ignored
1167 }
1168 };
1169
1170 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001171 * Update details based on {@link #mChart} inspection range depending on
1172 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1173 * of applications data usage, and when {@link #isAppDetailMode()} update
1174 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001175 */
1176 private void updateDetailData() {
1177 if (LOGD) Log.d(TAG, "updateDetailData()");
1178
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001179 final long start = mChart.getInspectStart();
1180 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001181 final long now = System.currentTimeMillis();
1182
1183 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001184
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001185 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001186 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001187 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001188 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001189 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001190 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001191 final long foregroundBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001192 final long totalBytes = defaultBytes + foregroundBytes;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001193
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001194 if (mAppTotal != null) {
1195 mAppTotal.setText(Formatter.formatFileSize(context, totalBytes));
1196 }
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001197 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1198 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1199
1200 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001201 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001202
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001203 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001204
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001205 mCycleSummary.setVisibility(View.GONE);
1206
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001207 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001208 if (mChartData != null) {
1209 entry = mChartData.network.getValues(start, end, now, null);
1210 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001211
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001212 mCycleSummary.setVisibility(View.VISIBLE);
1213
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001214 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001215 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001216 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001217 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001218
1219 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1220 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001221 mCycleSummary.setText(totalPhrase);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001222
László Dávid0b8bf4e2012-10-24 23:31:47 +02001223 if (TAB_MOBILE.equals(mCurrentTab) || TAB_3G.equals(mCurrentTab)
1224 || TAB_4G.equals(mCurrentTab)) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001225 if (isAppDetailMode()) {
1226 mDisclaimer.setVisibility(View.GONE);
1227 } else {
1228 mDisclaimer.setVisibility(View.VISIBLE);
1229 }
Jeff Sharkeye557c332012-04-13 16:04:07 -07001230 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001231 mDisclaimer.setVisibility(View.GONE);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001232 }
1233
Jeff Sharkey92811822012-05-04 11:47:29 -07001234 // initial layout is finished above, ensure we have transitions
1235 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001236 }
1237
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001238 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1239 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001240 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001241 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001242 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001243 }
1244
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001245 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001246 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1247 mChartData = data;
1248 mChart.bindNetworkStats(mChartData.network);
1249 mChart.bindDetailNetworkStats(mChartData.detail);
1250
1251 // calcuate policy cycles based on available data
1252 updatePolicy(true);
1253 updateAppDetail();
1254
1255 // force scroll to top of body when showing detail
1256 if (mChartData.detail != null) {
1257 mListView.smoothScrollToPosition(0);
1258 }
1259 }
1260
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001261 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001262 public void onLoaderReset(Loader<ChartData> loader) {
1263 mChartData = null;
1264 mChart.bindNetworkStats(null);
1265 mChart.bindDetailNetworkStats(null);
1266 }
1267 };
1268
1269 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001270 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001271 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001272 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001273 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001274 }
1275
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001276 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001277 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001278 final int[] restrictedUids = mPolicyManager.getUidsWithPolicy(
Jeff Sharkeye557c332012-04-13 16:04:07 -07001279 POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001280 mAdapter.bindStats(data, restrictedUids);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001281 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001282 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001283
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001284 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001285 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001286 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001287 updateEmptyVisible();
1288 }
1289
1290 private void updateEmptyVisible() {
1291 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1292 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001293 mStupidPadding.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001294 }
1295 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001296
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001297 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001298 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001299 final Context context = getActivity();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001300 if (hasReadyMobileRadio(context)) {
1301 final TelephonyManager tele = TelephonyManager.from(context);
1302 return mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001303 } else {
1304 return false;
1305 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001306 }
1307
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001308 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001309 private void setMobilePolicySplit(boolean split) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001310 final Context context = getActivity();
1311 if (hasReadyMobileRadio(context)) {
1312 final TelephonyManager tele = TelephonyManager.from(context);
1313 mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split);
1314 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001315 }
1316
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001317 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001318 final TelephonyManager tele = TelephonyManager.from(context);
1319 final String actualSubscriberId = tele.getSubscriberId();
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -08001320 return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001321 }
1322
Jeff Sharkey8a503642011-06-10 13:31:21 -07001323 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001324 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001325 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001326 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001327 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001328
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001329 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001330 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001331 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001332 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001333
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001334 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001335 public void requestWarningEdit() {
1336 WarningEditorFragment.show(DataUsageSummary.this);
1337 }
1338
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001339 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001340 public void requestLimitEdit() {
1341 LimitEditorFragment.show(DataUsageSummary.this);
1342 }
1343 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001344
1345 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001346 * List item that reflects a specific data usage cycle.
1347 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001348 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001349 public CharSequence label;
1350 public long start;
1351 public long end;
1352
Jeff Sharkey8a503642011-06-10 13:31:21 -07001353 CycleItem(CharSequence label) {
1354 this.label = label;
1355 }
1356
1357 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001358 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001359 this.start = start;
1360 this.end = end;
1361 }
1362
Jeff Sharkey8a503642011-06-10 13:31:21 -07001363 @Override
1364 public String toString() {
1365 return label.toString();
1366 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001367
1368 @Override
1369 public boolean equals(Object o) {
1370 if (o instanceof CycleItem) {
1371 final CycleItem another = (CycleItem) o;
1372 return start == another.start && end == another.end;
1373 }
1374 return false;
1375 }
1376
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001377 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001378 public int compareTo(CycleItem another) {
1379 return Long.compare(start, another.start);
1380 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001381 }
1382
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001383 private static final StringBuilder sBuilder = new StringBuilder(50);
1384 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1385 sBuilder, Locale.getDefault());
1386
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001387 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001388 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001389
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001390 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001391 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001392 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1393 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001394 }
1395 }
1396
Jeff Sharkey8a503642011-06-10 13:31:21 -07001397 /**
1398 * Special-case data usage cycle that triggers dialog to change
1399 * {@link NetworkPolicy#cycleDay}.
1400 */
1401 public static class CycleChangeItem extends CycleItem {
1402 public CycleChangeItem(Context context) {
1403 super(context.getString(R.string.data_usage_change_cycle));
1404 }
1405 }
1406
1407 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001408 private boolean mChangePossible = false;
1409 private boolean mChangeVisible = false;
1410
1411 private final CycleChangeItem mChangeItem;
1412
Jeff Sharkey8a503642011-06-10 13:31:21 -07001413 public CycleAdapter(Context context) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001414 super(context, R.layout.data_usage_cycle_item);
1415 setDropDownViewResource(R.layout.data_usage_cycle_item_dropdown);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001416 mChangeItem = new CycleChangeItem(context);
1417 }
1418
1419 public void setChangePossible(boolean possible) {
1420 mChangePossible = possible;
1421 updateChange();
1422 }
1423
1424 public void setChangeVisible(boolean visible) {
1425 mChangeVisible = visible;
1426 updateChange();
1427 }
1428
1429 private void updateChange() {
1430 remove(mChangeItem);
1431 if (mChangePossible && mChangeVisible) {
1432 add(mChangeItem);
1433 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001434 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001435
1436 /**
1437 * Find position of {@link CycleItem} in this adapter which is nearest
1438 * the given {@link CycleItem}.
1439 */
1440 public int findNearestPosition(CycleItem target) {
1441 if (target != null) {
1442 final int count = getCount();
1443 for (int i = count - 1; i >= 0; i--) {
1444 final CycleItem item = getItem(i);
1445 if (item instanceof CycleChangeItem) {
1446 continue;
1447 } else if (item.compareTo(target) >= 0) {
1448 return i;
1449 }
1450 }
1451 }
1452 return 0;
1453 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001454 }
1455
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001456 public static class AppItem implements Comparable<AppItem>, Parcelable {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001457 public static final int CATEGORY_USER = 0;
1458 public static final int CATEGORY_APP_TITLE = 1;
1459 public static final int CATEGORY_APP = 2;
1460
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001461 public final int key;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001462 public boolean restricted;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001463 public int category;
1464
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001465 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001466 public long total;
1467
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001468 public AppItem() {
1469 this.key = 0;
1470 }
1471
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001472 public AppItem(int key) {
1473 this.key = key;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001474 }
1475
1476 public AppItem(Parcel parcel) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001477 key = parcel.readInt();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001478 uids = parcel.readSparseBooleanArray();
1479 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001480 }
1481
1482 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001483 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001484 }
1485
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001486 @Override
1487 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001488 dest.writeInt(key);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001489 dest.writeSparseBooleanArray(uids);
1490 dest.writeLong(total);
1491 }
1492
1493 @Override
1494 public int describeContents() {
1495 return 0;
1496 }
1497
1498 @Override
1499 public int compareTo(AppItem another) {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001500 int comparison = Integer.compare(another.category, category);
1501 if (comparison == 0) {
1502 comparison = Long.compare(another.total, total);
1503 }
1504 return comparison;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001505 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001506
1507 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1508 @Override
1509 public AppItem createFromParcel(Parcel in) {
1510 return new AppItem(in);
1511 }
1512
1513 @Override
1514 public AppItem[] newArray(int size) {
1515 return new AppItem[size];
1516 }
1517 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001518 }
1519
Jeff Sharkey8a503642011-06-10 13:31:21 -07001520 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001521 * Adapter of applications, sorted by total usage descending.
1522 */
1523 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001524 private final UidDetailProvider mProvider;
1525 private final int mInsetSide;
1526
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001527 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001528 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001529
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001530 public DataUsageAdapter(UidDetailProvider provider, int insetSide) {
1531 mProvider = checkNotNull(provider);
1532 mInsetSide = insetSide;
1533 }
1534
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001535 /**
1536 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1537 */
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001538 public void bindStats(NetworkStats stats, int[] restrictedUids) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001539 mItems.clear();
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001540 mLargest = 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001541
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001542 final int currentUserId = ActivityManager.getCurrentUser();
1543 final SparseArray<AppItem> knownItems = new SparseArray<AppItem>();
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001544 boolean hasApps = false;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001545
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001546 NetworkStats.Entry entry = null;
1547 final int size = stats != null ? stats.size() : 0;
1548 for (int i = 0; i < size; i++) {
1549 entry = stats.getValues(i, entry);
1550
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001551 // Decide how to collapse items together
1552 final int uid = entry.uid;
1553 final int collapseKey;
1554 if (UserHandle.isApp(uid)) {
1555 if (UserHandle.getUserId(uid) == currentUserId) {
1556 collapseKey = uid;
1557 } else {
1558 collapseKey = UidDetailProvider.buildKeyForUser(UserHandle.getUserId(uid));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001559 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001560 } else if (uid == UID_REMOVED || uid == UID_TETHERING) {
1561 collapseKey = uid;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001562 } else {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001563 collapseKey = android.os.Process.SYSTEM_UID;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001564 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001565
1566 AppItem item = knownItems.get(collapseKey);
1567 if (item == null) {
1568 item = new AppItem(collapseKey);
1569 mItems.add(item);
1570 knownItems.put(item.key, item);
1571 }
1572 item.addUid(uid);
1573 item.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001574 if (item.total > mLargest) {
1575 mLargest = item.total;
1576 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001577 }
1578
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001579 for (int uid : restrictedUids) {
1580 // Only splice in restricted state for current user
1581 if (UserHandle.getUserId(uid) != currentUserId) continue;
1582
1583 AppItem item = knownItems.get(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001584 if (item == null) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001585 item = new AppItem(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001586 item.total = -1;
1587 mItems.add(item);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001588 knownItems.put(item.key, item);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001589 }
1590 item.restricted = true;
1591 }
1592
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001593 hasApps = !mItems.isEmpty();
1594 if (hasApps) {
1595 final AppItem title = new AppItem();
1596 title.category = AppItem.CATEGORY_APP_TITLE;
1597 mItems.add(title);
1598 }
1599
Jeff Sharkey8a503642011-06-10 13:31:21 -07001600 Collections.sort(mItems);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001601 notifyDataSetChanged();
1602 }
1603
1604 @Override
1605 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001606 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001607 }
1608
1609 @Override
1610 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001611 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001612 }
1613
1614 @Override
1615 public long getItemId(int position) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001616 return mItems.get(position).key;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001617 }
1618
1619 @Override
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001620 public int getViewTypeCount() {
1621 return 2;
1622 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001623
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001624 @Override
1625 public int getItemViewType(int position) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001626 final AppItem item = mItems.get(position);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001627 if (item.category == AppItem.CATEGORY_APP_TITLE) {
1628 return 1;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001629 } else {
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001630 return 0;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001631 }
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001632 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001633
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001634 @Override
1635 public boolean areAllItemsEnabled() {
1636 return false;
1637 }
1638
1639 @Override
1640 public boolean isEnabled(int position) {
1641 return getItemViewType(position) == 0;
1642 }
1643
1644 @Override
1645 public View getView(int position, View convertView, ViewGroup parent) {
1646 final AppItem item = mItems.get(position);
1647 if (getItemViewType(position) == 1) {
1648 if (convertView == null) {
1649 convertView = inflateCategoryHeader(LayoutInflater.from(parent.getContext()),
1650 parent);
1651 }
1652
1653 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
1654 title.setText(R.string.data_usage_app);
1655
1656 } else {
1657 if (convertView == null) {
1658 convertView = LayoutInflater.from(parent.getContext()).inflate(
1659 R.layout.data_usage_item, parent, false);
1660
1661 if (mInsetSide > 0) {
1662 convertView.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
1663 }
1664 }
1665
1666 final Context context = parent.getContext();
1667
1668 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
1669 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1670 android.R.id.progress);
1671
1672 // kick off async load of app details
1673 UidDetailTask.bindView(mProvider, item, convertView);
1674
1675 if (item.restricted && item.total <= 0) {
1676 text1.setText(R.string.data_usage_app_restricted);
1677 progress.setVisibility(View.GONE);
1678 } else {
1679 text1.setText(Formatter.formatFileSize(context, item.total));
1680 progress.setVisibility(View.VISIBLE);
1681 }
1682
1683 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1684 progress.setProgress(percentTotal);
1685 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001686
1687 return convertView;
1688 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001689 }
1690
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001691 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001692 * Empty {@link Fragment} that controls display of UID details in
1693 * {@link DataUsageSummary}.
1694 */
1695 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001696 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001697
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001698 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001699 if (!parent.isAdded()) return;
1700
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001701 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001702 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001703
1704 final AppDetailsFragment fragment = new AppDetailsFragment();
1705 fragment.setArguments(args);
1706 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001707 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1708 ft.add(fragment, TAG_APP_DETAILS);
1709 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkeyb6548462014-07-21 15:38:06 -07001710 ft.setBreadCrumbTitle(
1711 parent.getResources().getString(R.string.data_usage_app_summary_title));
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001712 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001713 }
1714
1715 @Override
1716 public void onStart() {
1717 super.onStart();
1718 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001719 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001720 target.updateBody();
1721 }
1722
1723 @Override
1724 public void onStop() {
1725 super.onStop();
1726 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001727 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001728 target.updateBody();
1729 }
1730 }
1731
1732 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001733 * Dialog to request user confirmation before setting
1734 * {@link NetworkPolicy#limitBytes}.
1735 */
1736 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001737 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001738 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001739
1740 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001741 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001742
Jeff Sharkey461842a2011-09-25 18:22:48 -07001743 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001744 final CharSequence message;
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001745 final long minLimitBytes = (long) (
1746 parent.mPolicyEditor.getPolicy(parent.mTemplate).warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001747 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001748
1749 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001750 final String currentTab = parent.mCurrentTab;
1751 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001752 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001753 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001754 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001755 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001756 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001757 } else if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001758 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001759 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001760 } else {
1761 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001762 }
1763
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001764 final Bundle args = new Bundle();
1765 args.putCharSequence(EXTRA_MESSAGE, message);
1766 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1767
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001768 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1769 dialog.setArguments(args);
1770 dialog.setTargetFragment(parent, 0);
1771 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1772 }
1773
1774 @Override
1775 public Dialog onCreateDialog(Bundle savedInstanceState) {
1776 final Context context = getActivity();
1777
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001778 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001779 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1780
1781 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1782 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001783 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001784
1785 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001786 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001787 public void onClick(DialogInterface dialog, int which) {
1788 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1789 if (target != null) {
1790 target.setPolicyLimitBytes(limitBytes);
1791 }
1792 }
1793 });
1794
1795 return builder.create();
1796 }
1797 }
1798
1799 /**
1800 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1801 */
1802 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001803 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001804
1805 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001806 if (!parent.isAdded()) return;
1807
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001808 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001809 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001810
1811 final CycleEditorFragment dialog = new CycleEditorFragment();
1812 dialog.setArguments(args);
1813 dialog.setTargetFragment(parent, 0);
1814 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1815 }
1816
1817 @Override
1818 public Dialog onCreateDialog(Bundle savedInstanceState) {
1819 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001820 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1821 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001822
1823 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1824 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1825
1826 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1827 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1828
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001829 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1830 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001831
1832 cycleDayPicker.setMinValue(1);
1833 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001834 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001835 cycleDayPicker.setWrapSelectorWheel(true);
1836
1837 builder.setTitle(R.string.data_usage_cycle_editor_title);
1838 builder.setView(view);
1839
1840 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1841 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001842 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001843 public void onClick(DialogInterface dialog, int which) {
Jeff Sharkeyd277de92013-03-25 15:11:25 -07001844 // clear focus to finish pending text edits
1845 cycleDayPicker.clearFocus();
1846
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001847 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001848 final String cycleTimezone = new Time().timezone;
1849 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001850 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001851 }
1852 });
1853
1854 return builder.create();
1855 }
1856 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001857
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001858 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001859 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1860 */
1861 public static class WarningEditorFragment extends DialogFragment {
1862 private static final String EXTRA_TEMPLATE = "template";
1863
1864 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001865 if (!parent.isAdded()) return;
1866
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001867 final Bundle args = new Bundle();
1868 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1869
1870 final WarningEditorFragment dialog = new WarningEditorFragment();
1871 dialog.setArguments(args);
1872 dialog.setTargetFragment(parent, 0);
1873 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1874 }
1875
1876 @Override
1877 public Dialog onCreateDialog(Bundle savedInstanceState) {
1878 final Context context = getActivity();
1879 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1880 final NetworkPolicyEditor editor = target.mPolicyEditor;
1881
1882 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1883 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1884
1885 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1886 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1887
1888 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1889 final long warningBytes = editor.getPolicyWarningBytes(template);
1890 final long limitBytes = editor.getPolicyLimitBytes(template);
1891
1892 bytesPicker.setMinValue(0);
1893 if (limitBytes != LIMIT_DISABLED) {
1894 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1895 } else {
1896 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1897 }
1898 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1899 bytesPicker.setWrapSelectorWheel(false);
1900
1901 builder.setTitle(R.string.data_usage_warning_editor_title);
1902 builder.setView(view);
1903
1904 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1905 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001906 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001907 public void onClick(DialogInterface dialog, int which) {
1908 // clear focus to finish pending text edits
1909 bytesPicker.clearFocus();
1910
1911 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1912 editor.setPolicyWarningBytes(template, bytes);
1913 target.updatePolicy(false);
1914 }
1915 });
1916
1917 return builder.create();
1918 }
1919 }
1920
1921 /**
1922 * Dialog to edit {@link NetworkPolicy#limitBytes}.
1923 */
1924 public static class LimitEditorFragment extends DialogFragment {
1925 private static final String EXTRA_TEMPLATE = "template";
1926
1927 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001928 if (!parent.isAdded()) return;
1929
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001930 final Bundle args = new Bundle();
1931 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1932
1933 final LimitEditorFragment dialog = new LimitEditorFragment();
1934 dialog.setArguments(args);
1935 dialog.setTargetFragment(parent, 0);
1936 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
1937 }
1938
1939 @Override
1940 public Dialog onCreateDialog(Bundle savedInstanceState) {
1941 final Context context = getActivity();
1942 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1943 final NetworkPolicyEditor editor = target.mPolicyEditor;
1944
1945 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1946 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1947
1948 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1949 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1950
1951 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1952 final long warningBytes = editor.getPolicyWarningBytes(template);
1953 final long limitBytes = editor.getPolicyLimitBytes(template);
1954
1955 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09001956 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001957 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
1958 } else {
1959 bytesPicker.setMinValue(0);
1960 }
1961 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
1962 bytesPicker.setWrapSelectorWheel(false);
1963
1964 builder.setTitle(R.string.data_usage_limit_editor_title);
1965 builder.setView(view);
1966
1967 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1968 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001969 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001970 public void onClick(DialogInterface dialog, int which) {
1971 // clear focus to finish pending text edits
1972 bytesPicker.clearFocus();
1973
1974 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1975 editor.setPolicyLimitBytes(template, bytes);
1976 target.updatePolicy(false);
1977 }
1978 });
1979
1980 return builder.create();
1981 }
1982 }
1983 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07001984 * Dialog to request user confirmation before disabling data.
1985 */
1986 public static class ConfirmDataDisableFragment extends DialogFragment {
1987 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001988 if (!parent.isAdded()) return;
1989
Jeff Sharkey28130d92011-09-02 16:10:24 -07001990 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
1991 dialog.setTargetFragment(parent, 0);
1992 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
1993 }
1994
1995 @Override
1996 public Dialog onCreateDialog(Bundle savedInstanceState) {
1997 final Context context = getActivity();
1998
1999 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2000 builder.setMessage(R.string.data_usage_disable_mobile);
2001
2002 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002003 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07002004 public void onClick(DialogInterface dialog, int which) {
2005 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2006 if (target != null) {
2007 // TODO: extend to modify policy enabled flag.
2008 target.setMobileDataEnabled(false);
2009 }
2010 }
2011 });
2012 builder.setNegativeButton(android.R.string.cancel, null);
2013
2014 return builder.create();
2015 }
2016 }
2017
2018 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002019 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07002020 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002021 */
2022 public static class ConfirmRestrictFragment extends DialogFragment {
2023 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002024 if (!parent.isAdded()) return;
2025
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002026 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
2027 dialog.setTargetFragment(parent, 0);
2028 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
2029 }
2030
2031 @Override
2032 public Dialog onCreateDialog(Bundle savedInstanceState) {
2033 final Context context = getActivity();
2034
2035 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002036 builder.setTitle(R.string.data_usage_restrict_background_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07002037 if (Utils.hasMultipleUsers(context)) {
2038 builder.setMessage(R.string.data_usage_restrict_background_multiuser);
2039 } else {
2040 builder.setMessage(R.string.data_usage_restrict_background);
2041 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002042
2043 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002044 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002045 public void onClick(DialogInterface dialog, int which) {
2046 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2047 if (target != null) {
2048 target.setRestrictBackground(true);
2049 }
2050 }
2051 });
2052 builder.setNegativeButton(android.R.string.cancel, null);
2053
2054 return builder.create();
2055 }
2056 }
2057
2058 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002059 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
2060 * change has been denied, usually based on
2061 * {@link DataUsageSummary#hasLimitedNetworks()}.
2062 */
2063 public static class DeniedRestrictFragment extends DialogFragment {
2064 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002065 if (!parent.isAdded()) return;
2066
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002067 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
2068 dialog.setTargetFragment(parent, 0);
2069 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
2070 }
2071
2072 @Override
2073 public Dialog onCreateDialog(Bundle savedInstanceState) {
2074 final Context context = getActivity();
2075
2076 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2077 builder.setTitle(R.string.data_usage_app_restrict_background);
2078 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
2079 builder.setPositiveButton(android.R.string.ok, null);
2080
2081 return builder.create();
2082 }
2083 }
2084
2085 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002086 * Dialog to request user confirmation before setting
2087 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
2088 */
2089 public static class ConfirmAppRestrictFragment extends DialogFragment {
2090 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002091 if (!parent.isAdded()) return;
2092
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002093 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2094 dialog.setTargetFragment(parent, 0);
2095 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2096 }
2097
2098 @Override
2099 public Dialog onCreateDialog(Bundle savedInstanceState) {
2100 final Context context = getActivity();
2101
2102 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002103 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2104 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2105
2106 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002107 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002108 public void onClick(DialogInterface dialog, int which) {
2109 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2110 if (target != null) {
2111 target.setAppRestrictBackground(true);
2112 }
2113 }
2114 });
2115 builder.setNegativeButton(android.R.string.cancel, null);
2116
2117 return builder.create();
2118 }
2119 }
2120
2121 /**
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002122 * Dialog to inform user about changing auto-sync setting
2123 */
2124 public static class ConfirmAutoSyncChangeFragment extends DialogFragment {
Amith Yamasani665235f2012-06-07 19:58:34 -07002125 private static final String SAVE_ENABLING = "enabling";
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002126 private boolean mEnabling;
2127
2128 public static void show(DataUsageSummary parent, boolean enabling) {
2129 if (!parent.isAdded()) return;
2130
2131 final ConfirmAutoSyncChangeFragment dialog = new ConfirmAutoSyncChangeFragment();
2132 dialog.mEnabling = enabling;
2133 dialog.setTargetFragment(parent, 0);
2134 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_AUTO_SYNC_CHANGE);
2135 }
2136
2137 @Override
2138 public Dialog onCreateDialog(Bundle savedInstanceState) {
2139 final Context context = getActivity();
Amith Yamasani665235f2012-06-07 19:58:34 -07002140 if (savedInstanceState != null) {
2141 mEnabling = savedInstanceState.getBoolean(SAVE_ENABLING);
2142 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002143
2144 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2145 if (!mEnabling) {
2146 builder.setTitle(R.string.data_usage_auto_sync_off_dialog_title);
2147 builder.setMessage(R.string.data_usage_auto_sync_off_dialog);
2148 } else {
2149 builder.setTitle(R.string.data_usage_auto_sync_on_dialog_title);
2150 builder.setMessage(R.string.data_usage_auto_sync_on_dialog);
2151 }
2152
2153 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
2154 @Override
2155 public void onClick(DialogInterface dialog, int which) {
2156 ContentResolver.setMasterSyncAutomatically(mEnabling);
2157 }
2158 });
2159 builder.setNegativeButton(android.R.string.cancel, null);
2160
2161 return builder.create();
2162 }
Amith Yamasani665235f2012-06-07 19:58:34 -07002163
2164 @Override
2165 public void onSaveInstanceState(Bundle outState) {
2166 super.onSaveInstanceState(outState);
2167 outState.putBoolean(SAVE_ENABLING, mEnabling);
2168 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002169 }
2170
2171 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002172 * Compute default tab that should be selected, based on
2173 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2174 */
2175 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002176 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
2177 if (template == null) return null;
2178
2179 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002180 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002181 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002182 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002183 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002184 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002185 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002186 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002187 return TAB_WIFI;
2188 default:
2189 return null;
2190 }
2191 }
2192
2193 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002194 * Background task that loads {@link UidDetail}, binding to
2195 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002196 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002197 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2198 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002199 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002200 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002201
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002202 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002203 mProvider = checkNotNull(provider);
2204 mItem = checkNotNull(item);
2205 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002206 }
2207
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002208 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002209 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002210 final UidDetailTask existing = (UidDetailTask) target.getTag();
2211 if (existing != null) {
2212 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002213 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002214
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002215 final UidDetail cachedDetail = provider.getUidDetail(item.key, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002216 if (cachedDetail != null) {
2217 bindView(cachedDetail, target);
2218 } else {
2219 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2220 AsyncTask.THREAD_POOL_EXECUTOR));
2221 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002222 }
2223
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002224 private static void bindView(UidDetail detail, View target) {
2225 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2226 final TextView title = (TextView) target.findViewById(android.R.id.title);
2227
2228 if (detail != null) {
2229 icon.setImageDrawable(detail.icon);
2230 title.setText(detail.label);
2231 } else {
2232 icon.setImageDrawable(null);
2233 title.setText(null);
2234 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002235 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002236
2237 @Override
2238 protected void onPreExecute() {
2239 bindView(null, mTarget);
2240 }
2241
2242 @Override
2243 protected UidDetail doInBackground(Void... params) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002244 return mProvider.getUidDetail(mItem.key, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002245 }
2246
2247 @Override
2248 protected void onPostExecute(UidDetail result) {
2249 bindView(result, mTarget);
2250 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002251 }
2252
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002253 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002254 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002255 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002256 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002257 if (TEST_RADIOS) {
2258 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2259 }
2260
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002261 final ConnectivityManager conn = ConnectivityManager.from(context);
2262 final TelephonyManager tele = TelephonyManager.from(context);
2263
2264 // require both supported network and ready SIM
2265 return conn.isNetworkSupported(TYPE_MOBILE) && tele.getSimState() == SIM_STATE_READY;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002266 }
2267
2268 /**
2269 * Test if device has a mobile 4G data radio.
2270 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002271 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002272 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2273 return false;
2274 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002275 if (TEST_RADIOS) {
2276 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2277 }
2278
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002279 final ConnectivityManager conn = ConnectivityManager.from(context);
2280 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002281
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002282 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Wink Saville55434042012-06-14 12:33:43 -07002283 final boolean hasLte = (tele.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE)
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002284 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002285 return hasWimax || hasLte;
2286 }
2287
2288 /**
2289 * Test if device has a Wi-Fi data radio.
2290 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002291 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002292 if (TEST_RADIOS) {
2293 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2294 }
2295
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002296 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002297 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002298 }
2299
2300 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002301 * Test if device has an ethernet network connection.
2302 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002303 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002304 if (TEST_RADIOS) {
2305 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2306 }
2307
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002308 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002309 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2310
2311 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002312 if (mStatsSession != null) {
2313 try {
2314 ethernetBytes = mStatsSession.getSummaryForNetwork(
2315 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2316 .getTotalBytes();
2317 } catch (RemoteException e) {
2318 throw new RuntimeException(e);
2319 }
2320 } else {
2321 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002322 }
2323
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002324 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002325 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002326 }
2327
2328 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002329 * Inflate a {@link Preference} style layout, adding the given {@link View}
2330 * widget into {@link android.R.id#widget_frame}.
2331 */
2332 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2333 final View view = inflater.inflate(R.layout.preference, root, false);
2334 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2335 android.R.id.widget_frame);
2336 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2337 return view;
2338 }
2339
Jeff Sharkeyb6548462014-07-21 15:38:06 -07002340 private static View inflateCategoryHeader(LayoutInflater inflater, ViewGroup root) {
2341 final TypedArray a = inflater.getContext().obtainStyledAttributes(null,
2342 com.android.internal.R.styleable.Preference,
2343 com.android.internal.R.attr.preferenceCategoryStyle, 0);
2344 final int resId = a.getResourceId(com.android.internal.R.styleable.Preference_layout, 0);
2345 return inflater.inflate(resId, root, false);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002346 }
2347
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002348 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002349 * Test if any networks are currently limited.
2350 */
2351 private boolean hasLimitedNetworks() {
2352 return !buildLimitedNetworksList().isEmpty();
2353 }
2354
2355 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002356 * Build string describing currently limited networks, which defines when
2357 * background data is restricted.
2358 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002359 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002360 private CharSequence buildLimitedNetworksString() {
2361 final List<CharSequence> limited = buildLimitedNetworksList();
2362
2363 // handle case where no networks limited
2364 if (limited.isEmpty()) {
2365 limited.add(getText(R.string.data_usage_list_none));
2366 }
2367
2368 return TextUtils.join(limited);
2369 }
2370
2371 /**
2372 * Build list of currently limited networks, which defines when background
2373 * data is restricted.
2374 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002375 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002376 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002377 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002378
2379 // build combined list of all limited networks
2380 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002381
2382 final TelephonyManager tele = TelephonyManager.from(context);
2383 if (tele.getSimState() == SIM_STATE_READY) {
2384 final String subscriberId = getActiveSubscriberId(context);
2385 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2386 limited.add(getText(R.string.data_usage_list_mobile));
2387 }
2388 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2389 limited.add(getText(R.string.data_usage_tab_3g));
2390 }
2391 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2392 limited.add(getText(R.string.data_usage_tab_4g));
2393 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002394 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002395
2396 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002397 limited.add(getText(R.string.data_usage_tab_wifi));
2398 }
2399 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2400 limited.add(getText(R.string.data_usage_tab_ethernet));
2401 }
2402
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002403 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002404 }
2405
2406 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002407 * Inset both selector and divider {@link Drawable} on the given
2408 * {@link ListView} by the requested dimensions.
2409 */
2410 private static void insetListViewDrawables(ListView view, int insetSide) {
2411 final Drawable selector = view.getSelector();
2412 final Drawable divider = view.getDivider();
2413
2414 // fully unregister these drawables so callbacks can be maintained after
2415 // wrapping below.
2416 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2417 view.setSelector(stub);
2418 view.setDivider(stub);
2419
2420 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2421 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2422 }
2423
2424 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002425 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002426 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002427 */
2428 private static void setPreferenceTitle(View parent, int resId) {
2429 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2430 title.setText(resId);
2431 }
2432
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002433 /**
2434 * Set {@link android.R.id#summary} for a preference view inflated with
2435 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2436 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002437 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002438 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2439 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002440 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002441 }
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002442
2443 /**
2444 * For search
2445 */
2446 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -07002447 new BaseSearchIndexProvider() {
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002448 @Override
2449 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
2450 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
2451
2452 final Resources res = context.getResources();
2453
2454 // Add fragment title
2455 SearchIndexableRaw data = new SearchIndexableRaw(context);
2456 data.title = res.getString(R.string.data_usage_summary_title);
2457 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2458 result.add(data);
2459
2460 // Mobile data
2461 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002462 data.key = DATA_USAGE_ENABLE_MOBILE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002463 data.title = res.getString(R.string.data_usage_enable_mobile);
2464 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2465 result.add(data);
2466
2467 // Set mobile data limit
2468 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002469 data.key = DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002470 data.title = res.getString(R.string.data_usage_disable_mobile_limit);
2471 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2472 result.add(data);
2473
Fabrice Di Megliof2a52262014-04-17 17:20:27 -07002474 // Data usage cycle
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002475 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002476 data.key = DATA_USAGE_CYCLE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002477 data.title = res.getString(R.string.data_usage_cycle);
2478 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2479 result.add(data);
2480
2481 return result;
2482 }
2483 };
2484
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002485}