143cd1bcb10930c3e948f3d52278a986597672b29Tao Bao/*
243cd1bcb10930c3e948f3d52278a986597672b29Tao Bao * Copyright (C) 2016 The Android Open Source Project
343cd1bcb10930c3e948f3d52278a986597672b29Tao Bao *
443cd1bcb10930c3e948f3d52278a986597672b29Tao Bao * Licensed under the Apache License, Version 2.0 (the "License");
543cd1bcb10930c3e948f3d52278a986597672b29Tao Bao * you may not use this file except in compliance with the License.
643cd1bcb10930c3e948f3d52278a986597672b29Tao Bao * You may obtain a copy of the License at
743cd1bcb10930c3e948f3d52278a986597672b29Tao Bao *
843cd1bcb10930c3e948f3d52278a986597672b29Tao Bao *      http://www.apache.org/licenses/LICENSE-2.0
943cd1bcb10930c3e948f3d52278a986597672b29Tao Bao *
1043cd1bcb10930c3e948f3d52278a986597672b29Tao Bao * Unless required by applicable law or agreed to in writing, software
1143cd1bcb10930c3e948f3d52278a986597672b29Tao Bao * distributed under the License is distributed on an "AS IS" BASIS,
1243cd1bcb10930c3e948f3d52278a986597672b29Tao Bao * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1343cd1bcb10930c3e948f3d52278a986597672b29Tao Bao * See the License for the specific language governing permissions and
1443cd1bcb10930c3e948f3d52278a986597672b29Tao Bao * limitations under the License.
1543cd1bcb10930c3e948f3d52278a986597672b29Tao Bao */
1643cd1bcb10930c3e948f3d52278a986597672b29Tao Bao
1743cd1bcb10930c3e948f3d52278a986597672b29Tao Baopackage android.os;
1843cd1bcb10930c3e948f3d52278a986597672b29Tao Bao
1943cd1bcb10930c3e948f3d52278a986597672b29Tao Baoimport android.annotation.SystemApi;
2043cd1bcb10930c3e948f3d52278a986597672b29Tao Baoimport android.os.IUpdateEngine;
2143cd1bcb10930c3e948f3d52278a986597672b29Tao Baoimport android.os.IUpdateEngineCallback;
2243cd1bcb10930c3e948f3d52278a986597672b29Tao Baoimport android.os.RemoteException;
2343cd1bcb10930c3e948f3d52278a986597672b29Tao Bao
2443cd1bcb10930c3e948f3d52278a986597672b29Tao Bao/**
2543cd1bcb10930c3e948f3d52278a986597672b29Tao Bao * UpdateEngine handles calls to the update engine which takes care of A/B OTA
2643cd1bcb10930c3e948f3d52278a986597672b29Tao Bao * updates. It wraps up the update engine Binder APIs and exposes them as
27e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes * SystemApis, which will be called by the system app responsible for OTAs.
28e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes * On a Google device, this will be GmsCore.
29e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes *
30e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes * The minimal flow is:
31e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes * <ol>
32e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes * <li>Create a new UpdateEngine instance.
33e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes * <li>Call {@link #bind}, optionally providing callbacks.
34e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes * <li>Call {@link #applyPayload}.
35e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes * </ol>
36e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes *
37e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes * In addition, methods are provided to {@link #cancel} or
38e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes * {@link #suspend}/{@link #resume} application of an update.
3943cd1bcb10930c3e948f3d52278a986597672b29Tao Bao *
4043cd1bcb10930c3e948f3d52278a986597672b29Tao Bao * The APIs defined in this class and UpdateEngineCallback class must be in
4143cd1bcb10930c3e948f3d52278a986597672b29Tao Bao * sync with the ones in
4243cd1bcb10930c3e948f3d52278a986597672b29Tao Bao * system/update_engine/binder_bindings/android/os/IUpdateEngine.aidl and
4343cd1bcb10930c3e948f3d52278a986597672b29Tao Bao * system/update_engine/binder_bindings/android/os/IUpdateEngineCallback.aidl.
4443cd1bcb10930c3e948f3d52278a986597672b29Tao Bao *
4543cd1bcb10930c3e948f3d52278a986597672b29Tao Bao * {@hide}
4643cd1bcb10930c3e948f3d52278a986597672b29Tao Bao */
4743cd1bcb10930c3e948f3d52278a986597672b29Tao Bao@SystemApi
4843cd1bcb10930c3e948f3d52278a986597672b29Tao Baopublic class UpdateEngine {
4943cd1bcb10930c3e948f3d52278a986597672b29Tao Bao    private static final String TAG = "UpdateEngine";
5043cd1bcb10930c3e948f3d52278a986597672b29Tao Bao
5143cd1bcb10930c3e948f3d52278a986597672b29Tao Bao    private static final String UPDATE_ENGINE_SERVICE = "android.os.UpdateEngineService";
5243cd1bcb10930c3e948f3d52278a986597672b29Tao Bao
5343cd1bcb10930c3e948f3d52278a986597672b29Tao Bao    /**
5443cd1bcb10930c3e948f3d52278a986597672b29Tao Bao     * Error code from the update engine. Values must agree with the ones in
5543cd1bcb10930c3e948f3d52278a986597672b29Tao Bao     * system/update_engine/common/error_code.h.
5643cd1bcb10930c3e948f3d52278a986597672b29Tao Bao     */
5743cd1bcb10930c3e948f3d52278a986597672b29Tao Bao    @SystemApi
5843cd1bcb10930c3e948f3d52278a986597672b29Tao Bao    public static final class ErrorCodeConstants {
5943cd1bcb10930c3e948f3d52278a986597672b29Tao Bao        public static final int SUCCESS = 0;
6043cd1bcb10930c3e948f3d52278a986597672b29Tao Bao        public static final int ERROR = 1;
6143cd1bcb10930c3e948f3d52278a986597672b29Tao Bao        public static final int FILESYSTEM_COPIER_ERROR = 4;
6243cd1bcb10930c3e948f3d52278a986597672b29Tao Bao        public static final int POST_INSTALL_RUNNER_ERROR = 5;
6343cd1bcb10930c3e948f3d52278a986597672b29Tao Bao        public static final int PAYLOAD_MISMATCHED_TYPE_ERROR = 6;
6443cd1bcb10930c3e948f3d52278a986597672b29Tao Bao        public static final int INSTALL_DEVICE_OPEN_ERROR = 7;
6543cd1bcb10930c3e948f3d52278a986597672b29Tao Bao        public static final int KERNEL_DEVICE_OPEN_ERROR = 8;
6643cd1bcb10930c3e948f3d52278a986597672b29Tao Bao        public static final int DOWNLOAD_TRANSFER_ERROR = 9;
6743cd1bcb10930c3e948f3d52278a986597672b29Tao Bao        public static final int PAYLOAD_HASH_MISMATCH_ERROR = 10;
6843cd1bcb10930c3e948f3d52278a986597672b29Tao Bao        public static final int PAYLOAD_SIZE_MISMATCH_ERROR = 11;
6943cd1bcb10930c3e948f3d52278a986597672b29Tao Bao        public static final int DOWNLOAD_PAYLOAD_VERIFICATION_ERROR = 12;
70f5a53658b2563f6b0b7683c77c40f7d6a2a6a8f1scypher        public static final int UPDATED_BUT_NOT_ACTIVE = 52;
7143cd1bcb10930c3e948f3d52278a986597672b29Tao Bao    }
7243cd1bcb10930c3e948f3d52278a986597672b29Tao Bao
7343cd1bcb10930c3e948f3d52278a986597672b29Tao Bao    /**
7443cd1bcb10930c3e948f3d52278a986597672b29Tao Bao     * Update status code from the update engine. Values must agree with the
7543cd1bcb10930c3e948f3d52278a986597672b29Tao Bao     * ones in system/update_engine/client_library/include/update_engine/update_status.h.
7643cd1bcb10930c3e948f3d52278a986597672b29Tao Bao     */
7743cd1bcb10930c3e948f3d52278a986597672b29Tao Bao    @SystemApi
7843cd1bcb10930c3e948f3d52278a986597672b29Tao Bao    public static final class UpdateStatusConstants {
7943cd1bcb10930c3e948f3d52278a986597672b29Tao Bao        public static final int IDLE = 0;
8043cd1bcb10930c3e948f3d52278a986597672b29Tao Bao        public static final int CHECKING_FOR_UPDATE = 1;
8143cd1bcb10930c3e948f3d52278a986597672b29Tao Bao        public static final int UPDATE_AVAILABLE = 2;
8243cd1bcb10930c3e948f3d52278a986597672b29Tao Bao        public static final int DOWNLOADING = 3;
8343cd1bcb10930c3e948f3d52278a986597672b29Tao Bao        public static final int VERIFYING = 4;
8443cd1bcb10930c3e948f3d52278a986597672b29Tao Bao        public static final int FINALIZING = 5;
8543cd1bcb10930c3e948f3d52278a986597672b29Tao Bao        public static final int UPDATED_NEED_REBOOT = 6;
8643cd1bcb10930c3e948f3d52278a986597672b29Tao Bao        public static final int REPORTING_ERROR_EVENT = 7;
8743cd1bcb10930c3e948f3d52278a986597672b29Tao Bao        public static final int ATTEMPTING_ROLLBACK = 8;
8843cd1bcb10930c3e948f3d52278a986597672b29Tao Bao        public static final int DISABLED = 9;
8943cd1bcb10930c3e948f3d52278a986597672b29Tao Bao    }
9043cd1bcb10930c3e948f3d52278a986597672b29Tao Bao
9143cd1bcb10930c3e948f3d52278a986597672b29Tao Bao    private IUpdateEngine mUpdateEngine;
92445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao    private IUpdateEngineCallback mUpdateEngineCallback = null;
93445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao    private final Object mUpdateEngineCallbackLock = new Object();
9443cd1bcb10930c3e948f3d52278a986597672b29Tao Bao
95e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes    /**
96e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * Creates a new instance.
97e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     */
9843cd1bcb10930c3e948f3d52278a986597672b29Tao Bao    @SystemApi
9943cd1bcb10930c3e948f3d52278a986597672b29Tao Bao    public UpdateEngine() {
10043cd1bcb10930c3e948f3d52278a986597672b29Tao Bao        mUpdateEngine = IUpdateEngine.Stub.asInterface(
10143cd1bcb10930c3e948f3d52278a986597672b29Tao Bao                ServiceManager.getService(UPDATE_ENGINE_SERVICE));
10243cd1bcb10930c3e948f3d52278a986597672b29Tao Bao    }
10343cd1bcb10930c3e948f3d52278a986597672b29Tao Bao
104e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes    /**
105e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * Prepares this instance for use. The callback will be notified on any
106e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * status change, and when the update completes. A handler can be supplied
107e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * to control which thread runs the callback, or null.
108e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     */
10943cd1bcb10930c3e948f3d52278a986597672b29Tao Bao    @SystemApi
110b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao    public boolean bind(final UpdateEngineCallback callback, final Handler handler) {
111445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao        synchronized (mUpdateEngineCallbackLock) {
112445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao            mUpdateEngineCallback = new IUpdateEngineCallback.Stub() {
113445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao                @Override
114445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao                public void onStatusUpdate(final int status, final float percent) {
115445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao                    if (handler != null) {
116445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao                        handler.post(new Runnable() {
117445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao                            @Override
118445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao                            public void run() {
119445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao                                callback.onStatusUpdate(status, percent);
120445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao                            }
121445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao                        });
122445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao                    } else {
123445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao                        callback.onStatusUpdate(status, percent);
124445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao                    }
12543cd1bcb10930c3e948f3d52278a986597672b29Tao Bao                }
12643cd1bcb10930c3e948f3d52278a986597672b29Tao Bao
127445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao                @Override
128445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao                public void onPayloadApplicationComplete(final int errorCode) {
129445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao                    if (handler != null) {
130445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao                        handler.post(new Runnable() {
131445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao                            @Override
132445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao                            public void run() {
133445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao                                callback.onPayloadApplicationComplete(errorCode);
134445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao                            }
135445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao                        });
136445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao                    } else {
137445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao                        callback.onPayloadApplicationComplete(errorCode);
138445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao                    }
13943cd1bcb10930c3e948f3d52278a986597672b29Tao Bao                }
140445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao            };
14143cd1bcb10930c3e948f3d52278a986597672b29Tao Bao
142445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao            try {
143445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao                return mUpdateEngine.bind(mUpdateEngineCallback);
144445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao            } catch (RemoteException e) {
145445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao                throw e.rethrowFromSystemServer();
146445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao            }
147b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao        }
14843cd1bcb10930c3e948f3d52278a986597672b29Tao Bao    }
14943cd1bcb10930c3e948f3d52278a986597672b29Tao Bao
150e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes    /**
151e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * Equivalent to {@code bind(callback, null)}.
152e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     */
15343cd1bcb10930c3e948f3d52278a986597672b29Tao Bao    @SystemApi
154b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao    public boolean bind(final UpdateEngineCallback callback) {
15543cd1bcb10930c3e948f3d52278a986597672b29Tao Bao        return bind(callback, null);
15643cd1bcb10930c3e948f3d52278a986597672b29Tao Bao    }
15743cd1bcb10930c3e948f3d52278a986597672b29Tao Bao
158e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes    /**
159e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * Applies the payload found at the given {@code url}. For non-streaming
160e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * updates, the URL can be a local file using the {@code file://} scheme.
161e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     *
162e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * <p>The {@code offset} and {@code size} parameters specify the location
163e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * of the payload within the file represented by the URL. This is useful
164e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * if the downloadable package at the URL contains more than just the
165e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * update_engine payload (such as extra metadata). This is true for
166e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * Google's OTA system, where the URL points to a zip file in which the
167e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * payload is stored uncompressed within the zip file alongside other
168e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * data.
169e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     *
170e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * <p>The {@code headerKeyValuePairs} parameter is used to pass metadata
171e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * to update_engine. In Google's implementation, this is stored as
172e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * {@code payload_properties.txt} in the zip file. It's generated by the
173e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * script {@code system/update_engine/scripts/brillo_update_payload}.
174e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * The complete list of keys and their documentation is in
175e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * {@code system/update_engine/common/constants.cc}, but an example
176e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * might be:
177e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * <pre>
178e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * String[] pairs = {
179e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     *   "FILE_HASH=lURPCIkIAjtMOyB/EjQcl8zDzqtD6Ta3tJef6G/+z2k=",
180e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     *   "FILE_SIZE=871903868",
181e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     *   "METADATA_HASH=tBvj43QOB0Jn++JojcpVdbRLz0qdAuL+uTkSy7hokaw=",
182e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     *   "METADATA_SIZE=70604"
183e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * };
184e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * </pre>
185e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     */
18643cd1bcb10930c3e948f3d52278a986597672b29Tao Bao    @SystemApi
187b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao    public void applyPayload(String url, long offset, long size, String[] headerKeyValuePairs) {
188b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao        try {
189b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao            mUpdateEngine.applyPayload(url, offset, size, headerKeyValuePairs);
190b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao        } catch (RemoteException e) {
191b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao            throw e.rethrowFromSystemServer();
192b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao        }
19343cd1bcb10930c3e948f3d52278a986597672b29Tao Bao    }
19443cd1bcb10930c3e948f3d52278a986597672b29Tao Bao
195e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes    /**
196e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * Permanently cancels an in-progress update.
197e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     *
198e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * <p>See {@link #resetStatus} to undo a finshed update (only available
199e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * before the updated system has been rebooted).
200e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     *
201e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * <p>See {@link #suspend} for a way to temporarily stop an in-progress
202e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * update with the ability to resume it later.
203e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     */
20443cd1bcb10930c3e948f3d52278a986597672b29Tao Bao    @SystemApi
205b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao    public void cancel() {
206b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao        try {
207b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao            mUpdateEngine.cancel();
208b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao        } catch (RemoteException e) {
209b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao            throw e.rethrowFromSystemServer();
210b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao        }
21143cd1bcb10930c3e948f3d52278a986597672b29Tao Bao    }
21243cd1bcb10930c3e948f3d52278a986597672b29Tao Bao
213e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes    /**
214e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * Suspends an in-progress update. This can be undone by calling
215e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * {@link #resume}.
216e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     */
21743cd1bcb10930c3e948f3d52278a986597672b29Tao Bao    @SystemApi
218b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao    public void suspend() {
219b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao        try {
220b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao            mUpdateEngine.suspend();
221b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao        } catch (RemoteException e) {
222b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao            throw e.rethrowFromSystemServer();
223b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao        }
22443cd1bcb10930c3e948f3d52278a986597672b29Tao Bao    }
22543cd1bcb10930c3e948f3d52278a986597672b29Tao Bao
226e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes    /**
227e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * Resumes a suspended update.
228e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     */
22943cd1bcb10930c3e948f3d52278a986597672b29Tao Bao    @SystemApi
230b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao    public void resume() {
231b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao        try {
232b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao            mUpdateEngine.resume();
233b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao        } catch (RemoteException e) {
234b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao            throw e.rethrowFromSystemServer();
235b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao        }
236b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao    }
237b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao
238e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes    /**
239e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * Resets the bootable flag on the non-current partition and all internal
240e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * update_engine state. This can be used after an unwanted payload has been
241e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * successfully applied and the device has not yet been rebooted to signal
242e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * that we no longer want to boot into that updated system. After this call
243e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * completes, update_engine will no longer report
244e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * {@code UPDATED_NEED_REBOOT}, so your callback can remove any outstanding
245e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     * notification that rebooting into the new system is possible.
246e3ce3e8bab91117b92b2b859dff24bfe62428d5bElliott Hughes     */
247b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao    @SystemApi
248b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao    public void resetStatus() {
249b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao        try {
250b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao            mUpdateEngine.resetStatus();
251b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao        } catch (RemoteException e) {
252b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao            throw e.rethrowFromSystemServer();
253b7e47ae84f6fbfb082f5a286db2e5f0bae50c1a1Tao Bao        }
25443cd1bcb10930c3e948f3d52278a986597672b29Tao Bao    }
255445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao
256445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao    /**
257445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao     * Unbinds the last bound callback function.
258445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao     */
259445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao    @SystemApi
260445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao    public boolean unbind() {
261445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao        synchronized (mUpdateEngineCallbackLock) {
262445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao            if (mUpdateEngineCallback == null) {
263445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao                return true;
264445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao            }
265445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao            try {
266445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao                boolean result = mUpdateEngine.unbind(mUpdateEngineCallback);
267445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao                mUpdateEngineCallback = null;
268445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao                return result;
269445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao            } catch (RemoteException e) {
270445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao                throw e.rethrowFromSystemServer();
271445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao            }
272445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao        }
273445c304f5a2a2a93d06bdb24d0cd9472beb0a1fdTao Bao    }
274d27be3444f6bc5880489f4f8bc88b875bf7cedf8Tao Bao
275d27be3444f6bc5880489f4f8bc88b875bf7cedf8Tao Bao    /**
276d27be3444f6bc5880489f4f8bc88b875bf7cedf8Tao Bao     * Verifies that a payload associated with the given payload metadata
277d27be3444f6bc5880489f4f8bc88b875bf7cedf8Tao Bao     * {@code payloadMetadataFilename} can be safely applied to ths device.
278d27be3444f6bc5880489f4f8bc88b875bf7cedf8Tao Bao     * Returns {@code true} if the update can successfully be applied and
279d27be3444f6bc5880489f4f8bc88b875bf7cedf8Tao Bao     * returns {@code false} otherwise.
280d27be3444f6bc5880489f4f8bc88b875bf7cedf8Tao Bao     *
281d27be3444f6bc5880489f4f8bc88b875bf7cedf8Tao Bao     * @param payloadMetadataFilename the location of the metadata without the
282d27be3444f6bc5880489f4f8bc88b875bf7cedf8Tao Bao     * {@code file://} prefix.
283d27be3444f6bc5880489f4f8bc88b875bf7cedf8Tao Bao     */
284d27be3444f6bc5880489f4f8bc88b875bf7cedf8Tao Bao    @SystemApi
285d27be3444f6bc5880489f4f8bc88b875bf7cedf8Tao Bao    public boolean verifyPayloadMetadata(String payloadMetadataFilename) {
286d27be3444f6bc5880489f4f8bc88b875bf7cedf8Tao Bao        try {
287d27be3444f6bc5880489f4f8bc88b875bf7cedf8Tao Bao            return mUpdateEngine.verifyPayloadApplicable(payloadMetadataFilename);
288d27be3444f6bc5880489f4f8bc88b875bf7cedf8Tao Bao        } catch (RemoteException e) {
289d27be3444f6bc5880489f4f8bc88b875bf7cedf8Tao Bao            throw e.rethrowFromSystemServer();
290d27be3444f6bc5880489f4f8bc88b875bf7cedf8Tao Bao        }
291d27be3444f6bc5880489f4f8bc88b875bf7cedf8Tao Bao    }
29243cd1bcb10930c3e948f3d52278a986597672b29Tao Bao}
293