blob: 8ef20f89085c77e40de9fe1efb89dd9344ded253 [file] [log] [blame]
Selim Cinek5dbef2d2020-05-07 17:44:38 -07001/*
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
17package com.android.systemui.media
18
19import android.view.View
Selim Cinek2d7be5f2020-05-01 13:16:01 -070020import com.android.systemui.plugins.statusbar.StatusBarStateController
Lucas Dupin989a1112020-05-19 18:56:28 -070021import com.android.systemui.statusbar.NotificationLockscreenUserManager
Selim Cinek2d7be5f2020-05-01 13:16:01 -070022import com.android.systemui.statusbar.StatusBarState
23import com.android.systemui.statusbar.SysuiStatusBarStateController
Selim Cinek5dbef2d2020-05-07 17:44:38 -070024import com.android.systemui.statusbar.notification.stack.MediaHeaderView
25import com.android.systemui.statusbar.phone.KeyguardBypassController
26import javax.inject.Inject
27import 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
34class KeyguardMediaController @Inject constructor(
Selim Cinekb52642b2020-04-17 14:30:29 -070035 private val mediaHost: MediaHost,
Selim Cinek2d7be5f2020-05-01 13:16:01 -070036 private val bypassController: KeyguardBypassController,
Lucas Dupin989a1112020-05-19 18:56:28 -070037 private val statusBarStateController: SysuiStatusBarStateController,
38 private val notifLockscreenUserManager: NotificationLockscreenUserManager
Selim Cinek5dbef2d2020-05-07 17:44:38 -070039) {
Selim Cinek2d7be5f2020-05-01 13:16:01 -070040
41 init {
42 statusBarStateController.addCallback(object : StatusBarStateController.StateListener {
43 override fun onStateChanged(newState: Int) {
44 updateVisibility()
45 }
46 })
47 }
Selim Cinek2de5ebb2020-05-20 15:39:03 -070048
Selim Cinekafae4e72020-06-16 18:21:41 -070049 var visibilityChangedListener: ((Boolean) -> Unit)? = null
50 var view: MediaHeaderView? = null
51 private set
Selim Cinek5dbef2d2020-05-07 17:44:38 -070052
Selim Cinek5dbef2d2020-05-07 17:44:38 -070053 /**
54 * Attach this controller to a media view, initializing its state
55 */
Selim Cinekb52642b2020-04-17 14:30:29 -070056 fun attach(mediaView: MediaHeaderView) {
57 view = mediaView
Selim Cinek2d7be5f2020-05-01 13:16:01 -070058 // First let's set the desired state that we want for this host
Selim Cinek7ba605b2020-06-19 18:34:23 -070059 mediaHost.addVisibilityChangeListener { updateVisibility() }
Selim Cinekf0f74952020-04-21 11:45:16 -070060 mediaHost.expansion = 0.0f
Selim Cinekb52642b2020-04-17 14:30:29 -070061 mediaHost.showsOnlyActiveMedia = true
Selim Cinekafae4e72020-06-16 18:21:41 -070062 mediaHost.falsingProtectionNeeded = true
Selim Cinek2d7be5f2020-05-01 13:16:01 -070063
64 // Let's now initialize this view, which also creates the host view for us.
Selim Cinekb52642b2020-04-17 14:30:29 -070065 mediaHost.init(MediaHierarchyManager.LOCATION_LOCKSCREEN)
Selim Cinek2d7be5f2020-05-01 13:16:01 -070066 mediaView.setContentView(mediaHost.hostView)
Selim Cinek5dbef2d2020-05-07 17:44:38 -070067 }
68
Selim Cinek2d7be5f2020-05-01 13:16:01 -070069 private fun updateVisibility() {
Lucas Dupin989a1112020-05-19 18:56:28 -070070 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 Cinekafae4e72020-06-16 18:21:41 -070076 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 Cinek5dbef2d2020-05-07 17:44:38 -070082 }
Lucas Dupin5b27cbc2020-05-18 10:46:50 -070083}