1 /*
2  * Copyright (C) 2020 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.internal.inputmethod;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.util.proto.ProtoOutputStream;
22 import android.view.inputmethod.InputMethodManager;
23 import android.view.inputmethod.InputMethodManagerGlobal;
24 
25 import java.io.PrintWriter;
26 
27 /**
28  * An implementation of {@link ImeTracing} for non system_server processes.
29  */
30 class ImeTracingClientImpl extends ImeTracing {
ImeTracingClientImpl()31     ImeTracingClientImpl() {
32         sEnabled = InputMethodManagerGlobal.isImeTraceEnabled();
33     }
34 
35     @Override
addToBuffer(ProtoOutputStream proto, int source)36     public void addToBuffer(ProtoOutputStream proto, int source) {
37     }
38 
39     @Override
triggerClientDump(String where, @NonNull InputMethodManager immInstance, @Nullable byte[] icProto)40     public void triggerClientDump(String where, @NonNull InputMethodManager immInstance,
41             @Nullable byte[] icProto) {
42         if (!isEnabled() || !isAvailable()) {
43             return;
44         }
45 
46         synchronized (mDumpInProgressLock) {
47             if (mDumpInProgress) {
48                 return;
49             }
50             mDumpInProgress = true;
51         }
52 
53         try {
54             ProtoOutputStream proto = new ProtoOutputStream();
55             immInstance.dumpDebug(proto, icProto);
56             sendToService(proto.getBytes(), IME_TRACING_FROM_CLIENT, where);
57         } finally {
58             mDumpInProgress = false;
59         }
60     }
61 
62     @Override
triggerServiceDump(String where, @NonNull ServiceDumper dumper, @Nullable byte[] icProto)63     public void triggerServiceDump(String where, @NonNull ServiceDumper dumper,
64             @Nullable byte[] icProto) {
65         if (!isEnabled() || !isAvailable()) {
66             return;
67         }
68 
69         synchronized (mDumpInProgressLock) {
70             if (mDumpInProgress) {
71                 return;
72             }
73             mDumpInProgress = true;
74         }
75 
76         try {
77             ProtoOutputStream proto = new ProtoOutputStream();
78             dumper.dumpToProto(proto, icProto);
79             sendToService(proto.getBytes(), IME_TRACING_FROM_IMS, where);
80         } finally {
81             mDumpInProgress = false;
82         }
83     }
84 
85     @Override
triggerManagerServiceDump(String where)86     public void triggerManagerServiceDump(String where) {
87         // Intentionally left empty, this is implemented in ImeTracingServerImpl
88     }
89 
90     @Override
startTrace(PrintWriter pw)91     public void startTrace(PrintWriter pw) {
92     }
93 
94     @Override
stopTrace(PrintWriter pw)95     public void stopTrace(PrintWriter pw) {
96     }
97 }
98