1 /*
2  * Copyright (C) 2019 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.contentcaptureservice.cts;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 import android.util.Log;
22 import android.view.View;
23 import android.view.ViewStructure;
24 
25 import androidx.annotation.NonNull;
26 
27 import com.android.compatibility.common.util.Visitor;
28 
29 /**
30  * A view that can be used to emulate custom behavior (like virtual children) on
31  * {@link #onProvideContentCaptureStructure(ViewStructure, int)}.
32  */
33 public class CustomView extends View {
34 
35     private static final String TAG = CustomView.class.getSimpleName();
36 
37     private Visitor<ViewStructure> mDelegate;
38 
CustomView(Context context, AttributeSet attrs)39     public CustomView(Context context, AttributeSet attrs) {
40         super(context, attrs);
41         setImportantForContentCapture(View.IMPORTANT_FOR_CONTENT_CAPTURE_YES);
42     }
43 
44     @Override
onProvideContentCaptureStructure(ViewStructure structure, int flags)45     public void onProvideContentCaptureStructure(ViewStructure structure, int flags) {
46         if (mDelegate != null) {
47             Log.d(TAG, "onProvideContentCaptureStructure(): delegating");
48             structure.setClassName(getAccessibilityClassName().toString());
49             mDelegate.visit(structure);
50             Log.d(TAG, "onProvideContentCaptureStructure(): delegated");
51         } else {
52             superOnProvideContentCaptureStructure(structure, flags);
53         }
54     }
55 
56     @Override
getAccessibilityClassName()57     public CharSequence getAccessibilityClassName() {
58         return CustomView.class.getName();
59     }
60 
superOnProvideContentCaptureStructure(@onNull ViewStructure structure, int flags)61     void superOnProvideContentCaptureStructure(@NonNull ViewStructure structure, int flags) {
62         Log.d(TAG, "calling super.onProvideContentCaptureStructure()");
63         super.onProvideContentCaptureStructure(structure, flags);
64     }
65 
setContentCaptureDelegate(@onNull Visitor<ViewStructure> delegate)66     void setContentCaptureDelegate(@NonNull Visitor<ViewStructure> delegate) {
67         mDelegate = delegate;
68     }
69 }
70