• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * 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.WallpaperManager
20 import android.util.Log
21 import android.view.View
22 import com.android.systemui.dagger.SysUISingleton
23 import com.android.systemui.wallpapers.data.repository.WallpaperRepository
24 import javax.inject.Inject
25 import kotlin.math.max
26 
27 private const val TAG = "WallpaperController"
28 
29 /**
30  * Controller for wallpaper-related logic.
31  *
32  * Note: New logic should be added to [WallpaperRepository], not this class.
33  */
34 @SysUISingleton
35 class WallpaperController @Inject constructor(
36     private val wallpaperManager: WallpaperManager,
37     private val wallpaperRepository: WallpaperRepository,
38 ) {
39 
40     var rootView: View? = null
41 
42     private var notificationShadeZoomOut: Float = 0f
43     private var unfoldTransitionZoomOut: Float = 0f
44 
45     private val shouldUseDefaultUnfoldTransition: Boolean
46         get() = wallpaperRepository.wallpaperInfo.value?.shouldUseDefaultUnfoldTransition()
47             ?: true
48 
49     fun setNotificationShadeZoom(zoomOut: Float) {
50         notificationShadeZoomOut = zoomOut
51         updateZoom()
52     }
53 
54     fun setUnfoldTransitionZoom(zoomOut: Float) {
55         if (shouldUseDefaultUnfoldTransition) {
56             unfoldTransitionZoomOut = zoomOut
57             updateZoom()
58         }
59     }
60 
61     private fun updateZoom() {
62         setWallpaperZoom(max(notificationShadeZoomOut, unfoldTransitionZoomOut))
63     }
64 
65     private fun setWallpaperZoom(zoomOut: Float) {
66         try {
67             rootView?.let { root ->
68                 if (root.isAttachedToWindow && root.windowToken != null) {
69                     wallpaperManager.setWallpaperZoomOut(root.windowToken, zoomOut)
70                 } else {
71                     Log.i(TAG, "Won't set zoom. Window not attached $root")
72                 }
73             }
74         } catch (e: IllegalArgumentException) {
75             Log.w(TAG, "Can't set zoom. Window is gone: ${rootView?.windowToken}", e)
76         }
77     }
78 }
79