AccountVerificationController.java 10 KB

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