Record launched time

Fixes: 387481832
Flag: NONE minor change
Test: TutorialSchedulerRepositoryTest
Change-Id: I9689a641be162817bdc4e2a3ea3b80dc173d9971
diff --git a/packages/SystemUI/multivalentTests/src/com/android/systemui/inputdevice/data/repository/TutorialSchedulerRepositoryTest.kt b/packages/SystemUI/multivalentTests/src/com/android/systemui/inputdevice/data/repository/TutorialSchedulerRepositoryTest.kt
index 4630674..b9e8613 100644
--- a/packages/SystemUI/multivalentTests/src/com/android/systemui/inputdevice/data/repository/TutorialSchedulerRepositoryTest.kt
+++ b/packages/SystemUI/multivalentTests/src/com/android/systemui/inputdevice/data/repository/TutorialSchedulerRepositoryTest.kt
@@ -84,9 +84,11 @@
 
     @Test
     fun notifyKeyboard() = runTestAndClear {
-        underTest.setNotified(KEYBOARD)
+        val now = Instant.now()
+        underTest.setNotifiedTime(KEYBOARD, now)
 
         assertThat(underTest.isNotified(KEYBOARD)).isTrue()
+        assertThat(underTest.getNotifiedTime(KEYBOARD)!!.epochSecond).isEqualTo(now.epochSecond)
         assertThat(underTest.isNotified(TOUCHPAD)).isFalse()
     }
 
diff --git a/packages/SystemUI/src/com/android/systemui/inputdevice/tutorial/data/model/DeviceSchedulerInfo.kt b/packages/SystemUI/src/com/android/systemui/inputdevice/tutorial/data/model/DeviceSchedulerInfo.kt
index c436ef0..9a065be 100644
--- a/packages/SystemUI/src/com/android/systemui/inputdevice/tutorial/data/model/DeviceSchedulerInfo.kt
+++ b/packages/SystemUI/src/com/android/systemui/inputdevice/tutorial/data/model/DeviceSchedulerInfo.kt
@@ -19,23 +19,26 @@
 import java.time.Instant
 
 data class DeviceSchedulerInfo(
-    var launchTime: Instant? = null,
+    var launchedTime: Instant? = null,
     var firstConnectionTime: Instant? = null,
-    var isNotified: Boolean = false,
+    var notifiedTime: Instant? = null,
 ) {
     constructor(
         launchTimeSec: Long?,
         firstConnectionTimeSec: Long?,
-        isNotified: Boolean = false,
+        notifyTimeSec: Long?,
     ) : this(
         launchTimeSec?.let { Instant.ofEpochSecond(it) },
         firstConnectionTimeSec?.let { Instant.ofEpochSecond(it) },
-        isNotified,
+        notifyTimeSec?.let { Instant.ofEpochSecond(it) },
     )
 
     val wasEverConnected: Boolean
         get() = firstConnectionTime != null
 
     val isLaunched: Boolean
-        get() = launchTime != null
+        get() = launchedTime != null
+
+    val isNotified: Boolean
+        get() = notifiedTime != null
 }
diff --git a/packages/SystemUI/src/com/android/systemui/inputdevice/tutorial/data/repository/TutorialSchedulerRepository.kt b/packages/SystemUI/src/com/android/systemui/inputdevice/tutorial/data/repository/TutorialSchedulerRepository.kt
index 526e7db..8b0accd 100644
--- a/packages/SystemUI/src/com/android/systemui/inputdevice/tutorial/data/repository/TutorialSchedulerRepository.kt
+++ b/packages/SystemUI/src/com/android/systemui/inputdevice/tutorial/data/repository/TutorialSchedulerRepository.kt
@@ -19,7 +19,6 @@
 import android.content.Context
 import androidx.datastore.core.DataStore
 import androidx.datastore.preferences.core.Preferences
-import androidx.datastore.preferences.core.booleanPreferencesKey
 import androidx.datastore.preferences.core.edit
 import androidx.datastore.preferences.core.longPreferencesKey
 import androidx.datastore.preferences.preferencesDataStore
@@ -49,35 +48,42 @@
         preferencesDataStore(name = dataStoreName, scope = backgroundScope)
 
     suspend fun setScheduledTutorialLaunchTime(device: DeviceType, time: Instant) {
-        applicationContext.dataStore.edit { pref -> pref[getLaunchKey(device)] = time.epochSecond }
+        updateData(key = getLaunchedKey(device), value = time.epochSecond)
     }
 
     suspend fun isScheduledTutorialLaunched(deviceType: DeviceType): Boolean =
         loadData()[deviceType]!!.isLaunched
 
     suspend fun getScheduledTutorialLaunchTime(deviceType: DeviceType): Instant? =
-        loadData()[deviceType]!!.launchTime
+        loadData()[deviceType]!!.launchedTime
 
     suspend fun setFirstConnectionTime(device: DeviceType, time: Instant) {
-        applicationContext.dataStore.edit { pref -> pref[getConnectKey(device)] = time.epochSecond }
+        updateData(key = getConnectedKey(device), value = time.epochSecond)
     }
 
-    suspend fun setNotified(device: DeviceType) {
-        applicationContext.dataStore.edit { pref -> pref[getNotificationKey(device)] = true }
-    }
-
-    suspend fun isNotified(deviceType: DeviceType): Boolean = loadData()[deviceType]!!.isNotified
-
     suspend fun wasEverConnected(deviceType: DeviceType): Boolean =
         loadData()[deviceType]!!.wasEverConnected
 
     suspend fun getFirstConnectionTime(deviceType: DeviceType): Instant? =
         loadData()[deviceType]!!.firstConnectionTime
 
+    suspend fun setNotifiedTime(device: DeviceType, time: Instant) {
+        updateData(key = getNotifiedKey(device), value = time.epochSecond)
+    }
+
+    suspend fun isNotified(deviceType: DeviceType): Boolean = loadData()[deviceType]!!.isNotified
+
+    suspend fun getNotifiedTime(deviceType: DeviceType): Instant? =
+        loadData()[deviceType]!!.notifiedTime
+
     private suspend fun loadData(): Map<DeviceType, DeviceSchedulerInfo> {
         return applicationContext.dataStore.data.map { pref -> getSchedulerInfo(pref) }.first()
     }
 
+    private suspend fun <T> updateData(key: Preferences.Key<T>, value: T) {
+        applicationContext.dataStore.edit { pref -> pref[key] = value }
+    }
+
     private fun getSchedulerInfo(pref: Preferences): Map<DeviceType, DeviceSchedulerInfo> {
         return mapOf(
             DeviceType.KEYBOARD to getDeviceSchedulerInfo(pref, DeviceType.KEYBOARD),
@@ -86,20 +92,20 @@
     }
 
     private fun getDeviceSchedulerInfo(pref: Preferences, device: DeviceType): DeviceSchedulerInfo {
-        val launchTime = pref[getLaunchKey(device)]
-        val connectionTime = pref[getConnectKey(device)]
-        val isNotified = pref[getNotificationKey(device)] == true
-        return DeviceSchedulerInfo(launchTime, connectionTime, isNotified)
+        val launchedTime = pref[getLaunchedKey(device)]
+        val connectedTime = pref[getConnectedKey(device)]
+        val notifiedTime = pref[getNotifiedKey(device)]
+        return DeviceSchedulerInfo(launchedTime, connectedTime, notifiedTime)
     }
 
-    private fun getLaunchKey(device: DeviceType) =
-        longPreferencesKey(device.name + LAUNCH_TIME_SUFFIX)
+    private fun getLaunchedKey(device: DeviceType) =
+        longPreferencesKey(device.name + LAUNCHED_TIME_SUFFIX)
 
-    private fun getConnectKey(device: DeviceType) =
-        longPreferencesKey(device.name + CONNECT_TIME_SUFFIX)
+    private fun getConnectedKey(device: DeviceType) =
+        longPreferencesKey(device.name + CONNECTED_TIME_SUFFIX)
 
-    private fun getNotificationKey(device: DeviceType) =
-        booleanPreferencesKey(device.name + NOTIFIED_SUFFIX)
+    private fun getNotifiedKey(device: DeviceType) =
+        longPreferencesKey(device.name + NOTIFIED_TIME_SUFFIX)
 
     suspend fun clear() {
         applicationContext.dataStore.edit { it.clear() }
@@ -107,9 +113,9 @@
 
     companion object {
         const val DATASTORE_NAME = "TutorialScheduler"
-        const val LAUNCH_TIME_SUFFIX = "_LAUNCH_TIME"
-        const val CONNECT_TIME_SUFFIX = "_CONNECT_TIME"
-        const val NOTIFIED_SUFFIX = "_NOTIFIED"
+        const val LAUNCHED_TIME_SUFFIX = "_LAUNCHED_TIME"
+        const val CONNECTED_TIME_SUFFIX = "_CONNECTED_TIME"
+        const val NOTIFIED_TIME_SUFFIX = "_NOTIFIED_TIME"
     }
 }
 
diff --git a/packages/SystemUI/src/com/android/systemui/inputdevice/tutorial/domain/interactor/TutorialSchedulerInteractor.kt b/packages/SystemUI/src/com/android/systemui/inputdevice/tutorial/domain/interactor/TutorialSchedulerInteractor.kt
index 419eefe..9607053 100644
--- a/packages/SystemUI/src/com/android/systemui/inputdevice/tutorial/domain/interactor/TutorialSchedulerInteractor.kt
+++ b/packages/SystemUI/src/com/android/systemui/inputdevice/tutorial/domain/interactor/TutorialSchedulerInteractor.kt
@@ -111,9 +111,9 @@
             val tutorialType = resolveTutorialType(it)
 
             if (tutorialType == TutorialType.KEYBOARD || tutorialType == TutorialType.BOTH)
-                repo.setNotified(KEYBOARD)
+                repo.setNotifiedTime(KEYBOARD, Instant.now())
             if (tutorialType == TutorialType.TOUCHPAD || tutorialType == TutorialType.BOTH)
-                repo.setNotified(TOUCHPAD)
+                repo.setNotifiedTime(TOUCHPAD, Instant.now())
 
             logger.logTutorialLaunched(tutorialType)
             tutorialType