Issue 460, auto timeout of CaptureActivity after inactivity, for testing. Also break...
[zxing.git] / android / src / com / google / zxing / client / android / DecodeFormatManager.java
1 /*
2  * Copyright (C) 2010 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 java.util.Arrays;
20 import java.util.List;
21 import java.util.Vector;
22 import java.util.regex.Pattern;
23
24 import android.content.Intent;
25 import android.net.Uri;
26 import com.google.zxing.BarcodeFormat;
27
28 final class DecodeFormatManager {
29
30   private static final Pattern COMMA_PATTERN = Pattern.compile(",");
31
32   static final Vector<BarcodeFormat> PRODUCT_FORMATS;
33   static final Vector<BarcodeFormat> ONE_D_FORMATS;
34   static final Vector<BarcodeFormat> QR_CODE_FORMATS;
35   static final Vector<BarcodeFormat> ALL_FORMATS;
36   static {
37     PRODUCT_FORMATS = new Vector<BarcodeFormat>(5);
38     PRODUCT_FORMATS.add(BarcodeFormat.UPC_A);
39     PRODUCT_FORMATS.add(BarcodeFormat.UPC_E);
40     PRODUCT_FORMATS.add(BarcodeFormat.EAN_13);
41     PRODUCT_FORMATS.add(BarcodeFormat.EAN_8);
42     PRODUCT_FORMATS.add(BarcodeFormat.RSS14);
43     ONE_D_FORMATS = new Vector<BarcodeFormat>(PRODUCT_FORMATS.size() + 4);
44     ONE_D_FORMATS.addAll(PRODUCT_FORMATS);
45     ONE_D_FORMATS.add(BarcodeFormat.CODE_39);
46     ONE_D_FORMATS.add(BarcodeFormat.CODE_93);
47     ONE_D_FORMATS.add(BarcodeFormat.CODE_128);
48     ONE_D_FORMATS.add(BarcodeFormat.ITF);
49     QR_CODE_FORMATS = new Vector<BarcodeFormat>(1);
50     QR_CODE_FORMATS.add(BarcodeFormat.QR_CODE);
51     ALL_FORMATS = new Vector<BarcodeFormat>(ONE_D_FORMATS.size() + QR_CODE_FORMATS.size());
52     ALL_FORMATS.addAll(ONE_D_FORMATS);
53     ALL_FORMATS.addAll(QR_CODE_FORMATS);
54   }
55
56   private DecodeFormatManager() {}
57
58   static Vector<BarcodeFormat> parseDecodeFormats(Intent intent) {
59     List<String> scanFormats = null;
60     String scanFormatsString = intent.getStringExtra(Intents.Scan.SCAN_FORMATS);
61     if (scanFormatsString != null) {
62       scanFormats = Arrays.asList(COMMA_PATTERN.split(scanFormatsString));
63     }
64     return parseDecodeFormats(scanFormats, intent.getStringExtra(Intents.Scan.MODE));
65   }
66
67   static Vector<BarcodeFormat> parseDecodeFormats(Uri inputUri) {
68     List<String> formats = inputUri.getQueryParameters(Intents.Scan.SCAN_FORMATS);
69     if (formats != null && formats.size() == 1 && formats.get(0) != null){
70       formats = Arrays.asList(COMMA_PATTERN.split(formats.get(0)));
71     }
72     return parseDecodeFormats(formats, inputUri.getQueryParameter(Intents.Scan.MODE));
73   }
74
75   private static Vector<BarcodeFormat> parseDecodeFormats(Iterable<String> scanFormats,
76                                                           String decodeMode) {
77     if (scanFormats != null) {
78       Vector<BarcodeFormat> formats = new Vector<BarcodeFormat>();
79       try {
80         for (String format : scanFormats) {
81           formats.add(BarcodeFormat.valueOf(format));
82         }
83         return formats;
84       } catch (IllegalArgumentException iae) {
85         // ignore it then
86       }
87     }
88     if (decodeMode != null) {
89       if (Intents.Scan.PRODUCT_MODE.equals(decodeMode)) {
90         return PRODUCT_FORMATS;
91       }
92       if (Intents.Scan.QR_CODE_MODE.equals(decodeMode)) {
93         return QR_CODE_FORMATS;
94       }
95       if (Intents.Scan.ONE_D_MODE.equals(decodeMode)) {
96         return ONE_D_FORMATS;
97       }
98     }
99     return null;
100   }
101
102 }