tauri-plugin-app-icon 0.1.0

A Tauri plugin to programmatically change the app icon.
Documentation
package com.plugin.appicon

import android.app.Activity
import android.content.ComponentName
import android.content.pm.PackageManager
import app.tauri.annotation.Command
import app.tauri.annotation.TauriPlugin
import app.tauri.plugin.Invoke
import app.tauri.plugin.Plugin

@TauriPlugin
class AppIconPlugin(private val activity: Activity) : Plugin(activity) {

    private val pm: PackageManager by lazy {
        activity.applicationContext.packageManager
    }

    private val packageName: String by lazy {
        activity.applicationContext.packageName
    }

    private fun componentFor(name: String): ComponentName {
        return ComponentName(packageName, "$packageName.$name")
    }

    @Command
    fun isSupported(invoke: Invoke) {
        invoke.resolveObject(mapOf("value" to true))
    }

    @Command
    fun getName(invoke: Invoke) {
        val intent = pm.getLaunchIntentForPackage(packageName)
        val componentName = intent?.component ?: run {
            invoke.resolveObject(mapOf<String, String?>("value" to null))
            return
        }
        val status = pm.getComponentEnabledSetting(componentName)

        val name = if (status == PackageManager.COMPONENT_ENABLED_STATE_ENABLED ||
            status == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
            val shortName = componentName.shortClassName
            if (shortName == ".MainActivity") null
            else shortName.removePrefix(".")
        } else {
            null
        }

        invoke.resolveObject(mapOf("value" to (name ?: "default")))
    }

    @Command
    fun change(invoke: Invoke) {
        val args = invoke.getArgs()
        val name = args.getString("name") ?: run {
            invoke.reject("Must provide an icon name")
            return
        }

        val disableArray = args.optJSONArray("disable")

        pm.setComponentEnabledSetting(
            componentFor(name),
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP
        )

        if (disableArray != null) {
            for (i in 0 until disableArray.length()) {
                val item = disableArray.optString(i, "")
                if (item.isNotEmpty()) {
                    pm.setComponentEnabledSetting(
                        componentFor(item),
                        PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                        PackageManager.DONT_KILL_APP
                    )
                }
            }
        }

        pm.setComponentEnabledSetting(
            ComponentName(packageName, "$packageName.MainActivity"),
            PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
            PackageManager.DONT_KILL_APP
        )

        invoke.resolve()
    }

    @Command
    fun reset(invoke: Invoke) {
        val args = invoke.getArgs()
        val disableArray = args.optJSONArray("disable")

        pm.setComponentEnabledSetting(
            ComponentName(packageName, "$packageName.MainActivity"),
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP
        )

        if (disableArray != null) {
            for (i in 0 until disableArray.length()) {
                val item = disableArray.optString(i, "")
                if (item.isNotEmpty()) {
                    pm.setComponentEnabledSetting(
                        componentFor(item),
                        PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                        PackageManager.DONT_KILL_APP
                    )
                }
            }
        }

        invoke.resolve()
    }
}