Fix error in width of nanoAppId

NanoAppId should not be 32 bits as exposed in some APIs.
Adding new methods to accept a 64 bit nanoAppId and deprecating
the buggy API methods.

Test: Compile build, ensure CHRE applications (eg: geofencing) work.

Change-Id: I08b09ff1b1d23b616214282f200202c99620c300
diff --git a/api/system-current.txt b/api/system-current.txt
index cb0d7b1..48f8ce5 100644
--- a/api/system-current.txt
+++ b/api/system-current.txt
@@ -16935,10 +16935,11 @@
 
   public class NanoApp {
     ctor public NanoApp();
-    ctor public NanoApp(int, byte[]);
+    ctor public deprecated NanoApp(int, byte[]);
+    ctor public NanoApp(long, byte[]);
     method public int describeContents();
     method public byte[] getAppBinary();
-    method public int getAppId();
+    method public long getAppId();
     method public int getAppVersion();
     method public java.lang.String getName();
     method public int getNeededExecMemBytes();
@@ -16948,7 +16949,7 @@
     method public int[] getOutputEvents();
     method public java.lang.String getPublisher();
     method public void setAppBinary(byte[]);
-    method public void setAppId(int);
+    method public void setAppId(long);
     method public void setAppVersion(int);
     method public void setName(java.lang.String);
     method public void setNeededExecMemBytes(int);
diff --git a/core/java/android/hardware/location/NanoApp.java b/core/java/android/hardware/location/NanoApp.java
index 03ac4a2..d5d428e 100644
--- a/core/java/android/hardware/location/NanoApp.java
+++ b/core/java/android/hardware/location/NanoApp.java
@@ -39,7 +39,7 @@
     private String mPublisher;
     private String mName;
 
-    private int mAppId;
+    private long mAppId;
     private boolean mAppIdSet;
     private int mAppVersion;
 
@@ -53,7 +53,7 @@
 
     /**
      * If this version of the constructor is used, the methods
-     * {@link #setAppBinary(byte[])} and {@link #setAppId(int)} must be called
+     * {@link #setAppBinary(byte[])} and {@link #setAppId(long)} must be called
      * prior to passing this object to any managers.
      *
      * @see #NanoApp(int, byte[])
@@ -77,8 +77,29 @@
      * @see #setNeededExecMemBytes(int)
      * @see #setNeededSensors(int[])
      * @see #setOutputEvents(int[])
+     *
+     * @deprecated Use NanoApp(long, byte[]) instead
      */
-    public NanoApp(int appId, byte[] appBinary) {
+    @Deprecated public NanoApp(int appId, byte[] appBinary) {
+        Log.w(TAG, "NanoApp(int, byte[]) is deprecated, please use NanoApp(long, byte[]) instead.");
+    }
+
+    /**
+     * 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(long appId, byte[] appBinary) {
         mPublisher = UNKNOWN;
         mName = UNKNOWN;
 
@@ -116,9 +137,9 @@
     /**
      * set the app identifier
      *
-     * @param appId  add identifier
+     * @param appId  app identifier
      */
-    public void setAppId(int appId) {
+    public void setAppId(long appId) {
         mAppId = appId;
         mAppIdSet = true;
     }
@@ -209,7 +230,7 @@
      *
      * @return identifier for this app
      */
-    public int getAppId() {
+    public long getAppId() {
         return mAppId;
     }
 
@@ -280,7 +301,7 @@
         mPublisher = in.readString();
         mName = in.readString();
 
-        mAppId = in.readInt();
+        mAppId = in.readLong();
         mAppVersion = in.readInt();
         mNeededReadMemBytes = in.readInt();
         mNeededWriteMemBytes = in.readInt();
@@ -313,7 +334,7 @@
 
         out.writeString(mPublisher);
         out.writeString(mName);
-        out.writeInt(mAppId);
+        out.writeLong(mAppId);
         out.writeInt(mAppVersion);
         out.writeInt(mNeededReadMemBytes);
         out.writeInt(mNeededWriteMemBytes);
diff --git a/services/core/java/com/android/server/location/ContextHubService.java b/services/core/java/com/android/server/location/ContextHubService.java
index a4c0fa8..258fb6d 100644
--- a/services/core/java/com/android/server/location/ContextHubService.java
+++ b/services/core/java/com/android/server/location/ContextHubService.java
@@ -120,36 +120,6 @@
         return mContextHubInfo[contextHubHandle];
     }
 
-    // TODO(b/30808791): Remove this when NanoApp's API is correctly treating
-    // app IDs as 64-bits.
-    private static long parseAppId(NanoApp app) {
-        // NOTE: If this shifting seems odd (since it's actually "ONAN"), note
-        //     that it matches how this is defined in context_hub.h.
-        final int HEADER_MAGIC =
-            (((int)'N' <<  0) |
-             ((int)'A' <<  8) |
-             ((int)'N' << 16) |
-             ((int)'O' << 24));
-        final int HEADER_MAGIC_OFFSET = 4;
-        final int HEADER_APP_ID_OFFSET = 8;
-
-        ByteBuffer header = ByteBuffer.wrap(app.getAppBinary())
-            .order(ByteOrder.LITTLE_ENDIAN);
-
-        try {
-            if (header.getInt(HEADER_MAGIC_OFFSET) == HEADER_MAGIC) {
-                // This is a legitimate nanoapp header.  Let's grab the app ID.
-                return header.getLong(HEADER_APP_ID_OFFSET);
-            }
-        } catch (IndexOutOfBoundsException e) {
-            // The header is undersized.  We'll fall through to our code
-            // path below, which handles being unable to parse the header.
-        }
-        // We failed to parse the header.  Even through it's probably wrong,
-        // let's give NanoApp's idea of our ID.  This is at least consistent.
-        return app.getAppId();
-    }
-
     @Override
     public int loadNanoApp(int contextHubHandle, NanoApp app) throws RemoteException {
         checkPermissions();
@@ -169,15 +139,6 @@
         msgHeader[HEADER_FIELD_MSG_TYPE] = MSG_LOAD_NANO_APP;
 
         long appId = app.getAppId();
-        // TODO(b/30808791): Remove this hack when the NanoApp API is fixed,
-        //     and getAppId() returns a 'long' instead of an 'int'.
-        if ((appId >> 32) != 0) {
-            // We're unlikely to notice this warning, but at least
-            // we can avoid running our hack logic.
-            Log.w(TAG, "Code has not been updated since API fix.");
-        } else {
-            appId = parseAppId(app);
-        }
 
         msgHeader[HEADER_FIELD_LOAD_APP_ID_LO] = (int)(appId & 0xFFFFFFFF);
         msgHeader[HEADER_FIELD_LOAD_APP_ID_HI] = (int)((appId >> 32) & 0xFFFFFFFF);