replacing Otto with EventBus

This commit is contained in:
tibbi
2020-05-25 00:10:23 +02:00
parent 1b15544374
commit 7eafac0870
5 changed files with 21 additions and 34 deletions

View File

@ -57,6 +57,6 @@ android {
dependencies { dependencies {
implementation 'com.simplemobiletools:commons:5.28.23' implementation 'com.simplemobiletools:commons:5.28.23'
implementation 'com.squareup:otto:1.3.8' implementation 'org.greenrobot:eventbus:3.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta4' implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta4'
} }

View File

@ -12,7 +12,7 @@ import android.view.MenuItem
import android.view.WindowManager import android.view.WindowManager
import android.widget.ImageView import android.widget.ImageView
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.LICENSE_OTTO import com.simplemobiletools.commons.helpers.LICENSE_EVENT_BUS
import com.simplemobiletools.commons.helpers.PERMISSION_CAMERA import com.simplemobiletools.commons.helpers.PERMISSION_CAMERA
import com.simplemobiletools.commons.helpers.isNougatMR1Plus import com.simplemobiletools.commons.helpers.isNougatMR1Plus
import com.simplemobiletools.commons.helpers.isNougatPlus import com.simplemobiletools.commons.helpers.isNougatPlus
@ -20,12 +20,11 @@ import com.simplemobiletools.commons.models.FAQItem
import com.simplemobiletools.flashlight.BuildConfig import com.simplemobiletools.flashlight.BuildConfig
import com.simplemobiletools.flashlight.R import com.simplemobiletools.flashlight.R
import com.simplemobiletools.flashlight.extensions.config import com.simplemobiletools.flashlight.extensions.config
import com.simplemobiletools.flashlight.helpers.BusProvider
import com.simplemobiletools.flashlight.helpers.MyCameraImpl import com.simplemobiletools.flashlight.helpers.MyCameraImpl
import com.simplemobiletools.flashlight.models.Events import com.simplemobiletools.flashlight.models.Events
import com.squareup.otto.Bus
import com.squareup.otto.Subscribe
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import java.util.* import java.util.*
class MainActivity : SimpleActivity() { class MainActivity : SimpleActivity() {
@ -34,7 +33,7 @@ class MainActivity : SimpleActivity() {
private val FLASHLIGHT_STATE = "flashlight_state" private val FLASHLIGHT_STATE = "flashlight_state"
private val STROBOSCOPE_STATE = "stroboscope_state" private val STROBOSCOPE_STATE = "stroboscope_state"
private var mBus: Bus? = null private var mBus: EventBus? = null
private var mCameraImpl: MyCameraImpl? = null private var mCameraImpl: MyCameraImpl? = null
private var mIsFlashlightOn = false private var mIsFlashlightOn = false
@ -43,7 +42,7 @@ class MainActivity : SimpleActivity() {
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
appLaunched(BuildConfig.APPLICATION_ID) appLaunched(BuildConfig.APPLICATION_ID)
mBus = BusProvider.instance mBus = EventBus.getDefault()
changeIconColor(getContrastColor(), stroboscope_btn) changeIconColor(getContrastColor(), stroboscope_btn)
bright_display_btn.setOnClickListener { bright_display_btn.setOnClickListener {
@ -151,7 +150,7 @@ class MainActivity : SimpleActivity() {
} }
private fun launchAbout() { private fun launchAbout() {
val licenses = LICENSE_OTTO val licenses = LICENSE_EVENT_BUS
val faqItems = arrayListOf( val faqItems = arrayListOf(
FAQItem(R.string.faq_1_title_commons, R.string.faq_1_text_commons), FAQItem(R.string.faq_1_title_commons, R.string.faq_1_text_commons),

View File

@ -1,7 +0,0 @@
package com.simplemobiletools.flashlight.helpers
import com.squareup.otto.Bus
object BusProvider {
val instance = Bus()
}

View File

@ -6,7 +6,7 @@ import android.hardware.camera2.CameraManager
import android.os.Build import android.os.Build
import android.os.Handler import android.os.Handler
import com.simplemobiletools.flashlight.models.Events import com.simplemobiletools.flashlight.models.Events
import com.squareup.otto.Bus import org.greenrobot.eventbus.EventBus
internal class MarshmallowCamera constructor(val context: Context) { internal class MarshmallowCamera constructor(val context: Context) {
@ -21,12 +21,12 @@ internal class MarshmallowCamera constructor(val context: Context) {
} }
@TargetApi(Build.VERSION_CODES.M) @TargetApi(Build.VERSION_CODES.M)
fun toggleMarshmallowFlashlight(bus: Bus, enable: Boolean) { fun toggleMarshmallowFlashlight(enable: Boolean) {
try { try {
manager.setTorchMode(cameraId!!, enable) manager.setTorchMode(cameraId!!, enable)
} catch (e: Exception) { } catch (e: Exception) {
val mainRunnable = Runnable { val mainRunnable = Runnable {
bus.post(Events.CameraUnavailable()) EventBus.getDefault().post(Events.CameraUnavailable())
} }
Handler(context.mainLooper).post(mainRunnable) Handler(context.mainLooper).post(mainRunnable)
} }

View File

@ -11,7 +11,7 @@ import com.simplemobiletools.flashlight.R
import com.simplemobiletools.flashlight.extensions.config import com.simplemobiletools.flashlight.extensions.config
import com.simplemobiletools.flashlight.extensions.updateWidgets import com.simplemobiletools.flashlight.extensions.updateWidgets
import com.simplemobiletools.flashlight.models.Events import com.simplemobiletools.flashlight.models.Events
import com.squareup.otto.Bus import org.greenrobot.eventbus.EventBus
import java.io.IOException import java.io.IOException
class MyCameraImpl(val context: Context) { class MyCameraImpl(val context: Context) {
@ -23,16 +23,18 @@ class MyCameraImpl(val context: Context) {
private var camera: Camera? = null private var camera: Camera? = null
private var params: Camera.Parameters? = null private var params: Camera.Parameters? = null
private var bus: Bus? = null
private var isMarshmallow = false private var isMarshmallow = false
private var shouldEnableFlashlight = false private var shouldEnableFlashlight = false
private var isStroboSOS = false // are we sending SOS, or casual stroboscope? private var isStroboSOS = false // are we sending SOS, or casual stroboscope?
private var marshmallowCamera: MarshmallowCamera? = null private var marshmallowCamera: MarshmallowCamera? = null
@Volatile @Volatile
private var shouldStroboscopeStop = false private var shouldStroboscopeStop = false
@Volatile @Volatile
private var isStroboscopeRunning = false private var isStroboscopeRunning = false
@Volatile @Volatile
private var isSOSRunning = false private var isSOSRunning = false
@ -41,12 +43,6 @@ class MyCameraImpl(val context: Context) {
init { init {
isMarshmallow = isMarshmallowPlus() isMarshmallow = isMarshmallowPlus()
if (bus == null) {
bus = BusProvider.instance
bus!!.register(this)
}
handleCameraSetup() handleCameraSetup()
stroboFrequency = context.config.stroboscopeFrequency stroboFrequency = context.config.stroboscopeFrequency
} }
@ -82,7 +78,7 @@ class MyCameraImpl(val context: Context) {
fun stopStroboscope() { fun stopStroboscope() {
shouldStroboscopeStop = true shouldStroboscopeStop = true
bus!!.post(Events.StopStroboscope()) EventBus.getDefault().post(Events.StopStroboscope())
} }
fun toggleSOS(): Boolean { fun toggleSOS(): Boolean {
@ -115,7 +111,7 @@ class MyCameraImpl(val context: Context) {
fun stopSOS() { fun stopSOS() {
shouldStroboscopeStop = true shouldStroboscopeStop = true
bus!!.post(Events.StopSOS()) EventBus.getDefault().post(Events.StopSOS())
} }
private fun tryInitCamera(): Boolean { private fun tryInitCamera(): Boolean {
@ -163,7 +159,7 @@ class MyCameraImpl(val context: Context) {
params!!.flashMode = Camera.Parameters.FLASH_MODE_OFF params!!.flashMode = Camera.Parameters.FLASH_MODE_OFF
camera!!.parameters = params camera!!.parameters = params
} catch (e: Exception) { } catch (e: Exception) {
bus!!.post(Events.CameraUnavailable()) EventBus.getDefault().post(Events.CameraUnavailable())
} }
} }
@ -223,12 +219,12 @@ class MyCameraImpl(val context: Context) {
private fun stateChanged(isEnabled: Boolean) { private fun stateChanged(isEnabled: Boolean) {
isFlashlightOn = isEnabled isFlashlightOn = isEnabled
bus!!.post(Events.StateChanged(isEnabled)) EventBus.getDefault().post(Events.StateChanged(isEnabled))
context.updateWidgets(isEnabled) context.updateWidgets(isEnabled)
} }
private fun toggleMarshmallowFlashlight(enable: Boolean) { private fun toggleMarshmallowFlashlight(enable: Boolean) {
marshmallowCamera!!.toggleMarshmallowFlashlight(bus!!, enable) marshmallowCamera!!.toggleMarshmallowFlashlight(enable)
} }
fun releaseCamera() { fun releaseCamera() {
@ -239,7 +235,6 @@ class MyCameraImpl(val context: Context) {
camera?.release() camera?.release()
camera = null camera = null
bus?.unregister(this)
isFlashlightOn = false isFlashlightOn = false
shouldStroboscopeStop = true shouldStroboscopeStop = true
} }
@ -260,10 +255,10 @@ class MyCameraImpl(val context: Context) {
if (isNougatPlus()) { if (isNougatPlus()) {
while (!shouldStroboscopeStop) { while (!shouldStroboscopeStop) {
try { try {
marshmallowCamera!!.toggleMarshmallowFlashlight(bus!!, true) marshmallowCamera!!.toggleMarshmallowFlashlight(true)
val onDuration = if (isStroboSOS) SOS[sosIndex++ % SOS.size] else stroboFrequency val onDuration = if (isStroboSOS) SOS[sosIndex++ % SOS.size] else stroboFrequency
Thread.sleep(onDuration) Thread.sleep(onDuration)
marshmallowCamera!!.toggleMarshmallowFlashlight(bus!!, false) marshmallowCamera!!.toggleMarshmallowFlashlight(false)
val offDuration = if (isStroboSOS) SOS[sosIndex++ % SOS.size] else stroboFrequency val offDuration = if (isStroboSOS) SOS[sosIndex++ % SOS.size] else stroboFrequency
Thread.sleep(offDuration) Thread.sleep(offDuration)
} catch (e: Exception) { } catch (e: Exception) {