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

Commit b10d431c authored by Ricki Hirner's avatar Ricki Hirner
Browse files

Android 8 notification channel; remember first pending decision

parent c2c0e7f2
Loading
Loading
Loading
Loading
+7 −4
Original line number Diff line number Diff line
@@ -13,7 +13,6 @@ import android.app.Service
import android.content.Context
import android.content.Intent
import android.support.v4.app.NotificationCompat
import android.support.v4.app.NotificationManagerCompat
import android.widget.Toast
import java.io.ByteArrayInputStream
import java.io.File
@@ -117,7 +116,7 @@ class CustomCertService: Service() {

    private fun onReceiveDecision(cert: X509Certificate, trusted: Boolean) {
        // remove notification
        val nm = NotificationManagerCompat.from(this)
        val nm = NotificationUtils.createChannels(this)
        nm.cancel(CertUtils.getTag(cert), Constants.NOTIFICATION_CERT_DECISION)

        // put into trust store, if trusted
@@ -137,6 +136,7 @@ class CustomCertService: Service() {

        // notify receivers which are waiting for a decision
        pendingDecisions[cert]?.let { callbacks ->
            Constants.log.fine("Notifying ${callbacks.size} certificate decision listener(s)")
            callbacks.forEach {
                if (trusted)
                    it.accept()
@@ -192,6 +192,9 @@ class CustomCertService: Service() {
                    if (interactive) {
                        Constants.log.fine("Certificate not known and running in interactive mode, asking user")

                        // remember pending decision
                        pendingDecisions[cert] = mutableListOf(callback)

                        val decisionIntent = Intent(this@CustomCertService, TrustCertificateActivity::class.java)
                        decisionIntent.putExtra(TrustCertificateActivity.EXTRA_CERTIFICATE, raw)

@@ -203,7 +206,7 @@ class CustomCertService: Service() {
                        }

                        val id = Arrays.hashCode(raw)
                        val notify = NotificationCompat.Builder(this@CustomCertService)
                        val notify = NotificationCompat.Builder(this@CustomCertService, NotificationUtils.CHANNEL_CERTIFICATES)
                                .setSmallIcon(R.drawable.ic_lock_open_white)
                                .setContentTitle(this@CustomCertService.getString(R.string.certificate_notification_connection_security))
                                .setContentText(this@CustomCertService.getString(R.string.certificate_notification_user_interaction))
@@ -213,7 +216,7 @@ class CustomCertService: Service() {
                                .setContentIntent(PendingIntent.getActivity(this@CustomCertService, id, decisionIntent, PendingIntent.FLAG_UPDATE_CURRENT))
                                .setDeleteIntent(PendingIntent.getService(this@CustomCertService, id, rejectIntent, PendingIntent.FLAG_UPDATE_CURRENT))
                                .build()
                        val nm = NotificationManagerCompat.from(this@CustomCertService)
                        val nm = NotificationUtils.createChannels(this@CustomCertService)
                        nm.notify(CertUtils.getTag(cert), Constants.NOTIFICATION_CERT_DECISION, notify)

                        if (foreground) {
+30 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) Ricki Hirner (bitfire web engineering).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 */

package at.bitfire.cert4android

import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build

object NotificationUtils {

    val CHANNEL_CERTIFICATES = "cert4android"

    fun createChannels(context: Context): NotificationManager {
        val nm = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        if (Build.VERSION.SDK_INT >= 26)
            nm.createNotificationChannel(NotificationChannel(CHANNEL_CERTIFICATES,
                    context.getString(R.string.certificate_notification_connection_security), NotificationManager.IMPORTANCE_DEFAULT))

        return nm
    }

}
 No newline at end of file