ServerSelectionController.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * Nextcloud Talk application
  3. *
  4. * @author Mario Danic
  5. * Copyright (C) 2017 Mario Danic (mario@lovelyhq.com)
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.nextcloud.talk.controllers;
  21. import android.annotation.SuppressLint;
  22. import android.content.Intent;
  23. import android.content.pm.ActivityInfo;
  24. import android.net.Uri;
  25. import android.os.Bundle;
  26. import android.security.KeyChain;
  27. import androidx.annotation.NonNull;
  28. import android.text.Editable;
  29. import android.text.TextUtils;
  30. import android.text.TextWatcher;
  31. import android.view.LayoutInflater;
  32. import android.view.View;
  33. import android.view.ViewGroup;
  34. import android.view.inputmethod.EditorInfo;
  35. import android.widget.ProgressBar;
  36. import android.widget.TextView;
  37. import com.bluelinelabs.conductor.RouterTransaction;
  38. import com.bluelinelabs.conductor.changehandler.HorizontalChangeHandler;
  39. import com.nextcloud.talk.R;
  40. import com.nextcloud.talk.api.NcApi;
  41. import com.nextcloud.talk.application.NextcloudTalkApplication;
  42. import com.nextcloud.talk.controllers.base.BaseController;
  43. import com.nextcloud.talk.utils.AccountUtils;
  44. import com.nextcloud.talk.utils.ApiUtils;
  45. import com.nextcloud.talk.utils.singletons.ApplicationWideMessageHolder;
  46. import com.nextcloud.talk.utils.bundle.BundleKeys;
  47. import com.nextcloud.talk.utils.database.user.UserUtils;
  48. import com.nextcloud.talk.utils.preferences.AppPreferences;
  49. import java.security.cert.CertificateException;
  50. import javax.inject.Inject;
  51. import autodagger.AutoInjector;
  52. import butterknife.BindView;
  53. import butterknife.OnClick;
  54. import io.reactivex.android.schedulers.AndroidSchedulers;
  55. import io.reactivex.disposables.Disposable;
  56. import io.reactivex.schedulers.Schedulers;
  57. import studio.carbonylgroup.textfieldboxes.ExtendedEditText;
  58. import studio.carbonylgroup.textfieldboxes.TextFieldBoxes;
  59. @AutoInjector(NextcloudTalkApplication.class)
  60. public class ServerSelectionController extends BaseController {
  61. public static final String TAG = "ServerSelectionController";
  62. @BindView(R.id.extended_edit_text)
  63. ExtendedEditText serverEntry;
  64. @BindView(R.id.text_field_boxes)
  65. TextFieldBoxes textFieldBoxes;
  66. @BindView(R.id.progress_bar)
  67. ProgressBar progressBar;
  68. @BindView(R.id.helper_text_view)
  69. TextView providersTextView;
  70. @BindView(R.id.cert_text_view)
  71. TextView certTextView;
  72. @Inject
  73. NcApi ncApi;
  74. @Inject
  75. UserUtils userUtils;
  76. @Inject
  77. AppPreferences appPreferences;
  78. private Disposable statusQueryDisposable;
  79. @Override
  80. protected View inflateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) {
  81. return inflater.inflate(R.layout.controller_server_selection, container, false);
  82. }
  83. @SuppressLint("LongLogTag")
  84. @OnClick(R.id.cert_text_view)
  85. public void onCertClick() {
  86. if (getActivity() != null) {
  87. KeyChain.choosePrivateKeyAlias(getActivity(), alias -> {
  88. if (alias != null) {
  89. appPreferences.setTemporaryClientCertAlias(alias);
  90. } else {
  91. appPreferences.removeTemporaryClientCertAlias();
  92. }
  93. setCertTextView();
  94. }, new String[]{"RSA", "EC"}, null, null, -1, null);
  95. }
  96. }
  97. @Override
  98. protected void onViewBound(@NonNull View view) {
  99. super.onViewBound(view);
  100. NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
  101. if (getActivity() != null) {
  102. getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  103. }
  104. if (getActionBar() != null) {
  105. getActionBar().hide();
  106. }
  107. textFieldBoxes.getEndIconImageButton().setBackgroundDrawable(getResources().getDrawable(R.drawable
  108. .ic_arrow_forward_white_24px));
  109. textFieldBoxes.getEndIconImageButton().setAlpha(0.5f);
  110. textFieldBoxes.getEndIconImageButton().setEnabled(false);
  111. textFieldBoxes.getEndIconImageButton().setVisibility(View.VISIBLE);
  112. textFieldBoxes.getEndIconImageButton().setOnClickListener(view1 -> checkServerAndProceed());
  113. if (TextUtils.isEmpty(getResources().getString(R.string.nc_providers_url)) && (TextUtils.isEmpty(getResources
  114. ().getString(R.string.nc_import_account_type)))) {
  115. providersTextView.setVisibility(View.INVISIBLE);
  116. } else {
  117. if ((TextUtils.isEmpty(getResources
  118. ().getString(R.string.nc_import_account_type)) ||
  119. AccountUtils.findAccounts(userUtils.getUsers()).size() == 0) &&
  120. userUtils.getUsers().size() == 0) {
  121. providersTextView.setText(R.string.nc_get_from_provider);
  122. providersTextView.setOnClickListener(view12 -> {
  123. Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getResources()
  124. .getString(R.string.nc_providers_url)));
  125. startActivity(browserIntent);
  126. });
  127. } else if (AccountUtils.findAccounts(userUtils.getUsers()).size() > 0) {
  128. if (!TextUtils.isEmpty(AccountUtils.getAppNameBasedOnPackage(getResources()
  129. .getString(R.string.nc_import_accounts_from)))) {
  130. if (AccountUtils.findAccounts(userUtils.getUsers()).size() > 1) {
  131. providersTextView.setText(String.format(getResources().getString(R.string
  132. .nc_server_import_accounts), AccountUtils.getAppNameBasedOnPackage(getResources()
  133. .getString(R.string.nc_import_accounts_from))));
  134. } else {
  135. providersTextView.setText(String.format(getResources().getString(R.string
  136. .nc_server_import_account), AccountUtils.getAppNameBasedOnPackage(getResources()
  137. .getString(R.string.nc_import_accounts_from))));
  138. }
  139. } else {
  140. if (AccountUtils.findAccounts(userUtils.getUsers()).size() > 1) {
  141. providersTextView.setText(getResources().getString(R.string.nc_server_import_accounts_plain));
  142. } else {
  143. providersTextView.setText(getResources().getString(R.string
  144. .nc_server_import_account_plain));
  145. }
  146. }
  147. providersTextView.setOnClickListener(view13 -> {
  148. Bundle bundle = new Bundle();
  149. bundle.putBoolean(BundleKeys.KEY_IS_ACCOUNT_IMPORT, true);
  150. getRouter().pushController(RouterTransaction.with(
  151. new SwitchAccountController(bundle))
  152. .pushChangeHandler(new HorizontalChangeHandler())
  153. .popChangeHandler(new HorizontalChangeHandler()));
  154. });
  155. } else {
  156. providersTextView.setVisibility(View.INVISIBLE);
  157. }
  158. }
  159. serverEntry.requestFocus();
  160. serverEntry.addTextChangedListener(new TextWatcher() {
  161. @Override
  162. public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  163. }
  164. @Override
  165. public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  166. }
  167. @Override
  168. public void afterTextChanged(Editable editable) {
  169. if (!textFieldBoxes.isOnError() && !TextUtils.isEmpty(serverEntry.getText())) {
  170. toggleProceedButton(true);
  171. } else {
  172. toggleProceedButton(false);
  173. }
  174. }
  175. });
  176. serverEntry.setOnEditorActionListener((textView, i, keyEvent) -> {
  177. if (i == EditorInfo.IME_ACTION_DONE) {
  178. checkServerAndProceed();
  179. }
  180. return false;
  181. });
  182. }
  183. private void toggleProceedButton(boolean show) {
  184. textFieldBoxes.getEndIconImageButton().setEnabled(show);
  185. if (show) {
  186. textFieldBoxes.getEndIconImageButton().setAlpha(1f);
  187. } else {
  188. textFieldBoxes.getEndIconImageButton().setAlpha(0.5f);
  189. }
  190. }
  191. private void checkServerAndProceed() {
  192. dispose();
  193. String url = serverEntry.getText().toString().trim();
  194. serverEntry.setEnabled(false);
  195. progressBar.setVisibility(View.VISIBLE);
  196. if (providersTextView.getVisibility() != View.INVISIBLE) {
  197. providersTextView.setVisibility(View.INVISIBLE);
  198. certTextView.setVisibility(View.INVISIBLE);
  199. }
  200. if (url.endsWith("/")) {
  201. url = url.substring(0, url.length() - 1);
  202. }
  203. String queryUrl = url + ApiUtils.getUrlPostfixForStatus();
  204. if (url.startsWith("http://") || url.startsWith("https://")) {
  205. checkServer(queryUrl, false);
  206. } else {
  207. checkServer("https://" + queryUrl, true);
  208. }
  209. }
  210. private void checkServer(String queryUrl, boolean checkForcedHttps) {
  211. statusQueryDisposable = ncApi.getServerStatus(queryUrl)
  212. .subscribeOn(Schedulers.newThread())
  213. .observeOn(AndroidSchedulers.mainThread())
  214. .subscribe(status -> {
  215. String productName = getResources().getString(R.string.nc_server_product_name);
  216. String versionString = status.getVersion().substring(0, status.getVersion().indexOf("."));
  217. int version = Integer.parseInt(versionString);
  218. if (status.isInstalled() && !status.isMaintenance() &&
  219. !status.isNeedsUpgrade() &&
  220. version >= 13) {
  221. getRouter().pushController(RouterTransaction.with(
  222. new WebViewLoginController(queryUrl.replace("/status.php", ""),
  223. false))
  224. .pushChangeHandler(new HorizontalChangeHandler())
  225. .popChangeHandler(new HorizontalChangeHandler()));
  226. } else if (!status.isInstalled()) {
  227. textFieldBoxes.setError(String.format(
  228. getResources().getString(R.string.nc_server_not_installed), productName),
  229. true);
  230. toggleProceedButton(false);
  231. } else if (status.isNeedsUpgrade()) {
  232. textFieldBoxes.setError(String.format(getResources().
  233. getString(R.string.nc_server_db_upgrade_needed),
  234. productName), true);
  235. toggleProceedButton(false);
  236. } else if (status.isMaintenance()) {
  237. textFieldBoxes.setError(String.format(getResources().
  238. getString(R.string.nc_server_maintenance),
  239. productName),
  240. true);
  241. toggleProceedButton(false);
  242. } else if (!status.getVersion().startsWith("13.")) {
  243. textFieldBoxes.setError(String.format(getResources().
  244. getString(R.string.nc_server_version),
  245. getResources().getString(R.string.nc_app_name)
  246. , productName), true);
  247. toggleProceedButton(false);
  248. }
  249. }, throwable -> {
  250. if (checkForcedHttps) {
  251. checkServer(queryUrl.replace("https://", "http://"), false);
  252. } else {
  253. if (throwable.getLocalizedMessage() != null) {
  254. textFieldBoxes.setError(throwable.getLocalizedMessage(), true);
  255. } else if (throwable.getCause() instanceof CertificateException) {
  256. textFieldBoxes.setError(getResources().getString(R.string.nc_certificate_error),
  257. false);
  258. }
  259. if (serverEntry != null) {
  260. serverEntry.setEnabled(true);
  261. }
  262. progressBar.setVisibility(View.INVISIBLE);
  263. if (providersTextView.getVisibility() != View.INVISIBLE) {
  264. providersTextView.setVisibility(View.VISIBLE);
  265. certTextView.setVisibility(View.VISIBLE);
  266. }
  267. toggleProceedButton(false);
  268. dispose();
  269. }
  270. }, () -> {
  271. progressBar.setVisibility(View.INVISIBLE);
  272. if (providersTextView.getVisibility() != View.INVISIBLE) {
  273. providersTextView.setVisibility(View.VISIBLE);
  274. certTextView.setVisibility(View.VISIBLE);
  275. }
  276. dispose();
  277. });
  278. }
  279. @Override
  280. protected void onAttach(@NonNull View view) {
  281. super.onAttach(view);
  282. if (ApplicationWideMessageHolder.getInstance().getMessageType() != null) {
  283. if (ApplicationWideMessageHolder.getInstance().getMessageType()
  284. .equals(ApplicationWideMessageHolder.MessageType.ACCOUNT_SCHEDULED_FOR_DELETION)) {
  285. textFieldBoxes.setError(getResources().getString(R.string.nc_account_scheduled_for_deletion),
  286. false);
  287. ApplicationWideMessageHolder.getInstance().setMessageType(null);
  288. } else if (ApplicationWideMessageHolder.getInstance().getMessageType()
  289. .equals(ApplicationWideMessageHolder.MessageType.SERVER_WITHOUT_TALK)) {
  290. textFieldBoxes.setError(getResources().getString(R.string.nc_settings_no_talk_installed),
  291. false);
  292. } else if (ApplicationWideMessageHolder.getInstance().getMessageType()
  293. .equals(ApplicationWideMessageHolder.MessageType.FAILED_TO_IMPORT_ACCOUNT)) {
  294. textFieldBoxes.setError(getResources().getString(R.string.nc_server_failed_to_import_account),
  295. false);
  296. }
  297. ApplicationWideMessageHolder.getInstance().setMessageType(null);
  298. }
  299. setCertTextView();
  300. }
  301. private void setCertTextView() {
  302. if (getActivity() != null) {
  303. getActivity().runOnUiThread(() -> {
  304. if (!TextUtils.isEmpty(appPreferences.getTemporaryClientCertAlias())) {
  305. certTextView.setText(R.string.nc_change_cert_auth);
  306. } else {
  307. certTextView.setText(R.string.nc_configure_cert_auth);
  308. }
  309. textFieldBoxes.setError("", true);
  310. toggleProceedButton(true);
  311. });
  312. }
  313. }
  314. @Override
  315. protected void onDestroyView(@NonNull View view) {
  316. super.onDestroyView(view);
  317. if (getActivity() != null) {
  318. getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
  319. }
  320. }
  321. @Override
  322. public void onDestroy() {
  323. super.onDestroy();
  324. dispose();
  325. }
  326. private void dispose() {
  327. if (statusQueryDisposable != null && !statusQueryDisposable.isDisposed()) {
  328. statusQueryDisposable.dispose();
  329. }
  330. statusQueryDisposable = null;
  331. }
  332. }