blob: d127204bbd233189a92ce35c33abae133b5f897c [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 Sharkey08ce99e2012-04-06 11:21:28 -070072import android.net.INetworkStatsSession;
Jeff Sharkey8a503642011-06-10 13:31:21 -070073import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070074import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070075import android.net.NetworkStats;
76import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070077import android.net.NetworkTemplate;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070078import android.net.TrafficStats;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070079import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070080import android.os.Bundle;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070081import android.os.INetworkManagementService;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070082import android.os.Parcel;
83import android.os.Parcelable;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070084import android.os.RemoteException;
85import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070086import android.os.SystemProperties;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070087import android.os.UserId;
Jeff Sharkey8a503642011-06-10 13:31:21 -070088import android.preference.Preference;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070089import android.preference.PreferenceActivity;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070090import android.provider.Settings;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070091import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070092import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070093import android.text.format.DateUtils;
94import android.text.format.Formatter;
Jeff Sharkeye5223a02012-03-09 17:11:14 -080095import android.text.format.Time;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070096import android.util.Log;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070097import android.util.SparseArray;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070098import android.util.SparseBooleanArray;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070099import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700100import android.view.Menu;
101import android.view.MenuInflater;
102import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700103import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700104import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700105import android.view.ViewGroup;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700106import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700107import android.widget.AdapterView;
108import android.widget.AdapterView.OnItemClickListener;
109import android.widget.AdapterView.OnItemSelectedListener;
110import android.widget.ArrayAdapter;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700111import android.widget.BaseAdapter;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700112import android.widget.Button;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700113import android.widget.CheckBox;
114import android.widget.CompoundButton;
115import android.widget.CompoundButton.OnCheckedChangeListener;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700116import android.widget.ImageView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700117import android.widget.LinearLayout;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700118import android.widget.ListView;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700119import android.widget.NumberPicker;
Jeff Sharkey2412b0f2011-07-17 20:31:40 -0700120import android.widget.ProgressBar;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700121import android.widget.Spinner;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700122import android.widget.Switch;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700123import android.widget.TabHost;
124import android.widget.TabHost.OnTabChangeListener;
125import android.widget.TabHost.TabContentFactory;
126import android.widget.TabHost.TabSpec;
127import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700128import android.widget.TextView;
129
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700130import com.android.internal.telephony.Phone;
Jeff Sharkey5d706792011-09-08 18:57:17 -0700131import com.android.settings.drawable.InsetBoundsDrawable;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700132import com.android.settings.net.ChartData;
133import com.android.settings.net.ChartDataLoader;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700134import com.android.settings.net.DataUsageMeteredSettings;
Jeff Sharkeya662e492011-06-18 21:57:06 -0700135import com.android.settings.net.NetworkPolicyEditor;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700136import com.android.settings.net.SummaryForAllUidLoader;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700137import com.android.settings.net.UidDetail;
138import com.android.settings.net.UidDetailProvider;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700139import com.android.settings.widget.ChartDataUsageView;
140import com.android.settings.widget.ChartDataUsageView.DataUsageChartListener;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700141import com.android.settings.widget.PieChartView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700142import com.google.android.collect.Lists;
143
144import java.util.ArrayList;
145import java.util.Collections;
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700146import java.util.List;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700147import java.util.Locale;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700148
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700149import libcore.util.Objects;
150
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700151/**
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700152 * Panel showing data usage history across various networks, including options
153 * to inspect based on usage cycle and control through {@link NetworkPolicy}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700154 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700155public class DataUsageSummary extends Fragment {
156 private static final String TAG = "DataUsage";
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800157 private static final boolean LOGD = false;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700158
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700159 // TODO: remove this testing code
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700160 private static final boolean TEST_ANIM = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700161 private static final boolean TEST_RADIOS = false;
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800162
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700163 private static final String TEST_RADIOS_PROP = "test.radios";
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800164 private static final String TEST_SUBSCRIBER_PROP = "test.subscriberid";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700165
Jeff Sharkey8a503642011-06-10 13:31:21 -0700166 private static final String TAB_3G = "3g";
167 private static final String TAB_4G = "4g";
168 private static final String TAB_MOBILE = "mobile";
169 private static final String TAB_WIFI = "wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700170 private static final String TAB_ETHERNET = "ethernet";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700171
Jeff Sharkey28130d92011-09-02 16:10:24 -0700172 private static final String TAG_CONFIRM_DATA_DISABLE = "confirmDataDisable";
173 private static final String TAG_CONFIRM_DATA_ROAMING = "confirmDataRoaming";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700174 private static final String TAG_CONFIRM_LIMIT = "confirmLimit";
175 private static final String TAG_CYCLE_EDITOR = "cycleEditor";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700176 private static final String TAG_WARNING_EDITOR = "warningEditor";
177 private static final String TAG_LIMIT_EDITOR = "limitEditor";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700178 private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700179 private static final String TAG_DENIED_RESTRICT = "deniedRestrict";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700180 private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700181 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700182
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700183 private static final int LOADER_CHART_DATA = 2;
184 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700185
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700186 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700187 private INetworkStatsService mStatsService;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700188 private NetworkPolicyManager mPolicyManager;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700189 private ConnectivityManager mConnService;
190
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700191 private INetworkStatsSession mStatsSession;
192
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700193 private static final String PREF_FILE = "data_usage";
194 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700195 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700196
197 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700198
Jeff Sharkey8a503642011-06-10 13:31:21 -0700199 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700200 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700201 private TabWidget mTabWidget;
202 private ListView mListView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700203 private DataUsageAdapter mAdapter;
204
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700205 /** Distance to inset content from sides, when needed. */
206 private int mInsetSide = 0;
207
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700208 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700209
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700210 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700211 private LinearLayout mNetworkSwitches;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700212 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700213 private View mDataEnabledView;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700214 private CheckBox mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700215 private View mDisableAtLimitView;
216
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700217 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700218 private Spinner mCycleSpinner;
219 private CycleAdapter mCycleAdapter;
220
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700221 private ChartDataUsageView mChart;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700222 private TextView mUsageSummary;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700223 private TextView mEmpty;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700224
225 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700226 private ImageView mAppIcon;
227 private ViewGroup mAppTitles;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700228 private PieChartView mAppPieChart;
229 private TextView mAppForeground;
230 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700231 private Button mAppSettings;
232
233 private LinearLayout mAppSwitches;
234 private CheckBox mAppRestrict;
235 private View mAppRestrictView;
236
Jeff Sharkey8a503642011-06-10 13:31:21 -0700237 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700238 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700239
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700240 private NetworkTemplate mTemplate;
241 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700242
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700243 private AppItem mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700244
245 private Intent mAppSettingsIntent;
246
Jeff Sharkeya662e492011-06-18 21:57:06 -0700247 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700248
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700249 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700250 private String mIntentTab = null;
251
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700252 private MenuItem mMenuDataRoaming;
253 private MenuItem mMenuRestrictBackground;
254
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700255 /** Flag used to ignore listeners during binding. */
256 private boolean mBinding;
257
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700258 private UidDetailProvider mUidDetailProvider;
259
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700260 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700261 public void onCreate(Bundle savedInstanceState) {
262 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700263 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700264
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700265 mNetworkService = INetworkManagementService.Stub.asInterface(
266 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700267 mStatsService = INetworkStatsService.Stub.asInterface(
268 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700269 mPolicyManager = NetworkPolicyManager.from(context);
270 mConnService = ConnectivityManager.from(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700271
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700272 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700273
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700274 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700275 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700276
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700277 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700278 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700279
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700280 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700281 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700282
Jeff Sharkey8a503642011-06-10 13:31:21 -0700283 @Override
284 public View onCreateView(LayoutInflater inflater, ViewGroup container,
285 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700286
Jeff Sharkey8a503642011-06-10 13:31:21 -0700287 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700288 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
289
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700290 mUidDetailProvider = new UidDetailProvider(context);
291
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700292 try {
293 mStatsSession = mStatsService.openSession();
294 } catch (RemoteException e) {
295 throw new RuntimeException(e);
296 }
297
Jeff Sharkey8a503642011-06-10 13:31:21 -0700298 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700299 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700300 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
301 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700302
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700303 // decide if we need to manually inset our content, or if we should rely
304 // on parent container for inset.
305 final boolean shouldInset = mListView.getScrollBarStyle()
306 == View.SCROLLBARS_OUTSIDE_OVERLAY;
307 if (shouldInset) {
308 mInsetSide = view.getResources().getDimensionPixelOffset(
309 com.android.internal.R.dimen.preference_fragment_padding_side);
310 } else {
311 mInsetSide = 0;
312 }
313
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700314 // adjust padding around tabwidget as needed
Jeff Sharkey5d706792011-09-08 18:57:17 -0700315 prepareCustomPreferencesList(container, view, mListView, true);
316
Jeff Sharkey8a503642011-06-10 13:31:21 -0700317 mTabHost.setup();
318 mTabHost.setOnTabChangedListener(mTabListener);
319
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700320 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700321 mHeader.setClickable(true);
322
323 mListView.addHeaderView(mHeader, null, true);
324 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700325
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700326 if (mInsetSide > 0) {
327 // inset selector and divider drawables
328 insetListViewDrawables(mListView, mInsetSide);
329 mHeader.setPadding(mInsetSide, 0, mInsetSide, 0);
330 }
331
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700332 {
333 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700334 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
335 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700336 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700337
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700338 mDataEnabled = new Switch(inflater.getContext());
339 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
340 mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener);
341 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700342
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700343 mDisableAtLimit = new CheckBox(inflater.getContext());
344 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700345 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700346 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700347 mDisableAtLimitView.setClickable(true);
348 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700349 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
350 mNetworkSwitches.addView(mDisableAtLimitView);
351 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700352
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700353 // bind cycle dropdown
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700354 mCycleView = mHeader.findViewById(R.id.cycles);
355 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700356 mCycleAdapter = new CycleAdapter(context);
357 mCycleSpinner.setAdapter(mCycleAdapter);
358 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
359
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700360 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700361 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800362 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700363
364 {
365 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700366 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700367 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
368 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700369 mAppPieChart = (PieChartView) mAppDetail.findViewById(R.id.app_pie_chart);
370 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
371 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700372 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700373
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700374 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700375 mAppSettings.setOnClickListener(mAppSettingsListener);
376
377 mAppRestrict = new CheckBox(inflater.getContext());
378 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700379 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700380 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700381 mAppRestrictView.setClickable(true);
382 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700383 mAppRestrictView.setOnClickListener(mAppRestrictListener);
384 mAppSwitches.addView(mAppRestrictView);
385 }
386
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700387 mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700388 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700389
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700390 // only assign layout transitions once first layout is finished
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700391 mListView.getViewTreeObserver().addOnGlobalLayoutListener(mFirstLayoutListener);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700392
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700393 mAdapter = new DataUsageAdapter(mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700394 mListView.setOnItemClickListener(mListListener);
395 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700396
397 return view;
398 }
399
400 @Override
401 public void onResume() {
402 super.onResume();
403
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700404 // pick default tab based on incoming intent
405 final Intent intent = getActivity().getIntent();
406 mIntentTab = computeTabFromIntent(intent);
407
Jeff Sharkey8a503642011-06-10 13:31:21 -0700408 // this kicks off chain reaction which creates tabs, binds the body to
409 // selected network, and binds chart, cycles and detail list.
410 updateTabs();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700411
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700412 // kick off background task to update stats
413 new AsyncTask<Void, Void, Void>() {
414 @Override
415 protected Void doInBackground(Void... params) {
416 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700417 // wait a few seconds before kicking off
418 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700419 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700420 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700421 } catch (RemoteException e) {
422 }
423 return null;
424 }
425
426 @Override
427 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700428 if (isAdded()) {
429 updateBody();
430 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700431 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700432 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700433 }
434
Jeff Sharkey8a503642011-06-10 13:31:21 -0700435 @Override
436 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
437 inflater.inflate(R.menu.data_usage, menu);
438 }
439
440 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700441 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700442 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700443 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700444
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700445 mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700446 mMenuDataRoaming.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700447 mMenuDataRoaming.setChecked(getDataRoaming());
448
449 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700450 mMenuRestrictBackground.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
451 mMenuRestrictBackground.setChecked(mPolicyManager.getRestrictBackground());
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700452
453 final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700454 split4g.setVisible(hasMobile4gRadio(context) && !appDetailMode);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700455 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700456
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700457 final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700458 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700459 showWifi.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700460 showWifi.setChecked(mShowWifi);
461 } else {
462 showWifi.setVisible(false);
463 mShowWifi = true;
464 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700465
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700466 final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700467 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700468 showEthernet.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700469 showEthernet.setChecked(mShowEthernet);
470 } else {
471 showEthernet.setVisible(false);
472 mShowEthernet = true;
473 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700474
475 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
476 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
477 metered.setVisible(!appDetailMode);
478 } else {
479 metered.setVisible(false);
480 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700481 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700482
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700483 @Override
484 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700485 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700486 case R.id.data_usage_menu_roaming: {
487 final boolean dataRoaming = !item.isChecked();
488 if (dataRoaming) {
489 ConfirmDataRoamingFragment.show(this);
490 } else {
491 // no confirmation to disable roaming
492 setDataRoaming(false);
493 }
494 return true;
495 }
496 case R.id.data_usage_menu_restrict_background: {
497 final boolean restrictBackground = !item.isChecked();
498 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800499 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700500 } else {
501 // no confirmation to drop restriction
502 setRestrictBackground(false);
503 }
504 return true;
505 }
506 case R.id.data_usage_menu_split_4g: {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700507 final boolean mobileSplit = !item.isChecked();
Jeff Sharkeya662e492011-06-18 21:57:06 -0700508 setMobilePolicySplit(mobileSplit);
509 item.setChecked(isMobilePolicySplit());
Jeff Sharkey8a503642011-06-10 13:31:21 -0700510 updateTabs();
511 return true;
512 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700513 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700514 mShowWifi = !item.isChecked();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700515 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700516 item.setChecked(mShowWifi);
517 updateTabs();
518 return true;
519 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700520 case R.id.data_usage_menu_show_ethernet: {
521 mShowEthernet = !item.isChecked();
522 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
523 item.setChecked(mShowEthernet);
524 updateTabs();
525 return true;
526 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700527 case R.id.data_usage_menu_metered: {
528 final PreferenceActivity activity = (PreferenceActivity) getActivity();
529 activity.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
530 R.string.data_usage_metered_title, null, this, 0);
531 return true;
532 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700533 }
534 return false;
535 }
536
Jeff Sharkey94a90952011-06-13 22:31:09 -0700537 @Override
538 public void onDestroyView() {
539 super.onDestroyView();
540
541 mDataEnabledView = null;
542 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700543
544 mUidDetailProvider.clearCache();
545 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700546
547 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700548 }
549
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800550 @Override
551 public void onDestroy() {
552 if (this.isRemoving()) {
553 getFragmentManager()
554 .popBackStack(TAG_APP_DETAILS, FragmentManager.POP_BACK_STACK_INCLUSIVE);
555 }
556 super.onDestroy();
557 }
558
Jeff Sharkey8a503642011-06-10 13:31:21 -0700559 /**
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700560 * Listener to setup {@link LayoutTransition} after first layout pass.
561 */
562 private OnGlobalLayoutListener mFirstLayoutListener = new OnGlobalLayoutListener() {
563 /** {@inheritDoc} */
564 public void onGlobalLayout() {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700565 mListView.getViewTreeObserver().removeGlobalOnLayoutListener(mFirstLayoutListener);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700566
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700567 mTabsContainer.setLayoutTransition(buildLayoutTransition());
568 mHeader.setLayoutTransition(buildLayoutTransition());
569 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700570
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700571 final LayoutTransition chartTransition = buildLayoutTransition();
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700572 chartTransition.setStartDelay(LayoutTransition.APPEARING, 0);
573 chartTransition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700574 chartTransition.setAnimator(LayoutTransition.APPEARING, null);
Jeff Sharkey28130d92011-09-02 16:10:24 -0700575 chartTransition.setAnimator(LayoutTransition.DISAPPEARING, null);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700576 mChart.setLayoutTransition(chartTransition);
577 }
578 };
579
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700580 private static LayoutTransition buildLayoutTransition() {
581 final LayoutTransition transition = new LayoutTransition();
582 if (TEST_ANIM) {
583 transition.setDuration(1500);
584 }
585 transition.setAnimateParentHierarchy(false);
586 return transition;
587 }
588
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700589 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700590 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700591 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
592 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700593 */
594 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700595 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700596 mTabHost.clearAllTabs();
597
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700598 final boolean mobileSplit = isMobilePolicySplit();
599 if (mobileSplit && hasMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700600 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
601 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700602 } else if (hasReadyMobileRadio(context)) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700603 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700604 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700605 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700606 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
607 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700608 if (mShowEthernet && hasEthernet(context)) {
609 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
610 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700611
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700612 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
613 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
614 if (mIntentTab != null) {
615 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700616 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700617 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700618 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700619 mTabHost.setCurrentTabByTag(mIntentTab);
620 }
621 mIntentTab = null;
622 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700623 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700624 }
625 }
626
Jeff Sharkey8a503642011-06-10 13:31:21 -0700627 /**
628 * Factory that provide empty {@link View} to make {@link TabHost} happy.
629 */
630 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
631 /** {@inheritDoc} */
632 public View createTabContent(String tag) {
633 return new View(mTabHost.getContext());
634 }
635 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700636
Jeff Sharkey8a503642011-06-10 13:31:21 -0700637 /**
638 * Build {@link TabSpec} with thin indicator, and empty content.
639 */
640 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700641 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
642 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700643 }
644
645 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
646 /** {@inheritDoc} */
647 public void onTabChanged(String tabId) {
648 // user changed tab; update body
649 updateBody();
650 }
651 };
652
653 /**
654 * Update body content based on current tab. Loads
655 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
656 * binds them to visible controls.
657 */
658 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700659 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700660 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700661
Jeff Sharkeya662e492011-06-18 21:57:06 -0700662 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700663 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700664
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700665 if (currentTab == null) {
666 Log.w(TAG, "no tab selected; hiding body");
667 mListView.setVisibility(View.GONE);
668 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700669 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700670 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700671 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700672
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700673 final boolean tabChanged = !currentTab.equals(mCurrentTab);
674 mCurrentTab = currentTab;
675
Jeff Sharkey8a503642011-06-10 13:31:21 -0700676 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
677
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700678 mDataEnabledView.setVisibility(View.VISIBLE);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700679
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700680 // TODO: remove mobile tabs when SIM isn't ready
681 final TelephonyManager tele = TelephonyManager.from(context);
682
Jeff Sharkey8a503642011-06-10 13:31:21 -0700683 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700684 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
685 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700686 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700687
688 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700689 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
690 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
691 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700692 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700693
694 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700695 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
696 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
697 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700698 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700699
700 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700701 // wifi doesn't have any controls
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700702 mDataEnabledView.setVisibility(View.GONE);
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700703 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700704 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700705
706 } else if (TAB_ETHERNET.equals(currentTab)) {
707 // ethernet doesn't have any controls
708 mDataEnabledView.setVisibility(View.GONE);
709 mDisableAtLimitView.setVisibility(View.GONE);
710 mTemplate = buildTemplateEthernet();
711
712 } else {
713 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700714 }
715
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700716 // kick off loader for network history
717 // TODO: consider chaining two loaders together instead of reloading
718 // network history when showing app detail.
719 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700720 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700721
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700722 // detail mode can change visible menus, invalidate
723 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700724
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700725 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700726 }
727
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700728 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700729 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700730 }
731
732 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700733 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700734 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700735 */
736 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700737 final Context context = getActivity();
738 final PackageManager pm = context.getPackageManager();
739 final LayoutInflater inflater = getActivity().getLayoutInflater();
740
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700741 if (isAppDetailMode()) {
742 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700743 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700744 } else {
745 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700746 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700747
748 // hide detail stats when not in detail mode
749 mChart.bindDetailNetworkStats(null);
750 return;
751 }
752
753 // remove warning/limit sweeps while in detail mode
754 mChart.bindNetworkPolicy(null);
755
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700756 // show icon and all labels appearing under this app
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700757 final int appId = mCurrentApp.appId;
758 final UidDetail detail = mUidDetailProvider.getUidDetail(appId, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700759 mAppIcon.setImageDrawable(detail.icon);
760
761 mAppTitles.removeAllViews();
762 if (detail.detailLabels != null) {
763 for (CharSequence label : detail.detailLabels) {
764 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label));
765 }
766 } else {
767 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label));
768 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700769
770 // enable settings button when package provides it
771 // TODO: target torwards entire UID instead of just first package
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700772 final String[] packageNames = pm.getPackagesForUid(appId);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700773 if (packageNames != null && packageNames.length > 0) {
774 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
775 mAppSettingsIntent.setPackage(packageNames[0]);
776 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
777
778 final boolean matchFound = pm.resolveActivity(mAppSettingsIntent, 0) != null;
779 mAppSettings.setEnabled(matchFound);
780
781 } else {
782 mAppSettingsIntent = null;
783 mAppSettings.setEnabled(false);
784 }
785
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700786 updateDetailData();
787
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700788 if (UserId.isApp(appId) && !mPolicyManager.getRestrictBackground()
789 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700790 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800791 setPreferenceSummary(mAppRestrictView,
792 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700793
794 mAppRestrictView.setVisibility(View.VISIBLE);
795 mAppRestrict.setChecked(getAppRestrictBackground());
796
797 } else {
798 mAppRestrictView.setVisibility(View.GONE);
799 }
800 }
801
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700802 private void setPolicyWarningBytes(long warningBytes) {
803 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700804 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700805 updatePolicy(false);
806 }
807
808 private void setPolicyLimitBytes(long limitBytes) {
809 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700810 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700811 updatePolicy(false);
812 }
813
Jeff Sharkey28130d92011-09-02 16:10:24 -0700814 /**
815 * Local cache of value, used to work around delay when
816 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
817 */
818 private Boolean mMobileDataEnabled;
819
820 private boolean isMobileDataEnabled() {
821 if (mMobileDataEnabled != null) {
822 // TODO: deprecate and remove this once enabled flag is on policy
823 return mMobileDataEnabled;
824 } else {
825 return mConnService.getMobileDataEnabled();
826 }
827 }
828
829 private void setMobileDataEnabled(boolean enabled) {
830 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
831 mConnService.setMobileDataEnabled(enabled);
832 mMobileDataEnabled = enabled;
833 updatePolicy(false);
834 }
835
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700836 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
837 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked();
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700838 }
839
840 private boolean isBandwidthControlEnabled() {
841 try {
842 return mNetworkService.isBandwidthControlEnabled();
843 } catch (RemoteException e) {
844 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
845 return false;
846 }
847 }
848
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700849 private boolean getDataRoaming() {
850 final ContentResolver resolver = getActivity().getContentResolver();
851 return Settings.Secure.getInt(resolver, Settings.Secure.DATA_ROAMING, 0) != 0;
852 }
853
854 private void setDataRoaming(boolean enabled) {
855 // TODO: teach telephony DataConnectionTracker to watch and apply
856 // updates when changed.
857 final ContentResolver resolver = getActivity().getContentResolver();
858 Settings.Secure.putInt(resolver, Settings.Secure.DATA_ROAMING, enabled ? 1 : 0);
859 mMenuDataRoaming.setChecked(enabled);
860 }
861
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700862 public void setRestrictBackground(boolean restrictBackground) {
863 mPolicyManager.setRestrictBackground(restrictBackground);
864 mMenuRestrictBackground.setChecked(restrictBackground);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700865 }
866
867 private boolean getAppRestrictBackground() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700868 final int appId = mCurrentApp.appId;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700869 final int uidPolicy = mPolicyManager.getAppPolicy(appId);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700870 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
871 }
872
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700873 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700874 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700875 final int appId = mCurrentApp.appId;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700876 mPolicyManager.setAppPolicy(appId,
877 restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700878 mAppRestrict.setChecked(restrictBackground);
879 }
880
Jeff Sharkey8a503642011-06-10 13:31:21 -0700881 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700882 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
883 * current {@link #mTemplate}.
884 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700885 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700886 if (isAppDetailMode()) {
887 mNetworkSwitches.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700888 } else {
889 mNetworkSwitches.setVisibility(View.VISIBLE);
890 }
891
Jeff Sharkey28130d92011-09-02 16:10:24 -0700892 // TODO: move enabled state directly into policy
893 if (TAB_MOBILE.equals(mCurrentTab)) {
894 mBinding = true;
895 mDataEnabled.setChecked(isMobileDataEnabled());
896 mBinding = false;
897 }
898
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700899 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
900 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700901 mDisableAtLimitView.setVisibility(View.VISIBLE);
902 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700903 if (!isAppDetailMode()) {
904 mChart.bindNetworkPolicy(policy);
905 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700906
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700907 } else {
908 // controls are disabled; don't bind warning/limit sweeps
909 mDisableAtLimitView.setVisibility(View.GONE);
910 mChart.bindNetworkPolicy(null);
911 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700912
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700913 if (refreshCycle) {
914 // generate cycle list based on policy and available history
915 updateCycleList(policy);
916 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700917 }
918
919 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700920 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
921 * and available {@link NetworkStatsHistory} data. Always selects the newest
922 * item, updating the inspection range on {@link #mChart}.
923 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700924 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700925 // stash away currently selected cycle to try restoring below
926 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700927 mCycleAdapter.clear();
928
929 final Context context = mCycleSpinner.getContext();
930
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700931 long historyStart = Long.MAX_VALUE;
932 long historyEnd = Long.MIN_VALUE;
933 if (mChartData != null) {
934 historyStart = mChartData.network.getStart();
935 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700936 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700937
Jeff Sharkey461842a2011-09-25 18:22:48 -0700938 final long now = System.currentTimeMillis();
939 if (historyStart == Long.MAX_VALUE) historyStart = now;
940 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700941
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700942 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700943 if (policy != null) {
944 // find the next cycle boundary
945 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700946
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700947 // walk backwards, generating all valid cycle ranges
948 while (cycleEnd > historyStart) {
949 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
950 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
951 + historyStart);
952 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
953 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700954 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700955 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700956
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700957 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700958 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700959 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700960
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700961 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700962 // no policy defined cycles; show entry for each four-week period
963 long cycleEnd = historyEnd;
964 while (cycleEnd > historyStart) {
965 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
966 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
967 cycleEnd = cycleStart;
968 }
969
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700970 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700971 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700972
973 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700974 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700975 final int position = mCycleAdapter.findNearestPosition(previousItem);
976 mCycleSpinner.setSelection(position);
977
978 // only force-update cycle when changed; skipping preserves any
979 // user-defined inspection region.
980 final CycleItem selectedItem = mCycleAdapter.getItem(position);
981 if (!Objects.equal(selectedItem, previousItem)) {
982 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
983 } else {
984 // but still kick off loader for detailed list
985 updateDetailData();
986 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700987 } else {
988 updateDetailData();
989 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700990 }
991
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700992 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700993 /** {@inheritDoc} */
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700994 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
995 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700996
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700997 final boolean dataEnabled = isChecked;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700998 final String currentTab = mCurrentTab;
999 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001000 if (dataEnabled) {
1001 setMobileDataEnabled(true);
1002 } else {
1003 // disabling data; show confirmation dialog which eventually
1004 // calls setMobileDataEnabled() once user confirms.
1005 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1006 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001007 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001008
Jeff Sharkey28130d92011-09-02 16:10:24 -07001009 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001010 }
1011 };
1012
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001013 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001014 /** {@inheritDoc} */
1015 public void onClick(View v) {
1016 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001017 if (disableAtLimit) {
1018 // enabling limit; show confirmation dialog which eventually
1019 // calls setPolicyLimitBytes() once user confirms.
1020 ConfirmLimitFragment.show(DataUsageSummary.this);
1021 } else {
1022 setPolicyLimitBytes(LIMIT_DISABLED);
1023 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001024 }
1025 };
1026
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001027 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
1028 /** {@inheritDoc} */
1029 public void onClick(View v) {
1030 final boolean restrictBackground = !mAppRestrict.isChecked();
1031
1032 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001033 // enabling restriction; show confirmation dialog which
1034 // eventually calls setRestrictBackground() once user
1035 // confirms.
1036 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001037 } else {
1038 setAppRestrictBackground(false);
1039 }
1040 }
1041 };
1042
1043 private OnClickListener mAppSettingsListener = new OnClickListener() {
1044 /** {@inheritDoc} */
1045 public void onClick(View v) {
1046 // TODO: target torwards entire UID instead of just first package
1047 startActivity(mAppSettingsIntent);
1048 }
1049 };
1050
Jeff Sharkey8a503642011-06-10 13:31:21 -07001051 private OnItemClickListener mListListener = new OnItemClickListener() {
1052 /** {@inheritDoc} */
1053 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001054 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001055 final AppItem app = (AppItem) parent.getItemAtPosition(position);
1056 final UidDetail detail = mUidDetailProvider.getUidDetail(app.appId, true);
1057 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001058 }
1059 };
1060
1061 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
1062 /** {@inheritDoc} */
1063 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1064 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1065 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001066 // show cycle editor; will eventually call setPolicyCycleDay()
1067 // when user finishes editing.
1068 CycleEditorFragment.show(DataUsageSummary.this);
1069
1070 // reset spinner to something other than "change cycle..."
1071 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001072
1073 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001074 if (LOGD) {
1075 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1076 + cycle.end + "]");
1077 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001078
1079 // update chart to show selected cycle, and update detail data
1080 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001081 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001082
1083 updateDetailData();
1084 }
1085 }
1086
1087 /** {@inheritDoc} */
1088 public void onNothingSelected(AdapterView<?> parent) {
1089 // ignored
1090 }
1091 };
1092
1093 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001094 * Update details based on {@link #mChart} inspection range depending on
1095 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1096 * of applications data usage, and when {@link #isAppDetailMode()} update
1097 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001098 */
1099 private void updateDetailData() {
1100 if (LOGD) Log.d(TAG, "updateDetailData()");
1101
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001102 final long start = mChart.getInspectStart();
1103 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001104 final long now = System.currentTimeMillis();
1105
1106 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001107
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001108 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001109 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001110 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001111 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001112 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001113 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001114 final long foregroundBytes = entry.rxBytes + entry.txBytes;
1115
1116 mAppPieChart.setOriginAngle(175);
1117
1118 mAppPieChart.removeAllSlices();
1119 mAppPieChart.addSlice(foregroundBytes, Color.parseColor("#d88d3a"));
1120 mAppPieChart.addSlice(defaultBytes, Color.parseColor("#666666"));
1121
1122 mAppPieChart.generatePath();
1123
1124 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1125 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1126
1127 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001128 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001129
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001130 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001131
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001132 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001133 if (mChartData != null) {
1134 entry = mChartData.network.getValues(start, end, now, null);
1135 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001136
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001137 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001138 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001139 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001140 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001141
1142 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1143 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001144 final String rangePhrase = formatDateRange(context, start, end);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001145
1146 mUsageSummary.setText(
1147 getString(R.string.data_usage_total_during_range, totalPhrase, rangePhrase));
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001148 }
1149
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001150 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1151 ChartData>() {
1152 /** {@inheritDoc} */
1153 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001154 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001155 }
1156
1157 /** {@inheritDoc} */
1158 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1159 mChartData = data;
1160 mChart.bindNetworkStats(mChartData.network);
1161 mChart.bindDetailNetworkStats(mChartData.detail);
1162
1163 // calcuate policy cycles based on available data
1164 updatePolicy(true);
1165 updateAppDetail();
1166
1167 // force scroll to top of body when showing detail
1168 if (mChartData.detail != null) {
1169 mListView.smoothScrollToPosition(0);
1170 }
1171 }
1172
1173 /** {@inheritDoc} */
1174 public void onLoaderReset(Loader<ChartData> loader) {
1175 mChartData = null;
1176 mChart.bindNetworkStats(null);
1177 mChart.bindDetailNetworkStats(null);
1178 }
1179 };
1180
1181 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001182 NetworkStats>() {
1183 /** {@inheritDoc} */
1184 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001185 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001186 }
1187
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001188 /** {@inheritDoc} */
1189 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
1190 mAdapter.bindStats(data);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001191 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001192 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001193
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001194 /** {@inheritDoc} */
1195 public void onLoaderReset(Loader<NetworkStats> loader) {
1196 mAdapter.bindStats(null);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001197 updateEmptyVisible();
1198 }
1199
1200 private void updateEmptyVisible() {
1201 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1202 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001203 }
1204 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001205
Jeff Sharkeya662e492011-06-18 21:57:06 -07001206 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001207 final Context context = getActivity();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001208 if (hasReadyMobileRadio(context)) {
1209 final TelephonyManager tele = TelephonyManager.from(context);
1210 return mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001211 } else {
1212 return false;
1213 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001214 }
1215
1216 private void setMobilePolicySplit(boolean split) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001217 final Context context = getActivity();
1218 if (hasReadyMobileRadio(context)) {
1219 final TelephonyManager tele = TelephonyManager.from(context);
1220 mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split);
1221 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001222 }
1223
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001224 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001225 final TelephonyManager tele = TelephonyManager.from(context);
1226 final String actualSubscriberId = tele.getSubscriberId();
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -08001227 return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001228 }
1229
Jeff Sharkey8a503642011-06-10 13:31:21 -07001230 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
1231 /** {@inheritDoc} */
1232 public void onInspectRangeChanged() {
1233 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
1234 updateDetailData();
1235 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001236
Jeff Sharkey8a503642011-06-10 13:31:21 -07001237 /** {@inheritDoc} */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001238 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001239 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001240 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001241
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001242 /** {@inheritDoc} */
1243 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001244 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001245 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001246
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001247 /** {@inheritDoc} */
1248 public void requestWarningEdit() {
1249 WarningEditorFragment.show(DataUsageSummary.this);
1250 }
1251
1252 /** {@inheritDoc} */
1253 public void requestLimitEdit() {
1254 LimitEditorFragment.show(DataUsageSummary.this);
1255 }
1256 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001257
1258 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001259 * List item that reflects a specific data usage cycle.
1260 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001261 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001262 public CharSequence label;
1263 public long start;
1264 public long end;
1265
Jeff Sharkey8a503642011-06-10 13:31:21 -07001266 CycleItem(CharSequence label) {
1267 this.label = label;
1268 }
1269
1270 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001271 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001272 this.start = start;
1273 this.end = end;
1274 }
1275
Jeff Sharkey8a503642011-06-10 13:31:21 -07001276 @Override
1277 public String toString() {
1278 return label.toString();
1279 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001280
1281 @Override
1282 public boolean equals(Object o) {
1283 if (o instanceof CycleItem) {
1284 final CycleItem another = (CycleItem) o;
1285 return start == another.start && end == another.end;
1286 }
1287 return false;
1288 }
1289
1290 /** {@inheritDoc} */
1291 public int compareTo(CycleItem another) {
1292 return Long.compare(start, another.start);
1293 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001294 }
1295
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001296 private static final StringBuilder sBuilder = new StringBuilder(50);
1297 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1298 sBuilder, Locale.getDefault());
1299
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001300 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001301 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001302
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001303 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001304 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001305 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1306 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001307 }
1308 }
1309
Jeff Sharkey8a503642011-06-10 13:31:21 -07001310 /**
1311 * Special-case data usage cycle that triggers dialog to change
1312 * {@link NetworkPolicy#cycleDay}.
1313 */
1314 public static class CycleChangeItem extends CycleItem {
1315 public CycleChangeItem(Context context) {
1316 super(context.getString(R.string.data_usage_change_cycle));
1317 }
1318 }
1319
1320 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001321 private boolean mChangePossible = false;
1322 private boolean mChangeVisible = false;
1323
1324 private final CycleChangeItem mChangeItem;
1325
Jeff Sharkey8a503642011-06-10 13:31:21 -07001326 public CycleAdapter(Context context) {
1327 super(context, android.R.layout.simple_spinner_item);
1328 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001329 mChangeItem = new CycleChangeItem(context);
1330 }
1331
1332 public void setChangePossible(boolean possible) {
1333 mChangePossible = possible;
1334 updateChange();
1335 }
1336
1337 public void setChangeVisible(boolean visible) {
1338 mChangeVisible = visible;
1339 updateChange();
1340 }
1341
1342 private void updateChange() {
1343 remove(mChangeItem);
1344 if (mChangePossible && mChangeVisible) {
1345 add(mChangeItem);
1346 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001347 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001348
1349 /**
1350 * Find position of {@link CycleItem} in this adapter which is nearest
1351 * the given {@link CycleItem}.
1352 */
1353 public int findNearestPosition(CycleItem target) {
1354 if (target != null) {
1355 final int count = getCount();
1356 for (int i = count - 1; i >= 0; i--) {
1357 final CycleItem item = getItem(i);
1358 if (item instanceof CycleChangeItem) {
1359 continue;
1360 } else if (item.compareTo(target) >= 0) {
1361 return i;
1362 }
1363 }
1364 }
1365 return 0;
1366 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001367 }
1368
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001369 public static class AppItem implements Comparable<AppItem>, Parcelable {
1370 public final int appId;
1371 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001372 public long total;
1373
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001374 public AppItem(int appId) {
1375 this.appId = appId;
1376 }
1377
1378 public AppItem(Parcel parcel) {
1379 appId = parcel.readInt();
1380 uids = parcel.readSparseBooleanArray();
1381 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001382 }
1383
1384 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001385 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001386 }
1387
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001388 @Override
1389 public void writeToParcel(Parcel dest, int flags) {
1390 dest.writeInt(appId);
1391 dest.writeSparseBooleanArray(uids);
1392 dest.writeLong(total);
1393 }
1394
1395 @Override
1396 public int describeContents() {
1397 return 0;
1398 }
1399
1400 @Override
1401 public int compareTo(AppItem another) {
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001402 return Long.compare(another.total, total);
1403 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001404
1405 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1406 @Override
1407 public AppItem createFromParcel(Parcel in) {
1408 return new AppItem(in);
1409 }
1410
1411 @Override
1412 public AppItem[] newArray(int size) {
1413 return new AppItem[size];
1414 }
1415 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001416 }
1417
Jeff Sharkey8a503642011-06-10 13:31:21 -07001418 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001419 * Adapter of applications, sorted by total usage descending.
1420 */
1421 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001422 private final UidDetailProvider mProvider;
1423 private final int mInsetSide;
1424
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001425 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001426 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001427
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001428 public DataUsageAdapter(UidDetailProvider provider, int insetSide) {
1429 mProvider = checkNotNull(provider);
1430 mInsetSide = insetSide;
1431 }
1432
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001433 /**
1434 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1435 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001436 public void bindStats(NetworkStats stats) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001437 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001438
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001439 final AppItem systemItem = new AppItem(android.os.Process.SYSTEM_UID);
1440 final SparseArray<AppItem> knownUids = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001441
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001442 NetworkStats.Entry entry = null;
1443 final int size = stats != null ? stats.size() : 0;
1444 for (int i = 0; i < size; i++) {
1445 entry = stats.getValues(i, entry);
1446
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001447 final boolean isApp = UserId.isApp(entry.uid);
1448 final int appId = isApp ? UserId.getAppId(entry.uid) : entry.uid;
1449 if (isApp || appId == UID_REMOVED || appId == UID_TETHERING) {
1450 AppItem item = knownUids.get(appId);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001451 if (item == null) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001452 item = new AppItem(appId);
1453 knownUids.put(appId, item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001454 mItems.add(item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001455 }
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001456
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001457 item.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001458 item.addUid(entry.uid);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001459 } else {
1460 systemItem.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001461 systemItem.addUid(entry.uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001462 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001463 }
1464
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001465 if (systemItem.total > 0) {
1466 mItems.add(systemItem);
1467 }
1468
Jeff Sharkey8a503642011-06-10 13:31:21 -07001469 Collections.sort(mItems);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001470 mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001471 notifyDataSetChanged();
1472 }
1473
1474 @Override
1475 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001476 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001477 }
1478
1479 @Override
1480 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001481 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001482 }
1483
1484 @Override
1485 public long getItemId(int position) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001486 return mItems.get(position).appId;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001487 }
1488
1489 @Override
1490 public View getView(int position, View convertView, ViewGroup parent) {
1491 if (convertView == null) {
1492 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey5d706792011-09-08 18:57:17 -07001493 R.layout.data_usage_item, parent, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001494
1495 if (mInsetSide > 0) {
1496 convertView.setPadding(mInsetSide, 0, mInsetSide, 0);
1497 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001498 }
1499
1500 final Context context = parent.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001501
Jeff Sharkey28130d92011-09-02 16:10:24 -07001502 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001503 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1504 android.R.id.progress);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001505
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001506 // kick off async load of app details
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001507 final AppItem item = mItems.get(position);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001508 UidDetailTask.bindView(mProvider, item, convertView);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001509
Jeff Sharkey28130d92011-09-02 16:10:24 -07001510 text1.setText(Formatter.formatFileSize(context, item.total));
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001511
1512 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1513 progress.setProgress(percentTotal);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001514
1515 return convertView;
1516 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001517 }
1518
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001519 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001520 * Empty {@link Fragment} that controls display of UID details in
1521 * {@link DataUsageSummary}.
1522 */
1523 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001524 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001525
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001526 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001527 if (!parent.isAdded()) return;
1528
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001529 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001530 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001531
1532 final AppDetailsFragment fragment = new AppDetailsFragment();
1533 fragment.setArguments(args);
1534 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001535 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1536 ft.add(fragment, TAG_APP_DETAILS);
1537 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001538 ft.setBreadCrumbTitle(label);
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001539 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001540 }
1541
1542 @Override
1543 public void onStart() {
1544 super.onStart();
1545 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001546 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001547 target.updateBody();
1548 }
1549
1550 @Override
1551 public void onStop() {
1552 super.onStop();
1553 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001554 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001555 target.updateBody();
1556 }
1557 }
1558
1559 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001560 * Dialog to request user confirmation before setting
1561 * {@link NetworkPolicy#limitBytes}.
1562 */
1563 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001564 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001565 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001566
1567 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001568 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001569
Jeff Sharkey461842a2011-09-25 18:22:48 -07001570 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001571 final CharSequence message;
1572 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001573
1574 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001575 final String currentTab = parent.mCurrentTab;
1576 if (TAB_3G.equals(currentTab)) {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001577 message = buildDialogMessage(res, R.string.data_usage_tab_3g);
1578 limitBytes = 5 * GB_IN_BYTES;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001579 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001580 message = buildDialogMessage(res, R.string.data_usage_tab_4g);
1581 limitBytes = 5 * GB_IN_BYTES;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001582 } else if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001583 message = buildDialogMessage(res, R.string.data_usage_list_mobile);
1584 limitBytes = 5 * GB_IN_BYTES;
1585 } else if (TAB_WIFI.equals(currentTab)) {
1586 message = buildDialogMessage(res, R.string.data_usage_tab_wifi);
1587 limitBytes = 5 * GB_IN_BYTES;
1588 } else {
1589 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001590 }
1591
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001592 final Bundle args = new Bundle();
1593 args.putCharSequence(EXTRA_MESSAGE, message);
1594 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1595
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001596 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1597 dialog.setArguments(args);
1598 dialog.setTargetFragment(parent, 0);
1599 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1600 }
1601
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001602 private static CharSequence buildDialogMessage(Resources res, int networkResId) {
1603 return res.getString(R.string.data_usage_limit_dialog, res.getString(networkResId));
1604 }
1605
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001606 @Override
1607 public Dialog onCreateDialog(Bundle savedInstanceState) {
1608 final Context context = getActivity();
1609
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001610 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001611 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1612
1613 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1614 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001615 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001616
1617 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1618 public void onClick(DialogInterface dialog, int which) {
1619 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1620 if (target != null) {
1621 target.setPolicyLimitBytes(limitBytes);
1622 }
1623 }
1624 });
1625
1626 return builder.create();
1627 }
1628 }
1629
1630 /**
1631 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1632 */
1633 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001634 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001635
1636 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001637 if (!parent.isAdded()) return;
1638
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001639 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001640 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001641
1642 final CycleEditorFragment dialog = new CycleEditorFragment();
1643 dialog.setArguments(args);
1644 dialog.setTargetFragment(parent, 0);
1645 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1646 }
1647
1648 @Override
1649 public Dialog onCreateDialog(Bundle savedInstanceState) {
1650 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001651 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1652 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001653
1654 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1655 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1656
1657 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1658 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1659
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001660 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1661 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001662
1663 cycleDayPicker.setMinValue(1);
1664 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001665 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001666 cycleDayPicker.setWrapSelectorWheel(true);
1667
1668 builder.setTitle(R.string.data_usage_cycle_editor_title);
1669 builder.setView(view);
1670
1671 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1672 new DialogInterface.OnClickListener() {
1673 public void onClick(DialogInterface dialog, int which) {
1674 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001675 final String cycleTimezone = new Time().timezone;
1676 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001677 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001678 }
1679 });
1680
1681 return builder.create();
1682 }
1683 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001684
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001685 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001686 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1687 */
1688 public static class WarningEditorFragment extends DialogFragment {
1689 private static final String EXTRA_TEMPLATE = "template";
1690
1691 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001692 if (!parent.isAdded()) return;
1693
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001694 final Bundle args = new Bundle();
1695 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1696
1697 final WarningEditorFragment dialog = new WarningEditorFragment();
1698 dialog.setArguments(args);
1699 dialog.setTargetFragment(parent, 0);
1700 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1701 }
1702
1703 @Override
1704 public Dialog onCreateDialog(Bundle savedInstanceState) {
1705 final Context context = getActivity();
1706 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1707 final NetworkPolicyEditor editor = target.mPolicyEditor;
1708
1709 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1710 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1711
1712 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1713 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1714
1715 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1716 final long warningBytes = editor.getPolicyWarningBytes(template);
1717 final long limitBytes = editor.getPolicyLimitBytes(template);
1718
1719 bytesPicker.setMinValue(0);
1720 if (limitBytes != LIMIT_DISABLED) {
1721 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1722 } else {
1723 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1724 }
1725 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1726 bytesPicker.setWrapSelectorWheel(false);
1727
1728 builder.setTitle(R.string.data_usage_warning_editor_title);
1729 builder.setView(view);
1730
1731 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1732 new DialogInterface.OnClickListener() {
1733 public void onClick(DialogInterface dialog, int which) {
1734 // clear focus to finish pending text edits
1735 bytesPicker.clearFocus();
1736
1737 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1738 editor.setPolicyWarningBytes(template, bytes);
1739 target.updatePolicy(false);
1740 }
1741 });
1742
1743 return builder.create();
1744 }
1745 }
1746
1747 /**
1748 * Dialog to edit {@link NetworkPolicy#limitBytes}.
1749 */
1750 public static class LimitEditorFragment extends DialogFragment {
1751 private static final String EXTRA_TEMPLATE = "template";
1752
1753 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001754 if (!parent.isAdded()) return;
1755
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001756 final Bundle args = new Bundle();
1757 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1758
1759 final LimitEditorFragment dialog = new LimitEditorFragment();
1760 dialog.setArguments(args);
1761 dialog.setTargetFragment(parent, 0);
1762 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
1763 }
1764
1765 @Override
1766 public Dialog onCreateDialog(Bundle savedInstanceState) {
1767 final Context context = getActivity();
1768 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1769 final NetworkPolicyEditor editor = target.mPolicyEditor;
1770
1771 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1772 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1773
1774 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1775 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1776
1777 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1778 final long warningBytes = editor.getPolicyWarningBytes(template);
1779 final long limitBytes = editor.getPolicyLimitBytes(template);
1780
1781 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09001782 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001783 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
1784 } else {
1785 bytesPicker.setMinValue(0);
1786 }
1787 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
1788 bytesPicker.setWrapSelectorWheel(false);
1789
1790 builder.setTitle(R.string.data_usage_limit_editor_title);
1791 builder.setView(view);
1792
1793 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1794 new DialogInterface.OnClickListener() {
1795 public void onClick(DialogInterface dialog, int which) {
1796 // clear focus to finish pending text edits
1797 bytesPicker.clearFocus();
1798
1799 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1800 editor.setPolicyLimitBytes(template, bytes);
1801 target.updatePolicy(false);
1802 }
1803 });
1804
1805 return builder.create();
1806 }
1807 }
1808 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07001809 * Dialog to request user confirmation before disabling data.
1810 */
1811 public static class ConfirmDataDisableFragment extends DialogFragment {
1812 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001813 if (!parent.isAdded()) return;
1814
Jeff Sharkey28130d92011-09-02 16:10:24 -07001815 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
1816 dialog.setTargetFragment(parent, 0);
1817 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
1818 }
1819
1820 @Override
1821 public Dialog onCreateDialog(Bundle savedInstanceState) {
1822 final Context context = getActivity();
1823
1824 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1825 builder.setMessage(R.string.data_usage_disable_mobile);
1826
1827 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1828 public void onClick(DialogInterface dialog, int which) {
1829 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1830 if (target != null) {
1831 // TODO: extend to modify policy enabled flag.
1832 target.setMobileDataEnabled(false);
1833 }
1834 }
1835 });
1836 builder.setNegativeButton(android.R.string.cancel, null);
1837
1838 return builder.create();
1839 }
1840 }
1841
1842 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001843 * Dialog to request user confirmation before setting
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001844 * {@link Settings.Secure#DATA_ROAMING}.
1845 */
1846 public static class ConfirmDataRoamingFragment extends DialogFragment {
1847 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001848 if (!parent.isAdded()) return;
1849
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001850 final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment();
1851 dialog.setTargetFragment(parent, 0);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001852 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_ROAMING);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001853 }
1854
1855 @Override
1856 public Dialog onCreateDialog(Bundle savedInstanceState) {
1857 final Context context = getActivity();
1858
1859 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1860 builder.setTitle(R.string.roaming_reenable_title);
1861 builder.setMessage(R.string.roaming_warning);
1862
1863 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1864 public void onClick(DialogInterface dialog, int which) {
1865 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1866 if (target != null) {
1867 target.setDataRoaming(true);
1868 }
1869 }
1870 });
1871 builder.setNegativeButton(android.R.string.cancel, null);
1872
1873 return builder.create();
1874 }
1875 }
1876
1877 /**
1878 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001879 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001880 */
1881 public static class ConfirmRestrictFragment extends DialogFragment {
1882 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001883 if (!parent.isAdded()) return;
1884
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001885 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1886 dialog.setTargetFragment(parent, 0);
1887 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1888 }
1889
1890 @Override
1891 public Dialog onCreateDialog(Bundle savedInstanceState) {
1892 final Context context = getActivity();
1893
1894 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001895 builder.setTitle(R.string.data_usage_restrict_background_title);
Jeff Sharkey461842a2011-09-25 18:22:48 -07001896 builder.setMessage(getString(R.string.data_usage_restrict_background));
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001897
1898 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1899 public void onClick(DialogInterface dialog, int which) {
1900 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1901 if (target != null) {
1902 target.setRestrictBackground(true);
1903 }
1904 }
1905 });
1906 builder.setNegativeButton(android.R.string.cancel, null);
1907
1908 return builder.create();
1909 }
1910 }
1911
1912 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001913 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
1914 * change has been denied, usually based on
1915 * {@link DataUsageSummary#hasLimitedNetworks()}.
1916 */
1917 public static class DeniedRestrictFragment extends DialogFragment {
1918 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001919 if (!parent.isAdded()) return;
1920
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001921 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
1922 dialog.setTargetFragment(parent, 0);
1923 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
1924 }
1925
1926 @Override
1927 public Dialog onCreateDialog(Bundle savedInstanceState) {
1928 final Context context = getActivity();
1929
1930 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1931 builder.setTitle(R.string.data_usage_app_restrict_background);
1932 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
1933 builder.setPositiveButton(android.R.string.ok, null);
1934
1935 return builder.create();
1936 }
1937 }
1938
1939 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001940 * Dialog to request user confirmation before setting
1941 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
1942 */
1943 public static class ConfirmAppRestrictFragment extends DialogFragment {
1944 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001945 if (!parent.isAdded()) return;
1946
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001947 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
1948 dialog.setTargetFragment(parent, 0);
1949 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
1950 }
1951
1952 @Override
1953 public Dialog onCreateDialog(Bundle savedInstanceState) {
1954 final Context context = getActivity();
1955
1956 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001957 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
1958 builder.setMessage(R.string.data_usage_app_restrict_dialog);
1959
1960 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1961 public void onClick(DialogInterface dialog, int which) {
1962 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1963 if (target != null) {
1964 target.setAppRestrictBackground(true);
1965 }
1966 }
1967 });
1968 builder.setNegativeButton(android.R.string.cancel, null);
1969
1970 return builder.create();
1971 }
1972 }
1973
1974 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001975 * Compute default tab that should be selected, based on
1976 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
1977 */
1978 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07001979 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
1980 if (template == null) return null;
1981
1982 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07001983 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001984 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001985 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001986 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001987 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001988 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001989 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001990 return TAB_WIFI;
1991 default:
1992 return null;
1993 }
1994 }
1995
1996 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001997 * Background task that loads {@link UidDetail}, binding to
1998 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001999 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002000 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2001 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002002 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002003 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002004
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002005 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002006 mProvider = checkNotNull(provider);
2007 mItem = checkNotNull(item);
2008 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002009 }
2010
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002011 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002012 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002013 final UidDetailTask existing = (UidDetailTask) target.getTag();
2014 if (existing != null) {
2015 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002016 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002017
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002018 final UidDetail cachedDetail = provider.getUidDetail(item.appId, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002019 if (cachedDetail != null) {
2020 bindView(cachedDetail, target);
2021 } else {
2022 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2023 AsyncTask.THREAD_POOL_EXECUTOR));
2024 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002025 }
2026
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002027 private static void bindView(UidDetail detail, View target) {
2028 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2029 final TextView title = (TextView) target.findViewById(android.R.id.title);
2030
2031 if (detail != null) {
2032 icon.setImageDrawable(detail.icon);
2033 title.setText(detail.label);
2034 } else {
2035 icon.setImageDrawable(null);
2036 title.setText(null);
2037 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002038 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002039
2040 @Override
2041 protected void onPreExecute() {
2042 bindView(null, mTarget);
2043 }
2044
2045 @Override
2046 protected UidDetail doInBackground(Void... params) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002047 return mProvider.getUidDetail(mItem.appId, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002048 }
2049
2050 @Override
2051 protected void onPostExecute(UidDetail result) {
2052 bindView(result, mTarget);
2053 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002054 }
2055
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002056 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002057 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002058 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002059 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002060 if (TEST_RADIOS) {
2061 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2062 }
2063
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002064 final ConnectivityManager conn = ConnectivityManager.from(context);
2065 final TelephonyManager tele = TelephonyManager.from(context);
2066
2067 // require both supported network and ready SIM
2068 return conn.isNetworkSupported(TYPE_MOBILE) && tele.getSimState() == SIM_STATE_READY;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002069 }
2070
2071 /**
2072 * Test if device has a mobile 4G data radio.
2073 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002074 public static boolean hasMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002075 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2076 return false;
2077 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002078 if (TEST_RADIOS) {
2079 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2080 }
2081
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002082 final ConnectivityManager conn = ConnectivityManager.from(context);
2083 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002084
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002085 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002086 final boolean hasLte = tele.getLteOnCdmaMode() == Phone.LTE_ON_CDMA_TRUE;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002087 return hasWimax || hasLte;
2088 }
2089
2090 /**
2091 * Test if device has a Wi-Fi data radio.
2092 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002093 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002094 if (TEST_RADIOS) {
2095 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
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_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002100 }
2101
2102 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002103 * Test if device has an ethernet network connection.
2104 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002105 public static boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002106 if (TEST_RADIOS) {
2107 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2108 }
2109
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002110 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002111 return conn.isNetworkSupported(TYPE_ETHERNET);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002112 }
2113
2114 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002115 * Inflate a {@link Preference} style layout, adding the given {@link View}
2116 * widget into {@link android.R.id#widget_frame}.
2117 */
2118 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2119 final View view = inflater.inflate(R.layout.preference, root, false);
2120 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2121 android.R.id.widget_frame);
2122 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2123 return view;
2124 }
2125
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002126 private static View inflateAppTitle(
2127 LayoutInflater inflater, ViewGroup root, CharSequence label) {
2128 final TextView view = (TextView) inflater.inflate(
2129 R.layout.data_usage_app_title, root, false);
2130 view.setText(label);
2131 return view;
2132 }
2133
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002134 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002135 * Test if any networks are currently limited.
2136 */
2137 private boolean hasLimitedNetworks() {
2138 return !buildLimitedNetworksList().isEmpty();
2139 }
2140
2141 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002142 * Build string describing currently limited networks, which defines when
2143 * background data is restricted.
2144 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002145 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002146 private CharSequence buildLimitedNetworksString() {
2147 final List<CharSequence> limited = buildLimitedNetworksList();
2148
2149 // handle case where no networks limited
2150 if (limited.isEmpty()) {
2151 limited.add(getText(R.string.data_usage_list_none));
2152 }
2153
2154 return TextUtils.join(limited);
2155 }
2156
2157 /**
2158 * Build list of currently limited networks, which defines when background
2159 * data is restricted.
2160 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002161 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002162 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002163 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002164
2165 // build combined list of all limited networks
2166 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002167
2168 final TelephonyManager tele = TelephonyManager.from(context);
2169 if (tele.getSimState() == SIM_STATE_READY) {
2170 final String subscriberId = getActiveSubscriberId(context);
2171 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2172 limited.add(getText(R.string.data_usage_list_mobile));
2173 }
2174 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2175 limited.add(getText(R.string.data_usage_tab_3g));
2176 }
2177 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2178 limited.add(getText(R.string.data_usage_tab_4g));
2179 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002180 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002181
2182 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002183 limited.add(getText(R.string.data_usage_tab_wifi));
2184 }
2185 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2186 limited.add(getText(R.string.data_usage_tab_ethernet));
2187 }
2188
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002189 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002190 }
2191
2192 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002193 * Inset both selector and divider {@link Drawable} on the given
2194 * {@link ListView} by the requested dimensions.
2195 */
2196 private static void insetListViewDrawables(ListView view, int insetSide) {
2197 final Drawable selector = view.getSelector();
2198 final Drawable divider = view.getDivider();
2199
2200 // fully unregister these drawables so callbacks can be maintained after
2201 // wrapping below.
2202 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2203 view.setSelector(stub);
2204 view.setDivider(stub);
2205
2206 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2207 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2208 }
2209
2210 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002211 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002212 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002213 */
2214 private static void setPreferenceTitle(View parent, int resId) {
2215 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2216 title.setText(resId);
2217 }
2218
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002219 /**
2220 * Set {@link android.R.id#summary} for a preference view inflated with
2221 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2222 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002223 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002224 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2225 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002226 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002227 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002228}