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.SystemApi; 20 import android.os.RemoteException; 21 import android.util.Log; 22 23 import com.android.ims.internal.IImsEcbm; 24 import com.android.ims.internal.IImsEcbmListener; 25 26 import java.util.Objects; 27 28 /** 29 * Base implementation of ImsEcbm, which implements stub versions of the methods 30 * in the IImsEcbm AIDL. Override the methods that your implementation of ImsEcbm supports. 31 * 32 * DO NOT remove or change the existing APIs, only add new ones to this Base implementation or you 33 * will break other implementations of ImsEcbm maintained by other ImsServices. 34 * 35 * @hide 36 */ 37 @SystemApi 38 public class ImsEcbmImplBase { 39 private static final String TAG = "ImsEcbmImplBase"; 40 41 private final Object mLock = new Object(); 42 private IImsEcbmListener mListener; 43 private final IImsEcbm mImsEcbm = new IImsEcbm.Stub() { 44 @Override 45 public void setListener(IImsEcbmListener listener) { 46 synchronized (mLock) { 47 if (mListener != null && !mListener.asBinder().isBinderAlive()) { 48 Log.w(TAG, "setListener: discarding dead Binder"); 49 mListener = null; 50 } 51 if (mListener != null && listener != null && Objects.equals( 52 mListener.asBinder(), listener.asBinder())) { 53 return; 54 } 55 if (listener == null) { 56 mListener = null; 57 } else if (listener != null && mListener == null) { 58 mListener = listener; 59 } else { 60 // Warn that the listener is being replaced while active 61 Log.w(TAG, "setListener is being called when there is already an active " 62 + "listener"); 63 mListener = listener; 64 } 65 } 66 } 67 68 @Override 69 public void exitEmergencyCallbackMode() { 70 ImsEcbmImplBase.this.exitEmergencyCallbackMode(); 71 } 72 }; 73 74 /** @hide */ getImsEcbm()75 public IImsEcbm getImsEcbm() { 76 return mImsEcbm; 77 } 78 79 /** 80 * This method should be implemented by the IMS provider. Framework will trigger this method to 81 * request to come out of ECBM mode 82 */ exitEmergencyCallbackMode()83 public void exitEmergencyCallbackMode() { 84 Log.d(TAG, "exitEmergencyCallbackMode() not implemented"); 85 } 86 87 /** 88 * Notifies the framework when the device enters Emergency Callback Mode. 89 * 90 * @throws RuntimeException if the connection to the framework is not available. 91 */ enteredEcbm()92 public final void enteredEcbm() { 93 Log.d(TAG, "Entered ECBM."); 94 IImsEcbmListener listener; 95 synchronized (mLock) { 96 listener = mListener; 97 } 98 if (listener != null) { 99 try { 100 listener.enteredECBM(); 101 } catch (RemoteException e) { 102 throw new RuntimeException(e); 103 } 104 } 105 } 106 107 /** 108 * Notifies the framework when the device exits Emergency Callback Mode. 109 * 110 * @throws RuntimeException if the connection to the framework is not available. 111 */ exitedEcbm()112 public final void exitedEcbm() { 113 Log.d(TAG, "Exited ECBM."); 114 IImsEcbmListener listener; 115 synchronized (mLock) { 116 listener = mListener; 117 } 118 if (listener != null) { 119 try { 120 listener.exitedECBM(); 121 } catch (RemoteException e) { 122 throw new RuntimeException(e); 123 } 124 } 125 } 126 } 127