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

Unverified Commit e0fe1a2d authored by cketti's avatar cketti Committed by GitHub
Browse files

Merge pull request #5618 from k9mail/convert_to_kotlin

Convert classes in the "notification" package to Kotlin
parents d9bf7b49 5bba5356
Loading
Loading
Loading
Loading
+0 −39
Original line number Diff line number Diff line
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);
    }

    public static AddNotificationResult replaceNotification(NotificationHolder notificationHolder) {
        return new AddNotificationResult(notificationHolder, true);
    }

    public boolean shouldCancelNotification() {
        return cancelNotificationBeforeReuse;
    }

    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;
    }
}
+25 −0
Original line number Diff line number Diff line
package com.fsck.k9.notification

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
        }

    companion object {
        @JvmStatic
        fun newNotification(notificationHolder: NotificationHolder): AddNotificationResult {
            return AddNotificationResult(notificationHolder, shouldCancelNotification = false)
        }

        @JvmStatic
        fun replaceNotification(notificationHolder: NotificationHolder): AddNotificationResult {
            return AddNotificationResult(notificationHolder, shouldCancelNotification = true)
        }
    }
}
+0 −68
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
                .createNotificationBuilder(account, NotificationChannelManager.ChannelType.MISCELLANEOUS)
                .setSmallIcon(resourceProvider.getIconWarning())
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(true)
                .setTicker(title)
                .setContentTitle(title)
                .setContentText(text)
                .setContentIntent(editServerSettingsPendingIntent)
                .setStyle(new 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());
    }

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

    PendingIntent createContentIntent(Account account, boolean incoming) {
        return incoming ? actionCreator.getEditIncomingServerSettingsIntent(account) :
                actionCreator.getEditOutgoingServerSettingsIntent(account);
    }

    private NotificationManagerCompat getNotificationManager() {
        return notificationHelper.getNotificationManager();
    }
}
+59 −0
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.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.iconWarning)
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
            .setTicker(title)
            .setContentTitle(title)
            .setContentText(text)
            .setContentIntent(editServerSettingsPendingIntent)
            .setStyle(NotificationCompat.BigTextStyle().bigText(text))
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .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())
    }

    fun clearAuthenticationErrorNotification(account: Account, incoming: Boolean) {
        val notificationId = NotificationIds.getAuthenticationErrorNotificationId(account, incoming)
        notificationManager.cancel(notificationId)
    }

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

    private val notificationManager: NotificationManagerCompat
        get() = notificationHelper.getNotificationManager()
}
+0 −77
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)
                .setTicker(content.summary)
                .setGroup(groupKey)
                .setContentTitle(content.sender)
                .setContentText(content.subject)
                .setSubText(accountName);

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

        builder.setStyle(style);

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

        return builder;
    }

    protected NotificationCompat.Builder createAndInitializeNotificationBuilder(Account account) {
        return notificationHelper.createNotificationBuilder(account,
                NotificationChannelManager.ChannelType.MESSAGES)
                .setSmallIcon(getNewMailNotificationIcon())
                .setColor(account.getChipColor())
                .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;
    }

    protected BigTextStyle createBigTextStyle(Builder builder) {
        return new BigTextStyle(builder);
    }

    private int getNewMailNotificationIcon() {
        return resourceProvider.getIconNewMail();
    }
}
Loading