blob: f3ad9d80339499caa79deaf968488addd9aa7205 [file] [log] [blame]
John Spurlockaf8d6c42014-05-07 17:49:08 -04001/*
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
17package com.android.systemui.qs.tiles;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.res.Resources;
John Spurlockb98f7472014-07-08 17:09:42 -040023import android.view.LayoutInflater;
24import android.view.View;
25import android.view.ViewGroup;
John Spurlockaf8d6c42014-05-07 17:49:08 -040026
Chris Wren457a21c2015-05-06 17:50:34 -040027import com.android.internal.logging.MetricsLogger;
John Spurlockaf8d6c42014-05-07 17:49:08 -040028import com.android.systemui.R;
29import com.android.systemui.qs.QSTile;
30import com.android.systemui.qs.QSTileView;
31import com.android.systemui.qs.SignalTileView;
32import com.android.systemui.statusbar.policy.NetworkController;
Jason Monk07b75fe2015-05-14 16:47:03 -040033import com.android.systemui.statusbar.policy.NetworkController.IconState;
Jason Monkd2263cd2014-11-10 14:22:56 -050034import com.android.systemui.statusbar.policy.NetworkController.MobileDataController;
35import com.android.systemui.statusbar.policy.NetworkController.MobileDataController.DataUsageInfo;
Jason Monk07b75fe2015-05-14 16:47:03 -040036import com.android.systemui.statusbar.policy.NetworkController.SignalCallback;
37import com.android.systemui.statusbar.policy.SignalCallbackAdapter;
John Spurlockaf8d6c42014-05-07 17:49:08 -040038
39/** Quick settings tile: Cellular **/
40public 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 Monkd2263cd2014-11-10 14:22:56 -050045 private final MobileDataController mDataController;
John Spurlockb98f7472014-07-08 17:09:42 -040046 private final CellularDetailAdapter mDetailAdapter;
John Spurlockaf8d6c42014-05-07 17:49:08 -040047
Jason Monk878c0932015-07-21 10:18:01 -040048 private final CellSignalCallback mSignalCallback = new CellSignalCallback();
49
John Spurlockaf8d6c42014-05-07 17:49:08 -040050 public CellularTile(Host host) {
51 super(host);
52 mController = host.getNetworkController();
Jason Monkd2263cd2014-11-10 14:22:56 -050053 mDataController = mController.getMobileDataController();
John Spurlockb98f7472014-07-08 17:09:42 -040054 mDetailAdapter = new CellularDetailAdapter();
John Spurlockaf8d6c42014-05-07 17:49:08 -040055 }
56
57 @Override
58 protected SignalState newTileState() {
59 return new SignalState();
60 }
61
62 @Override
John Spurlockb98f7472014-07-08 17:09:42 -040063 public DetailAdapter getDetailAdapter() {
64 return mDetailAdapter;
65 }
66
67 @Override
John Spurlockccb6b9a2014-05-17 15:54:40 -040068 public void setListening(boolean listening) {
69 if (listening) {
Jason Monk07b75fe2015-05-14 16:47:03 -040070 mController.addSignalCallback(mSignalCallback);
John Spurlockccb6b9a2014-05-17 15:54:40 -040071 } else {
Jason Monk07b75fe2015-05-14 16:47:03 -040072 mController.removeSignalCallback(mSignalCallback);
John Spurlockccb6b9a2014-05-17 15:54:40 -040073 }
John Spurlockaf8d6c42014-05-07 17:49:08 -040074 }
75
76 @Override
77 public QSTileView createTileView(Context context) {
78 return new SignalTileView(context);
79 }
80
81 @Override
82 protected void handleClick() {
Chris Wren9e7283f2015-05-08 17:23:47 -040083 MetricsLogger.action(mContext, getMetricsCategory());
Jason Monkd2263cd2014-11-10 14:22:56 -050084 if (mDataController.isMobileDataSupported()) {
John Spurlockb98f7472014-07-08 17:09:42 -040085 showDetail(true);
86 } else {
Jason Monkee43cdf2015-06-19 14:20:46 -040087 mHost.startActivityDismissingKeyguard(CELLULAR_SETTINGS);
John Spurlockb98f7472014-07-08 17:09:42 -040088 }
John Spurlockaf8d6c42014-05-07 17:49:08 -040089 }
90
91 @Override
92 protected void handleUpdateState(SignalState state, Object arg) {
93 state.visible = mController.hasMobileDataFeature();
94 if (!state.visible) return;
Jason Monk878c0932015-07-21 10:18:01 -040095 CallbackInfo cb = (CallbackInfo) arg;
96 if (cb == null) {
97 cb = mSignalCallback.mInfo;
98 }
John Spurlockaf8d6c42014-05-07 17:49:08 -040099
100 final Resources r = mContext.getResources();
John Spurlock2d695812014-10-30 13:25:21 -0400101 final int iconId = cb.noSim ? R.drawable.ic_qs_no_sim
John Spurlockc1d48a52014-08-01 12:12:32 -0400102 : !cb.enabled || cb.airplaneModeEnabled ? R.drawable.ic_qs_signal_disabled
103 : cb.mobileSignalIconId > 0 ? cb.mobileSignalIconId
John Spurlockaf8d6c42014-05-07 17:49:08 -0400104 : R.drawable.ic_qs_signal_no_signal;
John Spurlock2d695812014-10-30 13:25:21 -0400105 state.icon = ResourceIcon.get(iconId);
Jorim Jaggic737b9b2014-09-08 23:57:20 +0200106 state.isOverlayIconWide = cb.isDataTypeIconWide;
Selim Cinek06d3bca2014-08-26 17:29:20 +0200107 state.autoMirrorDrawable = !cb.noSim;
Jeff Davidsonccda9962015-01-26 18:23:24 -0800108 state.overlayIconId = cb.enabled && (cb.dataTypeIconId > 0) ? cb.dataTypeIconId : 0;
John Spurlock2d695812014-10-30 13:25:21 -0400109 state.filter = iconId != R.drawable.ic_qs_no_sim;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400110 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 Wren457a21c2015-05-06 17:50:34 -0400129 @Override
130 public int getMetricsCategory() {
131 return MetricsLogger.QS_CELLULAR;
132 }
133
John Spurlockaf8d6c42014-05-07 17:49:08 -0400134 // 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 Spurlockd5cd2fd2014-07-27 11:36:33 -0400147 boolean airplaneModeEnabled;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400148 int mobileSignalIconId;
149 String signalContentDescription;
150 int dataTypeIconId;
151 String dataContentDescription;
152 boolean activityIn;
153 boolean activityOut;
154 String enabledDesc;
John Spurlock899f4392014-06-11 10:59:11 -0400155 boolean noSim;
Jorim Jaggic737b9b2014-09-08 23:57:20 +0200156 boolean isDataTypeIconWide;
John Spurlockaf8d6c42014-05-07 17:49:08 -0400157 }
158
Jason Monk878c0932015-07-21 10:18:01 -0400159 private final class CellSignalCallback extends SignalCallbackAdapter {
Jason Monkb5746272014-11-12 16:50:31 -0500160 private final CallbackInfo mInfo = new CallbackInfo();
John Spurlockaf8d6c42014-05-07 17:49:08 -0400161 @Override
Jason Monk07b75fe2015-05-14 16:47:03 -0400162 public void setWifiIndicators(boolean enabled, IconState statusIcon, IconState qsIcon,
163 boolean activityIn, boolean activityOut, String description) {
Jason Monkb5746272014-11-12 16:50:31 -0500164 mInfo.wifiEnabled = enabled;
Jason Monkb5746272014-11-12 16:50:31 -0500165 refreshState(mInfo);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400166 }
167
168 @Override
Andrew Flynn2fdbe122015-06-01 16:34:21 -0400169 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 Monk07b75fe2015-05-14 16:47:03 -0400172 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 Monkb5746272014-11-12 16:50:31 -0500181 mInfo.activityIn = activityIn;
182 mInfo.activityOut = activityOut;
183 mInfo.enabledDesc = description;
Jason Monk07b75fe2015-05-14 16:47:03 -0400184 mInfo.isDataTypeIconWide = qsType != 0 && isWide;
Jason Monkb5746272014-11-12 16:50:31 -0500185 refreshState(mInfo);
186 }
187
188 @Override
Jason Monk07b75fe2015-05-14 16:47:03 -0400189 public void setNoSims(boolean show) {
190 mInfo.noSim = show;
Jason Monkb5746272014-11-12 16:50:31 -0500191 if (mInfo.noSim) {
192 // Make sure signal gets cleared out when no sims.
193 mInfo.mobileSignalIconId = 0;
194 mInfo.dataTypeIconId = 0;
Jason Monkb36becf2015-01-08 11:12:39 -0500195 // 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 Monkb5746272014-11-12 16:50:31 -0500200 }
201 refreshState(mInfo);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400202 }
203
204 @Override
Jason Monk07b75fe2015-05-14 16:47:03 -0400205 public void setIsAirplaneMode(IconState icon) {
206 mInfo.airplaneModeEnabled = icon.visible;
Jason Monkb5746272014-11-12 16:50:31 -0500207 refreshState(mInfo);
John Spurlockaf8d6c42014-05-07 17:49:08 -0400208 }
John Spurlockb98f7472014-07-08 17:09:42 -0400209
Jason Monk07b75fe2015-05-14 16:47:03 -0400210 @Override
211 public void setMobileDataEnabled(boolean enabled) {
John Spurlockb98f7472014-07-08 17:09:42 -0400212 mDetailAdapter.setMobileDataEnabled(enabled);
213 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400214 };
John Spurlockb98f7472014-07-08 17:09:42 -0400215
216 private final class CellularDetailAdapter implements DetailAdapter {
John Spurlockb98f7472014-07-08 17:09:42 -0400217
218 @Override
219 public int getTitle() {
220 return R.string.quick_settings_cellular_detail_title;
221 }
222
223 @Override
224 public Boolean getToggleState() {
Jason Monkd2263cd2014-11-10 14:22:56 -0500225 return mDataController.isMobileDataSupported()
Jason Monkb5746272014-11-12 16:50:31 -0500226 ? mDataController.isMobileDataEnabled()
227 : null;
John Spurlockb98f7472014-07-08 17:09:42 -0400228 }
229
230 @Override
231 public Intent getSettingsIntent() {
232 return CELLULAR_SETTINGS;
233 }
234
235 @Override
236 public void setToggleState(boolean state) {
Chris Wren9e7283f2015-05-08 17:23:47 -0400237 MetricsLogger.action(mContext, MetricsLogger.QS_CELLULAR_TOGGLE, state);
Jason Monkd2263cd2014-11-10 14:22:56 -0500238 mDataController.setMobileDataEnabled(state);
John Spurlockb98f7472014-07-08 17:09:42 -0400239 }
240
241 @Override
Chris Wren457a21c2015-05-06 17:50:34 -0400242 public int getMetricsCategory() {
243 return MetricsLogger.QS_DATAUSAGEDETAIL;
244 }
245
246 @Override
John Spurlockb98f7472014-07-08 17:09:42 -0400247 public View createDetailView(Context context, View convertView, ViewGroup parent) {
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200248 final DataUsageDetailView v = (DataUsageDetailView) (convertView != null
249 ? convertView
250 : LayoutInflater.from(mContext).inflate(R.layout.data_usage, parent, false));
Jason Monkd2263cd2014-11-10 14:22:56 -0500251 final DataUsageInfo info = mDataController.getDataUsageInfo();
John Spurlockb98f7472014-07-08 17:09:42 -0400252 if (info == null) return v;
Jorim Jaggie17c4b42014-08-26 17:27:31 +0200253 v.bind(info);
John Spurlockb98f7472014-07-08 17:09:42 -0400254 return v;
255 }
256
257 public void setMobileDataEnabled(boolean enabled) {
258 fireToggleStateChanged(enabled);
259 }
John Spurlockb98f7472014-07-08 17:09:42 -0400260 }
John Spurlockaf8d6c42014-05-07 17:49:08 -0400261}