Plugins: bringup
This commit is contained in:
@@ -0,0 +1 @@
|
||||
/build
|
||||
@@ -0,0 +1,45 @@
|
||||
plugins {
|
||||
alias(libs.plugins.android.library)
|
||||
alias(libs.plugins.kotlin.android)
|
||||
}
|
||||
|
||||
android {
|
||||
compileSdk = libs.versions.compileSdk.get().toInt()
|
||||
|
||||
defaultConfig {
|
||||
minSdk = libs.versions.minSdk.get().toInt()
|
||||
|
||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||
consumerProguardFiles("consumer-rules.pro")
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
proguardFiles(
|
||||
getDefaultProguardFile("proguard-android-optimize.txt"),
|
||||
"proguard-rules.pro"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
sourceCompatibility = JavaVersion.VERSION_1_8
|
||||
targetCompatibility = JavaVersion.VERSION_1_8
|
||||
}
|
||||
|
||||
kotlinOptions {
|
||||
jvmTarget = "1.8"
|
||||
}
|
||||
namespace = "de.mm20.launcher2.services.plugins"
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(libs.bundles.kotlin)
|
||||
implementation(libs.androidx.core)
|
||||
implementation(libs.koin.android)
|
||||
|
||||
implementation(project(":core:ktx"))
|
||||
implementation(project(":core:base"))
|
||||
implementation(project(":core:crashreporter"))
|
||||
|
||||
}
|
||||
Vendored
+21
@@ -0,0 +1,21 @@
|
||||
# Add project specific ProGuard rules here.
|
||||
# You can control the set of applied configuration files using the
|
||||
# proguardFiles setting in build.gradle.
|
||||
#
|
||||
# For more details, see
|
||||
# http://developer.android.com/guide/developing/tools/proguard.html
|
||||
|
||||
# If your project uses WebView with JS, uncomment the following
|
||||
# and specify the fully qualified class name to the JavaScript interface
|
||||
# class:
|
||||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
|
||||
# public *;
|
||||
#}
|
||||
|
||||
# Uncomment this to preserve the line number information for
|
||||
# debugging stack traces.
|
||||
#-keepattributes SourceFile,LineNumberTable
|
||||
|
||||
# If you keep the line number information, uncomment this to
|
||||
# hide the original source file name.
|
||||
#-renamesourcefileattribute SourceFile
|
||||
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
</manifest>
|
||||
@@ -0,0 +1,8 @@
|
||||
package de.mm20.launcher2.plugins
|
||||
|
||||
import org.koin.android.ext.koin.androidContext
|
||||
import org.koin.dsl.module
|
||||
|
||||
val servicesPluginsModule = module {
|
||||
single<PluginService> { PluginServiceImpl(androidContext(), get()) }
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.net.Uri
|
||||
import de.mm20.launcher2.plugin.Plugin
|
||||
import de.mm20.launcher2.plugin.PluginType
|
||||
|
||||
class PluginScanner(
|
||||
private val context: Context,
|
||||
) {
|
||||
suspend fun findPlugins(): List<Plugin> {
|
||||
val contentResolvers = context.packageManager.queryIntentContentProviders(
|
||||
Intent("de.mm20.launcher2.action.PLUGIN"),
|
||||
PackageManager.GET_META_DATA,
|
||||
)
|
||||
val plugins = mutableListOf<Plugin>()
|
||||
|
||||
for (cr in contentResolvers) {
|
||||
val providerInfo = cr.providerInfo ?: continue
|
||||
val authority = providerInfo.authority ?: continue
|
||||
val bundle = context.contentResolver.call(
|
||||
Uri.Builder()
|
||||
.scheme("content")
|
||||
.authority(authority)
|
||||
.build(),
|
||||
"getType",
|
||||
null,
|
||||
null,
|
||||
) ?: continue
|
||||
val type = bundle.getString("type")
|
||||
?.let {
|
||||
try {
|
||||
PluginType.valueOf(it)
|
||||
} catch (e: IllegalArgumentException) {
|
||||
null
|
||||
}
|
||||
} ?: continue
|
||||
plugins.add(
|
||||
Plugin(
|
||||
label = cr.loadLabel(context.packageManager).toString(),
|
||||
description = providerInfo.metaData?.getString("de.mm20.launcher2.plugin.description"),
|
||||
packageName = providerInfo.packageName,
|
||||
className = providerInfo.name,
|
||||
type = type,
|
||||
authority = authority,
|
||||
settingsActivity = providerInfo.metaData?.getString("de.mm20.launcher2.plugin.settings"),
|
||||
enabled = false,
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
return plugins
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
package de.mm20.launcher2.plugins
|
||||
|
||||
import PluginScanner
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.net.Uri
|
||||
import androidx.core.content.ContextCompat
|
||||
import de.mm20.launcher2.plugin.Plugin
|
||||
import de.mm20.launcher2.plugin.PluginRepository
|
||||
import de.mm20.launcher2.plugin.PluginState
|
||||
import de.mm20.launcher2.plugin.PluginType
|
||||
import de.mm20.launcher2.plugin.contracts.PluginContract
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
data class PluginWithState(
|
||||
val plugin: Plugin,
|
||||
val state: PluginState?,
|
||||
)
|
||||
|
||||
interface PluginService {
|
||||
fun enablePlugin(plugin: Plugin)
|
||||
fun disablePlugin(plugin: Plugin)
|
||||
fun getPluginsWithState(type: PluginType? = null): Flow<List<PluginWithState>>
|
||||
suspend fun getPluginState(plugin: Plugin): PluginState?
|
||||
}
|
||||
|
||||
internal class PluginServiceImpl(
|
||||
private val context: Context,
|
||||
private val repository: PluginRepository,
|
||||
) : PluginService {
|
||||
|
||||
private val scope: CoroutineScope = CoroutineScope(Job() + Dispatchers.Default)
|
||||
|
||||
init {
|
||||
refreshPlugins()
|
||||
ContextCompat.registerReceiver(
|
||||
context,
|
||||
AppUpdateReceiver(),
|
||||
IntentFilter().apply {
|
||||
addAction(Intent.ACTION_PACKAGE_ADDED)
|
||||
addAction(Intent.ACTION_PACKAGE_REMOVED)
|
||||
addAction(Intent.ACTION_PACKAGE_REPLACED)
|
||||
addAction(Intent.ACTION_PACKAGE_CHANGED)
|
||||
},
|
||||
ContextCompat.RECEIVER_NOT_EXPORTED
|
||||
)
|
||||
}
|
||||
|
||||
override fun enablePlugin(plugin: Plugin) {
|
||||
repository.update(plugin.copy(enabled = true))
|
||||
}
|
||||
|
||||
override fun disablePlugin(plugin: Plugin) {
|
||||
repository.update(plugin.copy(enabled = false))
|
||||
}
|
||||
|
||||
private val mutex = Mutex()
|
||||
private fun refreshPlugins() {
|
||||
scope.launch {
|
||||
mutex.withLock {
|
||||
val enabledPlugins =
|
||||
repository.findMany(enabled = true).first().map { it.authority }
|
||||
val scanner = PluginScanner(context)
|
||||
val plugins = scanner.findPlugins().map {
|
||||
if (it.authority in enabledPlugins) {
|
||||
it.copy(enabled = true)
|
||||
} else {
|
||||
it
|
||||
}
|
||||
}
|
||||
repository.deleteMany()
|
||||
repository.insertMany(plugins)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun getPluginsWithState(
|
||||
type: PluginType?,
|
||||
): Flow<List<PluginWithState>> {
|
||||
return repository.findMany(
|
||||
type = type,
|
||||
).map {
|
||||
it.map {
|
||||
PluginWithState(
|
||||
plugin = it,
|
||||
state = getPluginState(it),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun getPluginState(plugin: Plugin): PluginState? {
|
||||
val bundle = withContext(Dispatchers.IO) {
|
||||
context.contentResolver.call(
|
||||
Uri.Builder()
|
||||
.scheme("content")
|
||||
.authority(plugin.authority)
|
||||
.build(),
|
||||
PluginContract.Methods.GetState,
|
||||
null,
|
||||
null
|
||||
)
|
||||
} ?: return null
|
||||
val type = bundle.getString("type") ?: return null
|
||||
return when (type) {
|
||||
"Ready" -> PluginState.Ready
|
||||
"SetupRequired" -> {
|
||||
val setupActivity = bundle.getString("setupActivity") ?: return null
|
||||
val message = bundle.getString("message")
|
||||
PluginState.SetupRequired(
|
||||
setupActivity = setupActivity,
|
||||
message = message,
|
||||
)
|
||||
}
|
||||
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
private inner class AppUpdateReceiver : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context?, intent: Intent?) {
|
||||
refreshPlugins()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user