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