1 /*
2  * Copyright (C) 2021 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.server.telecom;
18 
19 import android.annotation.NonNull;
20 import android.os.Binder;
21 import android.os.RemoteException;
22 import android.telecom.CallDiagnosticService;
23 import android.telecom.CallDiagnostics;
24 import android.telecom.Log;
25 
26 import com.android.internal.telecom.ICallDiagnosticServiceAdapter;
27 
28 /**
29  * Adapter class used to provide a path for messages FROM a {@link CallDiagnosticService} back to
30  * the telecom stack.
31  */
32 public class CallDiagnosticServiceAdapter extends ICallDiagnosticServiceAdapter.Stub {
33     public interface TelecomAdapter {
34         void displayDiagnosticMessage(String callId, int messageId, CharSequence message);
35         void clearDiagnosticMessage(String callId, int messageId);
36         void sendDeviceToDeviceMessage(String callId, @CallDiagnostics.MessageType int message,
37                 int value);
38         void overrideDisconnectMessage(String callId, CharSequence message);
39     }
40 
41     private final TelecomAdapter mTelecomAdapter;
42     private final String mOwnerPackageName;
43     private final String mOwnerPackageAbbreviation;
44     private final TelecomSystem.SyncRoot mLock;
45 
46     CallDiagnosticServiceAdapter(@NonNull TelecomAdapter telecomAdapter,
47             @NonNull String ownerPackageName, @NonNull TelecomSystem.SyncRoot lock) {
48         mTelecomAdapter = telecomAdapter;
49         mOwnerPackageName = ownerPackageName;
50         mOwnerPackageAbbreviation = Log.getPackageAbbreviation(ownerPackageName);
51         mLock = lock;
52     }
53 
54     @Override
55     public void displayDiagnosticMessage(String callId, int messageId, CharSequence message)
56             throws RemoteException {
57         try {
58             Log.startSession("CDSA.dDM", mOwnerPackageAbbreviation);
59             long token = Binder.clearCallingIdentity();
60             try {
61                 synchronized (mLock) {
62                     Log.i(this, "displayDiagnosticMessage; callId=%s, msg=%d/%s", callId, messageId,
63                             message);
64                     mTelecomAdapter.displayDiagnosticMessage(callId, messageId, message);
65                 }
66             } finally {
67                 Binder.restoreCallingIdentity(token);
68             }
69         } finally {
70             Log.endSession();
71         }
72     }
73 
74     @Override
75     public void clearDiagnosticMessage(String callId, int messageId) throws RemoteException {
76         try {
77             Log.startSession("CDSA.cDM", mOwnerPackageAbbreviation);
78             long token = Binder.clearCallingIdentity();
79             try {
80                 synchronized (mLock) {
81                     Log.i(this, "clearDiagnosticMessage; callId=%s, msg=%d", callId, messageId);
82                     mTelecomAdapter.clearDiagnosticMessage(callId, messageId);
83                 }
84             } finally {
85                 Binder.restoreCallingIdentity(token);
86             }
87         } finally {
88             Log.endSession();
89         }
90     }
91 
92     @Override
93     public void sendDeviceToDeviceMessage(String callId, @CallDiagnostics.MessageType int message,
94             int value)
95             throws RemoteException {
96         try {
97             Log.startSession("CDSA.sDTDM", mOwnerPackageAbbreviation);
98             long token = Binder.clearCallingIdentity();
99             try {
100                 synchronized (mLock) {
101                     Log.i(this, "sendDeviceToDeviceMessage; callId=%s, msg=%d/%d", callId, message,
102                             value);
103                     mTelecomAdapter.sendDeviceToDeviceMessage(callId, message, value);
104                 }
105             } finally {
106                 Binder.restoreCallingIdentity(token);
107             }
108         } finally {
109             Log.endSession();
110         }
111     }
112 
113     @Override
114     public void overrideDisconnectMessage(String callId, CharSequence message)
115             throws RemoteException {
116         try {
117             Log.startSession("CDSA.oDM", mOwnerPackageAbbreviation);
118             long token = Binder.clearCallingIdentity();
119             try {
120                 synchronized (mLock) {
121                     Log.i(this, "overrideDisconnectMessage; callId=%s, msg=%s", callId, message);
122                     mTelecomAdapter.overrideDisconnectMessage(callId, message);
123                 }
124             } finally {
125                 Binder.restoreCallingIdentity(token);
126             }
127         } finally {
128             Log.endSession();
129         }
130     }
131 }
132