Skip to content

Commit acc7f9c

Browse files
committed
Fixed leaks of background threads
1 parent 96782f0 commit acc7f9c

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ allprojects {
2424
}
2525

2626
project.ext {
27-
sdkVersion='0.8.3'
27+
sdkVersion='0.8.4'
2828
versionCode=1
2929

3030
compileSdkVersion=28

ui/src/main/java/io/snabble/sdk/ui/scanner/BarcodeView.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public class BarcodeView extends AppCompatImageView {
3939
private boolean adjustBrightness = true;
4040

4141
private Handler uiHandler;
42-
private Handler backgroundHandler;
4342
private int backgroundColor;
4443

4544
public BarcodeView(Context context) {
@@ -60,10 +59,6 @@ public BarcodeView(Context context, AttributeSet attrs, int defStyleAttr) {
6059
private void init(Context context, AttributeSet attrs) {
6160
uiHandler = new Handler(Looper.getMainLooper());
6261

63-
HandlerThread handlerThread = new HandlerThread("BarcodeView");
64-
handlerThread.start();
65-
backgroundHandler = new Handler(handlerThread.getLooper());
66-
6762
if (attrs != null) {
6863
TypedArray arr = context.getTheme().obtainStyledAttributes(
6964
attrs, R.styleable.BarcodeView, 0, 0);
@@ -155,6 +150,9 @@ private void generate() {
155150
height = h;
156151
generatedText = text;
157152

153+
final HandlerThread handlerThread = new HandlerThread("BarcodeView");
154+
handlerThread.start();
155+
final Handler backgroundHandler = new Handler(handlerThread.getLooper());
158156
backgroundHandler.post(new Runnable() {
159157
@Override
160158
public void run() {
@@ -220,6 +218,8 @@ public void run() {
220218
}
221219
});
222220
}
221+
222+
handlerThread.quit();
223223
}
224224
});
225225
}

ui/src/main/java/io/snabble/sdk/ui/search/SearchableProductAdapter.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ public enum SearchType {
2828
FOLDED_NAME
2929
}
3030

31+
private static Handler backgroundHandler;
32+
private Handler uiHandler;
33+
3134
private OnProductSelectedListener productSelectedListener;
3235
private SearchType searchType = SearchType.FOLDED_NAME;
3336
private ProductDatabase productDatabase;
3437
private Cursor cursor;
35-
private Handler backgroundHandler;
36-
private Handler uiHandler;
3738
private CancellationSignal cancellationSignal;
3839
private int itemCount = 0;
3940
private boolean showBarcode = true;
@@ -46,9 +47,11 @@ public SearchableProductAdapter() {
4647
public SearchableProductAdapter(ProductDatabase productDatabase) {
4748
this.productDatabase = productDatabase;
4849

49-
HandlerThread handlerThread = new HandlerThread("SearchableProductAdapter");
50-
handlerThread.start();
51-
backgroundHandler = new Handler(handlerThread.getLooper());
50+
if(backgroundHandler == null) {
51+
HandlerThread frameProcessingThread = new HandlerThread("SearchableProductAdapter");
52+
frameProcessingThread.start();
53+
backgroundHandler = new Handler(frameProcessingThread.getLooper());
54+
}
5255

5356
uiHandler = new Handler(Looper.getMainLooper());
5457
}

0 commit comments

Comments
 (0)