Unwind DecodeHandler out of DecodeThread to avoid a VerifyError ?
[zxing.git] / android / src / com / google / zxing / client / android / DecodeThread.java
1 /*
2  * Copyright (C) 2008 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.client.android;
18
19 import com.google.zxing.BarcodeFormat;
20 import com.google.zxing.DecodeHintType;
21 import com.google.zxing.ResultPointCallback;
22
23 import android.content.SharedPreferences;
24 import android.os.Handler;
25 import android.os.Looper;
26 import android.preference.PreferenceManager;
27
28 import java.util.Hashtable;
29 import java.util.Vector;
30
31 /**
32  * This thread does all the heavy lifting of decoding the images.
33  *
34  * @author dswitkin@google.com (Daniel Switkin)
35  */
36 final class DecodeThread extends Thread {
37
38   public static final String BARCODE_BITMAP = "barcode_bitmap";
39
40   private final Handler handler;
41
42   DecodeThread(CaptureActivity activity,
43                Vector<BarcodeFormat> decodeFormats,
44                String characterSet,
45                ResultPointCallback resultPointCallback) {
46     Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(3);
47
48     // The prefs can't change while the thread is running, so pick them up once here.
49     if (decodeFormats == null || decodeFormats.isEmpty()) {
50       SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
51       boolean decode1D = prefs.getBoolean(PreferencesActivity.KEY_DECODE_1D, true);
52       boolean decodeQR = prefs.getBoolean(PreferencesActivity.KEY_DECODE_QR, true);
53       if (decode1D && decodeQR) {
54         hints.put(DecodeHintType.POSSIBLE_FORMATS, CaptureActivity.ALL_FORMATS);
55       } else if (decode1D) {
56         hints.put(DecodeHintType.POSSIBLE_FORMATS, CaptureActivity.ONE_D_FORMATS);
57       } else if (decodeQR) {
58         hints.put(DecodeHintType.POSSIBLE_FORMATS, CaptureActivity.QR_CODE_FORMATS);
59       }
60     } else {
61       hints.put(DecodeHintType.POSSIBLE_FORMATS, decodeFormats);
62     }
63
64     if (characterSet != null) {
65       hints.put(DecodeHintType.CHARACTER_SET, characterSet);
66     }
67
68     hints.put(DecodeHintType.NEED_RESULT_POINT_CALLBACK, resultPointCallback);
69
70     this.handler = new DecodeHandler(activity, hints);
71   }
72
73   Handler getHandler() {
74     return handler;
75   }
76
77   @Override
78   public void run() {
79     Looper.prepare();
80     Looper.loop();
81   }
82
83 }