blob: 9f680231f7bd60882af6764699b3d00858a9271a [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;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070056import android.app.FragmentTransaction;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070057import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070058import android.content.ContentResolver;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070059import android.content.Context;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070060import android.content.DialogInterface;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -070061import android.content.Intent;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070062import android.content.Loader;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070063import android.content.SharedPreferences;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070064import android.content.pm.PackageManager;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070065import android.content.res.Resources;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070066import android.graphics.Color;
Jeff Sharkey5d706792011-09-08 18:57:17 -070067import android.graphics.drawable.ColorDrawable;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070068import android.graphics.drawable.Drawable;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070069import android.net.ConnectivityManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070070import android.net.INetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070071import android.net.INetworkStatsService;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070072import android.net.INetworkStatsSession;
Jeff Sharkey8a503642011-06-10 13:31:21 -070073import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070074import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070075import android.net.NetworkStats;
76import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070077import android.net.NetworkTemplate;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070078import android.net.TrafficStats;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070079import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070080import android.os.Bundle;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070081import android.os.INetworkManagementService;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070082import android.os.Parcel;
83import android.os.Parcelable;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070084import android.os.RemoteException;
85import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070086import android.os.SystemProperties;
Dianne Hackbornbb06a422012-08-16 11:01:57 -070087import android.os.UserHandle;
Jeff Sharkey8a503642011-06-10 13:31:21 -070088import android.preference.Preference;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070089import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070090import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070091import android.text.format.DateUtils;
92import android.text.format.Formatter;
Jeff Sharkeye5223a02012-03-09 17:11:14 -080093import android.text.format.Time;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070094import android.util.Log;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070095import android.util.SparseArray;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070096import android.util.SparseBooleanArray;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070097import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -070098import android.view.Menu;
99import android.view.MenuInflater;
100import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700101import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700102import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700103import android.view.ViewGroup;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700104import android.widget.AdapterView;
105import android.widget.AdapterView.OnItemClickListener;
106import android.widget.AdapterView.OnItemSelectedListener;
107import android.widget.ArrayAdapter;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700108import android.widget.BaseAdapter;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700109import android.widget.Button;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700110import android.widget.CheckBox;
111import android.widget.CompoundButton;
112import android.widget.CompoundButton.OnCheckedChangeListener;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700113import android.widget.ImageView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700114import android.widget.LinearLayout;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700115import android.widget.ListView;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700116import android.widget.NumberPicker;
Jeff Sharkey2412b0f2011-07-17 20:31:40 -0700117import android.widget.ProgressBar;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700118import android.widget.Spinner;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700119import android.widget.Switch;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700120import android.widget.TabHost;
121import android.widget.TabHost.OnTabChangeListener;
122import android.widget.TabHost.TabContentFactory;
123import android.widget.TabHost.TabSpec;
124import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700125import android.widget.TextView;
126
Wink Saville55434042012-06-14 12:33:43 -0700127import com.android.internal.telephony.PhoneConstants;
Jeff Sharkey5d706792011-09-08 18:57:17 -0700128import com.android.settings.drawable.InsetBoundsDrawable;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700129import com.android.settings.net.ChartData;
130import com.android.settings.net.ChartDataLoader;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700131import com.android.settings.net.DataUsageMeteredSettings;
Jeff Sharkeya662e492011-06-18 21:57:06 -0700132import com.android.settings.net.NetworkPolicyEditor;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700133import com.android.settings.net.SummaryForAllUidLoader;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700134import com.android.settings.net.UidDetail;
135import com.android.settings.net.UidDetailProvider;
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -0700136import com.android.settings.search.BaseSearchIndexProvider;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -0700137import com.android.settings.search.Indexable;
138import com.android.settings.search.SearchIndexableRaw;
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
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700144import libcore.util.Objects;
145
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700146import java.util.ArrayList;
147import java.util.Collections;
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700148import java.util.List;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700149import java.util.Locale;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700150
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 */
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700155public class DataUsageSummary extends HighlightingFragment implements Indexable {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700156 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
Fabrice Di Meglio2169c882014-04-18 15:18:40 -0700184 private static final String DATA_USAGE_ENABLE_MOBILE_KEY = "data_usage_enable_mobile";
185 private static final String DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY =
186 "data_usage_disable_mobile_limit";
187 private static final String DATA_USAGE_CYCLE_KEY = "data_usage_cycle";
188
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700189 private static final int LOADER_CHART_DATA = 2;
190 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700191
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700192 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700193 private INetworkStatsService mStatsService;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700194 private NetworkPolicyManager mPolicyManager;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700195 private ConnectivityManager mConnService;
196
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700197 private INetworkStatsSession mStatsSession;
198
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700199 private static final String PREF_FILE = "data_usage";
200 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700201 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700202
203 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700204
Jeff Sharkey8a503642011-06-10 13:31:21 -0700205 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700206 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700207 private TabWidget mTabWidget;
208 private ListView mListView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700209 private DataUsageAdapter mAdapter;
210
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700211 /** Distance to inset content from sides, when needed. */
212 private int mInsetSide = 0;
213
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700214 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700215
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700216 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700217 private LinearLayout mNetworkSwitches;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700218 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700219 private View mDataEnabledView;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700220 private CheckBox mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700221 private View mDisableAtLimitView;
222
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700223 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700224 private Spinner mCycleSpinner;
225 private CycleAdapter mCycleAdapter;
226
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700227 private ChartDataUsageView mChart;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700228 private TextView mUsageSummary;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700229 private TextView mEmpty;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700230
231 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700232 private ImageView mAppIcon;
233 private ViewGroup mAppTitles;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700234 private PieChartView mAppPieChart;
235 private TextView mAppForeground;
236 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700237 private Button mAppSettings;
238
239 private LinearLayout mAppSwitches;
240 private CheckBox mAppRestrict;
241 private View mAppRestrictView;
242
Jeff Sharkey8a503642011-06-10 13:31:21 -0700243 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700244 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700245
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700246 private NetworkTemplate mTemplate;
247 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700248
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700249 private AppItem mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700250
251 private Intent mAppSettingsIntent;
252
Jeff Sharkeya662e492011-06-18 21:57:06 -0700253 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700254
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700255 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700256 private String mIntentTab = null;
257
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700258 private MenuItem mMenuDataRoaming;
259 private MenuItem mMenuRestrictBackground;
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700260 private MenuItem mMenuAutoSync;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700261
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700262 /** Flag used to ignore listeners during binding. */
263 private boolean mBinding;
264
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700265 private UidDetailProvider mUidDetailProvider;
266
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700267 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700268 public void onCreate(Bundle savedInstanceState) {
269 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700270 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700271
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700272 mNetworkService = INetworkManagementService.Stub.asInterface(
273 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700274 mStatsService = INetworkStatsService.Stub.asInterface(
275 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700276 mPolicyManager = NetworkPolicyManager.from(context);
277 mConnService = ConnectivityManager.from(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700278
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700279 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700280
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700281 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700282 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700283
Jaewan Kimffce9c12013-03-19 16:53:45 +0900284 try {
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700285 if (!mNetworkService.isBandwidthControlEnabled()) {
286 Log.w(TAG, "No bandwidth control; leaving");
287 getActivity().finish();
288 }
289 } catch (RemoteException e) {
290 Log.w(TAG, "No bandwidth control; leaving");
291 getActivity().finish();
292 }
293
294 try {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900295 mStatsSession = mStatsService.openSession();
296 } catch (RemoteException e) {
297 throw new RuntimeException(e);
298 }
299
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700300 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700301 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700302
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700303 // override preferences when no mobile radio
304 if (!hasReadyMobileRadio(context)) {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900305 mShowWifi = true;
306 mShowEthernet = true;
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700307 }
308
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700309 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700310 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700311
Jeff Sharkey8a503642011-06-10 13:31:21 -0700312 @Override
313 public View onCreateView(LayoutInflater inflater, ViewGroup container,
314 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700315
Jeff Sharkey8a503642011-06-10 13:31:21 -0700316 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700317 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
318
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700319 mUidDetailProvider = new UidDetailProvider(context);
320
Jeff Sharkey8a503642011-06-10 13:31:21 -0700321 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700322 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700323 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
324 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700325
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700326 // decide if we need to manually inset our content, or if we should rely
327 // on parent container for inset.
328 final boolean shouldInset = mListView.getScrollBarStyle()
329 == View.SCROLLBARS_OUTSIDE_OVERLAY;
Amith Yamasani56f51a82013-08-05 10:07:23 -0700330 mInsetSide = 0;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700331
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700332 // adjust padding around tabwidget as needed
Amith Yamasani56f51a82013-08-05 10:07:23 -0700333 prepareCustomPreferencesList(container, view, mListView, false);
Jeff Sharkey5d706792011-09-08 18:57:17 -0700334
Jeff Sharkey8a503642011-06-10 13:31:21 -0700335 mTabHost.setup();
336 mTabHost.setOnTabChangedListener(mTabListener);
337
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700338 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700339 mHeader.setClickable(true);
340
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700341 mListView.addHeaderView(new View(context), null, true);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700342 mListView.addHeaderView(mHeader, null, true);
343 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700344
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700345 if (mInsetSide > 0) {
346 // inset selector and divider drawables
347 insetListViewDrawables(mListView, mInsetSide);
Fabrice Di Megliob27223f2013-01-15 18:54:11 -0800348 mHeader.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700349 }
350
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700351 {
352 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700353 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
354 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700355 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700356
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700357 mDataEnabled = new Switch(inflater.getContext());
358 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -0700359 mDataEnabledView.setTag(DATA_USAGE_ENABLE_MOBILE_KEY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700360 mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener);
361 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700362
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700363 mDisableAtLimit = new CheckBox(inflater.getContext());
364 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700365 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700366 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -0700367 mDisableAtLimitView.setTag(DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700368 mDisableAtLimitView.setClickable(true);
369 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700370 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
371 mNetworkSwitches.addView(mDisableAtLimitView);
372 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700373
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700374 // bind cycle dropdown
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700375 mCycleView = mHeader.findViewById(R.id.cycles);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -0700376 mCycleView.setTag(DATA_USAGE_CYCLE_KEY);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700377 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700378 mCycleAdapter = new CycleAdapter(context);
379 mCycleSpinner.setAdapter(mCycleAdapter);
380 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
381
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700382 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700383 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800384 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700385
386 {
387 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700388 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700389 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
390 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700391 mAppPieChart = (PieChartView) mAppDetail.findViewById(R.id.app_pie_chart);
392 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
393 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700394 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700395
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700396 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700397 mAppSettings.setOnClickListener(mAppSettingsListener);
398
399 mAppRestrict = new CheckBox(inflater.getContext());
400 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700401 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700402 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700403 mAppRestrictView.setClickable(true);
404 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700405 mAppRestrictView.setOnClickListener(mAppRestrictListener);
406 mAppSwitches.addView(mAppRestrictView);
407 }
408
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700409 mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700410 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700411
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700412 mAdapter = new DataUsageAdapter(mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700413 mListView.setOnItemClickListener(mListListener);
414 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700415
416 return view;
417 }
418
419 @Override
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700420 public void onViewStateRestored(Bundle savedInstanceState) {
421 super.onViewStateRestored(savedInstanceState);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700422
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700423 // pick default tab based on incoming intent
424 final Intent intent = getActivity().getIntent();
425 mIntentTab = computeTabFromIntent(intent);
426
Jeff Sharkey8a503642011-06-10 13:31:21 -0700427 // this kicks off chain reaction which creates tabs, binds the body to
428 // selected network, and binds chart, cycles and detail list.
429 updateTabs();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700430 }
431
432 @Override
433 public void onResume() {
434 super.onResume();
435
436 getView().post(new Runnable() {
437 @Override
438 public void run() {
439 highlightViewIfNeeded();
440 }
441 });
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700442
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700443 // kick off background task to update stats
444 new AsyncTask<Void, Void, Void>() {
445 @Override
446 protected Void doInBackground(Void... params) {
447 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700448 // wait a few seconds before kicking off
449 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700450 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700451 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700452 } catch (RemoteException e) {
453 }
454 return null;
455 }
456
457 @Override
458 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700459 if (isAdded()) {
460 updateBody();
461 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700462 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700463 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700464 }
465
Jeff Sharkey8a503642011-06-10 13:31:21 -0700466 @Override
467 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
468 inflater.inflate(R.menu.data_usage, menu);
469 }
470
471 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700472 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700473 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700474 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700475 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700476
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700477 mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700478 mMenuDataRoaming.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700479 mMenuDataRoaming.setChecked(getDataRoaming());
480
481 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700482 mMenuRestrictBackground.setVisible(
483 hasReadyMobileRadio(context) && isOwner && !appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700484 mMenuRestrictBackground.setChecked(mPolicyManager.getRestrictBackground());
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700485
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700486 mMenuAutoSync = menu.findItem(R.id.data_usage_menu_auto_sync);
487 mMenuAutoSync.setChecked(ContentResolver.getMasterSyncAutomatically());
Jeff Sharkeyfda48e32012-09-18 15:03:47 -0700488 mMenuAutoSync.setVisible(!appDetailMode);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700489
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700490 final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g);
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700491 split4g.setVisible(hasReadyMobile4gRadio(context) && isOwner && !appDetailMode);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700492 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700493
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700494 final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700495 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700496 showWifi.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700497 showWifi.setChecked(mShowWifi);
498 } else {
499 showWifi.setVisible(false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700500 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700501
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700502 final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700503 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700504 showEthernet.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700505 showEthernet.setChecked(mShowEthernet);
506 } else {
507 showEthernet.setVisible(false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700508 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700509
510 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
511 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700512 metered.setVisible(!appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700513 } else {
514 metered.setVisible(false);
515 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700516
517 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
518 String helpUrl;
519 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800520 HelpUtils.prepareHelpMenuItem(context, help, helpUrl);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700521 } else {
522 help.setVisible(false);
523 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700524 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700525
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700526 @Override
527 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700528 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700529 case R.id.data_usage_menu_roaming: {
530 final boolean dataRoaming = !item.isChecked();
531 if (dataRoaming) {
532 ConfirmDataRoamingFragment.show(this);
533 } else {
534 // no confirmation to disable roaming
535 setDataRoaming(false);
536 }
537 return true;
538 }
539 case R.id.data_usage_menu_restrict_background: {
540 final boolean restrictBackground = !item.isChecked();
541 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800542 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700543 } else {
544 // no confirmation to drop restriction
545 setRestrictBackground(false);
546 }
547 return true;
548 }
549 case R.id.data_usage_menu_split_4g: {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700550 final boolean mobileSplit = !item.isChecked();
Jeff Sharkeya662e492011-06-18 21:57:06 -0700551 setMobilePolicySplit(mobileSplit);
552 item.setChecked(isMobilePolicySplit());
Jeff Sharkey8a503642011-06-10 13:31:21 -0700553 updateTabs();
554 return true;
555 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700556 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700557 mShowWifi = !item.isChecked();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700558 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700559 item.setChecked(mShowWifi);
560 updateTabs();
561 return true;
562 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700563 case R.id.data_usage_menu_show_ethernet: {
564 mShowEthernet = !item.isChecked();
565 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
566 item.setChecked(mShowEthernet);
567 updateTabs();
568 return true;
569 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700570 case R.id.data_usage_menu_metered: {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800571 final SettingsActivity sa = (SettingsActivity) getActivity();
572 sa.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700573 R.string.data_usage_metered_title, null, this, 0);
574 return true;
575 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700576 case R.id.data_usage_menu_auto_sync: {
Guang Zhu167ba2a2012-10-19 17:55:05 -0700577 if (ActivityManager.isUserAMonkey()) {
578 Log.d("SyncState", "ignoring monkey's attempt to flip global sync state");
579 } else {
580 ConfirmAutoSyncChangeFragment.show(this, !item.isChecked());
581 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700582 return true;
583 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700584 }
585 return false;
586 }
587
Jeff Sharkey94a90952011-06-13 22:31:09 -0700588 @Override
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700589 public void onDestroy() {
Jeff Sharkey94a90952011-06-13 22:31:09 -0700590 mDataEnabledView = null;
591 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700592
593 mUidDetailProvider.clearCache();
594 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700595
596 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700597
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800598 super.onDestroy();
599 }
600
Jeff Sharkey8a503642011-06-10 13:31:21 -0700601 /**
Jeff Sharkey92811822012-05-04 11:47:29 -0700602 * Build and assign {@link LayoutTransition} to various containers. Should
603 * only be assigned after initial layout is complete.
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700604 */
Jeff Sharkey92811822012-05-04 11:47:29 -0700605 private void ensureLayoutTransitions() {
606 // skip when already setup
607 if (mChart.getLayoutTransition() != null) return;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700608
Jeff Sharkey92811822012-05-04 11:47:29 -0700609 mTabsContainer.setLayoutTransition(buildLayoutTransition());
610 mHeader.setLayoutTransition(buildLayoutTransition());
611 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700612
Jeff Sharkey92811822012-05-04 11:47:29 -0700613 final LayoutTransition chartTransition = buildLayoutTransition();
614 chartTransition.disableTransitionType(LayoutTransition.APPEARING);
615 chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
616 mChart.setLayoutTransition(chartTransition);
617 }
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700618
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700619 private static LayoutTransition buildLayoutTransition() {
620 final LayoutTransition transition = new LayoutTransition();
621 if (TEST_ANIM) {
622 transition.setDuration(1500);
623 }
624 transition.setAnimateParentHierarchy(false);
625 return transition;
626 }
627
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700628 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700629 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700630 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
631 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700632 */
633 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700634 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700635 mTabHost.clearAllTabs();
636
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700637 final boolean mobileSplit = isMobilePolicySplit();
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700638 if (mobileSplit && hasReadyMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700639 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
640 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700641 } else if (hasReadyMobileRadio(context)) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700642 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700643 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700644 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700645 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
646 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700647 if (mShowEthernet && hasEthernet(context)) {
648 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
649 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700650
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700651 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700652 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
653 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
654 if (mIntentTab != null) {
655 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700656 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700657 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700658 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700659 mTabHost.setCurrentTabByTag(mIntentTab);
660 }
661 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700662 } else if (noTabs) {
663 // no usable tabs, so hide body
664 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700665 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700666 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700667 }
668 }
669
Jeff Sharkey8a503642011-06-10 13:31:21 -0700670 /**
671 * Factory that provide empty {@link View} to make {@link TabHost} happy.
672 */
673 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700674 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700675 public View createTabContent(String tag) {
676 return new View(mTabHost.getContext());
677 }
678 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700679
Jeff Sharkey8a503642011-06-10 13:31:21 -0700680 /**
681 * Build {@link TabSpec} with thin indicator, and empty content.
682 */
683 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700684 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
685 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700686 }
687
688 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700689 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700690 public void onTabChanged(String tabId) {
691 // user changed tab; update body
692 updateBody();
693 }
694 };
695
696 /**
697 * Update body content based on current tab. Loads
698 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
699 * binds them to visible controls.
700 */
701 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700702 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700703 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700704
Jeff Sharkeya662e492011-06-18 21:57:06 -0700705 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700706 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700707 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700708
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700709 if (currentTab == null) {
710 Log.w(TAG, "no tab selected; hiding body");
711 mListView.setVisibility(View.GONE);
712 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700713 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700714 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700715 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700716
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700717 mCurrentTab = currentTab;
718
Jeff Sharkey8a503642011-06-10 13:31:21 -0700719 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
720
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700721 mDataEnabledView.setVisibility(isOwner ? View.VISIBLE : View.GONE);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700722
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700723 // TODO: remove mobile tabs when SIM isn't ready
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700724
Jeff Sharkey8a503642011-06-10 13:31:21 -0700725 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700726 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
727 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700728 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700729
730 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700731 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
732 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
733 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700734 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700735
736 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700737 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
738 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
739 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700740 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700741
742 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700743 // wifi doesn't have any controls
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700744 mDataEnabledView.setVisibility(View.GONE);
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700745 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700746 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700747
748 } else if (TAB_ETHERNET.equals(currentTab)) {
749 // ethernet doesn't have any controls
750 mDataEnabledView.setVisibility(View.GONE);
751 mDisableAtLimitView.setVisibility(View.GONE);
752 mTemplate = buildTemplateEthernet();
753
754 } else {
755 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700756 }
757
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700758 // kick off loader for network history
759 // TODO: consider chaining two loaders together instead of reloading
760 // network history when showing app detail.
761 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700762 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700763
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700764 // detail mode can change visible menus, invalidate
765 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700766
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700767 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700768 }
769
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700770 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700771 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700772 }
773
774 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700775 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700776 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700777 */
778 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700779 final Context context = getActivity();
780 final PackageManager pm = context.getPackageManager();
781 final LayoutInflater inflater = getActivity().getLayoutInflater();
782
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700783 if (isAppDetailMode()) {
784 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700785 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700786 } else {
787 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700788 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700789
790 // hide detail stats when not in detail mode
791 mChart.bindDetailNetworkStats(null);
792 return;
793 }
794
795 // remove warning/limit sweeps while in detail mode
796 mChart.bindNetworkPolicy(null);
797
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700798 // show icon and all labels appearing under this app
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700799 final int uid = mCurrentApp.key;
800 final UidDetail detail = mUidDetailProvider.getUidDetail(uid, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700801 mAppIcon.setImageDrawable(detail.icon);
802
803 mAppTitles.removeAllViews();
804 if (detail.detailLabels != null) {
805 for (CharSequence label : detail.detailLabels) {
806 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label));
807 }
808 } else {
809 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label));
810 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700811
812 // enable settings button when package provides it
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700813 final String[] packageNames = pm.getPackagesForUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700814 if (packageNames != null && packageNames.length > 0) {
815 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700816 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
817
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700818 // Search for match across all packages
819 boolean matchFound = false;
820 for (String packageName : packageNames) {
821 mAppSettingsIntent.setPackage(packageName);
822 if (pm.resolveActivity(mAppSettingsIntent, 0) != null) {
823 matchFound = true;
824 break;
825 }
826 }
827
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700828 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -0700829 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700830
831 } else {
832 mAppSettingsIntent = null;
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700833 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700834 }
835
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700836 updateDetailData();
837
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700838 if (UserHandle.isApp(uid) && !mPolicyManager.getRestrictBackground()
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700839 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700840 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800841 setPreferenceSummary(mAppRestrictView,
842 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700843
844 mAppRestrictView.setVisibility(View.VISIBLE);
845 mAppRestrict.setChecked(getAppRestrictBackground());
846
847 } else {
848 mAppRestrictView.setVisibility(View.GONE);
849 }
850 }
851
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700852 private void setPolicyWarningBytes(long warningBytes) {
853 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700854 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700855 updatePolicy(false);
856 }
857
858 private void setPolicyLimitBytes(long limitBytes) {
859 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700860 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700861 updatePolicy(false);
862 }
863
Jeff Sharkey28130d92011-09-02 16:10:24 -0700864 /**
865 * Local cache of value, used to work around delay when
866 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
867 */
868 private Boolean mMobileDataEnabled;
869
870 private boolean isMobileDataEnabled() {
871 if (mMobileDataEnabled != null) {
872 // TODO: deprecate and remove this once enabled flag is on policy
873 return mMobileDataEnabled;
874 } else {
875 return mConnService.getMobileDataEnabled();
876 }
877 }
878
879 private void setMobileDataEnabled(boolean enabled) {
880 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
881 mConnService.setMobileDataEnabled(enabled);
882 mMobileDataEnabled = enabled;
883 updatePolicy(false);
884 }
885
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700886 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700887 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked()
888 && ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700889 }
890
891 private boolean isBandwidthControlEnabled() {
892 try {
893 return mNetworkService.isBandwidthControlEnabled();
894 } catch (RemoteException e) {
895 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
896 return false;
897 }
898 }
899
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700900 private boolean getDataRoaming() {
901 final ContentResolver resolver = getActivity().getContentResolver();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800902 return android.provider.Settings.Global.getInt(resolver,
903 android.provider.Settings.Global.DATA_ROAMING, 0) != 0;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700904 }
905
906 private void setDataRoaming(boolean enabled) {
907 // TODO: teach telephony DataConnectionTracker to watch and apply
908 // updates when changed.
909 final ContentResolver resolver = getActivity().getContentResolver();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800910 android.provider.Settings.Global.putInt(resolver,
911 android.provider.Settings.Global.DATA_ROAMING, enabled ? 1 : 0);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700912 mMenuDataRoaming.setChecked(enabled);
913 }
914
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700915 public void setRestrictBackground(boolean restrictBackground) {
916 mPolicyManager.setRestrictBackground(restrictBackground);
917 mMenuRestrictBackground.setChecked(restrictBackground);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700918 }
919
920 private boolean getAppRestrictBackground() {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700921 final int uid = mCurrentApp.key;
922 final int uidPolicy = mPolicyManager.getUidPolicy(uid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700923 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
924 }
925
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700926 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700927 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700928 final int uid = mCurrentApp.key;
929 mPolicyManager.setUidPolicy(
930 uid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700931 mAppRestrict.setChecked(restrictBackground);
932 }
933
Jeff Sharkey8a503642011-06-10 13:31:21 -0700934 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700935 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
936 * current {@link #mTemplate}.
937 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700938 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700939 if (isAppDetailMode()) {
940 mNetworkSwitches.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700941 } else {
942 mNetworkSwitches.setVisibility(View.VISIBLE);
943 }
944
Jeff Sharkey28130d92011-09-02 16:10:24 -0700945 // TODO: move enabled state directly into policy
946 if (TAB_MOBILE.equals(mCurrentTab)) {
947 mBinding = true;
948 mDataEnabled.setChecked(isMobileDataEnabled());
949 mBinding = false;
950 }
951
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700952 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
953 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700954 mDisableAtLimitView.setVisibility(View.VISIBLE);
955 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700956 if (!isAppDetailMode()) {
957 mChart.bindNetworkPolicy(policy);
958 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700959
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700960 } else {
961 // controls are disabled; don't bind warning/limit sweeps
962 mDisableAtLimitView.setVisibility(View.GONE);
963 mChart.bindNetworkPolicy(null);
964 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700965
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700966 if (refreshCycle) {
967 // generate cycle list based on policy and available history
968 updateCycleList(policy);
969 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700970 }
971
972 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700973 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
974 * and available {@link NetworkStatsHistory} data. Always selects the newest
975 * item, updating the inspection range on {@link #mChart}.
976 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700977 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700978 // stash away currently selected cycle to try restoring below
979 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700980 mCycleAdapter.clear();
981
982 final Context context = mCycleSpinner.getContext();
983
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700984 long historyStart = Long.MAX_VALUE;
985 long historyEnd = Long.MIN_VALUE;
986 if (mChartData != null) {
987 historyStart = mChartData.network.getStart();
988 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700989 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700990
Jeff Sharkey461842a2011-09-25 18:22:48 -0700991 final long now = System.currentTimeMillis();
992 if (historyStart == Long.MAX_VALUE) historyStart = now;
993 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700994
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700995 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700996 if (policy != null) {
997 // find the next cycle boundary
998 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700999
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001000 // walk backwards, generating all valid cycle ranges
1001 while (cycleEnd > historyStart) {
1002 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
1003 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
1004 + historyStart);
1005 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1006 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001007 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001008 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001009
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001010 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001011 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001012 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001013
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001014 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001015 // no policy defined cycles; show entry for each four-week period
1016 long cycleEnd = historyEnd;
1017 while (cycleEnd > historyStart) {
1018 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
1019 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1020 cycleEnd = cycleStart;
1021 }
1022
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001023 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001024 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001025
1026 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001027 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001028 final int position = mCycleAdapter.findNearestPosition(previousItem);
1029 mCycleSpinner.setSelection(position);
1030
1031 // only force-update cycle when changed; skipping preserves any
1032 // user-defined inspection region.
1033 final CycleItem selectedItem = mCycleAdapter.getItem(position);
1034 if (!Objects.equal(selectedItem, previousItem)) {
1035 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1036 } else {
1037 // but still kick off loader for detailed list
1038 updateDetailData();
1039 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001040 } else {
1041 updateDetailData();
1042 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001043 }
1044
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001045 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001046 @Override
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001047 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
1048 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001049
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001050 final boolean dataEnabled = isChecked;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001051 final String currentTab = mCurrentTab;
1052 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001053 if (dataEnabled) {
1054 setMobileDataEnabled(true);
1055 } else {
1056 // disabling data; show confirmation dialog which eventually
1057 // calls setMobileDataEnabled() once user confirms.
1058 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1059 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001060 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001061
Jeff Sharkey28130d92011-09-02 16:10:24 -07001062 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001063 }
1064 };
1065
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001066 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001067 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001068 public void onClick(View v) {
1069 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001070 if (disableAtLimit) {
1071 // enabling limit; show confirmation dialog which eventually
1072 // calls setPolicyLimitBytes() once user confirms.
1073 ConfirmLimitFragment.show(DataUsageSummary.this);
1074 } else {
1075 setPolicyLimitBytes(LIMIT_DISABLED);
1076 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001077 }
1078 };
1079
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001080 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001081 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001082 public void onClick(View v) {
1083 final boolean restrictBackground = !mAppRestrict.isChecked();
1084
1085 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001086 // enabling restriction; show confirmation dialog which
1087 // eventually calls setRestrictBackground() once user
1088 // confirms.
1089 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001090 } else {
1091 setAppRestrictBackground(false);
1092 }
1093 }
1094 };
1095
1096 private OnClickListener mAppSettingsListener = new OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001097 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001098 public void onClick(View v) {
Jeff Sharkeyd92e0412012-04-24 11:35:43 -07001099 if (!isAdded()) return;
1100
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001101 // TODO: target torwards entire UID instead of just first package
1102 startActivity(mAppSettingsIntent);
1103 }
1104 };
1105
Jeff Sharkey8a503642011-06-10 13:31:21 -07001106 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001107 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001108 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001109 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001110 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001111
1112 // TODO: sigh, remove this hack once we understand 6450986
1113 if (mUidDetailProvider == null || app == null) return;
1114
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001115 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001116 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001117 }
1118 };
1119
1120 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001121 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001122 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1123 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1124 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001125 // show cycle editor; will eventually call setPolicyCycleDay()
1126 // when user finishes editing.
1127 CycleEditorFragment.show(DataUsageSummary.this);
1128
1129 // reset spinner to something other than "change cycle..."
1130 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001131
1132 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001133 if (LOGD) {
1134 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1135 + cycle.end + "]");
1136 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001137
1138 // update chart to show selected cycle, and update detail data
1139 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001140 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001141
1142 updateDetailData();
1143 }
1144 }
1145
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001146 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001147 public void onNothingSelected(AdapterView<?> parent) {
1148 // ignored
1149 }
1150 };
1151
1152 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001153 * Update details based on {@link #mChart} inspection range depending on
1154 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1155 * of applications data usage, and when {@link #isAppDetailMode()} update
1156 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001157 */
1158 private void updateDetailData() {
1159 if (LOGD) Log.d(TAG, "updateDetailData()");
1160
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001161 final long start = mChart.getInspectStart();
1162 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001163 final long now = System.currentTimeMillis();
1164
1165 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001166
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001167 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001168 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001169 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001170 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001171 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001172 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001173 final long foregroundBytes = entry.rxBytes + entry.txBytes;
1174
1175 mAppPieChart.setOriginAngle(175);
1176
1177 mAppPieChart.removeAllSlices();
1178 mAppPieChart.addSlice(foregroundBytes, Color.parseColor("#d88d3a"));
1179 mAppPieChart.addSlice(defaultBytes, Color.parseColor("#666666"));
1180
1181 mAppPieChart.generatePath();
1182
1183 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1184 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1185
1186 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001187 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001188
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001189 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001190
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001191 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001192 if (mChartData != null) {
1193 entry = mChartData.network.getValues(start, end, now, null);
1194 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001195
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001196 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001197 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001198 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001199 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001200
1201 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1202 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001203 final String rangePhrase = formatDateRange(context, start, end);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001204
Jeff Sharkeye557c332012-04-13 16:04:07 -07001205 final int summaryRes;
László Dávid0b8bf4e2012-10-24 23:31:47 +02001206 if (TAB_MOBILE.equals(mCurrentTab) || TAB_3G.equals(mCurrentTab)
1207 || TAB_4G.equals(mCurrentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001208 summaryRes = R.string.data_usage_total_during_range_mobile;
1209 } else {
1210 summaryRes = R.string.data_usage_total_during_range;
1211 }
1212
1213 mUsageSummary.setText(getString(summaryRes, totalPhrase, rangePhrase));
Jeff Sharkey92811822012-05-04 11:47:29 -07001214
1215 // initial layout is finished above, ensure we have transitions
1216 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001217 }
1218
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001219 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1220 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001221 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001222 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001223 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001224 }
1225
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001226 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001227 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1228 mChartData = data;
1229 mChart.bindNetworkStats(mChartData.network);
1230 mChart.bindDetailNetworkStats(mChartData.detail);
1231
1232 // calcuate policy cycles based on available data
1233 updatePolicy(true);
1234 updateAppDetail();
1235
1236 // force scroll to top of body when showing detail
1237 if (mChartData.detail != null) {
1238 mListView.smoothScrollToPosition(0);
1239 }
1240 }
1241
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001242 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001243 public void onLoaderReset(Loader<ChartData> loader) {
1244 mChartData = null;
1245 mChart.bindNetworkStats(null);
1246 mChart.bindDetailNetworkStats(null);
1247 }
1248 };
1249
1250 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001251 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001252 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001253 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001254 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001255 }
1256
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001257 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001258 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001259 final int[] restrictedUids = mPolicyManager.getUidsWithPolicy(
Jeff Sharkeye557c332012-04-13 16:04:07 -07001260 POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001261 mAdapter.bindStats(data, restrictedUids);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001262 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001263 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001264
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001265 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001266 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001267 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001268 updateEmptyVisible();
1269 }
1270
1271 private void updateEmptyVisible() {
1272 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1273 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001274 }
1275 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001276
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001277 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001278 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001279 final Context context = getActivity();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001280 if (hasReadyMobileRadio(context)) {
1281 final TelephonyManager tele = TelephonyManager.from(context);
1282 return mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001283 } else {
1284 return false;
1285 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001286 }
1287
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001288 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001289 private void setMobilePolicySplit(boolean split) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001290 final Context context = getActivity();
1291 if (hasReadyMobileRadio(context)) {
1292 final TelephonyManager tele = TelephonyManager.from(context);
1293 mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split);
1294 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001295 }
1296
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001297 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001298 final TelephonyManager tele = TelephonyManager.from(context);
1299 final String actualSubscriberId = tele.getSubscriberId();
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -08001300 return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001301 }
1302
Jeff Sharkey8a503642011-06-10 13:31:21 -07001303 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001304 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001305 public void onInspectRangeChanged() {
1306 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
1307 updateDetailData();
1308 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001309
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001310 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001311 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001312 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001313 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001314
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001315 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001316 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001317 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001318 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001319
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001320 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001321 public void requestWarningEdit() {
1322 WarningEditorFragment.show(DataUsageSummary.this);
1323 }
1324
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001325 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001326 public void requestLimitEdit() {
1327 LimitEditorFragment.show(DataUsageSummary.this);
1328 }
1329 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001330
1331 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001332 * List item that reflects a specific data usage cycle.
1333 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001334 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001335 public CharSequence label;
1336 public long start;
1337 public long end;
1338
Jeff Sharkey8a503642011-06-10 13:31:21 -07001339 CycleItem(CharSequence label) {
1340 this.label = label;
1341 }
1342
1343 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001344 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001345 this.start = start;
1346 this.end = end;
1347 }
1348
Jeff Sharkey8a503642011-06-10 13:31:21 -07001349 @Override
1350 public String toString() {
1351 return label.toString();
1352 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001353
1354 @Override
1355 public boolean equals(Object o) {
1356 if (o instanceof CycleItem) {
1357 final CycleItem another = (CycleItem) o;
1358 return start == another.start && end == another.end;
1359 }
1360 return false;
1361 }
1362
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001363 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001364 public int compareTo(CycleItem another) {
1365 return Long.compare(start, another.start);
1366 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001367 }
1368
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001369 private static final StringBuilder sBuilder = new StringBuilder(50);
1370 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1371 sBuilder, Locale.getDefault());
1372
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001373 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001374 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001375
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001376 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001377 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001378 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1379 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001380 }
1381 }
1382
Jeff Sharkey8a503642011-06-10 13:31:21 -07001383 /**
1384 * Special-case data usage cycle that triggers dialog to change
1385 * {@link NetworkPolicy#cycleDay}.
1386 */
1387 public static class CycleChangeItem extends CycleItem {
1388 public CycleChangeItem(Context context) {
1389 super(context.getString(R.string.data_usage_change_cycle));
1390 }
1391 }
1392
1393 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001394 private boolean mChangePossible = false;
1395 private boolean mChangeVisible = false;
1396
1397 private final CycleChangeItem mChangeItem;
1398
Jeff Sharkey8a503642011-06-10 13:31:21 -07001399 public CycleAdapter(Context context) {
1400 super(context, android.R.layout.simple_spinner_item);
1401 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001402 mChangeItem = new CycleChangeItem(context);
1403 }
1404
1405 public void setChangePossible(boolean possible) {
1406 mChangePossible = possible;
1407 updateChange();
1408 }
1409
1410 public void setChangeVisible(boolean visible) {
1411 mChangeVisible = visible;
1412 updateChange();
1413 }
1414
1415 private void updateChange() {
1416 remove(mChangeItem);
1417 if (mChangePossible && mChangeVisible) {
1418 add(mChangeItem);
1419 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001420 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001421
1422 /**
1423 * Find position of {@link CycleItem} in this adapter which is nearest
1424 * the given {@link CycleItem}.
1425 */
1426 public int findNearestPosition(CycleItem target) {
1427 if (target != null) {
1428 final int count = getCount();
1429 for (int i = count - 1; i >= 0; i--) {
1430 final CycleItem item = getItem(i);
1431 if (item instanceof CycleChangeItem) {
1432 continue;
1433 } else if (item.compareTo(target) >= 0) {
1434 return i;
1435 }
1436 }
1437 }
1438 return 0;
1439 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001440 }
1441
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001442 public static class AppItem implements Comparable<AppItem>, Parcelable {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001443 public final int key;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001444 public boolean restricted;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001445 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001446 public long total;
1447
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001448 public AppItem(int key) {
1449 this.key = key;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001450 }
1451
1452 public AppItem(Parcel parcel) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001453 key = parcel.readInt();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001454 uids = parcel.readSparseBooleanArray();
1455 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001456 }
1457
1458 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001459 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001460 }
1461
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001462 @Override
1463 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001464 dest.writeInt(key);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001465 dest.writeSparseBooleanArray(uids);
1466 dest.writeLong(total);
1467 }
1468
1469 @Override
1470 public int describeContents() {
1471 return 0;
1472 }
1473
1474 @Override
1475 public int compareTo(AppItem another) {
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001476 return Long.compare(another.total, total);
1477 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001478
1479 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1480 @Override
1481 public AppItem createFromParcel(Parcel in) {
1482 return new AppItem(in);
1483 }
1484
1485 @Override
1486 public AppItem[] newArray(int size) {
1487 return new AppItem[size];
1488 }
1489 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001490 }
1491
Jeff Sharkey8a503642011-06-10 13:31:21 -07001492 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001493 * Adapter of applications, sorted by total usage descending.
1494 */
1495 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001496 private final UidDetailProvider mProvider;
1497 private final int mInsetSide;
1498
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001499 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001500 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001501
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001502 public DataUsageAdapter(UidDetailProvider provider, int insetSide) {
1503 mProvider = checkNotNull(provider);
1504 mInsetSide = insetSide;
1505 }
1506
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001507 /**
1508 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1509 */
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001510 public void bindStats(NetworkStats stats, int[] restrictedUids) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001511 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001512
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001513 final int currentUserId = ActivityManager.getCurrentUser();
1514 final SparseArray<AppItem> knownItems = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001515
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001516 NetworkStats.Entry entry = null;
1517 final int size = stats != null ? stats.size() : 0;
1518 for (int i = 0; i < size; i++) {
1519 entry = stats.getValues(i, entry);
1520
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001521 // Decide how to collapse items together
1522 final int uid = entry.uid;
1523 final int collapseKey;
1524 if (UserHandle.isApp(uid)) {
1525 if (UserHandle.getUserId(uid) == currentUserId) {
1526 collapseKey = uid;
1527 } else {
1528 collapseKey = UidDetailProvider.buildKeyForUser(UserHandle.getUserId(uid));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001529 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001530 } else if (uid == UID_REMOVED || uid == UID_TETHERING) {
1531 collapseKey = uid;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001532 } else {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001533 collapseKey = android.os.Process.SYSTEM_UID;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001534 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001535
1536 AppItem item = knownItems.get(collapseKey);
1537 if (item == null) {
1538 item = new AppItem(collapseKey);
1539 mItems.add(item);
1540 knownItems.put(item.key, item);
1541 }
1542 item.addUid(uid);
1543 item.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001544 }
1545
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001546 for (int uid : restrictedUids) {
1547 // Only splice in restricted state for current user
1548 if (UserHandle.getUserId(uid) != currentUserId) continue;
1549
1550 AppItem item = knownItems.get(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001551 if (item == null) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001552 item = new AppItem(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001553 item.total = -1;
1554 mItems.add(item);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001555 knownItems.put(item.key, item);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001556 }
1557 item.restricted = true;
1558 }
1559
Jeff Sharkey8a503642011-06-10 13:31:21 -07001560 Collections.sort(mItems);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001561 mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001562 notifyDataSetChanged();
1563 }
1564
1565 @Override
1566 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001567 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001568 }
1569
1570 @Override
1571 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001572 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001573 }
1574
1575 @Override
1576 public long getItemId(int position) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001577 return mItems.get(position).key;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001578 }
1579
1580 @Override
1581 public View getView(int position, View convertView, ViewGroup parent) {
1582 if (convertView == null) {
1583 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey5d706792011-09-08 18:57:17 -07001584 R.layout.data_usage_item, parent, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001585
1586 if (mInsetSide > 0) {
Fabrice Di Megliob27223f2013-01-15 18:54:11 -08001587 convertView.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001588 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001589 }
1590
1591 final Context context = parent.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001592
Jeff Sharkey28130d92011-09-02 16:10:24 -07001593 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001594 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1595 android.R.id.progress);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001596
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001597 // kick off async load of app details
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001598 final AppItem item = mItems.get(position);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001599 UidDetailTask.bindView(mProvider, item, convertView);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001600
Jeff Sharkeye557c332012-04-13 16:04:07 -07001601 if (item.restricted && item.total <= 0) {
1602 text1.setText(R.string.data_usage_app_restricted);
1603 progress.setVisibility(View.GONE);
1604 } else {
1605 text1.setText(Formatter.formatFileSize(context, item.total));
1606 progress.setVisibility(View.VISIBLE);
1607 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001608
1609 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1610 progress.setProgress(percentTotal);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001611
1612 return convertView;
1613 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001614 }
1615
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001616 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001617 * Empty {@link Fragment} that controls display of UID details in
1618 * {@link DataUsageSummary}.
1619 */
1620 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001621 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001622
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001623 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001624 if (!parent.isAdded()) return;
1625
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001626 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001627 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001628
1629 final AppDetailsFragment fragment = new AppDetailsFragment();
1630 fragment.setArguments(args);
1631 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001632 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1633 ft.add(fragment, TAG_APP_DETAILS);
1634 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001635 ft.setBreadCrumbTitle(label);
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001636 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001637 }
1638
1639 @Override
1640 public void onStart() {
1641 super.onStart();
1642 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001643 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001644 target.updateBody();
1645 }
1646
1647 @Override
1648 public void onStop() {
1649 super.onStop();
1650 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001651 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001652 target.updateBody();
1653 }
1654 }
1655
1656 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001657 * Dialog to request user confirmation before setting
1658 * {@link NetworkPolicy#limitBytes}.
1659 */
1660 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001661 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001662 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001663
1664 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001665 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001666
Jeff Sharkey461842a2011-09-25 18:22:48 -07001667 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001668 final CharSequence message;
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001669 final long minLimitBytes = (long) (
1670 parent.mPolicyEditor.getPolicy(parent.mTemplate).warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001671 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001672
1673 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001674 final String currentTab = parent.mCurrentTab;
1675 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001676 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001677 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001678 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001679 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001680 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001681 } else if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001682 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001683 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001684 } else {
1685 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001686 }
1687
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001688 final Bundle args = new Bundle();
1689 args.putCharSequence(EXTRA_MESSAGE, message);
1690 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1691
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001692 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1693 dialog.setArguments(args);
1694 dialog.setTargetFragment(parent, 0);
1695 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1696 }
1697
1698 @Override
1699 public Dialog onCreateDialog(Bundle savedInstanceState) {
1700 final Context context = getActivity();
1701
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001702 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001703 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1704
1705 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1706 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001707 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001708
1709 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001710 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001711 public void onClick(DialogInterface dialog, int which) {
1712 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1713 if (target != null) {
1714 target.setPolicyLimitBytes(limitBytes);
1715 }
1716 }
1717 });
1718
1719 return builder.create();
1720 }
1721 }
1722
1723 /**
1724 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1725 */
1726 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001727 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001728
1729 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001730 if (!parent.isAdded()) return;
1731
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001732 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001733 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001734
1735 final CycleEditorFragment dialog = new CycleEditorFragment();
1736 dialog.setArguments(args);
1737 dialog.setTargetFragment(parent, 0);
1738 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1739 }
1740
1741 @Override
1742 public Dialog onCreateDialog(Bundle savedInstanceState) {
1743 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001744 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1745 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001746
1747 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1748 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1749
1750 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1751 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1752
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001753 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1754 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001755
1756 cycleDayPicker.setMinValue(1);
1757 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001758 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001759 cycleDayPicker.setWrapSelectorWheel(true);
1760
1761 builder.setTitle(R.string.data_usage_cycle_editor_title);
1762 builder.setView(view);
1763
1764 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1765 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001766 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001767 public void onClick(DialogInterface dialog, int which) {
Jeff Sharkeyd277de92013-03-25 15:11:25 -07001768 // clear focus to finish pending text edits
1769 cycleDayPicker.clearFocus();
1770
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001771 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001772 final String cycleTimezone = new Time().timezone;
1773 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001774 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001775 }
1776 });
1777
1778 return builder.create();
1779 }
1780 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001781
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001782 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001783 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1784 */
1785 public static class WarningEditorFragment extends DialogFragment {
1786 private static final String EXTRA_TEMPLATE = "template";
1787
1788 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001789 if (!parent.isAdded()) return;
1790
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001791 final Bundle args = new Bundle();
1792 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1793
1794 final WarningEditorFragment dialog = new WarningEditorFragment();
1795 dialog.setArguments(args);
1796 dialog.setTargetFragment(parent, 0);
1797 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1798 }
1799
1800 @Override
1801 public Dialog onCreateDialog(Bundle savedInstanceState) {
1802 final Context context = getActivity();
1803 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1804 final NetworkPolicyEditor editor = target.mPolicyEditor;
1805
1806 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1807 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1808
1809 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1810 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1811
1812 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1813 final long warningBytes = editor.getPolicyWarningBytes(template);
1814 final long limitBytes = editor.getPolicyLimitBytes(template);
1815
1816 bytesPicker.setMinValue(0);
1817 if (limitBytes != LIMIT_DISABLED) {
1818 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1819 } else {
1820 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1821 }
1822 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1823 bytesPicker.setWrapSelectorWheel(false);
1824
1825 builder.setTitle(R.string.data_usage_warning_editor_title);
1826 builder.setView(view);
1827
1828 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1829 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001830 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001831 public void onClick(DialogInterface dialog, int which) {
1832 // clear focus to finish pending text edits
1833 bytesPicker.clearFocus();
1834
1835 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1836 editor.setPolicyWarningBytes(template, bytes);
1837 target.updatePolicy(false);
1838 }
1839 });
1840
1841 return builder.create();
1842 }
1843 }
1844
1845 /**
1846 * Dialog to edit {@link NetworkPolicy#limitBytes}.
1847 */
1848 public static class LimitEditorFragment extends DialogFragment {
1849 private static final String EXTRA_TEMPLATE = "template";
1850
1851 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001852 if (!parent.isAdded()) return;
1853
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001854 final Bundle args = new Bundle();
1855 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1856
1857 final LimitEditorFragment dialog = new LimitEditorFragment();
1858 dialog.setArguments(args);
1859 dialog.setTargetFragment(parent, 0);
1860 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
1861 }
1862
1863 @Override
1864 public Dialog onCreateDialog(Bundle savedInstanceState) {
1865 final Context context = getActivity();
1866 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1867 final NetworkPolicyEditor editor = target.mPolicyEditor;
1868
1869 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1870 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1871
1872 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1873 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1874
1875 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1876 final long warningBytes = editor.getPolicyWarningBytes(template);
1877 final long limitBytes = editor.getPolicyLimitBytes(template);
1878
1879 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09001880 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001881 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
1882 } else {
1883 bytesPicker.setMinValue(0);
1884 }
1885 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
1886 bytesPicker.setWrapSelectorWheel(false);
1887
1888 builder.setTitle(R.string.data_usage_limit_editor_title);
1889 builder.setView(view);
1890
1891 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1892 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001893 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001894 public void onClick(DialogInterface dialog, int which) {
1895 // clear focus to finish pending text edits
1896 bytesPicker.clearFocus();
1897
1898 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1899 editor.setPolicyLimitBytes(template, bytes);
1900 target.updatePolicy(false);
1901 }
1902 });
1903
1904 return builder.create();
1905 }
1906 }
1907 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07001908 * Dialog to request user confirmation before disabling data.
1909 */
1910 public static class ConfirmDataDisableFragment extends DialogFragment {
1911 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001912 if (!parent.isAdded()) return;
1913
Jeff Sharkey28130d92011-09-02 16:10:24 -07001914 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
1915 dialog.setTargetFragment(parent, 0);
1916 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
1917 }
1918
1919 @Override
1920 public Dialog onCreateDialog(Bundle savedInstanceState) {
1921 final Context context = getActivity();
1922
1923 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1924 builder.setMessage(R.string.data_usage_disable_mobile);
1925
1926 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001927 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07001928 public void onClick(DialogInterface dialog, int which) {
1929 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1930 if (target != null) {
1931 // TODO: extend to modify policy enabled flag.
1932 target.setMobileDataEnabled(false);
1933 }
1934 }
1935 });
1936 builder.setNegativeButton(android.R.string.cancel, null);
1937
1938 return builder.create();
1939 }
1940 }
1941
1942 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001943 * Dialog to request user confirmation before setting
Christopher Tate5a64c732012-09-07 13:35:31 -07001944 * {@link android.provider.Settings.Global#DATA_ROAMING}.
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001945 */
1946 public static class ConfirmDataRoamingFragment extends DialogFragment {
1947 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001948 if (!parent.isAdded()) return;
1949
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001950 final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment();
1951 dialog.setTargetFragment(parent, 0);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001952 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_ROAMING);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001953 }
1954
1955 @Override
1956 public Dialog onCreateDialog(Bundle savedInstanceState) {
1957 final Context context = getActivity();
1958
1959 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1960 builder.setTitle(R.string.roaming_reenable_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07001961 if (Utils.hasMultipleUsers(context)) {
1962 builder.setMessage(R.string.roaming_warning_multiuser);
1963 } else {
1964 builder.setMessage(R.string.roaming_warning);
1965 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001966
1967 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001968 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001969 public void onClick(DialogInterface dialog, int which) {
1970 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1971 if (target != null) {
1972 target.setDataRoaming(true);
1973 }
1974 }
1975 });
1976 builder.setNegativeButton(android.R.string.cancel, null);
1977
1978 return builder.create();
1979 }
1980 }
1981
1982 /**
1983 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001984 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001985 */
1986 public static class ConfirmRestrictFragment extends DialogFragment {
1987 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001988 if (!parent.isAdded()) return;
1989
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001990 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1991 dialog.setTargetFragment(parent, 0);
1992 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1993 }
1994
1995 @Override
1996 public Dialog onCreateDialog(Bundle savedInstanceState) {
1997 final Context context = getActivity();
1998
1999 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002000 builder.setTitle(R.string.data_usage_restrict_background_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07002001 if (Utils.hasMultipleUsers(context)) {
2002 builder.setMessage(R.string.data_usage_restrict_background_multiuser);
2003 } else {
2004 builder.setMessage(R.string.data_usage_restrict_background);
2005 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002006
2007 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002008 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002009 public void onClick(DialogInterface dialog, int which) {
2010 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2011 if (target != null) {
2012 target.setRestrictBackground(true);
2013 }
2014 }
2015 });
2016 builder.setNegativeButton(android.R.string.cancel, null);
2017
2018 return builder.create();
2019 }
2020 }
2021
2022 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002023 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
2024 * change has been denied, usually based on
2025 * {@link DataUsageSummary#hasLimitedNetworks()}.
2026 */
2027 public static class DeniedRestrictFragment extends DialogFragment {
2028 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002029 if (!parent.isAdded()) return;
2030
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002031 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
2032 dialog.setTargetFragment(parent, 0);
2033 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
2034 }
2035
2036 @Override
2037 public Dialog onCreateDialog(Bundle savedInstanceState) {
2038 final Context context = getActivity();
2039
2040 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2041 builder.setTitle(R.string.data_usage_app_restrict_background);
2042 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
2043 builder.setPositiveButton(android.R.string.ok, null);
2044
2045 return builder.create();
2046 }
2047 }
2048
2049 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002050 * Dialog to request user confirmation before setting
2051 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
2052 */
2053 public static class ConfirmAppRestrictFragment extends DialogFragment {
2054 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002055 if (!parent.isAdded()) return;
2056
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002057 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2058 dialog.setTargetFragment(parent, 0);
2059 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2060 }
2061
2062 @Override
2063 public Dialog onCreateDialog(Bundle savedInstanceState) {
2064 final Context context = getActivity();
2065
2066 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002067 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2068 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2069
2070 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002071 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002072 public void onClick(DialogInterface dialog, int which) {
2073 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2074 if (target != null) {
2075 target.setAppRestrictBackground(true);
2076 }
2077 }
2078 });
2079 builder.setNegativeButton(android.R.string.cancel, null);
2080
2081 return builder.create();
2082 }
2083 }
2084
2085 /**
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002086 * Dialog to inform user about changing auto-sync setting
2087 */
2088 public static class ConfirmAutoSyncChangeFragment extends DialogFragment {
Amith Yamasani665235f2012-06-07 19:58:34 -07002089 private static final String SAVE_ENABLING = "enabling";
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002090 private boolean mEnabling;
2091
2092 public static void show(DataUsageSummary parent, boolean enabling) {
2093 if (!parent.isAdded()) return;
2094
2095 final ConfirmAutoSyncChangeFragment dialog = new ConfirmAutoSyncChangeFragment();
2096 dialog.mEnabling = enabling;
2097 dialog.setTargetFragment(parent, 0);
2098 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_AUTO_SYNC_CHANGE);
2099 }
2100
2101 @Override
2102 public Dialog onCreateDialog(Bundle savedInstanceState) {
2103 final Context context = getActivity();
Amith Yamasani665235f2012-06-07 19:58:34 -07002104 if (savedInstanceState != null) {
2105 mEnabling = savedInstanceState.getBoolean(SAVE_ENABLING);
2106 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002107
2108 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2109 if (!mEnabling) {
2110 builder.setTitle(R.string.data_usage_auto_sync_off_dialog_title);
2111 builder.setMessage(R.string.data_usage_auto_sync_off_dialog);
2112 } else {
2113 builder.setTitle(R.string.data_usage_auto_sync_on_dialog_title);
2114 builder.setMessage(R.string.data_usage_auto_sync_on_dialog);
2115 }
2116
2117 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
2118 @Override
2119 public void onClick(DialogInterface dialog, int which) {
2120 ContentResolver.setMasterSyncAutomatically(mEnabling);
2121 }
2122 });
2123 builder.setNegativeButton(android.R.string.cancel, null);
2124
2125 return builder.create();
2126 }
Amith Yamasani665235f2012-06-07 19:58:34 -07002127
2128 @Override
2129 public void onSaveInstanceState(Bundle outState) {
2130 super.onSaveInstanceState(outState);
2131 outState.putBoolean(SAVE_ENABLING, mEnabling);
2132 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002133 }
2134
2135 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002136 * Compute default tab that should be selected, based on
2137 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2138 */
2139 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002140 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
2141 if (template == null) return null;
2142
2143 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002144 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002145 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002146 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002147 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002148 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002149 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002150 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002151 return TAB_WIFI;
2152 default:
2153 return null;
2154 }
2155 }
2156
2157 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002158 * Background task that loads {@link UidDetail}, binding to
2159 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002160 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002161 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2162 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002163 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002164 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002165
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002166 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002167 mProvider = checkNotNull(provider);
2168 mItem = checkNotNull(item);
2169 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002170 }
2171
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002172 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002173 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002174 final UidDetailTask existing = (UidDetailTask) target.getTag();
2175 if (existing != null) {
2176 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002177 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002178
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002179 final UidDetail cachedDetail = provider.getUidDetail(item.key, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002180 if (cachedDetail != null) {
2181 bindView(cachedDetail, target);
2182 } else {
2183 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2184 AsyncTask.THREAD_POOL_EXECUTOR));
2185 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002186 }
2187
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002188 private static void bindView(UidDetail detail, View target) {
2189 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2190 final TextView title = (TextView) target.findViewById(android.R.id.title);
2191
2192 if (detail != null) {
2193 icon.setImageDrawable(detail.icon);
2194 title.setText(detail.label);
2195 } else {
2196 icon.setImageDrawable(null);
2197 title.setText(null);
2198 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002199 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002200
2201 @Override
2202 protected void onPreExecute() {
2203 bindView(null, mTarget);
2204 }
2205
2206 @Override
2207 protected UidDetail doInBackground(Void... params) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002208 return mProvider.getUidDetail(mItem.key, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002209 }
2210
2211 @Override
2212 protected void onPostExecute(UidDetail result) {
2213 bindView(result, mTarget);
2214 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002215 }
2216
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002217 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002218 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002219 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002220 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002221 if (TEST_RADIOS) {
2222 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2223 }
2224
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002225 final ConnectivityManager conn = ConnectivityManager.from(context);
2226 final TelephonyManager tele = TelephonyManager.from(context);
2227
2228 // require both supported network and ready SIM
2229 return conn.isNetworkSupported(TYPE_MOBILE) && tele.getSimState() == SIM_STATE_READY;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002230 }
2231
2232 /**
2233 * Test if device has a mobile 4G data radio.
2234 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002235 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002236 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2237 return false;
2238 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002239 if (TEST_RADIOS) {
2240 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2241 }
2242
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002243 final ConnectivityManager conn = ConnectivityManager.from(context);
2244 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002245
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002246 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Wink Saville55434042012-06-14 12:33:43 -07002247 final boolean hasLte = (tele.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE)
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002248 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002249 return hasWimax || hasLte;
2250 }
2251
2252 /**
2253 * Test if device has a Wi-Fi data radio.
2254 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002255 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002256 if (TEST_RADIOS) {
2257 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2258 }
2259
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002260 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002261 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002262 }
2263
2264 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002265 * Test if device has an ethernet network connection.
2266 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002267 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002268 if (TEST_RADIOS) {
2269 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2270 }
2271
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002272 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002273 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2274
2275 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002276 if (mStatsSession != null) {
2277 try {
2278 ethernetBytes = mStatsSession.getSummaryForNetwork(
2279 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2280 .getTotalBytes();
2281 } catch (RemoteException e) {
2282 throw new RuntimeException(e);
2283 }
2284 } else {
2285 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002286 }
2287
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002288 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002289 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002290 }
2291
2292 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002293 * Inflate a {@link Preference} style layout, adding the given {@link View}
2294 * widget into {@link android.R.id#widget_frame}.
2295 */
2296 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2297 final View view = inflater.inflate(R.layout.preference, root, false);
2298 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2299 android.R.id.widget_frame);
2300 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2301 return view;
2302 }
2303
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002304 private static View inflateAppTitle(
2305 LayoutInflater inflater, ViewGroup root, CharSequence label) {
2306 final TextView view = (TextView) inflater.inflate(
2307 R.layout.data_usage_app_title, root, false);
2308 view.setText(label);
2309 return view;
2310 }
2311
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002312 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002313 * Test if any networks are currently limited.
2314 */
2315 private boolean hasLimitedNetworks() {
2316 return !buildLimitedNetworksList().isEmpty();
2317 }
2318
2319 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002320 * Build string describing currently limited networks, which defines when
2321 * background data is restricted.
2322 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002323 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002324 private CharSequence buildLimitedNetworksString() {
2325 final List<CharSequence> limited = buildLimitedNetworksList();
2326
2327 // handle case where no networks limited
2328 if (limited.isEmpty()) {
2329 limited.add(getText(R.string.data_usage_list_none));
2330 }
2331
2332 return TextUtils.join(limited);
2333 }
2334
2335 /**
2336 * Build list of currently limited networks, which defines when background
2337 * data is restricted.
2338 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002339 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002340 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002341 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002342
2343 // build combined list of all limited networks
2344 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002345
2346 final TelephonyManager tele = TelephonyManager.from(context);
2347 if (tele.getSimState() == SIM_STATE_READY) {
2348 final String subscriberId = getActiveSubscriberId(context);
2349 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2350 limited.add(getText(R.string.data_usage_list_mobile));
2351 }
2352 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2353 limited.add(getText(R.string.data_usage_tab_3g));
2354 }
2355 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2356 limited.add(getText(R.string.data_usage_tab_4g));
2357 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002358 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002359
2360 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002361 limited.add(getText(R.string.data_usage_tab_wifi));
2362 }
2363 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2364 limited.add(getText(R.string.data_usage_tab_ethernet));
2365 }
2366
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002367 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002368 }
2369
2370 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002371 * Inset both selector and divider {@link Drawable} on the given
2372 * {@link ListView} by the requested dimensions.
2373 */
2374 private static void insetListViewDrawables(ListView view, int insetSide) {
2375 final Drawable selector = view.getSelector();
2376 final Drawable divider = view.getDivider();
2377
2378 // fully unregister these drawables so callbacks can be maintained after
2379 // wrapping below.
2380 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2381 view.setSelector(stub);
2382 view.setDivider(stub);
2383
2384 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2385 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2386 }
2387
2388 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002389 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002390 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002391 */
2392 private static void setPreferenceTitle(View parent, int resId) {
2393 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2394 title.setText(resId);
2395 }
2396
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002397 /**
2398 * Set {@link android.R.id#summary} for a preference view inflated with
2399 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2400 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002401 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002402 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2403 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002404 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002405 }
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002406
2407 /**
2408 * For search
2409 */
2410 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -07002411 new BaseSearchIndexProvider() {
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002412 @Override
2413 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
2414 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
2415
2416 final Resources res = context.getResources();
2417
2418 // Add fragment title
2419 SearchIndexableRaw data = new SearchIndexableRaw(context);
2420 data.title = res.getString(R.string.data_usage_summary_title);
2421 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2422 result.add(data);
2423
2424 // Mobile data
2425 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002426 data.key = DATA_USAGE_ENABLE_MOBILE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002427 data.title = res.getString(R.string.data_usage_enable_mobile);
2428 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2429 result.add(data);
2430
2431 // Set mobile data limit
2432 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002433 data.key = DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002434 data.title = res.getString(R.string.data_usage_disable_mobile_limit);
2435 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2436 result.add(data);
2437
Fabrice Di Megliof2a52262014-04-17 17:20:27 -07002438 // Data usage cycle
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002439 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002440 data.key = DATA_USAGE_CYCLE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002441 data.title = res.getString(R.string.data_usage_cycle);
2442 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2443 result.add(data);
2444
2445 return result;
2446 }
2447 };
2448
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002449}