CellularTile.java revision b98f747c714ff8252471ca0a3295c2cb9ccb4f3a
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 TextView title = (TextView) v.findViewById(android.R.id.title); 219 title.setText(R.string.quick_settings_cellular_detail_data_usage); 220 final TextView usage = (TextView) v.findViewById(R.id.usage_text); 221 usage.setText(formatBytes(info.usageLevel)); 222 final DataUsageGraph graph = (DataUsageGraph) v.findViewById(R.id.usage_graph); 223 graph.setLevels(info.maxLevel, info.limitLevel, info.warningLevel, info.usageLevel); 224 final TextView carrier = (TextView) v.findViewById(R.id.usage_carrier_text); 225 carrier.setText(info.carrier); 226 final TextView period = (TextView) v.findViewById(R.id.usage_period_text); 227 period.setText(info.period); 228 final TextView infoTop = (TextView) v.findViewById(R.id.usage_info_top_text); 229 // TODO 230 final TextView infoBottom = (TextView) v.findViewById(R.id.usage_info_bottom_text); 231 // TODO 232 return v; 233 } 234 235 public void setMobileDataEnabled(boolean enabled) { 236 fireToggleStateChanged(enabled); 237 } 238 239 private String formatBytes(long bytes) { 240 final long b = Math.abs(bytes); 241 double val; 242 String suffix; 243 if (b > 100 * MB) { 244 val = b / GB; 245 suffix = "GB"; 246 } else if (b > 100 * KB) { 247 val = b / MB; 248 suffix = "MB"; 249 } else { 250 val = b / KB; 251 suffix = "KB"; 252 } 253 return FORMAT.format(val * (bytes < 0 ? -1 : 1)) + " " + suffix; 254 } 255 } 256} 257