• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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;
18 
19 import com.android.layoutlib.bridge.impl.Layout;
20 import com.android.layoutlib.bridge.util.InsetUtil;
21 
22 import android.content.Context;
23 import android.graphics.Insets;
24 import android.util.Pair;
25 import android.view.View.AttachInfo;
26 import android.view.Window.OnContentApplyWindowInsetsListener;
27 
28 import static android.view.View.SYSTEM_UI_LAYOUT_FLAGS;
29 
30 /**
31  * Class allowing access to package-protected methods/fields.
32  */
33 public class AttachInfo_Accessor {
34     // Copied from PhoneWindow.java
35     private static final OnContentApplyWindowInsetsListener sDefaultContentInsetsApplier =
36             (view, insets) -> {
37                 if ((view.getWindowSystemUiVisibility() & SYSTEM_UI_LAYOUT_FLAGS) != 0) {
38                     return new Pair<>(Insets.NONE, insets);
39                 }
40                 Insets insetsToApply = insets.getSystemWindowInsets();
41                 return new Pair<>(insetsToApply,
42                         insets.inset(insetsToApply).consumeSystemWindowInsets());
43             };
44 
setAttachInfo(ViewGroup view)45     public static LayoutlibRenderer setAttachInfo(ViewGroup view) {
46         Context context = view.getContext();
47         WindowManagerImpl wm = (WindowManagerImpl)context.getSystemService(Context.WINDOW_SERVICE);
48         wm.setBaseRootView(view);
49         Display display = wm.getDefaultDisplay();
50         ViewRootImpl root = new ViewRootImpl(context, display, new IWindowSession.Default(),
51                 new WindowLayout());
52         root.setOnContentApplyWindowInsetsListener(sDefaultContentInsetsApplier);
53         LayoutlibRenderer renderer = new LayoutlibRenderer(context, false, "layoutlib-renderer");
54         AttachInfo info = root.mAttachInfo;
55         info.mThreadedRenderer = renderer.getThreadedRenderer();
56         info.mHasWindowFocus = true;
57         info.mWindowVisibility = View.VISIBLE;
58         info.mInTouchMode = false; // this is so that we can display selections.
59         info.mHardwareAccelerated = true;
60         info.mApplicationScale = 1.0f;
61         ViewRootImpl_Accessor.setChild(root, view);
62         view.assignParent(root);
63         if (view instanceof Layout) {
64             InsetsController insetsController = root.getInsetsController();
65             wm.createOrUpdateDisplayFrames(insetsController.getState());
66             InsetUtil.setupSysUiInsets(context, insetsController,
67                     ((Layout)view).getInsetsFrameProviders());
68         }
69         view.dispatchAttachedToWindow(info, 0);
70         root.mTmpFrames.displayFrame.set(wm.getCurrentWindowMetrics().getBounds());
71         return renderer;
72     }
73 
dispatchOnPreDraw(View view)74     public static void dispatchOnPreDraw(View view) {
75         view.mAttachInfo.mTreeObserver.dispatchOnPreDraw();
76     }
77 
dispatchOnGlobalLayout(View view)78     public static void dispatchOnGlobalLayout(View view) {
79         view.mAttachInfo.mTreeObserver.dispatchOnGlobalLayout();
80     }
81 
detachFromWindow(final View view)82     public static void detachFromWindow(final View view) {
83         if (view != null) {
84             final View.AttachInfo attachInfo = view.mAttachInfo;
85             view.dispatchDetachedFromWindow();
86             if (attachInfo != null) {
87                 ViewRootImpl_Accessor.detachFromWindow(attachInfo.mViewRootImpl);
88                 final ThreadedRenderer threadedRenderer = attachInfo.mThreadedRenderer;
89                 if(threadedRenderer != null) {
90                     threadedRenderer.destroy();
91                 }
92                 ThreadedRenderer rootRenderer =
93                         attachInfo.mViewRootImpl.mAttachInfo.mThreadedRenderer;
94                 if (rootRenderer != null) {
95                     rootRenderer.destroy();
96                 }
97             }
98         }
99     }
100 
getRootView(View view)101     public static ViewRootImpl getRootView(View view) {
102         return view.mAttachInfo != null ? view.mAttachInfo.mViewRootImpl : null;
103     }
104 }
105