Search in sources :

Example 1 with ContentPackMetadata

use of scal.io.liger.model.ContentPackMetadata in project storymaker by StoryMaker.

the class BaseHomeActivity method handleClick.

// HAD TO SPLIT OUT INTO A METHOD
public void handleClick(ExpansionIndexItem eItem, HashMap<String, ExpansionIndexItem> installedIds, boolean showDialog) {
    // initiate check/download whether installed or not
    // <- THIS SHOULD PICK UP EXISTING PARTIAL FILES
    HashMap<String, Thread> newThreads = StorymakerDownloadHelper.checkAndDownload(BaseHomeActivity.this, eItem, installedIndexItemDao, queueItemDao, true);
    // <- THIS ALSO NEEDS TO NOT INTERACT WITH THE INDEX
    // <- METADATA UPDATE SHOULD HAPPEN WHEN APP IS INITIALIZED
    // if any download threads were initiated, item is not ready to open
    boolean readyToOpen = true;
    if (newThreads.size() > 0) {
        readyToOpen = false;
        // update stored threads for index item
        ArrayList<Thread> currentThreads = downloadThreads.get(eItem.getExpansionId());
        if (currentThreads == null) {
            currentThreads = new ArrayList<Thread>();
        }
        for (Thread thread : newThreads.values()) {
            currentThreads.add(thread);
        }
        downloadThreads.put(eItem.getExpansionId(), currentThreads);
    }
    if (!installedIds.containsKey(eItem.getExpansionId())) {
        // if clicked item is not installed, update index
        // un-installed AvailableIndexItems need to be converted to InstalledIndexItems
        InstalledIndexItem iItem = new InstalledIndexItem(eItem);
        // FIXME we need to move this somewhere in the liger library that handles opening storypaths because this won't be triggered if we open open an instance by means other than a direct click
        java.util.Date thisDate = new java.util.Date();
        iItem.setCreationDate(thisDate);
        iItem.setLastModifiedDate(thisDate);
        iItem.setCreationDate(thisDate);
        StorymakerIndexManager.installedIndexAdd(BaseHomeActivity.this, iItem, installedIndexItemDao);
        Timber.d(eItem.getExpansionId() + " NOT INSTALLED, ADDING ITEM TO INDEX");
        // wait for index serialization
        try {
            synchronized (this) {
                // FIXME holy race conditions, batman
                wait(1000);
            }
        } catch (InterruptedException e) {
        // nop
        }
    } else {
        Timber.d(eItem.getExpansionId() + " INSTALLED, CHECKING FILE");
        // if clicked item is installed, check state
        if (readyToOpen) {
            // clear saved threads
            if (downloadThreads.get(eItem.getExpansionId()) != null) {
                downloadThreads.remove(eItem.getExpansionId());
            }
            // update db record with flag
            if (!eItem.isInstalled()) {
                Timber.d("SET INSTALLED FLAG FOR " + eItem.getExpansionId());
                eItem.setInstalledFlag(true);
                InstalledIndexItem iItem = new InstalledIndexItem(eItem);
                StorymakerIndexManager.installedIndexAdd(this, iItem, installedIndexItemDao);
            }
            // if file has been downloaded, open file
            Timber.d(eItem.getExpansionId() + " INSTALLED, FILE OK");
            // update with new thumbnail path
            // move this somewhere that it can be triggered by completed download?
            ContentPackMetadata metadata = scal.io.liger.IndexManager.loadContentMetadata(BaseHomeActivity.this, eItem.getPackageName(), eItem.getExpansionId(), StoryMakerApp.getCurrentLocale().getLanguage());
            if (metadata == null) {
                Toast.makeText(BaseHomeActivity.this, getString(R.string.home_metadata_missing), Toast.LENGTH_LONG).show();
                Timber.e("failed to load content metadata");
            } else if ((eItem.getThumbnailPath() == null) || (!eItem.getThumbnailPath().equals(metadata.getContentPackThumbnailPath()))) {
                // FIXME use StringUtils
                Timber.d(eItem.getExpansionId() + " FIRST OPEN, UPDATING THUMBNAIL PATH");
                eItem.setThumbnailPath(metadata.getContentPackThumbnailPath());
                // un-installed AvailableIndexItems need to be converted to InstalledIndexItems
                InstalledIndexItem iItem = new InstalledIndexItem(eItem);
                StorymakerIndexManager.installedIndexAdd(BaseHomeActivity.this, iItem, installedIndexItemDao);
                // wait for index serialization
                try {
                    synchronized (this) {
                        // FIXME holy race conditions, batman
                        wait(1000);
                    }
                } catch (InterruptedException e) {
                // nop
                }
            }
            ArrayList<scal.io.liger.model.InstanceIndexItem> contentIndex = scal.io.liger.IndexManager.loadContentIndexAsList(BaseHomeActivity.this, eItem.getPackageName(), eItem.getExpansionId(), StoryMakerApp.getCurrentLocale().getLanguage());
            if ((contentIndex == null) || (contentIndex.size() < 1)) {
                Toast.makeText(BaseHomeActivity.this, getString(R.string.home_index_missing), Toast.LENGTH_LONG).show();
                Timber.e("failed to load content index");
            } else if (contentIndex.size() == 1) {
                launchLiger(BaseHomeActivity.this, null, null, contentIndex.get(0).getInstanceFilePath());
            } else {
                String[] names = new String[contentIndex.size()];
                String[] paths = new String[contentIndex.size()];
                int i = 0;
                for (scal.io.liger.model.InstanceIndexItem item : contentIndex) {
                    names[i] = item.getTitle();
                    paths[i] = item.getInstanceFilePath();
                    i++;
                }
                showSPLSelectorPopup(names, paths);
            }
        } else {
            // if file is being downloaded, don't open
            Timber.d(eItem.getExpansionId() + " INSTALLED, CURRENTLY DOWNLOADING FILE");
            // if necessary, un-flag db record (this probably indicates an installed file that is being patched
            if (eItem.isInstalled()) {
                Timber.d("UN-SET INSTALLED FLAG FOR " + eItem.getExpansionId());
                eItem.setInstalledFlag(false);
                InstalledIndexItem iItem = new InstalledIndexItem(eItem);
                StorymakerIndexManager.installedIndexAdd(this, iItem, installedIndexItemDao);
            }
            if (showDialog) {
                new AlertDialog.Builder(BaseHomeActivity.this).setTitle(R.string.stop_download).setMessage(eItem.getTitle()).setNegativeButton(getString(R.string.cancel), null).setNeutralButton(getString(R.string.pause), new PauseListener(eItem)).setPositiveButton(getString(R.string.stop), new CancelListener(eItem)).show();
            }
        // Toast.makeText(HomeActivity.this, "Please wait for this content pack to finish downloading", Toast.LENGTH_LONG).show(); // FIXME move to strings.xml
        }
    }
}
Also used : InstalledIndexItem(scal.io.liger.model.sqlbrite.InstalledIndexItem) InstanceIndexItem(scal.io.liger.model.sqlbrite.InstanceIndexItem) ContentPackMetadata(scal.io.liger.model.ContentPackMetadata)

Aggregations

ContentPackMetadata (scal.io.liger.model.ContentPackMetadata)1 InstalledIndexItem (scal.io.liger.model.sqlbrite.InstalledIndexItem)1 InstanceIndexItem (scal.io.liger.model.sqlbrite.InstanceIndexItem)1