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.tradefed.targetprep;
17 
18 import com.android.tradefed.build.IBuildInfo;
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.log.LogUtil.CLog;
24 
25 import java.util.ArrayList;
26 import java.util.List;
27 
28 /**
29  * Add packages to whitelist to allow it to run in the background.
30  */
31 @OptionClass(alias = "add-whitelist-package")
32 public class AddWhitelistPackage extends BaseTargetPreparer implements ITargetCleaner {
33     @Option(
34             name = "whitelist-package-name",
35             description = "Name of package to put in whitelist"
36     )
37     private List<String> mPackages = new ArrayList<>();
38 
39     @Override
setUp(ITestDevice device, IBuildInfo buildInfo)40     public void setUp(ITestDevice device, IBuildInfo buildInfo)
41             throws TargetSetupError, BuildError, DeviceNotAvailableException {
42         for (String pkg : mPackages) {
43             device.executeShellCommand(
44                     String.format("dumpsys deviceidle whitelist +%s", pkg));
45         }
46 
47         CLog.d(device.executeShellCommand("dumpsys deviceidle whitelist"));
48     }
49 
50     @Override
tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable e)51     public void tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable e)
52             throws DeviceNotAvailableException {
53         for (String pkg : mPackages) {
54             device.executeShellCommand(
55                     String.format("dumpsys deviceidle whitelist -%s", pkg));
56         }
57     }
58 }
59