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