From cb83bd04b7dac57be7f382be7343bfe9f1a598ab Mon Sep 17 00:00:00 2001 From: Greg Kaiser Date: Thu, 7 Apr 2016 15:43:38 -0700 Subject: [PATCH] ContextHubManager: Avoid bad NanoApp objects We provide a preferred constructor for the user which will always generate a serializable NanoApp object. While we still support the other path, we throw a specific exception when trying to serialize a bad NanoApp object to ease debugging. Change-Id: I7ee610f0fce2f0dd489526e4819f66035281024b --- api/system-current.txt | 1 + .../android/hardware/location/NanoApp.java | 55 +++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/api/system-current.txt b/api/system-current.txt index d91f5d18a52c6..04110c0639c3f 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -15491,6 +15491,7 @@ package android.hardware.location { public class NanoApp { ctor public NanoApp(); + ctor public NanoApp(int, byte[]); method public int describeContents(); method public byte[] getAppBinary(); method public int getAppId(); diff --git a/core/java/android/hardware/location/NanoApp.java b/core/java/android/hardware/location/NanoApp.java index c8f3439aee700..8d50be671106c 100644 --- a/core/java/android/hardware/location/NanoApp.java +++ b/core/java/android/hardware/location/NanoApp.java @@ -18,6 +18,7 @@ package android.hardware.location; import android.annotation.SystemApi; import android.os.Parcel; import android.os.Parcelable; +import android.util.Log; /** A class describing nano apps. * A nano app is a piece of executable code that can be @@ -31,10 +32,15 @@ import android.os.Parcelable; */ @SystemApi public class NanoApp { + private final String TAG = "NanoApp"; + + private final String UNKNOWN = "Unknown"; + private String mPublisher; private String mName; private int mAppId; + private boolean mAppIdSet; private int mAppVersion; private int mNeededReadMemBytes; @@ -45,7 +51,48 @@ public class NanoApp { private int[] mOutputEvents; private byte[] mAppBinary; + /** + * If this version of the constructor is used, the methods + * {@link #setAppBinary(byte[])} and {@link #setAppId(int)} must be called + * prior to passing this object to any managers. + * + * @see #NanoApp(int, byte[]) + */ public NanoApp() { + this(0, null); + mAppIdSet = false; + } + + /** + * Initialize a NanoApp with the given id and binary. + * + * While this sets defaults for other fields, users will want to provide + * other values for those fields in most cases. + * + * @see #setPublisher(String) + * @see #setName(String) + * @see #setAppVersion(int) + * @see #setNeededReadMemBytes(int) + * @see #setNeededWriteMemBytes(int) + * @see #setNeededExecMemBytes(int) + * @see #setNeededSensors(int[]) + * @see #setOutputEvents(int[]) + */ + public NanoApp(int appId, byte[] appBinary) { + mPublisher = UNKNOWN; + mName = UNKNOWN; + + mAppId = appId; + mAppIdSet = true; + mAppVersion = 0; + + mNeededReadMemBytes = 0; + mNeededWriteMemBytes = 0; + mNeededExecMemBytes = 0; + + mNeededSensors = new int[0]; + mOutputEvents = new int[0]; + mAppBinary = appBinary; } /** @@ -73,6 +120,7 @@ public class NanoApp { */ public void setAppId(int appId) { mAppId = appId; + mAppIdSet = true; } /** @@ -256,6 +304,13 @@ public class NanoApp { } public void writeToParcel(Parcel out, int flags) { + if (mAppBinary == null) { + throw new IllegalStateException("Must set non-null AppBinary for nanoapp " + mName); + } + if (!mAppIdSet) { + throw new IllegalStateException("Must set AppId for nanoapp " + mName); + } + out.writeString(mPublisher); out.writeString(mName); out.writeInt(mAppId);