ServerSelectionController.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Nextcloud Talk application
  3. *
  4. * @author Mario Danic
  5. * Copyright (C) 2017 Mario Danic
  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.view.LayoutInflater;
  24. import android.view.View;
  25. import android.view.ViewGroup;
  26. import android.view.inputmethod.EditorInfo;
  27. import com.bluelinelabs.conductor.RouterTransaction;
  28. import com.bluelinelabs.conductor.changehandler.HorizontalChangeHandler;
  29. import com.nextcloud.talk.R;
  30. import com.nextcloud.talk.api.NcApi;
  31. import com.nextcloud.talk.api.helpers.api.ApiHelper;
  32. import com.nextcloud.talk.application.NextcloudTalkApplication;
  33. import com.nextcloud.talk.controllers.base.BaseController;
  34. import javax.inject.Inject;
  35. import autodagger.AutoInjector;
  36. import butterknife.BindView;
  37. import io.reactivex.android.schedulers.AndroidSchedulers;
  38. import io.reactivex.disposables.Disposable;
  39. import io.reactivex.schedulers.Schedulers;
  40. import studio.carbonylgroup.textfieldboxes.ExtendedEditText;
  41. import studio.carbonylgroup.textfieldboxes.TextFieldBoxes;
  42. @AutoInjector(NextcloudTalkApplication.class)
  43. public class ServerSelectionController extends BaseController {
  44. public static final String TAG = "ServerSelectionController";
  45. @BindView(R.id.extended_edit_text)
  46. ExtendedEditText serverEntry;
  47. @BindView(R.id.text_field_boxes)
  48. TextFieldBoxes textFieldBoxes;
  49. @Inject
  50. NcApi ncApi;
  51. private Disposable statusQueryDisposable;
  52. @Override
  53. protected View inflateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) {
  54. return inflater.inflate(R.layout.controller_server_selection, container, false);
  55. }
  56. @Override
  57. protected void onViewBound(@NonNull View view) {
  58. super.onViewBound(view);
  59. NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
  60. if (getActivity() != null) {
  61. getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  62. }
  63. textFieldBoxes.setLabelText(getResources().getString(R.string.nc_app_name) + " " + getResources().getString(R.string.nc_appended_server_url));
  64. serverEntry.requestFocus();
  65. serverEntry.setOnEditorActionListener((textView, i, keyEvent) -> {
  66. if (i == EditorInfo.IME_ACTION_DONE) {
  67. dispose();
  68. String url = serverEntry.getText().toString().trim();
  69. if (url.startsWith("http://") || url.startsWith("https://")) {
  70. serverEntry.setEnabled(false);
  71. if (url.endsWith("/")) {
  72. url = url.substring(0, url.length() - 1);
  73. }
  74. String queryUrl = url + ApiHelper.getUrlPostfixForStatus();
  75. final String finalServerUrl = url;
  76. statusQueryDisposable = ncApi.getServerStatus(queryUrl)
  77. .subscribeOn(Schedulers.newThread())
  78. .observeOn(AndroidSchedulers.mainThread())
  79. .subscribe(status -> {
  80. String productName = getResources().getString(R.string.nc_server_product_name);
  81. if (status.isInstalled() && !status.isMaintenance() &&
  82. !status.isNeedsUpgrade() &&
  83. status.getProductName().equals(
  84. getResources().getString(R.string.nc_server_product_name))) {
  85. getRouter().pushController(RouterTransaction.with(
  86. new WebViewLoginController(finalServerUrl))
  87. .pushChangeHandler(new HorizontalChangeHandler())
  88. .popChangeHandler(new HorizontalChangeHandler()));
  89. } else if (!status.isInstalled()) {
  90. textFieldBoxes.setError(String.format(
  91. getResources().getString(R.string.nc_server_not_installed), productName),
  92. true);
  93. } else if (status.isNeedsUpgrade()) {
  94. textFieldBoxes.setError(String.format(getResources().
  95. getString(R.string.nc_server_db_upgrade_needed), productName), true);
  96. } else if (status.isMaintenance()) {
  97. textFieldBoxes.setError(String.format(getResources().
  98. getString(R.string.nc_server_maintenance), productName),
  99. true);
  100. } else if (!status.getProductName().equals(
  101. getResources().getString(R.string.nc_server_product_name))) {
  102. textFieldBoxes.setError(String.format(getResources().
  103. getString(R.string.nc_server_not_nc),
  104. getResources().getString(R.string.nc_app_name)
  105. , productName), true);
  106. }
  107. }, throwable -> {
  108. textFieldBoxes.setError(throwable.getLocalizedMessage(), true);
  109. if (serverEntry != null) {
  110. serverEntry.setEnabled(true);
  111. }
  112. dispose();
  113. }, this::dispose);
  114. } else {
  115. textFieldBoxes.setError(getResources().getString(R.string.nc_server_url_prefix), true);
  116. serverEntry.setEnabled(true);
  117. return true;
  118. }
  119. }
  120. return false;
  121. });
  122. }
  123. @Override
  124. protected void onDestroyView(@NonNull View view) {
  125. super.onDestroyView(view);
  126. if (getActivity() != null) {
  127. getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
  128. }
  129. }
  130. private void dispose() {
  131. if (statusQueryDisposable != null && !statusQueryDisposable.isDisposed()) {
  132. statusQueryDisposable.dispose();
  133. }
  134. statusQueryDisposable = null;
  135. }
  136. }