AccountVerificationController.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 javax.inject.Inject;
  44. import autodagger.AutoInjector;
  45. import butterknife.BindView;
  46. import io.reactivex.CompletableObserver;
  47. import io.reactivex.android.schedulers.AndroidSchedulers;
  48. import io.reactivex.disposables.Disposable;
  49. import io.reactivex.schedulers.Schedulers;
  50. @AutoInjector(NextcloudTalkApplication.class)
  51. public class AccountVerificationController extends BaseController {
  52. public static final String TAG = "AccountVerificationController";
  53. @Inject
  54. NcApi ncApi;
  55. @Inject
  56. UserUtils userUtils;
  57. @BindView(R.id.progress_text)
  58. TextView progressText;
  59. private Disposable roomsQueryDisposable;
  60. private Disposable profileQueryDisposable;
  61. private Disposable dbQueryDisposable;
  62. private String baseUrl;
  63. private String username;
  64. private String token;
  65. public AccountVerificationController(Bundle args) {
  66. super(args);
  67. if (args != null) {
  68. baseUrl = args.getString(BundleKeys.KEY_BASE_URL);
  69. username = args.getString(BundleKeys.KEY_USERNAME);
  70. token = args.getString(BundleKeys.KEY_TOKEN);
  71. }
  72. }
  73. @Override
  74. protected View inflateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) {
  75. return inflater.inflate(R.layout.controller_account_verification, container, false);
  76. }
  77. @Override
  78. protected void onViewBound(@NonNull View view) {
  79. super.onViewBound(view);
  80. NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
  81. if (getActivity() != null) {
  82. getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  83. }
  84. if (getActionBar() != null) {
  85. getActionBar().hide();
  86. }
  87. dispose(null);
  88. String credentials = ApiHelper.getCredentials(username, token);
  89. roomsQueryDisposable = ncApi.getRooms(credentials, ApiHelper.getUrlForGetRooms(baseUrl))
  90. .subscribeOn(Schedulers.newThread())
  91. .observeOn(AndroidSchedulers.mainThread())
  92. .subscribe(roomsOverall -> {
  93. progressText.setText(String.format(getResources().getString(
  94. R.string.nc_nextcloud_talk_app_installed), getResources().getString(R.string.nc_app_name)));
  95. profileQueryDisposable = ncApi.getUserProfile(credentials,
  96. ApiHelper.getUrlForUserProfile(baseUrl))
  97. .subscribeOn(Schedulers.newThread())
  98. .observeOn(AndroidSchedulers.mainThread())
  99. .subscribe(userProfileOverall -> {
  100. progressText.setText(progressText.getText().toString() + "\n" +
  101. getResources().getString(R.string.nc_display_name_fetched));
  102. String displayName = null;
  103. if (!TextUtils.isEmpty(userProfileOverall.getOcs().getData()
  104. .getDisplayName())) {
  105. displayName = userProfileOverall.getOcs().getData()
  106. .getDisplayName();
  107. } else if (!TextUtils.isEmpty(userProfileOverall.getOcs().getData()
  108. .getDisplayNameAlt())) {
  109. displayName = userProfileOverall.getOcs().getData()
  110. .getDisplayNameAlt();
  111. }
  112. if (!TextUtils.isEmpty(displayName)) {
  113. dbQueryDisposable = userUtils.createOrUpdateUser(username, token,
  114. baseUrl, displayName, null, true)
  115. .subscribeOn(Schedulers.newThread())
  116. .observeOn(AndroidSchedulers.mainThread())
  117. .subscribe(userEntity -> {
  118. progressText.setText(progressText.getText().toString()
  119. + "\n" +
  120. getResources().getString(
  121. R.string.nc_display_name_stored));
  122. new JobRequest.Builder(PushRegistrationJob.TAG).
  123. setUpdateCurrent(true).startNow().build().schedule();
  124. userUtils.disableAllUsersWithoutId(userEntity.getId());
  125. if (userUtils.getUsers().size() == 1) {
  126. getRouter().setRoot(RouterTransaction.with(new
  127. MagicBottomNavigationController())
  128. .pushChangeHandler(new HorizontalChangeHandler())
  129. .popChangeHandler(new HorizontalChangeHandler()));
  130. } else {
  131. getRouter().popToRoot();
  132. }
  133. },
  134. throwable -> {
  135. progressText.setText(progressText.getText().toString() +
  136. "\n" +
  137. getResources().getString(
  138. R.string.nc_display_name_not_stored));
  139. abortVerification();
  140. }, () -> dispose(dbQueryDisposable));
  141. } else {
  142. progressText.setText(progressText.getText().toString() + "\n" +
  143. getResources().getString(R.string.nc_display_name_not_fetched));
  144. abortVerification();
  145. }
  146. }, throwable -> {
  147. progressText.setText(progressText.getText().toString()
  148. + "\n" + getResources().getString(
  149. R.string.nc_display_name_not_fetched));
  150. abortVerification();
  151. }, () -> dispose(profileQueryDisposable));
  152. }, throwable -> {
  153. progressText.setText(String.format(getResources().getString(
  154. R.string.nc_nextcloud_talk_app_not_installed), getResources().getString(R.string.nc_app_name)));
  155. ErrorMessageHolder.getInstance().setMessageType(
  156. ErrorMessageHolder.ErrorMessageType.SERVER_WITHOUT_TALK);
  157. abortVerification();
  158. }, () -> dispose(roomsQueryDisposable));
  159. }
  160. private void dispose(@Nullable Disposable disposable) {
  161. if (disposable != null && !disposable.isDisposed()) {
  162. disposable.dispose();
  163. } else if (disposable == null) {
  164. if (roomsQueryDisposable != null && !roomsQueryDisposable.isDisposed()) {
  165. roomsQueryDisposable.dispose();
  166. roomsQueryDisposable = null;
  167. }
  168. if (profileQueryDisposable != null && !profileQueryDisposable.isDisposed()) {
  169. profileQueryDisposable.dispose();
  170. profileQueryDisposable = null;
  171. }
  172. if (dbQueryDisposable != null && !dbQueryDisposable.isDisposed()) {
  173. dbQueryDisposable.dispose();
  174. dbQueryDisposable = null;
  175. }
  176. }
  177. disposable = null;
  178. }
  179. @Override
  180. protected void onDestroyView(@NonNull View view) {
  181. super.onDestroyView(view);
  182. if (getActivity() != null) {
  183. getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
  184. }
  185. }
  186. @Override
  187. public void onDestroy() {
  188. super.onDestroy();
  189. dispose(null);
  190. }
  191. private void abortVerification() {
  192. dispose(null);
  193. userUtils.deleteUser(username, baseUrl).subscribe(new CompletableObserver() {
  194. @Override
  195. public void onSubscribe(Disposable d) {
  196. }
  197. @Override
  198. public void onComplete() {
  199. new Handler().postDelayed(() -> getRouter().popToRoot(), 7500);
  200. }
  201. @Override
  202. public void onError(Throwable e) {
  203. }
  204. });
  205. }
  206. }