Android Open Source - notes Storage






From Project

Back to project page notes.

License

The source code is released under:

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004 Copyright (C) 2004 Sam Hocevar <sam@hocevar.net> Everyone is permitted to copy and distribute verbatim or...

If you think the Android project notes listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.iliakplv.notes.notes.storage;
/* w w  w. j av  a  2 s. c om*/
import android.content.Context;
import android.content.SharedPreferences;

import com.iliakplv.notes.NotesApplication;
import com.iliakplv.notes.notes.db.NotesDatabaseStorage;
import com.iliakplv.notes.notes.dropbox.NotesDropboxStorage;
import com.iliakplv.notes.utils.AppLog;

public final class Storage {

  private static final String TAG = Storage.class.getSimpleName();

  private static final StorageWrapper storageWrapper = new StorageWrapper();

  private static final String STORAGE_TYPE = "storage_type";
  public static final Type DEFAULT_STORAGE = Type.Database;
  private static Type currentStorageType = null;
  private static volatile boolean initialized = false;


  public static NotesStorage getStorage() {
    checkInit();
    return storageWrapper;
  }

  private static void checkInit() {
    if (!initialized) {
      throw new IllegalStateException("Storage must be initialized before usage!");
    }
  }

  public static Type getCurrentStorageType() {
    return currentStorageType;
  }

  /**
   * Storage initialization
   * @param newStorageType new storage type or null for last used or default storage
   */
  public static void init(Type newStorageType) {
    AppLog.d(TAG, "init() call. Initialized: " + initialized +
        " Current storage: " + String.valueOf(currentStorageType) +
        " New storage: " + String.valueOf(newStorageType));

    if (initialized && currentStorageType == newStorageType) {
      return;
    }

    final Context context = NotesApplication.getContext();
    final boolean lastUsedOrDefault = newStorageType == null;
    if (lastUsedOrDefault) {
      final SharedPreferences prefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE);
      newStorageType = Type.valueOf(prefs.getString(STORAGE_TYPE, DEFAULT_STORAGE.toString()));
    }

    switch (newStorageType) {
      case Database:
        storageWrapper.setTarget(new NotesDatabaseStorage());
        break;

      case Dropbox:
        storageWrapper.setTarget(new NotesDropboxStorage());
        break;

      default:
        throw new IllegalArgumentException("Unknown storage type: "
            + newStorageType.toString());
    }

    final SharedPreferences.Editor prefsEditor =
        context.getSharedPreferences(TAG, Context.MODE_PRIVATE).edit();
    prefsEditor.putString(STORAGE_TYPE, newStorageType.toString());
    prefsEditor.commit();

    currentStorageType = newStorageType;
    initialized = true;
  }


  /**
   * ******************************************
   *
   * Inner classes
   *
   * *******************************************
   */


  public static enum Type {
    Database,
    Dropbox
  }
}




Java Source Code List

com.iliakplv.notes.NotesApplication.java
com.iliakplv.notes.analytics.EventTracker.java
com.iliakplv.notes.analytics.Event.java
com.iliakplv.notes.gui.main.MainActivityTest.java
com.iliakplv.notes.gui.main.MainActivity.java
com.iliakplv.notes.gui.main.NavigationDrawerFragment.java
com.iliakplv.notes.gui.main.NoteDetailsFragment.java
com.iliakplv.notes.gui.main.NotesListFragment.java
com.iliakplv.notes.gui.main.dialogs.AboutDialog.java
com.iliakplv.notes.gui.main.dialogs.AbstractItemDialog.java
com.iliakplv.notes.gui.main.dialogs.DropboxAccountLinkingDialog.java
com.iliakplv.notes.gui.main.dialogs.LabelEditDialog.java
com.iliakplv.notes.gui.main.dialogs.NoteLabelsDialog.java
com.iliakplv.notes.gui.main.dialogs.SimpleItemDialog.java
com.iliakplv.notes.gui.main.dialogs.VoiceSearchInstallDialog.java
com.iliakplv.notes.gui.settings.SettingsActivity.java
com.iliakplv.notes.notes.AbstractNote.java
com.iliakplv.notes.notes.LabelComparator.java
com.iliakplv.notes.notes.Label.java
com.iliakplv.notes.notes.NoteComparator.java
com.iliakplv.notes.notes.NotesUtils.java
com.iliakplv.notes.notes.TextNote.java
com.iliakplv.notes.notes.db.NotesDatabaseAdapter.java
com.iliakplv.notes.notes.db.NotesDatabaseOpenHelper.java
com.iliakplv.notes.notes.db.NotesDatabaseStorage.java
com.iliakplv.notes.notes.dropbox.DropboxHelper.java
com.iliakplv.notes.notes.dropbox.NotesDropboxStorage.java
com.iliakplv.notes.notes.storage.NotesStorageListener.java
com.iliakplv.notes.notes.storage.NotesStorage.java
com.iliakplv.notes.notes.storage.StorageDataTransfer.java
com.iliakplv.notes.notes.storage.StorageWrapper.java
com.iliakplv.notes.notes.storage.Storage.java
com.iliakplv.notes.storage.StorageTest.java
com.iliakplv.notes.utils.AppLog.java
com.iliakplv.notes.utils.ConnectivityUtils.java
com.iliakplv.notes.utils.StringUtils.java
com.iliakplv.notes.utils.Utils.java