CellularTile.java revision 5e9049a362016c9f00823346d619303674f9df0e
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 17package com.android.systemui.qs.tiles; 18 19import android.content.ComponentName; 20import android.content.Context; 21import android.content.Intent; 22import android.content.res.Resources; 23import android.view.LayoutInflater; 24import android.view.View; 25import android.view.ViewGroup; 26import android.widget.TextView; 27 28import com.android.systemui.R; 29import com.android.systemui.qs.DataUsageGraph; 30import com.android.systemui.qs.QSTile; 31import com.android.systemui.qs.QSTileView; 32import com.android.systemui.qs.SignalTileView; 33import com.android.systemui.statusbar.policy.NetworkController; 34import com.android.systemui.statusbar.policy.NetworkController.DataUsageInfo; 35import com.android.systemui.statusbar.policy.NetworkController.NetworkSignalChangedCallback; 36 37import java.text.DecimalFormat; 38 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; 45 private final CellularDetailAdapter mDetailAdapter; 46 47 public CellularTile(Host host) { 48 super(host); 49 mController = host.getNetworkController(); 50 mDetailAdapter = new CellularDetailAdapter(); 51 } 52 53 @Override 54 protected SignalState newTileState() { 55 return new SignalState(); 56 } 57 58 @Override 59 public DetailAdapter getDetailAdapter() { 60 return mDetailAdapter; 61 } 62 63 @Override 64 public void setListening(boolean listening) { 65 if (listening) { 66 mController.addNetworkSignalChangedCallback(mCallback); 67 } else { 68 mController.removeNetworkSignalChangedCallback(mCallback); 69 } 70 } 71 72 @Override 73 public QSTileView createTileView(Context context) { 74 return new SignalTileView(context); 75 } 76 77 @Override 78 protected void handleClick() { 79 if (mController.isMobileDataSupported()) { 80 showDetail(true); 81 } else { 82 mHost.startSettingsActivity(CELLULAR_SETTINGS); 83 } 84 } 85 86 @Override 87 protected void handleUpdateState(SignalState state, Object arg) { 88 state.visible = mController.hasMobileDataFeature(); 89 if (!state.visible) return; 90 final CallbackInfo cb = (CallbackInfo) arg; 91 if (cb == null) return; 92 93 final Resources r = mContext.getResources(); 94 state.iconId = cb.noSim 95 ? R.drawable.stat_sys_no_sim 96 : cb.enabled && (cb.mobileSignalIconId > 0) 97 ? cb.mobileSignalIconId 98 : R.drawable.ic_qs_signal_no_signal; 99 state.overlayIconId = cb.enabled && (cb.dataTypeIconId > 0) && !cb.wifiEnabled 100 ? cb.dataTypeIconId 101 : 0; 102 state.filter = state.iconId != R.drawable.stat_sys_no_sim; 103 state.activityIn = cb.enabled && cb.activityIn; 104 state.activityOut = cb.enabled && cb.activityOut; 105 106 state.label = cb.enabled 107 ? removeTrailingPeriod(cb.enabledDesc) 108 : r.getString(R.string.quick_settings_rssi_emergency_only); 109 110 final String signalContentDesc = cb.enabled && (cb.mobileSignalIconId > 0) 111 ? cb.signalContentDescription 112 : r.getString(R.string.accessibility_no_signal); 113 final String dataContentDesc = cb.enabled && (cb.dataTypeIconId > 0) && !cb.wifiEnabled 114 ? cb.dataContentDescription 115 : r.getString(R.string.accessibility_no_data); 116 state.contentDescription = r.getString( 117 R.string.accessibility_quick_settings_mobile, 118 signalContentDesc, dataContentDesc, 119 state.label); 120 } 121 122 // Remove the period from the network name 123 public static String removeTrailingPeriod(String string) { 124 if (string == null) return null; 125 final int length = string.length(); 126 if (string.endsWith(".")) { 127 return string.substring(0, length - 1); 128 } 129 return string; 130 } 131 132 private static final class CallbackInfo { 133 boolean enabled; 134 boolean wifiEnabled; 135 int mobileSignalIconId; 136 String signalContentDescription; 137 int dataTypeIconId; 138 String dataContentDescription; 139 boolean activityIn; 140 boolean activityOut; 141 String enabledDesc; 142 boolean noSim; 143 } 144 145 private final NetworkSignalChangedCallback mCallback = new NetworkSignalChangedCallback() { 146 private boolean mWifiEnabled; 147 148 @Override 149 public void onWifiSignalChanged(boolean enabled, int wifiSignalIconId, 150 boolean activityIn, boolean activityOut, 151 String wifiSignalContentDescriptionId, String description) { 152 mWifiEnabled = enabled; 153 } 154 155 @Override 156 public void onMobileDataSignalChanged(boolean enabled, 157 int mobileSignalIconId, 158 String mobileSignalContentDescriptionId, int dataTypeIconId, 159 boolean activityIn, boolean activityOut, 160 String dataTypeContentDescriptionId, String description, boolean noSim) { 161 final CallbackInfo info = new CallbackInfo(); // TODO pool? 162 info.enabled = enabled; 163 info.wifiEnabled = mWifiEnabled; 164 info.mobileSignalIconId = mobileSignalIconId; 165 info.signalContentDescription = mobileSignalContentDescriptionId; 166 info.dataTypeIconId = dataTypeIconId; 167 info.dataContentDescription = dataTypeContentDescriptionId; 168 info.activityIn = activityIn; 169 info.activityOut = activityOut; 170 info.enabledDesc = description; 171 info.noSim = noSim; 172 refreshState(info); 173 } 174 175 @Override 176 public void onAirplaneModeChanged(boolean enabled) { 177 // noop 178 } 179 180 public void onMobileDataEnabled(boolean enabled) { 181 mDetailAdapter.setMobileDataEnabled(enabled); 182 } 183 }; 184 185 private final class CellularDetailAdapter implements DetailAdapter { 186 private static final double KB = 1024; 187 private static final double MB = 1024 * KB; 188 private static final double GB = 1024 * MB; 189 190 private final DecimalFormat FORMAT = new DecimalFormat("#.##"); 191 192 @Override 193 public int getTitle() { 194 return R.string.quick_settings_cellular_detail_title; 195 } 196 197 @Override 198 public Boolean getToggleState() { 199 return mController.isMobileDataSupported() ? mController.isMobileDataEnabled() : null; 200 } 201 202 @Override 203 public Intent getSettingsIntent() { 204 return CELLULAR_SETTINGS; 205 } 206 207 @Override 208 public void setToggleState(boolean state) { 209 mController.setMobileDataEnabled(state); 210 } 211 212 @Override 213 public View createDetailView(Context context, View convertView, ViewGroup parent) { 214 final View v = convertView != null ? convertView : LayoutInflater.from(mContext) 215 .inflate(R.layout.data_usage, parent, false); 216 final DataUsageInfo info = mController.getDataUsageInfo(); 217 if (info == null) return v; 218 final Resources res = mContext.getResources(); 219 int titleId; 220 long bytes; 221 int usageColor = R.color.system_accent_color; 222 String top = null, bottom = null; 223 if (info.limitLevel <= 0) { // no limit 224 titleId = R.string.quick_settings_cellular_detail_data_usage; 225 bytes = info.usageLevel; 226 } else if (info.usageLevel <= info.limitLevel) { // under limit 227 titleId = R.string.quick_settings_cellular_detail_remaining_data; 228 bytes = info.limitLevel - info.usageLevel; 229 top = res.getString(R.string.quick_settings_cellular_detail_data_used, 230 formatBytes(info.usageLevel)); 231 bottom = res.getString(R.string.quick_settings_cellular_detail_data_limit, 232 formatBytes(info.limitLevel)); 233 } else { // over limit 234 titleId = R.string.quick_settings_cellular_detail_over_limit; 235 bytes = info.usageLevel - info.limitLevel; 236 top = res.getString(R.string.quick_settings_cellular_detail_data_used, 237 formatBytes(info.usageLevel)); 238 bottom = res.getString(R.string.quick_settings_cellular_detail_data_limit, 239 formatBytes(info.limitLevel)); 240 usageColor = R.color.system_warning_color; 241 } 242 243 final TextView title = (TextView) v.findViewById(android.R.id.title); 244 title.setText(titleId); 245 final TextView usage = (TextView) v.findViewById(R.id.usage_text); 246 usage.setText(formatBytes(bytes)); 247 usage.setTextColor(res.getColor(usageColor)); 248 final DataUsageGraph graph = (DataUsageGraph) v.findViewById(R.id.usage_graph); 249 graph.setLevels(info.maxLevel, info.limitLevel, info.warningLevel, info.usageLevel); 250 final TextView carrier = (TextView) v.findViewById(R.id.usage_carrier_text); 251 carrier.setText(info.carrier); 252 final TextView period = (TextView) v.findViewById(R.id.usage_period_text); 253 period.setText(info.period); 254 final TextView infoTop = (TextView) v.findViewById(R.id.usage_info_top_text); 255 infoTop.setVisibility(top != null ? View.VISIBLE : View.GONE); 256 infoTop.setText(top); 257 final TextView infoBottom = (TextView) v.findViewById(R.id.usage_info_bottom_text); 258 infoBottom.setVisibility(bottom != null ? View.VISIBLE : View.GONE); 259 infoBottom.setText(bottom); 260 return v; 261 } 262 263 public void setMobileDataEnabled(boolean enabled) { 264 fireToggleStateChanged(enabled); 265 } 266 267 private String formatBytes(long bytes) { 268 final long b = Math.abs(bytes); 269 double val; 270 String suffix; 271 if (b > 100 * MB) { 272 val = b / GB; 273 suffix = "GB"; 274 } else if (b > 100 * KB) { 275 val = b / MB; 276 suffix = "MB"; 277 } else { 278 val = b / KB; 279 suffix = "KB"; 280 } 281 return FORMAT.format(val * (bytes < 0 ? -1 : 1)) + " " + suffix; 282 } 283 } 284} 285