1 /* 2 * Copyright (C) 2006 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.internal.telephony; 18 19 import android.content.BroadcastReceiver; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.IntentFilter; 23 import android.os.Handler; 24 import android.os.Message; 25 import android.telephony.ServiceState; 26 import android.telephony.SignalStrength; 27 import android.telephony.TelephonyManager; 28 import android.util.Log; 29 30 /** 31 * 32 * DO NOT USE THIS CLASS: 33 * 34 * Use android.telephony.TelephonyManager and PhoneStateListener instead. 35 * 36 * 37 */ 38 @Deprecated 39 public final class PhoneStateIntentReceiver extends BroadcastReceiver { 40 private static final String LOG_TAG = "PHONE"; 41 private static final boolean DBG = false; 42 43 private static final int NOTIF_PHONE = 1 << 0; 44 private static final int NOTIF_SERVICE = 1 << 1; 45 private static final int NOTIF_SIGNAL = 1 << 2; 46 47 private static final int NOTIF_MAX = 1 << 5; 48 49 Phone.State mPhoneState = Phone.State.IDLE; 50 ServiceState mServiceState = new ServiceState(); 51 SignalStrength mSignalStrength = new SignalStrength(); 52 53 private Context mContext; 54 private Handler mTarget; 55 private IntentFilter mFilter; 56 private int mWants; 57 private int mPhoneStateEventWhat; 58 private int mServiceStateEventWhat; 59 private int mLocationEventWhat; 60 private int mAsuEventWhat; 61 62 public PhoneStateIntentReceiver() { 63 super(); 64 mFilter = new IntentFilter(); 65 } 66 67 public PhoneStateIntentReceiver(Context context, Handler target) { 68 this(); 69 setContext(context); 70 setTarget(target); 71 } 72 73 public void setContext(Context c) { 74 mContext = c; 75 } 76 77 public void setTarget(Handler h) { 78 mTarget = h; 79 } 80 81 public Phone.State getPhoneState() { 82 if ((mWants & NOTIF_PHONE) == 0) { 83 throw new RuntimeException 84 ("client must call notifyPhoneCallState(int)"); 85 } 86 return mPhoneState; 87 } 88 89 public ServiceState getServiceState() { 90 if ((mWants & NOTIF_SERVICE) == 0) { 91 throw new RuntimeException 92 ("client must call notifyServiceState(int)"); 93 } 94 return mServiceState; 95 } 96 97 /** 98 * Returns current signal strength in "asu", ranging from 0-31 99 * or -1 if unknown 100 * 101 * For GSM, dBm = -113 + 2*asu 102 * 0 means "-113 dBm or less" 103 * 31 means "-51 dBm or greater" 104 * 105 * @return signal strength in asu, -1 if not yet updated 106 * Throws RuntimeException if client has not called notifySignalStrength() 107 */ 108 public int getSignalStrength() { 109 // TODO: use new SignalStrength instead of asu 110 if ((mWants & NOTIF_SIGNAL) == 0) { 111 throw new RuntimeException 112 ("client must call notifySignalStrength(int)"); 113 } 114 int gsmSignalStrength = mSignalStrength.getGsmSignalStrength(); 115 116 return (gsmSignalStrength == 99 ? -1 : gsmSignalStrength); 117 } 118 119 /** 120 * Return current signal strength in "dBm", ranging from -113 - -51dBm 121 * or -1 if unknown 122 * 123 * @return signal strength in dBm, -1 if not yet updated 124 * Throws RuntimeException if client has not called notifySignalStrength() 125 */ 126 public int getSignalStrengthDbm() { 127 if ((mWants & NOTIF_SIGNAL) == 0) { 128 throw new RuntimeException 129 ("client must call notifySignalStrength(int)"); 130 } 131 132 int dBm = -1; 133 134 if(!mSignalStrength.isGsm()) { 135 dBm = mSignalStrength.getCdmaDbm(); 136 } else { 137 int gsmSignalStrength = mSignalStrength.getGsmSignalStrength(); 138 int asu = (gsmSignalStrength == 99 ? -1 : gsmSignalStrength); 139 if (asu != -1) { 140 dBm = -113 + 2*asu; 141 } 142 } 143 return dBm; 144 } 145 146 public void notifyPhoneCallState(int eventWhat) { 147 mWants |= NOTIF_PHONE; 148 mPhoneStateEventWhat = eventWhat; 149 mFilter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED); 150 } 151 152 public boolean getNotifyPhoneCallState() { 153 return ((mWants & NOTIF_PHONE) != 0); 154 } 155 156 public void notifyServiceState(int eventWhat) { 157 mWants |= NOTIF_SERVICE; 158 mServiceStateEventWhat = eventWhat; 159 mFilter.addAction(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED); 160 } 161 162 public boolean getNotifyServiceState() { 163 return ((mWants & NOTIF_SERVICE) != 0); 164 } 165 166 public void notifySignalStrength (int eventWhat) { 167 mWants |= NOTIF_SIGNAL; 168 mAsuEventWhat = eventWhat; 169 mFilter.addAction(TelephonyIntents.ACTION_SIGNAL_STRENGTH_CHANGED); 170 } 171 172 public boolean getNotifySignalStrength() { 173 return ((mWants & NOTIF_SIGNAL) != 0); 174 } 175 176 public void registerIntent() { 177 mContext.registerReceiver(this, mFilter); 178 } 179 180 public void unregisterIntent() { 181 mContext.unregisterReceiver(this); 182 } 183 184 @Override 185 public void onReceive(Context context, Intent intent) { 186 String action = intent.getAction(); 187 188 try { 189 if (TelephonyIntents.ACTION_SIGNAL_STRENGTH_CHANGED.equals(action)) { 190 mSignalStrength = SignalStrength.newFromBundle(intent.getExtras()); 191 192 if (mTarget != null && getNotifySignalStrength()) { 193 Message message = Message.obtain(mTarget, mAsuEventWhat); 194 mTarget.sendMessage(message); 195 } 196 } else if (TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(action)) { 197 if (DBG) Log.d(LOG_TAG, "onReceiveIntent: ACTION_PHONE_STATE_CHANGED, state=" 198 + intent.getStringExtra(Phone.STATE_KEY)); 199 String phoneState = intent.getStringExtra(Phone.STATE_KEY); 200 mPhoneState = (Phone.State) Enum.valueOf( 201 Phone.State.class, phoneState); 202 203 if (mTarget != null && getNotifyPhoneCallState()) { 204 Message message = Message.obtain(mTarget, 205 mPhoneStateEventWhat); 206 mTarget.sendMessage(message); 207 } 208 } else if (TelephonyIntents.ACTION_SERVICE_STATE_CHANGED.equals(action)) { 209 mServiceState = ServiceState.newFromBundle(intent.getExtras()); 210 211 if (mTarget != null && getNotifyServiceState()) { 212 Message message = Message.obtain(mTarget, 213 mServiceStateEventWhat); 214 mTarget.sendMessage(message); 215 } 216 } 217 } catch (Exception ex) { 218 Log.e(LOG_TAG, "[PhoneStateIntentRecv] caught " + ex); 219 ex.printStackTrace(); 220 } 221 } 222 223 } 224