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