blob: 9f90d5966e4da7084705179cb5505a10c028f8fd [file] [log] [blame]
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -07001/*
2 * Copyright 2017 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 */
16package android.hardware.location;
17
Arthur Ishiguro1eef69f2018-11-08 15:17:11 -080018import android.annotation.Nullable;
Arthur Ishigurof2b6f012017-11-28 15:21:38 -080019import android.annotation.SystemApi;
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -070020import android.os.Parcel;
21import android.os.Parcelable;
22
Arthur Ishiguro1eef69f2018-11-08 15:17:11 -080023import java.util.Arrays;
24
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -070025/**
26 * A class describing messages send to or from nanoapps through the Context Hub Service.
27 *
28 * The basis of the class is in the IContextHub.hal ContextHubMsg definition.
29 *
30 * @hide
31 */
Arthur Ishigurof2b6f012017-11-28 15:21:38 -080032@SystemApi
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -070033public final class NanoAppMessage implements Parcelable {
Arthur Ishiguro1d3b23c2018-01-05 16:05:25 -080034 private static final int DEBUG_LOG_NUM_BYTES = 16;
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -070035 private long mNanoAppId;
36 private int mMessageType;
37 private byte[] mMessageBody;
38 private boolean mIsBroadcasted;
39
40 private NanoAppMessage(
41 long nanoAppId, int messageType, byte[] messageBody, boolean broadcasted) {
42 mNanoAppId = nanoAppId;
43 mMessageType = messageType;
44 mMessageBody = messageBody;
45 mIsBroadcasted = broadcasted;
46 }
47
48 /**
49 * Creates a NanoAppMessage object to send to a nanoapp.
50 *
51 * This factory method can be used to generate a NanoAppMessage object to be used in
52 * the ContextHubClient.sendMessageToNanoApp API.
53 *
54 * @param targetNanoAppId the ID of the nanoapp to send the message to
55 * @param messageType the nanoapp-dependent message type
56 * @param messageBody the byte array message contents
57 *
58 * @return the NanoAppMessage object
59 */
60 public static NanoAppMessage createMessageToNanoApp(
61 long targetNanoAppId, int messageType, byte[] messageBody) {
62 return new NanoAppMessage(
63 targetNanoAppId, messageType, messageBody, false /* broadcasted */);
64 }
65
66 /**
67 * Creates a NanoAppMessage object sent from a nanoapp.
68 *
69 * This factory method is intended only to be used by the Context Hub Service when delivering
70 * messages from a nanoapp to clients.
71 *
72 * @param sourceNanoAppId the ID of the nanoapp that the message was sent from
73 * @param messageType the nanoapp-dependent message type
74 * @param messageBody the byte array message contents
75 * @param broadcasted {@code true} if the message was broadcasted, {@code false} otherwise
76 *
77 * @return the NanoAppMessage object
78 */
79 public static NanoAppMessage createMessageFromNanoApp(
80 long sourceNanoAppId, int messageType, byte[] messageBody, boolean broadcasted) {
81 return new NanoAppMessage(sourceNanoAppId, messageType, messageBody, broadcasted);
82 }
83
84 /**
85 * @return the ID of the source or destination nanoapp
86 */
87 public long getNanoAppId() {
88 return mNanoAppId;
89 }
90
91 /**
92 * @return the type of the message that is nanoapp-dependent
93 */
94 public int getMessageType() {
95 return mMessageType;
96 }
97
98 /**
99 * @return the byte array contents of the message
100 */
101 public byte[] getMessageBody() {
102 return mMessageBody;
103 }
104
105 /**
106 * @return {@code true} if the message is broadcasted, {@code false} otherwise
107 */
108 public boolean isBroadcastMessage() {
109 return mIsBroadcasted;
110 }
111
112 private NanoAppMessage(Parcel in) {
113 mNanoAppId = in.readLong();
114 mIsBroadcasted = (in.readInt() == 1);
115 mMessageType = in.readInt();
116
117 int msgSize = in.readInt();
118 mMessageBody = new byte[msgSize];
119 in.readByteArray(mMessageBody);
120 }
121
122 @Override
123 public int describeContents() {
124 return 0;
125 }
126
127 @Override
128 public void writeToParcel(Parcel out, int flags) {
129 out.writeLong(mNanoAppId);
130 out.writeInt(mIsBroadcasted ? 1 : 0);
131 out.writeInt(mMessageType);
132
133 out.writeInt(mMessageBody.length);
134 out.writeByteArray(mMessageBody);
135 }
136
137 public static final Creator<NanoAppMessage> CREATOR =
138 new Creator<NanoAppMessage>() {
139 @Override
140 public NanoAppMessage createFromParcel(Parcel in) {
141 return new NanoAppMessage(in);
142 }
143
144 @Override
145 public NanoAppMessage[] newArray(int size) {
146 return new NanoAppMessage[size];
147 }
148 };
Arthur Ishiguro1d3b23c2018-01-05 16:05:25 -0800149
150 @Override
151 public String toString() {
152 int length = mMessageBody.length;
153
154 String ret = "NanoAppMessage[type = " + mMessageType + ", length = " + mMessageBody.length
155 + " bytes, " + (mIsBroadcasted ? "broadcast" : "unicast") + ", nanoapp = 0x"
156 + Long.toHexString(mNanoAppId) + "](";
157 if (length > 0) {
158 ret += "data = 0x";
159 }
160 for (int i = 0; i < Math.min(length, DEBUG_LOG_NUM_BYTES); i++) {
161 ret += Byte.toHexString(mMessageBody[i], true /* upperCase */);
162
163 if ((i + 1) % 4 == 0) {
164 ret += " ";
165 }
166 }
167 if (length > DEBUG_LOG_NUM_BYTES) {
168 ret += "...";
169 }
170 ret += ")";
171
172 return ret;
173 }
Arthur Ishiguro1eef69f2018-11-08 15:17:11 -0800174
175 @Override
176 public boolean equals(@Nullable Object object) {
177 if (object == this) {
178 return true;
179 }
180
181 boolean isEqual = false;
182 if (object instanceof NanoAppMessage) {
183 NanoAppMessage other = (NanoAppMessage) object;
184 isEqual = (other.getNanoAppId() == mNanoAppId)
185 && (other.getMessageType() == mMessageType)
186 && (other.isBroadcastMessage() == mIsBroadcasted)
187 && Arrays.equals(other.getMessageBody(), mMessageBody);
188 }
189
190 return isEqual;
191 }
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -0700192}