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 {
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'
}

View File

@ -12,7 +12,7 @@ import android.view.MenuItem
import android.view.WindowManager
import android.widget.ImageView
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.isNougatMR1Plus
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.R
import com.simplemobiletools.flashlight.extensions.config
import com.simplemobiletools.flashlight.helpers.BusProvider
import com.simplemobiletools.flashlight.helpers.MyCameraImpl
import com.simplemobiletools.flashlight.models.Events
import com.squareup.otto.Bus
import com.squareup.otto.Subscribe
import kotlinx.android.synthetic.main.activity_main.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import java.util.*
class MainActivity : SimpleActivity() {
@ -34,7 +33,7 @@ class MainActivity : SimpleActivity() {
private val FLASHLIGHT_STATE = "flashlight_state"
private val STROBOSCOPE_STATE = "stroboscope_state"
private var mBus: Bus? = null
private var mBus: EventBus? = null
private var mCameraImpl: MyCameraImpl? = null
private var mIsFlashlightOn = false
@ -43,7 +42,7 @@ class MainActivity : SimpleActivity() {
setContentView(R.layout.activity_main)
appLaunched(BuildConfig.APPLICATION_ID)
mBus = BusProvider.instance
mBus = EventBus.getDefault()
changeIconColor(getContrastColor(), stroboscope_btn)
bright_display_btn.setOnClickListener {
@ -151,7 +150,7 @@ class MainActivity : SimpleActivity() {
}
private fun launchAbout() {
val licenses = LICENSE_OTTO
val licenses = LICENSE_EVENT_BUS
val faqItems = arrayListOf(
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.Handler
import com.simplemobiletools.flashlight.models.Events
import com.squareup.otto.Bus
import org.greenrobot.eventbus.EventBus
internal class MarshmallowCamera constructor(val context: Context) {
@ -21,12 +21,12 @@ internal class MarshmallowCamera constructor(val context: Context) {
}
@TargetApi(Build.VERSION_CODES.M)
fun toggleMarshmallowFlashlight(bus: Bus, enable: Boolean) {
fun toggleMarshmallowFlashlight(enable: Boolean) {
try {
manager.setTorchMode(cameraId!!, enable)
} catch (e: Exception) {
val mainRunnable = Runnable {
bus.post(Events.CameraUnavailable())
EventBus.getDefault().post(Events.CameraUnavailable())
}
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.updateWidgets
import com.simplemobiletools.flashlight.models.Events
import com.squareup.otto.Bus
import org.greenrobot.eventbus.EventBus
import java.io.IOException
class MyCameraImpl(val context: Context) {
@ -23,16 +23,18 @@ class MyCameraImpl(val context: Context) {
private var camera: Camera? = null
private var params: Camera.Parameters? = null
private var bus: Bus? = null
private var isMarshmallow = false
private var shouldEnableFlashlight = false
private var isStroboSOS = false // are we sending SOS, or casual stroboscope?
private var marshmallowCamera: MarshmallowCamera? = null
@Volatile
private var shouldStroboscopeStop = false
@Volatile
private var isStroboscopeRunning = false
@Volatile
private var isSOSRunning = false
@ -41,12 +43,6 @@ class MyCameraImpl(val context: Context) {
init {
isMarshmallow = isMarshmallowPlus()
if (bus == null) {
bus = BusProvider.instance
bus!!.register(this)
}
handleCameraSetup()
stroboFrequency = context.config.stroboscopeFrequency
}
@ -82,7 +78,7 @@ class MyCameraImpl(val context: Context) {
fun stopStroboscope() {
shouldStroboscopeStop = true
bus!!.post(Events.StopStroboscope())
EventBus.getDefault().post(Events.StopStroboscope())
}
fun toggleSOS(): Boolean {
@ -115,7 +111,7 @@ class MyCameraImpl(val context: Context) {
fun stopSOS() {
shouldStroboscopeStop = true
bus!!.post(Events.StopSOS())
EventBus.getDefault().post(Events.StopSOS())
}
private fun tryInitCamera(): Boolean {
@ -163,7 +159,7 @@ class MyCameraImpl(val context: Context) {
params!!.flashMode = Camera.Parameters.FLASH_MODE_OFF
camera!!.parameters = params
} 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) {
isFlashlightOn = isEnabled
bus!!.post(Events.StateChanged(isEnabled))
EventBus.getDefault().post(Events.StateChanged(isEnabled))
context.updateWidgets(isEnabled)
}
private fun toggleMarshmallowFlashlight(enable: Boolean) {
marshmallowCamera!!.toggleMarshmallowFlashlight(bus!!, enable)
marshmallowCamera!!.toggleMarshmallowFlashlight(enable)
}
fun releaseCamera() {
@ -239,7 +235,6 @@ class MyCameraImpl(val context: Context) {
camera?.release()
camera = null
bus?.unregister(this)
isFlashlightOn = false
shouldStroboscopeStop = true
}
@ -260,10 +255,10 @@ class MyCameraImpl(val context: Context) {
if (isNougatPlus()) {
while (!shouldStroboscopeStop) {
try {
marshmallowCamera!!.toggleMarshmallowFlashlight(bus!!, true)
marshmallowCamera!!.toggleMarshmallowFlashlight(true)
val onDuration = if (isStroboSOS) SOS[sosIndex++ % SOS.size] else stroboFrequency
Thread.sleep(onDuration)
marshmallowCamera!!.toggleMarshmallowFlashlight(bus!!, false)
marshmallowCamera!!.toggleMarshmallowFlashlight(false)
val offDuration = if (isStroboSOS) SOS[sosIndex++ % SOS.size] else stroboFrequency
Thread.sleep(offDuration)
} catch (e: Exception) {