blob: 3d5678d2c6a320b1aca119da7624eab46b79e4b7 [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 Sharkey38305fb2012-09-14 16:26:51 -070051import android.app.ActivityManager;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070052import android.app.AlertDialog;
53import android.app.Dialog;
54import android.app.DialogFragment;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070055import android.app.Fragment;
Amith Yamasani5ba0a022011-11-07 12:29:00 -080056import android.app.FragmentManager;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070057import android.app.FragmentTransaction;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070058import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070059import android.content.ContentResolver;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070060import android.content.Context;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070061import android.content.DialogInterface;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -070062import android.content.Intent;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070063import android.content.Loader;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070064import android.content.SharedPreferences;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070065import android.content.pm.PackageManager;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070066import android.content.res.Resources;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070067import android.graphics.Color;
Jeff Sharkey5d706792011-09-08 18:57:17 -070068import android.graphics.drawable.ColorDrawable;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070069import android.graphics.drawable.Drawable;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070070import android.net.ConnectivityManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070071import android.net.INetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070072import android.net.INetworkStatsService;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070073import android.net.INetworkStatsSession;
Jeff Sharkey8a503642011-06-10 13:31:21 -070074import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070075import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070076import android.net.NetworkStats;
77import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070078import android.net.NetworkTemplate;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070079import android.net.TrafficStats;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070080import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070081import android.os.Bundle;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070082import android.os.INetworkManagementService;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070083import android.os.Parcel;
84import android.os.Parcelable;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070085import android.os.RemoteException;
86import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070087import android.os.SystemProperties;
Dianne Hackbornbb06a422012-08-16 11:01:57 -070088import android.os.UserHandle;
Jeff Sharkey8a503642011-06-10 13:31:21 -070089import android.preference.Preference;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070090import android.preference.PreferenceActivity;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070091import android.provider.Settings;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070092import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070093import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070094import android.text.format.DateUtils;
95import android.text.format.Formatter;
Jeff Sharkeye5223a02012-03-09 17:11:14 -080096import android.text.format.Time;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070097import android.util.Log;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070098import android.util.SparseArray;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070099import android.util.SparseBooleanArray;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700100import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700101import android.view.Menu;
102import android.view.MenuInflater;
103import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700104import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700105import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700106import android.view.ViewGroup;
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
Wink Saville55434042012-06-14 12:33:43 -0700130import com.android.internal.telephony.PhoneConstants;
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";
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700181 private static final String TAG_CONFIRM_AUTO_SYNC_CHANGE = "confirmAutoSyncChange";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700182 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700183
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700184 private static final int LOADER_CHART_DATA = 2;
185 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700186
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700187 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700188 private INetworkStatsService mStatsService;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700189 private NetworkPolicyManager mPolicyManager;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700190 private ConnectivityManager mConnService;
191
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700192 private INetworkStatsSession mStatsSession;
193
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700194 private static final String PREF_FILE = "data_usage";
195 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700196 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700197
198 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700199
Jeff Sharkey8a503642011-06-10 13:31:21 -0700200 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700201 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700202 private TabWidget mTabWidget;
203 private ListView mListView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700204 private DataUsageAdapter mAdapter;
205
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700206 /** Distance to inset content from sides, when needed. */
207 private int mInsetSide = 0;
208
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700209 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700210
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700211 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700212 private LinearLayout mNetworkSwitches;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700213 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700214 private View mDataEnabledView;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700215 private CheckBox mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700216 private View mDisableAtLimitView;
217
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700218 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700219 private Spinner mCycleSpinner;
220 private CycleAdapter mCycleAdapter;
221
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700222 private ChartDataUsageView mChart;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700223 private TextView mUsageSummary;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700224 private TextView mEmpty;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700225
226 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700227 private ImageView mAppIcon;
228 private ViewGroup mAppTitles;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700229 private PieChartView mAppPieChart;
230 private TextView mAppForeground;
231 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700232 private Button mAppSettings;
233
234 private LinearLayout mAppSwitches;
235 private CheckBox mAppRestrict;
236 private View mAppRestrictView;
237
Jeff Sharkey8a503642011-06-10 13:31:21 -0700238 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700239 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700240
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700241 private NetworkTemplate mTemplate;
242 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700243
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700244 private AppItem mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700245
246 private Intent mAppSettingsIntent;
247
Jeff Sharkeya662e492011-06-18 21:57:06 -0700248 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700249
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700250 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700251 private String mIntentTab = null;
252
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700253 private MenuItem mMenuDataRoaming;
254 private MenuItem mMenuRestrictBackground;
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700255 private MenuItem mMenuAutoSync;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700256
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700257 /** Flag used to ignore listeners during binding. */
258 private boolean mBinding;
259
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700260 private UidDetailProvider mUidDetailProvider;
261
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700262 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700263 public void onCreate(Bundle savedInstanceState) {
264 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700265 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700266
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700267 mNetworkService = INetworkManagementService.Stub.asInterface(
268 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700269 mStatsService = INetworkStatsService.Stub.asInterface(
270 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700271 mPolicyManager = NetworkPolicyManager.from(context);
272 mConnService = ConnectivityManager.from(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700273
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700274 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700275
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700276 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700277 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700278
Jaewan Kimffce9c12013-03-19 16:53:45 +0900279 try {
280 mStatsSession = mStatsService.openSession();
281 } catch (RemoteException e) {
282 throw new RuntimeException(e);
283 }
284
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700285 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700286 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700287
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700288 // override preferences when no mobile radio
289 if (!hasReadyMobileRadio(context)) {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900290 mShowWifi = true;
291 mShowEthernet = true;
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700292 }
293
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700294 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700295 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700296
Jeff Sharkey8a503642011-06-10 13:31:21 -0700297 @Override
298 public View onCreateView(LayoutInflater inflater, ViewGroup container,
299 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700300
Jeff Sharkey8a503642011-06-10 13:31:21 -0700301 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700302 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
303
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700304 mUidDetailProvider = new UidDetailProvider(context);
305
Jeff Sharkey8a503642011-06-10 13:31:21 -0700306 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700307 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700308 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
309 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700310
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700311 // decide if we need to manually inset our content, or if we should rely
312 // on parent container for inset.
313 final boolean shouldInset = mListView.getScrollBarStyle()
314 == View.SCROLLBARS_OUTSIDE_OVERLAY;
Amith Yamasani56f51a82013-08-05 10:07:23 -0700315 mInsetSide = 0;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700316
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700317 // adjust padding around tabwidget as needed
Amith Yamasani56f51a82013-08-05 10:07:23 -0700318 prepareCustomPreferencesList(container, view, mListView, false);
Jeff Sharkey5d706792011-09-08 18:57:17 -0700319
Jeff Sharkey8a503642011-06-10 13:31:21 -0700320 mTabHost.setup();
321 mTabHost.setOnTabChangedListener(mTabListener);
322
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700323 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700324 mHeader.setClickable(true);
325
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700326 mListView.addHeaderView(new View(context), null, true);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700327 mListView.addHeaderView(mHeader, null, true);
328 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700329
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700330 if (mInsetSide > 0) {
331 // inset selector and divider drawables
332 insetListViewDrawables(mListView, mInsetSide);
Fabrice Di Megliob27223f2013-01-15 18:54:11 -0800333 mHeader.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700334 }
335
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700336 {
337 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700338 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
339 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700340 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700341
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700342 mDataEnabled = new Switch(inflater.getContext());
343 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
344 mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener);
345 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700346
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700347 mDisableAtLimit = new CheckBox(inflater.getContext());
348 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700349 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700350 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700351 mDisableAtLimitView.setClickable(true);
352 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700353 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
354 mNetworkSwitches.addView(mDisableAtLimitView);
355 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700356
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700357 // bind cycle dropdown
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700358 mCycleView = mHeader.findViewById(R.id.cycles);
359 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700360 mCycleAdapter = new CycleAdapter(context);
361 mCycleSpinner.setAdapter(mCycleAdapter);
362 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
363
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700364 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700365 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800366 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700367
368 {
369 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700370 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700371 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
372 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700373 mAppPieChart = (PieChartView) mAppDetail.findViewById(R.id.app_pie_chart);
374 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
375 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700376 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700377
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700378 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700379 mAppSettings.setOnClickListener(mAppSettingsListener);
380
381 mAppRestrict = new CheckBox(inflater.getContext());
382 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700383 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700384 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700385 mAppRestrictView.setClickable(true);
386 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700387 mAppRestrictView.setOnClickListener(mAppRestrictListener);
388 mAppSwitches.addView(mAppRestrictView);
389 }
390
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700391 mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700392 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700393
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700394 mAdapter = new DataUsageAdapter(mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700395 mListView.setOnItemClickListener(mListListener);
396 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700397
398 return view;
399 }
400
401 @Override
402 public void onResume() {
403 super.onResume();
404
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700405 // pick default tab based on incoming intent
406 final Intent intent = getActivity().getIntent();
407 mIntentTab = computeTabFromIntent(intent);
408
Jeff Sharkey8a503642011-06-10 13:31:21 -0700409 // this kicks off chain reaction which creates tabs, binds the body to
410 // selected network, and binds chart, cycles and detail list.
411 updateTabs();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700412
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700413 // kick off background task to update stats
414 new AsyncTask<Void, Void, Void>() {
415 @Override
416 protected Void doInBackground(Void... params) {
417 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700418 // wait a few seconds before kicking off
419 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700420 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700421 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700422 } catch (RemoteException e) {
423 }
424 return null;
425 }
426
427 @Override
428 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700429 if (isAdded()) {
430 updateBody();
431 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700432 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700433 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700434 }
435
Jeff Sharkey8a503642011-06-10 13:31:21 -0700436 @Override
437 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
438 inflater.inflate(R.menu.data_usage, menu);
439 }
440
441 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700442 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700443 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700444 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700445 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700446
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700447 mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700448 mMenuDataRoaming.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700449 mMenuDataRoaming.setChecked(getDataRoaming());
450
451 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700452 mMenuRestrictBackground.setVisible(
453 hasReadyMobileRadio(context) && isOwner && !appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700454 mMenuRestrictBackground.setChecked(mPolicyManager.getRestrictBackground());
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700455
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700456 mMenuAutoSync = menu.findItem(R.id.data_usage_menu_auto_sync);
457 mMenuAutoSync.setChecked(ContentResolver.getMasterSyncAutomatically());
Jeff Sharkeyfda48e32012-09-18 15:03:47 -0700458 mMenuAutoSync.setVisible(!appDetailMode);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700459
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700460 final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g);
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700461 split4g.setVisible(hasReadyMobile4gRadio(context) && isOwner && !appDetailMode);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700462 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700463
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700464 final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700465 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700466 showWifi.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700467 showWifi.setChecked(mShowWifi);
468 } else {
469 showWifi.setVisible(false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700470 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700471
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700472 final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700473 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700474 showEthernet.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700475 showEthernet.setChecked(mShowEthernet);
476 } else {
477 showEthernet.setVisible(false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700478 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700479
480 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
481 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700482 metered.setVisible(!appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700483 } else {
484 metered.setVisible(false);
485 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700486
487 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
488 String helpUrl;
489 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800490 HelpUtils.prepareHelpMenuItem(context, help, helpUrl);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700491 } else {
492 help.setVisible(false);
493 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700494 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700495
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700496 @Override
497 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700498 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700499 case R.id.data_usage_menu_roaming: {
500 final boolean dataRoaming = !item.isChecked();
501 if (dataRoaming) {
502 ConfirmDataRoamingFragment.show(this);
503 } else {
504 // no confirmation to disable roaming
505 setDataRoaming(false);
506 }
507 return true;
508 }
509 case R.id.data_usage_menu_restrict_background: {
510 final boolean restrictBackground = !item.isChecked();
511 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800512 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700513 } else {
514 // no confirmation to drop restriction
515 setRestrictBackground(false);
516 }
517 return true;
518 }
519 case R.id.data_usage_menu_split_4g: {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700520 final boolean mobileSplit = !item.isChecked();
Jeff Sharkeya662e492011-06-18 21:57:06 -0700521 setMobilePolicySplit(mobileSplit);
522 item.setChecked(isMobilePolicySplit());
Jeff Sharkey8a503642011-06-10 13:31:21 -0700523 updateTabs();
524 return true;
525 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700526 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700527 mShowWifi = !item.isChecked();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700528 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700529 item.setChecked(mShowWifi);
530 updateTabs();
531 return true;
532 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700533 case R.id.data_usage_menu_show_ethernet: {
534 mShowEthernet = !item.isChecked();
535 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
536 item.setChecked(mShowEthernet);
537 updateTabs();
538 return true;
539 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700540 case R.id.data_usage_menu_metered: {
541 final PreferenceActivity activity = (PreferenceActivity) getActivity();
542 activity.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
543 R.string.data_usage_metered_title, null, this, 0);
544 return true;
545 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700546 case R.id.data_usage_menu_auto_sync: {
Guang Zhu167ba2a2012-10-19 17:55:05 -0700547 if (ActivityManager.isUserAMonkey()) {
548 Log.d("SyncState", "ignoring monkey's attempt to flip global sync state");
549 } else {
550 ConfirmAutoSyncChangeFragment.show(this, !item.isChecked());
551 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700552 return true;
553 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700554 }
555 return false;
556 }
557
Jeff Sharkey94a90952011-06-13 22:31:09 -0700558 @Override
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700559 public void onDestroy() {
Jeff Sharkey94a90952011-06-13 22:31:09 -0700560 mDataEnabledView = null;
561 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700562
563 mUidDetailProvider.clearCache();
564 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700565
566 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700567
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800568 if (this.isRemoving()) {
569 getFragmentManager()
570 .popBackStack(TAG_APP_DETAILS, FragmentManager.POP_BACK_STACK_INCLUSIVE);
571 }
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700572
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800573 super.onDestroy();
574 }
575
Jeff Sharkey8a503642011-06-10 13:31:21 -0700576 /**
Jeff Sharkey92811822012-05-04 11:47:29 -0700577 * Build and assign {@link LayoutTransition} to various containers. Should
578 * only be assigned after initial layout is complete.
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700579 */
Jeff Sharkey92811822012-05-04 11:47:29 -0700580 private void ensureLayoutTransitions() {
581 // skip when already setup
582 if (mChart.getLayoutTransition() != null) return;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700583
Jeff Sharkey92811822012-05-04 11:47:29 -0700584 mTabsContainer.setLayoutTransition(buildLayoutTransition());
585 mHeader.setLayoutTransition(buildLayoutTransition());
586 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700587
Jeff Sharkey92811822012-05-04 11:47:29 -0700588 final LayoutTransition chartTransition = buildLayoutTransition();
589 chartTransition.disableTransitionType(LayoutTransition.APPEARING);
590 chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
591 mChart.setLayoutTransition(chartTransition);
592 }
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700593
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700594 private static LayoutTransition buildLayoutTransition() {
595 final LayoutTransition transition = new LayoutTransition();
596 if (TEST_ANIM) {
597 transition.setDuration(1500);
598 }
599 transition.setAnimateParentHierarchy(false);
600 return transition;
601 }
602
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700603 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700604 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700605 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
606 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700607 */
608 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700609 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700610 mTabHost.clearAllTabs();
611
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700612 final boolean mobileSplit = isMobilePolicySplit();
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700613 if (mobileSplit && hasReadyMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700614 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
615 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700616 } else if (hasReadyMobileRadio(context)) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700617 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700618 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700619 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700620 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
621 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700622 if (mShowEthernet && hasEthernet(context)) {
623 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
624 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700625
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700626 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700627 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
628 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
629 if (mIntentTab != null) {
630 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700631 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700632 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700633 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700634 mTabHost.setCurrentTabByTag(mIntentTab);
635 }
636 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700637 } else if (noTabs) {
638 // no usable tabs, so hide body
639 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700640 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700641 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700642 }
643 }
644
Jeff Sharkey8a503642011-06-10 13:31:21 -0700645 /**
646 * Factory that provide empty {@link View} to make {@link TabHost} happy.
647 */
648 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700649 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700650 public View createTabContent(String tag) {
651 return new View(mTabHost.getContext());
652 }
653 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700654
Jeff Sharkey8a503642011-06-10 13:31:21 -0700655 /**
656 * Build {@link TabSpec} with thin indicator, and empty content.
657 */
658 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700659 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
660 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700661 }
662
663 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700664 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700665 public void onTabChanged(String tabId) {
666 // user changed tab; update body
667 updateBody();
668 }
669 };
670
671 /**
672 * Update body content based on current tab. Loads
673 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
674 * binds them to visible controls.
675 */
676 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700677 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700678 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700679
Jeff Sharkeya662e492011-06-18 21:57:06 -0700680 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700681 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700682 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700683
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700684 if (currentTab == null) {
685 Log.w(TAG, "no tab selected; hiding body");
686 mListView.setVisibility(View.GONE);
687 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700688 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700689 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700690 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700691
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700692 final boolean tabChanged = !currentTab.equals(mCurrentTab);
693 mCurrentTab = currentTab;
694
Jeff Sharkey8a503642011-06-10 13:31:21 -0700695 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
696
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700697 mDataEnabledView.setVisibility(isOwner ? View.VISIBLE : View.GONE);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700698
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700699 // TODO: remove mobile tabs when SIM isn't ready
700 final TelephonyManager tele = TelephonyManager.from(context);
701
Jeff Sharkey8a503642011-06-10 13:31:21 -0700702 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700703 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
704 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700705 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700706
707 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700708 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
709 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
710 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700711 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700712
713 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700714 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
715 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
716 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700717 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700718
719 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700720 // wifi doesn't have any controls
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700721 mDataEnabledView.setVisibility(View.GONE);
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700722 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700723 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700724
725 } else if (TAB_ETHERNET.equals(currentTab)) {
726 // ethernet doesn't have any controls
727 mDataEnabledView.setVisibility(View.GONE);
728 mDisableAtLimitView.setVisibility(View.GONE);
729 mTemplate = buildTemplateEthernet();
730
731 } else {
732 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700733 }
734
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700735 // kick off loader for network history
736 // TODO: consider chaining two loaders together instead of reloading
737 // network history when showing app detail.
738 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700739 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700740
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700741 // detail mode can change visible menus, invalidate
742 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700743
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700744 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700745 }
746
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700747 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700748 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700749 }
750
751 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700752 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700753 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700754 */
755 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700756 final Context context = getActivity();
757 final PackageManager pm = context.getPackageManager();
758 final LayoutInflater inflater = getActivity().getLayoutInflater();
759
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700760 if (isAppDetailMode()) {
761 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700762 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700763 } else {
764 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700765 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700766
767 // hide detail stats when not in detail mode
768 mChart.bindDetailNetworkStats(null);
769 return;
770 }
771
772 // remove warning/limit sweeps while in detail mode
773 mChart.bindNetworkPolicy(null);
774
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700775 // show icon and all labels appearing under this app
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700776 final int uid = mCurrentApp.key;
777 final UidDetail detail = mUidDetailProvider.getUidDetail(uid, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700778 mAppIcon.setImageDrawable(detail.icon);
779
780 mAppTitles.removeAllViews();
781 if (detail.detailLabels != null) {
782 for (CharSequence label : detail.detailLabels) {
783 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label));
784 }
785 } else {
786 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label));
787 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700788
789 // enable settings button when package provides it
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700790 final String[] packageNames = pm.getPackagesForUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700791 if (packageNames != null && packageNames.length > 0) {
792 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700793 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
794
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700795 // Search for match across all packages
796 boolean matchFound = false;
797 for (String packageName : packageNames) {
798 mAppSettingsIntent.setPackage(packageName);
799 if (pm.resolveActivity(mAppSettingsIntent, 0) != null) {
800 matchFound = true;
801 break;
802 }
803 }
804
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700805 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -0700806 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700807
808 } else {
809 mAppSettingsIntent = null;
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700810 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700811 }
812
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700813 updateDetailData();
814
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700815 if (UserHandle.isApp(uid) && !mPolicyManager.getRestrictBackground()
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700816 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700817 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800818 setPreferenceSummary(mAppRestrictView,
819 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700820
821 mAppRestrictView.setVisibility(View.VISIBLE);
822 mAppRestrict.setChecked(getAppRestrictBackground());
823
824 } else {
825 mAppRestrictView.setVisibility(View.GONE);
826 }
827 }
828
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700829 private void setPolicyWarningBytes(long warningBytes) {
830 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700831 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700832 updatePolicy(false);
833 }
834
835 private void setPolicyLimitBytes(long limitBytes) {
836 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700837 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700838 updatePolicy(false);
839 }
840
Jeff Sharkey28130d92011-09-02 16:10:24 -0700841 /**
842 * Local cache of value, used to work around delay when
843 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
844 */
845 private Boolean mMobileDataEnabled;
846
847 private boolean isMobileDataEnabled() {
848 if (mMobileDataEnabled != null) {
849 // TODO: deprecate and remove this once enabled flag is on policy
850 return mMobileDataEnabled;
851 } else {
852 return mConnService.getMobileDataEnabled();
853 }
854 }
855
856 private void setMobileDataEnabled(boolean enabled) {
857 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
858 mConnService.setMobileDataEnabled(enabled);
859 mMobileDataEnabled = enabled;
860 updatePolicy(false);
861 }
862
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700863 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700864 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked()
865 && ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700866 }
867
868 private boolean isBandwidthControlEnabled() {
869 try {
870 return mNetworkService.isBandwidthControlEnabled();
871 } catch (RemoteException e) {
872 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
873 return false;
874 }
875 }
876
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700877 private boolean getDataRoaming() {
878 final ContentResolver resolver = getActivity().getContentResolver();
Christopher Tate5a64c732012-09-07 13:35:31 -0700879 return Settings.Global.getInt(resolver, Settings.Global.DATA_ROAMING, 0) != 0;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700880 }
881
882 private void setDataRoaming(boolean enabled) {
883 // TODO: teach telephony DataConnectionTracker to watch and apply
884 // updates when changed.
885 final ContentResolver resolver = getActivity().getContentResolver();
Christopher Tate5a64c732012-09-07 13:35:31 -0700886 Settings.Global.putInt(resolver, Settings.Global.DATA_ROAMING, enabled ? 1 : 0);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700887 mMenuDataRoaming.setChecked(enabled);
888 }
889
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700890 public void setRestrictBackground(boolean restrictBackground) {
891 mPolicyManager.setRestrictBackground(restrictBackground);
892 mMenuRestrictBackground.setChecked(restrictBackground);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700893 }
894
895 private boolean getAppRestrictBackground() {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700896 final int uid = mCurrentApp.key;
897 final int uidPolicy = mPolicyManager.getUidPolicy(uid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700898 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
899 }
900
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700901 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700902 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700903 final int uid = mCurrentApp.key;
904 mPolicyManager.setUidPolicy(
905 uid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700906 mAppRestrict.setChecked(restrictBackground);
907 }
908
Jeff Sharkey8a503642011-06-10 13:31:21 -0700909 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700910 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
911 * current {@link #mTemplate}.
912 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700913 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700914 if (isAppDetailMode()) {
915 mNetworkSwitches.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700916 } else {
917 mNetworkSwitches.setVisibility(View.VISIBLE);
918 }
919
Jeff Sharkey28130d92011-09-02 16:10:24 -0700920 // TODO: move enabled state directly into policy
921 if (TAB_MOBILE.equals(mCurrentTab)) {
922 mBinding = true;
923 mDataEnabled.setChecked(isMobileDataEnabled());
924 mBinding = false;
925 }
926
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700927 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
928 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700929 mDisableAtLimitView.setVisibility(View.VISIBLE);
930 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700931 if (!isAppDetailMode()) {
932 mChart.bindNetworkPolicy(policy);
933 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700934
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700935 } else {
936 // controls are disabled; don't bind warning/limit sweeps
937 mDisableAtLimitView.setVisibility(View.GONE);
938 mChart.bindNetworkPolicy(null);
939 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700940
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700941 if (refreshCycle) {
942 // generate cycle list based on policy and available history
943 updateCycleList(policy);
944 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700945 }
946
947 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700948 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
949 * and available {@link NetworkStatsHistory} data. Always selects the newest
950 * item, updating the inspection range on {@link #mChart}.
951 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700952 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700953 // stash away currently selected cycle to try restoring below
954 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700955 mCycleAdapter.clear();
956
957 final Context context = mCycleSpinner.getContext();
958
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700959 long historyStart = Long.MAX_VALUE;
960 long historyEnd = Long.MIN_VALUE;
961 if (mChartData != null) {
962 historyStart = mChartData.network.getStart();
963 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700964 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700965
Jeff Sharkey461842a2011-09-25 18:22:48 -0700966 final long now = System.currentTimeMillis();
967 if (historyStart == Long.MAX_VALUE) historyStart = now;
968 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700969
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700970 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700971 if (policy != null) {
972 // find the next cycle boundary
973 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700974
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700975 // walk backwards, generating all valid cycle ranges
976 while (cycleEnd > historyStart) {
977 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
978 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
979 + historyStart);
980 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
981 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700982 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700983 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700984
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700985 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700986 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700987 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700988
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700989 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700990 // no policy defined cycles; show entry for each four-week period
991 long cycleEnd = historyEnd;
992 while (cycleEnd > historyStart) {
993 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
994 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
995 cycleEnd = cycleStart;
996 }
997
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700998 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700999 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001000
1001 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001002 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001003 final int position = mCycleAdapter.findNearestPosition(previousItem);
1004 mCycleSpinner.setSelection(position);
1005
1006 // only force-update cycle when changed; skipping preserves any
1007 // user-defined inspection region.
1008 final CycleItem selectedItem = mCycleAdapter.getItem(position);
1009 if (!Objects.equal(selectedItem, previousItem)) {
1010 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1011 } else {
1012 // but still kick off loader for detailed list
1013 updateDetailData();
1014 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001015 } else {
1016 updateDetailData();
1017 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001018 }
1019
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001020 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001021 @Override
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001022 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
1023 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001024
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001025 final boolean dataEnabled = isChecked;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001026 final String currentTab = mCurrentTab;
1027 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001028 if (dataEnabled) {
1029 setMobileDataEnabled(true);
1030 } else {
1031 // disabling data; show confirmation dialog which eventually
1032 // calls setMobileDataEnabled() once user confirms.
1033 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1034 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001035 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001036
Jeff Sharkey28130d92011-09-02 16:10:24 -07001037 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001038 }
1039 };
1040
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001041 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001042 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001043 public void onClick(View v) {
1044 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001045 if (disableAtLimit) {
1046 // enabling limit; show confirmation dialog which eventually
1047 // calls setPolicyLimitBytes() once user confirms.
1048 ConfirmLimitFragment.show(DataUsageSummary.this);
1049 } else {
1050 setPolicyLimitBytes(LIMIT_DISABLED);
1051 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001052 }
1053 };
1054
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001055 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001056 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001057 public void onClick(View v) {
1058 final boolean restrictBackground = !mAppRestrict.isChecked();
1059
1060 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001061 // enabling restriction; show confirmation dialog which
1062 // eventually calls setRestrictBackground() once user
1063 // confirms.
1064 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001065 } else {
1066 setAppRestrictBackground(false);
1067 }
1068 }
1069 };
1070
1071 private OnClickListener mAppSettingsListener = new OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001072 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001073 public void onClick(View v) {
Jeff Sharkeyd92e0412012-04-24 11:35:43 -07001074 if (!isAdded()) return;
1075
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001076 // TODO: target torwards entire UID instead of just first package
1077 startActivity(mAppSettingsIntent);
1078 }
1079 };
1080
Jeff Sharkey8a503642011-06-10 13:31:21 -07001081 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001082 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001083 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001084 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001085 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001086
1087 // TODO: sigh, remove this hack once we understand 6450986
1088 if (mUidDetailProvider == null || app == null) return;
1089
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001090 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001091 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001092 }
1093 };
1094
1095 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001096 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001097 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1098 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1099 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001100 // show cycle editor; will eventually call setPolicyCycleDay()
1101 // when user finishes editing.
1102 CycleEditorFragment.show(DataUsageSummary.this);
1103
1104 // reset spinner to something other than "change cycle..."
1105 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001106
1107 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001108 if (LOGD) {
1109 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1110 + cycle.end + "]");
1111 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001112
1113 // update chart to show selected cycle, and update detail data
1114 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001115 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001116
1117 updateDetailData();
1118 }
1119 }
1120
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001121 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001122 public void onNothingSelected(AdapterView<?> parent) {
1123 // ignored
1124 }
1125 };
1126
1127 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001128 * Update details based on {@link #mChart} inspection range depending on
1129 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1130 * of applications data usage, and when {@link #isAppDetailMode()} update
1131 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001132 */
1133 private void updateDetailData() {
1134 if (LOGD) Log.d(TAG, "updateDetailData()");
1135
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001136 final long start = mChart.getInspectStart();
1137 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001138 final long now = System.currentTimeMillis();
1139
1140 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001141
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001142 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001143 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001144 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001145 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001146 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001147 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001148 final long foregroundBytes = entry.rxBytes + entry.txBytes;
1149
1150 mAppPieChart.setOriginAngle(175);
1151
1152 mAppPieChart.removeAllSlices();
1153 mAppPieChart.addSlice(foregroundBytes, Color.parseColor("#d88d3a"));
1154 mAppPieChart.addSlice(defaultBytes, Color.parseColor("#666666"));
1155
1156 mAppPieChart.generatePath();
1157
1158 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1159 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1160
1161 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001162 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001163
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001164 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001165
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001166 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001167 if (mChartData != null) {
1168 entry = mChartData.network.getValues(start, end, now, null);
1169 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001170
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001171 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001172 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001173 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001174 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001175
1176 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1177 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001178 final String rangePhrase = formatDateRange(context, start, end);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001179
Jeff Sharkeye557c332012-04-13 16:04:07 -07001180 final int summaryRes;
1181 if (TAB_MOBILE.equals(mCurrentTab) || TAB_3G.equals(mCurrentApp)
1182 || TAB_4G.equals(mCurrentApp)) {
1183 summaryRes = R.string.data_usage_total_during_range_mobile;
1184 } else {
1185 summaryRes = R.string.data_usage_total_during_range;
1186 }
1187
1188 mUsageSummary.setText(getString(summaryRes, totalPhrase, rangePhrase));
Jeff Sharkey92811822012-05-04 11:47:29 -07001189
1190 // initial layout is finished above, ensure we have transitions
1191 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001192 }
1193
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001194 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1195 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001196 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001197 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001198 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001199 }
1200
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001201 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001202 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1203 mChartData = data;
1204 mChart.bindNetworkStats(mChartData.network);
1205 mChart.bindDetailNetworkStats(mChartData.detail);
1206
1207 // calcuate policy cycles based on available data
1208 updatePolicy(true);
1209 updateAppDetail();
1210
1211 // force scroll to top of body when showing detail
1212 if (mChartData.detail != null) {
1213 mListView.smoothScrollToPosition(0);
1214 }
1215 }
1216
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001217 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001218 public void onLoaderReset(Loader<ChartData> loader) {
1219 mChartData = null;
1220 mChart.bindNetworkStats(null);
1221 mChart.bindDetailNetworkStats(null);
1222 }
1223 };
1224
1225 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001226 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001227 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001228 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001229 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001230 }
1231
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001232 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001233 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001234 final int[] restrictedUids = mPolicyManager.getUidsWithPolicy(
Jeff Sharkeye557c332012-04-13 16:04:07 -07001235 POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001236 mAdapter.bindStats(data, restrictedUids);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001237 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001238 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001239
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001240 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001241 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001242 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001243 updateEmptyVisible();
1244 }
1245
1246 private void updateEmptyVisible() {
1247 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1248 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001249 }
1250 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001251
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001252 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001253 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001254 final Context context = getActivity();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001255 if (hasReadyMobileRadio(context)) {
1256 final TelephonyManager tele = TelephonyManager.from(context);
1257 return mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001258 } else {
1259 return false;
1260 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001261 }
1262
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001263 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001264 private void setMobilePolicySplit(boolean split) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001265 final Context context = getActivity();
1266 if (hasReadyMobileRadio(context)) {
1267 final TelephonyManager tele = TelephonyManager.from(context);
1268 mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split);
1269 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001270 }
1271
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001272 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001273 final TelephonyManager tele = TelephonyManager.from(context);
1274 final String actualSubscriberId = tele.getSubscriberId();
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -08001275 return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001276 }
1277
Jeff Sharkey8a503642011-06-10 13:31:21 -07001278 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001279 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001280 public void onInspectRangeChanged() {
1281 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
1282 updateDetailData();
1283 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001284
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001285 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001286 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001287 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001288 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001289
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001290 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001291 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001292 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001293 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001294
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001295 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001296 public void requestWarningEdit() {
1297 WarningEditorFragment.show(DataUsageSummary.this);
1298 }
1299
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001300 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001301 public void requestLimitEdit() {
1302 LimitEditorFragment.show(DataUsageSummary.this);
1303 }
1304 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001305
1306 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001307 * List item that reflects a specific data usage cycle.
1308 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001309 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001310 public CharSequence label;
1311 public long start;
1312 public long end;
1313
Jeff Sharkey8a503642011-06-10 13:31:21 -07001314 CycleItem(CharSequence label) {
1315 this.label = label;
1316 }
1317
1318 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001319 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001320 this.start = start;
1321 this.end = end;
1322 }
1323
Jeff Sharkey8a503642011-06-10 13:31:21 -07001324 @Override
1325 public String toString() {
1326 return label.toString();
1327 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001328
1329 @Override
1330 public boolean equals(Object o) {
1331 if (o instanceof CycleItem) {
1332 final CycleItem another = (CycleItem) o;
1333 return start == another.start && end == another.end;
1334 }
1335 return false;
1336 }
1337
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001338 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001339 public int compareTo(CycleItem another) {
1340 return Long.compare(start, another.start);
1341 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001342 }
1343
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001344 private static final StringBuilder sBuilder = new StringBuilder(50);
1345 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1346 sBuilder, Locale.getDefault());
1347
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001348 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001349 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001350
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001351 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001352 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001353 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1354 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001355 }
1356 }
1357
Jeff Sharkey8a503642011-06-10 13:31:21 -07001358 /**
1359 * Special-case data usage cycle that triggers dialog to change
1360 * {@link NetworkPolicy#cycleDay}.
1361 */
1362 public static class CycleChangeItem extends CycleItem {
1363 public CycleChangeItem(Context context) {
1364 super(context.getString(R.string.data_usage_change_cycle));
1365 }
1366 }
1367
1368 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001369 private boolean mChangePossible = false;
1370 private boolean mChangeVisible = false;
1371
1372 private final CycleChangeItem mChangeItem;
1373
Jeff Sharkey8a503642011-06-10 13:31:21 -07001374 public CycleAdapter(Context context) {
1375 super(context, android.R.layout.simple_spinner_item);
1376 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001377 mChangeItem = new CycleChangeItem(context);
1378 }
1379
1380 public void setChangePossible(boolean possible) {
1381 mChangePossible = possible;
1382 updateChange();
1383 }
1384
1385 public void setChangeVisible(boolean visible) {
1386 mChangeVisible = visible;
1387 updateChange();
1388 }
1389
1390 private void updateChange() {
1391 remove(mChangeItem);
1392 if (mChangePossible && mChangeVisible) {
1393 add(mChangeItem);
1394 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001395 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001396
1397 /**
1398 * Find position of {@link CycleItem} in this adapter which is nearest
1399 * the given {@link CycleItem}.
1400 */
1401 public int findNearestPosition(CycleItem target) {
1402 if (target != null) {
1403 final int count = getCount();
1404 for (int i = count - 1; i >= 0; i--) {
1405 final CycleItem item = getItem(i);
1406 if (item instanceof CycleChangeItem) {
1407 continue;
1408 } else if (item.compareTo(target) >= 0) {
1409 return i;
1410 }
1411 }
1412 }
1413 return 0;
1414 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001415 }
1416
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001417 public static class AppItem implements Comparable<AppItem>, Parcelable {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001418 public final int key;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001419 public boolean restricted;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001420 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001421 public long total;
1422
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001423 public AppItem(int key) {
1424 this.key = key;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001425 }
1426
1427 public AppItem(Parcel parcel) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001428 key = parcel.readInt();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001429 uids = parcel.readSparseBooleanArray();
1430 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001431 }
1432
1433 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001434 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001435 }
1436
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001437 @Override
1438 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001439 dest.writeInt(key);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001440 dest.writeSparseBooleanArray(uids);
1441 dest.writeLong(total);
1442 }
1443
1444 @Override
1445 public int describeContents() {
1446 return 0;
1447 }
1448
1449 @Override
1450 public int compareTo(AppItem another) {
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001451 return Long.compare(another.total, total);
1452 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001453
1454 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1455 @Override
1456 public AppItem createFromParcel(Parcel in) {
1457 return new AppItem(in);
1458 }
1459
1460 @Override
1461 public AppItem[] newArray(int size) {
1462 return new AppItem[size];
1463 }
1464 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001465 }
1466
Jeff Sharkey8a503642011-06-10 13:31:21 -07001467 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001468 * Adapter of applications, sorted by total usage descending.
1469 */
1470 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001471 private final UidDetailProvider mProvider;
1472 private final int mInsetSide;
1473
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001474 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001475 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001476
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001477 public DataUsageAdapter(UidDetailProvider provider, int insetSide) {
1478 mProvider = checkNotNull(provider);
1479 mInsetSide = insetSide;
1480 }
1481
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001482 /**
1483 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1484 */
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001485 public void bindStats(NetworkStats stats, int[] restrictedUids) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001486 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001487
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001488 final int currentUserId = ActivityManager.getCurrentUser();
1489 final SparseArray<AppItem> knownItems = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001490
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001491 NetworkStats.Entry entry = null;
1492 final int size = stats != null ? stats.size() : 0;
1493 for (int i = 0; i < size; i++) {
1494 entry = stats.getValues(i, entry);
1495
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001496 // Decide how to collapse items together
1497 final int uid = entry.uid;
1498 final int collapseKey;
1499 if (UserHandle.isApp(uid)) {
1500 if (UserHandle.getUserId(uid) == currentUserId) {
1501 collapseKey = uid;
1502 } else {
1503 collapseKey = UidDetailProvider.buildKeyForUser(UserHandle.getUserId(uid));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001504 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001505 } else if (uid == UID_REMOVED || uid == UID_TETHERING) {
1506 collapseKey = uid;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001507 } else {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001508 collapseKey = android.os.Process.SYSTEM_UID;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001509 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001510
1511 AppItem item = knownItems.get(collapseKey);
1512 if (item == null) {
1513 item = new AppItem(collapseKey);
1514 mItems.add(item);
1515 knownItems.put(item.key, item);
1516 }
1517 item.addUid(uid);
1518 item.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001519 }
1520
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001521 for (int uid : restrictedUids) {
1522 // Only splice in restricted state for current user
1523 if (UserHandle.getUserId(uid) != currentUserId) continue;
1524
1525 AppItem item = knownItems.get(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001526 if (item == null) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001527 item = new AppItem(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001528 item.total = -1;
1529 mItems.add(item);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001530 knownItems.put(item.key, item);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001531 }
1532 item.restricted = true;
1533 }
1534
Jeff Sharkey8a503642011-06-10 13:31:21 -07001535 Collections.sort(mItems);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001536 mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001537 notifyDataSetChanged();
1538 }
1539
1540 @Override
1541 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001542 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001543 }
1544
1545 @Override
1546 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001547 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001548 }
1549
1550 @Override
1551 public long getItemId(int position) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001552 return mItems.get(position).key;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001553 }
1554
1555 @Override
1556 public View getView(int position, View convertView, ViewGroup parent) {
1557 if (convertView == null) {
1558 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey5d706792011-09-08 18:57:17 -07001559 R.layout.data_usage_item, parent, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001560
1561 if (mInsetSide > 0) {
Fabrice Di Megliob27223f2013-01-15 18:54:11 -08001562 convertView.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001563 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001564 }
1565
1566 final Context context = parent.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001567
Jeff Sharkey28130d92011-09-02 16:10:24 -07001568 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001569 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1570 android.R.id.progress);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001571
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001572 // kick off async load of app details
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001573 final AppItem item = mItems.get(position);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001574 UidDetailTask.bindView(mProvider, item, convertView);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001575
Jeff Sharkeye557c332012-04-13 16:04:07 -07001576 if (item.restricted && item.total <= 0) {
1577 text1.setText(R.string.data_usage_app_restricted);
1578 progress.setVisibility(View.GONE);
1579 } else {
1580 text1.setText(Formatter.formatFileSize(context, item.total));
1581 progress.setVisibility(View.VISIBLE);
1582 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001583
1584 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1585 progress.setProgress(percentTotal);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001586
1587 return convertView;
1588 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001589 }
1590
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001591 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001592 * Empty {@link Fragment} that controls display of UID details in
1593 * {@link DataUsageSummary}.
1594 */
1595 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001596 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001597
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001598 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001599 if (!parent.isAdded()) return;
1600
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001601 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001602 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001603
1604 final AppDetailsFragment fragment = new AppDetailsFragment();
1605 fragment.setArguments(args);
1606 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001607 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1608 ft.add(fragment, TAG_APP_DETAILS);
1609 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001610 ft.setBreadCrumbTitle(label);
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001611 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001612 }
1613
1614 @Override
1615 public void onStart() {
1616 super.onStart();
1617 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001618 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001619 target.updateBody();
1620 }
1621
1622 @Override
1623 public void onStop() {
1624 super.onStop();
1625 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001626 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001627 target.updateBody();
1628 }
1629 }
1630
1631 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001632 * Dialog to request user confirmation before setting
1633 * {@link NetworkPolicy#limitBytes}.
1634 */
1635 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001636 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001637 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001638
1639 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001640 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001641
Jeff Sharkey461842a2011-09-25 18:22:48 -07001642 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001643 final CharSequence message;
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001644 final long minLimitBytes = (long) (
1645 parent.mPolicyEditor.getPolicy(parent.mTemplate).warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001646 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001647
1648 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001649 final String currentTab = parent.mCurrentTab;
1650 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001651 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001652 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001653 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001654 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001655 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001656 } else if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001657 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001658 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001659 } else {
1660 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001661 }
1662
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001663 final Bundle args = new Bundle();
1664 args.putCharSequence(EXTRA_MESSAGE, message);
1665 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1666
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001667 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1668 dialog.setArguments(args);
1669 dialog.setTargetFragment(parent, 0);
1670 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1671 }
1672
1673 @Override
1674 public Dialog onCreateDialog(Bundle savedInstanceState) {
1675 final Context context = getActivity();
1676
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001677 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001678 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1679
1680 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1681 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001682 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001683
1684 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001685 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001686 public void onClick(DialogInterface dialog, int which) {
1687 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1688 if (target != null) {
1689 target.setPolicyLimitBytes(limitBytes);
1690 }
1691 }
1692 });
1693
1694 return builder.create();
1695 }
1696 }
1697
1698 /**
1699 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1700 */
1701 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001702 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001703
1704 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001705 if (!parent.isAdded()) return;
1706
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001707 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001708 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001709
1710 final CycleEditorFragment dialog = new CycleEditorFragment();
1711 dialog.setArguments(args);
1712 dialog.setTargetFragment(parent, 0);
1713 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1714 }
1715
1716 @Override
1717 public Dialog onCreateDialog(Bundle savedInstanceState) {
1718 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001719 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1720 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001721
1722 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1723 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1724
1725 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1726 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1727
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001728 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1729 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001730
1731 cycleDayPicker.setMinValue(1);
1732 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001733 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001734 cycleDayPicker.setWrapSelectorWheel(true);
1735
1736 builder.setTitle(R.string.data_usage_cycle_editor_title);
1737 builder.setView(view);
1738
1739 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1740 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001741 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001742 public void onClick(DialogInterface dialog, int which) {
Jeff Sharkeyd277de92013-03-25 15:11:25 -07001743 // clear focus to finish pending text edits
1744 cycleDayPicker.clearFocus();
1745
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001746 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001747 final String cycleTimezone = new Time().timezone;
1748 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001749 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001750 }
1751 });
1752
1753 return builder.create();
1754 }
1755 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001756
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001757 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001758 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1759 */
1760 public static class WarningEditorFragment extends DialogFragment {
1761 private static final String EXTRA_TEMPLATE = "template";
1762
1763 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001764 if (!parent.isAdded()) return;
1765
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001766 final Bundle args = new Bundle();
1767 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1768
1769 final WarningEditorFragment dialog = new WarningEditorFragment();
1770 dialog.setArguments(args);
1771 dialog.setTargetFragment(parent, 0);
1772 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1773 }
1774
1775 @Override
1776 public Dialog onCreateDialog(Bundle savedInstanceState) {
1777 final Context context = getActivity();
1778 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1779 final NetworkPolicyEditor editor = target.mPolicyEditor;
1780
1781 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1782 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1783
1784 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1785 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1786
1787 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1788 final long warningBytes = editor.getPolicyWarningBytes(template);
1789 final long limitBytes = editor.getPolicyLimitBytes(template);
1790
1791 bytesPicker.setMinValue(0);
1792 if (limitBytes != LIMIT_DISABLED) {
1793 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1794 } else {
1795 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1796 }
1797 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1798 bytesPicker.setWrapSelectorWheel(false);
1799
1800 builder.setTitle(R.string.data_usage_warning_editor_title);
1801 builder.setView(view);
1802
1803 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1804 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001805 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001806 public void onClick(DialogInterface dialog, int which) {
1807 // clear focus to finish pending text edits
1808 bytesPicker.clearFocus();
1809
1810 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1811 editor.setPolicyWarningBytes(template, bytes);
1812 target.updatePolicy(false);
1813 }
1814 });
1815
1816 return builder.create();
1817 }
1818 }
1819
1820 /**
1821 * Dialog to edit {@link NetworkPolicy#limitBytes}.
1822 */
1823 public static class LimitEditorFragment extends DialogFragment {
1824 private static final String EXTRA_TEMPLATE = "template";
1825
1826 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001827 if (!parent.isAdded()) return;
1828
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001829 final Bundle args = new Bundle();
1830 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1831
1832 final LimitEditorFragment dialog = new LimitEditorFragment();
1833 dialog.setArguments(args);
1834 dialog.setTargetFragment(parent, 0);
1835 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
1836 }
1837
1838 @Override
1839 public Dialog onCreateDialog(Bundle savedInstanceState) {
1840 final Context context = getActivity();
1841 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1842 final NetworkPolicyEditor editor = target.mPolicyEditor;
1843
1844 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1845 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1846
1847 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1848 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1849
1850 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1851 final long warningBytes = editor.getPolicyWarningBytes(template);
1852 final long limitBytes = editor.getPolicyLimitBytes(template);
1853
1854 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09001855 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001856 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
1857 } else {
1858 bytesPicker.setMinValue(0);
1859 }
1860 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
1861 bytesPicker.setWrapSelectorWheel(false);
1862
1863 builder.setTitle(R.string.data_usage_limit_editor_title);
1864 builder.setView(view);
1865
1866 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1867 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001868 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001869 public void onClick(DialogInterface dialog, int which) {
1870 // clear focus to finish pending text edits
1871 bytesPicker.clearFocus();
1872
1873 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1874 editor.setPolicyLimitBytes(template, bytes);
1875 target.updatePolicy(false);
1876 }
1877 });
1878
1879 return builder.create();
1880 }
1881 }
1882 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07001883 * Dialog to request user confirmation before disabling data.
1884 */
1885 public static class ConfirmDataDisableFragment extends DialogFragment {
1886 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001887 if (!parent.isAdded()) return;
1888
Jeff Sharkey28130d92011-09-02 16:10:24 -07001889 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
1890 dialog.setTargetFragment(parent, 0);
1891 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
1892 }
1893
1894 @Override
1895 public Dialog onCreateDialog(Bundle savedInstanceState) {
1896 final Context context = getActivity();
1897
1898 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1899 builder.setMessage(R.string.data_usage_disable_mobile);
1900
1901 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001902 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07001903 public void onClick(DialogInterface dialog, int which) {
1904 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1905 if (target != null) {
1906 // TODO: extend to modify policy enabled flag.
1907 target.setMobileDataEnabled(false);
1908 }
1909 }
1910 });
1911 builder.setNegativeButton(android.R.string.cancel, null);
1912
1913 return builder.create();
1914 }
1915 }
1916
1917 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001918 * Dialog to request user confirmation before setting
Christopher Tate5a64c732012-09-07 13:35:31 -07001919 * {@link android.provider.Settings.Global#DATA_ROAMING}.
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001920 */
1921 public static class ConfirmDataRoamingFragment extends DialogFragment {
1922 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001923 if (!parent.isAdded()) return;
1924
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001925 final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment();
1926 dialog.setTargetFragment(parent, 0);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001927 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_ROAMING);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001928 }
1929
1930 @Override
1931 public Dialog onCreateDialog(Bundle savedInstanceState) {
1932 final Context context = getActivity();
1933
1934 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1935 builder.setTitle(R.string.roaming_reenable_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07001936 if (Utils.hasMultipleUsers(context)) {
1937 builder.setMessage(R.string.roaming_warning_multiuser);
1938 } else {
1939 builder.setMessage(R.string.roaming_warning);
1940 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001941
1942 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001943 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001944 public void onClick(DialogInterface dialog, int which) {
1945 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1946 if (target != null) {
1947 target.setDataRoaming(true);
1948 }
1949 }
1950 });
1951 builder.setNegativeButton(android.R.string.cancel, null);
1952
1953 return builder.create();
1954 }
1955 }
1956
1957 /**
1958 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001959 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001960 */
1961 public static class ConfirmRestrictFragment extends DialogFragment {
1962 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001963 if (!parent.isAdded()) return;
1964
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001965 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1966 dialog.setTargetFragment(parent, 0);
1967 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1968 }
1969
1970 @Override
1971 public Dialog onCreateDialog(Bundle savedInstanceState) {
1972 final Context context = getActivity();
1973
1974 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001975 builder.setTitle(R.string.data_usage_restrict_background_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07001976 if (Utils.hasMultipleUsers(context)) {
1977 builder.setMessage(R.string.data_usage_restrict_background_multiuser);
1978 } else {
1979 builder.setMessage(R.string.data_usage_restrict_background);
1980 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001981
1982 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001983 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001984 public void onClick(DialogInterface dialog, int which) {
1985 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1986 if (target != null) {
1987 target.setRestrictBackground(true);
1988 }
1989 }
1990 });
1991 builder.setNegativeButton(android.R.string.cancel, null);
1992
1993 return builder.create();
1994 }
1995 }
1996
1997 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001998 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
1999 * change has been denied, usually based on
2000 * {@link DataUsageSummary#hasLimitedNetworks()}.
2001 */
2002 public static class DeniedRestrictFragment extends DialogFragment {
2003 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002004 if (!parent.isAdded()) return;
2005
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002006 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
2007 dialog.setTargetFragment(parent, 0);
2008 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
2009 }
2010
2011 @Override
2012 public Dialog onCreateDialog(Bundle savedInstanceState) {
2013 final Context context = getActivity();
2014
2015 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2016 builder.setTitle(R.string.data_usage_app_restrict_background);
2017 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
2018 builder.setPositiveButton(android.R.string.ok, null);
2019
2020 return builder.create();
2021 }
2022 }
2023
2024 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002025 * Dialog to request user confirmation before setting
2026 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
2027 */
2028 public static class ConfirmAppRestrictFragment extends DialogFragment {
2029 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002030 if (!parent.isAdded()) return;
2031
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002032 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2033 dialog.setTargetFragment(parent, 0);
2034 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2035 }
2036
2037 @Override
2038 public Dialog onCreateDialog(Bundle savedInstanceState) {
2039 final Context context = getActivity();
2040
2041 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002042 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2043 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2044
2045 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002046 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002047 public void onClick(DialogInterface dialog, int which) {
2048 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2049 if (target != null) {
2050 target.setAppRestrictBackground(true);
2051 }
2052 }
2053 });
2054 builder.setNegativeButton(android.R.string.cancel, null);
2055
2056 return builder.create();
2057 }
2058 }
2059
2060 /**
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002061 * Dialog to inform user about changing auto-sync setting
2062 */
2063 public static class ConfirmAutoSyncChangeFragment extends DialogFragment {
Amith Yamasani665235f2012-06-07 19:58:34 -07002064 private static final String SAVE_ENABLING = "enabling";
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002065 private boolean mEnabling;
2066
2067 public static void show(DataUsageSummary parent, boolean enabling) {
2068 if (!parent.isAdded()) return;
2069
2070 final ConfirmAutoSyncChangeFragment dialog = new ConfirmAutoSyncChangeFragment();
2071 dialog.mEnabling = enabling;
2072 dialog.setTargetFragment(parent, 0);
2073 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_AUTO_SYNC_CHANGE);
2074 }
2075
2076 @Override
2077 public Dialog onCreateDialog(Bundle savedInstanceState) {
2078 final Context context = getActivity();
Amith Yamasani665235f2012-06-07 19:58:34 -07002079 if (savedInstanceState != null) {
2080 mEnabling = savedInstanceState.getBoolean(SAVE_ENABLING);
2081 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002082
2083 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2084 if (!mEnabling) {
2085 builder.setTitle(R.string.data_usage_auto_sync_off_dialog_title);
2086 builder.setMessage(R.string.data_usage_auto_sync_off_dialog);
2087 } else {
2088 builder.setTitle(R.string.data_usage_auto_sync_on_dialog_title);
2089 builder.setMessage(R.string.data_usage_auto_sync_on_dialog);
2090 }
2091
2092 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
2093 @Override
2094 public void onClick(DialogInterface dialog, int which) {
2095 ContentResolver.setMasterSyncAutomatically(mEnabling);
2096 }
2097 });
2098 builder.setNegativeButton(android.R.string.cancel, null);
2099
2100 return builder.create();
2101 }
Amith Yamasani665235f2012-06-07 19:58:34 -07002102
2103 @Override
2104 public void onSaveInstanceState(Bundle outState) {
2105 super.onSaveInstanceState(outState);
2106 outState.putBoolean(SAVE_ENABLING, mEnabling);
2107 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002108 }
2109
2110 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002111 * Compute default tab that should be selected, based on
2112 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2113 */
2114 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002115 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
2116 if (template == null) return null;
2117
2118 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002119 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002120 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002121 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002122 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002123 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002124 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002125 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002126 return TAB_WIFI;
2127 default:
2128 return null;
2129 }
2130 }
2131
2132 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002133 * Background task that loads {@link UidDetail}, binding to
2134 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002135 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002136 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2137 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002138 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002139 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002140
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002141 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002142 mProvider = checkNotNull(provider);
2143 mItem = checkNotNull(item);
2144 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002145 }
2146
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002147 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002148 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002149 final UidDetailTask existing = (UidDetailTask) target.getTag();
2150 if (existing != null) {
2151 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002152 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002153
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002154 final UidDetail cachedDetail = provider.getUidDetail(item.key, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002155 if (cachedDetail != null) {
2156 bindView(cachedDetail, target);
2157 } else {
2158 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2159 AsyncTask.THREAD_POOL_EXECUTOR));
2160 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002161 }
2162
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002163 private static void bindView(UidDetail detail, View target) {
2164 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2165 final TextView title = (TextView) target.findViewById(android.R.id.title);
2166
2167 if (detail != null) {
2168 icon.setImageDrawable(detail.icon);
2169 title.setText(detail.label);
2170 } else {
2171 icon.setImageDrawable(null);
2172 title.setText(null);
2173 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002174 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002175
2176 @Override
2177 protected void onPreExecute() {
2178 bindView(null, mTarget);
2179 }
2180
2181 @Override
2182 protected UidDetail doInBackground(Void... params) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002183 return mProvider.getUidDetail(mItem.key, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002184 }
2185
2186 @Override
2187 protected void onPostExecute(UidDetail result) {
2188 bindView(result, mTarget);
2189 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002190 }
2191
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002192 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002193 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002194 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002195 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002196 if (TEST_RADIOS) {
2197 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2198 }
2199
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002200 final ConnectivityManager conn = ConnectivityManager.from(context);
2201 final TelephonyManager tele = TelephonyManager.from(context);
2202
2203 // require both supported network and ready SIM
2204 return conn.isNetworkSupported(TYPE_MOBILE) && tele.getSimState() == SIM_STATE_READY;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002205 }
2206
2207 /**
2208 * Test if device has a mobile 4G data radio.
2209 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002210 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002211 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2212 return false;
2213 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002214 if (TEST_RADIOS) {
2215 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2216 }
2217
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002218 final ConnectivityManager conn = ConnectivityManager.from(context);
2219 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002220
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002221 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Wink Saville55434042012-06-14 12:33:43 -07002222 final boolean hasLte = (tele.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE)
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002223 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002224 return hasWimax || hasLte;
2225 }
2226
2227 /**
2228 * Test if device has a Wi-Fi data radio.
2229 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002230 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002231 if (TEST_RADIOS) {
2232 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2233 }
2234
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002235 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002236 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002237 }
2238
2239 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002240 * Test if device has an ethernet network connection.
2241 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002242 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002243 if (TEST_RADIOS) {
2244 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2245 }
2246
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002247 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002248 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2249
2250 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002251 if (mStatsSession != null) {
2252 try {
2253 ethernetBytes = mStatsSession.getSummaryForNetwork(
2254 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2255 .getTotalBytes();
2256 } catch (RemoteException e) {
2257 throw new RuntimeException(e);
2258 }
2259 } else {
2260 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002261 }
2262
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002263 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002264 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002265 }
2266
2267 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002268 * Inflate a {@link Preference} style layout, adding the given {@link View}
2269 * widget into {@link android.R.id#widget_frame}.
2270 */
2271 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2272 final View view = inflater.inflate(R.layout.preference, root, false);
2273 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2274 android.R.id.widget_frame);
2275 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2276 return view;
2277 }
2278
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002279 private static View inflateAppTitle(
2280 LayoutInflater inflater, ViewGroup root, CharSequence label) {
2281 final TextView view = (TextView) inflater.inflate(
2282 R.layout.data_usage_app_title, root, false);
2283 view.setText(label);
2284 return view;
2285 }
2286
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002287 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002288 * Test if any networks are currently limited.
2289 */
2290 private boolean hasLimitedNetworks() {
2291 return !buildLimitedNetworksList().isEmpty();
2292 }
2293
2294 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002295 * Build string describing currently limited networks, which defines when
2296 * background data is restricted.
2297 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002298 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002299 private CharSequence buildLimitedNetworksString() {
2300 final List<CharSequence> limited = buildLimitedNetworksList();
2301
2302 // handle case where no networks limited
2303 if (limited.isEmpty()) {
2304 limited.add(getText(R.string.data_usage_list_none));
2305 }
2306
2307 return TextUtils.join(limited);
2308 }
2309
2310 /**
2311 * Build list of currently limited networks, which defines when background
2312 * data is restricted.
2313 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002314 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002315 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002316 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002317
2318 // build combined list of all limited networks
2319 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002320
2321 final TelephonyManager tele = TelephonyManager.from(context);
2322 if (tele.getSimState() == SIM_STATE_READY) {
2323 final String subscriberId = getActiveSubscriberId(context);
2324 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2325 limited.add(getText(R.string.data_usage_list_mobile));
2326 }
2327 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2328 limited.add(getText(R.string.data_usage_tab_3g));
2329 }
2330 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2331 limited.add(getText(R.string.data_usage_tab_4g));
2332 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002333 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002334
2335 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002336 limited.add(getText(R.string.data_usage_tab_wifi));
2337 }
2338 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2339 limited.add(getText(R.string.data_usage_tab_ethernet));
2340 }
2341
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002342 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002343 }
2344
2345 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002346 * Inset both selector and divider {@link Drawable} on the given
2347 * {@link ListView} by the requested dimensions.
2348 */
2349 private static void insetListViewDrawables(ListView view, int insetSide) {
2350 final Drawable selector = view.getSelector();
2351 final Drawable divider = view.getDivider();
2352
2353 // fully unregister these drawables so callbacks can be maintained after
2354 // wrapping below.
2355 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2356 view.setSelector(stub);
2357 view.setDivider(stub);
2358
2359 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2360 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2361 }
2362
2363 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002364 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002365 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002366 */
2367 private static void setPreferenceTitle(View parent, int resId) {
2368 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2369 title.setText(resId);
2370 }
2371
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002372 /**
2373 * Set {@link android.R.id#summary} for a preference view inflated with
2374 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2375 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002376 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002377 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2378 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002379 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002380 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002381}