blob: 417c52543f133d50a34397b22d152ad12533c15e [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;
21import static android.net.ConnectivityManager.TYPE_WIMAX;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070022import static android.net.NetworkPolicy.LIMIT_DISABLED;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070023import static android.net.NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070024import static android.net.NetworkPolicyManager.POLICY_NONE;
25import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND;
Jeff Sharkey8a503642011-06-10 13:31:21 -070026import static android.net.NetworkPolicyManager.computeLastCycleBoundary;
27import static android.net.NetworkPolicyManager.computeNextCycleBoundary;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070028import static android.net.NetworkStats.SET_DEFAULT;
29import static android.net.NetworkStats.SET_FOREGROUND;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070030import static android.net.NetworkStats.TAG_NONE;
31import static android.net.NetworkStatsHistory.FIELD_RX_BYTES;
32import static android.net.NetworkStatsHistory.FIELD_TX_BYTES;
Jeff Sharkeya662e492011-06-18 21:57:06 -070033import static android.net.NetworkTemplate.MATCH_MOBILE_3G_LOWER;
34import static android.net.NetworkTemplate.MATCH_MOBILE_4G;
35import static android.net.NetworkTemplate.MATCH_MOBILE_ALL;
36import static android.net.NetworkTemplate.MATCH_WIFI;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070037import static android.net.NetworkTemplate.buildTemplateEthernet;
38import static android.net.NetworkTemplate.buildTemplateMobile3gLower;
39import static android.net.NetworkTemplate.buildTemplateMobile4g;
40import static android.net.NetworkTemplate.buildTemplateMobileAll;
41import static android.net.NetworkTemplate.buildTemplateWifi;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070042import static android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
43import static android.text.format.DateUtils.FORMAT_SHOW_DATE;
44import static android.text.format.Time.TIMEZONE_UTC;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070045import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -070046import static com.android.settings.Utils.prepareCustomPreferencesList;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070047
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070048import android.animation.LayoutTransition;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070049import android.app.AlertDialog;
50import android.app.Dialog;
51import android.app.DialogFragment;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070052import android.app.Fragment;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070053import android.app.FragmentTransaction;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070054import android.app.LoaderManager.LoaderCallbacks;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070055import android.content.ContentResolver;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070056import android.content.Context;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -070057import android.content.DialogInterface;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -070058import android.content.Intent;
Jeff Sharkey398b18f2011-07-10 18:56:30 -070059import android.content.Loader;
Jeff Sharkey29d56b32011-06-20 17:06:52 -070060import android.content.SharedPreferences;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070061import android.content.pm.ApplicationInfo;
62import android.content.pm.PackageInfo;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070063import android.content.pm.PackageManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070064import android.content.pm.PackageManager.NameNotFoundException;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070065import android.content.res.Resources;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070066import android.graphics.Color;
Jeff Sharkey5d706792011-09-08 18:57:17 -070067import android.graphics.drawable.ColorDrawable;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -070068import android.graphics.drawable.Drawable;
Jeff Sharkey5d706792011-09-08 18:57:17 -070069import android.graphics.drawable.InsetDrawable;
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 Sharkey8a503642011-06-10 13:31:21 -070073import android.net.NetworkPolicy;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -070074import android.net.NetworkPolicyManager;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070075import android.net.NetworkStats;
76import android.net.NetworkStatsHistory;
Jeff Sharkeya662e492011-06-18 21:57:06 -070077import android.net.NetworkTemplate;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -070078import android.net.TrafficStats;
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -070079import android.os.AsyncTask;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070080import android.os.Bundle;
Jeff Sharkey1ae43f92011-08-03 17:16:09 -070081import android.os.INetworkManagementService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070082import android.os.RemoteException;
83import android.os.ServiceManager;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -070084import android.os.SystemProperties;
Jeff Sharkey8a503642011-06-10 13:31:21 -070085import android.preference.Preference;
Jeff Sharkey9fab0da2011-07-09 17:52:31 -070086import android.provider.Settings;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -070087import android.telephony.TelephonyManager;
Jeff Sharkey8e911d72011-06-14 22:41:21 -070088import android.text.TextUtils;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070089import android.text.format.DateUtils;
90import android.text.format.Formatter;
91import android.util.Log;
Jeff Sharkey54d0af52011-08-11 18:26:57 -070092import android.util.SparseArray;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070093import android.view.LayoutInflater;
Jeff Sharkey8a503642011-06-10 13:31:21 -070094import android.view.Menu;
95import android.view.MenuInflater;
96import android.view.MenuItem;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070097import android.view.View;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -070098import android.view.View.OnClickListener;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -070099import android.view.ViewGroup;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700100import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700101import android.widget.AdapterView;
102import android.widget.AdapterView.OnItemClickListener;
103import android.widget.AdapterView.OnItemSelectedListener;
104import android.widget.ArrayAdapter;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700105import android.widget.BaseAdapter;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700106import android.widget.Button;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700107import android.widget.CheckBox;
108import android.widget.CompoundButton;
109import android.widget.CompoundButton.OnCheckedChangeListener;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700110import android.widget.ImageView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700111import android.widget.LinearLayout;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700112import android.widget.ListView;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700113import android.widget.NumberPicker;
Jeff Sharkey2412b0f2011-07-17 20:31:40 -0700114import android.widget.ProgressBar;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700115import android.widget.Spinner;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700116import android.widget.Switch;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700117import android.widget.TabHost;
118import android.widget.TabHost.OnTabChangeListener;
119import android.widget.TabHost.TabContentFactory;
120import android.widget.TabHost.TabSpec;
121import android.widget.TabWidget;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700122import android.widget.TextView;
123
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700124import com.android.internal.telephony.Phone;
Jeff Sharkey5d706792011-09-08 18:57:17 -0700125import com.android.settings.drawable.InsetBoundsDrawable;
126import com.android.settings.drawable.DrawableWrapper;
Jeff Sharkeya662e492011-06-18 21:57:06 -0700127import com.android.settings.net.NetworkPolicyEditor;
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700128import com.android.settings.net.SummaryForAllUidLoader;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700129import com.android.settings.widget.ChartDataUsageView;
130import com.android.settings.widget.ChartDataUsageView.DataUsageChartListener;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700131import com.android.settings.widget.PieChartView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700132import com.google.android.collect.Lists;
133
134import java.util.ArrayList;
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700135import java.util.Arrays;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700136import java.util.Collections;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700137import java.util.Locale;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700138
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700139import libcore.util.Objects;
140
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700141/**
142 * Panel show data usage history across various networks, including options to
143 * inspect based on usage cycle and control through {@link NetworkPolicy}.
144 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700145public class DataUsageSummary extends Fragment {
146 private static final String TAG = "DataUsage";
Jeff Sharkey8a503642011-06-10 13:31:21 -0700147 private static final boolean LOGD = true;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700148
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700149 // TODO: remove this testing code
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700150 private static final boolean TEST_ANIM = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700151 private static final boolean TEST_RADIOS = false;
152 private static final String TEST_RADIOS_PROP = "test.radios";
153
Jeff Sharkey8a503642011-06-10 13:31:21 -0700154 private static final String TAB_3G = "3g";
155 private static final String TAB_4G = "4g";
156 private static final String TAB_MOBILE = "mobile";
157 private static final String TAB_WIFI = "wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700158 private static final String TAB_ETHERNET = "ethernet";
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700159
Jeff Sharkey28130d92011-09-02 16:10:24 -0700160 private static final String TAG_CONFIRM_DATA_DISABLE = "confirmDataDisable";
161 private static final String TAG_CONFIRM_DATA_ROAMING = "confirmDataRoaming";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700162 private static final String TAG_CONFIRM_LIMIT = "confirmLimit";
163 private static final String TAG_CYCLE_EDITOR = "cycleEditor";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700164 private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict";
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700165 private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700166 private static final String TAG_APP_DETAILS = "appDetails";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700167
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700168 private static final int LOADER_SUMMARY = 2;
169
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700170 private static final long KB_IN_BYTES = 1024;
171 private static final long MB_IN_BYTES = KB_IN_BYTES * 1024;
172 private static final long GB_IN_BYTES = MB_IN_BYTES * 1024;
173
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700174 private INetworkManagementService mNetworkService;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700175 private INetworkStatsService mStatsService;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700176 private INetworkPolicyManager mPolicyService;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700177 private ConnectivityManager mConnService;
178
179 private static final String PREF_FILE = "data_usage";
180 private static final String PREF_SHOW_WIFI = "show_wifi";
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700181 private static final String PREF_SHOW_ETHERNET = "show_ethernet";
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700182
183 private SharedPreferences mPrefs;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700184
Jeff Sharkey8a503642011-06-10 13:31:21 -0700185 private TabHost mTabHost;
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700186 private ViewGroup mTabsContainer;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700187 private TabWidget mTabWidget;
188 private ListView mListView;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700189 private DataUsageAdapter mAdapter;
190
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700191 private ViewGroup mHeader;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700192
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700193 private ViewGroup mNetworkSwitchesContainer;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700194 private LinearLayout mNetworkSwitches;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700195 private Switch mDataEnabled;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700196 private View mDataEnabledView;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700197 private CheckBox mDisableAtLimit;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700198 private View mDisableAtLimitView;
199
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700200 private View mCycleView;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700201 private Spinner mCycleSpinner;
202 private CycleAdapter mCycleAdapter;
203
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700204 private ChartDataUsageView mChart;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700205 private TextView mUsageSummary;
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700206 private TextView mEmpty;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700207
208 private View mAppDetail;
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700209 private ImageView mAppIcon;
210 private ViewGroup mAppTitles;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700211 private PieChartView mAppPieChart;
212 private TextView mAppForeground;
213 private TextView mAppBackground;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700214 private Button mAppSettings;
215
216 private LinearLayout mAppSwitches;
217 private CheckBox mAppRestrict;
218 private View mAppRestrictView;
219
Jeff Sharkey8a503642011-06-10 13:31:21 -0700220 private boolean mShowWifi = false;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700221 private boolean mShowEthernet = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700222
Jeff Sharkeya662e492011-06-18 21:57:06 -0700223 private NetworkTemplate mTemplate = null;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700224
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700225 private int[] mAppDetailUids = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700226
227 private Intent mAppSettingsIntent;
228
Jeff Sharkeya662e492011-06-18 21:57:06 -0700229 private NetworkPolicyEditor mPolicyEditor;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700230
Jeff Sharkey8a503642011-06-10 13:31:21 -0700231 private NetworkStatsHistory mHistory;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700232 private NetworkStatsHistory mDetailHistory;
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700233 private NetworkStatsHistory mDetailHistoryDefault;
234 private NetworkStatsHistory mDetailHistoryForeground;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700235
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700236 private String mCurrentTab = null;
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700237 private String mIntentTab = null;
238
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700239 private MenuItem mMenuDataRoaming;
240 private MenuItem mMenuRestrictBackground;
241
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700242 /** Flag used to ignore listeners during binding. */
243 private boolean mBinding;
244
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700245 @Override
Jeff Sharkey8a503642011-06-10 13:31:21 -0700246 public void onCreate(Bundle savedInstanceState) {
247 super.onCreate(savedInstanceState);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700248
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700249 mNetworkService = INetworkManagementService.Stub.asInterface(
250 ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700251 mStatsService = INetworkStatsService.Stub.asInterface(
252 ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700253 mPolicyService = INetworkPolicyManager.Stub.asInterface(
254 ServiceManager.getService(Context.NETWORK_POLICY_SERVICE));
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700255 mConnService = (ConnectivityManager) getActivity().getSystemService(
256 Context.CONNECTIVITY_SERVICE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700257
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700258 mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700259
Jeff Sharkeya662e492011-06-18 21:57:06 -0700260 mPolicyEditor = new NetworkPolicyEditor(mPolicyService);
261 mPolicyEditor.read();
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700262
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700263 mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700264 mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700265
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700266 setHasOptionsMenu(true);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700267 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700268
Jeff Sharkey8a503642011-06-10 13:31:21 -0700269 @Override
270 public View onCreateView(LayoutInflater inflater, ViewGroup container,
271 Bundle savedInstanceState) {
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700272
Jeff Sharkey8a503642011-06-10 13:31:21 -0700273 final Context context = inflater.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700274 final View view = inflater.inflate(R.layout.data_usage_summary, container, false);
275
Jeff Sharkey8a503642011-06-10 13:31:21 -0700276 mTabHost = (TabHost) view.findViewById(android.R.id.tabhost);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700277 mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700278 mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs);
279 mListView = (ListView) view.findViewById(android.R.id.list);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700280
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700281 // adjust padding around tabwidget as needed
Jeff Sharkey5d706792011-09-08 18:57:17 -0700282 prepareCustomPreferencesList(container, view, mListView, true);
283
284 // inset selector and divider drawables
285 final int insetSide = view.getResources().getDimensionPixelOffset(
286 com.android.internal.R.dimen.preference_fragment_padding_side);
287 insetListViewDrawables(mListView, insetSide);
Jeff Sharkeyb654cbb2011-08-18 11:59:19 -0700288
Jeff Sharkey8a503642011-06-10 13:31:21 -0700289 mTabHost.setup();
290 mTabHost.setOnTabChangedListener(mTabListener);
291
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700292 mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700293 mListView.addHeaderView(mHeader, null, false);
294
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700295 {
296 // bind network switches
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700297 mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById(
298 R.id.network_switches_container);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700299 mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700300
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700301 mDataEnabled = new Switch(inflater.getContext());
302 mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled);
303 mDataEnabled.setOnCheckedChangeListener(mDataEnabledListener);
304 mNetworkSwitches.addView(mDataEnabledView);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700305
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700306 mDisableAtLimit = new CheckBox(inflater.getContext());
307 mDisableAtLimit.setClickable(false);
308 mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit);
309 mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener);
310 mNetworkSwitches.addView(mDisableAtLimitView);
311 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700312
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700313 // bind cycle dropdown
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700314 mCycleView = mHeader.findViewById(R.id.cycles);
315 mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700316 mCycleAdapter = new CycleAdapter(context);
317 mCycleSpinner.setAdapter(mCycleAdapter);
318 mCycleSpinner.setOnItemSelectedListener(mCycleListener);
319
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700320 mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700321 mChart.setListener(mChartListener);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700322
323 {
324 // bind app detail controls
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700325 mAppDetail = mHeader.findViewById(R.id.app_detail);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700326 mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon);
327 mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles);
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700328 mAppPieChart = (PieChartView) mAppDetail.findViewById(R.id.app_pie_chart);
329 mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground);
330 mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700331 mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700332
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700333 mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700334 mAppSettings.setOnClickListener(mAppSettingsListener);
335
336 mAppRestrict = new CheckBox(inflater.getContext());
337 mAppRestrict.setClickable(false);
338 mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700339 mAppRestrictView.setOnClickListener(mAppRestrictListener);
340 mAppSwitches.addView(mAppRestrictView);
341 }
342
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700343 mUsageSummary = (TextView) mHeader.findViewById(R.id.usage_summary);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -0700344 mEmpty = (TextView) mHeader.findViewById(android.R.id.empty);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700345
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700346 // only assign layout transitions once first layout is finished
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700347 mListView.getViewTreeObserver().addOnGlobalLayoutListener(mFirstLayoutListener);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700348
349 mAdapter = new DataUsageAdapter();
350 mListView.setOnItemClickListener(mListListener);
351 mListView.setAdapter(mAdapter);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700352
353 return view;
354 }
355
356 @Override
357 public void onResume() {
358 super.onResume();
359
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700360 // pick default tab based on incoming intent
361 final Intent intent = getActivity().getIntent();
362 mIntentTab = computeTabFromIntent(intent);
363
Jeff Sharkey8a503642011-06-10 13:31:21 -0700364 // this kicks off chain reaction which creates tabs, binds the body to
365 // selected network, and binds chart, cycles and detail list.
366 updateTabs();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700367
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700368 // kick off background task to update stats
369 new AsyncTask<Void, Void, Void>() {
370 @Override
371 protected Void doInBackground(Void... params) {
372 try {
373 mStatsService.forceUpdate();
374 } catch (RemoteException e) {
375 }
376 return null;
377 }
378
379 @Override
380 protected void onPostExecute(Void result) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700381 if (isAdded()) {
382 updateBody();
383 }
Jeff Sharkey398b18f2011-07-10 18:56:30 -0700384 }
385 }.execute();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700386 }
387
Jeff Sharkey8a503642011-06-10 13:31:21 -0700388 @Override
389 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
390 inflater.inflate(R.menu.data_usage, menu);
391 }
392
393 @Override
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700394 public void onPrepareOptionsMenu(Menu menu) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700395 final Context context = getActivity();
396
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700397 mMenuDataRoaming = menu.findItem(R.id.data_usage_menu_roaming);
398 mMenuDataRoaming.setVisible(hasMobileRadio(context));
399 mMenuDataRoaming.setChecked(getDataRoaming());
400
401 mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background);
402 mMenuRestrictBackground.setChecked(getRestrictBackground());
403
404 final MenuItem split4g = menu.findItem(R.id.data_usage_menu_split_4g);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700405 split4g.setVisible(hasMobile4gRadio(context));
Jeff Sharkeya662e492011-06-18 21:57:06 -0700406 split4g.setChecked(isMobilePolicySplit());
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700407
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700408 final MenuItem showWifi = menu.findItem(R.id.data_usage_menu_show_wifi);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700409 if (hasWifiRadio(context) && hasMobileRadio(context)) {
410 showWifi.setVisible(true);
411 showWifi.setChecked(mShowWifi);
412 } else {
413 showWifi.setVisible(false);
414 mShowWifi = true;
415 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700416
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700417 final MenuItem showEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet);
418 if (hasEthernet(context) && hasMobileRadio(context)) {
419 showEthernet.setVisible(true);
420 showEthernet.setChecked(mShowEthernet);
421 } else {
422 showEthernet.setVisible(false);
423 mShowEthernet = true;
424 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700425 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700426
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700427 @Override
428 public boolean onOptionsItemSelected(MenuItem item) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700429 switch (item.getItemId()) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700430 case R.id.data_usage_menu_roaming: {
431 final boolean dataRoaming = !item.isChecked();
432 if (dataRoaming) {
433 ConfirmDataRoamingFragment.show(this);
434 } else {
435 // no confirmation to disable roaming
436 setDataRoaming(false);
437 }
438 return true;
439 }
440 case R.id.data_usage_menu_restrict_background: {
441 final boolean restrictBackground = !item.isChecked();
442 if (restrictBackground) {
443 ConfirmRestrictFragment.show(this);
444 } else {
445 // no confirmation to drop restriction
446 setRestrictBackground(false);
447 }
448 return true;
449 }
450 case R.id.data_usage_menu_split_4g: {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700451 final boolean mobileSplit = !item.isChecked();
Jeff Sharkeya662e492011-06-18 21:57:06 -0700452 setMobilePolicySplit(mobileSplit);
453 item.setChecked(isMobilePolicySplit());
Jeff Sharkey8a503642011-06-10 13:31:21 -0700454 updateTabs();
455 return true;
456 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700457 case R.id.data_usage_menu_show_wifi: {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700458 mShowWifi = !item.isChecked();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700459 mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700460 item.setChecked(mShowWifi);
461 updateTabs();
462 return true;
463 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700464 case R.id.data_usage_menu_show_ethernet: {
465 mShowEthernet = !item.isChecked();
466 mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply();
467 item.setChecked(mShowEthernet);
468 updateTabs();
469 return true;
470 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700471 }
472 return false;
473 }
474
Jeff Sharkey94a90952011-06-13 22:31:09 -0700475 @Override
476 public void onDestroyView() {
477 super.onDestroyView();
478
479 mDataEnabledView = null;
480 mDisableAtLimitView = null;
481 }
482
Jeff Sharkey8a503642011-06-10 13:31:21 -0700483 /**
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700484 * Listener to setup {@link LayoutTransition} after first layout pass.
485 */
486 private OnGlobalLayoutListener mFirstLayoutListener = new OnGlobalLayoutListener() {
487 /** {@inheritDoc} */
488 public void onGlobalLayout() {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700489 mListView.getViewTreeObserver().removeGlobalOnLayoutListener(mFirstLayoutListener);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700490
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700491 mTabsContainer.setLayoutTransition(buildLayoutTransition());
492 mHeader.setLayoutTransition(buildLayoutTransition());
493 mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition());
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700494
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700495 final LayoutTransition chartTransition = buildLayoutTransition();
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700496 chartTransition.setStartDelay(LayoutTransition.APPEARING, 0);
497 chartTransition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
Jeff Sharkey28130d92011-09-02 16:10:24 -0700498 chartTransition.setAnimator(LayoutTransition.DISAPPEARING, null);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700499 mChart.setLayoutTransition(chartTransition);
500 }
501 };
502
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700503 private static LayoutTransition buildLayoutTransition() {
504 final LayoutTransition transition = new LayoutTransition();
505 if (TEST_ANIM) {
506 transition.setDuration(1500);
507 }
508 transition.setAnimateParentHierarchy(false);
509 return transition;
510 }
511
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700512 /**
Jeff Sharkeya662e492011-06-18 21:57:06 -0700513 * Rebuild all tabs based on {@link NetworkPolicyEditor} and
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700514 * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects
515 * first tab, and kicks off a full rebind of body contents.
Jeff Sharkey8a503642011-06-10 13:31:21 -0700516 */
517 private void updateTabs() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700518 final Context context = getActivity();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700519 mTabHost.clearAllTabs();
520
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700521 final boolean mobileSplit = isMobilePolicySplit();
522 if (mobileSplit && hasMobile4gRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700523 mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g));
524 mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g));
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700525 } else if (hasMobileRadio(context)) {
526 mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700527 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700528 if (mShowWifi && hasWifiRadio(context)) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700529 mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi));
530 }
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700531 if (mShowEthernet && hasEthernet(context)) {
532 mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet));
533 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700534
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700535 final boolean multipleTabs = mTabWidget.getTabCount() > 1;
536 mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE);
537 if (mIntentTab != null) {
538 if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) {
539 updateBody();
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700540 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700541 mTabHost.setCurrentTabByTag(mIntentTab);
542 }
543 mIntentTab = null;
544 } else {
545 if (mTabHost.getCurrentTab() == 0) {
546 updateBody();
547 } else {
Jeff Sharkeydd6efe12011-06-15 10:31:41 -0700548 mTabHost.setCurrentTab(0);
549 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700550 }
551 }
552
Jeff Sharkey8a503642011-06-10 13:31:21 -0700553 /**
554 * Factory that provide empty {@link View} to make {@link TabHost} happy.
555 */
556 private TabContentFactory mEmptyTabContent = new TabContentFactory() {
557 /** {@inheritDoc} */
558 public View createTabContent(String tag) {
559 return new View(mTabHost.getContext());
560 }
561 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700562
Jeff Sharkey8a503642011-06-10 13:31:21 -0700563 /**
564 * Build {@link TabSpec} with thin indicator, and empty content.
565 */
566 private TabSpec buildTabSpec(String tag, int titleRes) {
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700567 return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent(
568 mEmptyTabContent);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700569 }
570
571 private OnTabChangeListener mTabListener = new OnTabChangeListener() {
572 /** {@inheritDoc} */
573 public void onTabChanged(String tabId) {
574 // user changed tab; update body
575 updateBody();
576 }
577 };
578
579 /**
580 * Update body content based on current tab. Loads
581 * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and
582 * binds them to visible controls.
583 */
584 private void updateBody() {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700585 mBinding = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700586
Jeff Sharkeya662e492011-06-18 21:57:06 -0700587 final Context context = getActivity();
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700588 final String currentTab = mTabHost.getCurrentTabTag();
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700589
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700590 if (currentTab == null) {
591 Log.w(TAG, "no tab selected; hiding body");
592 mListView.setVisibility(View.GONE);
593 return;
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700594 } else {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700595 mListView.setVisibility(View.VISIBLE);
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700596 }
Jeff Sharkeya662e492011-06-18 21:57:06 -0700597
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700598 final boolean tabChanged = !currentTab.equals(mCurrentTab);
599 mCurrentTab = currentTab;
600
Jeff Sharkey8a503642011-06-10 13:31:21 -0700601 if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab);
602
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700603 mDataEnabledView.setVisibility(View.VISIBLE);
604 mDisableAtLimitView.setVisibility(View.VISIBLE);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700605
606 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700607 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile);
608 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit);
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700609 mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700610
611 } else if (TAB_3G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700612 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g);
613 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit);
614 // TODO: bind mDataEnabled to 3G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700615 mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context));
Jeff Sharkey8a503642011-06-10 13:31:21 -0700616
617 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700618 setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g);
619 setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit);
620 // TODO: bind mDataEnabled to 4G radio state
Jeff Sharkey9549e9f2011-07-14 20:01:13 -0700621 mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context));
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700622
623 } else if (TAB_WIFI.equals(currentTab)) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700624 // wifi doesn't have any controls
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700625 mDataEnabledView.setVisibility(View.GONE);
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700626 mDisableAtLimitView.setVisibility(View.GONE);
Jeff Sharkey131f9d62011-08-17 17:08:19 -0700627 mTemplate = buildTemplateWifi();
628
629 } else if (TAB_ETHERNET.equals(currentTab)) {
630 // ethernet doesn't have any controls
631 mDataEnabledView.setVisibility(View.GONE);
632 mDisableAtLimitView.setVisibility(View.GONE);
633 mTemplate = buildTemplateEthernet();
634
635 } else {
636 throw new IllegalStateException("unknown tab: " + currentTab);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700637 }
638
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -0700639 try {
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700640 // load stats for current template
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700641 mHistory = mStatsService.getHistoryForNetwork(
642 mTemplate, FIELD_RX_BYTES | FIELD_TX_BYTES);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700643 } catch (RemoteException e) {
644 // since we can't do much without policy or history, and we don't
645 // want to leave with half-baked UI, we bail hard.
646 throw new RuntimeException("problem reading network policy or stats", e);
647 }
648
Jeff Sharkey8a503642011-06-10 13:31:21 -0700649 // bind chart to historical stats
Jeff Sharkey8a503642011-06-10 13:31:21 -0700650 mChart.bindNetworkStats(mHistory);
651
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700652 // only update policy when switching tabs
653 updatePolicy(tabChanged);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700654 updateAppDetail();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700655
656 // force scroll to top of body
657 mListView.smoothScrollToPosition(0);
658
Jeff Sharkey29d56b32011-06-20 17:06:52 -0700659 mBinding = false;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700660 }
661
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700662 private boolean isAppDetailMode() {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700663 return mAppDetailUids != null;
664 }
665
666 private int getAppDetailPrimaryUid() {
667 return mAppDetailUids[0];
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700668 }
669
670 /**
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700671 * Update UID details panels to match {@link #mAppDetailUids}, showing or
672 * hiding them depending on {@link #isAppDetailMode()}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700673 */
674 private void updateAppDetail() {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700675 final Context context = getActivity();
676 final PackageManager pm = context.getPackageManager();
677 final LayoutInflater inflater = getActivity().getLayoutInflater();
678
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700679 if (isAppDetailMode()) {
680 mAppDetail.setVisibility(View.VISIBLE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700681 mCycleAdapter.setChangeVisible(false);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700682 } else {
683 mAppDetail.setVisibility(View.GONE);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700684 mCycleAdapter.setChangeVisible(true);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700685
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700686 mDetailHistory = null;
687 mDetailHistoryDefault = null;
688 mDetailHistoryForeground = null;
689
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700690 // hide detail stats when not in detail mode
691 mChart.bindDetailNetworkStats(null);
692 return;
693 }
694
695 // remove warning/limit sweeps while in detail mode
696 mChart.bindNetworkPolicy(null);
697
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700698 // show icon and all labels appearing under this app
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700699 final int primaryUid = getAppDetailPrimaryUid();
700 final UidDetail detail = resolveDetailForUid(context, primaryUid);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700701 mAppIcon.setImageDrawable(detail.icon);
702
703 mAppTitles.removeAllViews();
704 if (detail.detailLabels != null) {
705 for (CharSequence label : detail.detailLabels) {
706 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, label));
707 }
708 } else {
709 mAppTitles.addView(inflateAppTitle(inflater, mAppTitles, detail.label));
710 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700711
712 // enable settings button when package provides it
713 // TODO: target torwards entire UID instead of just first package
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700714 final String[] packageNames = pm.getPackagesForUid(primaryUid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700715 if (packageNames != null && packageNames.length > 0) {
716 mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE);
717 mAppSettingsIntent.setPackage(packageNames[0]);
718 mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT);
719
720 final boolean matchFound = pm.resolveActivity(mAppSettingsIntent, 0) != null;
721 mAppSettings.setEnabled(matchFound);
722
723 } else {
724 mAppSettingsIntent = null;
725 mAppSettings.setEnabled(false);
726 }
727
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700728 updateDetailHistory();
729 updateDetailData();
730
731 if (NetworkPolicyManager.isUidValidForPolicy(context, primaryUid)
732 && !getRestrictBackground() && isBandwidthControlEnabled()) {
733 setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background);
734 setPreferenceSummary(mAppRestrictView,
735 getString(R.string.data_usage_app_restrict_background_summary,
736 buildLimitedNetworksList()));
737
738 mAppRestrictView.setVisibility(View.VISIBLE);
739 mAppRestrict.setChecked(getAppRestrictBackground());
740
741 } else {
742 mAppRestrictView.setVisibility(View.GONE);
743 }
744 }
745
746 /**
747 * Update {@link #mDetailHistory} and related values based on
748 * {@link #mAppDetailUids}.
749 */
750 private void updateDetailHistory() {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700751 try {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700752 mDetailHistoryDefault = null;
753 mDetailHistoryForeground = null;
754
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700755 // load stats for current uid and template
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700756 for (int uid : mAppDetailUids) {
757 mDetailHistoryDefault = collectHistoryForUid(
758 uid, SET_DEFAULT, mDetailHistoryDefault);
759 mDetailHistoryForeground = collectHistoryForUid(
760 uid, SET_FOREGROUND, mDetailHistoryForeground);
761 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700762 } catch (RemoteException e) {
763 // since we can't do much without history, and we don't want to
764 // leave with half-baked UI, we bail hard.
765 throw new RuntimeException("problem reading network stats", e);
766 }
767
Jeff Sharkey54d0af52011-08-11 18:26:57 -0700768 mDetailHistory = new NetworkStatsHistory(mDetailHistoryForeground.getBucketDuration());
769 mDetailHistory.recordEntireHistory(mDetailHistoryDefault);
770 mDetailHistory.recordEntireHistory(mDetailHistoryForeground);
771
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700772 // bind chart to historical stats
773 mChart.bindDetailNetworkStats(mDetailHistory);
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700774 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700775
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700776 /**
777 * Collect {@link NetworkStatsHistory} for the requested UID, combining with
778 * an existing {@link NetworkStatsHistory} if provided.
779 */
780 private NetworkStatsHistory collectHistoryForUid(
781 int uid, int set, NetworkStatsHistory existing)
782 throws RemoteException {
783 final NetworkStatsHistory history = mStatsService.getHistoryForUid(
784 mTemplate, uid, set, TAG_NONE, FIELD_RX_BYTES | FIELD_TX_BYTES);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700785
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700786 if (existing != null) {
787 existing.recordEntireHistory(history);
788 return existing;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700789 } else {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700790 return history;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700791 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700792 }
793
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700794 private void setPolicyCycleDay(int cycleDay) {
795 if (LOGD) Log.d(TAG, "setPolicyCycleDay()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700796 mPolicyEditor.setPolicyCycleDay(mTemplate, cycleDay);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700797 updatePolicy(true);
798 }
799
800 private void setPolicyWarningBytes(long warningBytes) {
801 if (LOGD) Log.d(TAG, "setPolicyWarningBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700802 mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700803 updatePolicy(false);
804 }
805
806 private void setPolicyLimitBytes(long limitBytes) {
807 if (LOGD) Log.d(TAG, "setPolicyLimitBytes()");
Jeff Sharkeya662e492011-06-18 21:57:06 -0700808 mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -0700809 updatePolicy(false);
810 }
811
Jeff Sharkey28130d92011-09-02 16:10:24 -0700812 /**
813 * Local cache of value, used to work around delay when
814 * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async.
815 */
816 private Boolean mMobileDataEnabled;
817
818 private boolean isMobileDataEnabled() {
819 if (mMobileDataEnabled != null) {
820 // TODO: deprecate and remove this once enabled flag is on policy
821 return mMobileDataEnabled;
822 } else {
823 return mConnService.getMobileDataEnabled();
824 }
825 }
826
827 private void setMobileDataEnabled(boolean enabled) {
828 if (LOGD) Log.d(TAG, "setMobileDataEnabled()");
829 mConnService.setMobileDataEnabled(enabled);
830 mMobileDataEnabled = enabled;
831 updatePolicy(false);
832 }
833
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700834 private boolean isNetworkPolicyModifiable(NetworkPolicy policy) {
835 return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked();
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700836 }
837
838 private boolean isBandwidthControlEnabled() {
839 try {
840 return mNetworkService.isBandwidthControlEnabled();
841 } catch (RemoteException e) {
842 Log.w(TAG, "problem talking with INetworkManagementService: " + e);
843 return false;
844 }
845 }
846
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700847 private boolean getDataRoaming() {
848 final ContentResolver resolver = getActivity().getContentResolver();
849 return Settings.Secure.getInt(resolver, Settings.Secure.DATA_ROAMING, 0) != 0;
850 }
851
852 private void setDataRoaming(boolean enabled) {
853 // TODO: teach telephony DataConnectionTracker to watch and apply
854 // updates when changed.
855 final ContentResolver resolver = getActivity().getContentResolver();
856 Settings.Secure.putInt(resolver, Settings.Secure.DATA_ROAMING, enabled ? 1 : 0);
857 mMenuDataRoaming.setChecked(enabled);
858 }
859
860 private boolean getRestrictBackground() {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700861 try {
862 return mPolicyService.getRestrictBackground();
863 } catch (RemoteException e) {
864 Log.w(TAG, "problem talking with policy service: " + e);
865 return false;
866 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700867 }
868
869 private void setRestrictBackground(boolean restrictBackground) {
870 if (LOGD) Log.d(TAG, "setRestrictBackground()");
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700871 try {
872 mPolicyService.setRestrictBackground(restrictBackground);
873 mMenuRestrictBackground.setChecked(restrictBackground);
874 } catch (RemoteException e) {
875 Log.w(TAG, "problem talking with policy service: " + e);
876 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700877 }
878
879 private boolean getAppRestrictBackground() {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700880 final int primaryUid = getAppDetailPrimaryUid();
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700881 final int uidPolicy;
882 try {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700883 uidPolicy = mPolicyService.getUidPolicy(primaryUid);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -0700884 } catch (RemoteException e) {
885 // since we can't do much without policy, we bail hard.
886 throw new RuntimeException("problem reading network policy", e);
887 }
888
889 return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0;
890 }
891
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700892 private void setAppRestrictBackground(boolean restrictBackground) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -0700893 if (LOGD) Log.d(TAG, "setAppRestrictBackground()");
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700894 final int primaryUid = getAppDetailPrimaryUid();
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700895 try {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700896 mPolicyService.setUidPolicy(primaryUid,
897 restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700898 } catch (RemoteException e) {
899 throw new RuntimeException("unable to save policy", e);
900 }
901
902 mAppRestrict.setChecked(restrictBackground);
903 }
904
Jeff Sharkey8a503642011-06-10 13:31:21 -0700905 /**
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700906 * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for
907 * current {@link #mTemplate}.
908 */
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700909 private void updatePolicy(boolean refreshCycle) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700910 if (isAppDetailMode()) {
911 mNetworkSwitches.setVisibility(View.GONE);
912 // we fall through to update cycle list for detail mode
913 } else {
914 mNetworkSwitches.setVisibility(View.VISIBLE);
Jeff Sharkey2af35fb2011-06-24 17:30:27 -0700915
916 // when heading back to summary without cycle refresh, kick details
917 // update to repopulate list.
918 if (!refreshCycle) {
919 updateDetailData();
920 }
Jeff Sharkeyf54f4352011-06-23 22:15:54 -0700921 }
922
Jeff Sharkey28130d92011-09-02 16:10:24 -0700923 // TODO: move enabled state directly into policy
924 if (TAB_MOBILE.equals(mCurrentTab)) {
925 mBinding = true;
926 mDataEnabled.setChecked(isMobileDataEnabled());
927 mBinding = false;
928 }
929
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700930 final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate);
931 if (isNetworkPolicyModifiable(policy)) {
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700932 mDisableAtLimitView.setVisibility(View.VISIBLE);
933 mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -0700934 if (!isAppDetailMode()) {
935 mChart.bindNetworkPolicy(policy);
936 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700937
Jeff Sharkey1ae43f92011-08-03 17:16:09 -0700938 } else {
939 // controls are disabled; don't bind warning/limit sweeps
940 mDisableAtLimitView.setVisibility(View.GONE);
941 mChart.bindNetworkPolicy(null);
942 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700943
Jeff Sharkey4dfa6602011-06-13 00:42:03 -0700944 if (refreshCycle) {
945 // generate cycle list based on policy and available history
946 updateCycleList(policy);
947 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700948 }
949
950 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -0700951 * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay}
952 * and available {@link NetworkStatsHistory} data. Always selects the newest
953 * item, updating the inspection range on {@link #mChart}.
954 */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700955 private void updateCycleList(NetworkPolicy policy) {
Jeff Sharkey8a503642011-06-10 13:31:21 -0700956 mCycleAdapter.clear();
957
958 final Context context = mCycleSpinner.getContext();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700959 long historyStart = mHistory.getStart();
960 long historyEnd = mHistory.getEnd();
Jeff Sharkey8a503642011-06-10 13:31:21 -0700961
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700962 if (historyStart == Long.MAX_VALUE || historyEnd == Long.MIN_VALUE) {
963 historyStart = System.currentTimeMillis();
964 historyEnd = System.currentTimeMillis();
965 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700966
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700967 boolean hasCycles = false;
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700968 if (policy != null) {
969 // find the next cycle boundary
970 long cycleEnd = computeNextCycleBoundary(historyEnd, policy);
Jeff Sharkey8a503642011-06-10 13:31:21 -0700971
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700972 int guardCount = 0;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700973
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700974 // walk backwards, generating all valid cycle ranges
975 while (cycleEnd > historyStart) {
976 final long cycleStart = computeLastCycleBoundary(cycleEnd, policy);
977 Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs="
978 + historyStart);
979 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
980 cycleEnd = cycleStart;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700981 hasCycles = true;
Jeff Sharkey8a503642011-06-10 13:31:21 -0700982
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700983 // TODO: remove this guard once we have better testing
984 if (guardCount++ > 50) {
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700985 Log.wtf(TAG, "stuck generating ranges for historyStart=" + historyStart
986 + ", historyEnd=" + historyEnd + " and policy=" + policy);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700987 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700988 }
Jeff Sharkey8a503642011-06-10 13:31:21 -0700989
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700990 // one last cycle entry to modify policy cycle day
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700991 mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy));
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700992 }
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -0700993
Jeff Sharkey518bc9d2011-07-12 20:20:46 -0700994 if (!hasCycles) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -0700995 // no policy defined cycles; show entry for each four-week period
996 long cycleEnd = historyEnd;
997 while (cycleEnd > historyStart) {
998 final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4);
999 mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd));
1000 cycleEnd = cycleStart;
1001 }
1002
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001003 mCycleAdapter.setChangePossible(false);
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001004 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001005
1006 // force pick the current cycle (first item)
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001007 if (mCycleAdapter.getCount() > 0) {
1008 mCycleSpinner.setSelection(0);
1009 mCycleListener.onItemSelected(mCycleSpinner, null, 0, 0);
1010 } else {
1011 updateDetailData();
1012 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001013 }
1014
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001015 private OnCheckedChangeListener mDataEnabledListener = new OnCheckedChangeListener() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001016 /** {@inheritDoc} */
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001017 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
1018 if (mBinding) return;
Jeff Sharkey8a503642011-06-10 13:31:21 -07001019
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001020 final boolean dataEnabled = isChecked;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001021 final String currentTab = mCurrentTab;
1022 if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey28130d92011-09-02 16:10:24 -07001023 if (dataEnabled) {
1024 setMobileDataEnabled(true);
1025 } else {
1026 // disabling data; show confirmation dialog which eventually
1027 // calls setMobileDataEnabled() once user confirms.
1028 ConfirmDataDisableFragment.show(DataUsageSummary.this);
1029 }
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001030 }
Jeff Sharkey1ae43f92011-08-03 17:16:09 -07001031
Jeff Sharkey28130d92011-09-02 16:10:24 -07001032 updatePolicy(false);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001033 }
1034 };
1035
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001036 private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001037 /** {@inheritDoc} */
1038 public void onClick(View v) {
1039 final boolean disableAtLimit = !mDisableAtLimit.isChecked();
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001040 if (disableAtLimit) {
1041 // enabling limit; show confirmation dialog which eventually
1042 // calls setPolicyLimitBytes() once user confirms.
1043 ConfirmLimitFragment.show(DataUsageSummary.this);
1044 } else {
1045 setPolicyLimitBytes(LIMIT_DISABLED);
1046 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001047 }
1048 };
1049
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001050 private View.OnClickListener mAppRestrictListener = new View.OnClickListener() {
1051 /** {@inheritDoc} */
1052 public void onClick(View v) {
1053 final boolean restrictBackground = !mAppRestrict.isChecked();
1054
1055 if (restrictBackground) {
1056 // enabling restriction; show confirmation dialog which
1057 // eventually calls setRestrictBackground() once user confirms.
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001058 ConfirmAppRestrictFragment.show(DataUsageSummary.this);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001059 } else {
1060 setAppRestrictBackground(false);
1061 }
1062 }
1063 };
1064
1065 private OnClickListener mAppSettingsListener = new OnClickListener() {
1066 /** {@inheritDoc} */
1067 public void onClick(View v) {
1068 // TODO: target torwards entire UID instead of just first package
1069 startActivity(mAppSettingsIntent);
1070 }
1071 };
1072
Jeff Sharkey8a503642011-06-10 13:31:21 -07001073 private OnItemClickListener mListListener = new OnItemClickListener() {
1074 /** {@inheritDoc} */
1075 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001076 final Context context = view.getContext();
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001077 final AppUsageItem app = (AppUsageItem) parent.getItemAtPosition(position);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001078 final UidDetail detail = resolveDetailForUid(context, app.uids[0]);
1079 AppDetailsFragment.show(DataUsageSummary.this, app.uids, detail.label);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001080 }
1081 };
1082
1083 private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() {
1084 /** {@inheritDoc} */
1085 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1086 final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position);
1087 if (cycle instanceof CycleChangeItem) {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001088 // show cycle editor; will eventually call setPolicyCycleDay()
1089 // when user finishes editing.
1090 CycleEditorFragment.show(DataUsageSummary.this);
1091
1092 // reset spinner to something other than "change cycle..."
1093 mCycleSpinner.setSelection(0);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001094
1095 } else {
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001096 if (LOGD) {
1097 Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end="
1098 + cycle.end + "]");
1099 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001100
1101 // update chart to show selected cycle, and update detail data
1102 // to match updated sweep bounds.
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001103 mChart.setVisibleRange(cycle.start, cycle.end);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001104
1105 updateDetailData();
1106 }
1107 }
1108
1109 /** {@inheritDoc} */
1110 public void onNothingSelected(AdapterView<?> parent) {
1111 // ignored
1112 }
1113 };
1114
1115 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001116 * Update details based on {@link #mChart} inspection range depending on
1117 * current mode. In network mode, updates {@link #mAdapter} with sorted list
1118 * of applications data usage, and when {@link #isAppDetailMode()} update
1119 * app details.
Jeff Sharkey8a503642011-06-10 13:31:21 -07001120 */
1121 private void updateDetailData() {
1122 if (LOGD) Log.d(TAG, "updateDetailData()");
1123
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001124 final long start = mChart.getInspectStart();
1125 final long end = mChart.getInspectEnd();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001126 final long now = System.currentTimeMillis();
1127
1128 final Context context = getActivity();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001129
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001130 NetworkStatsHistory.Entry entry = null;
1131 if (isAppDetailMode() && mDetailHistory != null) {
1132 // bind foreground/background to piechart and labels
1133 entry = mDetailHistoryDefault.getValues(start, end, now, entry);
1134 final long defaultBytes = entry.rxBytes + entry.txBytes;
1135 entry = mDetailHistoryForeground.getValues(start, end, now, entry);
1136 final long foregroundBytes = entry.rxBytes + entry.txBytes;
1137
1138 mAppPieChart.setOriginAngle(175);
1139
1140 mAppPieChart.removeAllSlices();
1141 mAppPieChart.addSlice(foregroundBytes, Color.parseColor("#d88d3a"));
1142 mAppPieChart.addSlice(defaultBytes, Color.parseColor("#666666"));
1143
1144 mAppPieChart.generatePath();
1145
1146 mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes));
1147 mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes));
1148
1149 // and finally leave with summary data for label below
1150 entry = mDetailHistory.getValues(start, end, now, null);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001151
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001152 getLoaderManager().destroyLoader(LOADER_SUMMARY);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001153
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001154 } else {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001155 entry = mHistory.getValues(start, end, now, null);
1156
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001157 // kick off loader for detailed stats
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001158 // TODO: delay loader until animation is finished
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001159 getLoaderManager().restartLoader(LOADER_SUMMARY,
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001160 SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryForAllUid);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001161 }
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001162
1163 final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0;
1164 final String totalPhrase = Formatter.formatFileSize(context, totalBytes);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001165 final String rangePhrase = formatDateRange(context, start, end, false);
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001166
1167 mUsageSummary.setText(
1168 getString(R.string.data_usage_total_during_range, totalPhrase, rangePhrase));
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001169 }
1170
1171 private final LoaderCallbacks<NetworkStats> mSummaryForAllUid = new LoaderCallbacks<
1172 NetworkStats>() {
1173 /** {@inheritDoc} */
1174 public Loader<NetworkStats> onCreateLoader(int id, Bundle args) {
1175 return new SummaryForAllUidLoader(getActivity(), mStatsService, args);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001176 }
1177
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001178 /** {@inheritDoc} */
1179 public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) {
1180 mAdapter.bindStats(data);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001181 updateEmptyVisible();
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001182 }
Jeff Sharkeyaa5260e2011-06-14 23:21:59 -07001183
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001184 /** {@inheritDoc} */
1185 public void onLoaderReset(Loader<NetworkStats> loader) {
1186 mAdapter.bindStats(null);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001187 updateEmptyVisible();
1188 }
1189
1190 private void updateEmptyVisible() {
1191 final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode();
1192 mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
Jeff Sharkey398b18f2011-07-10 18:56:30 -07001193 }
1194 };
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001195
Jeff Sharkeya662e492011-06-18 21:57:06 -07001196 private boolean isMobilePolicySplit() {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001197 final Context context = getActivity();
1198 if (hasMobileRadio(context)) {
1199 final String subscriberId = getActiveSubscriberId(context);
1200 return mPolicyEditor.isMobilePolicySplit(subscriberId);
1201 } else {
1202 return false;
1203 }
Jeff Sharkeya662e492011-06-18 21:57:06 -07001204 }
1205
1206 private void setMobilePolicySplit(boolean split) {
1207 final String subscriberId = getActiveSubscriberId(getActivity());
1208 mPolicyEditor.setMobilePolicySplit(subscriberId, split);
1209 }
1210
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001211 private static String getActiveSubscriberId(Context context) {
1212 final TelephonyManager telephony = (TelephonyManager) context.getSystemService(
1213 Context.TELEPHONY_SERVICE);
1214 return telephony.getSubscriberId();
1215 }
1216
Jeff Sharkey8a503642011-06-10 13:31:21 -07001217 private DataUsageChartListener mChartListener = new DataUsageChartListener() {
1218 /** {@inheritDoc} */
1219 public void onInspectRangeChanged() {
1220 if (LOGD) Log.d(TAG, "onInspectRangeChanged()");
1221 updateDetailData();
1222 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001223
Jeff Sharkey8a503642011-06-10 13:31:21 -07001224 /** {@inheritDoc} */
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001225 public void onWarningChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001226 setPolicyWarningBytes(mChart.getWarningBytes());
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001227 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001228
Jeff Sharkey05cc0cc2011-06-12 23:11:24 -07001229 /** {@inheritDoc} */
1230 public void onLimitChanged() {
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001231 setPolicyLimitBytes(mChart.getLimitBytes());
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001232 }
1233 };
1234
1235
1236 /**
Jeff Sharkey8a503642011-06-10 13:31:21 -07001237 * List item that reflects a specific data usage cycle.
1238 */
1239 public static class CycleItem {
1240 public CharSequence label;
1241 public long start;
1242 public long end;
1243
Jeff Sharkey8a503642011-06-10 13:31:21 -07001244 CycleItem(CharSequence label) {
1245 this.label = label;
1246 }
1247
1248 public CycleItem(Context context, long start, long end) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001249 this.label = formatDateRange(context, start, end, true);
Jeff Sharkey8a503642011-06-10 13:31:21 -07001250 this.start = start;
1251 this.end = end;
1252 }
1253
Jeff Sharkey8a503642011-06-10 13:31:21 -07001254 @Override
1255 public String toString() {
1256 return label.toString();
1257 }
1258 }
1259
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001260 private static final StringBuilder sBuilder = new StringBuilder(50);
1261 private static final java.util.Formatter sFormatter = new java.util.Formatter(
1262 sBuilder, Locale.getDefault());
1263
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001264 public static String formatDateRange(Context context, long start, long end, boolean utcTime) {
1265 final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH;
1266 final String timezone = utcTime ? TIMEZONE_UTC : null;
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001267
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001268 synchronized (sBuilder) {
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001269 sBuilder.setLength(0);
Jeff Sharkeye2afc0f2011-08-01 15:29:30 -07001270 return DateUtils
1271 .formatDateRange(context, sFormatter, start, end, flags, timezone).toString();
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001272 }
1273 }
1274
Jeff Sharkey8a503642011-06-10 13:31:21 -07001275 /**
1276 * Special-case data usage cycle that triggers dialog to change
1277 * {@link NetworkPolicy#cycleDay}.
1278 */
1279 public static class CycleChangeItem extends CycleItem {
1280 public CycleChangeItem(Context context) {
1281 super(context.getString(R.string.data_usage_change_cycle));
1282 }
1283 }
1284
1285 public static class CycleAdapter extends ArrayAdapter<CycleItem> {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001286 private boolean mChangePossible = false;
1287 private boolean mChangeVisible = false;
1288
1289 private final CycleChangeItem mChangeItem;
1290
Jeff Sharkey8a503642011-06-10 13:31:21 -07001291 public CycleAdapter(Context context) {
1292 super(context, android.R.layout.simple_spinner_item);
1293 setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001294 mChangeItem = new CycleChangeItem(context);
1295 }
1296
1297 public void setChangePossible(boolean possible) {
1298 mChangePossible = possible;
1299 updateChange();
1300 }
1301
1302 public void setChangeVisible(boolean visible) {
1303 mChangeVisible = visible;
1304 updateChange();
1305 }
1306
1307 private void updateChange() {
1308 remove(mChangeItem);
1309 if (mChangePossible && mChangeVisible) {
1310 add(mChangeItem);
1311 }
Jeff Sharkey8a503642011-06-10 13:31:21 -07001312 }
1313 }
1314
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001315 private static class AppUsageItem implements Comparable<AppUsageItem> {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001316 public int[] uids;
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001317 public long total;
1318
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001319 public AppUsageItem(int uid) {
1320 uids = new int[] { uid };
1321 }
1322
1323 public void addUid(int uid) {
1324 if (contains(uids, uid)) return;
1325 final int length = uids.length;
1326 uids = Arrays.copyOf(uids, length + 1);
1327 uids[length] = uid;
1328 }
1329
Jeff Sharkey4dfa6602011-06-13 00:42:03 -07001330 /** {@inheritDoc} */
1331 public int compareTo(AppUsageItem another) {
1332 return Long.compare(another.total, total);
1333 }
1334 }
1335
Jeff Sharkey8a503642011-06-10 13:31:21 -07001336 /**
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001337 * Adapter of applications, sorted by total usage descending.
1338 */
1339 public static class DataUsageAdapter extends BaseAdapter {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001340 private ArrayList<AppUsageItem> mItems = Lists.newArrayList();
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001341 private long mLargest;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001342
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001343 /**
1344 * Bind the given {@link NetworkStats}, or {@code null} to clear list.
1345 */
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001346 public void bindStats(NetworkStats stats) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001347 mItems.clear();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001348
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001349 final AppUsageItem systemItem = new AppUsageItem(android.os.Process.SYSTEM_UID);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001350 final SparseArray<AppUsageItem> knownUids = new SparseArray<AppUsageItem>();
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001351
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001352 NetworkStats.Entry entry = null;
1353 final int size = stats != null ? stats.size() : 0;
1354 for (int i = 0; i < size; i++) {
1355 entry = stats.getValues(i, entry);
1356
1357 final int uid = entry.uid;
1358 final boolean isApp = uid >= android.os.Process.FIRST_APPLICATION_UID
1359 && uid <= android.os.Process.LAST_APPLICATION_UID;
1360 if (isApp || uid == TrafficStats.UID_REMOVED) {
1361 AppUsageItem item = knownUids.get(uid);
1362 if (item == null) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001363 item = new AppUsageItem(uid);
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001364 knownUids.put(uid, item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001365 mItems.add(item);
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001366 }
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001367
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001368 item.total += entry.rxBytes + entry.txBytes;
1369 } else {
1370 systemItem.total += entry.rxBytes + entry.txBytes;
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001371 systemItem.addUid(uid);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001372 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001373 }
1374
Jeff Sharkey54d0af52011-08-11 18:26:57 -07001375 if (systemItem.total > 0) {
1376 mItems.add(systemItem);
1377 }
1378
Jeff Sharkey8a503642011-06-10 13:31:21 -07001379 Collections.sort(mItems);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001380 mLargest = (mItems.size() > 0) ? mItems.get(0).total : 0;
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001381 notifyDataSetChanged();
1382 }
1383
1384 @Override
1385 public int getCount() {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001386 return mItems.size();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001387 }
1388
1389 @Override
1390 public Object getItem(int position) {
Jeff Sharkey8a503642011-06-10 13:31:21 -07001391 return mItems.get(position);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001392 }
1393
1394 @Override
1395 public long getItemId(int position) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001396 return mItems.get(position).uids[0];
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001397 }
1398
1399 @Override
1400 public View getView(int position, View convertView, ViewGroup parent) {
1401 if (convertView == null) {
1402 convertView = LayoutInflater.from(parent.getContext()).inflate(
Jeff Sharkey5d706792011-09-08 18:57:17 -07001403 R.layout.data_usage_item, parent, false);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001404 }
1405
1406 final Context context = parent.getContext();
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001407
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001408 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001409 final TextView title = (TextView) convertView.findViewById(android.R.id.title);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001410 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001411 final ProgressBar progress = (ProgressBar) convertView.findViewById(
1412 android.R.id.progress);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001413
Jeff Sharkey8a503642011-06-10 13:31:21 -07001414 final AppUsageItem item = mItems.get(position);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001415 final UidDetail detail = resolveDetailForUid(context, item.uids[0]);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001416
1417 icon.setImageDrawable(detail.icon);
1418 title.setText(detail.label);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001419 text1.setText(Formatter.formatFileSize(context, item.total));
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001420
1421 final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0;
1422 progress.setProgress(percentTotal);
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001423
1424 return convertView;
1425 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001426 }
1427
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001428 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001429 * Empty {@link Fragment} that controls display of UID details in
1430 * {@link DataUsageSummary}.
1431 */
1432 public static class AppDetailsFragment extends Fragment {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001433 private static final String EXTRA_UIDS = "uids";
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001434
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001435 public static void show(DataUsageSummary parent, int[] uids, CharSequence label) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001436 final Bundle args = new Bundle();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001437 args.putIntArray(EXTRA_UIDS, uids);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001438
1439 final AppDetailsFragment fragment = new AppDetailsFragment();
1440 fragment.setArguments(args);
1441 fragment.setTargetFragment(parent, 0);
1442
1443 final FragmentTransaction ft = parent.getFragmentManager().beginTransaction();
1444 ft.add(fragment, TAG_APP_DETAILS);
1445 ft.addToBackStack(TAG_APP_DETAILS);
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001446 ft.setBreadCrumbTitle(label);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001447 ft.commit();
1448 }
1449
1450 @Override
1451 public void onStart() {
1452 super.onStart();
1453 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001454 target.mAppDetailUids = getArguments().getIntArray(EXTRA_UIDS);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001455 target.updateBody();
1456 }
1457
1458 @Override
1459 public void onStop() {
1460 super.onStop();
1461 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001462 target.mAppDetailUids = null;
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001463 target.updateBody();
1464 }
1465 }
1466
1467 /**
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001468 * Dialog to request user confirmation before setting
1469 * {@link NetworkPolicy#limitBytes}.
1470 */
1471 public static class ConfirmLimitFragment extends DialogFragment {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001472 private static final String EXTRA_MESSAGE = "message";
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001473 private static final String EXTRA_LIMIT_BYTES = "limitBytes";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001474
1475 public static void show(DataUsageSummary parent) {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001476 final Resources res = parent.getResources();
1477
1478 final CharSequence message;
1479 final long limitBytes;
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001480
1481 // TODO: customize default limits based on network template
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001482 final String currentTab = parent.mCurrentTab;
1483 if (TAB_3G.equals(currentTab)) {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001484 message = buildDialogMessage(res, R.string.data_usage_tab_3g);
1485 limitBytes = 5 * GB_IN_BYTES;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001486 } else if (TAB_4G.equals(currentTab)) {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001487 message = buildDialogMessage(res, R.string.data_usage_tab_4g);
1488 limitBytes = 5 * GB_IN_BYTES;
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001489 } else if (TAB_MOBILE.equals(currentTab)) {
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001490 message = buildDialogMessage(res, R.string.data_usage_list_mobile);
1491 limitBytes = 5 * GB_IN_BYTES;
1492 } else if (TAB_WIFI.equals(currentTab)) {
1493 message = buildDialogMessage(res, R.string.data_usage_tab_wifi);
1494 limitBytes = 5 * GB_IN_BYTES;
1495 } else {
1496 throw new IllegalArgumentException("unknown current tab: " + currentTab);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001497 }
1498
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001499 final Bundle args = new Bundle();
1500 args.putCharSequence(EXTRA_MESSAGE, message);
1501 args.putLong(EXTRA_LIMIT_BYTES, limitBytes);
1502
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001503 final ConfirmLimitFragment dialog = new ConfirmLimitFragment();
1504 dialog.setArguments(args);
1505 dialog.setTargetFragment(parent, 0);
1506 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT);
1507 }
1508
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001509 private static CharSequence buildDialogMessage(Resources res, int networkResId) {
1510 return res.getString(R.string.data_usage_limit_dialog, res.getString(networkResId));
1511 }
1512
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001513 @Override
1514 public Dialog onCreateDialog(Bundle savedInstanceState) {
1515 final Context context = getActivity();
1516
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001517 final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001518 final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES);
1519
1520 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1521 builder.setTitle(R.string.data_usage_limit_dialog_title);
Jeff Sharkey131f9d62011-08-17 17:08:19 -07001522 builder.setMessage(message);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001523
1524 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1525 public void onClick(DialogInterface dialog, int which) {
1526 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1527 if (target != null) {
1528 target.setPolicyLimitBytes(limitBytes);
1529 }
1530 }
1531 });
1532
1533 return builder.create();
1534 }
1535 }
1536
1537 /**
1538 * Dialog to edit {@link NetworkPolicy#cycleDay}.
1539 */
1540 public static class CycleEditorFragment extends DialogFragment {
Jeff Sharkey2412b0f2011-07-17 20:31:40 -07001541 private static final String EXTRA_CYCLE_DAY = "cycleDay";
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001542
1543 public static void show(DataUsageSummary parent) {
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001544 final NetworkPolicy policy = parent.mPolicyEditor.getPolicy(parent.mTemplate);
Jeff Sharkey4c72ae52011-06-14 15:01:18 -07001545 final Bundle args = new Bundle();
1546 args.putInt(CycleEditorFragment.EXTRA_CYCLE_DAY, policy.cycleDay);
1547
1548 final CycleEditorFragment dialog = new CycleEditorFragment();
1549 dialog.setArguments(args);
1550 dialog.setTargetFragment(parent, 0);
1551 dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR);
1552 }
1553
1554 @Override
1555 public Dialog onCreateDialog(Bundle savedInstanceState) {
1556 final Context context = getActivity();
1557
1558 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1559 final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
1560
1561 final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false);
1562 final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day);
1563
1564 final int oldCycleDay = getArguments().getInt(EXTRA_CYCLE_DAY, 1);
1565
1566 cycleDayPicker.setMinValue(1);
1567 cycleDayPicker.setMaxValue(31);
1568 cycleDayPicker.setValue(oldCycleDay);
1569 cycleDayPicker.setWrapSelectorWheel(true);
1570
1571 builder.setTitle(R.string.data_usage_cycle_editor_title);
1572 builder.setView(view);
1573
1574 builder.setPositiveButton(R.string.data_usage_cycle_editor_positive,
1575 new DialogInterface.OnClickListener() {
1576 public void onClick(DialogInterface dialog, int which) {
1577 final int cycleDay = cycleDayPicker.getValue();
1578 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1579 if (target != null) {
1580 target.setPolicyCycleDay(cycleDay);
1581 }
1582 }
1583 });
1584
1585 return builder.create();
1586 }
1587 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001588
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001589 /**
Jeff Sharkey28130d92011-09-02 16:10:24 -07001590 * Dialog to request user confirmation before disabling data.
1591 */
1592 public static class ConfirmDataDisableFragment extends DialogFragment {
1593 public static void show(DataUsageSummary parent) {
1594 final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment();
1595 dialog.setTargetFragment(parent, 0);
1596 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE);
1597 }
1598
1599 @Override
1600 public Dialog onCreateDialog(Bundle savedInstanceState) {
1601 final Context context = getActivity();
1602
1603 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1604 builder.setMessage(R.string.data_usage_disable_mobile);
1605
1606 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1607 public void onClick(DialogInterface dialog, int which) {
1608 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1609 if (target != null) {
1610 // TODO: extend to modify policy enabled flag.
1611 target.setMobileDataEnabled(false);
1612 }
1613 }
1614 });
1615 builder.setNegativeButton(android.R.string.cancel, null);
1616
1617 return builder.create();
1618 }
1619 }
1620
1621 /**
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001622 * Dialog to request user confirmation before setting
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001623 * {@link Settings.Secure#DATA_ROAMING}.
1624 */
1625 public static class ConfirmDataRoamingFragment extends DialogFragment {
1626 public static void show(DataUsageSummary parent) {
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001627 final ConfirmDataRoamingFragment dialog = new ConfirmDataRoamingFragment();
1628 dialog.setTargetFragment(parent, 0);
Jeff Sharkey28130d92011-09-02 16:10:24 -07001629 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_ROAMING);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001630 }
1631
1632 @Override
1633 public Dialog onCreateDialog(Bundle savedInstanceState) {
1634 final Context context = getActivity();
1635
1636 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
1637 builder.setTitle(R.string.roaming_reenable_title);
1638 builder.setMessage(R.string.roaming_warning);
1639
1640 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1641 public void onClick(DialogInterface dialog, int which) {
1642 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1643 if (target != null) {
1644 target.setDataRoaming(true);
1645 }
1646 }
1647 });
1648 builder.setNegativeButton(android.R.string.cancel, null);
1649
1650 return builder.create();
1651 }
1652 }
1653
1654 /**
1655 * Dialog to request user confirmation before setting
Jeff Sharkeyd360e5e2011-07-26 19:30:26 -07001656 * {@link INetworkPolicyManager#setRestrictBackground(boolean)}.
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001657 */
1658 public static class ConfirmRestrictFragment extends DialogFragment {
1659 public static void show(DataUsageSummary parent) {
1660 final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment();
1661 dialog.setTargetFragment(parent, 0);
1662 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT);
1663 }
1664
1665 @Override
1666 public Dialog onCreateDialog(Bundle savedInstanceState) {
1667 final Context context = getActivity();
1668
1669 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001670 builder.setTitle(R.string.data_usage_restrict_background_title);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07001671
1672 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1673 if (target != null) {
1674 final CharSequence limitedNetworks = target.buildLimitedNetworksList();
1675 builder.setMessage(
1676 getString(R.string.data_usage_restrict_background, limitedNetworks));
1677 }
Jeff Sharkey9fab0da2011-07-09 17:52:31 -07001678
1679 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1680 public void onClick(DialogInterface dialog, int which) {
1681 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1682 if (target != null) {
1683 target.setRestrictBackground(true);
1684 }
1685 }
1686 });
1687 builder.setNegativeButton(android.R.string.cancel, null);
1688
1689 return builder.create();
1690 }
1691 }
1692
1693 /**
1694 * Dialog to request user confirmation before setting
1695 * {@link #POLICY_REJECT_METERED_BACKGROUND}.
1696 */
1697 public static class ConfirmAppRestrictFragment extends DialogFragment {
1698 public static void show(DataUsageSummary parent) {
1699 final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment();
1700 dialog.setTargetFragment(parent, 0);
1701 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT);
1702 }
1703
1704 @Override
1705 public Dialog onCreateDialog(Bundle savedInstanceState) {
1706 final Context context = getActivity();
1707
1708 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001709 builder.setTitle(R.string.data_usage_app_restrict_dialog_title);
1710 builder.setMessage(R.string.data_usage_app_restrict_dialog);
1711
1712 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
1713 public void onClick(DialogInterface dialog, int which) {
1714 final DataUsageSummary target = (DataUsageSummary) getTargetFragment();
1715 if (target != null) {
1716 target.setAppRestrictBackground(true);
1717 }
1718 }
1719 });
1720 builder.setNegativeButton(android.R.string.cancel, null);
1721
1722 return builder.create();
1723 }
1724 }
1725
1726 /**
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001727 * Compute default tab that should be selected, based on
1728 * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra.
1729 */
1730 private static String computeTabFromIntent(Intent intent) {
Jeff Sharkey271ec8a2011-07-20 16:59:16 -07001731 final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE);
1732 if (template == null) return null;
1733
1734 switch (template.getMatchRule()) {
Jeff Sharkeya662e492011-06-18 21:57:06 -07001735 case MATCH_MOBILE_3G_LOWER:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001736 return TAB_3G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001737 case MATCH_MOBILE_4G:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001738 return TAB_4G;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001739 case MATCH_MOBILE_ALL:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001740 return TAB_MOBILE;
Jeff Sharkeya662e492011-06-18 21:57:06 -07001741 case MATCH_WIFI:
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001742 return TAB_WIFI;
1743 default:
1744 return null;
1745 }
1746 }
1747
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001748 public static class UidDetail {
1749 public CharSequence label;
1750 public CharSequence[] detailLabels;
1751 public Drawable icon;
1752 }
1753
Jeff Sharkeydd6efe12011-06-15 10:31:41 -07001754 /**
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001755 * Resolve best descriptive label for the given UID.
1756 */
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001757 public static UidDetail resolveDetailForUid(Context context, int uid) {
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001758 final Resources res = context.getResources();
1759 final PackageManager pm = context.getPackageManager();
1760
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001761 final UidDetail detail = new UidDetail();
1762 detail.label = pm.getNameForUid(uid);
1763 detail.icon = pm.getDefaultActivityIcon();
1764
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001765 // handle special case labels
1766 switch (uid) {
1767 case android.os.Process.SYSTEM_UID:
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001768 detail.label = res.getString(R.string.process_kernel_label);
1769 detail.icon = pm.getDefaultActivityIcon();
1770 return detail;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001771 case TrafficStats.UID_REMOVED:
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001772 detail.label = res.getString(R.string.data_usage_uninstalled_apps);
1773 detail.icon = pm.getDefaultActivityIcon();
1774 return detail;
Jeff Sharkey518bc9d2011-07-12 20:20:46 -07001775 }
1776
1777 // otherwise fall back to using packagemanager labels
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001778 final String[] packageNames = pm.getPackagesForUid(uid);
1779 final int length = packageNames != null ? packageNames.length : 0;
1780
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001781 try {
1782 if (length == 1) {
1783 final ApplicationInfo info = pm.getApplicationInfo(packageNames[0], 0);
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001784 detail.label = info.loadLabel(pm).toString();
1785 detail.icon = info.loadIcon(pm);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001786 } else if (length > 1) {
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001787 detail.detailLabels = new CharSequence[length];
1788 for (int i = 0; i < length; i++) {
1789 final String packageName = packageNames[i];
1790 final PackageInfo packageInfo = pm.getPackageInfo(packageName, 0);
1791 final ApplicationInfo appInfo = pm.getApplicationInfo(packageName, 0);
1792
1793 detail.detailLabels[i] = appInfo.loadLabel(pm).toString();
1794 if (packageInfo.sharedUserLabel != 0) {
1795 detail.label = pm.getText(packageName, packageInfo.sharedUserLabel,
1796 packageInfo.applicationInfo).toString();
1797 detail.icon = appInfo.loadIcon(pm);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001798 }
1799 }
1800 }
1801 } catch (NameNotFoundException e) {
1802 }
1803
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001804 if (TextUtils.isEmpty(detail.label)) {
1805 detail.label = Integer.toString(uid);
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001806 }
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001807 return detail;
Jeff Sharkey8e911d72011-06-14 22:41:21 -07001808 }
1809
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001810 /**
1811 * Test if device has a mobile data radio.
1812 */
1813 private static boolean hasMobileRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001814 if (TEST_RADIOS) {
1815 return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile");
1816 }
1817
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001818 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
1819 Context.CONNECTIVITY_SERVICE);
1820
1821 // mobile devices should have MOBILE network tracker regardless of
1822 // connection status.
1823 return conn.getNetworkInfo(TYPE_MOBILE) != null;
1824 }
1825
1826 /**
1827 * Test if device has a mobile 4G data radio.
1828 */
1829 private static boolean hasMobile4gRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001830 if (TEST_RADIOS) {
1831 return SystemProperties.get(TEST_RADIOS_PROP).contains("4g");
1832 }
1833
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001834 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
1835 Context.CONNECTIVITY_SERVICE);
1836 final TelephonyManager telephony = (TelephonyManager) context.getSystemService(
1837 Context.TELEPHONY_SERVICE);
1838
1839 // WiMAX devices should have WiMAX network tracker regardless of
1840 // connection status.
1841 final boolean hasWimax = conn.getNetworkInfo(TYPE_WIMAX) != null;
1842 final boolean hasLte = telephony.getLteOnCdmaMode() == Phone.LTE_ON_CDMA_TRUE;
1843 return hasWimax || hasLte;
1844 }
1845
1846 /**
1847 * Test if device has a Wi-Fi data radio.
1848 */
1849 private static boolean hasWifiRadio(Context context) {
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001850 if (TEST_RADIOS) {
1851 return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi");
1852 }
1853
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001854 return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI);
1855 }
1856
1857 /**
Jeff Sharkey9549e9f2011-07-14 20:01:13 -07001858 * Test if device has an ethernet network connection.
1859 */
1860 private static boolean hasEthernet(Context context) {
1861 if (TEST_RADIOS) {
1862 return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet");
1863 }
1864
1865 final ConnectivityManager conn = (ConnectivityManager) context.getSystemService(
1866 Context.CONNECTIVITY_SERVICE);
1867 return conn.getNetworkInfo(TYPE_ETHERNET) != null;
1868 }
1869
1870 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001871 * Inflate a {@link Preference} style layout, adding the given {@link View}
1872 * widget into {@link android.R.id#widget_frame}.
1873 */
1874 private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) {
1875 final View view = inflater.inflate(R.layout.preference, root, false);
1876 final LinearLayout widgetFrame = (LinearLayout) view.findViewById(
1877 android.R.id.widget_frame);
1878 widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
1879 return view;
1880 }
1881
Jeff Sharkeyd39c6e42011-08-04 21:17:23 -07001882 private static View inflateAppTitle(
1883 LayoutInflater inflater, ViewGroup root, CharSequence label) {
1884 final TextView view = (TextView) inflater.inflate(
1885 R.layout.data_usage_app_title, root, false);
1886 view.setText(label);
1887 return view;
1888 }
1889
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001890 /**
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07001891 * Build string describing currently limited networks, which defines when
1892 * background data is restricted.
1893 */
1894 private CharSequence buildLimitedNetworksList() {
1895 final Context context = getActivity();
1896 final String subscriberId = getActiveSubscriberId(context);
1897
1898 // build combined list of all limited networks
1899 final ArrayList<CharSequence> limited = Lists.newArrayList();
1900 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) {
1901 limited.add(getText(R.string.data_usage_list_mobile));
1902 }
1903 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) {
1904 limited.add(getText(R.string.data_usage_tab_3g));
1905 }
1906 if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) {
1907 limited.add(getText(R.string.data_usage_tab_4g));
1908 }
1909 if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifi())) {
1910 limited.add(getText(R.string.data_usage_tab_wifi));
1911 }
1912 if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) {
1913 limited.add(getText(R.string.data_usage_tab_ethernet));
1914 }
1915
1916 // handle case where no networks limited
1917 if (limited.isEmpty()) {
1918 limited.add(getText(R.string.data_usage_list_none));
1919 }
1920
1921 return TextUtils.join(limited);
1922 }
1923
1924 /**
Jeff Sharkey5d706792011-09-08 18:57:17 -07001925 * Inset both selector and divider {@link Drawable} on the given
1926 * {@link ListView} by the requested dimensions.
1927 */
1928 private static void insetListViewDrawables(ListView view, int insetSide) {
1929 final Drawable selector = view.getSelector();
1930 final Drawable divider = view.getDivider();
1931
1932 // fully unregister these drawables so callbacks can be maintained after
1933 // wrapping below.
1934 final Drawable stub = new ColorDrawable(Color.TRANSPARENT);
1935 view.setSelector(stub);
1936 view.setDivider(stub);
1937
1938 view.setSelector(new InsetBoundsDrawable(selector, insetSide));
1939 view.setDivider(new InsetBoundsDrawable(divider, insetSide));
1940 }
1941
1942 /**
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001943 * Set {@link android.R.id#title} for a preference view inflated with
Jeff Sharkey52c3f442011-06-23 00:39:38 -07001944 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
Jeff Sharkey29d56b32011-06-20 17:06:52 -07001945 */
1946 private static void setPreferenceTitle(View parent, int resId) {
1947 final TextView title = (TextView) parent.findViewById(android.R.id.title);
1948 title.setText(resId);
1949 }
1950
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001951 /**
1952 * Set {@link android.R.id#summary} for a preference view inflated with
1953 * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}.
1954 */
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07001955 private static void setPreferenceSummary(View parent, CharSequence string) {
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001956 final TextView summary = (TextView) parent.findViewById(android.R.id.summary);
1957 summary.setVisibility(View.VISIBLE);
Jeff Sharkey30dde0f2011-08-17 15:08:12 -07001958 summary.setText(string);
Jeff Sharkeyf54f4352011-06-23 22:15:54 -07001959 }
Jeff Sharkey55d18a52011-08-27 17:09:43 -07001960
1961 private static boolean contains(int[] haystack, int needle) {
1962 for (int value : haystack) {
1963 if (value == needle) {
1964 return true;
1965 }
1966 }
1967 return false;
1968 }
Jeff Sharkeyab2d8d32011-05-30 16:19:56 -07001969}