1 /* <lambda>null2 * Copyright 2022 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.integration.macrobenchmark.target 18 19 import android.content.Context 20 import android.os.Build 21 import android.os.Bundle 22 import android.util.Log 23 import android.widget.TextView 24 import androidx.appcompat.app.AppCompatActivity 25 import androidx.tracing.trace 26 import androidx.work.OneTimeWorkRequestBuilder 27 import androidx.work.OutOfQuotaPolicy 28 import androidx.work.WorkInfo 29 import androidx.work.WorkManager 30 import java.util.concurrent.CountDownLatch 31 import kotlin.concurrent.thread 32 33 class BackgroundWorkActivity : AppCompatActivity() { 34 override fun onCreate(savedInstanceState: Bundle?) { 35 super.onCreate(savedInstanceState) 36 setContentView(R.layout.activity_main) 37 38 val notice = findViewById<TextView>(R.id.txtNotice) 39 notice.setText(R.string.app_notice) 40 41 startWork(this) 42 } 43 44 private fun startWork(context: Context) { 45 val count = 20 46 var countDownLatch = CountDownLatch(count) 47 48 for (i in 0 until count) { 49 var workRequest = 50 OneTimeWorkRequestBuilder<NoOpWorker>() 51 .setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST) 52 .build() 53 WorkManager.getInstance(context).beginWith(workRequest).enqueue() 54 WorkManager.getInstance(context).getWorkInfoByIdLiveData(workRequest.id).observe( 55 this 56 ) { workInfo -> 57 if (workInfo?.state == WorkInfo.State.SUCCEEDED) { 58 countDownLatch.countDown() 59 } 60 } 61 } 62 } 63 64 override fun onResume() { 65 super.onResume() 66 67 if (Build.VERSION.SDK_INT <= 23) { 68 // temporary logging/tracing to debug b/204572406 69 Log.d("Benchmark", "onResume") 70 trace("onResume") {} 71 } 72 } 73 74 init { 75 if (Build.VERSION.SDK_INT <= 23) { 76 // temporary tracing to debug b/204572406 77 thread { 78 while (true) { 79 trace("tracing") { Thread.sleep(50) } 80 } 81 } 82 } 83 } 84 } 85