]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/App.java
App: make the Room database handle available
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / App.java
index 724627971769db475e7007c799ed1ae81987edf6..7935beba307d7ffe4ff1eef8dcbef45a38535e2d 100644 (file)
@@ -23,8 +23,11 @@ import android.content.res.Resources;
 import android.database.sqlite.SQLiteDatabase;
 import android.util.Log;
 
+import androidx.room.Room;
+
+import net.ktnx.mobileledger.db.DB;
 import net.ktnx.mobileledger.model.Data;
-import net.ktnx.mobileledger.model.MobileLedgerProfile;
+import net.ktnx.mobileledger.ui.profiles.ProfileDetailModel;
 import net.ktnx.mobileledger.utils.Globals;
 import net.ktnx.mobileledger.utils.Logger;
 import net.ktnx.mobileledger.utils.MobileLedgerDatabase;
@@ -39,36 +42,77 @@ import java.util.Locale;
 
 public class App extends Application {
     public static App instance;
+    private static ProfileDetailModel profileModel;
     private MobileLedgerDatabase dbHelper;
     private boolean monthNamesPrepared = false;
+    private DB roomDatabase;
     public static SQLiteDatabase getDatabase() {
         if (instance == null)
             throw new RuntimeException("Application not created yet");
 
         return instance.getDB();
     }
+    public static DB getRoomDB() {
+        if (instance == null)
+            throw new RuntimeException("Application not created yet");
+
+        return instance.getRoomDatabase();
+    }
+    public DB getRoomDatabase(){
+        return roomDatabase;
+    }
     public static void prepareMonthNames() {
         instance.prepareMonthNames(false);
     }
+    public static void setAuthenticationDataFromProfileModel(ProfileDetailModel model) {
+        profileModel = model;
+    }
+    public static void resetAuthenticationData() {
+        profileModel = null;
+    }
+    private String getAuthURL() {
+        if (profileModel != null)
+            return profileModel.getUrl();
+        return Data.getProfile()
+                   .getUrl();
+    }
+    private String getAuthUserName() {
+        if (profileModel != null)
+            return profileModel.getAuthUserName();
+        return Data.getProfile()
+                   .getAuthUserName();
+    }
+    private String getAuthPassword() {
+        if (profileModel != null)
+            return profileModel.getAuthPassword();
+        return Data.getProfile()
+                   .getAuthPassword();
+    }
+    private boolean getAuthEnabled() {
+        if (profileModel != null)
+            return profileModel.getUseAuthentication();
+        return Data.getProfile()
+                   .isAuthEnabled();
+    }
     @Override
     public void onCreate() {
         Logger.debug("flow", "App onCreate()");
         instance = this;
         super.onCreate();
+        roomDatabase = Room.databaseBuilder(this, DB.class, MobileLedgerDatabase.DB_NAME)
+                              .build();
         Data.refreshCurrencyData(Locale.getDefault());
         Authenticator.setDefault(new Authenticator() {
             @Override
             protected PasswordAuthentication getPasswordAuthentication() {
-                MobileLedgerProfile p = Data.getProfile();
-                if (p.isAuthEnabled()) {
+                if (getAuthEnabled()) {
                     try {
-                        final URL url = new URL(p.getUrl());
+                        final URL url = new URL(getAuthURL());
                         final String requestingHost = getRequestingHost();
                         final String expectedHost = url.getHost();
                         if (requestingHost.equalsIgnoreCase(expectedHost))
-                            return new PasswordAuthentication(p.getAuthUserName(),
-                                    p.getAuthPassword()
-                                     .toCharArray());
+                            return new PasswordAuthentication(getAuthUserName(),
+                                    getAuthPassword().toCharArray());
                         else
                             Log.w("http-auth",
                                     String.format("Requesting host [%s] differs from expected [%s]",