1 /*
2  * Copyright 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.car.rotary;
18 
19 import android.content.Intent;
20 import android.content.pm.PackageManager;
21 import android.content.pm.ResolveInfo;
22 import android.text.TextUtils;
23 import android.view.accessibility.AccessibilityNodeInfo;
24 
25 import androidx.annotation.NonNull;
26 import androidx.annotation.Nullable;
27 import androidx.annotation.VisibleForTesting;
28 
29 import com.android.internal.util.dump.DualDumpOutputStream;
30 
31 import java.util.HashSet;
32 import java.util.List;
33 import java.util.Set;
34 
35 /**
36  * A helper class to support apps using {@link android.view.SurfaceView} for off-process rendering.
37  * <p>
38  * There are two kinds of apps involved in the off-process rendering process: the client apps and
39  * the host app. A client app holds a {@link android.view.SurfaceView} and delegates its rendering
40  * process to the host app. The host app uses the data provided by the client app to render the app
41  * content in the surface provided by the SurfaceView.
42  * <p>
43  * Although both the client app and the host app have independent <strong>view</strong> hierarchies,
44  * their <strong>node</strong> hierarchies are connected. The node hierarchy of the host app is
45  * embedded into the node hierarchy of the client app. To be more specific, the root node of the
46  * host app is the only child of the SurfaceView node, which is a leaf node of the client app.
47  */
48 class SurfaceViewHelper {
49 
50     /** The intent action to be used by the host app to bind to the RendererService. */
51     private static final String RENDER_ACTION = "android.car.template.host.RendererService";
52 
53     /** Package names of the client apps. */
54     private final Set<CharSequence> mClientApps = new HashSet<>();
55 
56     /** Package name of the host app. */
57     @Nullable
58     @VisibleForTesting
59     String mHostApp;
60 
61     /** Initializes the package name of the host app. */
initHostApp(@onNull PackageManager packageManager)62     void initHostApp(@NonNull PackageManager packageManager) {
63         List<ResolveInfo> rendererServices = packageManager.queryIntentServices(
64                 new Intent(RENDER_ACTION), PackageManager.GET_RESOLVED_FILTER);
65         if (rendererServices == null || rendererServices.isEmpty()) {
66             L.v("No host app found");
67             return;
68         }
69         mHostApp = rendererServices.get(0).serviceInfo.packageName;
70         L.v("Host app has been initialized: " + mHostApp);
71     }
72 
73     /** Clears the package name of the host app if the given {@code packageName} matches. */
clearHostApp(@onNull String packageName)74     void clearHostApp(@NonNull String packageName) {
75         if (packageName.equals(mHostApp)) {
76             mHostApp = null;
77             L.v("Host app has been set to null");
78         }
79     }
80 
81     /** Returns whether it supports AAOS template apps. */
supportTemplateApp()82     boolean supportTemplateApp() {
83         return !TextUtils.isEmpty(mHostApp);
84     }
85 
86     /** Adds the package name of the client app. */
addClientApp(@onNull CharSequence clientAppPackageName)87     void addClientApp(@NonNull CharSequence clientAppPackageName) {
88         mClientApps.add(clientAppPackageName);
89     }
90 
91     /** Returns whether the given {@code node} represents a view of the host app. */
isHostNode(@onNull AccessibilityNodeInfo node)92     boolean isHostNode(@NonNull AccessibilityNodeInfo node) {
93         return !TextUtils.isEmpty(mHostApp) && mHostApp.equals(node.getPackageName());
94     }
95 
96     /** Returns whether the given {@code node} represents a view of the client app. */
isClientNode(@onNull AccessibilityNodeInfo node)97     boolean isClientNode(@NonNull AccessibilityNodeInfo node) {
98         return mClientApps.contains(node.getPackageName());
99     }
100 
dump(@onNull DualDumpOutputStream dumpOutputStream, boolean dumpAsProto, @NonNull String fieldName, long fieldId)101     void dump(@NonNull DualDumpOutputStream dumpOutputStream, boolean dumpAsProto,
102             @NonNull String fieldName, long fieldId) {
103         long fieldToken = dumpOutputStream.start(fieldName, fieldId);
104         dumpOutputStream.write("hostApp", RotaryProtos.SurfaceViewHelper.HOST_APP, mHostApp);
105         DumpUtils.writeCharSequences(dumpOutputStream, dumpAsProto, "clientApps",
106                 RotaryProtos.SurfaceViewHelper.CLIENT_APPS, mClientApps);
107         dumpOutputStream.end(fieldToken);
108     }
109 
110 }
111