Skip to content

Commit

Permalink
* Fix for crash in MQTT when initializing null
Browse files Browse the repository at this point in the history
* Fix for dialog error when activity null
  • Loading branch information
thanksmister committed Aug 13, 2018
1 parent efa556c commit 30a1627
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 36 deletions.
2 changes: 1 addition & 1 deletion WallPanelApp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ repositories {
def versionMajor = 0
def versionMinor = 8
def versionPatch = 3
def versionBuild = 1 // bump for dog food builds, public betas, etc.
def versionBuild = 2 // bump for dog food builds, public betas, etc.

android {
kapt {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,19 @@ constructor(private val context: Context) {
buildDetectors(configuration)
if(multiDetector != null) {
try {
initCamera(configuration.cameraId, configuration.cameraFPS)
} catch (e : IOException) {
cameraSource = initCamera(configuration.cameraId, configuration.cameraFPS)
cameraSource!!.start()
} catch (e : Exception) {
Timber.e(e.message)
try {
if(configuration.cameraId == CAMERA_FACING_FRONT) {
initCamera(CAMERA_FACING_BACK, configuration.cameraFPS)
cameraSource = initCamera(CAMERA_FACING_BACK, configuration.cameraFPS)
cameraSource!!.start()
} else {
initCamera(CAMERA_FACING_FRONT, configuration.cameraFPS)
cameraSource = initCamera(CAMERA_FACING_FRONT, configuration.cameraFPS)
cameraSource!!.start()
}
} catch (e : IOException) {
} catch (e : Exception) {
Timber.e(e.message)
cameraSource!!.stop()
cameraCallback?.onCameraError()
Expand Down Expand Up @@ -404,20 +407,13 @@ constructor(private val context: Context) {
}

@SuppressLint("MissingPermission")
@Throws(Exception::class)
private fun initCamera(camerId: Int, fsp: Float) {
private fun initCamera(camerId: Int, fsp: Float): CameraSource {
Timber.d("initCamera camerId $camerId")
cameraSource = CameraSource.Builder(context, multiDetector)
return CameraSource.Builder(context, multiDetector)
.setRequestedFps(fsp)
.setRequestedPreviewSize(640, 480)
.setFacing(camerId)
.build()

try {
cameraSource!!.start()
} catch (e: RuntimeException) {
throw e
}
}

interface OnCompleteListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,18 @@ class MotionDetector private constructor(private val minLuma: Int, private val m
return sparseArray
}

val motionDetected = aggregateLumaMotionDetection!!.detect(img, w, h)
if (motionDetected) {
motion.type = MOTION_DETECTED
//Timber.d("MOTION_DETECTED")
} else {
try {
val motionDetected = aggregateLumaMotionDetection!!.detect(img, w, h)
if (motionDetected) {
motion.type = MOTION_DETECTED
//Timber.d("MOTION_DETECTED")
} else {
motion.type = MOTION_NOT_DETECTED
//Timber.d("MOTION_NOT_DETECTED")
}
} catch (e: Exception) {
Timber.e(e.message)
motion.type = MOTION_NOT_DETECTED
//Timber.d("MOTION_NOT_DETECTED")
}
sparseArray.put(0, motion)
return sparseArray
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ class MQTTService(private var context: Context, options: MQTTOptions,
try {
mqttClient!!.publish(mqttTopic, mqttMessage)
Timber.d("Command Topic: $mqttTopic Payload: $message")
} catch (e: NullPointerException) {
Timber.e(e.message)
} catch (e: MqttException) {
Timber.e("Error Sending Command: " + e.message)
e.printStackTrace()
Expand All @@ -248,6 +250,8 @@ class MQTTService(private var context: Context, options: MQTTOptions,
mqttClient!!.subscribe(topicFilters, MqttUtils.getQos(topicFilters!!.size),
MqttUtils.getMqttMessageListeners(topicFilters.size, listener))
}
} catch (e: NullPointerException) {
Timber.e(e.message)
} catch (e: MqttException) {
if (listener != null) {
listener!!.handleMqttException("Exception while subscribing: " + e.message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,18 +156,17 @@ class WallPanelService : LifecycleService(), MQTTModule.MQTTListener {
filter.addAction(Intent.ACTION_USER_PRESENT)
localBroadCastManager = LocalBroadcastManager.getInstance(this)
localBroadCastManager!!.registerReceiver(mBroadcastReceiver, filter)

}

override fun onDestroy() {
super.onDestroy()
if(localBroadCastManager != null) {
localBroadCastManager!!.unregisterReceiver(mBroadcastReceiver)
}
if(mqttModule != null) {
mqttModule!!.pause()
mqttModule?.pause()
mqttModule = null
}
if(localBroadCastManager != null) {
localBroadCastManager?.unregisterReceiver(mBroadcastReceiver)
}
cameraReader.stopCamera()
sensorReader.stopReadings()
stopHttp()
Expand Down Expand Up @@ -334,11 +333,9 @@ class WallPanelService : LifecycleService(), MQTTModule.MQTTListener {
}
}

private val restartMqttRunnable = object: Runnable {
override fun run() {
if (mqttModule != null) {
mqttModule!!.restart()
}
private val restartMqttRunnable = Runnable {
if (mqttModule != null) {
mqttModule!!.restart()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,12 @@ class CameraSettingsFragment : BaseSettingsFragment() {
qrCodePreference = findPreference("button_key_qr_code")
cameraTestPreference = findPreference("button_key_camera_test")

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (ActivityCompat.checkSelfPermission(activity!!.applicationContext, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && activity != null) {
if (ActivityCompat.checkSelfPermission(activity!!, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
configuration.cameraEnabled = false
dialogUtils.showAlertDialog(activity!!.applicationContext, getString(R.string.dialog_no_camera_permissions))
if(activity != null) {
dialogUtils.showAlertDialog(activity!!, getString(R.string.dialog_no_camera_permissions))
}
return
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ class DialogUtils(base: Context?) : ContextWrapper(base), LifecycleObserver {
hideDialog()
}

fun hideDialog() {
private fun hideDialog() {
if (dialog != null && dialog!!.isShowing) {
dialog!!.dismiss()
dialog = null
}
}

fun hideAlertDialog() {
private fun hideAlertDialog() {
if (alertDialog != null && alertDialog!!.isShowing) {
alertDialog!!.dismiss()
alertDialog = null
Expand Down

0 comments on commit 30a1627

Please sign in to comment.