Don't use Build.VERSION_CODES as it's not in 1.5
[zxing.git] / android / src / com / google / zxing / client / android / HelpActivity.java
1 /*
2  * Copyright 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 android.app.Activity;
20 import android.app.AlertDialog;
21 import android.content.DialogInterface;
22 import android.content.Intent;
23 import android.net.Uri;
24 import android.os.Build;
25 import android.os.Bundle;
26 import android.util.Log;
27 import android.view.View;
28 import android.view.KeyEvent;
29 import android.webkit.WebView;
30 import android.webkit.WebViewClient;
31 import android.widget.Button;
32
33 /**
34  * An HTML-based help screen with Back and Done buttons at the bottom.
35  *
36  * @author dswitkin@google.com (Daniel Switkin)
37  */
38 public final class HelpActivity extends Activity {
39
40   private static final String TAG = HelpActivity.class.getSimpleName();
41
42   // Actually guessing at the Desire's MODEL for now:
43   private static final String[] BUGGY_MODEL_SUBSTRINGS = {
44       "Desire",
45       "Pulse", // Camera doesn't come on
46       "Geeksphone", // Doesn't support YUV?
47       "supersonic", // aka Evo
48   };
49   private static final Uri BUGGY_URI =
50       Uri.parse("http://code.google.com/p/zxing/wiki/FrequentlyAskedQuestions");
51
52   // Use this key and one of the values below when launching this activity via intent. If not
53   // present, the default page will be loaded.
54   public static final String REQUESTED_PAGE_KEY = "requested_page_key";
55   public static final String DEFAULT_PAGE = "index.html";
56   public static final String WHATS_NEW_PAGE = "whatsnew.html";
57
58   private static final String BASE_URL = "file:///android_asset/html/";
59   private static final String WEBVIEW_STATE_PRESENT = "webview_state_present";
60
61   private static boolean initialized = false;
62   private WebView webView;
63   private Button backButton;
64
65   private final Button.OnClickListener backListener = new Button.OnClickListener() {
66     public void onClick(View view) {
67       webView.goBack();
68     }
69   };
70
71   private final Button.OnClickListener doneListener = new Button.OnClickListener() {
72     public void onClick(View view) {
73       finish();
74     }
75   };
76
77   private final DialogInterface.OnClickListener groupsListener =
78       new DialogInterface.OnClickListener() {
79     public void onClick(DialogInterface dialogInterface, int i) {
80       Intent intent = new Intent(Intent.ACTION_VIEW, BUGGY_URI);
81       intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
82       HelpActivity.this.startActivity(intent);
83     }
84   };
85
86   @Override
87   protected void onCreate(Bundle icicle) {
88     super.onCreate(icicle);
89     setContentView(R.layout.help);
90
91     webView = (WebView)findViewById(R.id.help_contents);
92     webView.setWebViewClient(new HelpClient());
93
94     // Froyo has a bug with calling onCreate() twice in a row, which causes the What's New page
95     // that's auto-loaded on first run to appear blank. As a workaround we only call restoreState()
96     // if a valid URL was loaded at the time the previous activity was torn down.
97     Intent intent = getIntent();
98     if (icicle != null && icicle.getBoolean(WEBVIEW_STATE_PRESENT, false)) {
99       webView.restoreState(icicle);
100     } else if (intent != null) {
101       String page = intent.getStringExtra(REQUESTED_PAGE_KEY);
102       if (page != null && page.length() > 0) {
103         webView.loadUrl(BASE_URL + page);
104       } else {
105         webView.loadUrl(BASE_URL + DEFAULT_PAGE);
106       }
107     } else {
108       webView.loadUrl(BASE_URL + DEFAULT_PAGE);
109     }
110
111     backButton = (Button) findViewById(R.id.back_button);
112     backButton.setOnClickListener(backListener);
113     View doneButton = findViewById(R.id.done_button);
114     doneButton.setOnClickListener(doneListener);
115
116     if (!initialized) {
117       initialized = true;
118       checkBuggyDevice();
119     }
120   }
121
122   private void checkBuggyDevice() {
123     String model = Build.MODEL;
124     Log.i(TAG, "Build model is " + model);
125     if (model != null) {
126       for (String buggyModelSubstring : BUGGY_MODEL_SUBSTRINGS) {
127         if (model.contains(buggyModelSubstring)) {
128           AlertDialog.Builder builder = new AlertDialog.Builder(this);
129           builder.setMessage(R.string.msg_buggy);
130           builder.setPositiveButton(R.string.button_ok, groupsListener);
131           builder.setNegativeButton(R.string.button_cancel, null);
132           builder.show();
133           break;
134         }
135       }
136     }
137   }
138
139   @Override
140   protected void onSaveInstanceState(Bundle state) {
141     String url = webView.getUrl();
142     if (url != null && url.length() > 0) {
143       webView.saveState(state);
144       state.putBoolean(WEBVIEW_STATE_PRESENT, true);
145     }
146   }
147
148   @Override
149   public boolean onKeyDown(int keyCode, KeyEvent event) {
150     if (keyCode == KeyEvent.KEYCODE_BACK) {
151       if (webView.canGoBack()) {
152         webView.goBack();
153         return true;
154       }
155     }
156     return super.onKeyDown(keyCode, event);
157   }
158
159   private final class HelpClient extends WebViewClient {
160     @Override
161     public void onPageFinished(WebView view, String url) {
162       setTitle(view.getTitle());
163       backButton.setEnabled(view.canGoBack());
164     }
165
166     @Override
167     public boolean shouldOverrideUrlLoading(WebView view, String url) {
168       if (url.startsWith("file")) {
169         // Keep local assets in this WebView.
170         return false;
171       } else {
172         // Open external URLs in Browser.
173         startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
174         return true;
175       }
176     }
177   }
178 }