blob: 202867490fb9cf61928712f9cbe0a504a937f530 [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
18import android.os.Parcel;
19import android.os.Parcelable;
20
21/**
22 * A class describing messages send to or from nanoapps through the Context Hub Service.
23 *
24 * The basis of the class is in the IContextHub.hal ContextHubMsg definition.
25 *
26 * @hide
27 */
28public final class NanoAppMessage implements Parcelable {
29 private long mNanoAppId;
30 private int mMessageType;
31 private byte[] mMessageBody;
32 private boolean mIsBroadcasted;
33
34 private NanoAppMessage(
35 long nanoAppId, int messageType, byte[] messageBody, boolean broadcasted) {
36 mNanoAppId = nanoAppId;
37 mMessageType = messageType;
38 mMessageBody = messageBody;
39 mIsBroadcasted = broadcasted;
40 }
41
42 /**
43 * Creates a NanoAppMessage object to send to a nanoapp.
44 *
45 * This factory method can be used to generate a NanoAppMessage object to be used in
46 * the ContextHubClient.sendMessageToNanoApp API.
47 *
48 * @param targetNanoAppId the ID of the nanoapp to send the message to
49 * @param messageType the nanoapp-dependent message type
50 * @param messageBody the byte array message contents
51 *
52 * @return the NanoAppMessage object
53 */
54 public static NanoAppMessage createMessageToNanoApp(
55 long targetNanoAppId, int messageType, byte[] messageBody) {
56 return new NanoAppMessage(
57 targetNanoAppId, messageType, messageBody, false /* broadcasted */);
58 }
59
60 /**
61 * Creates a NanoAppMessage object sent from a nanoapp.
62 *
63 * This factory method is intended only to be used by the Context Hub Service when delivering
64 * messages from a nanoapp to clients.
65 *
66 * @param sourceNanoAppId the ID of the nanoapp that the message was sent from
67 * @param messageType the nanoapp-dependent message type
68 * @param messageBody the byte array message contents
69 * @param broadcasted {@code true} if the message was broadcasted, {@code false} otherwise
70 *
71 * @return the NanoAppMessage object
72 */
73 public static NanoAppMessage createMessageFromNanoApp(
74 long sourceNanoAppId, int messageType, byte[] messageBody, boolean broadcasted) {
75 return new NanoAppMessage(sourceNanoAppId, messageType, messageBody, broadcasted);
76 }
77
78 /**
79 * @return the ID of the source or destination nanoapp
80 */
81 public long getNanoAppId() {
82 return mNanoAppId;
83 }
84
85 /**
86 * @return the type of the message that is nanoapp-dependent
87 */
88 public int getMessageType() {
89 return mMessageType;
90 }
91
92 /**
93 * @return the byte array contents of the message
94 */
95 public byte[] getMessageBody() {
96 return mMessageBody;
97 }
98
99 /**
100 * @return {@code true} if the message is broadcasted, {@code false} otherwise
101 */
102 public boolean isBroadcastMessage() {
103 return mIsBroadcasted;
104 }
105
106 private NanoAppMessage(Parcel in) {
107 mNanoAppId = in.readLong();
108 mIsBroadcasted = (in.readInt() == 1);
109 mMessageType = in.readInt();
110
111 int msgSize = in.readInt();
112 mMessageBody = new byte[msgSize];
113 in.readByteArray(mMessageBody);
114 }
115
116 @Override
117 public int describeContents() {
118 return 0;
119 }
120
121 @Override
122 public void writeToParcel(Parcel out, int flags) {
123 out.writeLong(mNanoAppId);
124 out.writeInt(mIsBroadcasted ? 1 : 0);
125 out.writeInt(mMessageType);
126
127 out.writeInt(mMessageBody.length);
128 out.writeByteArray(mMessageBody);
129 }
130
131 public static final Creator<NanoAppMessage> CREATOR =
132 new Creator<NanoAppMessage>() {
133 @Override
134 public NanoAppMessage createFromParcel(Parcel in) {
135 return new NanoAppMessage(in);
136 }
137
138 @Override
139 public NanoAppMessage[] newArray(int size) {
140 return new NanoAppMessage[size];
141 }
142 };
143}