• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download

<lambda>null1 package android.platform.helpers
2 
3 import android.platform.uiautomator_helpers.DeviceHelpers.shell
4 import android.platform.uiautomator_helpers.WaitUtils.ensureThat
5 import android.util.Log
6 
7 /** Allows to execute operations such as restart on a process identififed by [packageName]. */
8 class ProcessUtil(private val packageName: String) {
9 
10     /** Restart [packageName] running `am crash <package-name>`. */
11     fun restart() {
12         val initialPids = pids
13         // make sure the lock screen is enable.
14         Log.d(TAG, "Old $packageName PIDs=$initialPids)")
15         initialPids
16             .map { pid -> "kill $pid" }
17             .forEach { killCmd ->
18                 val result = shell(killCmd)
19                 Log.d(TAG, "Result of \"$killCmd\": \"$result\"")
20             }
21         ensureThat("All sysui process restarted") { allProcessesRestarted(initialPids) }
22     }
23 
24     private val pids: List<String>
25         get() {
26             val pidofResult = shell("pidof $packageName")
27             return if (pidofResult.isEmpty()) {
28                 emptyList()
29             } else pidofResult.split("\\s".toRegex())
30         }
31 
32     private fun allProcessesRestarted(initialPidsList: List<String>): Boolean =
33         (pids intersect initialPidsList).isEmpty()
34 
35     private companion object {
36         const val TAG = "ProcessUtils"
37     }
38 }
39