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    protected static final int EVENT_CALL_WAITING_INFO_CDMA        = 15;
61    protected static final int EVENT_THREE_WAY_DIAL_L2_RESULT_CDMA = 16;
62
63    protected void pollCallsWhenSafe() {
64        needsPoll = true;
65
66        if (checkNoOperationsPending()) {
67            lastRelevantPoll = obtainMessage(EVENT_POLL_CALLS_RESULT);
68            cm.getCurrentCalls(lastRelevantPoll);
69        }
70    }
71
72    protected void
73    pollCallsAfterDelay() {
74        Message msg = obtainMessage();
75
76        msg.what = EVENT_REPOLL_AFTER_DELAY;
77        sendMessageDelayed(msg, POLL_DELAY_MSEC);
78    }
79
80    protected boolean
81    isCommandExceptionRadioNotAvailable(Throwable e) {
82        return e != null && e instanceof CommandException
83                && ((CommandException)e).getCommandError()
84                        == CommandException.Error.RADIO_NOT_AVAILABLE;
85    }
86
87    protected abstract void handlePollCalls(AsyncResult ar);
88
89    protected void handleRadioAvailable() {
90        pollCallsWhenSafe();
91    }
92
93    /**
94     * Obtain a complete message that indicates that this operation
95     * does not require polling of getCurrentCalls(). However, if other
96     * operations that do need getCurrentCalls() are pending or are
97     * scheduled while this operation is pending, the invocation
98     * of getCurrentCalls() will be postponed until this
99     * operation is also complete.
100     */
101    protected Message
102    obtainNoPollCompleteMessage(int what) {
103        pendingOperations++;
104        lastRelevantPoll = null;
105        return obtainMessage(what);
106    }
107
108    /**
109     * @return true if we're idle or there's a call to getCurrentCalls() pending
110     * but nothing else
111     */
112    private boolean
113    checkNoOperationsPending() {
114        if (DBG_POLL) log("checkNoOperationsPending: pendingOperations=" +
115                pendingOperations);
116        return pendingOperations == 0;
117    }
118
119
120    //***** Overridden from Handler
121    public abstract void handleMessage (Message msg);
122
123    protected abstract void log(String msg);
124
125}
126