blob: d6c40b2d31d4c575860c5229e182ad448917424e [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 Sharkeya83a24f2011-09-16 01:52:39 -070038import static android.net.TrafficStats.UID_REMOVED;
39import static android.net.TrafficStats.UID_TETHERING;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070040import static android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
41import static android.text.format.DateUtils.FORMAT_SHOW_DATE;
42import static android.text.format.Time.TIMEZONE_UTC;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070043import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -070044import static com.android.internal.util.Preconditions.checkNotNull;
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -070045import static com.android.settings.Utils.prepareCustomPreferencesList;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070046
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070047import android.animation.LayoutTransition;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070048import android.app.AlertDialog;
49import android.app.Dialog;
50import android.app.DialogFragment;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070051import android.app.Fragment;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070052import android.app.FragmentTransaction;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070053import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070054import android.content.ContentResolver;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070055import android.content.Context;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070056import android.content.DialogInterface;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -070057import android.content.Intent;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070058import android.content.Loader;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070059import android.content.SharedPreferences;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070060import android.content.pm.PackageManager;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070061import android.content.res.Resources;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070062import android.graphics.Color;
Jeff Sharkey5d706792011-09-08 18:57:17 -070063import android.graphics.drawable.ColorDrawable;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070064import android.graphics.drawable.Drawable;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070065import android.net.ConnectivityManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070066import android.net.INetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070067import android.net.INetworkStatsService;
Jeff Sharkey8a503642011-06-10 13:31:21 -070068import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070069import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070070import android.net.NetworkStats;
71import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070072import android.net.NetworkTemplate;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070073import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070074import android.os.Bundle;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070075import android.os.INetworkManagementService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070076import android.os.RemoteException;
77import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070078import android.os.SystemProperties;
Jeff Sharkey8a503642011-06-10 13:31:21 -070079import android.preference.Preference;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070080import android.provider.Settings;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070081import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070082import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070083import android.text.format.DateUtils;
84import android.text.format.Formatter;
85import android.util.Log;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070086import android.util.SparseArray;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070087import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -070088import android.view.Menu;
89import android.view.MenuInflater;
90import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070091import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070092import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070093import android.view.ViewGroup;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -070094import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Jeff Sharkey8a503642011-06-10 13:31:21 -070095import android.widget.AdapterView;
96import android.widget.AdapterView.OnItemClickListener;
97import android.widget.AdapterView.OnItemSelectedListener;
98import android.widget.ArrayAdapter;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070099import android.widget.BaseAdapter;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700100import android.widget.Button;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700101import android.widget.CheckBox;
102import android.widget.CompoundButton;
103import android.widget.CompoundButton.OnCheckedChangeListener;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700104import android.widget.ImageView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700105import android.widget.LinearLayout;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700106import android.widget.ListView;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700107import android.widget.NumberPicker;
Jeff Sharkey2412b0f2011-07-17 20:31:40 -0700108import android.widget.ProgressBar;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700109import android.widget.Spinner;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700110import android.widget.Switch;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700111import android.widget.TabHost;
112import android.widget.TabHost.OnTabChangeListener;
113import android.widget.TabHost.TabContentFactory;
114import android.widget.TabHost.TabSpec;
115import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700116import android.widget.TextView;
117
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700118import com.android.internal.telephony.Phone;
Jeff Sharkey5d706792011-09-08 18:57:17 -0700119import com.android.settings.drawable.InsetBoundsDrawable;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700120import com.android.settings.net.ChartData;
121import com.android.settings.net.ChartDataLoader;
Jeff Sharkeya662e492011-06-18 21:57:06 -0700122import com.android.settings.net.NetworkPolicyEditor;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700123import com.android.settings.net.SummaryForAllUidLoader;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700124import com.android.settings.net.UidDetail;
125import com.android.settings.net.UidDetailProvider;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700126import com.android.settings.widget.ChartDataUsageView;
127import com.android.settings.widget.ChartDataUsageView.DataUsageChartListener;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700128import com.android.settings.widget.PieChartView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700129import com.google.android.collect.Lists;
130
131import java.util.ArrayList;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700132import java.util.Arrays;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700133import java.util.Collections;
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700134import java.util.List;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700135import java.util.Locale;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700136
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700137import libcore.util.Objects;
138
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700139/**
140 * Panel show data usage history across various networks, including options to
141 * inspect based on usage cycle and control through {@link NetworkPolicy}.
142 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700143public class DataUsageSummary extends Fragment {
144 private static final String TAG = "DataUsage";
Jeff Sharkey8a503642011-06-10 13:31:21 -0700145 private static final boolean LOGD = true;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700146
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700147 // TODO: remove this testing code
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700148 private static final boolean TEST_ANIM = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700149 private static final boolean TEST_RADIOS = false;
150 private static final String TEST_RADIOS_PROP = "test.radios";
151
Jeff Sharkey8a503642011-06-10 13:31:21 -0700152 private static final String TAB_3G = "3g";
153 private static final String TAB_4G = "4g";
154 private static final String TAB_MOBILE = "mobile";
155 private static final String TAB_WIFI = "wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700156 private static final String TAB_ETHERNET = "ethernet";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700157
Jeff Sharkey28130d92011-09-02 16:10:24 -0700158 private static final String TAG_CONFIRM_DATA_DISABLE = "confirmDataDisable";
159 private static final String TAG_CONFIRM_DATA_ROAMING = "confirmDataRoaming";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700160 private static final String TAG_CONFIRM_LIMIT = "confirmLimit";
161 private static final String TAG_CYCLE_EDITOR = "cycleEditor";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700162 private static final String TAG_WARNING_EDITOR = "warningEditor";
163 private static final String TAG_LIMIT_EDITOR = "limitEditor";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700164 private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700165 private static final String TAG_DENIED_RESTRICT = "deniedRestrict";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700166 private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700167 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700168
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700169 private static final int LOADER_CHART_DATA = 2;
170 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700171
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700172 private static final long KB_IN_BYTES = 1024;
173 private static final long MB_IN_BYTES = KB_IN_BYTES * 1024;
174 private static final long GB_IN_BYTES = MB_IN_BYTES * 1024;
175
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700176 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700177 private INetworkStatsService mStatsService;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700178 private INetworkPolicyManager mPolicyService;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700179 private ConnectivityManager mConnService;
180
181 private static final String PREF_FILE = "data_usage";
182 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700183 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700184
185 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700186
Jeff Sharkey8a503642011-06-10 13:31:21 -0700187 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700188 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700189 private TabWidget mTabWidget;
190 private ListView mListView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700191 private DataUsageAdapter mAdapter;
192
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700193 /** Distance to inset content from sides, when needed. */
194 private int mInsetSide = 0;
195
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700196 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700197
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700198 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700199 private LinearLayout mNetworkSwitches;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700200 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700201 private View mDataEnabledView;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700202 private CheckBox mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700203 private View mDisableAtLimitView;
204
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700205 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700206 private Spinner mCycleSpinner;
207 private CycleAdapter mCycleAdapter;
208
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700209 private ChartDataUsageView mChart;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700210 private TextView mUsageSummary;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700211 private TextView mEmpty;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700212
213 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700214 private ImageView mAppIcon;
215 private ViewGroup mAppTitles;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700216 private PieChartView mAppPieChart;
217 private TextView mAppForeground;
218 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700219 private Button mAppSettings;
220
221 private LinearLayout mAppSwitches;
222 private CheckBox mAppRestrict;
223 private View mAppRestrictView;
224
Jeff Sharkey8a503642011-06-10 13:31:21 -0700225 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700226 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700227
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700228 private NetworkTemplate mTemplate;
229 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700230
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700231 private int[] mAppDetailUids = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700232
233 private Intent mAppSettingsIntent;
234
Jeff Sharkeya662e492011-06-18 21:57:06 -0700235 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700236
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700237 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700238 private String mIntentTab = null;
239
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700240 private MenuItem mMenuDataRoaming;
241 private MenuItem mMenuRestrictBackground;
242
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700243 /** Flag used to ignore listeners during binding. */
244 private boolean mBinding;
245
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700246 private UidDetailProvider mUidDetailProvider;
247
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700248 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700249 public void onCreate(Bundle savedInstanceState) {
250 super.onCreate(savedInstanceState);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700251
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700252 mNetworkService = INetworkManagementService.Stub.asInterface(
253 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700254 mStatsService = INetworkStatsService.Stub.asInterface(
255 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700256 mPolicyService = INetworkPolicyManager.Stub.asInterface(
257 ServiceManager.getService(Context.NETWORK_POLICY_SERVICE));
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700258 mConnService = (ConnectivityManager) getActivity().getSystemService(
259 Context.CONNECTIVITY_SERVICE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700260
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700261 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700262
Jeff Sharkeya662e492011-06-18 21:57:06 -0700263 mPolicyEditor = new NetworkPolicyEditor(mPolicyService);
264 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700265
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700266 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700267 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700268
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700269 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700270 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700271
Jeff Sharkey8a503642011-06-10 13:31:21 -0700272 @Override
273 public View onCreateView(LayoutInflater inflater, ViewGroup container,
274 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700275
Jeff Sharkey8a503642011-06-10 13:31:21 -0700276 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700277 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
278
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700279 mUidDetailProvider = new UidDetailProvider(context);
280
Jeff Sharkey8a503642011-06-10 13:31:21 -0700281 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700282 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700283 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
284 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700285
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700286 // decide if we need to manually inset our content, or if we should rely
287 // on parent container for inset.
288 final boolean shouldInset = mListView.getScrollBarStyle()
289 == View.SCROLLBARS_OUTSIDE_OVERLAY;
290 if (shouldInset) {
291 mInsetSide = view.getResources().getDimensionPixelOffset(
292 com.android.internal.R.dimen.preference_fragment_padding_side);
293 } else {
294 mInsetSide = 0;
295 }
296
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700297 // adjust padding around tabwidget as needed
Jeff Sharkey5d706792011-09-08 18:57:17 -0700298 prepareCustomPreferencesList(container, view, mListView, true);
299
Jeff Sharkey8a503642011-06-10 13:31:21 -0700300 mTabHost.setup();
301 mTabHost.setOnTabChangedListener(mTabListener);
302
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700303 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700304 mHeader.setClickable(true);
305
306 mListView.addHeaderView(mHeader, null, true);
307 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700308
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700309 if (mInsetSide > 0) {
310 // inset selector and divider drawables
311 insetListViewDrawables(mListView, mInsetSide);
312 mHeader.setPadding(mInsetSide, 0, mInsetSide, 0);
313 }
314
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700315 {
316 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700317 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
318 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700319 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700320
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700321 mDataEnabled = new Switch(inflater.getContext());
322 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
323 mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener);
324 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700325
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700326 mDisableAtLimit = new CheckBox(inflater.getContext());
327 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700328 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700329 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700330 mDisableAtLimitView.setClickable(true);
331 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700332 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
333 mNetworkSwitches.addView(mDisableAtLimitView);
334 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700335
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700336 // bind cycle dropdown
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700337 mCycleView = mHeader.findViewById(R.id.cycles);
338 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700339 mCycleAdapter = new CycleAdapter(context);
340 mCycleSpinner.setAdapter(mCycleAdapter);
341 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
342
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700343 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700344 mChart.setListener(mChartListener);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700345
346 {
347 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700348 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700349 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
350 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700351 mAppPieChart = (PieChartView) mAppDetail.findViewById(R.id.app_pie_chart);
352 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
353 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700354 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700355
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700356 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700357 mAppSettings.setOnClickListener(mAppSettingsListener);
358
359 mAppRestrict = new CheckBox(inflater.getContext());
360 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700361 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700362 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700363 mAppRestrictView.setClickable(true);
364 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700365 mAppRestrictView.setOnClickListener(mAppRestrictListener);
366 mAppSwitches.addView(mAppRestrictView);
367 }
368
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700369 mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700370 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700371
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700372 // only assign layout transitions once first layout is finished
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700373 mListView.getViewTreeObserver().addOnGlobalLayoutListener(mFirstLayoutListener);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700374
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700375 mAdapter = new DataUsageAdapter(mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700376 mListView.setOnItemClickListener(mListListener);
377 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700378
379 return view;
380 }
381
382 @Override
383 public void onResume() {
384 super.onResume();
385
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700386 // pick default tab based on incoming intent
387 final Intent intent = getActivity().getIntent();
388 mIntentTab = computeTabFromIntent(intent);
389
Jeff Sharkey8a503642011-06-10 13:31:21 -0700390 // this kicks off chain reaction which creates tabs, binds the body to
391 // selected network, and binds chart, cycles and detail list.
392 updateTabs();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700393
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700394 // kick off background task to update stats
395 new AsyncTask<Void, Void, Void>() {
396 @Override
397 protected Void doInBackground(Void... params) {
398 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700399 // wait a few seconds before kicking off
400 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700401 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700402 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700403 } catch (RemoteException e) {
404 }
405 return null;
406 }
407
408 @Override
409 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700410 if (isAdded()) {
411 updateBody();
412 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700413 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700414 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700415 }
416
Jeff Sharkey8a503642011-06-10 13:31:21 -0700417 @Override
418 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
419 inflater.inflate(R.menu.data_usage, menu);
420 }
421
422 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700423 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700424 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700425 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700426
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700427 mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700428 mMenuDataRoaming.setVisible(hasMobileRadio(context) && !appDetailMode);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700429 mMenuDataRoaming.setChecked(getDataRoaming());
430
431 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700432 mMenuRestrictBackground.setVisible(!appDetailMode);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700433 mMenuRestrictBackground.setChecked(getRestrictBackground());
434
435 final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700436 split4g.setVisible(hasMobile4gRadio(context) && !appDetailMode);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700437 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700438
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700439 final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700440 if (hasWifiRadio(context) && hasMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700441 showWifi.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700442 showWifi.setChecked(mShowWifi);
443 } else {
444 showWifi.setVisible(false);
445 mShowWifi = true;
446 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700447
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700448 final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
449 if (hasEthernet(context) && hasMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700450 showEthernet.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700451 showEthernet.setChecked(mShowEthernet);
452 } else {
453 showEthernet.setVisible(false);
454 mShowEthernet = true;
455 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700456 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700457
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700458 @Override
459 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700460 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700461 case R.id.data_usage_menu_roaming: {
462 final boolean dataRoaming = !item.isChecked();
463 if (dataRoaming) {
464 ConfirmDataRoamingFragment.show(this);
465 } else {
466 // no confirmation to disable roaming
467 setDataRoaming(false);
468 }
469 return true;
470 }
471 case R.id.data_usage_menu_restrict_background: {
472 final boolean restrictBackground = !item.isChecked();
473 if (restrictBackground) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700474 if (hasLimitedNetworks()) {
475 ConfirmRestrictFragment.show(this);
476 } else {
477 DeniedRestrictFragment.show(this);
478 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700479 } else {
480 // no confirmation to drop restriction
481 setRestrictBackground(false);
482 }
483 return true;
484 }
485 case R.id.data_usage_menu_split_4g: {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700486 final boolean mobileSplit = !item.isChecked();
Jeff Sharkeya662e492011-06-18 21:57:06 -0700487 setMobilePolicySplit(mobileSplit);
488 item.setChecked(isMobilePolicySplit());
Jeff Sharkey8a503642011-06-10 13:31:21 -0700489 updateTabs();
490 return true;
491 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700492 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700493 mShowWifi = !item.isChecked();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700494 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700495 item.setChecked(mShowWifi);
496 updateTabs();
497 return true;
498 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700499 case R.id.data_usage_menu_show_ethernet: {
500 mShowEthernet = !item.isChecked();
501 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
502 item.setChecked(mShowEthernet);
503 updateTabs();
504 return true;
505 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700506 }
507 return false;
508 }
509
Jeff Sharkey94a90952011-06-13 22:31:09 -0700510 @Override
511 public void onDestroyView() {
512 super.onDestroyView();
513
514 mDataEnabledView = null;
515 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700516
517 mUidDetailProvider.clearCache();
518 mUidDetailProvider = null;
Jeff Sharkey94a90952011-06-13 22:31:09 -0700519 }
520
Jeff Sharkey8a503642011-06-10 13:31:21 -0700521 /**
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700522 * Listener to setup {@link LayoutTransition} after first layout pass.
523 */
524 private OnGlobalLayoutListener mFirstLayoutListener = new OnGlobalLayoutListener() {
525 /** {@inheritDoc} */
526 public void onGlobalLayout() {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700527 mListView.getViewTreeObserver().removeGlobalOnLayoutListener(mFirstLayoutListener);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700528
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700529 mTabsContainer.setLayoutTransition(buildLayoutTransition());
530 mHeader.setLayoutTransition(buildLayoutTransition());
531 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700532
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700533 final LayoutTransition chartTransition = buildLayoutTransition();
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700534 chartTransition.setStartDelay(LayoutTransition.APPEARING, 0);
535 chartTransition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700536 chartTransition.setAnimator(LayoutTransition.APPEARING, null);
Jeff Sharkey28130d92011-09-02 16:10:24 -0700537 chartTransition.setAnimator(LayoutTransition.DISAPPEARING, null);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700538 mChart.setLayoutTransition(chartTransition);
539 }
540 };
541
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700542 private static LayoutTransition buildLayoutTransition() {
543 final LayoutTransition transition = new LayoutTransition();
544 if (TEST_ANIM) {
545 transition.setDuration(1500);
546 }
547 transition.setAnimateParentHierarchy(false);
548 return transition;
549 }
550
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700551 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700552 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700553 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
554 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700555 */
556 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700557 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700558 mTabHost.clearAllTabs();
559
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700560 final boolean mobileSplit = isMobilePolicySplit();
561 if (mobileSplit && hasMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700562 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
563 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700564 } else if (hasMobileRadio(context)) {
565 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700566 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700567 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700568 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
569 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700570 if (mShowEthernet && hasEthernet(context)) {
571 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
572 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700573
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700574 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
575 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
576 if (mIntentTab != null) {
577 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700578 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700579 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700580 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700581 mTabHost.setCurrentTabByTag(mIntentTab);
582 }
583 mIntentTab = null;
584 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700585 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700586 }
587 }
588
Jeff Sharkey8a503642011-06-10 13:31:21 -0700589 /**
590 * Factory that provide empty {@link View} to make {@link TabHost} happy.
591 */
592 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
593 /** {@inheritDoc} */
594 public View createTabContent(String tag) {
595 return new View(mTabHost.getContext());
596 }
597 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700598
Jeff Sharkey8a503642011-06-10 13:31:21 -0700599 /**
600 * Build {@link TabSpec} with thin indicator, and empty content.
601 */
602 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700603 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
604 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700605 }
606
607 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
608 /** {@inheritDoc} */
609 public void onTabChanged(String tabId) {
610 // user changed tab; update body
611 updateBody();
612 }
613 };
614
615 /**
616 * Update body content based on current tab. Loads
617 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
618 * binds them to visible controls.
619 */
620 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700621 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700622 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700623
Jeff Sharkeya662e492011-06-18 21:57:06 -0700624 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700625 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700626
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700627 if (currentTab == null) {
628 Log.w(TAG, "no tab selected; hiding body");
629 mListView.setVisibility(View.GONE);
630 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700631 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700632 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700633 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700634
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700635 final boolean tabChanged = !currentTab.equals(mCurrentTab);
636 mCurrentTab = currentTab;
637
Jeff Sharkey8a503642011-06-10 13:31:21 -0700638 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
639
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700640 mDataEnabledView.setVisibility(View.VISIBLE);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700641
642 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700643 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
644 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700645 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700646
647 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700648 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
649 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
650 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700651 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700652
653 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700654 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
655 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
656 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700657 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700658
659 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700660 // wifi doesn't have any controls
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700661 mDataEnabledView.setVisibility(View.GONE);
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700662 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700663 mTemplate = buildTemplateWifi();
664
665 } else if (TAB_ETHERNET.equals(currentTab)) {
666 // ethernet doesn't have any controls
667 mDataEnabledView.setVisibility(View.GONE);
668 mDisableAtLimitView.setVisibility(View.GONE);
669 mTemplate = buildTemplateEthernet();
670
671 } else {
672 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700673 }
674
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700675 // kick off loader for network history
676 // TODO: consider chaining two loaders together instead of reloading
677 // network history when showing app detail.
678 getLoaderManager().restartLoader(LOADER_CHART_DATA,
679 ChartDataLoader.buildArgs(mTemplate, mAppDetailUids), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700680
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700681 // detail mode can change visible menus, invalidate
682 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700683
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700684 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700685 }
686
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700687 private boolean isAppDetailMode() {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700688 return mAppDetailUids != null;
689 }
690
691 private int getAppDetailPrimaryUid() {
692 return mAppDetailUids[0];
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700693 }
694
695 /**
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700696 * Update UID details panels to match {@link #mAppDetailUids}, showing or
697 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700698 */
699 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700700 final Context context = getActivity();
701 final PackageManager pm = context.getPackageManager();
702 final LayoutInflater inflater = getActivity().getLayoutInflater();
703
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700704 if (isAppDetailMode()) {
705 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700706 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700707 } else {
708 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700709 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700710
711 // hide detail stats when not in detail mode
712 mChart.bindDetailNetworkStats(null);
713 return;
714 }
715
716 // remove warning/limit sweeps while in detail mode
717 mChart.bindNetworkPolicy(null);
718
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700719 // show icon and all labels appearing under this app
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700720 final int primaryUid = getAppDetailPrimaryUid();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700721 final UidDetail detail = mUidDetailProvider.getUidDetail(primaryUid, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700722 mAppIcon.setImageDrawable(detail.icon);
723
724 mAppTitles.removeAllViews();
725 if (detail.detailLabels != null) {
726 for (CharSequence label : detail.detailLabels) {
727 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label));
728 }
729 } else {
730 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label));
731 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700732
733 // enable settings button when package provides it
734 // TODO: target torwards entire UID instead of just first package
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700735 final String[] packageNames = pm.getPackagesForUid(primaryUid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700736 if (packageNames != null && packageNames.length > 0) {
737 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
738 mAppSettingsIntent.setPackage(packageNames[0]);
739 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
740
741 final boolean matchFound = pm.resolveActivity(mAppSettingsIntent, 0) != null;
742 mAppSettings.setEnabled(matchFound);
743
744 } else {
745 mAppSettingsIntent = null;
746 mAppSettings.setEnabled(false);
747 }
748
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700749 updateDetailData();
750
751 if (NetworkPolicyManager.isUidValidForPolicy(context, primaryUid)
752 && !getRestrictBackground() && isBandwidthControlEnabled()) {
753 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
754 setPreferenceSummary(mAppRestrictView,
755 getString(R.string.data_usage_app_restrict_background_summary,
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700756 buildLimitedNetworksString()));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700757
758 mAppRestrictView.setVisibility(View.VISIBLE);
759 mAppRestrict.setChecked(getAppRestrictBackground());
760
761 } else {
762 mAppRestrictView.setVisibility(View.GONE);
763 }
764 }
765
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700766 private void setPolicyWarningBytes(long warningBytes) {
767 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700768 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700769 updatePolicy(false);
770 }
771
772 private void setPolicyLimitBytes(long limitBytes) {
773 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700774 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700775 updatePolicy(false);
776 }
777
Jeff Sharkey28130d92011-09-02 16:10:24 -0700778 /**
779 * Local cache of value, used to work around delay when
780 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
781 */
782 private Boolean mMobileDataEnabled;
783
784 private boolean isMobileDataEnabled() {
785 if (mMobileDataEnabled != null) {
786 // TODO: deprecate and remove this once enabled flag is on policy
787 return mMobileDataEnabled;
788 } else {
789 return mConnService.getMobileDataEnabled();
790 }
791 }
792
793 private void setMobileDataEnabled(boolean enabled) {
794 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
795 mConnService.setMobileDataEnabled(enabled);
796 mMobileDataEnabled = enabled;
797 updatePolicy(false);
798 }
799
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700800 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
801 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked();
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700802 }
803
804 private boolean isBandwidthControlEnabled() {
805 try {
806 return mNetworkService.isBandwidthControlEnabled();
807 } catch (RemoteException e) {
808 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
809 return false;
810 }
811 }
812
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700813 private boolean getDataRoaming() {
814 final ContentResolver resolver = getActivity().getContentResolver();
815 return Settings.Secure.getInt(resolver, Settings.Secure.DATA_ROAMING, 0) != 0;
816 }
817
818 private void setDataRoaming(boolean enabled) {
819 // TODO: teach telephony DataConnectionTracker to watch and apply
820 // updates when changed.
821 final ContentResolver resolver = getActivity().getContentResolver();
822 Settings.Secure.putInt(resolver, Settings.Secure.DATA_ROAMING, enabled ? 1 : 0);
823 mMenuDataRoaming.setChecked(enabled);
824 }
825
826 private boolean getRestrictBackground() {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700827 try {
828 return mPolicyService.getRestrictBackground();
829 } catch (RemoteException e) {
830 Log.w(TAG, "problem talking with policy service: " + e);
831 return false;
832 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700833 }
834
835 private void setRestrictBackground(boolean restrictBackground) {
836 if (LOGD) Log.d(TAG, "setRestrictBackground()");
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700837 try {
838 mPolicyService.setRestrictBackground(restrictBackground);
839 mMenuRestrictBackground.setChecked(restrictBackground);
840 } catch (RemoteException e) {
841 Log.w(TAG, "problem talking with policy service: " + e);
842 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700843 }
844
845 private boolean getAppRestrictBackground() {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700846 final int primaryUid = getAppDetailPrimaryUid();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700847 final int uidPolicy;
848 try {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700849 uidPolicy = mPolicyService.getUidPolicy(primaryUid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700850 } catch (RemoteException e) {
851 // since we can't do much without policy, we bail hard.
852 throw new RuntimeException("problem reading network policy", e);
853 }
854
855 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
856 }
857
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700858 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700859 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700860 final int primaryUid = getAppDetailPrimaryUid();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700861 try {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700862 mPolicyService.setUidPolicy(primaryUid,
863 restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700864 } catch (RemoteException e) {
865 throw new RuntimeException("unable to save policy", e);
866 }
867
868 mAppRestrict.setChecked(restrictBackground);
869 }
870
Jeff Sharkey8a503642011-06-10 13:31:21 -0700871 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700872 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
873 * current {@link #mTemplate}.
874 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700875 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700876 if (isAppDetailMode()) {
877 mNetworkSwitches.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700878 } else {
879 mNetworkSwitches.setVisibility(View.VISIBLE);
880 }
881
Jeff Sharkey28130d92011-09-02 16:10:24 -0700882 // TODO: move enabled state directly into policy
883 if (TAB_MOBILE.equals(mCurrentTab)) {
884 mBinding = true;
885 mDataEnabled.setChecked(isMobileDataEnabled());
886 mBinding = false;
887 }
888
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700889 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
890 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700891 mDisableAtLimitView.setVisibility(View.VISIBLE);
892 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700893 if (!isAppDetailMode()) {
894 mChart.bindNetworkPolicy(policy);
895 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700896
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700897 } else {
898 // controls are disabled; don't bind warning/limit sweeps
899 mDisableAtLimitView.setVisibility(View.GONE);
900 mChart.bindNetworkPolicy(null);
901 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700902
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700903 if (refreshCycle) {
904 // generate cycle list based on policy and available history
905 updateCycleList(policy);
906 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700907 }
908
909 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700910 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
911 * and available {@link NetworkStatsHistory} data. Always selects the newest
912 * item, updating the inspection range on {@link #mChart}.
913 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700914 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700915 // stash away currently selected cycle to try restoring below
916 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700917 mCycleAdapter.clear();
918
919 final Context context = mCycleSpinner.getContext();
920
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700921 long historyStart = Long.MAX_VALUE;
922 long historyEnd = Long.MIN_VALUE;
923 if (mChartData != null) {
924 historyStart = mChartData.network.getStart();
925 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700926 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700927
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700928 if (historyStart == Long.MAX_VALUE) historyStart = System.currentTimeMillis();
929 if (historyEnd == Long.MIN_VALUE) historyEnd = System.currentTimeMillis();
930
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700931 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700932 if (policy != null) {
933 // find the next cycle boundary
934 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700935
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700936 // walk backwards, generating all valid cycle ranges
937 while (cycleEnd > historyStart) {
938 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
939 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
940 + historyStart);
941 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
942 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700943 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700944 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700945
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700946 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700947 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700948 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700949
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700950 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700951 // no policy defined cycles; show entry for each four-week period
952 long cycleEnd = historyEnd;
953 while (cycleEnd > historyStart) {
954 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
955 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
956 cycleEnd = cycleStart;
957 }
958
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700959 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700960 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700961
962 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700963 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700964 final int position = mCycleAdapter.findNearestPosition(previousItem);
965 mCycleSpinner.setSelection(position);
966
967 // only force-update cycle when changed; skipping preserves any
968 // user-defined inspection region.
969 final CycleItem selectedItem = mCycleAdapter.getItem(position);
970 if (!Objects.equal(selectedItem, previousItem)) {
971 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
972 } else {
973 // but still kick off loader for detailed list
974 updateDetailData();
975 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700976 } else {
977 updateDetailData();
978 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700979 }
980
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700981 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700982 /** {@inheritDoc} */
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700983 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
984 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700985
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700986 final boolean dataEnabled = isChecked;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700987 final String currentTab = mCurrentTab;
988 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -0700989 if (dataEnabled) {
990 setMobileDataEnabled(true);
991 } else {
992 // disabling data; show confirmation dialog which eventually
993 // calls setMobileDataEnabled() once user confirms.
994 ConfirmDataDisableFragment.show(DataUsageSummary.this);
995 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700996 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700997
Jeff Sharkey28130d92011-09-02 16:10:24 -0700998 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700999 }
1000 };
1001
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001002 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001003 /** {@inheritDoc} */
1004 public void onClick(View v) {
1005 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001006 if (disableAtLimit) {
1007 // enabling limit; show confirmation dialog which eventually
1008 // calls setPolicyLimitBytes() once user confirms.
1009 ConfirmLimitFragment.show(DataUsageSummary.this);
1010 } else {
1011 setPolicyLimitBytes(LIMIT_DISABLED);
1012 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001013 }
1014 };
1015
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001016 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
1017 /** {@inheritDoc} */
1018 public void onClick(View v) {
1019 final boolean restrictBackground = !mAppRestrict.isChecked();
1020
1021 if (restrictBackground) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001022 if (hasLimitedNetworks()) {
1023 // enabling restriction; show confirmation dialog which
1024 // eventually calls setRestrictBackground() once user
1025 // confirms.
1026 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
1027 } else {
1028 // no limited networks; show dialog to guide user towards
1029 // setting a network limit. doesn't mutate restrict state.
1030 DeniedRestrictFragment.show(DataUsageSummary.this);
1031 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001032 } else {
1033 setAppRestrictBackground(false);
1034 }
1035 }
1036 };
1037
1038 private OnClickListener mAppSettingsListener = new OnClickListener() {
1039 /** {@inheritDoc} */
1040 public void onClick(View v) {
1041 // TODO: target torwards entire UID instead of just first package
1042 startActivity(mAppSettingsIntent);
1043 }
1044 };
1045
Jeff Sharkey8a503642011-06-10 13:31:21 -07001046 private OnItemClickListener mListListener = new OnItemClickListener() {
1047 /** {@inheritDoc} */
1048 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001049 final Context context = view.getContext();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001050 final AppUsageItem app = (AppUsageItem) parent.getItemAtPosition(position);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001051 final UidDetail detail = mUidDetailProvider.getUidDetail(app.uids[0], true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001052 AppDetailsFragment.show(DataUsageSummary.this, app.uids, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001053 }
1054 };
1055
1056 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
1057 /** {@inheritDoc} */
1058 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1059 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1060 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001061 // show cycle editor; will eventually call setPolicyCycleDay()
1062 // when user finishes editing.
1063 CycleEditorFragment.show(DataUsageSummary.this);
1064
1065 // reset spinner to something other than "change cycle..."
1066 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001067
1068 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001069 if (LOGD) {
1070 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1071 + cycle.end + "]");
1072 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001073
1074 // update chart to show selected cycle, and update detail data
1075 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001076 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001077
1078 updateDetailData();
1079 }
1080 }
1081
1082 /** {@inheritDoc} */
1083 public void onNothingSelected(AdapterView<?> parent) {
1084 // ignored
1085 }
1086 };
1087
1088 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001089 * Update details based on {@link #mChart} inspection range depending on
1090 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1091 * of applications data usage, and when {@link #isAppDetailMode()} update
1092 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001093 */
1094 private void updateDetailData() {
1095 if (LOGD) Log.d(TAG, "updateDetailData()");
1096
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001097 final long start = mChart.getInspectStart();
1098 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001099 final long now = System.currentTimeMillis();
1100
1101 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001102
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001103 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001104 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001105 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001106 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001107 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001108 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001109 final long foregroundBytes = entry.rxBytes + entry.txBytes;
1110
1111 mAppPieChart.setOriginAngle(175);
1112
1113 mAppPieChart.removeAllSlices();
1114 mAppPieChart.addSlice(foregroundBytes, Color.parseColor("#d88d3a"));
1115 mAppPieChart.addSlice(defaultBytes, Color.parseColor("#666666"));
1116
1117 mAppPieChart.generatePath();
1118
1119 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1120 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1121
1122 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001123 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001124
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001125 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001126
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001127 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001128 if (mChartData != null) {
1129 entry = mChartData.network.getValues(start, end, now, null);
1130 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001131
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001132 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001133 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001134 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001135 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001136
1137 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1138 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001139 final String rangePhrase = formatDateRange(context, start, end, false);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001140
1141 mUsageSummary.setText(
1142 getString(R.string.data_usage_total_during_range, totalPhrase, rangePhrase));
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001143 }
1144
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001145 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1146 ChartData>() {
1147 /** {@inheritDoc} */
1148 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
1149 return new ChartDataLoader(getActivity(), mStatsService, args);
1150 }
1151
1152 /** {@inheritDoc} */
1153 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1154 mChartData = data;
1155 mChart.bindNetworkStats(mChartData.network);
1156 mChart.bindDetailNetworkStats(mChartData.detail);
1157
1158 // calcuate policy cycles based on available data
1159 updatePolicy(true);
1160 updateAppDetail();
1161
1162 // force scroll to top of body when showing detail
1163 if (mChartData.detail != null) {
1164 mListView.smoothScrollToPosition(0);
1165 }
1166 }
1167
1168 /** {@inheritDoc} */
1169 public void onLoaderReset(Loader<ChartData> loader) {
1170 mChartData = null;
1171 mChart.bindNetworkStats(null);
1172 mChart.bindDetailNetworkStats(null);
1173 }
1174 };
1175
1176 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001177 NetworkStats>() {
1178 /** {@inheritDoc} */
1179 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
1180 return new SummaryForAllUidLoader(getActivity(), mStatsService, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001181 }
1182
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001183 /** {@inheritDoc} */
1184 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
1185 mAdapter.bindStats(data);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001186 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001187 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001188
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001189 /** {@inheritDoc} */
1190 public void onLoaderReset(Loader<NetworkStats> loader) {
1191 mAdapter.bindStats(null);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001192 updateEmptyVisible();
1193 }
1194
1195 private void updateEmptyVisible() {
1196 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1197 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001198 }
1199 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001200
Jeff Sharkeya662e492011-06-18 21:57:06 -07001201 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001202 final Context context = getActivity();
1203 if (hasMobileRadio(context)) {
1204 final String subscriberId = getActiveSubscriberId(context);
1205 return mPolicyEditor.isMobilePolicySplit(subscriberId);
1206 } else {
1207 return false;
1208 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001209 }
1210
1211 private void setMobilePolicySplit(boolean split) {
1212 final String subscriberId = getActiveSubscriberId(getActivity());
1213 mPolicyEditor.setMobilePolicySplit(subscriberId, split);
1214 }
1215
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001216 private static String getActiveSubscriberId(Context context) {
1217 final TelephonyManager telephony = (TelephonyManager) context.getSystemService(
1218 Context.TELEPHONY_SERVICE);
1219 return telephony.getSubscriberId();
1220 }
1221
Jeff Sharkey8a503642011-06-10 13:31:21 -07001222 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
1223 /** {@inheritDoc} */
1224 public void onInspectRangeChanged() {
1225 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
1226 updateDetailData();
1227 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001228
Jeff Sharkey8a503642011-06-10 13:31:21 -07001229 /** {@inheritDoc} */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001230 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001231 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001232 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001233
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001234 /** {@inheritDoc} */
1235 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001236 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001237 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001238
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001239 /** {@inheritDoc} */
1240 public void requestWarningEdit() {
1241 WarningEditorFragment.show(DataUsageSummary.this);
1242 }
1243
1244 /** {@inheritDoc} */
1245 public void requestLimitEdit() {
1246 LimitEditorFragment.show(DataUsageSummary.this);
1247 }
1248 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001249
1250 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001251 * List item that reflects a specific data usage cycle.
1252 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001253 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001254 public CharSequence label;
1255 public long start;
1256 public long end;
1257
Jeff Sharkey8a503642011-06-10 13:31:21 -07001258 CycleItem(CharSequence label) {
1259 this.label = label;
1260 }
1261
1262 public CycleItem(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001263 this.label = formatDateRange(context, start, end, true);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001264 this.start = start;
1265 this.end = end;
1266 }
1267
Jeff Sharkey8a503642011-06-10 13:31:21 -07001268 @Override
1269 public String toString() {
1270 return label.toString();
1271 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001272
1273 @Override
1274 public boolean equals(Object o) {
1275 if (o instanceof CycleItem) {
1276 final CycleItem another = (CycleItem) o;
1277 return start == another.start && end == another.end;
1278 }
1279 return false;
1280 }
1281
1282 /** {@inheritDoc} */
1283 public int compareTo(CycleItem another) {
1284 return Long.compare(start, another.start);
1285 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001286 }
1287
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001288 private static final StringBuilder sBuilder = new StringBuilder(50);
1289 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1290 sBuilder, Locale.getDefault());
1291
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001292 public static String formatDateRange(Context context, long start, long end, boolean utcTime) {
1293 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
1294 final String timezone = utcTime ? TIMEZONE_UTC : null;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001295
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001296 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001297 sBuilder.setLength(0);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001298 return DateUtils
1299 .formatDateRange(context, sFormatter, start, end, flags, timezone).toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001300 }
1301 }
1302
Jeff Sharkey8a503642011-06-10 13:31:21 -07001303 /**
1304 * Special-case data usage cycle that triggers dialog to change
1305 * {@link NetworkPolicy#cycleDay}.
1306 */
1307 public static class CycleChangeItem extends CycleItem {
1308 public CycleChangeItem(Context context) {
1309 super(context.getString(R.string.data_usage_change_cycle));
1310 }
1311 }
1312
1313 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001314 private boolean mChangePossible = false;
1315 private boolean mChangeVisible = false;
1316
1317 private final CycleChangeItem mChangeItem;
1318
Jeff Sharkey8a503642011-06-10 13:31:21 -07001319 public CycleAdapter(Context context) {
1320 super(context, android.R.layout.simple_spinner_item);
1321 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001322 mChangeItem = new CycleChangeItem(context);
1323 }
1324
1325 public void setChangePossible(boolean possible) {
1326 mChangePossible = possible;
1327 updateChange();
1328 }
1329
1330 public void setChangeVisible(boolean visible) {
1331 mChangeVisible = visible;
1332 updateChange();
1333 }
1334
1335 private void updateChange() {
1336 remove(mChangeItem);
1337 if (mChangePossible && mChangeVisible) {
1338 add(mChangeItem);
1339 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001340 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001341
1342 /**
1343 * Find position of {@link CycleItem} in this adapter which is nearest
1344 * the given {@link CycleItem}.
1345 */
1346 public int findNearestPosition(CycleItem target) {
1347 if (target != null) {
1348 final int count = getCount();
1349 for (int i = count - 1; i >= 0; i--) {
1350 final CycleItem item = getItem(i);
1351 if (item instanceof CycleChangeItem) {
1352 continue;
1353 } else if (item.compareTo(target) >= 0) {
1354 return i;
1355 }
1356 }
1357 }
1358 return 0;
1359 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001360 }
1361
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001362 private static class AppUsageItem implements Comparable<AppUsageItem> {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001363 public int[] uids;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001364 public long total;
1365
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001366 public AppUsageItem(int uid) {
1367 uids = new int[] { uid };
1368 }
1369
1370 public void addUid(int uid) {
1371 if (contains(uids, uid)) return;
1372 final int length = uids.length;
1373 uids = Arrays.copyOf(uids, length + 1);
1374 uids[length] = uid;
1375 }
1376
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001377 /** {@inheritDoc} */
1378 public int compareTo(AppUsageItem another) {
1379 return Long.compare(another.total, total);
1380 }
1381 }
1382
Jeff Sharkey8a503642011-06-10 13:31:21 -07001383 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001384 * Adapter of applications, sorted by total usage descending.
1385 */
1386 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001387 private final UidDetailProvider mProvider;
1388 private final int mInsetSide;
1389
Jeff Sharkey8a503642011-06-10 13:31:21 -07001390 private ArrayList<AppUsageItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001391 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001392
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001393 public DataUsageAdapter(UidDetailProvider provider, int insetSide) {
1394 mProvider = checkNotNull(provider);
1395 mInsetSide = insetSide;
1396 }
1397
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001398 /**
1399 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1400 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001401 public void bindStats(NetworkStats stats) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001402 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001403
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001404 final AppUsageItem systemItem = new AppUsageItem(android.os.Process.SYSTEM_UID);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001405 final SparseArray<AppUsageItem> knownUids = new SparseArray<AppUsageItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001406
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001407 NetworkStats.Entry entry = null;
1408 final int size = stats != null ? stats.size() : 0;
1409 for (int i = 0; i < size; i++) {
1410 entry = stats.getValues(i, entry);
1411
1412 final int uid = entry.uid;
1413 final boolean isApp = uid >= android.os.Process.FIRST_APPLICATION_UID
1414 && uid <= android.os.Process.LAST_APPLICATION_UID;
Jeff Sharkeya83a24f2011-09-16 01:52:39 -07001415 if (isApp || uid == UID_REMOVED || uid == UID_TETHERING) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001416 AppUsageItem item = knownUids.get(uid);
1417 if (item == null) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001418 item = new AppUsageItem(uid);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001419 knownUids.put(uid, item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001420 mItems.add(item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001421 }
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001422
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001423 item.total += entry.rxBytes + entry.txBytes;
1424 } else {
1425 systemItem.total += entry.rxBytes + entry.txBytes;
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001426 systemItem.addUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001427 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001428 }
1429
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001430 if (systemItem.total > 0) {
1431 mItems.add(systemItem);
1432 }
1433
Jeff Sharkey8a503642011-06-10 13:31:21 -07001434 Collections.sort(mItems);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001435 mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001436 notifyDataSetChanged();
1437 }
1438
1439 @Override
1440 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001441 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001442 }
1443
1444 @Override
1445 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001446 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001447 }
1448
1449 @Override
1450 public long getItemId(int position) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001451 return mItems.get(position).uids[0];
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001452 }
1453
1454 @Override
1455 public View getView(int position, View convertView, ViewGroup parent) {
1456 if (convertView == null) {
1457 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey5d706792011-09-08 18:57:17 -07001458 R.layout.data_usage_item, parent, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001459
1460 if (mInsetSide > 0) {
1461 convertView.setPadding(mInsetSide, 0, mInsetSide, 0);
1462 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001463 }
1464
1465 final Context context = parent.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001466
Jeff Sharkey28130d92011-09-02 16:10:24 -07001467 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001468 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1469 android.R.id.progress);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001470
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001471 // kick off async load of app details
Jeff Sharkey8a503642011-06-10 13:31:21 -07001472 final AppUsageItem item = mItems.get(position);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001473 UidDetailTask.bindView(mProvider, item, convertView);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001474
Jeff Sharkey28130d92011-09-02 16:10:24 -07001475 text1.setText(Formatter.formatFileSize(context, item.total));
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001476
1477 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1478 progress.setProgress(percentTotal);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001479
1480 return convertView;
1481 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001482 }
1483
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001484 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001485 * Empty {@link Fragment} that controls display of UID details in
1486 * {@link DataUsageSummary}.
1487 */
1488 public static class AppDetailsFragment extends Fragment {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001489 private static final String EXTRA_UIDS = "uids";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001490
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001491 public static void show(DataUsageSummary parent, int[] uids, CharSequence label) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001492 final Bundle args = new Bundle();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001493 args.putIntArray(EXTRA_UIDS, uids);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001494
1495 final AppDetailsFragment fragment = new AppDetailsFragment();
1496 fragment.setArguments(args);
1497 fragment.setTargetFragment(parent, 0);
1498
1499 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1500 ft.add(fragment, TAG_APP_DETAILS);
1501 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001502 ft.setBreadCrumbTitle(label);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001503 ft.commit();
1504 }
1505
1506 @Override
1507 public void onStart() {
1508 super.onStart();
1509 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001510 target.mAppDetailUids = getArguments().getIntArray(EXTRA_UIDS);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001511 target.updateBody();
1512 }
1513
1514 @Override
1515 public void onStop() {
1516 super.onStop();
1517 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001518 target.mAppDetailUids = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001519 target.updateBody();
1520 }
1521 }
1522
1523 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001524 * Dialog to request user confirmation before setting
1525 * {@link NetworkPolicy#limitBytes}.
1526 */
1527 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001528 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001529 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001530
1531 public static void show(DataUsageSummary parent) {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001532 final Resources res = parent.getResources();
1533
1534 final CharSequence message;
1535 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001536
1537 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001538 final String currentTab = parent.mCurrentTab;
1539 if (TAB_3G.equals(currentTab)) {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001540 message = buildDialogMessage(res, R.string.data_usage_tab_3g);
1541 limitBytes = 5 * GB_IN_BYTES;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001542 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001543 message = buildDialogMessage(res, R.string.data_usage_tab_4g);
1544 limitBytes = 5 * GB_IN_BYTES;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001545 } else if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001546 message = buildDialogMessage(res, R.string.data_usage_list_mobile);
1547 limitBytes = 5 * GB_IN_BYTES;
1548 } else if (TAB_WIFI.equals(currentTab)) {
1549 message = buildDialogMessage(res, R.string.data_usage_tab_wifi);
1550 limitBytes = 5 * GB_IN_BYTES;
1551 } else {
1552 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001553 }
1554
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001555 final Bundle args = new Bundle();
1556 args.putCharSequence(EXTRA_MESSAGE, message);
1557 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1558
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001559 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1560 dialog.setArguments(args);
1561 dialog.setTargetFragment(parent, 0);
1562 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1563 }
1564
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001565 private static CharSequence buildDialogMessage(Resources res, int networkResId) {
1566 return res.getString(R.string.data_usage_limit_dialog, res.getString(networkResId));
1567 }
1568
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001569 @Override
1570 public Dialog onCreateDialog(Bundle savedInstanceState) {
1571 final Context context = getActivity();
1572
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001573 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001574 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1575
1576 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1577 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001578 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001579
1580 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1581 public void onClick(DialogInterface dialog, int which) {
1582 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1583 if (target != null) {
1584 target.setPolicyLimitBytes(limitBytes);
1585 }
1586 }
1587 });
1588
1589 return builder.create();
1590 }
1591 }
1592
1593 /**
1594 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1595 */
1596 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001597 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001598
1599 public static void show(DataUsageSummary parent) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001600 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001601 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001602
1603 final CycleEditorFragment dialog = new CycleEditorFragment();
1604 dialog.setArguments(args);
1605 dialog.setTargetFragment(parent, 0);
1606 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1607 }
1608
1609 @Override
1610 public Dialog onCreateDialog(Bundle savedInstanceState) {
1611 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001612 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1613 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001614
1615 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1616 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1617
1618 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1619 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1620
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001621 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1622 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001623
1624 cycleDayPicker.setMinValue(1);
1625 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001626 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001627 cycleDayPicker.setWrapSelectorWheel(true);
1628
1629 builder.setTitle(R.string.data_usage_cycle_editor_title);
1630 builder.setView(view);
1631
1632 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1633 new DialogInterface.OnClickListener() {
1634 public void onClick(DialogInterface dialog, int which) {
1635 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001636 editor.setPolicyCycleDay(template, cycleDay);
1637 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001638 }
1639 });
1640
1641 return builder.create();
1642 }
1643 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001644
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001645 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001646 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1647 */
1648 public static class WarningEditorFragment extends DialogFragment {
1649 private static final String EXTRA_TEMPLATE = "template";
1650
1651 public static void show(DataUsageSummary parent) {
1652 final Bundle args = new Bundle();
1653 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1654
1655 final WarningEditorFragment dialog = new WarningEditorFragment();
1656 dialog.setArguments(args);
1657 dialog.setTargetFragment(parent, 0);
1658 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1659 }
1660
1661 @Override
1662 public Dialog onCreateDialog(Bundle savedInstanceState) {
1663 final Context context = getActivity();
1664 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1665 final NetworkPolicyEditor editor = target.mPolicyEditor;
1666
1667 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1668 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1669
1670 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1671 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1672
1673 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1674 final long warningBytes = editor.getPolicyWarningBytes(template);
1675 final long limitBytes = editor.getPolicyLimitBytes(template);
1676
1677 bytesPicker.setMinValue(0);
1678 if (limitBytes != LIMIT_DISABLED) {
1679 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1680 } else {
1681 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1682 }
1683 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1684 bytesPicker.setWrapSelectorWheel(false);
1685
1686 builder.setTitle(R.string.data_usage_warning_editor_title);
1687 builder.setView(view);
1688
1689 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1690 new DialogInterface.OnClickListener() {
1691 public void onClick(DialogInterface dialog, int which) {
1692 // clear focus to finish pending text edits
1693 bytesPicker.clearFocus();
1694
1695 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1696 editor.setPolicyWarningBytes(template, bytes);
1697 target.updatePolicy(false);
1698 }
1699 });
1700
1701 return builder.create();
1702 }
1703 }
1704
1705 /**
1706 * Dialog to edit {@link NetworkPolicy#limitBytes}.
1707 */
1708 public static class LimitEditorFragment extends DialogFragment {
1709 private static final String EXTRA_TEMPLATE = "template";
1710
1711 public static void show(DataUsageSummary parent) {
1712 final Bundle args = new Bundle();
1713 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1714
1715 final LimitEditorFragment dialog = new LimitEditorFragment();
1716 dialog.setArguments(args);
1717 dialog.setTargetFragment(parent, 0);
1718 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
1719 }
1720
1721 @Override
1722 public Dialog onCreateDialog(Bundle savedInstanceState) {
1723 final Context context = getActivity();
1724 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1725 final NetworkPolicyEditor editor = target.mPolicyEditor;
1726
1727 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1728 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1729
1730 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1731 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1732
1733 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1734 final long warningBytes = editor.getPolicyWarningBytes(template);
1735 final long limitBytes = editor.getPolicyLimitBytes(template);
1736
1737 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1738 if (warningBytes != WARNING_DISABLED) {
1739 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
1740 } else {
1741 bytesPicker.setMinValue(0);
1742 }
1743 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
1744 bytesPicker.setWrapSelectorWheel(false);
1745
1746 builder.setTitle(R.string.data_usage_limit_editor_title);
1747 builder.setView(view);
1748
1749 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1750 new DialogInterface.OnClickListener() {
1751 public void onClick(DialogInterface dialog, int which) {
1752 // clear focus to finish pending text edits
1753 bytesPicker.clearFocus();
1754
1755 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1756 editor.setPolicyLimitBytes(template, bytes);
1757 target.updatePolicy(false);
1758 }
1759 });
1760
1761 return builder.create();
1762 }
1763 }
1764 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07001765 * Dialog to request user confirmation before disabling data.
1766 */
1767 public static class ConfirmDataDisableFragment extends DialogFragment {
1768 public static void show(DataUsageSummary parent) {
1769 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
1770 dialog.setTargetFragment(parent, 0);
1771 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
1772 }
1773
1774 @Override
1775 public Dialog onCreateDialog(Bundle savedInstanceState) {
1776 final Context context = getActivity();
1777
1778 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1779 builder.setMessage(R.string.data_usage_disable_mobile);
1780
1781 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1782 public void onClick(DialogInterface dialog, int which) {
1783 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1784 if (target != null) {
1785 // TODO: extend to modify policy enabled flag.
1786 target.setMobileDataEnabled(false);
1787 }
1788 }
1789 });
1790 builder.setNegativeButton(android.R.string.cancel, null);
1791
1792 return builder.create();
1793 }
1794 }
1795
1796 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001797 * Dialog to request user confirmation before setting
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001798 * {@link Settings.Secure#DATA_ROAMING}.
1799 */
1800 public static class ConfirmDataRoamingFragment extends DialogFragment {
1801 public static void show(DataUsageSummary parent) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001802 final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment();
1803 dialog.setTargetFragment(parent, 0);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001804 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_ROAMING);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001805 }
1806
1807 @Override
1808 public Dialog onCreateDialog(Bundle savedInstanceState) {
1809 final Context context = getActivity();
1810
1811 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1812 builder.setTitle(R.string.roaming_reenable_title);
1813 builder.setMessage(R.string.roaming_warning);
1814
1815 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1816 public void onClick(DialogInterface dialog, int which) {
1817 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1818 if (target != null) {
1819 target.setDataRoaming(true);
1820 }
1821 }
1822 });
1823 builder.setNegativeButton(android.R.string.cancel, null);
1824
1825 return builder.create();
1826 }
1827 }
1828
1829 /**
1830 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001831 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001832 */
1833 public static class ConfirmRestrictFragment extends DialogFragment {
1834 public static void show(DataUsageSummary parent) {
1835 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1836 dialog.setTargetFragment(parent, 0);
1837 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1838 }
1839
1840 @Override
1841 public Dialog onCreateDialog(Bundle savedInstanceState) {
1842 final Context context = getActivity();
1843
1844 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001845 builder.setTitle(R.string.data_usage_restrict_background_title);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07001846
1847 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1848 if (target != null) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001849 final CharSequence limitedNetworks = target.buildLimitedNetworksString();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07001850 builder.setMessage(
1851 getString(R.string.data_usage_restrict_background, limitedNetworks));
1852 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001853
1854 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1855 public void onClick(DialogInterface dialog, int which) {
1856 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1857 if (target != null) {
1858 target.setRestrictBackground(true);
1859 }
1860 }
1861 });
1862 builder.setNegativeButton(android.R.string.cancel, null);
1863
1864 return builder.create();
1865 }
1866 }
1867
1868 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001869 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
1870 * change has been denied, usually based on
1871 * {@link DataUsageSummary#hasLimitedNetworks()}.
1872 */
1873 public static class DeniedRestrictFragment extends DialogFragment {
1874 public static void show(DataUsageSummary parent) {
1875 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
1876 dialog.setTargetFragment(parent, 0);
1877 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
1878 }
1879
1880 @Override
1881 public Dialog onCreateDialog(Bundle savedInstanceState) {
1882 final Context context = getActivity();
1883
1884 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1885 builder.setTitle(R.string.data_usage_app_restrict_background);
1886 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
1887 builder.setPositiveButton(android.R.string.ok, null);
1888
1889 return builder.create();
1890 }
1891 }
1892
1893 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001894 * Dialog to request user confirmation before setting
1895 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
1896 */
1897 public static class ConfirmAppRestrictFragment extends DialogFragment {
1898 public static void show(DataUsageSummary parent) {
1899 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
1900 dialog.setTargetFragment(parent, 0);
1901 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
1902 }
1903
1904 @Override
1905 public Dialog onCreateDialog(Bundle savedInstanceState) {
1906 final Context context = getActivity();
1907
1908 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001909 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
1910 builder.setMessage(R.string.data_usage_app_restrict_dialog);
1911
1912 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1913 public void onClick(DialogInterface dialog, int which) {
1914 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1915 if (target != null) {
1916 target.setAppRestrictBackground(true);
1917 }
1918 }
1919 });
1920 builder.setNegativeButton(android.R.string.cancel, null);
1921
1922 return builder.create();
1923 }
1924 }
1925
1926 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001927 * Compute default tab that should be selected, based on
1928 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
1929 */
1930 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07001931 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
1932 if (template == null) return null;
1933
1934 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07001935 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001936 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001937 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001938 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001939 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001940 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001941 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001942 return TAB_WIFI;
1943 default:
1944 return null;
1945 }
1946 }
1947
1948 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001949 * Background task that loads {@link UidDetail}, binding to
1950 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001951 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001952 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
1953 private final UidDetailProvider mProvider;
1954 private final AppUsageItem mItem;
1955 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001956
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001957 private UidDetailTask(UidDetailProvider provider, AppUsageItem item, View target) {
1958 mProvider = checkNotNull(provider);
1959 mItem = checkNotNull(item);
1960 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001961 }
1962
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001963 public static void bindView(
1964 UidDetailProvider provider, AppUsageItem item, View target) {
1965 final UidDetailTask existing = (UidDetailTask) target.getTag();
1966 if (existing != null) {
1967 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001968 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001969
1970 final UidDetail cachedDetail = provider.getUidDetail(item.uids[0], false);
1971 if (cachedDetail != null) {
1972 bindView(cachedDetail, target);
1973 } else {
1974 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
1975 AsyncTask.THREAD_POOL_EXECUTOR));
1976 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001977 }
1978
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001979 private static void bindView(UidDetail detail, View target) {
1980 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
1981 final TextView title = (TextView) target.findViewById(android.R.id.title);
1982
1983 if (detail != null) {
1984 icon.setImageDrawable(detail.icon);
1985 title.setText(detail.label);
1986 } else {
1987 icon.setImageDrawable(null);
1988 title.setText(null);
1989 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001990 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001991
1992 @Override
1993 protected void onPreExecute() {
1994 bindView(null, mTarget);
1995 }
1996
1997 @Override
1998 protected UidDetail doInBackground(Void... params) {
1999 return mProvider.getUidDetail(mItem.uids[0], true);
2000 }
2001
2002 @Override
2003 protected void onPostExecute(UidDetail result) {
2004 bindView(result, mTarget);
2005 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002006 }
2007
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002008 /**
2009 * Test if device has a mobile data radio.
2010 */
2011 private static boolean hasMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002012 if (TEST_RADIOS) {
2013 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2014 }
2015
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002016 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
2017 Context.CONNECTIVITY_SERVICE);
2018
2019 // mobile devices should have MOBILE network tracker regardless of
2020 // connection status.
2021 return conn.getNetworkInfo(TYPE_MOBILE) != null;
2022 }
2023
2024 /**
2025 * Test if device has a mobile 4G data radio.
2026 */
2027 private static boolean hasMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002028 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2029 return false;
2030 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002031 if (TEST_RADIOS) {
2032 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2033 }
2034
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002035 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
2036 Context.CONNECTIVITY_SERVICE);
2037 final TelephonyManager telephony = (TelephonyManager) context.getSystemService(
2038 Context.TELEPHONY_SERVICE);
2039
2040 // WiMAX devices should have WiMAX network tracker regardless of
2041 // connection status.
2042 final boolean hasWimax = conn.getNetworkInfo(TYPE_WIMAX) != null;
2043 final boolean hasLte = telephony.getLteOnCdmaMode() == Phone.LTE_ON_CDMA_TRUE;
2044 return hasWimax || hasLte;
2045 }
2046
2047 /**
2048 * Test if device has a Wi-Fi data radio.
2049 */
2050 private static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002051 if (TEST_RADIOS) {
2052 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2053 }
2054
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002055 return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI);
2056 }
2057
2058 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002059 * Test if device has an ethernet network connection.
2060 */
2061 private static boolean hasEthernet(Context context) {
2062 if (TEST_RADIOS) {
2063 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2064 }
2065
2066 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
2067 Context.CONNECTIVITY_SERVICE);
2068 return conn.getNetworkInfo(TYPE_ETHERNET) != null;
2069 }
2070
2071 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002072 * Inflate a {@link Preference} style layout, adding the given {@link View}
2073 * widget into {@link android.R.id#widget_frame}.
2074 */
2075 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2076 final View view = inflater.inflate(R.layout.preference, root, false);
2077 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2078 android.R.id.widget_frame);
2079 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2080 return view;
2081 }
2082
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002083 private static View inflateAppTitle(
2084 LayoutInflater inflater, ViewGroup root, CharSequence label) {
2085 final TextView view = (TextView) inflater.inflate(
2086 R.layout.data_usage_app_title, root, false);
2087 view.setText(label);
2088 return view;
2089 }
2090
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002091 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002092 * Test if any networks are currently limited.
2093 */
2094 private boolean hasLimitedNetworks() {
2095 return !buildLimitedNetworksList().isEmpty();
2096 }
2097
2098 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002099 * Build string describing currently limited networks, which defines when
2100 * background data is restricted.
2101 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002102 private CharSequence buildLimitedNetworksString() {
2103 final List<CharSequence> limited = buildLimitedNetworksList();
2104
2105 // handle case where no networks limited
2106 if (limited.isEmpty()) {
2107 limited.add(getText(R.string.data_usage_list_none));
2108 }
2109
2110 return TextUtils.join(limited);
2111 }
2112
2113 /**
2114 * Build list of currently limited networks, which defines when background
2115 * data is restricted.
2116 */
2117 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002118 final Context context = getActivity();
2119 final String subscriberId = getActiveSubscriberId(context);
2120
2121 // build combined list of all limited networks
2122 final ArrayList<CharSequence> limited = Lists.newArrayList();
2123 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2124 limited.add(getText(R.string.data_usage_list_mobile));
2125 }
2126 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2127 limited.add(getText(R.string.data_usage_tab_3g));
2128 }
2129 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2130 limited.add(getText(R.string.data_usage_tab_4g));
2131 }
2132 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifi())) {
2133 limited.add(getText(R.string.data_usage_tab_wifi));
2134 }
2135 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2136 limited.add(getText(R.string.data_usage_tab_ethernet));
2137 }
2138
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002139 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002140 }
2141
2142 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002143 * Inset both selector and divider {@link Drawable} on the given
2144 * {@link ListView} by the requested dimensions.
2145 */
2146 private static void insetListViewDrawables(ListView view, int insetSide) {
2147 final Drawable selector = view.getSelector();
2148 final Drawable divider = view.getDivider();
2149
2150 // fully unregister these drawables so callbacks can be maintained after
2151 // wrapping below.
2152 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2153 view.setSelector(stub);
2154 view.setDivider(stub);
2155
2156 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2157 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2158 }
2159
2160 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002161 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002162 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002163 */
2164 private static void setPreferenceTitle(View parent, int resId) {
2165 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2166 title.setText(resId);
2167 }
2168
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002169 /**
2170 * Set {@link android.R.id#summary} for a preference view inflated with
2171 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2172 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002173 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002174 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2175 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002176 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002177 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07002178
2179 private static boolean contains(int[] haystack, int needle) {
2180 for (int value : haystack) {
2181 if (value == needle) {
2182 return true;
2183 }
2184 }
2185 return false;
2186 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002187}