Kynetics/uf-android-client

View on GitHub
uf-client-service/src/main/kotlin/com/kynetics/uf/android/update/application/PackageInstallerBRHandler.kt

Summary

Maintainability
A
0 mins
Test Coverage
/*
 * Copyright © 2017-2023  Kynetics  LLC
 * 
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 */

package com.kynetics.uf.android.update.application

import android.app.PendingIntent
import android.content.*
import android.util.Log

object PackageInstallerBRHandler {
    const val ACTION_INSTALL_COMPLETE = "com.kynetics.action.INSTALL_COMPLETED"

    private var currentReceiver:BroadcastReceiver? = null

    private val intentFilter = IntentFilter(ACTION_INSTALL_COMPLETE)
    private val intent = Intent(ACTION_INSTALL_COMPLETE)

    fun registerReceiver(context: Context, receiver: PackageInstallerBroadcastReceiver){
        unregisterReceiver(context)
        currentReceiver = receiver
        context.registerReceiver(currentReceiver, intentFilter)
    }

    fun createIntentSender(context: Context, requestCode:Int): IntentSender {
        val pendingIntent = PendingIntent.getBroadcast(
                context,
                requestCode,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT)
        return pendingIntent.intentSender
    }

    private fun unregisterReceiver(context: Context){
        if(currentReceiver != null){
            try {
                context.unregisterReceiver(currentReceiver)
            } catch (e:Exception){
                Log.i(TAG, "Can't unregister receiver", e)
            }
        }
    }

    private val TAG = PackageInstallerBRHandler::class.java.simpleName
}