SwitchAccountController.kt 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Nextcloud Talk application
  3. *
  4. * @author Mario Danic
  5. * @author Andy Scherzinger
  6. * Copyright (C) 2022 Andy Scherzinger <info@andy-scherzinger.de>
  7. * Copyright (C) 2017 Mario Danic <mario@lovelyhq.com>
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * Parts related to account import were either copied from or inspired by the great work done by David Luhmer at:
  23. * https://github.com/nextcloud/ownCloud-Account-Importer
  24. */
  25. package com.nextcloud.talk.controllers
  26. import android.accounts.Account
  27. import android.os.Bundle
  28. import android.view.MenuItem
  29. import android.view.View
  30. import androidx.preference.PreferenceManager
  31. import androidx.recyclerview.widget.LinearLayoutManager
  32. import autodagger.AutoInjector
  33. import com.bluelinelabs.conductor.RouterTransaction
  34. import com.bluelinelabs.conductor.changehandler.HorizontalChangeHandler
  35. import com.nextcloud.talk.R
  36. import com.nextcloud.talk.adapters.items.AdvancedUserItem
  37. import com.nextcloud.talk.application.NextcloudTalkApplication
  38. import com.nextcloud.talk.application.NextcloudTalkApplication.Companion.sharedApplication
  39. import com.nextcloud.talk.controllers.base.NewBaseController
  40. import com.nextcloud.talk.controllers.util.viewBinding
  41. import com.nextcloud.talk.data.user.model.User
  42. import com.nextcloud.talk.databinding.ControllerGenericRvBinding
  43. import com.nextcloud.talk.models.ImportAccount
  44. import com.nextcloud.talk.models.json.participants.Participant
  45. import com.nextcloud.talk.users.UserManager
  46. import com.nextcloud.talk.utils.AccountUtils.findAccountsForUsers
  47. import com.nextcloud.talk.utils.AccountUtils.getInformationFromAccount
  48. import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_BASE_URL
  49. import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_IS_ACCOUNT_IMPORT
  50. import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_TOKEN
  51. import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_USERNAME
  52. import eu.davidea.flexibleadapter.FlexibleAdapter
  53. import eu.davidea.flexibleadapter.common.SmoothScrollLinearLayoutManager
  54. import eu.davidea.flexibleadapter.items.AbstractFlexibleItem
  55. import org.osmdroid.config.Configuration
  56. import java.net.CookieManager
  57. import javax.inject.Inject
  58. @AutoInjector(NextcloudTalkApplication::class)
  59. class SwitchAccountController(args: Bundle? = null) :
  60. NewBaseController(
  61. R.layout.controller_generic_rv,
  62. args
  63. ) {
  64. private val binding: ControllerGenericRvBinding by viewBinding(ControllerGenericRvBinding::bind)
  65. @Inject
  66. lateinit var userManager: UserManager
  67. @Inject
  68. lateinit var cookieManager: CookieManager
  69. private var adapter: FlexibleAdapter<AbstractFlexibleItem<*>>? = null
  70. private val userItems: MutableList<AbstractFlexibleItem<*>> = ArrayList()
  71. private var isAccountImport = false
  72. private val onImportItemClickListener = FlexibleAdapter.OnItemClickListener { _, position ->
  73. if (userItems.size > position) {
  74. val account = (userItems[position] as AdvancedUserItem).account
  75. reauthorizeFromImport(account)
  76. }
  77. true
  78. }
  79. private val onSwitchItemClickListener = FlexibleAdapter.OnItemClickListener { _, position ->
  80. if (userItems.size > position) {
  81. val user = (userItems[position] as AdvancedUserItem).user
  82. if (userManager.setUserAsActive(user).blockingGet()) {
  83. cookieManager.cookieStore.removeAll()
  84. if (activity != null) {
  85. activity!!.runOnUiThread { router.popCurrentController() }
  86. }
  87. }
  88. }
  89. true
  90. }
  91. init {
  92. setHasOptionsMenu(true)
  93. sharedApplication!!.componentApplication.inject(this)
  94. Configuration.getInstance().load(context, PreferenceManager.getDefaultSharedPreferences(context))
  95. if (args?.containsKey(KEY_IS_ACCOUNT_IMPORT) == true) {
  96. isAccountImport = true
  97. }
  98. }
  99. override fun onOptionsItemSelected(item: MenuItem): Boolean {
  100. return when (item.itemId) {
  101. android.R.id.home -> {
  102. router.popCurrentController()
  103. true
  104. }
  105. else -> super.onOptionsItemSelected(item)
  106. }
  107. }
  108. override fun onViewBound(view: View) {
  109. super.onViewBound(view)
  110. binding.swipeRefreshLayout.isEnabled = false
  111. actionBar?.show()
  112. if (adapter == null) {
  113. adapter = FlexibleAdapter(userItems, activity, false)
  114. var participant: Participant
  115. if (!isAccountImport) {
  116. for (user in userManager.users.blockingGet()) {
  117. if (!user.current) {
  118. val userId: String? = if (user.userId != null) {
  119. user.userId
  120. } else {
  121. user.username
  122. }
  123. participant = Participant()
  124. participant.actorType = Participant.ActorType.USERS
  125. participant.actorId = userId
  126. participant.displayName = user.displayName
  127. userItems.add(AdvancedUserItem(participant, user, null, viewThemeUtils))
  128. }
  129. }
  130. adapter!!.addListener(onSwitchItemClickListener)
  131. adapter!!.updateDataSet(userItems, false)
  132. } else {
  133. var account: Account
  134. var importAccount: ImportAccount
  135. var user: User
  136. for (accountObject in findAccountsForUsers(userManager.users.blockingGet())) {
  137. account = accountObject
  138. importAccount = getInformationFromAccount(account)
  139. participant = Participant()
  140. participant.actorType = Participant.ActorType.USERS
  141. participant.actorId = importAccount.getUsername()
  142. participant.displayName = importAccount.getUsername()
  143. user = User()
  144. user.baseUrl = importAccount.getBaseUrl()
  145. userItems.add(AdvancedUserItem(participant, user, account, viewThemeUtils))
  146. }
  147. adapter!!.addListener(onImportItemClickListener)
  148. adapter!!.updateDataSet(userItems, false)
  149. }
  150. }
  151. prepareViews()
  152. }
  153. private fun prepareViews() {
  154. val layoutManager: LinearLayoutManager = SmoothScrollLinearLayoutManager(activity)
  155. binding.recyclerView.layoutManager = layoutManager
  156. binding.recyclerView.setHasFixedSize(true)
  157. binding.recyclerView.adapter = adapter
  158. binding.swipeRefreshLayout.isEnabled = false
  159. }
  160. private fun reauthorizeFromImport(account: Account?) {
  161. val importAccount = getInformationFromAccount(account!!)
  162. val bundle = Bundle()
  163. bundle.putString(KEY_BASE_URL, importAccount.getBaseUrl())
  164. bundle.putString(KEY_USERNAME, importAccount.getUsername())
  165. bundle.putString(KEY_TOKEN, importAccount.getToken())
  166. bundle.putBoolean(KEY_IS_ACCOUNT_IMPORT, true)
  167. router.pushController(
  168. RouterTransaction.with(AccountVerificationController(bundle))
  169. .pushChangeHandler(HorizontalChangeHandler())
  170. .popChangeHandler(HorizontalChangeHandler())
  171. )
  172. }
  173. override val title: String
  174. get() =
  175. resources!!.getString(R.string.nc_select_an_account)
  176. }