Przeglądaj źródła

Improvements to login

Signed-off-by: Mario Danic <mario@lovelyhq.com>
Mario Danic 7 lat temu
rodzic
commit
562f5498d1

+ 3 - 3
app/src/main/java/com/nextcloud/talk/activities/CallActivity.java

@@ -242,9 +242,9 @@ public class CallActivity extends AppCompatActivity {
         basicInitialization();
 
         if (!userEntity.getCurrent()) {
-            userUtils.createOrUpdateUser(userEntity.getUsername(),
-                    userEntity.getToken(), userEntity.getBaseUrl(), null,
-                    null, true, null)
+            userUtils.createOrUpdateUser(null,
+                    null, null, null,
+                    null, true, null, userEntity.getId())
                     .subscribe(new Observer<UserEntity>() {
                         @Override
                         public void onSubscribe(Disposable d) {

+ 12 - 4
app/src/main/java/com/nextcloud/talk/controllers/AccountVerificationController.java

@@ -82,6 +82,7 @@ public class AccountVerificationController extends BaseController {
     private String username;
     private String token;
     private boolean isAccountImport;
+    private String originalProtocol;
 
     public AccountVerificationController(Bundle args) {
         super(args);
@@ -92,6 +93,9 @@ public class AccountVerificationController extends BaseController {
             if (args.containsKey(BundleKeys.KEY_IS_ACCOUNT_IMPORT)) {
                 isAccountImport = true;
             }
+            if (args.containsKey(BundleKeys.KEY_ORIGINAL_PROTOCOL)) {
+                originalProtocol = args.getString(BundleKeys.KEY_ORIGINAL_PROTOCOL);
+            }
         }
     }
 
@@ -115,7 +119,8 @@ public class AccountVerificationController extends BaseController {
 
         dispose(null);
 
-        if (isAccountImport && !baseUrl.startsWith("http://") && !baseUrl.startsWith("https://")) {
+        if (isAccountImport && !baseUrl.startsWith("http://") && !baseUrl.startsWith("https://") || (!TextUtils
+                .isEmpty(originalProtocol) && !baseUrl.startsWith(originalProtocol))) {
             determineBaseUrlProtocol(true);
         } else {
             checkEverything();
@@ -127,6 +132,9 @@ public class AccountVerificationController extends BaseController {
         cookieManager.getCookieStore().removeAll();
 
         String queryUrl;
+
+        baseUrl = baseUrl.replace("http://", "").replace("https://", "");
+
         if (checkForcedHttps) {
             queryUrl = "https://" + baseUrl + ApiHelper.getUrlPostfixForStatus();
         } else {
@@ -188,7 +196,7 @@ public class AccountVerificationController extends BaseController {
                                 if (!TextUtils.isEmpty(displayName)) {
                                     dbQueryDisposable = userUtils.createOrUpdateUser(username, token,
                                             baseUrl, displayName, null, true,
-                                            userProfileOverall.getOcs().getData().getUserId())
+                                            userProfileOverall.getOcs().getData().getUserId(), null)
                                             .subscribeOn(Schedulers.newThread())
                                             .observeOn(AndroidSchedulers.mainThread())
                                             .subscribe(userEntity -> {
@@ -300,7 +308,7 @@ public class AccountVerificationController extends BaseController {
 
                 @Override
                 public void onComplete() {
-                    new Handler().postDelayed(() -> getRouter().popToRoot(), 10000);
+                    new Handler().postDelayed(() -> getRouter().popToRoot(), 7500);
                 }
 
                 @Override
@@ -311,7 +319,7 @@ public class AccountVerificationController extends BaseController {
         } else {
             ErrorMessageHolder.getInstance().setMessageType(
                     ErrorMessageHolder.ErrorMessageType.FAILED_TO_IMPORT_ACCOUNT);
-            new Handler().postDelayed(() -> getRouter().popToRoot(), 10000);
+            new Handler().postDelayed(() -> getRouter().popToRoot(), 7500);
         }
     }
 

+ 3 - 3
app/src/main/java/com/nextcloud/talk/controllers/SwitchAccountController.java

@@ -97,9 +97,9 @@ public class SwitchAccountController extends BaseController {
                 public boolean onItemClick(int position) {
                     if (userItems.size() > position) {
                         UserEntity userEntity = ((AdvancedUserItem) userItems.get(position)).getEntity();
-                        userUtils.createOrUpdateUser(userEntity.getUsername(),
-                                userEntity.getToken(), userEntity.getBaseUrl(), null,
-                                null, true, null)
+                        userUtils.createOrUpdateUser(null,
+                                null, null, null,
+                                null, true, null, userEntity.getId())
                                 .subscribe(new Observer<UserEntity>() {
                                     @Override
                                     public void onSubscribe(Disposable d) {

+ 43 - 22
app/src/main/java/com/nextcloud/talk/controllers/WebViewLoginController.java

@@ -280,29 +280,50 @@ public class WebViewLoginController extends BaseController {
                     getRouter().popToRoot();
                 }
 
-                // We use the URL user entered because one provided by the server is NOT reliable
                 ErrorMessageHolder.ErrorMessageType finalErrorMessageType = errorMessageType;
-                userQueryDisposable = userUtils.createOrUpdateUser(loginData.getUsername(), loginData.getToken(),
-                        loginData.getServerUrl(), null, null, true,
-                        null).
-                        subscribe(userEntity -> {
-                                    cookieManager.getCookieStore().removeAll();
-                                    if (!isPasswordUpdate && finalErrorMessageType == null) {
-                                        BundleBuilder bundleBuilder = new BundleBuilder(new Bundle());
-                                        bundleBuilder.putString(BundleKeys.KEY_USERNAME, userEntity.getUsername());
-                                        bundleBuilder.putString(BundleKeys.KEY_TOKEN, userEntity.getToken());
-                                        bundleBuilder.putString(BundleKeys.KEY_BASE_URL, userEntity.getBaseUrl());
-                                        getRouter().pushController(RouterTransaction.with(new AccountVerificationController
-                                                (bundleBuilder.build())).pushChangeHandler(new HorizontalChangeHandler())
-                                                .popChangeHandler(new HorizontalChangeHandler()));
-                                    } else {
-                                        if (finalErrorMessageType != null) {
-                                            ErrorMessageHolder.getInstance().setMessageType(finalErrorMessageType);
-                                        }
-                                        getRouter().popToRoot();
-                                    }
-                                }, throwable -> dispose(),
-                                this::dispose);
+                cookieManager.getCookieStore().removeAll();
+
+                if (!isPasswordUpdate && finalErrorMessageType == null) {
+                    BundleBuilder bundleBuilder = new BundleBuilder(new Bundle());
+                    bundleBuilder.putString(BundleKeys.KEY_USERNAME, loginData.getUsername());
+                    bundleBuilder.putString(BundleKeys.KEY_TOKEN, loginData.getToken());
+                    bundleBuilder.putString(BundleKeys.KEY_BASE_URL, loginData.getServerUrl());
+                    String protocol = "";
+
+                    if (baseUrl.startsWith("http://")) {
+                        protocol = "http://";
+                    } else if (baseUrl.startsWith("https://")) {
+                        protocol = "https://";
+                    }
+
+                    if (!TextUtils.isEmpty(protocol)) {
+                        bundleBuilder.putString(BundleKeys.KEY_ORIGINAL_PROTOCOL, protocol);
+                    }
+                    getRouter().pushController(RouterTransaction.with(new AccountVerificationController
+                            (bundleBuilder.build())).pushChangeHandler(new HorizontalChangeHandler())
+                            .popChangeHandler(new HorizontalChangeHandler()));
+                } else {
+                    if (isPasswordUpdate) {
+                        if (currentUser != null) {
+                            userQueryDisposable = userUtils.createOrUpdateUser(null, null,
+                                    null, null, null, true,
+                                    null, currentUser.getId()).
+                                    subscribe(userEntity -> {
+                                                if (finalErrorMessageType != null) {
+                                                    ErrorMessageHolder.getInstance().setMessageType(finalErrorMessageType);
+                                                }
+                                                getRouter().popToRoot();
+                                            }, throwable -> dispose(),
+                                            this::dispose);
+                        }
+                    } else {
+                        if (finalErrorMessageType != null) {
+                            ErrorMessageHolder.getInstance().setMessageType(finalErrorMessageType);
+                        }
+                        getRouter().popToRoot();
+
+                    }
+                }
             }
         }
     }

+ 3 - 3
app/src/main/java/com/nextcloud/talk/utils/PushUtils.java

@@ -301,11 +301,11 @@ public class PushUtils {
                                                                         .getData().getPublicKey());
                                                         pushConfigurationState.setUsesRegularPass(false);
 
-                                                        userUtils.createOrUpdateUser(userEntity.getUsername(),
-                                                                userEntity.getToken(), userEntity.getBaseUrl(),
+                                                        userUtils.createOrUpdateUser(null,
+                                                                null, null,
                                                                 userEntity.getDisplayName(),
                                                                 LoganSquare.serialize(pushConfigurationState), null,
-                                                                null)
+                                                                null, userEntity.getId())
                                                                 .subscribe(new Consumer<UserEntity>() {
                                                                     @Override
                                                                     public void accept(UserEntity userEntity) throws Exception {

+ 1 - 0
app/src/main/java/com/nextcloud/talk/utils/bundle/BundleKeys.java

@@ -25,4 +25,5 @@ public class BundleKeys {
     public static final String KEY_TOKEN = "KEY_TOKEN";
     public static final String KEY_BASE_URL = "KEY_BASE_URL";
     public static final String KEY_IS_ACCOUNT_IMPORT = "IS_ACCOUNT_IMPORT";
+    public static final String KEY_ORIGINAL_PROTOCOL = "ORIGINAL_PROTOCOL";
 }

+ 12 - 5
app/src/main/java/com/nextcloud/talk/utils/database/user/UserUtils.java

@@ -148,13 +148,20 @@ public class UserUtils {
 
     }
 
-    public Observable<UserEntity> createOrUpdateUser(String username, String token, String serverUrl,
+    public Observable<UserEntity> createOrUpdateUser(@Nullable String username, @Nullable String token, @Nullable String
+                                                     serverUrl,
                                                      @Nullable String displayName,
                                                      @Nullable String pushConfigurationState,
                                                      @Nullable Boolean currentUser,
-                                                     @Nullable String userId) {
-        Result findUserQueryResult = dataStore.select(User.class).where(UserEntity.USERNAME.eq(username).
-                and(UserEntity.BASE_URL.eq(serverUrl.toLowerCase()))).limit(1).get();
+                                                     @Nullable String userId,
+                                                     @Nullable Long internalId) {
+        Result findUserQueryResult;
+        if (internalId == null) {
+            findUserQueryResult = dataStore.select(User.class).where(UserEntity.USERNAME.eq(username).
+                    and(UserEntity.BASE_URL.eq(serverUrl.toLowerCase()))).limit(1).get();
+        } else {
+            findUserQueryResult = dataStore.select(User.class).where(UserEntity.ID.eq(internalId)).get();
+        }
 
         UserEntity user = (UserEntity) findUserQueryResult.firstOrNull();
 
@@ -183,7 +190,7 @@ public class UserUtils {
                 user.setUserId(userId);
             }
 
-            if (!token.equals(user.getToken())) {
+            if (token != null && !token.equals(user.getToken())) {
                 user.setToken(token);
             }
 

+ 6 - 5
app/src/main/res/layout/controller_web_view_login.xml

@@ -20,9 +20,9 @@
   -->
 
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
-              android:layout_width="match_parent"
-              android:layout_height="match_parent"
-              android:orientation="vertical">
+                android:layout_width="match_parent"
+                android:layout_height="match_parent"
+                android:orientation="vertical">
 
     <ProgressBar
         android:id="@+id/progress_bar"
@@ -34,13 +34,14 @@
         android:layout_marginRight="@dimen/activity_horizontal_margin"
         android:layout_marginStart="@dimen/activity_horizontal_margin"
         android:indeterminate="true"
-        android:indeterminateTintMode="src_in"
-        android:indeterminateTint="@color/colorPrimary"/>
+        android:indeterminateTint="@color/colorPrimary"
+        android:indeterminateTintMode="src_in"/>
 
     <WebView
         android:id="@+id/webview"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
+        android:keepScreenOn="true"
         android:visibility="invisible">
 
     </WebView>