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

Commit 1546be24 authored by Vincent Bourgmayer's avatar Vincent Bourgmayer
Browse files

chore: remove ReLoginWithOidcService & add Hilt annotation

parent 1ab1c25f
Loading
Loading
Loading
Loading
Loading
+0 −3
Original line number Diff line number Diff line
@@ -192,9 +192,6 @@
        <activity android:name=".ReLoginWithOidcActivity"
            android:exported="false"/>
        <!-- account type "DAVx⁵" -->
        <service
            android:name=".ReLoginWithOidcService"
            android:exported="false"/>
        <service
            android:name=".syncadapter.AccountAuthenticatorService"
            android:exported="false">
+2 −0
Original line number Diff line number Diff line
@@ -20,7 +20,9 @@ import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import at.bitfire.davdroid.log.Logger
import at.bitfire.davdroid.ui.setup.EeloAuthenticatorFragment
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class ReLoginWithOidcActivity: AppCompatActivity() {

    private lateinit var eAccountType: String
+0 −109
Original line number Diff line number Diff line
/*
 * Copyright MURENA SAS 2024
 * 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.accounts.AccountManagerFuture
import android.app.PendingIntent
import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
import android.os.Bundle
import android.os.IBinder
import at.bitfire.davdroid.log.Logger
class ReLoginWithOidcService: Service() {

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        val context = this.applicationContext

        val accountManager = AccountManager.get(context)
        val eAccountType = context.getString(R.string.eelo_account_type)

        val accountRemoved = removeAccount(eAccountType, accountManager)

        if (accountRemoved) {
            openEAccountLoginForm(accountManager, eAccountType, context)
        } else {
            stopSelf()
        }

        return START_STICKY
    }

    private fun removeAccount(accountType: String, accountManager: AccountManager): Boolean {
        val account = accountManager.getAccountsByType(accountType).firstOrNull()

        return account?.let {
            accountManager.removeAccountExplicitly(it)
        } ?: false
    }

    private fun openEAccountLoginForm(accountManager: AccountManager, eAccountType: String, context: Context) {
        accountManager.addAccount(eAccountType,
            null,
            null,
            null,
            null,
             { future ->
                val intent = getIntentFromFuture(future)

                if (intent == null) {
                    stopSelf()
                    return@addAccount
                }

                intent.apply {
                        flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
                    }

                val pendingIntent = PendingIntent.getActivity(context,
                    0,
                    intent,
                    PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)

                try {
                    pendingIntent.send()
                } catch (exception: Exception){
                    Logger.log.warning("ReLogin: Can't start login activity:  ${exception.message}")
                }

                stopSelf()
            },
            null)
    }

    private fun getIntentFromFuture(future: AccountManagerFuture<Bundle>): Intent? {
        val bundle = future.result

        return try {
            if (VERSION.SDK_INT >= VERSION_CODES.TIRAMISU) {
                bundle?.getParcelable(AccountManager.KEY_INTENT, Intent::class.java)
            } else {
                @Suppress("DEPRECATION")
                bundle?.getParcelable(AccountManager.KEY_INTENT)
            }
        } catch (exception: Exception) {
            Logger.log.warning("${exception.javaClass}: can't add account: ${exception.message}")
            null
        }
    }

    override fun onBind(intent: Intent?): IBinder? {
        return null
    }
}
+0 −1
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@ import android.os.Build.VERSION_CODES
import androidx.core.app.NotificationManagerCompat
import at.bitfire.davdroid.R
import at.bitfire.davdroid.ReLoginWithOidcActivity
import at.bitfire.davdroid.ReLoginWithOidcService
import at.bitfire.davdroid.log.Logger
import at.bitfire.davdroid.settings.AccountSettings
import at.bitfire.davdroid.syncadapter.AccountUtils