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