Skip to content

Commit 3427e6e

Browse files
committed
* Dutch language support
* Fixed issue with repeating sound on disarm * Fixed bug with some devices and the MQTT service crashing
1 parent 233e8c8 commit 3427e6e

File tree

7 files changed

+147
-25
lines changed

7 files changed

+147
-25
lines changed

app/build.gradle

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ repositories {
3838
}
3939

4040
def versionMajor = 0
41-
def versionMinor = 6
42-
def versionPatch = 9
43-
def versionBuild = 2 // bump for dog food builds, public betas, etc.
41+
def versionMinor = 7
42+
def versionPatch = 0
43+
def versionBuild = 0 // bump for dog food builds, public betas, etc.
4444

4545
def ALARM_CODE() {
4646
Properties properties = new Properties()

app/src/main/java/com/thanksmister/iot/mqtt/alarmpanel/BaseActivity.kt

+17-11
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ package com.thanksmister.iot.mqtt.alarmpanel
2020

2121
import android.Manifest
2222
import android.app.KeyguardManager
23+
import android.arch.lifecycle.Observer
2324
import android.arch.lifecycle.ViewModelProvider
2425
import android.arch.lifecycle.ViewModelProviders
2526
import android.content.*
2627
import android.content.pm.PackageManager
27-
import android.net.ConnectivityManager
2828
import android.os.Build
2929
import android.os.Bundle
3030
import android.os.Handler
@@ -40,6 +40,7 @@ import com.thanksmister.iot.mqtt.alarmpanel.network.DarkSkyOptions
4040
import com.thanksmister.iot.mqtt.alarmpanel.network.ImageOptions
4141
import com.thanksmister.iot.mqtt.alarmpanel.network.MQTTOptions
4242
import com.thanksmister.iot.mqtt.alarmpanel.ui.Configuration
43+
import com.thanksmister.iot.mqtt.alarmpanel.managers.ConnectionLiveData
4344
import com.thanksmister.iot.mqtt.alarmpanel.ui.activities.MainActivity
4445
import com.thanksmister.iot.mqtt.alarmpanel.ui.views.ScreenSaverView
4546
import com.thanksmister.iot.mqtt.alarmpanel.utils.DialogUtils
@@ -69,6 +70,7 @@ abstract class BaseActivity : DaggerAppCompatActivity() {
6970
private var wakeLock: PowerManager.WakeLock? = null
7071
private var decorView: View? = null
7172
private var userPresent: Boolean = false
73+
private var connectionLiveData: ConnectionLiveData? = null
7274

7375
abstract fun getLayoutId(): Int
7476

@@ -92,6 +94,14 @@ abstract class BaseActivity : DaggerAppCompatActivity() {
9294

9395
override fun onStart(){
9496
super.onStart()
97+
connectionLiveData = ConnectionLiveData(this)
98+
connectionLiveData?.observe(this, Observer { connected ->
99+
if(connected!!) {
100+
handleNetworkConnect()
101+
} else {
102+
handleNetworkDisconnect()
103+
}
104+
})
95105
}
96106

97107
override fun onWindowFocusChanged(hasFocus: Boolean) {
@@ -181,17 +191,11 @@ abstract class BaseActivity : DaggerAppCompatActivity() {
181191
public override fun onResume() {
182192
super.onResume()
183193
releaseTemporaryWakeLock()
184-
registerReceiver(connReceiver, IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION))
185194
checkPermissions()
186195
}
187196

188197
override fun onPause() {
189198
super.onPause()
190-
try {
191-
unregisterReceiver(connReceiver)
192-
} catch (e: IllegalArgumentException) {
193-
Timber.e(e.message)
194-
}
195199
}
196200

197201
/**
@@ -276,7 +280,7 @@ abstract class BaseActivity : DaggerAppCompatActivity() {
276280
* to for extra network disconnect handling such as bring application
277281
* into foreground.
278282
*/
279-
fun handleNetworkDisconnect() {
283+
open fun handleNetworkDisconnect() {
280284
if(configuration.hasNotifications()) {
281285
val notifications = NotificationUtils(this@BaseActivity)
282286
notifications.createAlarmNotification(getString(R.string.text_notification_network_title), getString(R.string.text_notification_network_description))
@@ -286,16 +290,18 @@ abstract class BaseActivity : DaggerAppCompatActivity() {
286290
dialogUtils.showAlertDialogToDismiss(this@BaseActivity, getString(R.string.text_notification_network_title),
287291
getString(R.string.text_notification_network_description))
288292
}
293+
hasNetwork.set(false)
289294
}
290295

291296
/**
292297
* On network connect hide any alert dialogs generated by
293298
* the network disconnect and clear any notifications.
294299
*/
295-
fun handleNetworkConnect() {
300+
open fun handleNetworkConnect() {
296301
dialogUtils.hideAlertDialog()
297302
val notifications = NotificationUtils(this@BaseActivity)
298303
notifications.clearNotification()
304+
hasNetwork.set(true)
299305
}
300306

301307
fun hasNetworkConnectivity(): Boolean {
@@ -316,7 +322,7 @@ abstract class BaseActivity : DaggerAppCompatActivity() {
316322
* to run amok that is why we only notify the user once for network disconnect with
317323
* a boolean flag.
318324
*/
319-
private val connReceiver = object : BroadcastReceiver() {
325+
/*private val connReceiver = object : BroadcastReceiver() {
320326
override fun onReceive(context: Context, intent: Intent) {
321327
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
322328
val currentNetworkInfo = connectivityManager.activeNetworkInfo
@@ -331,5 +337,5 @@ abstract class BaseActivity : DaggerAppCompatActivity() {
331337
bringApplicationToForegroundIfNeeded()
332338
}
333339
}
334-
}
340+
}*/
335341
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright (c) 2018 ThanksMister LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software distributed
11+
* under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.thanksmister.iot.mqtt.alarmpanel.managers
18+
19+
import android.arch.lifecycle.MutableLiveData
20+
import android.content.BroadcastReceiver
21+
import android.content.Context
22+
import android.content.Intent
23+
import android.content.IntentFilter
24+
import android.net.ConnectivityManager
25+
26+
import timber.log.Timber
27+
import java.util.concurrent.atomic.AtomicBoolean
28+
29+
class ConnectionLiveData(private val context: Context) : MutableLiveData<Boolean>() {
30+
31+
private var connCallbackListener: ConnCallbackListener? = null
32+
33+
init {
34+
connCallbackListener = object : ConnCallbackListener {
35+
override fun networkConnect() {
36+
value = true
37+
}
38+
override fun networkDisconnect() {
39+
value = false
40+
}
41+
}
42+
}
43+
44+
override fun onActive() {
45+
super.onActive()
46+
context.registerReceiver(connectionReceiver, IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION))
47+
}
48+
49+
override fun onInactive() {
50+
super.onInactive()
51+
context.unregisterReceiver(connectionReceiver)
52+
}
53+
54+
private val connectionReceiver = object : BroadcastReceiver() {
55+
override fun onReceive(context: Context, intent: Intent) {
56+
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
57+
val currentNetworkInfo = connectivityManager.activeNetworkInfo
58+
if (currentNetworkInfo != null && currentNetworkInfo.isConnected) {
59+
Timber.d("Network Connected")
60+
hasNetwork.set(true)
61+
value = true
62+
} else if (hasNetwork.get()) {
63+
Timber.d("Network Disconnected")
64+
hasNetwork.set(false)
65+
value = false
66+
}
67+
}
68+
}
69+
70+
interface ConnCallbackListener {
71+
fun networkConnect()
72+
fun networkDisconnect()
73+
}
74+
75+
companion object {
76+
var hasNetwork = AtomicBoolean(true)
77+
}
78+
}

app/src/main/java/com/thanksmister/iot/mqtt/alarmpanel/network/MQTTService.kt

+20-11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package com.thanksmister.iot.mqtt.alarmpanel.network
44
import android.R.id.message
55
import android.content.Context
66
import android.text.TextUtils
7+
import com.thanksmister.iot.mqtt.alarmpanel.R
78
import com.thanksmister.iot.mqtt.alarmpanel.utils.MqttUtils
89
import com.thanksmister.iot.mqtt.alarmpanel.utils.StringUtils
910
import org.eclipse.paho.android.service.MqttAndroidClient
@@ -55,6 +56,7 @@ class MQTTService(private var context: Context, options: MQTTOptions,
5556

5657
@Throws(MqttException::class)
5758
override fun close() {
59+
Timber.d("close")
5860
if (mqttClient != null) {
5961
// TODO IllegalArgumentException: Invalid ClientHandle and no dialog showing sound stuck
6062
mqttClient?.setCallback(null)
@@ -111,8 +113,6 @@ class MQTTService(private var context: Context, options: MQTTOptions,
111113
*/
112114
private fun initialize(options: MQTTOptions) {
113115
Timber.d("initialize")
114-
if(options == null) return;
115-
116116
try {
117117
mqttOptions = options
118118
Timber.i("Service Configuration:")
@@ -133,15 +133,18 @@ class MQTTService(private var context: Context, options: MQTTOptions,
133133
}
134134
} catch (e: MqttException) {
135135
if (listener != null) {
136-
listener!!.handleMqttException("Could not initialize MQTT: " + e.message)
136+
//listener!!.handleMqttException("Could not initialize MQTT: " + e.message)
137+
listener!!.handleMqttException(context.getString(R.string.error_mqtt_connection))
137138
}
138139
} catch (e: IOException) {
139140
if (listener != null) {
140-
listener!!.handleMqttException("Could not initialize MQTT: " + e.message)
141+
//listener!!.handleMqttException("Could not initialize MQTT: " + e.message)
142+
listener!!.handleMqttException(context.getString(R.string.error_mqtt_connection))
141143
}
142144
} catch (e: GeneralSecurityException) {
143145
if (listener != null) {
144-
listener!!.handleMqttException("Could not initialize MQTT: " + e.message)
146+
//listener!!.handleMqttException("Could not initialize MQTT: " + e.message)
147+
listener!!.handleMqttException(context.getString(R.string.error_mqtt_connection))
145148
}
146149
}
147150

@@ -158,13 +161,17 @@ class MQTTService(private var context: Context, options: MQTTOptions,
158161
options.password = mqttOptions!!.getPassword()!!.toCharArray()
159162
}
160163

161-
mqttClient = MqttUtils.getMqttAndroidClient(context, mqttOptions?.brokerUrl!!, mqttOptions?.getClientId()!!, object : MqttCallbackExtended {
164+
/*mqttClient = MqttUtils.getMqttAndroidClient(context, mqttOptions?.brokerUrl!!, mqttOptions?.getClientId()!!)
165+
subscribeToTopics(mqttOptions!!.stateTopics)*/
166+
167+
/* mqttClient = MqttUtils.getMqttAndroidClient(context, mqttOptions?.brokerUrl!!, mqttOptions?.getClientId()!!, object : MqttCallbackExtended {
162168
@Throws(NullPointerException::class)
163169
override fun connectComplete(reconnect: Boolean, serverURI: String) {
164170
if (reconnect) {
165171
Timber.d("Reconnected to : " + serverURI)
166172
// Because Clean Session is true, we need to re-subscribe
167173
subscribeToTopics(mqttOptions!!.stateTopics)
174+
mqttClient?.setCallback(null)
168175
} else {
169176
Timber.d("Connected to: " + serverURI)
170177
}
@@ -182,7 +189,7 @@ class MQTTService(private var context: Context, options: MQTTOptions,
182189
183190
override fun deliveryComplete(token: IMqttDeliveryToken) {}
184191
})
185-
192+
*/
186193
options.isAutomaticReconnect = true
187194
options.isCleanSession = false
188195

@@ -205,14 +212,15 @@ class MQTTService(private var context: Context, options: MQTTOptions,
205212
override fun onFailure(asyncActionToken: IMqttToken, exception: Throwable) {
206213
if (listener != null && mqttOptions != null) {
207214
Timber.e("Failed to connect to: " + mqttOptions!!.brokerUrl + " exception: " + exception)
208-
listener!!.handleMqttException("Error connecting to the broker and port: " + mqttOptions!!.brokerUrl)
215+
//listener!!.handleMqttException("Error connecting to the broker and port: " + mqttOptions!!.brokerUrl)
216+
listener!!.handleMqttException(context.getString(R.string.error_mqtt_subscription))
209217
}
210218
}
211219
})
212220
} catch (e: MqttException) {
213221
Timber.e(e, "MqttException")
214222
if (listener != null) {
215-
listener?.handleMqttException("Error while connecting: " + e.message)
223+
listener?.handleMqttException("" + e.message)
216224
}
217225
}
218226

@@ -224,7 +232,7 @@ class MQTTService(private var context: Context, options: MQTTOptions,
224232
} catch (e: Exception) {
225233
e.printStackTrace()
226234
if (listener != null) {
227-
listener!!.handleMqttException("Error while connecting: " + e.message)
235+
listener!!.handleMqttException("" + e.message)
228236
}
229237
}
230238
}
@@ -240,7 +248,8 @@ class MQTTService(private var context: Context, options: MQTTOptions,
240248
Timber.e("Error Sending Command: " + e.message)
241249
e.printStackTrace()
242250
if (listener != null) {
243-
listener!!.handleMqttException("Error Sending Command: " + e.message)
251+
//listener!!.handleMqttException("Error Sending Command: " + e.message)
252+
listener!!.handleMqttException(context.getString(R.string.error_mqtt_subscription))
244253
}
245254
}
246255
}

app/src/main/java/com/thanksmister/iot/mqtt/alarmpanel/ui/activities/MainActivity.kt

+18
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import android.support.v4.app.FragmentStatePagerAdapter
3131
import android.support.v4.view.PagerAdapter
3232
import android.support.v4.view.ViewPager
3333
import android.support.v7.app.AlertDialog
34+
import android.view.View
3435
import com.thanksmister.iot.mqtt.alarmpanel.BaseActivity
3536
import com.thanksmister.iot.mqtt.alarmpanel.BuildConfig
3637
import com.thanksmister.iot.mqtt.alarmpanel.R
@@ -42,6 +43,7 @@ import com.thanksmister.iot.mqtt.alarmpanel.ui.modules.MQTTModule
4243
import com.thanksmister.iot.mqtt.alarmpanel.ui.modules.TextToSpeechModule
4344
import com.thanksmister.iot.mqtt.alarmpanel.utils.AlarmUtils
4445
import com.thanksmister.iot.mqtt.alarmpanel.utils.ComponentUtils.NOTIFICATION_STATE_TOPIC
46+
import com.thanksmister.iot.mqtt.alarmpanel.utils.NetworkUtils
4547
import com.thanksmister.iot.mqtt.alarmpanel.utils.NotificationUtils
4648
import io.reactivex.android.schedulers.AndroidSchedulers
4749
import io.reactivex.schedulers.Schedulers
@@ -266,6 +268,20 @@ class MainActivity : BaseActivity(), ViewPager.OnPageChangeListener, ControlsFra
266268
return R.layout.activity_main
267269
}
268270

271+
override fun handleNetworkConnect() {
272+
super.handleNetworkConnect()
273+
if (mqttModule != null) {
274+
mqttModule?.restart()
275+
}
276+
}
277+
278+
override fun handleNetworkDisconnect() {
279+
super.handleNetworkDisconnect()
280+
if (mqttModule != null) {
281+
mqttModule?.pause()
282+
}
283+
}
284+
269285
/**
270286
* Temporarily wake the screen so we can notify the user of pending alarm and
271287
* then allow the device to sleep again as needed after a set amount of time.
@@ -315,12 +331,14 @@ class MainActivity : BaseActivity(), ViewPager.OnPageChangeListener, ControlsFra
315331
}
316332

317333
override fun onMQTTException(message: String) {
334+
Timber.d("onMQTTException")
318335
this@MainActivity.runOnUiThread {
319336
dialogUtils.showAlertDialog(this@MainActivity, message)
320337
}
321338
}
322339

323340
override fun onMQTTDisconnect() {
341+
Timber.d("onMQTTDisconnect")
324342
this@MainActivity.runOnUiThread {
325343
dialogUtils.showAlertDialog(this@MainActivity, getString(R.string.error_mqtt_connection), DialogInterface.OnClickListener { _, _ ->
326344
if (mqttModule != null) {

app/src/main/java/com/thanksmister/iot/mqtt/alarmpanel/ui/modules/MQTTModule.kt

+6
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,16 @@ class MQTTModule (base: Context?, private val mqttOptions: MQTTOptions, private
7575
}
7676

7777
fun restart() {
78+
Timber.d("restart")
7879
stop()
7980
start()
8081
}
8182

83+
fun pause() {
84+
Timber.d("pause")
85+
stop()
86+
}
87+
8288
fun publish(command : String) {
8389
Timber.d("command: " + command)
8490
if(mqttService != null) {

app/src/main/java/com/thanksmister/iot/mqtt/alarmpanel/utils/MqttUtils.kt

+5
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ class MqttUtils {
5959
return mqttConnectOptions
6060
}
6161

62+
fun getMqttAndroidClient(context: Context, serverUri: String, clientId: String): MqttAndroidClient {
63+
val mqttAndroidClient = MqttAndroidClient(context, serverUri, clientId)
64+
return mqttAndroidClient
65+
}
66+
6267
fun getMqttAndroidClient(context: Context, serverUri: String, clientId: String,
6368
mqttCallbackExtended: MqttCallbackExtended): MqttAndroidClient {
6469
val mqttAndroidClient = MqttAndroidClient(context, serverUri, clientId)

0 commit comments

Comments
 (0)