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 | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 49 | private var view: MediaHeaderView? = null |
| 50 | |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 51 | /** |
| 52 | * Attach this controller to a media view, initializing its state |
| 53 | */ |
Selim Cinek | b52642b | 2020-04-17 14:30:29 -0700 | [diff] [blame] | 54 | fun attach(mediaView: MediaHeaderView) { |
| 55 | view = mediaView |
Selim Cinek | 2d7be5f | 2020-05-01 13:16:01 -0700 | [diff] [blame] | 56 | // First let's set the desired state that we want for this host |
| 57 | mediaHost.visibleChangedListener = { updateVisibility() } |
Selim Cinek | f0f7495 | 2020-04-21 11:45:16 -0700 | [diff] [blame] | 58 | mediaHost.expansion = 0.0f |
Selim Cinek | b52642b | 2020-04-17 14:30:29 -0700 | [diff] [blame] | 59 | mediaHost.showsOnlyActiveMedia = true |
Selim Cinek | 2d7be5f | 2020-05-01 13:16:01 -0700 | [diff] [blame] | 60 | |
| 61 | // 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] | 62 | mediaHost.init(MediaHierarchyManager.LOCATION_LOCKSCREEN) |
Selim Cinek | 2d7be5f | 2020-05-01 13:16:01 -0700 | [diff] [blame] | 63 | mediaView.setContentView(mediaHost.hostView) |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Selim Cinek | 2d7be5f | 2020-05-01 13:16:01 -0700 | [diff] [blame] | 66 | private fun updateVisibility() { |
Lucas Dupin | 989a111 | 2020-05-19 18:56:28 -0700 | [diff] [blame] | 67 | val keyguardOrUserSwitcher = (statusBarStateController.state == StatusBarState.KEYGUARD || |
| 68 | statusBarStateController.state == StatusBarState.FULLSCREEN_USER_SWITCHER) |
| 69 | val shouldBeVisible = mediaHost.visible && |
| 70 | !bypassController.bypassEnabled && |
| 71 | keyguardOrUserSwitcher && |
| 72 | notifLockscreenUserManager.shouldShowLockscreenNotifications() |
Selim Cinek | 5dbef2d | 2020-05-07 17:44:38 -0700 | [diff] [blame] | 73 | view?.visibility = if (shouldBeVisible) View.VISIBLE else View.GONE |
| 74 | } |
Lucas Dupin | 5b27cbc | 2020-05-18 10:46:50 -0700 | [diff] [blame] | 75 | } |