diff --git a/app/src/main/java/com/nextcloud/talk/adapters/items/AdvancedUserItem.java b/app/src/main/java/com/nextcloud/talk/adapters/items/AdvancedUserItem.java index cdc1ea72b..d1eb5beb3 100644 --- a/app/src/main/java/com/nextcloud/talk/adapters/items/AdvancedUserItem.java +++ b/app/src/main/java/com/nextcloud/talk/adapters/items/AdvancedUserItem.java @@ -125,7 +125,12 @@ public class AdvancedUserItem extends AbstractFlexibleItem. - */ - -package com.nextcloud.talk.controllers; - -import android.annotation.SuppressLint; -import android.content.Intent; -import android.content.pm.ActivityInfo; -import android.net.Uri; -import android.os.Bundle; -import android.security.KeyChain; -import android.text.TextUtils; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.view.inputmethod.EditorInfo; -import android.widget.LinearLayout; -import android.widget.TextView; - -import com.bluelinelabs.conductor.RouterTransaction; -import com.bluelinelabs.conductor.changehandler.HorizontalChangeHandler; -import com.google.android.material.textfield.TextInputEditText; -import com.google.android.material.textfield.TextInputLayout; -import com.nextcloud.talk.R; -import com.nextcloud.talk.api.NcApi; -import com.nextcloud.talk.application.NextcloudTalkApplication; -import com.nextcloud.talk.controllers.base.BaseController; -import com.nextcloud.talk.utils.AccountUtils; -import com.nextcloud.talk.utils.ApiUtils; -import com.nextcloud.talk.utils.DisplayUtils; -import com.nextcloud.talk.utils.bundle.BundleKeys; -import com.nextcloud.talk.utils.database.user.UserUtils; -import com.nextcloud.talk.utils.preferences.AppPreferences; -import com.nextcloud.talk.utils.singletons.ApplicationWideMessageHolder; - -import java.security.cert.CertificateException; - -import javax.inject.Inject; - -import androidx.annotation.NonNull; -import androidx.core.content.res.ResourcesCompat; -import autodagger.AutoInjector; -import butterknife.BindView; -import butterknife.OnClick; -import io.reactivex.android.schedulers.AndroidSchedulers; -import io.reactivex.disposables.Disposable; -import io.reactivex.schedulers.Schedulers; - -@AutoInjector(NextcloudTalkApplication.class) -public class ServerSelectionController extends BaseController { - - public static final String TAG = "ServerSelectionController"; - - @BindView(R.id.serverEntryTextInputLayout) - TextInputLayout serverEntryTextInputLayout; - @BindView(R.id.serverEntryTextInputEditText) - TextInputEditText serverEntryTextInputEditText; - @BindView(R.id.serverEntryProgressBar) - LinearLayout progressBar; - @BindView(R.id.error_text) - TextView errorText; - @BindView(R.id.host_url_input_helper_text) - TextView hostUrlInputHelperText; - @BindView(R.id.helper_text_view) - TextView providersTextView; - @BindView(R.id.cert_text_view) - TextView certTextView; - - @Inject - NcApi ncApi; - - @Inject - UserUtils userUtils; - - @Inject - AppPreferences appPreferences; - - private Disposable statusQueryDisposable; - - @NonNull - @Override - protected View inflateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) { - return inflater.inflate(R.layout.controller_server_selection, container, false); - } - - @SuppressLint("LongLogTag") - @OnClick(R.id.cert_text_view) - public void onCertClick() { - if (getActivity() != null) { - KeyChain.choosePrivateKeyAlias(getActivity(), alias -> { - if (alias != null) { - appPreferences.setTemporaryClientCertAlias(alias); - } else { - appPreferences.removeTemporaryClientCertAlias(); - } - - setCertTextView(); - }, new String[]{"RSA", "EC"}, null, null, -1, null); - } - } - - @Override - protected void onViewBound(@NonNull View view) { - super.onViewBound(view); - NextcloudTalkApplication.Companion.getSharedApplication().getComponentApplication().inject(this); - - if (getActivity() != null) { - getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); - } - - if (getActionBar() != null) { - getActionBar().hide(); - } - - hostUrlInputHelperText.setText(String.format( - getResources().getString(R.string.nc_server_helper_text), - getResources().getString(R.string.nc_server_product_name)) - ); - - serverEntryTextInputLayout.setEndIconOnClickListener(view1 -> checkServerAndProceed()); - - if (getResources().getBoolean(R.bool.hide_auth_cert)) { - certTextView.setVisibility(View.GONE); - } - - if (getResources().getBoolean(R.bool.hide_provider) || - TextUtils.isEmpty(getResources().getString(R.string.nc_providers_url)) && - (TextUtils.isEmpty(getResources().getString(R.string.nc_import_account_type)))) { - providersTextView.setVisibility(View.INVISIBLE); - } else { - if ((TextUtils.isEmpty(getResources - ().getString(R.string.nc_import_account_type)) || - AccountUtils.INSTANCE.findAccounts(userUtils.getUsers()).size() == 0) && - userUtils.getUsers().size() == 0) { - - providersTextView.setText(R.string.nc_get_from_provider); - providersTextView.setOnClickListener(view12 -> { - Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getResources() - .getString(R.string.nc_providers_url))); - startActivity(browserIntent); - }); - } else if (AccountUtils.INSTANCE.findAccounts(userUtils.getUsers()).size() > 0) { - if (!TextUtils.isEmpty(AccountUtils.INSTANCE.getAppNameBasedOnPackage(getResources() - .getString(R.string.nc_import_accounts_from)))) { - if (AccountUtils.INSTANCE.findAccounts(userUtils.getUsers()).size() > 1) { - providersTextView.setText(String.format(getResources().getString(R.string - .nc_server_import_accounts), AccountUtils.INSTANCE.getAppNameBasedOnPackage(getResources() - .getString(R.string.nc_import_accounts_from)))); - } else { - providersTextView.setText(String.format(getResources().getString(R.string - .nc_server_import_account), AccountUtils.INSTANCE.getAppNameBasedOnPackage(getResources() - .getString(R.string.nc_import_accounts_from)))); - } - } else { - if (AccountUtils.INSTANCE.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(BundleKeys.INSTANCE.getKEY_IS_ACCOUNT_IMPORT(), true); - getRouter().pushController(RouterTransaction.with( - new SwitchAccountController(bundle)) - .pushChangeHandler(new HorizontalChangeHandler()) - .popChangeHandler(new HorizontalChangeHandler())); - }); - } else { - providersTextView.setVisibility(View.INVISIBLE); - } - } - - serverEntryTextInputEditText.requestFocus(); - - if (!TextUtils.isEmpty(getResources().getString(R.string.weblogin_url))) { - serverEntryTextInputEditText.setText(getResources().getString(R.string.weblogin_url)); - checkServerAndProceed(); - } - - serverEntryTextInputEditText.setOnEditorActionListener((textView, i, keyEvent) -> { - if (i == EditorInfo.IME_ACTION_DONE) { - checkServerAndProceed(); - } - - return false; - }); - } - - private void checkServerAndProceed() { - dispose(); - - String url = serverEntryTextInputEditText.getText().toString().trim(); - - serverEntryTextInputEditText.setEnabled(false); - showProgressBar(); - if (providersTextView.getVisibility() != View.INVISIBLE) { - providersTextView.setVisibility(View.INVISIBLE); - certTextView.setVisibility(View.INVISIBLE); - } - - if (url.endsWith("/")) { - url = url.substring(0, url.length() - 1); - } - - String queryUrl = url + ApiUtils.getUrlPostfixForStatus(); - - if (url.startsWith("http://") || url.startsWith("https://")) { - checkServer(queryUrl, false); - } else { - checkServer("https://" + queryUrl, true); - } - } - - private void checkServer(String queryUrl, boolean checkForcedHttps) { - statusQueryDisposable = ncApi.getServerStatus(queryUrl) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(status -> { - String productName = getResources().getString(R.string.nc_server_product_name); - - String versionString = status.getVersion().substring(0, status.getVersion().indexOf(".")); - int version = Integer.parseInt(versionString); - if (status.isInstalled() && !status.isMaintenance() && - !status.isNeedsUpgrade() && - version >= 13) { - - getRouter().pushController(RouterTransaction.with( - new WebViewLoginController(queryUrl.replace("/status.php", ""), - false)) - .pushChangeHandler(new HorizontalChangeHandler()) - .popChangeHandler(new HorizontalChangeHandler())); - } else if (!status.isInstalled()) { - setErrorText(String.format( - getResources().getString(R.string.nc_server_not_installed), productName)); - } else if (status.isNeedsUpgrade()) { - setErrorText(String.format(getResources(). - getString(R.string.nc_server_db_upgrade_needed), - productName)); - } else if (status.isMaintenance()) { - setErrorText(String.format(getResources(). - getString(R.string.nc_server_maintenance), - productName)); - } else if (!status.getVersion().startsWith("13.")) { - setErrorText(String.format(getResources(). - getString(R.string.nc_server_version), - getResources().getString(R.string.nc_app_product_name), - productName)); - } - - }, throwable -> { - if (checkForcedHttps) { - checkServer(queryUrl.replace("https://", "http://"), false); - } else { - if (throwable.getLocalizedMessage() != null) { - setErrorText(throwable.getLocalizedMessage()); - } else if (throwable.getCause() instanceof CertificateException) { - setErrorText(getResources().getString(R.string.nc_certificate_error)); - } else { - hideProgressBar(); - } - - if (serverEntryTextInputEditText != null) { - serverEntryTextInputEditText.setEnabled(true); - } - - if (providersTextView.getVisibility() != View.INVISIBLE) { - providersTextView.setVisibility(View.VISIBLE); - certTextView.setVisibility(View.VISIBLE); - } - - dispose(); - } - }, () -> { - hideProgressBar(); - if (providersTextView.getVisibility() != View.INVISIBLE) { - providersTextView.setVisibility(View.VISIBLE); - certTextView.setVisibility(View.VISIBLE); - } - dispose(); - }); - } - - private void setErrorText(String text) { - errorText.setText(text); - errorText.setVisibility(View.VISIBLE); - progressBar.setVisibility(View.GONE); - } - - private void showProgressBar() { - errorText.setVisibility(View.GONE); - progressBar.setVisibility(View.VISIBLE); - } - - private void hideProgressBar() { - errorText.setVisibility(View.GONE); - progressBar.setVisibility(View.INVISIBLE); - } - - @Override - protected void onAttach(@NonNull View view) { - super.onAttach(view); - if (ApplicationWideMessageHolder.getInstance().getMessageType() != null) { - if (ApplicationWideMessageHolder.getInstance().getMessageType() - .equals(ApplicationWideMessageHolder.MessageType.ACCOUNT_SCHEDULED_FOR_DELETION)) { - setErrorText(getResources().getString(R.string.nc_account_scheduled_for_deletion)); - ApplicationWideMessageHolder.getInstance().setMessageType(null); - } else if (ApplicationWideMessageHolder.getInstance().getMessageType() - .equals(ApplicationWideMessageHolder.MessageType.SERVER_WITHOUT_TALK)) { - setErrorText(getResources().getString(R.string.nc_settings_no_talk_installed)); - } else if (ApplicationWideMessageHolder.getInstance().getMessageType() - .equals(ApplicationWideMessageHolder.MessageType.FAILED_TO_IMPORT_ACCOUNT)) { - setErrorText(getResources().getString(R.string.nc_server_failed_to_import_account)); - } - ApplicationWideMessageHolder.getInstance().setMessageType(null); - } - - if (getActivity() != null && getResources() != null) { - DisplayUtils.applyColorToStatusBar(getActivity(), ResourcesCompat.getColor(getResources(), R.color.colorPrimary, null)); - DisplayUtils.applyColorToNavigationBar(getActivity().getWindow(), ResourcesCompat.getColor(getResources(), R.color.colorPrimary, null)); - } - - setCertTextView(); - } - - private void setCertTextView() { - if (getActivity() != null) { - getActivity().runOnUiThread(() -> { - if (!TextUtils.isEmpty(appPreferences.getTemporaryClientCertAlias())) { - certTextView.setText(R.string.nc_change_cert_auth); - } else { - certTextView.setText(R.string.nc_configure_cert_auth); - } - - hideProgressBar(); - }); - } - } - - @Override - protected void onDestroyView(@NonNull View view) { - super.onDestroyView(view); - if (getActivity() != null) { - getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR); - } - } - - @Override - public void onDestroy() { - super.onDestroy(); - dispose(); - } - - private void dispose() { - if (statusQueryDisposable != null && !statusQueryDisposable.isDisposed()) { - statusQueryDisposable.dispose(); - } - - statusQueryDisposable = null; - } - - @Override - public AppBarLayoutType getAppBarLayoutType() { - return AppBarLayoutType.EMPTY; - } -} diff --git a/app/src/main/java/com/nextcloud/talk/controllers/ServerSelectionController.kt b/app/src/main/java/com/nextcloud/talk/controllers/ServerSelectionController.kt new file mode 100644 index 000000000..49cecbc27 --- /dev/null +++ b/app/src/main/java/com/nextcloud/talk/controllers/ServerSelectionController.kt @@ -0,0 +1,368 @@ +/* + * Nextcloud Talk application + * + * @author Andy Scherzinger + * @author Mario Danic + * Copyright (C) 2021 Andy Scherzinger (info@andy-scherzinger.de) + * Copyright (C) 2017 Mario Danic (mario@lovelyhq.com) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.nextcloud.talk.controllers + +import android.content.Intent +import android.content.pm.ActivityInfo +import android.net.Uri +import android.os.Bundle +import android.security.KeyChain +import android.text.TextUtils +import android.view.KeyEvent +import android.view.View +import android.view.inputmethod.EditorInfo +import android.widget.TextView +import androidx.core.content.res.ResourcesCompat +import autodagger.AutoInjector +import com.bluelinelabs.conductor.RouterTransaction +import com.bluelinelabs.conductor.changehandler.HorizontalChangeHandler +import com.nextcloud.talk.R +import com.nextcloud.talk.api.NcApi +import com.nextcloud.talk.application.NextcloudTalkApplication +import com.nextcloud.talk.application.NextcloudTalkApplication.Companion.sharedApplication +import com.nextcloud.talk.controllers.base.NewBaseController +import com.nextcloud.talk.controllers.util.viewBinding +import com.nextcloud.talk.databinding.ControllerServerSelectionBinding +import com.nextcloud.talk.models.database.UserEntity +import com.nextcloud.talk.models.json.generic.Status +import com.nextcloud.talk.utils.AccountUtils.findAccounts +import com.nextcloud.talk.utils.AccountUtils.getAppNameBasedOnPackage +import com.nextcloud.talk.utils.ApiUtils +import com.nextcloud.talk.utils.DisplayUtils +import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_IS_ACCOUNT_IMPORT +import com.nextcloud.talk.utils.database.user.UserUtils +import com.nextcloud.talk.utils.singletons.ApplicationWideMessageHolder +import io.reactivex.android.schedulers.AndroidSchedulers +import io.reactivex.disposables.Disposable +import io.reactivex.schedulers.Schedulers +import java.security.cert.CertificateException +import javax.inject.Inject + +@AutoInjector(NextcloudTalkApplication::class) +class ServerSelectionController : NewBaseController(R.layout.controller_server_selection) { + private val binding: ControllerServerSelectionBinding by viewBinding(ControllerServerSelectionBinding::bind) + + override val appBarLayoutType: AppBarLayoutType + get() = AppBarLayoutType.EMPTY + + @Inject + lateinit var ncApi: NcApi + + @Inject + lateinit var userUtils: UserUtils + + private var statusQueryDisposable: Disposable? = null + + fun onCertClick() { + if (activity != null) { + KeyChain.choosePrivateKeyAlias(activity!!, { alias: String? -> + if (alias != null) { + appPreferences!!.temporaryClientCertAlias = alias + } else { + appPreferences!!.removeTemporaryClientCertAlias() + } + setCertTextView() + }, arrayOf("RSA", "EC"), null, null, -1, null) + } + } + + override fun onViewBound(view: View) { + super.onViewBound(view) + sharedApplication!!.componentApplication.inject(this) + if (activity != null) { + activity!!.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT + } + + actionBar?.hide() + + binding.hostUrlInputHelperText.setText( + String.format( + resources!!.getString(R.string.nc_server_helper_text), + resources!!.getString(R.string.nc_server_product_name) + ) + ) + binding.serverEntryTextInputLayout.setEndIconOnClickListener { checkServerAndProceed() } + if (resources!!.getBoolean(R.bool.hide_auth_cert)) { + binding.certTextView.visibility = View.GONE + } + if (resources!!.getBoolean(R.bool.hide_provider) || + TextUtils.isEmpty(resources!!.getString(R.string.nc_providers_url)) && + TextUtils.isEmpty(resources!!.getString(R.string.nc_import_account_type)) + ) { + binding.helperTextView.visibility = View.INVISIBLE + } else { + if ( + (TextUtils.isEmpty(resources!!.getString(R.string.nc_import_account_type)) || + findAccounts(userUtils.users as List).isEmpty() + ) && + userUtils.users.size == 0 + ) { + binding.helperTextView.setText(R.string.nc_get_from_provider) + binding.helperTextView.setOnClickListener { + val browserIntent = Intent( + Intent.ACTION_VIEW, Uri.parse( + resources!! + .getString(R.string.nc_providers_url) + ) + ) + startActivity(browserIntent) + } + } else if (findAccounts(userUtils.users as List).size > 0) { + if (!TextUtils.isEmpty( + getAppNameBasedOnPackage(resources!!.getString(R.string.nc_import_accounts_from)) + ) + ) { + if (findAccounts(userUtils.users as List).size > 1) { + binding.helperTextView.setText( + String.format( + resources!!.getString(R.string.nc_server_import_accounts), + getAppNameBasedOnPackage(resources!!.getString(R.string.nc_import_accounts_from)) + ) + ) + } else { + binding.helperTextView.setText( + String.format( + resources!!.getString(R.string.nc_server_import_account), + getAppNameBasedOnPackage(resources!!.getString(R.string.nc_import_accounts_from)) + ) + ) + } + } else { + if (findAccounts(userUtils.users as List).size > 1) { + binding.helperTextView.text = resources!!.getString(R.string.nc_server_import_accounts_plain) + } else { + binding.helperTextView.text = resources!!.getString(R.string.nc_server_import_account_plain) + } + } + binding.helperTextView.setOnClickListener { + val bundle = Bundle() + bundle.putBoolean(KEY_IS_ACCOUNT_IMPORT, true) + router.pushController( + RouterTransaction.with( + SwitchAccountController(bundle) + ) + .pushChangeHandler(HorizontalChangeHandler()) + .popChangeHandler(HorizontalChangeHandler()) + ) + } + } else { + binding.helperTextView.visibility = View.INVISIBLE + } + } + binding.serverEntryTextInputEditText.requestFocus() + if (!TextUtils.isEmpty(resources!!.getString(R.string.weblogin_url))) { + binding.serverEntryTextInputEditText.setText(resources!!.getString(R.string.weblogin_url)) + checkServerAndProceed() + } + binding.serverEntryTextInputEditText.setOnEditorActionListener { _: TextView?, i: Int, _: KeyEvent? -> + if (i == EditorInfo.IME_ACTION_DONE) { + checkServerAndProceed() + } + false + } + binding.certTextView.setOnClickListener { onCertClick() } + } + + private fun checkServerAndProceed() { + dispose() + var url: String = binding.serverEntryTextInputEditText.text.toString().trim { it <= ' ' } + binding.serverEntryTextInputEditText.isEnabled = false + showserverEntryProgressBar() + if (binding.helperTextView.visibility != View.INVISIBLE) { + binding.helperTextView.visibility = View.INVISIBLE + binding.certTextView.visibility = View.INVISIBLE + } + if (url.endsWith("/")) { + url = url.substring(0, url.length - 1) + } + val queryUrl = url + ApiUtils.getUrlPostfixForStatus() + if (url.startsWith("http://") || url.startsWith("https://")) { + checkServer(queryUrl, false) + } else { + checkServer("https://$queryUrl", true) + } + } + + private fun checkServer(queryUrl: String, checkForcedHttps: Boolean) { + statusQueryDisposable = ncApi.getServerStatus(queryUrl) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ status: Status -> + val productName = resources!!.getString(R.string.nc_server_product_name) + val versionString: String = status.getVersion().substring(0, status.getVersion().indexOf(".")) + val version: Int = versionString.toInt() + if (status.isInstalled && !status.isMaintenance && + !status.isNeedsUpgrade && version >= 13 + ) { + router.pushController( + RouterTransaction.with( + WebViewLoginController( + queryUrl.replace("/status.php", ""), + false + ) + ) + .pushChangeHandler(HorizontalChangeHandler()) + .popChangeHandler(HorizontalChangeHandler()) + ) + } else if (!status.isInstalled) { + setErrorText( + String.format( + resources!!.getString(R.string.nc_server_not_installed), productName + ) + ) + } else if (status.isNeedsUpgrade) { + setErrorText( + String.format( + resources!!.getString(R.string.nc_server_db_upgrade_needed), + productName + ) + ) + } else if (status.isMaintenance) { + setErrorText( + String.format( + resources!!.getString(R.string.nc_server_maintenance), + productName + ) + ) + } else if (!status.getVersion().startsWith("13.")) { + setErrorText( + String.format( + resources!!.getString(R.string.nc_server_version), + resources!!.getString(R.string.nc_app_product_name), + productName + ) + ) + } + }, { throwable: Throwable -> + if (checkForcedHttps) { + checkServer(queryUrl.replace("https://", "http://"), false) + } else { + if (throwable.localizedMessage != null) { + setErrorText(throwable.localizedMessage) + } else if (throwable.cause is CertificateException) { + setErrorText(resources!!.getString(R.string.nc_certificate_error)) + } else { + hideserverEntryProgressBar() + } + + binding.serverEntryTextInputEditText.isEnabled = true + + if (binding.helperTextView.visibility != View.INVISIBLE) { + binding.helperTextView.visibility = View.VISIBLE + binding.certTextView.visibility = View.VISIBLE + } + dispose() + } + }) { + hideserverEntryProgressBar() + if (binding.helperTextView.visibility != View.INVISIBLE) { + binding.helperTextView.visibility = View.VISIBLE + binding.certTextView.visibility = View.VISIBLE + } + dispose() + } + } + + private fun setErrorText(text: String) { + binding.errorText.text = text + binding.errorText.visibility = View.VISIBLE + binding.serverEntryProgressBar.visibility = View.GONE + } + + private fun showserverEntryProgressBar() { + binding.errorText.visibility = View.GONE + binding.serverEntryProgressBar.visibility = View.VISIBLE + } + + private fun hideserverEntryProgressBar() { + binding.errorText.visibility = View.GONE + binding.serverEntryProgressBar.visibility = View.INVISIBLE + } + + override fun onAttach(view: View) { + super.onAttach(view) + if (ApplicationWideMessageHolder.getInstance().messageType != null) { + if (ApplicationWideMessageHolder.getInstance().messageType + == ApplicationWideMessageHolder.MessageType.ACCOUNT_SCHEDULED_FOR_DELETION + ) { + setErrorText(resources!!.getString(R.string.nc_account_scheduled_for_deletion)) + ApplicationWideMessageHolder.getInstance().messageType = null + } else if (ApplicationWideMessageHolder.getInstance().messageType + == ApplicationWideMessageHolder.MessageType.SERVER_WITHOUT_TALK + ) { + setErrorText(resources!!.getString(R.string.nc_settings_no_talk_installed)) + } else if (ApplicationWideMessageHolder.getInstance().messageType + == ApplicationWideMessageHolder.MessageType.FAILED_TO_IMPORT_ACCOUNT + ) { + setErrorText(resources!!.getString(R.string.nc_server_failed_to_import_account)) + } + ApplicationWideMessageHolder.getInstance().messageType = null + } + if (activity != null && resources != null) { + DisplayUtils.applyColorToStatusBar( + activity, + ResourcesCompat.getColor(resources!!, R.color.colorPrimary, null) + ) + DisplayUtils.applyColorToNavigationBar( + activity!!.window, + ResourcesCompat.getColor(resources!!, R.color.colorPrimary, null) + ) + } + setCertTextView() + } + + private fun setCertTextView() { + if (activity != null) { + activity!!.runOnUiThread { + if (!TextUtils.isEmpty(appPreferences!!.temporaryClientCertAlias)) { + binding.certTextView.setText(R.string.nc_change_cert_auth) + } else { + binding.certTextView.setText(R.string.nc_configure_cert_auth) + } + hideserverEntryProgressBar() + } + } + } + + override fun onDestroyView(view: View) { + super.onDestroyView(view) + if (activity != null) { + activity!!.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR + } + } + + public override fun onDestroy() { + super.onDestroy() + dispose() + } + + private fun dispose() { + if (statusQueryDisposable != null && !statusQueryDisposable!!.isDisposed) { + statusQueryDisposable!!.dispose() + } + statusQueryDisposable = null + } + + companion object { + const val TAG = "ServerSelectionController" + } +} diff --git a/app/src/main/java/com/nextcloud/talk/controllers/SwitchAccountController.java b/app/src/main/java/com/nextcloud/talk/controllers/SwitchAccountController.java deleted file mode 100644 index 4620a2680..000000000 --- a/app/src/main/java/com/nextcloud/talk/controllers/SwitchAccountController.java +++ /dev/null @@ -1,249 +0,0 @@ -/* - * Nextcloud Talk application - * - * @author Mario Danic - * Copyright (C) 2017 Mario Danic - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - * Parts related to account import were either copied from or inspired by the great work done by David Luhmer at: - * https://github.com/nextcloud/ownCloud-Account-Importer - */ - -package com.nextcloud.talk.controllers; - -import android.accounts.Account; -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.MenuItem; -import android.view.View; -import android.view.ViewGroup; -import androidx.annotation.NonNull; -import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; -import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; -import autodagger.AutoInjector; -import butterknife.BindView; -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.application.NextcloudTalkApplication; -import com.nextcloud.talk.controllers.base.BaseController; -import com.nextcloud.talk.models.ImportAccount; -import com.nextcloud.talk.models.database.UserEntity; -import com.nextcloud.talk.models.json.participants.Participant; -import com.nextcloud.talk.utils.AccountUtils; -import com.nextcloud.talk.utils.bundle.BundleKeys; -import com.nextcloud.talk.utils.database.user.UserUtils; -import eu.davidea.flexibleadapter.FlexibleAdapter; -import eu.davidea.flexibleadapter.common.SmoothScrollLinearLayoutManager; -import eu.davidea.flexibleadapter.items.AbstractFlexibleItem; -import io.reactivex.Observer; -import io.reactivex.disposables.Disposable; - -import javax.inject.Inject; -import java.net.CookieManager; -import java.util.ArrayList; -import java.util.List; - -@AutoInjector(NextcloudTalkApplication.class) -public class SwitchAccountController extends BaseController { - - @Inject - UserUtils userUtils; - - @BindView(R.id.recycler_view) - RecyclerView recyclerView; - - @Inject - CookieManager cookieManager; - - @BindView(R.id.swipe_refresh_layout) - SwipeRefreshLayout swipeRefreshLayout; - private FlexibleAdapter adapter; - private List userItems = new ArrayList<>(); - - private boolean isAccountImport = false; - - private FlexibleAdapter.OnItemClickListener onImportItemClickListener = new FlexibleAdapter.OnItemClickListener() { - @Override - public boolean onItemClick(View view, int position) { - if (userItems.size() > position) { - Account account = ((AdvancedUserItem) userItems.get(position)).getAccount(); - reauthorizeFromImport(account); - } - - return true; - } - }; - - private FlexibleAdapter.OnItemClickListener onSwitchItemClickListener = new FlexibleAdapter.OnItemClickListener() { - @Override - public boolean onItemClick(View view, int position) { - if (userItems.size() > position) { - UserEntity userEntity = ((AdvancedUserItem) userItems.get(position)).getEntity(); - userUtils.createOrUpdateUser(null, - null, null, null, - null, Boolean.TRUE, null, userEntity.getId(), null, null, null) - .subscribe(new Observer() { - @Override - public void onSubscribe(Disposable d) { - - } - - @Override - public void onNext(UserEntity userEntity) { - cookieManager.getCookieStore().removeAll(); - - userUtils.disableAllUsersWithoutId(userEntity.getId()); - if (getActivity() != null) { - getActivity().runOnUiThread(() -> getRouter().popCurrentController()); - } - } - - @Override - public void onError(Throwable e) { - - } - - @Override - public void onComplete() { - - } - }); - } - - return true; - } - }; - - public SwitchAccountController() { - setHasOptionsMenu(true); - } - - public SwitchAccountController(Bundle args) { - super(args); - setHasOptionsMenu(true); - - if (args.containsKey(BundleKeys.INSTANCE.getKEY_IS_ACCOUNT_IMPORT())) { - isAccountImport = true; - } - } - - @Override - public boolean onOptionsItemSelected(@NonNull MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - getRouter().popCurrentController(); - return true; - default: - return super.onOptionsItemSelected(item); - } - } - - @Override - protected View inflateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) { - return inflater.inflate(R.layout.controller_generic_rv, container, false); - } - - @Override - protected void onViewBound(@NonNull View view) { - super.onViewBound(view); - NextcloudTalkApplication.Companion.getSharedApplication().getComponentApplication().inject(this); - swipeRefreshLayout.setEnabled(false); - - if (getActionBar() != null) { - getActionBar().show(); - } - - if (adapter == null) { - adapter = new FlexibleAdapter<>(userItems, getActivity(), false); - - UserEntity userEntity; - Participant participant; - - if (!isAccountImport) { - for (Object userEntityObject : userUtils.getUsers()) { - userEntity = (UserEntity) userEntityObject; - if (!userEntity.getCurrent()) { - String userId; - if (userEntity.getUserId() != null) { - userId = userEntity.getUserId(); - } else { - userId = userEntity.getUsername(); - } - - participant = new Participant(); - participant.setActorType(Participant.ActorType.USERS); - participant.setActorId(userId); - participant.setDisplayName(userEntity.getDisplayName()); - userItems.add(new AdvancedUserItem(participant, userEntity, null)); - } - } - - adapter.addListener(onSwitchItemClickListener); - adapter.updateDataSet(userItems, false); - } else { - Account account; - ImportAccount importAccount; - for (Object accountObject : AccountUtils.INSTANCE.findAccounts(userUtils.getUsers())) { - account = (Account) accountObject; - importAccount = AccountUtils.INSTANCE.getInformationFromAccount(account); - - participant = new Participant(); - participant.setActorType(Participant.ActorType.USERS); - participant.setActorId(importAccount.getUsername()); - participant.setDisplayName(importAccount.getUsername()); - userEntity = new UserEntity(); - userEntity.setBaseUrl(importAccount.getBaseUrl()); - userItems.add(new AdvancedUserItem(participant, userEntity, account)); - } - - adapter.addListener(onImportItemClickListener); - adapter.updateDataSet(userItems, false); - } - - } - - prepareViews(); - } - - - private void prepareViews() { - LinearLayoutManager layoutManager = new SmoothScrollLinearLayoutManager(getActivity()); - recyclerView.setLayoutManager(layoutManager); - recyclerView.setHasFixedSize(true); - recyclerView.setAdapter(adapter); - - swipeRefreshLayout.setEnabled(false); - } - - private void reauthorizeFromImport(Account account) { - ImportAccount importAccount = AccountUtils.INSTANCE.getInformationFromAccount(account); - Bundle bundle = new Bundle(); - bundle.putString(BundleKeys.INSTANCE.getKEY_BASE_URL(), importAccount.getBaseUrl()); - bundle.putString(BundleKeys.INSTANCE.getKEY_USERNAME(), importAccount.getUsername()); - bundle.putString(BundleKeys.INSTANCE.getKEY_TOKEN(), importAccount.getToken()); - bundle.putBoolean(BundleKeys.INSTANCE.getKEY_IS_ACCOUNT_IMPORT(), true); - getRouter().pushController(RouterTransaction.with(new AccountVerificationController(bundle)) - .pushChangeHandler(new HorizontalChangeHandler()) - .popChangeHandler(new HorizontalChangeHandler())); - } - - @Override - protected String getTitle() { - return getResources().getString(R.string.nc_select_an_account); - } -} diff --git a/app/src/main/java/com/nextcloud/talk/controllers/SwitchAccountController.kt b/app/src/main/java/com/nextcloud/talk/controllers/SwitchAccountController.kt new file mode 100644 index 000000000..d613ce8cc --- /dev/null +++ b/app/src/main/java/com/nextcloud/talk/controllers/SwitchAccountController.kt @@ -0,0 +1,207 @@ +/* + * Nextcloud Talk application + * + * @author Mario Danic + * @author Andy Scherzinger + * Copyright (C) 2022 Andy Scherzinger (info@andy-scherzinger.de) + * Copyright (C) 2017 Mario Danic + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * Parts related to account import were either copied from or inspired by the great work done by David Luhmer at: + * https://github.com/nextcloud/ownCloud-Account-Importer + */ +package com.nextcloud.talk.controllers + +import android.accounts.Account +import android.os.Bundle +import android.view.MenuItem +import android.view.View +import androidx.preference.PreferenceManager +import androidx.recyclerview.widget.LinearLayoutManager +import autodagger.AutoInjector +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.application.NextcloudTalkApplication +import com.nextcloud.talk.application.NextcloudTalkApplication.Companion.sharedApplication +import com.nextcloud.talk.controllers.base.NewBaseController +import com.nextcloud.talk.controllers.util.viewBinding +import com.nextcloud.talk.databinding.ControllerGenericRvBinding +import com.nextcloud.talk.models.ImportAccount +import com.nextcloud.talk.models.database.UserEntity +import com.nextcloud.talk.models.json.participants.Participant +import com.nextcloud.talk.utils.AccountUtils.findAccounts +import com.nextcloud.talk.utils.AccountUtils.getInformationFromAccount +import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_BASE_URL +import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_IS_ACCOUNT_IMPORT +import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_TOKEN +import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_USERNAME +import com.nextcloud.talk.utils.database.user.UserUtils +import eu.davidea.flexibleadapter.FlexibleAdapter +import eu.davidea.flexibleadapter.common.SmoothScrollLinearLayoutManager +import eu.davidea.flexibleadapter.items.AbstractFlexibleItem +import io.reactivex.Observer +import io.reactivex.disposables.Disposable +import org.osmdroid.config.Configuration +import java.net.CookieManager +import java.util.ArrayList +import javax.inject.Inject + +@AutoInjector(NextcloudTalkApplication::class) +class SwitchAccountController(args: Bundle? = null) : + NewBaseController( + R.layout.controller_generic_rv, + args + ) { + private val binding: ControllerGenericRvBinding by viewBinding(ControllerGenericRvBinding::bind) + + @Inject + lateinit var userUtils: UserUtils + + @Inject + lateinit var cookieManager: CookieManager + + private var adapter: FlexibleAdapter>? = null + private val userItems: MutableList> = ArrayList() + private var isAccountImport = false + + private val onImportItemClickListener = FlexibleAdapter.OnItemClickListener { _, position -> + if (userItems.size > position) { + val account = (userItems[position] as AdvancedUserItem).account + reauthorizeFromImport(account) + } + true + } + private val onSwitchItemClickListener = FlexibleAdapter.OnItemClickListener { _, position -> + if (userItems.size > position) { + val userEntity = (userItems[position] as AdvancedUserItem).entity + userUtils.createOrUpdateUser( + null, + null, null, null, + null, java.lang.Boolean.TRUE, null, userEntity.id, null, null, null + ) + .subscribe(object : Observer { + override fun onSubscribe(d: Disposable) {} + override fun onNext(userEntity: UserEntity) { + cookieManager.cookieStore.removeAll() + userUtils.disableAllUsersWithoutId(userEntity.id) + if (activity != null) { + activity!!.runOnUiThread { router.popCurrentController() } + } + } + + override fun onError(e: Throwable) {} + override fun onComplete() {} + }) + } + true + } + + init { + setHasOptionsMenu(true) + sharedApplication!!.componentApplication.inject(this) + Configuration.getInstance().load(context, PreferenceManager.getDefaultSharedPreferences(context)) + if (args?.containsKey(KEY_IS_ACCOUNT_IMPORT) == true) { + isAccountImport = true + } + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + return when (item.itemId) { + android.R.id.home -> { + router.popCurrentController() + true + } + else -> super.onOptionsItemSelected(item) + } + } + + override fun onViewBound(view: View) { + super.onViewBound(view) + binding.swipeRefreshLayout.isEnabled = false + + actionBar?.show() + + if (adapter == null) { + adapter = FlexibleAdapter(userItems, activity, false) + var userEntity: UserEntity? + var participant: Participant + if (!isAccountImport) { + for (userEntityObject in userUtils.users) { + userEntity = userEntityObject as UserEntity? + if (!userEntity!!.current) { + var userId: String? + userId = if (userEntity.userId != null) { + userEntity.userId + } else { + userEntity.username + } + participant = Participant() + participant.setActorType(Participant.ActorType.USERS) + participant.setActorId(userId) + participant.setDisplayName(userEntity.displayName) + userItems.add(AdvancedUserItem(participant, userEntity, null)) + } + } + adapter!!.addListener(onSwitchItemClickListener) + adapter!!.updateDataSet(userItems, false) + } else { + var account: Account + var importAccount: ImportAccount + for (accountObject in findAccounts(userUtils.users as List)) { + account = accountObject + importAccount = getInformationFromAccount(account) + participant = Participant() + participant.setActorType(Participant.ActorType.USERS) + participant.setActorId(importAccount.getUsername()) + participant.setDisplayName(importAccount.getUsername()) + userEntity = UserEntity() + userEntity.baseUrl = importAccount.getBaseUrl() + userItems.add(AdvancedUserItem(participant, userEntity, account)) + } + adapter!!.addListener(onImportItemClickListener) + adapter!!.updateDataSet(userItems, false) + } + } + prepareViews() + } + + private fun prepareViews() { + val layoutManager: LinearLayoutManager = SmoothScrollLinearLayoutManager(activity) + binding.recyclerView.layoutManager = layoutManager + binding.recyclerView.setHasFixedSize(true) + binding.recyclerView.adapter = adapter + binding.swipeRefreshLayout.isEnabled = false + } + + private fun reauthorizeFromImport(account: Account?) { + val importAccount = getInformationFromAccount(account!!) + val bundle = Bundle() + bundle.putString(KEY_BASE_URL, importAccount.getBaseUrl()) + bundle.putString(KEY_USERNAME, importAccount.getUsername()) + bundle.putString(KEY_TOKEN, importAccount.getToken()) + bundle.putBoolean(KEY_IS_ACCOUNT_IMPORT, true) + router.pushController( + RouterTransaction.with(AccountVerificationController(bundle)) + .pushChangeHandler(HorizontalChangeHandler()) + .popChangeHandler(HorizontalChangeHandler()) + ) + } + + override val title: String + get() = + resources!!.getString(R.string.nc_select_an_account) +} diff --git a/scripts/analysis/findbugs-results.txt b/scripts/analysis/findbugs-results.txt index 28621d35a..db3a73389 100644 --- a/scripts/analysis/findbugs-results.txt +++ b/scripts/analysis/findbugs-results.txt @@ -1 +1 @@ -542 \ No newline at end of file +536 \ No newline at end of file diff --git a/scripts/analysis/lint-results.txt b/scripts/analysis/lint-results.txt index 437343559..909332510 100644 --- a/scripts/analysis/lint-results.txt +++ b/scripts/analysis/lint-results.txt @@ -1,2 +1,2 @@ DO NOT TOUCH; GENERATED BY DRONE - Lint Report: 1 error and 218 warnings + Lint Report: 1 error and 208 warnings