Add system APIs for A/B update.

Add android.os.UpdateEngine and UpdateEngineCallback classes that
communicate with the update engine via binder APIs for A/B update.

Bug: 26695366
Change-Id: I2471858d28df5443bc0b56d9a20281ccebde4484
diff --git a/api/system-current.txt b/api/system-current.txt
index 2adb1e6..93872a9 100644
--- a/api/system-current.txt
+++ b/api/system-current.txt
@@ -31497,6 +31497,51 @@
     ctor public TransactionTooLargeException(java.lang.String);
   }
 
+  public class UpdateEngine {
+    ctor public UpdateEngine();
+    method public void applyPayload(java.lang.String, long, long, java.lang.String[]) throws android.os.RemoteException;
+    method public boolean bind(android.os.UpdateEngineCallback, android.os.Handler) throws android.os.RemoteException;
+    method public boolean bind(android.os.UpdateEngineCallback) throws android.os.RemoteException;
+    method public void cancel() throws android.os.RemoteException;
+    method public void resume() throws android.os.RemoteException;
+    method public void suspend() throws android.os.RemoteException;
+  }
+
+  public static final class UpdateEngine.ErrorCodeConstants {
+    ctor public UpdateEngine.ErrorCodeConstants();
+    field public static final int DOWNLOAD_PAYLOAD_VERIFICATION_ERROR = 12; // 0xc
+    field public static final int DOWNLOAD_TRANSFER_ERROR = 9; // 0x9
+    field public static final int ERROR = 1; // 0x1
+    field public static final int FILESYSTEM_COPIER_ERROR = 4; // 0x4
+    field public static final int INSTALL_DEVICE_OPEN_ERROR = 7; // 0x7
+    field public static final int KERNEL_DEVICE_OPEN_ERROR = 8; // 0x8
+    field public static final int PAYLOAD_HASH_MISMATCH_ERROR = 10; // 0xa
+    field public static final int PAYLOAD_MISMATCHED_TYPE_ERROR = 6; // 0x6
+    field public static final int PAYLOAD_SIZE_MISMATCH_ERROR = 11; // 0xb
+    field public static final int POST_INSTALL_RUNNER_ERROR = 5; // 0x5
+    field public static final int SUCCESS = 0; // 0x0
+  }
+
+  public static final class UpdateEngine.UpdateStatusConstants {
+    ctor public UpdateEngine.UpdateStatusConstants();
+    field public static final int ATTEMPTING_ROLLBACK = 8; // 0x8
+    field public static final int CHECKING_FOR_UPDATE = 1; // 0x1
+    field public static final int DISABLED = 9; // 0x9
+    field public static final int DOWNLOADING = 3; // 0x3
+    field public static final int FINALIZING = 5; // 0x5
+    field public static final int IDLE = 0; // 0x0
+    field public static final int REPORTING_ERROR_EVENT = 7; // 0x7
+    field public static final int UPDATED_NEED_REBOOT = 6; // 0x6
+    field public static final int UPDATE_AVAILABLE = 2; // 0x2
+    field public static final int VERIFYING = 4; // 0x4
+  }
+
+  public abstract class UpdateEngineCallback {
+    ctor public UpdateEngineCallback();
+    method public abstract void onPayloadApplicationComplete(int);
+    method public abstract void onStatusUpdate(int, float);
+  }
+
   public final class UserHandle implements android.os.Parcelable {
     ctor public UserHandle(android.os.Parcel);
     method public int describeContents();
diff --git a/core/java/android/os/UpdateEngine.java b/core/java/android/os/UpdateEngine.java
new file mode 100644
index 0000000..80e6146
--- /dev/null
+++ b/core/java/android/os/UpdateEngine.java
@@ -0,0 +1,148 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.os;
+
+import android.annotation.SystemApi;
+import android.os.IUpdateEngine;
+import android.os.IUpdateEngineCallback;
+import android.os.RemoteException;
+
+import android.util.Log;
+
+/**
+ * UpdateEngine handles calls to the update engine which takes care of A/B OTA
+ * updates. It wraps up the update engine Binder APIs and exposes them as
+ * SystemApis, which will be called by system apps like GmsCore.
+ *
+ * The APIs defined in this class and UpdateEngineCallback class must be in
+ * sync with the ones in
+ * system/update_engine/binder_bindings/android/os/IUpdateEngine.aidl and
+ * system/update_engine/binder_bindings/android/os/IUpdateEngineCallback.aidl.
+ *
+ * {@hide}
+ */
+@SystemApi
+public class UpdateEngine {
+    private static final String TAG = "UpdateEngine";
+
+    private static final String UPDATE_ENGINE_SERVICE = "android.os.UpdateEngineService";
+
+    /**
+     * Error code from the update engine. Values must agree with the ones in
+     * system/update_engine/common/error_code.h.
+     */
+    @SystemApi
+    public static final class ErrorCodeConstants {
+        public static final int SUCCESS = 0;
+        public static final int ERROR = 1;
+        public static final int FILESYSTEM_COPIER_ERROR = 4;
+        public static final int POST_INSTALL_RUNNER_ERROR = 5;
+        public static final int PAYLOAD_MISMATCHED_TYPE_ERROR = 6;
+        public static final int INSTALL_DEVICE_OPEN_ERROR = 7;
+        public static final int KERNEL_DEVICE_OPEN_ERROR = 8;
+        public static final int DOWNLOAD_TRANSFER_ERROR = 9;
+        public static final int PAYLOAD_HASH_MISMATCH_ERROR = 10;
+        public static final int PAYLOAD_SIZE_MISMATCH_ERROR = 11;
+        public static final int DOWNLOAD_PAYLOAD_VERIFICATION_ERROR = 12;
+    }
+
+    /**
+     * Update status code from the update engine. Values must agree with the
+     * ones in system/update_engine/client_library/include/update_engine/update_status.h.
+     */
+    @SystemApi
+    public static final class UpdateStatusConstants {
+        public static final int IDLE = 0;
+        public static final int CHECKING_FOR_UPDATE = 1;
+        public static final int UPDATE_AVAILABLE = 2;
+        public static final int DOWNLOADING = 3;
+        public static final int VERIFYING = 4;
+        public static final int FINALIZING = 5;
+        public static final int UPDATED_NEED_REBOOT = 6;
+        public static final int REPORTING_ERROR_EVENT = 7;
+        public static final int ATTEMPTING_ROLLBACK = 8;
+        public static final int DISABLED = 9;
+    }
+
+    private IUpdateEngine mUpdateEngine;
+
+    @SystemApi
+    public UpdateEngine() {
+        mUpdateEngine = IUpdateEngine.Stub.asInterface(
+                ServiceManager.getService(UPDATE_ENGINE_SERVICE));
+    }
+
+    @SystemApi
+    public boolean bind(final UpdateEngineCallback callback, final Handler handler) throws RemoteException {
+        IUpdateEngineCallback updateEngineCallback = new IUpdateEngineCallback.Stub() {
+            @Override
+            public void onStatusUpdate(final int status, final float percent) {
+                if (handler != null) {
+                    handler.post(new Runnable() {
+                        @Override
+                        public void run() {
+                            callback.onStatusUpdate(status, percent);
+                        }
+                    });
+                } else {
+                    callback.onStatusUpdate(status, percent);
+                }
+            }
+
+            @Override
+            public void onPayloadApplicationComplete(final int errorCode) {
+                if (handler != null) {
+                    handler.post(new Runnable() {
+                        @Override
+                        public void run() {
+                            callback.onPayloadApplicationComplete(errorCode);
+                        }
+                    });
+                } else {
+                    callback.onPayloadApplicationComplete(errorCode);
+                }
+            }
+        };
+
+        return mUpdateEngine.bind(updateEngineCallback);
+    }
+
+    @SystemApi
+    public boolean bind(final UpdateEngineCallback callback) throws RemoteException {
+        return bind(callback, null);
+    }
+
+    @SystemApi
+    public void applyPayload(String url, long offset, long size, String[] headerKeyValuePairs) throws RemoteException {
+        mUpdateEngine.applyPayload(url, offset, size, headerKeyValuePairs);
+    }
+
+    @SystemApi
+    public void cancel() throws RemoteException {
+        mUpdateEngine.cancel();
+    }
+
+    @SystemApi
+    public void suspend() throws RemoteException {
+        mUpdateEngine.suspend();
+    }
+
+    @SystemApi
+    public void resume() throws RemoteException {
+        mUpdateEngine.resume();
+    }
+}
diff --git a/core/java/android/os/UpdateEngineCallback.java b/core/java/android/os/UpdateEngineCallback.java
new file mode 100644
index 0000000..b3b856f
--- /dev/null
+++ b/core/java/android/os/UpdateEngineCallback.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.os;
+
+import android.annotation.SystemApi;
+
+/**
+ * Callback function for UpdateEngine.
+ *
+ * The APIs defined in this class and UpdateEngine class must be in sync with
+ * the ones in
+ * system/update_engine/binder_bindings/android/os/IUpdateEngine.aidl and
+ * system/update_engine/binder_bindings/android/os/IUpdateEngineCallback.aidl.
+ *
+ * {@hide}
+ */
+@SystemApi
+public abstract class UpdateEngineCallback {
+
+    @SystemApi
+    public abstract void onStatusUpdate(int status, float percent);
+
+    @SystemApi
+    public abstract void onPayloadApplicationComplete(int errorCode);
+}