1 /*
2  * Copyright (C) 2023 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.view.contentprotection;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.view.contentcapture.ContentCaptureEvent;
22 import android.view.contentcapture.ViewNode;
23 
24 /**
25  * Utilities for reading data from {@link ContentCaptureEvent} and {@link ViewNode}.
26  *
27  * @hide
28  */
29 public final class ContentProtectionUtils {
30 
31     /** Returns the text extracted directly from the {@link ContentCaptureEvent}, if set. */
32     @Nullable
getEventText(@onNull ContentCaptureEvent event)33     public static String getEventText(@NonNull ContentCaptureEvent event) {
34         CharSequence text = event.getText();
35         if (text == null) {
36             return null;
37         }
38         return text.toString();
39     }
40 
41     /** Returns the text extracted from the event's {@link ViewNode}, if set. */
42     @Nullable
getViewNodeText(@onNull ContentCaptureEvent event)43     public static String getViewNodeText(@NonNull ContentCaptureEvent event) {
44         ViewNode viewNode = event.getViewNode();
45         if (viewNode == null) {
46             return null;
47         }
48         return getViewNodeText(viewNode);
49     }
50 
51     /** Returns the text extracted directly from the {@link ViewNode}, if set. */
52     @Nullable
getViewNodeText(@onNull ViewNode viewNode)53     public static String getViewNodeText(@NonNull ViewNode viewNode) {
54         CharSequence text = viewNode.getText();
55         if (text == null) {
56             return null;
57         }
58         return text.toString();
59     }
60 }
61