Grace Jia | ef5a4cc | 2022-12-13 11:08:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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.telecom; |
| 18 | |
| 19 | import android.annotation.IntDef; |
| 20 | import android.annotation.NonNull; |
| 21 | import android.annotation.SdkConstant; |
Grace Jia | acd4dad | 2023-01-17 18:14:36 -0800 | [diff] [blame] | 22 | import android.annotation.SystemApi; |
Grace Jia | ef5a4cc | 2022-12-13 11:08:55 -0800 | [diff] [blame] | 23 | import android.app.Service; |
| 24 | import android.content.Intent; |
| 25 | import android.os.Handler; |
| 26 | import android.os.IBinder; |
| 27 | import android.os.Looper; |
| 28 | import android.os.Message; |
| 29 | import android.os.RemoteException; |
| 30 | |
| 31 | import androidx.annotation.Nullable; |
| 32 | |
| 33 | import com.android.internal.telecom.ICallStreamingService; |
| 34 | import com.android.internal.telecom.IStreamingCallAdapter; |
| 35 | |
| 36 | import java.lang.annotation.Retention; |
| 37 | import java.lang.annotation.RetentionPolicy; |
| 38 | |
| 39 | /** |
| 40 | * This service is implemented by an app that wishes to provide functionality for a general call |
| 41 | * streaming sender for voip calls. |
Grace Jia | acd4dad | 2023-01-17 18:14:36 -0800 | [diff] [blame] | 42 | * <p> |
| 43 | * Below is an example manifest registration for a {@code CallStreamingService}. |
| 44 | * <pre> |
| 45 | * {@code |
| 46 | * <service android:name=".EgCallStreamingService" |
| 47 | * android:permission="android.permission.BIND_CALL_STREAMING_SERVICE" > |
| 48 | * ... |
| 49 | * <intent-filter> |
| 50 | * <action android:name="android.telecom.CallStreamingService" /> |
| 51 | * </intent-filter> |
| 52 | * </service> |
| 53 | * } |
| 54 | * </pre> |
Thomas Stuart | 6e37785 | 2023-04-18 16:54:52 -0700 | [diff] [blame] | 55 | * |
Grace Jia | acd4dad | 2023-01-17 18:14:36 -0800 | [diff] [blame] | 56 | * @hide |
Grace Jia | ef5a4cc | 2022-12-13 11:08:55 -0800 | [diff] [blame] | 57 | */ |
Grace Jia | acd4dad | 2023-01-17 18:14:36 -0800 | [diff] [blame] | 58 | @SystemApi |
Grace Jia | ef5a4cc | 2022-12-13 11:08:55 -0800 | [diff] [blame] | 59 | public abstract class CallStreamingService extends Service { |
| 60 | /** |
| 61 | * The {@link android.content.Intent} that must be declared as handled by the service. |
| 62 | */ |
| 63 | @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION) |
| 64 | public static final String SERVICE_INTERFACE = "android.telecom.CallStreamingService"; |
| 65 | |
| 66 | private static final int MSG_SET_STREAMING_CALL_ADAPTER = 1; |
| 67 | private static final int MSG_CALL_STREAMING_STARTED = 2; |
| 68 | private static final int MSG_CALL_STREAMING_STOPPED = 3; |
Thomas Stuart | 6e37785 | 2023-04-18 16:54:52 -0700 | [diff] [blame] | 69 | private static final int MSG_CALL_STREAMING_STATE_CHANGED = 4; |
Grace Jia | ef5a4cc | 2022-12-13 11:08:55 -0800 | [diff] [blame] | 70 | |
| 71 | /** Default Handler used to consolidate binder method calls onto a single thread. */ |
| 72 | private final Handler mHandler = new Handler(Looper.getMainLooper()) { |
| 73 | @Override |
| 74 | public void handleMessage(Message msg) { |
| 75 | if (mStreamingCallAdapter == null && msg.what != MSG_SET_STREAMING_CALL_ADAPTER) { |
Tyler Gunn | 8ae4277 | 2023-04-26 02:41:41 +0000 | [diff] [blame] | 76 | Log.i(this, "handleMessage: null adapter!"); |
Grace Jia | ef5a4cc | 2022-12-13 11:08:55 -0800 | [diff] [blame] | 77 | return; |
| 78 | } |
| 79 | |
| 80 | switch (msg.what) { |
| 81 | case MSG_SET_STREAMING_CALL_ADAPTER: |
Thomas Stuart | 6e37785 | 2023-04-18 16:54:52 -0700 | [diff] [blame] | 82 | if (msg.obj != null) { |
Tyler Gunn | 8ae4277 | 2023-04-26 02:41:41 +0000 | [diff] [blame] | 83 | Log.i(this, "MSG_SET_STREAMING_CALL_ADAPTER"); |
Thomas Stuart | 6e37785 | 2023-04-18 16:54:52 -0700 | [diff] [blame] | 84 | mStreamingCallAdapter = new StreamingCallAdapter( |
| 85 | (IStreamingCallAdapter) msg.obj); |
| 86 | } |
Grace Jia | ef5a4cc | 2022-12-13 11:08:55 -0800 | [diff] [blame] | 87 | break; |
| 88 | case MSG_CALL_STREAMING_STARTED: |
Tyler Gunn | 8ae4277 | 2023-04-26 02:41:41 +0000 | [diff] [blame] | 89 | Log.i(this, "MSG_CALL_STREAMING_STARTED"); |
Grace Jia | ef5a4cc | 2022-12-13 11:08:55 -0800 | [diff] [blame] | 90 | mCall = (StreamingCall) msg.obj; |
| 91 | mCall.setAdapter(mStreamingCallAdapter); |
| 92 | CallStreamingService.this.onCallStreamingStarted(mCall); |
| 93 | break; |
| 94 | case MSG_CALL_STREAMING_STOPPED: |
Tyler Gunn | 8ae4277 | 2023-04-26 02:41:41 +0000 | [diff] [blame] | 95 | Log.i(this, "MSG_CALL_STREAMING_STOPPED"); |
Grace Jia | ef5a4cc | 2022-12-13 11:08:55 -0800 | [diff] [blame] | 96 | mCall = null; |
| 97 | mStreamingCallAdapter = null; |
| 98 | CallStreamingService.this.onCallStreamingStopped(); |
| 99 | break; |
Thomas Stuart | 6e37785 | 2023-04-18 16:54:52 -0700 | [diff] [blame] | 100 | case MSG_CALL_STREAMING_STATE_CHANGED: |
Grace Jia | ef5a4cc | 2022-12-13 11:08:55 -0800 | [diff] [blame] | 101 | int state = (int) msg.obj; |
Thomas Stuart | 6e37785 | 2023-04-18 16:54:52 -0700 | [diff] [blame] | 102 | if (mStreamingCallAdapter != null) { |
| 103 | mCall.requestStreamingState(state); |
| 104 | CallStreamingService.this.onCallStreamingStateChanged(state); |
| 105 | } |
Grace Jia | ef5a4cc | 2022-12-13 11:08:55 -0800 | [diff] [blame] | 106 | break; |
| 107 | default: |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | }; |
| 112 | |
| 113 | @Nullable |
| 114 | @Override |
| 115 | public IBinder onBind(@NonNull Intent intent) { |
Tyler Gunn | 8ae4277 | 2023-04-26 02:41:41 +0000 | [diff] [blame] | 116 | Log.i(this, "onBind"); |
Grace Jia | ef5a4cc | 2022-12-13 11:08:55 -0800 | [diff] [blame] | 117 | return new CallStreamingServiceBinder(); |
| 118 | } |
| 119 | |
| 120 | /** Manages the binder calls so that the implementor does not need to deal with it. */ |
| 121 | private final class CallStreamingServiceBinder extends ICallStreamingService.Stub { |
| 122 | @Override |
| 123 | public void setStreamingCallAdapter(IStreamingCallAdapter streamingCallAdapter) |
| 124 | throws RemoteException { |
Tyler Gunn | 8ae4277 | 2023-04-26 02:41:41 +0000 | [diff] [blame] | 125 | Log.i(this, "setCallStreamingAdapter"); |
| 126 | mHandler.obtainMessage(MSG_SET_STREAMING_CALL_ADAPTER, streamingCallAdapter) |
Grace Jia | ef5a4cc | 2022-12-13 11:08:55 -0800 | [diff] [blame] | 127 | .sendToTarget(); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void onCallStreamingStarted(StreamingCall call) throws RemoteException { |
Tyler Gunn | 8ae4277 | 2023-04-26 02:41:41 +0000 | [diff] [blame] | 132 | Log.i(this, "onCallStreamingStarted"); |
Grace Jia | ef5a4cc | 2022-12-13 11:08:55 -0800 | [diff] [blame] | 133 | mHandler.obtainMessage(MSG_CALL_STREAMING_STARTED, call).sendToTarget(); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public void onCallStreamingStopped() throws RemoteException { |
| 138 | mHandler.obtainMessage(MSG_CALL_STREAMING_STOPPED).sendToTarget(); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public void onCallStreamingStateChanged(int state) throws RemoteException { |
Thomas Stuart | 6e37785 | 2023-04-18 16:54:52 -0700 | [diff] [blame] | 143 | mHandler.obtainMessage(MSG_CALL_STREAMING_STATE_CHANGED, state).sendToTarget(); |
Grace Jia | ef5a4cc | 2022-12-13 11:08:55 -0800 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Call streaming request reject reason used with |
| 149 | * {@link CallEventCallback#onCallStreamingFailed(int)} to indicate that telecom is rejecting a |
Grace Jia | acd4dad | 2023-01-17 18:14:36 -0800 | [diff] [blame] | 150 | * call streaming request due to unknown reason. |
| 151 | */ |
| 152 | public static final int STREAMING_FAILED_UNKNOWN = 0; |
| 153 | |
| 154 | /** |
| 155 | * Call streaming request reject reason used with |
| 156 | * {@link CallEventCallback#onCallStreamingFailed(int)} to indicate that telecom is rejecting a |
Grace Jia | ef5a4cc | 2022-12-13 11:08:55 -0800 | [diff] [blame] | 157 | * call streaming request because there's an ongoing streaming call on this device. |
| 158 | */ |
| 159 | public static final int STREAMING_FAILED_ALREADY_STREAMING = 1; |
| 160 | |
| 161 | /** |
| 162 | * Call streaming request reject reason used with |
| 163 | * {@link CallEventCallback#onCallStreamingFailed(int)} to indicate that telecom is rejecting a |
| 164 | * call streaming request because telecom can't find existing general streaming sender on this |
| 165 | * device. |
| 166 | */ |
| 167 | public static final int STREAMING_FAILED_NO_SENDER = 2; |
| 168 | |
| 169 | /** |
| 170 | * Call streaming request reject reason used with |
| 171 | * {@link CallEventCallback#onCallStreamingFailed(int)} to indicate that telecom is rejecting a |
| 172 | * call streaming request because telecom can't bind to the general streaming sender app. |
| 173 | */ |
| 174 | public static final int STREAMING_FAILED_SENDER_BINDING_ERROR = 3; |
| 175 | |
| 176 | private StreamingCallAdapter mStreamingCallAdapter; |
| 177 | private StreamingCall mCall; |
| 178 | |
| 179 | /** |
| 180 | * @hide |
| 181 | */ |
| 182 | @IntDef(prefix = {"STREAMING_FAILED"}, |
| 183 | value = { |
Grace Jia | acd4dad | 2023-01-17 18:14:36 -0800 | [diff] [blame] | 184 | STREAMING_FAILED_UNKNOWN, |
Grace Jia | ef5a4cc | 2022-12-13 11:08:55 -0800 | [diff] [blame] | 185 | STREAMING_FAILED_ALREADY_STREAMING, |
| 186 | STREAMING_FAILED_NO_SENDER, |
| 187 | STREAMING_FAILED_SENDER_BINDING_ERROR |
Thomas Stuart | 6e37785 | 2023-04-18 16:54:52 -0700 | [diff] [blame] | 188 | }) |
Grace Jia | ef5a4cc | 2022-12-13 11:08:55 -0800 | [diff] [blame] | 189 | @Retention(RetentionPolicy.SOURCE) |
Thomas Stuart | 6e37785 | 2023-04-18 16:54:52 -0700 | [diff] [blame] | 190 | public @interface StreamingFailedReason { |
| 191 | } |
| 192 | |
| 193 | ; |
Grace Jia | ef5a4cc | 2022-12-13 11:08:55 -0800 | [diff] [blame] | 194 | |
| 195 | /** |
| 196 | * Called when a {@code StreamingCall} has been added to this call streaming session. The call |
| 197 | * streaming sender should start to intercept the device audio using audio records and audio |
| 198 | * tracks from Audio frameworks. |
| 199 | * |
| 200 | * @param call a newly added {@code StreamingCall}. |
| 201 | */ |
| 202 | public void onCallStreamingStarted(@NonNull StreamingCall call) { |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Called when a current {@code StreamingCall} has been removed from this call streaming |
| 207 | * session. The call streaming sender should notify the streaming receiver that the call is |
| 208 | * stopped streaming and stop the device audio interception. |
| 209 | */ |
| 210 | public void onCallStreamingStopped() { |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Called when the streaming state of current {@code StreamingCall} changed. General streaming |
| 215 | * sender usually get notified of the holding/unholding from the original owner voip app of the |
| 216 | * call. |
| 217 | */ |
| 218 | public void onCallStreamingStateChanged(@StreamingCall.StreamingCallState int state) { |
| 219 | } |
| 220 | } |