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