blob: fe09c6a68629ac2ca0839498cdce4fe7db6317df [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 Sharkey9fab0da2011-07-09 17:52:31 -070058import android.content.ContentResolver;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070059import android.content.Context;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070060import android.content.DialogInterface;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -070061import android.content.Intent;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070062import android.content.Loader;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070063import android.content.SharedPreferences;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070064import android.content.pm.PackageManager;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070065import android.content.res.Resources;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070066import android.graphics.Color;
Jeff Sharkey5d706792011-09-08 18:57:17 -070067import android.graphics.drawable.ColorDrawable;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070068import android.graphics.drawable.Drawable;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070069import android.net.ConnectivityManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070070import android.net.INetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070071import android.net.INetworkStatsService;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070072import android.net.INetworkStatsSession;
Jeff Sharkey8a503642011-06-10 13:31:21 -070073import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070074import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070075import android.net.NetworkStats;
76import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070077import android.net.NetworkTemplate;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070078import android.net.TrafficStats;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070079import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070080import android.os.Bundle;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070081import android.os.INetworkManagementService;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070082import android.os.Parcel;
83import android.os.Parcelable;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070084import android.os.RemoteException;
85import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070086import android.os.SystemProperties;
Dianne Hackbornbb06a422012-08-16 11:01:57 -070087import android.os.UserHandle;
Jeff Sharkey8a503642011-06-10 13:31:21 -070088import android.preference.Preference;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070089import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070090import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070091import android.text.format.DateUtils;
92import android.text.format.Formatter;
Jeff Sharkeye5223a02012-03-09 17:11:14 -080093import android.text.format.Time;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070094import android.util.Log;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070095import android.util.SparseArray;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070096import android.util.SparseBooleanArray;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070097import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -070098import android.view.Menu;
99import android.view.MenuInflater;
100import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700101import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700102import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700103import android.view.ViewGroup;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700104import android.widget.AdapterView;
105import android.widget.AdapterView.OnItemClickListener;
106import android.widget.AdapterView.OnItemSelectedListener;
107import android.widget.ArrayAdapter;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700108import android.widget.BaseAdapter;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700109import android.widget.Button;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700110import android.widget.CheckBox;
111import android.widget.CompoundButton;
112import android.widget.CompoundButton.OnCheckedChangeListener;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700113import android.widget.ImageView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700114import android.widget.LinearLayout;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700115import android.widget.ListView;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700116import android.widget.NumberPicker;
Jeff Sharkey2412b0f2011-07-17 20:31:40 -0700117import android.widget.ProgressBar;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700118import android.widget.Spinner;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700119import android.widget.Switch;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700120import android.widget.TabHost;
121import android.widget.TabHost.OnTabChangeListener;
122import android.widget.TabHost.TabContentFactory;
123import android.widget.TabHost.TabSpec;
124import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700125import android.widget.TextView;
126
Wink Saville55434042012-06-14 12:33:43 -0700127import com.android.internal.telephony.PhoneConstants;
Jeff Sharkey5d706792011-09-08 18:57:17 -0700128import com.android.settings.drawable.InsetBoundsDrawable;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700129import com.android.settings.net.ChartData;
130import com.android.settings.net.ChartDataLoader;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700131import com.android.settings.net.DataUsageMeteredSettings;
Jeff Sharkeya662e492011-06-18 21:57:06 -0700132import com.android.settings.net.NetworkPolicyEditor;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700133import com.android.settings.net.SummaryForAllUidLoader;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700134import com.android.settings.net.UidDetail;
135import com.android.settings.net.UidDetailProvider;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700136import com.android.settings.widget.ChartDataUsageView;
137import com.android.settings.widget.ChartDataUsageView.DataUsageChartListener;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700138import com.android.settings.widget.PieChartView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700139import com.google.android.collect.Lists;
140
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700141import libcore.util.Objects;
142
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700143import java.util.ArrayList;
144import java.util.Collections;
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700145import java.util.List;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700146import java.util.Locale;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700147
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700148/**
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700149 * Panel showing data usage history across various networks, including options
150 * to inspect based on usage cycle and control through {@link NetworkPolicy}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700151 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700152public class DataUsageSummary extends Fragment {
153 private static final String TAG = "DataUsage";
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800154 private static final boolean LOGD = false;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700155
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700156 // TODO: remove this testing code
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700157 private static final boolean TEST_ANIM = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700158 private static final boolean TEST_RADIOS = false;
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800159
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700160 private static final String TEST_RADIOS_PROP = "test.radios";
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800161 private static final String TEST_SUBSCRIBER_PROP = "test.subscriberid";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700162
Jeff Sharkey8a503642011-06-10 13:31:21 -0700163 private static final String TAB_3G = "3g";
164 private static final String TAB_4G = "4g";
165 private static final String TAB_MOBILE = "mobile";
166 private static final String TAB_WIFI = "wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700167 private static final String TAB_ETHERNET = "ethernet";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700168
Jeff Sharkey28130d92011-09-02 16:10:24 -0700169 private static final String TAG_CONFIRM_DATA_DISABLE = "confirmDataDisable";
170 private static final String TAG_CONFIRM_DATA_ROAMING = "confirmDataRoaming";
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
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700181 private static final int LOADER_CHART_DATA = 2;
182 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700183
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700184 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700185 private INetworkStatsService mStatsService;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700186 private NetworkPolicyManager mPolicyManager;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700187 private ConnectivityManager mConnService;
188
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700189 private INetworkStatsSession mStatsSession;
190
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700191 private static final String PREF_FILE = "data_usage";
192 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700193 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700194
195 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700196
Jeff Sharkey8a503642011-06-10 13:31:21 -0700197 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700198 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700199 private TabWidget mTabWidget;
200 private ListView mListView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700201 private DataUsageAdapter mAdapter;
202
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700203 /** Distance to inset content from sides, when needed. */
204 private int mInsetSide = 0;
205
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700206 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700207
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700208 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700209 private LinearLayout mNetworkSwitches;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700210 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700211 private View mDataEnabledView;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700212 private CheckBox mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700213 private View mDisableAtLimitView;
214
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700215 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700216 private Spinner mCycleSpinner;
217 private CycleAdapter mCycleAdapter;
218
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700219 private ChartDataUsageView mChart;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700220 private TextView mUsageSummary;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700221 private TextView mEmpty;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700222
223 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700224 private ImageView mAppIcon;
225 private ViewGroup mAppTitles;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700226 private PieChartView mAppPieChart;
227 private TextView mAppForeground;
228 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700229 private Button mAppSettings;
230
231 private LinearLayout mAppSwitches;
232 private CheckBox mAppRestrict;
233 private View mAppRestrictView;
234
Jeff Sharkey8a503642011-06-10 13:31:21 -0700235 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700236 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700237
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700238 private NetworkTemplate mTemplate;
239 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700240
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700241 private AppItem mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700242
243 private Intent mAppSettingsIntent;
244
Jeff Sharkeya662e492011-06-18 21:57:06 -0700245 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700246
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700247 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700248 private String mIntentTab = null;
249
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700250 private MenuItem mMenuDataRoaming;
251 private MenuItem mMenuRestrictBackground;
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700252 private MenuItem mMenuAutoSync;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700253
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700254 /** Flag used to ignore listeners during binding. */
255 private boolean mBinding;
256
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700257 private UidDetailProvider mUidDetailProvider;
258
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700259 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700260 public void onCreate(Bundle savedInstanceState) {
261 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700262 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700263
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700264 mNetworkService = INetworkManagementService.Stub.asInterface(
265 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700266 mStatsService = INetworkStatsService.Stub.asInterface(
267 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700268 mPolicyManager = NetworkPolicyManager.from(context);
269 mConnService = ConnectivityManager.from(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700270
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700271 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700272
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700273 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700274 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700275
Jaewan Kimffce9c12013-03-19 16:53:45 +0900276 try {
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700277 if (!mNetworkService.isBandwidthControlEnabled()) {
278 Log.w(TAG, "No bandwidth control; leaving");
279 getActivity().finish();
280 }
281 } catch (RemoteException e) {
282 Log.w(TAG, "No bandwidth control; leaving");
283 getActivity().finish();
284 }
285
286 try {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900287 mStatsSession = mStatsService.openSession();
288 } catch (RemoteException e) {
289 throw new RuntimeException(e);
290 }
291
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700292 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700293 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700294
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700295 // override preferences when no mobile radio
296 if (!hasReadyMobileRadio(context)) {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900297 mShowWifi = true;
298 mShowEthernet = true;
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700299 }
300
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700301 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700302 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700303
Jeff Sharkey8a503642011-06-10 13:31:21 -0700304 @Override
305 public View onCreateView(LayoutInflater inflater, ViewGroup container,
306 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700307
Jeff Sharkey8a503642011-06-10 13:31:21 -0700308 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700309 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
310
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700311 mUidDetailProvider = new UidDetailProvider(context);
312
Jeff Sharkey8a503642011-06-10 13:31:21 -0700313 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700314 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700315 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
316 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700317
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700318 // decide if we need to manually inset our content, or if we should rely
319 // on parent container for inset.
320 final boolean shouldInset = mListView.getScrollBarStyle()
321 == View.SCROLLBARS_OUTSIDE_OVERLAY;
Amith Yamasani56f51a82013-08-05 10:07:23 -0700322 mInsetSide = 0;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700323
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700324 // adjust padding around tabwidget as needed
Amith Yamasani56f51a82013-08-05 10:07:23 -0700325 prepareCustomPreferencesList(container, view, mListView, false);
Jeff Sharkey5d706792011-09-08 18:57:17 -0700326
Jeff Sharkey8a503642011-06-10 13:31:21 -0700327 mTabHost.setup();
328 mTabHost.setOnTabChangedListener(mTabListener);
329
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700330 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700331 mHeader.setClickable(true);
332
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700333 mListView.addHeaderView(new View(context), null, true);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700334 mListView.addHeaderView(mHeader, null, true);
335 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700336
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700337 if (mInsetSide > 0) {
338 // inset selector and divider drawables
339 insetListViewDrawables(mListView, mInsetSide);
Fabrice Di Megliob27223f2013-01-15 18:54:11 -0800340 mHeader.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700341 }
342
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700343 {
344 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700345 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
346 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700347 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700348
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700349 mDataEnabled = new Switch(inflater.getContext());
350 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
351 mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener);
352 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700353
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700354 mDisableAtLimit = new CheckBox(inflater.getContext());
355 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700356 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700357 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700358 mDisableAtLimitView.setClickable(true);
359 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700360 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
361 mNetworkSwitches.addView(mDisableAtLimitView);
362 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700363
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700364 // bind cycle dropdown
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700365 mCycleView = mHeader.findViewById(R.id.cycles);
366 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700367 mCycleAdapter = new CycleAdapter(context);
368 mCycleSpinner.setAdapter(mCycleAdapter);
369 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
370
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700371 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700372 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800373 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700374
375 {
376 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700377 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700378 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
379 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700380 mAppPieChart = (PieChartView) mAppDetail.findViewById(R.id.app_pie_chart);
381 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
382 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700383 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700384
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700385 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700386 mAppSettings.setOnClickListener(mAppSettingsListener);
387
388 mAppRestrict = new CheckBox(inflater.getContext());
389 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700390 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700391 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700392 mAppRestrictView.setClickable(true);
393 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700394 mAppRestrictView.setOnClickListener(mAppRestrictListener);
395 mAppSwitches.addView(mAppRestrictView);
396 }
397
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700398 mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700399 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700400
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700401 mAdapter = new DataUsageAdapter(mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700402 mListView.setOnItemClickListener(mListListener);
403 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700404
405 return view;
406 }
407
408 @Override
409 public void onResume() {
410 super.onResume();
411
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700412 // pick default tab based on incoming intent
413 final Intent intent = getActivity().getIntent();
414 mIntentTab = computeTabFromIntent(intent);
415
Jeff Sharkey8a503642011-06-10 13:31:21 -0700416 // this kicks off chain reaction which creates tabs, binds the body to
417 // selected network, and binds chart, cycles and detail list.
418 updateTabs();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700419
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700420 // kick off background task to update stats
421 new AsyncTask<Void, Void, Void>() {
422 @Override
423 protected Void doInBackground(Void... params) {
424 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700425 // wait a few seconds before kicking off
426 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700427 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700428 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700429 } catch (RemoteException e) {
430 }
431 return null;
432 }
433
434 @Override
435 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700436 if (isAdded()) {
437 updateBody();
438 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700439 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700440 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700441 }
442
Jeff Sharkey8a503642011-06-10 13:31:21 -0700443 @Override
444 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
445 inflater.inflate(R.menu.data_usage, menu);
446 }
447
448 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700449 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700450 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700451 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700452 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700453
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700454 mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700455 mMenuDataRoaming.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700456 mMenuDataRoaming.setChecked(getDataRoaming());
457
458 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700459 mMenuRestrictBackground.setVisible(
460 hasReadyMobileRadio(context) && isOwner && !appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700461 mMenuRestrictBackground.setChecked(mPolicyManager.getRestrictBackground());
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700462
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700463 mMenuAutoSync = menu.findItem(R.id.data_usage_menu_auto_sync);
464 mMenuAutoSync.setChecked(ContentResolver.getMasterSyncAutomatically());
Jeff Sharkeyfda48e32012-09-18 15:03:47 -0700465 mMenuAutoSync.setVisible(!appDetailMode);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700466
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700467 final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g);
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700468 split4g.setVisible(hasReadyMobile4gRadio(context) && isOwner && !appDetailMode);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700469 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700470
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700471 final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700472 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700473 showWifi.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700474 showWifi.setChecked(mShowWifi);
475 } else {
476 showWifi.setVisible(false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700477 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700478
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700479 final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700480 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700481 showEthernet.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700482 showEthernet.setChecked(mShowEthernet);
483 } else {
484 showEthernet.setVisible(false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700485 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700486
487 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
488 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700489 metered.setVisible(!appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700490 } else {
491 metered.setVisible(false);
492 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700493
494 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
495 String helpUrl;
496 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800497 HelpUtils.prepareHelpMenuItem(context, help, helpUrl);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700498 } else {
499 help.setVisible(false);
500 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700501 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700502
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700503 @Override
504 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700505 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700506 case R.id.data_usage_menu_roaming: {
507 final boolean dataRoaming = !item.isChecked();
508 if (dataRoaming) {
509 ConfirmDataRoamingFragment.show(this);
510 } else {
511 // no confirmation to disable roaming
512 setDataRoaming(false);
513 }
514 return true;
515 }
516 case R.id.data_usage_menu_restrict_background: {
517 final boolean restrictBackground = !item.isChecked();
518 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800519 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700520 } else {
521 // no confirmation to drop restriction
522 setRestrictBackground(false);
523 }
524 return true;
525 }
526 case R.id.data_usage_menu_split_4g: {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700527 final boolean mobileSplit = !item.isChecked();
Jeff Sharkeya662e492011-06-18 21:57:06 -0700528 setMobilePolicySplit(mobileSplit);
529 item.setChecked(isMobilePolicySplit());
Jeff Sharkey8a503642011-06-10 13:31:21 -0700530 updateTabs();
531 return true;
532 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700533 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700534 mShowWifi = !item.isChecked();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700535 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700536 item.setChecked(mShowWifi);
537 updateTabs();
538 return true;
539 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700540 case R.id.data_usage_menu_show_ethernet: {
541 mShowEthernet = !item.isChecked();
542 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
543 item.setChecked(mShowEthernet);
544 updateTabs();
545 return true;
546 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700547 case R.id.data_usage_menu_metered: {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800548 final SettingsActivity sa = (SettingsActivity) getActivity();
549 sa.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700550 R.string.data_usage_metered_title, null, this, 0);
551 return true;
552 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700553 case R.id.data_usage_menu_auto_sync: {
Guang Zhu167ba2a2012-10-19 17:55:05 -0700554 if (ActivityManager.isUserAMonkey()) {
555 Log.d("SyncState", "ignoring monkey's attempt to flip global sync state");
556 } else {
557 ConfirmAutoSyncChangeFragment.show(this, !item.isChecked());
558 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700559 return true;
560 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700561 }
562 return false;
563 }
564
Jeff Sharkey94a90952011-06-13 22:31:09 -0700565 @Override
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700566 public void onDestroy() {
Jeff Sharkey94a90952011-06-13 22:31:09 -0700567 mDataEnabledView = null;
568 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700569
570 mUidDetailProvider.clearCache();
571 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700572
573 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700574
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800575 super.onDestroy();
576 }
577
Jeff Sharkey8a503642011-06-10 13:31:21 -0700578 /**
Jeff Sharkey92811822012-05-04 11:47:29 -0700579 * Build and assign {@link LayoutTransition} to various containers. Should
580 * only be assigned after initial layout is complete.
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700581 */
Jeff Sharkey92811822012-05-04 11:47:29 -0700582 private void ensureLayoutTransitions() {
583 // skip when already setup
584 if (mChart.getLayoutTransition() != null) return;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700585
Jeff Sharkey92811822012-05-04 11:47:29 -0700586 mTabsContainer.setLayoutTransition(buildLayoutTransition());
587 mHeader.setLayoutTransition(buildLayoutTransition());
588 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700589
Jeff Sharkey92811822012-05-04 11:47:29 -0700590 final LayoutTransition chartTransition = buildLayoutTransition();
591 chartTransition.disableTransitionType(LayoutTransition.APPEARING);
592 chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
593 mChart.setLayoutTransition(chartTransition);
594 }
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700595
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700596 private static LayoutTransition buildLayoutTransition() {
597 final LayoutTransition transition = new LayoutTransition();
598 if (TEST_ANIM) {
599 transition.setDuration(1500);
600 }
601 transition.setAnimateParentHierarchy(false);
602 return transition;
603 }
604
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700605 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700606 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700607 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
608 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700609 */
610 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700611 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700612 mTabHost.clearAllTabs();
613
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700614 final boolean mobileSplit = isMobilePolicySplit();
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700615 if (mobileSplit && hasReadyMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700616 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
617 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700618 } else if (hasReadyMobileRadio(context)) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700619 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700620 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700621 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700622 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
623 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700624 if (mShowEthernet && hasEthernet(context)) {
625 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
626 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700627
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700628 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700629 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
630 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
631 if (mIntentTab != null) {
632 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700633 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700634 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700635 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700636 mTabHost.setCurrentTabByTag(mIntentTab);
637 }
638 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700639 } else if (noTabs) {
640 // no usable tabs, so hide body
641 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700642 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700643 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700644 }
645 }
646
Jeff Sharkey8a503642011-06-10 13:31:21 -0700647 /**
648 * Factory that provide empty {@link View} to make {@link TabHost} happy.
649 */
650 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700651 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700652 public View createTabContent(String tag) {
653 return new View(mTabHost.getContext());
654 }
655 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700656
Jeff Sharkey8a503642011-06-10 13:31:21 -0700657 /**
658 * Build {@link TabSpec} with thin indicator, and empty content.
659 */
660 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700661 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
662 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700663 }
664
665 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700666 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700667 public void onTabChanged(String tabId) {
668 // user changed tab; update body
669 updateBody();
670 }
671 };
672
673 /**
674 * Update body content based on current tab. Loads
675 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
676 * binds them to visible controls.
677 */
678 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700679 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700680 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700681
Jeff Sharkeya662e492011-06-18 21:57:06 -0700682 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700683 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700684 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700685
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700686 if (currentTab == null) {
687 Log.w(TAG, "no tab selected; hiding body");
688 mListView.setVisibility(View.GONE);
689 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700690 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700691 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700692 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700693
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700694 mCurrentTab = currentTab;
695
Jeff Sharkey8a503642011-06-10 13:31:21 -0700696 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
697
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700698 mDataEnabledView.setVisibility(isOwner ? View.VISIBLE : View.GONE);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700699
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700700 // TODO: remove mobile tabs when SIM isn't ready
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700701
Jeff Sharkey8a503642011-06-10 13:31:21 -0700702 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700703 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
704 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700705 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700706
707 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700708 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
709 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
710 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700711 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700712
713 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700714 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
715 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
716 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700717 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700718
719 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700720 // wifi doesn't have any controls
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700721 mDataEnabledView.setVisibility(View.GONE);
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700722 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700723 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700724
725 } else if (TAB_ETHERNET.equals(currentTab)) {
726 // ethernet doesn't have any controls
727 mDataEnabledView.setVisibility(View.GONE);
728 mDisableAtLimitView.setVisibility(View.GONE);
729 mTemplate = buildTemplateEthernet();
730
731 } else {
732 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700733 }
734
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700735 // kick off loader for network history
736 // TODO: consider chaining two loaders together instead of reloading
737 // network history when showing app detail.
738 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700739 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700740
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700741 // detail mode can change visible menus, invalidate
742 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700743
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700744 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700745 }
746
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700747 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700748 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700749 }
750
751 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700752 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700753 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700754 */
755 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700756 final Context context = getActivity();
757 final PackageManager pm = context.getPackageManager();
758 final LayoutInflater inflater = getActivity().getLayoutInflater();
759
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700760 if (isAppDetailMode()) {
761 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700762 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700763 } else {
764 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700765 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700766
767 // hide detail stats when not in detail mode
768 mChart.bindDetailNetworkStats(null);
769 return;
770 }
771
772 // remove warning/limit sweeps while in detail mode
773 mChart.bindNetworkPolicy(null);
774
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700775 // show icon and all labels appearing under this app
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700776 final int uid = mCurrentApp.key;
777 final UidDetail detail = mUidDetailProvider.getUidDetail(uid, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700778 mAppIcon.setImageDrawable(detail.icon);
779
780 mAppTitles.removeAllViews();
781 if (detail.detailLabels != null) {
782 for (CharSequence label : detail.detailLabels) {
783 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label));
784 }
785 } else {
786 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label));
787 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700788
789 // enable settings button when package provides it
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700790 final String[] packageNames = pm.getPackagesForUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700791 if (packageNames != null && packageNames.length > 0) {
792 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700793 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
794
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700795 // Search for match across all packages
796 boolean matchFound = false;
797 for (String packageName : packageNames) {
798 mAppSettingsIntent.setPackage(packageName);
799 if (pm.resolveActivity(mAppSettingsIntent, 0) != null) {
800 matchFound = true;
801 break;
802 }
803 }
804
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700805 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -0700806 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700807
808 } else {
809 mAppSettingsIntent = null;
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700810 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700811 }
812
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700813 updateDetailData();
814
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700815 if (UserHandle.isApp(uid) && !mPolicyManager.getRestrictBackground()
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700816 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700817 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800818 setPreferenceSummary(mAppRestrictView,
819 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700820
821 mAppRestrictView.setVisibility(View.VISIBLE);
822 mAppRestrict.setChecked(getAppRestrictBackground());
823
824 } else {
825 mAppRestrictView.setVisibility(View.GONE);
826 }
827 }
828
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700829 private void setPolicyWarningBytes(long warningBytes) {
830 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700831 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700832 updatePolicy(false);
833 }
834
835 private void setPolicyLimitBytes(long limitBytes) {
836 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700837 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700838 updatePolicy(false);
839 }
840
Jeff Sharkey28130d92011-09-02 16:10:24 -0700841 /**
842 * Local cache of value, used to work around delay when
843 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
844 */
845 private Boolean mMobileDataEnabled;
846
847 private boolean isMobileDataEnabled() {
848 if (mMobileDataEnabled != null) {
849 // TODO: deprecate and remove this once enabled flag is on policy
850 return mMobileDataEnabled;
851 } else {
852 return mConnService.getMobileDataEnabled();
853 }
854 }
855
856 private void setMobileDataEnabled(boolean enabled) {
857 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
858 mConnService.setMobileDataEnabled(enabled);
859 mMobileDataEnabled = enabled;
860 updatePolicy(false);
861 }
862
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700863 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700864 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked()
865 && ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700866 }
867
868 private boolean isBandwidthControlEnabled() {
869 try {
870 return mNetworkService.isBandwidthControlEnabled();
871 } catch (RemoteException e) {
872 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
873 return false;
874 }
875 }
876
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700877 private boolean getDataRoaming() {
878 final ContentResolver resolver = getActivity().getContentResolver();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800879 return android.provider.Settings.Global.getInt(resolver,
880 android.provider.Settings.Global.DATA_ROAMING, 0) != 0;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700881 }
882
883 private void setDataRoaming(boolean enabled) {
884 // TODO: teach telephony DataConnectionTracker to watch and apply
885 // updates when changed.
886 final ContentResolver resolver = getActivity().getContentResolver();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800887 android.provider.Settings.Global.putInt(resolver,
888 android.provider.Settings.Global.DATA_ROAMING, enabled ? 1 : 0);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700889 mMenuDataRoaming.setChecked(enabled);
890 }
891
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700892 public void setRestrictBackground(boolean restrictBackground) {
893 mPolicyManager.setRestrictBackground(restrictBackground);
894 mMenuRestrictBackground.setChecked(restrictBackground);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700895 }
896
897 private boolean getAppRestrictBackground() {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700898 final int uid = mCurrentApp.key;
899 final int uidPolicy = mPolicyManager.getUidPolicy(uid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700900 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
901 }
902
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700903 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700904 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700905 final int uid = mCurrentApp.key;
906 mPolicyManager.setUidPolicy(
907 uid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700908 mAppRestrict.setChecked(restrictBackground);
909 }
910
Jeff Sharkey8a503642011-06-10 13:31:21 -0700911 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700912 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
913 * current {@link #mTemplate}.
914 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700915 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700916 if (isAppDetailMode()) {
917 mNetworkSwitches.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700918 } else {
919 mNetworkSwitches.setVisibility(View.VISIBLE);
920 }
921
Jeff Sharkey28130d92011-09-02 16:10:24 -0700922 // TODO: move enabled state directly into policy
923 if (TAB_MOBILE.equals(mCurrentTab)) {
924 mBinding = true;
925 mDataEnabled.setChecked(isMobileDataEnabled());
926 mBinding = false;
927 }
928
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700929 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
930 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700931 mDisableAtLimitView.setVisibility(View.VISIBLE);
932 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700933 if (!isAppDetailMode()) {
934 mChart.bindNetworkPolicy(policy);
935 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700936
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700937 } else {
938 // controls are disabled; don't bind warning/limit sweeps
939 mDisableAtLimitView.setVisibility(View.GONE);
940 mChart.bindNetworkPolicy(null);
941 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700942
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700943 if (refreshCycle) {
944 // generate cycle list based on policy and available history
945 updateCycleList(policy);
946 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700947 }
948
949 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700950 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
951 * and available {@link NetworkStatsHistory} data. Always selects the newest
952 * item, updating the inspection range on {@link #mChart}.
953 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700954 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700955 // stash away currently selected cycle to try restoring below
956 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700957 mCycleAdapter.clear();
958
959 final Context context = mCycleSpinner.getContext();
960
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700961 long historyStart = Long.MAX_VALUE;
962 long historyEnd = Long.MIN_VALUE;
963 if (mChartData != null) {
964 historyStart = mChartData.network.getStart();
965 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700966 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700967
Jeff Sharkey461842a2011-09-25 18:22:48 -0700968 final long now = System.currentTimeMillis();
969 if (historyStart == Long.MAX_VALUE) historyStart = now;
970 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700971
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700972 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700973 if (policy != null) {
974 // find the next cycle boundary
975 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700976
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700977 // walk backwards, generating all valid cycle ranges
978 while (cycleEnd > historyStart) {
979 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
980 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
981 + historyStart);
982 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
983 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700984 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700985 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700986
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700987 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700988 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700989 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700990
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700991 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700992 // no policy defined cycles; show entry for each four-week period
993 long cycleEnd = historyEnd;
994 while (cycleEnd > historyStart) {
995 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
996 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
997 cycleEnd = cycleStart;
998 }
999
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001000 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001001 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001002
1003 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001004 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001005 final int position = mCycleAdapter.findNearestPosition(previousItem);
1006 mCycleSpinner.setSelection(position);
1007
1008 // only force-update cycle when changed; skipping preserves any
1009 // user-defined inspection region.
1010 final CycleItem selectedItem = mCycleAdapter.getItem(position);
1011 if (!Objects.equal(selectedItem, previousItem)) {
1012 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1013 } else {
1014 // but still kick off loader for detailed list
1015 updateDetailData();
1016 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001017 } else {
1018 updateDetailData();
1019 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001020 }
1021
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001022 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001023 @Override
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001024 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
1025 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001026
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001027 final boolean dataEnabled = isChecked;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001028 final String currentTab = mCurrentTab;
1029 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001030 if (dataEnabled) {
1031 setMobileDataEnabled(true);
1032 } else {
1033 // disabling data; show confirmation dialog which eventually
1034 // calls setMobileDataEnabled() once user confirms.
1035 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1036 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001037 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001038
Jeff Sharkey28130d92011-09-02 16:10:24 -07001039 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001040 }
1041 };
1042
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001043 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001044 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001045 public void onClick(View v) {
1046 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001047 if (disableAtLimit) {
1048 // enabling limit; show confirmation dialog which eventually
1049 // calls setPolicyLimitBytes() once user confirms.
1050 ConfirmLimitFragment.show(DataUsageSummary.this);
1051 } else {
1052 setPolicyLimitBytes(LIMIT_DISABLED);
1053 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001054 }
1055 };
1056
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001057 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001058 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001059 public void onClick(View v) {
1060 final boolean restrictBackground = !mAppRestrict.isChecked();
1061
1062 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001063 // enabling restriction; show confirmation dialog which
1064 // eventually calls setRestrictBackground() once user
1065 // confirms.
1066 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001067 } else {
1068 setAppRestrictBackground(false);
1069 }
1070 }
1071 };
1072
1073 private OnClickListener mAppSettingsListener = new OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001074 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001075 public void onClick(View v) {
Jeff Sharkeyd92e0412012-04-24 11:35:43 -07001076 if (!isAdded()) return;
1077
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001078 // TODO: target torwards entire UID instead of just first package
1079 startActivity(mAppSettingsIntent);
1080 }
1081 };
1082
Jeff Sharkey8a503642011-06-10 13:31:21 -07001083 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001084 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001085 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001086 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001087 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001088
1089 // TODO: sigh, remove this hack once we understand 6450986
1090 if (mUidDetailProvider == null || app == null) return;
1091
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001092 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001093 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001094 }
1095 };
1096
1097 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001098 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001099 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1100 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1101 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001102 // show cycle editor; will eventually call setPolicyCycleDay()
1103 // when user finishes editing.
1104 CycleEditorFragment.show(DataUsageSummary.this);
1105
1106 // reset spinner to something other than "change cycle..."
1107 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001108
1109 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001110 if (LOGD) {
1111 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1112 + cycle.end + "]");
1113 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001114
1115 // update chart to show selected cycle, and update detail data
1116 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001117 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001118
1119 updateDetailData();
1120 }
1121 }
1122
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001123 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001124 public void onNothingSelected(AdapterView<?> parent) {
1125 // ignored
1126 }
1127 };
1128
1129 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001130 * Update details based on {@link #mChart} inspection range depending on
1131 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1132 * of applications data usage, and when {@link #isAppDetailMode()} update
1133 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001134 */
1135 private void updateDetailData() {
1136 if (LOGD) Log.d(TAG, "updateDetailData()");
1137
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001138 final long start = mChart.getInspectStart();
1139 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001140 final long now = System.currentTimeMillis();
1141
1142 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001143
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001144 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001145 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001146 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001147 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001148 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001149 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001150 final long foregroundBytes = entry.rxBytes + entry.txBytes;
1151
1152 mAppPieChart.setOriginAngle(175);
1153
1154 mAppPieChart.removeAllSlices();
1155 mAppPieChart.addSlice(foregroundBytes, Color.parseColor("#d88d3a"));
1156 mAppPieChart.addSlice(defaultBytes, Color.parseColor("#666666"));
1157
1158 mAppPieChart.generatePath();
1159
1160 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1161 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1162
1163 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001164 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001165
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001166 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001167
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001168 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001169 if (mChartData != null) {
1170 entry = mChartData.network.getValues(start, end, now, null);
1171 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001172
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001173 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001174 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001175 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001176 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001177
1178 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1179 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001180 final String rangePhrase = formatDateRange(context, start, end);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001181
Jeff Sharkeye557c332012-04-13 16:04:07 -07001182 final int summaryRes;
László Dávid0b8bf4e2012-10-24 23:31:47 +02001183 if (TAB_MOBILE.equals(mCurrentTab) || TAB_3G.equals(mCurrentTab)
1184 || TAB_4G.equals(mCurrentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001185 summaryRes = R.string.data_usage_total_during_range_mobile;
1186 } else {
1187 summaryRes = R.string.data_usage_total_during_range;
1188 }
1189
1190 mUsageSummary.setText(getString(summaryRes, totalPhrase, rangePhrase));
Jeff Sharkey92811822012-05-04 11:47:29 -07001191
1192 // initial layout is finished above, ensure we have transitions
1193 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001194 }
1195
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001196 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1197 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001198 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001199 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001200 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001201 }
1202
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001203 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001204 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1205 mChartData = data;
1206 mChart.bindNetworkStats(mChartData.network);
1207 mChart.bindDetailNetworkStats(mChartData.detail);
1208
1209 // calcuate policy cycles based on available data
1210 updatePolicy(true);
1211 updateAppDetail();
1212
1213 // force scroll to top of body when showing detail
1214 if (mChartData.detail != null) {
1215 mListView.smoothScrollToPosition(0);
1216 }
1217 }
1218
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001219 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001220 public void onLoaderReset(Loader<ChartData> loader) {
1221 mChartData = null;
1222 mChart.bindNetworkStats(null);
1223 mChart.bindDetailNetworkStats(null);
1224 }
1225 };
1226
1227 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001228 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001229 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001230 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001231 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001232 }
1233
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001234 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001235 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001236 final int[] restrictedUids = mPolicyManager.getUidsWithPolicy(
Jeff Sharkeye557c332012-04-13 16:04:07 -07001237 POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001238 mAdapter.bindStats(data, restrictedUids);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001239 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001240 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001241
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001242 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001243 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001244 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001245 updateEmptyVisible();
1246 }
1247
1248 private void updateEmptyVisible() {
1249 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1250 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001251 }
1252 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001253
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001254 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001255 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001256 final Context context = getActivity();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001257 if (hasReadyMobileRadio(context)) {
1258 final TelephonyManager tele = TelephonyManager.from(context);
1259 return mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001260 } else {
1261 return false;
1262 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001263 }
1264
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001265 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001266 private void setMobilePolicySplit(boolean split) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001267 final Context context = getActivity();
1268 if (hasReadyMobileRadio(context)) {
1269 final TelephonyManager tele = TelephonyManager.from(context);
1270 mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split);
1271 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001272 }
1273
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001274 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001275 final TelephonyManager tele = TelephonyManager.from(context);
1276 final String actualSubscriberId = tele.getSubscriberId();
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -08001277 return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001278 }
1279
Jeff Sharkey8a503642011-06-10 13:31:21 -07001280 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001281 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001282 public void onInspectRangeChanged() {
1283 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
1284 updateDetailData();
1285 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001286
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001287 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001288 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001289 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001290 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001291
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001292 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001293 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001294 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001295 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001296
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001297 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001298 public void requestWarningEdit() {
1299 WarningEditorFragment.show(DataUsageSummary.this);
1300 }
1301
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001302 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001303 public void requestLimitEdit() {
1304 LimitEditorFragment.show(DataUsageSummary.this);
1305 }
1306 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001307
1308 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001309 * List item that reflects a specific data usage cycle.
1310 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001311 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001312 public CharSequence label;
1313 public long start;
1314 public long end;
1315
Jeff Sharkey8a503642011-06-10 13:31:21 -07001316 CycleItem(CharSequence label) {
1317 this.label = label;
1318 }
1319
1320 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001321 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001322 this.start = start;
1323 this.end = end;
1324 }
1325
Jeff Sharkey8a503642011-06-10 13:31:21 -07001326 @Override
1327 public String toString() {
1328 return label.toString();
1329 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001330
1331 @Override
1332 public boolean equals(Object o) {
1333 if (o instanceof CycleItem) {
1334 final CycleItem another = (CycleItem) o;
1335 return start == another.start && end == another.end;
1336 }
1337 return false;
1338 }
1339
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001340 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001341 public int compareTo(CycleItem another) {
1342 return Long.compare(start, another.start);
1343 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001344 }
1345
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001346 private static final StringBuilder sBuilder = new StringBuilder(50);
1347 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1348 sBuilder, Locale.getDefault());
1349
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001350 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001351 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001352
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001353 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001354 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001355 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1356 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001357 }
1358 }
1359
Jeff Sharkey8a503642011-06-10 13:31:21 -07001360 /**
1361 * Special-case data usage cycle that triggers dialog to change
1362 * {@link NetworkPolicy#cycleDay}.
1363 */
1364 public static class CycleChangeItem extends CycleItem {
1365 public CycleChangeItem(Context context) {
1366 super(context.getString(R.string.data_usage_change_cycle));
1367 }
1368 }
1369
1370 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001371 private boolean mChangePossible = false;
1372 private boolean mChangeVisible = false;
1373
1374 private final CycleChangeItem mChangeItem;
1375
Jeff Sharkey8a503642011-06-10 13:31:21 -07001376 public CycleAdapter(Context context) {
1377 super(context, android.R.layout.simple_spinner_item);
1378 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001379 mChangeItem = new CycleChangeItem(context);
1380 }
1381
1382 public void setChangePossible(boolean possible) {
1383 mChangePossible = possible;
1384 updateChange();
1385 }
1386
1387 public void setChangeVisible(boolean visible) {
1388 mChangeVisible = visible;
1389 updateChange();
1390 }
1391
1392 private void updateChange() {
1393 remove(mChangeItem);
1394 if (mChangePossible && mChangeVisible) {
1395 add(mChangeItem);
1396 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001397 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001398
1399 /**
1400 * Find position of {@link CycleItem} in this adapter which is nearest
1401 * the given {@link CycleItem}.
1402 */
1403 public int findNearestPosition(CycleItem target) {
1404 if (target != null) {
1405 final int count = getCount();
1406 for (int i = count - 1; i >= 0; i--) {
1407 final CycleItem item = getItem(i);
1408 if (item instanceof CycleChangeItem) {
1409 continue;
1410 } else if (item.compareTo(target) >= 0) {
1411 return i;
1412 }
1413 }
1414 }
1415 return 0;
1416 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001417 }
1418
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001419 public static class AppItem implements Comparable<AppItem>, Parcelable {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001420 public final int key;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001421 public boolean restricted;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001422 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001423 public long total;
1424
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001425 public AppItem(int key) {
1426 this.key = key;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001427 }
1428
1429 public AppItem(Parcel parcel) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001430 key = parcel.readInt();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001431 uids = parcel.readSparseBooleanArray();
1432 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001433 }
1434
1435 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001436 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001437 }
1438
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001439 @Override
1440 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001441 dest.writeInt(key);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001442 dest.writeSparseBooleanArray(uids);
1443 dest.writeLong(total);
1444 }
1445
1446 @Override
1447 public int describeContents() {
1448 return 0;
1449 }
1450
1451 @Override
1452 public int compareTo(AppItem another) {
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001453 return Long.compare(another.total, total);
1454 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001455
1456 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1457 @Override
1458 public AppItem createFromParcel(Parcel in) {
1459 return new AppItem(in);
1460 }
1461
1462 @Override
1463 public AppItem[] newArray(int size) {
1464 return new AppItem[size];
1465 }
1466 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001467 }
1468
Jeff Sharkey8a503642011-06-10 13:31:21 -07001469 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001470 * Adapter of applications, sorted by total usage descending.
1471 */
1472 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001473 private final UidDetailProvider mProvider;
1474 private final int mInsetSide;
1475
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001476 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001477 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001478
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001479 public DataUsageAdapter(UidDetailProvider provider, int insetSide) {
1480 mProvider = checkNotNull(provider);
1481 mInsetSide = insetSide;
1482 }
1483
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001484 /**
1485 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1486 */
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001487 public void bindStats(NetworkStats stats, int[] restrictedUids) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001488 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001489
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001490 final int currentUserId = ActivityManager.getCurrentUser();
1491 final SparseArray<AppItem> knownItems = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001492
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001493 NetworkStats.Entry entry = null;
1494 final int size = stats != null ? stats.size() : 0;
1495 for (int i = 0; i < size; i++) {
1496 entry = stats.getValues(i, entry);
1497
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001498 // Decide how to collapse items together
1499 final int uid = entry.uid;
1500 final int collapseKey;
1501 if (UserHandle.isApp(uid)) {
1502 if (UserHandle.getUserId(uid) == currentUserId) {
1503 collapseKey = uid;
1504 } else {
1505 collapseKey = UidDetailProvider.buildKeyForUser(UserHandle.getUserId(uid));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001506 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001507 } else if (uid == UID_REMOVED || uid == UID_TETHERING) {
1508 collapseKey = uid;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001509 } else {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001510 collapseKey = android.os.Process.SYSTEM_UID;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001511 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001512
1513 AppItem item = knownItems.get(collapseKey);
1514 if (item == null) {
1515 item = new AppItem(collapseKey);
1516 mItems.add(item);
1517 knownItems.put(item.key, item);
1518 }
1519 item.addUid(uid);
1520 item.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001521 }
1522
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001523 for (int uid : restrictedUids) {
1524 // Only splice in restricted state for current user
1525 if (UserHandle.getUserId(uid) != currentUserId) continue;
1526
1527 AppItem item = knownItems.get(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001528 if (item == null) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001529 item = new AppItem(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001530 item.total = -1;
1531 mItems.add(item);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001532 knownItems.put(item.key, item);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001533 }
1534 item.restricted = true;
1535 }
1536
Jeff Sharkey8a503642011-06-10 13:31:21 -07001537 Collections.sort(mItems);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001538 mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001539 notifyDataSetChanged();
1540 }
1541
1542 @Override
1543 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001544 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001545 }
1546
1547 @Override
1548 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001549 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001550 }
1551
1552 @Override
1553 public long getItemId(int position) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001554 return mItems.get(position).key;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001555 }
1556
1557 @Override
1558 public View getView(int position, View convertView, ViewGroup parent) {
1559 if (convertView == null) {
1560 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey5d706792011-09-08 18:57:17 -07001561 R.layout.data_usage_item, parent, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001562
1563 if (mInsetSide > 0) {
Fabrice Di Megliob27223f2013-01-15 18:54:11 -08001564 convertView.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001565 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001566 }
1567
1568 final Context context = parent.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001569
Jeff Sharkey28130d92011-09-02 16:10:24 -07001570 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001571 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1572 android.R.id.progress);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001573
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001574 // kick off async load of app details
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001575 final AppItem item = mItems.get(position);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001576 UidDetailTask.bindView(mProvider, item, convertView);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001577
Jeff Sharkeye557c332012-04-13 16:04:07 -07001578 if (item.restricted && item.total <= 0) {
1579 text1.setText(R.string.data_usage_app_restricted);
1580 progress.setVisibility(View.GONE);
1581 } else {
1582 text1.setText(Formatter.formatFileSize(context, item.total));
1583 progress.setVisibility(View.VISIBLE);
1584 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001585
1586 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1587 progress.setProgress(percentTotal);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001588
1589 return convertView;
1590 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001591 }
1592
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001593 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001594 * Empty {@link Fragment} that controls display of UID details in
1595 * {@link DataUsageSummary}.
1596 */
1597 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001598 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001599
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001600 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001601 if (!parent.isAdded()) return;
1602
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001603 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001604 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001605
1606 final AppDetailsFragment fragment = new AppDetailsFragment();
1607 fragment.setArguments(args);
1608 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001609 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1610 ft.add(fragment, TAG_APP_DETAILS);
1611 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001612 ft.setBreadCrumbTitle(label);
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001613 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001614 }
1615
1616 @Override
1617 public void onStart() {
1618 super.onStart();
1619 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001620 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001621 target.updateBody();
1622 }
1623
1624 @Override
1625 public void onStop() {
1626 super.onStop();
1627 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001628 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001629 target.updateBody();
1630 }
1631 }
1632
1633 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001634 * Dialog to request user confirmation before setting
1635 * {@link NetworkPolicy#limitBytes}.
1636 */
1637 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001638 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001639 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001640
1641 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001642 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001643
Jeff Sharkey461842a2011-09-25 18:22:48 -07001644 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001645 final CharSequence message;
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001646 final long minLimitBytes = (long) (
1647 parent.mPolicyEditor.getPolicy(parent.mTemplate).warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001648 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001649
1650 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001651 final String currentTab = parent.mCurrentTab;
1652 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001653 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001654 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001655 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001656 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001657 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001658 } else if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001659 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001660 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001661 } else {
1662 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001663 }
1664
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001665 final Bundle args = new Bundle();
1666 args.putCharSequence(EXTRA_MESSAGE, message);
1667 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1668
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001669 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1670 dialog.setArguments(args);
1671 dialog.setTargetFragment(parent, 0);
1672 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1673 }
1674
1675 @Override
1676 public Dialog onCreateDialog(Bundle savedInstanceState) {
1677 final Context context = getActivity();
1678
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001679 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001680 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1681
1682 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1683 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001684 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001685
1686 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001687 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001688 public void onClick(DialogInterface dialog, int which) {
1689 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1690 if (target != null) {
1691 target.setPolicyLimitBytes(limitBytes);
1692 }
1693 }
1694 });
1695
1696 return builder.create();
1697 }
1698 }
1699
1700 /**
1701 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1702 */
1703 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001704 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001705
1706 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001707 if (!parent.isAdded()) return;
1708
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001709 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001710 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001711
1712 final CycleEditorFragment dialog = new CycleEditorFragment();
1713 dialog.setArguments(args);
1714 dialog.setTargetFragment(parent, 0);
1715 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1716 }
1717
1718 @Override
1719 public Dialog onCreateDialog(Bundle savedInstanceState) {
1720 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001721 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1722 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001723
1724 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1725 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1726
1727 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1728 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1729
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001730 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1731 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001732
1733 cycleDayPicker.setMinValue(1);
1734 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001735 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001736 cycleDayPicker.setWrapSelectorWheel(true);
1737
1738 builder.setTitle(R.string.data_usage_cycle_editor_title);
1739 builder.setView(view);
1740
1741 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1742 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001743 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001744 public void onClick(DialogInterface dialog, int which) {
Jeff Sharkeyd277de92013-03-25 15:11:25 -07001745 // clear focus to finish pending text edits
1746 cycleDayPicker.clearFocus();
1747
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001748 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001749 final String cycleTimezone = new Time().timezone;
1750 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001751 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001752 }
1753 });
1754
1755 return builder.create();
1756 }
1757 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001758
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001759 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001760 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1761 */
1762 public static class WarningEditorFragment extends DialogFragment {
1763 private static final String EXTRA_TEMPLATE = "template";
1764
1765 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001766 if (!parent.isAdded()) return;
1767
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001768 final Bundle args = new Bundle();
1769 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1770
1771 final WarningEditorFragment dialog = new WarningEditorFragment();
1772 dialog.setArguments(args);
1773 dialog.setTargetFragment(parent, 0);
1774 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1775 }
1776
1777 @Override
1778 public Dialog onCreateDialog(Bundle savedInstanceState) {
1779 final Context context = getActivity();
1780 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1781 final NetworkPolicyEditor editor = target.mPolicyEditor;
1782
1783 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1784 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1785
1786 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1787 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1788
1789 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1790 final long warningBytes = editor.getPolicyWarningBytes(template);
1791 final long limitBytes = editor.getPolicyLimitBytes(template);
1792
1793 bytesPicker.setMinValue(0);
1794 if (limitBytes != LIMIT_DISABLED) {
1795 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1796 } else {
1797 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1798 }
1799 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1800 bytesPicker.setWrapSelectorWheel(false);
1801
1802 builder.setTitle(R.string.data_usage_warning_editor_title);
1803 builder.setView(view);
1804
1805 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1806 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001807 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001808 public void onClick(DialogInterface dialog, int which) {
1809 // clear focus to finish pending text edits
1810 bytesPicker.clearFocus();
1811
1812 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1813 editor.setPolicyWarningBytes(template, bytes);
1814 target.updatePolicy(false);
1815 }
1816 });
1817
1818 return builder.create();
1819 }
1820 }
1821
1822 /**
1823 * Dialog to edit {@link NetworkPolicy#limitBytes}.
1824 */
1825 public static class LimitEditorFragment extends DialogFragment {
1826 private static final String EXTRA_TEMPLATE = "template";
1827
1828 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001829 if (!parent.isAdded()) return;
1830
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001831 final Bundle args = new Bundle();
1832 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1833
1834 final LimitEditorFragment dialog = new LimitEditorFragment();
1835 dialog.setArguments(args);
1836 dialog.setTargetFragment(parent, 0);
1837 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
1838 }
1839
1840 @Override
1841 public Dialog onCreateDialog(Bundle savedInstanceState) {
1842 final Context context = getActivity();
1843 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1844 final NetworkPolicyEditor editor = target.mPolicyEditor;
1845
1846 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1847 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1848
1849 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1850 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1851
1852 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1853 final long warningBytes = editor.getPolicyWarningBytes(template);
1854 final long limitBytes = editor.getPolicyLimitBytes(template);
1855
1856 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09001857 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001858 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
1859 } else {
1860 bytesPicker.setMinValue(0);
1861 }
1862 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
1863 bytesPicker.setWrapSelectorWheel(false);
1864
1865 builder.setTitle(R.string.data_usage_limit_editor_title);
1866 builder.setView(view);
1867
1868 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1869 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001870 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001871 public void onClick(DialogInterface dialog, int which) {
1872 // clear focus to finish pending text edits
1873 bytesPicker.clearFocus();
1874
1875 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1876 editor.setPolicyLimitBytes(template, bytes);
1877 target.updatePolicy(false);
1878 }
1879 });
1880
1881 return builder.create();
1882 }
1883 }
1884 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07001885 * Dialog to request user confirmation before disabling data.
1886 */
1887 public static class ConfirmDataDisableFragment extends DialogFragment {
1888 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001889 if (!parent.isAdded()) return;
1890
Jeff Sharkey28130d92011-09-02 16:10:24 -07001891 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
1892 dialog.setTargetFragment(parent, 0);
1893 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
1894 }
1895
1896 @Override
1897 public Dialog onCreateDialog(Bundle savedInstanceState) {
1898 final Context context = getActivity();
1899
1900 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1901 builder.setMessage(R.string.data_usage_disable_mobile);
1902
1903 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001904 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07001905 public void onClick(DialogInterface dialog, int which) {
1906 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1907 if (target != null) {
1908 // TODO: extend to modify policy enabled flag.
1909 target.setMobileDataEnabled(false);
1910 }
1911 }
1912 });
1913 builder.setNegativeButton(android.R.string.cancel, null);
1914
1915 return builder.create();
1916 }
1917 }
1918
1919 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001920 * Dialog to request user confirmation before setting
Christopher Tate5a64c732012-09-07 13:35:31 -07001921 * {@link android.provider.Settings.Global#DATA_ROAMING}.
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001922 */
1923 public static class ConfirmDataRoamingFragment extends DialogFragment {
1924 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001925 if (!parent.isAdded()) return;
1926
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001927 final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment();
1928 dialog.setTargetFragment(parent, 0);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001929 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_ROAMING);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001930 }
1931
1932 @Override
1933 public Dialog onCreateDialog(Bundle savedInstanceState) {
1934 final Context context = getActivity();
1935
1936 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1937 builder.setTitle(R.string.roaming_reenable_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07001938 if (Utils.hasMultipleUsers(context)) {
1939 builder.setMessage(R.string.roaming_warning_multiuser);
1940 } else {
1941 builder.setMessage(R.string.roaming_warning);
1942 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001943
1944 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001945 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001946 public void onClick(DialogInterface dialog, int which) {
1947 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1948 if (target != null) {
1949 target.setDataRoaming(true);
1950 }
1951 }
1952 });
1953 builder.setNegativeButton(android.R.string.cancel, null);
1954
1955 return builder.create();
1956 }
1957 }
1958
1959 /**
1960 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001961 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001962 */
1963 public static class ConfirmRestrictFragment extends DialogFragment {
1964 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001965 if (!parent.isAdded()) return;
1966
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001967 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1968 dialog.setTargetFragment(parent, 0);
1969 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1970 }
1971
1972 @Override
1973 public Dialog onCreateDialog(Bundle savedInstanceState) {
1974 final Context context = getActivity();
1975
1976 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001977 builder.setTitle(R.string.data_usage_restrict_background_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07001978 if (Utils.hasMultipleUsers(context)) {
1979 builder.setMessage(R.string.data_usage_restrict_background_multiuser);
1980 } else {
1981 builder.setMessage(R.string.data_usage_restrict_background);
1982 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001983
1984 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001985 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001986 public void onClick(DialogInterface dialog, int which) {
1987 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1988 if (target != null) {
1989 target.setRestrictBackground(true);
1990 }
1991 }
1992 });
1993 builder.setNegativeButton(android.R.string.cancel, null);
1994
1995 return builder.create();
1996 }
1997 }
1998
1999 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002000 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
2001 * change has been denied, usually based on
2002 * {@link DataUsageSummary#hasLimitedNetworks()}.
2003 */
2004 public static class DeniedRestrictFragment extends DialogFragment {
2005 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002006 if (!parent.isAdded()) return;
2007
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002008 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
2009 dialog.setTargetFragment(parent, 0);
2010 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
2011 }
2012
2013 @Override
2014 public Dialog onCreateDialog(Bundle savedInstanceState) {
2015 final Context context = getActivity();
2016
2017 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2018 builder.setTitle(R.string.data_usage_app_restrict_background);
2019 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
2020 builder.setPositiveButton(android.R.string.ok, null);
2021
2022 return builder.create();
2023 }
2024 }
2025
2026 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002027 * Dialog to request user confirmation before setting
2028 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
2029 */
2030 public static class ConfirmAppRestrictFragment extends DialogFragment {
2031 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002032 if (!parent.isAdded()) return;
2033
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002034 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2035 dialog.setTargetFragment(parent, 0);
2036 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2037 }
2038
2039 @Override
2040 public Dialog onCreateDialog(Bundle savedInstanceState) {
2041 final Context context = getActivity();
2042
2043 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002044 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2045 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2046
2047 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002048 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002049 public void onClick(DialogInterface dialog, int which) {
2050 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2051 if (target != null) {
2052 target.setAppRestrictBackground(true);
2053 }
2054 }
2055 });
2056 builder.setNegativeButton(android.R.string.cancel, null);
2057
2058 return builder.create();
2059 }
2060 }
2061
2062 /**
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002063 * Dialog to inform user about changing auto-sync setting
2064 */
2065 public static class ConfirmAutoSyncChangeFragment extends DialogFragment {
Amith Yamasani665235f2012-06-07 19:58:34 -07002066 private static final String SAVE_ENABLING = "enabling";
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002067 private boolean mEnabling;
2068
2069 public static void show(DataUsageSummary parent, boolean enabling) {
2070 if (!parent.isAdded()) return;
2071
2072 final ConfirmAutoSyncChangeFragment dialog = new ConfirmAutoSyncChangeFragment();
2073 dialog.mEnabling = enabling;
2074 dialog.setTargetFragment(parent, 0);
2075 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_AUTO_SYNC_CHANGE);
2076 }
2077
2078 @Override
2079 public Dialog onCreateDialog(Bundle savedInstanceState) {
2080 final Context context = getActivity();
Amith Yamasani665235f2012-06-07 19:58:34 -07002081 if (savedInstanceState != null) {
2082 mEnabling = savedInstanceState.getBoolean(SAVE_ENABLING);
2083 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002084
2085 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2086 if (!mEnabling) {
2087 builder.setTitle(R.string.data_usage_auto_sync_off_dialog_title);
2088 builder.setMessage(R.string.data_usage_auto_sync_off_dialog);
2089 } else {
2090 builder.setTitle(R.string.data_usage_auto_sync_on_dialog_title);
2091 builder.setMessage(R.string.data_usage_auto_sync_on_dialog);
2092 }
2093
2094 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
2095 @Override
2096 public void onClick(DialogInterface dialog, int which) {
2097 ContentResolver.setMasterSyncAutomatically(mEnabling);
2098 }
2099 });
2100 builder.setNegativeButton(android.R.string.cancel, null);
2101
2102 return builder.create();
2103 }
Amith Yamasani665235f2012-06-07 19:58:34 -07002104
2105 @Override
2106 public void onSaveInstanceState(Bundle outState) {
2107 super.onSaveInstanceState(outState);
2108 outState.putBoolean(SAVE_ENABLING, mEnabling);
2109 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002110 }
2111
2112 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002113 * Compute default tab that should be selected, based on
2114 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2115 */
2116 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002117 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
2118 if (template == null) return null;
2119
2120 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002121 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002122 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002123 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002124 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002125 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002126 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002127 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002128 return TAB_WIFI;
2129 default:
2130 return null;
2131 }
2132 }
2133
2134 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002135 * Background task that loads {@link UidDetail}, binding to
2136 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002137 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002138 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2139 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002140 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002141 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002142
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002143 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002144 mProvider = checkNotNull(provider);
2145 mItem = checkNotNull(item);
2146 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002147 }
2148
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002149 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002150 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002151 final UidDetailTask existing = (UidDetailTask) target.getTag();
2152 if (existing != null) {
2153 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002154 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002155
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002156 final UidDetail cachedDetail = provider.getUidDetail(item.key, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002157 if (cachedDetail != null) {
2158 bindView(cachedDetail, target);
2159 } else {
2160 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2161 AsyncTask.THREAD_POOL_EXECUTOR));
2162 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002163 }
2164
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002165 private static void bindView(UidDetail detail, View target) {
2166 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2167 final TextView title = (TextView) target.findViewById(android.R.id.title);
2168
2169 if (detail != null) {
2170 icon.setImageDrawable(detail.icon);
2171 title.setText(detail.label);
2172 } else {
2173 icon.setImageDrawable(null);
2174 title.setText(null);
2175 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002176 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002177
2178 @Override
2179 protected void onPreExecute() {
2180 bindView(null, mTarget);
2181 }
2182
2183 @Override
2184 protected UidDetail doInBackground(Void... params) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002185 return mProvider.getUidDetail(mItem.key, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002186 }
2187
2188 @Override
2189 protected void onPostExecute(UidDetail result) {
2190 bindView(result, mTarget);
2191 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002192 }
2193
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002194 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002195 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002196 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002197 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002198 if (TEST_RADIOS) {
2199 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2200 }
2201
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002202 final ConnectivityManager conn = ConnectivityManager.from(context);
2203 final TelephonyManager tele = TelephonyManager.from(context);
2204
2205 // require both supported network and ready SIM
2206 return conn.isNetworkSupported(TYPE_MOBILE) && tele.getSimState() == SIM_STATE_READY;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002207 }
2208
2209 /**
2210 * Test if device has a mobile 4G data radio.
2211 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002212 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002213 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2214 return false;
2215 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002216 if (TEST_RADIOS) {
2217 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2218 }
2219
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002220 final ConnectivityManager conn = ConnectivityManager.from(context);
2221 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002222
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002223 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Wink Saville55434042012-06-14 12:33:43 -07002224 final boolean hasLte = (tele.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE)
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002225 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002226 return hasWimax || hasLte;
2227 }
2228
2229 /**
2230 * Test if device has a Wi-Fi data radio.
2231 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002232 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002233 if (TEST_RADIOS) {
2234 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2235 }
2236
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002237 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002238 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002239 }
2240
2241 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002242 * Test if device has an ethernet network connection.
2243 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002244 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002245 if (TEST_RADIOS) {
2246 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2247 }
2248
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002249 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002250 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2251
2252 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002253 if (mStatsSession != null) {
2254 try {
2255 ethernetBytes = mStatsSession.getSummaryForNetwork(
2256 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2257 .getTotalBytes();
2258 } catch (RemoteException e) {
2259 throw new RuntimeException(e);
2260 }
2261 } else {
2262 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002263 }
2264
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002265 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002266 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002267 }
2268
2269 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002270 * Inflate a {@link Preference} style layout, adding the given {@link View}
2271 * widget into {@link android.R.id#widget_frame}.
2272 */
2273 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2274 final View view = inflater.inflate(R.layout.preference, root, false);
2275 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2276 android.R.id.widget_frame);
2277 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2278 return view;
2279 }
2280
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002281 private static View inflateAppTitle(
2282 LayoutInflater inflater, ViewGroup root, CharSequence label) {
2283 final TextView view = (TextView) inflater.inflate(
2284 R.layout.data_usage_app_title, root, false);
2285 view.setText(label);
2286 return view;
2287 }
2288
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002289 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002290 * Test if any networks are currently limited.
2291 */
2292 private boolean hasLimitedNetworks() {
2293 return !buildLimitedNetworksList().isEmpty();
2294 }
2295
2296 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002297 * Build string describing currently limited networks, which defines when
2298 * background data is restricted.
2299 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002300 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002301 private CharSequence buildLimitedNetworksString() {
2302 final List<CharSequence> limited = buildLimitedNetworksList();
2303
2304 // handle case where no networks limited
2305 if (limited.isEmpty()) {
2306 limited.add(getText(R.string.data_usage_list_none));
2307 }
2308
2309 return TextUtils.join(limited);
2310 }
2311
2312 /**
2313 * Build list of currently limited networks, which defines when background
2314 * data is restricted.
2315 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002316 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002317 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002318 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002319
2320 // build combined list of all limited networks
2321 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002322
2323 final TelephonyManager tele = TelephonyManager.from(context);
2324 if (tele.getSimState() == SIM_STATE_READY) {
2325 final String subscriberId = getActiveSubscriberId(context);
2326 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2327 limited.add(getText(R.string.data_usage_list_mobile));
2328 }
2329 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2330 limited.add(getText(R.string.data_usage_tab_3g));
2331 }
2332 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2333 limited.add(getText(R.string.data_usage_tab_4g));
2334 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002335 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002336
2337 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002338 limited.add(getText(R.string.data_usage_tab_wifi));
2339 }
2340 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2341 limited.add(getText(R.string.data_usage_tab_ethernet));
2342 }
2343
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002344 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002345 }
2346
2347 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002348 * Inset both selector and divider {@link Drawable} on the given
2349 * {@link ListView} by the requested dimensions.
2350 */
2351 private static void insetListViewDrawables(ListView view, int insetSide) {
2352 final Drawable selector = view.getSelector();
2353 final Drawable divider = view.getDivider();
2354
2355 // fully unregister these drawables so callbacks can be maintained after
2356 // wrapping below.
2357 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2358 view.setSelector(stub);
2359 view.setDivider(stub);
2360
2361 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2362 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2363 }
2364
2365 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002366 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002367 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002368 */
2369 private static void setPreferenceTitle(View parent, int resId) {
2370 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2371 title.setText(resId);
2372 }
2373
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002374 /**
2375 * Set {@link android.R.id#summary} for a preference view inflated with
2376 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2377 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002378 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002379 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2380 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002381 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002382 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002383}