Commit 3d2f6532 authored by Vitaliy Vashchenko's avatar Vitaliy Vashchenko

#681 Using snapshot if checksum package is not found on server and application...

#681 Using snapshot if checksum package is not found on server and application is in non-release mode.
parent 32791a1c
......@@ -105,64 +105,32 @@ public class TablexiaAssetsAsyncTask implements AsyncTask<Void>, TablexiaAssetsM
}
Log.info(getClass(), "User decided to use mobile data to download assets");
}
if (!downloadAssetsPackage(checksumPackageFileHandle, assetsPackageFileHandle)) {
if (TablexiaSettings.getInstance().getBuildType().equals(TablexiaSettings.BuildType.RELEASE)){
manager.onLoadUnsuccessful();
return null;
}else {
assetsPackageFileHandle = TablexiaAbstractFileManager.getFileStoragePathFileHandle(TablexiaAbstractFileManager.DownloadStorageType.EXTERNAL,
locale.getLanguage() + "_" + SNAPSHOT_FILE_NAME + ZIP_FILE_EXTENSION);
checksumPackageFileHandle = TablexiaAbstractFileManager.getFileStoragePathFileHandle(TablexiaAbstractFileManager.DownloadStorageType.EXTERNAL,
locale.getLanguage() + "_" + SNAPSHOT_FILE_NAME + CHECKSUM_FILE_EXTENSION);
Log.info(getClass(), "Downloading checksum file");
if (!tablexiaAssetsDownloader.downloadChecksumFile(checksumPackageFileHandle)) {
Log.info(getClass(), "Checksum file download failed");
if (!TablexiaSettings.getInstance().getBuildType().equals(TablexiaSettings.BuildType.RELEASE)) {
Log.info(getClass(),TablexiaSettings.getInstance().getBuildType().name() + " mode: trying to use snapshot");
assetsPackageName = locale.getLanguage() + "_" + SNAPSHOT_FILE_NAME + ZIP_FILE_EXTENSION;
checksumPackageName = locale.getLanguage() + "_" + SNAPSHOT_FILE_NAME + CHECKSUM_FILE_EXTENSION;
assetsPackageFileHandle = TablexiaAbstractFileManager.getFileStoragePathFileHandle(TablexiaAbstractFileManager.DownloadStorageType.EXTERNAL, assetsPackageName);
checksumPackageFileHandle = TablexiaAbstractFileManager.getFileStoragePathFileHandle(TablexiaAbstractFileManager.DownloadStorageType.EXTERNAL, checksumPackageName);
ApplicationBus.getInstance().post(TablexiaAssetsManager.AssetsManagerEvent.assetsCheckStartedEvent()).asynchronously();
if (!TablexiaAssetsUtil.checkAssetsPackage(checksumPackageFileHandle, assetsPackageFileHandle)) {
Log.info(getClass(), "Local snapshot is not valid");
Log.info(getClass(), "Local snapshot isn't valid.");
TablexiaAssetsUtil.removeSnapshotsForLocale(locale);
if (!tablexiaAssetsDownloader.downloadChecksumFile(checksumPackageFileHandle)) {
Log.info(getClass(), "Can't download snapshot checksum file");
manager.onLoadUnsuccessful();
return null;
}
if (!tablexiaAssetsDownloader.downloadSnapshotPackage(assetsPackageFileHandle)){
if (!downloadAssetsPackage(checksumPackageFileHandle, assetsPackageFileHandle)) {
Log.info(getClass(),"Can't download snapshot");
manager.onLoadUnsuccessful();
return null;
}
checkSpaceAndUnzip(assetsPackageFileHandle);
return null;
} else {
//Local snapshot is valid, unzip and use it
Log.info(getClass(), "Local snapshot package is valid");
checkSpaceAndUnzip(assetsPackageFileHandle);
return null;
Log.info(getClass(),"Snapshot was successfully downloaded");
}
}else {
//Failed to download valid assets on release
manager.onLoadUnsuccessful();
return null;
}
}
Log.info(getClass(), "Starting to download assets package");
if (!tablexiaAssetsDownloader.downloadAssetsPackage(assetsPackageFileHandle)) {
Log.info(getClass(), "Cant load assets package");
manager.onLoadUnsuccessful();
return null;
}
Log.info(getClass(), "Validating downloaded packages");
ApplicationBus.getInstance().post(TablexiaAssetsManager.AssetsManagerEvent.assetsCheckStartedEvent()).asynchronously();
if (!TablexiaAssetsUtil.checkAssetsPackage(checksumPackageFileHandle, assetsPackageFileHandle)) {
Log.info(getClass(), "Downloaded packages are not valid");
manager.onLoadUnsuccessful();
return null;
}
checkSpaceAndUnzip(assetsPackageFileHandle);
return null;
}
//Check If there is enough space to extract the package
......@@ -170,6 +138,15 @@ public class TablexiaAssetsAsyncTask implements AsyncTask<Void>, TablexiaAssetsM
return null;
}
private boolean downloadAssetsPackage(FileHandle checksumFileHandle, FileHandle assetsPackageFileHandle){
if (!tablexiaAssetsDownloader.downloadChecksumFile(checksumFileHandle)) return false;
Log.info(getClass(),"Checksum file was downloaded.");
if (!tablexiaAssetsDownloader.downloadAssetsPackage(assetsPackageFileHandle)) return false;
Log.info(getClass(),"Assets package was downloaded.");
return true;
}
private void checkSpaceAndUnzip(FileHandle assetsPackFileHandle) throws IOException {
if (isEnoughSpace(TablexiaAbstractFileManager.AssetsStorageType.EXTERNAL)) {
//Extract package and exit
......
......@@ -43,6 +43,7 @@ public class TablexiaAssetsDownloader implements TablexiaAssetsManager.ILoaderDi
private static final int TABLEXIA_ASSETS_DOWNLOAD_TIMEOUT = 2500;
private static final int STATUS_CODE_SUCCESS = 206;
private static final int STATUS_CODE_OK = 200;
private static final int STATUS_CODE_NF = 404;
private static final int NUMBER_OF_TRIES = 3;
private static final float TRIES_DELAY_SECONDS = 1.5f;
......@@ -60,6 +61,8 @@ public class TablexiaAssetsDownloader implements TablexiaAssetsManager.ILoaderDi
private final boolean[] downloadResult = new boolean[]{false};
private final boolean[] hasDownloadResult = new boolean[]{false};
private final boolean[] downloadInProcess = new boolean[]{false};
private final boolean[] errorOccured = new boolean[]{false};
private final byte[] byteBuffer = new byte[1024];
private FileHandle fileHandle;
......@@ -82,14 +85,27 @@ public class TablexiaAssetsDownloader implements TablexiaAssetsManager.ILoaderDi
public void reset(){
hasDownloadResult[0] = false;
downloadInProcess[0] = false;
errorOccured[0] = false;
}
@Override
public void handleHttpResponse(Net.HttpResponse httpResponse) {
int responseStatusCode = httpResponse.getStatus().getStatusCode();
if (responseStatusCode != STATUS_CODE_SUCCESS && responseStatusCode != STATUS_CODE_OK) {
if (responseStatusCode != STATUS_CODE_SUCCESS && responseStatusCode != STATUS_CODE_OK && responseStatusCode != STATUS_CODE_NF) {
Log.info(CLASS, String.format("Download failed. Returned status code: %d", httpResponse.getStatus().getStatusCode()));
tryAgain();
return;
}else if (responseStatusCode == STATUS_CODE_NF){
Log.info(getClass(),"File was not found on server");
errorOccured[0] = true;
hasDownloadResult[0] = true;
downloadResult[0] = false;
synchronized (DOWNLOAD_LOCK) {
DOWNLOAD_LOCK.notify();
}
return;
}
......@@ -177,6 +193,10 @@ public class TablexiaAssetsDownloader implements TablexiaAssetsManager.ILoaderDi
return downloadInProcess[0];
}
public boolean isErrorOccured(){
return errorOccured[0];
}
}
///// TABLEXIA ASSETS DOWNLOADER CLASS /////
......@@ -238,6 +258,8 @@ public class TablexiaAssetsDownloader implements TablexiaAssetsManager.ILoaderDi
}
}
} else { //Has Result
if (httpResponseListener.isErrorOccured()) break;
if (!httpResponseListener.isDownloadInProcess()) {
//show dialog
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment