1 /* 2 * Copyright (C) 2016 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 import android.support.annotation.FloatRange; 21 import android.support.annotation.NonNull; 22 import android.support.v4.os.UserManagerCompat; 23 import android.telecom.VideoProfile; 24 import com.android.dialer.common.Assert; 25 import com.android.dialer.common.LogUtil; 26 import com.android.dialer.logging.DialerImpression; 27 import com.android.dialer.logging.Logger; 28 import com.android.incallui.answer.protocol.AnswerScreen; 29 import com.android.incallui.answer.protocol.AnswerScreenDelegate; 30 import com.android.incallui.answerproximitysensor.AnswerProximitySensor; 31 import com.android.incallui.answerproximitysensor.PseudoScreenState; 32 import com.android.incallui.call.CallList; 33 import com.android.incallui.call.DialerCall; 34 import com.android.incallui.call.DialerCallListener; 35 36 /** Manages changes for an incoming call screen. */ 37 public class AnswerScreenPresenter 38 implements AnswerScreenDelegate, DialerCall.CannedTextResponsesLoadedListener { 39 @NonNull private final Context context; 40 @NonNull private final AnswerScreen answerScreen; 41 @NonNull private final DialerCall call; 42 43 public AnswerScreenPresenter( 44 @NonNull Context context, @NonNull AnswerScreen answerScreen, @NonNull DialerCall call) { 45 LogUtil.i("AnswerScreenPresenter.constructor", null); 46 this.context = Assert.isNotNull(context); 47 this.answerScreen = Assert.isNotNull(answerScreen); 48 this.call = Assert.isNotNull(call); 49 if (isSmsResponseAllowed(call)) { 50 answerScreen.setTextResponses(call.getCannedSmsResponses()); 51 } 52 call.addCannedTextResponsesLoadedListener(this); 53 54 PseudoScreenState pseudoScreenState = InCallPresenter.getInstance().getPseudoScreenState(); 55 if (AnswerProximitySensor.shouldUse(context, call)) { 56 new AnswerProximitySensor(context, call, pseudoScreenState); 57 } else { 58 pseudoScreenState.setOn(true); 59 } 60 } 61 62 @Override 63 public void onAnswerScreenUnready() { 64 call.removeCannedTextResponsesLoadedListener(this); 65 } 66 67 @Override 68 public void onDismissDialog() { 69 InCallPresenter.getInstance().onDismissDialog(); 70 } 71 72 @Override 73 public void onRejectCallWithMessage(String message) { 74 call.reject(true /* rejectWithMessage */, message); 75 onDismissDialog(); 76 } 77 78 @Override 79 public void onAnswer(boolean answerVideoAsAudio) { 80 if (answerScreen.isVideoUpgradeRequest()) { 81 if (answerVideoAsAudio) { 82 Logger.get(context) 83 .logCallImpression( 84 DialerImpression.Type.VIDEO_CALL_REQUEST_ACCEPTED_AS_AUDIO, 85 call.getUniqueCallId(), 86 call.getTimeAddedMs()); 87 call.getVideoTech().acceptVideoRequestAsAudio(); 88 } else { 89 Logger.get(context) 90 .logCallImpression( 91 DialerImpression.Type.VIDEO_CALL_REQUEST_ACCEPTED, 92 call.getUniqueCallId(), 93 call.getTimeAddedMs()); 94 call.getVideoTech().acceptVideoRequest(); 95 } 96 } else { 97 if (answerVideoAsAudio) { 98 call.answer(VideoProfile.STATE_AUDIO_ONLY); 99 } else { 100 call.answer(); 101 } 102 } 103 } 104 105 @Override 106 public void onReject() { 107 if (answerScreen.isVideoUpgradeRequest()) { 108 Logger.get(context) 109 .logCallImpression( 110 DialerImpression.Type.VIDEO_CALL_REQUEST_DECLINED, 111 call.getUniqueCallId(), 112 call.getTimeAddedMs()); 113 call.getVideoTech().declineVideoRequest(); 114 } else { 115 call.reject(false /* rejectWithMessage */, null); 116 } 117 } 118 119 @Override 120 public void onAnswerAndReleaseCall() { 121 LogUtil.enterBlock("AnswerScreenPresenter.onAnswerAndReleaseCall"); 122 DialerCall activeCall = CallList.getInstance().getActiveCall(); 123 if (activeCall == null) { 124 LogUtil.i("AnswerScreenPresenter.onAnswerAndReleaseCall", "activeCall == null"); 125 onAnswer(false); 126 } else { 127 activeCall.addListener(new AnswerOnDisconnected(activeCall)); 128 activeCall.disconnect(); 129 } 130 } 131 132 @Override 133 public void onCannedTextResponsesLoaded(DialerCall call) { 134 if (isSmsResponseAllowed(call)) { 135 answerScreen.setTextResponses(call.getCannedSmsResponses()); 136 } 137 } 138 139 @Override 140 public void updateWindowBackgroundColor(@FloatRange(from = -1f, to = 1.0f) float progress) { 141 InCallActivity activity = (InCallActivity) answerScreen.getAnswerScreenFragment().getActivity(); 142 if (activity != null) { 143 activity.updateWindowBackgroundColor(progress); 144 } 145 } 146 147 private class AnswerOnDisconnected implements DialerCallListener { 148 149 private final DialerCall disconnectingCall; 150 151 public AnswerOnDisconnected(DialerCall disconnectingCall) { 152 this.disconnectingCall = disconnectingCall; 153 } 154 155 @Override 156 public void onDialerCallDisconnect() { 157 LogUtil.i( 158 "AnswerScreenPresenter.AnswerOnDisconnected", "call disconnected, answering new call"); 159 call.answer(); 160 disconnectingCall.removeListener(this); 161 } 162 163 @Override 164 public void onDialerCallUpdate() {} 165 166 @Override 167 public void onDialerCallChildNumberChange() {} 168 169 @Override 170 public void onDialerCallLastForwardedNumberChange() {} 171 172 @Override 173 public void onDialerCallUpgradeToVideo() {} 174 175 @Override 176 public void onDialerCallSessionModificationStateChange() {} 177 178 @Override 179 public void onWiFiToLteHandover() {} 180 181 @Override 182 public void onHandoverToWifiFailure() {} 183 184 @Override 185 public void onInternationalCallOnWifi() {} 186 } 187 188 private boolean isSmsResponseAllowed(DialerCall call) { 189 return UserManagerCompat.isUserUnlocked(context) 190 && call.can(android.telecom.Call.Details.CAPABILITY_RESPOND_VIA_TEXT); 191 } 192 } 193