blob: 609602989ee6ccf692de0ef3ac4982e08e628f35 [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);
Jeff Sharkey461842a2011-09-25 18:22:48 -0700754 if (hasLimitedNetworks()) {
755 setPreferenceSummary(mAppRestrictView,
756 getString(R.string.data_usage_app_restrict_background_summary));
757 } else {
758 setPreferenceSummary(mAppRestrictView,
759 getString(R.string.data_usage_app_restrict_background_summary_disabled));
760 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700761
762 mAppRestrictView.setVisibility(View.VISIBLE);
763 mAppRestrict.setChecked(getAppRestrictBackground());
764
765 } else {
766 mAppRestrictView.setVisibility(View.GONE);
767 }
768 }
769
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700770 private void setPolicyWarningBytes(long warningBytes) {
771 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700772 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700773 updatePolicy(false);
774 }
775
776 private void setPolicyLimitBytes(long limitBytes) {
777 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700778 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700779 updatePolicy(false);
780 }
781
Jeff Sharkey28130d92011-09-02 16:10:24 -0700782 /**
783 * Local cache of value, used to work around delay when
784 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
785 */
786 private Boolean mMobileDataEnabled;
787
788 private boolean isMobileDataEnabled() {
789 if (mMobileDataEnabled != null) {
790 // TODO: deprecate and remove this once enabled flag is on policy
791 return mMobileDataEnabled;
792 } else {
793 return mConnService.getMobileDataEnabled();
794 }
795 }
796
797 private void setMobileDataEnabled(boolean enabled) {
798 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
799 mConnService.setMobileDataEnabled(enabled);
800 mMobileDataEnabled = enabled;
801 updatePolicy(false);
802 }
803
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700804 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
805 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked();
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700806 }
807
808 private boolean isBandwidthControlEnabled() {
809 try {
810 return mNetworkService.isBandwidthControlEnabled();
811 } catch (RemoteException e) {
812 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
813 return false;
814 }
815 }
816
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700817 private boolean getDataRoaming() {
818 final ContentResolver resolver = getActivity().getContentResolver();
819 return Settings.Secure.getInt(resolver, Settings.Secure.DATA_ROAMING, 0) != 0;
820 }
821
822 private void setDataRoaming(boolean enabled) {
823 // TODO: teach telephony DataConnectionTracker to watch and apply
824 // updates when changed.
825 final ContentResolver resolver = getActivity().getContentResolver();
826 Settings.Secure.putInt(resolver, Settings.Secure.DATA_ROAMING, enabled ? 1 : 0);
827 mMenuDataRoaming.setChecked(enabled);
828 }
829
830 private boolean getRestrictBackground() {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700831 try {
832 return mPolicyService.getRestrictBackground();
833 } catch (RemoteException e) {
834 Log.w(TAG, "problem talking with policy service: " + e);
835 return false;
836 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700837 }
838
839 private void setRestrictBackground(boolean restrictBackground) {
840 if (LOGD) Log.d(TAG, "setRestrictBackground()");
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700841 try {
842 mPolicyService.setRestrictBackground(restrictBackground);
843 mMenuRestrictBackground.setChecked(restrictBackground);
844 } catch (RemoteException e) {
845 Log.w(TAG, "problem talking with policy service: " + e);
846 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700847 }
848
849 private boolean getAppRestrictBackground() {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700850 final int primaryUid = getAppDetailPrimaryUid();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700851 final int uidPolicy;
852 try {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700853 uidPolicy = mPolicyService.getUidPolicy(primaryUid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700854 } catch (RemoteException e) {
855 // since we can't do much without policy, we bail hard.
856 throw new RuntimeException("problem reading network policy", e);
857 }
858
859 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
860 }
861
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700862 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700863 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700864 final int primaryUid = getAppDetailPrimaryUid();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700865 try {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700866 mPolicyService.setUidPolicy(primaryUid,
867 restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700868 } catch (RemoteException e) {
869 throw new RuntimeException("unable to save policy", e);
870 }
871
872 mAppRestrict.setChecked(restrictBackground);
873 }
874
Jeff Sharkey8a503642011-06-10 13:31:21 -0700875 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700876 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
877 * current {@link #mTemplate}.
878 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700879 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700880 if (isAppDetailMode()) {
881 mNetworkSwitches.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700882 } else {
883 mNetworkSwitches.setVisibility(View.VISIBLE);
884 }
885
Jeff Sharkey28130d92011-09-02 16:10:24 -0700886 // TODO: move enabled state directly into policy
887 if (TAB_MOBILE.equals(mCurrentTab)) {
888 mBinding = true;
889 mDataEnabled.setChecked(isMobileDataEnabled());
890 mBinding = false;
891 }
892
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700893 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
894 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700895 mDisableAtLimitView.setVisibility(View.VISIBLE);
896 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700897 if (!isAppDetailMode()) {
898 mChart.bindNetworkPolicy(policy);
899 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700900
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700901 } else {
902 // controls are disabled; don't bind warning/limit sweeps
903 mDisableAtLimitView.setVisibility(View.GONE);
904 mChart.bindNetworkPolicy(null);
905 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700906
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700907 if (refreshCycle) {
908 // generate cycle list based on policy and available history
909 updateCycleList(policy);
910 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700911 }
912
913 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700914 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
915 * and available {@link NetworkStatsHistory} data. Always selects the newest
916 * item, updating the inspection range on {@link #mChart}.
917 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700918 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700919 // stash away currently selected cycle to try restoring below
920 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700921 mCycleAdapter.clear();
922
923 final Context context = mCycleSpinner.getContext();
924
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700925 long historyStart = Long.MAX_VALUE;
926 long historyEnd = Long.MIN_VALUE;
927 if (mChartData != null) {
928 historyStart = mChartData.network.getStart();
929 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700930 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700931
Jeff Sharkey461842a2011-09-25 18:22:48 -0700932 final long now = System.currentTimeMillis();
933 if (historyStart == Long.MAX_VALUE) historyStart = now;
934 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700935
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700936 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700937 if (policy != null) {
938 // find the next cycle boundary
939 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700940
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700941 // walk backwards, generating all valid cycle ranges
942 while (cycleEnd > historyStart) {
943 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
944 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
945 + historyStart);
946 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
947 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700948 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700949 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700950
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700951 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700952 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700953 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700954
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700955 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700956 // no policy defined cycles; show entry for each four-week period
957 long cycleEnd = historyEnd;
958 while (cycleEnd > historyStart) {
959 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
960 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
961 cycleEnd = cycleStart;
962 }
963
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700964 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700965 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700966
967 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700968 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700969 final int position = mCycleAdapter.findNearestPosition(previousItem);
970 mCycleSpinner.setSelection(position);
971
972 // only force-update cycle when changed; skipping preserves any
973 // user-defined inspection region.
974 final CycleItem selectedItem = mCycleAdapter.getItem(position);
975 if (!Objects.equal(selectedItem, previousItem)) {
976 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
977 } else {
978 // but still kick off loader for detailed list
979 updateDetailData();
980 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700981 } else {
982 updateDetailData();
983 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700984 }
985
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700986 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700987 /** {@inheritDoc} */
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700988 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
989 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700990
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700991 final boolean dataEnabled = isChecked;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700992 final String currentTab = mCurrentTab;
993 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -0700994 if (dataEnabled) {
995 setMobileDataEnabled(true);
996 } else {
997 // disabling data; show confirmation dialog which eventually
998 // calls setMobileDataEnabled() once user confirms.
999 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1000 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001001 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001002
Jeff Sharkey28130d92011-09-02 16:10:24 -07001003 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001004 }
1005 };
1006
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001007 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001008 /** {@inheritDoc} */
1009 public void onClick(View v) {
1010 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001011 if (disableAtLimit) {
1012 // enabling limit; show confirmation dialog which eventually
1013 // calls setPolicyLimitBytes() once user confirms.
1014 ConfirmLimitFragment.show(DataUsageSummary.this);
1015 } else {
1016 setPolicyLimitBytes(LIMIT_DISABLED);
1017 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001018 }
1019 };
1020
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001021 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
1022 /** {@inheritDoc} */
1023 public void onClick(View v) {
1024 final boolean restrictBackground = !mAppRestrict.isChecked();
1025
1026 if (restrictBackground) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001027 if (hasLimitedNetworks()) {
1028 // enabling restriction; show confirmation dialog which
1029 // eventually calls setRestrictBackground() once user
1030 // confirms.
1031 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
1032 } else {
1033 // no limited networks; show dialog to guide user towards
1034 // setting a network limit. doesn't mutate restrict state.
1035 DeniedRestrictFragment.show(DataUsageSummary.this);
1036 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001037 } else {
1038 setAppRestrictBackground(false);
1039 }
1040 }
1041 };
1042
1043 private OnClickListener mAppSettingsListener = new OnClickListener() {
1044 /** {@inheritDoc} */
1045 public void onClick(View v) {
1046 // TODO: target torwards entire UID instead of just first package
1047 startActivity(mAppSettingsIntent);
1048 }
1049 };
1050
Jeff Sharkey8a503642011-06-10 13:31:21 -07001051 private OnItemClickListener mListListener = new OnItemClickListener() {
1052 /** {@inheritDoc} */
1053 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001054 final Context context = view.getContext();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001055 final AppUsageItem app = (AppUsageItem) parent.getItemAtPosition(position);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001056 final UidDetail detail = mUidDetailProvider.getUidDetail(app.uids[0], true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001057 AppDetailsFragment.show(DataUsageSummary.this, app.uids, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001058 }
1059 };
1060
1061 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
1062 /** {@inheritDoc} */
1063 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1064 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1065 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001066 // show cycle editor; will eventually call setPolicyCycleDay()
1067 // when user finishes editing.
1068 CycleEditorFragment.show(DataUsageSummary.this);
1069
1070 // reset spinner to something other than "change cycle..."
1071 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001072
1073 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001074 if (LOGD) {
1075 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1076 + cycle.end + "]");
1077 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001078
1079 // update chart to show selected cycle, and update detail data
1080 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001081 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001082
1083 updateDetailData();
1084 }
1085 }
1086
1087 /** {@inheritDoc} */
1088 public void onNothingSelected(AdapterView<?> parent) {
1089 // ignored
1090 }
1091 };
1092
1093 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001094 * Update details based on {@link #mChart} inspection range depending on
1095 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1096 * of applications data usage, and when {@link #isAppDetailMode()} update
1097 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001098 */
1099 private void updateDetailData() {
1100 if (LOGD) Log.d(TAG, "updateDetailData()");
1101
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001102 final long start = mChart.getInspectStart();
1103 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001104 final long now = System.currentTimeMillis();
1105
1106 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001107
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001108 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001109 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001110 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001111 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001112 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001113 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001114 final long foregroundBytes = entry.rxBytes + entry.txBytes;
1115
1116 mAppPieChart.setOriginAngle(175);
1117
1118 mAppPieChart.removeAllSlices();
1119 mAppPieChart.addSlice(foregroundBytes, Color.parseColor("#d88d3a"));
1120 mAppPieChart.addSlice(defaultBytes, Color.parseColor("#666666"));
1121
1122 mAppPieChart.generatePath();
1123
1124 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1125 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1126
1127 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001128 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001129
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001130 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001131
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001132 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001133 if (mChartData != null) {
1134 entry = mChartData.network.getValues(start, end, now, null);
1135 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001136
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001137 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001138 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001139 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001140 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001141
1142 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1143 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001144 final String rangePhrase = formatDateRange(context, start, end, false);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001145
1146 mUsageSummary.setText(
1147 getString(R.string.data_usage_total_during_range, totalPhrase, rangePhrase));
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001148 }
1149
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001150 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1151 ChartData>() {
1152 /** {@inheritDoc} */
1153 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
1154 return new ChartDataLoader(getActivity(), mStatsService, args);
1155 }
1156
1157 /** {@inheritDoc} */
1158 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1159 mChartData = data;
1160 mChart.bindNetworkStats(mChartData.network);
1161 mChart.bindDetailNetworkStats(mChartData.detail);
1162
1163 // calcuate policy cycles based on available data
1164 updatePolicy(true);
1165 updateAppDetail();
1166
1167 // force scroll to top of body when showing detail
1168 if (mChartData.detail != null) {
1169 mListView.smoothScrollToPosition(0);
1170 }
1171 }
1172
1173 /** {@inheritDoc} */
1174 public void onLoaderReset(Loader<ChartData> loader) {
1175 mChartData = null;
1176 mChart.bindNetworkStats(null);
1177 mChart.bindDetailNetworkStats(null);
1178 }
1179 };
1180
1181 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001182 NetworkStats>() {
1183 /** {@inheritDoc} */
1184 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
1185 return new SummaryForAllUidLoader(getActivity(), mStatsService, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001186 }
1187
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001188 /** {@inheritDoc} */
1189 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
1190 mAdapter.bindStats(data);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001191 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001192 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001193
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001194 /** {@inheritDoc} */
1195 public void onLoaderReset(Loader<NetworkStats> loader) {
1196 mAdapter.bindStats(null);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001197 updateEmptyVisible();
1198 }
1199
1200 private void updateEmptyVisible() {
1201 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1202 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001203 }
1204 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001205
Jeff Sharkeya662e492011-06-18 21:57:06 -07001206 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001207 final Context context = getActivity();
1208 if (hasMobileRadio(context)) {
1209 final String subscriberId = getActiveSubscriberId(context);
1210 return mPolicyEditor.isMobilePolicySplit(subscriberId);
1211 } else {
1212 return false;
1213 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001214 }
1215
1216 private void setMobilePolicySplit(boolean split) {
1217 final String subscriberId = getActiveSubscriberId(getActivity());
1218 mPolicyEditor.setMobilePolicySplit(subscriberId, split);
1219 }
1220
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001221 private static String getActiveSubscriberId(Context context) {
1222 final TelephonyManager telephony = (TelephonyManager) context.getSystemService(
1223 Context.TELEPHONY_SERVICE);
1224 return telephony.getSubscriberId();
1225 }
1226
Jeff Sharkey8a503642011-06-10 13:31:21 -07001227 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
1228 /** {@inheritDoc} */
1229 public void onInspectRangeChanged() {
1230 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
1231 updateDetailData();
1232 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001233
Jeff Sharkey8a503642011-06-10 13:31:21 -07001234 /** {@inheritDoc} */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001235 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001236 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001237 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001238
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001239 /** {@inheritDoc} */
1240 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001241 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001242 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001243
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001244 /** {@inheritDoc} */
1245 public void requestWarningEdit() {
1246 WarningEditorFragment.show(DataUsageSummary.this);
1247 }
1248
1249 /** {@inheritDoc} */
1250 public void requestLimitEdit() {
1251 LimitEditorFragment.show(DataUsageSummary.this);
1252 }
1253 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001254
1255 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001256 * List item that reflects a specific data usage cycle.
1257 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001258 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001259 public CharSequence label;
1260 public long start;
1261 public long end;
1262
Jeff Sharkey8a503642011-06-10 13:31:21 -07001263 CycleItem(CharSequence label) {
1264 this.label = label;
1265 }
1266
1267 public CycleItem(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001268 this.label = formatDateRange(context, start, end, true);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001269 this.start = start;
1270 this.end = end;
1271 }
1272
Jeff Sharkey8a503642011-06-10 13:31:21 -07001273 @Override
1274 public String toString() {
1275 return label.toString();
1276 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001277
1278 @Override
1279 public boolean equals(Object o) {
1280 if (o instanceof CycleItem) {
1281 final CycleItem another = (CycleItem) o;
1282 return start == another.start && end == another.end;
1283 }
1284 return false;
1285 }
1286
1287 /** {@inheritDoc} */
1288 public int compareTo(CycleItem another) {
1289 return Long.compare(start, another.start);
1290 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001291 }
1292
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001293 private static final StringBuilder sBuilder = new StringBuilder(50);
1294 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1295 sBuilder, Locale.getDefault());
1296
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001297 public static String formatDateRange(Context context, long start, long end, boolean utcTime) {
1298 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
1299 final String timezone = utcTime ? TIMEZONE_UTC : null;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001300
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001301 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001302 sBuilder.setLength(0);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001303 return DateUtils
1304 .formatDateRange(context, sFormatter, start, end, flags, timezone).toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001305 }
1306 }
1307
Jeff Sharkey8a503642011-06-10 13:31:21 -07001308 /**
1309 * Special-case data usage cycle that triggers dialog to change
1310 * {@link NetworkPolicy#cycleDay}.
1311 */
1312 public static class CycleChangeItem extends CycleItem {
1313 public CycleChangeItem(Context context) {
1314 super(context.getString(R.string.data_usage_change_cycle));
1315 }
1316 }
1317
1318 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001319 private boolean mChangePossible = false;
1320 private boolean mChangeVisible = false;
1321
1322 private final CycleChangeItem mChangeItem;
1323
Jeff Sharkey8a503642011-06-10 13:31:21 -07001324 public CycleAdapter(Context context) {
1325 super(context, android.R.layout.simple_spinner_item);
1326 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001327 mChangeItem = new CycleChangeItem(context);
1328 }
1329
1330 public void setChangePossible(boolean possible) {
1331 mChangePossible = possible;
1332 updateChange();
1333 }
1334
1335 public void setChangeVisible(boolean visible) {
1336 mChangeVisible = visible;
1337 updateChange();
1338 }
1339
1340 private void updateChange() {
1341 remove(mChangeItem);
1342 if (mChangePossible && mChangeVisible) {
1343 add(mChangeItem);
1344 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001345 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001346
1347 /**
1348 * Find position of {@link CycleItem} in this adapter which is nearest
1349 * the given {@link CycleItem}.
1350 */
1351 public int findNearestPosition(CycleItem target) {
1352 if (target != null) {
1353 final int count = getCount();
1354 for (int i = count - 1; i >= 0; i--) {
1355 final CycleItem item = getItem(i);
1356 if (item instanceof CycleChangeItem) {
1357 continue;
1358 } else if (item.compareTo(target) >= 0) {
1359 return i;
1360 }
1361 }
1362 }
1363 return 0;
1364 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001365 }
1366
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001367 private static class AppUsageItem implements Comparable<AppUsageItem> {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001368 public int[] uids;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001369 public long total;
1370
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001371 public AppUsageItem(int uid) {
1372 uids = new int[] { uid };
1373 }
1374
1375 public void addUid(int uid) {
1376 if (contains(uids, uid)) return;
1377 final int length = uids.length;
1378 uids = Arrays.copyOf(uids, length + 1);
1379 uids[length] = uid;
1380 }
1381
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001382 /** {@inheritDoc} */
1383 public int compareTo(AppUsageItem another) {
1384 return Long.compare(another.total, total);
1385 }
1386 }
1387
Jeff Sharkey8a503642011-06-10 13:31:21 -07001388 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001389 * Adapter of applications, sorted by total usage descending.
1390 */
1391 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001392 private final UidDetailProvider mProvider;
1393 private final int mInsetSide;
1394
Jeff Sharkey8a503642011-06-10 13:31:21 -07001395 private ArrayList<AppUsageItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001396 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001397
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001398 public DataUsageAdapter(UidDetailProvider provider, int insetSide) {
1399 mProvider = checkNotNull(provider);
1400 mInsetSide = insetSide;
1401 }
1402
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001403 /**
1404 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1405 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001406 public void bindStats(NetworkStats stats) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001407 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001408
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001409 final AppUsageItem systemItem = new AppUsageItem(android.os.Process.SYSTEM_UID);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001410 final SparseArray<AppUsageItem> knownUids = new SparseArray<AppUsageItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001411
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001412 NetworkStats.Entry entry = null;
1413 final int size = stats != null ? stats.size() : 0;
1414 for (int i = 0; i < size; i++) {
1415 entry = stats.getValues(i, entry);
1416
1417 final int uid = entry.uid;
1418 final boolean isApp = uid >= android.os.Process.FIRST_APPLICATION_UID
1419 && uid <= android.os.Process.LAST_APPLICATION_UID;
Jeff Sharkeya83a24f2011-09-16 01:52:39 -07001420 if (isApp || uid == UID_REMOVED || uid == UID_TETHERING) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001421 AppUsageItem item = knownUids.get(uid);
1422 if (item == null) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001423 item = new AppUsageItem(uid);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001424 knownUids.put(uid, item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001425 mItems.add(item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001426 }
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001427
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001428 item.total += entry.rxBytes + entry.txBytes;
1429 } else {
1430 systemItem.total += entry.rxBytes + entry.txBytes;
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001431 systemItem.addUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001432 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001433 }
1434
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001435 if (systemItem.total > 0) {
1436 mItems.add(systemItem);
1437 }
1438
Jeff Sharkey8a503642011-06-10 13:31:21 -07001439 Collections.sort(mItems);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001440 mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001441 notifyDataSetChanged();
1442 }
1443
1444 @Override
1445 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001446 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001447 }
1448
1449 @Override
1450 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001451 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001452 }
1453
1454 @Override
1455 public long getItemId(int position) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001456 return mItems.get(position).uids[0];
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001457 }
1458
1459 @Override
1460 public View getView(int position, View convertView, ViewGroup parent) {
1461 if (convertView == null) {
1462 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey5d706792011-09-08 18:57:17 -07001463 R.layout.data_usage_item, parent, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001464
1465 if (mInsetSide > 0) {
1466 convertView.setPadding(mInsetSide, 0, mInsetSide, 0);
1467 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001468 }
1469
1470 final Context context = parent.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001471
Jeff Sharkey28130d92011-09-02 16:10:24 -07001472 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001473 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1474 android.R.id.progress);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001475
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001476 // kick off async load of app details
Jeff Sharkey8a503642011-06-10 13:31:21 -07001477 final AppUsageItem item = mItems.get(position);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001478 UidDetailTask.bindView(mProvider, item, convertView);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001479
Jeff Sharkey28130d92011-09-02 16:10:24 -07001480 text1.setText(Formatter.formatFileSize(context, item.total));
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001481
1482 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1483 progress.setProgress(percentTotal);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001484
1485 return convertView;
1486 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001487 }
1488
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001489 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001490 * Empty {@link Fragment} that controls display of UID details in
1491 * {@link DataUsageSummary}.
1492 */
1493 public static class AppDetailsFragment extends Fragment {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001494 private static final String EXTRA_UIDS = "uids";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001495
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001496 public static void show(DataUsageSummary parent, int[] uids, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001497 if (!parent.isAdded()) return;
1498
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001499 final Bundle args = new Bundle();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001500 args.putIntArray(EXTRA_UIDS, uids);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001501
1502 final AppDetailsFragment fragment = new AppDetailsFragment();
1503 fragment.setArguments(args);
1504 fragment.setTargetFragment(parent, 0);
1505
1506 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1507 ft.add(fragment, TAG_APP_DETAILS);
1508 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001509 ft.setBreadCrumbTitle(label);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001510 ft.commit();
1511 }
1512
1513 @Override
1514 public void onStart() {
1515 super.onStart();
1516 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001517 target.mAppDetailUids = getArguments().getIntArray(EXTRA_UIDS);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001518 target.updateBody();
1519 }
1520
1521 @Override
1522 public void onStop() {
1523 super.onStop();
1524 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001525 target.mAppDetailUids = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001526 target.updateBody();
1527 }
1528 }
1529
1530 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001531 * Dialog to request user confirmation before setting
1532 * {@link NetworkPolicy#limitBytes}.
1533 */
1534 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001535 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001536 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001537
1538 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001539 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001540
Jeff Sharkey461842a2011-09-25 18:22:48 -07001541 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001542 final CharSequence message;
1543 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001544
1545 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001546 final String currentTab = parent.mCurrentTab;
1547 if (TAB_3G.equals(currentTab)) {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001548 message = buildDialogMessage(res, R.string.data_usage_tab_3g);
1549 limitBytes = 5 * GB_IN_BYTES;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001550 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001551 message = buildDialogMessage(res, R.string.data_usage_tab_4g);
1552 limitBytes = 5 * GB_IN_BYTES;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001553 } else if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001554 message = buildDialogMessage(res, R.string.data_usage_list_mobile);
1555 limitBytes = 5 * GB_IN_BYTES;
1556 } else if (TAB_WIFI.equals(currentTab)) {
1557 message = buildDialogMessage(res, R.string.data_usage_tab_wifi);
1558 limitBytes = 5 * GB_IN_BYTES;
1559 } else {
1560 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001561 }
1562
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001563 final Bundle args = new Bundle();
1564 args.putCharSequence(EXTRA_MESSAGE, message);
1565 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1566
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001567 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1568 dialog.setArguments(args);
1569 dialog.setTargetFragment(parent, 0);
1570 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1571 }
1572
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001573 private static CharSequence buildDialogMessage(Resources res, int networkResId) {
1574 return res.getString(R.string.data_usage_limit_dialog, res.getString(networkResId));
1575 }
1576
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001577 @Override
1578 public Dialog onCreateDialog(Bundle savedInstanceState) {
1579 final Context context = getActivity();
1580
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001581 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001582 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1583
1584 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1585 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001586 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001587
1588 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1589 public void onClick(DialogInterface dialog, int which) {
1590 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1591 if (target != null) {
1592 target.setPolicyLimitBytes(limitBytes);
1593 }
1594 }
1595 });
1596
1597 return builder.create();
1598 }
1599 }
1600
1601 /**
1602 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1603 */
1604 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001605 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001606
1607 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001608 if (!parent.isAdded()) return;
1609
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001610 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001611 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001612
1613 final CycleEditorFragment dialog = new CycleEditorFragment();
1614 dialog.setArguments(args);
1615 dialog.setTargetFragment(parent, 0);
1616 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1617 }
1618
1619 @Override
1620 public Dialog onCreateDialog(Bundle savedInstanceState) {
1621 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001622 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1623 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001624
1625 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1626 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1627
1628 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1629 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1630
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001631 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1632 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001633
1634 cycleDayPicker.setMinValue(1);
1635 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001636 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001637 cycleDayPicker.setWrapSelectorWheel(true);
1638
1639 builder.setTitle(R.string.data_usage_cycle_editor_title);
1640 builder.setView(view);
1641
1642 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1643 new DialogInterface.OnClickListener() {
1644 public void onClick(DialogInterface dialog, int which) {
1645 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001646 editor.setPolicyCycleDay(template, cycleDay);
1647 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001648 }
1649 });
1650
1651 return builder.create();
1652 }
1653 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001654
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001655 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001656 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1657 */
1658 public static class WarningEditorFragment extends DialogFragment {
1659 private static final String EXTRA_TEMPLATE = "template";
1660
1661 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001662 if (!parent.isAdded()) return;
1663
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001664 final Bundle args = new Bundle();
1665 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1666
1667 final WarningEditorFragment dialog = new WarningEditorFragment();
1668 dialog.setArguments(args);
1669 dialog.setTargetFragment(parent, 0);
1670 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1671 }
1672
1673 @Override
1674 public Dialog onCreateDialog(Bundle savedInstanceState) {
1675 final Context context = getActivity();
1676 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1677 final NetworkPolicyEditor editor = target.mPolicyEditor;
1678
1679 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1680 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1681
1682 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1683 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1684
1685 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1686 final long warningBytes = editor.getPolicyWarningBytes(template);
1687 final long limitBytes = editor.getPolicyLimitBytes(template);
1688
1689 bytesPicker.setMinValue(0);
1690 if (limitBytes != LIMIT_DISABLED) {
1691 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1692 } else {
1693 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1694 }
1695 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1696 bytesPicker.setWrapSelectorWheel(false);
1697
1698 builder.setTitle(R.string.data_usage_warning_editor_title);
1699 builder.setView(view);
1700
1701 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1702 new DialogInterface.OnClickListener() {
1703 public void onClick(DialogInterface dialog, int which) {
1704 // clear focus to finish pending text edits
1705 bytesPicker.clearFocus();
1706
1707 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1708 editor.setPolicyWarningBytes(template, bytes);
1709 target.updatePolicy(false);
1710 }
1711 });
1712
1713 return builder.create();
1714 }
1715 }
1716
1717 /**
1718 * Dialog to edit {@link NetworkPolicy#limitBytes}.
1719 */
1720 public static class LimitEditorFragment extends DialogFragment {
1721 private static final String EXTRA_TEMPLATE = "template";
1722
1723 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001724 if (!parent.isAdded()) return;
1725
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001726 final Bundle args = new Bundle();
1727 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1728
1729 final LimitEditorFragment dialog = new LimitEditorFragment();
1730 dialog.setArguments(args);
1731 dialog.setTargetFragment(parent, 0);
1732 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
1733 }
1734
1735 @Override
1736 public Dialog onCreateDialog(Bundle savedInstanceState) {
1737 final Context context = getActivity();
1738 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1739 final NetworkPolicyEditor editor = target.mPolicyEditor;
1740
1741 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1742 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1743
1744 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1745 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1746
1747 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1748 final long warningBytes = editor.getPolicyWarningBytes(template);
1749 final long limitBytes = editor.getPolicyLimitBytes(template);
1750
1751 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1752 if (warningBytes != WARNING_DISABLED) {
1753 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
1754 } else {
1755 bytesPicker.setMinValue(0);
1756 }
1757 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
1758 bytesPicker.setWrapSelectorWheel(false);
1759
1760 builder.setTitle(R.string.data_usage_limit_editor_title);
1761 builder.setView(view);
1762
1763 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1764 new DialogInterface.OnClickListener() {
1765 public void onClick(DialogInterface dialog, int which) {
1766 // clear focus to finish pending text edits
1767 bytesPicker.clearFocus();
1768
1769 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1770 editor.setPolicyLimitBytes(template, bytes);
1771 target.updatePolicy(false);
1772 }
1773 });
1774
1775 return builder.create();
1776 }
1777 }
1778 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07001779 * Dialog to request user confirmation before disabling data.
1780 */
1781 public static class ConfirmDataDisableFragment extends DialogFragment {
1782 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001783 if (!parent.isAdded()) return;
1784
Jeff Sharkey28130d92011-09-02 16:10:24 -07001785 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
1786 dialog.setTargetFragment(parent, 0);
1787 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
1788 }
1789
1790 @Override
1791 public Dialog onCreateDialog(Bundle savedInstanceState) {
1792 final Context context = getActivity();
1793
1794 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1795 builder.setMessage(R.string.data_usage_disable_mobile);
1796
1797 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1798 public void onClick(DialogInterface dialog, int which) {
1799 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1800 if (target != null) {
1801 // TODO: extend to modify policy enabled flag.
1802 target.setMobileDataEnabled(false);
1803 }
1804 }
1805 });
1806 builder.setNegativeButton(android.R.string.cancel, null);
1807
1808 return builder.create();
1809 }
1810 }
1811
1812 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001813 * Dialog to request user confirmation before setting
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001814 * {@link Settings.Secure#DATA_ROAMING}.
1815 */
1816 public static class ConfirmDataRoamingFragment extends DialogFragment {
1817 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001818 if (!parent.isAdded()) return;
1819
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001820 final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment();
1821 dialog.setTargetFragment(parent, 0);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001822 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_ROAMING);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001823 }
1824
1825 @Override
1826 public Dialog onCreateDialog(Bundle savedInstanceState) {
1827 final Context context = getActivity();
1828
1829 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1830 builder.setTitle(R.string.roaming_reenable_title);
1831 builder.setMessage(R.string.roaming_warning);
1832
1833 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1834 public void onClick(DialogInterface dialog, int which) {
1835 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1836 if (target != null) {
1837 target.setDataRoaming(true);
1838 }
1839 }
1840 });
1841 builder.setNegativeButton(android.R.string.cancel, null);
1842
1843 return builder.create();
1844 }
1845 }
1846
1847 /**
1848 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001849 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001850 */
1851 public static class ConfirmRestrictFragment extends DialogFragment {
1852 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001853 if (!parent.isAdded()) return;
1854
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001855 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1856 dialog.setTargetFragment(parent, 0);
1857 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1858 }
1859
1860 @Override
1861 public Dialog onCreateDialog(Bundle savedInstanceState) {
1862 final Context context = getActivity();
1863
1864 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001865 builder.setTitle(R.string.data_usage_restrict_background_title);
Jeff Sharkey461842a2011-09-25 18:22:48 -07001866 builder.setMessage(getString(R.string.data_usage_restrict_background));
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001867
1868 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1869 public void onClick(DialogInterface dialog, int which) {
1870 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1871 if (target != null) {
1872 target.setRestrictBackground(true);
1873 }
1874 }
1875 });
1876 builder.setNegativeButton(android.R.string.cancel, null);
1877
1878 return builder.create();
1879 }
1880 }
1881
1882 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001883 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
1884 * change has been denied, usually based on
1885 * {@link DataUsageSummary#hasLimitedNetworks()}.
1886 */
1887 public static class DeniedRestrictFragment extends DialogFragment {
1888 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001889 if (!parent.isAdded()) return;
1890
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001891 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
1892 dialog.setTargetFragment(parent, 0);
1893 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
1894 }
1895
1896 @Override
1897 public Dialog onCreateDialog(Bundle savedInstanceState) {
1898 final Context context = getActivity();
1899
1900 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1901 builder.setTitle(R.string.data_usage_app_restrict_background);
1902 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
1903 builder.setPositiveButton(android.R.string.ok, null);
1904
1905 return builder.create();
1906 }
1907 }
1908
1909 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001910 * Dialog to request user confirmation before setting
1911 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
1912 */
1913 public static class ConfirmAppRestrictFragment extends DialogFragment {
1914 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001915 if (!parent.isAdded()) return;
1916
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001917 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
1918 dialog.setTargetFragment(parent, 0);
1919 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
1920 }
1921
1922 @Override
1923 public Dialog onCreateDialog(Bundle savedInstanceState) {
1924 final Context context = getActivity();
1925
1926 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001927 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
1928 builder.setMessage(R.string.data_usage_app_restrict_dialog);
1929
1930 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1931 public void onClick(DialogInterface dialog, int which) {
1932 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1933 if (target != null) {
1934 target.setAppRestrictBackground(true);
1935 }
1936 }
1937 });
1938 builder.setNegativeButton(android.R.string.cancel, null);
1939
1940 return builder.create();
1941 }
1942 }
1943
1944 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001945 * Compute default tab that should be selected, based on
1946 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
1947 */
1948 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07001949 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
1950 if (template == null) return null;
1951
1952 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07001953 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001954 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001955 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001956 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001957 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001958 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001959 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001960 return TAB_WIFI;
1961 default:
1962 return null;
1963 }
1964 }
1965
1966 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001967 * Background task that loads {@link UidDetail}, binding to
1968 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001969 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001970 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
1971 private final UidDetailProvider mProvider;
1972 private final AppUsageItem mItem;
1973 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001974
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001975 private UidDetailTask(UidDetailProvider provider, AppUsageItem item, View target) {
1976 mProvider = checkNotNull(provider);
1977 mItem = checkNotNull(item);
1978 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001979 }
1980
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001981 public static void bindView(
1982 UidDetailProvider provider, AppUsageItem item, View target) {
1983 final UidDetailTask existing = (UidDetailTask) target.getTag();
1984 if (existing != null) {
1985 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001986 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001987
1988 final UidDetail cachedDetail = provider.getUidDetail(item.uids[0], false);
1989 if (cachedDetail != null) {
1990 bindView(cachedDetail, target);
1991 } else {
1992 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
1993 AsyncTask.THREAD_POOL_EXECUTOR));
1994 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001995 }
1996
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001997 private static void bindView(UidDetail detail, View target) {
1998 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
1999 final TextView title = (TextView) target.findViewById(android.R.id.title);
2000
2001 if (detail != null) {
2002 icon.setImageDrawable(detail.icon);
2003 title.setText(detail.label);
2004 } else {
2005 icon.setImageDrawable(null);
2006 title.setText(null);
2007 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002008 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002009
2010 @Override
2011 protected void onPreExecute() {
2012 bindView(null, mTarget);
2013 }
2014
2015 @Override
2016 protected UidDetail doInBackground(Void... params) {
2017 return mProvider.getUidDetail(mItem.uids[0], true);
2018 }
2019
2020 @Override
2021 protected void onPostExecute(UidDetail result) {
2022 bindView(result, mTarget);
2023 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002024 }
2025
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002026 /**
2027 * Test if device has a mobile data radio.
2028 */
2029 private static boolean hasMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002030 if (TEST_RADIOS) {
2031 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2032 }
2033
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002034 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
2035 Context.CONNECTIVITY_SERVICE);
2036
2037 // mobile devices should have MOBILE network tracker regardless of
2038 // connection status.
2039 return conn.getNetworkInfo(TYPE_MOBILE) != null;
2040 }
2041
2042 /**
2043 * Test if device has a mobile 4G data radio.
2044 */
2045 private static boolean hasMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002046 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2047 return false;
2048 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002049 if (TEST_RADIOS) {
2050 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2051 }
2052
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002053 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
2054 Context.CONNECTIVITY_SERVICE);
2055 final TelephonyManager telephony = (TelephonyManager) context.getSystemService(
2056 Context.TELEPHONY_SERVICE);
2057
2058 // WiMAX devices should have WiMAX network tracker regardless of
2059 // connection status.
2060 final boolean hasWimax = conn.getNetworkInfo(TYPE_WIMAX) != null;
2061 final boolean hasLte = telephony.getLteOnCdmaMode() == Phone.LTE_ON_CDMA_TRUE;
2062 return hasWimax || hasLte;
2063 }
2064
2065 /**
2066 * Test if device has a Wi-Fi data radio.
2067 */
2068 private static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002069 if (TEST_RADIOS) {
2070 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2071 }
2072
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002073 return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI);
2074 }
2075
2076 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002077 * Test if device has an ethernet network connection.
2078 */
2079 private static boolean hasEthernet(Context context) {
2080 if (TEST_RADIOS) {
2081 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2082 }
2083
2084 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
2085 Context.CONNECTIVITY_SERVICE);
2086 return conn.getNetworkInfo(TYPE_ETHERNET) != null;
2087 }
2088
2089 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002090 * Inflate a {@link Preference} style layout, adding the given {@link View}
2091 * widget into {@link android.R.id#widget_frame}.
2092 */
2093 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2094 final View view = inflater.inflate(R.layout.preference, root, false);
2095 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2096 android.R.id.widget_frame);
2097 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2098 return view;
2099 }
2100
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002101 private static View inflateAppTitle(
2102 LayoutInflater inflater, ViewGroup root, CharSequence label) {
2103 final TextView view = (TextView) inflater.inflate(
2104 R.layout.data_usage_app_title, root, false);
2105 view.setText(label);
2106 return view;
2107 }
2108
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002109 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002110 * Test if any networks are currently limited.
2111 */
2112 private boolean hasLimitedNetworks() {
2113 return !buildLimitedNetworksList().isEmpty();
2114 }
2115
2116 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002117 * Build string describing currently limited networks, which defines when
2118 * background data is restricted.
2119 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002120 private CharSequence buildLimitedNetworksString() {
2121 final List<CharSequence> limited = buildLimitedNetworksList();
2122
2123 // handle case where no networks limited
2124 if (limited.isEmpty()) {
2125 limited.add(getText(R.string.data_usage_list_none));
2126 }
2127
2128 return TextUtils.join(limited);
2129 }
2130
2131 /**
2132 * Build list of currently limited networks, which defines when background
2133 * data is restricted.
2134 */
2135 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002136 final Context context = getActivity();
2137 final String subscriberId = getActiveSubscriberId(context);
2138
2139 // build combined list of all limited networks
2140 final ArrayList<CharSequence> limited = Lists.newArrayList();
2141 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2142 limited.add(getText(R.string.data_usage_list_mobile));
2143 }
2144 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2145 limited.add(getText(R.string.data_usage_tab_3g));
2146 }
2147 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2148 limited.add(getText(R.string.data_usage_tab_4g));
2149 }
2150 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifi())) {
2151 limited.add(getText(R.string.data_usage_tab_wifi));
2152 }
2153 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2154 limited.add(getText(R.string.data_usage_tab_ethernet));
2155 }
2156
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002157 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002158 }
2159
2160 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002161 * Inset both selector and divider {@link Drawable} on the given
2162 * {@link ListView} by the requested dimensions.
2163 */
2164 private static void insetListViewDrawables(ListView view, int insetSide) {
2165 final Drawable selector = view.getSelector();
2166 final Drawable divider = view.getDivider();
2167
2168 // fully unregister these drawables so callbacks can be maintained after
2169 // wrapping below.
2170 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2171 view.setSelector(stub);
2172 view.setDivider(stub);
2173
2174 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2175 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2176 }
2177
2178 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002179 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002180 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002181 */
2182 private static void setPreferenceTitle(View parent, int resId) {
2183 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2184 title.setText(resId);
2185 }
2186
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002187 /**
2188 * Set {@link android.R.id#summary} for a preference view inflated with
2189 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2190 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002191 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002192 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2193 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002194 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002195 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07002196
2197 private static boolean contains(int[] haystack, int needle) {
2198 for (int value : haystack) {
2199 if (value == needle) {
2200 return true;
2201 }
2202 }
2203 return false;
2204 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002205}