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