1 /* 2 * Copyright (C) 2017 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.call; 18 19 import android.content.ActivityNotFoundException; 20 import android.content.Intent; 21 import android.os.Looper; 22 import android.support.annotation.MainThread; 23 import android.telecom.InCallService; 24 import com.android.dialer.common.LogUtil; 25 import java.util.List; 26 27 /** Wrapper around Telecom APIs. */ 28 public final class TelecomAdapter implements InCallServiceListener { 29 30 private static final String ADD_CALL_MODE_KEY = "add_call_mode"; 31 32 private static TelecomAdapter sInstance; 33 private InCallService mInCallService; 34 TelecomAdapter()35 private TelecomAdapter() {} 36 37 @MainThread getInstance()38 public static TelecomAdapter getInstance() { 39 if (!Looper.getMainLooper().isCurrentThread()) { 40 throw new IllegalStateException(); 41 } 42 if (sInstance == null) { 43 sInstance = new TelecomAdapter(); 44 } 45 return sInstance; 46 } 47 48 @Override setInCallService(InCallService inCallService)49 public void setInCallService(InCallService inCallService) { 50 mInCallService = inCallService; 51 } 52 53 @Override clearInCallService()54 public void clearInCallService() { 55 mInCallService = null; 56 } 57 getTelecomCallById(String callId)58 private android.telecom.Call getTelecomCallById(String callId) { 59 DialerCall call = CallList.getInstance().getCallById(callId); 60 return call == null ? null : call.getTelecomCall(); 61 } 62 mute(boolean shouldMute)63 public void mute(boolean shouldMute) { 64 if (mInCallService != null) { 65 mInCallService.setMuted(shouldMute); 66 } else { 67 LogUtil.e("TelecomAdapter.mute", "mInCallService is null"); 68 } 69 } 70 setAudioRoute(int route)71 public void setAudioRoute(int route) { 72 if (mInCallService != null) { 73 mInCallService.setAudioRoute(route); 74 } else { 75 LogUtil.e("TelecomAdapter.setAudioRoute", "mInCallService is null"); 76 } 77 } 78 merge(String callId)79 public void merge(String callId) { 80 android.telecom.Call call = getTelecomCallById(callId); 81 if (call != null) { 82 List<android.telecom.Call> conferenceable = call.getConferenceableCalls(); 83 if (!conferenceable.isEmpty()) { 84 call.conference(conferenceable.get(0)); 85 } else { 86 if (call.getDetails().can(android.telecom.Call.Details.CAPABILITY_MERGE_CONFERENCE)) { 87 call.mergeConference(); 88 } 89 } 90 } else { 91 LogUtil.e("TelecomAdapter.merge", "call not in call list " + callId); 92 } 93 } 94 swap(String callId)95 public void swap(String callId) { 96 android.telecom.Call call = getTelecomCallById(callId); 97 if (call != null) { 98 if (call.getDetails().can(android.telecom.Call.Details.CAPABILITY_SWAP_CONFERENCE)) { 99 call.swapConference(); 100 } 101 } else { 102 LogUtil.e("TelecomAdapter.swap", "call not in call list " + callId); 103 } 104 } 105 addCall()106 public void addCall() { 107 if (mInCallService != null) { 108 Intent intent = new Intent(Intent.ACTION_DIAL); 109 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 110 111 // when we request the dialer come up, we also want to inform 112 // it that we're going through the "add call" option from the 113 // InCallScreen / PhoneUtils. 114 intent.putExtra(ADD_CALL_MODE_KEY, true); 115 try { 116 LogUtil.d("TelecomAdapter.addCall", "Sending the add DialerCall intent"); 117 mInCallService.startActivity(intent); 118 } catch (ActivityNotFoundException e) { 119 // This is rather rare but possible. 120 // Note: this method is used even when the phone is encrypted. At that moment 121 // the system may not find any Activity which can accept this Intent. 122 LogUtil.e("TelecomAdapter.addCall", "Activity for adding calls isn't found.", e); 123 } 124 } 125 } 126 playDtmfTone(String callId, char digit)127 public void playDtmfTone(String callId, char digit) { 128 android.telecom.Call call = getTelecomCallById(callId); 129 if (call != null) { 130 call.playDtmfTone(digit); 131 } else { 132 LogUtil.e("TelecomAdapter.playDtmfTone", "call not in call list " + callId); 133 } 134 } 135 stopDtmfTone(String callId)136 public void stopDtmfTone(String callId) { 137 android.telecom.Call call = getTelecomCallById(callId); 138 if (call != null) { 139 call.stopDtmfTone(); 140 } else { 141 LogUtil.e("TelecomAdapter.stopDtmfTone", "call not in call list " + callId); 142 } 143 } 144 postDialContinue(String callId, boolean proceed)145 public void postDialContinue(String callId, boolean proceed) { 146 android.telecom.Call call = getTelecomCallById(callId); 147 if (call != null) { 148 call.postDialContinue(proceed); 149 } else { 150 LogUtil.e("TelecomAdapter.postDialContinue", "call not in call list " + callId); 151 } 152 } 153 canAddCall()154 public boolean canAddCall() { 155 if (mInCallService != null) { 156 return mInCallService.canAddCall(); 157 } 158 return false; 159 } 160 } 161