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

Commit a1c399c4 authored by Vincent Bourgmayer's avatar Vincent Bourgmayer
Browse files

refactor: replace checking AUTH_TOKEN by checking if auth_state is in user data

refactor: minor various refactoring like adding notification title & text in strings.xml
parent df7cda79
Loading
Loading
Loading
Loading
+13 −28
Original line number Diff line number Diff line
@@ -6,18 +6,17 @@ package at.bitfire.davdroid.receiver

import android.accounts.Account
import android.accounts.AccountManager
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import at.bitfire.davdroid.R
import at.bitfire.davdroid.log.Logger
import at.bitfire.davdroid.settings.AccountSettings
import at.bitfire.davdroid.syncadapter.AccountUtils
import com.owncloud.android.lib.common.accounts.AccountTypeUtils
import at.bitfire.davdroid.ui.NotificationUtils.notifyIfPossible

/**
 * There are circumstances when Android drops automatic sync of accounts and resets them
@@ -30,7 +29,6 @@ class BootCompletedReceiver: BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        Logger.log.info("Device has been rebooted; checking sync intervals etc.")
        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        val isOpenIdAvailable = isOpenIdAvailable()
        val eAccountType = context.getString(R.string.eelo_account_type)
@@ -44,41 +42,27 @@ class BootCompletedReceiver: BroadcastReceiver() {
                accountSettings.initSync()

                if (it.type.equals(eAccountType) && !isLoggedWithOpenId(it, accountManager)) {
                    notifyOpenIdSwitchRequest(it, notificationManager, context)
                    notifySwitchToOpenId(it, context)
                }
            }
    }

    /**
     * Check if OpenID is implemented in current version of AccountManager
     */
    private fun isOpenIdAvailable(): Boolean {
        return false
    }

    private fun isLoggedWithOpenId(account: Account, accountManager: AccountManager): Boolean {
        val authTokenType = AccountTypeUtils.getAuthTokenTypePass(account.type)

        val authToken = accountManager.getAuthToken(account, authTokenType, Bundle(), false, { future ->
            try {
                val token = future.result.getString(AccountManager.KEY_AUTHTOKEN)
            } catch (e: Exception) {
                println("Error retrieving token for account ${account.name}: ${e.message}")
            }
        }, null)

        val hasAuthStateData = accountManager.getUserData(account, AccountSettings.KEY_AUTH_STATE) != null
        val isPasswordNull = accountManager.getPassword(account).isNullOrEmpty()

        return isPasswordNull && authToken != null
        return isPasswordNull && hasAuthStateData
    }

    private fun notifyOpenIdSwitchRequest(account: Account,
                                          notificationManager: NotificationManager,
                                          context: Context) {
    private fun notifySwitchToOpenId(account: Account, context: Context) {
        val notifTag = "Switch to openID"
        val notifId = 3310
        val notifTitle = "${account.name}: Switch to OpenId"
        val notifText = "Now that OpenId is available, you should log out and re-loggin your account."
        val notifIntent = generateLogOutIntent(account.name, context)
        val notifTitle = context.getString(R.string.notification_account_title)
        val notifText = context.getString(R.string.notification_switch_to_openId_text)
        val notifIntent = generateLogoutIntent(account.name, context)

        val notification = NotificationCompat.Builder(context, "your_channel_id")
            .setContentTitle(notifTitle)
@@ -88,10 +72,11 @@ class BootCompletedReceiver: BroadcastReceiver() {
            .setAutoCancel(true)
            .build()

        notificationManager.notify(notifId, notification)
        val notificationManager = NotificationManagerCompat.from(context)
        notificationManager.notifyIfPossible(notifTag, notifId, notification)
    }

    private fun generateLogOutIntent(accountName: String, context: Context): PendingIntent {
    private fun generateLogoutIntent(accountName: String, context: Context): PendingIntent {
        val logoutIntent = Intent(context, LogoutReceiver::class.java).apply {
            putExtra("ACCOUNT_NAME", accountName)
        }
+3 −0
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@
    <string name="database_destructive_migration_title">Database corrupted</string>
    <string name="database_destructive_migration_text">All accounts have been removed locally.</string>

    <!-- Notifications -->
    <string name="notification_channel_debugging">Debugging</string>
    <string name="notification_channel_general">Other important messages</string>
    <string name="notification_channel_status">Low-priority status messages</string>
@@ -53,6 +54,8 @@
    <string name="notification_channel_sync_warnings_desc">Non-fatal synchronization problems like certain invalid files</string>
    <string name="notification_channel_sync_io_errors">Network and I/O errors</string>
    <string name="notification_channel_sync_io_errors_desc">Timeouts, connection problems, etc. (often temporary)</string>
    <string name="notification_account_title">Your account %1$s</string>
    <string name="notification_switch_to_openId_text">A new login service for a better experience is available. Tap the notification to start using it.</string>

    <!-- IntroActivity -->
    <string name="intro_slogan1">Your data. Your choice.</string>