blob: 62e7182a7ccda32621a8cfb7536591f8e2be83c3 [file] [log] [blame]
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -08001/*
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 */
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -080016package android.hardware.location;
17
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -080018import android.annotation.SystemApi;
19import android.os.Parcel;
20import android.os.Parcelable;
Greg Kaisercb83bd02016-04-07 15:43:38 -070021import android.util.Log;
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -080022
Arthur Ishiguro926c7342018-11-01 12:13:00 -070023import com.android.internal.util.Preconditions;
24
Peng Xu9ff7d222016-02-11 13:02:05 -080025/** A class describing nano apps.
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -080026 * A nano app is a piece of executable code that can be
27 * downloaded onto a specific architecture. These are targtted
28 * for low power compute domains on a device.
29 *
30 * Nano apps are expected to be used only by bundled apps only
31 * at this time.
32 *
Arthur Ishiguro581950c2018-01-05 10:57:46 -080033 * @deprecated Use {@link android.hardware.location.NanoAppBinary} instead to load a nanoapp with
34 * {@link android.hardware.location.ContextHubManager#loadNanoApp(
35 * ContextHubInfo, NanoAppBinary)}.
36 *
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -080037 * @hide
38 */
39@SystemApi
Arthur Ishiguro581950c2018-01-05 10:57:46 -080040@Deprecated
Arthur Ishigurof3ab9cd2018-02-28 11:27:50 -080041public class NanoApp implements Parcelable {
Greg Kaisercb83bd02016-04-07 15:43:38 -070042 private final String TAG = "NanoApp";
43
44 private final String UNKNOWN = "Unknown";
45
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -080046 private String mPublisher;
47 private String mName;
48
Ashutosh Joshi6f64bf02017-01-17 15:39:31 -080049 private long mAppId;
Greg Kaisercb83bd02016-04-07 15:43:38 -070050 private boolean mAppIdSet;
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -080051 private int mAppVersion;
52
53 private int mNeededReadMemBytes;
54 private int mNeededWriteMemBytes;
55 private int mNeededExecMemBytes;
56
57 private int[] mNeededSensors;
58 private int[] mOutputEvents;
59 private byte[] mAppBinary;
60
Greg Kaisercb83bd02016-04-07 15:43:38 -070061 /**
62 * If this version of the constructor is used, the methods
Ashutosh Joshi6f64bf02017-01-17 15:39:31 -080063 * {@link #setAppBinary(byte[])} and {@link #setAppId(long)} must be called
Greg Kaisercb83bd02016-04-07 15:43:38 -070064 * prior to passing this object to any managers.
65 *
Arthur Ishiguroafaddaa2017-07-12 16:24:09 -070066 * @see #NanoApp(long, byte[])
Greg Kaisercb83bd02016-04-07 15:43:38 -070067 */
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -080068 public NanoApp() {
Arthur Ishiguroafaddaa2017-07-12 16:24:09 -070069 this(0L, null);
Greg Kaisercb83bd02016-04-07 15:43:38 -070070 mAppIdSet = false;
71 }
72
73 /**
74 * Initialize a NanoApp with the given id and binary.
75 *
76 * While this sets defaults for other fields, users will want to provide
77 * other values for those fields in most cases.
78 *
79 * @see #setPublisher(String)
80 * @see #setName(String)
81 * @see #setAppVersion(int)
82 * @see #setNeededReadMemBytes(int)
83 * @see #setNeededWriteMemBytes(int)
84 * @see #setNeededExecMemBytes(int)
85 * @see #setNeededSensors(int[])
86 * @see #setOutputEvents(int[])
Ashutosh Joshi6f64bf02017-01-17 15:39:31 -080087 *
88 * @deprecated Use NanoApp(long, byte[]) instead
Greg Kaisercb83bd02016-04-07 15:43:38 -070089 */
Ashutosh Joshi6f64bf02017-01-17 15:39:31 -080090 @Deprecated public NanoApp(int appId, byte[] appBinary) {
91 Log.w(TAG, "NanoApp(int, byte[]) is deprecated, please use NanoApp(long, byte[]) instead.");
92 }
93
94 /**
95 * Initialize a NanoApp with the given id and binary.
96 *
97 * While this sets defaults for other fields, users will want to provide
98 * other values for those fields in most cases.
99 *
100 * @see #setPublisher(String)
101 * @see #setName(String)
102 * @see #setAppVersion(int)
103 * @see #setNeededReadMemBytes(int)
104 * @see #setNeededWriteMemBytes(int)
105 * @see #setNeededExecMemBytes(int)
106 * @see #setNeededSensors(int[])
107 * @see #setOutputEvents(int[])
108 */
109 public NanoApp(long appId, byte[] appBinary) {
Greg Kaisercb83bd02016-04-07 15:43:38 -0700110 mPublisher = UNKNOWN;
111 mName = UNKNOWN;
112
113 mAppId = appId;
114 mAppIdSet = true;
115 mAppVersion = 0;
116
117 mNeededReadMemBytes = 0;
118 mNeededWriteMemBytes = 0;
119 mNeededExecMemBytes = 0;
120
121 mNeededSensors = new int[0];
122 mOutputEvents = new int[0];
123 mAppBinary = appBinary;
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -0800124 }
125
126 /**
127 * Set the publisher name
128 *
129 * @param publisher name of the publisher of this nano app
130 */
131 public void setPublisher(String publisher) {
132 mPublisher = publisher;
133 }
134
135 /**
136 * set the name of the app
137 *
138 * @param name name of the app
139 */
140 public void setName(String name) {
141 mName = name;
142 }
143
144 /**
145 * set the app identifier
146 *
Ashutosh Joshi6f64bf02017-01-17 15:39:31 -0800147 * @param appId app identifier
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -0800148 */
Ashutosh Joshi6f64bf02017-01-17 15:39:31 -0800149 public void setAppId(long appId) {
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -0800150 mAppId = appId;
Greg Kaisercb83bd02016-04-07 15:43:38 -0700151 mAppIdSet = true;
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -0800152 }
153
154 /**
155 * Set the app version
156 *
157 * @param appVersion app version
158 */
159 public void setAppVersion(int appVersion) {
160 mAppVersion = appVersion;
161 }
162
163 /**
164 * set memory needed as read only
165 *
166 * @param neededReadMemBytes
167 * read only memory needed in bytes
168 */
169 public void setNeededReadMemBytes(int neededReadMemBytes) {
170 mNeededReadMemBytes = neededReadMemBytes;
171 }
172
173 /**
174 * set writable memory needed in bytes
175 *
176 * @param neededWriteMemBytes
177 * writable memory needed in bytes
178 */
179 public void setNeededWriteMemBytes(int neededWriteMemBytes) {
180 mNeededWriteMemBytes = neededWriteMemBytes;
181 }
182
183 /**
184 * set executable memory needed
185 *
186 * @param neededExecMemBytes
187 * executable memory needed in bytes
188 */
189 public void setNeededExecMemBytes(int neededExecMemBytes) {
190 mNeededExecMemBytes = neededExecMemBytes;
191 }
192
193 /**
194 * set the sensors needed for this app
195 *
196 * @param neededSensors
197 * needed Sensors
198 */
199 public void setNeededSensors(int[] neededSensors) {
Arthur Ishiguro926c7342018-11-01 12:13:00 -0700200 Preconditions.checkNotNull(neededSensors, "neededSensors must not be null");
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -0800201 mNeededSensors = neededSensors;
202 }
203
204 public void setOutputEvents(int[] outputEvents) {
Arthur Ishiguro926c7342018-11-01 12:13:00 -0700205 Preconditions.checkNotNull(outputEvents, "outputEvents must not be null");
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -0800206 mOutputEvents = outputEvents;
207 }
208
209 /**
210 * set output events returned by the nano app
211 *
212 * @param appBinary generated events
213 */
214 public void setAppBinary(byte[] appBinary) {
Arthur Ishiguro926c7342018-11-01 12:13:00 -0700215 Preconditions.checkNotNull(appBinary, "appBinary must not be null");
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -0800216 mAppBinary = appBinary;
217 }
218
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -0800219 /**
220 * get the publisher name
221 *
222 * @return publisher name
223 */
224 public String getPublisher() {
Greg Kaiser1f3560e2016-04-11 12:49:21 -0700225 return mPublisher;
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -0800226 }
227
228 /**
229 * get the name of the app
230 *
231 * @return app name
232 */
233 public String getName() {
Greg Kaiser1f3560e2016-04-11 12:49:21 -0700234 return mName;
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -0800235 }
236
237 /**
238 * get the identifier of the app
239 *
240 * @return identifier for this app
241 */
Ashutosh Joshi6f64bf02017-01-17 15:39:31 -0800242 public long getAppId() {
Greg Kaiser1f3560e2016-04-11 12:49:21 -0700243 return mAppId;
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -0800244 }
245
246 /**
247 * get the app version
248 *
249 * @return app version
250 */
251 public int getAppVersion() {
Greg Kaiser1f3560e2016-04-11 12:49:21 -0700252 return mAppVersion;
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -0800253 }
254
255 /**
256 * get the ammount of readable memory needed by this app
257 *
258 * @return readable memory needed in bytes
259 */
260 public int getNeededReadMemBytes() {
Greg Kaiser1f3560e2016-04-11 12:49:21 -0700261 return mNeededReadMemBytes;
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -0800262 }
263
264 /**
265 * get the ammount og writable memory needed in bytes
266 *
267 * @return writable memory needed in bytes
268 */
269 public int getNeededWriteMemBytes() {
Greg Kaiser1f3560e2016-04-11 12:49:21 -0700270 return mNeededWriteMemBytes;
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -0800271 }
272
273 /**
274 * executable memory needed in bytes
275 *
276 * @return executable memory needed in bytes
277 */
278 public int getNeededExecMemBytes() {
Greg Kaiser1f3560e2016-04-11 12:49:21 -0700279 return mNeededExecMemBytes;
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -0800280 }
281
282 /**
283 * get the sensors needed by this app
284 *
285 * @return sensors needed
286 */
287 public int[] getNeededSensors() {
Greg Kaiser1f3560e2016-04-11 12:49:21 -0700288 return mNeededSensors;
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -0800289 }
290
291 /**
292 * get the events generated by this app
293 *
294 * @return generated events
295 */
296 public int[] getOutputEvents() {
Greg Kaiser1f3560e2016-04-11 12:49:21 -0700297 return mOutputEvents;
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -0800298 }
299
300 /**
301 * get the binary for this app
302 *
303 * @return app binary
304 */
305 public byte[] getAppBinary() {
Greg Kaiser1f3560e2016-04-11 12:49:21 -0700306 return mAppBinary;
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -0800307 }
308
309 private NanoApp(Parcel in) {
310 mPublisher = in.readString();
311 mName = in.readString();
312
Ashutosh Joshi6f64bf02017-01-17 15:39:31 -0800313 mAppId = in.readLong();
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -0800314 mAppVersion = in.readInt();
315 mNeededReadMemBytes = in.readInt();
316 mNeededWriteMemBytes = in.readInt();
317 mNeededExecMemBytes = in.readInt();
318
319 int mNeededSensorsLength = in.readInt();
320 mNeededSensors = new int[mNeededSensorsLength];
321 in.readIntArray(mNeededSensors);
322
323 int mOutputEventsLength = in.readInt();
324 mOutputEvents = new int[mOutputEventsLength];
325 in.readIntArray(mOutputEvents);
326
327 int binaryLength = in.readInt();
328 mAppBinary = new byte[binaryLength];
329 in.readByteArray(mAppBinary);
330 }
331
332 public int describeContents() {
333 return 0;
334 }
335
336 public void writeToParcel(Parcel out, int flags) {
Greg Kaisercb83bd02016-04-07 15:43:38 -0700337 if (mAppBinary == null) {
338 throw new IllegalStateException("Must set non-null AppBinary for nanoapp " + mName);
339 }
340 if (!mAppIdSet) {
341 throw new IllegalStateException("Must set AppId for nanoapp " + mName);
342 }
343
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -0800344 out.writeString(mPublisher);
345 out.writeString(mName);
Ashutosh Joshi6f64bf02017-01-17 15:39:31 -0800346 out.writeLong(mAppId);
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -0800347 out.writeInt(mAppVersion);
348 out.writeInt(mNeededReadMemBytes);
349 out.writeInt(mNeededWriteMemBytes);
350 out.writeInt(mNeededExecMemBytes);
351
352 out.writeInt(mNeededSensors.length);
353 out.writeIntArray(mNeededSensors);
354
355 out.writeInt(mOutputEvents.length);
356 out.writeIntArray(mOutputEvents);
357
358 out.writeInt(mAppBinary.length);
359 out.writeByteArray(mAppBinary);
360 }
361
362 public static final Parcelable.Creator<NanoApp> CREATOR
363 = new Parcelable.Creator<NanoApp>() {
364 public NanoApp createFromParcel(Parcel in) {
365 return new NanoApp(in);
366 }
367
368 public NanoApp[] newArray(int size) {
369 return new NanoApp[size];
370 }
371 };
Ashutosh Joshi6239cc62016-04-04 16:19:29 -0700372
373 @Override
374 public String toString() {
375 String retVal = "Id : " + mAppId;
376 retVal += ", Version : " + mAppVersion;
377 retVal += ", Name : " + mName;
378 retVal += ", Publisher : " + mPublisher;
379
380 return retVal;
381 }
Ashutosh Joshi1d1ac542016-01-18 17:19:27 -0800382}