ServerSelectionController.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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.pm.ActivityInfo;
  22. import android.support.annotation.NonNull;
  23. import android.text.Editable;
  24. import android.text.TextUtils;
  25. import android.text.TextWatcher;
  26. import android.view.LayoutInflater;
  27. import android.view.View;
  28. import android.view.ViewGroup;
  29. import android.view.inputmethod.EditorInfo;
  30. import android.widget.ProgressBar;
  31. import com.bluelinelabs.conductor.RouterTransaction;
  32. import com.bluelinelabs.conductor.changehandler.HorizontalChangeHandler;
  33. import com.nextcloud.talk.R;
  34. import com.nextcloud.talk.api.NcApi;
  35. import com.nextcloud.talk.api.helpers.api.ApiHelper;
  36. import com.nextcloud.talk.application.NextcloudTalkApplication;
  37. import com.nextcloud.talk.controllers.base.BaseController;
  38. import com.nextcloud.talk.utils.ErrorMessageHolder;
  39. import java.security.cert.CertificateException;
  40. import javax.inject.Inject;
  41. import autodagger.AutoInjector;
  42. import butterknife.BindView;
  43. import io.reactivex.android.schedulers.AndroidSchedulers;
  44. import io.reactivex.disposables.Disposable;
  45. import io.reactivex.schedulers.Schedulers;
  46. import studio.carbonylgroup.textfieldboxes.ExtendedEditText;
  47. import studio.carbonylgroup.textfieldboxes.TextFieldBoxes;
  48. @AutoInjector(NextcloudTalkApplication.class)
  49. public class ServerSelectionController extends BaseController {
  50. public static final String TAG = "ServerSelectionController";
  51. @BindView(R.id.extended_edit_text)
  52. ExtendedEditText serverEntry;
  53. @BindView(R.id.text_field_boxes)
  54. TextFieldBoxes textFieldBoxes;
  55. @BindView(R.id.progress_bar)
  56. ProgressBar progressBar;
  57. @Inject
  58. NcApi ncApi;
  59. private Disposable statusQueryDisposable;
  60. @Override
  61. protected View inflateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) {
  62. return inflater.inflate(R.layout.controller_server_selection, container, false);
  63. }
  64. @Override
  65. protected void onViewBound(@NonNull View view) {
  66. super.onViewBound(view);
  67. NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
  68. if (getActivity() != null) {
  69. getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  70. }
  71. if (getActionBar() != null) {
  72. getActionBar().hide();
  73. }
  74. textFieldBoxes.getEndIconImageButton().setBackgroundDrawable(getResources().getDrawable(R.drawable
  75. .ic_arrow_forward_white_24px));
  76. textFieldBoxes.getEndIconImageButton().setAlpha(0.5f);
  77. textFieldBoxes.getEndIconImageButton().setEnabled(false);
  78. textFieldBoxes.getEndIconImageButton().setVisibility(View.VISIBLE);
  79. textFieldBoxes.getEndIconImageButton().setOnClickListener(view1 -> checkServerAndProceed());
  80. serverEntry.requestFocus();
  81. serverEntry.addTextChangedListener(new TextWatcher() {
  82. @Override
  83. public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  84. }
  85. @Override
  86. public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  87. }
  88. @Override
  89. public void afterTextChanged(Editable editable) {
  90. if (!textFieldBoxes.isOnError() && !TextUtils.isEmpty(serverEntry.getText())) {
  91. toggleProceedButton(true);
  92. } else {
  93. toggleProceedButton(false);
  94. }
  95. }
  96. });
  97. serverEntry.setOnEditorActionListener((textView, i, keyEvent) -> {
  98. if (i == EditorInfo.IME_ACTION_DONE) {
  99. checkServerAndProceed();
  100. }
  101. return false;
  102. });
  103. }
  104. private void toggleProceedButton(boolean show) {
  105. textFieldBoxes.getEndIconImageButton().setEnabled(show);
  106. if (show) {
  107. textFieldBoxes.getEndIconImageButton().setAlpha(1f);
  108. } else {
  109. textFieldBoxes.getEndIconImageButton().setAlpha(0.5f);
  110. }
  111. }
  112. private void checkServerAndProceed() {
  113. dispose();
  114. String url = serverEntry.getText().toString().trim();
  115. serverEntry.setEnabled(false);
  116. progressBar.setVisibility(View.VISIBLE);
  117. if (url.endsWith("/")) {
  118. url = url.substring(0, url.length() - 1);
  119. }
  120. String queryUrl = url + ApiHelper.getUrlPostfixForStatus();
  121. if (url.startsWith("http://") || url.startsWith("https://")) {
  122. checkServer(queryUrl, false);
  123. } else {
  124. checkServer("https://" + queryUrl, true);
  125. }
  126. }
  127. private void checkServer(String queryUrl, boolean checkForcedHttps) {
  128. statusQueryDisposable = ncApi.getServerStatus(queryUrl)
  129. .subscribeOn(Schedulers.newThread())
  130. .observeOn(AndroidSchedulers.mainThread())
  131. .subscribe(status -> {
  132. String productName = getResources().getString(R.string.nc_server_product_name);
  133. if (status.isInstalled() && !status.isMaintenance() &&
  134. !status.isNeedsUpgrade() &&
  135. status.getVersion().startsWith("13.")) {
  136. getRouter().pushController(RouterTransaction.with(
  137. new WebViewLoginController(queryUrl.replace("/status.php", ""),
  138. false))
  139. .pushChangeHandler(new HorizontalChangeHandler())
  140. .popChangeHandler(new HorizontalChangeHandler()));
  141. } else if (!status.isInstalled()) {
  142. textFieldBoxes.setError(String.format(
  143. getResources().getString(R.string.nc_server_not_installed), productName),
  144. true);
  145. } else if (status.isNeedsUpgrade()) {
  146. textFieldBoxes.setError(String.format(getResources().
  147. getString(R.string.nc_server_db_upgrade_needed),
  148. productName), true);
  149. } else if (status.isMaintenance()) {
  150. textFieldBoxes.setError(String.format(getResources().
  151. getString(R.string.nc_server_maintenance),
  152. productName),
  153. true);
  154. } else if (!status.getVersion().startsWith("13.")) {
  155. textFieldBoxes.setError(String.format(getResources().
  156. getString(R.string.nc_server_version),
  157. getResources().getString(R.string.nc_app_name)
  158. , productName), true);
  159. }
  160. }, throwable -> {
  161. if (checkForcedHttps) {
  162. checkServer(queryUrl.replace("https://", "http://"), false);
  163. } else {
  164. if (throwable.getLocalizedMessage() != null) {
  165. textFieldBoxes.setError(throwable.getLocalizedMessage(), true);
  166. } else if (throwable.getCause() instanceof CertificateException) {
  167. textFieldBoxes.setError(getResources().getString(R.string.nc_certificate_error),
  168. false);
  169. }
  170. if (serverEntry != null) {
  171. serverEntry.setEnabled(true);
  172. }
  173. progressBar.setVisibility(View.GONE);
  174. toggleProceedButton(false);
  175. dispose();
  176. }
  177. }, () -> {
  178. progressBar.setVisibility(View.GONE);
  179. dispose();
  180. });
  181. }
  182. @Override
  183. protected void onAttach(@NonNull View view) {
  184. super.onAttach(view);
  185. if (ErrorMessageHolder.getInstance().getMessageType() != null &&
  186. ErrorMessageHolder.getInstance().getMessageType()
  187. .equals(ErrorMessageHolder.ErrorMessageType.ACCOUNT_SCHEDULED_FOR_DELETION)) {
  188. textFieldBoxes.setError(getResources().getString(R.string.nc_account_scheduled_for_deletion),
  189. false);
  190. ErrorMessageHolder.getInstance().setMessageType(null);
  191. }
  192. }
  193. @Override
  194. protected void onDestroyView(@NonNull View view) {
  195. super.onDestroyView(view);
  196. if (getActivity() != null) {
  197. getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
  198. }
  199. }
  200. @Override
  201. public void onDestroy() {
  202. super.onDestroy();
  203. dispose();
  204. }
  205. private void dispose() {
  206. if (statusQueryDisposable != null && !statusQueryDisposable.isDisposed()) {
  207. statusQueryDisposable.dispose();
  208. }
  209. statusQueryDisposable = null;
  210. }
  211. }