Add more unit tests for client.result, and more small code tweaks.
[zxing.git] / android / src / com / google / zxing / client / android / result / ResultHandlerFactory.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.result;
18
19 import android.app.Activity;
20 import com.google.zxing.Result;
21 import com.google.zxing.client.result.ParsedResult;
22 import com.google.zxing.client.result.ParsedResultType;
23 import com.google.zxing.client.result.ResultParser;
24
25 public final class ResultHandlerFactory {
26
27   private ResultHandlerFactory() {
28   }
29
30   public static ResultHandler makeResultHandler(Activity activity, Result rawResult) {
31     ParsedResult result = parseResult(rawResult);
32     ParsedResultType type = result.getType();
33     if (type.equals(ParsedResultType.ADDRESSBOOK)) {
34       return new AddressBookResultHandler(activity, result);
35     } else if (type.equals(ParsedResultType.EMAIL_ADDRESS)) {
36       return new EmailAddressResultHandler(activity, result);
37     } else if (type.equals(ParsedResultType.PRODUCT)) {
38       return new ProductResultHandler(activity, result);
39     } else if (type.equals(ParsedResultType.URI)) {
40       return new URIResultHandler(activity, result);
41     } else if (type.equals(ParsedResultType.TEXT)) {
42       return new TextResultHandler(activity, result);
43     } else if (type.equals(ParsedResultType.GEO)) {
44       return new GeoResultHandler(activity, result);
45     } else if (type.equals(ParsedResultType.TEL)) {
46       return new TelResultHandler(activity, result);
47     } else if (type.equals(ParsedResultType.SMS)) {
48       return new SMSResultHandler(activity, result);
49     } else if (type.equals(ParsedResultType.CALENDAR)) {
50       return new CalendarResultHandler(activity, result);
51     } else if (type.equals(ParsedResultType.ISBN)) {
52       return new ISBNResultHandler(activity, result);
53     } else {
54       // The TextResultHandler is the fallthrough for unsupported formats.
55       return new TextResultHandler(activity, result);
56     }
57   }
58
59   private static ParsedResult parseResult(Result rawResult) {
60     ParsedResult result = ResultParser.parseResult(rawResult);
61
62     // Disabled for now. To reactivate, create an AndroidIntentResultHandler.
63 //        if (result.getType().equals(ParsedResultType.TEXT)) {
64 //            String rawText = rawResult.getText();
65 //            AndroidIntentParsedResult androidResult = AndroidIntentParsedResult.parse(rawText);
66 //            if (androidResult != null) {
67 //                Intent intent = androidResult.getIntent();
68 //                if (!Intent.ACTION_VIEW.equals(intent.getAction())) {
69 //                    // For now, don't take anything that just parses as a View action. A lot
70 //                    // of things are accepted as a View action by default.
71 //                    result = androidResult;
72 //                }
73 //            }
74 //        }
75     return result;
76   }
77
78 }