|
| 1 | +package io.homeassistant.companion.android.widgets.grid |
| 2 | + |
| 3 | +import android.appwidget.AppWidgetManager |
| 4 | +import android.appwidget.AppWidgetProvider |
| 5 | +import android.content.Context |
| 6 | +import android.content.Intent |
| 7 | +import android.os.Bundle |
| 8 | +import android.util.Log |
| 9 | +import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper |
| 10 | +import com.fasterxml.jackson.module.kotlin.readValue |
| 11 | +import dagger.hilt.android.AndroidEntryPoint |
| 12 | +import io.homeassistant.companion.android.common.data.servers.ServerManager |
| 13 | +import io.homeassistant.companion.android.database.widget.GridWidgetDao |
| 14 | +import io.homeassistant.companion.android.widgets.common.WidgetAuthenticationActivity |
| 15 | +import java.util.regex.Pattern |
| 16 | +import javax.inject.Inject |
| 17 | +import kotlin.text.split |
| 18 | +import kotlinx.coroutines.CoroutineScope |
| 19 | +import kotlinx.coroutines.Dispatchers |
| 20 | +import kotlinx.coroutines.Job |
| 21 | +import kotlinx.coroutines.launch |
| 22 | + |
| 23 | +@AndroidEntryPoint |
| 24 | +class GridWidget : AppWidgetProvider() { |
| 25 | + companion object { |
| 26 | + private const val TAG = "GridWidget" |
| 27 | + const val CALL_SERVICE = |
| 28 | + "io.homeassistant.companion.android.widgets.grid.GridWidget.CALL_SERVICE" |
| 29 | + const val CALL_SERVICE_AUTH = |
| 30 | + "io.homeassistant.companion.android.widgets.grid.GridWidget.CALL_SERVICE_AUTH" |
| 31 | + const val EXTRA_ACTION_ID = |
| 32 | + "io.homeassistant.companion.android.widgets.grid.GridWidget.EXTRA_ACTION_ID" |
| 33 | + } |
| 34 | + |
| 35 | + @Inject |
| 36 | + lateinit var serverManager: ServerManager |
| 37 | + |
| 38 | + @Inject |
| 39 | + lateinit var gridWidgetDao: GridWidgetDao |
| 40 | + |
| 41 | + private val mainScope: CoroutineScope = CoroutineScope(Dispatchers.Main + Job()) |
| 42 | + |
| 43 | + override fun onReceive(context: Context, intent: Intent) { |
| 44 | + val action = intent.action |
| 45 | + val appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID) |
| 46 | + val actionId = intent.getIntExtra(EXTRA_ACTION_ID, -1) |
| 47 | + |
| 48 | + super.onReceive(context, intent) |
| 49 | + when (action) { |
| 50 | + CALL_SERVICE_AUTH -> authThenCallConfiguredAction(context, appWidgetId, actionId) |
| 51 | + CALL_SERVICE -> callConfiguredAction(appWidgetId, actionId) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + override fun onUpdate( |
| 56 | + context: Context, |
| 57 | + appWidgetManager: AppWidgetManager, |
| 58 | + appWidgetIds: IntArray |
| 59 | + ) { |
| 60 | + appWidgetIds.forEach { appWidgetId -> |
| 61 | + val gridConfig = gridWidgetDao.get(appWidgetId)?.asGridConfiguration() |
| 62 | + appWidgetManager.updateAppWidget(appWidgetId, gridConfig.asRemoteViews(context, appWidgetId)) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + override fun onAppWidgetOptionsChanged(context: Context?, appWidgetManager: AppWidgetManager?, appWidgetId: Int, newOptions: Bundle?) { |
| 67 | + super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions) |
| 68 | + } |
| 69 | + |
| 70 | + override fun onDeleted(context: Context?, appWidgetIds: IntArray?) { |
| 71 | + super.onDeleted(context, appWidgetIds) |
| 72 | + } |
| 73 | + |
| 74 | + override fun onEnabled(context: Context?) { |
| 75 | + super.onEnabled(context) |
| 76 | + } |
| 77 | + |
| 78 | + override fun onDisabled(context: Context?) { |
| 79 | + super.onDisabled(context) |
| 80 | + } |
| 81 | + |
| 82 | + override fun onRestored(context: Context?, oldWidgetIds: IntArray?, newWidgetIds: IntArray?) { |
| 83 | + super.onRestored(context, oldWidgetIds, newWidgetIds) |
| 84 | + } |
| 85 | + |
| 86 | + private fun authThenCallConfiguredAction(context: Context, appWidgetId: Int, actionId: Int) { |
| 87 | + Log.d(TAG, "Calling authentication, then configured action") |
| 88 | + |
| 89 | + val extras = Bundle().apply { |
| 90 | + putInt(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId) |
| 91 | + putInt(EXTRA_ACTION_ID, actionId) |
| 92 | + } |
| 93 | + val intent = Intent(context, WidgetAuthenticationActivity::class.java).apply { |
| 94 | + flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_NEW_DOCUMENT |
| 95 | + putExtra(WidgetAuthenticationActivity.EXTRA_TARGET, GridWidget::class.java) |
| 96 | + putExtra(WidgetAuthenticationActivity.EXTRA_ACTION, CALL_SERVICE) |
| 97 | + putExtra(WidgetAuthenticationActivity.EXTRA_EXTRAS, extras) |
| 98 | + } |
| 99 | + context.startActivity(intent) |
| 100 | + } |
| 101 | + |
| 102 | + private fun callConfiguredAction(appWidgetId: Int, actionId: Int) { |
| 103 | + Log.d(TAG, "Calling widget action") |
| 104 | + |
| 105 | + val widget = gridWidgetDao.get(appWidgetId) |
| 106 | + val item = widget?.items?.find { it.id == actionId } |
| 107 | + |
| 108 | + mainScope.launch { |
| 109 | + // Load the action call data from Shared Preferences |
| 110 | + val domain = item?.domain |
| 111 | + val action = item?.service |
| 112 | + val actionDataJson = item?.serviceData |
| 113 | + |
| 114 | + Log.d( |
| 115 | + TAG, |
| 116 | + "Action Call Data loaded:" + System.lineSeparator() + |
| 117 | + "domain: " + domain + System.lineSeparator() + |
| 118 | + "action: " + action + System.lineSeparator() + |
| 119 | + "action_data: " + actionDataJson |
| 120 | + ) |
| 121 | + |
| 122 | + if (domain == null || action == null || actionDataJson == null) { |
| 123 | + Log.w(TAG, "Action Call Data incomplete. Aborting action call") |
| 124 | + } else { |
| 125 | + // If everything loaded correctly, package the action data and attempt the call |
| 126 | + try { |
| 127 | + // Convert JSON to HashMap |
| 128 | + val actionDataMap: HashMap<String, Any> = |
| 129 | + jacksonObjectMapper().readValue(actionDataJson) |
| 130 | + |
| 131 | + if (actionDataMap["entity_id"] != null) { |
| 132 | + val entityIdWithoutBrackets = Pattern.compile("\\[(.*?)\\]") |
| 133 | + .matcher(actionDataMap["entity_id"].toString()) |
| 134 | + if (entityIdWithoutBrackets.find()) { |
| 135 | + val value = entityIdWithoutBrackets.group(1) |
| 136 | + if (value != null) { |
| 137 | + if (value == "all" || |
| 138 | + value.split(",").contains("all") |
| 139 | + ) { |
| 140 | + actionDataMap["entity_id"] = "all" |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + Log.d(TAG, "Sending action call to Home Assistant") |
| 147 | + serverManager.integrationRepository(widget.gridWidget.serverId).callAction(domain, action, actionDataMap) |
| 148 | + Log.d(TAG, "Action call sent successfully") |
| 149 | + } catch (e: Exception) { |
| 150 | + Log.e(TAG, "Failed to call action", e) |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments