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 PhoneStateIntentReceiver()62 public PhoneStateIntentReceiver() { 63 super(); 64 mFilter = new IntentFilter(); 65 } 66 PhoneStateIntentReceiver(Context context, Handler target)67 public PhoneStateIntentReceiver(Context context, Handler target) { 68 this(); 69 setContext(context); 70 setTarget(target); 71 } 72 setContext(Context c)73 public void setContext(Context c) { 74 mContext = c; 75 } 76 setTarget(Handler h)77 public void setTarget(Handler h) { 78 mTarget = h; 79 } 80 getPhoneState()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 getServiceState()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 as an asu 0..31 99 * 100 * Throws RuntimeException if client has not called notifySignalStrength() 101 */ getSignalStrengthLevelAsu()102 public int getSignalStrengthLevelAsu() { 103 // TODO: use new SignalStrength instead of asu 104 if ((mWants & NOTIF_SIGNAL) == 0) { 105 throw new RuntimeException 106 ("client must call notifySignalStrength(int)"); 107 } 108 return mSignalStrength.getAsuLevel(); 109 } 110 111 /** 112 * Return current signal strength in "dBm", ranging from -113 - -51dBm 113 * or -1 if unknown 114 * 115 * @return signal strength in dBm, -1 if not yet updated 116 * Throws RuntimeException if client has not called notifySignalStrength() 117 */ getSignalStrengthDbm()118 public int getSignalStrengthDbm() { 119 if ((mWants & NOTIF_SIGNAL) == 0) { 120 throw new RuntimeException 121 ("client must call notifySignalStrength(int)"); 122 } 123 return mSignalStrength.getDbm(); 124 } 125 notifyPhoneCallState(int eventWhat)126 public void notifyPhoneCallState(int eventWhat) { 127 mWants |= NOTIF_PHONE; 128 mPhoneStateEventWhat = eventWhat; 129 mFilter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED); 130 } 131 getNotifyPhoneCallState()132 public boolean getNotifyPhoneCallState() { 133 return ((mWants & NOTIF_PHONE) != 0); 134 } 135 notifyServiceState(int eventWhat)136 public void notifyServiceState(int eventWhat) { 137 mWants |= NOTIF_SERVICE; 138 mServiceStateEventWhat = eventWhat; 139 mFilter.addAction(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED); 140 } 141 getNotifyServiceState()142 public boolean getNotifyServiceState() { 143 return ((mWants & NOTIF_SERVICE) != 0); 144 } 145 notifySignalStrength(int eventWhat)146 public void notifySignalStrength (int eventWhat) { 147 mWants |= NOTIF_SIGNAL; 148 mAsuEventWhat = eventWhat; 149 mFilter.addAction(TelephonyIntents.ACTION_SIGNAL_STRENGTH_CHANGED); 150 } 151 getNotifySignalStrength()152 public boolean getNotifySignalStrength() { 153 return ((mWants & NOTIF_SIGNAL) != 0); 154 } 155 registerIntent()156 public void registerIntent() { 157 mContext.registerReceiver(this, mFilter); 158 } 159 unregisterIntent()160 public void unregisterIntent() { 161 mContext.unregisterReceiver(this); 162 } 163 164 @Override onReceive(Context context, Intent intent)165 public void onReceive(Context context, Intent intent) { 166 String action = intent.getAction(); 167 168 try { 169 if (TelephonyIntents.ACTION_SIGNAL_STRENGTH_CHANGED.equals(action)) { 170 mSignalStrength = SignalStrength.newFromBundle(intent.getExtras()); 171 172 if (mTarget != null && getNotifySignalStrength()) { 173 Message message = Message.obtain(mTarget, mAsuEventWhat); 174 mTarget.sendMessage(message); 175 } 176 } else if (TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(action)) { 177 if (DBG) Log.d(LOG_TAG, "onReceiveIntent: ACTION_PHONE_STATE_CHANGED, state=" 178 + intent.getStringExtra(Phone.STATE_KEY)); 179 String phoneState = intent.getStringExtra(Phone.STATE_KEY); 180 mPhoneState = (Phone.State) Enum.valueOf( 181 Phone.State.class, phoneState); 182 183 if (mTarget != null && getNotifyPhoneCallState()) { 184 Message message = Message.obtain(mTarget, 185 mPhoneStateEventWhat); 186 mTarget.sendMessage(message); 187 } 188 } else if (TelephonyIntents.ACTION_SERVICE_STATE_CHANGED.equals(action)) { 189 mServiceState = ServiceState.newFromBundle(intent.getExtras()); 190 191 if (mTarget != null && getNotifyServiceState()) { 192 Message message = Message.obtain(mTarget, 193 mServiceStateEventWhat); 194 mTarget.sendMessage(message); 195 } 196 } 197 } catch (Exception ex) { 198 Log.e(LOG_TAG, "[PhoneStateIntentRecv] caught " + ex); 199 ex.printStackTrace(); 200 } 201 } 202 203 } 204