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 */
16package android.hardware.location;
17
18import android.annotation.SystemApi;
19import android.os.Parcel;
20import android.os.Parcelable;
21import android.util.Log;
22
23/** A class describing nano apps.
24 * A nano app is a piece of executable code that can be
25 * downloaded onto a specific architecture. These are targtted
26 * for low power compute domains on a device.
27 *
28 * Nano apps are expected to be used only by bundled apps only
29 * at this time.
30 *
31 * @hide
32 */
33@SystemApi
34public class NanoApp {
35    private final String TAG = "NanoApp";
36
37    private final String UNKNOWN = "Unknown";
38
39    private String mPublisher;
40    private String mName;
41
42    private long mAppId;
43    private boolean mAppIdSet;
44    private int mAppVersion;
45
46    private int mNeededReadMemBytes;
47    private int mNeededWriteMemBytes;
48    private int mNeededExecMemBytes;
49
50    private int[] mNeededSensors;
51    private int[] mOutputEvents;
52    private byte[] mAppBinary;
53
54    /**
55     * If this version of the constructor is used, the methods
56     * {@link #setAppBinary(byte[])} and {@link #setAppId(long)} must be called
57     * prior to passing this object to any managers.
58     *
59     * @see #NanoApp(int, byte[])
60     */
61    public NanoApp() {
62        this(0, null);
63        mAppIdSet = false;
64    }
65
66    /**
67     * Initialize a NanoApp with the given id and binary.
68     *
69     * While this sets defaults for other fields, users will want to provide
70     * other values for those fields in most cases.
71     *
72     * @see #setPublisher(String)
73     * @see #setName(String)
74     * @see #setAppVersion(int)
75     * @see #setNeededReadMemBytes(int)
76     * @see #setNeededWriteMemBytes(int)
77     * @see #setNeededExecMemBytes(int)
78     * @see #setNeededSensors(int[])
79     * @see #setOutputEvents(int[])
80     *
81     * @deprecated Use NanoApp(long, byte[]) instead
82     */
83    @Deprecated public NanoApp(int appId, byte[] appBinary) {
84        Log.w(TAG, "NanoApp(int, byte[]) is deprecated, please use NanoApp(long, byte[]) instead.");
85    }
86
87    /**
88     * Initialize a NanoApp with the given id and binary.
89     *
90     * While this sets defaults for other fields, users will want to provide
91     * other values for those fields in most cases.
92     *
93     * @see #setPublisher(String)
94     * @see #setName(String)
95     * @see #setAppVersion(int)
96     * @see #setNeededReadMemBytes(int)
97     * @see #setNeededWriteMemBytes(int)
98     * @see #setNeededExecMemBytes(int)
99     * @see #setNeededSensors(int[])
100     * @see #setOutputEvents(int[])
101     */
102    public NanoApp(long appId, byte[] appBinary) {
103        mPublisher = UNKNOWN;
104        mName = UNKNOWN;
105
106        mAppId = appId;
107        mAppIdSet = true;
108        mAppVersion = 0;
109
110        mNeededReadMemBytes = 0;
111        mNeededWriteMemBytes = 0;
112        mNeededExecMemBytes = 0;
113
114        mNeededSensors = new int[0];
115        mOutputEvents = new int[0];
116        mAppBinary = appBinary;
117    }
118
119    /**
120     * Set the publisher name
121     *
122     * @param publisher name of the publisher of this nano app
123     */
124    public void setPublisher(String publisher) {
125        mPublisher = publisher;
126    }
127
128    /**
129     * set the name of the app
130     *
131     * @param name   name of the app
132     */
133    public void setName(String name) {
134        mName = name;
135    }
136
137    /**
138     * set the app identifier
139     *
140     * @param appId  app identifier
141     */
142    public void setAppId(long appId) {
143        mAppId = appId;
144        mAppIdSet = true;
145    }
146
147    /**
148     * Set the app version
149     *
150     * @param appVersion app version
151     */
152    public void setAppVersion(int appVersion) {
153        mAppVersion = appVersion;
154    }
155
156    /**
157     * set memory needed as read only
158     *
159     * @param neededReadMemBytes
160     *               read only memory needed in bytes
161     */
162    public void setNeededReadMemBytes(int neededReadMemBytes) {
163        mNeededReadMemBytes = neededReadMemBytes;
164    }
165
166    /**
167     * set writable memory needed in bytes
168     *
169     * @param neededWriteMemBytes
170     *               writable memory needed in bytes
171     */
172    public void setNeededWriteMemBytes(int neededWriteMemBytes) {
173        mNeededWriteMemBytes = neededWriteMemBytes;
174    }
175
176    /**
177     * set executable memory needed
178     *
179     * @param neededExecMemBytes
180     *               executable memory needed in bytes
181     */
182    public void setNeededExecMemBytes(int neededExecMemBytes) {
183        mNeededExecMemBytes = neededExecMemBytes;
184    }
185
186    /**
187     * set the sensors needed for this app
188     *
189     * @param neededSensors
190     *               needed Sensors
191     */
192    public void setNeededSensors(int[] neededSensors) {
193        mNeededSensors = neededSensors;
194    }
195
196    public void setOutputEvents(int[] outputEvents) {
197        mOutputEvents = outputEvents;
198    }
199
200    /**
201     * set output events returned by the nano app
202     *
203     * @param appBinary generated events
204     */
205    public void setAppBinary(byte[] appBinary) {
206        mAppBinary = appBinary;
207    }
208
209
210    /**
211     * get the publisher name
212     *
213     * @return publisher name
214     */
215    public String getPublisher() {
216        return mPublisher;
217    }
218
219    /**
220     * get the name of the app
221     *
222     * @return app name
223     */
224    public String getName() {
225        return mName;
226    }
227
228    /**
229     * get the identifier of the app
230     *
231     * @return identifier for this app
232     */
233    public long getAppId() {
234        return mAppId;
235    }
236
237    /**
238     * get the app version
239     *
240     * @return app version
241     */
242    public int getAppVersion() {
243        return mAppVersion;
244    }
245
246    /**
247     * get the ammount of readable memory needed by this app
248     *
249     * @return readable memory needed in bytes
250     */
251    public int getNeededReadMemBytes() {
252        return mNeededReadMemBytes;
253    }
254
255    /**
256     * get the ammount og writable memory needed in bytes
257     *
258     * @return writable memory needed in bytes
259     */
260    public int getNeededWriteMemBytes() {
261        return mNeededWriteMemBytes;
262    }
263
264    /**
265     * executable memory needed in bytes
266     *
267     * @return executable memory needed in bytes
268     */
269    public int getNeededExecMemBytes() {
270        return mNeededExecMemBytes;
271    }
272
273    /**
274     * get the sensors needed by this app
275     *
276     * @return sensors needed
277     */
278    public int[] getNeededSensors() {
279        return mNeededSensors;
280    }
281
282    /**
283     * get the events generated by this app
284     *
285     * @return generated events
286     */
287    public int[] getOutputEvents() {
288        return mOutputEvents;
289    }
290
291    /**
292     * get the binary for this app
293     *
294     * @return app binary
295     */
296    public byte[] getAppBinary() {
297        return mAppBinary;
298    }
299
300    private NanoApp(Parcel in) {
301        mPublisher = in.readString();
302        mName = in.readString();
303
304        mAppId = in.readLong();
305        mAppVersion = in.readInt();
306        mNeededReadMemBytes = in.readInt();
307        mNeededWriteMemBytes = in.readInt();
308        mNeededExecMemBytes = in.readInt();
309
310        int mNeededSensorsLength = in.readInt();
311        mNeededSensors = new int[mNeededSensorsLength];
312        in.readIntArray(mNeededSensors);
313
314        int mOutputEventsLength = in.readInt();
315        mOutputEvents = new int[mOutputEventsLength];
316        in.readIntArray(mOutputEvents);
317
318        int binaryLength = in.readInt();
319        mAppBinary = new byte[binaryLength];
320        in.readByteArray(mAppBinary);
321    }
322
323    public int describeContents() {
324        return 0;
325    }
326
327    public void writeToParcel(Parcel out, int flags) {
328        if (mAppBinary == null) {
329            throw new IllegalStateException("Must set non-null AppBinary for nanoapp " + mName);
330        }
331        if (!mAppIdSet) {
332            throw new IllegalStateException("Must set AppId for nanoapp " + mName);
333        }
334
335        out.writeString(mPublisher);
336        out.writeString(mName);
337        out.writeLong(mAppId);
338        out.writeInt(mAppVersion);
339        out.writeInt(mNeededReadMemBytes);
340        out.writeInt(mNeededWriteMemBytes);
341        out.writeInt(mNeededExecMemBytes);
342
343        out.writeInt(mNeededSensors.length);
344        out.writeIntArray(mNeededSensors);
345
346        out.writeInt(mOutputEvents.length);
347        out.writeIntArray(mOutputEvents);
348
349        out.writeInt(mAppBinary.length);
350        out.writeByteArray(mAppBinary);
351    }
352
353    public static final Parcelable.Creator<NanoApp> CREATOR
354            = new Parcelable.Creator<NanoApp>() {
355        public NanoApp createFromParcel(Parcel in) {
356            return new NanoApp(in);
357        }
358
359        public NanoApp[] newArray(int size) {
360            return new NanoApp[size];
361        }
362    };
363
364    @Override
365    public String toString() {
366        String retVal = "Id : " + mAppId;
367        retVal += ", Version : " + mAppVersion;
368        retVal += ", Name : " + mName;
369        retVal += ", Publisher : " + mPublisher;
370
371        return retVal;
372    }
373}
374