blob: bbc3606ec267c4538aa3904e90dcb57a2c976880 [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;
21import static android.net.ConnectivityManager.TYPE_WIMAX;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070022import static android.net.NetworkPolicy.LIMIT_DISABLED;
Jeff Sharkeya53188f2011-09-13 19:56:45 -070023import static android.net.NetworkPolicy.WARNING_DISABLED;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070024import static android.net.NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070025import static android.net.NetworkPolicyManager.POLICY_NONE;
26import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND;
Jeff Sharkey8a503642011-06-10 13:31:21 -070027import static android.net.NetworkPolicyManager.computeLastCycleBoundary;
28import static android.net.NetworkPolicyManager.computeNextCycleBoundary;
Jeff Sharkeya662e492011-06-18 21:57:06 -070029import static android.net.NetworkTemplate.MATCH_MOBILE_3G_LOWER;
30import static android.net.NetworkTemplate.MATCH_MOBILE_4G;
31import static android.net.NetworkTemplate.MATCH_MOBILE_ALL;
32import static android.net.NetworkTemplate.MATCH_WIFI;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070033import static android.net.NetworkTemplate.buildTemplateEthernet;
34import static android.net.NetworkTemplate.buildTemplateMobile3gLower;
35import static android.net.NetworkTemplate.buildTemplateMobile4g;
36import static android.net.NetworkTemplate.buildTemplateMobileAll;
37import static android.net.NetworkTemplate.buildTemplateWifi;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070038import static android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
39import static android.text.format.DateUtils.FORMAT_SHOW_DATE;
40import static android.text.format.Time.TIMEZONE_UTC;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070041import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -070042import static com.android.internal.util.Preconditions.checkNotNull;
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -070043import static com.android.settings.Utils.prepareCustomPreferencesList;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070044
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070045import android.animation.LayoutTransition;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070046import android.app.AlertDialog;
47import android.app.Dialog;
48import android.app.DialogFragment;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070049import android.app.Fragment;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070050import android.app.FragmentTransaction;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070051import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070052import android.content.ContentResolver;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070053import android.content.Context;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070054import android.content.DialogInterface;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -070055import android.content.Intent;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070056import android.content.Loader;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070057import android.content.SharedPreferences;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070058import android.content.pm.PackageManager;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070059import android.content.res.Resources;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070060import android.graphics.Color;
Jeff Sharkey5d706792011-09-08 18:57:17 -070061import android.graphics.drawable.ColorDrawable;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070062import android.graphics.drawable.Drawable;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070063import android.net.ConnectivityManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070064import android.net.INetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070065import android.net.INetworkStatsService;
Jeff Sharkey8a503642011-06-10 13:31:21 -070066import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070067import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070068import android.net.NetworkStats;
69import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070070import android.net.NetworkTemplate;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070071import android.net.TrafficStats;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070072import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070073import android.os.Bundle;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070074import android.os.INetworkManagementService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070075import android.os.RemoteException;
76import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070077import android.os.SystemProperties;
Jeff Sharkey8a503642011-06-10 13:31:21 -070078import android.preference.Preference;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070079import android.provider.Settings;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070080import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070081import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070082import android.text.format.DateUtils;
83import android.text.format.Formatter;
84import android.util.Log;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070085import android.util.SparseArray;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070086import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -070087import android.view.Menu;
88import android.view.MenuInflater;
89import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070090import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070091import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070092import android.view.ViewGroup;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -070093import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Jeff Sharkey8a503642011-06-10 13:31:21 -070094import android.widget.AdapterView;
95import android.widget.AdapterView.OnItemClickListener;
96import android.widget.AdapterView.OnItemSelectedListener;
97import android.widget.ArrayAdapter;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070098import android.widget.BaseAdapter;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070099import android.widget.Button;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700100import android.widget.CheckBox;
101import android.widget.CompoundButton;
102import android.widget.CompoundButton.OnCheckedChangeListener;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700103import android.widget.ImageView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700104import android.widget.LinearLayout;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700105import android.widget.ListView;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700106import android.widget.NumberPicker;
Jeff Sharkey2412b0f2011-07-17 20:31:40 -0700107import android.widget.ProgressBar;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700108import android.widget.Spinner;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700109import android.widget.Switch;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700110import android.widget.TabHost;
111import android.widget.TabHost.OnTabChangeListener;
112import android.widget.TabHost.TabContentFactory;
113import android.widget.TabHost.TabSpec;
114import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700115import android.widget.TextView;
116
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700117import com.android.internal.telephony.Phone;
Jeff Sharkey5d706792011-09-08 18:57:17 -0700118import com.android.settings.drawable.InsetBoundsDrawable;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700119import com.android.settings.net.ChartData;
120import com.android.settings.net.ChartDataLoader;
Jeff Sharkeya662e492011-06-18 21:57:06 -0700121import com.android.settings.net.NetworkPolicyEditor;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700122import com.android.settings.net.SummaryForAllUidLoader;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700123import com.android.settings.net.UidDetail;
124import com.android.settings.net.UidDetailProvider;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700125import com.android.settings.widget.ChartDataUsageView;
126import com.android.settings.widget.ChartDataUsageView.DataUsageChartListener;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700127import com.android.settings.widget.PieChartView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700128import com.google.android.collect.Lists;
129
130import java.util.ArrayList;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700131import java.util.Arrays;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700132import java.util.Collections;
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700133import java.util.List;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700134import java.util.Locale;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700135
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700136import libcore.util.Objects;
137
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700138/**
139 * Panel show data usage history across various networks, including options to
140 * inspect based on usage cycle and control through {@link NetworkPolicy}.
141 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700142public class DataUsageSummary extends Fragment {
143 private static final String TAG = "DataUsage";
Jeff Sharkey8a503642011-06-10 13:31:21 -0700144 private static final boolean LOGD = true;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700145
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700146 // TODO: remove this testing code
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700147 private static final boolean TEST_ANIM = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700148 private static final boolean TEST_RADIOS = false;
149 private static final String TEST_RADIOS_PROP = "test.radios";
150
Jeff Sharkey8a503642011-06-10 13:31:21 -0700151 private static final String TAB_3G = "3g";
152 private static final String TAB_4G = "4g";
153 private static final String TAB_MOBILE = "mobile";
154 private static final String TAB_WIFI = "wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700155 private static final String TAB_ETHERNET = "ethernet";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700156
Jeff Sharkey28130d92011-09-02 16:10:24 -0700157 private static final String TAG_CONFIRM_DATA_DISABLE = "confirmDataDisable";
158 private static final String TAG_CONFIRM_DATA_ROAMING = "confirmDataRoaming";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700159 private static final String TAG_CONFIRM_LIMIT = "confirmLimit";
160 private static final String TAG_CYCLE_EDITOR = "cycleEditor";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700161 private static final String TAG_WARNING_EDITOR = "warningEditor";
162 private static final String TAG_LIMIT_EDITOR = "limitEditor";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700163 private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700164 private static final String TAG_DENIED_RESTRICT = "deniedRestrict";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700165 private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700166 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700167
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700168 private static final int LOADER_CHART_DATA = 2;
169 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700170
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700171 private static final long KB_IN_BYTES = 1024;
172 private static final long MB_IN_BYTES = KB_IN_BYTES * 1024;
173 private static final long GB_IN_BYTES = MB_IN_BYTES * 1024;
174
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700175 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700176 private INetworkStatsService mStatsService;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700177 private INetworkPolicyManager mPolicyService;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700178 private ConnectivityManager mConnService;
179
180 private static final String PREF_FILE = "data_usage";
181 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700182 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700183
184 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700185
Jeff Sharkey8a503642011-06-10 13:31:21 -0700186 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700187 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700188 private TabWidget mTabWidget;
189 private ListView mListView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700190 private DataUsageAdapter mAdapter;
191
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700192 /** Distance to inset content from sides, when needed. */
193 private int mInsetSide = 0;
194
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700195 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700196
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700197 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700198 private LinearLayout mNetworkSwitches;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700199 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700200 private View mDataEnabledView;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700201 private CheckBox mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700202 private View mDisableAtLimitView;
203
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700204 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700205 private Spinner mCycleSpinner;
206 private CycleAdapter mCycleAdapter;
207
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700208 private ChartDataUsageView mChart;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700209 private TextView mUsageSummary;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700210 private TextView mEmpty;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700211
212 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700213 private ImageView mAppIcon;
214 private ViewGroup mAppTitles;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700215 private PieChartView mAppPieChart;
216 private TextView mAppForeground;
217 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700218 private Button mAppSettings;
219
220 private LinearLayout mAppSwitches;
221 private CheckBox mAppRestrict;
222 private View mAppRestrictView;
223
Jeff Sharkey8a503642011-06-10 13:31:21 -0700224 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700225 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700226
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700227 private NetworkTemplate mTemplate;
228 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700229
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700230 private int[] mAppDetailUids = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700231
232 private Intent mAppSettingsIntent;
233
Jeff Sharkeya662e492011-06-18 21:57:06 -0700234 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700235
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700236 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700237 private String mIntentTab = null;
238
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700239 private MenuItem mMenuDataRoaming;
240 private MenuItem mMenuRestrictBackground;
241
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700242 /** Flag used to ignore listeners during binding. */
243 private boolean mBinding;
244
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700245 private UidDetailProvider mUidDetailProvider;
246
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700247 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700248 public void onCreate(Bundle savedInstanceState) {
249 super.onCreate(savedInstanceState);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700250
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700251 mNetworkService = INetworkManagementService.Stub.asInterface(
252 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700253 mStatsService = INetworkStatsService.Stub.asInterface(
254 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700255 mPolicyService = INetworkPolicyManager.Stub.asInterface(
256 ServiceManager.getService(Context.NETWORK_POLICY_SERVICE));
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700257 mConnService = (ConnectivityManager) getActivity().getSystemService(
258 Context.CONNECTIVITY_SERVICE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700259
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700260 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700261
Jeff Sharkeya662e492011-06-18 21:57:06 -0700262 mPolicyEditor = new NetworkPolicyEditor(mPolicyService);
263 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700264
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700265 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700266 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700267
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700268 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700269 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700270
Jeff Sharkey8a503642011-06-10 13:31:21 -0700271 @Override
272 public View onCreateView(LayoutInflater inflater, ViewGroup container,
273 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700274
Jeff Sharkey8a503642011-06-10 13:31:21 -0700275 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700276 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
277
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700278 mUidDetailProvider = new UidDetailProvider(context);
279
Jeff Sharkey8a503642011-06-10 13:31:21 -0700280 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700281 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700282 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
283 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700284
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700285 // decide if we need to manually inset our content, or if we should rely
286 // on parent container for inset.
287 final boolean shouldInset = mListView.getScrollBarStyle()
288 == View.SCROLLBARS_OUTSIDE_OVERLAY;
289 if (shouldInset) {
290 mInsetSide = view.getResources().getDimensionPixelOffset(
291 com.android.internal.R.dimen.preference_fragment_padding_side);
292 } else {
293 mInsetSide = 0;
294 }
295
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700296 // adjust padding around tabwidget as needed
Jeff Sharkey5d706792011-09-08 18:57:17 -0700297 prepareCustomPreferencesList(container, view, mListView, true);
298
Jeff Sharkey8a503642011-06-10 13:31:21 -0700299 mTabHost.setup();
300 mTabHost.setOnTabChangedListener(mTabListener);
301
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700302 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700303 mHeader.setClickable(true);
304
305 mListView.addHeaderView(mHeader, null, true);
306 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700307
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700308 if (mInsetSide > 0) {
309 // inset selector and divider drawables
310 insetListViewDrawables(mListView, mInsetSide);
311 mHeader.setPadding(mInsetSide, 0, mInsetSide, 0);
312 }
313
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700314 {
315 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700316 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
317 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700318 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700319
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700320 mDataEnabled = new Switch(inflater.getContext());
321 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
322 mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener);
323 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700324
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700325 mDisableAtLimit = new CheckBox(inflater.getContext());
326 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700327 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700328 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700329 mDisableAtLimitView.setClickable(true);
330 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700331 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
332 mNetworkSwitches.addView(mDisableAtLimitView);
333 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700334
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700335 // bind cycle dropdown
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700336 mCycleView = mHeader.findViewById(R.id.cycles);
337 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700338 mCycleAdapter = new CycleAdapter(context);
339 mCycleSpinner.setAdapter(mCycleAdapter);
340 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
341
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700342 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700343 mChart.setListener(mChartListener);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700344
345 {
346 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700347 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700348 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
349 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700350 mAppPieChart = (PieChartView) mAppDetail.findViewById(R.id.app_pie_chart);
351 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
352 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700353 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700354
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700355 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700356 mAppSettings.setOnClickListener(mAppSettingsListener);
357
358 mAppRestrict = new CheckBox(inflater.getContext());
359 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700360 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700361 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700362 mAppRestrictView.setClickable(true);
363 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700364 mAppRestrictView.setOnClickListener(mAppRestrictListener);
365 mAppSwitches.addView(mAppRestrictView);
366 }
367
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700368 mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700369 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700370
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700371 // only assign layout transitions once first layout is finished
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700372 mListView.getViewTreeObserver().addOnGlobalLayoutListener(mFirstLayoutListener);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700373
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700374 mAdapter = new DataUsageAdapter(mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700375 mListView.setOnItemClickListener(mListListener);
376 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700377
378 return view;
379 }
380
381 @Override
382 public void onResume() {
383 super.onResume();
384
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700385 // pick default tab based on incoming intent
386 final Intent intent = getActivity().getIntent();
387 mIntentTab = computeTabFromIntent(intent);
388
Jeff Sharkey8a503642011-06-10 13:31:21 -0700389 // this kicks off chain reaction which creates tabs, binds the body to
390 // selected network, and binds chart, cycles and detail list.
391 updateTabs();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700392
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700393 // kick off background task to update stats
394 new AsyncTask<Void, Void, Void>() {
395 @Override
396 protected Void doInBackground(Void... params) {
397 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700398 // wait a few seconds before kicking off
399 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700400 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700401 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700402 } catch (RemoteException e) {
403 }
404 return null;
405 }
406
407 @Override
408 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700409 if (isAdded()) {
410 updateBody();
411 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700412 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700413 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700414 }
415
Jeff Sharkey8a503642011-06-10 13:31:21 -0700416 @Override
417 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
418 inflater.inflate(R.menu.data_usage, menu);
419 }
420
421 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700422 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700423 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700424 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700425
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700426 mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700427 mMenuDataRoaming.setVisible(hasMobileRadio(context) && !appDetailMode);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700428 mMenuDataRoaming.setChecked(getDataRoaming());
429
430 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700431 mMenuRestrictBackground.setVisible(!appDetailMode);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700432 mMenuRestrictBackground.setChecked(getRestrictBackground());
433
434 final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700435 split4g.setVisible(hasMobile4gRadio(context) && !appDetailMode);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700436 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700437
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700438 final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700439 if (hasWifiRadio(context) && hasMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700440 showWifi.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700441 showWifi.setChecked(mShowWifi);
442 } else {
443 showWifi.setVisible(false);
444 mShowWifi = true;
445 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700446
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700447 final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
448 if (hasEthernet(context) && hasMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700449 showEthernet.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700450 showEthernet.setChecked(mShowEthernet);
451 } else {
452 showEthernet.setVisible(false);
453 mShowEthernet = true;
454 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700455 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700456
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700457 @Override
458 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700459 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700460 case R.id.data_usage_menu_roaming: {
461 final boolean dataRoaming = !item.isChecked();
462 if (dataRoaming) {
463 ConfirmDataRoamingFragment.show(this);
464 } else {
465 // no confirmation to disable roaming
466 setDataRoaming(false);
467 }
468 return true;
469 }
470 case R.id.data_usage_menu_restrict_background: {
471 final boolean restrictBackground = !item.isChecked();
472 if (restrictBackground) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700473 if (hasLimitedNetworks()) {
474 ConfirmRestrictFragment.show(this);
475 } else {
476 DeniedRestrictFragment.show(this);
477 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700478 } else {
479 // no confirmation to drop restriction
480 setRestrictBackground(false);
481 }
482 return true;
483 }
484 case R.id.data_usage_menu_split_4g: {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700485 final boolean mobileSplit = !item.isChecked();
Jeff Sharkeya662e492011-06-18 21:57:06 -0700486 setMobilePolicySplit(mobileSplit);
487 item.setChecked(isMobilePolicySplit());
Jeff Sharkey8a503642011-06-10 13:31:21 -0700488 updateTabs();
489 return true;
490 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700491 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700492 mShowWifi = !item.isChecked();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700493 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700494 item.setChecked(mShowWifi);
495 updateTabs();
496 return true;
497 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700498 case R.id.data_usage_menu_show_ethernet: {
499 mShowEthernet = !item.isChecked();
500 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
501 item.setChecked(mShowEthernet);
502 updateTabs();
503 return true;
504 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700505 }
506 return false;
507 }
508
Jeff Sharkey94a90952011-06-13 22:31:09 -0700509 @Override
510 public void onDestroyView() {
511 super.onDestroyView();
512
513 mDataEnabledView = null;
514 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700515
516 mUidDetailProvider.clearCache();
517 mUidDetailProvider = null;
Jeff Sharkey94a90952011-06-13 22:31:09 -0700518 }
519
Jeff Sharkey8a503642011-06-10 13:31:21 -0700520 /**
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700521 * Listener to setup {@link LayoutTransition} after first layout pass.
522 */
523 private OnGlobalLayoutListener mFirstLayoutListener = new OnGlobalLayoutListener() {
524 /** {@inheritDoc} */
525 public void onGlobalLayout() {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700526 mListView.getViewTreeObserver().removeGlobalOnLayoutListener(mFirstLayoutListener);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700527
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700528 mTabsContainer.setLayoutTransition(buildLayoutTransition());
529 mHeader.setLayoutTransition(buildLayoutTransition());
530 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700531
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700532 final LayoutTransition chartTransition = buildLayoutTransition();
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700533 chartTransition.setStartDelay(LayoutTransition.APPEARING, 0);
534 chartTransition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700535 chartTransition.setAnimator(LayoutTransition.APPEARING, null);
Jeff Sharkey28130d92011-09-02 16:10:24 -0700536 chartTransition.setAnimator(LayoutTransition.DISAPPEARING, null);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700537 mChart.setLayoutTransition(chartTransition);
538 }
539 };
540
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700541 private static LayoutTransition buildLayoutTransition() {
542 final LayoutTransition transition = new LayoutTransition();
543 if (TEST_ANIM) {
544 transition.setDuration(1500);
545 }
546 transition.setAnimateParentHierarchy(false);
547 return transition;
548 }
549
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700550 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700551 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700552 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
553 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700554 */
555 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700556 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700557 mTabHost.clearAllTabs();
558
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700559 final boolean mobileSplit = isMobilePolicySplit();
560 if (mobileSplit && hasMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700561 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
562 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700563 } else if (hasMobileRadio(context)) {
564 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700565 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700566 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700567 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
568 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700569 if (mShowEthernet && hasEthernet(context)) {
570 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
571 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700572
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700573 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
574 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
575 if (mIntentTab != null) {
576 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700577 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700578 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700579 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700580 mTabHost.setCurrentTabByTag(mIntentTab);
581 }
582 mIntentTab = null;
583 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700584 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700585 }
586 }
587
Jeff Sharkey8a503642011-06-10 13:31:21 -0700588 /**
589 * Factory that provide empty {@link View} to make {@link TabHost} happy.
590 */
591 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
592 /** {@inheritDoc} */
593 public View createTabContent(String tag) {
594 return new View(mTabHost.getContext());
595 }
596 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700597
Jeff Sharkey8a503642011-06-10 13:31:21 -0700598 /**
599 * Build {@link TabSpec} with thin indicator, and empty content.
600 */
601 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700602 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
603 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700604 }
605
606 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
607 /** {@inheritDoc} */
608 public void onTabChanged(String tabId) {
609 // user changed tab; update body
610 updateBody();
611 }
612 };
613
614 /**
615 * Update body content based on current tab. Loads
616 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
617 * binds them to visible controls.
618 */
619 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700620 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700621 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700622
Jeff Sharkeya662e492011-06-18 21:57:06 -0700623 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700624 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700625
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700626 if (currentTab == null) {
627 Log.w(TAG, "no tab selected; hiding body");
628 mListView.setVisibility(View.GONE);
629 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700630 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700631 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700632 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700633
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700634 final boolean tabChanged = !currentTab.equals(mCurrentTab);
635 mCurrentTab = currentTab;
636
Jeff Sharkey8a503642011-06-10 13:31:21 -0700637 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
638
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700639 mDataEnabledView.setVisibility(View.VISIBLE);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700640
641 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700642 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
643 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700644 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700645
646 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700647 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
648 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
649 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700650 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700651
652 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700653 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
654 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
655 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700656 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700657
658 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700659 // wifi doesn't have any controls
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700660 mDataEnabledView.setVisibility(View.GONE);
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700661 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700662 mTemplate = buildTemplateWifi();
663
664 } else if (TAB_ETHERNET.equals(currentTab)) {
665 // ethernet doesn't have any controls
666 mDataEnabledView.setVisibility(View.GONE);
667 mDisableAtLimitView.setVisibility(View.GONE);
668 mTemplate = buildTemplateEthernet();
669
670 } else {
671 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700672 }
673
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700674 // kick off loader for network history
675 // TODO: consider chaining two loaders together instead of reloading
676 // network history when showing app detail.
677 getLoaderManager().restartLoader(LOADER_CHART_DATA,
678 ChartDataLoader.buildArgs(mTemplate, mAppDetailUids), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700679
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700680 // detail mode can change visible menus, invalidate
681 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700682
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700683 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700684 }
685
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700686 private boolean isAppDetailMode() {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700687 return mAppDetailUids != null;
688 }
689
690 private int getAppDetailPrimaryUid() {
691 return mAppDetailUids[0];
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700692 }
693
694 /**
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700695 * Update UID details panels to match {@link #mAppDetailUids}, showing or
696 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700697 */
698 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700699 final Context context = getActivity();
700 final PackageManager pm = context.getPackageManager();
701 final LayoutInflater inflater = getActivity().getLayoutInflater();
702
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700703 if (isAppDetailMode()) {
704 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700705 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700706 } else {
707 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700708 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700709
710 // hide detail stats when not in detail mode
711 mChart.bindDetailNetworkStats(null);
712 return;
713 }
714
715 // remove warning/limit sweeps while in detail mode
716 mChart.bindNetworkPolicy(null);
717
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700718 // show icon and all labels appearing under this app
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700719 final int primaryUid = getAppDetailPrimaryUid();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700720 final UidDetail detail = mUidDetailProvider.getUidDetail(primaryUid, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700721 mAppIcon.setImageDrawable(detail.icon);
722
723 mAppTitles.removeAllViews();
724 if (detail.detailLabels != null) {
725 for (CharSequence label : detail.detailLabels) {
726 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label));
727 }
728 } else {
729 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label));
730 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700731
732 // enable settings button when package provides it
733 // TODO: target torwards entire UID instead of just first package
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700734 final String[] packageNames = pm.getPackagesForUid(primaryUid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700735 if (packageNames != null && packageNames.length > 0) {
736 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
737 mAppSettingsIntent.setPackage(packageNames[0]);
738 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
739
740 final boolean matchFound = pm.resolveActivity(mAppSettingsIntent, 0) != null;
741 mAppSettings.setEnabled(matchFound);
742
743 } else {
744 mAppSettingsIntent = null;
745 mAppSettings.setEnabled(false);
746 }
747
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700748 updateDetailData();
749
750 if (NetworkPolicyManager.isUidValidForPolicy(context, primaryUid)
751 && !getRestrictBackground() && isBandwidthControlEnabled()) {
752 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
753 setPreferenceSummary(mAppRestrictView,
754 getString(R.string.data_usage_app_restrict_background_summary,
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700755 buildLimitedNetworksString()));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700756
757 mAppRestrictView.setVisibility(View.VISIBLE);
758 mAppRestrict.setChecked(getAppRestrictBackground());
759
760 } else {
761 mAppRestrictView.setVisibility(View.GONE);
762 }
763 }
764
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700765 private void setPolicyWarningBytes(long warningBytes) {
766 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700767 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700768 updatePolicy(false);
769 }
770
771 private void setPolicyLimitBytes(long limitBytes) {
772 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700773 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700774 updatePolicy(false);
775 }
776
Jeff Sharkey28130d92011-09-02 16:10:24 -0700777 /**
778 * Local cache of value, used to work around delay when
779 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
780 */
781 private Boolean mMobileDataEnabled;
782
783 private boolean isMobileDataEnabled() {
784 if (mMobileDataEnabled != null) {
785 // TODO: deprecate and remove this once enabled flag is on policy
786 return mMobileDataEnabled;
787 } else {
788 return mConnService.getMobileDataEnabled();
789 }
790 }
791
792 private void setMobileDataEnabled(boolean enabled) {
793 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
794 mConnService.setMobileDataEnabled(enabled);
795 mMobileDataEnabled = enabled;
796 updatePolicy(false);
797 }
798
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700799 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
800 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked();
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700801 }
802
803 private boolean isBandwidthControlEnabled() {
804 try {
805 return mNetworkService.isBandwidthControlEnabled();
806 } catch (RemoteException e) {
807 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
808 return false;
809 }
810 }
811
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700812 private boolean getDataRoaming() {
813 final ContentResolver resolver = getActivity().getContentResolver();
814 return Settings.Secure.getInt(resolver, Settings.Secure.DATA_ROAMING, 0) != 0;
815 }
816
817 private void setDataRoaming(boolean enabled) {
818 // TODO: teach telephony DataConnectionTracker to watch and apply
819 // updates when changed.
820 final ContentResolver resolver = getActivity().getContentResolver();
821 Settings.Secure.putInt(resolver, Settings.Secure.DATA_ROAMING, enabled ? 1 : 0);
822 mMenuDataRoaming.setChecked(enabled);
823 }
824
825 private boolean getRestrictBackground() {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700826 try {
827 return mPolicyService.getRestrictBackground();
828 } catch (RemoteException e) {
829 Log.w(TAG, "problem talking with policy service: " + e);
830 return false;
831 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700832 }
833
834 private void setRestrictBackground(boolean restrictBackground) {
835 if (LOGD) Log.d(TAG, "setRestrictBackground()");
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700836 try {
837 mPolicyService.setRestrictBackground(restrictBackground);
838 mMenuRestrictBackground.setChecked(restrictBackground);
839 } catch (RemoteException e) {
840 Log.w(TAG, "problem talking with policy service: " + e);
841 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700842 }
843
844 private boolean getAppRestrictBackground() {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700845 final int primaryUid = getAppDetailPrimaryUid();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700846 final int uidPolicy;
847 try {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700848 uidPolicy = mPolicyService.getUidPolicy(primaryUid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700849 } catch (RemoteException e) {
850 // since we can't do much without policy, we bail hard.
851 throw new RuntimeException("problem reading network policy", e);
852 }
853
854 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
855 }
856
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700857 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700858 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700859 final int primaryUid = getAppDetailPrimaryUid();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700860 try {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700861 mPolicyService.setUidPolicy(primaryUid,
862 restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700863 } catch (RemoteException e) {
864 throw new RuntimeException("unable to save policy", e);
865 }
866
867 mAppRestrict.setChecked(restrictBackground);
868 }
869
Jeff Sharkey8a503642011-06-10 13:31:21 -0700870 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700871 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
872 * current {@link #mTemplate}.
873 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700874 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700875 if (isAppDetailMode()) {
876 mNetworkSwitches.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700877 } else {
878 mNetworkSwitches.setVisibility(View.VISIBLE);
879 }
880
Jeff Sharkey28130d92011-09-02 16:10:24 -0700881 // TODO: move enabled state directly into policy
882 if (TAB_MOBILE.equals(mCurrentTab)) {
883 mBinding = true;
884 mDataEnabled.setChecked(isMobileDataEnabled());
885 mBinding = false;
886 }
887
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700888 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
889 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700890 mDisableAtLimitView.setVisibility(View.VISIBLE);
891 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700892 if (!isAppDetailMode()) {
893 mChart.bindNetworkPolicy(policy);
894 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700895
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700896 } else {
897 // controls are disabled; don't bind warning/limit sweeps
898 mDisableAtLimitView.setVisibility(View.GONE);
899 mChart.bindNetworkPolicy(null);
900 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700901
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700902 if (refreshCycle) {
903 // generate cycle list based on policy and available history
904 updateCycleList(policy);
905 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700906 }
907
908 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700909 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
910 * and available {@link NetworkStatsHistory} data. Always selects the newest
911 * item, updating the inspection range on {@link #mChart}.
912 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700913 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700914 // stash away currently selected cycle to try restoring below
915 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700916 mCycleAdapter.clear();
917
918 final Context context = mCycleSpinner.getContext();
919
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700920 long historyStart = Long.MAX_VALUE;
921 long historyEnd = Long.MIN_VALUE;
922 if (mChartData != null) {
923 historyStart = mChartData.network.getStart();
924 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700925 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700926
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700927 if (historyStart == Long.MAX_VALUE) historyStart = System.currentTimeMillis();
928 if (historyEnd == Long.MIN_VALUE) historyEnd = System.currentTimeMillis();
929
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700930 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700931 if (policy != null) {
932 // find the next cycle boundary
933 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700934
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700935 // walk backwards, generating all valid cycle ranges
936 while (cycleEnd > historyStart) {
937 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
938 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
939 + historyStart);
940 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
941 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700942 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700943 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700944
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700945 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700946 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700947 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700948
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700949 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700950 // no policy defined cycles; show entry for each four-week period
951 long cycleEnd = historyEnd;
952 while (cycleEnd > historyStart) {
953 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
954 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
955 cycleEnd = cycleStart;
956 }
957
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700958 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700959 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700960
961 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700962 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700963 final int position = mCycleAdapter.findNearestPosition(previousItem);
964 mCycleSpinner.setSelection(position);
965
966 // only force-update cycle when changed; skipping preserves any
967 // user-defined inspection region.
968 final CycleItem selectedItem = mCycleAdapter.getItem(position);
969 if (!Objects.equal(selectedItem, previousItem)) {
970 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
971 } else {
972 // but still kick off loader for detailed list
973 updateDetailData();
974 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700975 } else {
976 updateDetailData();
977 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700978 }
979
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700980 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700981 /** {@inheritDoc} */
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700982 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
983 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700984
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700985 final boolean dataEnabled = isChecked;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700986 final String currentTab = mCurrentTab;
987 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -0700988 if (dataEnabled) {
989 setMobileDataEnabled(true);
990 } else {
991 // disabling data; show confirmation dialog which eventually
992 // calls setMobileDataEnabled() once user confirms.
993 ConfirmDataDisableFragment.show(DataUsageSummary.this);
994 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700995 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700996
Jeff Sharkey28130d92011-09-02 16:10:24 -0700997 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700998 }
999 };
1000
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001001 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001002 /** {@inheritDoc} */
1003 public void onClick(View v) {
1004 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001005 if (disableAtLimit) {
1006 // enabling limit; show confirmation dialog which eventually
1007 // calls setPolicyLimitBytes() once user confirms.
1008 ConfirmLimitFragment.show(DataUsageSummary.this);
1009 } else {
1010 setPolicyLimitBytes(LIMIT_DISABLED);
1011 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001012 }
1013 };
1014
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001015 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
1016 /** {@inheritDoc} */
1017 public void onClick(View v) {
1018 final boolean restrictBackground = !mAppRestrict.isChecked();
1019
1020 if (restrictBackground) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001021 if (hasLimitedNetworks()) {
1022 // enabling restriction; show confirmation dialog which
1023 // eventually calls setRestrictBackground() once user
1024 // confirms.
1025 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
1026 } else {
1027 // no limited networks; show dialog to guide user towards
1028 // setting a network limit. doesn't mutate restrict state.
1029 DeniedRestrictFragment.show(DataUsageSummary.this);
1030 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001031 } else {
1032 setAppRestrictBackground(false);
1033 }
1034 }
1035 };
1036
1037 private OnClickListener mAppSettingsListener = new OnClickListener() {
1038 /** {@inheritDoc} */
1039 public void onClick(View v) {
1040 // TODO: target torwards entire UID instead of just first package
1041 startActivity(mAppSettingsIntent);
1042 }
1043 };
1044
Jeff Sharkey8a503642011-06-10 13:31:21 -07001045 private OnItemClickListener mListListener = new OnItemClickListener() {
1046 /** {@inheritDoc} */
1047 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001048 final Context context = view.getContext();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001049 final AppUsageItem app = (AppUsageItem) parent.getItemAtPosition(position);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001050 final UidDetail detail = mUidDetailProvider.getUidDetail(app.uids[0], true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001051 AppDetailsFragment.show(DataUsageSummary.this, app.uids, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001052 }
1053 };
1054
1055 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
1056 /** {@inheritDoc} */
1057 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1058 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1059 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001060 // show cycle editor; will eventually call setPolicyCycleDay()
1061 // when user finishes editing.
1062 CycleEditorFragment.show(DataUsageSummary.this);
1063
1064 // reset spinner to something other than "change cycle..."
1065 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001066
1067 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001068 if (LOGD) {
1069 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1070 + cycle.end + "]");
1071 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001072
1073 // update chart to show selected cycle, and update detail data
1074 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001075 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001076
1077 updateDetailData();
1078 }
1079 }
1080
1081 /** {@inheritDoc} */
1082 public void onNothingSelected(AdapterView<?> parent) {
1083 // ignored
1084 }
1085 };
1086
1087 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001088 * Update details based on {@link #mChart} inspection range depending on
1089 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1090 * of applications data usage, and when {@link #isAppDetailMode()} update
1091 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001092 */
1093 private void updateDetailData() {
1094 if (LOGD) Log.d(TAG, "updateDetailData()");
1095
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001096 final long start = mChart.getInspectStart();
1097 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001098 final long now = System.currentTimeMillis();
1099
1100 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001101
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001102 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001103 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001104 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001105 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001106 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001107 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001108 final long foregroundBytes = entry.rxBytes + entry.txBytes;
1109
1110 mAppPieChart.setOriginAngle(175);
1111
1112 mAppPieChart.removeAllSlices();
1113 mAppPieChart.addSlice(foregroundBytes, Color.parseColor("#d88d3a"));
1114 mAppPieChart.addSlice(defaultBytes, Color.parseColor("#666666"));
1115
1116 mAppPieChart.generatePath();
1117
1118 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1119 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1120
1121 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001122 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001123
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001124 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001125
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001126 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001127 if (mChartData != null) {
1128 entry = mChartData.network.getValues(start, end, now, null);
1129 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001130
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001131 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001132 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001133 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001134 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001135
1136 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1137 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001138 final String rangePhrase = formatDateRange(context, start, end, false);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001139
1140 mUsageSummary.setText(
1141 getString(R.string.data_usage_total_during_range, totalPhrase, rangePhrase));
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001142 }
1143
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001144 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1145 ChartData>() {
1146 /** {@inheritDoc} */
1147 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
1148 return new ChartDataLoader(getActivity(), mStatsService, args);
1149 }
1150
1151 /** {@inheritDoc} */
1152 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1153 mChartData = data;
1154 mChart.bindNetworkStats(mChartData.network);
1155 mChart.bindDetailNetworkStats(mChartData.detail);
1156
1157 // calcuate policy cycles based on available data
1158 updatePolicy(true);
1159 updateAppDetail();
1160
1161 // force scroll to top of body when showing detail
1162 if (mChartData.detail != null) {
1163 mListView.smoothScrollToPosition(0);
1164 }
1165 }
1166
1167 /** {@inheritDoc} */
1168 public void onLoaderReset(Loader<ChartData> loader) {
1169 mChartData = null;
1170 mChart.bindNetworkStats(null);
1171 mChart.bindDetailNetworkStats(null);
1172 }
1173 };
1174
1175 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001176 NetworkStats>() {
1177 /** {@inheritDoc} */
1178 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
1179 return new SummaryForAllUidLoader(getActivity(), mStatsService, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001180 }
1181
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001182 /** {@inheritDoc} */
1183 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
1184 mAdapter.bindStats(data);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001185 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001186 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001187
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001188 /** {@inheritDoc} */
1189 public void onLoaderReset(Loader<NetworkStats> loader) {
1190 mAdapter.bindStats(null);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001191 updateEmptyVisible();
1192 }
1193
1194 private void updateEmptyVisible() {
1195 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1196 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001197 }
1198 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001199
Jeff Sharkeya662e492011-06-18 21:57:06 -07001200 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001201 final Context context = getActivity();
1202 if (hasMobileRadio(context)) {
1203 final String subscriberId = getActiveSubscriberId(context);
1204 return mPolicyEditor.isMobilePolicySplit(subscriberId);
1205 } else {
1206 return false;
1207 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001208 }
1209
1210 private void setMobilePolicySplit(boolean split) {
1211 final String subscriberId = getActiveSubscriberId(getActivity());
1212 mPolicyEditor.setMobilePolicySplit(subscriberId, split);
1213 }
1214
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001215 private static String getActiveSubscriberId(Context context) {
1216 final TelephonyManager telephony = (TelephonyManager) context.getSystemService(
1217 Context.TELEPHONY_SERVICE);
1218 return telephony.getSubscriberId();
1219 }
1220
Jeff Sharkey8a503642011-06-10 13:31:21 -07001221 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
1222 /** {@inheritDoc} */
1223 public void onInspectRangeChanged() {
1224 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
1225 updateDetailData();
1226 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001227
Jeff Sharkey8a503642011-06-10 13:31:21 -07001228 /** {@inheritDoc} */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001229 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001230 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001231 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001232
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001233 /** {@inheritDoc} */
1234 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001235 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001236 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001237
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001238 /** {@inheritDoc} */
1239 public void requestWarningEdit() {
1240 WarningEditorFragment.show(DataUsageSummary.this);
1241 }
1242
1243 /** {@inheritDoc} */
1244 public void requestLimitEdit() {
1245 LimitEditorFragment.show(DataUsageSummary.this);
1246 }
1247 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001248
1249 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001250 * List item that reflects a specific data usage cycle.
1251 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001252 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001253 public CharSequence label;
1254 public long start;
1255 public long end;
1256
Jeff Sharkey8a503642011-06-10 13:31:21 -07001257 CycleItem(CharSequence label) {
1258 this.label = label;
1259 }
1260
1261 public CycleItem(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001262 this.label = formatDateRange(context, start, end, true);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001263 this.start = start;
1264 this.end = end;
1265 }
1266
Jeff Sharkey8a503642011-06-10 13:31:21 -07001267 @Override
1268 public String toString() {
1269 return label.toString();
1270 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001271
1272 @Override
1273 public boolean equals(Object o) {
1274 if (o instanceof CycleItem) {
1275 final CycleItem another = (CycleItem) o;
1276 return start == another.start && end == another.end;
1277 }
1278 return false;
1279 }
1280
1281 /** {@inheritDoc} */
1282 public int compareTo(CycleItem another) {
1283 return Long.compare(start, another.start);
1284 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001285 }
1286
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001287 private static final StringBuilder sBuilder = new StringBuilder(50);
1288 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1289 sBuilder, Locale.getDefault());
1290
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001291 public static String formatDateRange(Context context, long start, long end, boolean utcTime) {
1292 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
1293 final String timezone = utcTime ? TIMEZONE_UTC : null;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001294
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001295 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001296 sBuilder.setLength(0);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001297 return DateUtils
1298 .formatDateRange(context, sFormatter, start, end, flags, timezone).toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001299 }
1300 }
1301
Jeff Sharkey8a503642011-06-10 13:31:21 -07001302 /**
1303 * Special-case data usage cycle that triggers dialog to change
1304 * {@link NetworkPolicy#cycleDay}.
1305 */
1306 public static class CycleChangeItem extends CycleItem {
1307 public CycleChangeItem(Context context) {
1308 super(context.getString(R.string.data_usage_change_cycle));
1309 }
1310 }
1311
1312 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001313 private boolean mChangePossible = false;
1314 private boolean mChangeVisible = false;
1315
1316 private final CycleChangeItem mChangeItem;
1317
Jeff Sharkey8a503642011-06-10 13:31:21 -07001318 public CycleAdapter(Context context) {
1319 super(context, android.R.layout.simple_spinner_item);
1320 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001321 mChangeItem = new CycleChangeItem(context);
1322 }
1323
1324 public void setChangePossible(boolean possible) {
1325 mChangePossible = possible;
1326 updateChange();
1327 }
1328
1329 public void setChangeVisible(boolean visible) {
1330 mChangeVisible = visible;
1331 updateChange();
1332 }
1333
1334 private void updateChange() {
1335 remove(mChangeItem);
1336 if (mChangePossible && mChangeVisible) {
1337 add(mChangeItem);
1338 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001339 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001340
1341 /**
1342 * Find position of {@link CycleItem} in this adapter which is nearest
1343 * the given {@link CycleItem}.
1344 */
1345 public int findNearestPosition(CycleItem target) {
1346 if (target != null) {
1347 final int count = getCount();
1348 for (int i = count - 1; i >= 0; i--) {
1349 final CycleItem item = getItem(i);
1350 if (item instanceof CycleChangeItem) {
1351 continue;
1352 } else if (item.compareTo(target) >= 0) {
1353 return i;
1354 }
1355 }
1356 }
1357 return 0;
1358 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001359 }
1360
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001361 private static class AppUsageItem implements Comparable<AppUsageItem> {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001362 public int[] uids;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001363 public long total;
1364
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001365 public AppUsageItem(int uid) {
1366 uids = new int[] { uid };
1367 }
1368
1369 public void addUid(int uid) {
1370 if (contains(uids, uid)) return;
1371 final int length = uids.length;
1372 uids = Arrays.copyOf(uids, length + 1);
1373 uids[length] = uid;
1374 }
1375
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001376 /** {@inheritDoc} */
1377 public int compareTo(AppUsageItem another) {
1378 return Long.compare(another.total, total);
1379 }
1380 }
1381
Jeff Sharkey8a503642011-06-10 13:31:21 -07001382 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001383 * Adapter of applications, sorted by total usage descending.
1384 */
1385 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001386 private final UidDetailProvider mProvider;
1387 private final int mInsetSide;
1388
Jeff Sharkey8a503642011-06-10 13:31:21 -07001389 private ArrayList<AppUsageItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001390 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001391
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001392 public DataUsageAdapter(UidDetailProvider provider, int insetSide) {
1393 mProvider = checkNotNull(provider);
1394 mInsetSide = insetSide;
1395 }
1396
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001397 /**
1398 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1399 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001400 public void bindStats(NetworkStats stats) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001401 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001402
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001403 final AppUsageItem systemItem = new AppUsageItem(android.os.Process.SYSTEM_UID);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001404 final SparseArray<AppUsageItem> knownUids = new SparseArray<AppUsageItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001405
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001406 NetworkStats.Entry entry = null;
1407 final int size = stats != null ? stats.size() : 0;
1408 for (int i = 0; i < size; i++) {
1409 entry = stats.getValues(i, entry);
1410
1411 final int uid = entry.uid;
1412 final boolean isApp = uid >= android.os.Process.FIRST_APPLICATION_UID
1413 && uid <= android.os.Process.LAST_APPLICATION_UID;
1414 if (isApp || uid == TrafficStats.UID_REMOVED) {
1415 AppUsageItem item = knownUids.get(uid);
1416 if (item == null) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001417 item = new AppUsageItem(uid);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001418 knownUids.put(uid, item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001419 mItems.add(item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001420 }
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001421
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001422 item.total += entry.rxBytes + entry.txBytes;
1423 } else {
1424 systemItem.total += entry.rxBytes + entry.txBytes;
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001425 systemItem.addUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001426 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001427 }
1428
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001429 if (systemItem.total > 0) {
1430 mItems.add(systemItem);
1431 }
1432
Jeff Sharkey8a503642011-06-10 13:31:21 -07001433 Collections.sort(mItems);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001434 mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001435 notifyDataSetChanged();
1436 }
1437
1438 @Override
1439 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001440 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001441 }
1442
1443 @Override
1444 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001445 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001446 }
1447
1448 @Override
1449 public long getItemId(int position) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001450 return mItems.get(position).uids[0];
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001451 }
1452
1453 @Override
1454 public View getView(int position, View convertView, ViewGroup parent) {
1455 if (convertView == null) {
1456 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey5d706792011-09-08 18:57:17 -07001457 R.layout.data_usage_item, parent, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001458
1459 if (mInsetSide > 0) {
1460 convertView.setPadding(mInsetSide, 0, mInsetSide, 0);
1461 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001462 }
1463
1464 final Context context = parent.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001465
Jeff Sharkey28130d92011-09-02 16:10:24 -07001466 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001467 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1468 android.R.id.progress);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001469
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001470 // kick off async load of app details
Jeff Sharkey8a503642011-06-10 13:31:21 -07001471 final AppUsageItem item = mItems.get(position);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001472 UidDetailTask.bindView(mProvider, item, convertView);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001473
Jeff Sharkey28130d92011-09-02 16:10:24 -07001474 text1.setText(Formatter.formatFileSize(context, item.total));
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001475
1476 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1477 progress.setProgress(percentTotal);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001478
1479 return convertView;
1480 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001481 }
1482
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001483 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001484 * Empty {@link Fragment} that controls display of UID details in
1485 * {@link DataUsageSummary}.
1486 */
1487 public static class AppDetailsFragment extends Fragment {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001488 private static final String EXTRA_UIDS = "uids";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001489
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001490 public static void show(DataUsageSummary parent, int[] uids, CharSequence label) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001491 final Bundle args = new Bundle();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001492 args.putIntArray(EXTRA_UIDS, uids);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001493
1494 final AppDetailsFragment fragment = new AppDetailsFragment();
1495 fragment.setArguments(args);
1496 fragment.setTargetFragment(parent, 0);
1497
1498 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1499 ft.add(fragment, TAG_APP_DETAILS);
1500 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001501 ft.setBreadCrumbTitle(label);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001502 ft.commit();
1503 }
1504
1505 @Override
1506 public void onStart() {
1507 super.onStart();
1508 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001509 target.mAppDetailUids = getArguments().getIntArray(EXTRA_UIDS);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001510 target.updateBody();
1511 }
1512
1513 @Override
1514 public void onStop() {
1515 super.onStop();
1516 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001517 target.mAppDetailUids = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001518 target.updateBody();
1519 }
1520 }
1521
1522 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001523 * Dialog to request user confirmation before setting
1524 * {@link NetworkPolicy#limitBytes}.
1525 */
1526 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001527 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001528 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001529
1530 public static void show(DataUsageSummary parent) {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001531 final Resources res = parent.getResources();
1532
1533 final CharSequence message;
1534 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001535
1536 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001537 final String currentTab = parent.mCurrentTab;
1538 if (TAB_3G.equals(currentTab)) {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001539 message = buildDialogMessage(res, R.string.data_usage_tab_3g);
1540 limitBytes = 5 * GB_IN_BYTES;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001541 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001542 message = buildDialogMessage(res, R.string.data_usage_tab_4g);
1543 limitBytes = 5 * GB_IN_BYTES;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001544 } else if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001545 message = buildDialogMessage(res, R.string.data_usage_list_mobile);
1546 limitBytes = 5 * GB_IN_BYTES;
1547 } else if (TAB_WIFI.equals(currentTab)) {
1548 message = buildDialogMessage(res, R.string.data_usage_tab_wifi);
1549 limitBytes = 5 * GB_IN_BYTES;
1550 } else {
1551 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001552 }
1553
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001554 final Bundle args = new Bundle();
1555 args.putCharSequence(EXTRA_MESSAGE, message);
1556 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1557
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001558 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1559 dialog.setArguments(args);
1560 dialog.setTargetFragment(parent, 0);
1561 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1562 }
1563
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001564 private static CharSequence buildDialogMessage(Resources res, int networkResId) {
1565 return res.getString(R.string.data_usage_limit_dialog, res.getString(networkResId));
1566 }
1567
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001568 @Override
1569 public Dialog onCreateDialog(Bundle savedInstanceState) {
1570 final Context context = getActivity();
1571
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001572 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001573 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1574
1575 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1576 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001577 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001578
1579 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1580 public void onClick(DialogInterface dialog, int which) {
1581 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1582 if (target != null) {
1583 target.setPolicyLimitBytes(limitBytes);
1584 }
1585 }
1586 });
1587
1588 return builder.create();
1589 }
1590 }
1591
1592 /**
1593 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1594 */
1595 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001596 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001597
1598 public static void show(DataUsageSummary parent) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001599 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001600 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001601
1602 final CycleEditorFragment dialog = new CycleEditorFragment();
1603 dialog.setArguments(args);
1604 dialog.setTargetFragment(parent, 0);
1605 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1606 }
1607
1608 @Override
1609 public Dialog onCreateDialog(Bundle savedInstanceState) {
1610 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001611 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1612 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001613
1614 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1615 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1616
1617 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1618 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1619
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001620 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1621 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001622
1623 cycleDayPicker.setMinValue(1);
1624 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001625 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001626 cycleDayPicker.setWrapSelectorWheel(true);
1627
1628 builder.setTitle(R.string.data_usage_cycle_editor_title);
1629 builder.setView(view);
1630
1631 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1632 new DialogInterface.OnClickListener() {
1633 public void onClick(DialogInterface dialog, int which) {
1634 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001635 editor.setPolicyCycleDay(template, cycleDay);
1636 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001637 }
1638 });
1639
1640 return builder.create();
1641 }
1642 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001643
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001644 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001645 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1646 */
1647 public static class WarningEditorFragment extends DialogFragment {
1648 private static final String EXTRA_TEMPLATE = "template";
1649
1650 public static void show(DataUsageSummary parent) {
1651 final Bundle args = new Bundle();
1652 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1653
1654 final WarningEditorFragment dialog = new WarningEditorFragment();
1655 dialog.setArguments(args);
1656 dialog.setTargetFragment(parent, 0);
1657 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1658 }
1659
1660 @Override
1661 public Dialog onCreateDialog(Bundle savedInstanceState) {
1662 final Context context = getActivity();
1663 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1664 final NetworkPolicyEditor editor = target.mPolicyEditor;
1665
1666 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1667 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1668
1669 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1670 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1671
1672 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1673 final long warningBytes = editor.getPolicyWarningBytes(template);
1674 final long limitBytes = editor.getPolicyLimitBytes(template);
1675
1676 bytesPicker.setMinValue(0);
1677 if (limitBytes != LIMIT_DISABLED) {
1678 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1679 } else {
1680 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1681 }
1682 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1683 bytesPicker.setWrapSelectorWheel(false);
1684
1685 builder.setTitle(R.string.data_usage_warning_editor_title);
1686 builder.setView(view);
1687
1688 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1689 new DialogInterface.OnClickListener() {
1690 public void onClick(DialogInterface dialog, int which) {
1691 // clear focus to finish pending text edits
1692 bytesPicker.clearFocus();
1693
1694 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1695 editor.setPolicyWarningBytes(template, bytes);
1696 target.updatePolicy(false);
1697 }
1698 });
1699
1700 return builder.create();
1701 }
1702 }
1703
1704 /**
1705 * Dialog to edit {@link NetworkPolicy#limitBytes}.
1706 */
1707 public static class LimitEditorFragment extends DialogFragment {
1708 private static final String EXTRA_TEMPLATE = "template";
1709
1710 public static void show(DataUsageSummary parent) {
1711 final Bundle args = new Bundle();
1712 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1713
1714 final LimitEditorFragment dialog = new LimitEditorFragment();
1715 dialog.setArguments(args);
1716 dialog.setTargetFragment(parent, 0);
1717 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
1718 }
1719
1720 @Override
1721 public Dialog onCreateDialog(Bundle savedInstanceState) {
1722 final Context context = getActivity();
1723 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1724 final NetworkPolicyEditor editor = target.mPolicyEditor;
1725
1726 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1727 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1728
1729 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1730 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1731
1732 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1733 final long warningBytes = editor.getPolicyWarningBytes(template);
1734 final long limitBytes = editor.getPolicyLimitBytes(template);
1735
1736 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1737 if (warningBytes != WARNING_DISABLED) {
1738 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
1739 } else {
1740 bytesPicker.setMinValue(0);
1741 }
1742 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
1743 bytesPicker.setWrapSelectorWheel(false);
1744
1745 builder.setTitle(R.string.data_usage_limit_editor_title);
1746 builder.setView(view);
1747
1748 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1749 new DialogInterface.OnClickListener() {
1750 public void onClick(DialogInterface dialog, int which) {
1751 // clear focus to finish pending text edits
1752 bytesPicker.clearFocus();
1753
1754 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1755 editor.setPolicyLimitBytes(template, bytes);
1756 target.updatePolicy(false);
1757 }
1758 });
1759
1760 return builder.create();
1761 }
1762 }
1763 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07001764 * Dialog to request user confirmation before disabling data.
1765 */
1766 public static class ConfirmDataDisableFragment extends DialogFragment {
1767 public static void show(DataUsageSummary parent) {
1768 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
1769 dialog.setTargetFragment(parent, 0);
1770 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
1771 }
1772
1773 @Override
1774 public Dialog onCreateDialog(Bundle savedInstanceState) {
1775 final Context context = getActivity();
1776
1777 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1778 builder.setMessage(R.string.data_usage_disable_mobile);
1779
1780 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1781 public void onClick(DialogInterface dialog, int which) {
1782 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1783 if (target != null) {
1784 // TODO: extend to modify policy enabled flag.
1785 target.setMobileDataEnabled(false);
1786 }
1787 }
1788 });
1789 builder.setNegativeButton(android.R.string.cancel, null);
1790
1791 return builder.create();
1792 }
1793 }
1794
1795 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001796 * Dialog to request user confirmation before setting
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001797 * {@link Settings.Secure#DATA_ROAMING}.
1798 */
1799 public static class ConfirmDataRoamingFragment extends DialogFragment {
1800 public static void show(DataUsageSummary parent) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001801 final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment();
1802 dialog.setTargetFragment(parent, 0);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001803 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_ROAMING);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001804 }
1805
1806 @Override
1807 public Dialog onCreateDialog(Bundle savedInstanceState) {
1808 final Context context = getActivity();
1809
1810 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1811 builder.setTitle(R.string.roaming_reenable_title);
1812 builder.setMessage(R.string.roaming_warning);
1813
1814 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1815 public void onClick(DialogInterface dialog, int which) {
1816 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1817 if (target != null) {
1818 target.setDataRoaming(true);
1819 }
1820 }
1821 });
1822 builder.setNegativeButton(android.R.string.cancel, null);
1823
1824 return builder.create();
1825 }
1826 }
1827
1828 /**
1829 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001830 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001831 */
1832 public static class ConfirmRestrictFragment extends DialogFragment {
1833 public static void show(DataUsageSummary parent) {
1834 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1835 dialog.setTargetFragment(parent, 0);
1836 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1837 }
1838
1839 @Override
1840 public Dialog onCreateDialog(Bundle savedInstanceState) {
1841 final Context context = getActivity();
1842
1843 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001844 builder.setTitle(R.string.data_usage_restrict_background_title);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07001845
1846 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1847 if (target != null) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001848 final CharSequence limitedNetworks = target.buildLimitedNetworksString();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07001849 builder.setMessage(
1850 getString(R.string.data_usage_restrict_background, limitedNetworks));
1851 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001852
1853 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1854 public void onClick(DialogInterface dialog, int which) {
1855 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1856 if (target != null) {
1857 target.setRestrictBackground(true);
1858 }
1859 }
1860 });
1861 builder.setNegativeButton(android.R.string.cancel, null);
1862
1863 return builder.create();
1864 }
1865 }
1866
1867 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001868 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
1869 * change has been denied, usually based on
1870 * {@link DataUsageSummary#hasLimitedNetworks()}.
1871 */
1872 public static class DeniedRestrictFragment extends DialogFragment {
1873 public static void show(DataUsageSummary parent) {
1874 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
1875 dialog.setTargetFragment(parent, 0);
1876 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
1877 }
1878
1879 @Override
1880 public Dialog onCreateDialog(Bundle savedInstanceState) {
1881 final Context context = getActivity();
1882
1883 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1884 builder.setTitle(R.string.data_usage_app_restrict_background);
1885 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
1886 builder.setPositiveButton(android.R.string.ok, null);
1887
1888 return builder.create();
1889 }
1890 }
1891
1892 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001893 * Dialog to request user confirmation before setting
1894 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
1895 */
1896 public static class ConfirmAppRestrictFragment extends DialogFragment {
1897 public static void show(DataUsageSummary parent) {
1898 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
1899 dialog.setTargetFragment(parent, 0);
1900 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
1901 }
1902
1903 @Override
1904 public Dialog onCreateDialog(Bundle savedInstanceState) {
1905 final Context context = getActivity();
1906
1907 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001908 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
1909 builder.setMessage(R.string.data_usage_app_restrict_dialog);
1910
1911 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1912 public void onClick(DialogInterface dialog, int which) {
1913 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1914 if (target != null) {
1915 target.setAppRestrictBackground(true);
1916 }
1917 }
1918 });
1919 builder.setNegativeButton(android.R.string.cancel, null);
1920
1921 return builder.create();
1922 }
1923 }
1924
1925 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001926 * Compute default tab that should be selected, based on
1927 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
1928 */
1929 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07001930 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
1931 if (template == null) return null;
1932
1933 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07001934 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001935 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001936 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001937 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001938 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001939 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001940 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001941 return TAB_WIFI;
1942 default:
1943 return null;
1944 }
1945 }
1946
1947 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001948 * Background task that loads {@link UidDetail}, binding to
1949 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001950 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001951 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
1952 private final UidDetailProvider mProvider;
1953 private final AppUsageItem mItem;
1954 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001955
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001956 private UidDetailTask(UidDetailProvider provider, AppUsageItem item, View target) {
1957 mProvider = checkNotNull(provider);
1958 mItem = checkNotNull(item);
1959 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001960 }
1961
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001962 public static void bindView(
1963 UidDetailProvider provider, AppUsageItem item, View target) {
1964 final UidDetailTask existing = (UidDetailTask) target.getTag();
1965 if (existing != null) {
1966 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001967 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001968
1969 final UidDetail cachedDetail = provider.getUidDetail(item.uids[0], false);
1970 if (cachedDetail != null) {
1971 bindView(cachedDetail, target);
1972 } else {
1973 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
1974 AsyncTask.THREAD_POOL_EXECUTOR));
1975 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001976 }
1977
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001978 private static void bindView(UidDetail detail, View target) {
1979 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
1980 final TextView title = (TextView) target.findViewById(android.R.id.title);
1981
1982 if (detail != null) {
1983 icon.setImageDrawable(detail.icon);
1984 title.setText(detail.label);
1985 } else {
1986 icon.setImageDrawable(null);
1987 title.setText(null);
1988 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001989 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001990
1991 @Override
1992 protected void onPreExecute() {
1993 bindView(null, mTarget);
1994 }
1995
1996 @Override
1997 protected UidDetail doInBackground(Void... params) {
1998 return mProvider.getUidDetail(mItem.uids[0], true);
1999 }
2000
2001 @Override
2002 protected void onPostExecute(UidDetail result) {
2003 bindView(result, mTarget);
2004 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002005 }
2006
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002007 /**
2008 * Test if device has a mobile data radio.
2009 */
2010 private static boolean hasMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002011 if (TEST_RADIOS) {
2012 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2013 }
2014
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002015 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
2016 Context.CONNECTIVITY_SERVICE);
2017
2018 // mobile devices should have MOBILE network tracker regardless of
2019 // connection status.
2020 return conn.getNetworkInfo(TYPE_MOBILE) != null;
2021 }
2022
2023 /**
2024 * Test if device has a mobile 4G data radio.
2025 */
2026 private static boolean hasMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002027 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2028 return false;
2029 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002030 if (TEST_RADIOS) {
2031 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2032 }
2033
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002034 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
2035 Context.CONNECTIVITY_SERVICE);
2036 final TelephonyManager telephony = (TelephonyManager) context.getSystemService(
2037 Context.TELEPHONY_SERVICE);
2038
2039 // WiMAX devices should have WiMAX network tracker regardless of
2040 // connection status.
2041 final boolean hasWimax = conn.getNetworkInfo(TYPE_WIMAX) != null;
2042 final boolean hasLte = telephony.getLteOnCdmaMode() == Phone.LTE_ON_CDMA_TRUE;
2043 return hasWimax || hasLte;
2044 }
2045
2046 /**
2047 * Test if device has a Wi-Fi data radio.
2048 */
2049 private static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002050 if (TEST_RADIOS) {
2051 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2052 }
2053
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002054 return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI);
2055 }
2056
2057 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002058 * Test if device has an ethernet network connection.
2059 */
2060 private static boolean hasEthernet(Context context) {
2061 if (TEST_RADIOS) {
2062 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2063 }
2064
2065 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
2066 Context.CONNECTIVITY_SERVICE);
2067 return conn.getNetworkInfo(TYPE_ETHERNET) != null;
2068 }
2069
2070 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002071 * Inflate a {@link Preference} style layout, adding the given {@link View}
2072 * widget into {@link android.R.id#widget_frame}.
2073 */
2074 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2075 final View view = inflater.inflate(R.layout.preference, root, false);
2076 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2077 android.R.id.widget_frame);
2078 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2079 return view;
2080 }
2081
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002082 private static View inflateAppTitle(
2083 LayoutInflater inflater, ViewGroup root, CharSequence label) {
2084 final TextView view = (TextView) inflater.inflate(
2085 R.layout.data_usage_app_title, root, false);
2086 view.setText(label);
2087 return view;
2088 }
2089
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002090 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002091 * Test if any networks are currently limited.
2092 */
2093 private boolean hasLimitedNetworks() {
2094 return !buildLimitedNetworksList().isEmpty();
2095 }
2096
2097 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002098 * Build string describing currently limited networks, which defines when
2099 * background data is restricted.
2100 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002101 private CharSequence buildLimitedNetworksString() {
2102 final List<CharSequence> limited = buildLimitedNetworksList();
2103
2104 // handle case where no networks limited
2105 if (limited.isEmpty()) {
2106 limited.add(getText(R.string.data_usage_list_none));
2107 }
2108
2109 return TextUtils.join(limited);
2110 }
2111
2112 /**
2113 * Build list of currently limited networks, which defines when background
2114 * data is restricted.
2115 */
2116 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002117 final Context context = getActivity();
2118 final String subscriberId = getActiveSubscriberId(context);
2119
2120 // build combined list of all limited networks
2121 final ArrayList<CharSequence> limited = Lists.newArrayList();
2122 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2123 limited.add(getText(R.string.data_usage_list_mobile));
2124 }
2125 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2126 limited.add(getText(R.string.data_usage_tab_3g));
2127 }
2128 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2129 limited.add(getText(R.string.data_usage_tab_4g));
2130 }
2131 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifi())) {
2132 limited.add(getText(R.string.data_usage_tab_wifi));
2133 }
2134 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2135 limited.add(getText(R.string.data_usage_tab_ethernet));
2136 }
2137
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002138 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002139 }
2140
2141 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002142 * Inset both selector and divider {@link Drawable} on the given
2143 * {@link ListView} by the requested dimensions.
2144 */
2145 private static void insetListViewDrawables(ListView view, int insetSide) {
2146 final Drawable selector = view.getSelector();
2147 final Drawable divider = view.getDivider();
2148
2149 // fully unregister these drawables so callbacks can be maintained after
2150 // wrapping below.
2151 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2152 view.setSelector(stub);
2153 view.setDivider(stub);
2154
2155 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2156 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2157 }
2158
2159 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002160 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002161 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002162 */
2163 private static void setPreferenceTitle(View parent, int resId) {
2164 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2165 title.setText(resId);
2166 }
2167
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002168 /**
2169 * Set {@link android.R.id#summary} for a preference view inflated with
2170 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2171 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002172 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002173 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2174 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002175 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002176 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07002177
2178 private static boolean contains(int[] haystack, int needle) {
2179 for (int value : haystack) {
2180 if (value == needle) {
2181 return true;
2182 }
2183 }
2184 return false;
2185 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002186}