1 /* 2 * Copyright (C) 2019 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.cts.install.lib; 18 19 import android.content.Context; 20 import android.content.pm.PackageInstaller; 21 import android.content.pm.PackageManager; 22 23 import androidx.test.InstrumentationRegistry; 24 25 /** 26 * Helper class for uninstalling test apps. 27 */ 28 public class Uninstall { 29 /** 30 * Uninstalls all of the given packages. 31 * Does nothing if the package is not installed or installed on /system 32 */ packages(String... packageNames)33 public static void packages(String... packageNames) throws InterruptedException { 34 for (String pkg : packageNames) { 35 uninstallSinglePackage(pkg); 36 } 37 } 38 uninstallSinglePackage(String packageName)39 private static void uninstallSinglePackage(String packageName) throws InterruptedException { 40 // No need to uninstall if the package isn't installed. 41 if (InstallUtils.getInstalledVersion(packageName) == -1 42 || InstallUtils.isSystemAppWithoutUpdate(packageName)) { 43 return; 44 } 45 46 Context context = InstrumentationRegistry.getContext(); 47 PackageManager packageManager = context.getPackageManager(); 48 PackageInstaller packageInstaller = packageManager.getPackageInstaller(); 49 packageInstaller.uninstall(packageName, LocalIntentSender.getIntentSender()); 50 InstallUtils.assertStatusSuccess(LocalIntentSender.getIntentSenderResult()); 51 } 52 } 53