1 /* 2 * Copyright (C) 2024 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.adservices.common; 18 19 import static android.os.Build.VERSION.SDK_INT; 20 21 import android.app.UiAutomation; 22 import android.os.ParcelFileDescriptor; 23 import android.util.Log; 24 25 import androidx.test.platform.app.InstrumentationRegistry; 26 27 import com.android.adservices.shared.testing.AndroidLogger; 28 import com.android.adservices.shared.testing.shell.CommandResult; 29 import com.android.compatibility.common.util.FileUtils; 30 import com.android.compatibility.common.util.SystemUtil; 31 32 import java.io.FileInputStream; 33 import java.io.IOException; 34 35 /** Device-side implementation of {@link AbstractAdServicesShellCommandHelper}. */ 36 public final class AdServicesShellCommandHelper extends AbstractAdServicesShellCommandHelper { AdServicesShellCommandHelper()37 public AdServicesShellCommandHelper() { 38 super(AdServicesSupportHelper.getInstance(), AndroidLogger.getInstance()); 39 } 40 41 private static final int OUT_DESCRIPTOR_INDEX = 0; 42 private static final int IN_DESCRIPTOR_INDEX = 1; 43 private static final int ERR_DESCRIPTOR_INDEX = 2; 44 45 @Override runShellCommand(String cmd)46 protected String runShellCommand(String cmd) { 47 return SystemUtil.runShellCommand(cmd).strip(); 48 } 49 50 @Override runShellCommandRwe(String cmd)51 protected CommandResult runShellCommandRwe(String cmd) { 52 String out = ""; 53 String err = ""; 54 try { 55 ParcelFileDescriptor[] fds = executeShellCommandRwe(cmd); 56 ParcelFileDescriptor fdOut = fds[OUT_DESCRIPTOR_INDEX]; 57 ParcelFileDescriptor fdIn = fds[IN_DESCRIPTOR_INDEX]; 58 ParcelFileDescriptor fdErr = fds[ERR_DESCRIPTOR_INDEX]; 59 60 if (fdIn != null) { 61 try { 62 // not using stdin 63 fdIn.close(); 64 } catch (Exception e) { 65 // Ignore 66 } 67 } 68 69 try (FileInputStream fis = new ParcelFileDescriptor.AutoCloseInputStream(fdOut)) { 70 out = new String(FileUtils.readInputStreamFully(fis)).strip(); 71 } 72 try (FileInputStream fis = new ParcelFileDescriptor.AutoCloseInputStream(fdErr)) { 73 err = new String(FileUtils.readInputStreamFully(fis)).strip(); 74 } 75 return new CommandResult(out, err); 76 } catch (IOException e) { 77 Log.e(TAG, "Exception occurred while reading file descriptor: ", e); 78 return new CommandResult(out, e.getMessage()); 79 } 80 } 81 82 @Override getDeviceApiLevel()83 protected int getDeviceApiLevel() { 84 return SDK_INT; 85 } 86 executeShellCommandRwe(String cmd)87 private static ParcelFileDescriptor[] executeShellCommandRwe(String cmd) { 88 UiAutomation uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation(); 89 return uiAutomation.executeShellCommandRwe(cmd); 90 } 91 } 92