]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/blob - BarCodeScanner/mobile/src/main/java/cz/cvut/fel/dce/barcodescanner/result/supplement/URIResultInfoRetriever.java
Initial commit
[hornmich/skoda-qr-demo.git] / BarCodeScanner / mobile / src / main / java / cz / cvut / fel / dce / barcodescanner / result / supplement / URIResultInfoRetriever.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 cz.cvut.fel.dce.barcodescanner.result.supplement;
18
19 import android.content.Context;
20 import android.widget.TextView;
21
22 import cz.cvut.fel.dce.barcodescanner.HttpHelper;
23 import cz.cvut.fel.dce.barcodescanner.R;
24 import cz.cvut.fel.dce.barcodescanner.history.HistoryManager;
25 import com.google.zxing.client.result.URIParsedResult;
26
27 import java.io.IOException;
28 import java.net.URI;
29 import java.net.URISyntaxException;
30
31 final class URIResultInfoRetriever extends SupplementalInfoRetriever {
32
33   private static final int MAX_REDIRECTS = 5;
34
35   private final URIParsedResult result;
36   private final String redirectString;
37
38   URIResultInfoRetriever(TextView textView, URIParsedResult result, HistoryManager historyManager, Context context) {
39     super(textView, historyManager);
40     redirectString = context.getString(R.string.msg_redirect);
41     this.result = result;
42   }
43
44   @Override
45   void retrieveSupplementalInfo() throws IOException {
46     URI oldURI;
47     try {
48       oldURI = new URI(result.getURI());
49     } catch (URISyntaxException ignored) {
50       return;
51     }
52     URI newURI = HttpHelper.unredirect(oldURI);
53     int count = 0;
54     while (count++ < MAX_REDIRECTS && !oldURI.equals(newURI)) {
55       append(result.getDisplayResult(), 
56              null, 
57              new String[] { redirectString + " : " + newURI }, 
58              newURI.toString());
59       oldURI = newURI;
60       newURI = HttpHelper.unredirect(newURI);
61     }
62   }
63
64 }