1 /*
2  * Copyright (C) 2013 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 
17 package com.android.incallui;
18 
19 import android.content.Context;
20 
21 import com.android.incallui.InCallPresenter.InCallDetailsListener;
22 import com.android.incallui.InCallPresenter.InCallState;
23 import com.android.incallui.InCallPresenter.InCallStateListener;
24 import com.android.incallui.InCallPresenter.IncomingCallListener;
25 
26 import com.google.common.base.Preconditions;
27 
28 import java.util.ArrayList;
29 import java.util.List;
30 
31 /**
32  * Logic for call buttons.
33  */
34 public class ConferenceManagerPresenter
35         extends Presenter<ConferenceManagerPresenter.ConferenceManagerUi>
36         implements InCallStateListener, InCallDetailsListener, IncomingCallListener {
37 
38     private Context mContext;
39 
40     @Override
onUiReady(ConferenceManagerUi ui)41     public void onUiReady(ConferenceManagerUi ui) {
42         super.onUiReady(ui);
43 
44         // register for call state changes last
45         InCallPresenter.getInstance().addListener(this);
46         InCallPresenter.getInstance().addIncomingCallListener(this);
47     }
48 
49     @Override
onUiUnready(ConferenceManagerUi ui)50     public void onUiUnready(ConferenceManagerUi ui) {
51         super.onUiUnready(ui);
52 
53         InCallPresenter.getInstance().removeListener(this);
54         InCallPresenter.getInstance().removeIncomingCallListener(this);
55     }
56 
57     @Override
onStateChange(InCallState oldState, InCallState newState, CallList callList)58     public void onStateChange(InCallState oldState, InCallState newState, CallList callList) {
59         if (getUi().isFragmentVisible()) {
60             Log.v(this, "onStateChange" + newState);
61             if (newState == InCallState.INCALL) {
62                 final Call call = callList.getActiveOrBackgroundCall();
63                 if (call != null && call.isConferenceCall()) {
64                     Log.v(this, "Number of existing calls is " +
65                             String.valueOf(call.getChildCallIds().size()));
66                     update(callList);
67                 } else {
68                     InCallPresenter.getInstance().showConferenceCallManager(false);
69                 }
70             } else {
71                 InCallPresenter.getInstance().showConferenceCallManager(false);
72             }
73         }
74     }
75 
76     @Override
onDetailsChanged(Call call, android.telecom.Call.Details details)77     public void onDetailsChanged(Call call, android.telecom.Call.Details details) {
78         boolean canDisconnect = details.can(
79                 android.telecom.Call.Details.CAPABILITY_DISCONNECT_FROM_CONFERENCE);
80         boolean canSeparate = details.can(
81                 android.telecom.Call.Details.CAPABILITY_SEPARATE_FROM_CONFERENCE);
82 
83         if (call.can(android.telecom.Call.Details.CAPABILITY_DISCONNECT_FROM_CONFERENCE)
84                 != canDisconnect
85             || call.can(android.telecom.Call.Details.CAPABILITY_SEPARATE_FROM_CONFERENCE)
86                 != canSeparate) {
87             getUi().refreshCall(call);
88         }
89 
90         if (!details.can(
91                 android.telecom.Call.Details.CAPABILITY_MANAGE_CONFERENCE)) {
92             InCallPresenter.getInstance().showConferenceCallManager(false);
93         }
94     }
95 
96     @Override
onIncomingCall(InCallState oldState, InCallState newState, Call call)97     public void onIncomingCall(InCallState oldState, InCallState newState, Call call) {
98         // When incoming call exists, set conference ui invisible.
99         if (getUi().isFragmentVisible()) {
100             Log.d(this, "onIncomingCall()... Conference ui is showing, hide it.");
101             InCallPresenter.getInstance().showConferenceCallManager(false);
102         }
103     }
104 
init(Context context, CallList callList)105     public void init(Context context, CallList callList) {
106         mContext = Preconditions.checkNotNull(context);
107         mContext = context;
108         update(callList);
109     }
110 
111     /**
112      * Updates the conference participant adapter.
113      *
114      * @param callList The callList.
115      */
update(CallList callList)116     private void update(CallList callList) {
117         // callList is non null, but getActiveOrBackgroundCall() may return null
118         final Call currentCall = callList.getActiveOrBackgroundCall();
119         if (currentCall == null) {
120             return;
121         }
122 
123         ArrayList<Call> calls = new ArrayList<>(currentCall.getChildCallIds().size());
124         for (String callerId : currentCall.getChildCallIds()) {
125             calls.add(callList.getCallById(callerId));
126         }
127 
128         Log.d(this, "Number of calls is " + String.valueOf(calls.size()));
129 
130         // Users can split out a call from the conference call if either the active call or the
131         // holding call is empty. If both are filled, users can not split out another call.
132         final boolean hasActiveCall = (callList.getActiveCall() != null);
133         final boolean hasHoldingCall = (callList.getBackgroundCall() != null);
134         boolean canSeparate = !(hasActiveCall && hasHoldingCall);
135 
136         getUi().update(mContext, calls, canSeparate);
137     }
138 
139     public interface ConferenceManagerUi extends Ui {
isFragmentVisible()140         boolean isFragmentVisible();
update(Context context, List<Call> participants, boolean parentCanSeparate)141         void update(Context context, List<Call> participants, boolean parentCanSeparate);
refreshCall(Call call)142         void refreshCall(Call call);
143     }
144 }
145