Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.settings; |
| 18 | |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 19 | import static android.net.ConnectivityManager.TYPE_ETHERNET; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 20 | import static android.net.ConnectivityManager.TYPE_MOBILE; |
Jeff Sharkey | bdf98e8 | 2011-11-10 17:17:24 -0800 | [diff] [blame] | 21 | import static android.net.ConnectivityManager.TYPE_WIFI; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 22 | import static android.net.ConnectivityManager.TYPE_WIMAX; |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 23 | import static android.net.NetworkPolicy.LIMIT_DISABLED; |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 24 | import static android.net.NetworkPolicy.WARNING_DISABLED; |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 25 | import static android.net.NetworkPolicyManager.EXTRA_NETWORK_TEMPLATE; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 26 | import static android.net.NetworkPolicyManager.POLICY_NONE; |
| 27 | import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 28 | import static android.net.NetworkPolicyManager.computeLastCycleBoundary; |
| 29 | import static android.net.NetworkPolicyManager.computeNextCycleBoundary; |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 30 | import static android.net.NetworkTemplate.MATCH_MOBILE_3G_LOWER; |
| 31 | import static android.net.NetworkTemplate.MATCH_MOBILE_4G; |
| 32 | import static android.net.NetworkTemplate.MATCH_MOBILE_ALL; |
| 33 | import static android.net.NetworkTemplate.MATCH_WIFI; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 34 | import static android.net.NetworkTemplate.buildTemplateEthernet; |
| 35 | import static android.net.NetworkTemplate.buildTemplateMobile3gLower; |
| 36 | import static android.net.NetworkTemplate.buildTemplateMobile4g; |
| 37 | import static android.net.NetworkTemplate.buildTemplateMobileAll; |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 38 | import static android.net.NetworkTemplate.buildTemplateWifiWildcard; |
Jeff Sharkey | 77dae91 | 2012-02-03 14:50:33 -0800 | [diff] [blame] | 39 | import static android.net.TrafficStats.GB_IN_BYTES; |
| 40 | import static android.net.TrafficStats.MB_IN_BYTES; |
Jeff Sharkey | a83a24f | 2011-09-16 01:52:39 -0700 | [diff] [blame] | 41 | import static android.net.TrafficStats.UID_REMOVED; |
| 42 | import static android.net.TrafficStats.UID_TETHERING; |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 43 | import static android.telephony.TelephonyManager.SIM_STATE_READY; |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 44 | import static android.text.format.DateUtils.FORMAT_ABBREV_MONTH; |
| 45 | import static android.text.format.DateUtils.FORMAT_SHOW_DATE; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 46 | import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 47 | import static com.android.internal.util.Preconditions.checkNotNull; |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 48 | import static com.android.settings.Utils.prepareCustomPreferencesList; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 49 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 50 | import android.animation.LayoutTransition; |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 51 | import android.app.ActivityManager; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 52 | import android.app.AlertDialog; |
| 53 | import android.app.Dialog; |
| 54 | import android.app.DialogFragment; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 55 | import android.app.Fragment; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 56 | import android.app.FragmentTransaction; |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 57 | import android.app.LoaderManager.LoaderCallbacks; |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 58 | import android.content.ComponentName; |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 59 | import android.content.ContentResolver; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 60 | import android.content.Context; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 61 | import android.content.DialogInterface; |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 62 | import android.content.Intent; |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 63 | import android.content.Loader; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 64 | import android.content.SharedPreferences; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 65 | import android.content.pm.PackageManager; |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 66 | import android.content.res.Resources; |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 67 | import android.content.res.TypedArray; |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 68 | import android.graphics.Color; |
Jeff Sharkey | 5d70679 | 2011-09-08 18:57:17 -0700 | [diff] [blame] | 69 | import android.graphics.drawable.ColorDrawable; |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 70 | import android.graphics.drawable.Drawable; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 71 | import android.net.ConnectivityManager; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 72 | import android.net.INetworkPolicyManager; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 73 | import android.net.INetworkStatsService; |
Jeff Sharkey | 08ce99e | 2012-04-06 11:21:28 -0700 | [diff] [blame] | 74 | import android.net.INetworkStatsSession; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 75 | import android.net.NetworkPolicy; |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 76 | import android.net.NetworkPolicyManager; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 77 | import android.net.NetworkStats; |
| 78 | import android.net.NetworkStatsHistory; |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 79 | import android.net.NetworkTemplate; |
Jeff Sharkey | 08ce99e | 2012-04-06 11:21:28 -0700 | [diff] [blame] | 80 | import android.net.TrafficStats; |
Jeff Sharkey | aa5260e | 2011-06-14 23:21:59 -0700 | [diff] [blame] | 81 | import android.os.AsyncTask; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 82 | import android.os.Bundle; |
Jeff Sharkey | 1ae43f9 | 2011-08-03 17:16:09 -0700 | [diff] [blame] | 83 | import android.os.INetworkManagementService; |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 84 | import android.os.Parcel; |
| 85 | import android.os.Parcelable; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 86 | import android.os.RemoteException; |
| 87 | import android.os.ServiceManager; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 88 | import android.os.SystemProperties; |
Dianne Hackborn | bb06a42 | 2012-08-16 11:01:57 -0700 | [diff] [blame] | 89 | import android.os.UserHandle; |
Zoltan Szatmary-Ban | 0924f19 | 2014-07-21 16:42:50 +0100 | [diff] [blame^] | 90 | import android.os.UserManager; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 91 | import android.preference.Preference; |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 92 | import android.telephony.TelephonyManager; |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 93 | import android.text.TextUtils; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 94 | import android.text.format.DateUtils; |
| 95 | import android.text.format.Formatter; |
Jeff Sharkey | e5223a0 | 2012-03-09 17:11:14 -0800 | [diff] [blame] | 96 | import android.text.format.Time; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 97 | import android.util.Log; |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 98 | import android.util.SparseArray; |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 99 | import android.util.SparseBooleanArray; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 100 | import android.view.LayoutInflater; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 101 | import android.view.Menu; |
| 102 | import android.view.MenuInflater; |
| 103 | import android.view.MenuItem; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 104 | import android.view.View; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 105 | import android.view.View.OnClickListener; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 106 | import android.view.ViewGroup; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 107 | import android.widget.AdapterView; |
| 108 | import android.widget.AdapterView.OnItemClickListener; |
| 109 | import android.widget.AdapterView.OnItemSelectedListener; |
| 110 | import android.widget.ArrayAdapter; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 111 | import android.widget.BaseAdapter; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 112 | import android.widget.Button; |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 113 | import android.widget.ImageView; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 114 | import android.widget.LinearLayout; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 115 | import android.widget.ListView; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 116 | import android.widget.NumberPicker; |
Jeff Sharkey | 2412b0f | 2011-07-17 20:31:40 -0700 | [diff] [blame] | 117 | import android.widget.ProgressBar; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 118 | import android.widget.Spinner; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 119 | import android.widget.Switch; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 120 | import android.widget.TabHost; |
| 121 | import android.widget.TabHost.OnTabChangeListener; |
| 122 | import android.widget.TabHost.TabContentFactory; |
| 123 | import android.widget.TabHost.TabSpec; |
| 124 | import android.widget.TabWidget; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 125 | import android.widget.TextView; |
| 126 | |
Wink Saville | 5543404 | 2012-06-14 12:33:43 -0700 | [diff] [blame] | 127 | import com.android.internal.telephony.PhoneConstants; |
Jeff Sharkey | 5d70679 | 2011-09-08 18:57:17 -0700 | [diff] [blame] | 128 | import com.android.settings.drawable.InsetBoundsDrawable; |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 129 | import com.android.settings.net.ChartData; |
| 130 | import com.android.settings.net.ChartDataLoader; |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 131 | import com.android.settings.net.DataUsageMeteredSettings; |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 132 | import com.android.settings.net.NetworkPolicyEditor; |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 133 | import com.android.settings.net.SummaryForAllUidLoader; |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 134 | import com.android.settings.net.UidDetail; |
| 135 | import com.android.settings.net.UidDetailProvider; |
Fabrice Di Meglio | 45f754e | 2014-04-10 19:25:42 -0700 | [diff] [blame] | 136 | import com.android.settings.search.BaseSearchIndexProvider; |
Fabrice Di Meglio | 758c3ff | 2014-04-10 13:47:30 -0700 | [diff] [blame] | 137 | import com.android.settings.search.Indexable; |
| 138 | import com.android.settings.search.SearchIndexableRaw; |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 139 | import com.android.settings.widget.ChartDataUsageView; |
| 140 | import com.android.settings.widget.ChartDataUsageView.DataUsageChartListener; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 141 | import com.google.android.collect.Lists; |
| 142 | |
Jeff Sharkey | 78ff1b8 | 2013-09-09 18:42:33 -0700 | [diff] [blame] | 143 | import libcore.util.Objects; |
| 144 | |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 145 | import java.util.ArrayList; |
| 146 | import java.util.Collections; |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 147 | import java.util.List; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 148 | import java.util.Locale; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 149 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 150 | /** |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 151 | * Panel showing data usage history across various networks, including options |
| 152 | * to inspect based on usage cycle and control through {@link NetworkPolicy}. |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 153 | */ |
Fabrice Di Meglio | f2a5226 | 2014-04-17 17:20:27 -0700 | [diff] [blame] | 154 | public class DataUsageSummary extends HighlightingFragment implements Indexable { |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 155 | private static final String TAG = "DataUsage"; |
Jeff Sharkey | bdf98e8 | 2011-11-10 17:17:24 -0800 | [diff] [blame] | 156 | private static final boolean LOGD = false; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 157 | |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 158 | // TODO: remove this testing code |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 159 | private static final boolean TEST_ANIM = false; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 160 | private static final boolean TEST_RADIOS = false; |
Jeff Sharkey | f3871fb | 2012-02-03 19:27:07 -0800 | [diff] [blame] | 161 | |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 162 | private static final String TEST_RADIOS_PROP = "test.radios"; |
Jeff Sharkey | f3871fb | 2012-02-03 19:27:07 -0800 | [diff] [blame] | 163 | private static final String TEST_SUBSCRIBER_PROP = "test.subscriberid"; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 164 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 165 | private static final String TAB_3G = "3g"; |
| 166 | private static final String TAB_4G = "4g"; |
| 167 | private static final String TAB_MOBILE = "mobile"; |
| 168 | private static final String TAB_WIFI = "wifi"; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 169 | private static final String TAB_ETHERNET = "ethernet"; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 170 | |
Jeff Sharkey | 28130d9 | 2011-09-02 16:10:24 -0700 | [diff] [blame] | 171 | private static final String TAG_CONFIRM_DATA_DISABLE = "confirmDataDisable"; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 172 | private static final String TAG_CONFIRM_LIMIT = "confirmLimit"; |
| 173 | private static final String TAG_CYCLE_EDITOR = "cycleEditor"; |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 174 | private static final String TAG_WARNING_EDITOR = "warningEditor"; |
| 175 | private static final String TAG_LIMIT_EDITOR = "limitEditor"; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 176 | private static final String TAG_CONFIRM_RESTRICT = "confirmRestrict"; |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 177 | private static final String TAG_DENIED_RESTRICT = "deniedRestrict"; |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 178 | private static final String TAG_CONFIRM_APP_RESTRICT = "confirmAppRestrict"; |
Amith Yamasani | d1ab828 | 2012-05-18 09:50:08 -0700 | [diff] [blame] | 179 | private static final String TAG_CONFIRM_AUTO_SYNC_CHANGE = "confirmAutoSyncChange"; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 180 | private static final String TAG_APP_DETAILS = "appDetails"; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 181 | |
Fabrice Di Meglio | 2169c88 | 2014-04-18 15:18:40 -0700 | [diff] [blame] | 182 | private static final String DATA_USAGE_ENABLE_MOBILE_KEY = "data_usage_enable_mobile"; |
| 183 | private static final String DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY = |
| 184 | "data_usage_disable_mobile_limit"; |
| 185 | private static final String DATA_USAGE_CYCLE_KEY = "data_usage_cycle"; |
| 186 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 187 | private static final int LOADER_CHART_DATA = 2; |
| 188 | private static final int LOADER_SUMMARY = 3; |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 189 | |
Jeff Sharkey | 1ae43f9 | 2011-08-03 17:16:09 -0700 | [diff] [blame] | 190 | private INetworkManagementService mNetworkService; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 191 | private INetworkStatsService mStatsService; |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 192 | private NetworkPolicyManager mPolicyManager; |
Robert Greenwalt | 0d4c500 | 2014-05-21 20:02:32 -0700 | [diff] [blame] | 193 | private TelephonyManager mTelephonyManager; |
| 194 | |
Jeff Sharkey | 08ce99e | 2012-04-06 11:21:28 -0700 | [diff] [blame] | 195 | private INetworkStatsSession mStatsSession; |
| 196 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 197 | private static final String PREF_FILE = "data_usage"; |
| 198 | private static final String PREF_SHOW_WIFI = "show_wifi"; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 199 | private static final String PREF_SHOW_ETHERNET = "show_ethernet"; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 200 | |
| 201 | private SharedPreferences mPrefs; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 202 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 203 | private TabHost mTabHost; |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 204 | private ViewGroup mTabsContainer; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 205 | private TabWidget mTabWidget; |
| 206 | private ListView mListView; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 207 | private DataUsageAdapter mAdapter; |
| 208 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 209 | /** Distance to inset content from sides, when needed. */ |
| 210 | private int mInsetSide = 0; |
| 211 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 212 | private ViewGroup mHeader; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 213 | |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 214 | private ViewGroup mNetworkSwitchesContainer; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 215 | private LinearLayout mNetworkSwitches; |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 216 | private boolean mDataEnabledSupported; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 217 | private Switch mDataEnabled; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 218 | private View mDataEnabledView; |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 219 | private boolean mDisableAtLimitSupported; |
| 220 | private Switch mDisableAtLimit; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 221 | private View mDisableAtLimitView; |
| 222 | |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 223 | private View mCycleView; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 224 | private Spinner mCycleSpinner; |
| 225 | private CycleAdapter mCycleAdapter; |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 226 | private TextView mCycleSummary; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 227 | |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 228 | private ChartDataUsageView mChart; |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 229 | private View mDisclaimer; |
Jeff Sharkey | e2afc0f | 2011-08-01 15:29:30 -0700 | [diff] [blame] | 230 | private TextView mEmpty; |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 231 | private View mStupidPadding; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 232 | |
| 233 | private View mAppDetail; |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 234 | private ImageView mAppIcon; |
| 235 | private ViewGroup mAppTitles; |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 236 | private TextView mAppTotal; |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 237 | private TextView mAppForeground; |
| 238 | private TextView mAppBackground; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 239 | private Button mAppSettings; |
| 240 | |
| 241 | private LinearLayout mAppSwitches; |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 242 | private Switch mAppRestrict; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 243 | private View mAppRestrictView; |
| 244 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 245 | private boolean mShowWifi = false; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 246 | private boolean mShowEthernet = false; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 247 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 248 | private NetworkTemplate mTemplate; |
| 249 | private ChartData mChartData; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 250 | |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 251 | private AppItem mCurrentApp = null; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 252 | |
| 253 | private Intent mAppSettingsIntent; |
| 254 | |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 255 | private NetworkPolicyEditor mPolicyEditor; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 256 | |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 257 | private String mCurrentTab = null; |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 258 | private String mIntentTab = null; |
| 259 | |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 260 | private MenuItem mMenuRestrictBackground; |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 261 | private MenuItem mMenuShowWifi; |
| 262 | private MenuItem mMenuShowEthernet; |
| 263 | private MenuItem mMenuSimCards; |
| 264 | private MenuItem mMenuCellularNetworks; |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 265 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 266 | /** Flag used to ignore listeners during binding. */ |
| 267 | private boolean mBinding; |
| 268 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 269 | private UidDetailProvider mUidDetailProvider; |
| 270 | |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 271 | @Override |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 272 | public void onCreate(Bundle savedInstanceState) { |
| 273 | super.onCreate(savedInstanceState); |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 274 | final Context context = getActivity(); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 275 | |
Jeff Sharkey | 1ae43f9 | 2011-08-03 17:16:09 -0700 | [diff] [blame] | 276 | mNetworkService = INetworkManagementService.Stub.asInterface( |
| 277 | ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE)); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 278 | mStatsService = INetworkStatsService.Stub.asInterface( |
| 279 | ServiceManager.getService(Context.NETWORK_STATS_SERVICE)); |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 280 | mPolicyManager = NetworkPolicyManager.from(context); |
Robert Greenwalt | 0d4c500 | 2014-05-21 20:02:32 -0700 | [diff] [blame] | 281 | mTelephonyManager = TelephonyManager.from(context); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 282 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 283 | mPrefs = getActivity().getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE); |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 284 | |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 285 | mPolicyEditor = new NetworkPolicyEditor(mPolicyManager); |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 286 | mPolicyEditor.read(); |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 287 | |
Jaewan Kim | ffce9c1 | 2013-03-19 16:53:45 +0900 | [diff] [blame] | 288 | try { |
Jeff Sharkey | 78ff1b8 | 2013-09-09 18:42:33 -0700 | [diff] [blame] | 289 | if (!mNetworkService.isBandwidthControlEnabled()) { |
| 290 | Log.w(TAG, "No bandwidth control; leaving"); |
| 291 | getActivity().finish(); |
| 292 | } |
| 293 | } catch (RemoteException e) { |
| 294 | Log.w(TAG, "No bandwidth control; leaving"); |
| 295 | getActivity().finish(); |
| 296 | } |
| 297 | |
| 298 | try { |
Jaewan Kim | ffce9c1 | 2013-03-19 16:53:45 +0900 | [diff] [blame] | 299 | mStatsSession = mStatsService.openSession(); |
| 300 | } catch (RemoteException e) { |
| 301 | throw new RuntimeException(e); |
| 302 | } |
| 303 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 304 | mShowWifi = mPrefs.getBoolean(PREF_SHOW_WIFI, false); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 305 | mShowEthernet = mPrefs.getBoolean(PREF_SHOW_ETHERNET, false); |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 306 | |
Jeff Sharkey | 0bc5b93 | 2012-05-03 15:02:06 -0700 | [diff] [blame] | 307 | // override preferences when no mobile radio |
| 308 | if (!hasReadyMobileRadio(context)) { |
Jaewan Kim | ffce9c1 | 2013-03-19 16:53:45 +0900 | [diff] [blame] | 309 | mShowWifi = true; |
| 310 | mShowEthernet = true; |
Jeff Sharkey | 0bc5b93 | 2012-05-03 15:02:06 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 313 | setHasOptionsMenu(true); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 314 | } |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 315 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 316 | @Override |
| 317 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 318 | Bundle savedInstanceState) { |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 319 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 320 | final Context context = inflater.getContext(); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 321 | final View view = inflater.inflate(R.layout.data_usage_summary, container, false); |
| 322 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 323 | mUidDetailProvider = new UidDetailProvider(context); |
| 324 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 325 | mTabHost = (TabHost) view.findViewById(android.R.id.tabhost); |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 326 | mTabsContainer = (ViewGroup) view.findViewById(R.id.tabs_container); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 327 | mTabWidget = (TabWidget) view.findViewById(android.R.id.tabs); |
| 328 | mListView = (ListView) view.findViewById(android.R.id.list); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 329 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 330 | // decide if we need to manually inset our content, or if we should rely |
| 331 | // on parent container for inset. |
| 332 | final boolean shouldInset = mListView.getScrollBarStyle() |
| 333 | == View.SCROLLBARS_OUTSIDE_OVERLAY; |
Amith Yamasani | 56f51a8 | 2013-08-05 10:07:23 -0700 | [diff] [blame] | 334 | mInsetSide = 0; |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 335 | |
Jeff Sharkey | b654cbb | 2011-08-18 11:59:19 -0700 | [diff] [blame] | 336 | // adjust padding around tabwidget as needed |
Amith Yamasani | 56f51a8 | 2013-08-05 10:07:23 -0700 | [diff] [blame] | 337 | prepareCustomPreferencesList(container, view, mListView, false); |
Jeff Sharkey | 5d70679 | 2011-09-08 18:57:17 -0700 | [diff] [blame] | 338 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 339 | mTabHost.setup(); |
| 340 | mTabHost.setOnTabChangedListener(mTabListener); |
| 341 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 342 | mHeader = (ViewGroup) inflater.inflate(R.layout.data_usage_header, mListView, false); |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 343 | mHeader.setClickable(true); |
| 344 | |
Jeff Sharkey | 92b518c | 2013-03-25 16:13:00 -0700 | [diff] [blame] | 345 | mListView.addHeaderView(new View(context), null, true); |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 346 | mListView.addHeaderView(mHeader, null, true); |
| 347 | mListView.setItemsCanFocus(true); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 348 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 349 | if (mInsetSide > 0) { |
| 350 | // inset selector and divider drawables |
| 351 | insetListViewDrawables(mListView, mInsetSide); |
Fabrice Di Meglio | b27223f | 2013-01-15 18:54:11 -0800 | [diff] [blame] | 352 | mHeader.setPaddingRelative(mInsetSide, 0, mInsetSide, 0); |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 355 | { |
| 356 | // bind network switches |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 357 | mNetworkSwitchesContainer = (ViewGroup) mHeader.findViewById( |
| 358 | R.id.network_switches_container); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 359 | mNetworkSwitches = (LinearLayout) mHeader.findViewById(R.id.network_switches); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 360 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 361 | mDataEnabled = new Switch(inflater.getContext()); |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 362 | mDataEnabled.setClickable(false); |
| 363 | mDataEnabled.setFocusable(false); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 364 | mDataEnabledView = inflatePreference(inflater, mNetworkSwitches, mDataEnabled); |
Fabrice Di Meglio | c70b7f6 | 2014-06-19 11:29:26 -0700 | [diff] [blame] | 365 | mDataEnabledView.setTag(R.id.preference_highlight_key, |
| 366 | DATA_USAGE_ENABLE_MOBILE_KEY); |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 367 | mDataEnabledView.setClickable(true); |
| 368 | mDataEnabledView.setFocusable(true); |
| 369 | mDataEnabledView.setOnClickListener(mDataEnabledListener); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 370 | mNetworkSwitches.addView(mDataEnabledView); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 371 | |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 372 | mDisableAtLimit = new Switch(inflater.getContext()); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 373 | mDisableAtLimit.setClickable(false); |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 374 | mDisableAtLimit.setFocusable(false); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 375 | mDisableAtLimitView = inflatePreference(inflater, mNetworkSwitches, mDisableAtLimit); |
Fabrice Di Meglio | c70b7f6 | 2014-06-19 11:29:26 -0700 | [diff] [blame] | 376 | mDisableAtLimitView.setTag(R.id.preference_highlight_key, |
| 377 | DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY); |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 378 | mDisableAtLimitView.setClickable(true); |
| 379 | mDisableAtLimitView.setFocusable(true); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 380 | mDisableAtLimitView.setOnClickListener(mDisableAtLimitListener); |
| 381 | mNetworkSwitches.addView(mDisableAtLimitView); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 382 | |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 383 | mCycleView = inflater.inflate(R.layout.data_usage_cycles, mNetworkSwitches, false); |
| 384 | mCycleView.setTag(R.id.preference_highlight_key, DATA_USAGE_CYCLE_KEY); |
| 385 | mCycleSpinner = (Spinner) mCycleView.findViewById(R.id.cycles_spinner); |
| 386 | mCycleAdapter = new CycleAdapter(context); |
| 387 | mCycleSpinner.setAdapter(mCycleAdapter); |
| 388 | mCycleSpinner.setOnItemSelectedListener(mCycleListener); |
| 389 | mCycleSummary = (TextView) mCycleView.findViewById(R.id.cycle_summary); |
| 390 | mNetworkSwitches.addView(mCycleView); |
| 391 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 392 | |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 393 | mChart = (ChartDataUsageView) mHeader.findViewById(R.id.chart); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 394 | mChart.setListener(mChartListener); |
Jeff Sharkey | bdf98e8 | 2011-11-10 17:17:24 -0800 | [diff] [blame] | 395 | mChart.bindNetworkPolicy(null); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 396 | |
| 397 | { |
| 398 | // bind app detail controls |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 399 | mAppDetail = mHeader.findViewById(R.id.app_detail); |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 400 | mAppIcon = (ImageView) mAppDetail.findViewById(R.id.app_icon); |
| 401 | mAppTitles = (ViewGroup) mAppDetail.findViewById(R.id.app_titles); |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 402 | mAppForeground = (TextView) mAppDetail.findViewById(R.id.app_foreground); |
| 403 | mAppBackground = (TextView) mAppDetail.findViewById(R.id.app_background); |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 404 | mAppSwitches = (LinearLayout) mAppDetail.findViewById(R.id.app_switches); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 405 | |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 406 | mAppSettings = (Button) mAppDetail.findViewById(R.id.app_settings); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 407 | mAppSettings.setOnClickListener(mAppSettingsListener); |
| 408 | |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 409 | mAppRestrict = new Switch(inflater.getContext()); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 410 | mAppRestrict.setClickable(false); |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 411 | mAppRestrict.setFocusable(false); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 412 | mAppRestrictView = inflatePreference(inflater, mAppSwitches, mAppRestrict); |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 413 | mAppRestrictView.setClickable(true); |
| 414 | mAppRestrictView.setFocusable(true); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 415 | mAppRestrictView.setOnClickListener(mAppRestrictListener); |
| 416 | mAppSwitches.addView(mAppRestrictView); |
| 417 | } |
| 418 | |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 419 | mDisclaimer = mHeader.findViewById(R.id.disclaimer); |
Jeff Sharkey | e2afc0f | 2011-08-01 15:29:30 -0700 | [diff] [blame] | 420 | mEmpty = (TextView) mHeader.findViewById(android.R.id.empty); |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 421 | mStupidPadding = mHeader.findViewById(R.id.stupid_padding); |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 422 | |
Zoltan Szatmary-Ban | 0924f19 | 2014-07-21 16:42:50 +0100 | [diff] [blame^] | 423 | final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE); |
| 424 | mAdapter = new DataUsageAdapter(um, mUidDetailProvider, mInsetSide); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 425 | mListView.setOnItemClickListener(mListListener); |
| 426 | mListView.setAdapter(mAdapter); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 427 | |
| 428 | return view; |
| 429 | } |
| 430 | |
| 431 | @Override |
Fabrice Di Meglio | f2a5226 | 2014-04-17 17:20:27 -0700 | [diff] [blame] | 432 | public void onViewStateRestored(Bundle savedInstanceState) { |
| 433 | super.onViewStateRestored(savedInstanceState); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 434 | |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 435 | // pick default tab based on incoming intent |
| 436 | final Intent intent = getActivity().getIntent(); |
| 437 | mIntentTab = computeTabFromIntent(intent); |
| 438 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 439 | // this kicks off chain reaction which creates tabs, binds the body to |
| 440 | // selected network, and binds chart, cycles and detail list. |
| 441 | updateTabs(); |
Fabrice Di Meglio | f2a5226 | 2014-04-17 17:20:27 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | @Override |
| 445 | public void onResume() { |
| 446 | super.onResume(); |
| 447 | |
| 448 | getView().post(new Runnable() { |
| 449 | @Override |
| 450 | public void run() { |
| 451 | highlightViewIfNeeded(); |
| 452 | } |
| 453 | }); |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 454 | |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 455 | // kick off background task to update stats |
| 456 | new AsyncTask<Void, Void, Void>() { |
| 457 | @Override |
| 458 | protected Void doInBackground(Void... params) { |
| 459 | try { |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 460 | // wait a few seconds before kicking off |
| 461 | Thread.sleep(2 * DateUtils.SECOND_IN_MILLIS); |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 462 | mStatsService.forceUpdate(); |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 463 | } catch (InterruptedException e) { |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 464 | } catch (RemoteException e) { |
| 465 | } |
| 466 | return null; |
| 467 | } |
| 468 | |
| 469 | @Override |
| 470 | protected void onPostExecute(Void result) { |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 471 | if (isAdded()) { |
| 472 | updateBody(); |
| 473 | } |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 474 | } |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 475 | }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 478 | @Override |
| 479 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
| 480 | inflater.inflate(R.menu.data_usage, menu); |
| 481 | } |
| 482 | |
| 483 | @Override |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 484 | public void onPrepareOptionsMenu(Menu menu) { |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 485 | final Context context = getActivity(); |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 486 | final boolean appDetailMode = isAppDetailMode(); |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 487 | final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 488 | |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 489 | mMenuShowWifi = menu.findItem(R.id.data_usage_menu_show_wifi); |
| 490 | // TODO: Define behavior of this sync button. See: http://b/16076571 |
| 491 | if (hasWifiRadio(context) && hasReadyMobileRadio(context)) { |
| 492 | mMenuShowWifi.setVisible(!appDetailMode); |
| 493 | } else { |
| 494 | mMenuShowWifi.setVisible(false); |
| 495 | } |
| 496 | |
| 497 | mMenuShowEthernet = menu.findItem(R.id.data_usage_menu_show_ethernet); |
| 498 | if (hasEthernet(context) && hasReadyMobileRadio(context)) { |
| 499 | mMenuShowEthernet.setVisible(!appDetailMode); |
| 500 | } else { |
| 501 | mMenuShowEthernet.setVisible(false); |
| 502 | } |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 503 | |
| 504 | mMenuRestrictBackground = menu.findItem(R.id.data_usage_menu_restrict_background); |
Jeff Sharkey | 92b518c | 2013-03-25 16:13:00 -0700 | [diff] [blame] | 505 | mMenuRestrictBackground.setVisible( |
| 506 | hasReadyMobileRadio(context) && isOwner && !appDetailMode); |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 507 | |
| 508 | final MenuItem metered = menu.findItem(R.id.data_usage_menu_metered); |
| 509 | if (hasReadyMobileRadio(context) || hasWifiRadio(context)) { |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame] | 510 | metered.setVisible(!appDetailMode); |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 511 | } else { |
| 512 | metered.setVisible(false); |
| 513 | } |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 514 | |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 515 | // TODO: show when multiple sims available |
| 516 | mMenuSimCards = menu.findItem(R.id.data_usage_menu_sim_cards); |
| 517 | mMenuSimCards.setVisible(false); |
| 518 | |
| 519 | mMenuCellularNetworks = menu.findItem(R.id.data_usage_menu_cellular_networks); |
| 520 | if (hasReadyMobileRadio(context)) { |
| 521 | mMenuCellularNetworks.setVisible(!appDetailMode); |
| 522 | } else { |
| 523 | mMenuCellularNetworks.setVisible(false); |
| 524 | } |
| 525 | |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 526 | final MenuItem help = menu.findItem(R.id.data_usage_menu_help); |
| 527 | String helpUrl; |
| 528 | if (!TextUtils.isEmpty(helpUrl = getResources().getString(R.string.help_url_data_usage))) { |
Amith Yamasani | aeb57ed | 2012-12-06 14:40:51 -0800 | [diff] [blame] | 529 | HelpUtils.prepareHelpMenuItem(context, help, helpUrl); |
Amith Yamasani | b0b37ae | 2012-04-23 15:35:36 -0700 | [diff] [blame] | 530 | } else { |
| 531 | help.setVisible(false); |
| 532 | } |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 533 | |
| 534 | updateMenuTitles(); |
| 535 | } |
| 536 | |
| 537 | private void updateMenuTitles() { |
| 538 | if (mPolicyManager.getRestrictBackground()) { |
| 539 | mMenuRestrictBackground.setTitle(R.string.data_usage_menu_allow_background); |
| 540 | } else { |
| 541 | mMenuRestrictBackground.setTitle(R.string.data_usage_menu_restrict_background); |
| 542 | } |
| 543 | |
| 544 | if (mShowWifi) { |
| 545 | mMenuShowWifi.setTitle(R.string.data_usage_menu_hide_wifi); |
| 546 | } else { |
| 547 | mMenuShowWifi.setTitle(R.string.data_usage_menu_show_wifi); |
| 548 | } |
| 549 | |
| 550 | if (mShowEthernet) { |
| 551 | mMenuShowEthernet.setTitle(R.string.data_usage_menu_hide_ethernet); |
| 552 | } else { |
| 553 | mMenuShowEthernet.setTitle(R.string.data_usage_menu_show_ethernet); |
| 554 | } |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 555 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 556 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 557 | @Override |
| 558 | public boolean onOptionsItemSelected(MenuItem item) { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 559 | switch (item.getItemId()) { |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 560 | case R.id.data_usage_menu_restrict_background: { |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 561 | final boolean restrictBackground = !mPolicyManager.getRestrictBackground(); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 562 | if (restrictBackground) { |
Jeff Sharkey | 3038c52 | 2011-11-30 15:37:51 -0800 | [diff] [blame] | 563 | ConfirmRestrictFragment.show(this); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 564 | } else { |
| 565 | // no confirmation to drop restriction |
| 566 | setRestrictBackground(false); |
| 567 | } |
| 568 | return true; |
| 569 | } |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 570 | case R.id.data_usage_menu_show_wifi: { |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 571 | mShowWifi = !mShowWifi; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 572 | mPrefs.edit().putBoolean(PREF_SHOW_WIFI, mShowWifi).apply(); |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 573 | updateMenuTitles(); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 574 | updateTabs(); |
| 575 | return true; |
| 576 | } |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 577 | case R.id.data_usage_menu_show_ethernet: { |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 578 | mShowEthernet = !mShowEthernet; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 579 | mPrefs.edit().putBoolean(PREF_SHOW_ETHERNET, mShowEthernet).apply(); |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 580 | updateMenuTitles(); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 581 | updateTabs(); |
| 582 | return true; |
| 583 | } |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 584 | case R.id.data_usage_menu_sim_cards: { |
| 585 | // TODO: hook up to sim cards |
| 586 | return true; |
| 587 | } |
| 588 | case R.id.data_usage_menu_cellular_networks: { |
| 589 | final Intent intent = new Intent(Intent.ACTION_MAIN); |
| 590 | intent.setComponent(new ComponentName("com.android.phone", |
| 591 | "com.android.phone.MobileNetworkSettings")); |
| 592 | startActivity(intent); |
| 593 | return true; |
| 594 | } |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 595 | case R.id.data_usage_menu_metered: { |
Fabrice Di Meglio | 263bcc8 | 2014-01-17 19:17:58 -0800 | [diff] [blame] | 596 | final SettingsActivity sa = (SettingsActivity) getActivity(); |
| 597 | sa.startPreferencePanel(DataUsageMeteredSettings.class.getCanonicalName(), null, |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 598 | R.string.data_usage_metered_title, null, this, 0); |
| 599 | return true; |
| 600 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 601 | } |
| 602 | return false; |
| 603 | } |
| 604 | |
Jeff Sharkey | 94a9095 | 2011-06-13 22:31:09 -0700 | [diff] [blame] | 605 | @Override |
Jeff Sharkey | 02b327e | 2012-05-15 11:33:59 -0700 | [diff] [blame] | 606 | public void onDestroy() { |
Jeff Sharkey | 94a9095 | 2011-06-13 22:31:09 -0700 | [diff] [blame] | 607 | mDataEnabledView = null; |
| 608 | mDisableAtLimitView = null; |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 609 | |
| 610 | mUidDetailProvider.clearCache(); |
| 611 | mUidDetailProvider = null; |
Jeff Sharkey | 08ce99e | 2012-04-06 11:21:28 -0700 | [diff] [blame] | 612 | |
| 613 | TrafficStats.closeQuietly(mStatsSession); |
Jeff Sharkey | 94a9095 | 2011-06-13 22:31:09 -0700 | [diff] [blame] | 614 | |
Amith Yamasani | 5ba0a02 | 2011-11-07 12:29:00 -0800 | [diff] [blame] | 615 | super.onDestroy(); |
| 616 | } |
| 617 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 618 | /** |
Jeff Sharkey | 9281182 | 2012-05-04 11:47:29 -0700 | [diff] [blame] | 619 | * Build and assign {@link LayoutTransition} to various containers. Should |
| 620 | * only be assigned after initial layout is complete. |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 621 | */ |
Jeff Sharkey | 9281182 | 2012-05-04 11:47:29 -0700 | [diff] [blame] | 622 | private void ensureLayoutTransitions() { |
| 623 | // skip when already setup |
| 624 | if (mChart.getLayoutTransition() != null) return; |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 625 | |
Jeff Sharkey | 9281182 | 2012-05-04 11:47:29 -0700 | [diff] [blame] | 626 | mTabsContainer.setLayoutTransition(buildLayoutTransition()); |
| 627 | mHeader.setLayoutTransition(buildLayoutTransition()); |
| 628 | mNetworkSwitchesContainer.setLayoutTransition(buildLayoutTransition()); |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 629 | |
Jeff Sharkey | 9281182 | 2012-05-04 11:47:29 -0700 | [diff] [blame] | 630 | final LayoutTransition chartTransition = buildLayoutTransition(); |
| 631 | chartTransition.disableTransitionType(LayoutTransition.APPEARING); |
| 632 | chartTransition.disableTransitionType(LayoutTransition.DISAPPEARING); |
| 633 | mChart.setLayoutTransition(chartTransition); |
| 634 | } |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 635 | |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 636 | private static LayoutTransition buildLayoutTransition() { |
| 637 | final LayoutTransition transition = new LayoutTransition(); |
| 638 | if (TEST_ANIM) { |
| 639 | transition.setDuration(1500); |
| 640 | } |
| 641 | transition.setAnimateParentHierarchy(false); |
| 642 | return transition; |
| 643 | } |
| 644 | |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 645 | /** |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 646 | * Rebuild all tabs based on {@link NetworkPolicyEditor} and |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 647 | * {@link #mShowWifi}, hiding the tabs entirely when applicable. Selects |
| 648 | * first tab, and kicks off a full rebind of body contents. |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 649 | */ |
| 650 | private void updateTabs() { |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 651 | final Context context = getActivity(); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 652 | mTabHost.clearAllTabs(); |
| 653 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 654 | final boolean mobileSplit = isMobilePolicySplit(); |
Jeff Sharkey | ad17de3 | 2012-04-11 11:03:25 -0700 | [diff] [blame] | 655 | if (mobileSplit && hasReadyMobile4gRadio(context)) { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 656 | mTabHost.addTab(buildTabSpec(TAB_3G, R.string.data_usage_tab_3g)); |
| 657 | mTabHost.addTab(buildTabSpec(TAB_4G, R.string.data_usage_tab_4g)); |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 658 | } else if (hasReadyMobileRadio(context)) { |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 659 | mTabHost.addTab(buildTabSpec(TAB_MOBILE, R.string.data_usage_tab_mobile)); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 660 | } |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 661 | if (mShowWifi && hasWifiRadio(context)) { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 662 | mTabHost.addTab(buildTabSpec(TAB_WIFI, R.string.data_usage_tab_wifi)); |
| 663 | } |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 664 | if (mShowEthernet && hasEthernet(context)) { |
| 665 | mTabHost.addTab(buildTabSpec(TAB_ETHERNET, R.string.data_usage_tab_ethernet)); |
| 666 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 667 | |
Jeff Sharkey | ad17de3 | 2012-04-11 11:03:25 -0700 | [diff] [blame] | 668 | final boolean noTabs = mTabWidget.getTabCount() == 0; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 669 | final boolean multipleTabs = mTabWidget.getTabCount() > 1; |
| 670 | mTabWidget.setVisibility(multipleTabs ? View.VISIBLE : View.GONE); |
| 671 | if (mIntentTab != null) { |
| 672 | if (Objects.equal(mIntentTab, mTabHost.getCurrentTabTag())) { |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 673 | // already hit updateBody() when added; ignore |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 674 | updateBody(); |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 675 | } else { |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 676 | mTabHost.setCurrentTabByTag(mIntentTab); |
| 677 | } |
| 678 | mIntentTab = null; |
Jeff Sharkey | ad17de3 | 2012-04-11 11:03:25 -0700 | [diff] [blame] | 679 | } else if (noTabs) { |
| 680 | // no usable tabs, so hide body |
| 681 | updateBody(); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 682 | } else { |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 683 | // already hit updateBody() when added; ignore |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 687 | /** |
| 688 | * Factory that provide empty {@link View} to make {@link TabHost} happy. |
| 689 | */ |
| 690 | private TabContentFactory mEmptyTabContent = new TabContentFactory() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 691 | @Override |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 692 | public View createTabContent(String tag) { |
| 693 | return new View(mTabHost.getContext()); |
| 694 | } |
| 695 | }; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 696 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 697 | /** |
| 698 | * Build {@link TabSpec} with thin indicator, and empty content. |
| 699 | */ |
| 700 | private TabSpec buildTabSpec(String tag, int titleRes) { |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 701 | return mTabHost.newTabSpec(tag).setIndicator(getText(titleRes)).setContent( |
| 702 | mEmptyTabContent); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | private OnTabChangeListener mTabListener = new OnTabChangeListener() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 706 | @Override |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 707 | public void onTabChanged(String tabId) { |
| 708 | // user changed tab; update body |
| 709 | updateBody(); |
| 710 | } |
| 711 | }; |
| 712 | |
| 713 | /** |
| 714 | * Update body content based on current tab. Loads |
| 715 | * {@link NetworkStatsHistory} and {@link NetworkPolicy} from system, and |
| 716 | * binds them to visible controls. |
| 717 | */ |
| 718 | private void updateBody() { |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 719 | mBinding = true; |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 720 | if (!isAdded()) return; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 721 | |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 722 | final Context context = getActivity(); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 723 | final String currentTab = mTabHost.getCurrentTabTag(); |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 724 | final boolean isOwner = ActivityManager.getCurrentUser() == UserHandle.USER_OWNER; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 725 | |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 726 | if (currentTab == null) { |
| 727 | Log.w(TAG, "no tab selected; hiding body"); |
| 728 | mListView.setVisibility(View.GONE); |
| 729 | return; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 730 | } else { |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 731 | mListView.setVisibility(View.VISIBLE); |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 732 | } |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 733 | |
Jeff Sharkey | 2af35fb | 2011-06-24 17:30:27 -0700 | [diff] [blame] | 734 | mCurrentTab = currentTab; |
| 735 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 736 | if (LOGD) Log.d(TAG, "updateBody() with currentTab=" + currentTab); |
| 737 | |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 738 | mDataEnabledSupported = isOwner; |
| 739 | mDisableAtLimitSupported = true; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 740 | |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 741 | // TODO: remove mobile tabs when SIM isn't ready |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 742 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 743 | if (TAB_MOBILE.equals(currentTab)) { |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 744 | setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_mobile); |
| 745 | setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_mobile_limit); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 746 | mTemplate = buildTemplateMobileAll(getActiveSubscriberId(context)); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 747 | |
| 748 | } else if (TAB_3G.equals(currentTab)) { |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 749 | setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_3g); |
| 750 | setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_3g_limit); |
| 751 | // TODO: bind mDataEnabled to 3G radio state |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 752 | mTemplate = buildTemplateMobile3gLower(getActiveSubscriberId(context)); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 753 | |
| 754 | } else if (TAB_4G.equals(currentTab)) { |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 755 | setPreferenceTitle(mDataEnabledView, R.string.data_usage_enable_4g); |
| 756 | setPreferenceTitle(mDisableAtLimitView, R.string.data_usage_disable_4g_limit); |
| 757 | // TODO: bind mDataEnabled to 4G radio state |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 758 | mTemplate = buildTemplateMobile4g(getActiveSubscriberId(context)); |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 759 | |
| 760 | } else if (TAB_WIFI.equals(currentTab)) { |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 761 | // wifi doesn't have any controls |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 762 | mDataEnabledSupported = false; |
| 763 | mDisableAtLimitSupported = false; |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 764 | mTemplate = buildTemplateWifiWildcard(); |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 765 | |
| 766 | } else if (TAB_ETHERNET.equals(currentTab)) { |
| 767 | // ethernet doesn't have any controls |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 768 | mDataEnabledSupported = false; |
| 769 | mDisableAtLimitSupported = false; |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 770 | mTemplate = buildTemplateEthernet(); |
| 771 | |
| 772 | } else { |
| 773 | throw new IllegalStateException("unknown tab: " + currentTab); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 776 | // kick off loader for network history |
| 777 | // TODO: consider chaining two loaders together instead of reloading |
| 778 | // network history when showing app detail. |
| 779 | getLoaderManager().restartLoader(LOADER_CHART_DATA, |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 780 | ChartDataLoader.buildArgs(mTemplate, mCurrentApp), mChartDataCallbacks); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 781 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 782 | // detail mode can change visible menus, invalidate |
| 783 | getActivity().invalidateOptionsMenu(); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 784 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 785 | mBinding = false; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 786 | } |
| 787 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 788 | private boolean isAppDetailMode() { |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 789 | return mCurrentApp != null; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | /** |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 793 | * Update UID details panels to match {@link #mCurrentApp}, showing or |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 794 | * hiding them depending on {@link #isAppDetailMode()}. |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 795 | */ |
| 796 | private void updateAppDetail() { |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 797 | final Context context = getActivity(); |
| 798 | final PackageManager pm = context.getPackageManager(); |
| 799 | final LayoutInflater inflater = getActivity().getLayoutInflater(); |
| 800 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 801 | if (isAppDetailMode()) { |
| 802 | mAppDetail.setVisibility(View.VISIBLE); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 803 | mCycleAdapter.setChangeVisible(false); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 804 | } else { |
| 805 | mAppDetail.setVisibility(View.GONE); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 806 | mCycleAdapter.setChangeVisible(true); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 807 | |
| 808 | // hide detail stats when not in detail mode |
| 809 | mChart.bindDetailNetworkStats(null); |
| 810 | return; |
| 811 | } |
| 812 | |
| 813 | // remove warning/limit sweeps while in detail mode |
| 814 | mChart.bindNetworkPolicy(null); |
| 815 | |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 816 | // show icon and all labels appearing under this app |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 817 | final int uid = mCurrentApp.key; |
| 818 | final UidDetail detail = mUidDetailProvider.getUidDetail(uid, true); |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 819 | mAppIcon.setImageDrawable(detail.icon); |
| 820 | |
| 821 | mAppTitles.removeAllViews(); |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 822 | |
| 823 | View title = null; |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 824 | if (detail.detailLabels != null) { |
| 825 | for (CharSequence label : detail.detailLabels) { |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 826 | title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false); |
| 827 | ((TextView) title.findViewById(R.id.app_title)).setText(label); |
| 828 | mAppTitles.addView(title); |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 829 | } |
| 830 | } else { |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 831 | title = inflater.inflate(R.layout.data_usage_app_title, mAppTitles, false); |
| 832 | ((TextView) title.findViewById(R.id.app_title)).setText(detail.label); |
| 833 | mAppTitles.addView(title); |
| 834 | } |
| 835 | |
| 836 | // Remember last slot for summary |
| 837 | if (title != null) { |
| 838 | mAppTotal = (TextView) title.findViewById(R.id.app_summary); |
| 839 | } else { |
| 840 | mAppTotal = null; |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 841 | } |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 842 | |
| 843 | // enable settings button when package provides it |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 844 | final String[] packageNames = pm.getPackagesForUid(uid); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 845 | if (packageNames != null && packageNames.length > 0) { |
| 846 | mAppSettingsIntent = new Intent(Intent.ACTION_MANAGE_NETWORK_USAGE); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 847 | mAppSettingsIntent.addCategory(Intent.CATEGORY_DEFAULT); |
| 848 | |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 849 | // Search for match across all packages |
| 850 | boolean matchFound = false; |
| 851 | for (String packageName : packageNames) { |
| 852 | mAppSettingsIntent.setPackage(packageName); |
| 853 | if (pm.resolveActivity(mAppSettingsIntent, 0) != null) { |
| 854 | matchFound = true; |
| 855 | break; |
| 856 | } |
| 857 | } |
| 858 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 859 | mAppSettings.setEnabled(matchFound); |
Jeff Sharkey | d92e041 | 2012-04-24 11:35:43 -0700 | [diff] [blame] | 860 | mAppSettings.setVisibility(View.VISIBLE); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 861 | |
| 862 | } else { |
| 863 | mAppSettingsIntent = null; |
Jeff Sharkey | 34e964d | 2012-04-21 15:41:48 -0700 | [diff] [blame] | 864 | mAppSettings.setVisibility(View.GONE); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 865 | } |
| 866 | |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 867 | updateDetailData(); |
| 868 | |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 869 | if (UserHandle.isApp(uid) && !mPolicyManager.getRestrictBackground() |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 870 | && isBandwidthControlEnabled() && hasReadyMobileRadio(context)) { |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 871 | setPreferenceTitle(mAppRestrictView, R.string.data_usage_app_restrict_background); |
Jeff Sharkey | 3038c52 | 2011-11-30 15:37:51 -0800 | [diff] [blame] | 872 | setPreferenceSummary(mAppRestrictView, |
| 873 | getString(R.string.data_usage_app_restrict_background_summary)); |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 874 | |
| 875 | mAppRestrictView.setVisibility(View.VISIBLE); |
| 876 | mAppRestrict.setChecked(getAppRestrictBackground()); |
| 877 | |
| 878 | } else { |
| 879 | mAppRestrictView.setVisibility(View.GONE); |
| 880 | } |
| 881 | } |
| 882 | |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 883 | private void setPolicyWarningBytes(long warningBytes) { |
| 884 | if (LOGD) Log.d(TAG, "setPolicyWarningBytes()"); |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 885 | mPolicyEditor.setPolicyWarningBytes(mTemplate, warningBytes); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 886 | updatePolicy(false); |
| 887 | } |
| 888 | |
| 889 | private void setPolicyLimitBytes(long limitBytes) { |
| 890 | if (LOGD) Log.d(TAG, "setPolicyLimitBytes()"); |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 891 | mPolicyEditor.setPolicyLimitBytes(mTemplate, limitBytes); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 892 | updatePolicy(false); |
| 893 | } |
| 894 | |
Jeff Sharkey | 28130d9 | 2011-09-02 16:10:24 -0700 | [diff] [blame] | 895 | /** |
| 896 | * Local cache of value, used to work around delay when |
| 897 | * {@link ConnectivityManager#setMobileDataEnabled(boolean)} is async. |
| 898 | */ |
| 899 | private Boolean mMobileDataEnabled; |
| 900 | |
| 901 | private boolean isMobileDataEnabled() { |
| 902 | if (mMobileDataEnabled != null) { |
| 903 | // TODO: deprecate and remove this once enabled flag is on policy |
| 904 | return mMobileDataEnabled; |
| 905 | } else { |
Robert Greenwalt | 0d4c500 | 2014-05-21 20:02:32 -0700 | [diff] [blame] | 906 | return mTelephonyManager.getDataEnabled(); |
Jeff Sharkey | 28130d9 | 2011-09-02 16:10:24 -0700 | [diff] [blame] | 907 | } |
| 908 | } |
| 909 | |
| 910 | private void setMobileDataEnabled(boolean enabled) { |
| 911 | if (LOGD) Log.d(TAG, "setMobileDataEnabled()"); |
Robert Greenwalt | 0d4c500 | 2014-05-21 20:02:32 -0700 | [diff] [blame] | 912 | mTelephonyManager.setDataEnabled(enabled); |
Jeff Sharkey | 28130d9 | 2011-09-02 16:10:24 -0700 | [diff] [blame] | 913 | mMobileDataEnabled = enabled; |
| 914 | updatePolicy(false); |
| 915 | } |
| 916 | |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 917 | private boolean isNetworkPolicyModifiable(NetworkPolicy policy) { |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 918 | return policy != null && isBandwidthControlEnabled() && mDataEnabled.isChecked() |
| 919 | && ActivityManager.getCurrentUser() == UserHandle.USER_OWNER; |
Jeff Sharkey | 1ae43f9 | 2011-08-03 17:16:09 -0700 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | private boolean isBandwidthControlEnabled() { |
| 923 | try { |
| 924 | return mNetworkService.isBandwidthControlEnabled(); |
| 925 | } catch (RemoteException e) { |
| 926 | Log.w(TAG, "problem talking with INetworkManagementService: " + e); |
| 927 | return false; |
| 928 | } |
| 929 | } |
| 930 | |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 931 | public void setRestrictBackground(boolean restrictBackground) { |
| 932 | mPolicyManager.setRestrictBackground(restrictBackground); |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 933 | updateMenuTitles(); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | private boolean getAppRestrictBackground() { |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 937 | final int uid = mCurrentApp.key; |
| 938 | final int uidPolicy = mPolicyManager.getUidPolicy(uid); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 939 | return (uidPolicy & POLICY_REJECT_METERED_BACKGROUND) != 0; |
| 940 | } |
| 941 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 942 | private void setAppRestrictBackground(boolean restrictBackground) { |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 943 | if (LOGD) Log.d(TAG, "setAppRestrictBackground()"); |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 944 | final int uid = mCurrentApp.key; |
| 945 | mPolicyManager.setUidPolicy( |
| 946 | uid, restrictBackground ? POLICY_REJECT_METERED_BACKGROUND : POLICY_NONE); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 947 | mAppRestrict.setChecked(restrictBackground); |
| 948 | } |
| 949 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 950 | /** |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 951 | * Update chart sweeps and cycle list to reflect {@link NetworkPolicy} for |
| 952 | * current {@link #mTemplate}. |
| 953 | */ |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 954 | private void updatePolicy(boolean refreshCycle) { |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 955 | boolean dataEnabledVisible = mDataEnabledSupported; |
| 956 | boolean disableAtLimitVisible = mDisableAtLimitSupported; |
| 957 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 958 | if (isAppDetailMode()) { |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 959 | dataEnabledVisible = false; |
| 960 | disableAtLimitVisible = false; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 961 | } |
| 962 | |
Jeff Sharkey | 28130d9 | 2011-09-02 16:10:24 -0700 | [diff] [blame] | 963 | // TODO: move enabled state directly into policy |
| 964 | if (TAB_MOBILE.equals(mCurrentTab)) { |
| 965 | mBinding = true; |
| 966 | mDataEnabled.setChecked(isMobileDataEnabled()); |
| 967 | mBinding = false; |
| 968 | } |
| 969 | |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 970 | final NetworkPolicy policy = mPolicyEditor.getPolicy(mTemplate); |
| 971 | if (isNetworkPolicyModifiable(policy)) { |
Jeff Sharkey | 1ae43f9 | 2011-08-03 17:16:09 -0700 | [diff] [blame] | 972 | mDisableAtLimit.setChecked(policy != null && policy.limitBytes != LIMIT_DISABLED); |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 973 | if (!isAppDetailMode()) { |
| 974 | mChart.bindNetworkPolicy(policy); |
| 975 | } |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 976 | |
Jeff Sharkey | 1ae43f9 | 2011-08-03 17:16:09 -0700 | [diff] [blame] | 977 | } else { |
| 978 | // controls are disabled; don't bind warning/limit sweeps |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 979 | disableAtLimitVisible = false; |
Jeff Sharkey | 1ae43f9 | 2011-08-03 17:16:09 -0700 | [diff] [blame] | 980 | mChart.bindNetworkPolicy(null); |
| 981 | } |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 982 | |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 983 | mDataEnabledView.setVisibility(dataEnabledVisible ? View.VISIBLE : View.GONE); |
| 984 | mDisableAtLimitView.setVisibility(disableAtLimitVisible ? View.VISIBLE : View.GONE); |
| 985 | |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 986 | if (refreshCycle) { |
| 987 | // generate cycle list based on policy and available history |
| 988 | updateCycleList(policy); |
| 989 | } |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | /** |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 993 | * Rebuild {@link #mCycleAdapter} based on {@link NetworkPolicy#cycleDay} |
| 994 | * and available {@link NetworkStatsHistory} data. Always selects the newest |
| 995 | * item, updating the inspection range on {@link #mChart}. |
| 996 | */ |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 997 | private void updateCycleList(NetworkPolicy policy) { |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 998 | // stash away currently selected cycle to try restoring below |
| 999 | final CycleItem previousItem = (CycleItem) mCycleSpinner.getSelectedItem(); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1000 | mCycleAdapter.clear(); |
| 1001 | |
| 1002 | final Context context = mCycleSpinner.getContext(); |
| 1003 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1004 | long historyStart = Long.MAX_VALUE; |
| 1005 | long historyEnd = Long.MIN_VALUE; |
| 1006 | if (mChartData != null) { |
| 1007 | historyStart = mChartData.network.getStart(); |
| 1008 | historyEnd = mChartData.network.getEnd(); |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 1009 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1010 | |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 1011 | final long now = System.currentTimeMillis(); |
| 1012 | if (historyStart == Long.MAX_VALUE) historyStart = now; |
| 1013 | if (historyEnd == Long.MIN_VALUE) historyEnd = now + 1; |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1014 | |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 1015 | boolean hasCycles = false; |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 1016 | if (policy != null) { |
| 1017 | // find the next cycle boundary |
| 1018 | long cycleEnd = computeNextCycleBoundary(historyEnd, policy); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1019 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 1020 | // walk backwards, generating all valid cycle ranges |
| 1021 | while (cycleEnd > historyStart) { |
| 1022 | final long cycleStart = computeLastCycleBoundary(cycleEnd, policy); |
| 1023 | Log.d(TAG, "generating cs=" + cycleStart + " to ce=" + cycleEnd + " waiting for hs=" |
| 1024 | + historyStart); |
| 1025 | mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd)); |
| 1026 | cycleEnd = cycleStart; |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 1027 | hasCycles = true; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1028 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1029 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 1030 | // one last cycle entry to modify policy cycle day |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 1031 | mCycleAdapter.setChangePossible(isNetworkPolicyModifiable(policy)); |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 1032 | } |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 1033 | |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 1034 | if (!hasCycles) { |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 1035 | // no policy defined cycles; show entry for each four-week period |
| 1036 | long cycleEnd = historyEnd; |
| 1037 | while (cycleEnd > historyStart) { |
| 1038 | final long cycleStart = cycleEnd - (DateUtils.WEEK_IN_MILLIS * 4); |
| 1039 | mCycleAdapter.add(new CycleItem(context, cycleStart, cycleEnd)); |
| 1040 | cycleEnd = cycleStart; |
| 1041 | } |
| 1042 | |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 1043 | mCycleAdapter.setChangePossible(false); |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 1044 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1045 | |
| 1046 | // force pick the current cycle (first item) |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 1047 | if (mCycleAdapter.getCount() > 0) { |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1048 | final int position = mCycleAdapter.findNearestPosition(previousItem); |
| 1049 | mCycleSpinner.setSelection(position); |
| 1050 | |
| 1051 | // only force-update cycle when changed; skipping preserves any |
| 1052 | // user-defined inspection region. |
| 1053 | final CycleItem selectedItem = mCycleAdapter.getItem(position); |
| 1054 | if (!Objects.equal(selectedItem, previousItem)) { |
| 1055 | mCycleListener.onItemSelected(mCycleSpinner, null, position, 0); |
| 1056 | } else { |
| 1057 | // but still kick off loader for detailed list |
| 1058 | updateDetailData(); |
| 1059 | } |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 1060 | } else { |
| 1061 | updateDetailData(); |
| 1062 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 1065 | private View.OnClickListener mDataEnabledListener = new View.OnClickListener() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1066 | @Override |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 1067 | public void onClick(View v) { |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 1068 | if (mBinding) return; |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1069 | |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 1070 | final boolean dataEnabled = !mDataEnabled.isChecked(); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 1071 | final String currentTab = mCurrentTab; |
| 1072 | if (TAB_MOBILE.equals(currentTab)) { |
Jeff Sharkey | 28130d9 | 2011-09-02 16:10:24 -0700 | [diff] [blame] | 1073 | if (dataEnabled) { |
| 1074 | setMobileDataEnabled(true); |
| 1075 | } else { |
| 1076 | // disabling data; show confirmation dialog which eventually |
| 1077 | // calls setMobileDataEnabled() once user confirms. |
| 1078 | ConfirmDataDisableFragment.show(DataUsageSummary.this); |
| 1079 | } |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 1080 | } |
Jeff Sharkey | 1ae43f9 | 2011-08-03 17:16:09 -0700 | [diff] [blame] | 1081 | |
Jeff Sharkey | 28130d9 | 2011-09-02 16:10:24 -0700 | [diff] [blame] | 1082 | updatePolicy(false); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1083 | } |
| 1084 | }; |
| 1085 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 1086 | private View.OnClickListener mDisableAtLimitListener = new View.OnClickListener() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1087 | @Override |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1088 | public void onClick(View v) { |
| 1089 | final boolean disableAtLimit = !mDisableAtLimit.isChecked(); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1090 | if (disableAtLimit) { |
| 1091 | // enabling limit; show confirmation dialog which eventually |
| 1092 | // calls setPolicyLimitBytes() once user confirms. |
| 1093 | ConfirmLimitFragment.show(DataUsageSummary.this); |
| 1094 | } else { |
| 1095 | setPolicyLimitBytes(LIMIT_DISABLED); |
| 1096 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1097 | } |
| 1098 | }; |
| 1099 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1100 | private View.OnClickListener mAppRestrictListener = new View.OnClickListener() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1101 | @Override |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1102 | public void onClick(View v) { |
| 1103 | final boolean restrictBackground = !mAppRestrict.isChecked(); |
| 1104 | |
| 1105 | if (restrictBackground) { |
Jeff Sharkey | 3038c52 | 2011-11-30 15:37:51 -0800 | [diff] [blame] | 1106 | // enabling restriction; show confirmation dialog which |
| 1107 | // eventually calls setRestrictBackground() once user |
| 1108 | // confirms. |
| 1109 | ConfirmAppRestrictFragment.show(DataUsageSummary.this); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1110 | } else { |
| 1111 | setAppRestrictBackground(false); |
| 1112 | } |
| 1113 | } |
| 1114 | }; |
| 1115 | |
| 1116 | private OnClickListener mAppSettingsListener = new OnClickListener() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1117 | @Override |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1118 | public void onClick(View v) { |
Jeff Sharkey | d92e041 | 2012-04-24 11:35:43 -0700 | [diff] [blame] | 1119 | if (!isAdded()) return; |
| 1120 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1121 | // TODO: target torwards entire UID instead of just first package |
| 1122 | startActivity(mAppSettingsIntent); |
| 1123 | } |
| 1124 | }; |
| 1125 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1126 | private OnItemClickListener mListListener = new OnItemClickListener() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1127 | @Override |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1128 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 1129 | final Context context = view.getContext(); |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1130 | final AppItem app = (AppItem) parent.getItemAtPosition(position); |
Jeff Sharkey | 3da415f | 2012-05-18 14:00:10 -0700 | [diff] [blame] | 1131 | |
| 1132 | // TODO: sigh, remove this hack once we understand 6450986 |
| 1133 | if (mUidDetailProvider == null || app == null) return; |
| 1134 | |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 1135 | final UidDetail detail = mUidDetailProvider.getUidDetail(app.key, true); |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1136 | AppDetailsFragment.show(DataUsageSummary.this, app, detail.label); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1137 | } |
| 1138 | }; |
| 1139 | |
| 1140 | private OnItemSelectedListener mCycleListener = new OnItemSelectedListener() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1141 | @Override |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1142 | public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { |
| 1143 | final CycleItem cycle = (CycleItem) parent.getItemAtPosition(position); |
| 1144 | if (cycle instanceof CycleChangeItem) { |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1145 | // show cycle editor; will eventually call setPolicyCycleDay() |
| 1146 | // when user finishes editing. |
| 1147 | CycleEditorFragment.show(DataUsageSummary.this); |
| 1148 | |
| 1149 | // reset spinner to something other than "change cycle..." |
| 1150 | mCycleSpinner.setSelection(0); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1151 | |
| 1152 | } else { |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 1153 | if (LOGD) { |
| 1154 | Log.d(TAG, "showing cycle " + cycle + ", start=" + cycle.start + ", end=" |
| 1155 | + cycle.end + "]"); |
| 1156 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1157 | |
| 1158 | // update chart to show selected cycle, and update detail data |
| 1159 | // to match updated sweep bounds. |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1160 | mChart.setVisibleRange(cycle.start, cycle.end); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1161 | |
| 1162 | updateDetailData(); |
| 1163 | } |
| 1164 | } |
| 1165 | |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1166 | @Override |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1167 | public void onNothingSelected(AdapterView<?> parent) { |
| 1168 | // ignored |
| 1169 | } |
| 1170 | }; |
| 1171 | |
| 1172 | /** |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1173 | * Update details based on {@link #mChart} inspection range depending on |
| 1174 | * current mode. In network mode, updates {@link #mAdapter} with sorted list |
| 1175 | * of applications data usage, and when {@link #isAppDetailMode()} update |
| 1176 | * app details. |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1177 | */ |
| 1178 | private void updateDetailData() { |
| 1179 | if (LOGD) Log.d(TAG, "updateDetailData()"); |
| 1180 | |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 1181 | final long start = mChart.getInspectStart(); |
| 1182 | final long end = mChart.getInspectEnd(); |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1183 | final long now = System.currentTimeMillis(); |
| 1184 | |
| 1185 | final Context context = getActivity(); |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 1186 | |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 1187 | NetworkStatsHistory.Entry entry = null; |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1188 | if (isAppDetailMode() && mChartData != null && mChartData.detail != null) { |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 1189 | // bind foreground/background to piechart and labels |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1190 | entry = mChartData.detailDefault.getValues(start, end, now, entry); |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 1191 | final long defaultBytes = entry.rxBytes + entry.txBytes; |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1192 | entry = mChartData.detailForeground.getValues(start, end, now, entry); |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 1193 | final long foregroundBytes = entry.rxBytes + entry.txBytes; |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 1194 | final long totalBytes = defaultBytes + foregroundBytes; |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 1195 | |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 1196 | if (mAppTotal != null) { |
| 1197 | mAppTotal.setText(Formatter.formatFileSize(context, totalBytes)); |
| 1198 | } |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 1199 | mAppBackground.setText(Formatter.formatFileSize(context, defaultBytes)); |
| 1200 | mAppForeground.setText(Formatter.formatFileSize(context, foregroundBytes)); |
| 1201 | |
| 1202 | // and finally leave with summary data for label below |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1203 | entry = mChartData.detail.getValues(start, end, now, null); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1204 | |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1205 | getLoaderManager().destroyLoader(LOADER_SUMMARY); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1206 | |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 1207 | mCycleSummary.setVisibility(View.GONE); |
| 1208 | |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1209 | } else { |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1210 | if (mChartData != null) { |
| 1211 | entry = mChartData.network.getValues(start, end, now, null); |
| 1212 | } |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1213 | |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 1214 | mCycleSummary.setVisibility(View.VISIBLE); |
| 1215 | |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1216 | // kick off loader for detailed stats |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1217 | getLoaderManager().restartLoader(LOADER_SUMMARY, |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1218 | SummaryForAllUidLoader.buildArgs(mTemplate, start, end), mSummaryCallbacks); |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1219 | } |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1220 | |
| 1221 | final long totalBytes = entry != null ? entry.rxBytes + entry.txBytes : 0; |
| 1222 | final String totalPhrase = Formatter.formatFileSize(context, totalBytes); |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 1223 | mCycleSummary.setText(totalPhrase); |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1224 | |
László Dávid | 0b8bf4e | 2012-10-24 23:31:47 +0200 | [diff] [blame] | 1225 | if (TAB_MOBILE.equals(mCurrentTab) || TAB_3G.equals(mCurrentTab) |
| 1226 | || TAB_4G.equals(mCurrentTab)) { |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 1227 | if (isAppDetailMode()) { |
| 1228 | mDisclaimer.setVisibility(View.GONE); |
| 1229 | } else { |
| 1230 | mDisclaimer.setVisibility(View.VISIBLE); |
| 1231 | } |
Jeff Sharkey | e557c33 | 2012-04-13 16:04:07 -0700 | [diff] [blame] | 1232 | } else { |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 1233 | mDisclaimer.setVisibility(View.GONE); |
Jeff Sharkey | e557c33 | 2012-04-13 16:04:07 -0700 | [diff] [blame] | 1234 | } |
| 1235 | |
Jeff Sharkey | 9281182 | 2012-05-04 11:47:29 -0700 | [diff] [blame] | 1236 | // initial layout is finished above, ensure we have transitions |
| 1237 | ensureLayoutTransitions(); |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1238 | } |
| 1239 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1240 | private final LoaderCallbacks<ChartData> mChartDataCallbacks = new LoaderCallbacks< |
| 1241 | ChartData>() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1242 | @Override |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1243 | public Loader<ChartData> onCreateLoader(int id, Bundle args) { |
Jeff Sharkey | 08ce99e | 2012-04-06 11:21:28 -0700 | [diff] [blame] | 1244 | return new ChartDataLoader(getActivity(), mStatsSession, args); |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1245 | } |
| 1246 | |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1247 | @Override |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1248 | public void onLoadFinished(Loader<ChartData> loader, ChartData data) { |
| 1249 | mChartData = data; |
| 1250 | mChart.bindNetworkStats(mChartData.network); |
| 1251 | mChart.bindDetailNetworkStats(mChartData.detail); |
| 1252 | |
| 1253 | // calcuate policy cycles based on available data |
| 1254 | updatePolicy(true); |
| 1255 | updateAppDetail(); |
| 1256 | |
| 1257 | // force scroll to top of body when showing detail |
| 1258 | if (mChartData.detail != null) { |
| 1259 | mListView.smoothScrollToPosition(0); |
| 1260 | } |
| 1261 | } |
| 1262 | |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1263 | @Override |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1264 | public void onLoaderReset(Loader<ChartData> loader) { |
| 1265 | mChartData = null; |
| 1266 | mChart.bindNetworkStats(null); |
| 1267 | mChart.bindDetailNetworkStats(null); |
| 1268 | } |
| 1269 | }; |
| 1270 | |
| 1271 | private final LoaderCallbacks<NetworkStats> mSummaryCallbacks = new LoaderCallbacks< |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1272 | NetworkStats>() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1273 | @Override |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1274 | public Loader<NetworkStats> onCreateLoader(int id, Bundle args) { |
Jeff Sharkey | 08ce99e | 2012-04-06 11:21:28 -0700 | [diff] [blame] | 1275 | return new SummaryForAllUidLoader(getActivity(), mStatsSession, args); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1276 | } |
| 1277 | |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1278 | @Override |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1279 | public void onLoadFinished(Loader<NetworkStats> loader, NetworkStats data) { |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 1280 | final int[] restrictedUids = mPolicyManager.getUidsWithPolicy( |
Jeff Sharkey | e557c33 | 2012-04-13 16:04:07 -0700 | [diff] [blame] | 1281 | POLICY_REJECT_METERED_BACKGROUND); |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 1282 | mAdapter.bindStats(data, restrictedUids); |
Jeff Sharkey | e2afc0f | 2011-08-01 15:29:30 -0700 | [diff] [blame] | 1283 | updateEmptyVisible(); |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1284 | } |
Jeff Sharkey | aa5260e | 2011-06-14 23:21:59 -0700 | [diff] [blame] | 1285 | |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1286 | @Override |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1287 | public void onLoaderReset(Loader<NetworkStats> loader) { |
Jeff Sharkey | e557c33 | 2012-04-13 16:04:07 -0700 | [diff] [blame] | 1288 | mAdapter.bindStats(null, new int[0]); |
Jeff Sharkey | e2afc0f | 2011-08-01 15:29:30 -0700 | [diff] [blame] | 1289 | updateEmptyVisible(); |
| 1290 | } |
| 1291 | |
| 1292 | private void updateEmptyVisible() { |
| 1293 | final boolean isEmpty = mAdapter.isEmpty() && !isAppDetailMode(); |
| 1294 | mEmpty.setVisibility(isEmpty ? View.VISIBLE : View.GONE); |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 1295 | mStupidPadding.setVisibility(isEmpty ? View.VISIBLE : View.GONE); |
Jeff Sharkey | 398b18f | 2011-07-10 18:56:30 -0700 | [diff] [blame] | 1296 | } |
| 1297 | }; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1298 | |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1299 | @Deprecated |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 1300 | private boolean isMobilePolicySplit() { |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 1301 | final Context context = getActivity(); |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 1302 | if (hasReadyMobileRadio(context)) { |
| 1303 | final TelephonyManager tele = TelephonyManager.from(context); |
| 1304 | return mPolicyEditor.isMobilePolicySplit(getActiveSubscriberId(context)); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 1305 | } else { |
| 1306 | return false; |
| 1307 | } |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 1308 | } |
| 1309 | |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1310 | @Deprecated |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 1311 | private void setMobilePolicySplit(boolean split) { |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 1312 | final Context context = getActivity(); |
| 1313 | if (hasReadyMobileRadio(context)) { |
| 1314 | final TelephonyManager tele = TelephonyManager.from(context); |
| 1315 | mPolicyEditor.setMobilePolicySplit(getActiveSubscriberId(context), split); |
| 1316 | } |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 1317 | } |
| 1318 | |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 1319 | private static String getActiveSubscriberId(Context context) { |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 1320 | final TelephonyManager tele = TelephonyManager.from(context); |
| 1321 | final String actualSubscriberId = tele.getSubscriberId(); |
Jeff Sharkey | f3871fb | 2012-02-03 19:27:07 -0800 | [diff] [blame] | 1322 | return SystemProperties.get(TEST_SUBSCRIBER_PROP, actualSubscriberId); |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 1323 | } |
| 1324 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1325 | private DataUsageChartListener mChartListener = new DataUsageChartListener() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1326 | @Override |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 1327 | public void onWarningChanged() { |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1328 | setPolicyWarningBytes(mChart.getWarningBytes()); |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 1329 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1330 | |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1331 | @Override |
Jeff Sharkey | 05cc0cc | 2011-06-12 23:11:24 -0700 | [diff] [blame] | 1332 | public void onLimitChanged() { |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1333 | setPolicyLimitBytes(mChart.getLimitBytes()); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1334 | } |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1335 | |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1336 | @Override |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1337 | public void requestWarningEdit() { |
| 1338 | WarningEditorFragment.show(DataUsageSummary.this); |
| 1339 | } |
| 1340 | |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1341 | @Override |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1342 | public void requestLimitEdit() { |
| 1343 | LimitEditorFragment.show(DataUsageSummary.this); |
| 1344 | } |
| 1345 | }; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1346 | |
| 1347 | /** |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1348 | * List item that reflects a specific data usage cycle. |
| 1349 | */ |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1350 | public static class CycleItem implements Comparable<CycleItem> { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1351 | public CharSequence label; |
| 1352 | public long start; |
| 1353 | public long end; |
| 1354 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1355 | CycleItem(CharSequence label) { |
| 1356 | this.label = label; |
| 1357 | } |
| 1358 | |
| 1359 | public CycleItem(Context context, long start, long end) { |
Jeff Sharkey | e5223a0 | 2012-03-09 17:11:14 -0800 | [diff] [blame] | 1360 | this.label = formatDateRange(context, start, end); |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1361 | this.start = start; |
| 1362 | this.end = end; |
| 1363 | } |
| 1364 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1365 | @Override |
| 1366 | public String toString() { |
| 1367 | return label.toString(); |
| 1368 | } |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1369 | |
| 1370 | @Override |
| 1371 | public boolean equals(Object o) { |
| 1372 | if (o instanceof CycleItem) { |
| 1373 | final CycleItem another = (CycleItem) o; |
| 1374 | return start == another.start && end == another.end; |
| 1375 | } |
| 1376 | return false; |
| 1377 | } |
| 1378 | |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1379 | @Override |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1380 | public int compareTo(CycleItem another) { |
| 1381 | return Long.compare(start, another.start); |
| 1382 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1383 | } |
| 1384 | |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1385 | private static final StringBuilder sBuilder = new StringBuilder(50); |
| 1386 | private static final java.util.Formatter sFormatter = new java.util.Formatter( |
| 1387 | sBuilder, Locale.getDefault()); |
| 1388 | |
Jeff Sharkey | e5223a0 | 2012-03-09 17:11:14 -0800 | [diff] [blame] | 1389 | public static String formatDateRange(Context context, long start, long end) { |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 1390 | final int flags = FORMAT_SHOW_DATE | FORMAT_ABBREV_MONTH; |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1391 | |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 1392 | synchronized (sBuilder) { |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1393 | sBuilder.setLength(0); |
Jeff Sharkey | e5223a0 | 2012-03-09 17:11:14 -0800 | [diff] [blame] | 1394 | return DateUtils.formatDateRange(context, sFormatter, start, end, flags, null) |
| 1395 | .toString(); |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 1396 | } |
| 1397 | } |
| 1398 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1399 | /** |
| 1400 | * Special-case data usage cycle that triggers dialog to change |
| 1401 | * {@link NetworkPolicy#cycleDay}. |
| 1402 | */ |
| 1403 | public static class CycleChangeItem extends CycleItem { |
| 1404 | public CycleChangeItem(Context context) { |
| 1405 | super(context.getString(R.string.data_usage_change_cycle)); |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | public static class CycleAdapter extends ArrayAdapter<CycleItem> { |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 1410 | private boolean mChangePossible = false; |
| 1411 | private boolean mChangeVisible = false; |
| 1412 | |
| 1413 | private final CycleChangeItem mChangeItem; |
| 1414 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1415 | public CycleAdapter(Context context) { |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 1416 | super(context, R.layout.data_usage_cycle_item); |
| 1417 | setDropDownViewResource(R.layout.data_usage_cycle_item_dropdown); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 1418 | mChangeItem = new CycleChangeItem(context); |
| 1419 | } |
| 1420 | |
| 1421 | public void setChangePossible(boolean possible) { |
| 1422 | mChangePossible = possible; |
| 1423 | updateChange(); |
| 1424 | } |
| 1425 | |
| 1426 | public void setChangeVisible(boolean visible) { |
| 1427 | mChangeVisible = visible; |
| 1428 | updateChange(); |
| 1429 | } |
| 1430 | |
| 1431 | private void updateChange() { |
| 1432 | remove(mChangeItem); |
| 1433 | if (mChangePossible && mChangeVisible) { |
| 1434 | add(mChangeItem); |
| 1435 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1436 | } |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1437 | |
| 1438 | /** |
| 1439 | * Find position of {@link CycleItem} in this adapter which is nearest |
| 1440 | * the given {@link CycleItem}. |
| 1441 | */ |
| 1442 | public int findNearestPosition(CycleItem target) { |
| 1443 | if (target != null) { |
| 1444 | final int count = getCount(); |
| 1445 | for (int i = count - 1; i >= 0; i--) { |
| 1446 | final CycleItem item = getItem(i); |
| 1447 | if (item instanceof CycleChangeItem) { |
| 1448 | continue; |
| 1449 | } else if (item.compareTo(target) >= 0) { |
| 1450 | return i; |
| 1451 | } |
| 1452 | } |
| 1453 | } |
| 1454 | return 0; |
| 1455 | } |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1456 | } |
| 1457 | |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1458 | public static class AppItem implements Comparable<AppItem>, Parcelable { |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 1459 | public static final int CATEGORY_USER = 0; |
| 1460 | public static final int CATEGORY_APP_TITLE = 1; |
| 1461 | public static final int CATEGORY_APP = 2; |
| 1462 | |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 1463 | public final int key; |
Jeff Sharkey | e557c33 | 2012-04-13 16:04:07 -0700 | [diff] [blame] | 1464 | public boolean restricted; |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 1465 | public int category; |
| 1466 | |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1467 | public SparseBooleanArray uids = new SparseBooleanArray(); |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 1468 | public long total; |
| 1469 | |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 1470 | public AppItem() { |
| 1471 | this.key = 0; |
| 1472 | } |
| 1473 | |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 1474 | public AppItem(int key) { |
| 1475 | this.key = key; |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | public AppItem(Parcel parcel) { |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 1479 | key = parcel.readInt(); |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1480 | uids = parcel.readSparseBooleanArray(); |
| 1481 | total = parcel.readLong(); |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | public void addUid(int uid) { |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1485 | uids.put(uid, true); |
Jeff Sharkey | 55d18a5 | 2011-08-27 17:09:43 -0700 | [diff] [blame] | 1486 | } |
| 1487 | |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1488 | @Override |
| 1489 | public void writeToParcel(Parcel dest, int flags) { |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 1490 | dest.writeInt(key); |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1491 | dest.writeSparseBooleanArray(uids); |
| 1492 | dest.writeLong(total); |
| 1493 | } |
| 1494 | |
| 1495 | @Override |
| 1496 | public int describeContents() { |
| 1497 | return 0; |
| 1498 | } |
| 1499 | |
| 1500 | @Override |
| 1501 | public int compareTo(AppItem another) { |
Zoltan Szatmary-Ban | 0924f19 | 2014-07-21 16:42:50 +0100 | [diff] [blame^] | 1502 | int comparison = Integer.compare(category, another.category); |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 1503 | if (comparison == 0) { |
| 1504 | comparison = Long.compare(another.total, total); |
| 1505 | } |
| 1506 | return comparison; |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 1507 | } |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1508 | |
| 1509 | public static final Creator<AppItem> CREATOR = new Creator<AppItem>() { |
| 1510 | @Override |
| 1511 | public AppItem createFromParcel(Parcel in) { |
| 1512 | return new AppItem(in); |
| 1513 | } |
| 1514 | |
| 1515 | @Override |
| 1516 | public AppItem[] newArray(int size) { |
| 1517 | return new AppItem[size]; |
| 1518 | } |
| 1519 | }; |
Jeff Sharkey | 4dfa660 | 2011-06-13 00:42:03 -0700 | [diff] [blame] | 1520 | } |
| 1521 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1522 | /** |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1523 | * Adapter of applications, sorted by total usage descending. |
| 1524 | */ |
| 1525 | public static class DataUsageAdapter extends BaseAdapter { |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1526 | private final UidDetailProvider mProvider; |
| 1527 | private final int mInsetSide; |
Zoltan Szatmary-Ban | 0924f19 | 2014-07-21 16:42:50 +0100 | [diff] [blame^] | 1528 | private final UserManager mUm; |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1529 | |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1530 | private ArrayList<AppItem> mItems = Lists.newArrayList(); |
Jeff Sharkey | 2412b0f | 2011-07-17 20:31:40 -0700 | [diff] [blame] | 1531 | private long mLargest; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1532 | |
Zoltan Szatmary-Ban | 0924f19 | 2014-07-21 16:42:50 +0100 | [diff] [blame^] | 1533 | public DataUsageAdapter(final UserManager userManager, UidDetailProvider provider, int insetSide) { |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1534 | mProvider = checkNotNull(provider); |
| 1535 | mInsetSide = insetSide; |
Zoltan Szatmary-Ban | 0924f19 | 2014-07-21 16:42:50 +0100 | [diff] [blame^] | 1536 | mUm = userManager; |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1537 | } |
| 1538 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1539 | /** |
| 1540 | * Bind the given {@link NetworkStats}, or {@code null} to clear list. |
| 1541 | */ |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 1542 | public void bindStats(NetworkStats stats, int[] restrictedUids) { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1543 | mItems.clear(); |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 1544 | mLargest = 0; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1545 | |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 1546 | final int currentUserId = ActivityManager.getCurrentUser(); |
Zoltan Szatmary-Ban | 0924f19 | 2014-07-21 16:42:50 +0100 | [diff] [blame^] | 1547 | final List<UserHandle> profiles = mUm.getUserProfiles(); |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 1548 | final SparseArray<AppItem> knownItems = new SparseArray<AppItem>(); |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 1549 | |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 1550 | NetworkStats.Entry entry = null; |
| 1551 | final int size = stats != null ? stats.size() : 0; |
| 1552 | for (int i = 0; i < size; i++) { |
| 1553 | entry = stats.getValues(i, entry); |
| 1554 | |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 1555 | // Decide how to collapse items together |
| 1556 | final int uid = entry.uid; |
Zoltan Szatmary-Ban | 0924f19 | 2014-07-21 16:42:50 +0100 | [diff] [blame^] | 1557 | |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 1558 | final int collapseKey; |
Zoltan Szatmary-Ban | 0924f19 | 2014-07-21 16:42:50 +0100 | [diff] [blame^] | 1559 | final int category; |
| 1560 | final int userId = UserHandle.getUserId(uid); |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 1561 | if (UserHandle.isApp(uid)) { |
Zoltan Szatmary-Ban | 0924f19 | 2014-07-21 16:42:50 +0100 | [diff] [blame^] | 1562 | if (profiles.contains(new UserHandle(userId))) { |
| 1563 | if (userId != currentUserId) { |
| 1564 | // Add to a managed user item. |
| 1565 | final int managedKey = UidDetailProvider.buildKeyForUser(userId); |
| 1566 | accumulate(managedKey, knownItems, entry, |
| 1567 | AppItem.CATEGORY_USER); |
| 1568 | } |
| 1569 | // Add to app item. |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 1570 | collapseKey = uid; |
Zoltan Szatmary-Ban | 0924f19 | 2014-07-21 16:42:50 +0100 | [diff] [blame^] | 1571 | category = AppItem.CATEGORY_APP; |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 1572 | } else { |
Zoltan Szatmary-Ban | 0924f19 | 2014-07-21 16:42:50 +0100 | [diff] [blame^] | 1573 | // Add to other user item. |
| 1574 | collapseKey = UidDetailProvider.buildKeyForUser(userId); |
| 1575 | category = AppItem.CATEGORY_USER; |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 1576 | } |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 1577 | } else if (uid == UID_REMOVED || uid == UID_TETHERING) { |
| 1578 | collapseKey = uid; |
Zoltan Szatmary-Ban | 0924f19 | 2014-07-21 16:42:50 +0100 | [diff] [blame^] | 1579 | category = AppItem.CATEGORY_APP; |
Jeff Sharkey | 54d0af5 | 2011-08-11 18:26:57 -0700 | [diff] [blame] | 1580 | } else { |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 1581 | collapseKey = android.os.Process.SYSTEM_UID; |
Zoltan Szatmary-Ban | 0924f19 | 2014-07-21 16:42:50 +0100 | [diff] [blame^] | 1582 | category = AppItem.CATEGORY_APP; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1583 | } |
Zoltan Szatmary-Ban | 0924f19 | 2014-07-21 16:42:50 +0100 | [diff] [blame^] | 1584 | accumulate(collapseKey, knownItems, entry, category); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1585 | } |
| 1586 | |
Zoltan Szatmary-Ban | 0924f19 | 2014-07-21 16:42:50 +0100 | [diff] [blame^] | 1587 | final int restrictedUidsMax = restrictedUids.length; |
| 1588 | for (int i = 0; i < restrictedUidsMax; ++i) { |
| 1589 | final int uid = restrictedUids[i]; |
| 1590 | // Only splice in restricted state for current user or managed users |
| 1591 | if (!profiles.contains(new UserHandle(UserHandle.getUserId(uid)))) { |
| 1592 | continue; |
| 1593 | } |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 1594 | |
| 1595 | AppItem item = knownItems.get(uid); |
Jeff Sharkey | e557c33 | 2012-04-13 16:04:07 -0700 | [diff] [blame] | 1596 | if (item == null) { |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 1597 | item = new AppItem(uid); |
Jeff Sharkey | e557c33 | 2012-04-13 16:04:07 -0700 | [diff] [blame] | 1598 | item.total = -1; |
| 1599 | mItems.add(item); |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 1600 | knownItems.put(item.key, item); |
Jeff Sharkey | e557c33 | 2012-04-13 16:04:07 -0700 | [diff] [blame] | 1601 | } |
| 1602 | item.restricted = true; |
| 1603 | } |
| 1604 | |
Zoltan Szatmary-Ban | 0924f19 | 2014-07-21 16:42:50 +0100 | [diff] [blame^] | 1605 | if (!mItems.isEmpty()) { |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 1606 | final AppItem title = new AppItem(); |
| 1607 | title.category = AppItem.CATEGORY_APP_TITLE; |
| 1608 | mItems.add(title); |
| 1609 | } |
| 1610 | |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1611 | Collections.sort(mItems); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1612 | notifyDataSetChanged(); |
| 1613 | } |
| 1614 | |
Zoltan Szatmary-Ban | 0924f19 | 2014-07-21 16:42:50 +0100 | [diff] [blame^] | 1615 | /** |
| 1616 | * Accumulate data usage of a network stats entry for the item mapped by the collapse key. |
| 1617 | * Creates the item if needed. |
| 1618 | * |
| 1619 | * @param collapseKey the collapse key used to map the item. |
| 1620 | * @param knownItems collection of known (already existing) items. |
| 1621 | * @param entry the network stats entry to extract data usage from. |
| 1622 | * @param itemCategory the item is categorized on the list view by this category. Must be |
| 1623 | * either AppItem.APP_ITEM_CATEGORY or AppItem.MANAGED_USER_ITEM_CATEGORY |
| 1624 | */ |
| 1625 | private void accumulate(int collapseKey, final SparseArray<AppItem> knownItems, |
| 1626 | NetworkStats.Entry entry, int itemCategory) { |
| 1627 | final int uid = entry.uid; |
| 1628 | AppItem item = knownItems.get(collapseKey); |
| 1629 | if (item == null) { |
| 1630 | item = new AppItem(collapseKey); |
| 1631 | item.category = itemCategory; |
| 1632 | mItems.add(item); |
| 1633 | knownItems.put(item.key, item); |
| 1634 | } |
| 1635 | item.addUid(uid); |
| 1636 | item.total += entry.rxBytes + entry.txBytes; |
| 1637 | if (mLargest < item.total) { |
| 1638 | mLargest = item.total; |
| 1639 | } |
| 1640 | } |
| 1641 | |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1642 | @Override |
| 1643 | public int getCount() { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1644 | return mItems.size(); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1645 | } |
| 1646 | |
| 1647 | @Override |
| 1648 | public Object getItem(int position) { |
Jeff Sharkey | 8a50364 | 2011-06-10 13:31:21 -0700 | [diff] [blame] | 1649 | return mItems.get(position); |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1650 | } |
| 1651 | |
| 1652 | @Override |
| 1653 | public long getItemId(int position) { |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 1654 | return mItems.get(position).key; |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1655 | } |
| 1656 | |
Zoltan Szatmary-Ban | 0924f19 | 2014-07-21 16:42:50 +0100 | [diff] [blame^] | 1657 | /** |
| 1658 | * See {@link #getItemViewType} for the view types. |
| 1659 | */ |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1660 | @Override |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 1661 | public int getViewTypeCount() { |
| 1662 | return 2; |
| 1663 | } |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 1664 | |
Zoltan Szatmary-Ban | 0924f19 | 2014-07-21 16:42:50 +0100 | [diff] [blame^] | 1665 | /** |
| 1666 | * Returns 1 for separator items and 0 for anything else. |
| 1667 | */ |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 1668 | @Override |
| 1669 | public int getItemViewType(int position) { |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1670 | final AppItem item = mItems.get(position); |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 1671 | if (item.category == AppItem.CATEGORY_APP_TITLE) { |
| 1672 | return 1; |
Jeff Sharkey | e557c33 | 2012-04-13 16:04:07 -0700 | [diff] [blame] | 1673 | } else { |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 1674 | return 0; |
Jeff Sharkey | e557c33 | 2012-04-13 16:04:07 -0700 | [diff] [blame] | 1675 | } |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 1676 | } |
Jeff Sharkey | 2412b0f | 2011-07-17 20:31:40 -0700 | [diff] [blame] | 1677 | |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 1678 | @Override |
| 1679 | public boolean areAllItemsEnabled() { |
| 1680 | return false; |
| 1681 | } |
| 1682 | |
| 1683 | @Override |
| 1684 | public boolean isEnabled(int position) { |
Zoltan Szatmary-Ban | 0924f19 | 2014-07-21 16:42:50 +0100 | [diff] [blame^] | 1685 | if (position > mItems.size()) { |
| 1686 | throw new ArrayIndexOutOfBoundsException(); |
| 1687 | } |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 1688 | return getItemViewType(position) == 0; |
| 1689 | } |
| 1690 | |
| 1691 | @Override |
| 1692 | public View getView(int position, View convertView, ViewGroup parent) { |
| 1693 | final AppItem item = mItems.get(position); |
| 1694 | if (getItemViewType(position) == 1) { |
| 1695 | if (convertView == null) { |
| 1696 | convertView = inflateCategoryHeader(LayoutInflater.from(parent.getContext()), |
| 1697 | parent); |
| 1698 | } |
| 1699 | |
| 1700 | final TextView title = (TextView) convertView.findViewById(android.R.id.title); |
| 1701 | title.setText(R.string.data_usage_app); |
| 1702 | |
| 1703 | } else { |
| 1704 | if (convertView == null) { |
| 1705 | convertView = LayoutInflater.from(parent.getContext()).inflate( |
| 1706 | R.layout.data_usage_item, parent, false); |
| 1707 | |
| 1708 | if (mInsetSide > 0) { |
| 1709 | convertView.setPaddingRelative(mInsetSide, 0, mInsetSide, 0); |
| 1710 | } |
| 1711 | } |
| 1712 | |
| 1713 | final Context context = parent.getContext(); |
| 1714 | |
| 1715 | final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1); |
| 1716 | final ProgressBar progress = (ProgressBar) convertView.findViewById( |
| 1717 | android.R.id.progress); |
| 1718 | |
| 1719 | // kick off async load of app details |
| 1720 | UidDetailTask.bindView(mProvider, item, convertView); |
| 1721 | |
| 1722 | if (item.restricted && item.total <= 0) { |
| 1723 | text1.setText(R.string.data_usage_app_restricted); |
| 1724 | progress.setVisibility(View.GONE); |
| 1725 | } else { |
| 1726 | text1.setText(Formatter.formatFileSize(context, item.total)); |
| 1727 | progress.setVisibility(View.VISIBLE); |
| 1728 | } |
| 1729 | |
| 1730 | final int percentTotal = mLargest != 0 ? (int) (item.total * 100 / mLargest) : 0; |
| 1731 | progress.setProgress(percentTotal); |
| 1732 | } |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1733 | |
| 1734 | return convertView; |
| 1735 | } |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1736 | } |
| 1737 | |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1738 | /** |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1739 | * Empty {@link Fragment} that controls display of UID details in |
| 1740 | * {@link DataUsageSummary}. |
| 1741 | */ |
| 1742 | public static class AppDetailsFragment extends Fragment { |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1743 | private static final String EXTRA_APP = "app"; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1744 | |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1745 | public static void show(DataUsageSummary parent, AppItem app, CharSequence label) { |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 1746 | if (!parent.isAdded()) return; |
| 1747 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1748 | final Bundle args = new Bundle(); |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1749 | args.putParcelable(EXTRA_APP, app); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1750 | |
| 1751 | final AppDetailsFragment fragment = new AppDetailsFragment(); |
| 1752 | fragment.setArguments(args); |
| 1753 | fragment.setTargetFragment(parent, 0); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1754 | final FragmentTransaction ft = parent.getFragmentManager().beginTransaction(); |
| 1755 | ft.add(fragment, TAG_APP_DETAILS); |
| 1756 | ft.addToBackStack(TAG_APP_DETAILS); |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 1757 | ft.setBreadCrumbTitle( |
| 1758 | parent.getResources().getString(R.string.data_usage_app_summary_title)); |
Jeff Sharkey | 3235ddb | 2012-03-15 16:40:31 -0700 | [diff] [blame] | 1759 | ft.commitAllowingStateLoss(); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1760 | } |
| 1761 | |
| 1762 | @Override |
| 1763 | public void onStart() { |
| 1764 | super.onStart(); |
| 1765 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1766 | target.mCurrentApp = getArguments().getParcelable(EXTRA_APP); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1767 | target.updateBody(); |
| 1768 | } |
| 1769 | |
| 1770 | @Override |
| 1771 | public void onStop() { |
| 1772 | super.onStop(); |
| 1773 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 1774 | target.mCurrentApp = null; |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 1775 | target.updateBody(); |
| 1776 | } |
| 1777 | } |
| 1778 | |
| 1779 | /** |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1780 | * Dialog to request user confirmation before setting |
| 1781 | * {@link NetworkPolicy#limitBytes}. |
| 1782 | */ |
| 1783 | public static class ConfirmLimitFragment extends DialogFragment { |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 1784 | private static final String EXTRA_MESSAGE = "message"; |
Jeff Sharkey | 2412b0f | 2011-07-17 20:31:40 -0700 | [diff] [blame] | 1785 | private static final String EXTRA_LIMIT_BYTES = "limitBytes"; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1786 | |
| 1787 | public static void show(DataUsageSummary parent) { |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 1788 | if (!parent.isAdded()) return; |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 1789 | |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 1790 | final Resources res = parent.getResources(); |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 1791 | final CharSequence message; |
Jeff Sharkey | 34e964d | 2012-04-21 15:41:48 -0700 | [diff] [blame] | 1792 | final long minLimitBytes = (long) ( |
| 1793 | parent.mPolicyEditor.getPolicy(parent.mTemplate).warningBytes * 1.2f); |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 1794 | final long limitBytes; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1795 | |
| 1796 | // TODO: customize default limits based on network template |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 1797 | final String currentTab = parent.mCurrentTab; |
| 1798 | if (TAB_3G.equals(currentTab)) { |
Jeff Sharkey | e557c33 | 2012-04-13 16:04:07 -0700 | [diff] [blame] | 1799 | message = res.getString(R.string.data_usage_limit_dialog_mobile); |
Jeff Sharkey | 34e964d | 2012-04-21 15:41:48 -0700 | [diff] [blame] | 1800 | limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 1801 | } else if (TAB_4G.equals(currentTab)) { |
Jeff Sharkey | e557c33 | 2012-04-13 16:04:07 -0700 | [diff] [blame] | 1802 | message = res.getString(R.string.data_usage_limit_dialog_mobile); |
Jeff Sharkey | 34e964d | 2012-04-21 15:41:48 -0700 | [diff] [blame] | 1803 | limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes); |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 1804 | } else if (TAB_MOBILE.equals(currentTab)) { |
Jeff Sharkey | e557c33 | 2012-04-13 16:04:07 -0700 | [diff] [blame] | 1805 | message = res.getString(R.string.data_usage_limit_dialog_mobile); |
Jeff Sharkey | 34e964d | 2012-04-21 15:41:48 -0700 | [diff] [blame] | 1806 | limitBytes = Math.max(5 * GB_IN_BYTES, minLimitBytes); |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 1807 | } else { |
| 1808 | throw new IllegalArgumentException("unknown current tab: " + currentTab); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1809 | } |
| 1810 | |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 1811 | final Bundle args = new Bundle(); |
| 1812 | args.putCharSequence(EXTRA_MESSAGE, message); |
| 1813 | args.putLong(EXTRA_LIMIT_BYTES, limitBytes); |
| 1814 | |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1815 | final ConfirmLimitFragment dialog = new ConfirmLimitFragment(); |
| 1816 | dialog.setArguments(args); |
| 1817 | dialog.setTargetFragment(parent, 0); |
| 1818 | dialog.show(parent.getFragmentManager(), TAG_CONFIRM_LIMIT); |
| 1819 | } |
| 1820 | |
| 1821 | @Override |
| 1822 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 1823 | final Context context = getActivity(); |
| 1824 | |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 1825 | final CharSequence message = getArguments().getCharSequence(EXTRA_MESSAGE); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1826 | final long limitBytes = getArguments().getLong(EXTRA_LIMIT_BYTES); |
| 1827 | |
| 1828 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 1829 | builder.setTitle(R.string.data_usage_limit_dialog_title); |
Jeff Sharkey | 131f9d6 | 2011-08-17 17:08:19 -0700 | [diff] [blame] | 1830 | builder.setMessage(message); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1831 | |
| 1832 | builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1833 | @Override |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1834 | public void onClick(DialogInterface dialog, int which) { |
| 1835 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
| 1836 | if (target != null) { |
| 1837 | target.setPolicyLimitBytes(limitBytes); |
| 1838 | } |
| 1839 | } |
| 1840 | }); |
| 1841 | |
| 1842 | return builder.create(); |
| 1843 | } |
| 1844 | } |
| 1845 | |
| 1846 | /** |
| 1847 | * Dialog to edit {@link NetworkPolicy#cycleDay}. |
| 1848 | */ |
| 1849 | public static class CycleEditorFragment extends DialogFragment { |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1850 | private static final String EXTRA_TEMPLATE = "template"; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1851 | |
| 1852 | public static void show(DataUsageSummary parent) { |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 1853 | if (!parent.isAdded()) return; |
| 1854 | |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1855 | final Bundle args = new Bundle(); |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1856 | args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1857 | |
| 1858 | final CycleEditorFragment dialog = new CycleEditorFragment(); |
| 1859 | dialog.setArguments(args); |
| 1860 | dialog.setTargetFragment(parent, 0); |
| 1861 | dialog.show(parent.getFragmentManager(), TAG_CYCLE_EDITOR); |
| 1862 | } |
| 1863 | |
| 1864 | @Override |
| 1865 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 1866 | final Context context = getActivity(); |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1867 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
| 1868 | final NetworkPolicyEditor editor = target.mPolicyEditor; |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1869 | |
| 1870 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 1871 | final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext()); |
| 1872 | |
| 1873 | final View view = dialogInflater.inflate(R.layout.data_usage_cycle_editor, null, false); |
| 1874 | final NumberPicker cycleDayPicker = (NumberPicker) view.findViewById(R.id.cycle_day); |
| 1875 | |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1876 | final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE); |
| 1877 | final int cycleDay = editor.getPolicyCycleDay(template); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1878 | |
| 1879 | cycleDayPicker.setMinValue(1); |
| 1880 | cycleDayPicker.setMaxValue(31); |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1881 | cycleDayPicker.setValue(cycleDay); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1882 | cycleDayPicker.setWrapSelectorWheel(true); |
| 1883 | |
| 1884 | builder.setTitle(R.string.data_usage_cycle_editor_title); |
| 1885 | builder.setView(view); |
| 1886 | |
| 1887 | builder.setPositiveButton(R.string.data_usage_cycle_editor_positive, |
| 1888 | new DialogInterface.OnClickListener() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1889 | @Override |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1890 | public void onClick(DialogInterface dialog, int which) { |
Jeff Sharkey | d277de9 | 2013-03-25 15:11:25 -0700 | [diff] [blame] | 1891 | // clear focus to finish pending text edits |
| 1892 | cycleDayPicker.clearFocus(); |
| 1893 | |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1894 | final int cycleDay = cycleDayPicker.getValue(); |
Jeff Sharkey | e5223a0 | 2012-03-09 17:11:14 -0800 | [diff] [blame] | 1895 | final String cycleTimezone = new Time().timezone; |
| 1896 | editor.setPolicyCycleDay(template, cycleDay, cycleTimezone); |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1897 | target.updatePolicy(true); |
Jeff Sharkey | 4c72ae5 | 2011-06-14 15:01:18 -0700 | [diff] [blame] | 1898 | } |
| 1899 | }); |
| 1900 | |
| 1901 | return builder.create(); |
| 1902 | } |
| 1903 | } |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 1904 | |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 1905 | /** |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1906 | * Dialog to edit {@link NetworkPolicy#warningBytes}. |
| 1907 | */ |
| 1908 | public static class WarningEditorFragment extends DialogFragment { |
| 1909 | private static final String EXTRA_TEMPLATE = "template"; |
| 1910 | |
| 1911 | public static void show(DataUsageSummary parent) { |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 1912 | if (!parent.isAdded()) return; |
| 1913 | |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1914 | final Bundle args = new Bundle(); |
| 1915 | args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate); |
| 1916 | |
| 1917 | final WarningEditorFragment dialog = new WarningEditorFragment(); |
| 1918 | dialog.setArguments(args); |
| 1919 | dialog.setTargetFragment(parent, 0); |
| 1920 | dialog.show(parent.getFragmentManager(), TAG_WARNING_EDITOR); |
| 1921 | } |
| 1922 | |
| 1923 | @Override |
| 1924 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 1925 | final Context context = getActivity(); |
| 1926 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
| 1927 | final NetworkPolicyEditor editor = target.mPolicyEditor; |
| 1928 | |
| 1929 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 1930 | final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext()); |
| 1931 | |
| 1932 | final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false); |
| 1933 | final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes); |
| 1934 | |
| 1935 | final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE); |
| 1936 | final long warningBytes = editor.getPolicyWarningBytes(template); |
| 1937 | final long limitBytes = editor.getPolicyLimitBytes(template); |
| 1938 | |
| 1939 | bytesPicker.setMinValue(0); |
| 1940 | if (limitBytes != LIMIT_DISABLED) { |
| 1941 | bytesPicker.setMaxValue((int) (limitBytes / MB_IN_BYTES) - 1); |
| 1942 | } else { |
| 1943 | bytesPicker.setMaxValue(Integer.MAX_VALUE); |
| 1944 | } |
| 1945 | bytesPicker.setValue((int) (warningBytes / MB_IN_BYTES)); |
| 1946 | bytesPicker.setWrapSelectorWheel(false); |
| 1947 | |
| 1948 | builder.setTitle(R.string.data_usage_warning_editor_title); |
| 1949 | builder.setView(view); |
| 1950 | |
| 1951 | builder.setPositiveButton(R.string.data_usage_cycle_editor_positive, |
| 1952 | new DialogInterface.OnClickListener() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 1953 | @Override |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1954 | public void onClick(DialogInterface dialog, int which) { |
| 1955 | // clear focus to finish pending text edits |
| 1956 | bytesPicker.clearFocus(); |
| 1957 | |
| 1958 | final long bytes = bytesPicker.getValue() * MB_IN_BYTES; |
| 1959 | editor.setPolicyWarningBytes(template, bytes); |
| 1960 | target.updatePolicy(false); |
| 1961 | } |
| 1962 | }); |
| 1963 | |
| 1964 | return builder.create(); |
| 1965 | } |
| 1966 | } |
| 1967 | |
| 1968 | /** |
| 1969 | * Dialog to edit {@link NetworkPolicy#limitBytes}. |
| 1970 | */ |
| 1971 | public static class LimitEditorFragment extends DialogFragment { |
| 1972 | private static final String EXTRA_TEMPLATE = "template"; |
| 1973 | |
| 1974 | public static void show(DataUsageSummary parent) { |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 1975 | if (!parent.isAdded()) return; |
| 1976 | |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 1977 | final Bundle args = new Bundle(); |
| 1978 | args.putParcelable(EXTRA_TEMPLATE, parent.mTemplate); |
| 1979 | |
| 1980 | final LimitEditorFragment dialog = new LimitEditorFragment(); |
| 1981 | dialog.setArguments(args); |
| 1982 | dialog.setTargetFragment(parent, 0); |
| 1983 | dialog.show(parent.getFragmentManager(), TAG_LIMIT_EDITOR); |
| 1984 | } |
| 1985 | |
| 1986 | @Override |
| 1987 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 1988 | final Context context = getActivity(); |
| 1989 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
| 1990 | final NetworkPolicyEditor editor = target.mPolicyEditor; |
| 1991 | |
| 1992 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 1993 | final LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext()); |
| 1994 | |
| 1995 | final View view = dialogInflater.inflate(R.layout.data_usage_bytes_editor, null, false); |
| 1996 | final NumberPicker bytesPicker = (NumberPicker) view.findViewById(R.id.bytes); |
| 1997 | |
| 1998 | final NetworkTemplate template = getArguments().getParcelable(EXTRA_TEMPLATE); |
| 1999 | final long warningBytes = editor.getPolicyWarningBytes(template); |
| 2000 | final long limitBytes = editor.getPolicyLimitBytes(template); |
| 2001 | |
| 2002 | bytesPicker.setMaxValue(Integer.MAX_VALUE); |
Shuhrat Dehkanov | f9237f6 | 2012-01-26 23:04:02 +0900 | [diff] [blame] | 2003 | if (warningBytes != WARNING_DISABLED && limitBytes > 0) { |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 2004 | bytesPicker.setMinValue((int) (warningBytes / MB_IN_BYTES) + 1); |
| 2005 | } else { |
| 2006 | bytesPicker.setMinValue(0); |
| 2007 | } |
| 2008 | bytesPicker.setValue((int) (limitBytes / MB_IN_BYTES)); |
| 2009 | bytesPicker.setWrapSelectorWheel(false); |
| 2010 | |
| 2011 | builder.setTitle(R.string.data_usage_limit_editor_title); |
| 2012 | builder.setView(view); |
| 2013 | |
| 2014 | builder.setPositiveButton(R.string.data_usage_cycle_editor_positive, |
| 2015 | new DialogInterface.OnClickListener() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 2016 | @Override |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 2017 | public void onClick(DialogInterface dialog, int which) { |
| 2018 | // clear focus to finish pending text edits |
| 2019 | bytesPicker.clearFocus(); |
| 2020 | |
| 2021 | final long bytes = bytesPicker.getValue() * MB_IN_BYTES; |
| 2022 | editor.setPolicyLimitBytes(template, bytes); |
| 2023 | target.updatePolicy(false); |
| 2024 | } |
| 2025 | }); |
| 2026 | |
| 2027 | return builder.create(); |
| 2028 | } |
| 2029 | } |
| 2030 | /** |
Jeff Sharkey | 28130d9 | 2011-09-02 16:10:24 -0700 | [diff] [blame] | 2031 | * Dialog to request user confirmation before disabling data. |
| 2032 | */ |
| 2033 | public static class ConfirmDataDisableFragment extends DialogFragment { |
| 2034 | public static void show(DataUsageSummary parent) { |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 2035 | if (!parent.isAdded()) return; |
| 2036 | |
Jeff Sharkey | 28130d9 | 2011-09-02 16:10:24 -0700 | [diff] [blame] | 2037 | final ConfirmDataDisableFragment dialog = new ConfirmDataDisableFragment(); |
| 2038 | dialog.setTargetFragment(parent, 0); |
| 2039 | dialog.show(parent.getFragmentManager(), TAG_CONFIRM_DATA_DISABLE); |
| 2040 | } |
| 2041 | |
| 2042 | @Override |
| 2043 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 2044 | final Context context = getActivity(); |
| 2045 | |
| 2046 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 2047 | builder.setMessage(R.string.data_usage_disable_mobile); |
| 2048 | |
| 2049 | builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 2050 | @Override |
Jeff Sharkey | 28130d9 | 2011-09-02 16:10:24 -0700 | [diff] [blame] | 2051 | public void onClick(DialogInterface dialog, int which) { |
| 2052 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
| 2053 | if (target != null) { |
| 2054 | // TODO: extend to modify policy enabled flag. |
| 2055 | target.setMobileDataEnabled(false); |
| 2056 | } |
| 2057 | } |
| 2058 | }); |
| 2059 | builder.setNegativeButton(android.R.string.cancel, null); |
| 2060 | |
| 2061 | return builder.create(); |
| 2062 | } |
| 2063 | } |
| 2064 | |
| 2065 | /** |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 2066 | * Dialog to request user confirmation before setting |
Jeff Sharkey | d360e5e | 2011-07-26 19:30:26 -0700 | [diff] [blame] | 2067 | * {@link INetworkPolicyManager#setRestrictBackground(boolean)}. |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 2068 | */ |
| 2069 | public static class ConfirmRestrictFragment extends DialogFragment { |
| 2070 | public static void show(DataUsageSummary parent) { |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 2071 | if (!parent.isAdded()) return; |
| 2072 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 2073 | final ConfirmRestrictFragment dialog = new ConfirmRestrictFragment(); |
| 2074 | dialog.setTargetFragment(parent, 0); |
| 2075 | dialog.show(parent.getFragmentManager(), TAG_CONFIRM_RESTRICT); |
| 2076 | } |
| 2077 | |
| 2078 | @Override |
| 2079 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 2080 | final Context context = getActivity(); |
| 2081 | |
| 2082 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 2083 | builder.setTitle(R.string.data_usage_restrict_background_title); |
Amith Yamasani | 9627a8e | 2012-09-23 12:54:14 -0700 | [diff] [blame] | 2084 | if (Utils.hasMultipleUsers(context)) { |
| 2085 | builder.setMessage(R.string.data_usage_restrict_background_multiuser); |
| 2086 | } else { |
| 2087 | builder.setMessage(R.string.data_usage_restrict_background); |
| 2088 | } |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 2089 | |
| 2090 | builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 2091 | @Override |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 2092 | public void onClick(DialogInterface dialog, int which) { |
| 2093 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
| 2094 | if (target != null) { |
| 2095 | target.setRestrictBackground(true); |
| 2096 | } |
| 2097 | } |
| 2098 | }); |
| 2099 | builder.setNegativeButton(android.R.string.cancel, null); |
| 2100 | |
| 2101 | return builder.create(); |
| 2102 | } |
| 2103 | } |
| 2104 | |
| 2105 | /** |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 2106 | * Dialog to inform user that {@link #POLICY_REJECT_METERED_BACKGROUND} |
| 2107 | * change has been denied, usually based on |
| 2108 | * {@link DataUsageSummary#hasLimitedNetworks()}. |
| 2109 | */ |
| 2110 | public static class DeniedRestrictFragment extends DialogFragment { |
| 2111 | public static void show(DataUsageSummary parent) { |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 2112 | if (!parent.isAdded()) return; |
| 2113 | |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 2114 | final DeniedRestrictFragment dialog = new DeniedRestrictFragment(); |
| 2115 | dialog.setTargetFragment(parent, 0); |
| 2116 | dialog.show(parent.getFragmentManager(), TAG_DENIED_RESTRICT); |
| 2117 | } |
| 2118 | |
| 2119 | @Override |
| 2120 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 2121 | final Context context = getActivity(); |
| 2122 | |
| 2123 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 2124 | builder.setTitle(R.string.data_usage_app_restrict_background); |
| 2125 | builder.setMessage(R.string.data_usage_restrict_denied_dialog); |
| 2126 | builder.setPositiveButton(android.R.string.ok, null); |
| 2127 | |
| 2128 | return builder.create(); |
| 2129 | } |
| 2130 | } |
| 2131 | |
| 2132 | /** |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 2133 | * Dialog to request user confirmation before setting |
| 2134 | * {@link #POLICY_REJECT_METERED_BACKGROUND}. |
| 2135 | */ |
| 2136 | public static class ConfirmAppRestrictFragment extends DialogFragment { |
| 2137 | public static void show(DataUsageSummary parent) { |
Jeff Sharkey | 461842a | 2011-09-25 18:22:48 -0700 | [diff] [blame] | 2138 | if (!parent.isAdded()) return; |
| 2139 | |
Jeff Sharkey | 9fab0da | 2011-07-09 17:52:31 -0700 | [diff] [blame] | 2140 | final ConfirmAppRestrictFragment dialog = new ConfirmAppRestrictFragment(); |
| 2141 | dialog.setTargetFragment(parent, 0); |
| 2142 | dialog.show(parent.getFragmentManager(), TAG_CONFIRM_APP_RESTRICT); |
| 2143 | } |
| 2144 | |
| 2145 | @Override |
| 2146 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 2147 | final Context context = getActivity(); |
| 2148 | |
| 2149 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 2150 | builder.setTitle(R.string.data_usage_app_restrict_dialog_title); |
| 2151 | builder.setMessage(R.string.data_usage_app_restrict_dialog); |
| 2152 | |
| 2153 | builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
Jeff Sharkey | 76c5ed4 | 2012-04-09 10:53:09 -0700 | [diff] [blame] | 2154 | @Override |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 2155 | public void onClick(DialogInterface dialog, int which) { |
| 2156 | final DataUsageSummary target = (DataUsageSummary) getTargetFragment(); |
| 2157 | if (target != null) { |
| 2158 | target.setAppRestrictBackground(true); |
| 2159 | } |
| 2160 | } |
| 2161 | }); |
| 2162 | builder.setNegativeButton(android.R.string.cancel, null); |
| 2163 | |
| 2164 | return builder.create(); |
| 2165 | } |
| 2166 | } |
| 2167 | |
| 2168 | /** |
Amith Yamasani | d1ab828 | 2012-05-18 09:50:08 -0700 | [diff] [blame] | 2169 | * Dialog to inform user about changing auto-sync setting |
| 2170 | */ |
| 2171 | public static class ConfirmAutoSyncChangeFragment extends DialogFragment { |
Amith Yamasani | 665235f | 2012-06-07 19:58:34 -0700 | [diff] [blame] | 2172 | private static final String SAVE_ENABLING = "enabling"; |
Amith Yamasani | d1ab828 | 2012-05-18 09:50:08 -0700 | [diff] [blame] | 2173 | private boolean mEnabling; |
| 2174 | |
| 2175 | public static void show(DataUsageSummary parent, boolean enabling) { |
| 2176 | if (!parent.isAdded()) return; |
| 2177 | |
| 2178 | final ConfirmAutoSyncChangeFragment dialog = new ConfirmAutoSyncChangeFragment(); |
| 2179 | dialog.mEnabling = enabling; |
| 2180 | dialog.setTargetFragment(parent, 0); |
| 2181 | dialog.show(parent.getFragmentManager(), TAG_CONFIRM_AUTO_SYNC_CHANGE); |
| 2182 | } |
| 2183 | |
| 2184 | @Override |
| 2185 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 2186 | final Context context = getActivity(); |
Amith Yamasani | 665235f | 2012-06-07 19:58:34 -0700 | [diff] [blame] | 2187 | if (savedInstanceState != null) { |
| 2188 | mEnabling = savedInstanceState.getBoolean(SAVE_ENABLING); |
| 2189 | } |
Amith Yamasani | d1ab828 | 2012-05-18 09:50:08 -0700 | [diff] [blame] | 2190 | |
| 2191 | final AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 2192 | if (!mEnabling) { |
| 2193 | builder.setTitle(R.string.data_usage_auto_sync_off_dialog_title); |
| 2194 | builder.setMessage(R.string.data_usage_auto_sync_off_dialog); |
| 2195 | } else { |
| 2196 | builder.setTitle(R.string.data_usage_auto_sync_on_dialog_title); |
| 2197 | builder.setMessage(R.string.data_usage_auto_sync_on_dialog); |
| 2198 | } |
| 2199 | |
| 2200 | builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
| 2201 | @Override |
| 2202 | public void onClick(DialogInterface dialog, int which) { |
| 2203 | ContentResolver.setMasterSyncAutomatically(mEnabling); |
| 2204 | } |
| 2205 | }); |
| 2206 | builder.setNegativeButton(android.R.string.cancel, null); |
| 2207 | |
| 2208 | return builder.create(); |
| 2209 | } |
Amith Yamasani | 665235f | 2012-06-07 19:58:34 -0700 | [diff] [blame] | 2210 | |
| 2211 | @Override |
| 2212 | public void onSaveInstanceState(Bundle outState) { |
| 2213 | super.onSaveInstanceState(outState); |
| 2214 | outState.putBoolean(SAVE_ENABLING, mEnabling); |
| 2215 | } |
Amith Yamasani | d1ab828 | 2012-05-18 09:50:08 -0700 | [diff] [blame] | 2216 | } |
| 2217 | |
| 2218 | /** |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 2219 | * Compute default tab that should be selected, based on |
| 2220 | * {@link NetworkPolicyManager#EXTRA_NETWORK_TEMPLATE} extra. |
| 2221 | */ |
| 2222 | private static String computeTabFromIntent(Intent intent) { |
Jeff Sharkey | 271ec8a | 2011-07-20 16:59:16 -0700 | [diff] [blame] | 2223 | final NetworkTemplate template = intent.getParcelableExtra(EXTRA_NETWORK_TEMPLATE); |
| 2224 | if (template == null) return null; |
| 2225 | |
| 2226 | switch (template.getMatchRule()) { |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 2227 | case MATCH_MOBILE_3G_LOWER: |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 2228 | return TAB_3G; |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 2229 | case MATCH_MOBILE_4G: |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 2230 | return TAB_4G; |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 2231 | case MATCH_MOBILE_ALL: |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 2232 | return TAB_MOBILE; |
Jeff Sharkey | a662e49 | 2011-06-18 21:57:06 -0700 | [diff] [blame] | 2233 | case MATCH_WIFI: |
Jeff Sharkey | dd6efe1 | 2011-06-15 10:31:41 -0700 | [diff] [blame] | 2234 | return TAB_WIFI; |
| 2235 | default: |
| 2236 | return null; |
| 2237 | } |
| 2238 | } |
| 2239 | |
| 2240 | /** |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2241 | * Background task that loads {@link UidDetail}, binding to |
| 2242 | * {@link DataUsageAdapter} row item when finished. |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 2243 | */ |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2244 | private static class UidDetailTask extends AsyncTask<Void, Void, UidDetail> { |
| 2245 | private final UidDetailProvider mProvider; |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 2246 | private final AppItem mItem; |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2247 | private final View mTarget; |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 2248 | |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 2249 | private UidDetailTask(UidDetailProvider provider, AppItem item, View target) { |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2250 | mProvider = checkNotNull(provider); |
| 2251 | mItem = checkNotNull(item); |
| 2252 | mTarget = checkNotNull(target); |
Jeff Sharkey | 518bc9d | 2011-07-12 20:20:46 -0700 | [diff] [blame] | 2253 | } |
| 2254 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2255 | public static void bindView( |
Jeff Sharkey | ef6e1ff | 2012-03-21 17:09:07 -0700 | [diff] [blame] | 2256 | UidDetailProvider provider, AppItem item, View target) { |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2257 | final UidDetailTask existing = (UidDetailTask) target.getTag(); |
| 2258 | if (existing != null) { |
| 2259 | existing.cancel(false); |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 2260 | } |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2261 | |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 2262 | final UidDetail cachedDetail = provider.getUidDetail(item.key, false); |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2263 | if (cachedDetail != null) { |
| 2264 | bindView(cachedDetail, target); |
| 2265 | } else { |
| 2266 | target.setTag(new UidDetailTask(provider, item, target).executeOnExecutor( |
| 2267 | AsyncTask.THREAD_POOL_EXECUTOR)); |
| 2268 | } |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 2269 | } |
| 2270 | |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2271 | private static void bindView(UidDetail detail, View target) { |
| 2272 | final ImageView icon = (ImageView) target.findViewById(android.R.id.icon); |
| 2273 | final TextView title = (TextView) target.findViewById(android.R.id.title); |
| 2274 | |
| 2275 | if (detail != null) { |
| 2276 | icon.setImageDrawable(detail.icon); |
| 2277 | title.setText(detail.label); |
| 2278 | } else { |
| 2279 | icon.setImageDrawable(null); |
| 2280 | title.setText(null); |
| 2281 | } |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 2282 | } |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2283 | |
| 2284 | @Override |
| 2285 | protected void onPreExecute() { |
| 2286 | bindView(null, mTarget); |
| 2287 | } |
| 2288 | |
| 2289 | @Override |
| 2290 | protected UidDetail doInBackground(Void... params) { |
Jeff Sharkey | 38305fb | 2012-09-14 16:26:51 -0700 | [diff] [blame] | 2291 | return mProvider.getUidDetail(mItem.key, true); |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2292 | } |
| 2293 | |
| 2294 | @Override |
| 2295 | protected void onPostExecute(UidDetail result) { |
| 2296 | bindView(result, mTarget); |
| 2297 | } |
Jeff Sharkey | 8e911d7 | 2011-06-14 22:41:21 -0700 | [diff] [blame] | 2298 | } |
| 2299 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 2300 | /** |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 2301 | * Test if device has a mobile data radio with SIM in ready state. |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 2302 | */ |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 2303 | public static boolean hasReadyMobileRadio(Context context) { |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 2304 | if (TEST_RADIOS) { |
| 2305 | return SystemProperties.get(TEST_RADIOS_PROP).contains("mobile"); |
| 2306 | } |
| 2307 | |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 2308 | final ConnectivityManager conn = ConnectivityManager.from(context); |
| 2309 | final TelephonyManager tele = TelephonyManager.from(context); |
| 2310 | |
| 2311 | // require both supported network and ready SIM |
| 2312 | return conn.isNetworkSupported(TYPE_MOBILE) && tele.getSimState() == SIM_STATE_READY; |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 2313 | } |
| 2314 | |
| 2315 | /** |
| 2316 | * Test if device has a mobile 4G data radio. |
| 2317 | */ |
Jeff Sharkey | ad17de3 | 2012-04-11 11:03:25 -0700 | [diff] [blame] | 2318 | public static boolean hasReadyMobile4gRadio(Context context) { |
Jeff Sharkey | b98c55b | 2011-09-11 17:29:49 -0700 | [diff] [blame] | 2319 | if (!NetworkPolicyEditor.ENABLE_SPLIT_POLICIES) { |
| 2320 | return false; |
| 2321 | } |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 2322 | if (TEST_RADIOS) { |
| 2323 | return SystemProperties.get(TEST_RADIOS_PROP).contains("4g"); |
| 2324 | } |
| 2325 | |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 2326 | final ConnectivityManager conn = ConnectivityManager.from(context); |
| 2327 | final TelephonyManager tele = TelephonyManager.from(context); |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 2328 | |
Jeff Sharkey | bdf98e8 | 2011-11-10 17:17:24 -0800 | [diff] [blame] | 2329 | final boolean hasWimax = conn.isNetworkSupported(TYPE_WIMAX); |
Wink Saville | 5543404 | 2012-06-14 12:33:43 -0700 | [diff] [blame] | 2330 | final boolean hasLte = (tele.getLteOnCdmaMode() == PhoneConstants.LTE_ON_CDMA_TRUE) |
Jeff Sharkey | ad17de3 | 2012-04-11 11:03:25 -0700 | [diff] [blame] | 2331 | && hasReadyMobileRadio(context); |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 2332 | return hasWimax || hasLte; |
| 2333 | } |
| 2334 | |
| 2335 | /** |
| 2336 | * Test if device has a Wi-Fi data radio. |
| 2337 | */ |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 2338 | public static boolean hasWifiRadio(Context context) { |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 2339 | if (TEST_RADIOS) { |
| 2340 | return SystemProperties.get(TEST_RADIOS_PROP).contains("wifi"); |
| 2341 | } |
| 2342 | |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 2343 | final ConnectivityManager conn = ConnectivityManager.from(context); |
Jeff Sharkey | bdf98e8 | 2011-11-10 17:17:24 -0800 | [diff] [blame] | 2344 | return conn.isNetworkSupported(TYPE_WIFI); |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 2345 | } |
| 2346 | |
| 2347 | /** |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 2348 | * Test if device has an ethernet network connection. |
| 2349 | */ |
Jeff Sharkey | b67c4a8 | 2012-05-24 11:01:29 -0700 | [diff] [blame] | 2350 | public boolean hasEthernet(Context context) { |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 2351 | if (TEST_RADIOS) { |
| 2352 | return SystemProperties.get(TEST_RADIOS_PROP).contains("ethernet"); |
| 2353 | } |
| 2354 | |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 2355 | final ConnectivityManager conn = ConnectivityManager.from(context); |
Jeff Sharkey | b67c4a8 | 2012-05-24 11:01:29 -0700 | [diff] [blame] | 2356 | final boolean hasEthernet = conn.isNetworkSupported(TYPE_ETHERNET); |
| 2357 | |
| 2358 | final long ethernetBytes; |
Jeff Sharkey | d878909 | 2012-05-29 10:19:50 -0700 | [diff] [blame] | 2359 | if (mStatsSession != null) { |
| 2360 | try { |
| 2361 | ethernetBytes = mStatsSession.getSummaryForNetwork( |
| 2362 | NetworkTemplate.buildTemplateEthernet(), Long.MIN_VALUE, Long.MAX_VALUE) |
| 2363 | .getTotalBytes(); |
| 2364 | } catch (RemoteException e) { |
| 2365 | throw new RuntimeException(e); |
| 2366 | } |
| 2367 | } else { |
| 2368 | ethernetBytes = 0; |
Jeff Sharkey | b67c4a8 | 2012-05-24 11:01:29 -0700 | [diff] [blame] | 2369 | } |
| 2370 | |
Jeff Sharkey | d878909 | 2012-05-29 10:19:50 -0700 | [diff] [blame] | 2371 | // only show ethernet when both hardware present and traffic has occurred |
Jeff Sharkey | b67c4a8 | 2012-05-24 11:01:29 -0700 | [diff] [blame] | 2372 | return hasEthernet && ethernetBytes > 0; |
Jeff Sharkey | 9549e9f | 2011-07-14 20:01:13 -0700 | [diff] [blame] | 2373 | } |
| 2374 | |
| 2375 | /** |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 2376 | * Inflate a {@link Preference} style layout, adding the given {@link View} |
| 2377 | * widget into {@link android.R.id#widget_frame}. |
| 2378 | */ |
| 2379 | private static View inflatePreference(LayoutInflater inflater, ViewGroup root, View widget) { |
| 2380 | final View view = inflater.inflate(R.layout.preference, root, false); |
| 2381 | final LinearLayout widgetFrame = (LinearLayout) view.findViewById( |
| 2382 | android.R.id.widget_frame); |
| 2383 | widgetFrame.addView(widget, new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT)); |
| 2384 | return view; |
| 2385 | } |
| 2386 | |
Jeff Sharkey | b654846 | 2014-07-21 15:38:06 -0700 | [diff] [blame] | 2387 | private static View inflateCategoryHeader(LayoutInflater inflater, ViewGroup root) { |
| 2388 | final TypedArray a = inflater.getContext().obtainStyledAttributes(null, |
| 2389 | com.android.internal.R.styleable.Preference, |
| 2390 | com.android.internal.R.attr.preferenceCategoryStyle, 0); |
| 2391 | final int resId = a.getResourceId(com.android.internal.R.styleable.Preference_layout, 0); |
| 2392 | return inflater.inflate(resId, root, false); |
Jeff Sharkey | d39c6e4 | 2011-08-04 21:17:23 -0700 | [diff] [blame] | 2393 | } |
| 2394 | |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 2395 | /** |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 2396 | * Test if any networks are currently limited. |
| 2397 | */ |
| 2398 | private boolean hasLimitedNetworks() { |
| 2399 | return !buildLimitedNetworksList().isEmpty(); |
| 2400 | } |
| 2401 | |
| 2402 | /** |
Jeff Sharkey | 30dde0f | 2011-08-17 15:08:12 -0700 | [diff] [blame] | 2403 | * Build string describing currently limited networks, which defines when |
| 2404 | * background data is restricted. |
| 2405 | */ |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 2406 | @Deprecated |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 2407 | private CharSequence buildLimitedNetworksString() { |
| 2408 | final List<CharSequence> limited = buildLimitedNetworksList(); |
| 2409 | |
| 2410 | // handle case where no networks limited |
| 2411 | if (limited.isEmpty()) { |
| 2412 | limited.add(getText(R.string.data_usage_list_none)); |
| 2413 | } |
| 2414 | |
| 2415 | return TextUtils.join(limited); |
| 2416 | } |
| 2417 | |
| 2418 | /** |
| 2419 | * Build list of currently limited networks, which defines when background |
| 2420 | * data is restricted. |
| 2421 | */ |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 2422 | @Deprecated |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 2423 | private List<CharSequence> buildLimitedNetworksList() { |
Jeff Sharkey | 30dde0f | 2011-08-17 15:08:12 -0700 | [diff] [blame] | 2424 | final Context context = getActivity(); |
Jeff Sharkey | 30dde0f | 2011-08-17 15:08:12 -0700 | [diff] [blame] | 2425 | |
| 2426 | // build combined list of all limited networks |
| 2427 | final ArrayList<CharSequence> limited = Lists.newArrayList(); |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 2428 | |
| 2429 | final TelephonyManager tele = TelephonyManager.from(context); |
| 2430 | if (tele.getSimState() == SIM_STATE_READY) { |
| 2431 | final String subscriberId = getActiveSubscriberId(context); |
| 2432 | if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobileAll(subscriberId))) { |
| 2433 | limited.add(getText(R.string.data_usage_list_mobile)); |
| 2434 | } |
| 2435 | if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile3gLower(subscriberId))) { |
| 2436 | limited.add(getText(R.string.data_usage_tab_3g)); |
| 2437 | } |
| 2438 | if (mPolicyEditor.hasLimitedPolicy(buildTemplateMobile4g(subscriberId))) { |
| 2439 | limited.add(getText(R.string.data_usage_tab_4g)); |
| 2440 | } |
Jeff Sharkey | 30dde0f | 2011-08-17 15:08:12 -0700 | [diff] [blame] | 2441 | } |
Jeff Sharkey | 313f7d8 | 2012-04-03 21:13:25 -0700 | [diff] [blame] | 2442 | |
| 2443 | if (mPolicyEditor.hasLimitedPolicy(buildTemplateWifiWildcard())) { |
Jeff Sharkey | 30dde0f | 2011-08-17 15:08:12 -0700 | [diff] [blame] | 2444 | limited.add(getText(R.string.data_usage_tab_wifi)); |
| 2445 | } |
| 2446 | if (mPolicyEditor.hasLimitedPolicy(buildTemplateEthernet())) { |
| 2447 | limited.add(getText(R.string.data_usage_tab_ethernet)); |
| 2448 | } |
| 2449 | |
Jeff Sharkey | a53188f | 2011-09-13 19:56:45 -0700 | [diff] [blame] | 2450 | return limited; |
Jeff Sharkey | 30dde0f | 2011-08-17 15:08:12 -0700 | [diff] [blame] | 2451 | } |
| 2452 | |
| 2453 | /** |
Jeff Sharkey | 5d70679 | 2011-09-08 18:57:17 -0700 | [diff] [blame] | 2454 | * Inset both selector and divider {@link Drawable} on the given |
| 2455 | * {@link ListView} by the requested dimensions. |
| 2456 | */ |
| 2457 | private static void insetListViewDrawables(ListView view, int insetSide) { |
| 2458 | final Drawable selector = view.getSelector(); |
| 2459 | final Drawable divider = view.getDivider(); |
| 2460 | |
| 2461 | // fully unregister these drawables so callbacks can be maintained after |
| 2462 | // wrapping below. |
| 2463 | final Drawable stub = new ColorDrawable(Color.TRANSPARENT); |
| 2464 | view.setSelector(stub); |
| 2465 | view.setDivider(stub); |
| 2466 | |
| 2467 | view.setSelector(new InsetBoundsDrawable(selector, insetSide)); |
| 2468 | view.setDivider(new InsetBoundsDrawable(divider, insetSide)); |
| 2469 | } |
| 2470 | |
| 2471 | /** |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 2472 | * Set {@link android.R.id#title} for a preference view inflated with |
Jeff Sharkey | 52c3f44 | 2011-06-23 00:39:38 -0700 | [diff] [blame] | 2473 | * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}. |
Jeff Sharkey | 29d56b3 | 2011-06-20 17:06:52 -0700 | [diff] [blame] | 2474 | */ |
| 2475 | private static void setPreferenceTitle(View parent, int resId) { |
| 2476 | final TextView title = (TextView) parent.findViewById(android.R.id.title); |
| 2477 | title.setText(resId); |
| 2478 | } |
| 2479 | |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 2480 | /** |
| 2481 | * Set {@link android.R.id#summary} for a preference view inflated with |
| 2482 | * {@link #inflatePreference(LayoutInflater, ViewGroup, View)}. |
| 2483 | */ |
Jeff Sharkey | 30dde0f | 2011-08-17 15:08:12 -0700 | [diff] [blame] | 2484 | private static void setPreferenceSummary(View parent, CharSequence string) { |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 2485 | final TextView summary = (TextView) parent.findViewById(android.R.id.summary); |
| 2486 | summary.setVisibility(View.VISIBLE); |
Jeff Sharkey | 30dde0f | 2011-08-17 15:08:12 -0700 | [diff] [blame] | 2487 | summary.setText(string); |
Jeff Sharkey | f54f435 | 2011-06-23 22:15:54 -0700 | [diff] [blame] | 2488 | } |
Fabrice Di Meglio | 758c3ff | 2014-04-10 13:47:30 -0700 | [diff] [blame] | 2489 | |
| 2490 | /** |
| 2491 | * For search |
| 2492 | */ |
| 2493 | public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = |
Fabrice Di Meglio | 45f754e | 2014-04-10 19:25:42 -0700 | [diff] [blame] | 2494 | new BaseSearchIndexProvider() { |
Fabrice Di Meglio | 758c3ff | 2014-04-10 13:47:30 -0700 | [diff] [blame] | 2495 | @Override |
| 2496 | public List<SearchIndexableRaw> getRawDataToIndex(Context context, boolean enabled) { |
| 2497 | final List<SearchIndexableRaw> result = new ArrayList<SearchIndexableRaw>(); |
| 2498 | |
| 2499 | final Resources res = context.getResources(); |
| 2500 | |
| 2501 | // Add fragment title |
| 2502 | SearchIndexableRaw data = new SearchIndexableRaw(context); |
| 2503 | data.title = res.getString(R.string.data_usage_summary_title); |
| 2504 | data.screenTitle = res.getString(R.string.data_usage_summary_title); |
| 2505 | result.add(data); |
| 2506 | |
| 2507 | // Mobile data |
| 2508 | data = new SearchIndexableRaw(context); |
Fabrice Di Meglio | 2169c88 | 2014-04-18 15:18:40 -0700 | [diff] [blame] | 2509 | data.key = DATA_USAGE_ENABLE_MOBILE_KEY; |
Fabrice Di Meglio | 758c3ff | 2014-04-10 13:47:30 -0700 | [diff] [blame] | 2510 | data.title = res.getString(R.string.data_usage_enable_mobile); |
| 2511 | data.screenTitle = res.getString(R.string.data_usage_summary_title); |
| 2512 | result.add(data); |
| 2513 | |
| 2514 | // Set mobile data limit |
| 2515 | data = new SearchIndexableRaw(context); |
Fabrice Di Meglio | 2169c88 | 2014-04-18 15:18:40 -0700 | [diff] [blame] | 2516 | data.key = DATA_USAGE_DISABLE_MOBILE_LIMIT_KEY; |
Fabrice Di Meglio | 758c3ff | 2014-04-10 13:47:30 -0700 | [diff] [blame] | 2517 | data.title = res.getString(R.string.data_usage_disable_mobile_limit); |
| 2518 | data.screenTitle = res.getString(R.string.data_usage_summary_title); |
| 2519 | result.add(data); |
| 2520 | |
Fabrice Di Meglio | f2a5226 | 2014-04-17 17:20:27 -0700 | [diff] [blame] | 2521 | // Data usage cycle |
Fabrice Di Meglio | 758c3ff | 2014-04-10 13:47:30 -0700 | [diff] [blame] | 2522 | data = new SearchIndexableRaw(context); |
Fabrice Di Meglio | 2169c88 | 2014-04-18 15:18:40 -0700 | [diff] [blame] | 2523 | data.key = DATA_USAGE_CYCLE_KEY; |
Fabrice Di Meglio | 758c3ff | 2014-04-10 13:47:30 -0700 | [diff] [blame] | 2524 | data.title = res.getString(R.string.data_usage_cycle); |
| 2525 | data.screenTitle = res.getString(R.string.data_usage_summary_title); |
| 2526 | result.add(data); |
| 2527 | |
| 2528 | return result; |
| 2529 | } |
| 2530 | }; |
| 2531 | |
Jeff Sharkey | ab2d8d3 | 2011-05-30 16:19:56 -0700 | [diff] [blame] | 2532 | } |