1 /* 2 * Copyright (C) 2016 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.hardware.location; 18 19 import android.annotation.NonNull; 20 import android.annotation.SystemApi; 21 import android.os.Parcel; 22 import android.os.Parcelable; 23 24 import libcore.util.HexEncoding; 25 26 import java.util.Arrays; 27 28 /** 29 * @deprecated Use {@link android.hardware.location.NanoAppMessage} instead to send messages with 30 * {@link android.hardware.location.ContextHubClient#sendMessageToNanoApp( 31 * NanoAppMessage)} and receive messages with 32 * {@link android.hardware.location.ContextHubClientCallback#onMessageFromNanoApp( 33 * ContextHubClient, NanoAppMessage)}. 34 * 35 * @hide 36 */ 37 @SystemApi 38 @Deprecated 39 public class ContextHubMessage implements Parcelable { 40 private static final int DEBUG_LOG_NUM_BYTES = 16; 41 private int mType; 42 private int mVersion; 43 private byte[]mData; 44 45 /** 46 * Get the message type 47 * 48 * @return int - message type 49 */ getMsgType()50 public int getMsgType() { 51 return mType; 52 } 53 54 /** 55 * get message version 56 * 57 * @return int - message version 58 */ getVersion()59 public int getVersion() { 60 return mVersion; 61 } 62 63 /** 64 * get message data 65 * 66 * @return byte[] - message data buffer 67 */ getData()68 public byte[] getData() { 69 return Arrays.copyOf(mData, mData.length); 70 } 71 72 /** 73 * set message type 74 * 75 * @param msgType - message type 76 */ setMsgType(int msgType)77 public void setMsgType(int msgType) { 78 mType = msgType; 79 } 80 81 /** 82 * Set message version 83 * 84 * @param version - message version 85 */ setVersion(int version)86 public void setVersion(int version) { 87 mVersion = version; 88 } 89 90 /** 91 * set message data 92 * 93 * @param data - message buffer 94 */ setMsgData(byte[] data)95 public void setMsgData(byte[] data) { 96 mData = Arrays.copyOf(data, data.length); 97 } 98 99 /** 100 * Constructor for a context hub message 101 * 102 * @param msgType - message type 103 * @param version - version 104 * @param data - message buffer 105 */ ContextHubMessage(int msgType, int version, byte[] data)106 public ContextHubMessage(int msgType, int version, byte[] data) { 107 mType = msgType; 108 mVersion = version; 109 mData = Arrays.copyOf(data, data.length); 110 } 111 describeContents()112 public int describeContents() { 113 return 0; 114 } 115 ContextHubMessage(Parcel in)116 private ContextHubMessage(Parcel in) { 117 mType = in.readInt(); 118 mVersion = in.readInt(); 119 int bufferLength = in.readInt(); 120 mData = new byte[bufferLength]; 121 in.readByteArray(mData); 122 } 123 writeToParcel(Parcel out, int flags)124 public void writeToParcel(Parcel out, int flags) { 125 out.writeInt(mType); 126 out.writeInt(mVersion); 127 out.writeInt(mData.length); 128 out.writeByteArray(mData); 129 } 130 131 public static final @NonNull Parcelable.Creator<ContextHubMessage> CREATOR 132 = new Parcelable.Creator<ContextHubMessage>() { 133 public ContextHubMessage createFromParcel(Parcel in) { 134 return new ContextHubMessage(in); 135 } 136 137 public ContextHubMessage[] newArray(int size) { 138 return new ContextHubMessage[size]; 139 } 140 }; 141 142 @NonNull 143 @Override toString()144 public String toString() { 145 int length = mData.length; 146 147 String ret = 148 "ContextHubMessage[type = " + mType + ", length = " + mData.length + " bytes]("; 149 if (length > 0) { 150 ret += "data = 0x"; 151 } 152 for (int i = 0; i < Math.min(length, DEBUG_LOG_NUM_BYTES); i++) { 153 ret += HexEncoding.encodeToString(mData[i], true /* upperCase */); 154 155 if ((i + 1) % 4 == 0) { 156 ret += " "; 157 } 158 } 159 if (length > DEBUG_LOG_NUM_BYTES) { 160 ret += "..."; 161 } 162 ret += ")"; 163 164 return ret; 165 } 166 } 167