Merge "updater_config: make utils/PayloadSpecs non-static"

This commit is contained in:
Zhomart Mukhamejanov 2018-05-21 16:54:04 +00:00 committed by Gerrit Code Review
commit 928f2cbf9b
4 changed files with 17 additions and 13 deletions

View file

@ -116,6 +116,8 @@ public class PrepareStreamingService extends IntentService {
PackageFiles.PAYLOAD_PROPERTIES_FILE_NAME PackageFiles.PAYLOAD_PROPERTIES_FILE_NAME
); );
private final PayloadSpecs mPayloadSpecs = new PayloadSpecs();
@Override @Override
protected void onHandleIntent(Intent intent) { protected void onHandleIntent(Intent intent) {
Log.d(TAG, "On handle intent is called"); Log.d(TAG, "On handle intent is called");
@ -137,7 +139,7 @@ public class PrepareStreamingService extends IntentService {
* 3. Checks OTA package compatibility with the device. * 3. Checks OTA package compatibility with the device.
* 4. Constructs {@link PayloadSpec} for streaming update. * 4. Constructs {@link PayloadSpec} for streaming update.
*/ */
private static PayloadSpec execute(UpdateConfig config) private PayloadSpec execute(UpdateConfig config)
throws IOException, PreparationFailedException { throws IOException, PreparationFailedException {
downloadPreStreamingFiles(config, OTA_PACKAGE_DIR); downloadPreStreamingFiles(config, OTA_PACKAGE_DIR);
@ -164,7 +166,7 @@ public class PrepareStreamingService extends IntentService {
} }
} }
return PayloadSpecs.forStreaming(config.getUrl(), return mPayloadSpecs.forStreaming(config.getUrl(),
payloadBinary.get().getOffset(), payloadBinary.get().getOffset(),
payloadBinary.get().getSize(), payloadBinary.get().getSize(),
Paths.get(OTA_PACKAGE_DIR, PAYLOAD_PROPERTIES_FILE_NAME).toFile()); Paths.get(OTA_PACKAGE_DIR, PAYLOAD_PROPERTIES_FILE_NAME).toFile());
@ -176,7 +178,7 @@ public class PrepareStreamingService extends IntentService {
* in directory {@code dir}. * in directory {@code dir}.
* @throws IOException when can't download a file * @throws IOException when can't download a file
*/ */
private static void downloadPreStreamingFiles(UpdateConfig config, String dir) private void downloadPreStreamingFiles(UpdateConfig config, String dir)
throws IOException { throws IOException {
Log.d(TAG, "Deleting existing files from " + dir); Log.d(TAG, "Deleting existing files from " + dir);
for (String file : PRE_STREAMING_FILES_SET) { for (String file : PRE_STREAMING_FILES_SET) {
@ -200,7 +202,7 @@ public class PrepareStreamingService extends IntentService {
* @param file physical location of {@link PackageFiles#COMPATIBILITY_ZIP_FILE_NAME} * @param file physical location of {@link PackageFiles#COMPATIBILITY_ZIP_FILE_NAME}
* @return true if OTA package is compatible with this device * @return true if OTA package is compatible with this device
*/ */
private static boolean verifyPackageCompatibility(File file) { private boolean verifyPackageCompatibility(File file) {
try { try {
return RecoverySystem.verifyPackageCompatibility(file); return RecoverySystem.verifyPackageCompatibility(file);
} catch (IOException e) { } catch (IOException e) {

View file

@ -77,6 +77,7 @@ public class MainActivity extends Activity {
new AtomicInteger(UpdateEngine.UpdateStatusConstants.IDLE); new AtomicInteger(UpdateEngine.UpdateStatusConstants.IDLE);
private PayloadSpec mLastPayloadSpec; private PayloadSpec mLastPayloadSpec;
private AtomicBoolean mManualSwitchSlotRequired = new AtomicBoolean(true); private AtomicBoolean mManualSwitchSlotRequired = new AtomicBoolean(true);
private final PayloadSpecs mPayloadSpecs = new PayloadSpecs();
/** /**
* Listen to {@code update_engine} events. * Listen to {@code update_engine} events.
@ -338,7 +339,7 @@ public class MainActivity extends Activity {
if (config.getInstallType() == UpdateConfig.AB_INSTALL_TYPE_NON_STREAMING) { if (config.getInstallType() == UpdateConfig.AB_INSTALL_TYPE_NON_STREAMING) {
PayloadSpec payload; PayloadSpec payload;
try { try {
payload = PayloadSpecs.forNonStreaming(config.getUpdatePackageFile()); payload = mPayloadSpecs.forNonStreaming(config.getUpdatePackageFile());
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, "Error creating payload spec", e); Log.e(TAG, "Error creating payload spec", e);
Toast.makeText(this, "Error creating payload spec", Toast.LENGTH_LONG) Toast.makeText(this, "Error creating payload spec", Toast.LENGTH_LONG)

View file

@ -43,7 +43,7 @@ public final class PayloadSpecs {
* zip file. So we enumerate the entries to identify the offset of the payload file. * zip file. So we enumerate the entries to identify the offset of the payload file.
* http://developer.android.com/reference/java/util/zip/ZipFile.html#entries() * http://developer.android.com/reference/java/util/zip/ZipFile.html#entries()
*/ */
public static PayloadSpec forNonStreaming(File packageFile) throws IOException { public PayloadSpec forNonStreaming(File packageFile) throws IOException {
boolean payloadFound = false; boolean payloadFound = false;
long payloadOffset = 0; long payloadOffset = 0;
long payloadSize = 0; long payloadSize = 0;
@ -100,7 +100,7 @@ public final class PayloadSpecs {
/** /**
* Creates a {@link PayloadSpec} for streaming update. * Creates a {@link PayloadSpec} for streaming update.
*/ */
public static PayloadSpec forStreaming(String updateUrl, public PayloadSpec forStreaming(String updateUrl,
long offset, long offset,
long size, long size,
File propertiesFile) throws IOException { File propertiesFile) throws IOException {
@ -115,7 +115,7 @@ public final class PayloadSpecs {
/** /**
* Converts an {@link PayloadSpec} to a string. * Converts an {@link PayloadSpec} to a string.
*/ */
public static String toString(PayloadSpec payloadSpec) { public String specToString(PayloadSpec payloadSpec) {
return "<PayloadSpec url=" + payloadSpec.getUrl() return "<PayloadSpec url=" + payloadSpec.getUrl()
+ ", offset=" + payloadSpec.getOffset() + ", offset=" + payloadSpec.getOffset()
+ ", size=" + payloadSpec.getSize() + ", size=" + payloadSpec.getSize()
@ -124,6 +124,4 @@ public final class PayloadSpecs {
+ ">"; + ">";
} }
private PayloadSpecs() {}
} }

View file

@ -55,6 +55,8 @@ public class PayloadSpecsTest {
private Context mTargetContext; private Context mTargetContext;
private Context mTestContext; private Context mTestContext;
private PayloadSpecs mPayloadSpecs;
@Rule @Rule
public final ExpectedException thrown = ExpectedException.none(); public final ExpectedException thrown = ExpectedException.none();
@ -64,6 +66,7 @@ public class PayloadSpecsTest {
mTestContext = InstrumentationRegistry.getContext(); mTestContext = InstrumentationRegistry.getContext();
mTestDir = mTargetContext.getFilesDir(); mTestDir = mTargetContext.getFilesDir();
mPayloadSpecs = new PayloadSpecs();
} }
@Test @Test
@ -75,7 +78,7 @@ public class PayloadSpecsTest {
java.nio.file.Files.deleteIfExists(packageFile.toPath()); java.nio.file.Files.deleteIfExists(packageFile.toPath());
java.nio.file.Files.copy(mTestContext.getResources().openRawResource(R.raw.ota_002_package), java.nio.file.Files.copy(mTestContext.getResources().openRawResource(R.raw.ota_002_package),
packageFile.toPath()); packageFile.toPath());
PayloadSpec spec = PayloadSpecs.forNonStreaming(packageFile); PayloadSpec spec = mPayloadSpecs.forNonStreaming(packageFile);
assertEquals("correct url", "file://" + packageFile.getAbsolutePath(), spec.getUrl()); assertEquals("correct url", "file://" + packageFile.getAbsolutePath(), spec.getUrl());
assertEquals("correct payload offset", assertEquals("correct payload offset",
@ -90,7 +93,7 @@ public class PayloadSpecsTest {
@Test @Test
public void forNonStreaming_IOException() throws Exception { public void forNonStreaming_IOException() throws Exception {
thrown.expect(IOException.class); thrown.expect(IOException.class);
PayloadSpecs.forNonStreaming(new File("/fake/news.zip")); mPayloadSpecs.forNonStreaming(new File("/fake/news.zip"));
} }
@Test @Test
@ -100,7 +103,7 @@ public class PayloadSpecsTest {
long size = 200; long size = 200;
File propertiesFile = createMockPropertiesFile(); File propertiesFile = createMockPropertiesFile();
PayloadSpec spec = PayloadSpecs.forStreaming(url, offset, size, propertiesFile); PayloadSpec spec = mPayloadSpecs.forStreaming(url, offset, size, propertiesFile);
assertEquals("same url", url, spec.getUrl()); assertEquals("same url", url, spec.getUrl());
assertEquals("same offset", offset, spec.getOffset()); assertEquals("same offset", offset, spec.getOffset());
assertEquals("same size", size, spec.getSize()); assertEquals("same size", size, spec.getSize());