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 package com.android.internal.widget.remotecompose.core.operations; 17 18 import static com.android.internal.widget.remotecompose.core.documentation.DocumentedOperation.INT; 19 20 import android.annotation.NonNull; 21 22 import com.android.internal.widget.remotecompose.core.Operation; 23 import com.android.internal.widget.remotecompose.core.Operations; 24 import com.android.internal.widget.remotecompose.core.RemoteContext; 25 import com.android.internal.widget.remotecompose.core.SerializableToString; 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.documentation.DocumentedOperation; 29 import com.android.internal.widget.remotecompose.core.operations.utilities.StringSerializer; 30 import com.android.internal.widget.remotecompose.core.serialize.MapSerializer; 31 import com.android.internal.widget.remotecompose.core.serialize.Serializable; 32 33 import java.util.List; 34 35 public class ComponentValue extends Operation implements SerializableToString, Serializable { 36 private static final int OP_CODE = Operations.COMPONENT_VALUE; 37 private static final String CLASS_NAME = "ComponentValue"; 38 39 public static final int WIDTH = 0; 40 public static final int HEIGHT = 1; 41 42 private int mType = WIDTH; 43 private int mComponentID = -1; 44 private int mValueId = -1; 45 46 /** 47 * The OP_CODE for this command 48 * 49 * @return the opcode 50 */ id()51 public static int id() { 52 return OP_CODE; 53 } 54 55 /** 56 * The name of the class 57 * 58 * @return the name 59 */ 60 @NonNull name()61 public static String name() { 62 return CLASS_NAME; 63 } 64 65 @NonNull 66 @Override toString()67 public String toString() { 68 return CLASS_NAME + "(" + mType + ", " + mComponentID + ", " + mValueId + ")"; 69 } 70 getType()71 public int getType() { 72 return mType; 73 } 74 getComponentId()75 public int getComponentId() { 76 return mComponentID; 77 } 78 getValueId()79 public int getValueId() { 80 return mValueId; 81 } 82 83 @Override write(@onNull WireBuffer buffer)84 public void write(@NonNull WireBuffer buffer) { 85 apply(buffer, mType, mComponentID, mValueId); 86 } 87 88 @Override apply(@onNull RemoteContext context)89 public void apply(@NonNull RemoteContext context) { 90 // Nothing 91 } 92 93 /** 94 * Read this operation and add it to the list of operations 95 * 96 * @param buffer the buffer to read 97 * @param operations the list of operations that will be added to 98 */ read(@onNull WireBuffer buffer, @NonNull List<Operation> operations)99 public static void read(@NonNull WireBuffer buffer, @NonNull List<Operation> operations) { 100 int type = buffer.readInt(); 101 int componentId = buffer.readInt(); 102 int valueId = buffer.readInt(); 103 ComponentValue op = new ComponentValue(type, componentId, valueId); 104 operations.add(op); 105 } 106 107 /** 108 * Populate the documentation with a description of this operation 109 * 110 * @param doc to append the description to. 111 */ documentation(@onNull DocumentationBuilder doc)112 public static void documentation(@NonNull DocumentationBuilder doc) { 113 doc.operation("Expressions Operations", OP_CODE, CLASS_NAME) 114 .description("Encode a component-related value (eg its width, height etc.)") 115 .field( 116 DocumentedOperation.INT, 117 "TYPE", 118 "The type of value, either WIDTH(0) or HEIGHT(1)") 119 .field(INT, "COMPONENT_ID", "The component id to reference") 120 .field( 121 INT, 122 "VALUE_ID", 123 "The id of the RemoteFloat representing the described" 124 + " component value, which can be used in expressions"); 125 } 126 ComponentValue(int type, int componentId, int valueId)127 public ComponentValue(int type, int componentId, int valueId) { 128 mType = type; 129 mComponentID = componentId; 130 mValueId = valueId; 131 } 132 133 /** 134 * Writes out the ComponentValue to the buffer 135 * 136 * @param buffer buffer to write to 137 * @param type type of value (WIDTH or HEIGHT) 138 * @param componentId component id to reference 139 * @param valueId remote float used to represent the component value 140 */ apply(@onNull WireBuffer buffer, int type, int componentId, int valueId)141 public static void apply(@NonNull WireBuffer buffer, int type, int componentId, int valueId) { 142 buffer.start(OP_CODE); 143 buffer.writeInt(type); 144 buffer.writeInt(componentId); 145 buffer.writeInt(valueId); 146 } 147 148 @NonNull 149 @Override deepToString(@onNull String indent)150 public String deepToString(@NonNull String indent) { 151 return null; 152 } 153 154 @Override serializeToString(int indent, @NonNull StringSerializer serializer)155 public void serializeToString(int indent, @NonNull StringSerializer serializer) { 156 String type = "WIDTH"; 157 if (mType == HEIGHT) { 158 type = "HEIGHT"; 159 } 160 serializer.append( 161 indent, 162 CLASS_NAME 163 + " value " 164 + mValueId 165 + " set to " 166 + type 167 + " of Component " 168 + mComponentID); 169 } 170 171 @Override serialize(MapSerializer serializer)172 public void serialize(MapSerializer serializer) { 173 serializer 174 .addType(CLASS_NAME) 175 .add("valueId", mValueId) 176 .add("componentValueType", typeToString(mType)) 177 .add("componentId", mComponentID); 178 } 179 typeToString(int type)180 private String typeToString(int type) { 181 if (type == WIDTH) return "WIDTH"; 182 if (type == HEIGHT) return "HEIGHT"; 183 return "INVALID_TYPE"; 184 } 185 } 186