diff --git a/android/assets/keystore/tablexiaTrustKeystore b/android/assets/keystore/tablexiaTrustKeystore new file mode 100644 index 0000000000000000000000000000000000000000..c6fc6aa613a00d20a189b6bfcf97cdea8c8294f3 Binary files /dev/null and b/android/assets/keystore/tablexiaTrustKeystore differ diff --git a/core/src/cz/nic/tablexia/loader/TablexiaAbstractFileManager.java b/core/src/cz/nic/tablexia/loader/TablexiaAbstractFileManager.java index ec8ccc68c65fcf79b85101a0eb685904d04a89d4..63deed69fccdf658f20d4a13046ccde2e6c9b686 100644 --- a/core/src/cz/nic/tablexia/loader/TablexiaAbstractFileManager.java +++ b/core/src/cz/nic/tablexia/loader/TablexiaAbstractFileManager.java @@ -115,6 +115,30 @@ public abstract class TablexiaAbstractFileManager extends AssetManager { } } + public enum DownloadStorageType implements StorageType { + + INTERNAL(RootStorageType.INTERNAL, DownloadStorageType.DOWNLOAD_DIRECTORY), + EXTERNAL(RootStorageType.EXTERNAL, DownloadStorageType.DOWNLOAD_DIRECTORY); + + public static final String DOWNLOAD_DIRECTORY = "download/"; + + private String storagePath; + private FileHandleResolver resolver; + + DownloadStorageType(RootStorageType rootStorageType, String assetsPath) { + this.storagePath = rootStorageType.getStoragePath() + assetsPath; + this.resolver = rootStorageType.getResolver(); + } + + public String getStoragePath() { + return storagePath; + } + + public FileHandleResolver getResolver() { + return resolver; + } + } + public static FileHandle getFileStoragePathFileHandle(StorageType storageType, String additionalPath) { return storageType.getResolver().resolve(storageType.getStoragePath() + additionalPath); } diff --git a/core/src/cz/nic/tablexia/loader/zip/ZipAssetLoader.java b/core/src/cz/nic/tablexia/loader/zip/ZipAssetLoader.java index 8f2b68b726b8b7ae10733791f78d6b55044c8a9f..51e390f0e69920b22549a7288e7e5d8d28bbb023 100644 --- a/core/src/cz/nic/tablexia/loader/zip/ZipAssetLoader.java +++ b/core/src/cz/nic/tablexia/loader/zip/ZipAssetLoader.java @@ -1,5 +1,7 @@ package cz.nic.tablexia.loader.zip; +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.Net; import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.utils.async.AsyncTask; @@ -8,6 +10,8 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.security.NoSuchAlgorithmException; import java.util.Locale; import java.util.Map; @@ -15,10 +19,12 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import cz.nic.tablexia.TablexiaSettings; +import cz.nic.tablexia.bus.ApplicationBus; import cz.nic.tablexia.checksum.Checksum; import cz.nic.tablexia.loader.IApplicationLoader; import cz.nic.tablexia.loader.TablexiaAbstractFileManager; import cz.nic.tablexia.loader.TablexiaDataManager; +import cz.nic.tablexia.screen.AbstractTablexiaScreen; import cz.nic.tablexia.util.Log; /** @@ -29,8 +35,12 @@ import cz.nic.tablexia.util.Log; */ public class ZipAssetLoader extends TablexiaDataManager<Void> implements IApplicationLoader { - public static final TablexiaAbstractFileManager.RootStorageType ZIP_FILES_STORAGE_TYPE = TablexiaAbstractFileManager.RootStorageType.INTERNAL; - public static final String ZIP_FILE_EXTENSION = ".zip"; + public static final TablexiaAbstractFileManager.RootStorageType ZIP_FILES_STORAGE_TYPE = TablexiaAbstractFileManager.RootStorageType.INTERNAL; + public static final String ZIP_FILE_EXTENSION = ".zip"; + private static final String TABLEXIA_ASSETS_DOWNLOAD_BASE_PATH = "https://www.tablexia.cz/static/assets/"; + private static final int TABLEXIA_ASSETS_DOWNLOAD_TIMEOUT = 2500; + + private static final Object LOCK = new Object(); private static class ZipAssetLoaderTask implements AsyncTask<Void> { @@ -68,6 +78,10 @@ public class ZipAssetLoader extends TablexiaDataManager<Void> implements IApplic startTime = System.nanoTime(); } + prepareKeyStore(); + download("cs.zip"); + download("sk.zip"); + // check current content if (buildChecksums != null) { String buildChecksum = buildChecksums.get(language); @@ -110,6 +124,69 @@ public class ZipAssetLoader extends TablexiaDataManager<Void> implements IApplic return null; } + private void prepareKeyStore() { + System.setProperty("javax.net.ssl.trustStore", TablexiaAbstractFileManager.getFileStoragePath(TablexiaAbstractFileManager.RootStorageType.INTERNAL, "keystore/tablexiaTrustKeystore")); + System.setProperty("javax.net.ssl.trustStorePassword","tablexia"); + } + + public void download(final String fileName) { + + Net.HttpRequest request = new Net.HttpRequest(Net.HttpMethods.GET); + request.setTimeOut(TABLEXIA_ASSETS_DOWNLOAD_TIMEOUT); + request.setUrl(TABLEXIA_ASSETS_DOWNLOAD_BASE_PATH + fileName); + + Gdx.net.sendHttpRequest(request, new Net.HttpResponseListener() { + + @Override + public void handleHttpResponse(Net.HttpResponse httpResponse) { + long length = Long.parseLong(httpResponse.getHeader("Content-Length")); + + InputStream is = httpResponse.getResultAsStream(); + OutputStream os = TablexiaAbstractFileManager.getFileStoragePathFileHandle(TablexiaAbstractFileManager.DownloadStorageType.EXTERNAL, fileName).write(false); + + byte[] bytes = new byte[1024]; + int count = -1; + long read = 0; + try { + while ((count = is.read(bytes, 0, bytes.length)) != -1) { + os.write(bytes, 0, count); + read += count; + + final int progress = ((int) (((double) read / (double) length) * 100)); + ApplicationBus.getInstance().post(new AbstractTablexiaScreen.ScreenInfoEvent("Downloading: ", fileName + " [ " + progress + "% ]")).asynchronously(); + } + + // download complete + synchronized (LOCK) { + LOCK.notify(); + } + + } catch (IOException e) { + Log.err(ZipAssetLoader.class, "Cannot download file: " + fileName + " !", e); + } + } + + @Override + public void failed(Throwable t) { + Log.err(ZipAssetLoader.class, "Downloading of file: " + fileName + " Failed!", t); + } + + @Override + public void cancelled() { + Log.info(ZipAssetLoader.class, "Downloading of file: " + fileName + " Canceled!"); + } + }); + + // wait to download complete + synchronized (LOCK) { + try { + LOCK.wait(); + } catch (InterruptedException e) { + Log.err(ZipAssetLoader.class, "Cannot wait to download end!", e); + } + } + } + public static void unzip(FileHandle zipFile, String extractDestinationDirectory) throws IOException { ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(zipFile.read())); try {