Search in sources :

Example 1 with WildcardFileFilter

use of org.apache.commons.io.filefilter.WildcardFileFilter in project storymaker by StoryMaker.

the class StorymakerDownloadHelper method checkAndDownload.

public static HashMap<String, Thread> checkAndDownload(Context context, ExpansionIndexItem installedItem, InstalledIndexItemDao installedDao, QueueItemDao queueDao, boolean confirmDownload) {
    boolean mainFileOk = true;
    boolean patchFileOk = true;
    boolean fileStateOk = true;
    String filePath = StorymakerIndexManager.buildFilePath(installedItem, context);
    String fileName = StorymakerIndexManager.buildFileName(installedItem, scal.io.liger.Constants.MAIN);
    HashMap<String, Thread> downloadThreads = new HashMap<String, Thread>();
    File expansionFile = new File(filePath + fileName);
    if (expansionFile.exists()) {
        if (expansionFile.length() == 0) {
            Timber.d("CONTENT PACK FILE " + fileName + " IS A ZERO BYTE FILE ");
            mainFileOk = false;
        }
        if ((installedItem.getExpansionFileSize() > 0) && (installedItem.getExpansionFileSize() > expansionFile.length())) {
            Timber.d("CONTENT PACK FILE " + fileName + " IS TOO SMALL (" + expansionFile.length() + "/" + installedItem.getExpansionFileSize() + ")");
            mainFileOk = false;
        }
    // NOTE: unsure what to do in this state.  incomplete downloads should be .tmp or .part,
    //       so this is probably a broken file that should be deleted and redownloaded
    } else {
        // file does not exist, flag for downloading
        // (download process will handle .tmp and .part files)
        Timber.d("CONTENT PACK FILE " + fileName + " DOES NOT EXIST ");
        mainFileOk = false;
    }
    if (mainFileOk) {
        Timber.d("CONTENT PACK FILE " + fileName + " CHECKS OUT, NO DOWNLOAD");
    } else {
        Timber.d("CONTENT PACK FILE " + fileName + " MUST BE DOWNLOADED");
        final StorymakerDownloadManager mainDownload = new StorymakerDownloadManager(fileName, installedItem, context, installedDao, queueDao, confirmDownload);
        Thread mainDownloadThread = new Thread(mainDownload);
        // Toast.makeText(context, "Starting download of " + installedItem.getExpansionId() + " content pack.", Toast.LENGTH_LONG).show(); // FIXME move to strings
        // expansionDownloadThread.start();
        downloadThreads.put(scal.io.liger.Constants.MAIN, mainDownloadThread);
        // downloading a new content pack 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 (installedItem.getPatchFileVersion() != null) {
        if ((installedItem.getExpansionFileVersion() != null) && (Integer.parseInt(installedItem.getPatchFileVersion()) < Integer.parseInt(installedItem.getExpansionFileVersion()))) {
            // FIXME getFileFolderName can return null if the media is in some sort of disconnected state. in that case we need to revert to internal
            File obbDirectory = new File(ZipHelper.getObbFolderName(context));
            File fileDirectory = new File(ZipHelper.getFileFolderName(context));
            String nameFilter = installedItem.getExpansionId() + "." + scal.io.liger.Constants.PATCH + "*" + ".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 = StorymakerIndexManager.buildFileName(installedItem, scal.io.liger.Constants.PATCH);
            expansionFile = new File(filePath + patchName);
            if (expansionFile.exists()) {
                if (expansionFile.length() == 0) {
                    Timber.d("CONTENT PACK PATCH " + patchName + " IS A ZERO BYTE FILE ");
                    patchFileOk = false;
                }
                if ((installedItem.getPatchFileSize() > 0) && (installedItem.getPatchFileSize() > expansionFile.length())) {
                    Timber.d("CONTENT PACK PATCH " + patchName + " IS TOO SMALL (" + expansionFile.length() + "/" + installedItem.getPatchFileSize() + ")");
                    patchFileOk = false;
                }
            // NOTE: unsure what to do in this state.  incomplete downloads should be .tmp or .part,
            //       so this is probably a broken file that should be deleted and redownloaded
            } else {
                // file does not exist, flag for downloading
                // (download process will handle .tmp and .part files)
                Timber.d("CONTENT PACK PATCH " + patchName + " DOES NOT EXIST ");
                patchFileOk = false;
            }
            if (patchFileOk) {
                Timber.d("CONTENT PACK PATCH " + patchName + " CHECKS OUT, NO DOWNLOAD");
            } else {
                Timber.d("CONTENT PACK PATCH " + patchName + " MUST BE DOWNLOADED");
                final StorymakerDownloadManager patchDownload = new StorymakerDownloadManager(patchName, installedItem, context, installedDao, queueDao, confirmDownload);
                Thread patchDownloadThread = new Thread(patchDownload);
                // Toast.makeText(context, "Starting download of expansion file patch.", Toast.LENGTH_LONG).show(); // FIXME move to strings
                // expansionDownloadThread.start();
                downloadThreads.put(scal.io.liger.Constants.PATCH, patchDownloadThread);
                // downloading a new content pack patch, must clear ZipHelper cache
                // ZipHelper.clearCache();
                fileStateOk = false;
            }
        }
    }
    // this may be confusing as we are starting threads that may not result in downloads
    boolean downloadStarted = false;
    Thread mainThread = downloadThreads.get(scal.io.liger.Constants.MAIN);
    if (mainThread != null) {
        Timber.d("STARTING THREAD " + mainThread.getId());
        mainThread.start();
        downloadStarted = true;
    }
    Thread patchThread = downloadThreads.get(scal.io.liger.Constants.PATCH);
    if (patchThread != null) {
        Timber.d("STARTING THREAD " + patchThread.getId());
        patchThread.start();
        downloadStarted = true;
    }
    if (downloadStarted) {
    // ZipHelper.clearCache();
    }
    return downloadThreads;
}
Also used : HashMap(java.util.HashMap) File(java.io.File) WildcardFileFilter(org.apache.commons.io.filefilter.WildcardFileFilter)

Example 2 with WildcardFileFilter

use of org.apache.commons.io.filefilter.WildcardFileFilter 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)

Example 3 with WildcardFileFilter

use of org.apache.commons.io.filefilter.WildcardFileFilter in project storymaker by StoryMaker.

the class StorymakerDownloadManager method downloadFromLigerServer.

private void downloadFromLigerServer() {
    String ligerUrl = indexItem.getExpansionFileUrl();
    String ligerPath = StorymakerIndexManager.buildFilePath(indexItem, context);
    String ligerObb = fileName;
    Timber.d("DOWNLOADING " + ligerObb + " FROM " + ligerUrl + " TO " + ligerPath);
    try {
        URI expansionFileUri = null;
        HttpGet request = null;
        HttpResponse response = null;
        File targetFolder = new File(ligerPath);
        String nameFilter = "";
        if (ligerObb.contains(indexItem.getExpansionFileVersion())) {
            nameFilter = fileName.replace(indexItem.getExpansionFileVersion(), "*") + "*.tmp";
        } else {
            nameFilter = fileName + "*.tmp";
        }
        Timber.d("CLEANUP: DELETING " + nameFilter + " FROM " + targetFolder.getPath());
        WildcardFileFilter oldFileFilter = new WildcardFileFilter(nameFilter);
        for (File oldFile : FileUtils.listFiles(targetFolder, oldFileFilter, null)) {
            Timber.d("CLEANUP: FOUND " + oldFile.getPath() + ", DELETING");
            FileUtils.deleteQuietly(oldFile);
        }
        // additional cleanup of pre-name-change files
        if (fileName.contains(scal.io.liger.Constants.MAIN)) {
            nameFilter = fileName.replace(scal.io.liger.Constants.MAIN + ".", "").replace(indexItem.getExpansionFileVersion(), "*");
            Timber.d("CLEANUP: DELETING OLD FILES " + nameFilter + " FROM " + targetFolder.getPath());
            oldFileFilter = new WildcardFileFilter(nameFilter);
            for (File oldFile : FileUtils.listFiles(targetFolder, oldFileFilter, null)) {
                Timber.d("CLEANUP: FOUND OLD FILE " + oldFile.getPath() + ", DELETING");
                FileUtils.deleteQuietly(oldFile);
            }
        }
        File targetFile = new File(targetFolder, ligerObb + ".tmp");
        // if there is no connectivity, do not queue item (no longer seems to pause if connection is unavailable)
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if ((ni != null) && (ni.isConnectedOrConnecting())) {
            if (context instanceof Activity) {
                // FIXME move to strings
                Utils.toastOnUiThread((Activity) context, "Starting download of " + indexItem.getTitle() + ".", false);
            }
            // check preferences.  will also need to check whether tor is active within method
            SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
            boolean useTor = settings.getBoolean("pusetor", false);
            boolean useManager = settings.getBoolean("pusedownloadmanager", false);
            //if (checkTor(useTor, context)) {
            if (useTor && useManager) {
                Timber.e("ANDROID DOWNLOAD MANAGER IS NOT COMPATABLE WITH TOR");
                if (context instanceof Activity) {
                    // FIXME move to strings
                    Utils.toastOnUiThread((Activity) context, "Check settings, can't use download manager and tor", true);
                }
                StorymakerQueueManager.checkQueueFinished(context, targetFile.getName());
            } else if (useTor || !useManager) {
                downloadWithTor(useTor, Uri.parse(ligerUrl + ligerObb), mAppTitle + " content download", ligerObb, targetFile);
            } else {
                downloadWithManager(Uri.parse(ligerUrl + ligerObb), mAppTitle + " content download", ligerObb, Uri.fromFile(targetFile));
            }
        } else {
            Timber.d("NO CONNECTION, NOT QUEUEING DOWNLOAD: " + ligerUrl + ligerObb + " -> " + targetFile.getPath());
            if (context instanceof Activity) {
                // FIXME move to strings
                Utils.toastOnUiThread((Activity) context, "Check settings, no connection, can't start download", true);
            }
            StorymakerQueueManager.checkQueueFinished(context, targetFile.getName());
        }
    } catch (Exception e) {
        Timber.e(e, "DOWNLOAD ERROR: " + ligerUrl + ligerObb + " -> " + e.getMessage());
    }
}
Also used : NetworkInfo(android.net.NetworkInfo) SharedPreferences(android.content.SharedPreferences) ConnectivityManager(android.net.ConnectivityManager) HttpGet(ch.boye.httpclientandroidlib.client.methods.HttpGet) HttpResponse(ch.boye.httpclientandroidlib.HttpResponse) Activity(android.app.Activity) URI(java.net.URI) File(java.io.File) WildcardFileFilter(org.apache.commons.io.filefilter.WildcardFileFilter) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) ConnectTimeoutException(ch.boye.httpclientandroidlib.conn.ConnectTimeoutException)

Example 4 with WildcardFileFilter

use of org.apache.commons.io.filefilter.WildcardFileFilter in project storymaker by StoryMaker.

the class CompleteTest method cleanup.

private void cleanup(String directory) {
    WildcardFileFilter oldFileFilter = new WildcardFileFilter("*instance*");
    for (File oldFile : FileUtils.listFiles(new File(directory), oldFileFilter, null)) {
        Timber.d("CLEANUP: FOUND " + oldFile.getPath() + ", DELETING");
        FileUtils.deleteQuietly(oldFile);
    }
}
Also used : WildcardFileFilter(org.apache.commons.io.filefilter.WildcardFileFilter) File(java.io.File)

Example 5 with WildcardFileFilter

use of org.apache.commons.io.filefilter.WildcardFileFilter in project storymaker by StoryMaker.

the class DefaultLibraryTest method cleanup.

private void cleanup(String directory) {
    WildcardFileFilter oldFileFilter = new WildcardFileFilter("*instance*");
    for (File oldFile : FileUtils.listFiles(new File(directory), oldFileFilter, null)) {
        Timber.d("CLEANUP: FOUND " + oldFile.getPath() + ", DELETING");
        FileUtils.deleteQuietly(oldFile);
    }
}
Also used : WildcardFileFilter(org.apache.commons.io.filefilter.WildcardFileFilter) File(java.io.File)

Aggregations

WildcardFileFilter (org.apache.commons.io.filefilter.WildcardFileFilter)22 File (java.io.File)21 FileFilter (java.io.FileFilter)6 IOException (java.io.IOException)4 HashMap (java.util.HashMap)3 Activity (android.app.Activity)2 SystemEnvironment (com.thoughtworks.go.util.SystemEnvironment)2 AndFileFilter (org.apache.commons.io.filefilter.AndFileFilter)2 SharedPreferences (android.content.SharedPreferences)1 ConnectivityManager (android.net.ConnectivityManager)1 NetworkInfo (android.net.NetworkInfo)1 HttpResponse (ch.boye.httpclientandroidlib.HttpResponse)1 HttpGet (ch.boye.httpclientandroidlib.client.methods.HttpGet)1 ConnectTimeoutException (ch.boye.httpclientandroidlib.conn.ConnectTimeoutException)1 ButterflyModule (edu.mit.simile.butterfly.ButterflyModule)1 FileIteratingFirehose (io.druid.data.input.impl.FileIteratingFirehose)1 IAE (io.druid.java.util.common.IAE)1 ISE (io.druid.java.util.common.ISE)1 SocketTimeoutException (java.net.SocketTimeoutException)1 URI (java.net.URI)1