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