瀏覽代碼

Partial support for account import

Signed-off-by: Mario Danic <mario@lovelyhq.com>
Mario Danic 7 年之前
父節點
當前提交
9cb42ab2b8

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

@@ -44,6 +44,8 @@ import com.nextcloud.talk.utils.ErrorMessageHolder;
 import com.nextcloud.talk.utils.bundle.BundleKeys;
 import com.nextcloud.talk.utils.database.user.UserUtils;
 
+import java.net.CookieManager;
+
 import javax.inject.Inject;
 
 import autodagger.AutoInjector;
@@ -65,6 +67,9 @@ public class AccountVerificationController extends BaseController {
     @Inject
     UserUtils userUtils;
 
+    @Inject
+    CookieManager cookieManager;
+
     @BindView(R.id.progress_text)
     TextView progressText;
 
@@ -75,6 +80,7 @@ public class AccountVerificationController extends BaseController {
     private String baseUrl;
     private String username;
     private String token;
+    private boolean isAccountImport;
 
     public AccountVerificationController(Bundle args) {
         super(args);
@@ -82,6 +88,9 @@ public class AccountVerificationController extends BaseController {
             baseUrl = args.getString(BundleKeys.KEY_BASE_URL);
             username = args.getString(BundleKeys.KEY_USERNAME);
             token = args.getString(BundleKeys.KEY_TOKEN);
+            if (args.containsKey(BundleKeys.KEY_IS_ACCOUNT_IMPORT)) {
+                isAccountImport = true;
+            }
         }
     }
 
@@ -106,6 +115,7 @@ public class AccountVerificationController extends BaseController {
         dispose(null);
 
         String credentials = ApiHelper.getCredentials(username, token);
+        cookieManager.getCookieStore().removeAll();
 
         roomsQueryDisposable = ncApi.getRooms(credentials, ApiHelper.getUrlForGetRooms(baseUrl))
                 .subscribeOn(Schedulers.newThread())
@@ -147,6 +157,7 @@ public class AccountVerificationController extends BaseController {
                                                         new JobRequest.Builder(PushRegistrationJob.TAG).
                                                                 setUpdateCurrent(true).startNow().build().schedule();
 
+                                                        cookieManager.getCookieStore().removeAll();
                                                         userUtils.disableAllUsersWithoutId(userEntity.getId());
 
                                                         if (userUtils.getUsers().size() == 1) {
@@ -155,6 +166,8 @@ public class AccountVerificationController extends BaseController {
                                                                     .pushChangeHandler(new HorizontalChangeHandler())
                                                                     .popChangeHandler(new HorizontalChangeHandler()));
                                                         } else {
+                                                            ErrorMessageHolder.getInstance().setMessageType(
+                                                                    ErrorMessageHolder.ErrorMessageType.ACCOUNT_WAS_IMPORTED);
                                                             getRouter().popToRoot();
                                                         }
                                                     },
@@ -231,22 +244,28 @@ public class AccountVerificationController extends BaseController {
     private void abortVerification() {
         dispose(null);
 
-        userUtils.deleteUser(username, baseUrl).subscribe(new CompletableObserver() {
-            @Override
-            public void onSubscribe(Disposable d) {
+        if (!isAccountImport) {
+            userUtils.deleteUser(username, baseUrl).subscribe(new CompletableObserver() {
+                @Override
+                public void onSubscribe(Disposable d) {
 
-            }
+                }
 
-            @Override
-            public void onComplete() {
-                new Handler().postDelayed(() -> getRouter().popToRoot(), 7500);
-            }
+                @Override
+                public void onComplete() {
+                    new Handler().postDelayed(() -> getRouter().popToRoot(), 10000);
+                }
 
-            @Override
-            public void onError(Throwable e) {
+                @Override
+                public void onError(Throwable e) {
 
-            }
-        });
+                }
+            });
+        } else {
+            ErrorMessageHolder.getInstance().setMessageType(
+                    ErrorMessageHolder.ErrorMessageType.FAILED_TO_IMPORT_ACCOUNT);
+            new Handler().postDelayed(() -> getRouter().popToRoot(), 10000);
+        }
     }
 
 }

+ 20 - 7
app/src/main/java/com/nextcloud/talk/controllers/ServerSelectionController.java

@@ -44,6 +44,7 @@ import com.nextcloud.talk.application.NextcloudTalkApplication;
 import com.nextcloud.talk.controllers.base.BaseController;
 import com.nextcloud.talk.utils.AccountUtils;
 import com.nextcloud.talk.utils.ErrorMessageHolder;
+import com.nextcloud.talk.utils.bundle.BundleKeys;
 import com.nextcloud.talk.utils.database.user.UserUtils;
 
 import java.security.cert.CertificateException;
@@ -110,7 +111,8 @@ public class ServerSelectionController extends BaseController {
             providersTextView.setVisibility(View.GONE);
         } else {
             if ((TextUtils.isEmpty(getResources
-                    ().getString(R.string.nc_import_account_type)) || AccountUtils.findAccounts().size() == 0) &&
+                    ().getString(R.string.nc_import_account_type)) ||
+                    AccountUtils.findAccounts(userUtils.getUsers()).size() == 0) &&
                     userUtils.getUsers().size() == 0) {
 
                 providersTextView.setText(R.string.nc_get_from_provider);
@@ -119,19 +121,30 @@ public class ServerSelectionController extends BaseController {
                             .getString(R.string.nc_providers_url)));
                     startActivity(browserIntent);
                 });
-            } else if (AccountUtils.findAccounts().size() > 0) {
+            } else if (AccountUtils.findAccounts(userUtils.getUsers()).size() > 0) {
                 if (!TextUtils.isEmpty(AccountUtils.getAppNameBasedOnPackage(getResources()
                                 .getString(R.string.nc_import_accounts_from)))) {
-                    providersTextView.setText(String.format(getResources().getString(R.string
-                            .nc_server_import_accounts), AccountUtils.getAppNameBasedOnPackage(getResources()
-                            .getString(R.string.nc_import_accounts_from))));
+                    if (AccountUtils.findAccounts(userUtils.getUsers()).size() > 1) {
+                        providersTextView.setText(String.format(getResources().getString(R.string
+                                .nc_server_import_accounts), AccountUtils.getAppNameBasedOnPackage(getResources()
+                                .getString(R.string.nc_import_accounts_from))));
+                    } else {
+                        providersTextView.setText(String.format(getResources().getString(R.string
+                                .nc_server_import_account), AccountUtils.getAppNameBasedOnPackage(getResources()
+                                .getString(R.string.nc_import_accounts_from))));
+                    }
                 } else {
-                    providersTextView.setText(getResources().getString(R.string.nc_server_import_accounts_plain_plural));
+                    if (AccountUtils.findAccounts(userUtils.getUsers()).size() > 1) {
+                        providersTextView.setText(getResources().getString(R.string.nc_server_import_accounts_plain));
+                    } else {
+                        providersTextView.setText(getResources().getString(R.string
+                                .nc_server_import_account_plain));
+                    }
                 }
 
                 providersTextView.setOnClickListener(view13 -> {
                     Bundle bundle = new Bundle();
-                    bundle.putBoolean("isAccountImport", true);
+                    bundle.putBoolean(BundleKeys.KEY_IS_ACCOUNT_IMPORT, true);
                     getRouter().pushController(RouterTransaction.with(
                             new SwitchAccountController(bundle))
                             .pushChangeHandler(new HorizontalChangeHandler())

+ 12 - 0
app/src/main/java/com/nextcloud/talk/controllers/SettingsController.java

@@ -160,6 +160,7 @@ public class SettingsController extends BaseController {
     @Override
     protected void onViewBound(@NonNull View view) {
         super.onViewBound(view);
+
         NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
 
         userEntity = userUtils.getCurrentUser();
@@ -236,6 +237,7 @@ public class SettingsController extends BaseController {
     @Override
     protected void onAttach(@NonNull View view) {
         super.onAttach(view);
+        getActionBar().show();
 
         dispose(null);
         userEntity = userUtils.getCurrentUser();
@@ -368,6 +370,16 @@ public class SettingsController extends BaseController {
                     messageText.setTextColor(getResources().getColor(R.color.nc_darkRed));
                     messageText.setText(getResources().getString(R.string.nc_settings_wrong_account));
                     messageView.setVisibility(View.VISIBLE);
+                case ACCOUNT_WAS_IMPORTED:
+                    messageText.setTextColor(getResources().getColor(R.color.colorPrimary));
+                    messageText.setText(getResources().getString(R.string.nc_Server_account_imported));
+                    messageView.setVisibility(View.VISIBLE);
+                    break;
+                case FAILED_TO_IMPORT_ACCOUNT:
+                    messageText.setTextColor(getResources().getColor(R.color.nc_darkRed));
+                    messageText.setText(getResources().getString(R.string.nc_server_failed_to_import_account));
+                    messageView.setVisibility(View.VISIBLE);
+                    break;
                 default:
                     messageView.setVisibility(View.GONE);
                     break;

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

@@ -41,6 +41,8 @@ import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 
+import com.bluelinelabs.conductor.RouterTransaction;
+import com.bluelinelabs.conductor.changehandler.HorizontalChangeHandler;
 import com.nextcloud.talk.R;
 import com.nextcloud.talk.adapters.items.AdvancedUserItem;
 import com.nextcloud.talk.api.models.json.participants.Participant;
@@ -49,6 +51,8 @@ import com.nextcloud.talk.controllers.base.BaseController;
 import com.nextcloud.talk.models.ImportAccount;
 import com.nextcloud.talk.persistence.entities.UserEntity;
 import com.nextcloud.talk.utils.AccountUtils;
+import com.nextcloud.talk.utils.bundle.BundleBuilder;
+import com.nextcloud.talk.utils.bundle.BundleKeys;
 import com.nextcloud.talk.utils.database.user.UserUtils;
 
 import java.net.CookieManager;
@@ -85,16 +89,13 @@ public class SwitchAccountController extends BaseController {
 
     private boolean isAccountImport = false;
 
-    private FlexibleAdapter.OnItemClickListener onImportItemClickListener = new FlexibleAdapter.OnItemClickListener() {
-        @Override
-        public boolean onItemClick(int position) {
-            if (userItems.size() > position) {
-                Account account = ((AdvancedUserItem) userItems.get(position)).getAccount();
-                getAuthTokenForAccount(account);
-            }
-
-            return true;
+    private FlexibleAdapter.OnItemClickListener onImportItemClickListener = position -> {
+        if (userItems.size() > position) {
+            Account account = ((AdvancedUserItem) userItems.get(position)).getAccount();
+            getAuthTokenForAccount(account);
         }
+
+        return true;
     };
 
     private FlexibleAdapter.OnItemClickListener onSwitchItemClickListener =
@@ -141,7 +142,7 @@ public class SwitchAccountController extends BaseController {
     public SwitchAccountController(Bundle args) {
         super(args);
 
-        if (args.containsKey("isAccountImport")) {
+        if (args.containsKey(BundleKeys.KEY_IS_ACCOUNT_IMPORT)) {
             isAccountImport = true;
         }
     }
@@ -188,15 +189,16 @@ public class SwitchAccountController extends BaseController {
                         participant.setName(importAccount.getUsername());
                         participant.setUserId(importAccount.getUsername());
                         userEntity = new UserEntity();
-                        userEntity.setBaseUrl(importAccount.getServerUrl());
+                        userEntity.setBaseUrl(importAccount.getBaseUrl());
                         userItems.add(new AdvancedUserItem(participant, userEntity, account));
                     }
-                }
 
-                adapter.addListener(onSwitchItemClickListener);
+                adapter.addListener(onImportItemClickListener);
                 adapter.updateDataSet(userItems, false);
             }
 
+            }
+
         prepareViews();
     }
 
@@ -218,7 +220,7 @@ public class SwitchAccountController extends BaseController {
         final AccountManager accMgr = AccountManager.get(getActivity());
 
         final AlertDialog alertDialog = new AlertDialog.Builder(getActivity())
-                .setTitle(getResources().getString(R.string.nc_server_import_accounts_plain_singular))
+                .setTitle(getResources().getString(R.string.nc_server_import_account_plain))
                 .setMessage(getResources().getString(R.string.nc_server_import_account_notification))
                 .create();
 
@@ -236,6 +238,14 @@ public class SwitchAccountController extends BaseController {
                         try {
                             ImportAccount importAccount = AccountUtils.getInformationFromAccount(account, future
                                     .getResult());
+                            BundleBuilder bundleBuilder = new BundleBuilder(new Bundle());
+                            bundleBuilder.putString(BundleKeys.KEY_USERNAME, importAccount.getUsername());
+                            bundleBuilder.putString(BundleKeys.KEY_TOKEN, importAccount.getToken());
+                            bundleBuilder.putString(BundleKeys.KEY_BASE_URL, importAccount.getBaseUrl());
+                            bundleBuilder.putBoolean(BundleKeys.KEY_IS_ACCOUNT_IMPORT, true);
+                            getRouter().pushController(RouterTransaction.with(new AccountVerificationController
+                                    (bundleBuilder.build())).pushChangeHandler(new HorizontalChangeHandler())
+                                    .popChangeHandler(new HorizontalChangeHandler()));
                         } catch (OperationCanceledException e) {
                             Log.e(TAG, "Access was denied");
                             // TODO: The user has denied you access to the API, handle this later on

+ 3 - 3
app/src/main/java/com/nextcloud/talk/models/ImportAccount.java

@@ -28,11 +28,11 @@ import lombok.Data;
 public class ImportAccount {
     public String username;
     @Nullable public String token;
-    public String serverUrl;
+    public String baseUrl;
 
-    public ImportAccount(String username, @Nullable String token, String serverUrl) {
+    public ImportAccount(String username, @Nullable String token, String baseUrl) {
         this.username = username;
         this.token = token;
-        this.serverUrl = serverUrl;
+        this.baseUrl = baseUrl;
     }
 }

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

@@ -62,7 +62,7 @@ public class AccountUtils {
                     internalUserEntity = userEntitiesList.get(i);
                     importAccount = getInformationFromAccount(account, null);
                     if (internalUserEntity.getUsername().equals(importAccount.getUsername()) &&
-                            internalUserEntity.getBaseUrl().equals(importAccount.getServerUrl())) {
+                            internalUserEntity.getBaseUrl().equals(importAccount.getBaseUrl())) {
                         accountFound = true;
                         break;
                     }
@@ -95,10 +95,12 @@ public class AccountUtils {
         String urlString = account.name.substring(lastAtPos + 1);
         String username = account.name.substring(0, lastAtPos);
 
-        if (!urlString.startsWith("http"))
-            urlString = "https://" + urlString;
+        if (!urlString.startsWith("http")) {
+            urlString = "http://" + urlString;
+        }
 
         String password = null;
+
         if (data != null) {
             password = data.getString(AccountManager.KEY_AUTHTOKEN);
         }
@@ -109,6 +111,8 @@ public class AccountUtils {
             urlString = url.getProtocol() + "://" + url.getHost();
             if (url.getPath().contains("/owncloud")) {
                 urlString += url.getPath().substring(0, url.getPath().indexOf("/owncloud") + 9);
+            } else if (url.getPath().contains("/nextcloud")) {
+                urlString += url.getPath().substring(0, url.getPath().indexOf("/nextcloud") + 10);
             } else if (url.getPath().contains("/")) {
                 urlString += url.getPath().substring(0, url.getPath().indexOf("/"));
             }

+ 2 - 1
app/src/main/java/com/nextcloud/talk/utils/ErrorMessageHolder.java

@@ -39,7 +39,8 @@ public class ErrorMessageHolder {
     }
 
     public enum ErrorMessageType {
-        WRONG_ACCOUNT, ACCOUNT_UPDATED_NOT_ADDED, ACCOUNT_SCHEDULED_FOR_DELETION, SERVER_WITHOUT_TALK
+        WRONG_ACCOUNT, ACCOUNT_UPDATED_NOT_ADDED, ACCOUNT_SCHEDULED_FOR_DELETION, SERVER_WITHOUT_TALK,
+        FAILED_TO_IMPORT_ACCOUNT, ACCOUNT_WAS_IMPORTED
     }
 
 

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

@@ -24,5 +24,5 @@ public class BundleKeys {
     public static final String KEY_USERNAME = "KEY_USERNAME";
     public static final String KEY_TOKEN = "KEY_TOKEN";
     public static final String KEY_BASE_URL = "KEY_BASE_URL";
-    public static final String KEY_SETTINGS_MESSAGE = "KEY_SETTINGS_MESSAGE";
+    public static final String KEY_IS_ACCOUNT_IMPORT = "IS_ACCOUNT_IMPORT";
 }

+ 1 - 0
app/src/main/res/layout/controller_account_verification.xml

@@ -50,6 +50,7 @@
         android:layout_marginRight="@dimen/activity_horizontal_margin"
         android:layout_marginStart="@dimen/activity_horizontal_margin"
         android:layout_marginTop="@dimen/padding_between_elements"
+        android:textAlignment="center"
         android:textSize="18sp"
         tools:text="Verifying..."/>
 

+ 7 - 3
app/src/main/res/values/strings.xml

@@ -11,10 +11,14 @@
     <string name="nc_server_db_upgrade_needed">Please upgrade your %1$s database</string>
     <string name="nc_server_maintenance">Please bring your %1$s out of maintenance</string>
     <string name="nc_server_version">%1$s only works with %2$s 13 and up</string>
-    <string name="nc_server_import_accounts_plain_singular">Import account</string>
-    <string name="nc_server_import_accounts_plain_plural">Import accounts</string>
+    <string name="nc_server_import_account_plain">Import account</string>
+    <string name="nc_server_import_accounts_plain">Import accounts</string>
+    <string name="nc_server_import_account">Import account from the %1$s app</string>
     <string name="nc_server_import_accounts">Import accounts from the %1$s app</string>
-    <string name="nc_server_import_account_notification">Please grant access to the selected account in the notification bar!"</string>
+    <string name="nc_server_failed_to_import_account">Failed to import selected account</string>
+    <string name="nc_Server_account_imported">Selected account is now imported and available</string>
+    <string name="nc_server_import_account_notification">Please grant access to the selected account in the
+        notification bar!"</string>
     <string name="nc_get_from_provider">Do you not have a server yet?\nClick here to get one from a provider</string>
 
     <!-- Account verification -->