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