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 17 package com.android.tests.sysmem.host; 18 19 import com.android.tradefed.device.DeviceNotAvailableException; 20 import com.android.tradefed.device.ITestDevice; 21 22 /** 23 * Wrapper around ITestDevice exposing useful device functions. 24 */ 25 class Device { 26 27 private ITestDevice mDevice; 28 Device(ITestDevice device)29 Device(ITestDevice device) { 30 mDevice = device; 31 } 32 33 /** 34 * Execute a shell command and return the output as a string. 35 */ executeShellCommand(String command)36 public String executeShellCommand(String command) throws TestException { 37 try { 38 return mDevice.executeShellCommand(command); 39 } catch (DeviceNotAvailableException e) { 40 throw new TestException(e); 41 } 42 } 43 44 /** 45 * Enable adb root 46 */ enableAdbRoot()47 public void enableAdbRoot() throws TestException { 48 try { 49 mDevice.enableAdbRoot(); 50 } catch (DeviceNotAvailableException e) { 51 throw new TestException(e); 52 } 53 } 54 55 /** 56 * Returns the pid for the process with the given name. 57 */ getProcessPid(String name)58 public int getProcessPid(String name) throws TestException { 59 try { 60 String pid = mDevice.getProcessPid(name); 61 if (pid == null) { 62 throw new TestException("failed to get pid for " + name); 63 } 64 return Integer.parseInt(pid); 65 } catch (DeviceNotAvailableException e) { 66 throw new TestException(e); 67 } 68 } 69 } 70