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
19
20import android.annotation.NonNull;
21import android.annotation.Nullable;
22import android.annotation.SystemApi;
23import android.os.Parcel;
24import android.os.Parcelable;
25
26import libcore.util.EmptyArray;
27
28/**
29 * @hide
30 */
31@SystemApi
32public class NanoAppInstanceInfo {
33    private String mPublisher;
34    private String mName;
35
36    private long mAppId;
37    private int mAppVersion;
38
39    private int mNeededReadMemBytes;
40    private int mNeededWriteMemBytes;
41    private int mNeededExecMemBytes;
42
43    private int[] mNeededSensors;
44    private int[] mOutputEvents;
45
46    private int mContexthubId;
47    private int mHandle;
48
49    public NanoAppInstanceInfo() {
50        mNeededSensors = EmptyArray.INT;
51        mOutputEvents = EmptyArray.INT;
52    }
53
54    /**
55     * get the publisher of this app
56     *
57     * @return String - name of the publisher
58     */
59    public String getPublisher() {
60        return mPublisher;
61    }
62
63
64    /**
65     * set the publisher name for the app
66     *
67     * @param publisher - name of the publisher
68     *
69     * @hide
70     */
71    public void setPublisher(String publisher) {
72        mPublisher = publisher;
73    }
74
75    /**
76     * get the name of the app
77     *
78     * @return String - name of the app
79     */
80    public String getName() {
81        return mName;
82    }
83
84    /**
85     * set the name of the app
86     *
87     * @param name - name of the app
88     *
89     * @hide
90     */
91    public void setName(String name) {
92        mName = name;
93    }
94
95    /**
96     * Get the application identifier
97     *
98     * @return int - application identifier
99     */
100    public long getAppId() {
101        return mAppId;
102    }
103
104    /**
105     * Set the application identifier
106     *
107     * @param appId - application identifier
108     *
109     * @hide
110     */
111    public void setAppId(long appId) {
112        mAppId = appId;
113    }
114
115    /**
116     * Get the application version
117     *
118     * NOTE: There is a race condition where shortly after loading, this
119     * may return -1 instead of the correct version.
120     *
121     * TODO(b/30970527): Fix this race condition.
122     *
123     * @return int - version of the app
124     */
125    public int getAppVersion() {
126        return mAppVersion;
127    }
128
129    /**
130     * Set the application version
131     *
132     * @param appVersion - version of the app
133     *
134     * @hide
135     */
136    public void setAppVersion(int appVersion) {
137        mAppVersion = appVersion;
138    }
139
140    /**
141     * Get the read memory needed by the app
142     *
143     * @return int - readable memory needed in bytes
144     */
145    public int getNeededReadMemBytes() {
146        return mNeededReadMemBytes;
147    }
148
149    /**
150     * Set the read memory needed by the app
151     *
152     * @param neededReadMemBytes - readable Memory needed in bytes
153     *
154     * @hide
155     */
156    public void setNeededReadMemBytes(int neededReadMemBytes) {
157        mNeededReadMemBytes = neededReadMemBytes;
158    }
159
160    /**
161     *  get writable memory needed by the app
162     *
163     * @return int - writable memory needed by the app
164     */
165    public int getNeededWriteMemBytes() {
166        return mNeededWriteMemBytes;
167    }
168
169    /**
170     * set writable memory needed by the app
171     *
172     * @param neededWriteMemBytes - writable memory needed by the
173     *                            app
174     *
175     * @hide
176     */
177    public void setNeededWriteMemBytes(int neededWriteMemBytes) {
178        mNeededWriteMemBytes = neededWriteMemBytes;
179    }
180
181    /**
182     * get executable memory needed by the app
183     *
184     * @return int - executable memory needed by the app
185     */
186    public int getNeededExecMemBytes() {
187        return mNeededExecMemBytes;
188    }
189
190    /**
191     * set executable memory needed by the app
192     *
193     * @param neededExecMemBytes - executable memory needed by the
194     *                           app
195     *
196     * @hide
197     */
198    public void setNeededExecMemBytes(int neededExecMemBytes) {
199        mNeededExecMemBytes = neededExecMemBytes;
200    }
201
202    /**
203     * Get the sensors needed by this app
204     *
205     * @return int[] all the required sensors needed by this app
206     */
207    @NonNull
208    public int[] getNeededSensors() {
209        return mNeededSensors;
210    }
211
212    /**
213     * set the sensors needed by this app
214     *
215     * @param neededSensors - all the sensors needed by this app
216     *
217     * @hide
218     */
219    public void setNeededSensors(@Nullable int[] neededSensors) {
220        mNeededSensors = neededSensors != null ? neededSensors : EmptyArray.INT;
221    }
222
223    /**
224     * get the events generated by this app
225     *
226     * @return all the events that can be generated by this app
227     */
228    @NonNull
229    public int[] getOutputEvents() {
230        return mOutputEvents;
231    }
232
233    /**
234     * set the output events that can be generated by this app
235     *
236     * @param outputEvents - the events that may be generated by
237     *                     this app
238     *
239     * @hide
240     */
241    public void setOutputEvents(@Nullable int[] outputEvents) {
242        mOutputEvents = outputEvents != null ? outputEvents : EmptyArray.INT;
243    }
244
245    /**
246     * get the context hub identifier
247     *
248     * @return int - system unique hub identifier
249     */
250    public int getContexthubId() {
251        return mContexthubId;
252    }
253
254    /**
255     * set the context hub identifier
256     *
257     * @param contexthubId - system wide unique identifier
258     *
259     * @hide
260     */
261    public void setContexthubId(int contexthubId) {
262        mContexthubId = contexthubId;
263    }
264
265    /**
266     * get a handle to the nano app instance
267     *
268     * @return int - handle to this instance
269     */
270    public int getHandle() {
271        return mHandle;
272    }
273
274    /**
275     * set the handle for an app instance
276     *
277     * @param handle - handle to this instance
278     *
279     * @hide
280     */
281    public void setHandle(int handle) {
282        mHandle = handle;
283    }
284
285
286    private NanoAppInstanceInfo(Parcel in) {
287        mPublisher = in.readString();
288        mName = in.readString();
289
290        mAppId = in.readLong();
291        mAppVersion = in.readInt();
292        mNeededReadMemBytes = in.readInt();
293        mNeededWriteMemBytes = in.readInt();
294        mNeededExecMemBytes = in.readInt();
295
296        int neededSensorsLength = in.readInt();
297        mNeededSensors = new int[neededSensorsLength];
298        in.readIntArray(mNeededSensors);
299
300        int outputEventsLength = in.readInt();
301        mOutputEvents = new int[outputEventsLength];
302        in.readIntArray(mOutputEvents);
303    }
304
305    public int describeContents() {
306        return 0;
307    }
308
309    public void writeToParcel(Parcel out, int flags) {
310        out.writeString(mPublisher);
311        out.writeString(mName);
312        out.writeLong(mAppId);
313        out.writeInt(mAppVersion);
314        out.writeInt(mContexthubId);
315        out.writeInt(mNeededReadMemBytes);
316        out.writeInt(mNeededWriteMemBytes);
317        out.writeInt(mNeededExecMemBytes);
318
319        // arrays are never null
320        out.writeInt(mNeededSensors.length);
321        out.writeIntArray(mNeededSensors);
322        out.writeInt(mOutputEvents.length);
323        out.writeIntArray(mOutputEvents);
324    }
325
326    public static final Parcelable.Creator<NanoAppInstanceInfo> CREATOR
327            = new Parcelable.Creator<NanoAppInstanceInfo>() {
328        public NanoAppInstanceInfo createFromParcel(Parcel in) {
329            return new NanoAppInstanceInfo(in);
330        }
331
332        public NanoAppInstanceInfo[] newArray(int size) {
333            return new NanoAppInstanceInfo[size];
334        }
335    };
336
337    @Override
338    public String toString() {
339        String retVal = "handle : " + mHandle;
340        retVal += ", Id : 0x" + Long.toHexString(mAppId);
341        retVal += ", Version : " + mAppVersion;
342        retVal += ", Name : " + mName;
343        retVal += ", Publisher : " + mPublisher;
344
345        return retVal;
346    }
347}
348