blob: b0c38b99eef3cf1722ea2940762d9c72591b58c0 [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 Sharkey4c72ae52011-06-14 15:01:18 -070051import android.app.AlertDialog;
52import android.app.Dialog;
53import android.app.DialogFragment;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070054import android.app.Fragment;
Amith Yamasani5ba0a022011-11-07 12:29:00 -080055import android.app.FragmentManager;
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;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070087import android.os.UserId;
Jeff Sharkey8a503642011-06-10 13:31:21 -070088import android.preference.Preference;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070089import android.preference.PreferenceActivity;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070090import android.provider.Settings;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070091import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070092import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070093import android.text.format.DateUtils;
94import android.text.format.Formatter;
Jeff Sharkeye5223a02012-03-09 17:11:14 -080095import android.text.format.Time;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070096import android.util.Log;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070097import android.util.SparseArray;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070098import android.util.SparseBooleanArray;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070099import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700100import android.view.Menu;
101import android.view.MenuInflater;
102import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700103import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700104import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700105import android.view.ViewGroup;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700106import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700107import android.widget.AdapterView;
108import android.widget.AdapterView.OnItemClickListener;
109import android.widget.AdapterView.OnItemSelectedListener;
110import android.widget.ArrayAdapter;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700111import android.widget.BaseAdapter;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700112import android.widget.Button;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700113import android.widget.CheckBox;
114import android.widget.CompoundButton;
115import android.widget.CompoundButton.OnCheckedChangeListener;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700116import android.widget.ImageView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700117import android.widget.LinearLayout;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700118import android.widget.ListView;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700119import android.widget.NumberPicker;
Jeff Sharkey2412b0f2011-07-17 20:31:40 -0700120import android.widget.ProgressBar;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700121import android.widget.Spinner;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700122import android.widget.Switch;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700123import android.widget.TabHost;
124import android.widget.TabHost.OnTabChangeListener;
125import android.widget.TabHost.TabContentFactory;
126import android.widget.TabHost.TabSpec;
127import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700128import android.widget.TextView;
129
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700130import com.android.internal.telephony.Phone;
Jeff Sharkey5d706792011-09-08 18:57:17 -0700131import com.android.settings.drawable.InsetBoundsDrawable;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700132import com.android.settings.net.ChartData;
133import com.android.settings.net.ChartDataLoader;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700134import com.android.settings.net.DataUsageMeteredSettings;
Jeff Sharkeya662e492011-06-18 21:57:06 -0700135import com.android.settings.net.NetworkPolicyEditor;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700136import com.android.settings.net.SummaryForAllUidLoader;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700137import com.android.settings.net.UidDetail;
138import com.android.settings.net.UidDetailProvider;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700139import com.android.settings.widget.ChartDataUsageView;
140import com.android.settings.widget.ChartDataUsageView.DataUsageChartListener;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700141import com.android.settings.widget.PieChartView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700142import com.google.android.collect.Lists;
143
144import java.util.ArrayList;
145import java.util.Collections;
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700146import java.util.List;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700147import java.util.Locale;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700148
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700149import libcore.util.Objects;
150
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700151/**
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700152 * Panel showing data usage history across various networks, including options
153 * to inspect based on usage cycle and control through {@link NetworkPolicy}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700154 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700155public class DataUsageSummary extends Fragment {
156 private static final String TAG = "DataUsage";
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800157 private static final boolean LOGD = false;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700158
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700159 // TODO: remove this testing code
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700160 private static final boolean TEST_ANIM = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700161 private static final boolean TEST_RADIOS = false;
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800162
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700163 private static final String TEST_RADIOS_PROP = "test.radios";
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800164 private static final String TEST_SUBSCRIBER_PROP = "test.subscriberid";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700165
Jeff Sharkey8a503642011-06-10 13:31:21 -0700166 private static final String TAB_3G = "3g";
167 private static final String TAB_4G = "4g";
168 private static final String TAB_MOBILE = "mobile";
169 private static final String TAB_WIFI = "wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700170 private static final String TAB_ETHERNET = "ethernet";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700171
Jeff Sharkey28130d92011-09-02 16:10:24 -0700172 private static final String TAG_CONFIRM_DATA_DISABLE = "confirmDataDisable";
173 private static final String TAG_CONFIRM_DATA_ROAMING = "confirmDataRoaming";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700174 private static final String TAG_CONFIRM_LIMIT = "confirmLimit";
175 private static final String TAG_CYCLE_EDITOR = "cycleEditor";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700176 private static final String TAG_WARNING_EDITOR = "warningEditor";
177 private static final String TAG_LIMIT_EDITOR = "limitEditor";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700178 private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700179 private static final String TAG_DENIED_RESTRICT = "deniedRestrict";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700180 private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700181 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700182
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700183 private static final int LOADER_CHART_DATA = 2;
184 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700185
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700186 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700187 private INetworkStatsService mStatsService;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700188 private NetworkPolicyManager mPolicyManager;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700189 private ConnectivityManager mConnService;
190
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700191 private INetworkStatsSession mStatsSession;
192
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700193 private static final String PREF_FILE = "data_usage";
194 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700195 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700196
197 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700198
Jeff Sharkey8a503642011-06-10 13:31:21 -0700199 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700200 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700201 private TabWidget mTabWidget;
202 private ListView mListView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700203 private DataUsageAdapter mAdapter;
204
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700205 /** Distance to inset content from sides, when needed. */
206 private int mInsetSide = 0;
207
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700208 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700209
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700210 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700211 private LinearLayout mNetworkSwitches;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700212 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700213 private View mDataEnabledView;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700214 private CheckBox mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700215 private View mDisableAtLimitView;
216
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700217 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700218 private Spinner mCycleSpinner;
219 private CycleAdapter mCycleAdapter;
220
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700221 private ChartDataUsageView mChart;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700222 private TextView mUsageSummary;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700223 private TextView mEmpty;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700224
225 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700226 private ImageView mAppIcon;
227 private ViewGroup mAppTitles;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700228 private PieChartView mAppPieChart;
229 private TextView mAppForeground;
230 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700231 private Button mAppSettings;
232
233 private LinearLayout mAppSwitches;
234 private CheckBox mAppRestrict;
235 private View mAppRestrictView;
236
Jeff Sharkey8a503642011-06-10 13:31:21 -0700237 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700238 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700239
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700240 private NetworkTemplate mTemplate;
241 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700242
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700243 private AppItem mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700244
245 private Intent mAppSettingsIntent;
246
Jeff Sharkeya662e492011-06-18 21:57:06 -0700247 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700248
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700249 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700250 private String mIntentTab = null;
251
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700252 private MenuItem mMenuDataRoaming;
253 private MenuItem mMenuRestrictBackground;
254
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700255 /** Flag used to ignore listeners during binding. */
256 private boolean mBinding;
257
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700258 private UidDetailProvider mUidDetailProvider;
259
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700260 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700261 public void onCreate(Bundle savedInstanceState) {
262 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700263 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700264
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700265 mNetworkService = INetworkManagementService.Stub.asInterface(
266 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700267 mStatsService = INetworkStatsService.Stub.asInterface(
268 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700269 mPolicyManager = NetworkPolicyManager.from(context);
270 mConnService = ConnectivityManager.from(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700271
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700272 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700273
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700274 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700275 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700276
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700277 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700278 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700279
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700280 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700281 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700282
Jeff Sharkey8a503642011-06-10 13:31:21 -0700283 @Override
284 public View onCreateView(LayoutInflater inflater, ViewGroup container,
285 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700286
Jeff Sharkey8a503642011-06-10 13:31:21 -0700287 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700288 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
289
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700290 mUidDetailProvider = new UidDetailProvider(context);
291
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700292 try {
293 mStatsSession = mStatsService.openSession();
294 } catch (RemoteException e) {
295 throw new RuntimeException(e);
296 }
297
Jeff Sharkey8a503642011-06-10 13:31:21 -0700298 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700299 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700300 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
301 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700302
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700303 // decide if we need to manually inset our content, or if we should rely
304 // on parent container for inset.
305 final boolean shouldInset = mListView.getScrollBarStyle()
306 == View.SCROLLBARS_OUTSIDE_OVERLAY;
307 if (shouldInset) {
308 mInsetSide = view.getResources().getDimensionPixelOffset(
309 com.android.internal.R.dimen.preference_fragment_padding_side);
310 } else {
311 mInsetSide = 0;
312 }
313
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700314 // adjust padding around tabwidget as needed
Jeff Sharkey5d706792011-09-08 18:57:17 -0700315 prepareCustomPreferencesList(container, view, mListView, true);
316
Jeff Sharkey8a503642011-06-10 13:31:21 -0700317 mTabHost.setup();
318 mTabHost.setOnTabChangedListener(mTabListener);
319
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700320 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700321 mHeader.setClickable(true);
322
323 mListView.addHeaderView(mHeader, null, true);
324 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700325
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700326 if (mInsetSide > 0) {
327 // inset selector and divider drawables
328 insetListViewDrawables(mListView, mInsetSide);
329 mHeader.setPadding(mInsetSide, 0, mInsetSide, 0);
330 }
331
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700332 {
333 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700334 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
335 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700336 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700337
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700338 mDataEnabled = new Switch(inflater.getContext());
339 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
340 mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener);
341 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700342
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700343 mDisableAtLimit = new CheckBox(inflater.getContext());
344 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700345 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700346 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700347 mDisableAtLimitView.setClickable(true);
348 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700349 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
350 mNetworkSwitches.addView(mDisableAtLimitView);
351 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700352
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700353 // bind cycle dropdown
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700354 mCycleView = mHeader.findViewById(R.id.cycles);
355 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700356 mCycleAdapter = new CycleAdapter(context);
357 mCycleSpinner.setAdapter(mCycleAdapter);
358 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
359
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700360 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700361 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800362 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700363
364 {
365 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700366 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700367 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
368 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700369 mAppPieChart = (PieChartView) mAppDetail.findViewById(R.id.app_pie_chart);
370 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
371 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700372 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700373
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700374 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700375 mAppSettings.setOnClickListener(mAppSettingsListener);
376
377 mAppRestrict = new CheckBox(inflater.getContext());
378 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700379 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700380 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700381 mAppRestrictView.setClickable(true);
382 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700383 mAppRestrictView.setOnClickListener(mAppRestrictListener);
384 mAppSwitches.addView(mAppRestrictView);
385 }
386
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700387 mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700388 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700389
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700390 // only assign layout transitions once first layout is finished
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700391 mListView.getViewTreeObserver().addOnGlobalLayoutListener(mFirstLayoutListener);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700392
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700393 mAdapter = new DataUsageAdapter(mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700394 mListView.setOnItemClickListener(mListListener);
395 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700396
397 return view;
398 }
399
400 @Override
401 public void onResume() {
402 super.onResume();
403
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700404 // pick default tab based on incoming intent
405 final Intent intent = getActivity().getIntent();
406 mIntentTab = computeTabFromIntent(intent);
407
Jeff Sharkey8a503642011-06-10 13:31:21 -0700408 // this kicks off chain reaction which creates tabs, binds the body to
409 // selected network, and binds chart, cycles and detail list.
410 updateTabs();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700411
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700412 // kick off background task to update stats
413 new AsyncTask<Void, Void, Void>() {
414 @Override
415 protected Void doInBackground(Void... params) {
416 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700417 // wait a few seconds before kicking off
418 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700419 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700420 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700421 } catch (RemoteException e) {
422 }
423 return null;
424 }
425
426 @Override
427 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700428 if (isAdded()) {
429 updateBody();
430 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700431 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700432 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700433 }
434
Jeff Sharkey8a503642011-06-10 13:31:21 -0700435 @Override
436 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
437 inflater.inflate(R.menu.data_usage, menu);
438 }
439
440 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700441 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700442 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700443 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700444
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700445 mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700446 mMenuDataRoaming.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700447 mMenuDataRoaming.setChecked(getDataRoaming());
448
449 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700450 mMenuRestrictBackground.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
451 mMenuRestrictBackground.setChecked(mPolicyManager.getRestrictBackground());
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700452
453 final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700454 split4g.setVisible(hasMobile4gRadio(context) && !appDetailMode);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700455 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700456
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700457 final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700458 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700459 showWifi.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700460 showWifi.setChecked(mShowWifi);
461 } else {
462 showWifi.setVisible(false);
463 mShowWifi = true;
464 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700465
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700466 final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700467 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700468 showEthernet.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700469 showEthernet.setChecked(mShowEthernet);
470 } else {
471 showEthernet.setVisible(false);
472 mShowEthernet = true;
473 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700474
475 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
476 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
477 metered.setVisible(!appDetailMode);
478 } else {
479 metered.setVisible(false);
480 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700481 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700482
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700483 @Override
484 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700485 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700486 case R.id.data_usage_menu_roaming: {
487 final boolean dataRoaming = !item.isChecked();
488 if (dataRoaming) {
489 ConfirmDataRoamingFragment.show(this);
490 } else {
491 // no confirmation to disable roaming
492 setDataRoaming(false);
493 }
494 return true;
495 }
496 case R.id.data_usage_menu_restrict_background: {
497 final boolean restrictBackground = !item.isChecked();
498 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800499 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700500 } else {
501 // no confirmation to drop restriction
502 setRestrictBackground(false);
503 }
504 return true;
505 }
506 case R.id.data_usage_menu_split_4g: {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700507 final boolean mobileSplit = !item.isChecked();
Jeff Sharkeya662e492011-06-18 21:57:06 -0700508 setMobilePolicySplit(mobileSplit);
509 item.setChecked(isMobilePolicySplit());
Jeff Sharkey8a503642011-06-10 13:31:21 -0700510 updateTabs();
511 return true;
512 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700513 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700514 mShowWifi = !item.isChecked();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700515 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700516 item.setChecked(mShowWifi);
517 updateTabs();
518 return true;
519 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700520 case R.id.data_usage_menu_show_ethernet: {
521 mShowEthernet = !item.isChecked();
522 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
523 item.setChecked(mShowEthernet);
524 updateTabs();
525 return true;
526 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700527 case R.id.data_usage_menu_metered: {
528 final PreferenceActivity activity = (PreferenceActivity) getActivity();
529 activity.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
530 R.string.data_usage_metered_title, null, this, 0);
531 return true;
532 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700533 }
534 return false;
535 }
536
Jeff Sharkey94a90952011-06-13 22:31:09 -0700537 @Override
538 public void onDestroyView() {
539 super.onDestroyView();
540
541 mDataEnabledView = null;
542 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700543
544 mUidDetailProvider.clearCache();
545 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700546
547 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700548 }
549
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800550 @Override
551 public void onDestroy() {
552 if (this.isRemoving()) {
553 getFragmentManager()
554 .popBackStack(TAG_APP_DETAILS, FragmentManager.POP_BACK_STACK_INCLUSIVE);
555 }
556 super.onDestroy();
557 }
558
Jeff Sharkey8a503642011-06-10 13:31:21 -0700559 /**
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700560 * Listener to setup {@link LayoutTransition} after first layout pass.
561 */
562 private OnGlobalLayoutListener mFirstLayoutListener = new OnGlobalLayoutListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700563 @Override
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700564 public void onGlobalLayout() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700565 mListView.getViewTreeObserver().removeOnGlobalLayoutListener(mFirstLayoutListener);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700566
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700567 mTabsContainer.setLayoutTransition(buildLayoutTransition());
568 mHeader.setLayoutTransition(buildLayoutTransition());
569 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700570
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700571 final LayoutTransition chartTransition = buildLayoutTransition();
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700572 chartTransition.setStartDelay(LayoutTransition.APPEARING, 0);
573 chartTransition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700574 chartTransition.setAnimator(LayoutTransition.APPEARING, null);
Jeff Sharkey28130d92011-09-02 16:10:24 -0700575 chartTransition.setAnimator(LayoutTransition.DISAPPEARING, null);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700576 mChart.setLayoutTransition(chartTransition);
577 }
578 };
579
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700580 private static LayoutTransition buildLayoutTransition() {
581 final LayoutTransition transition = new LayoutTransition();
582 if (TEST_ANIM) {
583 transition.setDuration(1500);
584 }
585 transition.setAnimateParentHierarchy(false);
586 return transition;
587 }
588
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700589 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700590 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700591 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
592 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700593 */
594 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700595 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700596 mTabHost.clearAllTabs();
597
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700598 final boolean mobileSplit = isMobilePolicySplit();
599 if (mobileSplit && hasMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700600 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
601 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700602 } else if (hasReadyMobileRadio(context)) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700603 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700604 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700605 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700606 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
607 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700608 if (mShowEthernet && hasEthernet(context)) {
609 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
610 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700611
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700612 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
613 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
614 if (mIntentTab != null) {
615 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700616 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700617 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700618 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700619 mTabHost.setCurrentTabByTag(mIntentTab);
620 }
621 mIntentTab = null;
622 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700623 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700624 }
625 }
626
Jeff Sharkey8a503642011-06-10 13:31:21 -0700627 /**
628 * Factory that provide empty {@link View} to make {@link TabHost} happy.
629 */
630 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700631 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700632 public View createTabContent(String tag) {
633 return new View(mTabHost.getContext());
634 }
635 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700636
Jeff Sharkey8a503642011-06-10 13:31:21 -0700637 /**
638 * Build {@link TabSpec} with thin indicator, and empty content.
639 */
640 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700641 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
642 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700643 }
644
645 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700646 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700647 public void onTabChanged(String tabId) {
648 // user changed tab; update body
649 updateBody();
650 }
651 };
652
653 /**
654 * Update body content based on current tab. Loads
655 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
656 * binds them to visible controls.
657 */
658 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700659 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700660 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700661
Jeff Sharkeya662e492011-06-18 21:57:06 -0700662 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700663 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700664
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700665 if (currentTab == null) {
666 Log.w(TAG, "no tab selected; hiding body");
667 mListView.setVisibility(View.GONE);
668 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700669 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700670 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700671 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700672
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700673 final boolean tabChanged = !currentTab.equals(mCurrentTab);
674 mCurrentTab = currentTab;
675
Jeff Sharkey8a503642011-06-10 13:31:21 -0700676 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
677
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700678 mDataEnabledView.setVisibility(View.VISIBLE);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700679
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700680 // TODO: remove mobile tabs when SIM isn't ready
681 final TelephonyManager tele = TelephonyManager.from(context);
682
Jeff Sharkey8a503642011-06-10 13:31:21 -0700683 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700684 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
685 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700686 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700687
688 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700689 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
690 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
691 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700692 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700693
694 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700695 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
696 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
697 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700698 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700699
700 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700701 // wifi doesn't have any controls
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700702 mDataEnabledView.setVisibility(View.GONE);
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700703 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700704 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700705
706 } else if (TAB_ETHERNET.equals(currentTab)) {
707 // ethernet doesn't have any controls
708 mDataEnabledView.setVisibility(View.GONE);
709 mDisableAtLimitView.setVisibility(View.GONE);
710 mTemplate = buildTemplateEthernet();
711
712 } else {
713 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700714 }
715
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700716 // kick off loader for network history
717 // TODO: consider chaining two loaders together instead of reloading
718 // network history when showing app detail.
719 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700720 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700721
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700722 // detail mode can change visible menus, invalidate
723 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700724
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700725 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700726 }
727
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700728 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700729 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700730 }
731
732 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700733 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700734 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700735 */
736 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700737 final Context context = getActivity();
738 final PackageManager pm = context.getPackageManager();
739 final LayoutInflater inflater = getActivity().getLayoutInflater();
740
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700741 if (isAppDetailMode()) {
742 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700743 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700744 } else {
745 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700746 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700747
748 // hide detail stats when not in detail mode
749 mChart.bindDetailNetworkStats(null);
750 return;
751 }
752
753 // remove warning/limit sweeps while in detail mode
754 mChart.bindNetworkPolicy(null);
755
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700756 // show icon and all labels appearing under this app
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700757 final int appId = mCurrentApp.appId;
758 final UidDetail detail = mUidDetailProvider.getUidDetail(appId, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700759 mAppIcon.setImageDrawable(detail.icon);
760
761 mAppTitles.removeAllViews();
762 if (detail.detailLabels != null) {
763 for (CharSequence label : detail.detailLabels) {
764 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label));
765 }
766 } else {
767 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label));
768 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700769
770 // enable settings button when package provides it
771 // TODO: target torwards entire UID instead of just first package
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700772 final String[] packageNames = pm.getPackagesForUid(appId);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700773 if (packageNames != null && packageNames.length > 0) {
774 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
775 mAppSettingsIntent.setPackage(packageNames[0]);
776 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
777
778 final boolean matchFound = pm.resolveActivity(mAppSettingsIntent, 0) != null;
779 mAppSettings.setEnabled(matchFound);
780
781 } else {
782 mAppSettingsIntent = null;
783 mAppSettings.setEnabled(false);
784 }
785
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700786 updateDetailData();
787
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700788 if (UserId.isApp(appId) && !mPolicyManager.getRestrictBackground()
789 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700790 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800791 setPreferenceSummary(mAppRestrictView,
792 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700793
794 mAppRestrictView.setVisibility(View.VISIBLE);
795 mAppRestrict.setChecked(getAppRestrictBackground());
796
797 } else {
798 mAppRestrictView.setVisibility(View.GONE);
799 }
800 }
801
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700802 private void setPolicyWarningBytes(long warningBytes) {
803 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700804 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700805 updatePolicy(false);
806 }
807
808 private void setPolicyLimitBytes(long limitBytes) {
809 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700810 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700811 updatePolicy(false);
812 }
813
Jeff Sharkey28130d92011-09-02 16:10:24 -0700814 /**
815 * Local cache of value, used to work around delay when
816 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
817 */
818 private Boolean mMobileDataEnabled;
819
820 private boolean isMobileDataEnabled() {
821 if (mMobileDataEnabled != null) {
822 // TODO: deprecate and remove this once enabled flag is on policy
823 return mMobileDataEnabled;
824 } else {
825 return mConnService.getMobileDataEnabled();
826 }
827 }
828
829 private void setMobileDataEnabled(boolean enabled) {
830 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
831 mConnService.setMobileDataEnabled(enabled);
832 mMobileDataEnabled = enabled;
833 updatePolicy(false);
834 }
835
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700836 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
837 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked();
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700838 }
839
840 private boolean isBandwidthControlEnabled() {
841 try {
842 return mNetworkService.isBandwidthControlEnabled();
843 } catch (RemoteException e) {
844 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
845 return false;
846 }
847 }
848
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700849 private boolean getDataRoaming() {
850 final ContentResolver resolver = getActivity().getContentResolver();
851 return Settings.Secure.getInt(resolver, Settings.Secure.DATA_ROAMING, 0) != 0;
852 }
853
854 private void setDataRoaming(boolean enabled) {
855 // TODO: teach telephony DataConnectionTracker to watch and apply
856 // updates when changed.
857 final ContentResolver resolver = getActivity().getContentResolver();
858 Settings.Secure.putInt(resolver, Settings.Secure.DATA_ROAMING, enabled ? 1 : 0);
859 mMenuDataRoaming.setChecked(enabled);
860 }
861
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700862 public void setRestrictBackground(boolean restrictBackground) {
863 mPolicyManager.setRestrictBackground(restrictBackground);
864 mMenuRestrictBackground.setChecked(restrictBackground);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700865 }
866
867 private boolean getAppRestrictBackground() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700868 final int appId = mCurrentApp.appId;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700869 final int uidPolicy = mPolicyManager.getAppPolicy(appId);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700870 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
871 }
872
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700873 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700874 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700875 final int appId = mCurrentApp.appId;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700876 mPolicyManager.setAppPolicy(appId,
877 restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700878 mAppRestrict.setChecked(restrictBackground);
879 }
880
Jeff Sharkey8a503642011-06-10 13:31:21 -0700881 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700882 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
883 * current {@link #mTemplate}.
884 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700885 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700886 if (isAppDetailMode()) {
887 mNetworkSwitches.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700888 } else {
889 mNetworkSwitches.setVisibility(View.VISIBLE);
890 }
891
Jeff Sharkey28130d92011-09-02 16:10:24 -0700892 // TODO: move enabled state directly into policy
893 if (TAB_MOBILE.equals(mCurrentTab)) {
894 mBinding = true;
895 mDataEnabled.setChecked(isMobileDataEnabled());
896 mBinding = false;
897 }
898
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700899 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
900 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700901 mDisableAtLimitView.setVisibility(View.VISIBLE);
902 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700903 if (!isAppDetailMode()) {
904 mChart.bindNetworkPolicy(policy);
905 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700906
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700907 } else {
908 // controls are disabled; don't bind warning/limit sweeps
909 mDisableAtLimitView.setVisibility(View.GONE);
910 mChart.bindNetworkPolicy(null);
911 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700912
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700913 if (refreshCycle) {
914 // generate cycle list based on policy and available history
915 updateCycleList(policy);
916 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700917 }
918
919 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700920 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
921 * and available {@link NetworkStatsHistory} data. Always selects the newest
922 * item, updating the inspection range on {@link #mChart}.
923 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700924 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700925 // stash away currently selected cycle to try restoring below
926 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700927 mCycleAdapter.clear();
928
929 final Context context = mCycleSpinner.getContext();
930
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700931 long historyStart = Long.MAX_VALUE;
932 long historyEnd = Long.MIN_VALUE;
933 if (mChartData != null) {
934 historyStart = mChartData.network.getStart();
935 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700936 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700937
Jeff Sharkey461842a2011-09-25 18:22:48 -0700938 final long now = System.currentTimeMillis();
939 if (historyStart == Long.MAX_VALUE) historyStart = now;
940 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700941
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700942 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700943 if (policy != null) {
944 // find the next cycle boundary
945 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700946
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700947 // walk backwards, generating all valid cycle ranges
948 while (cycleEnd > historyStart) {
949 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
950 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
951 + historyStart);
952 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
953 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700954 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700955 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700956
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700957 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700958 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700959 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700960
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700961 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700962 // no policy defined cycles; show entry for each four-week period
963 long cycleEnd = historyEnd;
964 while (cycleEnd > historyStart) {
965 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
966 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
967 cycleEnd = cycleStart;
968 }
969
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700970 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700971 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700972
973 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700974 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700975 final int position = mCycleAdapter.findNearestPosition(previousItem);
976 mCycleSpinner.setSelection(position);
977
978 // only force-update cycle when changed; skipping preserves any
979 // user-defined inspection region.
980 final CycleItem selectedItem = mCycleAdapter.getItem(position);
981 if (!Objects.equal(selectedItem, previousItem)) {
982 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
983 } else {
984 // but still kick off loader for detailed list
985 updateDetailData();
986 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700987 } else {
988 updateDetailData();
989 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700990 }
991
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700992 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700993 @Override
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700994 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
995 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700996
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700997 final boolean dataEnabled = isChecked;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700998 final String currentTab = mCurrentTab;
999 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001000 if (dataEnabled) {
1001 setMobileDataEnabled(true);
1002 } else {
1003 // disabling data; show confirmation dialog which eventually
1004 // calls setMobileDataEnabled() once user confirms.
1005 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1006 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001007 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001008
Jeff Sharkey28130d92011-09-02 16:10:24 -07001009 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001010 }
1011 };
1012
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001013 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001014 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001015 public void onClick(View v) {
1016 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001017 if (disableAtLimit) {
1018 // enabling limit; show confirmation dialog which eventually
1019 // calls setPolicyLimitBytes() once user confirms.
1020 ConfirmLimitFragment.show(DataUsageSummary.this);
1021 } else {
1022 setPolicyLimitBytes(LIMIT_DISABLED);
1023 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001024 }
1025 };
1026
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001027 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001028 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001029 public void onClick(View v) {
1030 final boolean restrictBackground = !mAppRestrict.isChecked();
1031
1032 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001033 // enabling restriction; show confirmation dialog which
1034 // eventually calls setRestrictBackground() once user
1035 // confirms.
1036 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001037 } else {
1038 setAppRestrictBackground(false);
1039 }
1040 }
1041 };
1042
1043 private OnClickListener mAppSettingsListener = new OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001044 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001045 public void onClick(View v) {
1046 // TODO: target torwards entire UID instead of just first package
1047 startActivity(mAppSettingsIntent);
1048 }
1049 };
1050
Jeff Sharkey8a503642011-06-10 13:31:21 -07001051 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001052 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001053 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001054 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001055 final AppItem app = (AppItem) parent.getItemAtPosition(position);
1056 final UidDetail detail = mUidDetailProvider.getUidDetail(app.appId, true);
1057 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001058 }
1059 };
1060
1061 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001062 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001063 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1064 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1065 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001066 // show cycle editor; will eventually call setPolicyCycleDay()
1067 // when user finishes editing.
1068 CycleEditorFragment.show(DataUsageSummary.this);
1069
1070 // reset spinner to something other than "change cycle..."
1071 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001072
1073 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001074 if (LOGD) {
1075 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1076 + cycle.end + "]");
1077 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001078
1079 // update chart to show selected cycle, and update detail data
1080 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001081 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001082
1083 updateDetailData();
1084 }
1085 }
1086
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001087 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001088 public void onNothingSelected(AdapterView<?> parent) {
1089 // ignored
1090 }
1091 };
1092
1093 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001094 * Update details based on {@link #mChart} inspection range depending on
1095 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1096 * of applications data usage, and when {@link #isAppDetailMode()} update
1097 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001098 */
1099 private void updateDetailData() {
1100 if (LOGD) Log.d(TAG, "updateDetailData()");
1101
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001102 final long start = mChart.getInspectStart();
1103 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001104 final long now = System.currentTimeMillis();
1105
1106 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001107
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001108 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001109 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001110 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001111 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001112 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001113 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001114 final long foregroundBytes = entry.rxBytes + entry.txBytes;
1115
1116 mAppPieChart.setOriginAngle(175);
1117
1118 mAppPieChart.removeAllSlices();
1119 mAppPieChart.addSlice(foregroundBytes, Color.parseColor("#d88d3a"));
1120 mAppPieChart.addSlice(defaultBytes, Color.parseColor("#666666"));
1121
1122 mAppPieChart.generatePath();
1123
1124 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1125 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1126
1127 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001128 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001129
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001130 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001131
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001132 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001133 if (mChartData != null) {
1134 entry = mChartData.network.getValues(start, end, now, null);
1135 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001136
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001137 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001138 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001139 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001140 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001141
1142 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1143 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001144 final String rangePhrase = formatDateRange(context, start, end);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001145
1146 mUsageSummary.setText(
1147 getString(R.string.data_usage_total_during_range, totalPhrase, rangePhrase));
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001148 }
1149
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001150 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1151 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001152 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001153 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001154 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001155 }
1156
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001157 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001158 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1159 mChartData = data;
1160 mChart.bindNetworkStats(mChartData.network);
1161 mChart.bindDetailNetworkStats(mChartData.detail);
1162
1163 // calcuate policy cycles based on available data
1164 updatePolicy(true);
1165 updateAppDetail();
1166
1167 // force scroll to top of body when showing detail
1168 if (mChartData.detail != null) {
1169 mListView.smoothScrollToPosition(0);
1170 }
1171 }
1172
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001173 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001174 public void onLoaderReset(Loader<ChartData> loader) {
1175 mChartData = null;
1176 mChart.bindNetworkStats(null);
1177 mChart.bindDetailNetworkStats(null);
1178 }
1179 };
1180
1181 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001182 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001183 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001184 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001185 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001186 }
1187
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001188 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001189 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
1190 mAdapter.bindStats(data);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001191 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001192 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001193
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001194 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001195 public void onLoaderReset(Loader<NetworkStats> loader) {
1196 mAdapter.bindStats(null);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001197 updateEmptyVisible();
1198 }
1199
1200 private void updateEmptyVisible() {
1201 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1202 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001203 }
1204 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001205
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001206 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001207 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001208 final Context context = getActivity();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001209 if (hasReadyMobileRadio(context)) {
1210 final TelephonyManager tele = TelephonyManager.from(context);
1211 return mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001212 } else {
1213 return false;
1214 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001215 }
1216
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001217 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001218 private void setMobilePolicySplit(boolean split) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001219 final Context context = getActivity();
1220 if (hasReadyMobileRadio(context)) {
1221 final TelephonyManager tele = TelephonyManager.from(context);
1222 mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split);
1223 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001224 }
1225
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001226 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001227 final TelephonyManager tele = TelephonyManager.from(context);
1228 final String actualSubscriberId = tele.getSubscriberId();
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -08001229 return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001230 }
1231
Jeff Sharkey8a503642011-06-10 13:31:21 -07001232 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001233 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001234 public void onInspectRangeChanged() {
1235 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
1236 updateDetailData();
1237 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001238
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001239 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001240 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001241 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001242 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001243
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001244 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001245 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001246 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001247 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001248
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001249 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001250 public void requestWarningEdit() {
1251 WarningEditorFragment.show(DataUsageSummary.this);
1252 }
1253
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001254 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001255 public void requestLimitEdit() {
1256 LimitEditorFragment.show(DataUsageSummary.this);
1257 }
1258 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001259
1260 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001261 * List item that reflects a specific data usage cycle.
1262 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001263 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001264 public CharSequence label;
1265 public long start;
1266 public long end;
1267
Jeff Sharkey8a503642011-06-10 13:31:21 -07001268 CycleItem(CharSequence label) {
1269 this.label = label;
1270 }
1271
1272 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001273 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001274 this.start = start;
1275 this.end = end;
1276 }
1277
Jeff Sharkey8a503642011-06-10 13:31:21 -07001278 @Override
1279 public String toString() {
1280 return label.toString();
1281 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001282
1283 @Override
1284 public boolean equals(Object o) {
1285 if (o instanceof CycleItem) {
1286 final CycleItem another = (CycleItem) o;
1287 return start == another.start && end == another.end;
1288 }
1289 return false;
1290 }
1291
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001292 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001293 public int compareTo(CycleItem another) {
1294 return Long.compare(start, another.start);
1295 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001296 }
1297
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001298 private static final StringBuilder sBuilder = new StringBuilder(50);
1299 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1300 sBuilder, Locale.getDefault());
1301
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001302 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001303 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001304
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001305 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001306 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001307 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1308 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001309 }
1310 }
1311
Jeff Sharkey8a503642011-06-10 13:31:21 -07001312 /**
1313 * Special-case data usage cycle that triggers dialog to change
1314 * {@link NetworkPolicy#cycleDay}.
1315 */
1316 public static class CycleChangeItem extends CycleItem {
1317 public CycleChangeItem(Context context) {
1318 super(context.getString(R.string.data_usage_change_cycle));
1319 }
1320 }
1321
1322 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001323 private boolean mChangePossible = false;
1324 private boolean mChangeVisible = false;
1325
1326 private final CycleChangeItem mChangeItem;
1327
Jeff Sharkey8a503642011-06-10 13:31:21 -07001328 public CycleAdapter(Context context) {
1329 super(context, android.R.layout.simple_spinner_item);
1330 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001331 mChangeItem = new CycleChangeItem(context);
1332 }
1333
1334 public void setChangePossible(boolean possible) {
1335 mChangePossible = possible;
1336 updateChange();
1337 }
1338
1339 public void setChangeVisible(boolean visible) {
1340 mChangeVisible = visible;
1341 updateChange();
1342 }
1343
1344 private void updateChange() {
1345 remove(mChangeItem);
1346 if (mChangePossible && mChangeVisible) {
1347 add(mChangeItem);
1348 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001349 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001350
1351 /**
1352 * Find position of {@link CycleItem} in this adapter which is nearest
1353 * the given {@link CycleItem}.
1354 */
1355 public int findNearestPosition(CycleItem target) {
1356 if (target != null) {
1357 final int count = getCount();
1358 for (int i = count - 1; i >= 0; i--) {
1359 final CycleItem item = getItem(i);
1360 if (item instanceof CycleChangeItem) {
1361 continue;
1362 } else if (item.compareTo(target) >= 0) {
1363 return i;
1364 }
1365 }
1366 }
1367 return 0;
1368 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001369 }
1370
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001371 public static class AppItem implements Comparable<AppItem>, Parcelable {
1372 public final int appId;
1373 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001374 public long total;
1375
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001376 public AppItem(int appId) {
1377 this.appId = appId;
1378 }
1379
1380 public AppItem(Parcel parcel) {
1381 appId = parcel.readInt();
1382 uids = parcel.readSparseBooleanArray();
1383 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001384 }
1385
1386 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001387 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001388 }
1389
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001390 @Override
1391 public void writeToParcel(Parcel dest, int flags) {
1392 dest.writeInt(appId);
1393 dest.writeSparseBooleanArray(uids);
1394 dest.writeLong(total);
1395 }
1396
1397 @Override
1398 public int describeContents() {
1399 return 0;
1400 }
1401
1402 @Override
1403 public int compareTo(AppItem another) {
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001404 return Long.compare(another.total, total);
1405 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001406
1407 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1408 @Override
1409 public AppItem createFromParcel(Parcel in) {
1410 return new AppItem(in);
1411 }
1412
1413 @Override
1414 public AppItem[] newArray(int size) {
1415 return new AppItem[size];
1416 }
1417 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001418 }
1419
Jeff Sharkey8a503642011-06-10 13:31:21 -07001420 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001421 * Adapter of applications, sorted by total usage descending.
1422 */
1423 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001424 private final UidDetailProvider mProvider;
1425 private final int mInsetSide;
1426
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001427 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001428 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001429
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001430 public DataUsageAdapter(UidDetailProvider provider, int insetSide) {
1431 mProvider = checkNotNull(provider);
1432 mInsetSide = insetSide;
1433 }
1434
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001435 /**
1436 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1437 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001438 public void bindStats(NetworkStats stats) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001439 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001440
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001441 final AppItem systemItem = new AppItem(android.os.Process.SYSTEM_UID);
1442 final SparseArray<AppItem> knownUids = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001443
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001444 NetworkStats.Entry entry = null;
1445 final int size = stats != null ? stats.size() : 0;
1446 for (int i = 0; i < size; i++) {
1447 entry = stats.getValues(i, entry);
1448
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001449 final boolean isApp = UserId.isApp(entry.uid);
1450 final int appId = isApp ? UserId.getAppId(entry.uid) : entry.uid;
1451 if (isApp || appId == UID_REMOVED || appId == UID_TETHERING) {
1452 AppItem item = knownUids.get(appId);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001453 if (item == null) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001454 item = new AppItem(appId);
1455 knownUids.put(appId, item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001456 mItems.add(item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001457 }
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001458
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001459 item.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001460 item.addUid(entry.uid);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001461 } else {
1462 systemItem.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001463 systemItem.addUid(entry.uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001464 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001465 }
1466
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001467 if (systemItem.total > 0) {
1468 mItems.add(systemItem);
1469 }
1470
Jeff Sharkey8a503642011-06-10 13:31:21 -07001471 Collections.sort(mItems);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001472 mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001473 notifyDataSetChanged();
1474 }
1475
1476 @Override
1477 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001478 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001479 }
1480
1481 @Override
1482 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001483 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001484 }
1485
1486 @Override
1487 public long getItemId(int position) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001488 return mItems.get(position).appId;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001489 }
1490
1491 @Override
1492 public View getView(int position, View convertView, ViewGroup parent) {
1493 if (convertView == null) {
1494 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey5d706792011-09-08 18:57:17 -07001495 R.layout.data_usage_item, parent, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001496
1497 if (mInsetSide > 0) {
1498 convertView.setPadding(mInsetSide, 0, mInsetSide, 0);
1499 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001500 }
1501
1502 final Context context = parent.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001503
Jeff Sharkey28130d92011-09-02 16:10:24 -07001504 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001505 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1506 android.R.id.progress);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001507
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001508 // kick off async load of app details
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001509 final AppItem item = mItems.get(position);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001510 UidDetailTask.bindView(mProvider, item, convertView);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001511
Jeff Sharkey28130d92011-09-02 16:10:24 -07001512 text1.setText(Formatter.formatFileSize(context, item.total));
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001513
1514 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1515 progress.setProgress(percentTotal);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001516
1517 return convertView;
1518 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001519 }
1520
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001521 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001522 * Empty {@link Fragment} that controls display of UID details in
1523 * {@link DataUsageSummary}.
1524 */
1525 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001526 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001527
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001528 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001529 if (!parent.isAdded()) return;
1530
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001531 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001532 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001533
1534 final AppDetailsFragment fragment = new AppDetailsFragment();
1535 fragment.setArguments(args);
1536 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001537 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1538 ft.add(fragment, TAG_APP_DETAILS);
1539 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001540 ft.setBreadCrumbTitle(label);
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001541 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001542 }
1543
1544 @Override
1545 public void onStart() {
1546 super.onStart();
1547 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001548 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001549 target.updateBody();
1550 }
1551
1552 @Override
1553 public void onStop() {
1554 super.onStop();
1555 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001556 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001557 target.updateBody();
1558 }
1559 }
1560
1561 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001562 * Dialog to request user confirmation before setting
1563 * {@link NetworkPolicy#limitBytes}.
1564 */
1565 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001566 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001567 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001568
1569 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001570 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001571
Jeff Sharkey461842a2011-09-25 18:22:48 -07001572 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001573 final CharSequence message;
1574 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001575
1576 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001577 final String currentTab = parent.mCurrentTab;
1578 if (TAB_3G.equals(currentTab)) {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001579 message = buildDialogMessage(res, R.string.data_usage_tab_3g);
1580 limitBytes = 5 * GB_IN_BYTES;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001581 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001582 message = buildDialogMessage(res, R.string.data_usage_tab_4g);
1583 limitBytes = 5 * GB_IN_BYTES;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001584 } else if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001585 message = buildDialogMessage(res, R.string.data_usage_list_mobile);
1586 limitBytes = 5 * GB_IN_BYTES;
1587 } else if (TAB_WIFI.equals(currentTab)) {
1588 message = buildDialogMessage(res, R.string.data_usage_tab_wifi);
1589 limitBytes = 5 * GB_IN_BYTES;
1590 } else {
1591 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001592 }
1593
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001594 final Bundle args = new Bundle();
1595 args.putCharSequence(EXTRA_MESSAGE, message);
1596 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1597
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001598 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1599 dialog.setArguments(args);
1600 dialog.setTargetFragment(parent, 0);
1601 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1602 }
1603
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001604 private static CharSequence buildDialogMessage(Resources res, int networkResId) {
1605 return res.getString(R.string.data_usage_limit_dialog, res.getString(networkResId));
1606 }
1607
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001608 @Override
1609 public Dialog onCreateDialog(Bundle savedInstanceState) {
1610 final Context context = getActivity();
1611
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001612 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001613 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1614
1615 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1616 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001617 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001618
1619 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001620 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001621 public void onClick(DialogInterface dialog, int which) {
1622 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1623 if (target != null) {
1624 target.setPolicyLimitBytes(limitBytes);
1625 }
1626 }
1627 });
1628
1629 return builder.create();
1630 }
1631 }
1632
1633 /**
1634 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1635 */
1636 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001637 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001638
1639 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001640 if (!parent.isAdded()) return;
1641
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001642 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001643 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001644
1645 final CycleEditorFragment dialog = new CycleEditorFragment();
1646 dialog.setArguments(args);
1647 dialog.setTargetFragment(parent, 0);
1648 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1649 }
1650
1651 @Override
1652 public Dialog onCreateDialog(Bundle savedInstanceState) {
1653 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001654 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1655 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001656
1657 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1658 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1659
1660 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1661 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1662
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001663 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1664 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001665
1666 cycleDayPicker.setMinValue(1);
1667 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001668 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001669 cycleDayPicker.setWrapSelectorWheel(true);
1670
1671 builder.setTitle(R.string.data_usage_cycle_editor_title);
1672 builder.setView(view);
1673
1674 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1675 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001676 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001677 public void onClick(DialogInterface dialog, int which) {
1678 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001679 final String cycleTimezone = new Time().timezone;
1680 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001681 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001682 }
1683 });
1684
1685 return builder.create();
1686 }
1687 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001688
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001689 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001690 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1691 */
1692 public static class WarningEditorFragment extends DialogFragment {
1693 private static final String EXTRA_TEMPLATE = "template";
1694
1695 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001696 if (!parent.isAdded()) return;
1697
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001698 final Bundle args = new Bundle();
1699 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1700
1701 final WarningEditorFragment dialog = new WarningEditorFragment();
1702 dialog.setArguments(args);
1703 dialog.setTargetFragment(parent, 0);
1704 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1705 }
1706
1707 @Override
1708 public Dialog onCreateDialog(Bundle savedInstanceState) {
1709 final Context context = getActivity();
1710 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1711 final NetworkPolicyEditor editor = target.mPolicyEditor;
1712
1713 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1714 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1715
1716 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1717 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1718
1719 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1720 final long warningBytes = editor.getPolicyWarningBytes(template);
1721 final long limitBytes = editor.getPolicyLimitBytes(template);
1722
1723 bytesPicker.setMinValue(0);
1724 if (limitBytes != LIMIT_DISABLED) {
1725 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1726 } else {
1727 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1728 }
1729 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1730 bytesPicker.setWrapSelectorWheel(false);
1731
1732 builder.setTitle(R.string.data_usage_warning_editor_title);
1733 builder.setView(view);
1734
1735 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1736 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001737 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001738 public void onClick(DialogInterface dialog, int which) {
1739 // clear focus to finish pending text edits
1740 bytesPicker.clearFocus();
1741
1742 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1743 editor.setPolicyWarningBytes(template, bytes);
1744 target.updatePolicy(false);
1745 }
1746 });
1747
1748 return builder.create();
1749 }
1750 }
1751
1752 /**
1753 * Dialog to edit {@link NetworkPolicy#limitBytes}.
1754 */
1755 public static class LimitEditorFragment extends DialogFragment {
1756 private static final String EXTRA_TEMPLATE = "template";
1757
1758 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001759 if (!parent.isAdded()) return;
1760
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001761 final Bundle args = new Bundle();
1762 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1763
1764 final LimitEditorFragment dialog = new LimitEditorFragment();
1765 dialog.setArguments(args);
1766 dialog.setTargetFragment(parent, 0);
1767 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
1768 }
1769
1770 @Override
1771 public Dialog onCreateDialog(Bundle savedInstanceState) {
1772 final Context context = getActivity();
1773 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1774 final NetworkPolicyEditor editor = target.mPolicyEditor;
1775
1776 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1777 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1778
1779 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1780 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1781
1782 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1783 final long warningBytes = editor.getPolicyWarningBytes(template);
1784 final long limitBytes = editor.getPolicyLimitBytes(template);
1785
1786 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09001787 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001788 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
1789 } else {
1790 bytesPicker.setMinValue(0);
1791 }
1792 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
1793 bytesPicker.setWrapSelectorWheel(false);
1794
1795 builder.setTitle(R.string.data_usage_limit_editor_title);
1796 builder.setView(view);
1797
1798 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1799 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001800 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001801 public void onClick(DialogInterface dialog, int which) {
1802 // clear focus to finish pending text edits
1803 bytesPicker.clearFocus();
1804
1805 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1806 editor.setPolicyLimitBytes(template, bytes);
1807 target.updatePolicy(false);
1808 }
1809 });
1810
1811 return builder.create();
1812 }
1813 }
1814 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07001815 * Dialog to request user confirmation before disabling data.
1816 */
1817 public static class ConfirmDataDisableFragment extends DialogFragment {
1818 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001819 if (!parent.isAdded()) return;
1820
Jeff Sharkey28130d92011-09-02 16:10:24 -07001821 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
1822 dialog.setTargetFragment(parent, 0);
1823 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
1824 }
1825
1826 @Override
1827 public Dialog onCreateDialog(Bundle savedInstanceState) {
1828 final Context context = getActivity();
1829
1830 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1831 builder.setMessage(R.string.data_usage_disable_mobile);
1832
1833 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001834 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07001835 public void onClick(DialogInterface dialog, int which) {
1836 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1837 if (target != null) {
1838 // TODO: extend to modify policy enabled flag.
1839 target.setMobileDataEnabled(false);
1840 }
1841 }
1842 });
1843 builder.setNegativeButton(android.R.string.cancel, null);
1844
1845 return builder.create();
1846 }
1847 }
1848
1849 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001850 * Dialog to request user confirmation before setting
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001851 * {@link android.provider.Settings.Secure#DATA_ROAMING}.
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001852 */
1853 public static class ConfirmDataRoamingFragment extends DialogFragment {
1854 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001855 if (!parent.isAdded()) return;
1856
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001857 final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment();
1858 dialog.setTargetFragment(parent, 0);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001859 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_ROAMING);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001860 }
1861
1862 @Override
1863 public Dialog onCreateDialog(Bundle savedInstanceState) {
1864 final Context context = getActivity();
1865
1866 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1867 builder.setTitle(R.string.roaming_reenable_title);
1868 builder.setMessage(R.string.roaming_warning);
1869
1870 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001871 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001872 public void onClick(DialogInterface dialog, int which) {
1873 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1874 if (target != null) {
1875 target.setDataRoaming(true);
1876 }
1877 }
1878 });
1879 builder.setNegativeButton(android.R.string.cancel, null);
1880
1881 return builder.create();
1882 }
1883 }
1884
1885 /**
1886 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001887 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001888 */
1889 public static class ConfirmRestrictFragment extends DialogFragment {
1890 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001891 if (!parent.isAdded()) return;
1892
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001893 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1894 dialog.setTargetFragment(parent, 0);
1895 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1896 }
1897
1898 @Override
1899 public Dialog onCreateDialog(Bundle savedInstanceState) {
1900 final Context context = getActivity();
1901
1902 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001903 builder.setTitle(R.string.data_usage_restrict_background_title);
Jeff Sharkey461842a2011-09-25 18:22:48 -07001904 builder.setMessage(getString(R.string.data_usage_restrict_background));
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001905
1906 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001907 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001908 public void onClick(DialogInterface dialog, int which) {
1909 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1910 if (target != null) {
1911 target.setRestrictBackground(true);
1912 }
1913 }
1914 });
1915 builder.setNegativeButton(android.R.string.cancel, null);
1916
1917 return builder.create();
1918 }
1919 }
1920
1921 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001922 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
1923 * change has been denied, usually based on
1924 * {@link DataUsageSummary#hasLimitedNetworks()}.
1925 */
1926 public static class DeniedRestrictFragment extends DialogFragment {
1927 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001928 if (!parent.isAdded()) return;
1929
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001930 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
1931 dialog.setTargetFragment(parent, 0);
1932 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
1933 }
1934
1935 @Override
1936 public Dialog onCreateDialog(Bundle savedInstanceState) {
1937 final Context context = getActivity();
1938
1939 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1940 builder.setTitle(R.string.data_usage_app_restrict_background);
1941 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
1942 builder.setPositiveButton(android.R.string.ok, null);
1943
1944 return builder.create();
1945 }
1946 }
1947
1948 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001949 * Dialog to request user confirmation before setting
1950 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
1951 */
1952 public static class ConfirmAppRestrictFragment extends DialogFragment {
1953 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001954 if (!parent.isAdded()) return;
1955
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001956 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
1957 dialog.setTargetFragment(parent, 0);
1958 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
1959 }
1960
1961 @Override
1962 public Dialog onCreateDialog(Bundle savedInstanceState) {
1963 final Context context = getActivity();
1964
1965 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001966 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
1967 builder.setMessage(R.string.data_usage_app_restrict_dialog);
1968
1969 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001970 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001971 public void onClick(DialogInterface dialog, int which) {
1972 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1973 if (target != null) {
1974 target.setAppRestrictBackground(true);
1975 }
1976 }
1977 });
1978 builder.setNegativeButton(android.R.string.cancel, null);
1979
1980 return builder.create();
1981 }
1982 }
1983
1984 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001985 * Compute default tab that should be selected, based on
1986 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
1987 */
1988 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07001989 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
1990 if (template == null) return null;
1991
1992 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07001993 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001994 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001995 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001996 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001997 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001998 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001999 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002000 return TAB_WIFI;
2001 default:
2002 return null;
2003 }
2004 }
2005
2006 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002007 * Background task that loads {@link UidDetail}, binding to
2008 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002009 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002010 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2011 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002012 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002013 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002014
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002015 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002016 mProvider = checkNotNull(provider);
2017 mItem = checkNotNull(item);
2018 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002019 }
2020
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002021 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002022 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002023 final UidDetailTask existing = (UidDetailTask) target.getTag();
2024 if (existing != null) {
2025 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002026 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002027
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002028 final UidDetail cachedDetail = provider.getUidDetail(item.appId, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002029 if (cachedDetail != null) {
2030 bindView(cachedDetail, target);
2031 } else {
2032 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2033 AsyncTask.THREAD_POOL_EXECUTOR));
2034 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002035 }
2036
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002037 private static void bindView(UidDetail detail, View target) {
2038 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2039 final TextView title = (TextView) target.findViewById(android.R.id.title);
2040
2041 if (detail != null) {
2042 icon.setImageDrawable(detail.icon);
2043 title.setText(detail.label);
2044 } else {
2045 icon.setImageDrawable(null);
2046 title.setText(null);
2047 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002048 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002049
2050 @Override
2051 protected void onPreExecute() {
2052 bindView(null, mTarget);
2053 }
2054
2055 @Override
2056 protected UidDetail doInBackground(Void... params) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002057 return mProvider.getUidDetail(mItem.appId, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002058 }
2059
2060 @Override
2061 protected void onPostExecute(UidDetail result) {
2062 bindView(result, mTarget);
2063 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002064 }
2065
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002066 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002067 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002068 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002069 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002070 if (TEST_RADIOS) {
2071 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2072 }
2073
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002074 final ConnectivityManager conn = ConnectivityManager.from(context);
2075 final TelephonyManager tele = TelephonyManager.from(context);
2076
2077 // require both supported network and ready SIM
2078 return conn.isNetworkSupported(TYPE_MOBILE) && tele.getSimState() == SIM_STATE_READY;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002079 }
2080
2081 /**
2082 * Test if device has a mobile 4G data radio.
2083 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002084 public static boolean hasMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002085 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2086 return false;
2087 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002088 if (TEST_RADIOS) {
2089 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2090 }
2091
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002092 final ConnectivityManager conn = ConnectivityManager.from(context);
2093 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002094
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002095 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002096 final boolean hasLte = tele.getLteOnCdmaMode() == Phone.LTE_ON_CDMA_TRUE;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002097 return hasWimax || hasLte;
2098 }
2099
2100 /**
2101 * Test if device has a Wi-Fi data radio.
2102 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002103 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002104 if (TEST_RADIOS) {
2105 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2106 }
2107
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002108 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002109 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002110 }
2111
2112 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002113 * Test if device has an ethernet network connection.
2114 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002115 public static boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002116 if (TEST_RADIOS) {
2117 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2118 }
2119
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002120 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002121 return conn.isNetworkSupported(TYPE_ETHERNET);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002122 }
2123
2124 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002125 * Inflate a {@link Preference} style layout, adding the given {@link View}
2126 * widget into {@link android.R.id#widget_frame}.
2127 */
2128 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2129 final View view = inflater.inflate(R.layout.preference, root, false);
2130 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2131 android.R.id.widget_frame);
2132 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2133 return view;
2134 }
2135
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002136 private static View inflateAppTitle(
2137 LayoutInflater inflater, ViewGroup root, CharSequence label) {
2138 final TextView view = (TextView) inflater.inflate(
2139 R.layout.data_usage_app_title, root, false);
2140 view.setText(label);
2141 return view;
2142 }
2143
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002144 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002145 * Test if any networks are currently limited.
2146 */
2147 private boolean hasLimitedNetworks() {
2148 return !buildLimitedNetworksList().isEmpty();
2149 }
2150
2151 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002152 * Build string describing currently limited networks, which defines when
2153 * background data is restricted.
2154 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002155 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002156 private CharSequence buildLimitedNetworksString() {
2157 final List<CharSequence> limited = buildLimitedNetworksList();
2158
2159 // handle case where no networks limited
2160 if (limited.isEmpty()) {
2161 limited.add(getText(R.string.data_usage_list_none));
2162 }
2163
2164 return TextUtils.join(limited);
2165 }
2166
2167 /**
2168 * Build list of currently limited networks, which defines when background
2169 * data is restricted.
2170 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002171 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002172 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002173 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002174
2175 // build combined list of all limited networks
2176 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002177
2178 final TelephonyManager tele = TelephonyManager.from(context);
2179 if (tele.getSimState() == SIM_STATE_READY) {
2180 final String subscriberId = getActiveSubscriberId(context);
2181 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2182 limited.add(getText(R.string.data_usage_list_mobile));
2183 }
2184 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2185 limited.add(getText(R.string.data_usage_tab_3g));
2186 }
2187 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2188 limited.add(getText(R.string.data_usage_tab_4g));
2189 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002190 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002191
2192 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002193 limited.add(getText(R.string.data_usage_tab_wifi));
2194 }
2195 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2196 limited.add(getText(R.string.data_usage_tab_ethernet));
2197 }
2198
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002199 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002200 }
2201
2202 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002203 * Inset both selector and divider {@link Drawable} on the given
2204 * {@link ListView} by the requested dimensions.
2205 */
2206 private static void insetListViewDrawables(ListView view, int insetSide) {
2207 final Drawable selector = view.getSelector();
2208 final Drawable divider = view.getDivider();
2209
2210 // fully unregister these drawables so callbacks can be maintained after
2211 // wrapping below.
2212 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2213 view.setSelector(stub);
2214 view.setDivider(stub);
2215
2216 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2217 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2218 }
2219
2220 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002221 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002222 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002223 */
2224 private static void setPreferenceTitle(View parent, int resId) {
2225 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2226 title.setText(resId);
2227 }
2228
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002229 /**
2230 * Set {@link android.R.id#summary} for a preference view inflated with
2231 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2232 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002233 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002234 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2235 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002236 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002237 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002238}