GreenDaoDatabase.java 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package com.sunil.mvp.db;
  2. import android.content.Context;
  3. import android.support.annotation.NonNull;
  4. import com.sunil.mvp.db.model.DaoMaster;
  5. import com.sunil.mvp.db.model.DaoSession;
  6. import org.greenrobot.greendao.database.Database;
  7. /**
  8. * Created by sunil on 12-08-2017.
  9. */
  10. public class GreenDaoDatabase {
  11. private static DaoSession mDaoSession;
  12. public static final boolean ENCRYPTED = false;
  13. private GreenDaoDatabase() {
  14. }
  15. private static class SingletonHolder {
  16. private static final GreenDaoDatabase INSTANCE = new GreenDaoDatabase();
  17. }
  18. public static GreenDaoDatabase getInstance() {
  19. return SingletonHolder.INSTANCE;
  20. }
  21. public void init(Context context) {
  22. init(context, "db");
  23. }
  24. public void init(@NonNull Context context, @NonNull String dbName) {
  25. DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context.getApplicationContext(), ENCRYPTED ? "notes-db-encrypted" : "articles-db");
  26. Database db = ENCRYPTED ? helper.getEncryptedWritableDb("super-secret") : helper.getWritableDb();
  27. mDaoSession = new DaoMaster(db).newSession();
  28. }
  29. public DaoSession getDaoSession() {
  30. if (null == mDaoSession) {
  31. throw new NullPointerException("green db has not been initialized");
  32. }
  33. return mDaoSession;
  34. }
  35. }