1 /*
2  * Copyright (C) 2023 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  *      http://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 
17 @file:JvmName("Extensions")
18 
19 package android.tools.traces
20 
21 import android.os.SystemClock
22 import android.tools.SECOND_AS_NANOSECONDS
23 import android.tools.Timestamp
24 import android.tools.Timestamps
25 import androidx.test.platform.app.InstrumentationRegistry
26 import java.io.File
27 import java.time.Instant
28 
29 /**
30  * Gets the default flicker output dir. By default, the data is stored in /sdcard/flicker instead of
31  * using the app's internal data directory to be accessible by other components (i.e. FilePuller)
32  */
getDefaultFlickerOutputDirnull33 fun getDefaultFlickerOutputDir() =
34     InstrumentationRegistry.getInstrumentation().targetContext.filesDir
35 
36 /** @return the current timestamp as [Timestamp] */
37 fun now(): Timestamp {
38     val now = Instant.now()
39     return Timestamps.from(
40         elapsedNanos = SystemClock.elapsedRealtimeNanos(),
41         systemUptimeNanos = SystemClock.uptimeNanos(),
42         unixNanos = now.epochSecond * SECOND_AS_NANOSECONDS + now.nano
43     )
44 }
45 
deleteIfExistsnull46 fun File.deleteIfExists(): Boolean =
47     if (this.exists()) {
48         this.delete()
49     } else {
50         false
51     }
52