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