blob: 86a73c28fef6a41638ecf33b39e44a1b12c58c7a [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;
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700195 private TelephonyManager mTelephonyManager;
196
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700197
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700198 private INetworkStatsSession mStatsSession;
199
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700200 private static final String PREF_FILE = "data_usage";
201 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700202 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700203
204 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700205
Jeff Sharkey8a503642011-06-10 13:31:21 -0700206 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700207 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700208 private TabWidget mTabWidget;
209 private ListView mListView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700210 private DataUsageAdapter mAdapter;
211
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700212 /** Distance to inset content from sides, when needed. */
213 private int mInsetSide = 0;
214
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700215 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700216
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700217 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700218 private LinearLayout mNetworkSwitches;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700219 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700220 private View mDataEnabledView;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700221 private CheckBox mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700222 private View mDisableAtLimitView;
223
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700224 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700225 private Spinner mCycleSpinner;
226 private CycleAdapter mCycleAdapter;
227
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700228 private ChartDataUsageView mChart;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700229 private TextView mUsageSummary;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700230 private TextView mEmpty;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700231
232 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700233 private ImageView mAppIcon;
234 private ViewGroup mAppTitles;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700235 private PieChartView mAppPieChart;
236 private TextView mAppForeground;
237 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700238 private Button mAppSettings;
239
240 private LinearLayout mAppSwitches;
241 private CheckBox mAppRestrict;
242 private View mAppRestrictView;
243
Jeff Sharkey8a503642011-06-10 13:31:21 -0700244 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700245 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700246
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700247 private NetworkTemplate mTemplate;
248 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700249
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700250 private AppItem mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700251
252 private Intent mAppSettingsIntent;
253
Jeff Sharkeya662e492011-06-18 21:57:06 -0700254 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700255
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700256 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700257 private String mIntentTab = null;
258
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700259 private MenuItem mMenuDataRoaming;
260 private MenuItem mMenuRestrictBackground;
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700261 private MenuItem mMenuAutoSync;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700262
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700263 /** Flag used to ignore listeners during binding. */
264 private boolean mBinding;
265
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700266 private UidDetailProvider mUidDetailProvider;
267
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700268 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700269 public void onCreate(Bundle savedInstanceState) {
270 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700271 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700272
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700273 mNetworkService = INetworkManagementService.Stub.asInterface(
274 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700275 mStatsService = INetworkStatsService.Stub.asInterface(
276 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700277 mPolicyManager = NetworkPolicyManager.from(context);
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700278 mTelephonyManager = TelephonyManager.from(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700279
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700280 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700281
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700282 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700283 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700284
Jaewan Kimffce9c12013-03-19 16:53:45 +0900285 try {
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700286 if (!mNetworkService.isBandwidthControlEnabled()) {
287 Log.w(TAG, "No bandwidth control; leaving");
288 getActivity().finish();
289 }
290 } catch (RemoteException e) {
291 Log.w(TAG, "No bandwidth control; leaving");
292 getActivity().finish();
293 }
294
295 try {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900296 mStatsSession = mStatsService.openSession();
297 } catch (RemoteException e) {
298 throw new RuntimeException(e);
299 }
300
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700301 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700302 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700303
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700304 // override preferences when no mobile radio
305 if (!hasReadyMobileRadio(context)) {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900306 mShowWifi = true;
307 mShowEthernet = true;
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700308 }
309
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700310 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700311 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700312
Jeff Sharkey8a503642011-06-10 13:31:21 -0700313 @Override
314 public View onCreateView(LayoutInflater inflater, ViewGroup container,
315 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700316
Jeff Sharkey8a503642011-06-10 13:31:21 -0700317 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700318 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
319
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700320 mUidDetailProvider = new UidDetailProvider(context);
321
Jeff Sharkey8a503642011-06-10 13:31:21 -0700322 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700323 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700324 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
325 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700326
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700327 // decide if we need to manually inset our content, or if we should rely
328 // on parent container for inset.
329 final boolean shouldInset = mListView.getScrollBarStyle()
330 == View.SCROLLBARS_OUTSIDE_OVERLAY;
Amith Yamasani56f51a82013-08-05 10:07:23 -0700331 mInsetSide = 0;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700332
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700333 // adjust padding around tabwidget as needed
Amith Yamasani56f51a82013-08-05 10:07:23 -0700334 prepareCustomPreferencesList(container, view, mListView, false);
Jeff Sharkey5d706792011-09-08 18:57:17 -0700335
Jeff Sharkey8a503642011-06-10 13:31:21 -0700336 mTabHost.setup();
337 mTabHost.setOnTabChangedListener(mTabListener);
338
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700339 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700340 mHeader.setClickable(true);
341
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700342 mListView.addHeaderView(new View(context), null, true);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700343 mListView.addHeaderView(mHeader, null, true);
344 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700345
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700346 if (mInsetSide > 0) {
347 // inset selector and divider drawables
348 insetListViewDrawables(mListView, mInsetSide);
Fabrice Di Megliob27223f2013-01-15 18:54:11 -0800349 mHeader.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700350 }
351
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700352 {
353 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700354 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
355 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700356 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700357
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700358 mDataEnabled = new Switch(inflater.getContext());
359 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -0700360 mDataEnabledView.setTag(DATA_USAGE_ENABLE_MOBILE_KEY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700361 mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener);
362 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700363
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700364 mDisableAtLimit = new CheckBox(inflater.getContext());
365 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700366 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700367 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -0700368 mDisableAtLimitView.setTag(DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700369 mDisableAtLimitView.setClickable(true);
370 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700371 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
372 mNetworkSwitches.addView(mDisableAtLimitView);
373 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700374
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700375 // bind cycle dropdown
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700376 mCycleView = mHeader.findViewById(R.id.cycles);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -0700377 mCycleView.setTag(DATA_USAGE_CYCLE_KEY);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700378 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700379 mCycleAdapter = new CycleAdapter(context);
380 mCycleSpinner.setAdapter(mCycleAdapter);
381 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
382
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700383 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700384 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800385 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700386
387 {
388 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700389 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700390 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
391 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700392 mAppPieChart = (PieChartView) mAppDetail.findViewById(R.id.app_pie_chart);
393 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
394 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700395 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700396
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700397 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700398 mAppSettings.setOnClickListener(mAppSettingsListener);
399
400 mAppRestrict = new CheckBox(inflater.getContext());
401 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700402 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700403 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700404 mAppRestrictView.setClickable(true);
405 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700406 mAppRestrictView.setOnClickListener(mAppRestrictListener);
407 mAppSwitches.addView(mAppRestrictView);
408 }
409
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700410 mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700411 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700412
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700413 mAdapter = new DataUsageAdapter(mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700414 mListView.setOnItemClickListener(mListListener);
415 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700416
417 return view;
418 }
419
420 @Override
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700421 public void onViewStateRestored(Bundle savedInstanceState) {
422 super.onViewStateRestored(savedInstanceState);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700423
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700424 // pick default tab based on incoming intent
425 final Intent intent = getActivity().getIntent();
426 mIntentTab = computeTabFromIntent(intent);
427
Jeff Sharkey8a503642011-06-10 13:31:21 -0700428 // this kicks off chain reaction which creates tabs, binds the body to
429 // selected network, and binds chart, cycles and detail list.
430 updateTabs();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700431 }
432
433 @Override
434 public void onResume() {
435 super.onResume();
436
437 getView().post(new Runnable() {
438 @Override
439 public void run() {
440 highlightViewIfNeeded();
441 }
442 });
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700443
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700444 // kick off background task to update stats
445 new AsyncTask<Void, Void, Void>() {
446 @Override
447 protected Void doInBackground(Void... params) {
448 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700449 // wait a few seconds before kicking off
450 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700451 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700452 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700453 } catch (RemoteException e) {
454 }
455 return null;
456 }
457
458 @Override
459 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700460 if (isAdded()) {
461 updateBody();
462 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700463 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700464 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700465 }
466
Jeff Sharkey8a503642011-06-10 13:31:21 -0700467 @Override
468 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
469 inflater.inflate(R.menu.data_usage, menu);
470 }
471
472 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700473 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700474 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700475 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700476 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700477
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700478 mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700479 mMenuDataRoaming.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700480 mMenuDataRoaming.setChecked(getDataRoaming());
481
482 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700483 mMenuRestrictBackground.setVisible(
484 hasReadyMobileRadio(context) && isOwner && !appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700485 mMenuRestrictBackground.setChecked(mPolicyManager.getRestrictBackground());
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700486
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700487 mMenuAutoSync = menu.findItem(R.id.data_usage_menu_auto_sync);
488 mMenuAutoSync.setChecked(ContentResolver.getMasterSyncAutomatically());
Jeff Sharkeyfda48e32012-09-18 15:03:47 -0700489 mMenuAutoSync.setVisible(!appDetailMode);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700490
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700491 final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g);
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700492 split4g.setVisible(hasReadyMobile4gRadio(context) && isOwner && !appDetailMode);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700493 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700494
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700495 final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700496 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700497 showWifi.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700498 showWifi.setChecked(mShowWifi);
499 } else {
500 showWifi.setVisible(false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700501 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700502
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700503 final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700504 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700505 showEthernet.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700506 showEthernet.setChecked(mShowEthernet);
507 } else {
508 showEthernet.setVisible(false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700509 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700510
511 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
512 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700513 metered.setVisible(!appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700514 } else {
515 metered.setVisible(false);
516 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700517
518 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
519 String helpUrl;
520 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800521 HelpUtils.prepareHelpMenuItem(context, help, helpUrl);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700522 } else {
523 help.setVisible(false);
524 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700525 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700526
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700527 @Override
528 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700529 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700530 case R.id.data_usage_menu_roaming: {
531 final boolean dataRoaming = !item.isChecked();
532 if (dataRoaming) {
533 ConfirmDataRoamingFragment.show(this);
534 } else {
535 // no confirmation to disable roaming
536 setDataRoaming(false);
537 }
538 return true;
539 }
540 case R.id.data_usage_menu_restrict_background: {
541 final boolean restrictBackground = !item.isChecked();
542 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800543 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700544 } else {
545 // no confirmation to drop restriction
546 setRestrictBackground(false);
547 }
548 return true;
549 }
550 case R.id.data_usage_menu_split_4g: {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700551 final boolean mobileSplit = !item.isChecked();
Jeff Sharkeya662e492011-06-18 21:57:06 -0700552 setMobilePolicySplit(mobileSplit);
553 item.setChecked(isMobilePolicySplit());
Jeff Sharkey8a503642011-06-10 13:31:21 -0700554 updateTabs();
555 return true;
556 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700557 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700558 mShowWifi = !item.isChecked();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700559 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700560 item.setChecked(mShowWifi);
561 updateTabs();
562 return true;
563 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700564 case R.id.data_usage_menu_show_ethernet: {
565 mShowEthernet = !item.isChecked();
566 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
567 item.setChecked(mShowEthernet);
568 updateTabs();
569 return true;
570 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700571 case R.id.data_usage_menu_metered: {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800572 final SettingsActivity sa = (SettingsActivity) getActivity();
573 sa.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700574 R.string.data_usage_metered_title, null, this, 0);
575 return true;
576 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700577 case R.id.data_usage_menu_auto_sync: {
Guang Zhu167ba2a2012-10-19 17:55:05 -0700578 if (ActivityManager.isUserAMonkey()) {
579 Log.d("SyncState", "ignoring monkey's attempt to flip global sync state");
580 } else {
581 ConfirmAutoSyncChangeFragment.show(this, !item.isChecked());
582 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700583 return true;
584 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700585 }
586 return false;
587 }
588
Jeff Sharkey94a90952011-06-13 22:31:09 -0700589 @Override
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700590 public void onDestroy() {
Jeff Sharkey94a90952011-06-13 22:31:09 -0700591 mDataEnabledView = null;
592 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700593
594 mUidDetailProvider.clearCache();
595 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700596
597 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700598
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800599 super.onDestroy();
600 }
601
Jeff Sharkey8a503642011-06-10 13:31:21 -0700602 /**
Jeff Sharkey92811822012-05-04 11:47:29 -0700603 * Build and assign {@link LayoutTransition} to various containers. Should
604 * only be assigned after initial layout is complete.
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700605 */
Jeff Sharkey92811822012-05-04 11:47:29 -0700606 private void ensureLayoutTransitions() {
607 // skip when already setup
608 if (mChart.getLayoutTransition() != null) return;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700609
Jeff Sharkey92811822012-05-04 11:47:29 -0700610 mTabsContainer.setLayoutTransition(buildLayoutTransition());
611 mHeader.setLayoutTransition(buildLayoutTransition());
612 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700613
Jeff Sharkey92811822012-05-04 11:47:29 -0700614 final LayoutTransition chartTransition = buildLayoutTransition();
615 chartTransition.disableTransitionType(LayoutTransition.APPEARING);
616 chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
617 mChart.setLayoutTransition(chartTransition);
618 }
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700619
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700620 private static LayoutTransition buildLayoutTransition() {
621 final LayoutTransition transition = new LayoutTransition();
622 if (TEST_ANIM) {
623 transition.setDuration(1500);
624 }
625 transition.setAnimateParentHierarchy(false);
626 return transition;
627 }
628
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700629 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700630 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700631 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
632 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700633 */
634 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700635 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700636 mTabHost.clearAllTabs();
637
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700638 final boolean mobileSplit = isMobilePolicySplit();
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700639 if (mobileSplit && hasReadyMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700640 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
641 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700642 } else if (hasReadyMobileRadio(context)) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700643 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700644 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700645 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700646 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
647 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700648 if (mShowEthernet && hasEthernet(context)) {
649 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
650 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700651
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700652 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700653 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
654 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
655 if (mIntentTab != null) {
656 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700657 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700658 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700659 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700660 mTabHost.setCurrentTabByTag(mIntentTab);
661 }
662 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700663 } else if (noTabs) {
664 // no usable tabs, so hide body
665 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700666 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700667 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700668 }
669 }
670
Jeff Sharkey8a503642011-06-10 13:31:21 -0700671 /**
672 * Factory that provide empty {@link View} to make {@link TabHost} happy.
673 */
674 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700675 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700676 public View createTabContent(String tag) {
677 return new View(mTabHost.getContext());
678 }
679 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700680
Jeff Sharkey8a503642011-06-10 13:31:21 -0700681 /**
682 * Build {@link TabSpec} with thin indicator, and empty content.
683 */
684 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700685 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
686 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700687 }
688
689 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700690 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700691 public void onTabChanged(String tabId) {
692 // user changed tab; update body
693 updateBody();
694 }
695 };
696
697 /**
698 * Update body content based on current tab. Loads
699 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
700 * binds them to visible controls.
701 */
702 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700703 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700704 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700705
Jeff Sharkeya662e492011-06-18 21:57:06 -0700706 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700707 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700708 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700709
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700710 if (currentTab == null) {
711 Log.w(TAG, "no tab selected; hiding body");
712 mListView.setVisibility(View.GONE);
713 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700714 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700715 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700716 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700717
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700718 mCurrentTab = currentTab;
719
Jeff Sharkey8a503642011-06-10 13:31:21 -0700720 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
721
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700722 mDataEnabledView.setVisibility(isOwner ? View.VISIBLE : View.GONE);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700723
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700724 // TODO: remove mobile tabs when SIM isn't ready
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700725
Jeff Sharkey8a503642011-06-10 13:31:21 -0700726 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700727 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
728 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700729 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700730
731 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700732 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
733 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
734 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700735 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700736
737 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700738 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
739 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
740 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700741 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700742
743 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700744 // wifi doesn't have any controls
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700745 mDataEnabledView.setVisibility(View.GONE);
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700746 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700747 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700748
749 } else if (TAB_ETHERNET.equals(currentTab)) {
750 // ethernet doesn't have any controls
751 mDataEnabledView.setVisibility(View.GONE);
752 mDisableAtLimitView.setVisibility(View.GONE);
753 mTemplate = buildTemplateEthernet();
754
755 } else {
756 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700757 }
758
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700759 // kick off loader for network history
760 // TODO: consider chaining two loaders together instead of reloading
761 // network history when showing app detail.
762 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700763 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700764
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700765 // detail mode can change visible menus, invalidate
766 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700767
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700768 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700769 }
770
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700771 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700772 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700773 }
774
775 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700776 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700777 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700778 */
779 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700780 final Context context = getActivity();
781 final PackageManager pm = context.getPackageManager();
782 final LayoutInflater inflater = getActivity().getLayoutInflater();
783
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700784 if (isAppDetailMode()) {
785 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700786 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700787 } else {
788 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700789 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700790
791 // hide detail stats when not in detail mode
792 mChart.bindDetailNetworkStats(null);
793 return;
794 }
795
796 // remove warning/limit sweeps while in detail mode
797 mChart.bindNetworkPolicy(null);
798
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700799 // show icon and all labels appearing under this app
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700800 final int uid = mCurrentApp.key;
801 final UidDetail detail = mUidDetailProvider.getUidDetail(uid, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700802 mAppIcon.setImageDrawable(detail.icon);
803
804 mAppTitles.removeAllViews();
805 if (detail.detailLabels != null) {
806 for (CharSequence label : detail.detailLabels) {
807 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label));
808 }
809 } else {
810 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label));
811 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700812
813 // enable settings button when package provides it
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700814 final String[] packageNames = pm.getPackagesForUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700815 if (packageNames != null && packageNames.length > 0) {
816 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700817 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
818
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700819 // Search for match across all packages
820 boolean matchFound = false;
821 for (String packageName : packageNames) {
822 mAppSettingsIntent.setPackage(packageName);
823 if (pm.resolveActivity(mAppSettingsIntent, 0) != null) {
824 matchFound = true;
825 break;
826 }
827 }
828
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700829 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -0700830 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700831
832 } else {
833 mAppSettingsIntent = null;
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700834 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700835 }
836
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700837 updateDetailData();
838
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700839 if (UserHandle.isApp(uid) && !mPolicyManager.getRestrictBackground()
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700840 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700841 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800842 setPreferenceSummary(mAppRestrictView,
843 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700844
845 mAppRestrictView.setVisibility(View.VISIBLE);
846 mAppRestrict.setChecked(getAppRestrictBackground());
847
848 } else {
849 mAppRestrictView.setVisibility(View.GONE);
850 }
851 }
852
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700853 private void setPolicyWarningBytes(long warningBytes) {
854 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700855 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700856 updatePolicy(false);
857 }
858
859 private void setPolicyLimitBytes(long limitBytes) {
860 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700861 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700862 updatePolicy(false);
863 }
864
Jeff Sharkey28130d92011-09-02 16:10:24 -0700865 /**
866 * Local cache of value, used to work around delay when
867 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
868 */
869 private Boolean mMobileDataEnabled;
870
871 private boolean isMobileDataEnabled() {
872 if (mMobileDataEnabled != null) {
873 // TODO: deprecate and remove this once enabled flag is on policy
874 return mMobileDataEnabled;
875 } else {
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700876 return mTelephonyManager.getDataEnabled();
Jeff Sharkey28130d92011-09-02 16:10:24 -0700877 }
878 }
879
880 private void setMobileDataEnabled(boolean enabled) {
881 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
Robert Greenwalt0d4c5002014-05-21 20:02:32 -0700882 mTelephonyManager.setDataEnabled(enabled);
Jeff Sharkey28130d92011-09-02 16:10:24 -0700883 mMobileDataEnabled = enabled;
884 updatePolicy(false);
885 }
886
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700887 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700888 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked()
889 && ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700890 }
891
892 private boolean isBandwidthControlEnabled() {
893 try {
894 return mNetworkService.isBandwidthControlEnabled();
895 } catch (RemoteException e) {
896 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
897 return false;
898 }
899 }
900
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700901 private boolean getDataRoaming() {
902 final ContentResolver resolver = getActivity().getContentResolver();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800903 return android.provider.Settings.Global.getInt(resolver,
904 android.provider.Settings.Global.DATA_ROAMING, 0) != 0;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700905 }
906
907 private void setDataRoaming(boolean enabled) {
908 // TODO: teach telephony DataConnectionTracker to watch and apply
909 // updates when changed.
910 final ContentResolver resolver = getActivity().getContentResolver();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800911 android.provider.Settings.Global.putInt(resolver,
912 android.provider.Settings.Global.DATA_ROAMING, enabled ? 1 : 0);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700913 mMenuDataRoaming.setChecked(enabled);
914 }
915
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700916 public void setRestrictBackground(boolean restrictBackground) {
917 mPolicyManager.setRestrictBackground(restrictBackground);
918 mMenuRestrictBackground.setChecked(restrictBackground);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700919 }
920
921 private boolean getAppRestrictBackground() {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700922 final int uid = mCurrentApp.key;
923 final int uidPolicy = mPolicyManager.getUidPolicy(uid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700924 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
925 }
926
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700927 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700928 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700929 final int uid = mCurrentApp.key;
930 mPolicyManager.setUidPolicy(
931 uid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700932 mAppRestrict.setChecked(restrictBackground);
933 }
934
Jeff Sharkey8a503642011-06-10 13:31:21 -0700935 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700936 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
937 * current {@link #mTemplate}.
938 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700939 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700940 if (isAppDetailMode()) {
941 mNetworkSwitches.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700942 } else {
943 mNetworkSwitches.setVisibility(View.VISIBLE);
944 }
945
Jeff Sharkey28130d92011-09-02 16:10:24 -0700946 // TODO: move enabled state directly into policy
947 if (TAB_MOBILE.equals(mCurrentTab)) {
948 mBinding = true;
949 mDataEnabled.setChecked(isMobileDataEnabled());
950 mBinding = false;
951 }
952
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700953 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
954 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700955 mDisableAtLimitView.setVisibility(View.VISIBLE);
956 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700957 if (!isAppDetailMode()) {
958 mChart.bindNetworkPolicy(policy);
959 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700960
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700961 } else {
962 // controls are disabled; don't bind warning/limit sweeps
963 mDisableAtLimitView.setVisibility(View.GONE);
964 mChart.bindNetworkPolicy(null);
965 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700966
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700967 if (refreshCycle) {
968 // generate cycle list based on policy and available history
969 updateCycleList(policy);
970 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700971 }
972
973 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700974 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
975 * and available {@link NetworkStatsHistory} data. Always selects the newest
976 * item, updating the inspection range on {@link #mChart}.
977 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700978 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700979 // stash away currently selected cycle to try restoring below
980 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700981 mCycleAdapter.clear();
982
983 final Context context = mCycleSpinner.getContext();
984
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700985 long historyStart = Long.MAX_VALUE;
986 long historyEnd = Long.MIN_VALUE;
987 if (mChartData != null) {
988 historyStart = mChartData.network.getStart();
989 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700990 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700991
Jeff Sharkey461842a2011-09-25 18:22:48 -0700992 final long now = System.currentTimeMillis();
993 if (historyStart == Long.MAX_VALUE) historyStart = now;
994 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700995
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700996 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700997 if (policy != null) {
998 // find the next cycle boundary
999 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001000
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001001 // walk backwards, generating all valid cycle ranges
1002 while (cycleEnd > historyStart) {
1003 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
1004 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
1005 + historyStart);
1006 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1007 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001008 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001009 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001010
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001011 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001012 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001013 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001014
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001015 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001016 // no policy defined cycles; show entry for each four-week period
1017 long cycleEnd = historyEnd;
1018 while (cycleEnd > historyStart) {
1019 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
1020 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1021 cycleEnd = cycleStart;
1022 }
1023
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001024 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001025 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001026
1027 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001028 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001029 final int position = mCycleAdapter.findNearestPosition(previousItem);
1030 mCycleSpinner.setSelection(position);
1031
1032 // only force-update cycle when changed; skipping preserves any
1033 // user-defined inspection region.
1034 final CycleItem selectedItem = mCycleAdapter.getItem(position);
1035 if (!Objects.equal(selectedItem, previousItem)) {
1036 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1037 } else {
1038 // but still kick off loader for detailed list
1039 updateDetailData();
1040 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001041 } else {
1042 updateDetailData();
1043 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001044 }
1045
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001046 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001047 @Override
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001048 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
1049 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001050
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001051 final boolean dataEnabled = isChecked;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001052 final String currentTab = mCurrentTab;
1053 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001054 if (dataEnabled) {
1055 setMobileDataEnabled(true);
1056 } else {
1057 // disabling data; show confirmation dialog which eventually
1058 // calls setMobileDataEnabled() once user confirms.
1059 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1060 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001061 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001062
Jeff Sharkey28130d92011-09-02 16:10:24 -07001063 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001064 }
1065 };
1066
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001067 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001068 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001069 public void onClick(View v) {
1070 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001071 if (disableAtLimit) {
1072 // enabling limit; show confirmation dialog which eventually
1073 // calls setPolicyLimitBytes() once user confirms.
1074 ConfirmLimitFragment.show(DataUsageSummary.this);
1075 } else {
1076 setPolicyLimitBytes(LIMIT_DISABLED);
1077 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001078 }
1079 };
1080
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001081 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001082 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001083 public void onClick(View v) {
1084 final boolean restrictBackground = !mAppRestrict.isChecked();
1085
1086 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001087 // enabling restriction; show confirmation dialog which
1088 // eventually calls setRestrictBackground() once user
1089 // confirms.
1090 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001091 } else {
1092 setAppRestrictBackground(false);
1093 }
1094 }
1095 };
1096
1097 private OnClickListener mAppSettingsListener = new OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001098 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001099 public void onClick(View v) {
Jeff Sharkeyd92e0412012-04-24 11:35:43 -07001100 if (!isAdded()) return;
1101
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001102 // TODO: target torwards entire UID instead of just first package
1103 startActivity(mAppSettingsIntent);
1104 }
1105 };
1106
Jeff Sharkey8a503642011-06-10 13:31:21 -07001107 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001108 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001109 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001110 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001111 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001112
1113 // TODO: sigh, remove this hack once we understand 6450986
1114 if (mUidDetailProvider == null || app == null) return;
1115
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001116 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001117 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001118 }
1119 };
1120
1121 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001122 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001123 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1124 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1125 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001126 // show cycle editor; will eventually call setPolicyCycleDay()
1127 // when user finishes editing.
1128 CycleEditorFragment.show(DataUsageSummary.this);
1129
1130 // reset spinner to something other than "change cycle..."
1131 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001132
1133 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001134 if (LOGD) {
1135 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1136 + cycle.end + "]");
1137 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001138
1139 // update chart to show selected cycle, and update detail data
1140 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001141 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001142
1143 updateDetailData();
1144 }
1145 }
1146
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001147 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001148 public void onNothingSelected(AdapterView<?> parent) {
1149 // ignored
1150 }
1151 };
1152
1153 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001154 * Update details based on {@link #mChart} inspection range depending on
1155 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1156 * of applications data usage, and when {@link #isAppDetailMode()} update
1157 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001158 */
1159 private void updateDetailData() {
1160 if (LOGD) Log.d(TAG, "updateDetailData()");
1161
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001162 final long start = mChart.getInspectStart();
1163 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001164 final long now = System.currentTimeMillis();
1165
1166 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001167
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001168 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001169 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001170 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001171 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001172 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001173 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001174 final long foregroundBytes = entry.rxBytes + entry.txBytes;
1175
1176 mAppPieChart.setOriginAngle(175);
1177
1178 mAppPieChart.removeAllSlices();
1179 mAppPieChart.addSlice(foregroundBytes, Color.parseColor("#d88d3a"));
1180 mAppPieChart.addSlice(defaultBytes, Color.parseColor("#666666"));
1181
1182 mAppPieChart.generatePath();
1183
1184 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1185 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1186
1187 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001188 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001189
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001190 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001191
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001192 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001193 if (mChartData != null) {
1194 entry = mChartData.network.getValues(start, end, now, null);
1195 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001196
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001197 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001198 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001199 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001200 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001201
1202 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1203 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001204 final String rangePhrase = formatDateRange(context, start, end);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001205
Jeff Sharkeye557c332012-04-13 16:04:07 -07001206 final int summaryRes;
László Dávid0b8bf4e2012-10-24 23:31:47 +02001207 if (TAB_MOBILE.equals(mCurrentTab) || TAB_3G.equals(mCurrentTab)
1208 || TAB_4G.equals(mCurrentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001209 summaryRes = R.string.data_usage_total_during_range_mobile;
1210 } else {
1211 summaryRes = R.string.data_usage_total_during_range;
1212 }
1213
1214 mUsageSummary.setText(getString(summaryRes, totalPhrase, rangePhrase));
Jeff Sharkey92811822012-05-04 11:47:29 -07001215
1216 // initial layout is finished above, ensure we have transitions
1217 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001218 }
1219
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001220 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1221 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001222 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001223 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001224 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001225 }
1226
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001227 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001228 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1229 mChartData = data;
1230 mChart.bindNetworkStats(mChartData.network);
1231 mChart.bindDetailNetworkStats(mChartData.detail);
1232
1233 // calcuate policy cycles based on available data
1234 updatePolicy(true);
1235 updateAppDetail();
1236
1237 // force scroll to top of body when showing detail
1238 if (mChartData.detail != null) {
1239 mListView.smoothScrollToPosition(0);
1240 }
1241 }
1242
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001243 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001244 public void onLoaderReset(Loader<ChartData> loader) {
1245 mChartData = null;
1246 mChart.bindNetworkStats(null);
1247 mChart.bindDetailNetworkStats(null);
1248 }
1249 };
1250
1251 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001252 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001253 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001254 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001255 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001256 }
1257
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001258 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001259 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001260 final int[] restrictedUids = mPolicyManager.getUidsWithPolicy(
Jeff Sharkeye557c332012-04-13 16:04:07 -07001261 POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001262 mAdapter.bindStats(data, restrictedUids);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001263 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001264 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001265
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001266 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001267 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001268 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001269 updateEmptyVisible();
1270 }
1271
1272 private void updateEmptyVisible() {
1273 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1274 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001275 }
1276 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001277
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001278 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001279 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001280 final Context context = getActivity();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001281 if (hasReadyMobileRadio(context)) {
1282 final TelephonyManager tele = TelephonyManager.from(context);
1283 return mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001284 } else {
1285 return false;
1286 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001287 }
1288
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001289 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001290 private void setMobilePolicySplit(boolean split) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001291 final Context context = getActivity();
1292 if (hasReadyMobileRadio(context)) {
1293 final TelephonyManager tele = TelephonyManager.from(context);
1294 mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split);
1295 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001296 }
1297
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001298 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001299 final TelephonyManager tele = TelephonyManager.from(context);
1300 final String actualSubscriberId = tele.getSubscriberId();
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -08001301 return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001302 }
1303
Jeff Sharkey8a503642011-06-10 13:31:21 -07001304 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001305 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001306 public void onInspectRangeChanged() {
1307 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
1308 updateDetailData();
1309 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001310
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001311 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001312 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001313 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001314 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001315
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001316 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001317 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001318 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001319 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001320
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001321 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001322 public void requestWarningEdit() {
1323 WarningEditorFragment.show(DataUsageSummary.this);
1324 }
1325
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001326 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001327 public void requestLimitEdit() {
1328 LimitEditorFragment.show(DataUsageSummary.this);
1329 }
1330 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001331
1332 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001333 * List item that reflects a specific data usage cycle.
1334 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001335 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001336 public CharSequence label;
1337 public long start;
1338 public long end;
1339
Jeff Sharkey8a503642011-06-10 13:31:21 -07001340 CycleItem(CharSequence label) {
1341 this.label = label;
1342 }
1343
1344 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001345 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001346 this.start = start;
1347 this.end = end;
1348 }
1349
Jeff Sharkey8a503642011-06-10 13:31:21 -07001350 @Override
1351 public String toString() {
1352 return label.toString();
1353 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001354
1355 @Override
1356 public boolean equals(Object o) {
1357 if (o instanceof CycleItem) {
1358 final CycleItem another = (CycleItem) o;
1359 return start == another.start && end == another.end;
1360 }
1361 return false;
1362 }
1363
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001364 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001365 public int compareTo(CycleItem another) {
1366 return Long.compare(start, another.start);
1367 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001368 }
1369
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001370 private static final StringBuilder sBuilder = new StringBuilder(50);
1371 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1372 sBuilder, Locale.getDefault());
1373
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001374 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001375 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001376
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001377 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001378 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001379 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1380 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001381 }
1382 }
1383
Jeff Sharkey8a503642011-06-10 13:31:21 -07001384 /**
1385 * Special-case data usage cycle that triggers dialog to change
1386 * {@link NetworkPolicy#cycleDay}.
1387 */
1388 public static class CycleChangeItem extends CycleItem {
1389 public CycleChangeItem(Context context) {
1390 super(context.getString(R.string.data_usage_change_cycle));
1391 }
1392 }
1393
1394 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001395 private boolean mChangePossible = false;
1396 private boolean mChangeVisible = false;
1397
1398 private final CycleChangeItem mChangeItem;
1399
Jeff Sharkey8a503642011-06-10 13:31:21 -07001400 public CycleAdapter(Context context) {
1401 super(context, android.R.layout.simple_spinner_item);
1402 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001403 mChangeItem = new CycleChangeItem(context);
1404 }
1405
1406 public void setChangePossible(boolean possible) {
1407 mChangePossible = possible;
1408 updateChange();
1409 }
1410
1411 public void setChangeVisible(boolean visible) {
1412 mChangeVisible = visible;
1413 updateChange();
1414 }
1415
1416 private void updateChange() {
1417 remove(mChangeItem);
1418 if (mChangePossible && mChangeVisible) {
1419 add(mChangeItem);
1420 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001421 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001422
1423 /**
1424 * Find position of {@link CycleItem} in this adapter which is nearest
1425 * the given {@link CycleItem}.
1426 */
1427 public int findNearestPosition(CycleItem target) {
1428 if (target != null) {
1429 final int count = getCount();
1430 for (int i = count - 1; i >= 0; i--) {
1431 final CycleItem item = getItem(i);
1432 if (item instanceof CycleChangeItem) {
1433 continue;
1434 } else if (item.compareTo(target) >= 0) {
1435 return i;
1436 }
1437 }
1438 }
1439 return 0;
1440 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001441 }
1442
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001443 public static class AppItem implements Comparable<AppItem>, Parcelable {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001444 public final int key;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001445 public boolean restricted;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001446 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001447 public long total;
1448
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001449 public AppItem(int key) {
1450 this.key = key;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001451 }
1452
1453 public AppItem(Parcel parcel) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001454 key = parcel.readInt();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001455 uids = parcel.readSparseBooleanArray();
1456 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001457 }
1458
1459 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001460 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001461 }
1462
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001463 @Override
1464 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001465 dest.writeInt(key);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001466 dest.writeSparseBooleanArray(uids);
1467 dest.writeLong(total);
1468 }
1469
1470 @Override
1471 public int describeContents() {
1472 return 0;
1473 }
1474
1475 @Override
1476 public int compareTo(AppItem another) {
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001477 return Long.compare(another.total, total);
1478 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001479
1480 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1481 @Override
1482 public AppItem createFromParcel(Parcel in) {
1483 return new AppItem(in);
1484 }
1485
1486 @Override
1487 public AppItem[] newArray(int size) {
1488 return new AppItem[size];
1489 }
1490 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001491 }
1492
Jeff Sharkey8a503642011-06-10 13:31:21 -07001493 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001494 * Adapter of applications, sorted by total usage descending.
1495 */
1496 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001497 private final UidDetailProvider mProvider;
1498 private final int mInsetSide;
1499
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001500 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001501 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001502
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001503 public DataUsageAdapter(UidDetailProvider provider, int insetSide) {
1504 mProvider = checkNotNull(provider);
1505 mInsetSide = insetSide;
1506 }
1507
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001508 /**
1509 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1510 */
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001511 public void bindStats(NetworkStats stats, int[] restrictedUids) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001512 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001513
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001514 final int currentUserId = ActivityManager.getCurrentUser();
1515 final SparseArray<AppItem> knownItems = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001516
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001517 NetworkStats.Entry entry = null;
1518 final int size = stats != null ? stats.size() : 0;
1519 for (int i = 0; i < size; i++) {
1520 entry = stats.getValues(i, entry);
1521
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001522 // Decide how to collapse items together
1523 final int uid = entry.uid;
1524 final int collapseKey;
1525 if (UserHandle.isApp(uid)) {
1526 if (UserHandle.getUserId(uid) == currentUserId) {
1527 collapseKey = uid;
1528 } else {
1529 collapseKey = UidDetailProvider.buildKeyForUser(UserHandle.getUserId(uid));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001530 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001531 } else if (uid == UID_REMOVED || uid == UID_TETHERING) {
1532 collapseKey = uid;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001533 } else {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001534 collapseKey = android.os.Process.SYSTEM_UID;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001535 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001536
1537 AppItem item = knownItems.get(collapseKey);
1538 if (item == null) {
1539 item = new AppItem(collapseKey);
1540 mItems.add(item);
1541 knownItems.put(item.key, item);
1542 }
1543 item.addUid(uid);
1544 item.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001545 }
1546
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001547 for (int uid : restrictedUids) {
1548 // Only splice in restricted state for current user
1549 if (UserHandle.getUserId(uid) != currentUserId) continue;
1550
1551 AppItem item = knownItems.get(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001552 if (item == null) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001553 item = new AppItem(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001554 item.total = -1;
1555 mItems.add(item);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001556 knownItems.put(item.key, item);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001557 }
1558 item.restricted = true;
1559 }
1560
Jeff Sharkey8a503642011-06-10 13:31:21 -07001561 Collections.sort(mItems);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001562 mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001563 notifyDataSetChanged();
1564 }
1565
1566 @Override
1567 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001568 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001569 }
1570
1571 @Override
1572 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001573 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001574 }
1575
1576 @Override
1577 public long getItemId(int position) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001578 return mItems.get(position).key;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001579 }
1580
1581 @Override
1582 public View getView(int position, View convertView, ViewGroup parent) {
1583 if (convertView == null) {
1584 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey5d706792011-09-08 18:57:17 -07001585 R.layout.data_usage_item, parent, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001586
1587 if (mInsetSide > 0) {
Fabrice Di Megliob27223f2013-01-15 18:54:11 -08001588 convertView.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001589 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001590 }
1591
1592 final Context context = parent.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001593
Jeff Sharkey28130d92011-09-02 16:10:24 -07001594 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001595 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1596 android.R.id.progress);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001597
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001598 // kick off async load of app details
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001599 final AppItem item = mItems.get(position);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001600 UidDetailTask.bindView(mProvider, item, convertView);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001601
Jeff Sharkeye557c332012-04-13 16:04:07 -07001602 if (item.restricted && item.total <= 0) {
1603 text1.setText(R.string.data_usage_app_restricted);
1604 progress.setVisibility(View.GONE);
1605 } else {
1606 text1.setText(Formatter.formatFileSize(context, item.total));
1607 progress.setVisibility(View.VISIBLE);
1608 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001609
1610 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1611 progress.setProgress(percentTotal);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001612
1613 return convertView;
1614 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001615 }
1616
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001617 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001618 * Empty {@link Fragment} that controls display of UID details in
1619 * {@link DataUsageSummary}.
1620 */
1621 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001622 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001623
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001624 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001625 if (!parent.isAdded()) return;
1626
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001627 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001628 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001629
1630 final AppDetailsFragment fragment = new AppDetailsFragment();
1631 fragment.setArguments(args);
1632 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001633 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1634 ft.add(fragment, TAG_APP_DETAILS);
1635 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001636 ft.setBreadCrumbTitle(label);
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001637 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001638 }
1639
1640 @Override
1641 public void onStart() {
1642 super.onStart();
1643 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001644 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001645 target.updateBody();
1646 }
1647
1648 @Override
1649 public void onStop() {
1650 super.onStop();
1651 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001652 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001653 target.updateBody();
1654 }
1655 }
1656
1657 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001658 * Dialog to request user confirmation before setting
1659 * {@link NetworkPolicy#limitBytes}.
1660 */
1661 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001662 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001663 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001664
1665 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001666 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001667
Jeff Sharkey461842a2011-09-25 18:22:48 -07001668 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001669 final CharSequence message;
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001670 final long minLimitBytes = (long) (
1671 parent.mPolicyEditor.getPolicy(parent.mTemplate).warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001672 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001673
1674 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001675 final String currentTab = parent.mCurrentTab;
1676 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001677 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001678 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001679 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001680 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001681 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001682 } else if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001683 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001684 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001685 } else {
1686 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001687 }
1688
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001689 final Bundle args = new Bundle();
1690 args.putCharSequence(EXTRA_MESSAGE, message);
1691 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1692
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001693 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1694 dialog.setArguments(args);
1695 dialog.setTargetFragment(parent, 0);
1696 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1697 }
1698
1699 @Override
1700 public Dialog onCreateDialog(Bundle savedInstanceState) {
1701 final Context context = getActivity();
1702
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001703 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001704 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1705
1706 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1707 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001708 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001709
1710 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001711 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001712 public void onClick(DialogInterface dialog, int which) {
1713 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1714 if (target != null) {
1715 target.setPolicyLimitBytes(limitBytes);
1716 }
1717 }
1718 });
1719
1720 return builder.create();
1721 }
1722 }
1723
1724 /**
1725 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1726 */
1727 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001728 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001729
1730 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001731 if (!parent.isAdded()) return;
1732
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001733 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001734 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001735
1736 final CycleEditorFragment dialog = new CycleEditorFragment();
1737 dialog.setArguments(args);
1738 dialog.setTargetFragment(parent, 0);
1739 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1740 }
1741
1742 @Override
1743 public Dialog onCreateDialog(Bundle savedInstanceState) {
1744 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001745 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1746 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001747
1748 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1749 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1750
1751 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1752 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1753
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001754 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1755 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001756
1757 cycleDayPicker.setMinValue(1);
1758 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001759 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001760 cycleDayPicker.setWrapSelectorWheel(true);
1761
1762 builder.setTitle(R.string.data_usage_cycle_editor_title);
1763 builder.setView(view);
1764
1765 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1766 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001767 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001768 public void onClick(DialogInterface dialog, int which) {
Jeff Sharkeyd277de92013-03-25 15:11:25 -07001769 // clear focus to finish pending text edits
1770 cycleDayPicker.clearFocus();
1771
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001772 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001773 final String cycleTimezone = new Time().timezone;
1774 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001775 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001776 }
1777 });
1778
1779 return builder.create();
1780 }
1781 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001782
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001783 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001784 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1785 */
1786 public static class WarningEditorFragment extends DialogFragment {
1787 private static final String EXTRA_TEMPLATE = "template";
1788
1789 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001790 if (!parent.isAdded()) return;
1791
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001792 final Bundle args = new Bundle();
1793 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1794
1795 final WarningEditorFragment dialog = new WarningEditorFragment();
1796 dialog.setArguments(args);
1797 dialog.setTargetFragment(parent, 0);
1798 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1799 }
1800
1801 @Override
1802 public Dialog onCreateDialog(Bundle savedInstanceState) {
1803 final Context context = getActivity();
1804 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1805 final NetworkPolicyEditor editor = target.mPolicyEditor;
1806
1807 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1808 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1809
1810 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1811 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1812
1813 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1814 final long warningBytes = editor.getPolicyWarningBytes(template);
1815 final long limitBytes = editor.getPolicyLimitBytes(template);
1816
1817 bytesPicker.setMinValue(0);
1818 if (limitBytes != LIMIT_DISABLED) {
1819 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1820 } else {
1821 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1822 }
1823 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1824 bytesPicker.setWrapSelectorWheel(false);
1825
1826 builder.setTitle(R.string.data_usage_warning_editor_title);
1827 builder.setView(view);
1828
1829 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1830 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001831 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001832 public void onClick(DialogInterface dialog, int which) {
1833 // clear focus to finish pending text edits
1834 bytesPicker.clearFocus();
1835
1836 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1837 editor.setPolicyWarningBytes(template, bytes);
1838 target.updatePolicy(false);
1839 }
1840 });
1841
1842 return builder.create();
1843 }
1844 }
1845
1846 /**
1847 * Dialog to edit {@link NetworkPolicy#limitBytes}.
1848 */
1849 public static class LimitEditorFragment extends DialogFragment {
1850 private static final String EXTRA_TEMPLATE = "template";
1851
1852 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001853 if (!parent.isAdded()) return;
1854
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001855 final Bundle args = new Bundle();
1856 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1857
1858 final LimitEditorFragment dialog = new LimitEditorFragment();
1859 dialog.setArguments(args);
1860 dialog.setTargetFragment(parent, 0);
1861 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
1862 }
1863
1864 @Override
1865 public Dialog onCreateDialog(Bundle savedInstanceState) {
1866 final Context context = getActivity();
1867 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1868 final NetworkPolicyEditor editor = target.mPolicyEditor;
1869
1870 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1871 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1872
1873 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1874 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1875
1876 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1877 final long warningBytes = editor.getPolicyWarningBytes(template);
1878 final long limitBytes = editor.getPolicyLimitBytes(template);
1879
1880 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09001881 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001882 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
1883 } else {
1884 bytesPicker.setMinValue(0);
1885 }
1886 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
1887 bytesPicker.setWrapSelectorWheel(false);
1888
1889 builder.setTitle(R.string.data_usage_limit_editor_title);
1890 builder.setView(view);
1891
1892 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1893 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001894 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001895 public void onClick(DialogInterface dialog, int which) {
1896 // clear focus to finish pending text edits
1897 bytesPicker.clearFocus();
1898
1899 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1900 editor.setPolicyLimitBytes(template, bytes);
1901 target.updatePolicy(false);
1902 }
1903 });
1904
1905 return builder.create();
1906 }
1907 }
1908 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07001909 * Dialog to request user confirmation before disabling data.
1910 */
1911 public static class ConfirmDataDisableFragment extends DialogFragment {
1912 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001913 if (!parent.isAdded()) return;
1914
Jeff Sharkey28130d92011-09-02 16:10:24 -07001915 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
1916 dialog.setTargetFragment(parent, 0);
1917 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
1918 }
1919
1920 @Override
1921 public Dialog onCreateDialog(Bundle savedInstanceState) {
1922 final Context context = getActivity();
1923
1924 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1925 builder.setMessage(R.string.data_usage_disable_mobile);
1926
1927 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001928 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07001929 public void onClick(DialogInterface dialog, int which) {
1930 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1931 if (target != null) {
1932 // TODO: extend to modify policy enabled flag.
1933 target.setMobileDataEnabled(false);
1934 }
1935 }
1936 });
1937 builder.setNegativeButton(android.R.string.cancel, null);
1938
1939 return builder.create();
1940 }
1941 }
1942
1943 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001944 * Dialog to request user confirmation before setting
Christopher Tate5a64c732012-09-07 13:35:31 -07001945 * {@link android.provider.Settings.Global#DATA_ROAMING}.
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001946 */
1947 public static class ConfirmDataRoamingFragment extends DialogFragment {
1948 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001949 if (!parent.isAdded()) return;
1950
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001951 final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment();
1952 dialog.setTargetFragment(parent, 0);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001953 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_ROAMING);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001954 }
1955
1956 @Override
1957 public Dialog onCreateDialog(Bundle savedInstanceState) {
1958 final Context context = getActivity();
1959
1960 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1961 builder.setTitle(R.string.roaming_reenable_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07001962 if (Utils.hasMultipleUsers(context)) {
1963 builder.setMessage(R.string.roaming_warning_multiuser);
1964 } else {
1965 builder.setMessage(R.string.roaming_warning);
1966 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001967
1968 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001969 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001970 public void onClick(DialogInterface dialog, int which) {
1971 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1972 if (target != null) {
1973 target.setDataRoaming(true);
1974 }
1975 }
1976 });
1977 builder.setNegativeButton(android.R.string.cancel, null);
1978
1979 return builder.create();
1980 }
1981 }
1982
1983 /**
1984 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001985 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001986 */
1987 public static class ConfirmRestrictFragment extends DialogFragment {
1988 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001989 if (!parent.isAdded()) return;
1990
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001991 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1992 dialog.setTargetFragment(parent, 0);
1993 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1994 }
1995
1996 @Override
1997 public Dialog onCreateDialog(Bundle savedInstanceState) {
1998 final Context context = getActivity();
1999
2000 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002001 builder.setTitle(R.string.data_usage_restrict_background_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07002002 if (Utils.hasMultipleUsers(context)) {
2003 builder.setMessage(R.string.data_usage_restrict_background_multiuser);
2004 } else {
2005 builder.setMessage(R.string.data_usage_restrict_background);
2006 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002007
2008 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002009 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002010 public void onClick(DialogInterface dialog, int which) {
2011 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2012 if (target != null) {
2013 target.setRestrictBackground(true);
2014 }
2015 }
2016 });
2017 builder.setNegativeButton(android.R.string.cancel, null);
2018
2019 return builder.create();
2020 }
2021 }
2022
2023 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002024 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
2025 * change has been denied, usually based on
2026 * {@link DataUsageSummary#hasLimitedNetworks()}.
2027 */
2028 public static class DeniedRestrictFragment extends DialogFragment {
2029 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002030 if (!parent.isAdded()) return;
2031
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002032 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
2033 dialog.setTargetFragment(parent, 0);
2034 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
2035 }
2036
2037 @Override
2038 public Dialog onCreateDialog(Bundle savedInstanceState) {
2039 final Context context = getActivity();
2040
2041 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2042 builder.setTitle(R.string.data_usage_app_restrict_background);
2043 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
2044 builder.setPositiveButton(android.R.string.ok, null);
2045
2046 return builder.create();
2047 }
2048 }
2049
2050 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002051 * Dialog to request user confirmation before setting
2052 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
2053 */
2054 public static class ConfirmAppRestrictFragment extends DialogFragment {
2055 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002056 if (!parent.isAdded()) return;
2057
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002058 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2059 dialog.setTargetFragment(parent, 0);
2060 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2061 }
2062
2063 @Override
2064 public Dialog onCreateDialog(Bundle savedInstanceState) {
2065 final Context context = getActivity();
2066
2067 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002068 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2069 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2070
2071 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002072 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002073 public void onClick(DialogInterface dialog, int which) {
2074 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2075 if (target != null) {
2076 target.setAppRestrictBackground(true);
2077 }
2078 }
2079 });
2080 builder.setNegativeButton(android.R.string.cancel, null);
2081
2082 return builder.create();
2083 }
2084 }
2085
2086 /**
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002087 * Dialog to inform user about changing auto-sync setting
2088 */
2089 public static class ConfirmAutoSyncChangeFragment extends DialogFragment {
Amith Yamasani665235f2012-06-07 19:58:34 -07002090 private static final String SAVE_ENABLING = "enabling";
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002091 private boolean mEnabling;
2092
2093 public static void show(DataUsageSummary parent, boolean enabling) {
2094 if (!parent.isAdded()) return;
2095
2096 final ConfirmAutoSyncChangeFragment dialog = new ConfirmAutoSyncChangeFragment();
2097 dialog.mEnabling = enabling;
2098 dialog.setTargetFragment(parent, 0);
2099 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_AUTO_SYNC_CHANGE);
2100 }
2101
2102 @Override
2103 public Dialog onCreateDialog(Bundle savedInstanceState) {
2104 final Context context = getActivity();
Amith Yamasani665235f2012-06-07 19:58:34 -07002105 if (savedInstanceState != null) {
2106 mEnabling = savedInstanceState.getBoolean(SAVE_ENABLING);
2107 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002108
2109 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2110 if (!mEnabling) {
2111 builder.setTitle(R.string.data_usage_auto_sync_off_dialog_title);
2112 builder.setMessage(R.string.data_usage_auto_sync_off_dialog);
2113 } else {
2114 builder.setTitle(R.string.data_usage_auto_sync_on_dialog_title);
2115 builder.setMessage(R.string.data_usage_auto_sync_on_dialog);
2116 }
2117
2118 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
2119 @Override
2120 public void onClick(DialogInterface dialog, int which) {
2121 ContentResolver.setMasterSyncAutomatically(mEnabling);
2122 }
2123 });
2124 builder.setNegativeButton(android.R.string.cancel, null);
2125
2126 return builder.create();
2127 }
Amith Yamasani665235f2012-06-07 19:58:34 -07002128
2129 @Override
2130 public void onSaveInstanceState(Bundle outState) {
2131 super.onSaveInstanceState(outState);
2132 outState.putBoolean(SAVE_ENABLING, mEnabling);
2133 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002134 }
2135
2136 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002137 * Compute default tab that should be selected, based on
2138 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2139 */
2140 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002141 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
2142 if (template == null) return null;
2143
2144 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002145 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002146 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002147 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002148 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002149 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002150 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002151 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002152 return TAB_WIFI;
2153 default:
2154 return null;
2155 }
2156 }
2157
2158 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002159 * Background task that loads {@link UidDetail}, binding to
2160 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002161 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002162 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2163 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002164 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002165 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002166
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002167 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002168 mProvider = checkNotNull(provider);
2169 mItem = checkNotNull(item);
2170 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002171 }
2172
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002173 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002174 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002175 final UidDetailTask existing = (UidDetailTask) target.getTag();
2176 if (existing != null) {
2177 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002178 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002179
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002180 final UidDetail cachedDetail = provider.getUidDetail(item.key, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002181 if (cachedDetail != null) {
2182 bindView(cachedDetail, target);
2183 } else {
2184 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2185 AsyncTask.THREAD_POOL_EXECUTOR));
2186 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002187 }
2188
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002189 private static void bindView(UidDetail detail, View target) {
2190 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2191 final TextView title = (TextView) target.findViewById(android.R.id.title);
2192
2193 if (detail != null) {
2194 icon.setImageDrawable(detail.icon);
2195 title.setText(detail.label);
2196 } else {
2197 icon.setImageDrawable(null);
2198 title.setText(null);
2199 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002200 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002201
2202 @Override
2203 protected void onPreExecute() {
2204 bindView(null, mTarget);
2205 }
2206
2207 @Override
2208 protected UidDetail doInBackground(Void... params) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002209 return mProvider.getUidDetail(mItem.key, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002210 }
2211
2212 @Override
2213 protected void onPostExecute(UidDetail result) {
2214 bindView(result, mTarget);
2215 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002216 }
2217
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002218 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002219 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002220 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002221 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002222 if (TEST_RADIOS) {
2223 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2224 }
2225
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002226 final ConnectivityManager conn = ConnectivityManager.from(context);
2227 final TelephonyManager tele = TelephonyManager.from(context);
2228
2229 // require both supported network and ready SIM
2230 return conn.isNetworkSupported(TYPE_MOBILE) && tele.getSimState() == SIM_STATE_READY;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002231 }
2232
2233 /**
2234 * Test if device has a mobile 4G data radio.
2235 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002236 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002237 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2238 return false;
2239 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002240 if (TEST_RADIOS) {
2241 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2242 }
2243
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002244 final ConnectivityManager conn = ConnectivityManager.from(context);
2245 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002246
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002247 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Wink Saville55434042012-06-14 12:33:43 -07002248 final boolean hasLte = (tele.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE)
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002249 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002250 return hasWimax || hasLte;
2251 }
2252
2253 /**
2254 * Test if device has a Wi-Fi data radio.
2255 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002256 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002257 if (TEST_RADIOS) {
2258 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2259 }
2260
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002261 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002262 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002263 }
2264
2265 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002266 * Test if device has an ethernet network connection.
2267 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002268 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002269 if (TEST_RADIOS) {
2270 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2271 }
2272
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002273 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002274 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2275
2276 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002277 if (mStatsSession != null) {
2278 try {
2279 ethernetBytes = mStatsSession.getSummaryForNetwork(
2280 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2281 .getTotalBytes();
2282 } catch (RemoteException e) {
2283 throw new RuntimeException(e);
2284 }
2285 } else {
2286 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002287 }
2288
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002289 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002290 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002291 }
2292
2293 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002294 * Inflate a {@link Preference} style layout, adding the given {@link View}
2295 * widget into {@link android.R.id#widget_frame}.
2296 */
2297 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2298 final View view = inflater.inflate(R.layout.preference, root, false);
2299 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2300 android.R.id.widget_frame);
2301 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2302 return view;
2303 }
2304
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002305 private static View inflateAppTitle(
2306 LayoutInflater inflater, ViewGroup root, CharSequence label) {
2307 final TextView view = (TextView) inflater.inflate(
2308 R.layout.data_usage_app_title, root, false);
2309 view.setText(label);
2310 return view;
2311 }
2312
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002313 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002314 * Test if any networks are currently limited.
2315 */
2316 private boolean hasLimitedNetworks() {
2317 return !buildLimitedNetworksList().isEmpty();
2318 }
2319
2320 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002321 * Build string describing currently limited networks, which defines when
2322 * background data is restricted.
2323 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002324 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002325 private CharSequence buildLimitedNetworksString() {
2326 final List<CharSequence> limited = buildLimitedNetworksList();
2327
2328 // handle case where no networks limited
2329 if (limited.isEmpty()) {
2330 limited.add(getText(R.string.data_usage_list_none));
2331 }
2332
2333 return TextUtils.join(limited);
2334 }
2335
2336 /**
2337 * Build list of currently limited networks, which defines when background
2338 * data is restricted.
2339 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002340 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002341 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002342 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002343
2344 // build combined list of all limited networks
2345 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002346
2347 final TelephonyManager tele = TelephonyManager.from(context);
2348 if (tele.getSimState() == SIM_STATE_READY) {
2349 final String subscriberId = getActiveSubscriberId(context);
2350 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2351 limited.add(getText(R.string.data_usage_list_mobile));
2352 }
2353 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2354 limited.add(getText(R.string.data_usage_tab_3g));
2355 }
2356 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2357 limited.add(getText(R.string.data_usage_tab_4g));
2358 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002359 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002360
2361 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002362 limited.add(getText(R.string.data_usage_tab_wifi));
2363 }
2364 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2365 limited.add(getText(R.string.data_usage_tab_ethernet));
2366 }
2367
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002368 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002369 }
2370
2371 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002372 * Inset both selector and divider {@link Drawable} on the given
2373 * {@link ListView} by the requested dimensions.
2374 */
2375 private static void insetListViewDrawables(ListView view, int insetSide) {
2376 final Drawable selector = view.getSelector();
2377 final Drawable divider = view.getDivider();
2378
2379 // fully unregister these drawables so callbacks can be maintained after
2380 // wrapping below.
2381 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2382 view.setSelector(stub);
2383 view.setDivider(stub);
2384
2385 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2386 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2387 }
2388
2389 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002390 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002391 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002392 */
2393 private static void setPreferenceTitle(View parent, int resId) {
2394 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2395 title.setText(resId);
2396 }
2397
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002398 /**
2399 * Set {@link android.R.id#summary} for a preference view inflated with
2400 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2401 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002402 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002403 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2404 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002405 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002406 }
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002407
2408 /**
2409 * For search
2410 */
2411 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -07002412 new BaseSearchIndexProvider() {
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002413 @Override
2414 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
2415 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
2416
2417 final Resources res = context.getResources();
2418
2419 // Add fragment title
2420 SearchIndexableRaw data = new SearchIndexableRaw(context);
2421 data.title = res.getString(R.string.data_usage_summary_title);
2422 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2423 result.add(data);
2424
2425 // Mobile data
2426 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002427 data.key = DATA_USAGE_ENABLE_MOBILE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002428 data.title = res.getString(R.string.data_usage_enable_mobile);
2429 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2430 result.add(data);
2431
2432 // Set mobile data limit
2433 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002434 data.key = DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002435 data.title = res.getString(R.string.data_usage_disable_mobile_limit);
2436 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2437 result.add(data);
2438
Fabrice Di Megliof2a52262014-04-17 17:20:27 -07002439 // Data usage cycle
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002440 data = new SearchIndexableRaw(context);
Fabrice Di Meglio2169c882014-04-18 15:18:40 -07002441 data.key = DATA_USAGE_CYCLE_KEY;
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002442 data.title = res.getString(R.string.data_usage_cycle);
2443 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2444 result.add(data);
2445
2446 return result;
2447 }
2448 };
2449
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002450}