Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 03509665 authored by Fahim Salam Chowdhury's avatar Fahim Salam Chowdhury 👽
Browse files

1087-Block_multiple_ecloud_account_login

parent 8604982b
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -30,5 +30,7 @@ object Constants {

    const val AUTH_TOKEN_TYPE = "oauth2-access-token"

    const val EELO_SYNC_URL = "https://ecloud.global"
    const val EELO_SYNC_SCHEME = "https://"
    const val EELO_SYNC_HOST = "ecloud.global"
    const val EELO_SYNC_URL = EELO_SYNC_SCHEME + EELO_SYNC_HOST
}
+42 −0
Original line number Diff line number Diff line
/*
 * Copyright ECORP SAS 2022
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package at.bitfire.davdroid

import android.accounts.AccountManager
import android.app.Activity
import android.content.Context
import com.google.android.material.dialog.MaterialAlertDialogBuilder

object ECloudAccountHelper {

    fun alreadyHasECloudAccount(context: Context) : Boolean {
        val accountManager = AccountManager.get(context)
        val eCloudAccounts = accountManager.getAccountsByType(context.getString(R.string.eelo_account_type))
        return eCloudAccounts.isNotEmpty()
    }

    fun showMultipleECloudAccountNotAcceptedDialog(activity: Activity) {
        MaterialAlertDialogBuilder(activity, R.style.CustomAlertDialogStyle)
                .setIcon(R.drawable.ic_error)
                .setMessage(R.string.multiple_ecloud_account_not_permitted_message)
                .setCancelable(false)
                .setPositiveButton(android.R.string.ok) { _, _ ->
                    activity.finish()
                }
                .show()
    }
}
 No newline at end of file
+17 −0
Original line number Diff line number Diff line
@@ -17,10 +17,12 @@ import androidx.fragment.app.viewModels
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import at.bitfire.davdroid.Constants
import at.bitfire.davdroid.R
import at.bitfire.davdroid.log.Logger
import at.bitfire.davdroid.ui.DebugInfoActivity
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import at.bitfire.davdroid.ECloudAccountHelper
import java.lang.ref.WeakReference
import java.util.logging.Level
import kotlin.concurrent.thread
@@ -33,6 +35,11 @@ class DetectConfigurationFragment: Fragment() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        if (model.blockProceedWithLogin(loginModel)) {
            ECloudAccountHelper.showMultipleECloudAccountNotAcceptedDialog(requireActivity())
            return
        }

        model.detectConfiguration(loginModel).observe(this, { result ->
            // save result for next step
            loginModel.configuration = result
@@ -63,6 +70,16 @@ class DetectConfigurationFragment: Fragment() {
        private var detectionThread: WeakReference<Thread>? = null
        private var result = MutableLiveData<DavResourceFinder.Configuration>()

        /**
         * User can't login using multiple ecloud accounts.
         * This method checks if the login host is eelo_host then, check user already has any eelo account set up.
         * If found eCloundAccount return true, false otherwise.
         */
        fun blockProceedWithLogin(loginModel: LoginModel) : Boolean {
            val context = getApplication<Application>()
            return (loginModel.baseURI?.host.equals(Constants.EELO_SYNC_HOST) && ECloudAccountHelper.alreadyHasECloudAccount(context))
        }

        fun detectConfiguration(loginModel: LoginModel): LiveData<DavResourceFinder.Configuration> {
            synchronized(result) {
                if (detectionThread != null)
+5 −6
Original line number Diff line number Diff line
@@ -25,8 +25,10 @@ import android.view.ViewGroup
import android.widget.Toast
import androidx.core.widget.doOnTextChanged
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProviders
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import at.bitfire.davdroid.Constants
import at.bitfire.davdroid.ECloudAccountHelper
import at.bitfire.davdroid.R
import at.bitfire.davdroid.databinding.FragmentEeloAuthenticatorBinding
import at.bitfire.davdroid.db.Credentials
@@ -36,8 +38,8 @@ import java.net.URI

class EeloAuthenticatorFragment : Fragment() {

    private lateinit var model: EeloAuthenticatorModel
    private lateinit var loginModel: LoginModel
    private val model by viewModels<EeloAuthenticatorModel>()
    private val loginModel by activityViewModels<LoginModel>()

    val TOGGLE_BUTTON_CHECKED_KEY = "toggle_button_checked"
    var toggleButtonState = false
@@ -51,9 +53,6 @@ class EeloAuthenticatorFragment : Fragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {

        model = ViewModelProviders.of(this).get(EeloAuthenticatorModel::class.java)
        loginModel = ViewModelProviders.of(requireActivity()).get(LoginModel::class.java)

        if (!isNetworkAvailable()) {
            Toast.makeText(context, "Please check your internet connection", Toast.LENGTH_LONG).show()
            requireActivity().finish()
+9 −2
Original line number Diff line number Diff line
@@ -16,11 +16,13 @@

package at.bitfire.davdroid.ui.setup

import android.app.Application
import android.content.Intent
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import at.bitfire.davdroid.ECloudAccountHelper

class EeloAuthenticatorModel : ViewModel() {
class EeloAuthenticatorModel(application: Application) : AndroidViewModel(application) {

    private var initialized = false

@@ -60,4 +62,9 @@ class EeloAuthenticatorModel : ViewModel() {

        initialized = true
    }

    fun blockProceedWithLogin(): Boolean {
        val context = getApplication<Application>()
        return ECloudAccountHelper.alreadyHasECloudAccount(context)
    }
}
Loading