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.telephony.imsmedia;
18 
19 import android.os.Binder;
20 import android.os.IBinder;
21 
22 import java.util.concurrent.Executor;
23 
24 /**
25  * Text session callback APIs
26  *
27  * @hide
28  */
29 public class TextSessionCallback extends ImsMediaManager.SessionCallback {
30 
31     private final CallbackBinder mCallbackBinder = new CallbackBinder(this);
32 
33     /** @hide */
34     @Override
getBinder()35     public IBinder getBinder() {
36         return mCallbackBinder;
37     }
38 
39     /** @hide */
40     @Override
setExecutor(final Executor executor)41     public void setExecutor(final Executor executor) {
42         mCallbackBinder.setExecutor(executor);
43     }
44 
45     private static class CallbackBinder extends IImsTextSessionCallback.Stub {
46         private final TextSessionCallback mLocalCallback;
47         private Executor mExecutor;
48 
CallbackBinder(final TextSessionCallback localCallback)49         CallbackBinder(final TextSessionCallback localCallback) {
50             mLocalCallback = localCallback;
51         }
52 
53         @Override
onOpenSessionSuccess(final IImsTextSession session)54         public void onOpenSessionSuccess(final IImsTextSession session) {
55             if (mLocalCallback == null) return;
56 
57             final long callingIdentity = Binder.clearCallingIdentity();
58             try {
59                 mExecutor.execute(()
60                         -> mLocalCallback.onOpenSessionSuccess(new ImsTextSession(session)));
61             } finally {
62                 restoreCallingIdentity(callingIdentity);
63             }
64         }
65 
66         @Override
onOpenSessionFailure(final int error)67         public void onOpenSessionFailure(final int error) {
68             if (mLocalCallback == null) return;
69 
70             final long callingIdentity = Binder.clearCallingIdentity();
71             try {
72                 mExecutor.execute(() -> mLocalCallback.onOpenSessionFailure(error));
73             } finally {
74                 restoreCallingIdentity(callingIdentity);
75             }
76         }
77 
78         @Override
onSessionClosed()79         public void onSessionClosed() {
80             if (mLocalCallback == null) return;
81 
82             final long callingIdentity = Binder.clearCallingIdentity();
83             try {
84                 mExecutor.execute(() -> mLocalCallback.onSessionClosed());
85             } finally {
86                 restoreCallingIdentity(callingIdentity);
87             }
88         }
89 
90         @Override
onModifySessionResponse(final TextConfig config, final @ImsMediaSession.SessionOperationResult int result)91         public void onModifySessionResponse(final TextConfig config,
92                 final @ImsMediaSession.SessionOperationResult int result) {
93             if (mLocalCallback == null) return;
94 
95             final long callingIdentity = Binder.clearCallingIdentity();
96             try {
97                 mExecutor.execute(() -> mLocalCallback.onModifySessionResponse(config, result));
98             } finally {
99                 restoreCallingIdentity(callingIdentity);
100             }
101         }
102 
103         @Override
notifyMediaInactivity(final int packetType)104         public void notifyMediaInactivity(final int packetType) {
105             if (mLocalCallback == null) return;
106 
107             final long callingIdentity = Binder.clearCallingIdentity();
108             try {
109                 mExecutor.execute(() -> mLocalCallback.notifyMediaInactivity(packetType));
110             } finally {
111                 restoreCallingIdentity(callingIdentity);
112             }
113         }
114 
115         @Override
onRttReceived(final String text)116         public void onRttReceived(final String text) {
117             if (mLocalCallback == null) return;
118 
119             final long callingIdentity = Binder.clearCallingIdentity();
120             try {
121                 mExecutor.execute(() -> mLocalCallback.onRttReceived(text));
122             } finally {
123                 restoreCallingIdentity(callingIdentity);
124             }
125         }
126 
setExecutor(final Executor executor)127         private void setExecutor(final Executor executor) {
128             mExecutor = executor;
129         }
130     }
131 
132     /**
133      * Called when ImsMediaSession#modifySession() API is handled
134      *
135      * @param config The TextConfig passed in ImsMediaSession#modifySession()
136      * @param result The result of modify session
137      */
onModifySessionResponse(final TextConfig config, final @ImsMediaSession.SessionOperationResult int result)138     public void onModifySessionResponse(final TextConfig config,
139             final @ImsMediaSession.SessionOperationResult int result) {
140         // Base Implementation
141     }
142 
143     /**
144      * Called when there is text stream received from the network
145      *
146      * @param text The text string received from the network
147      */
onRttReceived(final String text)148     public void onRttReceived(final String text) {
149         // Base Implementation
150     }
151 
152     /**
153      * Notifies media inactivity observed as per thresholds set by
154      * setMediaQualityThreshold() API
155      *
156      * @param packetType either RTP or RTCP
157      */
notifyMediaInactivity(final int packetType)158     public void notifyMediaInactivity(final int packetType) {
159          // Base Implementation
160     }
161 }
162