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