John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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.systemui.qs.tiles; |
| 18 | |
| 19 | import android.content.ComponentName; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.res.Resources; |
John Spurlock | b98f747 | 2014-07-08 17:09:42 -0400 | [diff] [blame] | 23 | import android.view.LayoutInflater; |
| 24 | import android.view.View; |
| 25 | import android.view.ViewGroup; |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 26 | |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 27 | import com.android.internal.logging.MetricsLogger; |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 28 | import com.android.internal.logging.MetricsProto.MetricsEvent; |
Jason Monk | f668d7cc | 2016-01-14 10:38:41 -0500 | [diff] [blame] | 29 | import com.android.settingslib.net.DataUsageController; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 30 | import com.android.systemui.R; |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 31 | import com.android.systemui.qs.QSIconView; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 32 | import com.android.systemui.qs.QSTile; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 33 | import com.android.systemui.qs.SignalTileView; |
| 34 | import com.android.systemui.statusbar.policy.NetworkController; |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 35 | import com.android.systemui.statusbar.policy.NetworkController.IconState; |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 36 | import com.android.systemui.statusbar.policy.SignalCallbackAdapter; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 37 | |
| 38 | /** Quick settings tile: Cellular **/ |
| 39 | public class CellularTile extends QSTile<QSTile.SignalState> { |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 40 | static final Intent CELLULAR_SETTINGS = new Intent().setComponent(new ComponentName( |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 41 | "com.android.settings", "com.android.settings.Settings$DataUsageSummaryActivity")); |
| 42 | |
| 43 | private final NetworkController mController; |
Jason Monk | f668d7cc | 2016-01-14 10:38:41 -0500 | [diff] [blame] | 44 | private final DataUsageController mDataController; |
John Spurlock | b98f747 | 2014-07-08 17:09:42 -0400 | [diff] [blame] | 45 | private final CellularDetailAdapter mDetailAdapter; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 46 | |
Jason Monk | 878c093 | 2015-07-21 10:18:01 -0400 | [diff] [blame] | 47 | private final CellSignalCallback mSignalCallback = new CellSignalCallback(); |
| 48 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 49 | public CellularTile(Host host) { |
| 50 | super(host); |
| 51 | mController = host.getNetworkController(); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 52 | mDataController = mController.getMobileDataController(); |
John Spurlock | b98f747 | 2014-07-08 17:09:42 -0400 | [diff] [blame] | 53 | mDetailAdapter = new CellularDetailAdapter(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | @Override |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 57 | public SignalState newTileState() { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 58 | return new SignalState(); |
| 59 | } |
| 60 | |
| 61 | @Override |
John Spurlock | b98f747 | 2014-07-08 17:09:42 -0400 | [diff] [blame] | 62 | public DetailAdapter getDetailAdapter() { |
| 63 | return mDetailAdapter; |
| 64 | } |
| 65 | |
| 66 | @Override |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 67 | public void setListening(boolean listening) { |
| 68 | if (listening) { |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 69 | mController.addSignalCallback(mSignalCallback); |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 70 | } else { |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 71 | mController.removeSignalCallback(mSignalCallback); |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 72 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Override |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 76 | public QSIconView createTileView(Context context) { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 77 | return new SignalTileView(context); |
| 78 | } |
| 79 | |
| 80 | @Override |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 81 | public Intent getLongClickIntent() { |
| 82 | return CELLULAR_SETTINGS; |
| 83 | } |
| 84 | |
| 85 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 86 | protected void handleClick() { |
Chris Wren | 9e7283f | 2015-05-08 17:23:47 -0400 | [diff] [blame] | 87 | MetricsLogger.action(mContext, getMetricsCategory()); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 88 | if (mDataController.isMobileDataSupported()) { |
John Spurlock | b98f747 | 2014-07-08 17:09:42 -0400 | [diff] [blame] | 89 | showDetail(true); |
| 90 | } else { |
Jason Monk | ee43cdf | 2015-06-19 14:20:46 -0400 | [diff] [blame] | 91 | mHost.startActivityDismissingKeyguard(CELLULAR_SETTINGS); |
John Spurlock | b98f747 | 2014-07-08 17:09:42 -0400 | [diff] [blame] | 92 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Override |
Jason Monk | deba7a4 | 2015-12-08 16:14:10 -0500 | [diff] [blame] | 96 | protected void handleSecondaryClick() { |
| 97 | boolean dataEnabled = mDataController.isMobileDataSupported() |
| 98 | && mDataController.isMobileDataEnabled(); |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 99 | MetricsLogger.action(mContext, MetricsEvent.QS_CELLULAR_TOGGLE, !dataEnabled); |
Jason Monk | deba7a4 | 2015-12-08 16:14:10 -0500 | [diff] [blame] | 100 | mDataController.setMobileDataEnabled(!dataEnabled); |
| 101 | } |
| 102 | |
| 103 | @Override |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 104 | public CharSequence getTileLabel() { |
| 105 | return mContext.getString(R.string.quick_settings_cellular_detail_title); |
| 106 | } |
| 107 | |
| 108 | @Override |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 109 | protected void handleUpdateState(SignalState state, Object arg) { |
Jason Monk | 878c093 | 2015-07-21 10:18:01 -0400 | [diff] [blame] | 110 | CallbackInfo cb = (CallbackInfo) arg; |
| 111 | if (cb == null) { |
| 112 | cb = mSignalCallback.mInfo; |
| 113 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 114 | |
| 115 | final Resources r = mContext.getResources(); |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 116 | final int iconId = cb.noSim ? R.drawable.ic_qs_no_sim |
John Spurlock | c1d48a5 | 2014-08-01 12:12:32 -0400 | [diff] [blame] | 117 | : !cb.enabled || cb.airplaneModeEnabled ? R.drawable.ic_qs_signal_disabled |
| 118 | : cb.mobileSignalIconId > 0 ? cb.mobileSignalIconId |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 119 | : R.drawable.ic_qs_signal_no_signal; |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 120 | state.icon = ResourceIcon.get(iconId); |
Jorim Jaggi | c737b9b | 2014-09-08 23:57:20 +0200 | [diff] [blame] | 121 | state.isOverlayIconWide = cb.isDataTypeIconWide; |
Selim Cinek | 06d3bca | 2014-08-26 17:29:20 +0200 | [diff] [blame] | 122 | state.autoMirrorDrawable = !cb.noSim; |
Jeff Davidson | ccda996 | 2015-01-26 18:23:24 -0800 | [diff] [blame] | 123 | state.overlayIconId = cb.enabled && (cb.dataTypeIconId > 0) ? cb.dataTypeIconId : 0; |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 124 | state.filter = iconId != R.drawable.ic_qs_no_sim; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 125 | state.activityIn = cb.enabled && cb.activityIn; |
| 126 | state.activityOut = cb.enabled && cb.activityOut; |
| 127 | |
| 128 | state.label = cb.enabled |
| 129 | ? removeTrailingPeriod(cb.enabledDesc) |
| 130 | : r.getString(R.string.quick_settings_rssi_emergency_only); |
| 131 | |
| 132 | final String signalContentDesc = cb.enabled && (cb.mobileSignalIconId > 0) |
| 133 | ? cb.signalContentDescription |
| 134 | : r.getString(R.string.accessibility_no_signal); |
| 135 | final String dataContentDesc = cb.enabled && (cb.dataTypeIconId > 0) && !cb.wifiEnabled |
| 136 | ? cb.dataContentDescription |
| 137 | : r.getString(R.string.accessibility_no_data); |
| 138 | state.contentDescription = r.getString( |
| 139 | R.string.accessibility_quick_settings_mobile, |
| 140 | signalContentDesc, dataContentDesc, |
| 141 | state.label); |
| 142 | } |
| 143 | |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 144 | @Override |
| 145 | public int getMetricsCategory() { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 146 | return MetricsEvent.QS_CELLULAR; |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 147 | } |
| 148 | |
Jason Monk | c3f42c1 | 2016-02-05 12:33:13 -0500 | [diff] [blame] | 149 | @Override |
| 150 | public boolean isAvailable() { |
| 151 | return mController.hasMobileDataFeature(); |
| 152 | } |
| 153 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 154 | // Remove the period from the network name |
| 155 | public static String removeTrailingPeriod(String string) { |
| 156 | if (string == null) return null; |
| 157 | final int length = string.length(); |
| 158 | if (string.endsWith(".")) { |
| 159 | return string.substring(0, length - 1); |
| 160 | } |
| 161 | return string; |
| 162 | } |
| 163 | |
| 164 | private static final class CallbackInfo { |
| 165 | boolean enabled; |
| 166 | boolean wifiEnabled; |
John Spurlock | d5cd2fd | 2014-07-27 11:36:33 -0400 | [diff] [blame] | 167 | boolean airplaneModeEnabled; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 168 | int mobileSignalIconId; |
| 169 | String signalContentDescription; |
| 170 | int dataTypeIconId; |
| 171 | String dataContentDescription; |
| 172 | boolean activityIn; |
| 173 | boolean activityOut; |
| 174 | String enabledDesc; |
John Spurlock | 899f439 | 2014-06-11 10:59:11 -0400 | [diff] [blame] | 175 | boolean noSim; |
Jorim Jaggi | c737b9b | 2014-09-08 23:57:20 +0200 | [diff] [blame] | 176 | boolean isDataTypeIconWide; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 177 | } |
| 178 | |
Jason Monk | 878c093 | 2015-07-21 10:18:01 -0400 | [diff] [blame] | 179 | private final class CellSignalCallback extends SignalCallbackAdapter { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 180 | private final CallbackInfo mInfo = new CallbackInfo(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 181 | @Override |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 182 | public void setWifiIndicators(boolean enabled, IconState statusIcon, IconState qsIcon, |
| 183 | boolean activityIn, boolean activityOut, String description) { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 184 | mInfo.wifiEnabled = enabled; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 185 | refreshState(mInfo); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Override |
Andrew Flynn | 2fdbe12 | 2015-06-01 16:34:21 -0400 | [diff] [blame] | 189 | public void setMobileDataIndicators(IconState statusIcon, IconState qsIcon, int statusType, |
| 190 | int qsType, boolean activityIn, boolean activityOut, String typeContentDescription, |
| 191 | String description, boolean isWide, int subId) { |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 192 | if (qsIcon == null) { |
| 193 | // Not data sim, don't display. |
| 194 | return; |
| 195 | } |
| 196 | mInfo.enabled = qsIcon.visible; |
| 197 | mInfo.mobileSignalIconId = qsIcon.icon; |
| 198 | mInfo.signalContentDescription = qsIcon.contentDescription; |
| 199 | mInfo.dataTypeIconId = qsType; |
| 200 | mInfo.dataContentDescription = typeContentDescription; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 201 | mInfo.activityIn = activityIn; |
| 202 | mInfo.activityOut = activityOut; |
| 203 | mInfo.enabledDesc = description; |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 204 | mInfo.isDataTypeIconWide = qsType != 0 && isWide; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 205 | refreshState(mInfo); |
| 206 | } |
| 207 | |
| 208 | @Override |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 209 | public void setNoSims(boolean show) { |
| 210 | mInfo.noSim = show; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 211 | if (mInfo.noSim) { |
| 212 | // Make sure signal gets cleared out when no sims. |
| 213 | mInfo.mobileSignalIconId = 0; |
| 214 | mInfo.dataTypeIconId = 0; |
Jason Monk | b36becf | 2015-01-08 11:12:39 -0500 | [diff] [blame] | 215 | // Show a No SIMs description to avoid emergency calls message. |
| 216 | mInfo.enabled = true; |
| 217 | mInfo.enabledDesc = mContext.getString( |
| 218 | R.string.keyguard_missing_sim_message_short); |
| 219 | mInfo.signalContentDescription = mInfo.enabledDesc; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 220 | } |
| 221 | refreshState(mInfo); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | @Override |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 225 | public void setIsAirplaneMode(IconState icon) { |
| 226 | mInfo.airplaneModeEnabled = icon.visible; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 227 | refreshState(mInfo); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 228 | } |
John Spurlock | b98f747 | 2014-07-08 17:09:42 -0400 | [diff] [blame] | 229 | |
Jason Monk | 07b75fe | 2015-05-14 16:47:03 -0400 | [diff] [blame] | 230 | @Override |
| 231 | public void setMobileDataEnabled(boolean enabled) { |
John Spurlock | b98f747 | 2014-07-08 17:09:42 -0400 | [diff] [blame] | 232 | mDetailAdapter.setMobileDataEnabled(enabled); |
| 233 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 234 | }; |
John Spurlock | b98f747 | 2014-07-08 17:09:42 -0400 | [diff] [blame] | 235 | |
| 236 | private final class CellularDetailAdapter implements DetailAdapter { |
John Spurlock | b98f747 | 2014-07-08 17:09:42 -0400 | [diff] [blame] | 237 | |
| 238 | @Override |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 239 | public CharSequence getTitle() { |
| 240 | return mContext.getString(R.string.quick_settings_cellular_detail_title); |
John Spurlock | b98f747 | 2014-07-08 17:09:42 -0400 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | @Override |
| 244 | public Boolean getToggleState() { |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 245 | return mDataController.isMobileDataSupported() |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 246 | ? mDataController.isMobileDataEnabled() |
| 247 | : null; |
John Spurlock | b98f747 | 2014-07-08 17:09:42 -0400 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public Intent getSettingsIntent() { |
| 252 | return CELLULAR_SETTINGS; |
| 253 | } |
| 254 | |
| 255 | @Override |
| 256 | public void setToggleState(boolean state) { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 257 | MetricsLogger.action(mContext, MetricsEvent.QS_CELLULAR_TOGGLE, state); |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 258 | mDataController.setMobileDataEnabled(state); |
John Spurlock | b98f747 | 2014-07-08 17:09:42 -0400 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | @Override |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 262 | public int getMetricsCategory() { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 263 | return MetricsEvent.QS_DATAUSAGEDETAIL; |
Chris Wren | 457a21c | 2015-05-06 17:50:34 -0400 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | @Override |
John Spurlock | b98f747 | 2014-07-08 17:09:42 -0400 | [diff] [blame] | 267 | public View createDetailView(Context context, View convertView, ViewGroup parent) { |
Jorim Jaggi | e17c4b4 | 2014-08-26 17:27:31 +0200 | [diff] [blame] | 268 | final DataUsageDetailView v = (DataUsageDetailView) (convertView != null |
| 269 | ? convertView |
| 270 | : LayoutInflater.from(mContext).inflate(R.layout.data_usage, parent, false)); |
Jason Monk | f668d7cc | 2016-01-14 10:38:41 -0500 | [diff] [blame] | 271 | final DataUsageController.DataUsageInfo info = mDataController.getDataUsageInfo(); |
John Spurlock | b98f747 | 2014-07-08 17:09:42 -0400 | [diff] [blame] | 272 | if (info == null) return v; |
Jorim Jaggi | e17c4b4 | 2014-08-26 17:27:31 +0200 | [diff] [blame] | 273 | v.bind(info); |
John Spurlock | b98f747 | 2014-07-08 17:09:42 -0400 | [diff] [blame] | 274 | return v; |
| 275 | } |
| 276 | |
| 277 | public void setMobileDataEnabled(boolean enabled) { |
| 278 | fireToggleStateChanged(enabled); |
| 279 | } |
John Spurlock | b98f747 | 2014-07-08 17:09:42 -0400 | [diff] [blame] | 280 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 281 | } |