• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2024 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  *      https://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 package com.android.devicediagnostics
17 
18 import android.content.Context
19 import android.icu.text.DateFormat
20 import android.os.AsyncTask
21 import java.time.Instant
22 import java.util.Date
23 import java.util.Locale
24 
25 // Must only be called on the UI thread.
runInBackgroundnull26 fun runInBackground(handler: () -> Unit) {
27     val task =
28         object : AsyncTask<Void, Void, Void>() {
29             override fun doInBackground(vararg params: Void?): Void? {
30                 handler()
31                 return null
32             }
33         }
34     task.execute()
35 }
36 
getDefaultLocalenull37 fun getDefaultLocale(context: Context): Locale {
38     return context.resources.configuration.locales.get(0)
39 }
40 
localizeDateTimestampnull41 fun localizeDateTimestamp(locale: Locale, seconds: Long): String {
42     val instant = Instant.ofEpochSecond(seconds)
43     val formatter = DateFormat.getDateInstance(DateFormat.LONG, locale)
44     return formatter.format(Date.from(instant))
45 }
46