1 /*
2  * Copyright 2021 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 package androidx.benchmark.macro
18 
19 import android.content.Intent
20 import android.os.Bundle
21 import android.util.Log
22 import android.widget.TextView
23 import androidx.activity.ComponentActivity
24 
25 /** Activity with configurable text and launch time, for testing. */
26 class ConfigurableActivity : ComponentActivity() {
onCreatenull27     override fun onCreate(savedInstanceState: Bundle?) {
28         super.onCreate(savedInstanceState)
29         val textContent = intent.getStringExtra(EXTRA_TEXT)
30 
31         val view = TextView(this).apply { text = textContent }
32         setContentView(view)
33 
34         val sleepDurMs = intent.getLongExtra(EXTRA_SLEEP_DUR_MS, 0)
35         if (sleepDurMs > 0) {
36             Log.d(TAG, "sleeping $sleepDurMs ms")
37             Thread.sleep(sleepDurMs)
38             Log.d(TAG, "sleep complete")
39         }
40 
41         val reportFullyDrawnDelayMs =
42             intent.getLongExtra(EXTRA_REPORT_FULLY_DRAWN_DELAY_MS, /* default */ -1)
43         when (reportFullyDrawnDelayMs) {
44             -1L -> {} // ignore
45             0L -> reportFullyDrawn() // report immediately
46             else -> {
47                 // report delayed, modify text
48                 val runnable = {
49                     view.text =
50                         if (textContent == INNER_ACTIVITY_TEXT) {
51                             INNER_ACTIVITY_FULLY_DRAWN_TEXT
52                         } else {
53                             FULLY_DRAWN_TEXT
54                         }
55                     reportFullyDrawn()
56                 }
57                 view.postDelayed(runnable, reportFullyDrawnDelayMs)
58             }
59         }
60         // enable in-app navigation, which carries forward report fully drawn delay
61         view.setOnClickListener {
62             startActivity(
63                 createIntent(
64                         text = INNER_ACTIVITY_TEXT,
65                         reportFullyDrawnDelayMs = reportFullyDrawnDelayMs
66                     )
67                     .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
68             )
69         }
70     }
71 
72     companion object {
73         private const val TAG = "ConfigurableActivity"
74         private const val ACTION: String = "androidx.benchmark.macro.CONFIGURABLE_ACTIVITY"
75         private const val EXTRA_TEXT: String = "TEXT"
76         private const val EXTRA_SLEEP_DUR_MS: String = "SLEEP_DUR_MS"
77         private const val EXTRA_REPORT_FULLY_DRAWN_DELAY_MS = "REPORT_FULLY_DRAWN_DELAY_MS"
78         const val FULLY_DRAWN_TEXT = "FULLY DRAWN"
79         const val INNER_ACTIVITY_TEXT = "INNER ACTIVITY"
80         const val INNER_ACTIVITY_FULLY_DRAWN_TEXT = "INNER ACTIVITY FULLY DRAWN"
81 
createIntentnull82         fun createIntent(
83             text: String,
84             sleepDurMs: Long = 0,
85             reportFullyDrawnDelayMs: Long? = null
86         ): Intent {
87             return Intent().apply {
88                 action = ACTION
89                 putExtra(EXTRA_TEXT, text)
90                 putExtra(EXTRA_SLEEP_DUR_MS, sleepDurMs)
91                 putExtra(EXTRA_REPORT_FULLY_DRAWN_DELAY_MS, reportFullyDrawnDelayMs)
92             }
93         }
94     }
95 }
96