1 /** 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * ``` 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * ``` 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.android.healthconnect.controller.tests.utils 16 17 import android.provider.Settings.System 18 import androidx.test.platform.app.InstrumentationRegistry 19 import com.android.compatibility.common.util.UserSettings 20 import com.android.compatibility.common.util.UserSettings.Namespace 21 import org.junit.rules.TestWatcher 22 import org.junit.runner.Description 23 24 /** Test Rule that clears the user time format setting so the locale-defined time format is used. */ 25 class ClearTimeFormatRule : TestWatcher() { 26 private lateinit var systemSettings: UserSettings 27 private var previousTimeFormat: String? = null 28 startingnull29 override fun starting(description: Description) { 30 super.starting(description) 31 32 val context = InstrumentationRegistry.getInstrumentation().context 33 systemSettings = UserSettings(context, Namespace.SYSTEM) 34 previousTimeFormat = systemSettings.get(System.TIME_12_24) 35 if (previousTimeFormat != null) { 36 // Clear setting so locale-defined time format is used. 37 systemSettings.syncSet(System.TIME_12_24, null) 38 } 39 } 40 finishednull41 override fun finished(description: Description) { 42 super.finished(description) 43 44 if (previousTimeFormat != null) { 45 systemSettings.syncSet(System.TIME_12_24, previousTimeFormat) 46 } 47 } 48 }