Search in sources :

Example 1 with SwitchStorageProviderResult

use of com.newsrob.jobs.SwitchStorageProviderResult in project newsrob by marianokamp.

the class EntryManager method switchStorageProvider.

private void switchStorageProvider() {
    Log.d(TAG, "Switch Storage Provider");
    if (isModelCurrentlyUpdated())
        return;
    final String newPrefValue = getSharedPreferences().getString(SETTINGS_STORAGE_PROVIDER_KEY, null);
    final String oldStorageProviderClass = fileContextAdapter.getClass().getName();
    final String newStorageProviderClass = STORAGE_PROVIDER_SD_CARD.equals(newPrefValue) ? SdCardStorageAdapter.class.getName() : PhoneMemoryStorageAdapter.class.getName();
    if (!oldStorageProviderClass.equals(newStorageProviderClass)) {
        runningThread = new Thread(new Runnable() {

            public void run() {
                final PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
                final PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
                Log.i(TAG, "Wake lock acquired at " + new Date().toString() + ".");
                wl.acquire();
                Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
                final Timing t = new Timing("Storage Provider Switch", ctx);
                ModelUpdateResult result = null;
                if (isModelCurrentlyUpdated())
                    return;
                try {
                    lockModel("EM.switchStorageProvider.run");
                } catch (final IllegalStateException ise) {
                    return;
                }
                try {
                    Log.i(TAG, "Switching storage providers started at " + new Date().toString() + ".");
                    fireModelUpdateStarted("Switching storage providers", false, true);
                    Log.d(TAG, "Change of storage provider detected.");
                    final List<Job> jobList = new LinkedList<Job>();
                    final Job clearOldStorageProvider = new Job("Clearing Old Storage Provider", EntryManager.this) {

                        @Override
                        public void run() {
                            Log.d(TAG, "Clearing the old storage provider.");
                            doClearCache();
                            if (fileContextAdapter.canWrite())
                                WebPageDownloadDirector.removeAllAssets(fileContextAdapter, ctx);
                        }
                    };
                    jobList.add(clearOldStorageProvider);
                    final Job switchStorageProviders = new Job("Switching Storage Providers", EntryManager.this) {

                        @Override
                        public void run() throws Exception {
                            Log.d(TAG, "Establishing new storage provider: " + newStorageProviderClass);
                            fileContextAdapter = newStorageProviderClass.equals(SdCardStorageAdapter.class.getName()) ? new SdCardStorageAdapter(ctx) : new PhoneMemoryStorageAdapter(ctx);
                            Log.d(TAG, "New storage provider established.");
                        }
                    };
                    jobList.add(switchStorageProviders);
                    final Job clearNewStorageProvider = new Job("Clearing New Storage Provider", EntryManager.this) {

                        @Override
                        public void run() {
                            Log.d(TAG, "Clearing the new storage provider.");
                            doClearCache();
                            if (fileContextAdapter.canWrite())
                                WebPageDownloadDirector.removeAllAssets(fileContextAdapter, ctx);
                        }
                    };
                    jobList.add(clearNewStorageProvider);
                    runJobs(jobList);
                    result = new SwitchStorageProviderResult();
                } catch (final Throwable throwable) {
                    result = new SwitchStorageProviderFailed(throwable);
                    Log.d(TAG, "Problem during switching storage providers.", throwable);
                    t.stop();
                } finally {
                    unlockModel("EM.switchStorageProvider.run");
                    clearCancelState();
                    fireModelUpdateFinished(result);
                    fireStatusUpdated();
                    Log.i(TAG, "Switching storage providers finished at " + new Date().toString() + ".");
                    wl.release();
                    t.stop();
                }
            }
        }, "Storage Provider Switch Worker");
        runningThread.start();
    }
}
Also used : ModelUpdateResult(com.newsrob.jobs.ModelUpdateResult) Date(java.util.Date) LinkedList(java.util.LinkedList) SdCardStorageAdapter(com.newsrob.storage.SdCardStorageAdapter) SwitchStorageProviderResult(com.newsrob.jobs.SwitchStorageProviderResult) PowerManager(android.os.PowerManager) SwitchStorageProviderFailed(com.newsrob.jobs.SwitchStorageProviderFailed) Timing(com.newsrob.util.Timing) Job(com.newsrob.jobs.Job) PhoneMemoryStorageAdapter(com.newsrob.storage.PhoneMemoryStorageAdapter)

Aggregations

PowerManager (android.os.PowerManager)1 Job (com.newsrob.jobs.Job)1 ModelUpdateResult (com.newsrob.jobs.ModelUpdateResult)1 SwitchStorageProviderFailed (com.newsrob.jobs.SwitchStorageProviderFailed)1 SwitchStorageProviderResult (com.newsrob.jobs.SwitchStorageProviderResult)1 PhoneMemoryStorageAdapter (com.newsrob.storage.PhoneMemoryStorageAdapter)1 SdCardStorageAdapter (com.newsrob.storage.SdCardStorageAdapter)1 Timing (com.newsrob.util.Timing)1 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1