Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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.media |
| 18 | |
| 19 | import android.view.View |
Selim Cinek | 2d7be5f | 2020-05-01 13:16:01 -0700 | [diff] [blame] | 20 | import com.android.systemui.plugins.statusbar.StatusBarStateController |
Lucas Dupin | 989a111 | 2020-05-19 18:56:28 -0700 | [diff] [blame] | 21 | import com.android.systemui.statusbar.NotificationLockscreenUserManager |
Selim Cinek | 2d7be5f | 2020-05-01 13:16:01 -0700 | [diff] [blame] | 22 | import com.android.systemui.statusbar.StatusBarState |
| 23 | import com.android.systemui.statusbar.SysuiStatusBarStateController |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 24 | import com.android.systemui.statusbar.notification.stack.MediaHeaderView |
| 25 | import com.android.systemui.statusbar.phone.KeyguardBypassController |
| 26 | import javax.inject.Inject |
| 27 | import javax.inject.Singleton |
| 28 | |
| 29 | /** |
| 30 | * A class that controls the media notifications on the lock screen, handles its visibility and |
| 31 | * is responsible for the embedding of he media experience. |
| 32 | */ |
| 33 | @Singleton |
| 34 | class KeyguardMediaController @Inject constructor( |
Selim Cinek | b52642b | 2020-04-17 14:30:29 -0700 | [diff] [blame] | 35 | private val mediaHost: MediaHost, |
Selim Cinek | 2d7be5f | 2020-05-01 13:16:01 -0700 | [diff] [blame] | 36 | private val bypassController: KeyguardBypassController, |
Lucas Dupin | 989a111 | 2020-05-19 18:56:28 -0700 | [diff] [blame] | 37 | private val statusBarStateController: SysuiStatusBarStateController, |
| 38 | private val notifLockscreenUserManager: NotificationLockscreenUserManager |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 39 | ) { |
Selim Cinek | 2d7be5f | 2020-05-01 13:16:01 -0700 | [diff] [blame] | 40 | |
| 41 | init { |
| 42 | statusBarStateController.addCallback(object : StatusBarStateController.StateListener { |
| 43 | override fun onStateChanged(newState: Int) { |
| 44 | updateVisibility() |
| 45 | } |
| 46 | }) |
| 47 | } |
Selim Cinek | 2de5ebb | 2020-05-20 15:39:03 -0700 | [diff] [blame] | 48 | |
Selim Cinek | afae4e7 | 2020-06-16 18:21:41 -0700 | [diff] [blame] | 49 | var visibilityChangedListener: ((Boolean) -> Unit)? = null |
| 50 | var view: MediaHeaderView? = null |
| 51 | private set |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 52 | |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 53 | /** |
| 54 | * Attach this controller to a media view, initializing its state |
| 55 | */ |
Selim Cinek | b52642b | 2020-04-17 14:30:29 -0700 | [diff] [blame] | 56 | fun attach(mediaView: MediaHeaderView) { |
| 57 | view = mediaView |
Selim Cinek | 2d7be5f | 2020-05-01 13:16:01 -0700 | [diff] [blame] | 58 | // First let's set the desired state that we want for this host |
Selim Cinek | 7ba605b | 2020-06-19 18:34:23 -0700 | [diff] [blame] | 59 | mediaHost.addVisibilityChangeListener { updateVisibility() } |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 60 | mediaHost.expansion = 0.0f |
Selim Cinek | b52642b | 2020-04-17 14:30:29 -0700 | [diff] [blame] | 61 | mediaHost.showsOnlyActiveMedia = true |
Selim Cinek | afae4e7 | 2020-06-16 18:21:41 -0700 | [diff] [blame] | 62 | mediaHost.falsingProtectionNeeded = true |
Selim Cinek | 2d7be5f | 2020-05-01 13:16:01 -0700 | [diff] [blame] | 63 | |
| 64 | // Let's now initialize this view, which also creates the host view for us. |
Selim Cinek | b52642b | 2020-04-17 14:30:29 -0700 | [diff] [blame] | 65 | mediaHost.init(MediaHierarchyManager.LOCATION_LOCKSCREEN) |
Selim Cinek | 2d7be5f | 2020-05-01 13:16:01 -0700 | [diff] [blame] | 66 | mediaView.setContentView(mediaHost.hostView) |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Selim Cinek | 2d7be5f | 2020-05-01 13:16:01 -0700 | [diff] [blame] | 69 | private fun updateVisibility() { |
Lucas Dupin | 989a111 | 2020-05-19 18:56:28 -0700 | [diff] [blame] | 70 | val keyguardOrUserSwitcher = (statusBarStateController.state == StatusBarState.KEYGUARD || |
| 71 | statusBarStateController.state == StatusBarState.FULLSCREEN_USER_SWITCHER) |
| 72 | val shouldBeVisible = mediaHost.visible && |
| 73 | !bypassController.bypassEnabled && |
| 74 | keyguardOrUserSwitcher && |
| 75 | notifLockscreenUserManager.shouldShowLockscreenNotifications() |
Selim Cinek | afae4e7 | 2020-06-16 18:21:41 -0700 | [diff] [blame] | 76 | val previousVisibility = view?.visibility ?: View.GONE |
| 77 | val newVisibility = if (shouldBeVisible) View.VISIBLE else View.GONE |
| 78 | view?.visibility = newVisibility |
| 79 | if (previousVisibility != newVisibility) { |
| 80 | visibilityChangedListener?.invoke(shouldBeVisible) |
| 81 | } |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 82 | } |
Lucas Dupin | 5b27cbc | 2020-05-18 10:46:50 -0700 | [diff] [blame] | 83 | } |