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 36 @Inject 37 constructor( 38 private val wallpaperManager: WallpaperManager, 39 private val wallpaperRepository: WallpaperRepository, 40 ) { 41 42 var rootView: View? = null 43 set(value) { 44 field = value 45 wallpaperRepository.rootView = value 46 } 47 48 private var notificationShadeZoomOut: Float = 0f 49 private var unfoldTransitionZoomOut: Float = 0f 50 51 private val shouldUseDefaultUnfoldTransition: Boolean 52 get() = wallpaperRepository.wallpaperInfo.value?.shouldUseDefaultUnfoldTransition() ?: true 53 54 fun setNotificationShadeZoom(zoomOut: Float) { 55 notificationShadeZoomOut = zoomOut 56 updateZoom() 57 } 58 59 fun setUnfoldTransitionZoom(zoomOut: Float) { 60 if (shouldUseDefaultUnfoldTransition) { 61 unfoldTransitionZoomOut = zoomOut 62 updateZoom() 63 } 64 } 65 66 private fun updateZoom() { 67 setWallpaperZoom(max(notificationShadeZoomOut, unfoldTransitionZoomOut)) 68 } 69 70 private fun setWallpaperZoom(zoomOut: Float) { 71 try { 72 rootView?.let { root -> 73 if (root.isAttachedToWindow && root.windowToken != null) { 74 wallpaperManager.setWallpaperZoomOut(root.windowToken, zoomOut) 75 } else { 76 Log.i(TAG, "Won't set zoom. Window not attached $root") 77 } 78 } 79 } catch (e: IllegalArgumentException) { 80 Log.w(TAG, "Can't set zoom. Window is gone: ${rootView?.windowToken}", e) 81 } 82 } 83 } 84