1 /*
2  * Copyright (C) 2018 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 package android.contentcaptureservice.cts;
17 
18 import android.app.Activity;
19 import android.os.Bundle;
20 import android.util.Log;
21 import android.view.ViewGroup;
22 import android.widget.LinearLayout;
23 
24 import androidx.annotation.NonNull;
25 
26 import com.android.compatibility.common.util.DoubleVisitor;
27 
28 /**
29  * Base class for classes that have a {@code root_view} root view.
30  */
31 abstract class AbstractRootViewActivity extends AbstractContentCaptureActivity {
32 
33     private static final String TAG = AbstractRootViewActivity.class.getSimpleName();
34 
35     private static DoubleVisitor<AbstractRootViewActivity, LinearLayout> sRootViewVisitor;
36     private static DoubleVisitor<AbstractRootViewActivity, LinearLayout> sOnAnimationVisitor;
37 
38     private LinearLayout mRootView;
39 
40     /**
41      * Sets a visitor called when the activity is created.
42      */
onRootView(@onNull DoubleVisitor<AbstractRootViewActivity, LinearLayout> visitor)43     static void onRootView(@NonNull DoubleVisitor<AbstractRootViewActivity, LinearLayout> visitor) {
44         sRootViewVisitor = visitor;
45     }
46 
47     /**
48      * Sets a visitor to be called on {@link Activity#onEnterAnimationComplete()}.
49      */
onAnimationComplete( @onNull DoubleVisitor<AbstractRootViewActivity, LinearLayout> visitor)50     static void onAnimationComplete(
51             @NonNull DoubleVisitor<AbstractRootViewActivity, LinearLayout> visitor) {
52         sOnAnimationVisitor = visitor;
53     }
54 
55     @Override
onCreate(Bundle savedInstanceState)56     protected final void onCreate(Bundle savedInstanceState) {
57         super.onCreate(savedInstanceState);
58         setContentViewOnCreate(savedInstanceState);
59 
60         mRootView = findViewById(R.id.root_view);
61 
62         Log.d(TAG, "onCreate(): parents for " + getClass() + ": rootView=" + mRootView
63                 + "\ngrandParent=" + getGrandParent()
64                 + "\ngrandGrandParent=" + getGrandGrandParent());
65 
66         if (sRootViewVisitor != null) {
67             Log.d(TAG, "Applying visitor to " + this + "/" + mRootView);
68             try {
69                 sRootViewVisitor.visit(this, mRootView);
70             } finally {
71                 sRootViewVisitor = null;
72             }
73         }
74     }
75 
76     @Override
onResume()77     protected void onResume() {
78         super.onResume();
79 
80         Log.d(TAG, "AutofillIds for " + getClass() + ": "
81                 + " rootView=" + getRootView().getAutofillId()
82                 + ", grandParent=" + getGrandParent().getAutofillId()
83                 + ", grandGrandParent=" + getGrandGrandParent().getAutofillId());
84     }
85 
86     @Override
onEnterAnimationComplete()87     public void onEnterAnimationComplete() {
88         if (sOnAnimationVisitor != null) {
89             Log.i(TAG, "onEnterAnimationComplete(): applying visitor on " + this);
90             try {
91                 sOnAnimationVisitor.visit(this, mRootView);
92             } finally {
93                 sOnAnimationVisitor = null;
94             }
95         } else {
96             Log.i(TAG, "onEnterAnimationComplete(): no visitor on " + this);
97         }
98     }
99 
getRootView()100     public LinearLayout getRootView() {
101         return mRootView;
102     }
103 
104     // TODO(b/122315042): remove this method when not needed anymore
105     @NonNull
getGrandParent()106     public ViewGroup getGrandParent() {
107         return (ViewGroup) mRootView.getParent();
108     }
109 
110     // TODO(b/122315042): remove this method when not needed anymore
111     @NonNull
getGrandGrandParent()112     public ViewGroup getGrandGrandParent() {
113         return (ViewGroup) getGrandParent().getParent();
114     }
115 
116     /**
117      * The real "onCreate" method that should be extended by subclasses.
118      *
119      */
setContentViewOnCreate(Bundle savedInstanceState)120     protected abstract void setContentViewOnCreate(Bundle savedInstanceState);
121 }
122