@@ -11,7 +11,7 @@ import com.simplemobiletools.flashlight.R
1111import com.simplemobiletools.flashlight.extensions.config
1212import com.simplemobiletools.flashlight.extensions.updateWidgets
1313import com.simplemobiletools.flashlight.models.Events
14- import com.squareup.otto.Bus
14+ import org.greenrobot.eventbus.EventBus
1515import java.io.IOException
1616
1717class 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