ServerSelectionController.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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.content.Intent;
  22. import android.content.pm.ActivityInfo;
  23. import android.net.Uri;
  24. import android.os.Bundle;
  25. import android.support.annotation.NonNull;
  26. import android.text.Editable;
  27. import android.text.TextUtils;
  28. import android.text.TextWatcher;
  29. import android.view.LayoutInflater;
  30. import android.view.View;
  31. import android.view.ViewGroup;
  32. import android.view.inputmethod.EditorInfo;
  33. import android.widget.ProgressBar;
  34. import android.widget.TextView;
  35. import com.bluelinelabs.conductor.RouterTransaction;
  36. import com.bluelinelabs.conductor.changehandler.HorizontalChangeHandler;
  37. import com.nextcloud.talk.R;
  38. import com.nextcloud.talk.api.NcApi;
  39. import com.nextcloud.talk.api.helpers.api.ApiHelper;
  40. import com.nextcloud.talk.application.NextcloudTalkApplication;
  41. import com.nextcloud.talk.controllers.base.BaseController;
  42. import com.nextcloud.talk.utils.AccountUtils;
  43. import com.nextcloud.talk.utils.ErrorMessageHolder;
  44. import com.nextcloud.talk.utils.bundle.BundleKeys;
  45. import com.nextcloud.talk.utils.database.user.UserUtils;
  46. import java.security.cert.CertificateException;
  47. import javax.inject.Inject;
  48. import autodagger.AutoInjector;
  49. import butterknife.BindView;
  50. import io.reactivex.android.schedulers.AndroidSchedulers;
  51. import io.reactivex.disposables.Disposable;
  52. import io.reactivex.schedulers.Schedulers;
  53. import studio.carbonylgroup.textfieldboxes.ExtendedEditText;
  54. import studio.carbonylgroup.textfieldboxes.TextFieldBoxes;
  55. @AutoInjector(NextcloudTalkApplication.class)
  56. public class ServerSelectionController extends BaseController {
  57. public static final String TAG = "ServerSelectionController";
  58. @BindView(R.id.extended_edit_text)
  59. ExtendedEditText serverEntry;
  60. @BindView(R.id.text_field_boxes)
  61. TextFieldBoxes textFieldBoxes;
  62. @BindView(R.id.progress_bar)
  63. ProgressBar progressBar;
  64. @BindView(R.id.helper_text_view)
  65. TextView providersTextView;
  66. @Inject
  67. NcApi ncApi;
  68. @Inject
  69. UserUtils userUtils;
  70. private Disposable statusQueryDisposable;
  71. @Override
  72. protected View inflateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) {
  73. return inflater.inflate(R.layout.controller_server_selection, container, false);
  74. }
  75. @Override
  76. protected void onViewBound(@NonNull View view) {
  77. super.onViewBound(view);
  78. NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
  79. if (getActivity() != null) {
  80. getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  81. }
  82. if (getActionBar() != null) {
  83. getActionBar().hide();
  84. }
  85. textFieldBoxes.getEndIconImageButton().setBackgroundDrawable(getResources().getDrawable(R.drawable
  86. .ic_arrow_forward_white_24px));
  87. textFieldBoxes.getEndIconImageButton().setAlpha(0.5f);
  88. textFieldBoxes.getEndIconImageButton().setEnabled(false);
  89. textFieldBoxes.getEndIconImageButton().setVisibility(View.VISIBLE);
  90. textFieldBoxes.getEndIconImageButton().setOnClickListener(view1 -> checkServerAndProceed());
  91. if (TextUtils.isEmpty(getResources().getString(R.string.nc_providers_url)) && (TextUtils.isEmpty(getResources
  92. ().getString(R.string.nc_import_account_type)))) {
  93. providersTextView.setVisibility(View.GONE);
  94. } else {
  95. if ((TextUtils.isEmpty(getResources
  96. ().getString(R.string.nc_import_account_type)) ||
  97. AccountUtils.findAccounts(userUtils.getUsers()).size() == 0) &&
  98. userUtils.getUsers().size() == 0) {
  99. providersTextView.setText(R.string.nc_get_from_provider);
  100. providersTextView.setOnClickListener(view12 -> {
  101. Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(getResources()
  102. .getString(R.string.nc_providers_url)));
  103. startActivity(browserIntent);
  104. });
  105. } else if (AccountUtils.findAccounts(userUtils.getUsers()).size() > 0) {
  106. if (!TextUtils.isEmpty(AccountUtils.getAppNameBasedOnPackage(getResources()
  107. .getString(R.string.nc_import_accounts_from)))) {
  108. if (AccountUtils.findAccounts(userUtils.getUsers()).size() > 1) {
  109. providersTextView.setText(String.format(getResources().getString(R.string
  110. .nc_server_import_accounts), AccountUtils.getAppNameBasedOnPackage(getResources()
  111. .getString(R.string.nc_import_accounts_from))));
  112. } else {
  113. providersTextView.setText(String.format(getResources().getString(R.string
  114. .nc_server_import_account), AccountUtils.getAppNameBasedOnPackage(getResources()
  115. .getString(R.string.nc_import_accounts_from))));
  116. }
  117. } else {
  118. if (AccountUtils.findAccounts(userUtils.getUsers()).size() > 1) {
  119. providersTextView.setText(getResources().getString(R.string.nc_server_import_accounts_plain));
  120. } else {
  121. providersTextView.setText(getResources().getString(R.string
  122. .nc_server_import_account_plain));
  123. }
  124. }
  125. providersTextView.setOnClickListener(view13 -> {
  126. Bundle bundle = new Bundle();
  127. bundle.putBoolean(BundleKeys.KEY_IS_ACCOUNT_IMPORT, true);
  128. getRouter().pushController(RouterTransaction.with(
  129. new SwitchAccountController(bundle))
  130. .pushChangeHandler(new HorizontalChangeHandler())
  131. .popChangeHandler(new HorizontalChangeHandler()));
  132. });
  133. } else {
  134. providersTextView.setVisibility(View.GONE);
  135. }
  136. }
  137. serverEntry.requestFocus();
  138. serverEntry.addTextChangedListener(new TextWatcher() {
  139. @Override
  140. public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  141. }
  142. @Override
  143. public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  144. }
  145. @Override
  146. public void afterTextChanged(Editable editable) {
  147. if (!textFieldBoxes.isOnError() && !TextUtils.isEmpty(serverEntry.getText())) {
  148. toggleProceedButton(true);
  149. } else {
  150. toggleProceedButton(false);
  151. }
  152. }
  153. });
  154. serverEntry.setOnEditorActionListener((textView, i, keyEvent) -> {
  155. if (i == EditorInfo.IME_ACTION_DONE) {
  156. checkServerAndProceed();
  157. }
  158. return false;
  159. });
  160. }
  161. private void toggleProceedButton(boolean show) {
  162. textFieldBoxes.getEndIconImageButton().setEnabled(show);
  163. if (show) {
  164. textFieldBoxes.getEndIconImageButton().setAlpha(1f);
  165. } else {
  166. textFieldBoxes.getEndIconImageButton().setAlpha(0.5f);
  167. }
  168. }
  169. private void checkServerAndProceed() {
  170. dispose();
  171. String url = serverEntry.getText().toString().trim();
  172. serverEntry.setEnabled(false);
  173. progressBar.setVisibility(View.VISIBLE);
  174. providersTextView.setVisibility(View.INVISIBLE);
  175. if (url.endsWith("/")) {
  176. url = url.substring(0, url.length() - 1);
  177. }
  178. String queryUrl = url + ApiHelper.getUrlPostfixForStatus();
  179. if (url.startsWith("http://") || url.startsWith("https://")) {
  180. checkServer(queryUrl, false);
  181. } else {
  182. checkServer("https://" + queryUrl, true);
  183. }
  184. }
  185. private void checkServer(String queryUrl, boolean checkForcedHttps) {
  186. statusQueryDisposable = ncApi.getServerStatus(queryUrl)
  187. .subscribeOn(Schedulers.newThread())
  188. .observeOn(AndroidSchedulers.mainThread())
  189. .subscribe(status -> {
  190. String productName = getResources().getString(R.string.nc_server_product_name);
  191. String versionString = status.getVersion().substring(0, status.getVersion().indexOf("."));
  192. int version = Integer.parseInt(versionString);
  193. if (status.isInstalled() && !status.isMaintenance() &&
  194. !status.isNeedsUpgrade() &&
  195. version >= 13) {
  196. getRouter().pushController(RouterTransaction.with(
  197. new WebViewLoginController(queryUrl.replace("/status.php", ""),
  198. false))
  199. .pushChangeHandler(new HorizontalChangeHandler())
  200. .popChangeHandler(new HorizontalChangeHandler()));
  201. } else if (!status.isInstalled()) {
  202. textFieldBoxes.setError(String.format(
  203. getResources().getString(R.string.nc_server_not_installed), productName),
  204. true);
  205. toggleProceedButton(false);
  206. } else if (status.isNeedsUpgrade()) {
  207. textFieldBoxes.setError(String.format(getResources().
  208. getString(R.string.nc_server_db_upgrade_needed),
  209. productName), true);
  210. toggleProceedButton(false);
  211. } else if (status.isMaintenance()) {
  212. textFieldBoxes.setError(String.format(getResources().
  213. getString(R.string.nc_server_maintenance),
  214. productName),
  215. true);
  216. toggleProceedButton(false);
  217. } else if (!status.getVersion().startsWith("13.")) {
  218. textFieldBoxes.setError(String.format(getResources().
  219. getString(R.string.nc_server_version),
  220. getResources().getString(R.string.nc_app_name)
  221. , productName), true);
  222. toggleProceedButton(false);
  223. }
  224. }, throwable -> {
  225. if (checkForcedHttps) {
  226. checkServer(queryUrl.replace("https://", "http://"), false);
  227. } else {
  228. if (throwable.getLocalizedMessage() != null) {
  229. textFieldBoxes.setError(throwable.getLocalizedMessage(), true);
  230. } else if (throwable.getCause() instanceof CertificateException) {
  231. textFieldBoxes.setError(getResources().getString(R.string.nc_certificate_error),
  232. false);
  233. }
  234. if (serverEntry != null) {
  235. serverEntry.setEnabled(true);
  236. }
  237. progressBar.setVisibility(View.INVISIBLE);
  238. providersTextView.setVisibility(View.VISIBLE);
  239. toggleProceedButton(false);
  240. dispose();
  241. }
  242. }, () -> {
  243. progressBar.setVisibility(View.INVISIBLE);
  244. providersTextView.setVisibility(View.VISIBLE);
  245. dispose();
  246. });
  247. }
  248. @Override
  249. protected void onAttach(@NonNull View view) {
  250. super.onAttach(view);
  251. if (ErrorMessageHolder.getInstance().getMessageType() != null) {
  252. if (ErrorMessageHolder.getInstance().getMessageType()
  253. .equals(ErrorMessageHolder.ErrorMessageType.ACCOUNT_SCHEDULED_FOR_DELETION)) {
  254. textFieldBoxes.setError(getResources().getString(R.string.nc_account_scheduled_for_deletion),
  255. false);
  256. ErrorMessageHolder.getInstance().setMessageType(null);
  257. } else if (ErrorMessageHolder.getInstance().getMessageType()
  258. .equals(ErrorMessageHolder.ErrorMessageType.SERVER_WITHOUT_TALK)) {
  259. textFieldBoxes.setError(getResources().getString(R.string.nc_settings_no_talk_installed),
  260. false);
  261. }
  262. ErrorMessageHolder.getInstance().setMessageType(null);
  263. }
  264. }
  265. @Override
  266. protected void onDestroyView(@NonNull View view) {
  267. super.onDestroyView(view);
  268. if (getActivity() != null) {
  269. getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
  270. }
  271. }
  272. @Override
  273. public void onDestroy() {
  274. super.onDestroy();
  275. dispose();
  276. }
  277. private void dispose() {
  278. if (statusQueryDisposable != null && !statusQueryDisposable.isDisposed()) {
  279. statusQueryDisposable.dispose();
  280. }
  281. statusQueryDisposable = null;
  282. }
  283. }