Group plugins by package
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package de.mm20.launcher2.sdk
|
||||
|
||||
import android.app.PendingIntent
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import de.mm20.launcher2.sdk.base.BasePluginProvider
|
||||
|
||||
|
||||
sealed class PluginState {
|
||||
/**
|
||||
* Plugin is ready to be used.
|
||||
*/
|
||||
data class Ready(
|
||||
/**
|
||||
* Status text, providing additional info what this plugin is currently configured to do.
|
||||
* For example "Search %user's files on %service"
|
||||
*/
|
||||
val text: String? = null,
|
||||
) : PluginState()
|
||||
|
||||
/**
|
||||
* Plugin requires some setup, e.g. user needs to login to a service.
|
||||
*/
|
||||
data class SetupRequired(
|
||||
/**
|
||||
* Activity to start to setup the plugin.
|
||||
*/
|
||||
val setupActivity: Intent,
|
||||
/**
|
||||
* Optional message to display to the user, describing what needs to be done to setup the plugin.
|
||||
*/
|
||||
val message: String? = null,
|
||||
) : PluginState()
|
||||
}
|
||||
@@ -1,12 +1,13 @@
|
||||
package de.mm20.launcher2.sdk.base
|
||||
|
||||
import android.app.PendingIntent
|
||||
import android.content.ContentProvider
|
||||
import android.content.Context
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Bundle
|
||||
import de.mm20.launcher2.plugin.PluginState
|
||||
import de.mm20.launcher2.plugin.PluginType
|
||||
import de.mm20.launcher2.plugin.contracts.PluginContract
|
||||
import de.mm20.launcher2.sdk.PluginState
|
||||
import kotlinx.coroutines.runBlocking
|
||||
|
||||
abstract class BasePluginProvider : ContentProvider() {
|
||||
@@ -17,23 +18,14 @@ abstract class BasePluginProvider : ContentProvider() {
|
||||
putString("type", getPluginType().name)
|
||||
}
|
||||
|
||||
PluginContract.Methods.GetState -> Bundle().apply {
|
||||
PluginContract.Methods.GetState -> {
|
||||
val state = runBlocking {
|
||||
getPluginState()
|
||||
}
|
||||
|
||||
when (state) {
|
||||
is PluginState.SetupRequired -> {
|
||||
putString("type", "SetupRequired")
|
||||
putString("setupActivity", state.setupActivity)
|
||||
putString("message", state.message)
|
||||
}
|
||||
|
||||
is PluginState.Ready -> {
|
||||
putString("type", "Ready")
|
||||
}
|
||||
}
|
||||
return state.toBundle()
|
||||
}
|
||||
|
||||
PluginContract.Methods.GetConfig -> {
|
||||
getPluginConfig()
|
||||
}
|
||||
@@ -49,7 +41,7 @@ abstract class BasePluginProvider : ContentProvider() {
|
||||
}
|
||||
|
||||
open suspend fun getPluginState(): PluginState {
|
||||
return PluginState.Ready
|
||||
return PluginState.Ready()
|
||||
}
|
||||
|
||||
internal fun checkPermissionOrThrow(context: Context) {
|
||||
@@ -59,4 +51,31 @@ abstract class BasePluginProvider : ContentProvider() {
|
||||
throw SecurityException("Caller does not have permission to use plugins")
|
||||
}
|
||||
|
||||
private fun PluginState.toBundle(): Bundle {
|
||||
when (this) {
|
||||
is PluginState.Ready -> {
|
||||
return Bundle().apply {
|
||||
putString("type", "Ready")
|
||||
putString("text", text)
|
||||
}
|
||||
}
|
||||
|
||||
is PluginState.SetupRequired -> {
|
||||
val requestCode = (this::class.qualifiedName + "-setup").hashCode()
|
||||
return Bundle().apply {
|
||||
putString("type", "SetupRequired")
|
||||
putParcelable(
|
||||
"setupActivity",
|
||||
PendingIntent.getActivity(
|
||||
context,
|
||||
requestCode,
|
||||
setupActivity,
|
||||
PendingIntent.FLAG_IMMUTABLE,
|
||||
)
|
||||
)
|
||||
putString("message", message)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user