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 com.android.systemui.shared.system;
17 
18 import android.graphics.Canvas;
19 import android.graphics.Rect;
20 import android.graphics.RecordingCanvas;
21 import android.view.View;
22 import android.view.ViewRootImpl;
23 import android.view.WindowCallbacks;
24 
25 public class WindowCallbacksCompat {
26 
27     private final WindowCallbacks mWindowCallbacks = new WindowCallbacks() {
28         @Override
29         public void onWindowSizeIsChanging(Rect newBounds, boolean fullscreen, Rect systemInsets,
30                 Rect stableInsets) {
31             WindowCallbacksCompat.this.onWindowSizeIsChanging(newBounds, fullscreen, systemInsets,
32                     stableInsets);
33         }
34 
35         @Override
36         public void onWindowDragResizeStart(Rect initialBounds, boolean fullscreen,
37                 Rect systemInsets, Rect stableInsets, int resizeMode) {
38             WindowCallbacksCompat.this.onWindowDragResizeStart(initialBounds, fullscreen,
39                     systemInsets, stableInsets, resizeMode);
40         }
41 
42         @Override
43         public void onWindowDragResizeEnd() {
44             WindowCallbacksCompat.this.onWindowDragResizeEnd();
45         }
46 
47         @Override
48         public boolean onContentDrawn(int offsetX, int offsetY, int sizeX, int sizeY) {
49             return WindowCallbacksCompat.this.onContentDrawn(offsetX, offsetY, sizeX, sizeY);
50         }
51 
52         @Override
53         public void onRequestDraw(boolean reportNextDraw) {
54             WindowCallbacksCompat.this.onRequestDraw(reportNextDraw);
55         }
56 
57         @Override
58         public void onPostDraw(RecordingCanvas canvas) {
59             WindowCallbacksCompat.this.onPostDraw(canvas);
60         }
61     };
62 
63     private final View mView;
64 
WindowCallbacksCompat(View view)65     public WindowCallbacksCompat(View view) {
66         mView = view;
67     }
68 
onWindowSizeIsChanging(Rect newBounds, boolean fullscreen, Rect systemInsets, Rect stableInsets)69     public void onWindowSizeIsChanging(Rect newBounds, boolean fullscreen, Rect systemInsets,
70             Rect stableInsets) { }
71 
onWindowDragResizeStart(Rect initialBounds, boolean fullscreen, Rect systemInsets, Rect stableInsets, int resizeMode)72     public void onWindowDragResizeStart(Rect initialBounds, boolean fullscreen, Rect systemInsets,
73             Rect stableInsets, int resizeMode) { }
74 
onWindowDragResizeEnd()75     public void onWindowDragResizeEnd() { }
76 
onContentDrawn(int offsetX, int offsetY, int sizeX, int sizeY)77     public boolean onContentDrawn(int offsetX, int offsetY, int sizeX, int sizeY) {
78         return false;
79     }
80 
onRequestDraw(boolean reportNextDraw)81     public void onRequestDraw(boolean reportNextDraw) {
82         if (reportNextDraw) {
83             reportDrawFinish();
84         }
85     }
86 
onPostDraw(Canvas canvas)87     public void onPostDraw(Canvas canvas) { }
88 
reportDrawFinish()89     public void reportDrawFinish() {
90         mView.getViewRootImpl().reportDrawFinish();
91     }
92 
attach()93     public boolean attach() {
94         ViewRootImpl root = mView.getViewRootImpl();
95         if (root != null) {
96             root.addWindowCallbacks(mWindowCallbacks);
97             root.requestInvalidateRootRenderNode();
98             return true;
99         }
100         return false;
101     }
102 
detach()103     public void detach() {
104         ViewRootImpl root = mView.getViewRootImpl();
105         if (root != null) {
106             root.removeWindowCallbacks(mWindowCallbacks);
107         }
108     }
109 }
110