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 android.telephony.ims.stub; 18 19 import android.annotation.NonNull; 20 import android.annotation.SystemApi; 21 import android.os.RemoteException; 22 import android.telephony.ims.ImsExternalCallState; 23 import android.util.Log; 24 25 import com.android.ims.internal.IImsExternalCallStateListener; 26 import com.android.ims.internal.IImsMultiEndpoint; 27 import com.android.internal.telephony.util.TelephonyUtils; 28 29 import java.util.List; 30 import java.util.Objects; 31 import java.util.concurrent.CancellationException; 32 import java.util.concurrent.CompletableFuture; 33 import java.util.concurrent.CompletionException; 34 import java.util.concurrent.Executor; 35 36 /** 37 * Base implementation of ImsMultiEndpoint, which implements stub versions of the methods 38 * in the IImsMultiEndpoint AIDL. Override the methods that your implementation of 39 * ImsMultiEndpoint supports. 40 * 41 * DO NOT remove or change the existing APIs, only add new ones to this Base implementation or you 42 * will break other implementations of ImsMultiEndpoint maintained by other ImsServices. 43 * 44 * @hide 45 */ 46 @SystemApi 47 public class ImsMultiEndpointImplBase { 48 private static final String TAG = "MultiEndpointImplBase"; 49 50 private IImsExternalCallStateListener mListener; 51 private final Object mLock = new Object(); 52 private Executor mExecutor = Runnable::run; 53 54 private final IImsMultiEndpoint mImsMultiEndpoint = new IImsMultiEndpoint.Stub() { 55 56 @Override 57 public void setListener(IImsExternalCallStateListener listener) throws RemoteException { 58 executeMethodAsync(() -> { 59 if (mListener != null && !mListener.asBinder().isBinderAlive()) { 60 Log.w(TAG, "setListener: discarding dead Binder"); 61 mListener = null; 62 } 63 if (mListener != null && listener != null && Objects.equals( 64 mListener.asBinder(), listener.asBinder())) { 65 return; 66 } 67 68 if (listener == null) { 69 mListener = null; 70 } else if (listener != null && mListener == null) { 71 mListener = listener; 72 } else { 73 // Warn that the listener is being replaced while active 74 Log.w(TAG, "setListener is being called when there is already an active " 75 + "listener"); 76 mListener = listener; 77 } 78 }, "setListener"); 79 } 80 81 @Override 82 public void requestImsExternalCallStateInfo() throws RemoteException { 83 executeMethodAsync(() -> ImsMultiEndpointImplBase.this 84 .requestImsExternalCallStateInfo(), "requestImsExternalCallStateInfo"); 85 } 86 87 // Call the methods with a clean calling identity on the executor and wait indefinitely for 88 // the future to return. 89 private void executeMethodAsync(Runnable r, String errorLogName) { 90 try { 91 CompletableFuture.runAsync( 92 () -> TelephonyUtils.runWithCleanCallingIdentity(r), mExecutor).join(); 93 } catch (CancellationException | CompletionException e) { 94 Log.w(TAG, "ImsMultiEndpointImplBase Binder - " + errorLogName + " exception: " 95 + e.getMessage()); 96 } 97 } 98 }; 99 100 /** @hide */ getIImsMultiEndpoint()101 public IImsMultiEndpoint getIImsMultiEndpoint() { 102 return mImsMultiEndpoint; 103 } 104 105 /** 106 * Notifies framework when Dialog Event Package update is received 107 * 108 * @throws RuntimeException if the connection to the framework is not available. 109 */ onImsExternalCallStateUpdate(List<ImsExternalCallState> externalCallDialogs)110 public final void onImsExternalCallStateUpdate(List<ImsExternalCallState> externalCallDialogs) { 111 Log.d(TAG, "ims external call state update triggered."); 112 IImsExternalCallStateListener listener; 113 synchronized (mLock) { 114 listener = mListener; 115 } 116 if (listener != null) { 117 try { 118 listener.onImsExternalCallStateUpdate(externalCallDialogs); 119 } catch (RemoteException e) { 120 throw new RuntimeException(e); 121 } 122 } 123 } 124 125 /** 126 * This method should be implemented by the IMS provider. Framework will trigger this to get the 127 * latest Dialog Event Package information. Should 128 */ requestImsExternalCallStateInfo()129 public void requestImsExternalCallStateInfo() { 130 Log.d(TAG, "requestImsExternalCallStateInfo() not implemented"); 131 } 132 133 /** 134 * Set default Executor from MmTelFeature. 135 * @param executor The default executor for the framework to use when executing the methods 136 * overridden by the implementation of ImsMultiEndpoint. 137 * @hide 138 */ setDefaultExecutor(@onNull Executor executor)139 public final void setDefaultExecutor(@NonNull Executor executor) { 140 mExecutor = executor; 141 } 142 } 143