blob: a0b18fd8deae05a959e5dded4b0d0b05362d0c18 [file] [log] [blame]
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings;
18
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070019import static android.net.ConnectivityManager.TYPE_ETHERNET;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070020import static android.net.ConnectivityManager.TYPE_MOBILE;
Jeff Sharkeybdf98e82011-11-10 17:17:24 -080021import static android.net.ConnectivityManager.TYPE_WIFI;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070022import static android.net.ConnectivityManager.TYPE_WIMAX;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070023import static android.net.NetworkPolicy.LIMIT_DISABLED;
Jeff Sharkeya53188f2011-09-13 19:56:45 -070024import static android.net.NetworkPolicy.WARNING_DISABLED;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070025import static android.net.NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070026import static android.net.NetworkPolicyManager.POLICY_NONE;
27import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND;
Jeff Sharkey8a503642011-06-10 13:31:21 -070028import static android.net.NetworkPolicyManager.computeLastCycleBoundary;
29import static android.net.NetworkPolicyManager.computeNextCycleBoundary;
Jeff Sharkeya662e492011-06-18 21:57:06 -070030import static android.net.NetworkTemplate.MATCH_MOBILE_3G_LOWER;
31import static android.net.NetworkTemplate.MATCH_MOBILE_4G;
32import static android.net.NetworkTemplate.MATCH_MOBILE_ALL;
33import static android.net.NetworkTemplate.MATCH_WIFI;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070034import static android.net.NetworkTemplate.buildTemplateEthernet;
35import static android.net.NetworkTemplate.buildTemplateMobile3gLower;
36import static android.net.NetworkTemplate.buildTemplateMobile4g;
37import static android.net.NetworkTemplate.buildTemplateMobileAll;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070038import static android.net.NetworkTemplate.buildTemplateWifiWildcard;
Jeff Sharkey77dae912012-02-03 14:50:33 -080039import static android.net.TrafficStats.GB_IN_BYTES;
40import static android.net.TrafficStats.MB_IN_BYTES;
Jeff Sharkeya83a24f2011-09-16 01:52:39 -070041import static android.net.TrafficStats.UID_REMOVED;
42import static android.net.TrafficStats.UID_TETHERING;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070043import static android.telephony.TelephonyManager.SIM_STATE_READY;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070044import static android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
45import static android.text.format.DateUtils.FORMAT_SHOW_DATE;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070046import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -070047import static com.android.internal.util.Preconditions.checkNotNull;
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -070048import static com.android.settings.Utils.prepareCustomPreferencesList;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070049
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070050import android.animation.LayoutTransition;
Jeff Sharkey38305fb2012-09-14 16:26:51 -070051import android.app.ActivityManager;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070052import android.app.AlertDialog;
53import android.app.Dialog;
54import android.app.DialogFragment;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070055import android.app.Fragment;
Amith Yamasani5ba0a022011-11-07 12:29:00 -080056import android.app.FragmentManager;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070057import android.app.FragmentTransaction;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070058import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070059import android.content.ContentResolver;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070060import android.content.Context;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070061import android.content.DialogInterface;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -070062import android.content.Intent;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070063import android.content.Loader;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070064import android.content.SharedPreferences;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070065import android.content.pm.PackageManager;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070066import android.content.res.Resources;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070067import android.graphics.Color;
Jeff Sharkey5d706792011-09-08 18:57:17 -070068import android.graphics.drawable.ColorDrawable;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070069import android.graphics.drawable.Drawable;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070070import android.net.ConnectivityManager;
Jeff Sharkey8a503642011-06-10 13:31:21 -070071import android.net.INetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070072import android.net.INetworkStatsService;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070073import android.net.INetworkStatsSession;
Jeff Sharkey8a503642011-06-10 13:31:21 -070074import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070075import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070076import android.net.NetworkStats;
77import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070078import android.net.NetworkTemplate;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -070079import android.net.TrafficStats;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070080import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070081import android.os.Bundle;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070082import android.os.INetworkManagementService;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070083import android.os.Parcel;
84import android.os.Parcelable;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070085import android.os.RemoteException;
86import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070087import android.os.SystemProperties;
Dianne Hackbornbb06a422012-08-16 11:01:57 -070088import android.os.UserHandle;
Jeff Sharkey8a503642011-06-10 13:31:21 -070089import android.preference.Preference;
Jeff Sharkey313f7d82012-04-03 21:13:25 -070090import android.preference.PreferenceActivity;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070091import android.provider.Settings;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070092import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070093import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070094import android.text.format.DateUtils;
95import android.text.format.Formatter;
Jeff Sharkeye5223a02012-03-09 17:11:14 -080096import android.text.format.Time;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070097import android.util.Log;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070098import android.util.SparseArray;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -070099import android.util.SparseBooleanArray;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700100import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700101import android.view.Menu;
102import android.view.MenuInflater;
103import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700104import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700105import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700106import android.view.ViewGroup;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700107import android.widget.AdapterView;
108import android.widget.AdapterView.OnItemClickListener;
109import android.widget.AdapterView.OnItemSelectedListener;
110import android.widget.ArrayAdapter;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700111import android.widget.BaseAdapter;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700112import android.widget.Button;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700113import android.widget.CheckBox;
114import android.widget.CompoundButton;
115import android.widget.CompoundButton.OnCheckedChangeListener;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700116import android.widget.ImageView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700117import android.widget.LinearLayout;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700118import android.widget.ListView;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700119import android.widget.NumberPicker;
Jeff Sharkey2412b0f2011-07-17 20:31:40 -0700120import android.widget.ProgressBar;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700121import android.widget.Spinner;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700122import android.widget.Switch;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700123import android.widget.TabHost;
124import android.widget.TabHost.OnTabChangeListener;
125import android.widget.TabHost.TabContentFactory;
126import android.widget.TabHost.TabSpec;
127import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700128import android.widget.TextView;
129
Wink Saville55434042012-06-14 12:33:43 -0700130import com.android.internal.telephony.PhoneConstants;
Jeff Sharkey5d706792011-09-08 18:57:17 -0700131import com.android.settings.drawable.InsetBoundsDrawable;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700132import com.android.settings.net.ChartData;
133import com.android.settings.net.ChartDataLoader;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700134import com.android.settings.net.DataUsageMeteredSettings;
Jeff Sharkeya662e492011-06-18 21:57:06 -0700135import com.android.settings.net.NetworkPolicyEditor;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700136import com.android.settings.net.SummaryForAllUidLoader;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700137import com.android.settings.net.UidDetail;
138import com.android.settings.net.UidDetailProvider;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700139import com.android.settings.widget.ChartDataUsageView;
140import com.android.settings.widget.ChartDataUsageView.DataUsageChartListener;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700141import com.android.settings.widget.PieChartView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700142import com.google.android.collect.Lists;
143
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 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700155public class DataUsageSummary extends Fragment {
156 private static final String TAG = "DataUsage";
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800157 private static final boolean LOGD = false;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700158
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700159 // TODO: remove this testing code
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700160 private static final boolean TEST_ANIM = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700161 private static final boolean TEST_RADIOS = false;
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800162
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700163 private static final String TEST_RADIOS_PROP = "test.radios";
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -0800164 private static final String TEST_SUBSCRIBER_PROP = "test.subscriberid";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700165
Jeff Sharkey8a503642011-06-10 13:31:21 -0700166 private static final String TAB_3G = "3g";
167 private static final String TAB_4G = "4g";
168 private static final String TAB_MOBILE = "mobile";
169 private static final String TAB_WIFI = "wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700170 private static final String TAB_ETHERNET = "ethernet";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700171
Jeff Sharkey28130d92011-09-02 16:10:24 -0700172 private static final String TAG_CONFIRM_DATA_DISABLE = "confirmDataDisable";
173 private static final String TAG_CONFIRM_DATA_ROAMING = "confirmDataRoaming";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700174 private static final String TAG_CONFIRM_LIMIT = "confirmLimit";
175 private static final String TAG_CYCLE_EDITOR = "cycleEditor";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700176 private static final String TAG_WARNING_EDITOR = "warningEditor";
177 private static final String TAG_LIMIT_EDITOR = "limitEditor";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700178 private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700179 private static final String TAG_DENIED_RESTRICT = "deniedRestrict";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700180 private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict";
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700181 private static final String TAG_CONFIRM_AUTO_SYNC_CHANGE = "confirmAutoSyncChange";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700182 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700183
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700184 private static final int LOADER_CHART_DATA = 2;
185 private static final int LOADER_SUMMARY = 3;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700186
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700187 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700188 private INetworkStatsService mStatsService;
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700189 private NetworkPolicyManager mPolicyManager;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700190 private ConnectivityManager mConnService;
191
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700192 private INetworkStatsSession mStatsSession;
193
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700194 private static final String PREF_FILE = "data_usage";
195 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700196 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700197
198 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700199
Jeff Sharkey8a503642011-06-10 13:31:21 -0700200 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700201 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700202 private TabWidget mTabWidget;
203 private ListView mListView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700204 private DataUsageAdapter mAdapter;
205
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700206 /** Distance to inset content from sides, when needed. */
207 private int mInsetSide = 0;
208
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700209 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700210
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700211 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700212 private LinearLayout mNetworkSwitches;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700213 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700214 private View mDataEnabledView;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700215 private CheckBox mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700216 private View mDisableAtLimitView;
217
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700218 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700219 private Spinner mCycleSpinner;
220 private CycleAdapter mCycleAdapter;
221
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700222 private ChartDataUsageView mChart;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700223 private TextView mUsageSummary;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700224 private TextView mEmpty;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700225
226 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700227 private ImageView mAppIcon;
228 private ViewGroup mAppTitles;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700229 private PieChartView mAppPieChart;
230 private TextView mAppForeground;
231 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700232 private Button mAppSettings;
233
234 private LinearLayout mAppSwitches;
235 private CheckBox mAppRestrict;
236 private View mAppRestrictView;
237
Jeff Sharkey8a503642011-06-10 13:31:21 -0700238 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700239 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700240
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700241 private NetworkTemplate mTemplate;
242 private ChartData mChartData;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700243
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700244 private AppItem mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700245
246 private Intent mAppSettingsIntent;
247
Jeff Sharkeya662e492011-06-18 21:57:06 -0700248 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700249
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700250 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700251 private String mIntentTab = null;
252
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700253 private MenuItem mMenuDataRoaming;
254 private MenuItem mMenuRestrictBackground;
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700255 private MenuItem mMenuAutoSync;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700256
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700257 /** Flag used to ignore listeners during binding. */
258 private boolean mBinding;
259
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700260 private UidDetailProvider mUidDetailProvider;
261
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700262 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700263 public void onCreate(Bundle savedInstanceState) {
264 super.onCreate(savedInstanceState);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700265 final Context context = getActivity();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700266
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700267 mNetworkService = INetworkManagementService.Stub.asInterface(
268 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700269 mStatsService = INetworkStatsService.Stub.asInterface(
270 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700271 mPolicyManager = NetworkPolicyManager.from(context);
272 mConnService = ConnectivityManager.from(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700273
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700274 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700275
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700276 mPolicyEditor = new NetworkPolicyEditor(mPolicyManager);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700277 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700278
Jaewan Kimffce9c12013-03-19 16:53:45 +0900279 try {
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);
354 mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener);
355 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700356
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700357 mDisableAtLimit = new CheckBox(inflater.getContext());
358 mDisableAtLimit.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700359 mDisableAtLimit.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700360 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700361 mDisableAtLimitView.setClickable(true);
362 mDisableAtLimitView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700363 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
364 mNetworkSwitches.addView(mDisableAtLimitView);
365 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700366
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700367 // bind cycle dropdown
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700368 mCycleView = mHeader.findViewById(R.id.cycles);
369 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700370 mCycleAdapter = new CycleAdapter(context);
371 mCycleSpinner.setAdapter(mCycleAdapter);
372 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
373
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700374 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700375 mChart.setListener(mChartListener);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -0800376 mChart.bindNetworkPolicy(null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700377
378 {
379 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700380 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700381 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
382 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700383 mAppPieChart = (PieChartView) mAppDetail.findViewById(R.id.app_pie_chart);
384 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
385 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700386 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700387
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700388 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700389 mAppSettings.setOnClickListener(mAppSettingsListener);
390
391 mAppRestrict = new CheckBox(inflater.getContext());
392 mAppRestrict.setClickable(false);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700393 mAppRestrict.setFocusable(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700394 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700395 mAppRestrictView.setClickable(true);
396 mAppRestrictView.setFocusable(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700397 mAppRestrictView.setOnClickListener(mAppRestrictListener);
398 mAppSwitches.addView(mAppRestrictView);
399 }
400
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700401 mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700402 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700403
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700404 mAdapter = new DataUsageAdapter(mUidDetailProvider, mInsetSide);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700405 mListView.setOnItemClickListener(mListListener);
406 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700407
408 return view;
409 }
410
411 @Override
412 public void onResume() {
413 super.onResume();
414
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700415 // pick default tab based on incoming intent
416 final Intent intent = getActivity().getIntent();
417 mIntentTab = computeTabFromIntent(intent);
418
Jeff Sharkey8a503642011-06-10 13:31:21 -0700419 // this kicks off chain reaction which creates tabs, binds the body to
420 // selected network, and binds chart, cycles and detail list.
421 updateTabs();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700422
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700423 // kick off background task to update stats
424 new AsyncTask<Void, Void, Void>() {
425 @Override
426 protected Void doInBackground(Void... params) {
427 try {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700428 // wait a few seconds before kicking off
429 Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS);
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700430 mStatsService.forceUpdate();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700431 } catch (InterruptedException e) {
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700432 } catch (RemoteException e) {
433 }
434 return null;
435 }
436
437 @Override
438 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700439 if (isAdded()) {
440 updateBody();
441 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700442 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700443 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700444 }
445
Jeff Sharkey8a503642011-06-10 13:31:21 -0700446 @Override
447 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
448 inflater.inflate(R.menu.data_usage, menu);
449 }
450
451 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700452 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700453 final Context context = getActivity();
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700454 final boolean appDetailMode = isAppDetailMode();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700455 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700456
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700457 mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700458 mMenuDataRoaming.setVisible(hasReadyMobileRadio(context) && !appDetailMode);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700459 mMenuDataRoaming.setChecked(getDataRoaming());
460
461 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
Jeff Sharkey92b518c2013-03-25 16:13:00 -0700462 mMenuRestrictBackground.setVisible(
463 hasReadyMobileRadio(context) && isOwner && !appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700464 mMenuRestrictBackground.setChecked(mPolicyManager.getRestrictBackground());
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700465
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700466 mMenuAutoSync = menu.findItem(R.id.data_usage_menu_auto_sync);
467 mMenuAutoSync.setChecked(ContentResolver.getMasterSyncAutomatically());
Jeff Sharkeyfda48e32012-09-18 15:03:47 -0700468 mMenuAutoSync.setVisible(!appDetailMode);
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700469
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700470 final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g);
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700471 split4g.setVisible(hasReadyMobile4gRadio(context) && isOwner && !appDetailMode);
Jeff Sharkeya662e492011-06-18 21:57:06 -0700472 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700473
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700474 final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700475 if (hasWifiRadio(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700476 showWifi.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700477 showWifi.setChecked(mShowWifi);
478 } else {
479 showWifi.setVisible(false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700480 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700481
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700482 final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700483 if (hasEthernet(context) && hasReadyMobileRadio(context)) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700484 showEthernet.setVisible(!appDetailMode);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700485 showEthernet.setChecked(mShowEthernet);
486 } else {
487 showEthernet.setVisible(false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700488 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700489
490 final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered);
491 if (hasReadyMobileRadio(context) || hasWifiRadio(context)) {
Amith Yamasani9627a8e2012-09-23 12:54:14 -0700492 metered.setVisible(!appDetailMode);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700493 } else {
494 metered.setVisible(false);
495 }
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700496
497 final MenuItem help = menu.findItem(R.id.data_usage_menu_help);
498 String helpUrl;
499 if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) {
Amith Yamasaniaeb57ed2012-12-06 14:40:51 -0800500 HelpUtils.prepareHelpMenuItem(context, help, helpUrl);
Amith Yamasanib0b37ae2012-04-23 15:35:36 -0700501 } else {
502 help.setVisible(false);
503 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700504 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700505
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700506 @Override
507 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700508 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700509 case R.id.data_usage_menu_roaming: {
510 final boolean dataRoaming = !item.isChecked();
511 if (dataRoaming) {
512 ConfirmDataRoamingFragment.show(this);
513 } else {
514 // no confirmation to disable roaming
515 setDataRoaming(false);
516 }
517 return true;
518 }
519 case R.id.data_usage_menu_restrict_background: {
520 final boolean restrictBackground = !item.isChecked();
521 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -0800522 ConfirmRestrictFragment.show(this);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700523 } else {
524 // no confirmation to drop restriction
525 setRestrictBackground(false);
526 }
527 return true;
528 }
529 case R.id.data_usage_menu_split_4g: {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700530 final boolean mobileSplit = !item.isChecked();
Jeff Sharkeya662e492011-06-18 21:57:06 -0700531 setMobilePolicySplit(mobileSplit);
532 item.setChecked(isMobilePolicySplit());
Jeff Sharkey8a503642011-06-10 13:31:21 -0700533 updateTabs();
534 return true;
535 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700536 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700537 mShowWifi = !item.isChecked();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700538 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700539 item.setChecked(mShowWifi);
540 updateTabs();
541 return true;
542 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700543 case R.id.data_usage_menu_show_ethernet: {
544 mShowEthernet = !item.isChecked();
545 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
546 item.setChecked(mShowEthernet);
547 updateTabs();
548 return true;
549 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700550 case R.id.data_usage_menu_metered: {
551 final PreferenceActivity activity = (PreferenceActivity) getActivity();
552 activity.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null,
553 R.string.data_usage_metered_title, null, this, 0);
554 return true;
555 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700556 case R.id.data_usage_menu_auto_sync: {
Guang Zhu167ba2a2012-10-19 17:55:05 -0700557 if (ActivityManager.isUserAMonkey()) {
558 Log.d("SyncState", "ignoring monkey's attempt to flip global sync state");
559 } else {
560 ConfirmAutoSyncChangeFragment.show(this, !item.isChecked());
561 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700562 return true;
563 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700564 }
565 return false;
566 }
567
Jeff Sharkey94a90952011-06-13 22:31:09 -0700568 @Override
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700569 public void onDestroy() {
Jeff Sharkey94a90952011-06-13 22:31:09 -0700570 mDataEnabledView = null;
571 mDisableAtLimitView = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700572
573 mUidDetailProvider.clearCache();
574 mUidDetailProvider = null;
Jeff Sharkey08ce99e2012-04-06 11:21:28 -0700575
576 TrafficStats.closeQuietly(mStatsSession);
Jeff Sharkey94a90952011-06-13 22:31:09 -0700577
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800578 if (this.isRemoving()) {
579 getFragmentManager()
580 .popBackStack(TAG_APP_DETAILS, FragmentManager.POP_BACK_STACK_INCLUSIVE);
581 }
Jeff Sharkey02b327e2012-05-15 11:33:59 -0700582
Amith Yamasani5ba0a022011-11-07 12:29:00 -0800583 super.onDestroy();
584 }
585
Jeff Sharkey8a503642011-06-10 13:31:21 -0700586 /**
Jeff Sharkey92811822012-05-04 11:47:29 -0700587 * Build and assign {@link LayoutTransition} to various containers. Should
588 * only be assigned after initial layout is complete.
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700589 */
Jeff Sharkey92811822012-05-04 11:47:29 -0700590 private void ensureLayoutTransitions() {
591 // skip when already setup
592 if (mChart.getLayoutTransition() != null) return;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700593
Jeff Sharkey92811822012-05-04 11:47:29 -0700594 mTabsContainer.setLayoutTransition(buildLayoutTransition());
595 mHeader.setLayoutTransition(buildLayoutTransition());
596 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700597
Jeff Sharkey92811822012-05-04 11:47:29 -0700598 final LayoutTransition chartTransition = buildLayoutTransition();
599 chartTransition.disableTransitionType(LayoutTransition.APPEARING);
600 chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING);
601 mChart.setLayoutTransition(chartTransition);
602 }
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700603
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700604 private static LayoutTransition buildLayoutTransition() {
605 final LayoutTransition transition = new LayoutTransition();
606 if (TEST_ANIM) {
607 transition.setDuration(1500);
608 }
609 transition.setAnimateParentHierarchy(false);
610 return transition;
611 }
612
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700613 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700614 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700615 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
616 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700617 */
618 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700619 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700620 mTabHost.clearAllTabs();
621
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700622 final boolean mobileSplit = isMobilePolicySplit();
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700623 if (mobileSplit && hasReadyMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700624 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
625 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700626 } else if (hasReadyMobileRadio(context)) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700627 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700628 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700629 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700630 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
631 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700632 if (mShowEthernet && hasEthernet(context)) {
633 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
634 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700635
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700636 final boolean noTabs = mTabWidget.getTabCount() == 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700637 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
638 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
639 if (mIntentTab != null) {
640 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700641 // already hit updateBody() when added; ignore
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700642 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700643 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700644 mTabHost.setCurrentTabByTag(mIntentTab);
645 }
646 mIntentTab = null;
Jeff Sharkeyad17de32012-04-11 11:03:25 -0700647 } else if (noTabs) {
648 // no usable tabs, so hide body
649 updateBody();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700650 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700651 // already hit updateBody() when added; ignore
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700652 }
653 }
654
Jeff Sharkey8a503642011-06-10 13:31:21 -0700655 /**
656 * Factory that provide empty {@link View} to make {@link TabHost} happy.
657 */
658 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700659 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700660 public View createTabContent(String tag) {
661 return new View(mTabHost.getContext());
662 }
663 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700664
Jeff Sharkey8a503642011-06-10 13:31:21 -0700665 /**
666 * Build {@link TabSpec} with thin indicator, and empty content.
667 */
668 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700669 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
670 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700671 }
672
673 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -0700674 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700675 public void onTabChanged(String tabId) {
676 // user changed tab; update body
677 updateBody();
678 }
679 };
680
681 /**
682 * Update body content based on current tab. Loads
683 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
684 * binds them to visible controls.
685 */
686 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700687 mBinding = true;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700688 if (!isAdded()) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700689
Jeff Sharkeya662e492011-06-18 21:57:06 -0700690 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700691 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700692 final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700693
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700694 if (currentTab == null) {
695 Log.w(TAG, "no tab selected; hiding body");
696 mListView.setVisibility(View.GONE);
697 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700698 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700699 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700700 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700701
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700702 final boolean tabChanged = !currentTab.equals(mCurrentTab);
703 mCurrentTab = currentTab;
704
Jeff Sharkey8a503642011-06-10 13:31:21 -0700705 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
706
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700707 mDataEnabledView.setVisibility(isOwner ? View.VISIBLE : View.GONE);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700708
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700709 // TODO: remove mobile tabs when SIM isn't ready
710 final TelephonyManager tele = TelephonyManager.from(context);
711
Jeff Sharkey8a503642011-06-10 13:31:21 -0700712 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700713 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
714 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700715 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700716
717 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700718 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
719 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
720 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700721 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700722
723 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700724 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
725 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
726 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700727 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700728
729 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700730 // wifi doesn't have any controls
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700731 mDataEnabledView.setVisibility(View.GONE);
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700732 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700733 mTemplate = buildTemplateWifiWildcard();
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700734
735 } else if (TAB_ETHERNET.equals(currentTab)) {
736 // ethernet doesn't have any controls
737 mDataEnabledView.setVisibility(View.GONE);
738 mDisableAtLimitView.setVisibility(View.GONE);
739 mTemplate = buildTemplateEthernet();
740
741 } else {
742 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700743 }
744
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700745 // kick off loader for network history
746 // TODO: consider chaining two loaders together instead of reloading
747 // network history when showing app detail.
748 getLoaderManager().restartLoader(LOADER_CHART_DATA,
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700749 ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700750
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700751 // detail mode can change visible menus, invalidate
752 getActivity().invalidateOptionsMenu();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700753
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700754 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700755 }
756
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700757 private boolean isAppDetailMode() {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700758 return mCurrentApp != null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700759 }
760
761 /**
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -0700762 * Update UID details panels to match {@link #mCurrentApp}, showing or
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700763 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700764 */
765 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700766 final Context context = getActivity();
767 final PackageManager pm = context.getPackageManager();
768 final LayoutInflater inflater = getActivity().getLayoutInflater();
769
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700770 if (isAppDetailMode()) {
771 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700772 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700773 } else {
774 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700775 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700776
777 // hide detail stats when not in detail mode
778 mChart.bindDetailNetworkStats(null);
779 return;
780 }
781
782 // remove warning/limit sweeps while in detail mode
783 mChart.bindNetworkPolicy(null);
784
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700785 // show icon and all labels appearing under this app
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700786 final int uid = mCurrentApp.key;
787 final UidDetail detail = mUidDetailProvider.getUidDetail(uid, true);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700788 mAppIcon.setImageDrawable(detail.icon);
789
790 mAppTitles.removeAllViews();
791 if (detail.detailLabels != null) {
792 for (CharSequence label : detail.detailLabels) {
793 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label));
794 }
795 } else {
796 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label));
797 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700798
799 // enable settings button when package provides it
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700800 final String[] packageNames = pm.getPackagesForUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700801 if (packageNames != null && packageNames.length > 0) {
802 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700803 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
804
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700805 // Search for match across all packages
806 boolean matchFound = false;
807 for (String packageName : packageNames) {
808 mAppSettingsIntent.setPackage(packageName);
809 if (pm.resolveActivity(mAppSettingsIntent, 0) != null) {
810 matchFound = true;
811 break;
812 }
813 }
814
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700815 mAppSettings.setEnabled(matchFound);
Jeff Sharkeyd92e0412012-04-24 11:35:43 -0700816 mAppSettings.setVisibility(View.VISIBLE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700817
818 } else {
819 mAppSettingsIntent = null;
Jeff Sharkey34e964d2012-04-21 15:41:48 -0700820 mAppSettings.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700821 }
822
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700823 updateDetailData();
824
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700825 if (UserHandle.isApp(uid) && !mPolicyManager.getRestrictBackground()
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700826 && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700827 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
Jeff Sharkey3038c522011-11-30 15:37:51 -0800828 setPreferenceSummary(mAppRestrictView,
829 getString(R.string.data_usage_app_restrict_background_summary));
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700830
831 mAppRestrictView.setVisibility(View.VISIBLE);
832 mAppRestrict.setChecked(getAppRestrictBackground());
833
834 } else {
835 mAppRestrictView.setVisibility(View.GONE);
836 }
837 }
838
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700839 private void setPolicyWarningBytes(long warningBytes) {
840 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700841 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700842 updatePolicy(false);
843 }
844
845 private void setPolicyLimitBytes(long limitBytes) {
846 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700847 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700848 updatePolicy(false);
849 }
850
Jeff Sharkey28130d92011-09-02 16:10:24 -0700851 /**
852 * Local cache of value, used to work around delay when
853 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
854 */
855 private Boolean mMobileDataEnabled;
856
857 private boolean isMobileDataEnabled() {
858 if (mMobileDataEnabled != null) {
859 // TODO: deprecate and remove this once enabled flag is on policy
860 return mMobileDataEnabled;
861 } else {
862 return mConnService.getMobileDataEnabled();
863 }
864 }
865
866 private void setMobileDataEnabled(boolean enabled) {
867 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
868 mConnService.setMobileDataEnabled(enabled);
869 mMobileDataEnabled = enabled;
870 updatePolicy(false);
871 }
872
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700873 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700874 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked()
875 && ActivityManager.getCurrentUser() == UserHandle.USER_OWNER;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700876 }
877
878 private boolean isBandwidthControlEnabled() {
879 try {
880 return mNetworkService.isBandwidthControlEnabled();
881 } catch (RemoteException e) {
882 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
883 return false;
884 }
885 }
886
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700887 private boolean getDataRoaming() {
888 final ContentResolver resolver = getActivity().getContentResolver();
Christopher Tate5a64c732012-09-07 13:35:31 -0700889 return Settings.Global.getInt(resolver, Settings.Global.DATA_ROAMING, 0) != 0;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700890 }
891
892 private void setDataRoaming(boolean enabled) {
893 // TODO: teach telephony DataConnectionTracker to watch and apply
894 // updates when changed.
895 final ContentResolver resolver = getActivity().getContentResolver();
Christopher Tate5a64c732012-09-07 13:35:31 -0700896 Settings.Global.putInt(resolver, Settings.Global.DATA_ROAMING, enabled ? 1 : 0);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700897 mMenuDataRoaming.setChecked(enabled);
898 }
899
Jeff Sharkey313f7d82012-04-03 21:13:25 -0700900 public void setRestrictBackground(boolean restrictBackground) {
901 mPolicyManager.setRestrictBackground(restrictBackground);
902 mMenuRestrictBackground.setChecked(restrictBackground);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700903 }
904
905 private boolean getAppRestrictBackground() {
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700906 final int uid = mCurrentApp.key;
907 final int uidPolicy = mPolicyManager.getUidPolicy(uid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700908 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
909 }
910
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700911 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700912 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey38305fb2012-09-14 16:26:51 -0700913 final int uid = mCurrentApp.key;
914 mPolicyManager.setUidPolicy(
915 uid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700916 mAppRestrict.setChecked(restrictBackground);
917 }
918
Jeff Sharkey8a503642011-06-10 13:31:21 -0700919 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700920 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
921 * current {@link #mTemplate}.
922 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700923 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700924 if (isAppDetailMode()) {
925 mNetworkSwitches.setVisibility(View.GONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700926 } else {
927 mNetworkSwitches.setVisibility(View.VISIBLE);
928 }
929
Jeff Sharkey28130d92011-09-02 16:10:24 -0700930 // TODO: move enabled state directly into policy
931 if (TAB_MOBILE.equals(mCurrentTab)) {
932 mBinding = true;
933 mDataEnabled.setChecked(isMobileDataEnabled());
934 mBinding = false;
935 }
936
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700937 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
938 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700939 mDisableAtLimitView.setVisibility(View.VISIBLE);
940 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700941 if (!isAppDetailMode()) {
942 mChart.bindNetworkPolicy(policy);
943 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700944
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700945 } else {
946 // controls are disabled; don't bind warning/limit sweeps
947 mDisableAtLimitView.setVisibility(View.GONE);
948 mChart.bindNetworkPolicy(null);
949 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700950
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700951 if (refreshCycle) {
952 // generate cycle list based on policy and available history
953 updateCycleList(policy);
954 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700955 }
956
957 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700958 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
959 * and available {@link NetworkStatsHistory} data. Always selects the newest
960 * item, updating the inspection range on {@link #mChart}.
961 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700962 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -0700963 // stash away currently selected cycle to try restoring below
964 final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700965 mCycleAdapter.clear();
966
967 final Context context = mCycleSpinner.getContext();
968
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700969 long historyStart = Long.MAX_VALUE;
970 long historyEnd = Long.MIN_VALUE;
971 if (mChartData != null) {
972 historyStart = mChartData.network.getStart();
973 historyEnd = mChartData.network.getEnd();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700974 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700975
Jeff Sharkey461842a2011-09-25 18:22:48 -0700976 final long now = System.currentTimeMillis();
977 if (historyStart == Long.MAX_VALUE) historyStart = now;
978 if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -0700979
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700980 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700981 if (policy != null) {
982 // find the next cycle boundary
983 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700984
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700985 // walk backwards, generating all valid cycle ranges
986 while (cycleEnd > historyStart) {
987 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
988 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
989 + historyStart);
990 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
991 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700992 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700993 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700994
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700995 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700996 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700997 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700998
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700999 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001000 // no policy defined cycles; show entry for each four-week period
1001 long cycleEnd = historyEnd;
1002 while (cycleEnd > historyStart) {
1003 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
1004 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1005 cycleEnd = cycleStart;
1006 }
1007
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001008 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001009 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001010
1011 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001012 if (mCycleAdapter.getCount() > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001013 final int position = mCycleAdapter.findNearestPosition(previousItem);
1014 mCycleSpinner.setSelection(position);
1015
1016 // only force-update cycle when changed; skipping preserves any
1017 // user-defined inspection region.
1018 final CycleItem selectedItem = mCycleAdapter.getItem(position);
1019 if (!Objects.equal(selectedItem, previousItem)) {
1020 mCycleListener.onItemSelected(mCycleSpinner, null, position, 0);
1021 } else {
1022 // but still kick off loader for detailed list
1023 updateDetailData();
1024 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001025 } else {
1026 updateDetailData();
1027 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001028 }
1029
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001030 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001031 @Override
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001032 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
1033 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001034
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001035 final boolean dataEnabled = isChecked;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001036 final String currentTab = mCurrentTab;
1037 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001038 if (dataEnabled) {
1039 setMobileDataEnabled(true);
1040 } else {
1041 // disabling data; show confirmation dialog which eventually
1042 // calls setMobileDataEnabled() once user confirms.
1043 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1044 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001045 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001046
Jeff Sharkey28130d92011-09-02 16:10:24 -07001047 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001048 }
1049 };
1050
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001051 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001052 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001053 public void onClick(View v) {
1054 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001055 if (disableAtLimit) {
1056 // enabling limit; show confirmation dialog which eventually
1057 // calls setPolicyLimitBytes() once user confirms.
1058 ConfirmLimitFragment.show(DataUsageSummary.this);
1059 } else {
1060 setPolicyLimitBytes(LIMIT_DISABLED);
1061 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001062 }
1063 };
1064
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001065 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001066 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001067 public void onClick(View v) {
1068 final boolean restrictBackground = !mAppRestrict.isChecked();
1069
1070 if (restrictBackground) {
Jeff Sharkey3038c522011-11-30 15:37:51 -08001071 // enabling restriction; show confirmation dialog which
1072 // eventually calls setRestrictBackground() once user
1073 // confirms.
1074 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001075 } else {
1076 setAppRestrictBackground(false);
1077 }
1078 }
1079 };
1080
1081 private OnClickListener mAppSettingsListener = new OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001082 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001083 public void onClick(View v) {
Jeff Sharkeyd92e0412012-04-24 11:35:43 -07001084 if (!isAdded()) return;
1085
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001086 // TODO: target torwards entire UID instead of just first package
1087 startActivity(mAppSettingsIntent);
1088 }
1089 };
1090
Jeff Sharkey8a503642011-06-10 13:31:21 -07001091 private OnItemClickListener mListListener = new OnItemClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001092 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001093 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001094 final Context context = view.getContext();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001095 final AppItem app = (AppItem) parent.getItemAtPosition(position);
Jeff Sharkey3da415f2012-05-18 14:00:10 -07001096
1097 // TODO: sigh, remove this hack once we understand 6450986
1098 if (mUidDetailProvider == null || app == null) return;
1099
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001100 final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001101 AppDetailsFragment.show(DataUsageSummary.this, app, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001102 }
1103 };
1104
1105 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001106 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001107 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1108 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1109 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001110 // show cycle editor; will eventually call setPolicyCycleDay()
1111 // when user finishes editing.
1112 CycleEditorFragment.show(DataUsageSummary.this);
1113
1114 // reset spinner to something other than "change cycle..."
1115 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001116
1117 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001118 if (LOGD) {
1119 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1120 + cycle.end + "]");
1121 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001122
1123 // update chart to show selected cycle, and update detail data
1124 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001125 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001126
1127 updateDetailData();
1128 }
1129 }
1130
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001131 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001132 public void onNothingSelected(AdapterView<?> parent) {
1133 // ignored
1134 }
1135 };
1136
1137 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001138 * Update details based on {@link #mChart} inspection range depending on
1139 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1140 * of applications data usage, and when {@link #isAppDetailMode()} update
1141 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001142 */
1143 private void updateDetailData() {
1144 if (LOGD) Log.d(TAG, "updateDetailData()");
1145
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001146 final long start = mChart.getInspectStart();
1147 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001148 final long now = System.currentTimeMillis();
1149
1150 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001151
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001152 NetworkStatsHistory.Entry entry = null;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001153 if (isAppDetailMode() && mChartData != null && mChartData.detail != null) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001154 // bind foreground/background to piechart and labels
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001155 entry = mChartData.detailDefault.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001156 final long defaultBytes = entry.rxBytes + entry.txBytes;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001157 entry = mChartData.detailForeground.getValues(start, end, now, entry);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001158 final long foregroundBytes = entry.rxBytes + entry.txBytes;
1159
1160 mAppPieChart.setOriginAngle(175);
1161
1162 mAppPieChart.removeAllSlices();
1163 mAppPieChart.addSlice(foregroundBytes, Color.parseColor("#d88d3a"));
1164 mAppPieChart.addSlice(defaultBytes, Color.parseColor("#666666"));
1165
1166 mAppPieChart.generatePath();
1167
1168 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1169 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1170
1171 // and finally leave with summary data for label below
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001172 entry = mChartData.detail.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001173
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001174 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001175
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001176 } else {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001177 if (mChartData != null) {
1178 entry = mChartData.network.getValues(start, end, now, null);
1179 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001180
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001181 // kick off loader for detailed stats
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001182 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001183 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001184 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001185
1186 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1187 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001188 final String rangePhrase = formatDateRange(context, start, end);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001189
Jeff Sharkeye557c332012-04-13 16:04:07 -07001190 final int summaryRes;
1191 if (TAB_MOBILE.equals(mCurrentTab) || TAB_3G.equals(mCurrentApp)
1192 || TAB_4G.equals(mCurrentApp)) {
1193 summaryRes = R.string.data_usage_total_during_range_mobile;
1194 } else {
1195 summaryRes = R.string.data_usage_total_during_range;
1196 }
1197
1198 mUsageSummary.setText(getString(summaryRes, totalPhrase, rangePhrase));
Jeff Sharkey92811822012-05-04 11:47:29 -07001199
1200 // initial layout is finished above, ensure we have transitions
1201 ensureLayoutTransitions();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001202 }
1203
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001204 private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks<
1205 ChartData>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001206 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001207 public Loader<ChartData> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001208 return new ChartDataLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001209 }
1210
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001211 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001212 public void onLoadFinished(Loader<ChartData> loader, ChartData data) {
1213 mChartData = data;
1214 mChart.bindNetworkStats(mChartData.network);
1215 mChart.bindDetailNetworkStats(mChartData.detail);
1216
1217 // calcuate policy cycles based on available data
1218 updatePolicy(true);
1219 updateAppDetail();
1220
1221 // force scroll to top of body when showing detail
1222 if (mChartData.detail != null) {
1223 mListView.smoothScrollToPosition(0);
1224 }
1225 }
1226
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001227 @Override
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001228 public void onLoaderReset(Loader<ChartData> loader) {
1229 mChartData = null;
1230 mChart.bindNetworkStats(null);
1231 mChart.bindDetailNetworkStats(null);
1232 }
1233 };
1234
1235 private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks<
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001236 NetworkStats>() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001237 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001238 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
Jeff Sharkey08ce99e2012-04-06 11:21:28 -07001239 return new SummaryForAllUidLoader(getActivity(), mStatsSession, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001240 }
1241
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001242 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001243 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001244 final int[] restrictedUids = mPolicyManager.getUidsWithPolicy(
Jeff Sharkeye557c332012-04-13 16:04:07 -07001245 POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001246 mAdapter.bindStats(data, restrictedUids);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001247 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001248 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001249
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001250 @Override
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001251 public void onLoaderReset(Loader<NetworkStats> loader) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001252 mAdapter.bindStats(null, new int[0]);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001253 updateEmptyVisible();
1254 }
1255
1256 private void updateEmptyVisible() {
1257 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1258 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001259 }
1260 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001261
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001262 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001263 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001264 final Context context = getActivity();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001265 if (hasReadyMobileRadio(context)) {
1266 final TelephonyManager tele = TelephonyManager.from(context);
1267 return mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001268 } else {
1269 return false;
1270 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001271 }
1272
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001273 @Deprecated
Jeff Sharkeya662e492011-06-18 21:57:06 -07001274 private void setMobilePolicySplit(boolean split) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001275 final Context context = getActivity();
1276 if (hasReadyMobileRadio(context)) {
1277 final TelephonyManager tele = TelephonyManager.from(context);
1278 mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split);
1279 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001280 }
1281
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001282 private static String getActiveSubscriberId(Context context) {
Jeff Sharkey313f7d82012-04-03 21:13:25 -07001283 final TelephonyManager tele = TelephonyManager.from(context);
1284 final String actualSubscriberId = tele.getSubscriberId();
Jeff Sharkeyf3871fb2012-02-03 19:27:07 -08001285 return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001286 }
1287
Jeff Sharkey8a503642011-06-10 13:31:21 -07001288 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001289 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -07001290 public void onInspectRangeChanged() {
1291 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
1292 updateDetailData();
1293 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001294
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001295 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001296 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001297 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001298 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001299
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001300 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001301 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001302 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001303 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001304
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001305 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001306 public void requestWarningEdit() {
1307 WarningEditorFragment.show(DataUsageSummary.this);
1308 }
1309
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001310 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001311 public void requestLimitEdit() {
1312 LimitEditorFragment.show(DataUsageSummary.this);
1313 }
1314 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001315
1316 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001317 * List item that reflects a specific data usage cycle.
1318 */
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001319 public static class CycleItem implements Comparable<CycleItem> {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001320 public CharSequence label;
1321 public long start;
1322 public long end;
1323
Jeff Sharkey8a503642011-06-10 13:31:21 -07001324 CycleItem(CharSequence label) {
1325 this.label = label;
1326 }
1327
1328 public CycleItem(Context context, long start, long end) {
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001329 this.label = formatDateRange(context, start, end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001330 this.start = start;
1331 this.end = end;
1332 }
1333
Jeff Sharkey8a503642011-06-10 13:31:21 -07001334 @Override
1335 public String toString() {
1336 return label.toString();
1337 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001338
1339 @Override
1340 public boolean equals(Object o) {
1341 if (o instanceof CycleItem) {
1342 final CycleItem another = (CycleItem) o;
1343 return start == another.start && end == another.end;
1344 }
1345 return false;
1346 }
1347
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001348 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001349 public int compareTo(CycleItem another) {
1350 return Long.compare(start, another.start);
1351 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001352 }
1353
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001354 private static final StringBuilder sBuilder = new StringBuilder(50);
1355 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1356 sBuilder, Locale.getDefault());
1357
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001358 public static String formatDateRange(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001359 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001360
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001361 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001362 sBuilder.setLength(0);
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001363 return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null)
1364 .toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001365 }
1366 }
1367
Jeff Sharkey8a503642011-06-10 13:31:21 -07001368 /**
1369 * Special-case data usage cycle that triggers dialog to change
1370 * {@link NetworkPolicy#cycleDay}.
1371 */
1372 public static class CycleChangeItem extends CycleItem {
1373 public CycleChangeItem(Context context) {
1374 super(context.getString(R.string.data_usage_change_cycle));
1375 }
1376 }
1377
1378 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001379 private boolean mChangePossible = false;
1380 private boolean mChangeVisible = false;
1381
1382 private final CycleChangeItem mChangeItem;
1383
Jeff Sharkey8a503642011-06-10 13:31:21 -07001384 public CycleAdapter(Context context) {
1385 super(context, android.R.layout.simple_spinner_item);
1386 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001387 mChangeItem = new CycleChangeItem(context);
1388 }
1389
1390 public void setChangePossible(boolean possible) {
1391 mChangePossible = possible;
1392 updateChange();
1393 }
1394
1395 public void setChangeVisible(boolean visible) {
1396 mChangeVisible = visible;
1397 updateChange();
1398 }
1399
1400 private void updateChange() {
1401 remove(mChangeItem);
1402 if (mChangePossible && mChangeVisible) {
1403 add(mChangeItem);
1404 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001405 }
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001406
1407 /**
1408 * Find position of {@link CycleItem} in this adapter which is nearest
1409 * the given {@link CycleItem}.
1410 */
1411 public int findNearestPosition(CycleItem target) {
1412 if (target != null) {
1413 final int count = getCount();
1414 for (int i = count - 1; i >= 0; i--) {
1415 final CycleItem item = getItem(i);
1416 if (item instanceof CycleChangeItem) {
1417 continue;
1418 } else if (item.compareTo(target) >= 0) {
1419 return i;
1420 }
1421 }
1422 }
1423 return 0;
1424 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001425 }
1426
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001427 public static class AppItem implements Comparable<AppItem>, Parcelable {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001428 public final int key;
Jeff Sharkeye557c332012-04-13 16:04:07 -07001429 public boolean restricted;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001430 public SparseBooleanArray uids = new SparseBooleanArray();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001431 public long total;
1432
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001433 public AppItem(int key) {
1434 this.key = key;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001435 }
1436
1437 public AppItem(Parcel parcel) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001438 key = parcel.readInt();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001439 uids = parcel.readSparseBooleanArray();
1440 total = parcel.readLong();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001441 }
1442
1443 public void addUid(int uid) {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001444 uids.put(uid, true);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001445 }
1446
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001447 @Override
1448 public void writeToParcel(Parcel dest, int flags) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001449 dest.writeInt(key);
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001450 dest.writeSparseBooleanArray(uids);
1451 dest.writeLong(total);
1452 }
1453
1454 @Override
1455 public int describeContents() {
1456 return 0;
1457 }
1458
1459 @Override
1460 public int compareTo(AppItem another) {
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001461 return Long.compare(another.total, total);
1462 }
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001463
1464 public static final Creator<AppItem> CREATOR = new Creator<AppItem>() {
1465 @Override
1466 public AppItem createFromParcel(Parcel in) {
1467 return new AppItem(in);
1468 }
1469
1470 @Override
1471 public AppItem[] newArray(int size) {
1472 return new AppItem[size];
1473 }
1474 };
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001475 }
1476
Jeff Sharkey8a503642011-06-10 13:31:21 -07001477 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001478 * Adapter of applications, sorted by total usage descending.
1479 */
1480 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001481 private final UidDetailProvider mProvider;
1482 private final int mInsetSide;
1483
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001484 private ArrayList<AppItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001485 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001486
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001487 public DataUsageAdapter(UidDetailProvider provider, int insetSide) {
1488 mProvider = checkNotNull(provider);
1489 mInsetSide = insetSide;
1490 }
1491
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001492 /**
1493 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1494 */
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001495 public void bindStats(NetworkStats stats, int[] restrictedUids) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001496 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001497
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001498 final int currentUserId = ActivityManager.getCurrentUser();
1499 final SparseArray<AppItem> knownItems = new SparseArray<AppItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001500
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001501 NetworkStats.Entry entry = null;
1502 final int size = stats != null ? stats.size() : 0;
1503 for (int i = 0; i < size; i++) {
1504 entry = stats.getValues(i, entry);
1505
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001506 // Decide how to collapse items together
1507 final int uid = entry.uid;
1508 final int collapseKey;
1509 if (UserHandle.isApp(uid)) {
1510 if (UserHandle.getUserId(uid) == currentUserId) {
1511 collapseKey = uid;
1512 } else {
1513 collapseKey = UidDetailProvider.buildKeyForUser(UserHandle.getUserId(uid));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001514 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001515 } else if (uid == UID_REMOVED || uid == UID_TETHERING) {
1516 collapseKey = uid;
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001517 } else {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001518 collapseKey = android.os.Process.SYSTEM_UID;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001519 }
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001520
1521 AppItem item = knownItems.get(collapseKey);
1522 if (item == null) {
1523 item = new AppItem(collapseKey);
1524 mItems.add(item);
1525 knownItems.put(item.key, item);
1526 }
1527 item.addUid(uid);
1528 item.total += entry.rxBytes + entry.txBytes;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001529 }
1530
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001531 for (int uid : restrictedUids) {
1532 // Only splice in restricted state for current user
1533 if (UserHandle.getUserId(uid) != currentUserId) continue;
1534
1535 AppItem item = knownItems.get(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001536 if (item == null) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001537 item = new AppItem(uid);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001538 item.total = -1;
1539 mItems.add(item);
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001540 knownItems.put(item.key, item);
Jeff Sharkeye557c332012-04-13 16:04:07 -07001541 }
1542 item.restricted = true;
1543 }
1544
Jeff Sharkey8a503642011-06-10 13:31:21 -07001545 Collections.sort(mItems);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001546 mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001547 notifyDataSetChanged();
1548 }
1549
1550 @Override
1551 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001552 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001553 }
1554
1555 @Override
1556 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001557 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001558 }
1559
1560 @Override
1561 public long getItemId(int position) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07001562 return mItems.get(position).key;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001563 }
1564
1565 @Override
1566 public View getView(int position, View convertView, ViewGroup parent) {
1567 if (convertView == null) {
1568 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey5d706792011-09-08 18:57:17 -07001569 R.layout.data_usage_item, parent, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001570
1571 if (mInsetSide > 0) {
Fabrice Di Megliob27223f2013-01-15 18:54:11 -08001572 convertView.setPaddingRelative(mInsetSide, 0, mInsetSide, 0);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001573 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001574 }
1575
1576 final Context context = parent.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001577
Jeff Sharkey28130d92011-09-02 16:10:24 -07001578 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001579 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1580 android.R.id.progress);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001581
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001582 // kick off async load of app details
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001583 final AppItem item = mItems.get(position);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07001584 UidDetailTask.bindView(mProvider, item, convertView);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001585
Jeff Sharkeye557c332012-04-13 16:04:07 -07001586 if (item.restricted && item.total <= 0) {
1587 text1.setText(R.string.data_usage_app_restricted);
1588 progress.setVisibility(View.GONE);
1589 } else {
1590 text1.setText(Formatter.formatFileSize(context, item.total));
1591 progress.setVisibility(View.VISIBLE);
1592 }
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001593
1594 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1595 progress.setProgress(percentTotal);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001596
1597 return convertView;
1598 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001599 }
1600
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001601 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001602 * Empty {@link Fragment} that controls display of UID details in
1603 * {@link DataUsageSummary}.
1604 */
1605 public static class AppDetailsFragment extends Fragment {
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001606 private static final String EXTRA_APP = "app";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001607
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001608 public static void show(DataUsageSummary parent, AppItem app, CharSequence label) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001609 if (!parent.isAdded()) return;
1610
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001611 final Bundle args = new Bundle();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001612 args.putParcelable(EXTRA_APP, app);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001613
1614 final AppDetailsFragment fragment = new AppDetailsFragment();
1615 fragment.setArguments(args);
1616 fragment.setTargetFragment(parent, 0);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001617 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1618 ft.add(fragment, TAG_APP_DETAILS);
1619 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001620 ft.setBreadCrumbTitle(label);
Jeff Sharkey3235ddb2012-03-15 16:40:31 -07001621 ft.commitAllowingStateLoss();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001622 }
1623
1624 @Override
1625 public void onStart() {
1626 super.onStart();
1627 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001628 target.mCurrentApp = getArguments().getParcelable(EXTRA_APP);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001629 target.updateBody();
1630 }
1631
1632 @Override
1633 public void onStop() {
1634 super.onStop();
1635 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07001636 target.mCurrentApp = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001637 target.updateBody();
1638 }
1639 }
1640
1641 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001642 * Dialog to request user confirmation before setting
1643 * {@link NetworkPolicy#limitBytes}.
1644 */
1645 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001646 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001647 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001648
1649 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001650 if (!parent.isAdded()) return;
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001651
Jeff Sharkey461842a2011-09-25 18:22:48 -07001652 final Resources res = parent.getResources();
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001653 final CharSequence message;
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001654 final long minLimitBytes = (long) (
1655 parent.mPolicyEditor.getPolicy(parent.mTemplate).warningBytes * 1.2f);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001656 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001657
1658 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001659 final String currentTab = parent.mCurrentTab;
1660 if (TAB_3G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001661 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001662 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001663 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001664 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001665 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001666 } else if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkeye557c332012-04-13 16:04:07 -07001667 message = res.getString(R.string.data_usage_limit_dialog_mobile);
Jeff Sharkey34e964d2012-04-21 15:41:48 -07001668 limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001669 } else {
1670 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001671 }
1672
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001673 final Bundle args = new Bundle();
1674 args.putCharSequence(EXTRA_MESSAGE, message);
1675 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1676
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001677 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1678 dialog.setArguments(args);
1679 dialog.setTargetFragment(parent, 0);
1680 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1681 }
1682
1683 @Override
1684 public Dialog onCreateDialog(Bundle savedInstanceState) {
1685 final Context context = getActivity();
1686
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001687 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001688 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1689
1690 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1691 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001692 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001693
1694 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001695 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001696 public void onClick(DialogInterface dialog, int which) {
1697 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1698 if (target != null) {
1699 target.setPolicyLimitBytes(limitBytes);
1700 }
1701 }
1702 });
1703
1704 return builder.create();
1705 }
1706 }
1707
1708 /**
1709 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1710 */
1711 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001712 private static final String EXTRA_TEMPLATE = "template";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001713
1714 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001715 if (!parent.isAdded()) return;
1716
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001717 final Bundle args = new Bundle();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001718 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001719
1720 final CycleEditorFragment dialog = new CycleEditorFragment();
1721 dialog.setArguments(args);
1722 dialog.setTargetFragment(parent, 0);
1723 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1724 }
1725
1726 @Override
1727 public Dialog onCreateDialog(Bundle savedInstanceState) {
1728 final Context context = getActivity();
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001729 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1730 final NetworkPolicyEditor editor = target.mPolicyEditor;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001731
1732 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1733 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1734
1735 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1736 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1737
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001738 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1739 final int cycleDay = editor.getPolicyCycleDay(template);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001740
1741 cycleDayPicker.setMinValue(1);
1742 cycleDayPicker.setMaxValue(31);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001743 cycleDayPicker.setValue(cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001744 cycleDayPicker.setWrapSelectorWheel(true);
1745
1746 builder.setTitle(R.string.data_usage_cycle_editor_title);
1747 builder.setView(view);
1748
1749 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1750 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001751 @Override
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001752 public void onClick(DialogInterface dialog, int which) {
Jeff Sharkeyd277de92013-03-25 15:11:25 -07001753 // clear focus to finish pending text edits
1754 cycleDayPicker.clearFocus();
1755
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001756 final int cycleDay = cycleDayPicker.getValue();
Jeff Sharkeye5223a02012-03-09 17:11:14 -08001757 final String cycleTimezone = new Time().timezone;
1758 editor.setPolicyCycleDay(template, cycleDay, cycleTimezone);
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001759 target.updatePolicy(true);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001760 }
1761 });
1762
1763 return builder.create();
1764 }
1765 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001766
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001767 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001768 * Dialog to edit {@link NetworkPolicy#warningBytes}.
1769 */
1770 public static class WarningEditorFragment extends DialogFragment {
1771 private static final String EXTRA_TEMPLATE = "template";
1772
1773 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001774 if (!parent.isAdded()) return;
1775
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001776 final Bundle args = new Bundle();
1777 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1778
1779 final WarningEditorFragment dialog = new WarningEditorFragment();
1780 dialog.setArguments(args);
1781 dialog.setTargetFragment(parent, 0);
1782 dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR);
1783 }
1784
1785 @Override
1786 public Dialog onCreateDialog(Bundle savedInstanceState) {
1787 final Context context = getActivity();
1788 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1789 final NetworkPolicyEditor editor = target.mPolicyEditor;
1790
1791 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1792 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1793
1794 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1795 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1796
1797 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1798 final long warningBytes = editor.getPolicyWarningBytes(template);
1799 final long limitBytes = editor.getPolicyLimitBytes(template);
1800
1801 bytesPicker.setMinValue(0);
1802 if (limitBytes != LIMIT_DISABLED) {
1803 bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1);
1804 } else {
1805 bytesPicker.setMaxValue(Integer.MAX_VALUE);
1806 }
1807 bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES));
1808 bytesPicker.setWrapSelectorWheel(false);
1809
1810 builder.setTitle(R.string.data_usage_warning_editor_title);
1811 builder.setView(view);
1812
1813 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1814 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001815 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001816 public void onClick(DialogInterface dialog, int which) {
1817 // clear focus to finish pending text edits
1818 bytesPicker.clearFocus();
1819
1820 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1821 editor.setPolicyWarningBytes(template, bytes);
1822 target.updatePolicy(false);
1823 }
1824 });
1825
1826 return builder.create();
1827 }
1828 }
1829
1830 /**
1831 * Dialog to edit {@link NetworkPolicy#limitBytes}.
1832 */
1833 public static class LimitEditorFragment extends DialogFragment {
1834 private static final String EXTRA_TEMPLATE = "template";
1835
1836 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001837 if (!parent.isAdded()) return;
1838
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001839 final Bundle args = new Bundle();
1840 args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate);
1841
1842 final LimitEditorFragment dialog = new LimitEditorFragment();
1843 dialog.setArguments(args);
1844 dialog.setTargetFragment(parent, 0);
1845 dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR);
1846 }
1847
1848 @Override
1849 public Dialog onCreateDialog(Bundle savedInstanceState) {
1850 final Context context = getActivity();
1851 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1852 final NetworkPolicyEditor editor = target.mPolicyEditor;
1853
1854 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1855 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1856
1857 final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false);
1858 final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes);
1859
1860 final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE);
1861 final long warningBytes = editor.getPolicyWarningBytes(template);
1862 final long limitBytes = editor.getPolicyLimitBytes(template);
1863
1864 bytesPicker.setMaxValue(Integer.MAX_VALUE);
Shuhrat Dehkanovf9237f62012-01-26 23:04:02 +09001865 if (warningBytes != WARNING_DISABLED && limitBytes > 0) {
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001866 bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1);
1867 } else {
1868 bytesPicker.setMinValue(0);
1869 }
1870 bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES));
1871 bytesPicker.setWrapSelectorWheel(false);
1872
1873 builder.setTitle(R.string.data_usage_limit_editor_title);
1874 builder.setView(view);
1875
1876 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1877 new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001878 @Override
Jeff Sharkeya53188f2011-09-13 19:56:45 -07001879 public void onClick(DialogInterface dialog, int which) {
1880 // clear focus to finish pending text edits
1881 bytesPicker.clearFocus();
1882
1883 final long bytes = bytesPicker.getValue() * MB_IN_BYTES;
1884 editor.setPolicyLimitBytes(template, bytes);
1885 target.updatePolicy(false);
1886 }
1887 });
1888
1889 return builder.create();
1890 }
1891 }
1892 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07001893 * Dialog to request user confirmation before disabling data.
1894 */
1895 public static class ConfirmDataDisableFragment extends DialogFragment {
1896 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001897 if (!parent.isAdded()) return;
1898
Jeff Sharkey28130d92011-09-02 16:10:24 -07001899 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
1900 dialog.setTargetFragment(parent, 0);
1901 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
1902 }
1903
1904 @Override
1905 public Dialog onCreateDialog(Bundle savedInstanceState) {
1906 final Context context = getActivity();
1907
1908 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1909 builder.setMessage(R.string.data_usage_disable_mobile);
1910
1911 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001912 @Override
Jeff Sharkey28130d92011-09-02 16:10:24 -07001913 public void onClick(DialogInterface dialog, int which) {
1914 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1915 if (target != null) {
1916 // TODO: extend to modify policy enabled flag.
1917 target.setMobileDataEnabled(false);
1918 }
1919 }
1920 });
1921 builder.setNegativeButton(android.R.string.cancel, null);
1922
1923 return builder.create();
1924 }
1925 }
1926
1927 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001928 * Dialog to request user confirmation before setting
Christopher Tate5a64c732012-09-07 13:35:31 -07001929 * {@link android.provider.Settings.Global#DATA_ROAMING}.
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001930 */
1931 public static class ConfirmDataRoamingFragment extends DialogFragment {
1932 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001933 if (!parent.isAdded()) return;
1934
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001935 final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment();
1936 dialog.setTargetFragment(parent, 0);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001937 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_ROAMING);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001938 }
1939
1940 @Override
1941 public Dialog onCreateDialog(Bundle savedInstanceState) {
1942 final Context context = getActivity();
1943
1944 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1945 builder.setTitle(R.string.roaming_reenable_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07001946 if (Utils.hasMultipleUsers(context)) {
1947 builder.setMessage(R.string.roaming_warning_multiuser);
1948 } else {
1949 builder.setMessage(R.string.roaming_warning);
1950 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001951
1952 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001953 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001954 public void onClick(DialogInterface dialog, int which) {
1955 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1956 if (target != null) {
1957 target.setDataRoaming(true);
1958 }
1959 }
1960 });
1961 builder.setNegativeButton(android.R.string.cancel, null);
1962
1963 return builder.create();
1964 }
1965 }
1966
1967 /**
1968 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001969 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001970 */
1971 public static class ConfirmRestrictFragment extends DialogFragment {
1972 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07001973 if (!parent.isAdded()) return;
1974
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001975 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1976 dialog.setTargetFragment(parent, 0);
1977 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1978 }
1979
1980 @Override
1981 public Dialog onCreateDialog(Bundle savedInstanceState) {
1982 final Context context = getActivity();
1983
1984 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001985 builder.setTitle(R.string.data_usage_restrict_background_title);
Amith Yamasani9627a8e2012-09-23 12:54:14 -07001986 if (Utils.hasMultipleUsers(context)) {
1987 builder.setMessage(R.string.data_usage_restrict_background_multiuser);
1988 } else {
1989 builder.setMessage(R.string.data_usage_restrict_background);
1990 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001991
1992 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07001993 @Override
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001994 public void onClick(DialogInterface dialog, int which) {
1995 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1996 if (target != null) {
1997 target.setRestrictBackground(true);
1998 }
1999 }
2000 });
2001 builder.setNegativeButton(android.R.string.cancel, null);
2002
2003 return builder.create();
2004 }
2005 }
2006
2007 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002008 * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND}
2009 * change has been denied, usually based on
2010 * {@link DataUsageSummary#hasLimitedNetworks()}.
2011 */
2012 public static class DeniedRestrictFragment extends DialogFragment {
2013 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002014 if (!parent.isAdded()) return;
2015
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002016 final DeniedRestrictFragment dialog = new DeniedRestrictFragment();
2017 dialog.setTargetFragment(parent, 0);
2018 dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT);
2019 }
2020
2021 @Override
2022 public Dialog onCreateDialog(Bundle savedInstanceState) {
2023 final Context context = getActivity();
2024
2025 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2026 builder.setTitle(R.string.data_usage_app_restrict_background);
2027 builder.setMessage(R.string.data_usage_restrict_denied_dialog);
2028 builder.setPositiveButton(android.R.string.ok, null);
2029
2030 return builder.create();
2031 }
2032 }
2033
2034 /**
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002035 * Dialog to request user confirmation before setting
2036 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
2037 */
2038 public static class ConfirmAppRestrictFragment extends DialogFragment {
2039 public static void show(DataUsageSummary parent) {
Jeff Sharkey461842a2011-09-25 18:22:48 -07002040 if (!parent.isAdded()) return;
2041
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07002042 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
2043 dialog.setTargetFragment(parent, 0);
2044 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
2045 }
2046
2047 @Override
2048 public Dialog onCreateDialog(Bundle savedInstanceState) {
2049 final Context context = getActivity();
2050
2051 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002052 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
2053 builder.setMessage(R.string.data_usage_app_restrict_dialog);
2054
2055 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
Jeff Sharkey76c5ed42012-04-09 10:53:09 -07002056 @Override
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002057 public void onClick(DialogInterface dialog, int which) {
2058 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
2059 if (target != null) {
2060 target.setAppRestrictBackground(true);
2061 }
2062 }
2063 });
2064 builder.setNegativeButton(android.R.string.cancel, null);
2065
2066 return builder.create();
2067 }
2068 }
2069
2070 /**
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002071 * Dialog to inform user about changing auto-sync setting
2072 */
2073 public static class ConfirmAutoSyncChangeFragment extends DialogFragment {
Amith Yamasani665235f2012-06-07 19:58:34 -07002074 private static final String SAVE_ENABLING = "enabling";
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002075 private boolean mEnabling;
2076
2077 public static void show(DataUsageSummary parent, boolean enabling) {
2078 if (!parent.isAdded()) return;
2079
2080 final ConfirmAutoSyncChangeFragment dialog = new ConfirmAutoSyncChangeFragment();
2081 dialog.mEnabling = enabling;
2082 dialog.setTargetFragment(parent, 0);
2083 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_AUTO_SYNC_CHANGE);
2084 }
2085
2086 @Override
2087 public Dialog onCreateDialog(Bundle savedInstanceState) {
2088 final Context context = getActivity();
Amith Yamasani665235f2012-06-07 19:58:34 -07002089 if (savedInstanceState != null) {
2090 mEnabling = savedInstanceState.getBoolean(SAVE_ENABLING);
2091 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002092
2093 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
2094 if (!mEnabling) {
2095 builder.setTitle(R.string.data_usage_auto_sync_off_dialog_title);
2096 builder.setMessage(R.string.data_usage_auto_sync_off_dialog);
2097 } else {
2098 builder.setTitle(R.string.data_usage_auto_sync_on_dialog_title);
2099 builder.setMessage(R.string.data_usage_auto_sync_on_dialog);
2100 }
2101
2102 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
2103 @Override
2104 public void onClick(DialogInterface dialog, int which) {
2105 ContentResolver.setMasterSyncAutomatically(mEnabling);
2106 }
2107 });
2108 builder.setNegativeButton(android.R.string.cancel, null);
2109
2110 return builder.create();
2111 }
Amith Yamasani665235f2012-06-07 19:58:34 -07002112
2113 @Override
2114 public void onSaveInstanceState(Bundle outState) {
2115 super.onSaveInstanceState(outState);
2116 outState.putBoolean(SAVE_ENABLING, mEnabling);
2117 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -07002118 }
2119
2120 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002121 * Compute default tab that should be selected, based on
2122 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
2123 */
2124 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07002125 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
2126 if (template == null) return null;
2127
2128 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07002129 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002130 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002131 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002132 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002133 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002134 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07002135 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07002136 return TAB_WIFI;
2137 default:
2138 return null;
2139 }
2140 }
2141
2142 /**
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002143 * Background task that loads {@link UidDetail}, binding to
2144 * {@link DataUsageAdapter} row item when finished.
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002145 */
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002146 private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> {
2147 private final UidDetailProvider mProvider;
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002148 private final AppItem mItem;
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002149 private final View mTarget;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002150
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002151 private UidDetailTask(UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002152 mProvider = checkNotNull(provider);
2153 mItem = checkNotNull(item);
2154 mTarget = checkNotNull(target);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07002155 }
2156
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002157 public static void bindView(
Jeff Sharkeyef6e1ff2012-03-21 17:09:07 -07002158 UidDetailProvider provider, AppItem item, View target) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002159 final UidDetailTask existing = (UidDetailTask) target.getTag();
2160 if (existing != null) {
2161 existing.cancel(false);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002162 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002163
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002164 final UidDetail cachedDetail = provider.getUidDetail(item.key, false);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002165 if (cachedDetail != null) {
2166 bindView(cachedDetail, target);
2167 } else {
2168 target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor(
2169 AsyncTask.THREAD_POOL_EXECUTOR));
2170 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002171 }
2172
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002173 private static void bindView(UidDetail detail, View target) {
2174 final ImageView icon = (ImageView) target.findViewById(android.R.id.icon);
2175 final TextView title = (TextView) target.findViewById(android.R.id.title);
2176
2177 if (detail != null) {
2178 icon.setImageDrawable(detail.icon);
2179 title.setText(detail.label);
2180 } else {
2181 icon.setImageDrawable(null);
2182 title.setText(null);
2183 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002184 }
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002185
2186 @Override
2187 protected void onPreExecute() {
2188 bindView(null, mTarget);
2189 }
2190
2191 @Override
2192 protected UidDetail doInBackground(Void... params) {
Jeff Sharkey38305fb2012-09-14 16:26:51 -07002193 return mProvider.getUidDetail(mItem.key, true);
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002194 }
2195
2196 @Override
2197 protected void onPostExecute(UidDetail result) {
2198 bindView(result, mTarget);
2199 }
Jeff Sharkey8e911d72011-06-14 22:41:21 -07002200 }
2201
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002202 /**
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002203 * Test if device has a mobile data radio with SIM in ready state.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002204 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002205 public static boolean hasReadyMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002206 if (TEST_RADIOS) {
2207 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
2208 }
2209
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002210 final ConnectivityManager conn = ConnectivityManager.from(context);
2211 final TelephonyManager tele = TelephonyManager.from(context);
2212
2213 // require both supported network and ready SIM
2214 return conn.isNetworkSupported(TYPE_MOBILE) && tele.getSimState() == SIM_STATE_READY;
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002215 }
2216
2217 /**
2218 * Test if device has a mobile 4G data radio.
2219 */
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002220 public static boolean hasReadyMobile4gRadio(Context context) {
Jeff Sharkeyb98c55b2011-09-11 17:29:49 -07002221 if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) {
2222 return false;
2223 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002224 if (TEST_RADIOS) {
2225 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
2226 }
2227
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002228 final ConnectivityManager conn = ConnectivityManager.from(context);
2229 final TelephonyManager tele = TelephonyManager.from(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002230
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002231 final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX);
Wink Saville55434042012-06-14 12:33:43 -07002232 final boolean hasLte = (tele.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE)
Jeff Sharkeyad17de32012-04-11 11:03:25 -07002233 && hasReadyMobileRadio(context);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002234 return hasWimax || hasLte;
2235 }
2236
2237 /**
2238 * Test if device has a Wi-Fi data radio.
2239 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002240 public static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002241 if (TEST_RADIOS) {
2242 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
2243 }
2244
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002245 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeybdf98e82011-11-10 17:17:24 -08002246 return conn.isNetworkSupported(TYPE_WIFI);
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002247 }
2248
2249 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002250 * Test if device has an ethernet network connection.
2251 */
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002252 public boolean hasEthernet(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002253 if (TEST_RADIOS) {
2254 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
2255 }
2256
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002257 final ConnectivityManager conn = ConnectivityManager.from(context);
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002258 final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET);
2259
2260 final long ethernetBytes;
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002261 if (mStatsSession != null) {
2262 try {
2263 ethernetBytes = mStatsSession.getSummaryForNetwork(
2264 NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE)
2265 .getTotalBytes();
2266 } catch (RemoteException e) {
2267 throw new RuntimeException(e);
2268 }
2269 } else {
2270 ethernetBytes = 0;
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002271 }
2272
Jeff Sharkeyd8789092012-05-29 10:19:50 -07002273 // only show ethernet when both hardware present and traffic has occurred
Jeff Sharkeyb67c4a82012-05-24 11:01:29 -07002274 return hasEthernet && ethernetBytes > 0;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07002275 }
2276
2277 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002278 * Inflate a {@link Preference} style layout, adding the given {@link View}
2279 * widget into {@link android.R.id#widget_frame}.
2280 */
2281 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
2282 final View view = inflater.inflate(R.layout.preference, root, false);
2283 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
2284 android.R.id.widget_frame);
2285 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
2286 return view;
2287 }
2288
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07002289 private static View inflateAppTitle(
2290 LayoutInflater inflater, ViewGroup root, CharSequence label) {
2291 final TextView view = (TextView) inflater.inflate(
2292 R.layout.data_usage_app_title, root, false);
2293 view.setText(label);
2294 return view;
2295 }
2296
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002297 /**
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002298 * Test if any networks are currently limited.
2299 */
2300 private boolean hasLimitedNetworks() {
2301 return !buildLimitedNetworksList().isEmpty();
2302 }
2303
2304 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002305 * Build string describing currently limited networks, which defines when
2306 * background data is restricted.
2307 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002308 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002309 private CharSequence buildLimitedNetworksString() {
2310 final List<CharSequence> limited = buildLimitedNetworksList();
2311
2312 // handle case where no networks limited
2313 if (limited.isEmpty()) {
2314 limited.add(getText(R.string.data_usage_list_none));
2315 }
2316
2317 return TextUtils.join(limited);
2318 }
2319
2320 /**
2321 * Build list of currently limited networks, which defines when background
2322 * data is restricted.
2323 */
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002324 @Deprecated
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002325 private List<CharSequence> buildLimitedNetworksList() {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002326 final Context context = getActivity();
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002327
2328 // build combined list of all limited networks
2329 final ArrayList<CharSequence> limited = Lists.newArrayList();
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002330
2331 final TelephonyManager tele = TelephonyManager.from(context);
2332 if (tele.getSimState() == SIM_STATE_READY) {
2333 final String subscriberId = getActiveSubscriberId(context);
2334 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
2335 limited.add(getText(R.string.data_usage_list_mobile));
2336 }
2337 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
2338 limited.add(getText(R.string.data_usage_tab_3g));
2339 }
2340 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
2341 limited.add(getText(R.string.data_usage_tab_4g));
2342 }
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002343 }
Jeff Sharkey313f7d82012-04-03 21:13:25 -07002344
2345 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) {
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002346 limited.add(getText(R.string.data_usage_tab_wifi));
2347 }
2348 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
2349 limited.add(getText(R.string.data_usage_tab_ethernet));
2350 }
2351
Jeff Sharkeya53188f2011-09-13 19:56:45 -07002352 return limited;
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002353 }
2354
2355 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07002356 * Inset both selector and divider {@link Drawable} on the given
2357 * {@link ListView} by the requested dimensions.
2358 */
2359 private static void insetListViewDrawables(ListView view, int insetSide) {
2360 final Drawable selector = view.getSelector();
2361 final Drawable divider = view.getDivider();
2362
2363 // fully unregister these drawables so callbacks can be maintained after
2364 // wrapping below.
2365 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
2366 view.setSelector(stub);
2367 view.setDivider(stub);
2368
2369 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
2370 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
2371 }
2372
2373 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002374 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07002375 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07002376 */
2377 private static void setPreferenceTitle(View parent, int resId) {
2378 final TextView title = (TextView) parent.findViewById(android.R.id.title);
2379 title.setText(resId);
2380 }
2381
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002382 /**
2383 * Set {@link android.R.id#summary} for a preference view inflated with
2384 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
2385 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002386 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002387 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
2388 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07002389 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07002390 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07002391}