blob: a2f0c3f335773b9ba112660f4e60c3a7ce3ac8d8 [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 Sharkeya83a24f2011-09-16 01:52:39 -070039import static android.net.TrafficStats.UID_REMOVED;
40import static android.net.TrafficStats.UID_TETHERING;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070041import static android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
42import static android.text.format.DateUtils.FORMAT_SHOW_DATE;
43import static android.text.format.Time.TIMEZONE_UTC;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070044import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -070045import static com.android.internal.util.Preconditions.checkNotNull;
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -070046import static com.android.settings.Utils.prepareCustomPreferencesList;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070047
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070048import android.animation.LayoutTransition;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070049import android.app.AlertDialog;
50import android.app.Dialog;
51import android.app.DialogFragment;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070052import android.app.Fragment;
Amith Yamasani5ba0a022011-11-07 12:29:00 -080053import android.app.FragmentManager;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070054import android.app.FragmentTransaction;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070055import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070056import android.content.ContentResolver;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070057import android.content.Context;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070058import android.content.DialogInterface;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -070059import android.content.Intent;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070060import android.content.Loader;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070061import android.content.SharedPreferences;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070062import android.content.pm.PackageManager;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070063import android.content.res.Resources;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070064import android.graphics.Color;
Jeff Sharkey5d706792011-09-08 18:57:17 -070065import android.graphics.drawable.ColorDrawable;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070066import android.graphics.drawable.Drawable;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070067import android.net.ConnectivityManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070068import android.net.INetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070069import android.net.INetworkStatsService;
Jeff Sharkey8a503642011-06-10 13:31:21 -070070import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070071import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070072import android.net.NetworkStats;
73import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070074import android.net.NetworkTemplate;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070075import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070076import android.os.Bundle;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070077import android.os.INetworkManagementService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070078import android.os.RemoteException;
79import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070080import android.os.SystemProperties;
Jeff Sharkey8a503642011-06-10 13:31:21 -070081import android.preference.Preference;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070082import android.provider.Settings;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070083import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070084import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070085import android.text.format.DateUtils;
86import android.text.format.Formatter;
87import android.util.Log;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070088import android.util.SparseArray;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070089import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -070090import android.view.Menu;
91import android.view.MenuInflater;
92import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070093import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070094import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070095import android.view.ViewGroup;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -070096import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Jeff Sharkey8a503642011-06-10 13:31:21 -070097import android.widget.AdapterView;
98import android.widget.AdapterView.OnItemClickListener;
99import android.widget.AdapterView.OnItemSelectedListener;
100import android.widget.ArrayAdapter;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700101import android.widget.BaseAdapter;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700102import android.widget.Button;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700103import android.widget.CheckBox;
104import android.widget.CompoundButton;
105import android.widget.CompoundButton.OnCheckedChangeListener;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700106import android.widget.ImageView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700107import android.widget.LinearLayout;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700108import android.widget.ListView;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700109import android.widget.NumberPicker;
Jeff Sharkey2412b0f2011-07-17 20:31:40 -0700110import android.widget.ProgressBar;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700111import android.widget.Spinner;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700112import android.widget.Switch;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700113import android.widget.TabHost;
114import android.widget.TabHost.OnTabChangeListener;
115import android.widget.TabHost.TabContentFactory;
116import android.widget.TabHost.TabSpec;
117import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700118import android.widget.TextView;
119
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700120import com.android.internal.telephony.Phone;
Jeff Sharkey5d706792011-09-08 18:57:17 -0700121import com.android.settings.drawable.InsetBoundsDrawable;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700122import com.android.settings.net.ChartData;
123import com.android.settings.net.ChartDataLoader;
Jeff Sharkeya662e492011-06-18 21:57:06 -0700124import com.android.settings.net.NetworkPolicyEditor;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700125import com.android.settings.net.SummaryForAllUidLoader;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700126import com.android.settings.net.UidDetail;
127import com.android.settings.net.UidDetailProvider;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700128import com.android.settings.widget.ChartDataUsageView;
129import com.android.settings.widget.ChartDataUsageView.DataUsageChartListener;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700130import com.android.settings.widget.PieChartView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700131import com.google.android.collect.Lists;
132
133import java.util.ArrayList;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700134import java.util.Arrays;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700135import java.util.Collections;
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700136import java.util.List;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700137import java.util.Locale;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700138
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700139import libcore.util.Objects;
140
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700141/**
142 * Panel show data usage history across various networks, including options to
143 * inspect based on usage cycle and control through {@link NetworkPolicy}.
144 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700145public class DataUsageSummary extends Fragment {
146 private static final String TAG = "DataUsage";
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800147 private static final boolean LOGD = false;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700148
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700149 // TODO: remove this testing code
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700150 private static final boolean TEST_ANIM = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700151 private static final boolean TEST_RADIOS = false;
152 private static final String TEST_RADIOS_PROP = "test.radios";
153
Jeff Sharkey8a503642011-06-10 13:31:21 -0700154 private static final String TAB_3G = "3g";
155 private static final String TAB_4G = "4g";
156 private static final String TAB_MOBILE = "mobile";
157 private static final String TAB_WIFI = "wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700158 private static final String TAB_ETHERNET = "ethernet";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700159
Jeff Sharkey28130d92011-09-02 16:10:24 -0700160 private static final String TAG_CONFIRM_DATA_DISABLE = "confirmDataDisable";
161 private static final String TAG_CONFIRM_DATA_ROAMING = "confirmDataRoaming";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700162 private static final String TAG_CONFIRM_LIMIT = "confirmLimit";
163 private static final String TAG_CYCLE_EDITOR = "cycleEditor";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700164 private static final String TAG_WARNING_EDITOR = "warningEditor";
165 private static final String TAG_LIMIT_EDITOR = "limitEditor";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700166 private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700167 private static final String TAG_DENIED_RESTRICT = "deniedRestrict";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700168 private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700169 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700170
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700171 private static final int LOADER_CHART_DATA = 2;
172 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700173
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700174 private static final long KB_IN_BYTES = 1024;
175 private static final long MB_IN_BYTES = KB_IN_BYTES * 1024;
176 private static final long GB_IN_BYTES = MB_IN_BYTES * 1024;
177
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700178 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700179 private INetworkStatsService mStatsService;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700180 private INetworkPolicyManager mPolicyService;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700181 private ConnectivityManager mConnService;
182
183 private static final String PREF_FILE = "data_usage";
184 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700185 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700186
187 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700188
Jeff Sharkey8a503642011-06-10 13:31:21 -0700189 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700190 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700191 private TabWidget mTabWidget;
192 private ListView mListView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700193 private DataUsageAdapter mAdapter;
194
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700195 /** Distance to inset content from sides, when needed. */
196 private int mInsetSide = 0;
197
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700198 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700199
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700200 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700201 private LinearLayout mNetworkSwitches;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700202 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700203 private View mDataEnabledView;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700204 private CheckBox mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700205 private View mDisableAtLimitView;
206
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700207 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700208 private Spinner mCycleSpinner;
209 private CycleAdapter mCycleAdapter;
210
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700211 private ChartDataUsageView mChart;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700212 private TextView mUsageSummary;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700213 private TextView mEmpty;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700214
215 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700216 private ImageView mAppIcon;
217 private ViewGroup mAppTitles;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700218 private PieChartView mAppPieChart;
219 private TextView mAppForeground;
220 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700221 private Button mAppSettings;
222
223 private LinearLayout mAppSwitches;
224 private CheckBox mAppRestrict;
225 private View mAppRestrictView;
226
Jeff Sharkey8a503642011-06-10 13:31:21 -0700227 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700228 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700229
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700230 private NetworkTemplate mTemplate;
231 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700232
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700233 private int[] mAppDetailUids = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700234
235 private Intent mAppSettingsIntent;
236
Jeff Sharkeya662e492011-06-18 21:57:06 -0700237 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700238
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700239 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700240 private String mIntentTab = null;
241
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700242 private MenuItem mMenuDataRoaming;
243 private MenuItem mMenuRestrictBackground;
244
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700245 /** Flag used to ignore listeners during binding. */
246 private boolean mBinding;
247
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700248 private UidDetailProvider mUidDetailProvider;
249
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700250 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700251 public void onCreate(Bundle savedInstanceState) {
252 super.onCreate(savedInstanceState);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700253
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700254 mNetworkService = INetworkManagementService.Stub.asInterface(
255 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700256 mStatsService = INetworkStatsService.Stub.asInterface(
257 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700258 mPolicyService = INetworkPolicyManager.Stub.asInterface(
259 ServiceManager.getService(Context.NETWORK_POLICY_SERVICE));
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700260 mConnService = (ConnectivityManager) getActivity().getSystemService(
261 Context.CONNECTIVITY_SERVICE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700262
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700263 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700264
Jeff Sharkeya662e492011-06-18 21:57:06 -0700265 mPolicyEditor = new NetworkPolicyEditor(mPolicyService);
266 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700267
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700268 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700269 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700270
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700271 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700272 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700273
Jeff Sharkey8a503642011-06-10 13:31:21 -0700274 @Override
275 public View onCreateView(LayoutInflater inflater, ViewGroup container,
276 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700277
Jeff Sharkey8a503642011-06-10 13:31:21 -0700278 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700279 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
280
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700281 mUidDetailProvider = new UidDetailProvider(context);
282
Jeff Sharkey8a503642011-06-10 13:31:21 -0700283 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700284 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700285 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
286 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700287
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700288 // decide if we need to manually inset our content, or if we should rely
289 // on parent container for inset.
290 final boolean shouldInset = mListView.getScrollBarStyle()
291 == View.SCROLLBARS_OUTSIDE_OVERLAY;
292 if (shouldInset) {
293 mInsetSide = view.getResources().getDimensionPixelOffset(
294 com.android.internal.R.dimen.preference_fragment_padding_side);
295 } else {
296 mInsetSide = 0;
297 }
298
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700299 // adjust padding around tabwidget as needed
Jeff Sharkey5d706792011-09-08 18:57:17 -0700300 prepareCustomPreferencesList(container, view, mListView, true);
301
Jeff Sharkey8a503642011-06-10 13:31:21 -0700302 mTabHost.setup();
303 mTabHost.setOnTabChangedListener(mTabListener);
304
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700305 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700306 mHeader.setClickable(true);
307
308 mListView.addHeaderView(mHeader, null, true);
309 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700310
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700311 if (mInsetSide > 0) {
312 // inset selector and divider drawables
313 insetListViewDrawables(mListView, mInsetSide);
314 mHeader.setPadding(mInsetSide, 0, mInsetSide, 0);
315 }
316
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700317 {
318 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700319 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
320 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700321 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700322
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700323 mDataEnabled = new Switch(inflater.getContext());
324 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
325 mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener);
326 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700327
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700328 mDisableAtLimit = new CheckBox(inflater.getContext());
329 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700330 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700331 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700332 mDisableAtLimitView.setClickable(true);
333 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700334 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
335 mNetworkSwitches.addView(mDisableAtLimitView);
336 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700337
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700338 // bind cycle dropdown
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700339 mCycleView = mHeader.findViewById(R.id.cycles);
340 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700341 mCycleAdapter = new CycleAdapter(context);
342 mCycleSpinner.setAdapter(mCycleAdapter);
343 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
344
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700345 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700346 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800347 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700348
349 {
350 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700351 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700352 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
353 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700354 mAppPieChart = (PieChartView) mAppDetail.findViewById(R.id.app_pie_chart);
355 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
356 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700357 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700358
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700359 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700360 mAppSettings.setOnClickListener(mAppSettingsListener);
361
362 mAppRestrict = new CheckBox(inflater.getContext());
363 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700364 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700365 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700366 mAppRestrictView.setClickable(true);
367 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700368 mAppRestrictView.setOnClickListener(mAppRestrictListener);
369 mAppSwitches.addView(mAppRestrictView);
370 }
371
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700372 mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700373 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700374
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700375 // only assign layout transitions once first layout is finished
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700376 mListView.getViewTreeObserver().addOnGlobalLayoutListener(mFirstLayoutListener);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700377
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700378 mAdapter = new DataUsageAdapter(mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700379 mListView.setOnItemClickListener(mListListener);
380 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700381
382 return view;
383 }
384
385 @Override
386 public void onResume() {
387 super.onResume();
388
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700389 // pick default tab based on incoming intent
390 final Intent intent = getActivity().getIntent();
391 mIntentTab = computeTabFromIntent(intent);
392
Jeff Sharkey8a503642011-06-10 13:31:21 -0700393 // this kicks off chain reaction which creates tabs, binds the body to
394 // selected network, and binds chart, cycles and detail list.
395 updateTabs();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700396
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700397 // kick off background task to update stats
398 new AsyncTask<Void, Void, Void>() {
399 @Override
400 protected Void doInBackground(Void... params) {
401 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700402 // wait a few seconds before kicking off
403 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700404 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700405 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700406 } catch (RemoteException e) {
407 }
408 return null;
409 }
410
411 @Override
412 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700413 if (isAdded()) {
414 updateBody();
415 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700416 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700417 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700418 }
419
Jeff Sharkey8a503642011-06-10 13:31:21 -0700420 @Override
421 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
422 inflater.inflate(R.menu.data_usage, menu);
423 }
424
425 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700426 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700427 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700428 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700429
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700430 mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700431 mMenuDataRoaming.setVisible(hasMobileRadio(context) && !appDetailMode);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700432 mMenuDataRoaming.setChecked(getDataRoaming());
433
434 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800435 mMenuRestrictBackground.setVisible(hasMobileRadio(context) && !appDetailMode);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700436 mMenuRestrictBackground.setChecked(getRestrictBackground());
437
438 final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700439 split4g.setVisible(hasMobile4gRadio(context) && !appDetailMode);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700440 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700441
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700442 final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700443 if (hasWifiRadio(context) && hasMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700444 showWifi.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700445 showWifi.setChecked(mShowWifi);
446 } else {
447 showWifi.setVisible(false);
448 mShowWifi = true;
449 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700450
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700451 final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
452 if (hasEthernet(context) && hasMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700453 showEthernet.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700454 showEthernet.setChecked(mShowEthernet);
455 } else {
456 showEthernet.setVisible(false);
457 mShowEthernet = true;
458 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700459 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700460
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700461 @Override
462 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700463 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700464 case R.id.data_usage_menu_roaming: {
465 final boolean dataRoaming = !item.isChecked();
466 if (dataRoaming) {
467 ConfirmDataRoamingFragment.show(this);
468 } else {
469 // no confirmation to disable roaming
470 setDataRoaming(false);
471 }
472 return true;
473 }
474 case R.id.data_usage_menu_restrict_background: {
475 final boolean restrictBackground = !item.isChecked();
476 if (restrictBackground) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700477 if (hasLimitedNetworks()) {
478 ConfirmRestrictFragment.show(this);
479 } else {
480 DeniedRestrictFragment.show(this);
481 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700482 } else {
483 // no confirmation to drop restriction
484 setRestrictBackground(false);
485 }
486 return true;
487 }
488 case R.id.data_usage_menu_split_4g: {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700489 final boolean mobileSplit = !item.isChecked();
Jeff Sharkeya662e492011-06-18 21:57:06 -0700490 setMobilePolicySplit(mobileSplit);
491 item.setChecked(isMobilePolicySplit());
Jeff Sharkey8a503642011-06-10 13:31:21 -0700492 updateTabs();
493 return true;
494 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700495 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700496 mShowWifi = !item.isChecked();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700497 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700498 item.setChecked(mShowWifi);
499 updateTabs();
500 return true;
501 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700502 case R.id.data_usage_menu_show_ethernet: {
503 mShowEthernet = !item.isChecked();
504 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
505 item.setChecked(mShowEthernet);
506 updateTabs();
507 return true;
508 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700509 }
510 return false;
511 }
512
Jeff Sharkey94a90952011-06-13 22:31:09 -0700513 @Override
514 public void onDestroyView() {
515 super.onDestroyView();
516
517 mDataEnabledView = null;
518 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700519
520 mUidDetailProvider.clearCache();
521 mUidDetailProvider = null;
Jeff Sharkey94a90952011-06-13 22:31:09 -0700522 }
523
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800524 @Override
525 public void onDestroy() {
526 if (this.isRemoving()) {
527 getFragmentManager()
528 .popBackStack(TAG_APP_DETAILS, FragmentManager.POP_BACK_STACK_INCLUSIVE);
529 }
530 super.onDestroy();
531 }
532
Jeff Sharkey8a503642011-06-10 13:31:21 -0700533 /**
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700534 * Listener to setup {@link LayoutTransition} after first layout pass.
535 */
536 private OnGlobalLayoutListener mFirstLayoutListener = new OnGlobalLayoutListener() {
537 /** {@inheritDoc} */
538 public void onGlobalLayout() {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700539 mListView.getViewTreeObserver().removeGlobalOnLayoutListener(mFirstLayoutListener);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700540
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700541 mTabsContainer.setLayoutTransition(buildLayoutTransition());
542 mHeader.setLayoutTransition(buildLayoutTransition());
543 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700544
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700545 final LayoutTransition chartTransition = buildLayoutTransition();
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700546 chartTransition.setStartDelay(LayoutTransition.APPEARING, 0);
547 chartTransition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700548 chartTransition.setAnimator(LayoutTransition.APPEARING, null);
Jeff Sharkey28130d92011-09-02 16:10:24 -0700549 chartTransition.setAnimator(LayoutTransition.DISAPPEARING, null);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700550 mChart.setLayoutTransition(chartTransition);
551 }
552 };
553
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700554 private static LayoutTransition buildLayoutTransition() {
555 final LayoutTransition transition = new LayoutTransition();
556 if (TEST_ANIM) {
557 transition.setDuration(1500);
558 }
559 transition.setAnimateParentHierarchy(false);
560 return transition;
561 }
562
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700563 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700564 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700565 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
566 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700567 */
568 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700569 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700570 mTabHost.clearAllTabs();
571
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700572 final boolean mobileSplit = isMobilePolicySplit();
573 if (mobileSplit && hasMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700574 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
575 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700576 } else if (hasMobileRadio(context)) {
577 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700578 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700579 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700580 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
581 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700582 if (mShowEthernet && hasEthernet(context)) {
583 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
584 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700585
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700586 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
587 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
588 if (mIntentTab != null) {
589 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700590 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700591 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700592 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700593 mTabHost.setCurrentTabByTag(mIntentTab);
594 }
595 mIntentTab = null;
596 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700597 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700598 }
599 }
600
Jeff Sharkey8a503642011-06-10 13:31:21 -0700601 /**
602 * Factory that provide empty {@link View} to make {@link TabHost} happy.
603 */
604 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
605 /** {@inheritDoc} */
606 public View createTabContent(String tag) {
607 return new View(mTabHost.getContext());
608 }
609 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700610
Jeff Sharkey8a503642011-06-10 13:31:21 -0700611 /**
612 * Build {@link TabSpec} with thin indicator, and empty content.
613 */
614 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700615 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
616 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700617 }
618
619 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
620 /** {@inheritDoc} */
621 public void onTabChanged(String tabId) {
622 // user changed tab; update body
623 updateBody();
624 }
625 };
626
627 /**
628 * Update body content based on current tab. Loads
629 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
630 * binds them to visible controls.
631 */
632 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700633 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700634 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700635
Jeff Sharkeya662e492011-06-18 21:57:06 -0700636 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700637 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700638
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700639 if (currentTab == null) {
640 Log.w(TAG, "no tab selected; hiding body");
641 mListView.setVisibility(View.GONE);
642 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700643 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700644 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700645 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700646
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700647 final boolean tabChanged = !currentTab.equals(mCurrentTab);
648 mCurrentTab = currentTab;
649
Jeff Sharkey8a503642011-06-10 13:31:21 -0700650 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
651
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700652 mDataEnabledView.setVisibility(View.VISIBLE);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700653
654 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700655 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
656 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700657 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700658
659 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700660 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
661 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
662 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700663 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700664
665 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700666 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
667 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
668 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700669 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700670
671 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700672 // wifi doesn't have any controls
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700673 mDataEnabledView.setVisibility(View.GONE);
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700674 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700675 mTemplate = buildTemplateWifi();
676
677 } else if (TAB_ETHERNET.equals(currentTab)) {
678 // ethernet doesn't have any controls
679 mDataEnabledView.setVisibility(View.GONE);
680 mDisableAtLimitView.setVisibility(View.GONE);
681 mTemplate = buildTemplateEthernet();
682
683 } else {
684 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700685 }
686
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700687 // kick off loader for network history
688 // TODO: consider chaining two loaders together instead of reloading
689 // network history when showing app detail.
690 getLoaderManager().restartLoader(LOADER_CHART_DATA,
691 ChartDataLoader.buildArgs(mTemplate, mAppDetailUids), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700692
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700693 // detail mode can change visible menus, invalidate
694 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700695
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700696 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700697 }
698
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700699 private boolean isAppDetailMode() {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700700 return mAppDetailUids != null;
701 }
702
703 private int getAppDetailPrimaryUid() {
704 return mAppDetailUids[0];
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700705 }
706
707 /**
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700708 * Update UID details panels to match {@link #mAppDetailUids}, showing or
709 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700710 */
711 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700712 final Context context = getActivity();
713 final PackageManager pm = context.getPackageManager();
714 final LayoutInflater inflater = getActivity().getLayoutInflater();
715
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700716 if (isAppDetailMode()) {
717 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700718 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700719 } else {
720 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700721 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700722
723 // hide detail stats when not in detail mode
724 mChart.bindDetailNetworkStats(null);
725 return;
726 }
727
728 // remove warning/limit sweeps while in detail mode
729 mChart.bindNetworkPolicy(null);
730
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700731 // show icon and all labels appearing under this app
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700732 final int primaryUid = getAppDetailPrimaryUid();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700733 final UidDetail detail = mUidDetailProvider.getUidDetail(primaryUid, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700734 mAppIcon.setImageDrawable(detail.icon);
735
736 mAppTitles.removeAllViews();
737 if (detail.detailLabels != null) {
738 for (CharSequence label : detail.detailLabels) {
739 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label));
740 }
741 } else {
742 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label));
743 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700744
745 // enable settings button when package provides it
746 // TODO: target torwards entire UID instead of just first package
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700747 final String[] packageNames = pm.getPackagesForUid(primaryUid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700748 if (packageNames != null && packageNames.length > 0) {
749 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
750 mAppSettingsIntent.setPackage(packageNames[0]);
751 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
752
753 final boolean matchFound = pm.resolveActivity(mAppSettingsIntent, 0) != null;
754 mAppSettings.setEnabled(matchFound);
755
756 } else {
757 mAppSettingsIntent = null;
758 mAppSettings.setEnabled(false);
759 }
760
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700761 updateDetailData();
762
763 if (NetworkPolicyManager.isUidValidForPolicy(context, primaryUid)
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800764 && !getRestrictBackground() && isBandwidthControlEnabled()
765 && hasMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700766 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey461842a2011-09-25 18:22:48 -0700767 if (hasLimitedNetworks()) {
768 setPreferenceSummary(mAppRestrictView,
769 getString(R.string.data_usage_app_restrict_background_summary));
770 } else {
771 setPreferenceSummary(mAppRestrictView,
772 getString(R.string.data_usage_app_restrict_background_summary_disabled));
773 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700774
775 mAppRestrictView.setVisibility(View.VISIBLE);
776 mAppRestrict.setChecked(getAppRestrictBackground());
777
778 } else {
779 mAppRestrictView.setVisibility(View.GONE);
780 }
781 }
782
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700783 private void setPolicyWarningBytes(long warningBytes) {
784 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700785 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700786 updatePolicy(false);
787 }
788
789 private void setPolicyLimitBytes(long limitBytes) {
790 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700791 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700792 updatePolicy(false);
793 }
794
Jeff Sharkey28130d92011-09-02 16:10:24 -0700795 /**
796 * Local cache of value, used to work around delay when
797 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
798 */
799 private Boolean mMobileDataEnabled;
800
801 private boolean isMobileDataEnabled() {
802 if (mMobileDataEnabled != null) {
803 // TODO: deprecate and remove this once enabled flag is on policy
804 return mMobileDataEnabled;
805 } else {
806 return mConnService.getMobileDataEnabled();
807 }
808 }
809
810 private void setMobileDataEnabled(boolean enabled) {
811 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
812 mConnService.setMobileDataEnabled(enabled);
813 mMobileDataEnabled = enabled;
814 updatePolicy(false);
815 }
816
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700817 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
818 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked();
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700819 }
820
821 private boolean isBandwidthControlEnabled() {
822 try {
823 return mNetworkService.isBandwidthControlEnabled();
824 } catch (RemoteException e) {
825 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
826 return false;
827 }
828 }
829
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700830 private boolean getDataRoaming() {
831 final ContentResolver resolver = getActivity().getContentResolver();
832 return Settings.Secure.getInt(resolver, Settings.Secure.DATA_ROAMING, 0) != 0;
833 }
834
835 private void setDataRoaming(boolean enabled) {
836 // TODO: teach telephony DataConnectionTracker to watch and apply
837 // updates when changed.
838 final ContentResolver resolver = getActivity().getContentResolver();
839 Settings.Secure.putInt(resolver, Settings.Secure.DATA_ROAMING, enabled ? 1 : 0);
840 mMenuDataRoaming.setChecked(enabled);
841 }
842
843 private boolean getRestrictBackground() {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700844 try {
845 return mPolicyService.getRestrictBackground();
846 } catch (RemoteException e) {
847 Log.w(TAG, "problem talking with policy service: " + e);
848 return false;
849 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700850 }
851
852 private void setRestrictBackground(boolean restrictBackground) {
853 if (LOGD) Log.d(TAG, "setRestrictBackground()");
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700854 try {
855 mPolicyService.setRestrictBackground(restrictBackground);
856 mMenuRestrictBackground.setChecked(restrictBackground);
857 } catch (RemoteException e) {
858 Log.w(TAG, "problem talking with policy service: " + e);
859 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700860 }
861
862 private boolean getAppRestrictBackground() {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700863 final int primaryUid = getAppDetailPrimaryUid();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700864 final int uidPolicy;
865 try {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700866 uidPolicy = mPolicyService.getUidPolicy(primaryUid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700867 } catch (RemoteException e) {
868 // since we can't do much without policy, we bail hard.
869 throw new RuntimeException("problem reading network policy", e);
870 }
871
872 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
873 }
874
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700875 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700876 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700877 final int primaryUid = getAppDetailPrimaryUid();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700878 try {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700879 mPolicyService.setUidPolicy(primaryUid,
880 restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700881 } catch (RemoteException e) {
882 throw new RuntimeException("unable to save policy", e);
883 }
884
885 mAppRestrict.setChecked(restrictBackground);
886 }
887
Jeff Sharkey8a503642011-06-10 13:31:21 -0700888 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700889 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
890 * current {@link #mTemplate}.
891 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700892 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700893 if (isAppDetailMode()) {
894 mNetworkSwitches.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700895 } else {
896 mNetworkSwitches.setVisibility(View.VISIBLE);
897 }
898
Jeff Sharkey28130d92011-09-02 16:10:24 -0700899 // TODO: move enabled state directly into policy
900 if (TAB_MOBILE.equals(mCurrentTab)) {
901 mBinding = true;
902 mDataEnabled.setChecked(isMobileDataEnabled());
903 mBinding = false;
904 }
905
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700906 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
907 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700908 mDisableAtLimitView.setVisibility(View.VISIBLE);
909 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700910 if (!isAppDetailMode()) {
911 mChart.bindNetworkPolicy(policy);
912 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700913
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700914 } else {
915 // controls are disabled; don't bind warning/limit sweeps
916 mDisableAtLimitView.setVisibility(View.GONE);
917 mChart.bindNetworkPolicy(null);
918 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700919
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700920 if (refreshCycle) {
921 // generate cycle list based on policy and available history
922 updateCycleList(policy);
923 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700924 }
925
926 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700927 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
928 * and available {@link NetworkStatsHistory} data. Always selects the newest
929 * item, updating the inspection range on {@link #mChart}.
930 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700931 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700932 // stash away currently selected cycle to try restoring below
933 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700934 mCycleAdapter.clear();
935
936 final Context context = mCycleSpinner.getContext();
937
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700938 long historyStart = Long.MAX_VALUE;
939 long historyEnd = Long.MIN_VALUE;
940 if (mChartData != null) {
941 historyStart = mChartData.network.getStart();
942 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700943 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700944
Jeff Sharkey461842a2011-09-25 18:22:48 -0700945 final long now = System.currentTimeMillis();
946 if (historyStart == Long.MAX_VALUE) historyStart = now;
947 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700948
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700949 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700950 if (policy != null) {
951 // find the next cycle boundary
952 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700953
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700954 // walk backwards, generating all valid cycle ranges
955 while (cycleEnd > historyStart) {
956 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
957 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
958 + historyStart);
959 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
960 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700961 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700962 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700963
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700964 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700965 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700966 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700967
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700968 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700969 // no policy defined cycles; show entry for each four-week period
970 long cycleEnd = historyEnd;
971 while (cycleEnd > historyStart) {
972 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
973 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
974 cycleEnd = cycleStart;
975 }
976
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700977 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700978 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700979
980 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700981 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700982 final int position = mCycleAdapter.findNearestPosition(previousItem);
983 mCycleSpinner.setSelection(position);
984
985 // only force-update cycle when changed; skipping preserves any
986 // user-defined inspection region.
987 final CycleItem selectedItem = mCycleAdapter.getItem(position);
988 if (!Objects.equal(selectedItem, previousItem)) {
989 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
990 } else {
991 // but still kick off loader for detailed list
992 updateDetailData();
993 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700994 } else {
995 updateDetailData();
996 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700997 }
998
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700999 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001000 /** {@inheritDoc} */
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001001 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
1002 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001003
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001004 final boolean dataEnabled = isChecked;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001005 final String currentTab = mCurrentTab;
1006 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001007 if (dataEnabled) {
1008 setMobileDataEnabled(true);
1009 } else {
1010 // disabling data; show confirmation dialog which eventually
1011 // calls setMobileDataEnabled() once user confirms.
1012 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1013 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001014 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001015
Jeff Sharkey28130d92011-09-02 16:10:24 -07001016 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001017 }
1018 };
1019
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001020 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001021 /** {@inheritDoc} */
1022 public void onClick(View v) {
1023 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001024 if (disableAtLimit) {
1025 // enabling limit; show confirmation dialog which eventually
1026 // calls setPolicyLimitBytes() once user confirms.
1027 ConfirmLimitFragment.show(DataUsageSummary.this);
1028 } else {
1029 setPolicyLimitBytes(LIMIT_DISABLED);
1030 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001031 }
1032 };
1033
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001034 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
1035 /** {@inheritDoc} */
1036 public void onClick(View v) {
1037 final boolean restrictBackground = !mAppRestrict.isChecked();
1038
1039 if (restrictBackground) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001040 if (hasLimitedNetworks()) {
1041 // enabling restriction; show confirmation dialog which
1042 // eventually calls setRestrictBackground() once user
1043 // confirms.
1044 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
1045 } else {
1046 // no limited networks; show dialog to guide user towards
1047 // setting a network limit. doesn't mutate restrict state.
1048 DeniedRestrictFragment.show(DataUsageSummary.this);
1049 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001050 } else {
1051 setAppRestrictBackground(false);
1052 }
1053 }
1054 };
1055
1056 private OnClickListener mAppSettingsListener = new OnClickListener() {
1057 /** {@inheritDoc} */
1058 public void onClick(View v) {
1059 // TODO: target torwards entire UID instead of just first package
1060 startActivity(mAppSettingsIntent);
1061 }
1062 };
1063
Jeff Sharkey8a503642011-06-10 13:31:21 -07001064 private OnItemClickListener mListListener = new OnItemClickListener() {
1065 /** {@inheritDoc} */
1066 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001067 final Context context = view.getContext();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001068 final AppUsageItem app = (AppUsageItem) parent.getItemAtPosition(position);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001069 final UidDetail detail = mUidDetailProvider.getUidDetail(app.uids[0], true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001070 AppDetailsFragment.show(DataUsageSummary.this, app.uids, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001071 }
1072 };
1073
1074 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
1075 /** {@inheritDoc} */
1076 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1077 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1078 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001079 // show cycle editor; will eventually call setPolicyCycleDay()
1080 // when user finishes editing.
1081 CycleEditorFragment.show(DataUsageSummary.this);
1082
1083 // reset spinner to something other than "change cycle..."
1084 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001085
1086 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001087 if (LOGD) {
1088 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1089 + cycle.end + "]");
1090 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001091
1092 // update chart to show selected cycle, and update detail data
1093 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001094 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001095
1096 updateDetailData();
1097 }
1098 }
1099
1100 /** {@inheritDoc} */
1101 public void onNothingSelected(AdapterView<?> parent) {
1102 // ignored
1103 }
1104 };
1105
1106 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001107 * Update details based on {@link #mChart} inspection range depending on
1108 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1109 * of applications data usage, and when {@link #isAppDetailMode()} update
1110 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001111 */
1112 private void updateDetailData() {
1113 if (LOGD) Log.d(TAG, "updateDetailData()");
1114
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001115 final long start = mChart.getInspectStart();
1116 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001117 final long now = System.currentTimeMillis();
1118
1119 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001120
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001121 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001122 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001123 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001124 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001125 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001126 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001127 final long foregroundBytes = entry.rxBytes + entry.txBytes;
1128
1129 mAppPieChart.setOriginAngle(175);
1130
1131 mAppPieChart.removeAllSlices();
1132 mAppPieChart.addSlice(foregroundBytes, Color.parseColor("#d88d3a"));
1133 mAppPieChart.addSlice(defaultBytes, Color.parseColor("#666666"));
1134
1135 mAppPieChart.generatePath();
1136
1137 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1138 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1139
1140 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001141 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001142
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001143 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001144
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001145 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001146 if (mChartData != null) {
1147 entry = mChartData.network.getValues(start, end, now, null);
1148 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001149
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001150 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001151 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001152 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001153 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001154
1155 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1156 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001157 final String rangePhrase = formatDateRange(context, start, end, false);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001158
1159 mUsageSummary.setText(
1160 getString(R.string.data_usage_total_during_range, totalPhrase, rangePhrase));
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001161 }
1162
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001163 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1164 ChartData>() {
1165 /** {@inheritDoc} */
1166 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
1167 return new ChartDataLoader(getActivity(), mStatsService, args);
1168 }
1169
1170 /** {@inheritDoc} */
1171 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1172 mChartData = data;
1173 mChart.bindNetworkStats(mChartData.network);
1174 mChart.bindDetailNetworkStats(mChartData.detail);
1175
1176 // calcuate policy cycles based on available data
1177 updatePolicy(true);
1178 updateAppDetail();
1179
1180 // force scroll to top of body when showing detail
1181 if (mChartData.detail != null) {
1182 mListView.smoothScrollToPosition(0);
1183 }
1184 }
1185
1186 /** {@inheritDoc} */
1187 public void onLoaderReset(Loader<ChartData> loader) {
1188 mChartData = null;
1189 mChart.bindNetworkStats(null);
1190 mChart.bindDetailNetworkStats(null);
1191 }
1192 };
1193
1194 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001195 NetworkStats>() {
1196 /** {@inheritDoc} */
1197 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
1198 return new SummaryForAllUidLoader(getActivity(), mStatsService, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001199 }
1200
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001201 /** {@inheritDoc} */
1202 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
1203 mAdapter.bindStats(data);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001204 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001205 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001206
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001207 /** {@inheritDoc} */
1208 public void onLoaderReset(Loader<NetworkStats> loader) {
1209 mAdapter.bindStats(null);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001210 updateEmptyVisible();
1211 }
1212
1213 private void updateEmptyVisible() {
1214 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1215 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001216 }
1217 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001218
Jeff Sharkeya662e492011-06-18 21:57:06 -07001219 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001220 final Context context = getActivity();
1221 if (hasMobileRadio(context)) {
1222 final String subscriberId = getActiveSubscriberId(context);
1223 return mPolicyEditor.isMobilePolicySplit(subscriberId);
1224 } else {
1225 return false;
1226 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001227 }
1228
1229 private void setMobilePolicySplit(boolean split) {
1230 final String subscriberId = getActiveSubscriberId(getActivity());
1231 mPolicyEditor.setMobilePolicySplit(subscriberId, split);
1232 }
1233
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001234 private static String getActiveSubscriberId(Context context) {
1235 final TelephonyManager telephony = (TelephonyManager) context.getSystemService(
1236 Context.TELEPHONY_SERVICE);
1237 return telephony.getSubscriberId();
1238 }
1239
Jeff Sharkey8a503642011-06-10 13:31:21 -07001240 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
1241 /** {@inheritDoc} */
1242 public void onInspectRangeChanged() {
1243 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
1244 updateDetailData();
1245 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001246
Jeff Sharkey8a503642011-06-10 13:31:21 -07001247 /** {@inheritDoc} */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001248 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001249 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001250 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001251
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001252 /** {@inheritDoc} */
1253 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001254 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001255 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001256
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001257 /** {@inheritDoc} */
1258 public void requestWarningEdit() {
1259 WarningEditorFragment.show(DataUsageSummary.this);
1260 }
1261
1262 /** {@inheritDoc} */
1263 public void requestLimitEdit() {
1264 LimitEditorFragment.show(DataUsageSummary.this);
1265 }
1266 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001267
1268 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001269 * List item that reflects a specific data usage cycle.
1270 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001271 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001272 public CharSequence label;
1273 public long start;
1274 public long end;
1275
Jeff Sharkey8a503642011-06-10 13:31:21 -07001276 CycleItem(CharSequence label) {
1277 this.label = label;
1278 }
1279
1280 public CycleItem(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001281 this.label = formatDateRange(context, start, end, true);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001282 this.start = start;
1283 this.end = end;
1284 }
1285
Jeff Sharkey8a503642011-06-10 13:31:21 -07001286 @Override
1287 public String toString() {
1288 return label.toString();
1289 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001290
1291 @Override
1292 public boolean equals(Object o) {
1293 if (o instanceof CycleItem) {
1294 final CycleItem another = (CycleItem) o;
1295 return start == another.start && end == another.end;
1296 }
1297 return false;
1298 }
1299
1300 /** {@inheritDoc} */
1301 public int compareTo(CycleItem another) {
1302 return Long.compare(start, another.start);
1303 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001304 }
1305
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001306 private static final StringBuilder sBuilder = new StringBuilder(50);
1307 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1308 sBuilder, Locale.getDefault());
1309
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001310 public static String formatDateRange(Context context, long start, long end, boolean utcTime) {
1311 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
1312 final String timezone = utcTime ? TIMEZONE_UTC : null;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001313
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001314 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001315 sBuilder.setLength(0);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001316 return DateUtils
1317 .formatDateRange(context, sFormatter, start, end, flags, timezone).toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001318 }
1319 }
1320
Jeff Sharkey8a503642011-06-10 13:31:21 -07001321 /**
1322 * Special-case data usage cycle that triggers dialog to change
1323 * {@link NetworkPolicy#cycleDay}.
1324 */
1325 public static class CycleChangeItem extends CycleItem {
1326 public CycleChangeItem(Context context) {
1327 super(context.getString(R.string.data_usage_change_cycle));
1328 }
1329 }
1330
1331 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001332 private boolean mChangePossible = false;
1333 private boolean mChangeVisible = false;
1334
1335 private final CycleChangeItem mChangeItem;
1336
Jeff Sharkey8a503642011-06-10 13:31:21 -07001337 public CycleAdapter(Context context) {
1338 super(context, android.R.layout.simple_spinner_item);
1339 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001340 mChangeItem = new CycleChangeItem(context);
1341 }
1342
1343 public void setChangePossible(boolean possible) {
1344 mChangePossible = possible;
1345 updateChange();
1346 }
1347
1348 public void setChangeVisible(boolean visible) {
1349 mChangeVisible = visible;
1350 updateChange();
1351 }
1352
1353 private void updateChange() {
1354 remove(mChangeItem);
1355 if (mChangePossible && mChangeVisible) {
1356 add(mChangeItem);
1357 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001358 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001359
1360 /**
1361 * Find position of {@link CycleItem} in this adapter which is nearest
1362 * the given {@link CycleItem}.
1363 */
1364 public int findNearestPosition(CycleItem target) {
1365 if (target != null) {
1366 final int count = getCount();
1367 for (int i = count - 1; i >= 0; i--) {
1368 final CycleItem item = getItem(i);
1369 if (item instanceof CycleChangeItem) {
1370 continue;
1371 } else if (item.compareTo(target) >= 0) {
1372 return i;
1373 }
1374 }
1375 }
1376 return 0;
1377 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001378 }
1379
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001380 private static class AppUsageItem implements Comparable<AppUsageItem> {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001381 public int[] uids;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001382 public long total;
1383
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001384 public AppUsageItem(int uid) {
1385 uids = new int[] { uid };
1386 }
1387
1388 public void addUid(int uid) {
1389 if (contains(uids, uid)) return;
1390 final int length = uids.length;
1391 uids = Arrays.copyOf(uids, length + 1);
1392 uids[length] = uid;
1393 }
1394
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001395 /** {@inheritDoc} */
1396 public int compareTo(AppUsageItem another) {
1397 return Long.compare(another.total, total);
1398 }
1399 }
1400
Jeff Sharkey8a503642011-06-10 13:31:21 -07001401 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001402 * Adapter of applications, sorted by total usage descending.
1403 */
1404 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001405 private final UidDetailProvider mProvider;
1406 private final int mInsetSide;
1407
Jeff Sharkey8a503642011-06-10 13:31:21 -07001408 private ArrayList<AppUsageItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001409 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001410
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001411 public DataUsageAdapter(UidDetailProvider provider, int insetSide) {
1412 mProvider = checkNotNull(provider);
1413 mInsetSide = insetSide;
1414 }
1415
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001416 /**
1417 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1418 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001419 public void bindStats(NetworkStats stats) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001420 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001421
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001422 final AppUsageItem systemItem = new AppUsageItem(android.os.Process.SYSTEM_UID);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001423 final SparseArray<AppUsageItem> knownUids = new SparseArray<AppUsageItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001424
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001425 NetworkStats.Entry entry = null;
1426 final int size = stats != null ? stats.size() : 0;
1427 for (int i = 0; i < size; i++) {
1428 entry = stats.getValues(i, entry);
1429
1430 final int uid = entry.uid;
1431 final boolean isApp = uid >= android.os.Process.FIRST_APPLICATION_UID
1432 && uid <= android.os.Process.LAST_APPLICATION_UID;
Jeff Sharkeya83a24f2011-09-16 01:52:39 -07001433 if (isApp || uid == UID_REMOVED || uid == UID_TETHERING) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001434 AppUsageItem item = knownUids.get(uid);
1435 if (item == null) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001436 item = new AppUsageItem(uid);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001437 knownUids.put(uid, item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001438 mItems.add(item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001439 }
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001440
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001441 item.total += entry.rxBytes + entry.txBytes;
1442 } else {
1443 systemItem.total += entry.rxBytes + entry.txBytes;
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001444 systemItem.addUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001445 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001446 }
1447
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001448 if (systemItem.total > 0) {
1449 mItems.add(systemItem);
1450 }
1451
Jeff Sharkey8a503642011-06-10 13:31:21 -07001452 Collections.sort(mItems);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001453 mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001454 notifyDataSetChanged();
1455 }
1456
1457 @Override
1458 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001459 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001460 }
1461
1462 @Override
1463 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001464 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001465 }
1466
1467 @Override
1468 public long getItemId(int position) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001469 return mItems.get(position).uids[0];
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001470 }
1471
1472 @Override
1473 public View getView(int position, View convertView, ViewGroup parent) {
1474 if (convertView == null) {
1475 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey5d706792011-09-08 18:57:17 -07001476 R.layout.data_usage_item, parent, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001477
1478 if (mInsetSide > 0) {
1479 convertView.setPadding(mInsetSide, 0, mInsetSide, 0);
1480 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001481 }
1482
1483 final Context context = parent.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001484
Jeff Sharkey28130d92011-09-02 16:10:24 -07001485 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001486 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1487 android.R.id.progress);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001488
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001489 // kick off async load of app details
Jeff Sharkey8a503642011-06-10 13:31:21 -07001490 final AppUsageItem item = mItems.get(position);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001491 UidDetailTask.bindView(mProvider, item, convertView);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001492
Jeff Sharkey28130d92011-09-02 16:10:24 -07001493 text1.setText(Formatter.formatFileSize(context, item.total));
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001494
1495 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1496 progress.setProgress(percentTotal);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001497
1498 return convertView;
1499 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001500 }
1501
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001502 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001503 * Empty {@link Fragment} that controls display of UID details in
1504 * {@link DataUsageSummary}.
1505 */
1506 public static class AppDetailsFragment extends Fragment {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001507 private static final String EXTRA_UIDS = "uids";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001508
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001509 public static void show(DataUsageSummary parent, int[] uids, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001510 if (!parent.isAdded()) return;
1511
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001512 final Bundle args = new Bundle();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001513 args.putIntArray(EXTRA_UIDS, uids);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001514
1515 final AppDetailsFragment fragment = new AppDetailsFragment();
1516 fragment.setArguments(args);
1517 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001518 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1519 ft.add(fragment, TAG_APP_DETAILS);
1520 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001521 ft.setBreadCrumbTitle(label);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001522 ft.commit();
1523 }
1524
1525 @Override
1526 public void onStart() {
1527 super.onStart();
1528 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001529 target.mAppDetailUids = getArguments().getIntArray(EXTRA_UIDS);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001530 target.updateBody();
1531 }
1532
1533 @Override
1534 public void onStop() {
1535 super.onStop();
1536 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001537 target.mAppDetailUids = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001538 target.updateBody();
1539 }
1540 }
1541
1542 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001543 * Dialog to request user confirmation before setting
1544 * {@link NetworkPolicy#limitBytes}.
1545 */
1546 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001547 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001548 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001549
1550 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001551 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001552
Jeff Sharkey461842a2011-09-25 18:22:48 -07001553 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001554 final CharSequence message;
1555 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001556
1557 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001558 final String currentTab = parent.mCurrentTab;
1559 if (TAB_3G.equals(currentTab)) {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001560 message = buildDialogMessage(res, R.string.data_usage_tab_3g);
1561 limitBytes = 5 * GB_IN_BYTES;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001562 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001563 message = buildDialogMessage(res, R.string.data_usage_tab_4g);
1564 limitBytes = 5 * GB_IN_BYTES;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001565 } else if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001566 message = buildDialogMessage(res, R.string.data_usage_list_mobile);
1567 limitBytes = 5 * GB_IN_BYTES;
1568 } else if (TAB_WIFI.equals(currentTab)) {
1569 message = buildDialogMessage(res, R.string.data_usage_tab_wifi);
1570 limitBytes = 5 * GB_IN_BYTES;
1571 } else {
1572 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001573 }
1574
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001575 final Bundle args = new Bundle();
1576 args.putCharSequence(EXTRA_MESSAGE, message);
1577 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1578
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001579 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1580 dialog.setArguments(args);
1581 dialog.setTargetFragment(parent, 0);
1582 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1583 }
1584
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001585 private static CharSequence buildDialogMessage(Resources res, int networkResId) {
1586 return res.getString(R.string.data_usage_limit_dialog, res.getString(networkResId));
1587 }
1588
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001589 @Override
1590 public Dialog onCreateDialog(Bundle savedInstanceState) {
1591 final Context context = getActivity();
1592
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001593 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001594 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1595
1596 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1597 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001598 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001599
1600 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1601 public void onClick(DialogInterface dialog, int which) {
1602 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1603 if (target != null) {
1604 target.setPolicyLimitBytes(limitBytes);
1605 }
1606 }
1607 });
1608
1609 return builder.create();
1610 }
1611 }
1612
1613 /**
1614 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1615 */
1616 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001617 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001618
1619 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001620 if (!parent.isAdded()) return;
1621
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001622 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001623 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001624
1625 final CycleEditorFragment dialog = new CycleEditorFragment();
1626 dialog.setArguments(args);
1627 dialog.setTargetFragment(parent, 0);
1628 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1629 }
1630
1631 @Override
1632 public Dialog onCreateDialog(Bundle savedInstanceState) {
1633 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001634 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1635 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001636
1637 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1638 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1639
1640 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1641 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1642
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001643 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1644 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001645
1646 cycleDayPicker.setMinValue(1);
1647 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001648 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001649 cycleDayPicker.setWrapSelectorWheel(true);
1650
1651 builder.setTitle(R.string.data_usage_cycle_editor_title);
1652 builder.setView(view);
1653
1654 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1655 new DialogInterface.OnClickListener() {
1656 public void onClick(DialogInterface dialog, int which) {
1657 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001658 editor.setPolicyCycleDay(template, cycleDay);
1659 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001660 }
1661 });
1662
1663 return builder.create();
1664 }
1665 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001666
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001667 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001668 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1669 */
1670 public static class WarningEditorFragment extends DialogFragment {
1671 private static final String EXTRA_TEMPLATE = "template";
1672
1673 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001674 if (!parent.isAdded()) return;
1675
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001676 final Bundle args = new Bundle();
1677 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1678
1679 final WarningEditorFragment dialog = new WarningEditorFragment();
1680 dialog.setArguments(args);
1681 dialog.setTargetFragment(parent, 0);
1682 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1683 }
1684
1685 @Override
1686 public Dialog onCreateDialog(Bundle savedInstanceState) {
1687 final Context context = getActivity();
1688 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1689 final NetworkPolicyEditor editor = target.mPolicyEditor;
1690
1691 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1692 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1693
1694 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1695 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1696
1697 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1698 final long warningBytes = editor.getPolicyWarningBytes(template);
1699 final long limitBytes = editor.getPolicyLimitBytes(template);
1700
1701 bytesPicker.setMinValue(0);
1702 if (limitBytes != LIMIT_DISABLED) {
1703 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1704 } else {
1705 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1706 }
1707 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1708 bytesPicker.setWrapSelectorWheel(false);
1709
1710 builder.setTitle(R.string.data_usage_warning_editor_title);
1711 builder.setView(view);
1712
1713 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1714 new DialogInterface.OnClickListener() {
1715 public void onClick(DialogInterface dialog, int which) {
1716 // clear focus to finish pending text edits
1717 bytesPicker.clearFocus();
1718
1719 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1720 editor.setPolicyWarningBytes(template, bytes);
1721 target.updatePolicy(false);
1722 }
1723 });
1724
1725 return builder.create();
1726 }
1727 }
1728
1729 /**
1730 * Dialog to edit {@link NetworkPolicy#limitBytes}.
1731 */
1732 public static class LimitEditorFragment extends DialogFragment {
1733 private static final String EXTRA_TEMPLATE = "template";
1734
1735 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001736 if (!parent.isAdded()) return;
1737
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001738 final Bundle args = new Bundle();
1739 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1740
1741 final LimitEditorFragment dialog = new LimitEditorFragment();
1742 dialog.setArguments(args);
1743 dialog.setTargetFragment(parent, 0);
1744 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
1745 }
1746
1747 @Override
1748 public Dialog onCreateDialog(Bundle savedInstanceState) {
1749 final Context context = getActivity();
1750 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1751 final NetworkPolicyEditor editor = target.mPolicyEditor;
1752
1753 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1754 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1755
1756 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1757 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1758
1759 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1760 final long warningBytes = editor.getPolicyWarningBytes(template);
1761 final long limitBytes = editor.getPolicyLimitBytes(template);
1762
1763 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09001764 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001765 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
1766 } else {
1767 bytesPicker.setMinValue(0);
1768 }
1769 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
1770 bytesPicker.setWrapSelectorWheel(false);
1771
1772 builder.setTitle(R.string.data_usage_limit_editor_title);
1773 builder.setView(view);
1774
1775 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1776 new DialogInterface.OnClickListener() {
1777 public void onClick(DialogInterface dialog, int which) {
1778 // clear focus to finish pending text edits
1779 bytesPicker.clearFocus();
1780
1781 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1782 editor.setPolicyLimitBytes(template, bytes);
1783 target.updatePolicy(false);
1784 }
1785 });
1786
1787 return builder.create();
1788 }
1789 }
1790 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07001791 * Dialog to request user confirmation before disabling data.
1792 */
1793 public static class ConfirmDataDisableFragment extends DialogFragment {
1794 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001795 if (!parent.isAdded()) return;
1796
Jeff Sharkey28130d92011-09-02 16:10:24 -07001797 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
1798 dialog.setTargetFragment(parent, 0);
1799 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
1800 }
1801
1802 @Override
1803 public Dialog onCreateDialog(Bundle savedInstanceState) {
1804 final Context context = getActivity();
1805
1806 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1807 builder.setMessage(R.string.data_usage_disable_mobile);
1808
1809 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1810 public void onClick(DialogInterface dialog, int which) {
1811 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1812 if (target != null) {
1813 // TODO: extend to modify policy enabled flag.
1814 target.setMobileDataEnabled(false);
1815 }
1816 }
1817 });
1818 builder.setNegativeButton(android.R.string.cancel, null);
1819
1820 return builder.create();
1821 }
1822 }
1823
1824 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001825 * Dialog to request user confirmation before setting
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001826 * {@link Settings.Secure#DATA_ROAMING}.
1827 */
1828 public static class ConfirmDataRoamingFragment extends DialogFragment {
1829 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001830 if (!parent.isAdded()) return;
1831
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001832 final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment();
1833 dialog.setTargetFragment(parent, 0);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001834 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_ROAMING);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001835 }
1836
1837 @Override
1838 public Dialog onCreateDialog(Bundle savedInstanceState) {
1839 final Context context = getActivity();
1840
1841 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1842 builder.setTitle(R.string.roaming_reenable_title);
1843 builder.setMessage(R.string.roaming_warning);
1844
1845 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1846 public void onClick(DialogInterface dialog, int which) {
1847 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1848 if (target != null) {
1849 target.setDataRoaming(true);
1850 }
1851 }
1852 });
1853 builder.setNegativeButton(android.R.string.cancel, null);
1854
1855 return builder.create();
1856 }
1857 }
1858
1859 /**
1860 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001861 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001862 */
1863 public static class ConfirmRestrictFragment extends DialogFragment {
1864 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001865 if (!parent.isAdded()) return;
1866
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001867 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1868 dialog.setTargetFragment(parent, 0);
1869 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1870 }
1871
1872 @Override
1873 public Dialog onCreateDialog(Bundle savedInstanceState) {
1874 final Context context = getActivity();
1875
1876 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001877 builder.setTitle(R.string.data_usage_restrict_background_title);
Jeff Sharkey461842a2011-09-25 18:22:48 -07001878 builder.setMessage(getString(R.string.data_usage_restrict_background));
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001879
1880 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1881 public void onClick(DialogInterface dialog, int which) {
1882 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1883 if (target != null) {
1884 target.setRestrictBackground(true);
1885 }
1886 }
1887 });
1888 builder.setNegativeButton(android.R.string.cancel, null);
1889
1890 return builder.create();
1891 }
1892 }
1893
1894 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001895 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
1896 * change has been denied, usually based on
1897 * {@link DataUsageSummary#hasLimitedNetworks()}.
1898 */
1899 public static class DeniedRestrictFragment extends DialogFragment {
1900 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001901 if (!parent.isAdded()) return;
1902
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001903 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
1904 dialog.setTargetFragment(parent, 0);
1905 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
1906 }
1907
1908 @Override
1909 public Dialog onCreateDialog(Bundle savedInstanceState) {
1910 final Context context = getActivity();
1911
1912 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1913 builder.setTitle(R.string.data_usage_app_restrict_background);
1914 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
1915 builder.setPositiveButton(android.R.string.ok, null);
1916
1917 return builder.create();
1918 }
1919 }
1920
1921 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001922 * Dialog to request user confirmation before setting
1923 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
1924 */
1925 public static class ConfirmAppRestrictFragment extends DialogFragment {
1926 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001927 if (!parent.isAdded()) return;
1928
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001929 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
1930 dialog.setTargetFragment(parent, 0);
1931 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
1932 }
1933
1934 @Override
1935 public Dialog onCreateDialog(Bundle savedInstanceState) {
1936 final Context context = getActivity();
1937
1938 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001939 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
1940 builder.setMessage(R.string.data_usage_app_restrict_dialog);
1941
1942 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1943 public void onClick(DialogInterface dialog, int which) {
1944 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1945 if (target != null) {
1946 target.setAppRestrictBackground(true);
1947 }
1948 }
1949 });
1950 builder.setNegativeButton(android.R.string.cancel, null);
1951
1952 return builder.create();
1953 }
1954 }
1955
1956 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001957 * Compute default tab that should be selected, based on
1958 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
1959 */
1960 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07001961 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
1962 if (template == null) return null;
1963
1964 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07001965 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001966 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001967 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001968 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001969 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001970 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001971 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001972 return TAB_WIFI;
1973 default:
1974 return null;
1975 }
1976 }
1977
1978 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001979 * Background task that loads {@link UidDetail}, binding to
1980 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001981 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001982 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
1983 private final UidDetailProvider mProvider;
1984 private final AppUsageItem mItem;
1985 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001986
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001987 private UidDetailTask(UidDetailProvider provider, AppUsageItem item, View target) {
1988 mProvider = checkNotNull(provider);
1989 mItem = checkNotNull(item);
1990 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001991 }
1992
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001993 public static void bindView(
1994 UidDetailProvider provider, AppUsageItem item, View target) {
1995 final UidDetailTask existing = (UidDetailTask) target.getTag();
1996 if (existing != null) {
1997 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001998 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001999
2000 final UidDetail cachedDetail = provider.getUidDetail(item.uids[0], false);
2001 if (cachedDetail != null) {
2002 bindView(cachedDetail, target);
2003 } else {
2004 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2005 AsyncTask.THREAD_POOL_EXECUTOR));
2006 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002007 }
2008
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002009 private static void bindView(UidDetail detail, View target) {
2010 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2011 final TextView title = (TextView) target.findViewById(android.R.id.title);
2012
2013 if (detail != null) {
2014 icon.setImageDrawable(detail.icon);
2015 title.setText(detail.label);
2016 } else {
2017 icon.setImageDrawable(null);
2018 title.setText(null);
2019 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002020 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002021
2022 @Override
2023 protected void onPreExecute() {
2024 bindView(null, mTarget);
2025 }
2026
2027 @Override
2028 protected UidDetail doInBackground(Void... params) {
2029 return mProvider.getUidDetail(mItem.uids[0], true);
2030 }
2031
2032 @Override
2033 protected void onPostExecute(UidDetail result) {
2034 bindView(result, mTarget);
2035 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002036 }
2037
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002038 /**
2039 * Test if device has a mobile data radio.
2040 */
2041 private static boolean hasMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002042 if (TEST_RADIOS) {
2043 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2044 }
2045
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002046 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
2047 Context.CONNECTIVITY_SERVICE);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002048 return conn.isNetworkSupported(TYPE_MOBILE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002049 }
2050
2051 /**
2052 * Test if device has a mobile 4G data radio.
2053 */
2054 private static boolean hasMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002055 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2056 return false;
2057 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002058 if (TEST_RADIOS) {
2059 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2060 }
2061
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002062 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
2063 Context.CONNECTIVITY_SERVICE);
2064 final TelephonyManager telephony = (TelephonyManager) context.getSystemService(
2065 Context.TELEPHONY_SERVICE);
2066
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002067 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002068 final boolean hasLte = telephony.getLteOnCdmaMode() == Phone.LTE_ON_CDMA_TRUE;
2069 return hasWimax || hasLte;
2070 }
2071
2072 /**
2073 * Test if device has a Wi-Fi data radio.
2074 */
2075 private static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002076 if (TEST_RADIOS) {
2077 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2078 }
2079
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002080 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
2081 Context.CONNECTIVITY_SERVICE);
2082 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002083 }
2084
2085 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002086 * Test if device has an ethernet network connection.
2087 */
2088 private static boolean hasEthernet(Context context) {
2089 if (TEST_RADIOS) {
2090 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2091 }
2092
2093 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
2094 Context.CONNECTIVITY_SERVICE);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002095 return conn.isNetworkSupported(TYPE_ETHERNET);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002096 }
2097
2098 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002099 * Inflate a {@link Preference} style layout, adding the given {@link View}
2100 * widget into {@link android.R.id#widget_frame}.
2101 */
2102 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2103 final View view = inflater.inflate(R.layout.preference, root, false);
2104 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2105 android.R.id.widget_frame);
2106 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2107 return view;
2108 }
2109
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002110 private static View inflateAppTitle(
2111 LayoutInflater inflater, ViewGroup root, CharSequence label) {
2112 final TextView view = (TextView) inflater.inflate(
2113 R.layout.data_usage_app_title, root, false);
2114 view.setText(label);
2115 return view;
2116 }
2117
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002118 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002119 * Test if any networks are currently limited.
2120 */
2121 private boolean hasLimitedNetworks() {
2122 return !buildLimitedNetworksList().isEmpty();
2123 }
2124
2125 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002126 * Build string describing currently limited networks, which defines when
2127 * background data is restricted.
2128 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002129 private CharSequence buildLimitedNetworksString() {
2130 final List<CharSequence> limited = buildLimitedNetworksList();
2131
2132 // handle case where no networks limited
2133 if (limited.isEmpty()) {
2134 limited.add(getText(R.string.data_usage_list_none));
2135 }
2136
2137 return TextUtils.join(limited);
2138 }
2139
2140 /**
2141 * Build list of currently limited networks, which defines when background
2142 * data is restricted.
2143 */
2144 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002145 final Context context = getActivity();
2146 final String subscriberId = getActiveSubscriberId(context);
2147
2148 // build combined list of all limited networks
2149 final ArrayList<CharSequence> limited = Lists.newArrayList();
2150 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2151 limited.add(getText(R.string.data_usage_list_mobile));
2152 }
2153 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2154 limited.add(getText(R.string.data_usage_tab_3g));
2155 }
2156 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2157 limited.add(getText(R.string.data_usage_tab_4g));
2158 }
2159 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifi())) {
2160 limited.add(getText(R.string.data_usage_tab_wifi));
2161 }
2162 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2163 limited.add(getText(R.string.data_usage_tab_ethernet));
2164 }
2165
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002166 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002167 }
2168
2169 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002170 * Inset both selector and divider {@link Drawable} on the given
2171 * {@link ListView} by the requested dimensions.
2172 */
2173 private static void insetListViewDrawables(ListView view, int insetSide) {
2174 final Drawable selector = view.getSelector();
2175 final Drawable divider = view.getDivider();
2176
2177 // fully unregister these drawables so callbacks can be maintained after
2178 // wrapping below.
2179 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2180 view.setSelector(stub);
2181 view.setDivider(stub);
2182
2183 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2184 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2185 }
2186
2187 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002188 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002189 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002190 */
2191 private static void setPreferenceTitle(View parent, int resId) {
2192 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2193 title.setText(resId);
2194 }
2195
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002196 /**
2197 * Set {@link android.R.id#summary} for a preference view inflated with
2198 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2199 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002200 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002201 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2202 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002203 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002204 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07002205
2206 private static boolean contains(int[] haystack, int needle) {
2207 for (int value : haystack) {
2208 if (value == needle) {
2209 return true;
2210 }
2211 }
2212 return false;
2213 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002214}