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