1 /*
2  * Copyright (C) 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 com.android.pandora
18 
19 import android.content.Context
20 import android.os.Bundle
21 import android.os.Debug
22 import android.util.Log
23 import androidx.test.core.app.ApplicationProvider.getApplicationContext
24 import androidx.test.platform.app.InstrumentationRegistry
25 import androidx.test.runner.MonitoringInstrumentation
26 
27 @kotlinx.coroutines.ExperimentalCoroutinesApi
28 class Main : MonitoringInstrumentation() {
29 
30     private val TAG = "PandoraMain"
31 
onCreatenull32     override fun onCreate(arguments: Bundle) {
33         super.onCreate(arguments)
34 
35         // Activate debugger.
36         if (arguments.getString("debug").toBoolean()) {
37             Log.i(TAG, "Waiting for debugger to connect...")
38             Debug.waitForDebugger()
39             Log.i(TAG, "Debugger connected")
40         }
41 
42         // Start instrumentation thread.
43         start()
44     }
45 
onStartnull46     override fun onStart() {
47         super.onStart()
48 
49         val context: Context = getApplicationContext()
50         val uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation()
51         // Adopt all the permissions of the shell
52         uiAutomation.adoptShellPermissionIdentity()
53 
54         while (true) {
55             val server = Server(context)
56             server.awaitTermination()
57             server.deinit()
58         }
59     }
60 }
61