1 /*
2  * Copyright (C) 2016 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.compatibility.common.tradefed.targetprep;
18 
19 import com.android.tradefed.config.Option;
20 import com.android.tradefed.config.OptionClass;
21 import com.android.tradefed.device.DeviceNotAvailableException;
22 import com.android.tradefed.device.ITestDevice;
23 import com.android.tradefed.invoker.TestInformation;
24 import com.android.tradefed.log.LogUtil.CLog;
25 import com.android.tradefed.targetprep.BuildError;
26 import com.android.tradefed.targetprep.TargetSetupError;
27 
28 /**
29  * Disables a package if installed.
30  */
31 @OptionClass(alias="package-disabler")
32 public class PackageDisabler extends PreconditionPreparer {
33 
34     @Option(name = "package", description = "Name of the package", mandatory = true)
35     protected String mPackageName = null;
36 
37     @Override
run(TestInformation testInfo)38     public void run(TestInformation testInfo)
39             throws TargetSetupError, BuildError, DeviceNotAvailableException {
40         ITestDevice device = testInfo.getDevice();
41         if (device.getAppPackageInfo(mPackageName) != null) {
42             CLog.i("Package %s installed, disabling ...", mPackageName);
43             device.executeShellCommand("pm disable-user " + mPackageName);
44         }
45     }
46 }
47