Search in sources :

Example 1 with AvailableIndexItem

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

the class BaseHomeActivity method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // copy index file
    // TODO: REPLACE THIS WITH INDEX DOWNLOAD (IF LOGGED IN) <- NEED TO COPY FILE FOR BASELINE CONTENT
    StorymakerIndexManager.copyAvailableIndex(this, false);
    // initialize db
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    // version check (sqlite upgrade requires migration)
    int appMigrationVersion = preferences.getInt("APP_MIGRATION_VERSION", 0);
    Timber.d("MIGRATION CHECK: " + appMigrationVersion + " vs. " + Constants.APP_MIGRATION_VERSION);
    if (appMigrationVersion != Constants.APP_MIGRATION_VERSION) {
        Timber.d("MIGRATION REQUIRED, RE-ENCRYPTING DATABASE");
        final boolean[] dbStatus = { false };
        try {
            SQLiteDatabaseHook dbHook = new SQLiteDatabaseHook() {

                public void preKey(SQLiteDatabase database) {
                }

                public void postKey(SQLiteDatabase database) {
                    Cursor cursor = database.rawQuery("PRAGMA cipher_migrate", new String[] {});
                    String value = "";
                    if (cursor != null) {
                        cursor.moveToFirst();
                        value = cursor.getString(0);
                        cursor.close();
                    }
                    // this result is currently ignored, checking if db is null instead
                    dbStatus[0] = Integer.valueOf(value) == 0;
                }
            };
            File dbPath = getDatabasePath("sm.db");
            Timber.d("MIGRATING DATABASE AT " + dbPath.getPath());
            SQLiteDatabase sqldb = SQLiteDatabase.openOrCreateDatabase(dbPath, "foo", null, dbHook);
            if (sqldb != null) {
                Timber.d("MIGRATED DATABASE NOT NULL");
                sqldb.close();
                // update preferences if migration succeeded
                preferences.edit().putInt("APP_MIGRATION_VERSION", Constants.APP_MIGRATION_VERSION).commit();
            } else {
                Timber.e("MIGRATED DATABASE IS NULL");
            }
        } catch (Exception ex) {
            Timber.e("EXCEPTION WHILE MIGRATING DATABASE: " + ex.getMessage());
        }
    }
    int availableIndexVersion = preferences.getInt("AVAILABLE_INDEX_VERSION", 0);
    Timber.d("VERSION CHECK: " + availableIndexVersion + " vs. " + scal.io.liger.Constants.AVAILABLE_INDEX_VERSION);
    if (availableIndexVersion != scal.io.liger.Constants.AVAILABLE_INDEX_VERSION) {
        // load db from file
        HashMap<String, scal.io.liger.model.ExpansionIndexItem> availableItemsFromFile = scal.io.liger.IndexManager.loadAvailableIdIndex(this);
        if (availableItemsFromFile.size() == 0) {
            Timber.d("NOTHING LOADED FROM AVAILABLE FILE");
        } else {
            for (scal.io.liger.model.ExpansionIndexItem item : availableItemsFromFile.values()) {
                Timber.d("ADDING " + item.getExpansionId() + " TO DATABASE (AVAILABLE)");
                // replaces existing items, should trigger updates to installed items and table as needed
                availableIndexItemDao.addAvailableIndexItem(item, true);
                // ugly solution to deal with the fact that the popup menu assumes there will be threads for an item we tried to download/install
                ArrayList<Thread> noThreads = new ArrayList<Thread>();
                downloadThreads.put(item.getExpansionId(), noThreads);
            }
        }
        // the following migration stuff is currently piggy-backing on the index update stuff
        // if found, migrate installed index
        File installedFile = new File(StorageHelper.getActualStorageDirectory(this), "installed_index.json");
        if (installedFile.exists()) {
            HashMap<String, scal.io.liger.model.ExpansionIndexItem> installedItemsFromFile = scal.io.liger.IndexManager.loadInstalledIdIndex(this);
            if (installedItemsFromFile.size() == 0) {
                Timber.d("NOTHING LOADED FROM INSTALLED INDEX FILE");
            } else {
                for (scal.io.liger.model.ExpansionIndexItem item : installedItemsFromFile.values()) {
                    Timber.d("ADDING " + item.getExpansionId() + " TO DATABASE (INSTALLED)");
                    // replaces existing items, should trigger updates to installed items and table as needed
                    installedIndexItemDao.addInstalledIndexItem(item, true);
                }
            }
            installedFile.delete();
        } else {
            Timber.d("NO INSTALLED INDEX FILE");
        }
        // if found, migrate instance index
        File instanceFile = new File(StorageHelper.getActualStorageDirectory(this), "instance_index.json");
        if (instanceFile.exists()) {
            HashMap<String, scal.io.liger.model.InstanceIndexItem> instanceItemsFromFile = scal.io.liger.IndexManager.loadInstanceIndex(this);
            if (instanceItemsFromFile.size() == 0) {
                Timber.d("NOTHING LOADED FROM INSTANCE INDEX FILE");
            } else {
                for (scal.io.liger.model.InstanceIndexItem item : instanceItemsFromFile.values()) {
                    Timber.d("ADDING " + item.getInstanceFilePath() + " TO DATABASE (INSTANCE)");
                    // replaces existing items, should trigger updates to installed items and table as needed
                    instanceIndexItemDao.addInstanceIndexItem(item, true);
                }
            }
            instanceFile.delete();
        } else {
            Timber.d("NO INSTANCE INDEX FILE");
        }
        // update preferences
        preferences.edit().putInt("AVAILABLE_INDEX_VERSION", scal.io.liger.Constants.AVAILABLE_INDEX_VERSION).commit();
        if (getIntent() != null && getIntent().hasExtra("showlauncher")) {
            if (getIntent().getBooleanExtra("showlauncher", false)) {
                showLauncherIcon();
            }
        }
    }
    // dumb test
    // check values
    availableIndexItemDao.getAvailableIndexItems().take(1).subscribe(new Action1<List<AvailableIndexItem>>() {

        @Override
        public void call(List<AvailableIndexItem> expansionIndexItems) {
            for (ExpansionIndexItem item : expansionIndexItems) {
                Timber.d("AVAILABLE ITEM " + item.getExpansionId() + ", TITLE: " + item.getTitle());
            }
        }
    });
    installedIndexItemDao.getInstalledIndexItems().take(1).subscribe(new Action1<List<InstalledIndexItem>>() {

        @Override
        public void call(List<InstalledIndexItem> expansionIndexItems) {
            for (ExpansionIndexItem item : expansionIndexItems) {
                Timber.d("INSTALLED ITEM " + item.getExpansionId() + ", TITLE: " + item.getTitle());
            }
        }
    });
    // file cleanup
    File actualStorageDirectory = StorageHelper.getActualStorageDirectory(this);
    if (actualStorageDirectory != null) {
        JsonHelper.cleanup(actualStorageDirectory.getPath());
    } else {
    // this is an error, will deal with it below
    }
    // default
    loggedIn = false;
    if (actualStorageDirectory != null) {
        // NEW/TEMP
        // DOWNLOAD AVAILABE INDEX FOR CURRENT USER AND SAVE TO TARGET FILE
        // NEED TO ACCOUNT FOR POSSIBLE MISSING INDEX
        // force download at startup (maybe only force on a timetable?)
        IndexTask iTask = new IndexTask(this, true);
        iTask.execute();
    } else {
        //show storage error message
        new AlertDialog.Builder(this).setTitle(Utils.getAppName(this)).setIcon(android.R.drawable.ic_dialog_info).setMessage(R.string.err_storage_not_available).show();
    }
    // we want to grab required updates without restarting the app
    // integrate with index task
    // if (!DownloadHelper.checkAndDownload(this)) {
    //     Toast.makeText(this, "Downloading content and/or updating installed files", Toast.LENGTH_LONG).show(); // FIXME move to strings.xml
    // }
    // i don't think we ever want to do this
    // IndexManager.copyInstalledIndex(this);
    //setContentView(R.layout.activity_home);
    //mRecyclerView = (RecyclerView) findViewById(scal.io.liger.R.id.recyclerView);
    //mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    //mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
    //mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    //    @Override
    //    public void onRefresh() {
    //        IndexTask iTask = new IndexTask(HomeActivity.this, true); // force download on manual refresh
    //        iTask.execute();
    //    }
    //});
    //mTabMenu = getMenu("home");
    // action bar stuff
    getActionBar().setDisplayHomeAsUpEnabled(true);
    checkForTor();
    checkForUpdates();
}
Also used : ArrayList(java.util.ArrayList) ExpansionIndexItem(scal.io.liger.model.sqlbrite.ExpansionIndexItem) InstalledIndexItem(scal.io.liger.model.sqlbrite.InstalledIndexItem) Cursor(net.sqlcipher.Cursor) InstanceIndexItem(scal.io.liger.model.sqlbrite.InstanceIndexItem) AvailableIndexItem(scal.io.liger.model.sqlbrite.AvailableIndexItem) List(java.util.List) ArrayList(java.util.ArrayList) SharedPreferences(android.content.SharedPreferences) IOException(java.io.IOException) SQLiteDatabase(net.sqlcipher.database.SQLiteDatabase) File(java.io.File) SQLiteDatabaseHook(net.sqlcipher.database.SQLiteDatabaseHook)

Aggregations

SharedPreferences (android.content.SharedPreferences)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Cursor (net.sqlcipher.Cursor)1 SQLiteDatabase (net.sqlcipher.database.SQLiteDatabase)1 SQLiteDatabaseHook (net.sqlcipher.database.SQLiteDatabaseHook)1 AvailableIndexItem (scal.io.liger.model.sqlbrite.AvailableIndexItem)1 ExpansionIndexItem (scal.io.liger.model.sqlbrite.ExpansionIndexItem)1 InstalledIndexItem (scal.io.liger.model.sqlbrite.InstalledIndexItem)1 InstanceIndexItem (scal.io.liger.model.sqlbrite.InstanceIndexItem)1