1 /* 2 * Copyright (C) 2022 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 android.car.cts.builtin.pm; 18 19 import android.car.cts.builtin.CtsCarShellCommand; 20 21 import com.android.tradefed.device.ITestDevice; 22 23 public final class ComponentEnabledStateCommand extends CtsCarShellCommand { 24 public static final String COMPONENT_ENABLED_STATE_DEFAULT = "COMPONENT_ENABLED_STATE_DEFAULT"; 25 public static final String COMPONENT_ENABLED_STATE_ENABLED = "COMPONENT_ENABLED_STATE_ENABLED"; 26 public static final String COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED = 27 "COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED"; 28 public static final String COMPONENT_ENABLED_STATE_UNSUPPORTED = 29 "COMPONENT_ENABLED_STATE_UNSUPPORTED"; 30 31 public static final String COMMAND_ACTION_GET = "get"; 32 public static final String COMMAND_ACTION_ENABLE = "enable"; 33 public static final String COMMAND_ACTION_DISABLE_UNTIL_USED = "disable_until_used"; 34 public static final String COMMAND_ACTION_DEFAULT = "default"; 35 36 private static final String COMMAND_NAME = "cmd car_service control-component-enabled-state"; 37 private static final String NEW_STATE_RETURN_HEADER = "New State: "; 38 private static final String GET_STATE_RETURN_HEADER = "Current State: "; 39 40 private String mNewState; 41 private String mCurrentState; 42 ComponentEnabledStateCommand(ITestDevice device)43 public ComponentEnabledStateCommand(ITestDevice device) { 44 super(COMMAND_NAME, device); 45 } 46 getNewState()47 public String getNewState() { 48 return mNewState; 49 } 50 getCurrentState()51 public String getCurrentState() { 52 return mCurrentState; 53 } 54 parseCommandReturn()55 protected void parseCommandReturn() throws Exception { 56 if (mCommandArgs.length != 2) { 57 throw new IllegalArgumentException("Expected two arguments: action and pkg name"); 58 } 59 60 switch (mCommandArgs[0]) { 61 case COMMAND_ACTION_GET: 62 if (!mCommandReturn.startsWith(GET_STATE_RETURN_HEADER)) { 63 throw new Exception("get enabled state error: " + mCommandReturn); 64 } 65 mCurrentState = mCommandReturn.substring(GET_STATE_RETURN_HEADER.length()).trim(); 66 break; 67 case COMMAND_ACTION_ENABLE: 68 case COMMAND_ACTION_DEFAULT: 69 case COMMAND_ACTION_DISABLE_UNTIL_USED: 70 if (!mCommandReturn.startsWith(NEW_STATE_RETURN_HEADER)) { 71 throw new Exception("change enabled state error: " + mCommandReturn); 72 } 73 mNewState = mCommandReturn.substring(NEW_STATE_RETURN_HEADER.length()).trim(); 74 break; 75 default: 76 throw new IllegalArgumentException("Unsupported action"); 77 } 78 } 79 } 80