CallTracker.java revision dda5391d5079537e275c9f4ed2637a1484d0e4e8
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.os.AsyncResult;
20import android.os.Handler;
21import android.os.Message;
22import android.util.Log;
23
24import com.android.internal.telephony.CommandException;
25
26
27/**
28 * {@hide}
29 */
30public abstract class CallTracker extends Handler {
31
32    private static final boolean DBG_POLL = false;
33
34    //***** Constants
35
36    static final int POLL_DELAY_MSEC = 250;
37
38    protected int pendingOperations;
39    protected boolean needsPoll;
40    protected Message lastRelevantPoll;
41
42    public CommandsInterface cm;
43
44
45    //***** Events
46
47    protected static final int EVENT_POLL_CALLS_RESULT    = 1;
48    protected static final int EVENT_CALL_STATE_CHANGE    = 2;
49    protected static final int EVENT_REPOLL_AFTER_DELAY   = 3;
50    protected static final int EVENT_OPERATION_COMPLETE       = 4;
51    protected static final int EVENT_GET_LAST_CALL_FAIL_CAUSE = 5;
52
53    protected static final int EVENT_SWITCH_RESULT        = 8;
54    protected static final int EVENT_RADIO_AVAILABLE      = 9;
55    protected static final int EVENT_RADIO_NOT_AVAILABLE  = 10;
56    protected static final int EVENT_CONFERENCE_RESULT    = 11;
57    protected static final int EVENT_SEPARATE_RESULT      = 12;
58    protected static final int EVENT_ECT_RESULT           = 13;
59    protected static final int EVENT_EXIT_ECM_RESPONSE_CDMA = 14;
60
61
62    protected void pollCallsWhenSafe() {
63        needsPoll = true;
64
65        if (checkNoOperationsPending()) {
66            lastRelevantPoll = obtainMessage(EVENT_POLL_CALLS_RESULT);
67            cm.getCurrentCalls(lastRelevantPoll);
68        }
69    }
70
71    protected void
72    pollCallsAfterDelay() {
73        Message msg = obtainMessage();
74
75        msg.what = EVENT_REPOLL_AFTER_DELAY;
76        sendMessageDelayed(msg, POLL_DELAY_MSEC);
77    }
78
79    protected boolean
80    isCommandExceptionRadioNotAvailable(Throwable e) {
81        return e != null && e instanceof CommandException
82                && ((CommandException)e).getCommandError()
83                        == CommandException.Error.RADIO_NOT_AVAILABLE;
84    }
85
86    protected abstract void handlePollCalls(AsyncResult ar);
87
88    protected void handleRadioAvailable() {
89        pollCallsWhenSafe();
90    }
91
92    /**
93     * Obtain a complete message that indicates that this operation
94     * does not require polling of getCurrentCalls(). However, if other
95     * operations that do need getCurrentCalls() are pending or are
96     * scheduled while this operation is pending, the invocation
97     * of getCurrentCalls() will be postponed until this
98     * operation is also complete.
99     */
100    protected Message
101    obtainNoPollCompleteMessage(int what) {
102        pendingOperations++;
103        lastRelevantPoll = null;
104        return obtainMessage(what);
105    }
106
107    /**
108     * @return true if we're idle or there's a call to getCurrentCalls() pending
109     * but nothing else
110     */
111    private boolean
112    checkNoOperationsPending() {
113        if (DBG_POLL) log("checkNoOperationsPending: pendingOperations=" +
114                pendingOperations);
115        return pendingOperations == 0;
116    }
117
118
119    //***** Overridden from Handler
120    public abstract void handleMessage (Message msg);
121
122    protected abstract void log(String msg);
123
124}
125