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