SessionMixin.kt 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Chris Narkiewicz
  5. * Copyright (C) 2020 Chris Narkiewicz <hello@ezaquarii.com>
  6. * Copyright (C) 2020 Nextcloud GmbH
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. package com.nextcloud.client.mixins
  22. import android.accounts.Account
  23. import android.app.Activity
  24. import android.content.ContentResolver
  25. import android.content.Intent
  26. import android.os.Bundle
  27. import com.nextcloud.client.account.User
  28. import com.nextcloud.client.account.UserAccountManager
  29. import com.nextcloud.java.util.Optional
  30. import com.owncloud.android.datamodel.FileDataStorageManager
  31. import com.owncloud.android.lib.resources.status.OCCapability
  32. import com.owncloud.android.ui.activity.BaseActivity
  33. /**
  34. * Session mixin collects all account / user handling logic currently
  35. * spread over various activities.
  36. *
  37. * It is an intermediary step facilitating comprehensive rework of
  38. * account handling logic.
  39. */
  40. class SessionMixin constructor(
  41. private val activity: Activity,
  42. private val contentResolver: ContentResolver,
  43. private val accountManager: UserAccountManager
  44. ) : ActivityMixin {
  45. private companion object {
  46. private val TAG = BaseActivity::class.java.simpleName
  47. }
  48. var currentAccount: Account? = null
  49. private set
  50. var storageManager: FileDataStorageManager? = null
  51. private set
  52. var capabilities: OCCapability? = null
  53. private set
  54. fun setAccount(account: Account?) {
  55. val validAccount = account != null && accountManager.setCurrentOwnCloudAccount(account.name)
  56. if (validAccount) {
  57. currentAccount = account
  58. } else {
  59. swapToDefaultAccount()
  60. }
  61. currentAccount?.let {
  62. val storageManager = FileDataStorageManager(currentAccount, contentResolver)
  63. this.storageManager = storageManager
  64. this.capabilities = storageManager.getCapability(it.name)
  65. }
  66. }
  67. fun setUser(user: User) {
  68. setAccount(user.toPlatformAccount())
  69. }
  70. fun getUser(): Optional<User> = when (val it = this.currentAccount) {
  71. null -> Optional.empty()
  72. else -> accountManager.getUser(it.name)
  73. }
  74. /**
  75. * Tries to swap the current ownCloud [Account] for other valid and existing.
  76. *
  77. * If no valid ownCloud [Account] exists, then the user is requested
  78. * to create a new ownCloud [Account].
  79. */
  80. private fun swapToDefaultAccount() {
  81. // default to the most recently used account
  82. val newAccount = accountManager.currentAccount
  83. if (newAccount == null) {
  84. // no account available: force account creation
  85. startAccountCreation()
  86. } else {
  87. currentAccount = newAccount
  88. }
  89. }
  90. /**
  91. * Launches the account creation activity.
  92. */
  93. fun startAccountCreation() {
  94. accountManager.startAccountCreation(activity)
  95. }
  96. override fun onNewIntent(intent: Intent) {
  97. super.onNewIntent(intent)
  98. val current = accountManager.currentAccount
  99. val currentAccount = this.currentAccount
  100. if (current != null && currentAccount != null && !currentAccount.name.equals(current.name)) {
  101. this.currentAccount = current
  102. }
  103. }
  104. /**
  105. * Since ownCloud {@link Account} can be managed from the system setting menu, the existence of the {@link
  106. * Account} associated to the instance must be checked every time it is restarted.
  107. */
  108. override fun onRestart() {
  109. super.onRestart()
  110. val validAccount = currentAccount != null && accountManager.exists(currentAccount)
  111. if (!validAccount) {
  112. swapToDefaultAccount()
  113. }
  114. }
  115. override fun onCreate(savedInstanceState: Bundle?) {
  116. super.onCreate(savedInstanceState)
  117. val account = accountManager.currentAccount
  118. setAccount(account)
  119. }
  120. }