1 /* 2 * Copyright (C) 2018 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 package com.android.compatibility.common.util; 17 18 public class AmUtils { 19 private static final String TAG = "CtsAmUtils"; 20 21 private static final String DUMPSYS_ACTIVITY_PROCESSES = "dumpsys activity --proto processes"; 22 23 public static int STANDBY_BUCKET_DOES_NOT_EXIST = -1; 24 AmUtils()25 private AmUtils() { 26 } 27 28 /** Run "adb shell am make-uid-idle PACKAGE" */ runMakeUidIdle(String packageName)29 public static void runMakeUidIdle(String packageName) { 30 SystemUtil.runShellCommandForNoOutput("am make-uid-idle " + packageName); 31 } 32 33 /** Run "adb shell am kill PACKAGE" */ runKill(String packageName)34 public static void runKill(String packageName) throws Exception { 35 runKill(packageName, false /* wait */); 36 } 37 runKill(String packageName, boolean wait)38 public static void runKill(String packageName, boolean wait) throws Exception { 39 SystemUtil.runShellCommandForNoOutput("am kill --user cur " + packageName); 40 41 if (!wait) { 42 return; 43 } 44 45 TestUtils.waitUntil("package process was not killed:" + packageName, 46 () -> !isProcessRunning(packageName)); 47 } 48 isProcessRunning(String packageName)49 private static boolean isProcessRunning(String packageName) { 50 final String output = SystemUtil.runShellCommand("ps -A -o NAME"); 51 String[] packages = output.split("\\n"); 52 for (int i = packages.length -1; i >=0; --i) { 53 if (packages[i].equals(packageName)) { 54 return true; 55 } 56 } 57 return false; 58 } 59 60 /** Run "adb shell am set-standby-bucket" */ setStandbyBucket(String packageName, int value)61 public static void setStandbyBucket(String packageName, int value) { 62 SystemUtil.runShellCommandForNoOutput("am set-standby-bucket " + packageName 63 + " " + value); 64 } 65 66 /** 67 * Run "adb shell am get-standby-bucket", 68 * return #STANDBY_BUCKET_DOES_NOT_EXIST for invalid packages 69 * */ getStandbyBucket(String packageName)70 public static int getStandbyBucket(String packageName) { 71 final String value = SystemUtil.runShellCommand("am get-standby-bucket " + packageName); 72 try { 73 return Integer.parseInt(value.trim()); 74 } catch (NumberFormatException nfe) { 75 return STANDBY_BUCKET_DOES_NOT_EXIST; 76 } 77 } 78 79 /** Wait until all broad queues are idle. */ waitForBroadcastIdle()80 public static void waitForBroadcastIdle() { 81 SystemUtil.runCommandAndPrintOnLogcat(TAG, "am wait-for-broadcast-idle"); 82 } 83 84 /** Wait until all broad queues have passed barrier. */ waitForBroadcastBarrier()85 public static void waitForBroadcastBarrier() { 86 SystemUtil.runCommandAndPrintOnLogcat(TAG, "am wait-for-broadcast-barrier"); 87 } 88 } 89