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; 23 import android.view.LayoutInflater; 24 import android.view.View; 25 import android.view.ViewGroup; 26 27 import com.android.systemui.R; 28 import com.android.systemui.qs.QSTile; 29 import com.android.systemui.qs.QSTileView; 30 import com.android.systemui.qs.SignalTileView; 31 import com.android.systemui.statusbar.policy.NetworkController; 32 import com.android.systemui.statusbar.policy.NetworkController.MobileDataController; 33 import com.android.systemui.statusbar.policy.NetworkController.MobileDataController.DataUsageInfo; 34 import com.android.systemui.statusbar.policy.NetworkController.NetworkSignalChangedCallback; 35 36 /** Quick settings tile: Cellular **/ 37 public class CellularTile extends QSTile<QSTile.SignalState> { 38 private static final Intent CELLULAR_SETTINGS = new Intent().setComponent(new ComponentName( 39 "com.android.settings", "com.android.settings.Settings$DataUsageSummaryActivity")); 40 41 private final NetworkController mController; 42 private final MobileDataController mDataController; 43 private final CellularDetailAdapter mDetailAdapter; 44 45 public CellularTile(Host host) { 46 super(host); 47 mController = host.getNetworkController(); 48 mDataController = mController.getMobileDataController(); 49 mDetailAdapter = new CellularDetailAdapter(); 50 } 51 52 @Override 53 protected SignalState newTileState() { 54 return new SignalState(); 55 } 56 57 @Override 58 public DetailAdapter getDetailAdapter() { 59 return mDetailAdapter; 60 } 61 62 @Override 63 public void setListening(boolean listening) { 64 if (listening) { 65 mController.addNetworkSignalChangedCallback(mCallback); 66 } else { 67 mController.removeNetworkSignalChangedCallback(mCallback); 68 } 69 } 70 71 @Override 72 public QSTileView createTileView(Context context) { 73 return new SignalTileView(context); 74 } 75 76 @Override 77 protected void handleClick() { 78 if (mDataController.isMobileDataSupported()) { 79 showDetail(true); 80 } else { 81 mHost.startSettingsActivity(CELLULAR_SETTINGS); 82 } 83 } 84 85 @Override 86 protected void handleUpdateState(SignalState state, Object arg) { 87 state.visible = mController.hasMobileDataFeature(); 88 if (!state.visible) return; 89 final CallbackInfo cb = (CallbackInfo) arg; 90 if (cb == null) return; 91 92 final Resources r = mContext.getResources(); 93 final int iconId = cb.noSim ? R.drawable.ic_qs_no_sim 94 : !cb.enabled || cb.airplaneModeEnabled ? R.drawable.ic_qs_signal_disabled 95 : cb.mobileSignalIconId > 0 ? cb.mobileSignalIconId 96 : R.drawable.ic_qs_signal_no_signal; 97 state.icon = ResourceIcon.get(iconId); 98 state.isOverlayIconWide = cb.isDataTypeIconWide; 99 state.autoMirrorDrawable = !cb.noSim; 100 state.overlayIconId = cb.enabled && (cb.dataTypeIconId > 0) ? cb.dataTypeIconId : 0; 101 state.filter = iconId != R.drawable.ic_qs_no_sim; 102 state.activityIn = cb.enabled && cb.activityIn; 103 state.activityOut = cb.enabled && cb.activityOut; 104 105 state.label = cb.enabled 106 ? removeTrailingPeriod(cb.enabledDesc) 107 : r.getString(R.string.quick_settings_rssi_emergency_only); 108 109 final String signalContentDesc = cb.enabled && (cb.mobileSignalIconId > 0) 110 ? cb.signalContentDescription 111 : r.getString(R.string.accessibility_no_signal); 112 final String dataContentDesc = cb.enabled && (cb.dataTypeIconId > 0) && !cb.wifiEnabled 113 ? cb.dataContentDescription 114 : r.getString(R.string.accessibility_no_data); 115 state.contentDescription = r.getString( 116 R.string.accessibility_quick_settings_mobile, 117 signalContentDesc, dataContentDesc, 118 state.label); 119 } 120 121 // Remove the period from the network name 122 public static String removeTrailingPeriod(String string) { 123 if (string == null) return null; 124 final int length = string.length(); 125 if (string.endsWith(".")) { 126 return string.substring(0, length - 1); 127 } 128 return string; 129 } 130 131 private static final class CallbackInfo { 132 boolean enabled; 133 boolean wifiEnabled; 134 boolean wifiConnected; 135 boolean airplaneModeEnabled; 136 int mobileSignalIconId; 137 String signalContentDescription; 138 int dataTypeIconId; 139 String dataContentDescription; 140 boolean activityIn; 141 boolean activityOut; 142 String enabledDesc; 143 boolean noSim; 144 boolean isDataTypeIconWide; 145 } 146 147 private final NetworkSignalChangedCallback mCallback = new NetworkSignalChangedCallback() { 148 private final CallbackInfo mInfo = new CallbackInfo(); 149 150 @Override 151 public void onWifiSignalChanged(boolean enabled, boolean connected, int wifiSignalIconId, 152 boolean activityIn, boolean activityOut, 153 String wifiSignalContentDescriptionId, String description) { 154 mInfo.wifiEnabled = enabled; 155 mInfo.wifiConnected = connected; 156 refreshState(mInfo); 157 } 158 159 @Override 160 public void onMobileDataSignalChanged(boolean enabled, 161 int mobileSignalIconId, 162 String mobileSignalContentDescriptionId, int dataTypeIconId, 163 boolean activityIn, boolean activityOut, 164 String dataTypeContentDescriptionId, String description, 165 boolean isDataTypeIconWide) { 166 mInfo.enabled = enabled; 167 mInfo.mobileSignalIconId = mobileSignalIconId; 168 mInfo.signalContentDescription = mobileSignalContentDescriptionId; 169 mInfo.dataTypeIconId = dataTypeIconId; 170 mInfo.dataContentDescription = dataTypeContentDescriptionId; 171 mInfo.activityIn = activityIn; 172 mInfo.activityOut = activityOut; 173 mInfo.enabledDesc = description; 174 mInfo.isDataTypeIconWide = isDataTypeIconWide; 175 refreshState(mInfo); 176 } 177 178 @Override 179 public void onNoSimVisibleChanged(boolean visible) { 180 mInfo.noSim = visible; 181 if (mInfo.noSim) { 182 // Make sure signal gets cleared out when no sims. 183 mInfo.mobileSignalIconId = 0; 184 mInfo.dataTypeIconId = 0; 185 // Show a No SIMs description to avoid emergency calls message. 186 mInfo.enabled = true; 187 mInfo.enabledDesc = mContext.getString( 188 R.string.keyguard_missing_sim_message_short); 189 mInfo.signalContentDescription = mInfo.enabledDesc; 190 } 191 refreshState(mInfo); 192 } 193 194 @Override 195 public void onAirplaneModeChanged(boolean enabled) { 196 mInfo.airplaneModeEnabled = enabled; 197 refreshState(mInfo); 198 } 199 200 public void onMobileDataEnabled(boolean enabled) { 201 mDetailAdapter.setMobileDataEnabled(enabled); 202 } 203 }; 204 205 private final class CellularDetailAdapter implements DetailAdapter { 206 207 @Override 208 public int getTitle() { 209 return R.string.quick_settings_cellular_detail_title; 210 } 211 212 @Override 213 public Boolean getToggleState() { 214 return mDataController.isMobileDataSupported() 215 ? mDataController.isMobileDataEnabled() 216 : null; 217 } 218 219 @Override 220 public Intent getSettingsIntent() { 221 return CELLULAR_SETTINGS; 222 } 223 224 @Override 225 public void setToggleState(boolean state) { 226 mDataController.setMobileDataEnabled(state); 227 } 228 229 @Override 230 public View createDetailView(Context context, View convertView, ViewGroup parent) { 231 final DataUsageDetailView v = (DataUsageDetailView) (convertView != null 232 ? convertView 233 : LayoutInflater.from(mContext).inflate(R.layout.data_usage, parent, false)); 234 final DataUsageInfo info = mDataController.getDataUsageInfo(); 235 if (info == null) return v; 236 v.bind(info); 237 return v; 238 } 239 240 public void setMobileDataEnabled(boolean enabled) { 241 fireToggleStateChanged(enabled); 242 } 243 } 244 } 245