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.view.contentcapture;
17 
18 import android.annotation.NonNull;
19 import android.annotation.Nullable;
20 import android.graphics.Insets;
21 import android.view.autofill.AutofillId;
22 import android.view.contentcapture.ViewNode.ViewStructureImpl;
23 
24 /**
25  * A session that is explicitly created by the app (and hence is a descendant of
26  * {@link MainContentCaptureSession}).
27  *
28  * @hide
29  */
30 final class ChildContentCaptureSession extends ContentCaptureSession {
31 
32     @NonNull
33     private final ContentCaptureSession mParent;
34 
35     /** @hide */
ChildContentCaptureSession(@onNull ContentCaptureSession parent, @NonNull ContentCaptureContext clientContext)36     protected ChildContentCaptureSession(@NonNull ContentCaptureSession parent,
37             @NonNull ContentCaptureContext clientContext) {
38         super(clientContext);
39         mParent = parent;
40     }
41 
42     @Override
getMainCaptureSession()43     MainContentCaptureSession getMainCaptureSession() {
44         if (mParent instanceof MainContentCaptureSession) {
45             return (MainContentCaptureSession) mParent;
46         }
47         return mParent.getMainCaptureSession();
48     }
49 
50     @Override
newChild(@onNull ContentCaptureContext clientContext)51     ContentCaptureSession newChild(@NonNull ContentCaptureContext clientContext) {
52         final ContentCaptureSession child = new ChildContentCaptureSession(this, clientContext);
53         getMainCaptureSession().notifyChildSessionStarted(mId, child.mId, clientContext);
54         return child;
55     }
56 
57     @Override
flush(@lushReason int reason)58     void flush(@FlushReason int reason) {
59         mParent.flush(reason);
60     }
61 
62     @Override
updateContentCaptureContext(@ullable ContentCaptureContext context)63     public void updateContentCaptureContext(@Nullable ContentCaptureContext context) {
64         getMainCaptureSession().notifyContextUpdated(mId, context);
65     }
66 
67     @Override
onDestroy()68     void onDestroy() {
69         getMainCaptureSession().notifyChildSessionFinished(mParent.mId, mId);
70     }
71 
72     @Override
internalNotifyViewAppeared(@onNull ViewStructureImpl node)73     void internalNotifyViewAppeared(@NonNull ViewStructureImpl node) {
74         getMainCaptureSession().notifyViewAppeared(mId, node);
75     }
76 
77     @Override
internalNotifyViewDisappeared(@onNull AutofillId id)78     void internalNotifyViewDisappeared(@NonNull AutofillId id) {
79         getMainCaptureSession().notifyViewDisappeared(mId, id);
80     }
81 
82     @Override
internalNotifyViewTextChanged(@onNull AutofillId id, @Nullable CharSequence text)83     void internalNotifyViewTextChanged(@NonNull AutofillId id, @Nullable CharSequence text) {
84         getMainCaptureSession().notifyViewTextChanged(mId, id, text);
85     }
86 
87     @Override
internalNotifyViewInsetsChanged(@onNull Insets viewInsets)88     void internalNotifyViewInsetsChanged(@NonNull Insets viewInsets) {
89         getMainCaptureSession().notifyViewInsetsChanged(mId, viewInsets);
90     }
91 
92     @Override
internalNotifyViewTreeEvent(boolean started)93     public void internalNotifyViewTreeEvent(boolean started) {
94         getMainCaptureSession().notifyViewTreeEvent(mId, started);
95     }
96 
97     @Override
internalNotifySessionResumed()98     void internalNotifySessionResumed() {
99         getMainCaptureSession().notifySessionResumed();
100     }
101 
102     @Override
internalNotifySessionPaused()103     void internalNotifySessionPaused() {
104         getMainCaptureSession().notifySessionPaused();
105     }
106 
107     @Override
isContentCaptureEnabled()108     boolean isContentCaptureEnabled() {
109         return getMainCaptureSession().isContentCaptureEnabled();
110     }
111 }
112