summaryrefslogtreecommitdiff
path: root/InCallUI/src/com/android/incallui/InCallPresenter.java
blob: 47d2d69c880a622a5382a96e35247c7fd5ec4dc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.incallui;

import com.google.android.collect.Sets;
import com.google.common.base.Preconditions;

import android.content.Context;
import android.content.Intent;

import com.android.services.telephony.common.Call;

import java.util.Set;

/**
 * Takes updates from the CallList and notifies the InCallActivity (UI)
 * of the changes.
 * Responsible for starting the activity for a new call and finishing the activity when all calls
 * are disconnected.
 * Creates and manages the in-call state and provides a listener pattern for the presenters
 * that want to listen in on the in-call state changes.
 * TODO(klp): This class has become more of a state machine at this point.  Consider renaming.
 */
public class InCallPresenter implements CallList.Listener {

    private static InCallPresenter sInCallPresenter;

    private final StatusBarNotifier mStatusBarNotifier;
    private final Set<InCallStateListener> mListeners = Sets.newHashSet();

    private InCallState mInCallState = InCallState.HIDDEN;
    private InCallActivity mInCallActivity;

    public static InCallPresenter getInstance() {
        Preconditions.checkNotNull(sInCallPresenter);
        return sInCallPresenter;
    }

    public static synchronized InCallPresenter init(Context context) {
        Preconditions.checkState(sInCallPresenter == null);
        sInCallPresenter = new InCallPresenter(context);
        return sInCallPresenter;
    }

    public void setActivity(InCallActivity inCallActivity) {
        mInCallActivity = inCallActivity;
        mInCallState = InCallState.STARTED;

        Logger.d(this, "UI Initialized");

        // Since the UI just came up, imitate an update from the call list
        // to set the proper UI state.
        onCallListChange(CallList.getInstance());
    }

    /**
     * Called when there is a change to the call list.
     * Sets the In-Call state for the entire in-call app based on the information it gets from
     * CallList. Dispatches the in-call state to all listeners. Can trigger the creation or
     * destruction of the UI based on the states that is calculates.
     */
    @Override
    public void onCallListChange(CallList callList) {
        // fast fail if we are still starting up
        if (mInCallState == InCallState.STARTING_UP) {
            Logger.d(this, "Already on STARTING_UP, ignoring until ready");
            return;
        }

        InCallState newState = getPotentialStateFromCallList(callList);
        newState = startOrFinishUi(newState);

        // Set the new state before announcing it to the world
        mInCallState = newState;

        // notify listeners of new state
        for (InCallStateListener listener : mListeners) {
            Logger.d(this, "Notify " + listener + " of state " + mInCallState.toString());
            listener.onStateChange(mInCallState, callList);
        }
    }

    /**
     * Given the call list, return the state in which the in-call screen should be.
     */
    public InCallState getPotentialStateFromCallList(CallList callList) {
        InCallState newState = InCallState.HIDDEN;

        if (callList.getIncomingCall() != null) {
            newState = InCallState.INCOMING;
        } else if (callList.getActiveCall() != null ||
                callList.getBackgroundCall() != null) {
            newState = InCallState.INCALL;
        }

        return newState;
    }

    public void addListener(InCallStateListener listener) {
        Preconditions.checkNotNull(listener);
        mListeners.add(listener);
    }

    public void removeListener(InCallStateListener listener) {
        Preconditions.checkNotNull(listener);
        mListeners.remove(listener);
    }

    /**
     * When the state of in-call changes, this is the first method to get called. It determines if
     * the UI needs to be started or finished depending on the new state and does it.
     * It returns a potential new middle state (STARTING_UP) if appropriate.
     */
    private InCallState startOrFinishUi(InCallState newState) {
        Logger.d(this, "startOrFInishUi: " + newState.toString());

        // TODO(klp): Consider a proper state machine implementation

        // if we need to show something, we need to start the Ui...
        if (!newState.isHidden()) {

            // When we attempt to go to any state from HIDDEN, it means that we need to create the
            // entire UI. However, the StatusBarNotifier is in charge of starting up the Ui because
            // it has special behavior in case we have to deal with an immersive foreground app.
            // We set the STARTING_UP state to let StatusBarNotifier know it needs to start the
            // the Ui.
            if (mInCallState.isHidden()) {
                return InCallState.STARTING_UP;
            }

        } else if (mInCallActivity != null) {
            // Null out reference before we start end sequence
            InCallActivity temp = mInCallActivity;
            mInCallActivity = null;

            temp.finish();
        }

        return newState;
    }

    /**
     * Private constructor. Must use getInstance() to get this singleton.
     */
    private InCallPresenter(Context context) {
        Preconditions.checkNotNull(context);

        mStatusBarNotifier = new StatusBarNotifier(context);
        addListener(mStatusBarNotifier);

        CallList.getInstance().addListener(this);
    }

    /**
     * All the main states of InCallActivity.
     */
    public enum InCallState {
        // InCall Screen is off and there are no calls
        HIDDEN,

        // In call is in the process of starting up
        STARTING_UP,

        // In call has started but is not displaying any information
        STARTED,

        // Incoming-call screen is up
        INCOMING,

        // In-call experience is showing
        INCALL;

        public boolean isIncoming() {
            return (this == INCOMING);
        }

        public boolean isHidden() {
            return (this == HIDDEN);
        }
    }

    /**
     * Interface implemented by classes that need to know about the InCall State.
     */
    public interface InCallStateListener {
        // TODO(klp): Enhance state to contain the call objects instead of passing CallList
        public void onStateChange(InCallState state, CallList callList);
    }
}