Turn on Data Matrix, including separate scan option in Android
[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> DATA_MATRIX_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     DATA_MATRIX_FORMATS = new Vector<BarcodeFormat>(1);
52     DATA_MATRIX_FORMATS.add(BarcodeFormat.DATA_MATRIX);
53   }
54
55   private DecodeFormatManager() {}
56
57   static Vector<BarcodeFormat> parseDecodeFormats(Intent intent) {
58     List<String> scanFormats = null;
59     String scanFormatsString = intent.getStringExtra(Intents.Scan.SCAN_FORMATS);
60     if (scanFormatsString != null) {
61       scanFormats = Arrays.asList(COMMA_PATTERN.split(scanFormatsString));
62     }
63     return parseDecodeFormats(scanFormats, intent.getStringExtra(Intents.Scan.MODE));
64   }
65
66   static Vector<BarcodeFormat> parseDecodeFormats(Uri inputUri) {
67     List<String> formats = inputUri.getQueryParameters(Intents.Scan.SCAN_FORMATS);
68     if (formats != null && formats.size() == 1 && formats.get(0) != null){
69       formats = Arrays.asList(COMMA_PATTERN.split(formats.get(0)));
70     }
71     return parseDecodeFormats(formats, inputUri.getQueryParameter(Intents.Scan.MODE));
72   }
73
74   private static Vector<BarcodeFormat> parseDecodeFormats(Iterable<String> scanFormats,
75                                                           String decodeMode) {
76     if (scanFormats != null) {
77       Vector<BarcodeFormat> formats = new Vector<BarcodeFormat>();
78       try {
79         for (String format : scanFormats) {
80           formats.add(BarcodeFormat.valueOf(format));
81         }
82         return formats;
83       } catch (IllegalArgumentException iae) {
84         // ignore it then
85       }
86     }
87     if (decodeMode != null) {
88       if (Intents.Scan.PRODUCT_MODE.equals(decodeMode)) {
89         return PRODUCT_FORMATS;
90       }
91       if (Intents.Scan.QR_CODE_MODE.equals(decodeMode)) {
92         return QR_CODE_FORMATS;
93       }
94       if (Intents.Scan.DATA_MATRIX_MODE.equals(decodeMode)) {
95         return DATA_MATRIX_FORMATS;
96       }
97       if (Intents.Scan.ONE_D_MODE.equals(decodeMode)) {
98         return ONE_D_FORMATS;
99       }
100     }
101     return null;
102   }
103
104 }