AccountVerificationController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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.os.Bundle;
  23. import android.os.Handler;
  24. import android.support.annotation.NonNull;
  25. import android.support.annotation.Nullable;
  26. import android.text.TextUtils;
  27. import android.view.LayoutInflater;
  28. import android.view.View;
  29. import android.view.ViewGroup;
  30. import android.widget.TextView;
  31. import com.bluelinelabs.conductor.RouterTransaction;
  32. import com.bluelinelabs.conductor.changehandler.HorizontalChangeHandler;
  33. import com.evernote.android.job.JobRequest;
  34. import com.nextcloud.talk.R;
  35. import com.nextcloud.talk.api.NcApi;
  36. import com.nextcloud.talk.api.helpers.api.ApiHelper;
  37. import com.nextcloud.talk.application.NextcloudTalkApplication;
  38. import com.nextcloud.talk.controllers.base.BaseController;
  39. import com.nextcloud.talk.jobs.PushRegistrationJob;
  40. import com.nextcloud.talk.utils.ErrorMessageHolder;
  41. import com.nextcloud.talk.utils.bundle.BundleKeys;
  42. import com.nextcloud.talk.utils.database.user.UserUtils;
  43. import java.net.CookieManager;
  44. import javax.inject.Inject;
  45. import autodagger.AutoInjector;
  46. import butterknife.BindView;
  47. import io.reactivex.CompletableObserver;
  48. import io.reactivex.android.schedulers.AndroidSchedulers;
  49. import io.reactivex.disposables.Disposable;
  50. import io.reactivex.schedulers.Schedulers;
  51. @AutoInjector(NextcloudTalkApplication.class)
  52. public class AccountVerificationController extends BaseController {
  53. public static final String TAG = "AccountVerificationController";
  54. @Inject
  55. NcApi ncApi;
  56. @Inject
  57. UserUtils userUtils;
  58. @Inject
  59. CookieManager cookieManager;
  60. @BindView(R.id.progress_text)
  61. TextView progressText;
  62. private Disposable roomsQueryDisposable;
  63. private Disposable profileQueryDisposable;
  64. private Disposable dbQueryDisposable;
  65. private Disposable statusQueryDisposable;
  66. private String baseUrl;
  67. private String username;
  68. private String token;
  69. private boolean isAccountImport;
  70. public AccountVerificationController(Bundle args) {
  71. super(args);
  72. if (args != null) {
  73. baseUrl = args.getString(BundleKeys.KEY_BASE_URL);
  74. username = args.getString(BundleKeys.KEY_USERNAME);
  75. token = args.getString(BundleKeys.KEY_TOKEN);
  76. if (args.containsKey(BundleKeys.KEY_IS_ACCOUNT_IMPORT)) {
  77. isAccountImport = true;
  78. }
  79. }
  80. }
  81. @Override
  82. protected View inflateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) {
  83. return inflater.inflate(R.layout.controller_account_verification, container, false);
  84. }
  85. @Override
  86. protected void onViewBound(@NonNull View view) {
  87. super.onViewBound(view);
  88. NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
  89. if (getActivity() != null) {
  90. getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  91. }
  92. if (getActionBar() != null) {
  93. getActionBar().hide();
  94. }
  95. dispose(null);
  96. if (isAccountImport && !baseUrl.startsWith("http://") && !baseUrl.startsWith("https://")) {
  97. determineBaseUrlProtocol(true);
  98. } else {
  99. checkEverything();
  100. }
  101. }
  102. private void determineBaseUrlProtocol(boolean checkForcedHttps) {
  103. cookieManager.getCookieStore().removeAll();
  104. String queryUrl;
  105. if (checkForcedHttps) {
  106. queryUrl = "https://" + baseUrl + ApiHelper.getUrlPostfixForStatus();
  107. } else {
  108. queryUrl = "http://" + baseUrl + ApiHelper.getUrlPostfixForStatus();
  109. }
  110. statusQueryDisposable = ncApi.getServerStatus(queryUrl)
  111. .subscribeOn(Schedulers.newThread())
  112. .observeOn(AndroidSchedulers.mainThread())
  113. .subscribe(status -> {
  114. if (checkForcedHttps) {
  115. baseUrl = "https://" + baseUrl;
  116. } else {
  117. baseUrl = "http://" + baseUrl;
  118. }
  119. checkEverything();
  120. }, throwable -> {
  121. if (checkForcedHttps) {
  122. determineBaseUrlProtocol(false);
  123. } else {
  124. abortVerification();
  125. }
  126. }, () -> {
  127. statusQueryDisposable.dispose();
  128. });
  129. }
  130. private void checkEverything() {
  131. String credentials = ApiHelper.getCredentials(username, token);
  132. cookieManager.getCookieStore().removeAll();
  133. roomsQueryDisposable = ncApi.getRooms(credentials, ApiHelper.getUrlForGetRooms(baseUrl))
  134. .subscribeOn(Schedulers.newThread())
  135. .observeOn(AndroidSchedulers.mainThread())
  136. .subscribe(roomsOverall -> {
  137. progressText.setText(String.format(getResources().getString(
  138. R.string.nc_nextcloud_talk_app_installed), getResources().getString(R.string.nc_app_name)));
  139. profileQueryDisposable = ncApi.getUserProfile(credentials,
  140. ApiHelper.getUrlForUserProfile(baseUrl))
  141. .subscribeOn(Schedulers.newThread())
  142. .observeOn(AndroidSchedulers.mainThread())
  143. .subscribe(userProfileOverall -> {
  144. progressText.setText(progressText.getText().toString() + "\n" +
  145. getResources().getString(R.string.nc_display_name_fetched));
  146. String displayName = null;
  147. if (!TextUtils.isEmpty(userProfileOverall.getOcs().getData()
  148. .getDisplayName())) {
  149. displayName = userProfileOverall.getOcs().getData()
  150. .getDisplayName();
  151. } else if (!TextUtils.isEmpty(userProfileOverall.getOcs().getData()
  152. .getDisplayNameAlt())) {
  153. displayName = userProfileOverall.getOcs().getData()
  154. .getDisplayNameAlt();
  155. }
  156. if (!TextUtils.isEmpty(displayName)) {
  157. dbQueryDisposable = userUtils.createOrUpdateUser(username, token,
  158. baseUrl, displayName, null, true,
  159. userProfileOverall.getOcs().getData().getUserId())
  160. .subscribeOn(Schedulers.newThread())
  161. .observeOn(AndroidSchedulers.mainThread())
  162. .subscribe(userEntity -> {
  163. progressText.setText(progressText.getText().toString()
  164. + "\n" +
  165. getResources().getString(
  166. R.string.nc_display_name_stored));
  167. new JobRequest.Builder(PushRegistrationJob.TAG).
  168. setUpdateCurrent(true).startNow().build().schedule();
  169. cookieManager.getCookieStore().removeAll();
  170. userUtils.disableAllUsersWithoutId(userEntity.getId());
  171. if (userUtils.getUsers().size() == 1) {
  172. getRouter().setRoot(RouterTransaction.with(new
  173. MagicBottomNavigationController())
  174. .pushChangeHandler(new HorizontalChangeHandler())
  175. .popChangeHandler(new HorizontalChangeHandler()));
  176. } else {
  177. if (isAccountImport) {
  178. ErrorMessageHolder.getInstance().setMessageType(
  179. ErrorMessageHolder.ErrorMessageType.ACCOUNT_WAS_IMPORTED);
  180. }
  181. getRouter().popToRoot();
  182. }
  183. },
  184. throwable -> {
  185. progressText.setText(progressText.getText().toString() +
  186. "\n" +
  187. getResources().getString(
  188. R.string.nc_display_name_not_stored));
  189. abortVerification();
  190. }, () -> dispose(dbQueryDisposable));
  191. } else {
  192. progressText.setText(progressText.getText().toString() + "\n" +
  193. getResources().getString(R.string.nc_display_name_not_fetched));
  194. abortVerification();
  195. }
  196. }, throwable -> {
  197. progressText.setText(progressText.getText().toString()
  198. + "\n" + getResources().getString(
  199. R.string.nc_display_name_not_fetched));
  200. abortVerification();
  201. }, () -> dispose(profileQueryDisposable));
  202. }, throwable -> {
  203. progressText.setText(String.format(getResources().getString(
  204. R.string.nc_nextcloud_talk_app_not_installed), getResources().getString(R.string.nc_app_name)));
  205. ErrorMessageHolder.getInstance().setMessageType(
  206. ErrorMessageHolder.ErrorMessageType.SERVER_WITHOUT_TALK);
  207. abortVerification();
  208. }, () -> dispose(roomsQueryDisposable));
  209. }
  210. private void dispose(@Nullable Disposable disposable) {
  211. if (disposable != null && !disposable.isDisposed()) {
  212. disposable.dispose();
  213. } else if (disposable == null) {
  214. if (roomsQueryDisposable != null && !roomsQueryDisposable.isDisposed()) {
  215. roomsQueryDisposable.dispose();
  216. roomsQueryDisposable = null;
  217. }
  218. if (profileQueryDisposable != null && !profileQueryDisposable.isDisposed()) {
  219. profileQueryDisposable.dispose();
  220. profileQueryDisposable = null;
  221. }
  222. if (dbQueryDisposable != null && !dbQueryDisposable.isDisposed()) {
  223. dbQueryDisposable.dispose();
  224. dbQueryDisposable = null;
  225. }
  226. if (statusQueryDisposable != null && !statusQueryDisposable.isDisposed()) {
  227. statusQueryDisposable.dispose();
  228. statusQueryDisposable = null;
  229. }
  230. }
  231. disposable = null;
  232. }
  233. @Override
  234. protected void onDestroyView(@NonNull View view) {
  235. super.onDestroyView(view);
  236. if (getActivity() != null) {
  237. getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
  238. }
  239. }
  240. @Override
  241. public void onDestroy() {
  242. super.onDestroy();
  243. dispose(null);
  244. }
  245. private void abortVerification() {
  246. dispose(null);
  247. if (!isAccountImport) {
  248. userUtils.deleteUser(username, baseUrl).subscribe(new CompletableObserver() {
  249. @Override
  250. public void onSubscribe(Disposable d) {
  251. }
  252. @Override
  253. public void onComplete() {
  254. new Handler().postDelayed(() -> getRouter().popToRoot(), 10000);
  255. }
  256. @Override
  257. public void onError(Throwable e) {
  258. }
  259. });
  260. } else {
  261. ErrorMessageHolder.getInstance().setMessageType(
  262. ErrorMessageHolder.ErrorMessageType.FAILED_TO_IMPORT_ACCOUNT);
  263. new Handler().postDelayed(() -> getRouter().popToRoot(), 10000);
  264. }
  265. }
  266. }