blob: 656288a83558c9c3690e50e4703dd966420e4736 [file] [log] [blame]
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070019import static android.net.ConnectivityManager.TYPE_ETHERNET;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070020import static android.net.ConnectivityManager.TYPE_MOBILE;
Jeff Sharkeybdf98e82011-11-10 17:17:24 -080021import static android.net.ConnectivityManager.TYPE_WIFI;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070022import static android.net.ConnectivityManager.TYPE_WIMAX;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070023import static android.net.NetworkPolicy.LIMIT_DISABLED;
Jeff Sharkeya53188f2011-09-13 19:56:45 -070024import static android.net.NetworkPolicy.WARNING_DISABLED;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070025import static android.net.NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070026import static android.net.NetworkPolicyManager.POLICY_NONE;
27import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND;
Jeff Sharkey8a503642011-06-10 13:31:21 -070028import static android.net.NetworkPolicyManager.computeLastCycleBoundary;
29import static android.net.NetworkPolicyManager.computeNextCycleBoundary;
Jeff Sharkeya662e492011-06-18 21:57:06 -070030import static android.net.NetworkTemplate.MATCH_MOBILE_3G_LOWER;
31import static android.net.NetworkTemplate.MATCH_MOBILE_4G;
32import static android.net.NetworkTemplate.MATCH_MOBILE_ALL;
33import static android.net.NetworkTemplate.MATCH_WIFI;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070034import static android.net.NetworkTemplate.buildTemplateEthernet;
35import static android.net.NetworkTemplate.buildTemplateMobile3gLower;
36import static android.net.NetworkTemplate.buildTemplateMobile4g;
37import static android.net.NetworkTemplate.buildTemplateMobileAll;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070038import static android.net.NetworkTemplate.buildTemplateWifiWildcard;
Jeff Sharkey77dae912012-02-03 14:50:33 -080039import static android.net.TrafficStats.GB_IN_BYTES;
40import static android.net.TrafficStats.MB_IN_BYTES;
Jeff Sharkeya83a24f2011-09-16 01:52:39 -070041import static android.net.TrafficStats.UID_REMOVED;
42import static android.net.TrafficStats.UID_TETHERING;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070043import static android.telephony.TelephonyManager.SIM_STATE_READY;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070044import static android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
45import static android.text.format.DateUtils.FORMAT_SHOW_DATE;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070046import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -070047import static com.android.internal.util.Preconditions.checkNotNull;
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -070048import static com.android.settings.Utils.prepareCustomPreferencesList;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070049
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070050import android.animation.LayoutTransition;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070051import android.app.AlertDialog;
52import android.app.Dialog;
53import android.app.DialogFragment;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070054import android.app.Fragment;
Amith Yamasani5ba0a022011-11-07 12:29:00 -080055import android.app.FragmentManager;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070056import android.app.FragmentTransaction;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070057import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070058import android.content.ContentResolver;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070059import android.content.Context;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070060import android.content.DialogInterface;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -070061import android.content.Intent;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070062import android.content.Loader;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070063import android.content.SharedPreferences;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070064import android.content.pm.PackageManager;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070065import android.content.res.Resources;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070066import android.graphics.Color;
Jeff Sharkey5d706792011-09-08 18:57:17 -070067import android.graphics.drawable.ColorDrawable;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070068import android.graphics.drawable.Drawable;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070069import android.net.ConnectivityManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070070import android.net.INetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070071import android.net.INetworkStatsService;
Jeff Sharkey8a503642011-06-10 13:31:21 -070072import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070073import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070074import android.net.NetworkStats;
75import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070076import android.net.NetworkTemplate;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070077import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070078import android.os.Bundle;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070079import android.os.INetworkManagementService;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070080import android.os.Parcel;
81import android.os.Parcelable;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070082import android.os.RemoteException;
83import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070084import android.os.SystemProperties;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070085import android.os.UserId;
Jeff Sharkey8a503642011-06-10 13:31:21 -070086import android.preference.Preference;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070087import android.preference.PreferenceActivity;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070088import android.provider.Settings;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070089import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070090import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070091import android.text.format.DateUtils;
92import android.text.format.Formatter;
Jeff Sharkeye5223a02012-03-09 17:11:14 -080093import android.text.format.Time;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070094import android.util.Log;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070095import android.util.SparseArray;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070096import android.util.SparseBooleanArray;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070097import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -070098import android.view.Menu;
99import android.view.MenuInflater;
100import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700101import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700102import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700103import android.view.ViewGroup;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700104import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700105import android.widget.AdapterView;
106import android.widget.AdapterView.OnItemClickListener;
107import android.widget.AdapterView.OnItemSelectedListener;
108import android.widget.ArrayAdapter;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700109import android.widget.BaseAdapter;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700110import android.widget.Button;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700111import android.widget.CheckBox;
112import android.widget.CompoundButton;
113import android.widget.CompoundButton.OnCheckedChangeListener;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700114import android.widget.ImageView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700115import android.widget.LinearLayout;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700116import android.widget.ListView;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700117import android.widget.NumberPicker;
Jeff Sharkey2412b0f2011-07-17 20:31:40 -0700118import android.widget.ProgressBar;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700119import android.widget.Spinner;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700120import android.widget.Switch;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700121import android.widget.TabHost;
122import android.widget.TabHost.OnTabChangeListener;
123import android.widget.TabHost.TabContentFactory;
124import android.widget.TabHost.TabSpec;
125import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700126import android.widget.TextView;
127
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700128import com.android.internal.telephony.Phone;
Jeff Sharkey5d706792011-09-08 18:57:17 -0700129import com.android.settings.drawable.InsetBoundsDrawable;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700130import com.android.settings.net.ChartData;
131import com.android.settings.net.ChartDataLoader;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700132import com.android.settings.net.DataUsageMeteredSettings;
Jeff Sharkeya662e492011-06-18 21:57:06 -0700133import com.android.settings.net.NetworkPolicyEditor;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700134import com.android.settings.net.SummaryForAllUidLoader;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700135import com.android.settings.net.UidDetail;
136import com.android.settings.net.UidDetailProvider;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700137import com.android.settings.widget.ChartDataUsageView;
138import com.android.settings.widget.ChartDataUsageView.DataUsageChartListener;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700139import com.android.settings.widget.PieChartView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700140import com.google.android.collect.Lists;
141
142import java.util.ArrayList;
143import java.util.Collections;
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700144import java.util.List;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700145import java.util.Locale;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700146
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700147import libcore.util.Objects;
148
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700149/**
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700150 * Panel showing data usage history across various networks, including options
151 * to inspect based on usage cycle and control through {@link NetworkPolicy}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700152 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700153public class DataUsageSummary extends Fragment {
154 private static final String TAG = "DataUsage";
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800155 private static final boolean LOGD = false;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700156
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700157 // TODO: remove this testing code
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700158 private static final boolean TEST_ANIM = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700159 private static final boolean TEST_RADIOS = false;
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800160
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700161 private static final String TEST_RADIOS_PROP = "test.radios";
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800162 private static final String TEST_SUBSCRIBER_PROP = "test.subscriberid";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700163
Jeff Sharkey8a503642011-06-10 13:31:21 -0700164 private static final String TAB_3G = "3g";
165 private static final String TAB_4G = "4g";
166 private static final String TAB_MOBILE = "mobile";
167 private static final String TAB_WIFI = "wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700168 private static final String TAB_ETHERNET = "ethernet";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700169
Jeff Sharkey28130d92011-09-02 16:10:24 -0700170 private static final String TAG_CONFIRM_DATA_DISABLE = "confirmDataDisable";
171 private static final String TAG_CONFIRM_DATA_ROAMING = "confirmDataRoaming";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700172 private static final String TAG_CONFIRM_LIMIT = "confirmLimit";
173 private static final String TAG_CYCLE_EDITOR = "cycleEditor";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700174 private static final String TAG_WARNING_EDITOR = "warningEditor";
175 private static final String TAG_LIMIT_EDITOR = "limitEditor";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700176 private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700177 private static final String TAG_DENIED_RESTRICT = "deniedRestrict";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700178 private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700179 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700180
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700181 private static final int LOADER_CHART_DATA = 2;
182 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700183
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700184 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700185 private INetworkStatsService mStatsService;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700186 private NetworkPolicyManager mPolicyManager;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700187 private ConnectivityManager mConnService;
188
189 private static final String PREF_FILE = "data_usage";
190 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700191 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700192
193 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700194
Jeff Sharkey8a503642011-06-10 13:31:21 -0700195 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700196 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700197 private TabWidget mTabWidget;
198 private ListView mListView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700199 private DataUsageAdapter mAdapter;
200
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700201 /** Distance to inset content from sides, when needed. */
202 private int mInsetSide = 0;
203
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700204 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700205
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700206 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700207 private LinearLayout mNetworkSwitches;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700208 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700209 private View mDataEnabledView;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700210 private CheckBox mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700211 private View mDisableAtLimitView;
212
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700213 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700214 private Spinner mCycleSpinner;
215 private CycleAdapter mCycleAdapter;
216
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700217 private ChartDataUsageView mChart;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700218 private TextView mUsageSummary;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700219 private TextView mEmpty;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700220
221 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700222 private ImageView mAppIcon;
223 private ViewGroup mAppTitles;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700224 private PieChartView mAppPieChart;
225 private TextView mAppForeground;
226 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700227 private Button mAppSettings;
228
229 private LinearLayout mAppSwitches;
230 private CheckBox mAppRestrict;
231 private View mAppRestrictView;
232
Jeff Sharkey8a503642011-06-10 13:31:21 -0700233 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700234 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700235
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700236 private NetworkTemplate mTemplate;
237 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700238
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700239 private AppItem mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700240
241 private Intent mAppSettingsIntent;
242
Jeff Sharkeya662e492011-06-18 21:57:06 -0700243 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700244
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700245 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700246 private String mIntentTab = null;
247
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700248 private MenuItem mMenuDataRoaming;
249 private MenuItem mMenuRestrictBackground;
250
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700251 /** Flag used to ignore listeners during binding. */
252 private boolean mBinding;
253
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700254 private UidDetailProvider mUidDetailProvider;
255
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700256 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700257 public void onCreate(Bundle savedInstanceState) {
258 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700259 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700260
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700261 mNetworkService = INetworkManagementService.Stub.asInterface(
262 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700263 mStatsService = INetworkStatsService.Stub.asInterface(
264 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700265 mPolicyManager = NetworkPolicyManager.from(context);
266 mConnService = ConnectivityManager.from(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700267
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700268 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700269
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700270 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700271 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700272
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700273 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700274 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700275
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700276 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700277 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700278
Jeff Sharkey8a503642011-06-10 13:31:21 -0700279 @Override
280 public View onCreateView(LayoutInflater inflater, ViewGroup container,
281 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700282
Jeff Sharkey8a503642011-06-10 13:31:21 -0700283 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700284 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
285
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700286 mUidDetailProvider = new UidDetailProvider(context);
287
Jeff Sharkey8a503642011-06-10 13:31:21 -0700288 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700289 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700290 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
291 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700292
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700293 // decide if we need to manually inset our content, or if we should rely
294 // on parent container for inset.
295 final boolean shouldInset = mListView.getScrollBarStyle()
296 == View.SCROLLBARS_OUTSIDE_OVERLAY;
297 if (shouldInset) {
298 mInsetSide = view.getResources().getDimensionPixelOffset(
299 com.android.internal.R.dimen.preference_fragment_padding_side);
300 } else {
301 mInsetSide = 0;
302 }
303
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700304 // adjust padding around tabwidget as needed
Jeff Sharkey5d706792011-09-08 18:57:17 -0700305 prepareCustomPreferencesList(container, view, mListView, true);
306
Jeff Sharkey8a503642011-06-10 13:31:21 -0700307 mTabHost.setup();
308 mTabHost.setOnTabChangedListener(mTabListener);
309
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700310 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700311 mHeader.setClickable(true);
312
313 mListView.addHeaderView(mHeader, null, true);
314 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700315
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700316 if (mInsetSide > 0) {
317 // inset selector and divider drawables
318 insetListViewDrawables(mListView, mInsetSide);
319 mHeader.setPadding(mInsetSide, 0, mInsetSide, 0);
320 }
321
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700322 {
323 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700324 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
325 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700326 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700327
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700328 mDataEnabled = new Switch(inflater.getContext());
329 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
330 mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener);
331 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700332
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700333 mDisableAtLimit = new CheckBox(inflater.getContext());
334 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700335 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700336 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700337 mDisableAtLimitView.setClickable(true);
338 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700339 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
340 mNetworkSwitches.addView(mDisableAtLimitView);
341 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700342
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700343 // bind cycle dropdown
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700344 mCycleView = mHeader.findViewById(R.id.cycles);
345 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700346 mCycleAdapter = new CycleAdapter(context);
347 mCycleSpinner.setAdapter(mCycleAdapter);
348 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
349
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700350 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700351 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800352 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700353
354 {
355 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700356 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700357 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
358 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700359 mAppPieChart = (PieChartView) mAppDetail.findViewById(R.id.app_pie_chart);
360 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
361 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700362 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700363
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700364 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700365 mAppSettings.setOnClickListener(mAppSettingsListener);
366
367 mAppRestrict = new CheckBox(inflater.getContext());
368 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700369 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700370 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700371 mAppRestrictView.setClickable(true);
372 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700373 mAppRestrictView.setOnClickListener(mAppRestrictListener);
374 mAppSwitches.addView(mAppRestrictView);
375 }
376
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700377 mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700378 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700379
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700380 // only assign layout transitions once first layout is finished
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700381 mListView.getViewTreeObserver().addOnGlobalLayoutListener(mFirstLayoutListener);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700382
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700383 mAdapter = new DataUsageAdapter(mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700384 mListView.setOnItemClickListener(mListListener);
385 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700386
387 return view;
388 }
389
390 @Override
391 public void onResume() {
392 super.onResume();
393
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700394 // pick default tab based on incoming intent
395 final Intent intent = getActivity().getIntent();
396 mIntentTab = computeTabFromIntent(intent);
397
Jeff Sharkey8a503642011-06-10 13:31:21 -0700398 // this kicks off chain reaction which creates tabs, binds the body to
399 // selected network, and binds chart, cycles and detail list.
400 updateTabs();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700401
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700402 // kick off background task to update stats
403 new AsyncTask<Void, Void, Void>() {
404 @Override
405 protected Void doInBackground(Void... params) {
406 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700407 // wait a few seconds before kicking off
408 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700409 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700410 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700411 } catch (RemoteException e) {
412 }
413 return null;
414 }
415
416 @Override
417 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700418 if (isAdded()) {
419 updateBody();
420 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700421 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700422 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700423 }
424
Jeff Sharkey8a503642011-06-10 13:31:21 -0700425 @Override
426 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
427 inflater.inflate(R.menu.data_usage, menu);
428 }
429
430 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700431 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700432 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700433 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700434
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700435 mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700436 mMenuDataRoaming.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700437 mMenuDataRoaming.setChecked(getDataRoaming());
438
439 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700440 mMenuRestrictBackground.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
441 mMenuRestrictBackground.setChecked(mPolicyManager.getRestrictBackground());
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700442
443 final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700444 split4g.setVisible(hasMobile4gRadio(context) && !appDetailMode);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700445 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700446
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700447 final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700448 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700449 showWifi.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700450 showWifi.setChecked(mShowWifi);
451 } else {
452 showWifi.setVisible(false);
453 mShowWifi = true;
454 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700455
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700456 final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700457 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700458 showEthernet.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700459 showEthernet.setChecked(mShowEthernet);
460 } else {
461 showEthernet.setVisible(false);
462 mShowEthernet = true;
463 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700464
465 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
466 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
467 metered.setVisible(!appDetailMode);
468 } else {
469 metered.setVisible(false);
470 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700471 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700472
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700473 @Override
474 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700475 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700476 case R.id.data_usage_menu_roaming: {
477 final boolean dataRoaming = !item.isChecked();
478 if (dataRoaming) {
479 ConfirmDataRoamingFragment.show(this);
480 } else {
481 // no confirmation to disable roaming
482 setDataRoaming(false);
483 }
484 return true;
485 }
486 case R.id.data_usage_menu_restrict_background: {
487 final boolean restrictBackground = !item.isChecked();
488 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800489 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700490 } else {
491 // no confirmation to drop restriction
492 setRestrictBackground(false);
493 }
494 return true;
495 }
496 case R.id.data_usage_menu_split_4g: {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700497 final boolean mobileSplit = !item.isChecked();
Jeff Sharkeya662e492011-06-18 21:57:06 -0700498 setMobilePolicySplit(mobileSplit);
499 item.setChecked(isMobilePolicySplit());
Jeff Sharkey8a503642011-06-10 13:31:21 -0700500 updateTabs();
501 return true;
502 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700503 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700504 mShowWifi = !item.isChecked();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700505 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700506 item.setChecked(mShowWifi);
507 updateTabs();
508 return true;
509 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700510 case R.id.data_usage_menu_show_ethernet: {
511 mShowEthernet = !item.isChecked();
512 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
513 item.setChecked(mShowEthernet);
514 updateTabs();
515 return true;
516 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700517 case R.id.data_usage_menu_metered: {
518 final PreferenceActivity activity = (PreferenceActivity) getActivity();
519 activity.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
520 R.string.data_usage_metered_title, null, this, 0);
521 return true;
522 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700523 }
524 return false;
525 }
526
Jeff Sharkey94a90952011-06-13 22:31:09 -0700527 @Override
528 public void onDestroyView() {
529 super.onDestroyView();
530
531 mDataEnabledView = null;
532 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700533
534 mUidDetailProvider.clearCache();
535 mUidDetailProvider = null;
Jeff Sharkey94a90952011-06-13 22:31:09 -0700536 }
537
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800538 @Override
539 public void onDestroy() {
540 if (this.isRemoving()) {
541 getFragmentManager()
542 .popBackStack(TAG_APP_DETAILS, FragmentManager.POP_BACK_STACK_INCLUSIVE);
543 }
544 super.onDestroy();
545 }
546
Jeff Sharkey8a503642011-06-10 13:31:21 -0700547 /**
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700548 * Listener to setup {@link LayoutTransition} after first layout pass.
549 */
550 private OnGlobalLayoutListener mFirstLayoutListener = new OnGlobalLayoutListener() {
551 /** {@inheritDoc} */
552 public void onGlobalLayout() {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700553 mListView.getViewTreeObserver().removeGlobalOnLayoutListener(mFirstLayoutListener);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700554
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700555 mTabsContainer.setLayoutTransition(buildLayoutTransition());
556 mHeader.setLayoutTransition(buildLayoutTransition());
557 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700558
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700559 final LayoutTransition chartTransition = buildLayoutTransition();
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700560 chartTransition.setStartDelay(LayoutTransition.APPEARING, 0);
561 chartTransition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700562 chartTransition.setAnimator(LayoutTransition.APPEARING, null);
Jeff Sharkey28130d92011-09-02 16:10:24 -0700563 chartTransition.setAnimator(LayoutTransition.DISAPPEARING, null);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700564 mChart.setLayoutTransition(chartTransition);
565 }
566 };
567
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700568 private static LayoutTransition buildLayoutTransition() {
569 final LayoutTransition transition = new LayoutTransition();
570 if (TEST_ANIM) {
571 transition.setDuration(1500);
572 }
573 transition.setAnimateParentHierarchy(false);
574 return transition;
575 }
576
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700577 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700578 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700579 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
580 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700581 */
582 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700583 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700584 mTabHost.clearAllTabs();
585
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700586 final boolean mobileSplit = isMobilePolicySplit();
587 if (mobileSplit && hasMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700588 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
589 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700590 } else if (hasReadyMobileRadio(context)) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700591 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700592 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700593 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700594 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
595 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700596 if (mShowEthernet && hasEthernet(context)) {
597 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
598 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700599
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700600 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
601 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
602 if (mIntentTab != null) {
603 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700604 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700605 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700606 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700607 mTabHost.setCurrentTabByTag(mIntentTab);
608 }
609 mIntentTab = null;
610 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700611 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700612 }
613 }
614
Jeff Sharkey8a503642011-06-10 13:31:21 -0700615 /**
616 * Factory that provide empty {@link View} to make {@link TabHost} happy.
617 */
618 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
619 /** {@inheritDoc} */
620 public View createTabContent(String tag) {
621 return new View(mTabHost.getContext());
622 }
623 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700624
Jeff Sharkey8a503642011-06-10 13:31:21 -0700625 /**
626 * Build {@link TabSpec} with thin indicator, and empty content.
627 */
628 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700629 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
630 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700631 }
632
633 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
634 /** {@inheritDoc} */
635 public void onTabChanged(String tabId) {
636 // user changed tab; update body
637 updateBody();
638 }
639 };
640
641 /**
642 * Update body content based on current tab. Loads
643 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
644 * binds them to visible controls.
645 */
646 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700647 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700648 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700649
Jeff Sharkeya662e492011-06-18 21:57:06 -0700650 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700651 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700652
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700653 if (currentTab == null) {
654 Log.w(TAG, "no tab selected; hiding body");
655 mListView.setVisibility(View.GONE);
656 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700657 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700658 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700659 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700660
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700661 final boolean tabChanged = !currentTab.equals(mCurrentTab);
662 mCurrentTab = currentTab;
663
Jeff Sharkey8a503642011-06-10 13:31:21 -0700664 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
665
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700666 mDataEnabledView.setVisibility(View.VISIBLE);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700667
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700668 // TODO: remove mobile tabs when SIM isn't ready
669 final TelephonyManager tele = TelephonyManager.from(context);
670
Jeff Sharkey8a503642011-06-10 13:31:21 -0700671 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700672 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
673 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700674 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700675
676 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700677 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
678 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
679 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700680 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700681
682 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700683 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
684 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
685 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700686 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700687
688 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700689 // wifi doesn't have any controls
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700690 mDataEnabledView.setVisibility(View.GONE);
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700691 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700692 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700693
694 } else if (TAB_ETHERNET.equals(currentTab)) {
695 // ethernet doesn't have any controls
696 mDataEnabledView.setVisibility(View.GONE);
697 mDisableAtLimitView.setVisibility(View.GONE);
698 mTemplate = buildTemplateEthernet();
699
700 } else {
701 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700702 }
703
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700704 // kick off loader for network history
705 // TODO: consider chaining two loaders together instead of reloading
706 // network history when showing app detail.
707 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700708 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700709
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700710 // detail mode can change visible menus, invalidate
711 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700712
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700713 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700714 }
715
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700716 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700717 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700718 }
719
720 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700721 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700722 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700723 */
724 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700725 final Context context = getActivity();
726 final PackageManager pm = context.getPackageManager();
727 final LayoutInflater inflater = getActivity().getLayoutInflater();
728
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700729 if (isAppDetailMode()) {
730 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700731 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700732 } else {
733 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700734 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700735
736 // hide detail stats when not in detail mode
737 mChart.bindDetailNetworkStats(null);
738 return;
739 }
740
741 // remove warning/limit sweeps while in detail mode
742 mChart.bindNetworkPolicy(null);
743
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700744 // show icon and all labels appearing under this app
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700745 final int appId = mCurrentApp.appId;
746 final UidDetail detail = mUidDetailProvider.getUidDetail(appId, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700747 mAppIcon.setImageDrawable(detail.icon);
748
749 mAppTitles.removeAllViews();
750 if (detail.detailLabels != null) {
751 for (CharSequence label : detail.detailLabels) {
752 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label));
753 }
754 } else {
755 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label));
756 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700757
758 // enable settings button when package provides it
759 // TODO: target torwards entire UID instead of just first package
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700760 final String[] packageNames = pm.getPackagesForUid(appId);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700761 if (packageNames != null && packageNames.length > 0) {
762 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
763 mAppSettingsIntent.setPackage(packageNames[0]);
764 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
765
766 final boolean matchFound = pm.resolveActivity(mAppSettingsIntent, 0) != null;
767 mAppSettings.setEnabled(matchFound);
768
769 } else {
770 mAppSettingsIntent = null;
771 mAppSettings.setEnabled(false);
772 }
773
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700774 updateDetailData();
775
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700776 if (UserId.isApp(appId) && !mPolicyManager.getRestrictBackground()
777 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700778 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800779 setPreferenceSummary(mAppRestrictView,
780 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700781
782 mAppRestrictView.setVisibility(View.VISIBLE);
783 mAppRestrict.setChecked(getAppRestrictBackground());
784
785 } else {
786 mAppRestrictView.setVisibility(View.GONE);
787 }
788 }
789
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700790 private void setPolicyWarningBytes(long warningBytes) {
791 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700792 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700793 updatePolicy(false);
794 }
795
796 private void setPolicyLimitBytes(long limitBytes) {
797 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700798 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700799 updatePolicy(false);
800 }
801
Jeff Sharkey28130d92011-09-02 16:10:24 -0700802 /**
803 * Local cache of value, used to work around delay when
804 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
805 */
806 private Boolean mMobileDataEnabled;
807
808 private boolean isMobileDataEnabled() {
809 if (mMobileDataEnabled != null) {
810 // TODO: deprecate and remove this once enabled flag is on policy
811 return mMobileDataEnabled;
812 } else {
813 return mConnService.getMobileDataEnabled();
814 }
815 }
816
817 private void setMobileDataEnabled(boolean enabled) {
818 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
819 mConnService.setMobileDataEnabled(enabled);
820 mMobileDataEnabled = enabled;
821 updatePolicy(false);
822 }
823
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700824 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
825 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked();
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700826 }
827
828 private boolean isBandwidthControlEnabled() {
829 try {
830 return mNetworkService.isBandwidthControlEnabled();
831 } catch (RemoteException e) {
832 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
833 return false;
834 }
835 }
836
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700837 private boolean getDataRoaming() {
838 final ContentResolver resolver = getActivity().getContentResolver();
839 return Settings.Secure.getInt(resolver, Settings.Secure.DATA_ROAMING, 0) != 0;
840 }
841
842 private void setDataRoaming(boolean enabled) {
843 // TODO: teach telephony DataConnectionTracker to watch and apply
844 // updates when changed.
845 final ContentResolver resolver = getActivity().getContentResolver();
846 Settings.Secure.putInt(resolver, Settings.Secure.DATA_ROAMING, enabled ? 1 : 0);
847 mMenuDataRoaming.setChecked(enabled);
848 }
849
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700850 public void setRestrictBackground(boolean restrictBackground) {
851 mPolicyManager.setRestrictBackground(restrictBackground);
852 mMenuRestrictBackground.setChecked(restrictBackground);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700853 }
854
855 private boolean getAppRestrictBackground() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700856 final int appId = mCurrentApp.appId;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700857 final int uidPolicy = mPolicyManager.getAppPolicy(appId);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700858 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
859 }
860
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700861 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700862 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700863 final int appId = mCurrentApp.appId;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700864 mPolicyManager.setAppPolicy(appId,
865 restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700866 mAppRestrict.setChecked(restrictBackground);
867 }
868
Jeff Sharkey8a503642011-06-10 13:31:21 -0700869 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700870 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
871 * current {@link #mTemplate}.
872 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700873 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700874 if (isAppDetailMode()) {
875 mNetworkSwitches.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700876 } else {
877 mNetworkSwitches.setVisibility(View.VISIBLE);
878 }
879
Jeff Sharkey28130d92011-09-02 16:10:24 -0700880 // TODO: move enabled state directly into policy
881 if (TAB_MOBILE.equals(mCurrentTab)) {
882 mBinding = true;
883 mDataEnabled.setChecked(isMobileDataEnabled());
884 mBinding = false;
885 }
886
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700887 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
888 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700889 mDisableAtLimitView.setVisibility(View.VISIBLE);
890 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700891 if (!isAppDetailMode()) {
892 mChart.bindNetworkPolicy(policy);
893 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700894
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700895 } else {
896 // controls are disabled; don't bind warning/limit sweeps
897 mDisableAtLimitView.setVisibility(View.GONE);
898 mChart.bindNetworkPolicy(null);
899 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700900
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700901 if (refreshCycle) {
902 // generate cycle list based on policy and available history
903 updateCycleList(policy);
904 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700905 }
906
907 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700908 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
909 * and available {@link NetworkStatsHistory} data. Always selects the newest
910 * item, updating the inspection range on {@link #mChart}.
911 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700912 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700913 // stash away currently selected cycle to try restoring below
914 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700915 mCycleAdapter.clear();
916
917 final Context context = mCycleSpinner.getContext();
918
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700919 long historyStart = Long.MAX_VALUE;
920 long historyEnd = Long.MIN_VALUE;
921 if (mChartData != null) {
922 historyStart = mChartData.network.getStart();
923 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700924 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700925
Jeff Sharkey461842a2011-09-25 18:22:48 -0700926 final long now = System.currentTimeMillis();
927 if (historyStart == Long.MAX_VALUE) historyStart = now;
928 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700929
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700930 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700931 if (policy != null) {
932 // find the next cycle boundary
933 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700934
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700935 // walk backwards, generating all valid cycle ranges
936 while (cycleEnd > historyStart) {
937 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
938 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
939 + historyStart);
940 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
941 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700942 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700943 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700944
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700945 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700946 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700947 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700948
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700949 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700950 // no policy defined cycles; show entry for each four-week period
951 long cycleEnd = historyEnd;
952 while (cycleEnd > historyStart) {
953 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
954 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
955 cycleEnd = cycleStart;
956 }
957
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700958 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700959 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700960
961 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700962 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700963 final int position = mCycleAdapter.findNearestPosition(previousItem);
964 mCycleSpinner.setSelection(position);
965
966 // only force-update cycle when changed; skipping preserves any
967 // user-defined inspection region.
968 final CycleItem selectedItem = mCycleAdapter.getItem(position);
969 if (!Objects.equal(selectedItem, previousItem)) {
970 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
971 } else {
972 // but still kick off loader for detailed list
973 updateDetailData();
974 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700975 } else {
976 updateDetailData();
977 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700978 }
979
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700980 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700981 /** {@inheritDoc} */
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700982 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
983 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700984
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700985 final boolean dataEnabled = isChecked;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700986 final String currentTab = mCurrentTab;
987 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -0700988 if (dataEnabled) {
989 setMobileDataEnabled(true);
990 } else {
991 // disabling data; show confirmation dialog which eventually
992 // calls setMobileDataEnabled() once user confirms.
993 ConfirmDataDisableFragment.show(DataUsageSummary.this);
994 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700995 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700996
Jeff Sharkey28130d92011-09-02 16:10:24 -0700997 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700998 }
999 };
1000
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001001 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001002 /** {@inheritDoc} */
1003 public void onClick(View v) {
1004 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001005 if (disableAtLimit) {
1006 // enabling limit; show confirmation dialog which eventually
1007 // calls setPolicyLimitBytes() once user confirms.
1008 ConfirmLimitFragment.show(DataUsageSummary.this);
1009 } else {
1010 setPolicyLimitBytes(LIMIT_DISABLED);
1011 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001012 }
1013 };
1014
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001015 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
1016 /** {@inheritDoc} */
1017 public void onClick(View v) {
1018 final boolean restrictBackground = !mAppRestrict.isChecked();
1019
1020 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001021 // enabling restriction; show confirmation dialog which
1022 // eventually calls setRestrictBackground() once user
1023 // confirms.
1024 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001025 } else {
1026 setAppRestrictBackground(false);
1027 }
1028 }
1029 };
1030
1031 private OnClickListener mAppSettingsListener = new OnClickListener() {
1032 /** {@inheritDoc} */
1033 public void onClick(View v) {
1034 // TODO: target torwards entire UID instead of just first package
1035 startActivity(mAppSettingsIntent);
1036 }
1037 };
1038
Jeff Sharkey8a503642011-06-10 13:31:21 -07001039 private OnItemClickListener mListListener = new OnItemClickListener() {
1040 /** {@inheritDoc} */
1041 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001042 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001043 final AppItem app = (AppItem) parent.getItemAtPosition(position);
1044 final UidDetail detail = mUidDetailProvider.getUidDetail(app.appId, true);
1045 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001046 }
1047 };
1048
1049 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
1050 /** {@inheritDoc} */
1051 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1052 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1053 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001054 // show cycle editor; will eventually call setPolicyCycleDay()
1055 // when user finishes editing.
1056 CycleEditorFragment.show(DataUsageSummary.this);
1057
1058 // reset spinner to something other than "change cycle..."
1059 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001060
1061 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001062 if (LOGD) {
1063 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1064 + cycle.end + "]");
1065 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001066
1067 // update chart to show selected cycle, and update detail data
1068 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001069 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001070
1071 updateDetailData();
1072 }
1073 }
1074
1075 /** {@inheritDoc} */
1076 public void onNothingSelected(AdapterView<?> parent) {
1077 // ignored
1078 }
1079 };
1080
1081 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001082 * Update details based on {@link #mChart} inspection range depending on
1083 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1084 * of applications data usage, and when {@link #isAppDetailMode()} update
1085 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001086 */
1087 private void updateDetailData() {
1088 if (LOGD) Log.d(TAG, "updateDetailData()");
1089
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001090 final long start = mChart.getInspectStart();
1091 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001092 final long now = System.currentTimeMillis();
1093
1094 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001095
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001096 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001097 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001098 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001099 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001100 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001101 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001102 final long foregroundBytes = entry.rxBytes + entry.txBytes;
1103
1104 mAppPieChart.setOriginAngle(175);
1105
1106 mAppPieChart.removeAllSlices();
1107 mAppPieChart.addSlice(foregroundBytes, Color.parseColor("#d88d3a"));
1108 mAppPieChart.addSlice(defaultBytes, Color.parseColor("#666666"));
1109
1110 mAppPieChart.generatePath();
1111
1112 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1113 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1114
1115 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001116 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001117
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001118 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001119
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001120 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001121 if (mChartData != null) {
1122 entry = mChartData.network.getValues(start, end, now, null);
1123 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001124
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001125 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001126 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001127 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001128 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001129
1130 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1131 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001132 final String rangePhrase = formatDateRange(context, start, end);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001133
1134 mUsageSummary.setText(
1135 getString(R.string.data_usage_total_during_range, totalPhrase, rangePhrase));
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001136 }
1137
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001138 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1139 ChartData>() {
1140 /** {@inheritDoc} */
1141 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
1142 return new ChartDataLoader(getActivity(), mStatsService, args);
1143 }
1144
1145 /** {@inheritDoc} */
1146 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1147 mChartData = data;
1148 mChart.bindNetworkStats(mChartData.network);
1149 mChart.bindDetailNetworkStats(mChartData.detail);
1150
1151 // calcuate policy cycles based on available data
1152 updatePolicy(true);
1153 updateAppDetail();
1154
1155 // force scroll to top of body when showing detail
1156 if (mChartData.detail != null) {
1157 mListView.smoothScrollToPosition(0);
1158 }
1159 }
1160
1161 /** {@inheritDoc} */
1162 public void onLoaderReset(Loader<ChartData> loader) {
1163 mChartData = null;
1164 mChart.bindNetworkStats(null);
1165 mChart.bindDetailNetworkStats(null);
1166 }
1167 };
1168
1169 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001170 NetworkStats>() {
1171 /** {@inheritDoc} */
1172 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
1173 return new SummaryForAllUidLoader(getActivity(), mStatsService, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001174 }
1175
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001176 /** {@inheritDoc} */
1177 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
1178 mAdapter.bindStats(data);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001179 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001180 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001181
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001182 /** {@inheritDoc} */
1183 public void onLoaderReset(Loader<NetworkStats> loader) {
1184 mAdapter.bindStats(null);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001185 updateEmptyVisible();
1186 }
1187
1188 private void updateEmptyVisible() {
1189 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1190 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001191 }
1192 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001193
Jeff Sharkeya662e492011-06-18 21:57:06 -07001194 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001195 final Context context = getActivity();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001196 if (hasReadyMobileRadio(context)) {
1197 final TelephonyManager tele = TelephonyManager.from(context);
1198 return mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001199 } else {
1200 return false;
1201 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001202 }
1203
1204 private void setMobilePolicySplit(boolean split) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001205 final Context context = getActivity();
1206 if (hasReadyMobileRadio(context)) {
1207 final TelephonyManager tele = TelephonyManager.from(context);
1208 mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split);
1209 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001210 }
1211
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001212 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001213 final TelephonyManager tele = TelephonyManager.from(context);
1214 final String actualSubscriberId = tele.getSubscriberId();
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -08001215 return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001216 }
1217
Jeff Sharkey8a503642011-06-10 13:31:21 -07001218 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
1219 /** {@inheritDoc} */
1220 public void onInspectRangeChanged() {
1221 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
1222 updateDetailData();
1223 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001224
Jeff Sharkey8a503642011-06-10 13:31:21 -07001225 /** {@inheritDoc} */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001226 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001227 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001228 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001229
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001230 /** {@inheritDoc} */
1231 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001232 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001233 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001234
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001235 /** {@inheritDoc} */
1236 public void requestWarningEdit() {
1237 WarningEditorFragment.show(DataUsageSummary.this);
1238 }
1239
1240 /** {@inheritDoc} */
1241 public void requestLimitEdit() {
1242 LimitEditorFragment.show(DataUsageSummary.this);
1243 }
1244 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001245
1246 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001247 * List item that reflects a specific data usage cycle.
1248 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001249 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001250 public CharSequence label;
1251 public long start;
1252 public long end;
1253
Jeff Sharkey8a503642011-06-10 13:31:21 -07001254 CycleItem(CharSequence label) {
1255 this.label = label;
1256 }
1257
1258 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001259 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001260 this.start = start;
1261 this.end = end;
1262 }
1263
Jeff Sharkey8a503642011-06-10 13:31:21 -07001264 @Override
1265 public String toString() {
1266 return label.toString();
1267 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001268
1269 @Override
1270 public boolean equals(Object o) {
1271 if (o instanceof CycleItem) {
1272 final CycleItem another = (CycleItem) o;
1273 return start == another.start && end == another.end;
1274 }
1275 return false;
1276 }
1277
1278 /** {@inheritDoc} */
1279 public int compareTo(CycleItem another) {
1280 return Long.compare(start, another.start);
1281 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001282 }
1283
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001284 private static final StringBuilder sBuilder = new StringBuilder(50);
1285 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1286 sBuilder, Locale.getDefault());
1287
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001288 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001289 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001290
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001291 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001292 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001293 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1294 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001295 }
1296 }
1297
Jeff Sharkey8a503642011-06-10 13:31:21 -07001298 /**
1299 * Special-case data usage cycle that triggers dialog to change
1300 * {@link NetworkPolicy#cycleDay}.
1301 */
1302 public static class CycleChangeItem extends CycleItem {
1303 public CycleChangeItem(Context context) {
1304 super(context.getString(R.string.data_usage_change_cycle));
1305 }
1306 }
1307
1308 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001309 private boolean mChangePossible = false;
1310 private boolean mChangeVisible = false;
1311
1312 private final CycleChangeItem mChangeItem;
1313
Jeff Sharkey8a503642011-06-10 13:31:21 -07001314 public CycleAdapter(Context context) {
1315 super(context, android.R.layout.simple_spinner_item);
1316 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001317 mChangeItem = new CycleChangeItem(context);
1318 }
1319
1320 public void setChangePossible(boolean possible) {
1321 mChangePossible = possible;
1322 updateChange();
1323 }
1324
1325 public void setChangeVisible(boolean visible) {
1326 mChangeVisible = visible;
1327 updateChange();
1328 }
1329
1330 private void updateChange() {
1331 remove(mChangeItem);
1332 if (mChangePossible && mChangeVisible) {
1333 add(mChangeItem);
1334 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001335 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001336
1337 /**
1338 * Find position of {@link CycleItem} in this adapter which is nearest
1339 * the given {@link CycleItem}.
1340 */
1341 public int findNearestPosition(CycleItem target) {
1342 if (target != null) {
1343 final int count = getCount();
1344 for (int i = count - 1; i >= 0; i--) {
1345 final CycleItem item = getItem(i);
1346 if (item instanceof CycleChangeItem) {
1347 continue;
1348 } else if (item.compareTo(target) >= 0) {
1349 return i;
1350 }
1351 }
1352 }
1353 return 0;
1354 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001355 }
1356
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001357 public static class AppItem implements Comparable<AppItem>, Parcelable {
1358 public final int appId;
1359 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001360 public long total;
1361
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001362 public AppItem(int appId) {
1363 this.appId = appId;
1364 }
1365
1366 public AppItem(Parcel parcel) {
1367 appId = parcel.readInt();
1368 uids = parcel.readSparseBooleanArray();
1369 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001370 }
1371
1372 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001373 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001374 }
1375
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001376 @Override
1377 public void writeToParcel(Parcel dest, int flags) {
1378 dest.writeInt(appId);
1379 dest.writeSparseBooleanArray(uids);
1380 dest.writeLong(total);
1381 }
1382
1383 @Override
1384 public int describeContents() {
1385 return 0;
1386 }
1387
1388 @Override
1389 public int compareTo(AppItem another) {
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001390 return Long.compare(another.total, total);
1391 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001392
1393 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1394 @Override
1395 public AppItem createFromParcel(Parcel in) {
1396 return new AppItem(in);
1397 }
1398
1399 @Override
1400 public AppItem[] newArray(int size) {
1401 return new AppItem[size];
1402 }
1403 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001404 }
1405
Jeff Sharkey8a503642011-06-10 13:31:21 -07001406 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001407 * Adapter of applications, sorted by total usage descending.
1408 */
1409 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001410 private final UidDetailProvider mProvider;
1411 private final int mInsetSide;
1412
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001413 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001414 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001415
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001416 public DataUsageAdapter(UidDetailProvider provider, int insetSide) {
1417 mProvider = checkNotNull(provider);
1418 mInsetSide = insetSide;
1419 }
1420
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001421 /**
1422 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1423 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001424 public void bindStats(NetworkStats stats) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001425 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001426
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001427 final AppItem systemItem = new AppItem(android.os.Process.SYSTEM_UID);
1428 final SparseArray<AppItem> knownUids = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001429
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001430 NetworkStats.Entry entry = null;
1431 final int size = stats != null ? stats.size() : 0;
1432 for (int i = 0; i < size; i++) {
1433 entry = stats.getValues(i, entry);
1434
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001435 final boolean isApp = UserId.isApp(entry.uid);
1436 final int appId = isApp ? UserId.getAppId(entry.uid) : entry.uid;
1437 if (isApp || appId == UID_REMOVED || appId == UID_TETHERING) {
1438 AppItem item = knownUids.get(appId);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001439 if (item == null) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001440 item = new AppItem(appId);
1441 knownUids.put(appId, item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001442 mItems.add(item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001443 }
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001444
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001445 item.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001446 item.addUid(entry.uid);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001447 } else {
1448 systemItem.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001449 systemItem.addUid(entry.uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001450 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001451 }
1452
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001453 if (systemItem.total > 0) {
1454 mItems.add(systemItem);
1455 }
1456
Jeff Sharkey8a503642011-06-10 13:31:21 -07001457 Collections.sort(mItems);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001458 mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001459 notifyDataSetChanged();
1460 }
1461
1462 @Override
1463 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001464 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001465 }
1466
1467 @Override
1468 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001469 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001470 }
1471
1472 @Override
1473 public long getItemId(int position) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001474 return mItems.get(position).appId;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001475 }
1476
1477 @Override
1478 public View getView(int position, View convertView, ViewGroup parent) {
1479 if (convertView == null) {
1480 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey5d706792011-09-08 18:57:17 -07001481 R.layout.data_usage_item, parent, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001482
1483 if (mInsetSide > 0) {
1484 convertView.setPadding(mInsetSide, 0, mInsetSide, 0);
1485 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001486 }
1487
1488 final Context context = parent.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001489
Jeff Sharkey28130d92011-09-02 16:10:24 -07001490 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001491 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1492 android.R.id.progress);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001493
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001494 // kick off async load of app details
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001495 final AppItem item = mItems.get(position);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001496 UidDetailTask.bindView(mProvider, item, convertView);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001497
Jeff Sharkey28130d92011-09-02 16:10:24 -07001498 text1.setText(Formatter.formatFileSize(context, item.total));
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001499
1500 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1501 progress.setProgress(percentTotal);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001502
1503 return convertView;
1504 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001505 }
1506
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001507 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001508 * Empty {@link Fragment} that controls display of UID details in
1509 * {@link DataUsageSummary}.
1510 */
1511 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001512 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001513
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001514 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001515 if (!parent.isAdded()) return;
1516
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001517 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001518 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001519
1520 final AppDetailsFragment fragment = new AppDetailsFragment();
1521 fragment.setArguments(args);
1522 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001523 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1524 ft.add(fragment, TAG_APP_DETAILS);
1525 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001526 ft.setBreadCrumbTitle(label);
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001527 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001528 }
1529
1530 @Override
1531 public void onStart() {
1532 super.onStart();
1533 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001534 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001535 target.updateBody();
1536 }
1537
1538 @Override
1539 public void onStop() {
1540 super.onStop();
1541 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001542 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001543 target.updateBody();
1544 }
1545 }
1546
1547 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001548 * Dialog to request user confirmation before setting
1549 * {@link NetworkPolicy#limitBytes}.
1550 */
1551 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001552 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001553 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001554
1555 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001556 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001557
Jeff Sharkey461842a2011-09-25 18:22:48 -07001558 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001559 final CharSequence message;
1560 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001561
1562 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001563 final String currentTab = parent.mCurrentTab;
1564 if (TAB_3G.equals(currentTab)) {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001565 message = buildDialogMessage(res, R.string.data_usage_tab_3g);
1566 limitBytes = 5 * GB_IN_BYTES;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001567 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001568 message = buildDialogMessage(res, R.string.data_usage_tab_4g);
1569 limitBytes = 5 * GB_IN_BYTES;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001570 } else if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001571 message = buildDialogMessage(res, R.string.data_usage_list_mobile);
1572 limitBytes = 5 * GB_IN_BYTES;
1573 } else if (TAB_WIFI.equals(currentTab)) {
1574 message = buildDialogMessage(res, R.string.data_usage_tab_wifi);
1575 limitBytes = 5 * GB_IN_BYTES;
1576 } else {
1577 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001578 }
1579
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001580 final Bundle args = new Bundle();
1581 args.putCharSequence(EXTRA_MESSAGE, message);
1582 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1583
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001584 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1585 dialog.setArguments(args);
1586 dialog.setTargetFragment(parent, 0);
1587 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1588 }
1589
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001590 private static CharSequence buildDialogMessage(Resources res, int networkResId) {
1591 return res.getString(R.string.data_usage_limit_dialog, res.getString(networkResId));
1592 }
1593
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001594 @Override
1595 public Dialog onCreateDialog(Bundle savedInstanceState) {
1596 final Context context = getActivity();
1597
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001598 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001599 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1600
1601 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1602 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001603 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001604
1605 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1606 public void onClick(DialogInterface dialog, int which) {
1607 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1608 if (target != null) {
1609 target.setPolicyLimitBytes(limitBytes);
1610 }
1611 }
1612 });
1613
1614 return builder.create();
1615 }
1616 }
1617
1618 /**
1619 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1620 */
1621 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001622 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001623
1624 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001625 if (!parent.isAdded()) return;
1626
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001627 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001628 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001629
1630 final CycleEditorFragment dialog = new CycleEditorFragment();
1631 dialog.setArguments(args);
1632 dialog.setTargetFragment(parent, 0);
1633 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1634 }
1635
1636 @Override
1637 public Dialog onCreateDialog(Bundle savedInstanceState) {
1638 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001639 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1640 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001641
1642 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1643 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1644
1645 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1646 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1647
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001648 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1649 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001650
1651 cycleDayPicker.setMinValue(1);
1652 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001653 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001654 cycleDayPicker.setWrapSelectorWheel(true);
1655
1656 builder.setTitle(R.string.data_usage_cycle_editor_title);
1657 builder.setView(view);
1658
1659 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1660 new DialogInterface.OnClickListener() {
1661 public void onClick(DialogInterface dialog, int which) {
1662 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001663 final String cycleTimezone = new Time().timezone;
1664 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001665 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001666 }
1667 });
1668
1669 return builder.create();
1670 }
1671 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001672
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001673 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001674 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1675 */
1676 public static class WarningEditorFragment extends DialogFragment {
1677 private static final String EXTRA_TEMPLATE = "template";
1678
1679 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001680 if (!parent.isAdded()) return;
1681
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001682 final Bundle args = new Bundle();
1683 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1684
1685 final WarningEditorFragment dialog = new WarningEditorFragment();
1686 dialog.setArguments(args);
1687 dialog.setTargetFragment(parent, 0);
1688 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1689 }
1690
1691 @Override
1692 public Dialog onCreateDialog(Bundle savedInstanceState) {
1693 final Context context = getActivity();
1694 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1695 final NetworkPolicyEditor editor = target.mPolicyEditor;
1696
1697 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1698 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1699
1700 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1701 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1702
1703 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1704 final long warningBytes = editor.getPolicyWarningBytes(template);
1705 final long limitBytes = editor.getPolicyLimitBytes(template);
1706
1707 bytesPicker.setMinValue(0);
1708 if (limitBytes != LIMIT_DISABLED) {
1709 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1710 } else {
1711 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1712 }
1713 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1714 bytesPicker.setWrapSelectorWheel(false);
1715
1716 builder.setTitle(R.string.data_usage_warning_editor_title);
1717 builder.setView(view);
1718
1719 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1720 new DialogInterface.OnClickListener() {
1721 public void onClick(DialogInterface dialog, int which) {
1722 // clear focus to finish pending text edits
1723 bytesPicker.clearFocus();
1724
1725 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1726 editor.setPolicyWarningBytes(template, bytes);
1727 target.updatePolicy(false);
1728 }
1729 });
1730
1731 return builder.create();
1732 }
1733 }
1734
1735 /**
1736 * Dialog to edit {@link NetworkPolicy#limitBytes}.
1737 */
1738 public static class LimitEditorFragment extends DialogFragment {
1739 private static final String EXTRA_TEMPLATE = "template";
1740
1741 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001742 if (!parent.isAdded()) return;
1743
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001744 final Bundle args = new Bundle();
1745 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1746
1747 final LimitEditorFragment dialog = new LimitEditorFragment();
1748 dialog.setArguments(args);
1749 dialog.setTargetFragment(parent, 0);
1750 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
1751 }
1752
1753 @Override
1754 public Dialog onCreateDialog(Bundle savedInstanceState) {
1755 final Context context = getActivity();
1756 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1757 final NetworkPolicyEditor editor = target.mPolicyEditor;
1758
1759 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1760 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1761
1762 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1763 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1764
1765 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1766 final long warningBytes = editor.getPolicyWarningBytes(template);
1767 final long limitBytes = editor.getPolicyLimitBytes(template);
1768
1769 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09001770 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001771 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
1772 } else {
1773 bytesPicker.setMinValue(0);
1774 }
1775 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
1776 bytesPicker.setWrapSelectorWheel(false);
1777
1778 builder.setTitle(R.string.data_usage_limit_editor_title);
1779 builder.setView(view);
1780
1781 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1782 new DialogInterface.OnClickListener() {
1783 public void onClick(DialogInterface dialog, int which) {
1784 // clear focus to finish pending text edits
1785 bytesPicker.clearFocus();
1786
1787 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1788 editor.setPolicyLimitBytes(template, bytes);
1789 target.updatePolicy(false);
1790 }
1791 });
1792
1793 return builder.create();
1794 }
1795 }
1796 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07001797 * Dialog to request user confirmation before disabling data.
1798 */
1799 public static class ConfirmDataDisableFragment extends DialogFragment {
1800 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001801 if (!parent.isAdded()) return;
1802
Jeff Sharkey28130d92011-09-02 16:10:24 -07001803 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
1804 dialog.setTargetFragment(parent, 0);
1805 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
1806 }
1807
1808 @Override
1809 public Dialog onCreateDialog(Bundle savedInstanceState) {
1810 final Context context = getActivity();
1811
1812 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1813 builder.setMessage(R.string.data_usage_disable_mobile);
1814
1815 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1816 public void onClick(DialogInterface dialog, int which) {
1817 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1818 if (target != null) {
1819 // TODO: extend to modify policy enabled flag.
1820 target.setMobileDataEnabled(false);
1821 }
1822 }
1823 });
1824 builder.setNegativeButton(android.R.string.cancel, null);
1825
1826 return builder.create();
1827 }
1828 }
1829
1830 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001831 * Dialog to request user confirmation before setting
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001832 * {@link Settings.Secure#DATA_ROAMING}.
1833 */
1834 public static class ConfirmDataRoamingFragment extends DialogFragment {
1835 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001836 if (!parent.isAdded()) return;
1837
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001838 final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment();
1839 dialog.setTargetFragment(parent, 0);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001840 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_ROAMING);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001841 }
1842
1843 @Override
1844 public Dialog onCreateDialog(Bundle savedInstanceState) {
1845 final Context context = getActivity();
1846
1847 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1848 builder.setTitle(R.string.roaming_reenable_title);
1849 builder.setMessage(R.string.roaming_warning);
1850
1851 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1852 public void onClick(DialogInterface dialog, int which) {
1853 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1854 if (target != null) {
1855 target.setDataRoaming(true);
1856 }
1857 }
1858 });
1859 builder.setNegativeButton(android.R.string.cancel, null);
1860
1861 return builder.create();
1862 }
1863 }
1864
1865 /**
1866 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001867 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001868 */
1869 public static class ConfirmRestrictFragment extends DialogFragment {
1870 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001871 if (!parent.isAdded()) return;
1872
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001873 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1874 dialog.setTargetFragment(parent, 0);
1875 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1876 }
1877
1878 @Override
1879 public Dialog onCreateDialog(Bundle savedInstanceState) {
1880 final Context context = getActivity();
1881
1882 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001883 builder.setTitle(R.string.data_usage_restrict_background_title);
Jeff Sharkey461842a2011-09-25 18:22:48 -07001884 builder.setMessage(getString(R.string.data_usage_restrict_background));
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001885
1886 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1887 public void onClick(DialogInterface dialog, int which) {
1888 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1889 if (target != null) {
1890 target.setRestrictBackground(true);
1891 }
1892 }
1893 });
1894 builder.setNegativeButton(android.R.string.cancel, null);
1895
1896 return builder.create();
1897 }
1898 }
1899
1900 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001901 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
1902 * change has been denied, usually based on
1903 * {@link DataUsageSummary#hasLimitedNetworks()}.
1904 */
1905 public static class DeniedRestrictFragment extends DialogFragment {
1906 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001907 if (!parent.isAdded()) return;
1908
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001909 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
1910 dialog.setTargetFragment(parent, 0);
1911 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
1912 }
1913
1914 @Override
1915 public Dialog onCreateDialog(Bundle savedInstanceState) {
1916 final Context context = getActivity();
1917
1918 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1919 builder.setTitle(R.string.data_usage_app_restrict_background);
1920 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
1921 builder.setPositiveButton(android.R.string.ok, null);
1922
1923 return builder.create();
1924 }
1925 }
1926
1927 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001928 * Dialog to request user confirmation before setting
1929 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
1930 */
1931 public static class ConfirmAppRestrictFragment extends DialogFragment {
1932 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001933 if (!parent.isAdded()) return;
1934
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001935 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
1936 dialog.setTargetFragment(parent, 0);
1937 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
1938 }
1939
1940 @Override
1941 public Dialog onCreateDialog(Bundle savedInstanceState) {
1942 final Context context = getActivity();
1943
1944 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001945 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
1946 builder.setMessage(R.string.data_usage_app_restrict_dialog);
1947
1948 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1949 public void onClick(DialogInterface dialog, int which) {
1950 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1951 if (target != null) {
1952 target.setAppRestrictBackground(true);
1953 }
1954 }
1955 });
1956 builder.setNegativeButton(android.R.string.cancel, null);
1957
1958 return builder.create();
1959 }
1960 }
1961
1962 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001963 * Compute default tab that should be selected, based on
1964 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
1965 */
1966 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07001967 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
1968 if (template == null) return null;
1969
1970 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07001971 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001972 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001973 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001974 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001975 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001976 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001977 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001978 return TAB_WIFI;
1979 default:
1980 return null;
1981 }
1982 }
1983
1984 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001985 * Background task that loads {@link UidDetail}, binding to
1986 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001987 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001988 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
1989 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001990 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001991 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001992
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001993 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001994 mProvider = checkNotNull(provider);
1995 mItem = checkNotNull(item);
1996 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001997 }
1998
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001999 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002000 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002001 final UidDetailTask existing = (UidDetailTask) target.getTag();
2002 if (existing != null) {
2003 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002004 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002005
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002006 final UidDetail cachedDetail = provider.getUidDetail(item.appId, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002007 if (cachedDetail != null) {
2008 bindView(cachedDetail, target);
2009 } else {
2010 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2011 AsyncTask.THREAD_POOL_EXECUTOR));
2012 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002013 }
2014
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002015 private static void bindView(UidDetail detail, View target) {
2016 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2017 final TextView title = (TextView) target.findViewById(android.R.id.title);
2018
2019 if (detail != null) {
2020 icon.setImageDrawable(detail.icon);
2021 title.setText(detail.label);
2022 } else {
2023 icon.setImageDrawable(null);
2024 title.setText(null);
2025 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002026 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002027
2028 @Override
2029 protected void onPreExecute() {
2030 bindView(null, mTarget);
2031 }
2032
2033 @Override
2034 protected UidDetail doInBackground(Void... params) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002035 return mProvider.getUidDetail(mItem.appId, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002036 }
2037
2038 @Override
2039 protected void onPostExecute(UidDetail result) {
2040 bindView(result, mTarget);
2041 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002042 }
2043
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002044 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002045 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002046 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002047 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002048 if (TEST_RADIOS) {
2049 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2050 }
2051
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002052 final ConnectivityManager conn = ConnectivityManager.from(context);
2053 final TelephonyManager tele = TelephonyManager.from(context);
2054
2055 // require both supported network and ready SIM
2056 return conn.isNetworkSupported(TYPE_MOBILE) && tele.getSimState() == SIM_STATE_READY;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002057 }
2058
2059 /**
2060 * Test if device has a mobile 4G data radio.
2061 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002062 public static boolean hasMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002063 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2064 return false;
2065 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002066 if (TEST_RADIOS) {
2067 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2068 }
2069
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002070 final ConnectivityManager conn = ConnectivityManager.from(context);
2071 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002072
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002073 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002074 final boolean hasLte = tele.getLteOnCdmaMode() == Phone.LTE_ON_CDMA_TRUE;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002075 return hasWimax || hasLte;
2076 }
2077
2078 /**
2079 * Test if device has a Wi-Fi data radio.
2080 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002081 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002082 if (TEST_RADIOS) {
2083 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2084 }
2085
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002086 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002087 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002088 }
2089
2090 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002091 * Test if device has an ethernet network connection.
2092 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002093 public static boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002094 if (TEST_RADIOS) {
2095 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2096 }
2097
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002098 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002099 return conn.isNetworkSupported(TYPE_ETHERNET);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002100 }
2101
2102 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002103 * Inflate a {@link Preference} style layout, adding the given {@link View}
2104 * widget into {@link android.R.id#widget_frame}.
2105 */
2106 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2107 final View view = inflater.inflate(R.layout.preference, root, false);
2108 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2109 android.R.id.widget_frame);
2110 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2111 return view;
2112 }
2113
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002114 private static View inflateAppTitle(
2115 LayoutInflater inflater, ViewGroup root, CharSequence label) {
2116 final TextView view = (TextView) inflater.inflate(
2117 R.layout.data_usage_app_title, root, false);
2118 view.setText(label);
2119 return view;
2120 }
2121
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002122 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002123 * Test if any networks are currently limited.
2124 */
2125 private boolean hasLimitedNetworks() {
2126 return !buildLimitedNetworksList().isEmpty();
2127 }
2128
2129 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002130 * Build string describing currently limited networks, which defines when
2131 * background data is restricted.
2132 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002133 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002134 private CharSequence buildLimitedNetworksString() {
2135 final List<CharSequence> limited = buildLimitedNetworksList();
2136
2137 // handle case where no networks limited
2138 if (limited.isEmpty()) {
2139 limited.add(getText(R.string.data_usage_list_none));
2140 }
2141
2142 return TextUtils.join(limited);
2143 }
2144
2145 /**
2146 * Build list of currently limited networks, which defines when background
2147 * data is restricted.
2148 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002149 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002150 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002151 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002152
2153 // build combined list of all limited networks
2154 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002155
2156 final TelephonyManager tele = TelephonyManager.from(context);
2157 if (tele.getSimState() == SIM_STATE_READY) {
2158 final String subscriberId = getActiveSubscriberId(context);
2159 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2160 limited.add(getText(R.string.data_usage_list_mobile));
2161 }
2162 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2163 limited.add(getText(R.string.data_usage_tab_3g));
2164 }
2165 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2166 limited.add(getText(R.string.data_usage_tab_4g));
2167 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002168 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002169
2170 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002171 limited.add(getText(R.string.data_usage_tab_wifi));
2172 }
2173 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2174 limited.add(getText(R.string.data_usage_tab_ethernet));
2175 }
2176
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002177 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002178 }
2179
2180 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002181 * Inset both selector and divider {@link Drawable} on the given
2182 * {@link ListView} by the requested dimensions.
2183 */
2184 private static void insetListViewDrawables(ListView view, int insetSide) {
2185 final Drawable selector = view.getSelector();
2186 final Drawable divider = view.getDivider();
2187
2188 // fully unregister these drawables so callbacks can be maintained after
2189 // wrapping below.
2190 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2191 view.setSelector(stub);
2192 view.setDivider(stub);
2193
2194 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2195 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2196 }
2197
2198 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002199 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002200 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002201 */
2202 private static void setPreferenceTitle(View parent, int resId) {
2203 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2204 title.setText(resId);
2205 }
2206
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002207 /**
2208 * Set {@link android.R.id#summary} for a preference view inflated with
2209 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2210 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002211 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002212 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2213 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002214 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002215 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002216}