react-native-flic2 2.0.0-alpha.39 → 2.0.0-beta.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +796 -13
- package/android/build.gradle +1 -1
- package/android/src/main/AndroidManifest.xml +23 -0
- package/android/src/main/java/nl/xguard/flic2/ActivityUtil.kt +29 -0
- package/android/src/main/java/{com → nl/xguard}/flic2/Flic2ButtonListener.kt +25 -13
- package/android/src/main/java/{com → nl/xguard}/flic2/Flic2Converter.kt +2 -3
- package/android/src/main/java/{com → nl/xguard}/flic2/Flic2Module.kt +126 -85
- package/android/src/main/java/{com → nl/xguard}/flic2/Flic2Package.kt +2 -1
- package/android/src/main/java/nl/xguard/flic2/Flic2Service.kt +274 -0
- package/ios/Flic2.h +0 -1
- package/ios/Flic2.mm +223 -195
- package/lib/module/NativeFlic2.js +26 -0
- package/lib/module/NativeFlic2.js.map +1 -1
- package/lib/module/index.js +248 -76
- package/lib/module/index.js.map +1 -1
- package/lib/module/lib/typedEventEmitter.js +39 -0
- package/lib/module/lib/typedEventEmitter.js.map +1 -0
- package/lib/typescript/src/NativeFlic2.d.ts +44 -60
- package/lib/typescript/src/NativeFlic2.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +163 -80
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/lib/typedEventEmitter.d.ts +15 -0
- package/lib/typescript/src/lib/typedEventEmitter.d.ts.map +1 -0
- package/package.json +25 -25
- package/src/NativeFlic2.ts +62 -47
- package/src/index.ts +360 -0
- package/src/lib/typedEventEmitter.ts +63 -0
- package/android/src/main/java/com/flic2/Flic2Service.kt +0 -112
- package/src/index.tsx +0 -159
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
package nl.xguard.flic2
|
|
2
|
+
|
|
3
|
+
import android.app.Notification
|
|
4
|
+
import android.app.NotificationChannel
|
|
5
|
+
import android.app.NotificationManager
|
|
6
|
+
import android.app.PendingIntent
|
|
7
|
+
import android.app.Service
|
|
8
|
+
import android.content.BroadcastReceiver
|
|
9
|
+
import android.content.Context
|
|
10
|
+
import android.content.Intent
|
|
11
|
+
import android.content.pm.PackageManager
|
|
12
|
+
import android.os.Binder
|
|
13
|
+
import android.os.Build
|
|
14
|
+
import android.os.Handler
|
|
15
|
+
import android.os.IBinder
|
|
16
|
+
import android.os.Looper
|
|
17
|
+
import android.util.Log
|
|
18
|
+
import androidx.core.app.NotificationCompat
|
|
19
|
+
import io.flic.flic2libandroid.Flic2Manager
|
|
20
|
+
|
|
21
|
+
class Flic2Service : Service() {
|
|
22
|
+
|
|
23
|
+
private val binder = Flic2ServiceBinder()
|
|
24
|
+
private var manager: Flic2Manager? = null
|
|
25
|
+
private var isServiceStarted = false
|
|
26
|
+
private var notification: Notification? = null
|
|
27
|
+
|
|
28
|
+
companion object {
|
|
29
|
+
private const val TAG = "Flic2Service"
|
|
30
|
+
private const val DEFAULT_NOTIFICATION_ID = 123321
|
|
31
|
+
private const val DEFAULT_CHANNEL_ID = "Notification_Channel_Flic2Service"
|
|
32
|
+
|
|
33
|
+
// Metadata keys for notification configuration
|
|
34
|
+
private const val KEY_CHANNEL_NAME = "nl.xguard.flic2.notification_channel_name"
|
|
35
|
+
private const val KEY_CHANNEL_DESCRIPTION = "nl.xguard.flic2.notification_channel_description"
|
|
36
|
+
private const val NOTIFICATION_TITLE_KEY = "nl.xguard.flic2.notification_title"
|
|
37
|
+
private const val NOTIFICATION_TEXT_KEY = "nl.xguard.flic2.notification_text"
|
|
38
|
+
private const val NOTIFICATION_ICON_KEY = "nl.xguard.flic2.notification_icon"
|
|
39
|
+
private const val NOTIFICATION_ID_KEY = "nl.xguard.flic2.notification_id"
|
|
40
|
+
private const val CHANNEL_ID_KEY = "nl.xguard.flic2.notification_channel_id"
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
inner class Flic2ServiceBinder : Binder() {
|
|
44
|
+
fun getService(): Flic2Service = this@Flic2Service
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
override fun onCreate() {
|
|
48
|
+
super.onCreate()
|
|
49
|
+
Log.d(TAG, "Service onCreate")
|
|
50
|
+
|
|
51
|
+
try {
|
|
52
|
+
// Initialize Flic2Manager on main thread with Handler
|
|
53
|
+
// v1.1.0 API: init() returns void, must call getInstance() after
|
|
54
|
+
Flic2Manager.init(
|
|
55
|
+
applicationContext,
|
|
56
|
+
Handler(Looper.getMainLooper())
|
|
57
|
+
)
|
|
58
|
+
manager = Flic2Manager.getInstance()
|
|
59
|
+
Log.d(TAG, "Flic2Manager initialized successfully")
|
|
60
|
+
} catch (e: Exception) {
|
|
61
|
+
Log.e(TAG, "Failed to initialize Flic2Manager", e)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Create notification channel and notification in onCreate
|
|
65
|
+
createNotificationChannel()
|
|
66
|
+
notification = createNotification()
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
|
70
|
+
Log.d(TAG, "Service onStartCommand")
|
|
71
|
+
|
|
72
|
+
if (intent != null) {
|
|
73
|
+
if (Intent.ACTION_BOOT_COMPLETED == intent.action) {
|
|
74
|
+
Log.d(TAG, "onStartCommand: ACTION_BOOT_COMPLETED")
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Start foreground service if notification is ready
|
|
79
|
+
if (notification != null) {
|
|
80
|
+
startForegroundService()
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return START_STICKY
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
override fun onBind(intent: Intent?): IBinder {
|
|
87
|
+
Log.d(TAG, "Service onBind")
|
|
88
|
+
return binder
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
override fun onDestroy() {
|
|
92
|
+
Log.d(TAG, "Service onDestroy")
|
|
93
|
+
super.onDestroy()
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
fun getManager(): Flic2Manager? = manager
|
|
97
|
+
|
|
98
|
+
fun isManagerInitialized(): Boolean = manager != null
|
|
99
|
+
|
|
100
|
+
fun startForegroundService() {
|
|
101
|
+
if (!isServiceStarted && notification != null) {
|
|
102
|
+
isServiceStarted = true
|
|
103
|
+
try {
|
|
104
|
+
startForeground(getNotificationId(), notification)
|
|
105
|
+
} catch (e: Exception) {
|
|
106
|
+
Log.w(TAG, "startForegroundService() exception", e)
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
fun stopForegroundService() {
|
|
112
|
+
if (isServiceStarted) {
|
|
113
|
+
isServiceStarted = false
|
|
114
|
+
stopForeground(true)
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
private fun createNotificationChannel() {
|
|
119
|
+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
120
|
+
val channel = NotificationChannel(
|
|
121
|
+
getChannelId(),
|
|
122
|
+
getChannelName(),
|
|
123
|
+
NotificationManager.IMPORTANCE_LOW
|
|
124
|
+
).apply {
|
|
125
|
+
description = getChannelDescription()
|
|
126
|
+
setShowBadge(false)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
getSystemService(NotificationManager::class.java).createNotificationChannel(channel)
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private fun createNotification(): Notification {
|
|
134
|
+
val notificationIntent = Intent(this, Flic2Service::class.java)
|
|
135
|
+
val pendingIntent = PendingIntent.getActivity(
|
|
136
|
+
this,
|
|
137
|
+
0,
|
|
138
|
+
notificationIntent,
|
|
139
|
+
PendingIntent.FLAG_IMMUTABLE
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
return NotificationCompat.Builder(this, getChannelId())
|
|
143
|
+
.setContentTitle(getNotificationTitle())
|
|
144
|
+
.setContentText(getNotificationText())
|
|
145
|
+
.setSmallIcon(getNotificationIcon())
|
|
146
|
+
.setContentIntent(pendingIntent)
|
|
147
|
+
.setPriority(NotificationCompat.PRIORITY_LOW)
|
|
148
|
+
.setOngoing(true)
|
|
149
|
+
.build()
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
private fun getNotificationId(): Int {
|
|
153
|
+
return try {
|
|
154
|
+
val metadata = applicationContext.packageManager
|
|
155
|
+
.getApplicationInfo(applicationContext.packageName, PackageManager.GET_META_DATA)
|
|
156
|
+
.metaData
|
|
157
|
+
metadata?.getInt(NOTIFICATION_ID_KEY, DEFAULT_NOTIFICATION_ID) ?: DEFAULT_NOTIFICATION_ID
|
|
158
|
+
} catch (e: PackageManager.NameNotFoundException) {
|
|
159
|
+
Log.w(TAG, "getNotificationId() NameNotFoundException", e)
|
|
160
|
+
DEFAULT_NOTIFICATION_ID
|
|
161
|
+
} catch (e: Exception) {
|
|
162
|
+
Log.w(TAG, "getNotificationId() exception", e)
|
|
163
|
+
DEFAULT_NOTIFICATION_ID
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
private fun getChannelId(): String {
|
|
168
|
+
return try {
|
|
169
|
+
val metadata = applicationContext.packageManager
|
|
170
|
+
.getApplicationInfo(applicationContext.packageName, PackageManager.GET_META_DATA)
|
|
171
|
+
.metaData
|
|
172
|
+
metadata?.getString(CHANNEL_ID_KEY) ?: DEFAULT_CHANNEL_ID
|
|
173
|
+
} catch (e: PackageManager.NameNotFoundException) {
|
|
174
|
+
Log.w(TAG, "getChannelId() NameNotFoundException", e)
|
|
175
|
+
DEFAULT_CHANNEL_ID
|
|
176
|
+
} catch (e: Exception) {
|
|
177
|
+
Log.w(TAG, "getChannelId() exception", e)
|
|
178
|
+
DEFAULT_CHANNEL_ID
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
private fun getChannelName(): String {
|
|
183
|
+
return try {
|
|
184
|
+
val metadata = applicationContext.packageManager
|
|
185
|
+
.getApplicationInfo(applicationContext.packageName, PackageManager.GET_META_DATA)
|
|
186
|
+
.metaData
|
|
187
|
+
metadata?.getString(KEY_CHANNEL_NAME) ?: "Flic2Channel"
|
|
188
|
+
} catch (e: PackageManager.NameNotFoundException) {
|
|
189
|
+
Log.w(TAG, "getChannelName() NameNotFoundException", e)
|
|
190
|
+
"Flic2Channel"
|
|
191
|
+
} catch (e: Exception) {
|
|
192
|
+
Log.w(TAG, "getChannelName() exception", e)
|
|
193
|
+
"Flic2Channel"
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
private fun getChannelDescription(): String {
|
|
198
|
+
return try {
|
|
199
|
+
val metadata = applicationContext.packageManager
|
|
200
|
+
.getApplicationInfo(applicationContext.packageName, PackageManager.GET_META_DATA)
|
|
201
|
+
.metaData
|
|
202
|
+
metadata?.getString(KEY_CHANNEL_DESCRIPTION) ?: "Flic2Channel"
|
|
203
|
+
} catch (e: PackageManager.NameNotFoundException) {
|
|
204
|
+
Log.w(TAG, "getChannelDescription() NameNotFoundException", e)
|
|
205
|
+
"Flic2Channel"
|
|
206
|
+
} catch (e: Exception) {
|
|
207
|
+
Log.w(TAG, "getChannelDescription() exception", e)
|
|
208
|
+
"Flic2Channel"
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
private fun getNotificationTitle(): String {
|
|
213
|
+
return try {
|
|
214
|
+
val metadata = applicationContext.packageManager
|
|
215
|
+
.getApplicationInfo(applicationContext.packageName, PackageManager.GET_META_DATA)
|
|
216
|
+
.metaData
|
|
217
|
+
metadata?.getString(NOTIFICATION_TITLE_KEY) ?: "Flic 2"
|
|
218
|
+
} catch (e: PackageManager.NameNotFoundException) {
|
|
219
|
+
Log.w(TAG, "getNotificationTitle() NameNotFoundException", e)
|
|
220
|
+
"Flic 2"
|
|
221
|
+
} catch (e: Exception) {
|
|
222
|
+
Log.w(TAG, "getNotificationTitle() exception", e)
|
|
223
|
+
"Flic 2"
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private fun getNotificationText(): String {
|
|
228
|
+
return try {
|
|
229
|
+
val metadata = applicationContext.packageManager
|
|
230
|
+
.getApplicationInfo(applicationContext.packageName, PackageManager.GET_META_DATA)
|
|
231
|
+
.metaData
|
|
232
|
+
metadata?.getString(NOTIFICATION_TEXT_KEY) ?: "Flic 2 service is running"
|
|
233
|
+
} catch (e: PackageManager.NameNotFoundException) {
|
|
234
|
+
Log.w(TAG, "getNotificationText() NameNotFoundException", e)
|
|
235
|
+
"Flic 2 service is running"
|
|
236
|
+
} catch (e: Exception) {
|
|
237
|
+
Log.w(TAG, "getNotificationText() exception", e)
|
|
238
|
+
"Flic 2 service is running"
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
private fun getNotificationIcon(): Int {
|
|
243
|
+
return try {
|
|
244
|
+
val metadata = applicationContext.packageManager
|
|
245
|
+
.getApplicationInfo(applicationContext.packageName, PackageManager.GET_META_DATA)
|
|
246
|
+
.metaData
|
|
247
|
+
val icon = metadata?.getInt(NOTIFICATION_ICON_KEY, 0) ?: 0
|
|
248
|
+
if (icon != 0) icon else android.R.drawable.ic_dialog_info
|
|
249
|
+
} catch (e: PackageManager.NameNotFoundException) {
|
|
250
|
+
Log.w(TAG, "getNotificationIcon() NameNotFoundException", e)
|
|
251
|
+
android.R.drawable.ic_dialog_info
|
|
252
|
+
} catch (e: Exception) {
|
|
253
|
+
Log.w(TAG, "getNotificationIcon() exception", e)
|
|
254
|
+
android.R.drawable.ic_dialog_info
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// BootUpReceiver for handling device boot
|
|
259
|
+
class BootUpReceiver : BroadcastReceiver() {
|
|
260
|
+
override fun onReceive(context: Context, intent: Intent) {
|
|
261
|
+
Log.d(TAG, "BootUpReceiver()")
|
|
262
|
+
// The Application class's onCreate has already been called at this point, which is what we want
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// UpdateReceiver for handling app updates
|
|
267
|
+
class UpdateReceiver : BroadcastReceiver() {
|
|
268
|
+
override fun onReceive(context: Context, intent: Intent) {
|
|
269
|
+
Log.d(TAG, "UpdateReceiver()")
|
|
270
|
+
// The Application class's onCreate has already been called at this point, which is what we want
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|