| 1 |
/* |
| 2 |
* Copyright (C) 2022 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.test.silkfx.materials |
| 17 |
|
| 18 |
import android.app.Activity |
| 19 |
import android.content.Intent |
| 20 |
import android.content.Context |
| 21 |
import android.graphics.Bitmap |
| 22 |
import android.graphics.BitmapFactory |
| 23 |
import android.graphics.Color |
| 24 |
import android.graphics.drawable.ColorDrawable |
| 25 |
import android.graphics.drawable.PaintDrawable |
| 26 |
import android.graphics.drawable.Drawable |
| 27 |
import android.os.Bundle |
| 28 |
import android.provider.Settings |
| 29 |
import android.util.TypedValue |
| 30 |
import android.view.View |
| 31 |
import android.view.WindowManager |
| 32 |
import android.widget.ImageView |
| 33 |
import android.widget.SeekBar |
| 34 |
import android.widget.Switch |
| 35 |
import android.widget.TextView |
| 36 |
import com.android.test.silkfx.R |
| 37 |
import com.android.internal.graphics.drawable.BackgroundBlurDrawable |
| 38 |
import android.widget.LinearLayout |
| 39 |
import android.widget.Button |
| 40 |
|
| 41 |
import android.view.ViewRootImpl |
| 42 |
|
| 43 |
class BackgroundBlurActivity : Activity(), SeekBar.OnSeekBarChangeListener { |
| 44 |
var mBackgroundDrawable = PaintDrawable(Color.WHITE) |
| 45 |
var mBackgroundBlurRadius = 50 |
| 46 |
var mAlphaWithBlur = 0.2f |
| 47 |
var mAlphaNoBlur = 0.5f |
| 48 |
|
| 49 |
var mBlurBehindRadius = 10 |
| 50 |
var mDimAmountWithBlur = 0.2f |
| 51 |
var mDimAmountNoBlur = 0.2f |
| 52 |
|
| 53 |
var mBlurForceDisabled = false |
| 54 |
var mBatterySavingModeOn = false |
| 55 |
|
| 56 |
lateinit var blurBackgroundSeekBar: SeekBar |
| 57 |
lateinit var backgroundAlphaSeekBar : SeekBar |
| 58 |
lateinit var blurBehindSeekBar : SeekBar |
| 59 |
lateinit var dimAmountSeekBar : SeekBar |
| 60 |
|
| 61 |
val blurEnabledListener = { enabled : Boolean -> |
| 62 |
blurBackgroundSeekBar.setProgress(mBackgroundBlurRadius) |
| 63 |
blurBehindSeekBar.setProgress(mBlurBehindRadius) |
| 64 |
|
| 65 |
if (enabled) { |
| 66 |
setBackgroundBlur(mBackgroundBlurRadius) |
| 67 |
setBackgroundColorAlpha(mAlphaWithBlur) |
| 68 |
|
| 69 |
setBlurBehind(mBlurBehindRadius) |
| 70 |
setDimAmount(mDimAmountWithBlur) |
| 71 |
|
| 72 |
backgroundAlphaSeekBar.setProgress((mAlphaWithBlur * 100).toInt()) |
| 73 |
dimAmountSeekBar.setProgress((mDimAmountWithBlur * 100).toInt()) |
| 74 |
} else { |
| 75 |
setBackgroundColorAlpha(mAlphaNoBlur) |
| 76 |
setDimAmount(mDimAmountNoBlur) |
| 77 |
|
| 78 |
backgroundAlphaSeekBar.setProgress((mAlphaNoBlur * 100).toInt()) |
| 79 |
dimAmountSeekBar.setProgress((mDimAmountNoBlur * 100).toInt()) |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
override fun onCreate(savedInstanceState: Bundle?) { |
| 84 |
super.onCreate(savedInstanceState) |
| 85 |
setContentView(R.layout.activity_background_blur) |
| 86 |
|
| 87 |
window.addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND) |
| 88 |
window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND) |
| 89 |
|
| 90 |
mBackgroundDrawable.setCornerRadius(30f) |
| 91 |
window.setBackgroundDrawable(mBackgroundDrawable) |
| 92 |
|
| 93 |
mBatterySavingModeOn = |
| 94 |
Settings.Global.getInt(getContentResolver(), Settings.Global.LOW_POWER_MODE, 0) == 1 |
| 95 |
setBatterySavingModeOn(mBatterySavingModeOn) |
| 96 |
|
| 97 |
blurBackgroundSeekBar = requireViewById(R.id.set_background_blur) |
| 98 |
backgroundAlphaSeekBar = requireViewById(R.id.set_background_alpha) |
| 99 |
blurBehindSeekBar = requireViewById(R.id.set_blur_behind) |
| 100 |
dimAmountSeekBar = requireViewById(R.id.set_dim_amount) |
| 101 |
|
| 102 |
arrayOf(blurBackgroundSeekBar, backgroundAlphaSeekBar, blurBehindSeekBar, dimAmountSeekBar) |
| 103 |
.forEach { |
| 104 |
it.setOnSeekBarChangeListener(this) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
override fun onAttachedToWindow() { |
| 109 |
super.onAttachedToWindow() |
| 110 |
getWindowManager().addCrossWindowBlurEnabledListener(blurEnabledListener) |
| 111 |
} |
| 112 |
|
| 113 |
override fun onDetachedFromWindow() { |
| 114 |
super.onDetachedFromWindow() |
| 115 |
getWindowManager().removeCrossWindowBlurEnabledListener(blurEnabledListener) |
| 116 |
} |
| 117 |
|
| 118 |
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { |
| 119 |
when (seekBar) { |
| 120 |
blurBackgroundSeekBar -> setBackgroundBlur(progress) |
| 121 |
backgroundAlphaSeekBar -> setBackgroundColorAlpha(progress / 100.0f) |
| 122 |
blurBehindSeekBar -> setBlurBehind(progress) |
| 123 |
dimAmountSeekBar -> setDimAmount(progress / 100.0f) |
| 124 |
else -> throw IllegalArgumentException("Unknown seek bar") |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
override fun onStartTrackingTouch(seekBar: SeekBar?) {} |
| 129 |
override fun onStopTrackingTouch(seekBar: SeekBar?) {} |
| 130 |
|
| 131 |
fun setBlurDisabled(disabled: Boolean) { |
| 132 |
mBlurForceDisabled = disabled |
| 133 |
Settings.Global.putInt(getContentResolver(), Settings.Global.DISABLE_WINDOW_BLURS, |
| 134 |
if (mBlurForceDisabled) 1 else 0) |
| 135 |
(findViewById(R.id.toggle_blur_enabled) as Button) |
| 136 |
.setText(if (mBlurForceDisabled) "Enable blurs" else "Disable blurs") |
| 137 |
} |
| 138 |
|
| 139 |
fun toggleForceBlurDisabled(v: View) { |
| 140 |
setBlurDisabled(!mBlurForceDisabled) |
| 141 |
} |
| 142 |
|
| 143 |
fun setBackgroundBlur(radius: Int) { |
| 144 |
mBackgroundBlurRadius = radius |
| 145 |
(findViewById(R.id.background_blur_radius) as TextView).setText(radius.toString()) |
| 146 |
window.setBackgroundBlurRadius(mBackgroundBlurRadius) |
| 147 |
} |
| 148 |
|
| 149 |
fun setBlurBehind(radius: Int) { |
| 150 |
mBlurBehindRadius = radius |
| 151 |
(findViewById(R.id.blur_behind_radius) as TextView).setText(radius.toString()) |
| 152 |
window.getAttributes().setBlurBehindRadius(mBlurBehindRadius) |
| 153 |
window.setAttributes(window.getAttributes()) |
| 154 |
} |
| 155 |
|
| 156 |
fun setDimAmount(amount: Float) { |
| 157 |
if (getWindowManager().isCrossWindowBlurEnabled()) { |
| 158 |
mDimAmountWithBlur = amount |
| 159 |
} else { |
| 160 |
mDimAmountNoBlur = amount |
| 161 |
} |
| 162 |
(findViewById(R.id.dim_amount) as TextView).setText("%.2f".format(amount)) |
| 163 |
window.getAttributes().dimAmount = amount |
| 164 |
window.setAttributes(window.getAttributes()) |
| 165 |
} |
| 166 |
|
| 167 |
fun setBatterySavingModeOn(on: Boolean) { |
| 168 |
mBatterySavingModeOn = on |
| 169 |
Settings.Global.putInt(getContentResolver(), |
| 170 |
Settings.Global.LOW_POWER_MODE, if (on) 1 else 0) |
| 171 |
(findViewById(R.id.toggle_battery_saving_mode) as Button).setText( |
| 172 |
if (on) "Exit low power mode" else "Enter low power mode") |
| 173 |
} |
| 174 |
|
| 175 |
fun toggleBatterySavingMode(v: View) { |
| 176 |
setBatterySavingModeOn(!mBatterySavingModeOn) |
| 177 |
} |
| 178 |
|
| 179 |
fun setBackgroundColorAlpha(alpha: Float) { |
| 180 |
if (getWindowManager().isCrossWindowBlurEnabled()) { |
| 181 |
mAlphaWithBlur = alpha |
| 182 |
} else { |
| 183 |
mAlphaNoBlur = alpha |
| 184 |
} |
| 185 |
(findViewById(R.id.background_alpha) as TextView).setText("%.2f".format(alpha)) |
| 186 |
mBackgroundDrawable.setAlpha((alpha * 255f).toInt()) |
| 187 |
getWindowManager().updateViewLayout(window.getDecorView(), window.getAttributes()) |
| 188 |
} |
| 189 |
} |