From 46a51ac4b651518e7160d83c8462ab7a556bd344 Mon Sep 17 00:00:00 2001 From: Zhomart Mukhamejanov Date: Wed, 9 May 2018 09:53:45 -0700 Subject: updater_sample: add HAL compatibility check - Refactor PrepareStreamingService#onHandleIntent - Add PrepareStreamingService#verifyPackageCompatibility Test: on the device Bug: 79471299 Signed-off-by: Zhomart Mukhamejanov Change-Id: I1b18502f0638d66810a3f7ada582e4c7cea20cdb --- .../services/PrepareStreamingService.java | 85 +++++++++++++++------- 1 file changed, 59 insertions(+), 26 deletions(-) (limited to 'updater_sample/src') diff --git a/updater_sample/src/com/example/android/systemupdatersample/services/PrepareStreamingService.java b/updater_sample/src/com/example/android/systemupdatersample/services/PrepareStreamingService.java index 840a6d607..222bb0a58 100644 --- a/updater_sample/src/com/example/android/systemupdatersample/services/PrepareStreamingService.java +++ b/updater_sample/src/com/example/android/systemupdatersample/services/PrepareStreamingService.java @@ -16,6 +16,7 @@ package com.example.android.systemupdatersample.services; +import static com.example.android.systemupdatersample.util.PackageFiles.COMPATIBILITY_ZIP_FILE_NAME; import static com.example.android.systemupdatersample.util.PackageFiles.OTA_PACKAGE_DIR; import static com.example.android.systemupdatersample.util.PackageFiles.PAYLOAD_BINARY_FILE_NAME; import static com.example.android.systemupdatersample.util.PackageFiles.PAYLOAD_PROPERTIES_FILE_NAME; @@ -25,6 +26,7 @@ import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.os.Handler; +import android.os.RecoverySystem; import android.os.ResultReceiver; import android.util.Log; @@ -36,7 +38,9 @@ import com.example.android.systemupdatersample.util.PayloadSpecs; import com.example.android.systemupdatersample.util.UpdateConfigs; import com.google.common.collect.ImmutableSet; +import java.io.File; import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Paths; import java.util.Optional; @@ -119,45 +123,51 @@ public class PrepareStreamingService extends IntentService { ResultReceiver resultReceiver = intent.getParcelableExtra(EXTRA_PARAM_RESULT_RECEIVER); try { - downloadPreStreamingFiles(config, OTA_PACKAGE_DIR); - } catch (IOException e) { - Log.e(TAG, "Failed to download pre-streaming files", e); + PayloadSpec spec = execute(config); + resultReceiver.send(RESULT_CODE_SUCCESS, CallbackResultReceiver.createBundle(spec)); + } catch (Exception e) { + Log.e(TAG, "Failed to prepare streaming update", e); resultReceiver.send(RESULT_CODE_ERROR, null); - return; } + } + + /** + * 1. Downloads files for streaming updates. + * 2. Makes sure required files are present. + * 3. Checks OTA package compatibility with the device. + * 4. Constructs {@link PayloadSpec} for streaming update. + */ + private static PayloadSpec execute(UpdateConfig config) + throws IOException, PreparationFailedException { + + downloadPreStreamingFiles(config, OTA_PACKAGE_DIR); Optional payloadBinary = UpdateConfigs.getPropertyFile(PAYLOAD_BINARY_FILE_NAME, config); if (!payloadBinary.isPresent()) { - Log.e(TAG, "Failed to find " + PAYLOAD_BINARY_FILE_NAME + " in config"); - resultReceiver.send(RESULT_CODE_ERROR, null); - return; + throw new PreparationFailedException( + "Failed to find " + PAYLOAD_BINARY_FILE_NAME + " in config"); } - Optional properties = - UpdateConfigs.getPropertyFile(PAYLOAD_PROPERTIES_FILE_NAME, config); - - if (!properties.isPresent()) { - Log.e(TAG, "Failed to find " + PAYLOAD_PROPERTIES_FILE_NAME + " in config"); - resultReceiver.send(RESULT_CODE_ERROR, null); - return; + if (!UpdateConfigs.getPropertyFile(PAYLOAD_PROPERTIES_FILE_NAME, config).isPresent() + || !Paths.get(OTA_PACKAGE_DIR, PAYLOAD_PROPERTIES_FILE_NAME).toFile().exists()) { + throw new IOException(PAYLOAD_PROPERTIES_FILE_NAME + " not found"); } - PayloadSpec spec; - try { - spec = PayloadSpecs.forStreaming(config.getUrl(), - payloadBinary.get().getOffset(), - payloadBinary.get().getSize(), - Paths.get(OTA_PACKAGE_DIR, properties.get().getFilename()).toFile() - ); - } catch (IOException e) { - Log.e(TAG, "PayloadSpecs failed to create PayloadSpec for streaming", e); - resultReceiver.send(RESULT_CODE_ERROR, null); - return; + File compatibilityFile = Paths.get(OTA_PACKAGE_DIR, COMPATIBILITY_ZIP_FILE_NAME).toFile(); + if (compatibilityFile.isFile()) { + Log.i(TAG, "Verifying OTA package for compatibility with the device"); + if (!verifyPackageCompatibility(compatibilityFile)) { + throw new PreparationFailedException( + "OTA package is not compatible with this device"); + } } - resultReceiver.send(RESULT_CODE_SUCCESS, CallbackResultReceiver.createBundle(spec)); + return PayloadSpecs.forStreaming(config.getUrl(), + payloadBinary.get().getOffset(), + payloadBinary.get().getSize(), + Paths.get(OTA_PACKAGE_DIR, PAYLOAD_PROPERTIES_FILE_NAME).toFile()); } /** @@ -168,6 +178,10 @@ public class PrepareStreamingService extends IntentService { */ private static void downloadPreStreamingFiles(UpdateConfig config, String dir) throws IOException { + Log.d(TAG, "Deleting existing files from " + dir); + for (String file : PRE_STREAMING_FILES_SET) { + Files.deleteIfExists(Paths.get(OTA_PACKAGE_DIR, file)); + } Log.d(TAG, "Downloading files to " + dir); for (UpdateConfig.PackageFile file : config.getStreamingMetadata().getPropertyFiles()) { if (PRE_STREAMING_FILES_SET.contains(file.getFilename())) { @@ -182,6 +196,19 @@ public class PrepareStreamingService extends IntentService { } } + /** + * @param file physical location of {@link PackageFiles#COMPATIBILITY_ZIP_FILE_NAME} + * @return true if OTA package is compatible with this device + */ + private static boolean verifyPackageCompatibility(File file) { + try { + return RecoverySystem.verifyPackageCompatibility(file); + } catch (IOException e) { + Log.e(TAG, "Failed to verify package compatibility", e); + return false; + } + } + /** * Used by {@link PrepareStreamingService} to pass {@link PayloadSpec} * to {@link UpdateResultCallback#onReceiveResult}. @@ -213,4 +240,10 @@ public class PrepareStreamingService extends IntentService { } } + private static class PreparationFailedException extends Exception { + PreparationFailedException(String message) { + super(message); + } + } + } -- cgit v1.2.3