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

Commit 5bba5356 authored by cketti's avatar cketti
Browse files

Convert notification classes to Kotlin

parent ed6cbe1c
Loading
Loading
Loading
Loading
+20 −34
Original line number Diff line number Diff line
package com.fsck.k9.notification;
package com.fsck.k9.notification


class AddNotificationResult {
    private final NotificationHolder notificationHolder;
    private final boolean cancelNotificationBeforeReuse;


    private AddNotificationResult(NotificationHolder notificationHolder,
            boolean cancelNotificationBeforeReuse) {
        this.notificationHolder = notificationHolder;
        this.cancelNotificationBeforeReuse = cancelNotificationBeforeReuse;
    }

    public static AddNotificationResult newNotification(NotificationHolder notificationHolder) {
        return new AddNotificationResult(notificationHolder, false);
internal class AddNotificationResult private constructor(
    val notificationHolder: NotificationHolder,
    @get:JvmName("shouldCancelNotification")
    val shouldCancelNotification: Boolean
) {
    val notificationId: Int
        get() {
            check(shouldCancelNotification) { "shouldCancelNotification == false" }
            return notificationHolder.notificationId
        }

    public static AddNotificationResult replaceNotification(NotificationHolder notificationHolder) {
        return new AddNotificationResult(notificationHolder, true);
    companion object {
        @JvmStatic
        fun newNotification(notificationHolder: NotificationHolder): AddNotificationResult {
            return AddNotificationResult(notificationHolder, shouldCancelNotification = false)
        }

    public boolean shouldCancelNotification() {
        return cancelNotificationBeforeReuse;
        @JvmStatic
        fun replaceNotification(notificationHolder: NotificationHolder): AddNotificationResult {
            return AddNotificationResult(notificationHolder, shouldCancelNotification = true)
        }

    public int getNotificationId() {
        if (!shouldCancelNotification()) {
            throw new IllegalStateException("getNotificationId() can only be called when " +
                    "shouldCancelNotification() returns true");
        }

        return notificationHolder.notificationId;
    }

    public NotificationHolder getNotificationHolder() {
        return notificationHolder;
    }
}
+52 −61
Original line number Diff line number Diff line
package com.fsck.k9.notification;


import android.app.PendingIntent;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationCompat.BigTextStyle;
import androidx.core.app.NotificationManagerCompat;

import com.fsck.k9.Account;

import static com.fsck.k9.notification.NotificationHelper.NOTIFICATION_LED_BLINK_FAST;
import static com.fsck.k9.notification.NotificationHelper.NOTIFICATION_LED_FAILURE_COLOR;


class AuthenticationErrorNotifications {
    private final NotificationHelper notificationHelper;
    private final NotificationActionCreator actionCreator;
    private final NotificationResourceProvider resourceProvider;


    public AuthenticationErrorNotifications(NotificationHelper notificationHelper,
            NotificationActionCreator actionCreator, NotificationResourceProvider resourceProvider) {
        this.notificationHelper = notificationHelper;
        this.actionCreator = actionCreator;
        this.resourceProvider = resourceProvider;
    }

    public void showAuthenticationErrorNotification(Account account, boolean incoming) {
        int notificationId = NotificationIds.getAuthenticationErrorNotificationId(account, incoming);

        PendingIntent editServerSettingsPendingIntent = createContentIntent(account, incoming);
        String title = resourceProvider.authenticationErrorTitle();
        String text =  resourceProvider.authenticationErrorBody(account.getDescription());

        NotificationCompat.Builder builder = notificationHelper
package com.fsck.k9.notification

import android.app.PendingIntent
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.fsck.k9.Account

internal open class AuthenticationErrorNotifications(
    private val notificationHelper: NotificationHelper,
    private val actionCreator: NotificationActionCreator,
    private val resourceProvider: NotificationResourceProvider
) {
    fun showAuthenticationErrorNotification(account: Account, incoming: Boolean) {
        val notificationId = NotificationIds.getAuthenticationErrorNotificationId(account, incoming)
        val editServerSettingsPendingIntent = createContentIntent(account, incoming)
        val title = resourceProvider.authenticationErrorTitle()
        val text = resourceProvider.authenticationErrorBody(account.description)

        val notificationBuilder = notificationHelper
            .createNotificationBuilder(account, NotificationChannelManager.ChannelType.MISCELLANEOUS)
                .setSmallIcon(resourceProvider.getIconWarning())
            .setSmallIcon(resourceProvider.iconWarning)
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
            .setTicker(title)
            .setContentTitle(title)
            .setContentText(text)
            .setContentIntent(editServerSettingsPendingIntent)
                .setStyle(new BigTextStyle().bigText(text))
            .setStyle(NotificationCompat.BigTextStyle().bigText(text))
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setCategory(NotificationCompat.CATEGORY_ERROR);

        notificationHelper.configureNotification(builder, null, null,
                NOTIFICATION_LED_FAILURE_COLOR,
                NOTIFICATION_LED_BLINK_FAST, true);

        getNotificationManager().notify(notificationId, builder.build());
            .setCategory(NotificationCompat.CATEGORY_ERROR)

        notificationHelper.configureNotification(
            builder = notificationBuilder,
            ringtone = null,
            vibrationPattern = null,
            ledColor = NotificationHelper.NOTIFICATION_LED_FAILURE_COLOR,
            ledSpeed = NotificationHelper.NOTIFICATION_LED_BLINK_FAST,
            ringAndVibrate = true
        )

        notificationManager.notify(notificationId, notificationBuilder.build())
    }

    public void clearAuthenticationErrorNotification(Account account, boolean incoming) {
        int notificationId = NotificationIds.getAuthenticationErrorNotificationId(account, incoming);
        getNotificationManager().cancel(notificationId);
    fun clearAuthenticationErrorNotification(account: Account, incoming: Boolean) {
        val notificationId = NotificationIds.getAuthenticationErrorNotificationId(account, incoming)
        notificationManager.cancel(notificationId)
    }

    PendingIntent createContentIntent(Account account, boolean incoming) {
        return incoming ? actionCreator.getEditIncomingServerSettingsIntent(account) :
                actionCreator.getEditOutgoingServerSettingsIntent(account);
    protected open fun createContentIntent(account: Account, incoming: Boolean): PendingIntent {
        return if (incoming) {
            actionCreator.getEditIncomingServerSettingsIntent(account)
        } else {
            actionCreator.getEditOutgoingServerSettingsIntent(account)
        }

    private NotificationManagerCompat getNotificationManager() {
        return notificationHelper.getNotificationManager();
    }

    private val notificationManager: NotificationManagerCompat
        get() = notificationHelper.getNotificationManager()
}
+52 −69
Original line number Diff line number Diff line
package com.fsck.k9.notification;


import android.app.PendingIntent;
import android.content.Context;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationCompat.BigTextStyle;
import androidx.core.app.NotificationCompat.Builder;

import com.fsck.k9.Account;
import com.fsck.k9.K9;
import com.fsck.k9.K9.NotificationQuickDelete;


abstract class BaseNotifications {
    protected final Context context;
    protected final NotificationHelper notificationHelper;
    protected final NotificationActionCreator actionCreator;
    protected final NotificationResourceProvider resourceProvider;


    protected BaseNotifications(NotificationHelper notificationHelper, NotificationActionCreator actionCreator,
            NotificationResourceProvider resourceProvider) {
        this.context = notificationHelper.getContext();
        this.notificationHelper = notificationHelper;
        this.actionCreator = actionCreator;
        this.resourceProvider = resourceProvider;
    }

    protected NotificationCompat.Builder createBigTextStyleNotification(Account account, NotificationHolder holder,
            int notificationId) {
        String accountName = notificationHelper.getAccountName(account);
        NotificationContent content = holder.content;
        String groupKey = NotificationGroupKeys.getGroupKey(account);

        NotificationCompat.Builder builder = createAndInitializeNotificationBuilder(account)
package com.fsck.k9.notification

import android.content.Context
import androidx.core.app.NotificationCompat
import com.fsck.k9.Account
import com.fsck.k9.K9
import com.fsck.k9.K9.NotificationQuickDelete

internal abstract class BaseNotifications(
    protected val notificationHelper: NotificationHelper,
    protected val actionCreator: NotificationActionCreator,
    protected val resourceProvider: NotificationResourceProvider
) {
    protected val context: Context = notificationHelper.getContext()

    fun createBigTextStyleNotification(
        account: Account,
        holder: NotificationHolder,
        notificationId: Int
    ): NotificationCompat.Builder {
        val accountName = notificationHelper.getAccountName(account)
        val content = holder.content
        val groupKey = NotificationGroupKeys.getGroupKey(account)

        val builder = createAndInitializeNotificationBuilder(account)
            .setTicker(content.summary)
            .setGroup(groupKey)
            .setContentTitle(content.sender)
            .setContentText(content.subject)
                .setSubText(accountName);
            .setSubText(accountName)

        NotificationCompat.BigTextStyle style = createBigTextStyle(builder);
        style.bigText(content.preview);
        val style = createBigTextStyle(builder)
        style.bigText(content.preview)
        builder.setStyle(style)

        builder.setStyle(style);
        val contentIntent = actionCreator.createViewMessagePendingIntent(content.messageReference, notificationId)
        builder.setContentIntent(contentIntent)

        PendingIntent contentIntent = actionCreator.createViewMessagePendingIntent(
                content.messageReference, notificationId);
        builder.setContentIntent(contentIntent);

        return builder;
        return builder
    }

    protected NotificationCompat.Builder createAndInitializeNotificationBuilder(Account account) {
        return notificationHelper.createNotificationBuilder(account,
                NotificationChannelManager.ChannelType.MESSAGES)
                .setSmallIcon(getNewMailNotificationIcon())
                .setColor(account.getChipColor())
    fun createAndInitializeNotificationBuilder(account: Account): NotificationCompat.Builder {
        return notificationHelper.createNotificationBuilder(account, NotificationChannelManager.ChannelType.MESSAGES)
            .setSmallIcon(resourceProvider.iconNewMail)
            .setColor(account.chipColor)
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
                .setCategory(NotificationCompat.CATEGORY_EMAIL);
    }

    protected boolean isDeleteActionEnabled() {
        NotificationQuickDelete deleteOption = K9.getNotificationQuickDeleteBehaviour();
        return deleteOption == NotificationQuickDelete.ALWAYS || deleteOption == NotificationQuickDelete.FOR_SINGLE_MSG;
            .setCategory(NotificationCompat.CATEGORY_EMAIL)
    }

    protected BigTextStyle createBigTextStyle(Builder builder) {
        return new BigTextStyle(builder);
    fun isDeleteActionEnabled(): Boolean {
        val deleteOption = K9.notificationQuickDeleteBehaviour
        return deleteOption === NotificationQuickDelete.ALWAYS ||
            deleteOption === NotificationQuickDelete.FOR_SINGLE_MSG
    }

    private int getNewMailNotificationIcon() {
        return resourceProvider.getIconNewMail();
    protected open fun createBigTextStyle(builder: NotificationCompat.Builder?): NotificationCompat.BigTextStyle {
        return NotificationCompat.BigTextStyle(builder)
    }
}
+52 −62
Original line number Diff line number Diff line
package com.fsck.k9.notification;


import android.app.PendingIntent;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationCompat.BigTextStyle;
import androidx.core.app.NotificationManagerCompat;

import com.fsck.k9.Account;

import static com.fsck.k9.notification.NotificationHelper.NOTIFICATION_LED_BLINK_FAST;
import static com.fsck.k9.notification.NotificationHelper.NOTIFICATION_LED_FAILURE_COLOR;


class CertificateErrorNotifications {
    private final NotificationHelper notificationHelper;
    private final NotificationActionCreator actionCreator;
    private final NotificationResourceProvider resourceProvider;


    public CertificateErrorNotifications(NotificationHelper notificationHelper,
            NotificationActionCreator actionCreator, NotificationResourceProvider resourceProvider) {
        this.notificationHelper = notificationHelper;
        this.actionCreator = actionCreator;
        this.resourceProvider = resourceProvider;
    }

    public void showCertificateErrorNotification(Account account, boolean incoming) {
        int notificationId = NotificationIds.getCertificateErrorNotificationId(account, incoming);

        PendingIntent editServerSettingsPendingIntent = createContentIntent(account, incoming);
        String title = resourceProvider.certificateErrorTitle(account.getDescription());
        String text = resourceProvider.certificateErrorBody();

        NotificationCompat.Builder builder = notificationHelper
package com.fsck.k9.notification

import android.app.PendingIntent
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.fsck.k9.Account

internal open class CertificateErrorNotifications(
    private val notificationHelper: NotificationHelper,
    private val actionCreator: NotificationActionCreator,
    private val resourceProvider: NotificationResourceProvider
) {
    fun showCertificateErrorNotification(account: Account, incoming: Boolean) {
        val notificationId = NotificationIds.getCertificateErrorNotificationId(account, incoming)
        val editServerSettingsPendingIntent = createContentIntent(account, incoming)
        val title = resourceProvider.certificateErrorTitle(account.description)
        val text = resourceProvider.certificateErrorBody()

        val notificationBuilder = notificationHelper
            .createNotificationBuilder(account, NotificationChannelManager.ChannelType.MISCELLANEOUS)
                .setSmallIcon(resourceProvider.getIconWarning())
            .setSmallIcon(resourceProvider.iconWarning)
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
            .setTicker(title)
            .setContentTitle(title)
            .setContentText(text)
            .setContentIntent(editServerSettingsPendingIntent)
                .setStyle(new BigTextStyle().bigText(text))
            .setStyle(NotificationCompat.BigTextStyle().bigText(text))
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setCategory(NotificationCompat.CATEGORY_ERROR);

        notificationHelper.configureNotification(builder, null, null,
                NOTIFICATION_LED_FAILURE_COLOR,
                NOTIFICATION_LED_BLINK_FAST, true);

        getNotificationManager().notify(notificationId, builder.build());
            .setCategory(NotificationCompat.CATEGORY_ERROR)

        notificationHelper.configureNotification(
            builder = notificationBuilder,
            ringtone = null,
            vibrationPattern = null,
            ledColor = NotificationHelper.NOTIFICATION_LED_FAILURE_COLOR,
            ledSpeed = NotificationHelper.NOTIFICATION_LED_BLINK_FAST,
            ringAndVibrate = true
        )

        notificationManager.notify(notificationId, notificationBuilder.build())
    }

    public void clearCertificateErrorNotifications(Account account, boolean incoming) {
        int notificationId = NotificationIds.getCertificateErrorNotificationId(account, incoming);
        getNotificationManager().cancel(notificationId);
    fun clearCertificateErrorNotifications(account: Account, incoming: Boolean) {
        val notificationId = NotificationIds.getCertificateErrorNotificationId(account, incoming)
        notificationManager.cancel(notificationId)
    }

    PendingIntent createContentIntent(Account account, boolean incoming) {
        return incoming ?
                actionCreator.getEditIncomingServerSettingsIntent(account) :
                actionCreator.getEditOutgoingServerSettingsIntent(account);
    protected open fun createContentIntent(account: Account, incoming: Boolean): PendingIntent {
        return if (incoming) {
            actionCreator.getEditIncomingServerSettingsIntent(account)
        } else {
            actionCreator.getEditOutgoingServerSettingsIntent(account)
        }

    private NotificationManagerCompat getNotificationManager() {
        return notificationHelper.getNotificationManager();
    }

    private val notificationManager: NotificationManagerCompat
        get() = notificationHelper.getNotificationManager()
}
+173 −187

File changed.

Preview size limit exceeded, changes collapsed.

Loading