blob: ce8f291045dc6528cd667f26c5d970ca41614046 [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;
38import static android.net.NetworkTemplate.buildTemplateWifi;
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 Sharkeyd39c6e42011-08-04 21:17:23 -070043import static android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
44import static android.text.format.DateUtils.FORMAT_SHOW_DATE;
45import static android.text.format.Time.TIMEZONE_UTC;
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 Sharkey8a503642011-06-10 13:31:21 -070072import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070073import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070074import android.net.NetworkStats;
75import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070076import android.net.NetworkTemplate;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070077import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070078import android.os.Bundle;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070079import android.os.INetworkManagementService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070080import android.os.RemoteException;
81import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070082import android.os.SystemProperties;
Jeff Sharkey8a503642011-06-10 13:31:21 -070083import android.preference.Preference;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070084import android.provider.Settings;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070085import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070086import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070087import android.text.format.DateUtils;
88import android.text.format.Formatter;
89import android.util.Log;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070090import android.util.SparseArray;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070091import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -070092import android.view.Menu;
93import android.view.MenuInflater;
94import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070095import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070096import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070097import android.view.ViewGroup;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -070098import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Jeff Sharkey8a503642011-06-10 13:31:21 -070099import android.widget.AdapterView;
100import android.widget.AdapterView.OnItemClickListener;
101import android.widget.AdapterView.OnItemSelectedListener;
102import android.widget.ArrayAdapter;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700103import android.widget.BaseAdapter;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700104import android.widget.Button;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700105import android.widget.CheckBox;
106import android.widget.CompoundButton;
107import android.widget.CompoundButton.OnCheckedChangeListener;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700108import android.widget.ImageView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700109import android.widget.LinearLayout;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700110import android.widget.ListView;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700111import android.widget.NumberPicker;
Jeff Sharkey2412b0f2011-07-17 20:31:40 -0700112import android.widget.ProgressBar;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700113import android.widget.Spinner;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700114import android.widget.Switch;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700115import android.widget.TabHost;
116import android.widget.TabHost.OnTabChangeListener;
117import android.widget.TabHost.TabContentFactory;
118import android.widget.TabHost.TabSpec;
119import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700120import android.widget.TextView;
121
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700122import com.android.internal.telephony.Phone;
Jeff Sharkey5d706792011-09-08 18:57:17 -0700123import com.android.settings.drawable.InsetBoundsDrawable;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700124import com.android.settings.net.ChartData;
125import com.android.settings.net.ChartDataLoader;
Jeff Sharkeya662e492011-06-18 21:57:06 -0700126import com.android.settings.net.NetworkPolicyEditor;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700127import com.android.settings.net.SummaryForAllUidLoader;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700128import com.android.settings.net.UidDetail;
129import com.android.settings.net.UidDetailProvider;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700130import com.android.settings.widget.ChartDataUsageView;
131import com.android.settings.widget.ChartDataUsageView.DataUsageChartListener;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700132import com.android.settings.widget.PieChartView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700133import com.google.android.collect.Lists;
134
135import java.util.ArrayList;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700136import java.util.Arrays;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700137import java.util.Collections;
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700138import java.util.List;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700139import java.util.Locale;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700140
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700141import libcore.util.Objects;
142
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700143/**
144 * Panel show data usage history across various networks, including options to
145 * inspect based on usage cycle and control through {@link NetworkPolicy}.
146 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700147public class DataUsageSummary extends Fragment {
148 private static final String TAG = "DataUsage";
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800149 private static final boolean LOGD = false;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700150
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700151 // TODO: remove this testing code
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700152 private static final boolean TEST_ANIM = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700153 private static final boolean TEST_RADIOS = false;
154 private static final String TEST_RADIOS_PROP = "test.radios";
155
Jeff Sharkey8a503642011-06-10 13:31:21 -0700156 private static final String TAB_3G = "3g";
157 private static final String TAB_4G = "4g";
158 private static final String TAB_MOBILE = "mobile";
159 private static final String TAB_WIFI = "wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700160 private static final String TAB_ETHERNET = "ethernet";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700161
Jeff Sharkey28130d92011-09-02 16:10:24 -0700162 private static final String TAG_CONFIRM_DATA_DISABLE = "confirmDataDisable";
163 private static final String TAG_CONFIRM_DATA_ROAMING = "confirmDataRoaming";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700164 private static final String TAG_CONFIRM_LIMIT = "confirmLimit";
165 private static final String TAG_CYCLE_EDITOR = "cycleEditor";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700166 private static final String TAG_WARNING_EDITOR = "warningEditor";
167 private static final String TAG_LIMIT_EDITOR = "limitEditor";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700168 private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700169 private static final String TAG_DENIED_RESTRICT = "deniedRestrict";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700170 private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700171 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700172
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700173 private static final int LOADER_CHART_DATA = 2;
174 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700175
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700176 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700177 private INetworkStatsService mStatsService;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700178 private INetworkPolicyManager mPolicyService;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700179 private ConnectivityManager mConnService;
180
181 private static final String PREF_FILE = "data_usage";
182 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700183 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700184
185 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700186
Jeff Sharkey8a503642011-06-10 13:31:21 -0700187 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700188 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700189 private TabWidget mTabWidget;
190 private ListView mListView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700191 private DataUsageAdapter mAdapter;
192
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700193 /** Distance to inset content from sides, when needed. */
194 private int mInsetSide = 0;
195
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700196 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700197
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700198 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700199 private LinearLayout mNetworkSwitches;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700200 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700201 private View mDataEnabledView;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700202 private CheckBox mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700203 private View mDisableAtLimitView;
204
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700205 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700206 private Spinner mCycleSpinner;
207 private CycleAdapter mCycleAdapter;
208
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700209 private ChartDataUsageView mChart;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700210 private TextView mUsageSummary;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700211 private TextView mEmpty;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700212
213 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700214 private ImageView mAppIcon;
215 private ViewGroup mAppTitles;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700216 private PieChartView mAppPieChart;
217 private TextView mAppForeground;
218 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700219 private Button mAppSettings;
220
221 private LinearLayout mAppSwitches;
222 private CheckBox mAppRestrict;
223 private View mAppRestrictView;
224
Jeff Sharkey8a503642011-06-10 13:31:21 -0700225 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700226 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700227
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700228 private NetworkTemplate mTemplate;
229 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700230
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700231 private int[] mAppDetailUids = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700232
233 private Intent mAppSettingsIntent;
234
Jeff Sharkeya662e492011-06-18 21:57:06 -0700235 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700236
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700237 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700238 private String mIntentTab = null;
239
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700240 private MenuItem mMenuDataRoaming;
241 private MenuItem mMenuRestrictBackground;
242
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700243 /** Flag used to ignore listeners during binding. */
244 private boolean mBinding;
245
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700246 private UidDetailProvider mUidDetailProvider;
247
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700248 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700249 public void onCreate(Bundle savedInstanceState) {
250 super.onCreate(savedInstanceState);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700251
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700252 mNetworkService = INetworkManagementService.Stub.asInterface(
253 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700254 mStatsService = INetworkStatsService.Stub.asInterface(
255 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700256 mPolicyService = INetworkPolicyManager.Stub.asInterface(
257 ServiceManager.getService(Context.NETWORK_POLICY_SERVICE));
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700258 mConnService = (ConnectivityManager) getActivity().getSystemService(
259 Context.CONNECTIVITY_SERVICE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700260
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700261 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700262
Jeff Sharkeya662e492011-06-18 21:57:06 -0700263 mPolicyEditor = new NetworkPolicyEditor(mPolicyService);
264 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700265
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700266 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700267 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700268
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700269 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700270 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700271
Jeff Sharkey8a503642011-06-10 13:31:21 -0700272 @Override
273 public View onCreateView(LayoutInflater inflater, ViewGroup container,
274 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700275
Jeff Sharkey8a503642011-06-10 13:31:21 -0700276 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700277 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
278
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700279 mUidDetailProvider = new UidDetailProvider(context);
280
Jeff Sharkey8a503642011-06-10 13:31:21 -0700281 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700282 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700283 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
284 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700285
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700286 // decide if we need to manually inset our content, or if we should rely
287 // on parent container for inset.
288 final boolean shouldInset = mListView.getScrollBarStyle()
289 == View.SCROLLBARS_OUTSIDE_OVERLAY;
290 if (shouldInset) {
291 mInsetSide = view.getResources().getDimensionPixelOffset(
292 com.android.internal.R.dimen.preference_fragment_padding_side);
293 } else {
294 mInsetSide = 0;
295 }
296
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700297 // adjust padding around tabwidget as needed
Jeff Sharkey5d706792011-09-08 18:57:17 -0700298 prepareCustomPreferencesList(container, view, mListView, true);
299
Jeff Sharkey8a503642011-06-10 13:31:21 -0700300 mTabHost.setup();
301 mTabHost.setOnTabChangedListener(mTabListener);
302
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700303 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700304 mHeader.setClickable(true);
305
306 mListView.addHeaderView(mHeader, null, true);
307 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700308
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700309 if (mInsetSide > 0) {
310 // inset selector and divider drawables
311 insetListViewDrawables(mListView, mInsetSide);
312 mHeader.setPadding(mInsetSide, 0, mInsetSide, 0);
313 }
314
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700315 {
316 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700317 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
318 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700319 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700320
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700321 mDataEnabled = new Switch(inflater.getContext());
322 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
323 mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener);
324 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700325
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700326 mDisableAtLimit = new CheckBox(inflater.getContext());
327 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700328 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700329 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700330 mDisableAtLimitView.setClickable(true);
331 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700332 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
333 mNetworkSwitches.addView(mDisableAtLimitView);
334 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700335
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700336 // bind cycle dropdown
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700337 mCycleView = mHeader.findViewById(R.id.cycles);
338 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700339 mCycleAdapter = new CycleAdapter(context);
340 mCycleSpinner.setAdapter(mCycleAdapter);
341 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
342
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700343 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700344 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800345 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700346
347 {
348 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700349 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700350 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
351 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700352 mAppPieChart = (PieChartView) mAppDetail.findViewById(R.id.app_pie_chart);
353 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
354 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700355 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700356
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700357 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700358 mAppSettings.setOnClickListener(mAppSettingsListener);
359
360 mAppRestrict = new CheckBox(inflater.getContext());
361 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700362 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700363 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700364 mAppRestrictView.setClickable(true);
365 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700366 mAppRestrictView.setOnClickListener(mAppRestrictListener);
367 mAppSwitches.addView(mAppRestrictView);
368 }
369
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700370 mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700371 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700372
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700373 // only assign layout transitions once first layout is finished
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700374 mListView.getViewTreeObserver().addOnGlobalLayoutListener(mFirstLayoutListener);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700375
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700376 mAdapter = new DataUsageAdapter(mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700377 mListView.setOnItemClickListener(mListListener);
378 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700379
380 return view;
381 }
382
383 @Override
384 public void onResume() {
385 super.onResume();
386
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700387 // pick default tab based on incoming intent
388 final Intent intent = getActivity().getIntent();
389 mIntentTab = computeTabFromIntent(intent);
390
Jeff Sharkey8a503642011-06-10 13:31:21 -0700391 // this kicks off chain reaction which creates tabs, binds the body to
392 // selected network, and binds chart, cycles and detail list.
393 updateTabs();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700394
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700395 // kick off background task to update stats
396 new AsyncTask<Void, Void, Void>() {
397 @Override
398 protected Void doInBackground(Void... params) {
399 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700400 // wait a few seconds before kicking off
401 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700402 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700403 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700404 } catch (RemoteException e) {
405 }
406 return null;
407 }
408
409 @Override
410 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700411 if (isAdded()) {
412 updateBody();
413 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700414 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700415 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700416 }
417
Jeff Sharkey8a503642011-06-10 13:31:21 -0700418 @Override
419 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
420 inflater.inflate(R.menu.data_usage, menu);
421 }
422
423 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700424 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700425 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700426 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700427
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700428 mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700429 mMenuDataRoaming.setVisible(hasMobileRadio(context) && !appDetailMode);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700430 mMenuDataRoaming.setChecked(getDataRoaming());
431
432 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800433 mMenuRestrictBackground.setVisible(hasMobileRadio(context) && !appDetailMode);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700434 mMenuRestrictBackground.setChecked(getRestrictBackground());
435
436 final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700437 split4g.setVisible(hasMobile4gRadio(context) && !appDetailMode);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700438 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700439
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700440 final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700441 if (hasWifiRadio(context) && hasMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700442 showWifi.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700443 showWifi.setChecked(mShowWifi);
444 } else {
445 showWifi.setVisible(false);
446 mShowWifi = true;
447 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700448
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700449 final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
450 if (hasEthernet(context) && hasMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700451 showEthernet.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700452 showEthernet.setChecked(mShowEthernet);
453 } else {
454 showEthernet.setVisible(false);
455 mShowEthernet = true;
456 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700457 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700458
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700459 @Override
460 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700461 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700462 case R.id.data_usage_menu_roaming: {
463 final boolean dataRoaming = !item.isChecked();
464 if (dataRoaming) {
465 ConfirmDataRoamingFragment.show(this);
466 } else {
467 // no confirmation to disable roaming
468 setDataRoaming(false);
469 }
470 return true;
471 }
472 case R.id.data_usage_menu_restrict_background: {
473 final boolean restrictBackground = !item.isChecked();
474 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800475 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700476 } else {
477 // no confirmation to drop restriction
478 setRestrictBackground(false);
479 }
480 return true;
481 }
482 case R.id.data_usage_menu_split_4g: {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700483 final boolean mobileSplit = !item.isChecked();
Jeff Sharkeya662e492011-06-18 21:57:06 -0700484 setMobilePolicySplit(mobileSplit);
485 item.setChecked(isMobilePolicySplit());
Jeff Sharkey8a503642011-06-10 13:31:21 -0700486 updateTabs();
487 return true;
488 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700489 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700490 mShowWifi = !item.isChecked();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700491 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700492 item.setChecked(mShowWifi);
493 updateTabs();
494 return true;
495 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700496 case R.id.data_usage_menu_show_ethernet: {
497 mShowEthernet = !item.isChecked();
498 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
499 item.setChecked(mShowEthernet);
500 updateTabs();
501 return true;
502 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700503 }
504 return false;
505 }
506
Jeff Sharkey94a90952011-06-13 22:31:09 -0700507 @Override
508 public void onDestroyView() {
509 super.onDestroyView();
510
511 mDataEnabledView = null;
512 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700513
514 mUidDetailProvider.clearCache();
515 mUidDetailProvider = null;
Jeff Sharkey94a90952011-06-13 22:31:09 -0700516 }
517
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800518 @Override
519 public void onDestroy() {
520 if (this.isRemoving()) {
521 getFragmentManager()
522 .popBackStack(TAG_APP_DETAILS, FragmentManager.POP_BACK_STACK_INCLUSIVE);
523 }
524 super.onDestroy();
525 }
526
Jeff Sharkey8a503642011-06-10 13:31:21 -0700527 /**
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700528 * Listener to setup {@link LayoutTransition} after first layout pass.
529 */
530 private OnGlobalLayoutListener mFirstLayoutListener = new OnGlobalLayoutListener() {
531 /** {@inheritDoc} */
532 public void onGlobalLayout() {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700533 mListView.getViewTreeObserver().removeGlobalOnLayoutListener(mFirstLayoutListener);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700534
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700535 mTabsContainer.setLayoutTransition(buildLayoutTransition());
536 mHeader.setLayoutTransition(buildLayoutTransition());
537 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700538
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700539 final LayoutTransition chartTransition = buildLayoutTransition();
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700540 chartTransition.setStartDelay(LayoutTransition.APPEARING, 0);
541 chartTransition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700542 chartTransition.setAnimator(LayoutTransition.APPEARING, null);
Jeff Sharkey28130d92011-09-02 16:10:24 -0700543 chartTransition.setAnimator(LayoutTransition.DISAPPEARING, null);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700544 mChart.setLayoutTransition(chartTransition);
545 }
546 };
547
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700548 private static LayoutTransition buildLayoutTransition() {
549 final LayoutTransition transition = new LayoutTransition();
550 if (TEST_ANIM) {
551 transition.setDuration(1500);
552 }
553 transition.setAnimateParentHierarchy(false);
554 return transition;
555 }
556
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700557 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700558 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700559 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
560 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700561 */
562 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700563 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700564 mTabHost.clearAllTabs();
565
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700566 final boolean mobileSplit = isMobilePolicySplit();
567 if (mobileSplit && hasMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700568 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
569 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700570 } else if (hasMobileRadio(context)) {
571 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700572 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700573 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700574 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
575 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700576 if (mShowEthernet && hasEthernet(context)) {
577 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
578 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700579
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700580 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
581 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
582 if (mIntentTab != null) {
583 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700584 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700585 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700586 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700587 mTabHost.setCurrentTabByTag(mIntentTab);
588 }
589 mIntentTab = null;
590 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700591 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700592 }
593 }
594
Jeff Sharkey8a503642011-06-10 13:31:21 -0700595 /**
596 * Factory that provide empty {@link View} to make {@link TabHost} happy.
597 */
598 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
599 /** {@inheritDoc} */
600 public View createTabContent(String tag) {
601 return new View(mTabHost.getContext());
602 }
603 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700604
Jeff Sharkey8a503642011-06-10 13:31:21 -0700605 /**
606 * Build {@link TabSpec} with thin indicator, and empty content.
607 */
608 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700609 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
610 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700611 }
612
613 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
614 /** {@inheritDoc} */
615 public void onTabChanged(String tabId) {
616 // user changed tab; update body
617 updateBody();
618 }
619 };
620
621 /**
622 * Update body content based on current tab. Loads
623 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
624 * binds them to visible controls.
625 */
626 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700627 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700628 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700629
Jeff Sharkeya662e492011-06-18 21:57:06 -0700630 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700631 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700632
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700633 if (currentTab == null) {
634 Log.w(TAG, "no tab selected; hiding body");
635 mListView.setVisibility(View.GONE);
636 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700637 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700638 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700639 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700640
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700641 final boolean tabChanged = !currentTab.equals(mCurrentTab);
642 mCurrentTab = currentTab;
643
Jeff Sharkey8a503642011-06-10 13:31:21 -0700644 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
645
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700646 mDataEnabledView.setVisibility(View.VISIBLE);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700647
648 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700649 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
650 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700651 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700652
653 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700654 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
655 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
656 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700657 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700658
659 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700660 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
661 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
662 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700663 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700664
665 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700666 // wifi doesn't have any controls
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700667 mDataEnabledView.setVisibility(View.GONE);
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700668 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700669 mTemplate = buildTemplateWifi();
670
671 } else if (TAB_ETHERNET.equals(currentTab)) {
672 // ethernet doesn't have any controls
673 mDataEnabledView.setVisibility(View.GONE);
674 mDisableAtLimitView.setVisibility(View.GONE);
675 mTemplate = buildTemplateEthernet();
676
677 } else {
678 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700679 }
680
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700681 // kick off loader for network history
682 // TODO: consider chaining two loaders together instead of reloading
683 // network history when showing app detail.
684 getLoaderManager().restartLoader(LOADER_CHART_DATA,
685 ChartDataLoader.buildArgs(mTemplate, mAppDetailUids), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700686
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700687 // detail mode can change visible menus, invalidate
688 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700689
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700690 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700691 }
692
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700693 private boolean isAppDetailMode() {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700694 return mAppDetailUids != null;
695 }
696
697 private int getAppDetailPrimaryUid() {
698 return mAppDetailUids[0];
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700699 }
700
701 /**
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700702 * Update UID details panels to match {@link #mAppDetailUids}, showing or
703 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700704 */
705 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700706 final Context context = getActivity();
707 final PackageManager pm = context.getPackageManager();
708 final LayoutInflater inflater = getActivity().getLayoutInflater();
709
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700710 if (isAppDetailMode()) {
711 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700712 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700713 } else {
714 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700715 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700716
717 // hide detail stats when not in detail mode
718 mChart.bindDetailNetworkStats(null);
719 return;
720 }
721
722 // remove warning/limit sweeps while in detail mode
723 mChart.bindNetworkPolicy(null);
724
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700725 // show icon and all labels appearing under this app
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700726 final int primaryUid = getAppDetailPrimaryUid();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700727 final UidDetail detail = mUidDetailProvider.getUidDetail(primaryUid, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700728 mAppIcon.setImageDrawable(detail.icon);
729
730 mAppTitles.removeAllViews();
731 if (detail.detailLabels != null) {
732 for (CharSequence label : detail.detailLabels) {
733 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label));
734 }
735 } else {
736 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label));
737 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700738
739 // enable settings button when package provides it
740 // TODO: target torwards entire UID instead of just first package
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700741 final String[] packageNames = pm.getPackagesForUid(primaryUid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700742 if (packageNames != null && packageNames.length > 0) {
743 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
744 mAppSettingsIntent.setPackage(packageNames[0]);
745 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
746
747 final boolean matchFound = pm.resolveActivity(mAppSettingsIntent, 0) != null;
748 mAppSettings.setEnabled(matchFound);
749
750 } else {
751 mAppSettingsIntent = null;
752 mAppSettings.setEnabled(false);
753 }
754
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700755 updateDetailData();
756
757 if (NetworkPolicyManager.isUidValidForPolicy(context, primaryUid)
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800758 && !getRestrictBackground() && isBandwidthControlEnabled()
759 && hasMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700760 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800761 setPreferenceSummary(mAppRestrictView,
762 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700763
764 mAppRestrictView.setVisibility(View.VISIBLE);
765 mAppRestrict.setChecked(getAppRestrictBackground());
766
767 } else {
768 mAppRestrictView.setVisibility(View.GONE);
769 }
770 }
771
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700772 private void setPolicyWarningBytes(long warningBytes) {
773 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700774 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700775 updatePolicy(false);
776 }
777
778 private void setPolicyLimitBytes(long limitBytes) {
779 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700780 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700781 updatePolicy(false);
782 }
783
Jeff Sharkey28130d92011-09-02 16:10:24 -0700784 /**
785 * Local cache of value, used to work around delay when
786 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
787 */
788 private Boolean mMobileDataEnabled;
789
790 private boolean isMobileDataEnabled() {
791 if (mMobileDataEnabled != null) {
792 // TODO: deprecate and remove this once enabled flag is on policy
793 return mMobileDataEnabled;
794 } else {
795 return mConnService.getMobileDataEnabled();
796 }
797 }
798
799 private void setMobileDataEnabled(boolean enabled) {
800 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
801 mConnService.setMobileDataEnabled(enabled);
802 mMobileDataEnabled = enabled;
803 updatePolicy(false);
804 }
805
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700806 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
807 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked();
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700808 }
809
810 private boolean isBandwidthControlEnabled() {
811 try {
812 return mNetworkService.isBandwidthControlEnabled();
813 } catch (RemoteException e) {
814 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
815 return false;
816 }
817 }
818
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700819 private boolean getDataRoaming() {
820 final ContentResolver resolver = getActivity().getContentResolver();
821 return Settings.Secure.getInt(resolver, Settings.Secure.DATA_ROAMING, 0) != 0;
822 }
823
824 private void setDataRoaming(boolean enabled) {
825 // TODO: teach telephony DataConnectionTracker to watch and apply
826 // updates when changed.
827 final ContentResolver resolver = getActivity().getContentResolver();
828 Settings.Secure.putInt(resolver, Settings.Secure.DATA_ROAMING, enabled ? 1 : 0);
829 mMenuDataRoaming.setChecked(enabled);
830 }
831
832 private boolean getRestrictBackground() {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700833 try {
834 return mPolicyService.getRestrictBackground();
835 } catch (RemoteException e) {
836 Log.w(TAG, "problem talking with policy service: " + e);
837 return false;
838 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700839 }
840
841 private void setRestrictBackground(boolean restrictBackground) {
842 if (LOGD) Log.d(TAG, "setRestrictBackground()");
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700843 try {
844 mPolicyService.setRestrictBackground(restrictBackground);
845 mMenuRestrictBackground.setChecked(restrictBackground);
846 } catch (RemoteException e) {
847 Log.w(TAG, "problem talking with policy service: " + e);
848 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700849 }
850
851 private boolean getAppRestrictBackground() {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700852 final int primaryUid = getAppDetailPrimaryUid();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700853 final int uidPolicy;
854 try {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700855 uidPolicy = mPolicyService.getUidPolicy(primaryUid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700856 } catch (RemoteException e) {
857 // since we can't do much without policy, we bail hard.
858 throw new RuntimeException("problem reading network policy", e);
859 }
860
861 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
862 }
863
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700864 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700865 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700866 final int primaryUid = getAppDetailPrimaryUid();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700867 try {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700868 mPolicyService.setUidPolicy(primaryUid,
869 restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700870 } catch (RemoteException e) {
871 throw new RuntimeException("unable to save policy", e);
872 }
873
874 mAppRestrict.setChecked(restrictBackground);
875 }
876
Jeff Sharkey8a503642011-06-10 13:31:21 -0700877 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700878 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
879 * current {@link #mTemplate}.
880 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700881 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700882 if (isAppDetailMode()) {
883 mNetworkSwitches.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700884 } else {
885 mNetworkSwitches.setVisibility(View.VISIBLE);
886 }
887
Jeff Sharkey28130d92011-09-02 16:10:24 -0700888 // TODO: move enabled state directly into policy
889 if (TAB_MOBILE.equals(mCurrentTab)) {
890 mBinding = true;
891 mDataEnabled.setChecked(isMobileDataEnabled());
892 mBinding = false;
893 }
894
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700895 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
896 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700897 mDisableAtLimitView.setVisibility(View.VISIBLE);
898 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700899 if (!isAppDetailMode()) {
900 mChart.bindNetworkPolicy(policy);
901 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700902
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700903 } else {
904 // controls are disabled; don't bind warning/limit sweeps
905 mDisableAtLimitView.setVisibility(View.GONE);
906 mChart.bindNetworkPolicy(null);
907 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700908
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700909 if (refreshCycle) {
910 // generate cycle list based on policy and available history
911 updateCycleList(policy);
912 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700913 }
914
915 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700916 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
917 * and available {@link NetworkStatsHistory} data. Always selects the newest
918 * item, updating the inspection range on {@link #mChart}.
919 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700920 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700921 // stash away currently selected cycle to try restoring below
922 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700923 mCycleAdapter.clear();
924
925 final Context context = mCycleSpinner.getContext();
926
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700927 long historyStart = Long.MAX_VALUE;
928 long historyEnd = Long.MIN_VALUE;
929 if (mChartData != null) {
930 historyStart = mChartData.network.getStart();
931 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700932 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700933
Jeff Sharkey461842a2011-09-25 18:22:48 -0700934 final long now = System.currentTimeMillis();
935 if (historyStart == Long.MAX_VALUE) historyStart = now;
936 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700937
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700938 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700939 if (policy != null) {
940 // find the next cycle boundary
941 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700942
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700943 // walk backwards, generating all valid cycle ranges
944 while (cycleEnd > historyStart) {
945 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
946 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
947 + historyStart);
948 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
949 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700950 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700951 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700952
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700953 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700954 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700955 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700956
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700957 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700958 // no policy defined cycles; show entry for each four-week period
959 long cycleEnd = historyEnd;
960 while (cycleEnd > historyStart) {
961 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
962 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
963 cycleEnd = cycleStart;
964 }
965
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700966 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700967 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700968
969 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700970 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700971 final int position = mCycleAdapter.findNearestPosition(previousItem);
972 mCycleSpinner.setSelection(position);
973
974 // only force-update cycle when changed; skipping preserves any
975 // user-defined inspection region.
976 final CycleItem selectedItem = mCycleAdapter.getItem(position);
977 if (!Objects.equal(selectedItem, previousItem)) {
978 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
979 } else {
980 // but still kick off loader for detailed list
981 updateDetailData();
982 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700983 } else {
984 updateDetailData();
985 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700986 }
987
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700988 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700989 /** {@inheritDoc} */
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700990 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
991 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700992
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700993 final boolean dataEnabled = isChecked;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700994 final String currentTab = mCurrentTab;
995 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -0700996 if (dataEnabled) {
997 setMobileDataEnabled(true);
998 } else {
999 // disabling data; show confirmation dialog which eventually
1000 // calls setMobileDataEnabled() once user confirms.
1001 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1002 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001003 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001004
Jeff Sharkey28130d92011-09-02 16:10:24 -07001005 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001006 }
1007 };
1008
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001009 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001010 /** {@inheritDoc} */
1011 public void onClick(View v) {
1012 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001013 if (disableAtLimit) {
1014 // enabling limit; show confirmation dialog which eventually
1015 // calls setPolicyLimitBytes() once user confirms.
1016 ConfirmLimitFragment.show(DataUsageSummary.this);
1017 } else {
1018 setPolicyLimitBytes(LIMIT_DISABLED);
1019 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001020 }
1021 };
1022
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001023 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
1024 /** {@inheritDoc} */
1025 public void onClick(View v) {
1026 final boolean restrictBackground = !mAppRestrict.isChecked();
1027
1028 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001029 // enabling restriction; show confirmation dialog which
1030 // eventually calls setRestrictBackground() once user
1031 // confirms.
1032 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001033 } else {
1034 setAppRestrictBackground(false);
1035 }
1036 }
1037 };
1038
1039 private OnClickListener mAppSettingsListener = new OnClickListener() {
1040 /** {@inheritDoc} */
1041 public void onClick(View v) {
1042 // TODO: target torwards entire UID instead of just first package
1043 startActivity(mAppSettingsIntent);
1044 }
1045 };
1046
Jeff Sharkey8a503642011-06-10 13:31:21 -07001047 private OnItemClickListener mListListener = new OnItemClickListener() {
1048 /** {@inheritDoc} */
1049 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001050 final Context context = view.getContext();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001051 final AppUsageItem app = (AppUsageItem) parent.getItemAtPosition(position);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001052 final UidDetail detail = mUidDetailProvider.getUidDetail(app.uids[0], true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001053 AppDetailsFragment.show(DataUsageSummary.this, app.uids, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001054 }
1055 };
1056
1057 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
1058 /** {@inheritDoc} */
1059 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1060 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1061 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001062 // show cycle editor; will eventually call setPolicyCycleDay()
1063 // when user finishes editing.
1064 CycleEditorFragment.show(DataUsageSummary.this);
1065
1066 // reset spinner to something other than "change cycle..."
1067 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001068
1069 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001070 if (LOGD) {
1071 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1072 + cycle.end + "]");
1073 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001074
1075 // update chart to show selected cycle, and update detail data
1076 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001077 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001078
1079 updateDetailData();
1080 }
1081 }
1082
1083 /** {@inheritDoc} */
1084 public void onNothingSelected(AdapterView<?> parent) {
1085 // ignored
1086 }
1087 };
1088
1089 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001090 * Update details based on {@link #mChart} inspection range depending on
1091 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1092 * of applications data usage, and when {@link #isAppDetailMode()} update
1093 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001094 */
1095 private void updateDetailData() {
1096 if (LOGD) Log.d(TAG, "updateDetailData()");
1097
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001098 final long start = mChart.getInspectStart();
1099 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001100 final long now = System.currentTimeMillis();
1101
1102 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001103
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001104 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001105 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001106 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001107 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001108 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001109 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001110 final long foregroundBytes = entry.rxBytes + entry.txBytes;
1111
1112 mAppPieChart.setOriginAngle(175);
1113
1114 mAppPieChart.removeAllSlices();
1115 mAppPieChart.addSlice(foregroundBytes, Color.parseColor("#d88d3a"));
1116 mAppPieChart.addSlice(defaultBytes, Color.parseColor("#666666"));
1117
1118 mAppPieChart.generatePath();
1119
1120 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1121 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1122
1123 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001124 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001125
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001126 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001127
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001128 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001129 if (mChartData != null) {
1130 entry = mChartData.network.getValues(start, end, now, null);
1131 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001132
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001133 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001134 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001135 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001136 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001137
1138 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1139 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001140 final String rangePhrase = formatDateRange(context, start, end, false);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001141
1142 mUsageSummary.setText(
1143 getString(R.string.data_usage_total_during_range, totalPhrase, rangePhrase));
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001144 }
1145
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001146 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1147 ChartData>() {
1148 /** {@inheritDoc} */
1149 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
1150 return new ChartDataLoader(getActivity(), mStatsService, args);
1151 }
1152
1153 /** {@inheritDoc} */
1154 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1155 mChartData = data;
1156 mChart.bindNetworkStats(mChartData.network);
1157 mChart.bindDetailNetworkStats(mChartData.detail);
1158
1159 // calcuate policy cycles based on available data
1160 updatePolicy(true);
1161 updateAppDetail();
1162
1163 // force scroll to top of body when showing detail
1164 if (mChartData.detail != null) {
1165 mListView.smoothScrollToPosition(0);
1166 }
1167 }
1168
1169 /** {@inheritDoc} */
1170 public void onLoaderReset(Loader<ChartData> loader) {
1171 mChartData = null;
1172 mChart.bindNetworkStats(null);
1173 mChart.bindDetailNetworkStats(null);
1174 }
1175 };
1176
1177 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001178 NetworkStats>() {
1179 /** {@inheritDoc} */
1180 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
1181 return new SummaryForAllUidLoader(getActivity(), mStatsService, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001182 }
1183
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001184 /** {@inheritDoc} */
1185 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
1186 mAdapter.bindStats(data);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001187 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001188 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001189
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001190 /** {@inheritDoc} */
1191 public void onLoaderReset(Loader<NetworkStats> loader) {
1192 mAdapter.bindStats(null);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001193 updateEmptyVisible();
1194 }
1195
1196 private void updateEmptyVisible() {
1197 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1198 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001199 }
1200 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001201
Jeff Sharkeya662e492011-06-18 21:57:06 -07001202 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001203 final Context context = getActivity();
1204 if (hasMobileRadio(context)) {
1205 final String subscriberId = getActiveSubscriberId(context);
1206 return mPolicyEditor.isMobilePolicySplit(subscriberId);
1207 } else {
1208 return false;
1209 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001210 }
1211
1212 private void setMobilePolicySplit(boolean split) {
1213 final String subscriberId = getActiveSubscriberId(getActivity());
1214 mPolicyEditor.setMobilePolicySplit(subscriberId, split);
1215 }
1216
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001217 private static String getActiveSubscriberId(Context context) {
1218 final TelephonyManager telephony = (TelephonyManager) context.getSystemService(
1219 Context.TELEPHONY_SERVICE);
1220 return telephony.getSubscriberId();
1221 }
1222
Jeff Sharkey8a503642011-06-10 13:31:21 -07001223 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
1224 /** {@inheritDoc} */
1225 public void onInspectRangeChanged() {
1226 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
1227 updateDetailData();
1228 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001229
Jeff Sharkey8a503642011-06-10 13:31:21 -07001230 /** {@inheritDoc} */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001231 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001232 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001233 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001234
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001235 /** {@inheritDoc} */
1236 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001237 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001238 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001239
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001240 /** {@inheritDoc} */
1241 public void requestWarningEdit() {
1242 WarningEditorFragment.show(DataUsageSummary.this);
1243 }
1244
1245 /** {@inheritDoc} */
1246 public void requestLimitEdit() {
1247 LimitEditorFragment.show(DataUsageSummary.this);
1248 }
1249 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001250
1251 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001252 * List item that reflects a specific data usage cycle.
1253 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001254 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001255 public CharSequence label;
1256 public long start;
1257 public long end;
1258
Jeff Sharkey8a503642011-06-10 13:31:21 -07001259 CycleItem(CharSequence label) {
1260 this.label = label;
1261 }
1262
1263 public CycleItem(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001264 this.label = formatDateRange(context, start, end, true);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001265 this.start = start;
1266 this.end = end;
1267 }
1268
Jeff Sharkey8a503642011-06-10 13:31:21 -07001269 @Override
1270 public String toString() {
1271 return label.toString();
1272 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001273
1274 @Override
1275 public boolean equals(Object o) {
1276 if (o instanceof CycleItem) {
1277 final CycleItem another = (CycleItem) o;
1278 return start == another.start && end == another.end;
1279 }
1280 return false;
1281 }
1282
1283 /** {@inheritDoc} */
1284 public int compareTo(CycleItem another) {
1285 return Long.compare(start, another.start);
1286 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001287 }
1288
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001289 private static final StringBuilder sBuilder = new StringBuilder(50);
1290 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1291 sBuilder, Locale.getDefault());
1292
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001293 public static String formatDateRange(Context context, long start, long end, boolean utcTime) {
1294 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
1295 final String timezone = utcTime ? TIMEZONE_UTC : null;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001296
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001297 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001298 sBuilder.setLength(0);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001299 return DateUtils
1300 .formatDateRange(context, sFormatter, start, end, flags, timezone).toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001301 }
1302 }
1303
Jeff Sharkey8a503642011-06-10 13:31:21 -07001304 /**
1305 * Special-case data usage cycle that triggers dialog to change
1306 * {@link NetworkPolicy#cycleDay}.
1307 */
1308 public static class CycleChangeItem extends CycleItem {
1309 public CycleChangeItem(Context context) {
1310 super(context.getString(R.string.data_usage_change_cycle));
1311 }
1312 }
1313
1314 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001315 private boolean mChangePossible = false;
1316 private boolean mChangeVisible = false;
1317
1318 private final CycleChangeItem mChangeItem;
1319
Jeff Sharkey8a503642011-06-10 13:31:21 -07001320 public CycleAdapter(Context context) {
1321 super(context, android.R.layout.simple_spinner_item);
1322 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001323 mChangeItem = new CycleChangeItem(context);
1324 }
1325
1326 public void setChangePossible(boolean possible) {
1327 mChangePossible = possible;
1328 updateChange();
1329 }
1330
1331 public void setChangeVisible(boolean visible) {
1332 mChangeVisible = visible;
1333 updateChange();
1334 }
1335
1336 private void updateChange() {
1337 remove(mChangeItem);
1338 if (mChangePossible && mChangeVisible) {
1339 add(mChangeItem);
1340 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001341 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001342
1343 /**
1344 * Find position of {@link CycleItem} in this adapter which is nearest
1345 * the given {@link CycleItem}.
1346 */
1347 public int findNearestPosition(CycleItem target) {
1348 if (target != null) {
1349 final int count = getCount();
1350 for (int i = count - 1; i >= 0; i--) {
1351 final CycleItem item = getItem(i);
1352 if (item instanceof CycleChangeItem) {
1353 continue;
1354 } else if (item.compareTo(target) >= 0) {
1355 return i;
1356 }
1357 }
1358 }
1359 return 0;
1360 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001361 }
1362
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001363 private static class AppUsageItem implements Comparable<AppUsageItem> {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001364 public int[] uids;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001365 public long total;
1366
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001367 public AppUsageItem(int uid) {
1368 uids = new int[] { uid };
1369 }
1370
1371 public void addUid(int uid) {
1372 if (contains(uids, uid)) return;
1373 final int length = uids.length;
1374 uids = Arrays.copyOf(uids, length + 1);
1375 uids[length] = uid;
1376 }
1377
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001378 /** {@inheritDoc} */
1379 public int compareTo(AppUsageItem another) {
1380 return Long.compare(another.total, total);
1381 }
1382 }
1383
Jeff Sharkey8a503642011-06-10 13:31:21 -07001384 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001385 * Adapter of applications, sorted by total usage descending.
1386 */
1387 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001388 private final UidDetailProvider mProvider;
1389 private final int mInsetSide;
1390
Jeff Sharkey8a503642011-06-10 13:31:21 -07001391 private ArrayList<AppUsageItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001392 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001393
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001394 public DataUsageAdapter(UidDetailProvider provider, int insetSide) {
1395 mProvider = checkNotNull(provider);
1396 mInsetSide = insetSide;
1397 }
1398
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001399 /**
1400 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1401 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001402 public void bindStats(NetworkStats stats) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001403 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001404
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001405 final AppUsageItem systemItem = new AppUsageItem(android.os.Process.SYSTEM_UID);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001406 final SparseArray<AppUsageItem> knownUids = new SparseArray<AppUsageItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001407
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001408 NetworkStats.Entry entry = null;
1409 final int size = stats != null ? stats.size() : 0;
1410 for (int i = 0; i < size; i++) {
1411 entry = stats.getValues(i, entry);
1412
1413 final int uid = entry.uid;
1414 final boolean isApp = uid >= android.os.Process.FIRST_APPLICATION_UID
1415 && uid <= android.os.Process.LAST_APPLICATION_UID;
Jeff Sharkeya83a24f2011-09-16 01:52:39 -07001416 if (isApp || uid == UID_REMOVED || uid == UID_TETHERING) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001417 AppUsageItem item = knownUids.get(uid);
1418 if (item == null) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001419 item = new AppUsageItem(uid);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001420 knownUids.put(uid, item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001421 mItems.add(item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001422 }
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001423
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001424 item.total += entry.rxBytes + entry.txBytes;
1425 } else {
1426 systemItem.total += entry.rxBytes + entry.txBytes;
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001427 systemItem.addUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001428 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001429 }
1430
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001431 if (systemItem.total > 0) {
1432 mItems.add(systemItem);
1433 }
1434
Jeff Sharkey8a503642011-06-10 13:31:21 -07001435 Collections.sort(mItems);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001436 mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001437 notifyDataSetChanged();
1438 }
1439
1440 @Override
1441 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001442 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001443 }
1444
1445 @Override
1446 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001447 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001448 }
1449
1450 @Override
1451 public long getItemId(int position) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001452 return mItems.get(position).uids[0];
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001453 }
1454
1455 @Override
1456 public View getView(int position, View convertView, ViewGroup parent) {
1457 if (convertView == null) {
1458 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey5d706792011-09-08 18:57:17 -07001459 R.layout.data_usage_item, parent, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001460
1461 if (mInsetSide > 0) {
1462 convertView.setPadding(mInsetSide, 0, mInsetSide, 0);
1463 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001464 }
1465
1466 final Context context = parent.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001467
Jeff Sharkey28130d92011-09-02 16:10:24 -07001468 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001469 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1470 android.R.id.progress);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001471
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001472 // kick off async load of app details
Jeff Sharkey8a503642011-06-10 13:31:21 -07001473 final AppUsageItem item = mItems.get(position);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001474 UidDetailTask.bindView(mProvider, item, convertView);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001475
Jeff Sharkey28130d92011-09-02 16:10:24 -07001476 text1.setText(Formatter.formatFileSize(context, item.total));
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001477
1478 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1479 progress.setProgress(percentTotal);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001480
1481 return convertView;
1482 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001483 }
1484
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001485 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001486 * Empty {@link Fragment} that controls display of UID details in
1487 * {@link DataUsageSummary}.
1488 */
1489 public static class AppDetailsFragment extends Fragment {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001490 private static final String EXTRA_UIDS = "uids";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001491
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001492 public static void show(DataUsageSummary parent, int[] uids, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001493 if (!parent.isAdded()) return;
1494
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001495 final Bundle args = new Bundle();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001496 args.putIntArray(EXTRA_UIDS, uids);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001497
1498 final AppDetailsFragment fragment = new AppDetailsFragment();
1499 fragment.setArguments(args);
1500 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001501 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1502 ft.add(fragment, TAG_APP_DETAILS);
1503 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001504 ft.setBreadCrumbTitle(label);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001505 ft.commit();
1506 }
1507
1508 @Override
1509 public void onStart() {
1510 super.onStart();
1511 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001512 target.mAppDetailUids = getArguments().getIntArray(EXTRA_UIDS);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001513 target.updateBody();
1514 }
1515
1516 @Override
1517 public void onStop() {
1518 super.onStop();
1519 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001520 target.mAppDetailUids = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001521 target.updateBody();
1522 }
1523 }
1524
1525 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001526 * Dialog to request user confirmation before setting
1527 * {@link NetworkPolicy#limitBytes}.
1528 */
1529 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001530 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001531 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001532
1533 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001534 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001535
Jeff Sharkey461842a2011-09-25 18:22:48 -07001536 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001537 final CharSequence message;
1538 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001539
1540 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001541 final String currentTab = parent.mCurrentTab;
1542 if (TAB_3G.equals(currentTab)) {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001543 message = buildDialogMessage(res, R.string.data_usage_tab_3g);
1544 limitBytes = 5 * GB_IN_BYTES;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001545 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001546 message = buildDialogMessage(res, R.string.data_usage_tab_4g);
1547 limitBytes = 5 * GB_IN_BYTES;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001548 } else if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001549 message = buildDialogMessage(res, R.string.data_usage_list_mobile);
1550 limitBytes = 5 * GB_IN_BYTES;
1551 } else if (TAB_WIFI.equals(currentTab)) {
1552 message = buildDialogMessage(res, R.string.data_usage_tab_wifi);
1553 limitBytes = 5 * GB_IN_BYTES;
1554 } else {
1555 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001556 }
1557
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001558 final Bundle args = new Bundle();
1559 args.putCharSequence(EXTRA_MESSAGE, message);
1560 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1561
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001562 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1563 dialog.setArguments(args);
1564 dialog.setTargetFragment(parent, 0);
1565 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1566 }
1567
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001568 private static CharSequence buildDialogMessage(Resources res, int networkResId) {
1569 return res.getString(R.string.data_usage_limit_dialog, res.getString(networkResId));
1570 }
1571
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001572 @Override
1573 public Dialog onCreateDialog(Bundle savedInstanceState) {
1574 final Context context = getActivity();
1575
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001576 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001577 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1578
1579 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1580 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001581 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001582
1583 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1584 public void onClick(DialogInterface dialog, int which) {
1585 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1586 if (target != null) {
1587 target.setPolicyLimitBytes(limitBytes);
1588 }
1589 }
1590 });
1591
1592 return builder.create();
1593 }
1594 }
1595
1596 /**
1597 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1598 */
1599 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001600 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001601
1602 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001603 if (!parent.isAdded()) return;
1604
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001605 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001606 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001607
1608 final CycleEditorFragment dialog = new CycleEditorFragment();
1609 dialog.setArguments(args);
1610 dialog.setTargetFragment(parent, 0);
1611 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1612 }
1613
1614 @Override
1615 public Dialog onCreateDialog(Bundle savedInstanceState) {
1616 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001617 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1618 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001619
1620 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1621 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1622
1623 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1624 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1625
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001626 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1627 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001628
1629 cycleDayPicker.setMinValue(1);
1630 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001631 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001632 cycleDayPicker.setWrapSelectorWheel(true);
1633
1634 builder.setTitle(R.string.data_usage_cycle_editor_title);
1635 builder.setView(view);
1636
1637 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1638 new DialogInterface.OnClickListener() {
1639 public void onClick(DialogInterface dialog, int which) {
1640 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001641 editor.setPolicyCycleDay(template, cycleDay);
1642 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001643 }
1644 });
1645
1646 return builder.create();
1647 }
1648 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001649
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001650 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001651 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1652 */
1653 public static class WarningEditorFragment extends DialogFragment {
1654 private static final String EXTRA_TEMPLATE = "template";
1655
1656 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001657 if (!parent.isAdded()) return;
1658
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001659 final Bundle args = new Bundle();
1660 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1661
1662 final WarningEditorFragment dialog = new WarningEditorFragment();
1663 dialog.setArguments(args);
1664 dialog.setTargetFragment(parent, 0);
1665 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1666 }
1667
1668 @Override
1669 public Dialog onCreateDialog(Bundle savedInstanceState) {
1670 final Context context = getActivity();
1671 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1672 final NetworkPolicyEditor editor = target.mPolicyEditor;
1673
1674 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1675 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1676
1677 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1678 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1679
1680 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1681 final long warningBytes = editor.getPolicyWarningBytes(template);
1682 final long limitBytes = editor.getPolicyLimitBytes(template);
1683
1684 bytesPicker.setMinValue(0);
1685 if (limitBytes != LIMIT_DISABLED) {
1686 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1687 } else {
1688 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1689 }
1690 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1691 bytesPicker.setWrapSelectorWheel(false);
1692
1693 builder.setTitle(R.string.data_usage_warning_editor_title);
1694 builder.setView(view);
1695
1696 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1697 new DialogInterface.OnClickListener() {
1698 public void onClick(DialogInterface dialog, int which) {
1699 // clear focus to finish pending text edits
1700 bytesPicker.clearFocus();
1701
1702 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1703 editor.setPolicyWarningBytes(template, bytes);
1704 target.updatePolicy(false);
1705 }
1706 });
1707
1708 return builder.create();
1709 }
1710 }
1711
1712 /**
1713 * Dialog to edit {@link NetworkPolicy#limitBytes}.
1714 */
1715 public static class LimitEditorFragment extends DialogFragment {
1716 private static final String EXTRA_TEMPLATE = "template";
1717
1718 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001719 if (!parent.isAdded()) return;
1720
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001721 final Bundle args = new Bundle();
1722 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1723
1724 final LimitEditorFragment dialog = new LimitEditorFragment();
1725 dialog.setArguments(args);
1726 dialog.setTargetFragment(parent, 0);
1727 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
1728 }
1729
1730 @Override
1731 public Dialog onCreateDialog(Bundle savedInstanceState) {
1732 final Context context = getActivity();
1733 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1734 final NetworkPolicyEditor editor = target.mPolicyEditor;
1735
1736 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1737 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1738
1739 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1740 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1741
1742 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1743 final long warningBytes = editor.getPolicyWarningBytes(template);
1744 final long limitBytes = editor.getPolicyLimitBytes(template);
1745
1746 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09001747 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001748 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
1749 } else {
1750 bytesPicker.setMinValue(0);
1751 }
1752 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
1753 bytesPicker.setWrapSelectorWheel(false);
1754
1755 builder.setTitle(R.string.data_usage_limit_editor_title);
1756 builder.setView(view);
1757
1758 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1759 new DialogInterface.OnClickListener() {
1760 public void onClick(DialogInterface dialog, int which) {
1761 // clear focus to finish pending text edits
1762 bytesPicker.clearFocus();
1763
1764 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1765 editor.setPolicyLimitBytes(template, bytes);
1766 target.updatePolicy(false);
1767 }
1768 });
1769
1770 return builder.create();
1771 }
1772 }
1773 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07001774 * Dialog to request user confirmation before disabling data.
1775 */
1776 public static class ConfirmDataDisableFragment extends DialogFragment {
1777 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001778 if (!parent.isAdded()) return;
1779
Jeff Sharkey28130d92011-09-02 16:10:24 -07001780 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
1781 dialog.setTargetFragment(parent, 0);
1782 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
1783 }
1784
1785 @Override
1786 public Dialog onCreateDialog(Bundle savedInstanceState) {
1787 final Context context = getActivity();
1788
1789 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1790 builder.setMessage(R.string.data_usage_disable_mobile);
1791
1792 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1793 public void onClick(DialogInterface dialog, int which) {
1794 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1795 if (target != null) {
1796 // TODO: extend to modify policy enabled flag.
1797 target.setMobileDataEnabled(false);
1798 }
1799 }
1800 });
1801 builder.setNegativeButton(android.R.string.cancel, null);
1802
1803 return builder.create();
1804 }
1805 }
1806
1807 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001808 * Dialog to request user confirmation before setting
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001809 * {@link Settings.Secure#DATA_ROAMING}.
1810 */
1811 public static class ConfirmDataRoamingFragment extends DialogFragment {
1812 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001813 if (!parent.isAdded()) return;
1814
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001815 final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment();
1816 dialog.setTargetFragment(parent, 0);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001817 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_ROAMING);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001818 }
1819
1820 @Override
1821 public Dialog onCreateDialog(Bundle savedInstanceState) {
1822 final Context context = getActivity();
1823
1824 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1825 builder.setTitle(R.string.roaming_reenable_title);
1826 builder.setMessage(R.string.roaming_warning);
1827
1828 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1829 public void onClick(DialogInterface dialog, int which) {
1830 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1831 if (target != null) {
1832 target.setDataRoaming(true);
1833 }
1834 }
1835 });
1836 builder.setNegativeButton(android.R.string.cancel, null);
1837
1838 return builder.create();
1839 }
1840 }
1841
1842 /**
1843 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001844 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001845 */
1846 public static class ConfirmRestrictFragment extends DialogFragment {
1847 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001848 if (!parent.isAdded()) return;
1849
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001850 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1851 dialog.setTargetFragment(parent, 0);
1852 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1853 }
1854
1855 @Override
1856 public Dialog onCreateDialog(Bundle savedInstanceState) {
1857 final Context context = getActivity();
1858
1859 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001860 builder.setTitle(R.string.data_usage_restrict_background_title);
Jeff Sharkey461842a2011-09-25 18:22:48 -07001861 builder.setMessage(getString(R.string.data_usage_restrict_background));
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001862
1863 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1864 public void onClick(DialogInterface dialog, int which) {
1865 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1866 if (target != null) {
1867 target.setRestrictBackground(true);
1868 }
1869 }
1870 });
1871 builder.setNegativeButton(android.R.string.cancel, null);
1872
1873 return builder.create();
1874 }
1875 }
1876
1877 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001878 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
1879 * change has been denied, usually based on
1880 * {@link DataUsageSummary#hasLimitedNetworks()}.
1881 */
1882 public static class DeniedRestrictFragment extends DialogFragment {
1883 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001884 if (!parent.isAdded()) return;
1885
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001886 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
1887 dialog.setTargetFragment(parent, 0);
1888 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
1889 }
1890
1891 @Override
1892 public Dialog onCreateDialog(Bundle savedInstanceState) {
1893 final Context context = getActivity();
1894
1895 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1896 builder.setTitle(R.string.data_usage_app_restrict_background);
1897 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
1898 builder.setPositiveButton(android.R.string.ok, null);
1899
1900 return builder.create();
1901 }
1902 }
1903
1904 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001905 * Dialog to request user confirmation before setting
1906 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
1907 */
1908 public static class ConfirmAppRestrictFragment extends DialogFragment {
1909 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001910 if (!parent.isAdded()) return;
1911
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001912 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
1913 dialog.setTargetFragment(parent, 0);
1914 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
1915 }
1916
1917 @Override
1918 public Dialog onCreateDialog(Bundle savedInstanceState) {
1919 final Context context = getActivity();
1920
1921 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001922 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
1923 builder.setMessage(R.string.data_usage_app_restrict_dialog);
1924
1925 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1926 public void onClick(DialogInterface dialog, int which) {
1927 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1928 if (target != null) {
1929 target.setAppRestrictBackground(true);
1930 }
1931 }
1932 });
1933 builder.setNegativeButton(android.R.string.cancel, null);
1934
1935 return builder.create();
1936 }
1937 }
1938
1939 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001940 * Compute default tab that should be selected, based on
1941 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
1942 */
1943 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07001944 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
1945 if (template == null) return null;
1946
1947 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07001948 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001949 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001950 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001951 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001952 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001953 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001954 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001955 return TAB_WIFI;
1956 default:
1957 return null;
1958 }
1959 }
1960
1961 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001962 * Background task that loads {@link UidDetail}, binding to
1963 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001964 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001965 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
1966 private final UidDetailProvider mProvider;
1967 private final AppUsageItem mItem;
1968 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001969
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001970 private UidDetailTask(UidDetailProvider provider, AppUsageItem item, View target) {
1971 mProvider = checkNotNull(provider);
1972 mItem = checkNotNull(item);
1973 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001974 }
1975
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001976 public static void bindView(
1977 UidDetailProvider provider, AppUsageItem item, View target) {
1978 final UidDetailTask existing = (UidDetailTask) target.getTag();
1979 if (existing != null) {
1980 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001981 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001982
1983 final UidDetail cachedDetail = provider.getUidDetail(item.uids[0], false);
1984 if (cachedDetail != null) {
1985 bindView(cachedDetail, target);
1986 } else {
1987 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
1988 AsyncTask.THREAD_POOL_EXECUTOR));
1989 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001990 }
1991
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001992 private static void bindView(UidDetail detail, View target) {
1993 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
1994 final TextView title = (TextView) target.findViewById(android.R.id.title);
1995
1996 if (detail != null) {
1997 icon.setImageDrawable(detail.icon);
1998 title.setText(detail.label);
1999 } else {
2000 icon.setImageDrawable(null);
2001 title.setText(null);
2002 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002003 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002004
2005 @Override
2006 protected void onPreExecute() {
2007 bindView(null, mTarget);
2008 }
2009
2010 @Override
2011 protected UidDetail doInBackground(Void... params) {
2012 return mProvider.getUidDetail(mItem.uids[0], true);
2013 }
2014
2015 @Override
2016 protected void onPostExecute(UidDetail result) {
2017 bindView(result, mTarget);
2018 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002019 }
2020
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002021 /**
2022 * Test if device has a mobile data radio.
2023 */
2024 private static boolean hasMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002025 if (TEST_RADIOS) {
2026 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2027 }
2028
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002029 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
2030 Context.CONNECTIVITY_SERVICE);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002031 return conn.isNetworkSupported(TYPE_MOBILE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002032 }
2033
2034 /**
2035 * Test if device has a mobile 4G data radio.
2036 */
2037 private static boolean hasMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002038 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2039 return false;
2040 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002041 if (TEST_RADIOS) {
2042 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2043 }
2044
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002045 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
2046 Context.CONNECTIVITY_SERVICE);
2047 final TelephonyManager telephony = (TelephonyManager) context.getSystemService(
2048 Context.TELEPHONY_SERVICE);
2049
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002050 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002051 final boolean hasLte = telephony.getLteOnCdmaMode() == Phone.LTE_ON_CDMA_TRUE;
2052 return hasWimax || hasLte;
2053 }
2054
2055 /**
2056 * Test if device has a Wi-Fi data radio.
2057 */
2058 private static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002059 if (TEST_RADIOS) {
2060 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2061 }
2062
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002063 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
2064 Context.CONNECTIVITY_SERVICE);
2065 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002066 }
2067
2068 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002069 * Test if device has an ethernet network connection.
2070 */
2071 private static boolean hasEthernet(Context context) {
2072 if (TEST_RADIOS) {
2073 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2074 }
2075
2076 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
2077 Context.CONNECTIVITY_SERVICE);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002078 return conn.isNetworkSupported(TYPE_ETHERNET);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002079 }
2080
2081 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002082 * Inflate a {@link Preference} style layout, adding the given {@link View}
2083 * widget into {@link android.R.id#widget_frame}.
2084 */
2085 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2086 final View view = inflater.inflate(R.layout.preference, root, false);
2087 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2088 android.R.id.widget_frame);
2089 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2090 return view;
2091 }
2092
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002093 private static View inflateAppTitle(
2094 LayoutInflater inflater, ViewGroup root, CharSequence label) {
2095 final TextView view = (TextView) inflater.inflate(
2096 R.layout.data_usage_app_title, root, false);
2097 view.setText(label);
2098 return view;
2099 }
2100
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002101 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002102 * Test if any networks are currently limited.
2103 */
2104 private boolean hasLimitedNetworks() {
2105 return !buildLimitedNetworksList().isEmpty();
2106 }
2107
2108 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002109 * Build string describing currently limited networks, which defines when
2110 * background data is restricted.
2111 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002112 private CharSequence buildLimitedNetworksString() {
2113 final List<CharSequence> limited = buildLimitedNetworksList();
2114
2115 // handle case where no networks limited
2116 if (limited.isEmpty()) {
2117 limited.add(getText(R.string.data_usage_list_none));
2118 }
2119
2120 return TextUtils.join(limited);
2121 }
2122
2123 /**
2124 * Build list of currently limited networks, which defines when background
2125 * data is restricted.
2126 */
2127 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002128 final Context context = getActivity();
2129 final String subscriberId = getActiveSubscriberId(context);
2130
2131 // build combined list of all limited networks
2132 final ArrayList<CharSequence> limited = Lists.newArrayList();
2133 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2134 limited.add(getText(R.string.data_usage_list_mobile));
2135 }
2136 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2137 limited.add(getText(R.string.data_usage_tab_3g));
2138 }
2139 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2140 limited.add(getText(R.string.data_usage_tab_4g));
2141 }
2142 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifi())) {
2143 limited.add(getText(R.string.data_usage_tab_wifi));
2144 }
2145 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2146 limited.add(getText(R.string.data_usage_tab_ethernet));
2147 }
2148
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002149 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002150 }
2151
2152 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002153 * Inset both selector and divider {@link Drawable} on the given
2154 * {@link ListView} by the requested dimensions.
2155 */
2156 private static void insetListViewDrawables(ListView view, int insetSide) {
2157 final Drawable selector = view.getSelector();
2158 final Drawable divider = view.getDivider();
2159
2160 // fully unregister these drawables so callbacks can be maintained after
2161 // wrapping below.
2162 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2163 view.setSelector(stub);
2164 view.setDivider(stub);
2165
2166 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2167 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2168 }
2169
2170 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002171 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002172 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002173 */
2174 private static void setPreferenceTitle(View parent, int resId) {
2175 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2176 title.setText(resId);
2177 }
2178
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002179 /**
2180 * Set {@link android.R.id#summary} for a preference view inflated with
2181 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2182 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002183 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002184 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2185 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002186 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002187 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07002188
2189 private static boolean contains(int[] haystack, int needle) {
2190 for (int value : haystack) {
2191 if (value == needle) {
2192 return true;
2193 }
2194 }
2195 return false;
2196 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002197}