SwitchAccountController.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. * Parts related to account import were either copied from or inspired by the great work done by David Luhmer at:
  21. * https://github.com/nextcloud/ownCloud-Account-Importer
  22. */
  23. package com.nextcloud.talk.controllers;
  24. import android.accounts.Account;
  25. import android.os.Bundle;
  26. import android.support.annotation.NonNull;
  27. import android.support.v4.widget.SwipeRefreshLayout;
  28. import android.support.v7.widget.DividerItemDecoration;
  29. import android.support.v7.widget.LinearLayoutManager;
  30. import android.support.v7.widget.RecyclerView;
  31. import android.view.LayoutInflater;
  32. import android.view.MenuItem;
  33. import android.view.View;
  34. import android.view.ViewGroup;
  35. import com.bluelinelabs.conductor.RouterTransaction;
  36. import com.bluelinelabs.conductor.changehandler.HorizontalChangeHandler;
  37. import com.nextcloud.talk.R;
  38. import com.nextcloud.talk.adapters.items.AdvancedUserItem;
  39. import com.nextcloud.talk.application.NextcloudTalkApplication;
  40. import com.nextcloud.talk.controllers.base.BaseController;
  41. import com.nextcloud.talk.models.ImportAccount;
  42. import com.nextcloud.talk.models.database.UserEntity;
  43. import com.nextcloud.talk.models.json.participants.Participant;
  44. import com.nextcloud.talk.utils.AccountUtils;
  45. import com.nextcloud.talk.utils.bundle.BundleKeys;
  46. import com.nextcloud.talk.utils.database.user.UserUtils;
  47. import java.net.CookieManager;
  48. import java.util.ArrayList;
  49. import java.util.List;
  50. import javax.inject.Inject;
  51. import autodagger.AutoInjector;
  52. import butterknife.BindView;
  53. import eu.davidea.flexibleadapter.FlexibleAdapter;
  54. import eu.davidea.flexibleadapter.common.SmoothScrollLinearLayoutManager;
  55. import eu.davidea.flexibleadapter.items.AbstractFlexibleItem;
  56. import io.reactivex.Observer;
  57. import io.reactivex.disposables.Disposable;
  58. @AutoInjector(NextcloudTalkApplication.class)
  59. public class SwitchAccountController extends BaseController {
  60. private static final String TAG = "SwitchAccountController";
  61. @Inject
  62. UserUtils userUtils;
  63. @Inject
  64. CookieManager cookieManager;
  65. @BindView(R.id.recycler_view)
  66. RecyclerView recyclerView;
  67. @BindView(R.id.swipe_refresh_layout)
  68. SwipeRefreshLayout swipeRefreshLayout;
  69. private FlexibleAdapter<AbstractFlexibleItem> adapter;
  70. private List<AbstractFlexibleItem> userItems = new ArrayList<>();
  71. private boolean isAccountImport = false;
  72. private FlexibleAdapter.OnItemClickListener onImportItemClickListener = position -> {
  73. if (userItems.size() > position) {
  74. Account account = ((AdvancedUserItem) userItems.get(position)).getAccount();
  75. verifyAccount(account);
  76. }
  77. return true;
  78. };
  79. private FlexibleAdapter.OnItemClickListener onSwitchItemClickListener =
  80. new FlexibleAdapter.OnItemClickListener() {
  81. @Override
  82. public boolean onItemClick(int position) {
  83. if (userItems.size() > position) {
  84. UserEntity userEntity = ((AdvancedUserItem) userItems.get(position)).getEntity();
  85. userUtils.createOrUpdateUser(null,
  86. null, null, null,
  87. null, true, null, userEntity.getId())
  88. .subscribe(new Observer<UserEntity>() {
  89. @Override
  90. public void onSubscribe(Disposable d) {
  91. }
  92. @Override
  93. public void onNext(UserEntity userEntity) {
  94. cookieManager.getCookieStore().removeAll();
  95. userUtils.disableAllUsersWithoutId(userEntity.getId());
  96. getRouter().popCurrentController();
  97. }
  98. @Override
  99. public void onError(Throwable e) {
  100. }
  101. @Override
  102. public void onComplete() {
  103. }
  104. });
  105. }
  106. return true;
  107. }
  108. };
  109. public SwitchAccountController() {
  110. setHasOptionsMenu(true);
  111. }
  112. public SwitchAccountController(Bundle args) {
  113. super(args);
  114. setHasOptionsMenu(true);
  115. if (args.containsKey(BundleKeys.KEY_IS_ACCOUNT_IMPORT)) {
  116. isAccountImport = true;
  117. }
  118. }
  119. @Override
  120. public boolean onOptionsItemSelected(@NonNull MenuItem item) {
  121. switch (item.getItemId()) {
  122. case android.R.id.home:
  123. getRouter().popCurrentController();
  124. return true;
  125. default:
  126. return super.onOptionsItemSelected(item);
  127. }
  128. }
  129. @Override
  130. protected View inflateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) {
  131. return inflater.inflate(R.layout.controller_generic_rv, container, false);
  132. }
  133. @Override
  134. protected void onViewBound(@NonNull View view) {
  135. super.onViewBound(view);
  136. NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
  137. swipeRefreshLayout.setEnabled(false);
  138. if (adapter == null) {
  139. adapter = new FlexibleAdapter<>(userItems, getActivity(), false);
  140. UserEntity userEntity;
  141. Participant participant;
  142. if (!isAccountImport) {
  143. for (Object userEntityObject : userUtils.getUsers()) {
  144. userEntity = (UserEntity) userEntityObject;
  145. if (!userEntity.getCurrent()) {
  146. participant = new Participant();
  147. participant.setName(userEntity.getDisplayName());
  148. String userId;
  149. if (userEntity.getUserId() != null) {
  150. userId = userEntity.getUserId();
  151. } else {
  152. userId = userEntity.getUsername();
  153. }
  154. participant.setUserId(userId);
  155. userItems.add(new AdvancedUserItem(participant, userEntity, null));
  156. }
  157. }
  158. adapter.addListener(onSwitchItemClickListener);
  159. adapter.updateDataSet(userItems, false);
  160. } else {
  161. if (getActionBar() != null) {
  162. getActionBar().show();
  163. }
  164. Account account;
  165. ImportAccount importAccount;
  166. for (Object accountObject : AccountUtils.findAccounts(userUtils.getUsers())) {
  167. account = (Account) accountObject;
  168. importAccount = AccountUtils.getInformationFromAccount(account);
  169. participant = new Participant();
  170. participant.setName(importAccount.getUsername());
  171. participant.setUserId(importAccount.getUsername());
  172. userEntity = new UserEntity();
  173. userEntity.setBaseUrl(importAccount.getBaseUrl());
  174. userItems.add(new AdvancedUserItem(participant, userEntity, account));
  175. }
  176. adapter.addListener(onImportItemClickListener);
  177. adapter.updateDataSet(userItems, false);
  178. }
  179. }
  180. prepareViews();
  181. }
  182. @Override
  183. protected void onAttach(@NonNull View view) {
  184. super.onAttach(view);
  185. if (getActionBar() != null) {
  186. getActionBar().setDisplayHomeAsUpEnabled(true);
  187. }
  188. }
  189. private void prepareViews() {
  190. LinearLayoutManager layoutManager = new SmoothScrollLinearLayoutManager(getActivity());
  191. recyclerView.setLayoutManager(layoutManager);
  192. recyclerView.setHasFixedSize(true);
  193. recyclerView.setAdapter(adapter);
  194. recyclerView.addItemDecoration(new DividerItemDecoration(
  195. recyclerView.getContext(),
  196. layoutManager.getOrientation()
  197. ));
  198. swipeRefreshLayout.setEnabled(false);
  199. }
  200. private void verifyAccount(Account account) {
  201. ImportAccount importAccount = AccountUtils.getInformationFromAccount(account);
  202. Bundle bundle = new Bundle();
  203. bundle.putString(BundleKeys.KEY_USERNAME, importAccount.getUsername());
  204. bundle.putString(BundleKeys.KEY_TOKEN, importAccount.getToken());
  205. bundle.putString(BundleKeys.KEY_BASE_URL, importAccount.getBaseUrl());
  206. bundle.putBoolean(BundleKeys.KEY_IS_ACCOUNT_IMPORT, true);
  207. getRouter().pushController(RouterTransaction.with(new AccountVerificationController
  208. (bundle)).pushChangeHandler(new HorizontalChangeHandler())
  209. .popChangeHandler(new HorizontalChangeHandler()));
  210. }
  211. @Override
  212. protected String getTitle() {
  213. return getResources().getString(R.string.nc_select_an_account);
  214. }
  215. }