Partial support for account import

Signed-off-by: Mario Danic <mario@lovelyhq.com>
This commit is contained in:
Mario Danic 2017-12-30 23:43:44 +01:00
parent ec9451caae
commit 9cb42ab2b8
10 changed files with 108 additions and 44 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -28,11 +28,11 @@ import lombok.Data;
public class ImportAccount { public class ImportAccount {
public String username; public String username;
@Nullable public String token; @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.username = username;
this.token = token; this.token = token;
this.serverUrl = serverUrl; this.baseUrl = baseUrl;
} }
} }

View File

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

View File

@ -39,7 +39,8 @@ public class ErrorMessageHolder {
} }
public enum ErrorMessageType { 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
} }

View File

@ -24,5 +24,5 @@ public class BundleKeys {
public static final String KEY_USERNAME = "KEY_USERNAME"; public static final String KEY_USERNAME = "KEY_USERNAME";
public static final String KEY_TOKEN = "KEY_TOKEN"; public static final String KEY_TOKEN = "KEY_TOKEN";
public static final String KEY_BASE_URL = "KEY_BASE_URL"; 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";
} }

View File

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

View File

@ -11,10 +11,14 @@
<string name="nc_server_db_upgrade_needed">Please upgrade your %1$s database</string> <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_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_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_account_plain">Import account</string>
<string name="nc_server_import_accounts_plain_plural">Import accounts</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_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> <string name="nc_get_from_provider">Do you not have a server yet?\nClick here to get one from a provider</string>
<!-- Account verification --> <!-- Account verification -->