1/*
2 * Copyright (C) 2009 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.gsm;
18
19import android.content.Context;
20
21import android.os.Handler;
22import android.os.HandlerThread;
23import android.os.Looper;
24import android.os.Message;
25import android.telephony.Rlog;
26
27import com.android.internal.telephony.gsm.GSMPhone;
28import com.android.internal.telephony.test.SimulatedCommands;
29import com.android.internal.telephony.TestPhoneNotifier;
30
31/**
32 * This class creates a HandlerThread which waits for the various messages.
33 */
34public class GSMTestHandler extends HandlerThread implements Handler.Callback {
35
36    private Handler mHandler;
37    private Message mCurrentMessage;
38
39    private Boolean mMsgConsumed;
40    private SimulatedCommands sc;
41    private GSMPhone mGSMPhone;
42    private Context mContext;
43
44    private static final int FAIL_TIMEOUT_MILLIS = 5 * 1000;
45
46    public GSMTestHandler(Context context) {
47        super("GSMPhoneTest");
48        mMsgConsumed = false;
49        mContext = context;
50   }
51
52    @Override
53    protected void onLooperPrepared() {
54        sc = new SimulatedCommands();
55        mGSMPhone = new GSMPhone(mContext, sc, new TestPhoneNotifier(), true);
56        mHandler = new Handler(getLooper(), this);
57        synchronized (this) {
58            notifyAll();
59        }
60    }
61
62    public boolean handleMessage(Message msg) {
63        synchronized (this) {
64            mCurrentMessage = msg;
65            this.notifyAll();
66            while(!mMsgConsumed) {
67                try {
68                    this.wait();
69                } catch (InterruptedException e) {}
70            }
71            mMsgConsumed = false;
72        }
73        return true;
74    }
75
76
77    public void cleanup() {
78        Looper looper = getLooper();
79        if (looper != null) looper.quit();
80        mHandler = null;
81    }
82
83    public Handler getHandler() {
84        return mHandler;
85    }
86
87    public SimulatedCommands getSimulatedCommands() {
88        return sc;
89    }
90
91    public GSMPhone getGSMPhone() {
92        return mGSMPhone;
93    }
94
95    public Message waitForMessage(int code) {
96        Message msg;
97        while(true) {
98            msg = null;
99            synchronized (this) {
100                try {
101                    this.wait(FAIL_TIMEOUT_MILLIS);
102                } catch (InterruptedException e) {
103                }
104
105                // Check if timeout has occurred.
106                if (mCurrentMessage != null) {
107                    // Consume the message
108                    msg = Message.obtain();
109                    msg.copyFrom(mCurrentMessage);
110                    mCurrentMessage = null;
111                    mMsgConsumed = true;
112                    this.notifyAll();
113                }
114            }
115            if (msg == null || code == GSMPhoneTest.ANY_MESSAGE || msg.what == code) return msg;
116       }
117    }
118}
119