Search in sources :

Example 1 with LigerDownloadManager

use of scal.io.liger.LigerDownloadManager in project storymaker by StoryMaker.

the class StorymakerDownloadHelper method checkAndDownloadNew.

public static boolean checkAndDownloadNew(Context context) {
    boolean mainFileOk = true;
    boolean patchFileOk = true;
    boolean fileStateOk = true;
    String filePath = ZipHelper.getExpansionZipDirectory(context, scal.io.liger.Constants.MAIN, scal.io.liger.Constants.MAIN_VERSION);
    String fileName = ZipHelper.getExpansionZipFilename(context, scal.io.liger.Constants.MAIN, scal.io.liger.Constants.MAIN_VERSION);
    File expansionFile = new File(filePath + fileName);
    if (expansionFile.exists()) {
        if (expansionFile.length() == 0) {
            Timber.d("MAIN EXPANSION FILE " + fileName + " IS A ZERO BYTE FILE ");
            mainFileOk = false;
        }
        if ((scal.io.liger.Constants.MAIN_SIZE > 0) && (scal.io.liger.Constants.MAIN_SIZE > expansionFile.length())) {
            Timber.d("MAIN EXPANSION FILE " + fileName + " IS TOO SMALL (" + expansionFile.length() + "/" + scal.io.liger.Constants.MAIN_SIZE + ")");
            mainFileOk = false;
        }
    } else {
        // file does not exist, flag for downloading
        Timber.d("MAIN EXPANSION FILE " + fileName + " DOES NOT EXIST ");
        mainFileOk = false;
    }
    if (mainFileOk) {
        Timber.d("MAIN EXPANSION FILE " + fileName + " CHECKS OUT, NO DOWNLOAD");
    } else {
        Timber.d("MAIN EXPANSION FILE " + fileName + " MUST BE DOWNLOADED");
        final LigerDownloadManager expansionDownload = new LigerDownloadManager(scal.io.liger.Constants.MAIN, scal.io.liger.Constants.MAIN_VERSION, context);
        Thread expansionDownloadThread = new Thread(expansionDownload);
        expansionDownloadThread.start();
        // downloading a new main file, must clear ZipHelper cache
        // ZipHelper.clearCache();
        fileStateOk = false;
    }
    // if the main file is newer than the patch file, remove the patch file rather than downloading
    if (scal.io.liger.Constants.PATCH_VERSION > 0) {
        if (scal.io.liger.Constants.PATCH_VERSION < scal.io.liger.Constants.MAIN_VERSION) {
            File obbDirectory = new File(ZipHelper.getObbFolderName(context));
            File fileDirectory = new File(ZipHelper.getFileFolderName(context));
            String nameFilter = scal.io.liger.Constants.PATCH + ".*." + context.getPackageName() + ".obb";
            Timber.d("CLEANUP: DELETING " + nameFilter + " FROM " + obbDirectory.getPath());
            WildcardFileFilter obbFileFilter = new WildcardFileFilter(nameFilter);
            for (File obbFile : FileUtils.listFiles(obbDirectory, obbFileFilter, null)) {
                Timber.d("CLEANUP: FOUND " + obbFile.getPath() + ", DELETING");
                FileUtils.deleteQuietly(obbFile);
            }
            Timber.d("CLEANUP: DELETING " + nameFilter + " FROM " + fileDirectory.getPath());
            WildcardFileFilter fileFileFilter = new WildcardFileFilter(nameFilter);
            for (File fileFile : FileUtils.listFiles(fileDirectory, fileFileFilter, null)) {
                Timber.d("CLEANUP: FOUND " + fileFile.getPath() + ", DELETING");
                FileUtils.deleteQuietly(fileFile);
            }
        } else {
            String patchName = ZipHelper.getExpansionZipFilename(context, scal.io.liger.Constants.PATCH, scal.io.liger.Constants.PATCH_VERSION);
            expansionFile = new File(filePath + patchName);
            if (expansionFile.exists()) {
                if (expansionFile.length() == 0) {
                    Timber.d("EXPANSION FILE PATCH " + patchName + " IS A ZERO BYTE FILE ");
                    patchFileOk = false;
                }
                if ((scal.io.liger.Constants.PATCH_SIZE > 0) && (scal.io.liger.Constants.PATCH_SIZE > expansionFile.length())) {
                    Timber.d("EXPANSION FILE PATCH " + fileName + " IS TOO SMALL (" + expansionFile.length() + "/" + scal.io.liger.Constants.PATCH_SIZE + ")");
                    patchFileOk = false;
                }
            } else {
                // file does not exist, flag for downloading
                Timber.d("EXPANSION FILE PATCH " + patchName + " DOES NOT EXIST ");
                patchFileOk = false;
            }
            if (patchFileOk) {
                Timber.d("EXPANSION FILE PATCH " + patchName + " CHECKS OUT, NO DOWNLOAD");
            } else {
                Timber.d("EXPANSION FILE PATCH " + patchName + " MUST BE DOWNLOADED");
                final LigerDownloadManager expansionDownload = new LigerDownloadManager(scal.io.liger.Constants.PATCH, scal.io.liger.Constants.PATCH_VERSION, context);
                Thread expansionDownloadThread = new Thread(expansionDownload);
                expansionDownloadThread.start();
                // downloading a new patch file, must clear ZipHelper cache
                // ZipHelper.clearCache();
                fileStateOk = false;
            }
        }
    }
    return fileStateOk;
}
Also used : File(java.io.File) WildcardFileFilter(org.apache.commons.io.filefilter.WildcardFileFilter) LigerDownloadManager(scal.io.liger.LigerDownloadManager)

Aggregations

File (java.io.File)1 WildcardFileFilter (org.apache.commons.io.filefilter.WildcardFileFilter)1 LigerDownloadManager (scal.io.liger.LigerDownloadManager)1