]> git.ktnx.net Git - mobile-ledger-staging.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/App.java
fix server version detection when the first profile is being created
[mobile-ledger-staging.git] / app / src / main / java / net / ktnx / mobileledger / App.java
index b47e6d7faf14533a272517e4ac971badb0266061..a492e8a418ea1cfeb0c20e0d3e514d032ae55fd9 100644 (file)
@@ -24,7 +24,7 @@ import android.database.sqlite.SQLiteDatabase;
 import android.util.Log;
 
 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,13 +39,48 @@ import java.util.Locale;
 
 public class App extends Application {
     public static App instance;
+    private static ProfileDetailModel profileModel;
     private MobileLedgerDatabase dbHelper;
     private boolean monthNamesPrepared = false;
     public static SQLiteDatabase getDatabase() {
-        if (instance == null) throw new RuntimeException("Application not created yet");
+        if (instance == null)
+            throw new RuntimeException("Application not created yet");
 
         return instance.getDB();
     }
+    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()");
@@ -55,18 +90,18 @@ public class App extends Application {
         Authenticator.setDefault(new Authenticator() {
             @Override
             protected PasswordAuthentication getPasswordAuthentication() {
-                MobileLedgerProfile p = Data.profile.getValue();
-                if ((p != null) && 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());
-                        else Log.w("http-auth",
-                                String.format("Requesting host [%s] differs from expected [%s]",
-                                        requestingHost, expectedHost));
+                            return new PasswordAuthentication(getAuthUserName(),
+                                    getAuthPassword().toCharArray());
+                        else
+                            Log.w("http-auth",
+                                    String.format("Requesting host [%s] differs from expected [%s]",
+                                            requestingHost, expectedHost));
                     }
                     catch (MalformedURLException e) {
                         e.printStackTrace();
@@ -77,9 +112,6 @@ public class App extends Application {
             }
         });
     }
-    public static void prepareMonthNames() {
-        instance.prepareMonthNames(false);
-    }
     private void prepareMonthNames(boolean force) {
         if (force || monthNamesPrepared)
             return;
@@ -105,12 +137,11 @@ public class App extends Application {
         if (dbHelper == null)
             initDb();
 
-        final SQLiteDatabase db = dbHelper.getWritableDatabase();
-
-        return db;
+        return dbHelper.getWritableDatabase();
     }
     private synchronized void initDb() {
-        if (dbHelper != null) return;
+        if (dbHelper != null)
+            return;
 
         dbHelper = new MobileLedgerDatabase(this);
     }