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