1 /* 2 * Copyright (C) 2023 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.bouncer.ui.binder 18 19 import android.text.TextUtils 20 import android.util.PluralsMessageFormatter 21 import androidx.lifecycle.Lifecycle 22 import androidx.lifecycle.repeatOnLifecycle 23 import com.android.keyguard.BouncerKeyguardMessageArea 24 import com.android.keyguard.KeyguardMessageArea 25 import com.android.keyguard.KeyguardMessageAreaController 26 import com.android.systemui.Flags 27 import com.android.systemui.bouncer.domain.interactor.BouncerMessageInteractor 28 import com.android.systemui.bouncer.shared.model.Message 29 import com.android.systemui.bouncer.ui.BouncerMessageView 30 import com.android.systemui.lifecycle.repeatWhenAttached 31 import com.android.systemui.log.BouncerLogger 32 import kotlinx.coroutines.launch 33 34 object BouncerMessageViewBinder { 35 @JvmStatic bindnull36 fun bind( 37 view: BouncerMessageView, 38 interactor: BouncerMessageInteractor, 39 factory: KeyguardMessageAreaController.Factory, 40 bouncerLogger: BouncerLogger, 41 ) { 42 view.repeatWhenAttached { 43 if (!Flags.revampedBouncerMessages()) { 44 view.primaryMessageView?.disable() 45 view.secondaryMessageView?.disable() 46 return@repeatWhenAttached 47 } 48 view.init(factory) 49 view.primaryMessage?.setIsVisible(true) 50 view.secondaryMessage?.setIsVisible(true) 51 repeatOnLifecycle(Lifecycle.State.STARTED) { 52 bouncerLogger.startBouncerMessageInteractor() 53 launch { 54 interactor.bouncerMessage.collect { 55 bouncerLogger.bouncerMessageUpdated(it) 56 updateView( 57 view.primaryMessage, 58 view.primaryMessageView, 59 message = it?.message, 60 allowTruncation = true, 61 ) 62 updateView( 63 view.secondaryMessage, 64 view.secondaryMessageView, 65 message = it?.secondaryMessage, 66 allowTruncation = false, 67 ) 68 view.requestLayout() 69 } 70 } 71 } 72 } 73 } 74 updateViewnull75 private fun updateView( 76 controller: KeyguardMessageAreaController<KeyguardMessageArea>?, 77 view: BouncerKeyguardMessageArea?, 78 message: Message?, 79 allowTruncation: Boolean = false, 80 ) { 81 if (view == null || controller == null) return 82 if (message?.message != null || message?.messageResId != null) { 83 controller.setIsVisible(true) 84 var newMessage = message.message ?: "" 85 if (message.messageResId != null && message.messageResId != 0) { 86 newMessage = view.resources.getString(message.messageResId) 87 if (message.formatterArgs != null) { 88 newMessage = 89 PluralsMessageFormatter.format( 90 view.resources, 91 message.formatterArgs, 92 message.messageResId 93 ) 94 } 95 } 96 controller.setMessage(newMessage, message.animate) 97 } else { 98 controller.setIsVisible(false) 99 controller.setMessage(0) 100 } 101 message?.colorState?.let { controller.setNextMessageColor(it) } 102 view.ellipsize = 103 if (allowTruncation) TextUtils.TruncateAt.END else TextUtils.TruncateAt.MARQUEE 104 } 105 } 106