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
This commit is contained in:
Greg Kaiser
2016-04-07 15:43:38 -07:00
parent ffdaae004b
commit cb83bd04b7
2 changed files with 56 additions and 0 deletions

View File

@@ -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();

View File

@@ -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);