Skip to content

Commit 7eafac0

Browse files
committed
replacing Otto with EventBus
1 parent 1b15544 commit 7eafac0

File tree

5 files changed

+21
-34
lines changed

5 files changed

+21
-34
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@ android {
5757

5858
dependencies {
5959
implementation 'com.simplemobiletools:commons:5.28.23'
60-
implementation 'com.squareup:otto:1.3.8'
60+
implementation 'org.greenrobot:eventbus:3.2.0'
6161
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta4'
6262
}

app/src/main/kotlin/com/simplemobiletools/flashlight/activities/MainActivity.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,19 @@ import android.view.MenuItem
1212
import android.view.WindowManager
1313
import android.widget.ImageView
1414
import com.simplemobiletools.commons.extensions.*
15-
import com.simplemobiletools.commons.helpers.LICENSE_OTTO
15+
import com.simplemobiletools.commons.helpers.LICENSE_EVENT_BUS
1616
import com.simplemobiletools.commons.helpers.PERMISSION_CAMERA
1717
import com.simplemobiletools.commons.helpers.isNougatMR1Plus
1818
import com.simplemobiletools.commons.helpers.isNougatPlus
1919
import com.simplemobiletools.commons.models.FAQItem
2020
import com.simplemobiletools.flashlight.BuildConfig
2121
import com.simplemobiletools.flashlight.R
2222
import com.simplemobiletools.flashlight.extensions.config
23-
import com.simplemobiletools.flashlight.helpers.BusProvider
2423
import com.simplemobiletools.flashlight.helpers.MyCameraImpl
2524
import com.simplemobiletools.flashlight.models.Events
26-
import com.squareup.otto.Bus
27-
import com.squareup.otto.Subscribe
2825
import kotlinx.android.synthetic.main.activity_main.*
26+
import org.greenrobot.eventbus.EventBus
27+
import org.greenrobot.eventbus.Subscribe
2928
import java.util.*
3029

3130
class MainActivity : SimpleActivity() {
@@ -34,7 +33,7 @@ class MainActivity : SimpleActivity() {
3433
private val FLASHLIGHT_STATE = "flashlight_state"
3534
private val STROBOSCOPE_STATE = "stroboscope_state"
3635

37-
private var mBus: Bus? = null
36+
private var mBus: EventBus? = null
3837
private var mCameraImpl: MyCameraImpl? = null
3938
private var mIsFlashlightOn = false
4039

@@ -43,7 +42,7 @@ class MainActivity : SimpleActivity() {
4342
setContentView(R.layout.activity_main)
4443
appLaunched(BuildConfig.APPLICATION_ID)
4544

46-
mBus = BusProvider.instance
45+
mBus = EventBus.getDefault()
4746
changeIconColor(getContrastColor(), stroboscope_btn)
4847

4948
bright_display_btn.setOnClickListener {
@@ -151,7 +150,7 @@ class MainActivity : SimpleActivity() {
151150
}
152151

153152
private fun launchAbout() {
154-
val licenses = LICENSE_OTTO
153+
val licenses = LICENSE_EVENT_BUS
155154

156155
val faqItems = arrayListOf(
157156
FAQItem(R.string.faq_1_title_commons, R.string.faq_1_text_commons),

app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/BusProvider.kt

Lines changed: 0 additions & 7 deletions
This file was deleted.

app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/MarshmallowCamera.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import android.hardware.camera2.CameraManager
66
import android.os.Build
77
import android.os.Handler
88
import com.simplemobiletools.flashlight.models.Events
9-
import com.squareup.otto.Bus
9+
import org.greenrobot.eventbus.EventBus
1010

1111
internal class MarshmallowCamera constructor(val context: Context) {
1212

@@ -21,12 +21,12 @@ internal class MarshmallowCamera constructor(val context: Context) {
2121
}
2222

2323
@TargetApi(Build.VERSION_CODES.M)
24-
fun toggleMarshmallowFlashlight(bus: Bus, enable: Boolean) {
24+
fun toggleMarshmallowFlashlight(enable: Boolean) {
2525
try {
2626
manager.setTorchMode(cameraId!!, enable)
2727
} catch (e: Exception) {
2828
val mainRunnable = Runnable {
29-
bus.post(Events.CameraUnavailable())
29+
EventBus.getDefault().post(Events.CameraUnavailable())
3030
}
3131
Handler(context.mainLooper).post(mainRunnable)
3232
}

app/src/main/kotlin/com/simplemobiletools/flashlight/helpers/MyCameraImpl.kt

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import com.simplemobiletools.flashlight.R
1111
import com.simplemobiletools.flashlight.extensions.config
1212
import com.simplemobiletools.flashlight.extensions.updateWidgets
1313
import com.simplemobiletools.flashlight.models.Events
14-
import com.squareup.otto.Bus
14+
import org.greenrobot.eventbus.EventBus
1515
import java.io.IOException
1616

1717
class MyCameraImpl(val context: Context) {
@@ -23,16 +23,18 @@ class MyCameraImpl(val context: Context) {
2323

2424
private var camera: Camera? = null
2525
private var params: Camera.Parameters? = null
26-
private var bus: Bus? = null
2726
private var isMarshmallow = false
2827
private var shouldEnableFlashlight = false
2928
private var isStroboSOS = false // are we sending SOS, or casual stroboscope?
3029

3130
private var marshmallowCamera: MarshmallowCamera? = null
31+
3232
@Volatile
3333
private var shouldStroboscopeStop = false
34+
3435
@Volatile
3536
private var isStroboscopeRunning = false
37+
3638
@Volatile
3739
private var isSOSRunning = false
3840

@@ -41,12 +43,6 @@ class MyCameraImpl(val context: Context) {
4143

4244
init {
4345
isMarshmallow = isMarshmallowPlus()
44-
45-
if (bus == null) {
46-
bus = BusProvider.instance
47-
bus!!.register(this)
48-
}
49-
5046
handleCameraSetup()
5147
stroboFrequency = context.config.stroboscopeFrequency
5248
}
@@ -82,7 +78,7 @@ class MyCameraImpl(val context: Context) {
8278

8379
fun stopStroboscope() {
8480
shouldStroboscopeStop = true
85-
bus!!.post(Events.StopStroboscope())
81+
EventBus.getDefault().post(Events.StopStroboscope())
8682
}
8783

8884
fun toggleSOS(): Boolean {
@@ -115,7 +111,7 @@ class MyCameraImpl(val context: Context) {
115111

116112
fun stopSOS() {
117113
shouldStroboscopeStop = true
118-
bus!!.post(Events.StopSOS())
114+
EventBus.getDefault().post(Events.StopSOS())
119115
}
120116

121117
private fun tryInitCamera(): Boolean {
@@ -163,7 +159,7 @@ class MyCameraImpl(val context: Context) {
163159
params!!.flashMode = Camera.Parameters.FLASH_MODE_OFF
164160
camera!!.parameters = params
165161
} catch (e: Exception) {
166-
bus!!.post(Events.CameraUnavailable())
162+
EventBus.getDefault().post(Events.CameraUnavailable())
167163
}
168164
}
169165

@@ -223,12 +219,12 @@ class MyCameraImpl(val context: Context) {
223219

224220
private fun stateChanged(isEnabled: Boolean) {
225221
isFlashlightOn = isEnabled
226-
bus!!.post(Events.StateChanged(isEnabled))
222+
EventBus.getDefault().post(Events.StateChanged(isEnabled))
227223
context.updateWidgets(isEnabled)
228224
}
229225

230226
private fun toggleMarshmallowFlashlight(enable: Boolean) {
231-
marshmallowCamera!!.toggleMarshmallowFlashlight(bus!!, enable)
227+
marshmallowCamera!!.toggleMarshmallowFlashlight(enable)
232228
}
233229

234230
fun releaseCamera() {
@@ -239,7 +235,6 @@ class MyCameraImpl(val context: Context) {
239235
camera?.release()
240236
camera = null
241237

242-
bus?.unregister(this)
243238
isFlashlightOn = false
244239
shouldStroboscopeStop = true
245240
}
@@ -260,10 +255,10 @@ class MyCameraImpl(val context: Context) {
260255
if (isNougatPlus()) {
261256
while (!shouldStroboscopeStop) {
262257
try {
263-
marshmallowCamera!!.toggleMarshmallowFlashlight(bus!!, true)
258+
marshmallowCamera!!.toggleMarshmallowFlashlight(true)
264259
val onDuration = if (isStroboSOS) SOS[sosIndex++ % SOS.size] else stroboFrequency
265260
Thread.sleep(onDuration)
266-
marshmallowCamera!!.toggleMarshmallowFlashlight(bus!!, false)
261+
marshmallowCamera!!.toggleMarshmallowFlashlight(false)
267262
val offDuration = if (isStroboSOS) SOS[sosIndex++ % SOS.size] else stroboFrequency
268263
Thread.sleep(offDuration)
269264
} catch (e: Exception) {

0 commit comments

Comments
 (0)