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.util
18 
19 import android.app.WallpaperInfo
20 import android.app.WallpaperManager
21 import android.util.Log
22 import android.view.View
23 import com.android.systemui.dagger.SysUISingleton
24 import javax.inject.Inject
25 import kotlin.math.max
26 
27 private const val TAG = "WallpaperController"
28 
29 @SysUISingleton
30 class WallpaperController @Inject constructor(private val wallpaperManager: WallpaperManager) {
31 
32     var rootView: View? = null
33 
34     private var notificationShadeZoomOut: Float = 0f
35     private var unfoldTransitionZoomOut: Float = 0f
36 
37     private var wallpaperInfo: WallpaperInfo? = null
38 
39     fun onWallpaperInfoUpdated(wallpaperInfo: WallpaperInfo?) {
40         this.wallpaperInfo = wallpaperInfo
41     }
42 
43     private val shouldUseDefaultUnfoldTransition: Boolean
44         get() = wallpaperInfo?.shouldUseDefaultUnfoldTransition()
45             ?: true
46 
47     fun setNotificationShadeZoom(zoomOut: Float) {
48         notificationShadeZoomOut = zoomOut
49         updateZoom()
50     }
51 
52     fun setUnfoldTransitionZoom(zoomOut: Float) {
53         if (shouldUseDefaultUnfoldTransition) {
54             unfoldTransitionZoomOut = zoomOut
55             updateZoom()
56         }
57     }
58 
59     private fun updateZoom() {
60         setWallpaperZoom(max(notificationShadeZoomOut, unfoldTransitionZoomOut))
61     }
62 
63     private fun setWallpaperZoom(zoomOut: Float) {
64         try {
65             rootView?.let { root ->
66                 if (root.isAttachedToWindow && root.windowToken != null) {
67                     wallpaperManager.setWallpaperZoomOut(root.windowToken, zoomOut)
68                 } else {
69                     Log.i(TAG, "Won't set zoom. Window not attached $root")
70                 }
71             }
72         } catch (e: IllegalArgumentException) {
73             Log.w(TAG, "Can't set zoom. Window is gone: ${rootView?.windowToken}", e)
74         }
75     }
76 }
77