blob: b5299ee6ed980594f9e750106ef8cba7243227e9 [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
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700184 private static final int LOADER_CHART_DATA = 2;
185 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700186
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700187 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700188 private INetworkStatsService mStatsService;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700189 private NetworkPolicyManager mPolicyManager;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700190 private ConnectivityManager mConnService;
191
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700192 private INetworkStatsSession mStatsSession;
193
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700194 private static final String PREF_FILE = "data_usage";
195 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700196 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700197
198 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700199
Jeff Sharkey8a503642011-06-10 13:31:21 -0700200 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700201 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700202 private TabWidget mTabWidget;
203 private ListView mListView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700204 private DataUsageAdapter mAdapter;
205
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700206 /** Distance to inset content from sides, when needed. */
207 private int mInsetSide = 0;
208
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700209 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700210
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700211 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700212 private LinearLayout mNetworkSwitches;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700213 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700214 private View mDataEnabledView;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700215 private CheckBox mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700216 private View mDisableAtLimitView;
217
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700218 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700219 private Spinner mCycleSpinner;
220 private CycleAdapter mCycleAdapter;
221
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700222 private ChartDataUsageView mChart;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700223 private TextView mUsageSummary;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700224 private TextView mEmpty;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700225
226 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700227 private ImageView mAppIcon;
228 private ViewGroup mAppTitles;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700229 private PieChartView mAppPieChart;
230 private TextView mAppForeground;
231 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700232 private Button mAppSettings;
233
234 private LinearLayout mAppSwitches;
235 private CheckBox mAppRestrict;
236 private View mAppRestrictView;
237
Jeff Sharkey8a503642011-06-10 13:31:21 -0700238 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700239 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700240
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700241 private NetworkTemplate mTemplate;
242 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700243
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700244 private AppItem mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700245
246 private Intent mAppSettingsIntent;
247
Jeff Sharkeya662e492011-06-18 21:57:06 -0700248 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700249
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700250 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700251 private String mIntentTab = null;
252
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700253 private MenuItem mMenuDataRoaming;
254 private MenuItem mMenuRestrictBackground;
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700255 private MenuItem mMenuAutoSync;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700256
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700257 /** Flag used to ignore listeners during binding. */
258 private boolean mBinding;
259
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700260 private UidDetailProvider mUidDetailProvider;
261
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700262 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700263 public void onCreate(Bundle savedInstanceState) {
264 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700265 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700266
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700267 mNetworkService = INetworkManagementService.Stub.asInterface(
268 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700269 mStatsService = INetworkStatsService.Stub.asInterface(
270 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700271 mPolicyManager = NetworkPolicyManager.from(context);
272 mConnService = ConnectivityManager.from(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700273
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700274 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700275
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700276 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700277 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700278
Jaewan Kimffce9c12013-03-19 16:53:45 +0900279 try {
Jeff Sharkey78ff1b82013-09-09 18:42:33 -0700280 if (!mNetworkService.isBandwidthControlEnabled()) {
281 Log.w(TAG, "No bandwidth control; leaving");
282 getActivity().finish();
283 }
284 } catch (RemoteException e) {
285 Log.w(TAG, "No bandwidth control; leaving");
286 getActivity().finish();
287 }
288
289 try {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900290 mStatsSession = mStatsService.openSession();
291 } catch (RemoteException e) {
292 throw new RuntimeException(e);
293 }
294
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700295 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700296 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700297
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700298 // override preferences when no mobile radio
299 if (!hasReadyMobileRadio(context)) {
Jaewan Kimffce9c12013-03-19 16:53:45 +0900300 mShowWifi = true;
301 mShowEthernet = true;
Jeff Sharkey0bc5b932012-05-03 15:02:06 -0700302 }
303
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700304 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700305 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700306
Jeff Sharkey8a503642011-06-10 13:31:21 -0700307 @Override
308 public View onCreateView(LayoutInflater inflater, ViewGroup container,
309 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700310
Jeff Sharkey8a503642011-06-10 13:31:21 -0700311 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700312 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
313
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700314 mUidDetailProvider = new UidDetailProvider(context);
315
Jeff Sharkey8a503642011-06-10 13:31:21 -0700316 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700317 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700318 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
319 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700320
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700321 // decide if we need to manually inset our content, or if we should rely
322 // on parent container for inset.
323 final boolean shouldInset = mListView.getScrollBarStyle()
324 == View.SCROLLBARS_OUTSIDE_OVERLAY;
Amith Yamasani56f51a82013-08-05 10:07:23 -0700325 mInsetSide = 0;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700326
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700327 // adjust padding around tabwidget as needed
Amith Yamasani56f51a82013-08-05 10:07:23 -0700328 prepareCustomPreferencesList(container, view, mListView, false);
Jeff Sharkey5d706792011-09-08 18:57:17 -0700329
Jeff Sharkey8a503642011-06-10 13:31:21 -0700330 mTabHost.setup();
331 mTabHost.setOnTabChangedListener(mTabListener);
332
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700333 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700334 mHeader.setClickable(true);
335
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700336 mListView.addHeaderView(new View(context), null, true);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700337 mListView.addHeaderView(mHeader, null, true);
338 mListView.setItemsCanFocus(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700339
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700340 if (mInsetSide > 0) {
341 // inset selector and divider drawables
342 insetListViewDrawables(mListView, mInsetSide);
Fabrice Di Megliob27223f2013-01-15 18:54:11 -0800343 mHeader.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700344 }
345
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700346 {
347 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700348 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
349 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700350 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700351
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700352 mDataEnabled = new Switch(inflater.getContext());
353 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700354 mDataEnabledView.setTag("data_usage_enable_mobile");
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700355 mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener);
356 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700357
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700358 mDisableAtLimit = new CheckBox(inflater.getContext());
359 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700360 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700361 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700362 mDisableAtLimitView.setTag("data_usage_disable_mobile_limit");
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700363 mDisableAtLimitView.setClickable(true);
364 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700365 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
366 mNetworkSwitches.addView(mDisableAtLimitView);
367 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700368
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700369 // bind cycle dropdown
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700370 mCycleView = mHeader.findViewById(R.id.cycles);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700371 mCycleView.setTag("data_usage_cycle");
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700372 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700373 mCycleAdapter = new CycleAdapter(context);
374 mCycleSpinner.setAdapter(mCycleAdapter);
375 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
376
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700377 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700378 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800379 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700380
381 {
382 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700383 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700384 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
385 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700386 mAppPieChart = (PieChartView) mAppDetail.findViewById(R.id.app_pie_chart);
387 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
388 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700389 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700390
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700391 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700392 mAppSettings.setOnClickListener(mAppSettingsListener);
393
394 mAppRestrict = new CheckBox(inflater.getContext());
395 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700396 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700397 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700398 mAppRestrictView.setClickable(true);
399 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700400 mAppRestrictView.setOnClickListener(mAppRestrictListener);
401 mAppSwitches.addView(mAppRestrictView);
402 }
403
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700404 mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700405 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700406
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700407 mAdapter = new DataUsageAdapter(mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700408 mListView.setOnItemClickListener(mListListener);
409 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700410
411 return view;
412 }
413
414 @Override
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700415 public void onViewStateRestored(Bundle savedInstanceState) {
416 super.onViewStateRestored(savedInstanceState);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700417
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700418 // pick default tab based on incoming intent
419 final Intent intent = getActivity().getIntent();
420 mIntentTab = computeTabFromIntent(intent);
421
Jeff Sharkey8a503642011-06-10 13:31:21 -0700422 // this kicks off chain reaction which creates tabs, binds the body to
423 // selected network, and binds chart, cycles and detail list.
424 updateTabs();
Fabrice Di Megliof2a52262014-04-17 17:20:27 -0700425 }
426
427 @Override
428 public void onResume() {
429 super.onResume();
430
431 getView().post(new Runnable() {
432 @Override
433 public void run() {
434 highlightViewIfNeeded();
435 }
436 });
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700437
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700438 // kick off background task to update stats
439 new AsyncTask<Void, Void, Void>() {
440 @Override
441 protected Void doInBackground(Void... params) {
442 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700443 // wait a few seconds before kicking off
444 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700445 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700446 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700447 } catch (RemoteException e) {
448 }
449 return null;
450 }
451
452 @Override
453 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700454 if (isAdded()) {
455 updateBody();
456 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700457 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700458 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700459 }
460
Jeff Sharkey8a503642011-06-10 13:31:21 -0700461 @Override
462 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
463 inflater.inflate(R.menu.data_usage, menu);
464 }
465
466 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700467 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700468 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700469 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700470 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700471
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700472 mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700473 mMenuDataRoaming.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700474 mMenuDataRoaming.setChecked(getDataRoaming());
475
476 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700477 mMenuRestrictBackground.setVisible(
478 hasReadyMobileRadio(context) && isOwner && !appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700479 mMenuRestrictBackground.setChecked(mPolicyManager.getRestrictBackground());
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700480
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700481 mMenuAutoSync = menu.findItem(R.id.data_usage_menu_auto_sync);
482 mMenuAutoSync.setChecked(ContentResolver.getMasterSyncAutomatically());
Jeff Sharkeyfda48e32012-09-18 15:03:47 -0700483 mMenuAutoSync.setVisible(!appDetailMode);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700484
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700485 final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g);
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700486 split4g.setVisible(hasReadyMobile4gRadio(context) && isOwner && !appDetailMode);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700487 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700488
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700489 final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700490 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700491 showWifi.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700492 showWifi.setChecked(mShowWifi);
493 } else {
494 showWifi.setVisible(false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700495 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700496
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700497 final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700498 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700499 showEthernet.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700500 showEthernet.setChecked(mShowEthernet);
501 } else {
502 showEthernet.setVisible(false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700503 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700504
505 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
506 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700507 metered.setVisible(!appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700508 } else {
509 metered.setVisible(false);
510 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700511
512 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
513 String helpUrl;
514 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800515 HelpUtils.prepareHelpMenuItem(context, help, helpUrl);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700516 } else {
517 help.setVisible(false);
518 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700519 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700520
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700521 @Override
522 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700523 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700524 case R.id.data_usage_menu_roaming: {
525 final boolean dataRoaming = !item.isChecked();
526 if (dataRoaming) {
527 ConfirmDataRoamingFragment.show(this);
528 } else {
529 // no confirmation to disable roaming
530 setDataRoaming(false);
531 }
532 return true;
533 }
534 case R.id.data_usage_menu_restrict_background: {
535 final boolean restrictBackground = !item.isChecked();
536 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800537 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700538 } else {
539 // no confirmation to drop restriction
540 setRestrictBackground(false);
541 }
542 return true;
543 }
544 case R.id.data_usage_menu_split_4g: {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700545 final boolean mobileSplit = !item.isChecked();
Jeff Sharkeya662e492011-06-18 21:57:06 -0700546 setMobilePolicySplit(mobileSplit);
547 item.setChecked(isMobilePolicySplit());
Jeff Sharkey8a503642011-06-10 13:31:21 -0700548 updateTabs();
549 return true;
550 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700551 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700552 mShowWifi = !item.isChecked();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700553 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700554 item.setChecked(mShowWifi);
555 updateTabs();
556 return true;
557 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700558 case R.id.data_usage_menu_show_ethernet: {
559 mShowEthernet = !item.isChecked();
560 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
561 item.setChecked(mShowEthernet);
562 updateTabs();
563 return true;
564 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700565 case R.id.data_usage_menu_metered: {
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800566 final SettingsActivity sa = (SettingsActivity) getActivity();
567 sa.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700568 R.string.data_usage_metered_title, null, this, 0);
569 return true;
570 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700571 case R.id.data_usage_menu_auto_sync: {
Guang Zhu167ba2a2012-10-19 17:55:05 -0700572 if (ActivityManager.isUserAMonkey()) {
573 Log.d("SyncState", "ignoring monkey's attempt to flip global sync state");
574 } else {
575 ConfirmAutoSyncChangeFragment.show(this, !item.isChecked());
576 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700577 return true;
578 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700579 }
580 return false;
581 }
582
Jeff Sharkey94a90952011-06-13 22:31:09 -0700583 @Override
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700584 public void onDestroy() {
Jeff Sharkey94a90952011-06-13 22:31:09 -0700585 mDataEnabledView = null;
586 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700587
588 mUidDetailProvider.clearCache();
589 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700590
591 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700592
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800593 super.onDestroy();
594 }
595
Jeff Sharkey8a503642011-06-10 13:31:21 -0700596 /**
Jeff Sharkey92811822012-05-04 11:47:29 -0700597 * Build and assign {@link LayoutTransition} to various containers. Should
598 * only be assigned after initial layout is complete.
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700599 */
Jeff Sharkey92811822012-05-04 11:47:29 -0700600 private void ensureLayoutTransitions() {
601 // skip when already setup
602 if (mChart.getLayoutTransition() != null) return;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700603
Jeff Sharkey92811822012-05-04 11:47:29 -0700604 mTabsContainer.setLayoutTransition(buildLayoutTransition());
605 mHeader.setLayoutTransition(buildLayoutTransition());
606 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700607
Jeff Sharkey92811822012-05-04 11:47:29 -0700608 final LayoutTransition chartTransition = buildLayoutTransition();
609 chartTransition.disableTransitionType(LayoutTransition.APPEARING);
610 chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
611 mChart.setLayoutTransition(chartTransition);
612 }
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700613
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700614 private static LayoutTransition buildLayoutTransition() {
615 final LayoutTransition transition = new LayoutTransition();
616 if (TEST_ANIM) {
617 transition.setDuration(1500);
618 }
619 transition.setAnimateParentHierarchy(false);
620 return transition;
621 }
622
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700623 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700624 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700625 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
626 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700627 */
628 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700629 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700630 mTabHost.clearAllTabs();
631
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700632 final boolean mobileSplit = isMobilePolicySplit();
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700633 if (mobileSplit && hasReadyMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700634 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
635 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700636 } else if (hasReadyMobileRadio(context)) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700637 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700638 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700639 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700640 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
641 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700642 if (mShowEthernet && hasEthernet(context)) {
643 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
644 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700645
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700646 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700647 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
648 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
649 if (mIntentTab != null) {
650 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700651 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700652 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700653 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700654 mTabHost.setCurrentTabByTag(mIntentTab);
655 }
656 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700657 } else if (noTabs) {
658 // no usable tabs, so hide body
659 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700660 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700661 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700662 }
663 }
664
Jeff Sharkey8a503642011-06-10 13:31:21 -0700665 /**
666 * Factory that provide empty {@link View} to make {@link TabHost} happy.
667 */
668 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700669 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700670 public View createTabContent(String tag) {
671 return new View(mTabHost.getContext());
672 }
673 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700674
Jeff Sharkey8a503642011-06-10 13:31:21 -0700675 /**
676 * Build {@link TabSpec} with thin indicator, and empty content.
677 */
678 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700679 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
680 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700681 }
682
683 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700684 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700685 public void onTabChanged(String tabId) {
686 // user changed tab; update body
687 updateBody();
688 }
689 };
690
691 /**
692 * Update body content based on current tab. Loads
693 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
694 * binds them to visible controls.
695 */
696 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700697 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700698 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700699
Jeff Sharkeya662e492011-06-18 21:57:06 -0700700 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700701 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700702 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700703
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700704 if (currentTab == null) {
705 Log.w(TAG, "no tab selected; hiding body");
706 mListView.setVisibility(View.GONE);
707 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700708 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700709 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700710 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700711
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700712 mCurrentTab = currentTab;
713
Jeff Sharkey8a503642011-06-10 13:31:21 -0700714 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
715
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700716 mDataEnabledView.setVisibility(isOwner ? View.VISIBLE : View.GONE);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700717
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700718 // TODO: remove mobile tabs when SIM isn't ready
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700719
Jeff Sharkey8a503642011-06-10 13:31:21 -0700720 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700721 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
722 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700723 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700724
725 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700726 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
727 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
728 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700729 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700730
731 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700732 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
733 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
734 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700735 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700736
737 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700738 // wifi doesn't have any controls
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700739 mDataEnabledView.setVisibility(View.GONE);
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700740 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700741 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700742
743 } else if (TAB_ETHERNET.equals(currentTab)) {
744 // ethernet doesn't have any controls
745 mDataEnabledView.setVisibility(View.GONE);
746 mDisableAtLimitView.setVisibility(View.GONE);
747 mTemplate = buildTemplateEthernet();
748
749 } else {
750 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700751 }
752
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700753 // kick off loader for network history
754 // TODO: consider chaining two loaders together instead of reloading
755 // network history when showing app detail.
756 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700757 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700758
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700759 // detail mode can change visible menus, invalidate
760 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700761
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700762 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700763 }
764
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700765 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700766 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700767 }
768
769 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700770 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700771 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700772 */
773 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700774 final Context context = getActivity();
775 final PackageManager pm = context.getPackageManager();
776 final LayoutInflater inflater = getActivity().getLayoutInflater();
777
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700778 if (isAppDetailMode()) {
779 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700780 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700781 } else {
782 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700783 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700784
785 // hide detail stats when not in detail mode
786 mChart.bindDetailNetworkStats(null);
787 return;
788 }
789
790 // remove warning/limit sweeps while in detail mode
791 mChart.bindNetworkPolicy(null);
792
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700793 // show icon and all labels appearing under this app
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700794 final int uid = mCurrentApp.key;
795 final UidDetail detail = mUidDetailProvider.getUidDetail(uid, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700796 mAppIcon.setImageDrawable(detail.icon);
797
798 mAppTitles.removeAllViews();
799 if (detail.detailLabels != null) {
800 for (CharSequence label : detail.detailLabels) {
801 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label));
802 }
803 } else {
804 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label));
805 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700806
807 // enable settings button when package provides it
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700808 final String[] packageNames = pm.getPackagesForUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700809 if (packageNames != null && packageNames.length > 0) {
810 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700811 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
812
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700813 // Search for match across all packages
814 boolean matchFound = false;
815 for (String packageName : packageNames) {
816 mAppSettingsIntent.setPackage(packageName);
817 if (pm.resolveActivity(mAppSettingsIntent, 0) != null) {
818 matchFound = true;
819 break;
820 }
821 }
822
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700823 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -0700824 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700825
826 } else {
827 mAppSettingsIntent = null;
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700828 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700829 }
830
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700831 updateDetailData();
832
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700833 if (UserHandle.isApp(uid) && !mPolicyManager.getRestrictBackground()
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700834 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700835 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800836 setPreferenceSummary(mAppRestrictView,
837 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700838
839 mAppRestrictView.setVisibility(View.VISIBLE);
840 mAppRestrict.setChecked(getAppRestrictBackground());
841
842 } else {
843 mAppRestrictView.setVisibility(View.GONE);
844 }
845 }
846
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700847 private void setPolicyWarningBytes(long warningBytes) {
848 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700849 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700850 updatePolicy(false);
851 }
852
853 private void setPolicyLimitBytes(long limitBytes) {
854 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700855 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700856 updatePolicy(false);
857 }
858
Jeff Sharkey28130d92011-09-02 16:10:24 -0700859 /**
860 * Local cache of value, used to work around delay when
861 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
862 */
863 private Boolean mMobileDataEnabled;
864
865 private boolean isMobileDataEnabled() {
866 if (mMobileDataEnabled != null) {
867 // TODO: deprecate and remove this once enabled flag is on policy
868 return mMobileDataEnabled;
869 } else {
870 return mConnService.getMobileDataEnabled();
871 }
872 }
873
874 private void setMobileDataEnabled(boolean enabled) {
875 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
876 mConnService.setMobileDataEnabled(enabled);
877 mMobileDataEnabled = enabled;
878 updatePolicy(false);
879 }
880
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700881 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700882 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked()
883 && ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700884 }
885
886 private boolean isBandwidthControlEnabled() {
887 try {
888 return mNetworkService.isBandwidthControlEnabled();
889 } catch (RemoteException e) {
890 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
891 return false;
892 }
893 }
894
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700895 private boolean getDataRoaming() {
896 final ContentResolver resolver = getActivity().getContentResolver();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800897 return android.provider.Settings.Global.getInt(resolver,
898 android.provider.Settings.Global.DATA_ROAMING, 0) != 0;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700899 }
900
901 private void setDataRoaming(boolean enabled) {
902 // TODO: teach telephony DataConnectionTracker to watch and apply
903 // updates when changed.
904 final ContentResolver resolver = getActivity().getContentResolver();
Fabrice Di Meglio263bcc82014-01-17 19:17:58 -0800905 android.provider.Settings.Global.putInt(resolver,
906 android.provider.Settings.Global.DATA_ROAMING, enabled ? 1 : 0);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700907 mMenuDataRoaming.setChecked(enabled);
908 }
909
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700910 public void setRestrictBackground(boolean restrictBackground) {
911 mPolicyManager.setRestrictBackground(restrictBackground);
912 mMenuRestrictBackground.setChecked(restrictBackground);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700913 }
914
915 private boolean getAppRestrictBackground() {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700916 final int uid = mCurrentApp.key;
917 final int uidPolicy = mPolicyManager.getUidPolicy(uid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700918 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
919 }
920
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700921 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700922 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700923 final int uid = mCurrentApp.key;
924 mPolicyManager.setUidPolicy(
925 uid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700926 mAppRestrict.setChecked(restrictBackground);
927 }
928
Jeff Sharkey8a503642011-06-10 13:31:21 -0700929 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700930 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
931 * current {@link #mTemplate}.
932 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700933 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700934 if (isAppDetailMode()) {
935 mNetworkSwitches.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700936 } else {
937 mNetworkSwitches.setVisibility(View.VISIBLE);
938 }
939
Jeff Sharkey28130d92011-09-02 16:10:24 -0700940 // TODO: move enabled state directly into policy
941 if (TAB_MOBILE.equals(mCurrentTab)) {
942 mBinding = true;
943 mDataEnabled.setChecked(isMobileDataEnabled());
944 mBinding = false;
945 }
946
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700947 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
948 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700949 mDisableAtLimitView.setVisibility(View.VISIBLE);
950 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700951 if (!isAppDetailMode()) {
952 mChart.bindNetworkPolicy(policy);
953 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700954
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700955 } else {
956 // controls are disabled; don't bind warning/limit sweeps
957 mDisableAtLimitView.setVisibility(View.GONE);
958 mChart.bindNetworkPolicy(null);
959 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700960
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700961 if (refreshCycle) {
962 // generate cycle list based on policy and available history
963 updateCycleList(policy);
964 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700965 }
966
967 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700968 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
969 * and available {@link NetworkStatsHistory} data. Always selects the newest
970 * item, updating the inspection range on {@link #mChart}.
971 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700972 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700973 // stash away currently selected cycle to try restoring below
974 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700975 mCycleAdapter.clear();
976
977 final Context context = mCycleSpinner.getContext();
978
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700979 long historyStart = Long.MAX_VALUE;
980 long historyEnd = Long.MIN_VALUE;
981 if (mChartData != null) {
982 historyStart = mChartData.network.getStart();
983 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700984 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700985
Jeff Sharkey461842a2011-09-25 18:22:48 -0700986 final long now = System.currentTimeMillis();
987 if (historyStart == Long.MAX_VALUE) historyStart = now;
988 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700989
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700990 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700991 if (policy != null) {
992 // find the next cycle boundary
993 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700994
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700995 // walk backwards, generating all valid cycle ranges
996 while (cycleEnd > historyStart) {
997 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
998 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
999 + historyStart);
1000 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1001 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001002 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001003 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001004
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001005 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001006 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001007 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001008
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001009 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001010 // no policy defined cycles; show entry for each four-week period
1011 long cycleEnd = historyEnd;
1012 while (cycleEnd > historyStart) {
1013 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
1014 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1015 cycleEnd = cycleStart;
1016 }
1017
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001018 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001019 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001020
1021 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001022 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001023 final int position = mCycleAdapter.findNearestPosition(previousItem);
1024 mCycleSpinner.setSelection(position);
1025
1026 // only force-update cycle when changed; skipping preserves any
1027 // user-defined inspection region.
1028 final CycleItem selectedItem = mCycleAdapter.getItem(position);
1029 if (!Objects.equal(selectedItem, previousItem)) {
1030 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1031 } else {
1032 // but still kick off loader for detailed list
1033 updateDetailData();
1034 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001035 } else {
1036 updateDetailData();
1037 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001038 }
1039
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001040 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001041 @Override
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001042 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
1043 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001044
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001045 final boolean dataEnabled = isChecked;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001046 final String currentTab = mCurrentTab;
1047 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001048 if (dataEnabled) {
1049 setMobileDataEnabled(true);
1050 } else {
1051 // disabling data; show confirmation dialog which eventually
1052 // calls setMobileDataEnabled() once user confirms.
1053 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1054 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001055 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001056
Jeff Sharkey28130d92011-09-02 16:10:24 -07001057 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001058 }
1059 };
1060
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001061 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001062 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001063 public void onClick(View v) {
1064 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001065 if (disableAtLimit) {
1066 // enabling limit; show confirmation dialog which eventually
1067 // calls setPolicyLimitBytes() once user confirms.
1068 ConfirmLimitFragment.show(DataUsageSummary.this);
1069 } else {
1070 setPolicyLimitBytes(LIMIT_DISABLED);
1071 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001072 }
1073 };
1074
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001075 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001076 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001077 public void onClick(View v) {
1078 final boolean restrictBackground = !mAppRestrict.isChecked();
1079
1080 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001081 // enabling restriction; show confirmation dialog which
1082 // eventually calls setRestrictBackground() once user
1083 // confirms.
1084 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001085 } else {
1086 setAppRestrictBackground(false);
1087 }
1088 }
1089 };
1090
1091 private OnClickListener mAppSettingsListener = new OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001092 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001093 public void onClick(View v) {
Jeff Sharkeyd92e0412012-04-24 11:35:43 -07001094 if (!isAdded()) return;
1095
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001096 // TODO: target torwards entire UID instead of just first package
1097 startActivity(mAppSettingsIntent);
1098 }
1099 };
1100
Jeff Sharkey8a503642011-06-10 13:31:21 -07001101 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001102 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001103 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001104 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001105 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001106
1107 // TODO: sigh, remove this hack once we understand 6450986
1108 if (mUidDetailProvider == null || app == null) return;
1109
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001110 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001111 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001112 }
1113 };
1114
1115 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001116 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001117 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1118 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1119 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001120 // show cycle editor; will eventually call setPolicyCycleDay()
1121 // when user finishes editing.
1122 CycleEditorFragment.show(DataUsageSummary.this);
1123
1124 // reset spinner to something other than "change cycle..."
1125 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001126
1127 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001128 if (LOGD) {
1129 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1130 + cycle.end + "]");
1131 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001132
1133 // update chart to show selected cycle, and update detail data
1134 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001135 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001136
1137 updateDetailData();
1138 }
1139 }
1140
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001141 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001142 public void onNothingSelected(AdapterView<?> parent) {
1143 // ignored
1144 }
1145 };
1146
1147 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001148 * Update details based on {@link #mChart} inspection range depending on
1149 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1150 * of applications data usage, and when {@link #isAppDetailMode()} update
1151 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001152 */
1153 private void updateDetailData() {
1154 if (LOGD) Log.d(TAG, "updateDetailData()");
1155
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001156 final long start = mChart.getInspectStart();
1157 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001158 final long now = System.currentTimeMillis();
1159
1160 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001161
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001162 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001163 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001164 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001165 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001166 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001167 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001168 final long foregroundBytes = entry.rxBytes + entry.txBytes;
1169
1170 mAppPieChart.setOriginAngle(175);
1171
1172 mAppPieChart.removeAllSlices();
1173 mAppPieChart.addSlice(foregroundBytes, Color.parseColor("#d88d3a"));
1174 mAppPieChart.addSlice(defaultBytes, Color.parseColor("#666666"));
1175
1176 mAppPieChart.generatePath();
1177
1178 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1179 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1180
1181 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001182 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001183
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001184 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001185
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001186 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001187 if (mChartData != null) {
1188 entry = mChartData.network.getValues(start, end, now, null);
1189 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001190
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001191 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001192 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001193 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001194 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001195
1196 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1197 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001198 final String rangePhrase = formatDateRange(context, start, end);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001199
Jeff Sharkeye557c332012-04-13 16:04:07 -07001200 final int summaryRes;
László Dávid0b8bf4e2012-10-24 23:31:47 +02001201 if (TAB_MOBILE.equals(mCurrentTab) || TAB_3G.equals(mCurrentTab)
1202 || TAB_4G.equals(mCurrentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001203 summaryRes = R.string.data_usage_total_during_range_mobile;
1204 } else {
1205 summaryRes = R.string.data_usage_total_during_range;
1206 }
1207
1208 mUsageSummary.setText(getString(summaryRes, totalPhrase, rangePhrase));
Jeff Sharkey92811822012-05-04 11:47:29 -07001209
1210 // initial layout is finished above, ensure we have transitions
1211 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001212 }
1213
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001214 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1215 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001216 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001217 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001218 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001219 }
1220
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001221 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001222 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1223 mChartData = data;
1224 mChart.bindNetworkStats(mChartData.network);
1225 mChart.bindDetailNetworkStats(mChartData.detail);
1226
1227 // calcuate policy cycles based on available data
1228 updatePolicy(true);
1229 updateAppDetail();
1230
1231 // force scroll to top of body when showing detail
1232 if (mChartData.detail != null) {
1233 mListView.smoothScrollToPosition(0);
1234 }
1235 }
1236
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001237 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001238 public void onLoaderReset(Loader<ChartData> loader) {
1239 mChartData = null;
1240 mChart.bindNetworkStats(null);
1241 mChart.bindDetailNetworkStats(null);
1242 }
1243 };
1244
1245 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001246 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001247 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001248 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001249 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001250 }
1251
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001252 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001253 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001254 final int[] restrictedUids = mPolicyManager.getUidsWithPolicy(
Jeff Sharkeye557c332012-04-13 16:04:07 -07001255 POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001256 mAdapter.bindStats(data, restrictedUids);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001257 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001258 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001259
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001260 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001261 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001262 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001263 updateEmptyVisible();
1264 }
1265
1266 private void updateEmptyVisible() {
1267 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1268 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001269 }
1270 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001271
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001272 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001273 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001274 final Context context = getActivity();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001275 if (hasReadyMobileRadio(context)) {
1276 final TelephonyManager tele = TelephonyManager.from(context);
1277 return mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001278 } else {
1279 return false;
1280 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001281 }
1282
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001283 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001284 private void setMobilePolicySplit(boolean split) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001285 final Context context = getActivity();
1286 if (hasReadyMobileRadio(context)) {
1287 final TelephonyManager tele = TelephonyManager.from(context);
1288 mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split);
1289 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001290 }
1291
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001292 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001293 final TelephonyManager tele = TelephonyManager.from(context);
1294 final String actualSubscriberId = tele.getSubscriberId();
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -08001295 return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001296 }
1297
Jeff Sharkey8a503642011-06-10 13:31:21 -07001298 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001299 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001300 public void onInspectRangeChanged() {
1301 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
1302 updateDetailData();
1303 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001304
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001305 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001306 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001307 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001308 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001309
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001310 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001311 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001312 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001313 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001314
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001315 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001316 public void requestWarningEdit() {
1317 WarningEditorFragment.show(DataUsageSummary.this);
1318 }
1319
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001320 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001321 public void requestLimitEdit() {
1322 LimitEditorFragment.show(DataUsageSummary.this);
1323 }
1324 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001325
1326 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001327 * List item that reflects a specific data usage cycle.
1328 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001329 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001330 public CharSequence label;
1331 public long start;
1332 public long end;
1333
Jeff Sharkey8a503642011-06-10 13:31:21 -07001334 CycleItem(CharSequence label) {
1335 this.label = label;
1336 }
1337
1338 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001339 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001340 this.start = start;
1341 this.end = end;
1342 }
1343
Jeff Sharkey8a503642011-06-10 13:31:21 -07001344 @Override
1345 public String toString() {
1346 return label.toString();
1347 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001348
1349 @Override
1350 public boolean equals(Object o) {
1351 if (o instanceof CycleItem) {
1352 final CycleItem another = (CycleItem) o;
1353 return start == another.start && end == another.end;
1354 }
1355 return false;
1356 }
1357
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001358 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001359 public int compareTo(CycleItem another) {
1360 return Long.compare(start, another.start);
1361 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001362 }
1363
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001364 private static final StringBuilder sBuilder = new StringBuilder(50);
1365 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1366 sBuilder, Locale.getDefault());
1367
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001368 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001369 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001370
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001371 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001372 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001373 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1374 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001375 }
1376 }
1377
Jeff Sharkey8a503642011-06-10 13:31:21 -07001378 /**
1379 * Special-case data usage cycle that triggers dialog to change
1380 * {@link NetworkPolicy#cycleDay}.
1381 */
1382 public static class CycleChangeItem extends CycleItem {
1383 public CycleChangeItem(Context context) {
1384 super(context.getString(R.string.data_usage_change_cycle));
1385 }
1386 }
1387
1388 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001389 private boolean mChangePossible = false;
1390 private boolean mChangeVisible = false;
1391
1392 private final CycleChangeItem mChangeItem;
1393
Jeff Sharkey8a503642011-06-10 13:31:21 -07001394 public CycleAdapter(Context context) {
1395 super(context, android.R.layout.simple_spinner_item);
1396 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001397 mChangeItem = new CycleChangeItem(context);
1398 }
1399
1400 public void setChangePossible(boolean possible) {
1401 mChangePossible = possible;
1402 updateChange();
1403 }
1404
1405 public void setChangeVisible(boolean visible) {
1406 mChangeVisible = visible;
1407 updateChange();
1408 }
1409
1410 private void updateChange() {
1411 remove(mChangeItem);
1412 if (mChangePossible && mChangeVisible) {
1413 add(mChangeItem);
1414 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001415 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001416
1417 /**
1418 * Find position of {@link CycleItem} in this adapter which is nearest
1419 * the given {@link CycleItem}.
1420 */
1421 public int findNearestPosition(CycleItem target) {
1422 if (target != null) {
1423 final int count = getCount();
1424 for (int i = count - 1; i >= 0; i--) {
1425 final CycleItem item = getItem(i);
1426 if (item instanceof CycleChangeItem) {
1427 continue;
1428 } else if (item.compareTo(target) >= 0) {
1429 return i;
1430 }
1431 }
1432 }
1433 return 0;
1434 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001435 }
1436
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001437 public static class AppItem implements Comparable<AppItem>, Parcelable {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001438 public final int key;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001439 public boolean restricted;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001440 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001441 public long total;
1442
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001443 public AppItem(int key) {
1444 this.key = key;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001445 }
1446
1447 public AppItem(Parcel parcel) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001448 key = parcel.readInt();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001449 uids = parcel.readSparseBooleanArray();
1450 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001451 }
1452
1453 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001454 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001455 }
1456
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001457 @Override
1458 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001459 dest.writeInt(key);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001460 dest.writeSparseBooleanArray(uids);
1461 dest.writeLong(total);
1462 }
1463
1464 @Override
1465 public int describeContents() {
1466 return 0;
1467 }
1468
1469 @Override
1470 public int compareTo(AppItem another) {
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001471 return Long.compare(another.total, total);
1472 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001473
1474 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1475 @Override
1476 public AppItem createFromParcel(Parcel in) {
1477 return new AppItem(in);
1478 }
1479
1480 @Override
1481 public AppItem[] newArray(int size) {
1482 return new AppItem[size];
1483 }
1484 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001485 }
1486
Jeff Sharkey8a503642011-06-10 13:31:21 -07001487 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001488 * Adapter of applications, sorted by total usage descending.
1489 */
1490 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001491 private final UidDetailProvider mProvider;
1492 private final int mInsetSide;
1493
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001494 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001495 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001496
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001497 public DataUsageAdapter(UidDetailProvider provider, int insetSide) {
1498 mProvider = checkNotNull(provider);
1499 mInsetSide = insetSide;
1500 }
1501
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001502 /**
1503 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1504 */
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001505 public void bindStats(NetworkStats stats, int[] restrictedUids) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001506 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001507
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001508 final int currentUserId = ActivityManager.getCurrentUser();
1509 final SparseArray<AppItem> knownItems = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001510
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001511 NetworkStats.Entry entry = null;
1512 final int size = stats != null ? stats.size() : 0;
1513 for (int i = 0; i < size; i++) {
1514 entry = stats.getValues(i, entry);
1515
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001516 // Decide how to collapse items together
1517 final int uid = entry.uid;
1518 final int collapseKey;
1519 if (UserHandle.isApp(uid)) {
1520 if (UserHandle.getUserId(uid) == currentUserId) {
1521 collapseKey = uid;
1522 } else {
1523 collapseKey = UidDetailProvider.buildKeyForUser(UserHandle.getUserId(uid));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001524 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001525 } else if (uid == UID_REMOVED || uid == UID_TETHERING) {
1526 collapseKey = uid;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001527 } else {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001528 collapseKey = android.os.Process.SYSTEM_UID;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001529 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001530
1531 AppItem item = knownItems.get(collapseKey);
1532 if (item == null) {
1533 item = new AppItem(collapseKey);
1534 mItems.add(item);
1535 knownItems.put(item.key, item);
1536 }
1537 item.addUid(uid);
1538 item.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001539 }
1540
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001541 for (int uid : restrictedUids) {
1542 // Only splice in restricted state for current user
1543 if (UserHandle.getUserId(uid) != currentUserId) continue;
1544
1545 AppItem item = knownItems.get(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001546 if (item == null) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001547 item = new AppItem(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001548 item.total = -1;
1549 mItems.add(item);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001550 knownItems.put(item.key, item);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001551 }
1552 item.restricted = true;
1553 }
1554
Jeff Sharkey8a503642011-06-10 13:31:21 -07001555 Collections.sort(mItems);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001556 mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001557 notifyDataSetChanged();
1558 }
1559
1560 @Override
1561 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001562 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001563 }
1564
1565 @Override
1566 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001567 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001568 }
1569
1570 @Override
1571 public long getItemId(int position) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001572 return mItems.get(position).key;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001573 }
1574
1575 @Override
1576 public View getView(int position, View convertView, ViewGroup parent) {
1577 if (convertView == null) {
1578 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey5d706792011-09-08 18:57:17 -07001579 R.layout.data_usage_item, parent, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001580
1581 if (mInsetSide > 0) {
Fabrice Di Megliob27223f2013-01-15 18:54:11 -08001582 convertView.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001583 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001584 }
1585
1586 final Context context = parent.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001587
Jeff Sharkey28130d92011-09-02 16:10:24 -07001588 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001589 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1590 android.R.id.progress);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001591
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001592 // kick off async load of app details
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001593 final AppItem item = mItems.get(position);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001594 UidDetailTask.bindView(mProvider, item, convertView);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001595
Jeff Sharkeye557c332012-04-13 16:04:07 -07001596 if (item.restricted && item.total <= 0) {
1597 text1.setText(R.string.data_usage_app_restricted);
1598 progress.setVisibility(View.GONE);
1599 } else {
1600 text1.setText(Formatter.formatFileSize(context, item.total));
1601 progress.setVisibility(View.VISIBLE);
1602 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001603
1604 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1605 progress.setProgress(percentTotal);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001606
1607 return convertView;
1608 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001609 }
1610
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001611 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001612 * Empty {@link Fragment} that controls display of UID details in
1613 * {@link DataUsageSummary}.
1614 */
1615 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001616 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001617
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001618 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001619 if (!parent.isAdded()) return;
1620
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001621 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001622 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001623
1624 final AppDetailsFragment fragment = new AppDetailsFragment();
1625 fragment.setArguments(args);
1626 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001627 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1628 ft.add(fragment, TAG_APP_DETAILS);
1629 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001630 ft.setBreadCrumbTitle(label);
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001631 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001632 }
1633
1634 @Override
1635 public void onStart() {
1636 super.onStart();
1637 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001638 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001639 target.updateBody();
1640 }
1641
1642 @Override
1643 public void onStop() {
1644 super.onStop();
1645 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001646 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001647 target.updateBody();
1648 }
1649 }
1650
1651 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001652 * Dialog to request user confirmation before setting
1653 * {@link NetworkPolicy#limitBytes}.
1654 */
1655 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001656 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001657 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001658
1659 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001660 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001661
Jeff Sharkey461842a2011-09-25 18:22:48 -07001662 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001663 final CharSequence message;
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001664 final long minLimitBytes = (long) (
1665 parent.mPolicyEditor.getPolicy(parent.mTemplate).warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001666 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001667
1668 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001669 final String currentTab = parent.mCurrentTab;
1670 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001671 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001672 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001673 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001674 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001675 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001676 } else if (TAB_MOBILE.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 Sharkey131f9d62011-08-17 17:08:19 -07001679 } else {
1680 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001681 }
1682
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001683 final Bundle args = new Bundle();
1684 args.putCharSequence(EXTRA_MESSAGE, message);
1685 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1686
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001687 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1688 dialog.setArguments(args);
1689 dialog.setTargetFragment(parent, 0);
1690 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1691 }
1692
1693 @Override
1694 public Dialog onCreateDialog(Bundle savedInstanceState) {
1695 final Context context = getActivity();
1696
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001697 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001698 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1699
1700 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1701 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001702 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001703
1704 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001705 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001706 public void onClick(DialogInterface dialog, int which) {
1707 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1708 if (target != null) {
1709 target.setPolicyLimitBytes(limitBytes);
1710 }
1711 }
1712 });
1713
1714 return builder.create();
1715 }
1716 }
1717
1718 /**
1719 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1720 */
1721 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001722 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001723
1724 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001725 if (!parent.isAdded()) return;
1726
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001727 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001728 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001729
1730 final CycleEditorFragment dialog = new CycleEditorFragment();
1731 dialog.setArguments(args);
1732 dialog.setTargetFragment(parent, 0);
1733 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1734 }
1735
1736 @Override
1737 public Dialog onCreateDialog(Bundle savedInstanceState) {
1738 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001739 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1740 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001741
1742 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1743 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1744
1745 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1746 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -07001747 cycleDayPicker.setTag("data_usage_cycle");
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001748
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001749 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1750 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001751
1752 cycleDayPicker.setMinValue(1);
1753 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001754 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001755 cycleDayPicker.setWrapSelectorWheel(true);
1756
1757 builder.setTitle(R.string.data_usage_cycle_editor_title);
1758 builder.setView(view);
1759
1760 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1761 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001762 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001763 public void onClick(DialogInterface dialog, int which) {
Jeff Sharkeyd277de92013-03-25 15:11:25 -07001764 // clear focus to finish pending text edits
1765 cycleDayPicker.clearFocus();
1766
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001767 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001768 final String cycleTimezone = new Time().timezone;
1769 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001770 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001771 }
1772 });
1773
1774 return builder.create();
1775 }
1776 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001777
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001778 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001779 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1780 */
1781 public static class WarningEditorFragment extends DialogFragment {
1782 private static final String EXTRA_TEMPLATE = "template";
1783
1784 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001785 if (!parent.isAdded()) return;
1786
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001787 final Bundle args = new Bundle();
1788 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1789
1790 final WarningEditorFragment dialog = new WarningEditorFragment();
1791 dialog.setArguments(args);
1792 dialog.setTargetFragment(parent, 0);
1793 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1794 }
1795
1796 @Override
1797 public Dialog onCreateDialog(Bundle savedInstanceState) {
1798 final Context context = getActivity();
1799 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1800 final NetworkPolicyEditor editor = target.mPolicyEditor;
1801
1802 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1803 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1804
1805 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1806 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1807
1808 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1809 final long warningBytes = editor.getPolicyWarningBytes(template);
1810 final long limitBytes = editor.getPolicyLimitBytes(template);
1811
1812 bytesPicker.setMinValue(0);
1813 if (limitBytes != LIMIT_DISABLED) {
1814 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1815 } else {
1816 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1817 }
1818 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1819 bytesPicker.setWrapSelectorWheel(false);
1820
1821 builder.setTitle(R.string.data_usage_warning_editor_title);
1822 builder.setView(view);
1823
1824 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1825 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001826 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001827 public void onClick(DialogInterface dialog, int which) {
1828 // clear focus to finish pending text edits
1829 bytesPicker.clearFocus();
1830
1831 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1832 editor.setPolicyWarningBytes(template, bytes);
1833 target.updatePolicy(false);
1834 }
1835 });
1836
1837 return builder.create();
1838 }
1839 }
1840
1841 /**
1842 * Dialog to edit {@link NetworkPolicy#limitBytes}.
1843 */
1844 public static class LimitEditorFragment extends DialogFragment {
1845 private static final String EXTRA_TEMPLATE = "template";
1846
1847 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001848 if (!parent.isAdded()) return;
1849
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001850 final Bundle args = new Bundle();
1851 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1852
1853 final LimitEditorFragment dialog = new LimitEditorFragment();
1854 dialog.setArguments(args);
1855 dialog.setTargetFragment(parent, 0);
1856 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
1857 }
1858
1859 @Override
1860 public Dialog onCreateDialog(Bundle savedInstanceState) {
1861 final Context context = getActivity();
1862 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1863 final NetworkPolicyEditor editor = target.mPolicyEditor;
1864
1865 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1866 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1867
1868 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1869 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1870
1871 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1872 final long warningBytes = editor.getPolicyWarningBytes(template);
1873 final long limitBytes = editor.getPolicyLimitBytes(template);
1874
1875 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09001876 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001877 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
1878 } else {
1879 bytesPicker.setMinValue(0);
1880 }
1881 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
1882 bytesPicker.setWrapSelectorWheel(false);
1883
1884 builder.setTitle(R.string.data_usage_limit_editor_title);
1885 builder.setView(view);
1886
1887 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1888 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001889 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001890 public void onClick(DialogInterface dialog, int which) {
1891 // clear focus to finish pending text edits
1892 bytesPicker.clearFocus();
1893
1894 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1895 editor.setPolicyLimitBytes(template, bytes);
1896 target.updatePolicy(false);
1897 }
1898 });
1899
1900 return builder.create();
1901 }
1902 }
1903 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07001904 * Dialog to request user confirmation before disabling data.
1905 */
1906 public static class ConfirmDataDisableFragment extends DialogFragment {
1907 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001908 if (!parent.isAdded()) return;
1909
Jeff Sharkey28130d92011-09-02 16:10:24 -07001910 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
1911 dialog.setTargetFragment(parent, 0);
1912 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
1913 }
1914
1915 @Override
1916 public Dialog onCreateDialog(Bundle savedInstanceState) {
1917 final Context context = getActivity();
1918
1919 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1920 builder.setMessage(R.string.data_usage_disable_mobile);
1921
1922 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001923 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07001924 public void onClick(DialogInterface dialog, int which) {
1925 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1926 if (target != null) {
1927 // TODO: extend to modify policy enabled flag.
1928 target.setMobileDataEnabled(false);
1929 }
1930 }
1931 });
1932 builder.setNegativeButton(android.R.string.cancel, null);
1933
1934 return builder.create();
1935 }
1936 }
1937
1938 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001939 * Dialog to request user confirmation before setting
Christopher Tate5a64c732012-09-07 13:35:31 -07001940 * {@link android.provider.Settings.Global#DATA_ROAMING}.
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001941 */
1942 public static class ConfirmDataRoamingFragment extends DialogFragment {
1943 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001944 if (!parent.isAdded()) return;
1945
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001946 final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment();
1947 dialog.setTargetFragment(parent, 0);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001948 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_ROAMING);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001949 }
1950
1951 @Override
1952 public Dialog onCreateDialog(Bundle savedInstanceState) {
1953 final Context context = getActivity();
1954
1955 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1956 builder.setTitle(R.string.roaming_reenable_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07001957 if (Utils.hasMultipleUsers(context)) {
1958 builder.setMessage(R.string.roaming_warning_multiuser);
1959 } else {
1960 builder.setMessage(R.string.roaming_warning);
1961 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001962
1963 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001964 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001965 public void onClick(DialogInterface dialog, int which) {
1966 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1967 if (target != null) {
1968 target.setDataRoaming(true);
1969 }
1970 }
1971 });
1972 builder.setNegativeButton(android.R.string.cancel, null);
1973
1974 return builder.create();
1975 }
1976 }
1977
1978 /**
1979 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001980 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001981 */
1982 public static class ConfirmRestrictFragment extends DialogFragment {
1983 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001984 if (!parent.isAdded()) return;
1985
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001986 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1987 dialog.setTargetFragment(parent, 0);
1988 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1989 }
1990
1991 @Override
1992 public Dialog onCreateDialog(Bundle savedInstanceState) {
1993 final Context context = getActivity();
1994
1995 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001996 builder.setTitle(R.string.data_usage_restrict_background_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07001997 if (Utils.hasMultipleUsers(context)) {
1998 builder.setMessage(R.string.data_usage_restrict_background_multiuser);
1999 } else {
2000 builder.setMessage(R.string.data_usage_restrict_background);
2001 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002002
2003 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002004 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002005 public void onClick(DialogInterface dialog, int which) {
2006 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2007 if (target != null) {
2008 target.setRestrictBackground(true);
2009 }
2010 }
2011 });
2012 builder.setNegativeButton(android.R.string.cancel, null);
2013
2014 return builder.create();
2015 }
2016 }
2017
2018 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002019 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
2020 * change has been denied, usually based on
2021 * {@link DataUsageSummary#hasLimitedNetworks()}.
2022 */
2023 public static class DeniedRestrictFragment extends DialogFragment {
2024 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002025 if (!parent.isAdded()) return;
2026
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002027 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
2028 dialog.setTargetFragment(parent, 0);
2029 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
2030 }
2031
2032 @Override
2033 public Dialog onCreateDialog(Bundle savedInstanceState) {
2034 final Context context = getActivity();
2035
2036 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2037 builder.setTitle(R.string.data_usage_app_restrict_background);
2038 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
2039 builder.setPositiveButton(android.R.string.ok, null);
2040
2041 return builder.create();
2042 }
2043 }
2044
2045 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002046 * Dialog to request user confirmation before setting
2047 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
2048 */
2049 public static class ConfirmAppRestrictFragment extends DialogFragment {
2050 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002051 if (!parent.isAdded()) return;
2052
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002053 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2054 dialog.setTargetFragment(parent, 0);
2055 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2056 }
2057
2058 @Override
2059 public Dialog onCreateDialog(Bundle savedInstanceState) {
2060 final Context context = getActivity();
2061
2062 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002063 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2064 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2065
2066 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002067 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002068 public void onClick(DialogInterface dialog, int which) {
2069 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2070 if (target != null) {
2071 target.setAppRestrictBackground(true);
2072 }
2073 }
2074 });
2075 builder.setNegativeButton(android.R.string.cancel, null);
2076
2077 return builder.create();
2078 }
2079 }
2080
2081 /**
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002082 * Dialog to inform user about changing auto-sync setting
2083 */
2084 public static class ConfirmAutoSyncChangeFragment extends DialogFragment {
Amith Yamasani665235f2012-06-07 19:58:34 -07002085 private static final String SAVE_ENABLING = "enabling";
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002086 private boolean mEnabling;
2087
2088 public static void show(DataUsageSummary parent, boolean enabling) {
2089 if (!parent.isAdded()) return;
2090
2091 final ConfirmAutoSyncChangeFragment dialog = new ConfirmAutoSyncChangeFragment();
2092 dialog.mEnabling = enabling;
2093 dialog.setTargetFragment(parent, 0);
2094 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_AUTO_SYNC_CHANGE);
2095 }
2096
2097 @Override
2098 public Dialog onCreateDialog(Bundle savedInstanceState) {
2099 final Context context = getActivity();
Amith Yamasani665235f2012-06-07 19:58:34 -07002100 if (savedInstanceState != null) {
2101 mEnabling = savedInstanceState.getBoolean(SAVE_ENABLING);
2102 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002103
2104 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2105 if (!mEnabling) {
2106 builder.setTitle(R.string.data_usage_auto_sync_off_dialog_title);
2107 builder.setMessage(R.string.data_usage_auto_sync_off_dialog);
2108 } else {
2109 builder.setTitle(R.string.data_usage_auto_sync_on_dialog_title);
2110 builder.setMessage(R.string.data_usage_auto_sync_on_dialog);
2111 }
2112
2113 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
2114 @Override
2115 public void onClick(DialogInterface dialog, int which) {
2116 ContentResolver.setMasterSyncAutomatically(mEnabling);
2117 }
2118 });
2119 builder.setNegativeButton(android.R.string.cancel, null);
2120
2121 return builder.create();
2122 }
Amith Yamasani665235f2012-06-07 19:58:34 -07002123
2124 @Override
2125 public void onSaveInstanceState(Bundle outState) {
2126 super.onSaveInstanceState(outState);
2127 outState.putBoolean(SAVE_ENABLING, mEnabling);
2128 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002129 }
2130
2131 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002132 * Compute default tab that should be selected, based on
2133 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2134 */
2135 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002136 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
2137 if (template == null) return null;
2138
2139 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002140 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002141 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002142 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002143 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002144 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002145 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002146 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002147 return TAB_WIFI;
2148 default:
2149 return null;
2150 }
2151 }
2152
2153 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002154 * Background task that loads {@link UidDetail}, binding to
2155 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002156 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002157 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2158 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002159 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002160 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002161
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002162 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002163 mProvider = checkNotNull(provider);
2164 mItem = checkNotNull(item);
2165 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002166 }
2167
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002168 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002169 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002170 final UidDetailTask existing = (UidDetailTask) target.getTag();
2171 if (existing != null) {
2172 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002173 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002174
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002175 final UidDetail cachedDetail = provider.getUidDetail(item.key, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002176 if (cachedDetail != null) {
2177 bindView(cachedDetail, target);
2178 } else {
2179 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2180 AsyncTask.THREAD_POOL_EXECUTOR));
2181 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002182 }
2183
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002184 private static void bindView(UidDetail detail, View target) {
2185 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2186 final TextView title = (TextView) target.findViewById(android.R.id.title);
2187
2188 if (detail != null) {
2189 icon.setImageDrawable(detail.icon);
2190 title.setText(detail.label);
2191 } else {
2192 icon.setImageDrawable(null);
2193 title.setText(null);
2194 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002195 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002196
2197 @Override
2198 protected void onPreExecute() {
2199 bindView(null, mTarget);
2200 }
2201
2202 @Override
2203 protected UidDetail doInBackground(Void... params) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002204 return mProvider.getUidDetail(mItem.key, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002205 }
2206
2207 @Override
2208 protected void onPostExecute(UidDetail result) {
2209 bindView(result, mTarget);
2210 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002211 }
2212
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002213 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002214 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002215 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002216 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002217 if (TEST_RADIOS) {
2218 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2219 }
2220
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002221 final ConnectivityManager conn = ConnectivityManager.from(context);
2222 final TelephonyManager tele = TelephonyManager.from(context);
2223
2224 // require both supported network and ready SIM
2225 return conn.isNetworkSupported(TYPE_MOBILE) && tele.getSimState() == SIM_STATE_READY;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002226 }
2227
2228 /**
2229 * Test if device has a mobile 4G data radio.
2230 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002231 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002232 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2233 return false;
2234 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002235 if (TEST_RADIOS) {
2236 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2237 }
2238
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002239 final ConnectivityManager conn = ConnectivityManager.from(context);
2240 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002241
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002242 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Wink Saville55434042012-06-14 12:33:43 -07002243 final boolean hasLte = (tele.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE)
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002244 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002245 return hasWimax || hasLte;
2246 }
2247
2248 /**
2249 * Test if device has a Wi-Fi data radio.
2250 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002251 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002252 if (TEST_RADIOS) {
2253 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2254 }
2255
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002256 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002257 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002258 }
2259
2260 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002261 * Test if device has an ethernet network connection.
2262 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002263 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002264 if (TEST_RADIOS) {
2265 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2266 }
2267
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002268 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002269 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2270
2271 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002272 if (mStatsSession != null) {
2273 try {
2274 ethernetBytes = mStatsSession.getSummaryForNetwork(
2275 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2276 .getTotalBytes();
2277 } catch (RemoteException e) {
2278 throw new RuntimeException(e);
2279 }
2280 } else {
2281 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002282 }
2283
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002284 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002285 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002286 }
2287
2288 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002289 * Inflate a {@link Preference} style layout, adding the given {@link View}
2290 * widget into {@link android.R.id#widget_frame}.
2291 */
2292 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2293 final View view = inflater.inflate(R.layout.preference, root, false);
2294 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2295 android.R.id.widget_frame);
2296 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2297 return view;
2298 }
2299
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002300 private static View inflateAppTitle(
2301 LayoutInflater inflater, ViewGroup root, CharSequence label) {
2302 final TextView view = (TextView) inflater.inflate(
2303 R.layout.data_usage_app_title, root, false);
2304 view.setText(label);
2305 return view;
2306 }
2307
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002308 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002309 * Test if any networks are currently limited.
2310 */
2311 private boolean hasLimitedNetworks() {
2312 return !buildLimitedNetworksList().isEmpty();
2313 }
2314
2315 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002316 * Build string describing currently limited networks, which defines when
2317 * background data is restricted.
2318 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002319 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002320 private CharSequence buildLimitedNetworksString() {
2321 final List<CharSequence> limited = buildLimitedNetworksList();
2322
2323 // handle case where no networks limited
2324 if (limited.isEmpty()) {
2325 limited.add(getText(R.string.data_usage_list_none));
2326 }
2327
2328 return TextUtils.join(limited);
2329 }
2330
2331 /**
2332 * Build list of currently limited networks, which defines when background
2333 * data is restricted.
2334 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002335 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002336 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002337 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002338
2339 // build combined list of all limited networks
2340 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002341
2342 final TelephonyManager tele = TelephonyManager.from(context);
2343 if (tele.getSimState() == SIM_STATE_READY) {
2344 final String subscriberId = getActiveSubscriberId(context);
2345 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2346 limited.add(getText(R.string.data_usage_list_mobile));
2347 }
2348 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2349 limited.add(getText(R.string.data_usage_tab_3g));
2350 }
2351 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2352 limited.add(getText(R.string.data_usage_tab_4g));
2353 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002354 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002355
2356 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002357 limited.add(getText(R.string.data_usage_tab_wifi));
2358 }
2359 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2360 limited.add(getText(R.string.data_usage_tab_ethernet));
2361 }
2362
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002363 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002364 }
2365
2366 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002367 * Inset both selector and divider {@link Drawable} on the given
2368 * {@link ListView} by the requested dimensions.
2369 */
2370 private static void insetListViewDrawables(ListView view, int insetSide) {
2371 final Drawable selector = view.getSelector();
2372 final Drawable divider = view.getDivider();
2373
2374 // fully unregister these drawables so callbacks can be maintained after
2375 // wrapping below.
2376 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2377 view.setSelector(stub);
2378 view.setDivider(stub);
2379
2380 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2381 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2382 }
2383
2384 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002385 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002386 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002387 */
2388 private static void setPreferenceTitle(View parent, int resId) {
2389 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2390 title.setText(resId);
2391 }
2392
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002393 /**
2394 * Set {@link android.R.id#summary} for a preference view inflated with
2395 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2396 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002397 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002398 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2399 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002400 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002401 }
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002402
2403 /**
2404 * For search
2405 */
2406 public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
Fabrice Di Meglio45f754e2014-04-10 19:25:42 -07002407 new BaseSearchIndexProvider() {
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002408 @Override
2409 public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) {
2410 final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>();
2411
2412 final Resources res = context.getResources();
2413
2414 // Add fragment title
2415 SearchIndexableRaw data = new SearchIndexableRaw(context);
2416 data.title = res.getString(R.string.data_usage_summary_title);
2417 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2418 result.add(data);
2419
2420 // Mobile data
2421 data = new SearchIndexableRaw(context);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -07002422 data.key = "data_usage_enable_mobile";
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002423 data.title = res.getString(R.string.data_usage_enable_mobile);
2424 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2425 result.add(data);
2426
2427 // Set mobile data limit
2428 data = new SearchIndexableRaw(context);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -07002429 data.key = "data_usage_disable_mobile_limit";
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002430 data.title = res.getString(R.string.data_usage_disable_mobile_limit);
2431 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2432 result.add(data);
2433
Fabrice Di Megliof2a52262014-04-17 17:20:27 -07002434 // Data usage cycle
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002435 data = new SearchIndexableRaw(context);
Fabrice Di Megliof2a52262014-04-17 17:20:27 -07002436 data.key = "data_usage_cycle";
Fabrice Di Meglio758c3ff2014-04-10 13:47:30 -07002437 data.title = res.getString(R.string.data_usage_cycle);
2438 data.screenTitle = res.getString(R.string.data_usage_summary_title);
2439 result.add(data);
2440
2441 return result;
2442 }
2443 };
2444
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002445}