1
+ /*
2
+ * AndFHEM - Open Source Android application to control a FHEM home automation
3
+ * server.
4
+ *
5
+ * Copyright (c) 2011, Matthias Klass or third-party contributors as
6
+ * indicated by the @author tags or express copyright attribution
7
+ * statements applied by the authors. All third-party contributions are
8
+ * distributed under license by Red Hat Inc.
9
+ *
10
+ * This copyrighted material is made available to anyone wishing to use, modify,
11
+ * copy, or redistribute it subject to the terms and conditions of the GNU GENERAL PUBLIC LICENSE, as published by the Free Software Foundation.
12
+ *
13
+ * This program is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU GENERAL PUBLIC LICENSE
16
+ * for more details.
17
+ *
18
+ * You should have received a copy of the GNU GENERAL PUBLIC LICENSE
19
+ * along with this distribution; if not, write to:
20
+ * Free Software Foundation, Inc.
21
+ * 51 Franklin Street, Fifth Floor
22
+ * Boston, MA 02110-1301 USA
23
+ */
24
+
25
+ package li.klass.fhem.appwidget.action
26
+
27
+ import android.content.Context
28
+ import android.os.Bundle
29
+ import android.os.Handler
30
+ import android.widget.Toast
31
+ import com.google.common.base.Optional
32
+ import li.klass.fhem.R
33
+ import li.klass.fhem.appwidget.update.AppWidgetUpdateService
34
+ import li.klass.fhem.constants.Actions
35
+ import li.klass.fhem.constants.BundleExtraKeys.*
36
+ import li.klass.fhem.devices.backend.GenericDeviceService
37
+ import li.klass.fhem.devices.backend.ToggleableService
38
+ import li.klass.fhem.domain.core.FhemDevice
39
+ import li.klass.fhem.update.backend.DeviceListService
40
+ import li.klass.fhem.update.backend.DeviceListUpdateService
41
+ import org.jetbrains.anko.doAsync
42
+ import javax.inject.Inject
43
+
44
+ class AppWidgetActionHandler @Inject constructor(
45
+ private val deviceListService : DeviceListService ,
46
+ private val genericDeviceService : GenericDeviceService ,
47
+ private val toggleableService : ToggleableService ,
48
+ private val appWidgetUpdateService : AppWidgetUpdateService ,
49
+ deviceListUpdateService : DeviceListUpdateService
50
+ ) {
51
+ internal val handlers: Map <String , ActionHandler > = mapOf (
52
+ Actions .DEVICE_WIDGET_TOGGLE to object : ActionHandler {
53
+ override fun handle (device : FhemDevice ? , connectionId : String? , bundle : Bundle , context : Context ) {
54
+ device ? : return
55
+ toggleableService.toggleState(device, connectionId)
56
+ }
57
+ },
58
+ Actions .DEVICE_WIDGET_TARGET_STATE to object : ActionHandler {
59
+ override fun handle (device : FhemDevice ? , connectionId : String? , bundle : Bundle , context : Context ) {
60
+ device ? : return
61
+ val targetState = bundle.getString(DEVICE_TARGET_STATE ) ? : return
62
+ genericDeviceService.setState(device, targetState, Optional .fromNullable(connectionId), true )
63
+ }
64
+ },
65
+ Actions .WIDGET_REQUEST_UPDATE to object : ActionHandler {
66
+ override fun handle (device : FhemDevice ? , connectionId : String? , bundle : Bundle , context : Context ) {
67
+ Handler (context.mainLooper).post { Toast .makeText(context, R .string.widget_remote_update_started, Toast .LENGTH_LONG ).show() }
68
+ deviceListUpdateService.updateAllDevices(connectionId)
69
+ }
70
+ }
71
+ )
72
+
73
+ fun handle (context : Context , bundle : Bundle , action : String ) {
74
+ val handler = handlers.get(action) ? : return
75
+ val deviceName = bundle.getString(DEVICE_NAME )
76
+ val connectionId = bundle.getString(CONNECTION_ID )
77
+
78
+ doAsync {
79
+ val device = deviceName?.let { deviceListService.getDeviceForName(it, connectionId) }
80
+ handler.handle(device, connectionId, bundle, context)
81
+ appWidgetUpdateService.updateAllWidgets()
82
+ }
83
+ }
84
+
85
+ internal interface ActionHandler {
86
+ fun handle (device : FhemDevice ? , connectionId : String? , bundle : Bundle , context : Context )
87
+ }
88
+ }
0 commit comments