1 /* 2 * Copyright (C) 2023 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.internal.widget.remotecompose.core.operations; 17 18 import static com.android.internal.widget.remotecompose.core.documentation.DocumentedOperation.INT; 19 import static com.android.internal.widget.remotecompose.core.documentation.DocumentedOperation.UTF8; 20 21 import android.annotation.NonNull; 22 23 import com.android.internal.widget.remotecompose.core.Operation; 24 import com.android.internal.widget.remotecompose.core.Operations; 25 import com.android.internal.widget.remotecompose.core.RemoteContext; 26 import com.android.internal.widget.remotecompose.core.WireBuffer; 27 import com.android.internal.widget.remotecompose.core.documentation.DocumentationBuilder; 28 import com.android.internal.widget.remotecompose.core.serialize.MapSerializer; 29 import com.android.internal.widget.remotecompose.core.serialize.Serializable; 30 31 import java.util.List; 32 33 /** Operation to deal with Text data */ 34 public class NamedVariable extends Operation implements Serializable { 35 private static final int OP_CODE = Operations.NAMED_VARIABLE; 36 private static final String CLASS_NAME = "NamedVariable"; 37 public final int mVarId; 38 public final @NonNull String mVarName; 39 public final int mVarType; 40 public static final int MAX_STRING_SIZE = 4000; 41 public static final int COLOR_TYPE = 2; 42 public static final int FLOAT_TYPE = 1; 43 public static final int STRING_TYPE = 0; 44 public static final int IMAGE_TYPE = 3; 45 public static final int INT_TYPE = 4; 46 public static final int LONG_TYPE = 5; 47 NamedVariable(int varId, int varType, @NonNull String name)48 public NamedVariable(int varId, int varType, @NonNull String name) { 49 this.mVarId = varId; 50 this.mVarType = varType; 51 this.mVarName = name; 52 } 53 54 @Override write(@onNull WireBuffer buffer)55 public void write(@NonNull WireBuffer buffer) { 56 apply(buffer, mVarId, mVarType, mVarName); 57 } 58 59 @NonNull 60 @Override toString()61 public String toString() { 62 return "VariableName[" 63 + mVarId 64 + "] = \"" 65 + Utils.trimString(mVarName, 10) 66 + "\" type=" 67 + mVarType; 68 } 69 70 /** 71 * The name of the class 72 * 73 * @return the name 74 */ 75 @NonNull name()76 public static String name() { 77 return CLASS_NAME; 78 } 79 80 /** 81 * The OP_CODE for this command 82 * 83 * @return the opcode 84 */ id()85 public static int id() { 86 return OP_CODE; 87 } 88 89 /** 90 * Writes out the operation to the buffer 91 * 92 * @param buffer The buffer to write into 93 * @param varId id to label 94 * @param varType The type of variable 95 * @param text String 96 */ apply( @onNull WireBuffer buffer, int varId, int varType, @NonNull String text)97 public static void apply( 98 @NonNull WireBuffer buffer, int varId, int varType, @NonNull String text) { 99 buffer.start(Operations.NAMED_VARIABLE); 100 buffer.writeInt(varId); 101 buffer.writeInt(varType); 102 buffer.writeUTF8(text); 103 } 104 105 /** 106 * Read this operation and add it to the list of operations 107 * 108 * @param buffer the buffer to read 109 * @param operations the list of operations that will be added to 110 */ read(@onNull WireBuffer buffer, @NonNull List<Operation> operations)111 public static void read(@NonNull WireBuffer buffer, @NonNull List<Operation> operations) { 112 int varId = buffer.readInt(); 113 int varType = buffer.readInt(); 114 String text = buffer.readUTF8(MAX_STRING_SIZE); 115 operations.add(new NamedVariable(varId, varType, text)); 116 } 117 118 /** 119 * Populate the documentation with a description of this operation 120 * 121 * @param doc to append the description to. 122 */ documentation(@onNull DocumentationBuilder doc)123 public static void documentation(@NonNull DocumentationBuilder doc) { 124 doc.operation("Data Operations", OP_CODE, CLASS_NAME) 125 .description("Add a string name for an ID") 126 .field(INT, "varId", "id to label") 127 .field(INT, "varType", "The type of variable") 128 .field(UTF8, "name", "String"); 129 } 130 131 @Override apply(@onNull RemoteContext context)132 public void apply(@NonNull RemoteContext context) { 133 context.loadVariableName(mVarName, mVarId, mVarType); 134 } 135 136 @NonNull 137 @Override deepToString(@onNull String indent)138 public String deepToString(@NonNull String indent) { 139 return indent + toString(); 140 } 141 142 @Override serialize(MapSerializer serializer)143 public void serialize(MapSerializer serializer) { 144 serializer 145 .addType(CLASS_NAME) 146 .add("varId", mVarId) 147 .add("varName", mVarName) 148 .add("varType", typeToString()); 149 } 150 typeToString()151 private String typeToString() { 152 switch (mVarType) { 153 case COLOR_TYPE: 154 return "COLOR_TYPE"; 155 case FLOAT_TYPE: 156 return "FLOAT_TYPE"; 157 case STRING_TYPE: 158 return "STRING_TYPE"; 159 case IMAGE_TYPE: 160 return "IMAGE_TYPE"; 161 case INT_TYPE: 162 return "INT_TYPE"; 163 default: 164 return "INVALID_TYPE"; 165 } 166 } 167 } 168