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

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

feature: Add very basic code structure to notifiy a request to user to switch...

feature: Add very basic code structure to notifiy a request to user to switch to openID for /e/ account
parent 405cb5a6
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -703,6 +703,12 @@
            </intent-filter>
        </receiver>

        <receiver
            android:name=".receiver.LogoutReceiver"
            android:exported="true"
            android:enabled="true">
        </receiver>

        <receiver
            android:name=".receiver.AccountRemovedReceiver"
            android:enabled="true"
+71 −0
Original line number Diff line number Diff line
@@ -4,12 +4,20 @@

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 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

/**
 * There are circumstances when Android drops automatic sync of accounts and resets them
@@ -22,12 +30,75 @@ 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)

        val accountManager = AccountManager.get(context)

        // sync intervals are checked in App.onCreate()
        AccountUtils.getMainAccounts(context)
            .forEach {
                val accountSettings = AccountSettings(context, it)
                accountSettings.initSync()

                if (it.type.equals(eAccountType) && !isLoggedWithOpenId(it, accountManager)) {
                    notifyOpenIdSwitchRequest(it, notificationManager, 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 isPasswordNull = accountManager.getPassword(account).isNullOrEmpty()

        return isPasswordNull && authToken != null
    }

    private fun notifyOpenIdSwitchRequest(account: Account,
                                          notificationManager: NotificationManager,
                                          context: Context) {
        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 notification = NotificationCompat.Builder(context, "your_channel_id")
            .setContentTitle(notifTitle)
            .setContentText(notifText)
            .setSmallIcon(R.drawable.ic_info)
            .setContentIntent(notifIntent)
            .setAutoCancel(true)
            .build()

        notificationManager.notify(notifId, notification)
    }

    private fun generateLogOutIntent(accountName: String, context: Context): PendingIntent {
        val logoutIntent = Intent(context, LogoutReceiver::class.java).apply {
            putExtra("ACCOUNT_NAME", accountName)
        }

        return  PendingIntent.getBroadcast(context,
            0,
            logoutIntent,
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
    }
}
 No newline at end of file
+11 −0
Original line number Diff line number Diff line
package at.bitfire.davdroid.receiver

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent

class LogoutReceiver(): BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        TODO("Not yet implemented")
    }
}
 No newline at end of file