Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5566b5a7 authored by Juan Sebastian Martinez's avatar Juan Sebastian Martinez
Browse files

Adding a history log to the MSDL library.

The history is a fixed-size FIFO structure to keep track of basic
information about MSDL token playback. The history can be accessed by
any client that wishes to include this information in their logs.

Test: atest MSDLHistoryLoggerimplTest
Flag: NONE usage of this API is flagged separately
Bug: 344654090
Change-Id: If48339e93ede402a4d25cc20684aa8cc739faf29
parent 4a3586f3
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import com.google.android.msdl.data.model.MSDLToken
import com.google.android.msdl.data.repository.MSDLRepository
import com.google.android.msdl.data.repository.MSDLRepositoryImpl
import com.google.android.msdl.domain.MSDLPlayerImpl.Companion.REQUIRED_PRIMITIVES
import com.google.android.msdl.logging.MSDLEvent
import java.util.concurrent.Executor
import java.util.concurrent.Executors

@@ -51,6 +52,12 @@ interface MSDLPlayer {
     */
    fun playToken(token: MSDLToken, properties: InteractionProperties? = null)

    /**
     * Get the history of recent [MSDLEvent]s. The list can be useful to include in loggers and
     * system dumps for debugging purposes.
     */
    fun getHistory(): List<MSDLEvent>

    companion object {

        // TODO(b/355230334): remove once we have a system setting for the level
+11 −0
Original line number Diff line number Diff line
@@ -23,6 +23,9 @@ import com.google.android.msdl.data.model.FeedbackLevel
import com.google.android.msdl.data.model.HapticComposition
import com.google.android.msdl.data.model.MSDLToken
import com.google.android.msdl.data.repository.MSDLRepository
import com.google.android.msdl.logging.MSDLEvent
import com.google.android.msdl.logging.MSDLHistoryLogger
import com.google.android.msdl.logging.MSDLHistoryLoggerImpl
import java.util.concurrent.Executor

/**
@@ -43,6 +46,9 @@ internal class MSDLPlayerImpl(
    private val useHapticFallbackForToken: Map<MSDLToken, Boolean?>,
) : MSDLPlayer {

    /** A logger to keep a history of playback events */
    private val historyLogger = MSDLHistoryLoggerImpl(MSDLHistoryLogger.HISTORY_SIZE)

    // TODO(b/355230334): This should be retrieved from the system Settings
    override fun getSystemFeedbackLevel(): FeedbackLevel = MSDLPlayer.SYSTEM_FEEDBACK_LEVEL

@@ -90,11 +96,16 @@ internal class MSDLPlayerImpl(
                    VibrationAttributes.Builder().setUsage(VibrationAttributes.USAGE_TOUCH).build()
                }
            executor.execute { vibrator.vibrate(effect, attributes) }

            // 3. Log the event
            historyLogger.addEvent(MSDLEvent(token, properties))
        } else {
            // TODO(b/345248875): Play audio and haptics
        }
    }

    override fun getHistory(): List<MSDLEvent> = historyLogger.getHistory()

    companion object {
        val REQUIRED_PRIMITIVES =
            listOf(
+40 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.msdl.logging

import com.google.android.msdl.data.model.MSDLToken
import com.google.android.msdl.domain.InteractionProperties

/**
 * A summary that represents a MSDL event. The event summarizes the delivery of playback from a
 * [MSDLToken] along with optional [InteractionProperties].
 *
 * @param[tokenName] The name of the [MSDLToken] played.
 * @param[properties] The text representation of [InteractionProperties] used to play the token.
 * @param[timeStamp] A formatted time stamp for when the event occurred. The format for this time
 *   stamp is [MSDLHistoryLogger.DATE_FORMAT]
 */
data class MSDLEvent(val tokenName: String, val properties: String?, val timeStamp: String) {
    constructor(
        token: MSDLToken,
        properties: InteractionProperties?,
    ) : this(
        token.name,
        properties?.toString(),
        MSDLHistoryLogger.DATE_FORMAT.format(System.currentTimeMillis()),
    )
}
+40 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.msdl.logging

import java.text.SimpleDateFormat
import java.util.Locale

/** A logging component to keep track of recent [MSDLEvent]s */
interface MSDLHistoryLogger {

    /**
     * Add an event to the history.
     *
     * @param[event] The event to log.
     */
    fun addEvent(event: MSDLEvent)

    /** Get the history of latest events. */
    fun getHistory(): List<MSDLEvent>

    companion object {

        const val HISTORY_SIZE = 20
        val DATE_FORMAT = SimpleDateFormat("MM-dd HH:mm:ss.SSS", Locale.US)
    }
}
+39 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.msdl.logging

import androidx.annotation.VisibleForTesting
import androidx.annotation.VisibleForTesting.Companion.PACKAGE_PRIVATE
import java.util.ArrayDeque
import java.util.Deque

@VisibleForTesting(otherwise = PACKAGE_PRIVATE)
class MSDLHistoryLoggerImpl(private val maxHistorySize: Int) : MSDLHistoryLogger {

    // Use an [ArrayDequeue] with a fixed size as the history structure
    private val history: Deque<MSDLEvent> = ArrayDeque(maxHistorySize)

    override fun addEvent(event: MSDLEvent) {
        // Keep the history as a FIFO structure
        if (history.size == maxHistorySize) {
            history.removeFirst()
        }
        history.addLast(event)
    }

    override fun getHistory(): List<MSDLEvent> = history.toList()
}
Loading