1 /* 2 * Copyright (C) 2020 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.server.wm; 18 19 import android.text.TextUtils; 20 21 /** 22 * A class to represent an {@link android.content.Intent} extra that can be passed as a command line 23 * parameter. More options can be added as needed. Supported parameters are as follows 24 * 25 * <ol> 26 * <li>String</li> 27 * <li>boolean</li> 28 * </ol> 29 */ 30 public final class CliIntentExtra { 31 private final String mOption; 32 private final String mKey; 33 private final String mValue; 34 CliIntentExtra(String option, String key, String value)35 private CliIntentExtra(String option, String key, String value) { 36 if (TextUtils.isEmpty(option)) { 37 throw new IllegalArgumentException("Option must not be empty"); 38 } 39 if (TextUtils.isEmpty(key)) { 40 throw new IllegalArgumentException("Key must not be empty"); 41 } 42 if (TextUtils.isEmpty(value)) { 43 throw new IllegalArgumentException("Value must not be empty"); 44 } 45 mOption = option; 46 mKey = key; 47 mValue = value; 48 } 49 50 /** 51 * Returns the option to be used when creating the command line option. The option is provided 52 * in the constructor. 53 * 54 * @return {@link String} representing the option for the command line. 55 */ option()56 String option() { 57 return mOption; 58 } 59 60 /** 61 * Returns the key for the key-value pair that will be passed as an intent extra 62 * 63 * @return {@link String} representing the key for the {@link android.content.Intent} extra 64 */ key()65 String key() { 66 return mKey; 67 } 68 69 /** 70 * Returns the value for the key-value pair that will be passed as an 71 * {@link android.content.Intent} extra. All values are normalized to a {@link String} so they 72 * can be passed as a command line argument. 73 * 74 * @return {@link String} representing the parsed value for the key-value pair 75 */ value()76 String value() { 77 return mValue; 78 } 79 80 /** 81 * Appends the command line option and arguments to the command line command. The option, key, 82 * and value are appended separated by a space. 83 * 84 * @param sb {@link StringBuilder} representing the command 85 */ appendTo(StringBuilder sb)86 void appendTo(StringBuilder sb) { 87 sb.append(" ").append(option()).append(" ").append(key()).append(" ").append(value()); 88 } 89 90 /** 91 * Creates a {@link CliIntentExtra} for {@link String} intent extra. 92 * 93 * @param key the key in the key-value pair passed into the {@link android.content.Intent} extra 94 * @param value the value in the key-value pair pased into the {@link android.content.Intent} 95 * extra 96 * @return {@link CliIntentExtra} to construct a command with the key value pair as parameters 97 * for an {@link android.content.Intent} 98 */ extraString(String key, String value)99 public static CliIntentExtra extraString(String key, String value) { 100 return new CliIntentExtra("--es", key, value); 101 } 102 103 /** 104 * Creates a {@link CliIntentExtra} for {@link Boolean} intent extra. 105 * 106 * @param key the key in the key-value pair passed into the {@link android.content.Intent} extra 107 * @param value the value in the key-value pair pased into the {@link android.content.Intent} 108 * extra 109 * @return {@link CliIntentExtra} to construct a command with the key value pair as parameters 110 * for an {@link android.content.Intent} 111 */ extraBool(String key, boolean value)112 public static CliIntentExtra extraBool(String key, boolean value) { 113 return new CliIntentExtra("--ez", key, Boolean.toString(value)); 114 } 115 116 /** 117 * Creates a {@link CliIntentExtra} for {@link Integer} intent extra. 118 * 119 * @param key the key in the key-value pair passed into the {@link android.content.Intent} extra 120 * @param value the value in the key-value pair pased into the {@link android.content.Intent} 121 * extra 122 * @return {@link CliIntentExtra} to construct a command with the key value pair as parameters 123 * for an {@link android.content.Intent} 124 */ extraInt(String key, int value)125 public static CliIntentExtra extraInt(String key, int value) { 126 return new CliIntentExtra("--ei", key, Integer.toString(value)); 127 } 128 } 129