From 19d9924baca60e8145eb58281f3f9de3eb8d166c Mon Sep 17 00:00:00 2001 From: henry Date: Thu, 7 Sep 2017 20:31:38 -0400 Subject: [PATCH] Room Database implementation --- app/build.gradle | 10 +++- .../agorise/crystalwallet/IntroActivity.java | 15 +++++ .../application/CrystalApplication.java | 20 +++++++ .../crystalwallet/dao/CrystalDatabase.java | 29 ++++++++++ .../crystalwallet/dao/DatabaseConnection.java | 27 +++++++++ .../crystalwallet/models/AccountSeed.java | 46 +++++++++++++++ .../models/CryptoNetAccount.java | 58 +++++++++++++++++++ app/src/main/res/layout/activity_intro.xml | 17 ++++++ build.gradle | 4 ++ 9 files changed, 223 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/cy/agorise/crystalwallet/IntroActivity.java create mode 100644 app/src/main/java/cy/agorise/crystalwallet/application/CrystalApplication.java create mode 100644 app/src/main/java/cy/agorise/crystalwallet/dao/CrystalDatabase.java create mode 100644 app/src/main/java/cy/agorise/crystalwallet/dao/DatabaseConnection.java create mode 100644 app/src/main/java/cy/agorise/crystalwallet/models/AccountSeed.java create mode 100644 app/src/main/java/cy/agorise/crystalwallet/models/CryptoNetAccount.java create mode 100644 app/src/main/res/layout/activity_intro.xml diff --git a/app/build.gradle b/app/build.gradle index c6df5f7..89e1efc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,10 +2,10 @@ apply plugin: 'com.android.application' android { compileSdkVersion 26 - buildToolsVersion "25.0.3" + buildToolsVersion "26.0.1" defaultConfig { - applicationId "carbon.crypto.com.carbon" - minSdkVersion 21 + applicationId "cy.agorise.crystalwallet" + minSdkVersion 19 targetSdkVersion 26 versionCode 1 versionName "1.0" @@ -27,4 +27,8 @@ dependencies { compile 'com.android.support:appcompat-v7:26.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' + + + compile 'android.arch.persistence.room:runtime:1.0.0-alpha9'; + } diff --git a/app/src/main/java/cy/agorise/crystalwallet/IntroActivity.java b/app/src/main/java/cy/agorise/crystalwallet/IntroActivity.java new file mode 100644 index 0000000..41e0d7e --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/IntroActivity.java @@ -0,0 +1,15 @@ +package cy.agorise.crystalwallet; + +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; + +import carbon.crypto.com.carbon.R; + +public class IntroActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_intro); + } +} diff --git a/app/src/main/java/cy/agorise/crystalwallet/application/CrystalApplication.java b/app/src/main/java/cy/agorise/crystalwallet/application/CrystalApplication.java new file mode 100644 index 0000000..2d975f0 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/application/CrystalApplication.java @@ -0,0 +1,20 @@ +package cy.agorise.crystalwallet.application; + +import android.app.Application; + +import cy.agorise.crystalwallet.dao.DatabaseConnection; + +/** + * Created by Henry Varona on 6/9/2017. + */ + +public class CrystalApplication extends Application { + + @Override + public void onCreate() { + super.onCreate(); + + //initialize the database + DatabaseConnection.getConnection(this.getApplicationContext()); + } +} diff --git a/app/src/main/java/cy/agorise/crystalwallet/dao/CrystalDatabase.java b/app/src/main/java/cy/agorise/crystalwallet/dao/CrystalDatabase.java new file mode 100644 index 0000000..31b7445 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/CrystalDatabase.java @@ -0,0 +1,29 @@ +package cy.agorise.crystalwallet.dao; + +import android.arch.persistence.db.SupportSQLiteDatabase; +import android.arch.persistence.room.*; +import android.arch.persistence.room.migration.Migration; + +import cy.agorise.crystalwallet.models.AccountSeed; +import cy.agorise.crystalwallet.models.CryptoNetAccount; + +/** + * Created by Henry Varona on 4/9/2017. + */ + +@Database(entities = {AccountSeed.class, CryptoNetAccount.class}, version = 2) +public abstract class CrystalDatabase extends RoomDatabase { + + + static final Migration MIGRATION_1_2 = new Migration(1, 2) { + @Override + public void migrate(SupportSQLiteDatabase database) { + database.execSQL("CREATE TABLE 'account_seed' ('id' INTEGER PRIMARY KEY AUTOINCREMENT, " + + "'name' TEXT, 'master_seed' NUMERIC)"); + database.execSQL("CREATE TABLE 'crypto_net_account' ('id' INTEGER PRIMARY KEY AUTOINCREMENT, " + + "'seed_id', " + + "'account_number' INT, 'account_index' INT," + + "FOREIGN_KEY(seed_id) REFERENCES seed(id))"); + } + }; +} diff --git a/app/src/main/java/cy/agorise/crystalwallet/dao/DatabaseConnection.java b/app/src/main/java/cy/agorise/crystalwallet/dao/DatabaseConnection.java new file mode 100644 index 0000000..2d71438 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/DatabaseConnection.java @@ -0,0 +1,27 @@ +package cy.agorise.crystalwallet.dao; + +import android.arch.persistence.room.Room; +import android.content.Context; + +/** + * Created by Henry Varona on 6/9/2017. + */ + +public class DatabaseConnection { + private static final DatabaseConnection instance = new DatabaseConnection(); + private CrystalDatabase db; + + private DatabaseConnection(){ + } + + public static CrystalDatabase getConnection(Context context){ + if (instance.db == null){ + instance.db = Room.databaseBuilder(context, + CrystalDatabase.class, "CrystalWallet.db") + .addMigrations(CrystalDatabase.MIGRATION_1_2) + .build(); + } + + return instance.db; + } +} diff --git a/app/src/main/java/cy/agorise/crystalwallet/models/AccountSeed.java b/app/src/main/java/cy/agorise/crystalwallet/models/AccountSeed.java new file mode 100644 index 0000000..055195e --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/models/AccountSeed.java @@ -0,0 +1,46 @@ +package cy.agorise.crystalwallet.models; + +import android.arch.persistence.room.*; + +/** + * Created by Henry Varona on 6/9/2017. + */ +@Entity +public class AccountSeed { + + @PrimaryKey(autoGenerate = true) + @ColumnInfo(name = "id") + private int mId; + + @ColumnInfo(name = "name") + private String mName; + + @ColumnInfo(name = "master_seed") + private String mMasterSeed; + + public int getId() { + return mId; + } + + public void setId(int id){ + this.mId = id; + } + + public String getName() { + return mName; + } + + public void setName(String mName) { + this.mName = mName; + } + + public String getMasterSeed() { + return mMasterSeed; + } + + public void setMasterSeed(String mMasterSeed) { + this.mMasterSeed = mMasterSeed; + } + + +} diff --git a/app/src/main/java/cy/agorise/crystalwallet/models/CryptoNetAccount.java b/app/src/main/java/cy/agorise/crystalwallet/models/CryptoNetAccount.java new file mode 100644 index 0000000..58b1b62 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/models/CryptoNetAccount.java @@ -0,0 +1,58 @@ +package cy.agorise.crystalwallet.models; + +import android.arch.persistence.room.*; + +/** + * Created by Henry Varona on 6/9/2017. + */ + +@Entity (foreignKeys = @ForeignKey(entity = AccountSeed.class, + parentColumns = "id", + childColumns = "seed_id")) +public class CryptoNetAccount { + + @PrimaryKey(autoGenerate = true) + @ColumnInfo(name = "id") + private int mId; + + @ColumnInfo(name = "seed_id") + private int mSeedId; + + @ColumnInfo(name = "account_number") + private int mAccountNumber; + + @ColumnInfo(name = "account_index") + private int mAccountIndex; + + public int getId() { + return mId; + } + + public void setId(int id){ + this.mId = id; + } + + public int getSeedId() { + return mSeedId; + } + + public void setSeedId(int mSeedId) { + this.mSeedId = mSeedId; + } + + public int getAccountNumber() { + return mAccountNumber; + } + + public void setAccountNumber(int mAccountNumber) { + this.mAccountNumber = mAccountNumber; + } + + public int getAccountIndex() { + return mAccountIndex; + } + + public void setAccountIndex(int mAccountIndex) { + this.mAccountIndex = mAccountIndex; + } +} diff --git a/app/src/main/res/layout/activity_intro.xml b/app/src/main/res/layout/activity_intro.xml new file mode 100644 index 0000000..887d99c --- /dev/null +++ b/app/src/main/res/layout/activity_intro.xml @@ -0,0 +1,17 @@ + + + + + diff --git a/build.gradle b/build.gradle index c2eea8e..9f820f7 100644 --- a/build.gradle +++ b/build.gradle @@ -15,6 +15,10 @@ buildscript { allprojects { repositories { jcenter() + maven { + // For Room Persistence Library + url "https://maven.google.com" + } } }