[Contextual Edu] Add corruptionHandler to handle file corruption
When the proto file is corrupted, it should replace it with a new healthy file instead of throwing fatal exception in systemui.
Bug: 386211247
Test: manually use below command to create a corrupted file and replace the existing one.
It creates a corrupted file by only getting the first 10 bytes of the existing file
"head -c 10 USER10_ContextualEducation.preferences_pb > USER10_ContextualEducation.preferences_pb_corrupted"
run "adb shell stop && adb shell start". With this change, a new empty proto file replaces the corrupted one.
Flag: com.android.systemui.keyboard_touchpad_contextual_education
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:725577915491aa80c30512f87381df3b34d54e27)
Merged-In: I9a5a75420a76ff0aefab2e2f9b14770fd7b2ad7a
Change-Id: I9a5a75420a76ff0aefab2e2f9b14770fd7b2ad7a
diff --git a/packages/SystemUI/src/com/android/systemui/education/data/repository/UserContextualEducationRepository.kt b/packages/SystemUI/src/com/android/systemui/education/data/repository/UserContextualEducationRepository.kt
index 2978595..9596a54 100644
--- a/packages/SystemUI/src/com/android/systemui/education/data/repository/UserContextualEducationRepository.kt
+++ b/packages/SystemUI/src/com/android/systemui/education/data/repository/UserContextualEducationRepository.kt
@@ -20,10 +20,12 @@
import android.hardware.input.InputManager
import android.hardware.input.KeyGestureEvent
import androidx.datastore.core.DataStore
+import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler
import androidx.datastore.preferences.core.MutablePreferences
import androidx.datastore.preferences.core.PreferenceDataStoreFactory
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit
+import androidx.datastore.preferences.core.emptyPreferences
import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.core.longPreferencesKey
import androidx.datastore.preferences.preferencesDataStoreFile
@@ -68,7 +70,7 @@
suspend fun updateGestureEduModel(
gestureType: GestureType,
- transform: (GestureEduModel) -> GestureEduModel
+ transform: (GestureEduModel) -> GestureEduModel,
)
suspend fun updateEduDeviceConnectionTime(
@@ -149,6 +151,8 @@
String.format(DATASTORE_DIR, userId)
)
},
+ corruptionHandler =
+ ReplaceFileCorruptionHandler(produceNewData = { emptyPreferences() }),
scope = newDsScope,
)
dataStoreScope = newDsScope
@@ -159,7 +163,7 @@
private fun getGestureEduModel(
gestureType: GestureType,
- preferences: Preferences
+ preferences: Preferences,
): GestureEduModel {
return GestureEduModel(
signalCount = preferences[getSignalCountKey(gestureType)] ?: 0,
@@ -183,7 +187,7 @@
override suspend fun updateGestureEduModel(
gestureType: GestureType,
- transform: (GestureEduModel) -> GestureEduModel
+ transform: (GestureEduModel) -> GestureEduModel,
) {
datastore.filterNotNull().first().edit { preferences ->
val currentModel = getGestureEduModel(gestureType, preferences)
@@ -193,17 +197,17 @@
setInstant(
preferences,
updatedModel.lastShortcutTriggeredTime,
- getLastShortcutTriggeredTimeKey(gestureType)
+ getLastShortcutTriggeredTimeKey(gestureType),
)
setInstant(
preferences,
updatedModel.usageSessionStartTime,
- getUsageSessionStartTimeKey(gestureType)
+ getUsageSessionStartTimeKey(gestureType),
)
setInstant(
preferences,
updatedModel.lastEducationTime,
- getLastEducationTimeKey(gestureType)
+ getLastEducationTimeKey(gestureType),
)
}
}
@@ -220,12 +224,12 @@
setInstant(
preferences,
updatedModel.keyboardFirstConnectionTime,
- getKeyboardFirstConnectionTimeKey()
+ getKeyboardFirstConnectionTimeKey(),
)
setInstant(
preferences,
updatedModel.touchpadFirstConnectionTime,
- getTouchpadFirstConnectionTimeKey()
+ getTouchpadFirstConnectionTimeKey(),
)
}
}
@@ -235,7 +239,7 @@
keyboardFirstConnectionTime =
preferences[getKeyboardFirstConnectionTimeKey()]?.let { Instant.ofEpochSecond(it) },
touchpadFirstConnectionTime =
- preferences[getTouchpadFirstConnectionTimeKey()]?.let { Instant.ofEpochSecond(it) }
+ preferences[getTouchpadFirstConnectionTimeKey()]?.let { Instant.ofEpochSecond(it) },
)
}
@@ -263,7 +267,7 @@
private fun setInstant(
preferences: MutablePreferences,
instant: Instant?,
- key: Preferences.Key<Long>
+ key: Preferences.Key<Long>,
) {
if (instant != null) {
// Use epochSecond because an instant is defined as a signed long (64bit number) of