1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 *      http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15package com.android.systemui.plugin.testoverlayplugin;
16
17import android.content.Context;
18import android.graphics.Rect;
19import android.util.Log;
20import android.view.LayoutInflater;
21import android.view.View;
22import android.view.ViewGroup;
23
24import android.view.ViewTreeObserver.InternalInsetsInfo;
25import android.view.ViewTreeObserver.OnComputeInternalInsetsListener;
26import com.android.systemui.plugins.OverlayPlugin;
27import com.android.systemui.plugins.annotations.Requires;
28
29@Requires(target = OverlayPlugin.class, version = OverlayPlugin.VERSION)
30public class SampleOverlayPlugin implements OverlayPlugin {
31    private static final String TAG = "SampleOverlayPlugin";
32    private Context mPluginContext;
33
34    private View mStatusBarView;
35    private View mNavBarView;
36    private boolean mInputSetup;
37    private boolean mCollapseDesired;
38    private float mStatusBarHeight;
39
40    @Override
41    public void onCreate(Context sysuiContext, Context pluginContext) {
42        Log.d(TAG, "onCreate");
43        mPluginContext = pluginContext;
44    }
45
46    @Override
47    public void onDestroy() {
48        if (mInputSetup) {
49            mStatusBarView.getViewTreeObserver().removeOnComputeInternalInsetsListener(
50                    onComputeInternalInsetsListener);
51        }
52        Log.d(TAG, "onDestroy");
53        if (mStatusBarView != null) {
54            mStatusBarView.post(
55                    () -> ((ViewGroup) mStatusBarView.getParent()).removeView(mStatusBarView));
56        }
57        if (mNavBarView != null) {
58            mNavBarView.post(() -> ((ViewGroup) mNavBarView.getParent()).removeView(mNavBarView));
59        }
60    }
61
62    @Override
63    public void setup(View statusBar, View navBar) {
64        Log.d(TAG, "Setup");
65
66        int id = mPluginContext.getResources().getIdentifier("status_bar_height", "dimen",
67                "android");
68        mStatusBarHeight = mPluginContext.getResources().getDimension(id);
69        if (statusBar instanceof ViewGroup) {
70            mStatusBarView = LayoutInflater.from(mPluginContext)
71                    .inflate(R.layout.colored_overlay, (ViewGroup) statusBar, false);
72            ((ViewGroup) statusBar).addView(mStatusBarView);
73        }
74        if (navBar instanceof ViewGroup) {
75            mNavBarView = LayoutInflater.from(mPluginContext)
76                    .inflate(R.layout.colored_overlay, (ViewGroup) navBar, false);
77            ((ViewGroup) navBar).addView(mNavBarView);
78        }
79    }
80
81    @Override
82    public void setCollapseDesired(boolean collapseDesired) {
83        mCollapseDesired = collapseDesired;
84    }
85
86    @Override
87    public boolean holdStatusBarOpen() {
88        if (!mInputSetup) {
89            mInputSetup = true;
90            mStatusBarView.getViewTreeObserver().addOnComputeInternalInsetsListener(
91                    onComputeInternalInsetsListener);
92        }
93        return true;
94    }
95
96    final OnComputeInternalInsetsListener onComputeInternalInsetsListener = inoutInfo -> {
97        inoutInfo.setTouchableInsets(InternalInsetsInfo.TOUCHABLE_INSETS_REGION);
98        if (mCollapseDesired) {
99            inoutInfo.touchableRegion.set(new Rect(0, 0, 50000, (int) mStatusBarHeight));
100        } else {
101            inoutInfo.touchableRegion.set(new Rect(0, 0, 50000, 50000));
102        }
103    };
104}
105