Skip to content

Commit f98a9eb

Browse files
committed
use Otto for dispatching events instead of an interface
1 parent a9a9fa9 commit f98a9eb

File tree

12 files changed

+177
-57
lines changed

12 files changed

+177
-57
lines changed

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ dependencies {
3030
compile fileTree(dir: 'libs', include: ['*.jar'])
3131
compile 'com.android.support:appcompat-v7:23.4.0'
3232
compile 'com.jakewharton:butterknife:8.0.1'
33+
compile 'com.squareup:otto:1.3.8'
3334

3435
apt 'com.jakewharton:butterknife-compiler:8.0.1'
3536
}

app/proguard-rules.pro

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@
1515
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
1616
# public *;
1717
#}
18+
# Otto
19+
-keepattributes *Annotation*
20+
-keepclassmembers class ** {
21+
@com.squareup.otto.Subscribe public *;
22+
@com.squareup.otto.Produce public *;
23+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.simplemobiletools.flashlight;
2+
3+
import com.squareup.otto.Bus;
4+
5+
public class BusProvider {
6+
private static final Bus BUS = new Bus();
7+
8+
public static Bus getInstance() {
9+
return BUS;
10+
}
11+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.simplemobiletools.flashlight;
2+
3+
public class Events {
4+
public static class StateChanged {
5+
private static boolean mIsEnabled;
6+
7+
StateChanged(boolean isEnabled) {
8+
mIsEnabled = isEnabled;
9+
}
10+
11+
public boolean getIsEnabled() {
12+
return mIsEnabled;
13+
}
14+
}
15+
16+
public static class CameraUnavailable {
17+
}
18+
}

app/src/main/java/com/simplemobiletools/flashlight/MarshmallowCamera.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,26 @@
77
import android.os.Build;
88
import android.util.Log;
99

10+
import com.squareup.otto.Bus;
11+
1012
public class MarshmallowCamera {
1113
private static final String TAG = MyCameraImpl.class.getSimpleName();
1214

13-
private MyCamera mCallback;
1415
private Context mContext;
1516

16-
public MarshmallowCamera(MyCamera camera, Context cxt) {
17-
mCallback = camera;
17+
public MarshmallowCamera(Context cxt) {
1818
mContext = cxt;
1919
}
2020

2121
@TargetApi(Build.VERSION_CODES.M)
22-
public void toggleMarshmallowFlashlight(boolean enable) {
22+
public void toggleMarshmallowFlashlight(final Bus bus, boolean enable) {
2323
try {
2424
final CameraManager manager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
2525
final String[] list = manager.getCameraIdList();
2626
manager.setTorchMode(list[0], enable);
2727
} catch (CameraAccessException e) {
2828
Log.e(TAG, "toggle marshmallow flashlight " + e.getMessage());
29-
mCallback.cameraUnavailable();
29+
bus.post(new Events.CameraUnavailable());
3030
}
3131
}
3232
}

app/src/main/java/com/simplemobiletools/flashlight/MyCamera.java

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

app/src/main/java/com/simplemobiletools/flashlight/MyCameraImpl.java

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,35 @@
44
import android.hardware.Camera;
55
import android.util.Log;
66

7+
import com.squareup.otto.Bus;
8+
79
public class MyCameraImpl {
810
private static final String TAG = MyCameraImpl.class.getSimpleName();
911
private static Camera mCamera;
1012
private static Camera.Parameters mParams;
11-
private static MyCamera mCallback;
1213
private static Context mContext;
1314
private static MarshmallowCamera mMarshmallowCamera;
15+
private static Bus mBus;
1416

1517
private static boolean mIsFlashlightOn;
1618
private static boolean mIsMarshmallow;
1719

18-
public MyCameraImpl(MyCamera camera, Context cxt) {
19-
mCallback = camera;
20+
public MyCameraImpl(Context cxt) {
2021
mContext = cxt;
2122
mIsMarshmallow = isMarshmallow();
23+
24+
if (mBus == null) {
25+
mBus = BusProvider.getInstance();
26+
mBus.register(this);
27+
}
28+
2229
handleCameraSetup();
30+
checkFlashlight();
2331
}
2432

2533
public void toggleFlashlight() {
26-
handleCameraSetup();
2734
mIsFlashlightOn = !mIsFlashlightOn;
28-
29-
if (mIsFlashlightOn) {
30-
enableFlashlight();
31-
} else {
32-
disableFlashlight();
33-
}
35+
handleCameraSetup();
3436
}
3537

3638
public void handleCameraSetup() {
@@ -39,11 +41,12 @@ public void handleCameraSetup() {
3941
} else {
4042
setupCamera();
4143
}
44+
checkFlashlight();
4245
}
4346

4447
private void setupMarshmallowCamera() {
4548
if (mMarshmallowCamera == null) {
46-
mMarshmallowCamera = new MarshmallowCamera(mCallback, mContext);
49+
mMarshmallowCamera = new MarshmallowCamera(mContext);
4750
}
4851
}
4952

@@ -57,17 +60,23 @@ private void setupCamera() {
5760
mParams = mCamera.getParameters();
5861
mParams.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
5962
mCamera.setParameters(mParams);
60-
61-
if (mIsFlashlightOn)
62-
enableFlashlight();
6363
} catch (Exception e) {
6464
Log.e(TAG, "setup mCamera " + e.getMessage());
65-
mCallback.cameraUnavailable();
65+
mBus.post(new Events.CameraUnavailable());
6666
}
6767
}
6868
}
6969

70-
private void enableFlashlight() {
70+
public void checkFlashlight() {
71+
if (mIsFlashlightOn) {
72+
enableFlashlight();
73+
} else {
74+
disableFlashlight();
75+
}
76+
}
77+
78+
public void enableFlashlight() {
79+
mIsFlashlightOn = true;
7180
if (mIsMarshmallow) {
7281
toggleMarshmallowFlashlight(true);
7382
} else {
@@ -78,10 +87,11 @@ private void enableFlashlight() {
7887
mParams.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
7988
mCamera.setParameters(mParams);
8089
}
81-
mCallback.enableFlashlight();
90+
mBus.post(new Events.StateChanged(true));
8291
}
8392

8493
private void disableFlashlight() {
94+
mIsFlashlightOn = false;
8595
if (isMarshmallow()) {
8696
toggleMarshmallowFlashlight(false);
8797
} else {
@@ -92,18 +102,27 @@ private void disableFlashlight() {
92102
mParams.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
93103
mCamera.setParameters(mParams);
94104
}
95-
mCallback.disableFlashlight();
105+
mBus.post(new Events.StateChanged(false));
96106
}
97107

98108
private void toggleMarshmallowFlashlight(boolean enable) {
99-
mMarshmallowCamera.toggleMarshmallowFlashlight(enable);
109+
mMarshmallowCamera.toggleMarshmallowFlashlight(mBus, enable);
100110
}
101111

102112
public void releaseCamera() {
113+
if (mIsFlashlightOn) {
114+
disableFlashlight();
115+
}
116+
103117
if (mCamera != null) {
104118
mCamera.release();
105119
mCamera = null;
106120
}
121+
122+
if (mBus != null) {
123+
mBus.unregister(this);
124+
}
125+
mIsFlashlightOn = false;
107126
}
108127

109128
private boolean isMarshmallow() {

app/src/main/java/com/simplemobiletools/flashlight/MyWidgetProvider.java

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,17 @@
1010
import android.graphics.Bitmap;
1111
import android.widget.RemoteViews;
1212

13-
public class MyWidgetProvider extends AppWidgetProvider implements MyCamera {
13+
import com.squareup.otto.Bus;
14+
import com.squareup.otto.Subscribe;
15+
16+
public class MyWidgetProvider extends AppWidgetProvider {
1417
private static final String TOGGLE = "toggle";
1518
private static MyCameraImpl mCameraImpl;
1619
private static RemoteViews mRemoteViews;
1720
private static AppWidgetManager mWidgetManager;
1821
private static Bitmap mColoredBmp;
22+
private static Bus mBus;
23+
private static Context mContext;
1924

2025
private static int[] mWidgetIds;
2126

@@ -26,6 +31,7 @@ public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] a
2631
}
2732

2833
private void initVariables(Context context) {
34+
mContext = context;
2935
final ComponentName component = new ComponentName(context, MyWidgetProvider.class);
3036
mWidgetManager = AppWidgetManager.getInstance(context);
3137
mWidgetIds = mWidgetManager.getAppWidgetIds(component);
@@ -36,18 +42,23 @@ private void initVariables(Context context) {
3642
final PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
3743
mRemoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
3844
mRemoteViews.setOnClickPendingIntent(R.id.toggle_btn, pendingIntent);
39-
mCameraImpl = new MyCameraImpl(this, context);
45+
mCameraImpl = new MyCameraImpl(context);
4046

4147
final Resources res = context.getResources();
4248
final int appColor = res.getColor(R.color.colorPrimary);
4349
mColoredBmp = Utils.getColoredIcon(context.getResources(), appColor, R.mipmap.flashlight_small);
50+
51+
if (mBus == null) {
52+
mBus = BusProvider.getInstance();
53+
}
54+
registerBus();
4455
}
4556

4657
@Override
4758
public void onReceive(Context context, Intent intent) {
4859
final String action = intent.getAction();
4960
if (action.equals(TOGGLE)) {
50-
if (mCameraImpl == null) {
61+
if (mCameraImpl == null || mBus == null) {
5162
initVariables(context);
5263
}
5364

@@ -56,38 +67,62 @@ public void onReceive(Context context, Intent intent) {
5667
super.onReceive(context, intent);
5768
}
5869

59-
@Override
6070
public void enableFlashlight() {
6171
mRemoteViews.setImageViewBitmap(R.id.toggle_btn, mColoredBmp);
6272
for (int widgetId : mWidgetIds) {
6373
mWidgetManager.updateAppWidget(widgetId, mRemoteViews);
6474
}
6575
}
6676

67-
@Override
6877
public void disableFlashlight() {
6978
mRemoteViews.setImageViewResource(R.id.toggle_btn, R.mipmap.flashlight_small);
7079
for (int widgetId : mWidgetIds) {
7180
mWidgetManager.updateAppWidget(widgetId, mRemoteViews);
7281
}
73-
mCameraImpl.releaseCamera();
7482
}
7583

76-
@Override
77-
public void cameraUnavailable() {
84+
@Subscribe
85+
public void cameraUnavailable(Events.CameraUnavailable event) {
86+
if (mContext != null) {
87+
Utils.showToast(mContext, R.string.camera_error);
88+
disableFlashlight();
89+
}
90+
}
91+
92+
@Subscribe
93+
public void stateChangedEvent(Events.StateChanged event) {
94+
if (event.getIsEnabled()) {
95+
enableFlashlight();
96+
} else {
97+
disableFlashlight();
98+
}
7899
}
79100

80101
@Override
81102
public void onDeleted(Context context, int[] appWidgetIds) {
82103
super.onDeleted(context, appWidgetIds);
104+
unregisterBus();
83105
releaseCamera(context);
84106
}
85107

86108
private void releaseCamera(Context context) {
87109
if (mCameraImpl == null)
88110
initVariables(context);
89111

90-
disableFlashlight();
91112
mCameraImpl.releaseCamera();
92113
}
114+
115+
private void registerBus() {
116+
try {
117+
mBus.register(this);
118+
} catch (Exception ignored) {
119+
}
120+
}
121+
122+
private void unregisterBus() {
123+
try {
124+
mBus.unregister(this);
125+
} catch (Exception ignored) {
126+
}
127+
}
93128
}

app/src/main/java/com/simplemobiletools/flashlight/activities/LicenseActivity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ public void butterKnifeClicked() {
2424
openUrl(R.string.butterknife_url);
2525
}
2626

27+
@OnClick(R.id.license_otto_title)
28+
public void ottoClicked() {
29+
openUrl(R.string.otto_url);
30+
}
31+
2732
private void openUrl(int id) {
2833
final String url = getResources().getString(id);
2934
final Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

0 commit comments

Comments
 (0)