| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.hardware.location; |
| 17 |
|
| 18 |
import android.annotation.NonNull; |
| 19 |
import android.annotation.Nullable; |
| 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 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
@SystemApi |
| 36 |
public final class NanoAppMessage implements Parcelable { |
| 37 |
private static final int DEBUG_LOG_NUM_BYTES = 16; |
| 38 |
private long mNanoAppId; |
| 39 |
private int mMessageType; |
| 40 |
private byte[] mMessageBody; |
| 41 |
private boolean mIsBroadcasted; |
| 42 |
|
| 43 |
private NanoAppMessage( |
| 44 |
long nanoAppId, int messageType, byte[] messageBody, boolean broadcasted) { |
| 45 |
mNanoAppId = nanoAppId; |
| 46 |
mMessageType = messageType; |
| 47 |
mMessageBody = messageBody; |
| 48 |
mIsBroadcasted = broadcasted; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public static NanoAppMessage createMessageToNanoApp( |
| 64 |
long targetNanoAppId, int messageType, byte[] messageBody) { |
| 65 |
return new NanoAppMessage( |
| 66 |
targetNanoAppId, messageType, messageBody, false ); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public static NanoAppMessage createMessageFromNanoApp( |
| 83 |
long sourceNanoAppId, int messageType, byte[] messageBody, boolean broadcasted) { |
| 84 |
return new NanoAppMessage(sourceNanoAppId, messageType, messageBody, broadcasted); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public long getNanoAppId() { |
| 91 |
return mNanoAppId; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public int getMessageType() { |
| 98 |
return mMessageType; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public byte[] getMessageBody() { |
| 105 |
return mMessageBody; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public boolean isBroadcastMessage() { |
| 112 |
return mIsBroadcasted; |
| 113 |
} |
| 114 |
|
| 115 |
private NanoAppMessage(Parcel in) { |
| 116 |
mNanoAppId = in.readLong(); |
| 117 |
mIsBroadcasted = (in.readInt() == 1); |
| 118 |
mMessageType = in.readInt(); |
| 119 |
|
| 120 |
int msgSize = in.readInt(); |
| 121 |
mMessageBody = new byte[msgSize]; |
| 122 |
in.readByteArray(mMessageBody); |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public int describeContents() { |
| 127 |
return 0; |
| 128 |
} |
| 129 |
|
| 130 |
@Override |
| 131 |
public void writeToParcel(Parcel out, int flags) { |
| 132 |
out.writeLong(mNanoAppId); |
| 133 |
out.writeInt(mIsBroadcasted ? 1 : 0); |
| 134 |
out.writeInt(mMessageType); |
| 135 |
|
| 136 |
out.writeInt(mMessageBody.length); |
| 137 |
out.writeByteArray(mMessageBody); |
| 138 |
} |
| 139 |
|
| 140 |
public static final @NonNull Creator<NanoAppMessage> CREATOR = |
| 141 |
new Creator<NanoAppMessage>() { |
| 142 |
@Override |
| 143 |
public NanoAppMessage createFromParcel(Parcel in) { |
| 144 |
return new NanoAppMessage(in); |
| 145 |
} |
| 146 |
|
| 147 |
@Override |
| 148 |
public NanoAppMessage[] newArray(int size) { |
| 149 |
return new NanoAppMessage[size]; |
| 150 |
} |
| 151 |
}; |
| 152 |
|
| 153 |
@NonNull |
| 154 |
@Override |
| 155 |
public String toString() { |
| 156 |
int length = mMessageBody.length; |
| 157 |
|
| 158 |
String ret = "NanoAppMessage[type = " + mMessageType + ", length = " + mMessageBody.length |
| 159 |
+ " bytes, " + (mIsBroadcasted ? "broadcast" : "unicast") + ", nanoapp = 0x" |
| 160 |
+ Long.toHexString(mNanoAppId) + "]("; |
| 161 |
if (length > 0) { |
| 162 |
ret += "data = 0x"; |
| 163 |
} |
| 164 |
for (int i = 0; i < Math.min(length, DEBUG_LOG_NUM_BYTES); i++) { |
| 165 |
ret += HexEncoding.encodeToString(mMessageBody[i], true ); |
| 166 |
|
| 167 |
if ((i + 1) % 4 == 0) { |
| 168 |
ret += " "; |
| 169 |
} |
| 170 |
} |
| 171 |
if (length > DEBUG_LOG_NUM_BYTES) { |
| 172 |
ret += "..."; |
| 173 |
} |
| 174 |
ret += ")"; |
| 175 |
|
| 176 |
return ret; |
| 177 |
} |
| 178 |
|
| 179 |
@Override |
| 180 |
public boolean equals(@Nullable Object object) { |
| 181 |
if (object == this) { |
| 182 |
return true; |
| 183 |
} |
| 184 |
|
| 185 |
boolean isEqual = false; |
| 186 |
if (object instanceof NanoAppMessage) { |
| 187 |
NanoAppMessage other = (NanoAppMessage) object; |
| 188 |
isEqual = (other.getNanoAppId() == mNanoAppId) |
| 189 |
&& (other.getMessageType() == mMessageType) |
| 190 |
&& (other.isBroadcastMessage() == mIsBroadcasted) |
| 191 |
&& Arrays.equals(other.getMessageBody(), mMessageBody); |
| 192 |
} |
| 193 |
|
| 194 |
return isEqual; |
| 195 |
} |
| 196 |
} |