1 /*
2  * Copyright (C) 2021 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 com.android.systemui.tv;
18 
19 import android.app.Activity;
20 import android.graphics.PixelFormat;
21 import android.graphics.Rect;
22 import android.graphics.drawable.Drawable;
23 import android.os.Bundle;
24 import android.util.DisplayMetrics;
25 import android.util.Log;
26 import android.view.Gravity;
27 import android.view.View;
28 import android.view.WindowManager;
29 
30 import com.android.systemui.tv.res.R;
31 
32 import java.util.Collections;
33 import java.util.function.Consumer;
34 
35 /**
36  * Generic bottom sheet with up to two icons in the beginning and two buttons.
37  */
38 public abstract class TvBottomSheetActivity extends Activity {
39 
40     private static final String TAG = TvBottomSheetActivity.class.getSimpleName();
41     private Drawable mBackgroundWithBlur;
42     private Drawable mBackgroundWithoutBlur;
43 
44     private final Consumer<Boolean> mBlurConsumer = this::onBlurChanged;
45 
onBlurChanged(boolean enabled)46     private void onBlurChanged(boolean enabled) {
47         Log.v(TAG, "blur enabled: " + enabled);
48         getWindow().setBackgroundDrawable(enabled ? mBackgroundWithBlur : mBackgroundWithoutBlur);
49     }
50 
51     @Override
onCreate(Bundle savedInstanceState)52     public void onCreate(Bundle savedInstanceState) {
53         super.onCreate(savedInstanceState);
54         setContentView(R.layout.bottom_sheet);
55 
56         overridePendingTransition(R.anim.bottom_sheet_enter, 0);
57 
58         mBackgroundWithBlur = getResources()
59                 .getDrawable(R.drawable.bottom_sheet_background_with_blur);
60         mBackgroundWithoutBlur = getResources().getDrawable(R.drawable.bottom_sheet_background);
61 
62         DisplayMetrics metrics = getResources().getDisplayMetrics();
63         int screenWidth = metrics.widthPixels;
64         int screenHeight = metrics.heightPixels;
65         int marginPx = getResources().getDimensionPixelSize(R.dimen.bottom_sheet_margin);
66 
67         WindowManager.LayoutParams windowParams = getWindow().getAttributes();
68         windowParams.width = screenWidth - marginPx * 2;
69         windowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
70         windowParams.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
71         windowParams.horizontalMargin = 0f;
72         windowParams.verticalMargin = (float) marginPx / screenHeight;
73         windowParams.format = PixelFormat.TRANSPARENT;
74         windowParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
75         windowParams.flags |= WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
76         windowParams.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
77         getWindow().setAttributes(windowParams);
78         getWindow().setElevation(getWindow().getElevation() + 5);
79         getWindow().setBackgroundBlurRadius(getResources().getDimensionPixelSize(
80                 R.dimen.bottom_sheet_background_blur_radius));
81 
82         final View rootView = findViewById(R.id.bottom_sheet);
83         rootView.addOnLayoutChangeListener((view, l, t, r, b, oldL, oldT, oldR, oldB) -> {
84             rootView.setUnrestrictedPreferKeepClearRects(
85                     Collections.singletonList(new Rect(0, 0, r - l, b - t)));
86         });
87     }
88 
89     @Override
onAttachedToWindow()90     public void onAttachedToWindow() {
91         super.onAttachedToWindow();
92         getWindowManager().addCrossWindowBlurEnabledListener(mBlurConsumer);
93     }
94 
95     @Override
onDetachedFromWindow()96     public void onDetachedFromWindow() {
97         getWindowManager().removeCrossWindowBlurEnabledListener(mBlurConsumer);
98         super.onDetachedFromWindow();
99     }
100 
101     @Override
finish()102     public void finish() {
103         super.finish();
104         overridePendingTransition(0, R.anim.bottom_sheet_exit);
105     }
106 
107 }
108