cf56fd85d520d9909651f14648229b0507b4b189
[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.getName();
41
42   private static final String[] BUGGY_MODEL_SUBSTRINGS = {"Behold II", "Pulse"};
43   private static final Uri BUGGY_URI = Uri.parse("http://code.google.com/p/zxing/wiki/FrequentlyAskedQuestions");
44   private static final String DEFAULT_URL = "file:///android_asset/html/index.html";
45
46   private WebView webView;
47   private Button backButton;
48
49   private final Button.OnClickListener backListener = new Button.OnClickListener() {
50     public void onClick(View view) {
51       webView.goBack();
52     }
53   };
54
55   private final Button.OnClickListener doneListener = new Button.OnClickListener() {
56     public void onClick(View view) {
57       finish();
58     }
59   };
60
61   private final DialogInterface.OnClickListener groupsListener = new DialogInterface.OnClickListener() {
62     public void onClick(DialogInterface dialogInterface, int i) {
63       Intent intent = new Intent(Intent.ACTION_VIEW, BUGGY_URI);
64       intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);              
65       HelpActivity.this.startActivity(intent);
66     }
67   };
68
69   @Override
70   protected void onCreate(Bundle icicle) {
71     super.onCreate(icicle);
72     setContentView(R.layout.help);
73
74     webView = (WebView)findViewById(R.id.help_contents);
75     webView.setWebViewClient(new HelpClient());
76     if (icicle != null) {
77       webView.restoreState(icicle);
78     } else {
79       webView.loadUrl(DEFAULT_URL);
80     }
81
82     backButton = (Button)findViewById(R.id.back_button);
83     backButton.setOnClickListener(backListener);
84
85     Button doneButton = (Button)findViewById(R.id.done_button);
86     doneButton.setOnClickListener(doneListener);
87   }
88
89   @Override
90   public void onResume() {
91     super.onResume();
92     checkBuggyDevice();
93   }
94
95   private void checkBuggyDevice() {
96     String model = Build.MODEL;
97     Log.i(TAG, "Build model is " + model);
98     if (model != null) {
99       for (String buggyModelSubstring : BUGGY_MODEL_SUBSTRINGS) {
100         if (model.contains(buggyModelSubstring)) {
101           AlertDialog.Builder builder = new AlertDialog.Builder(this);
102           builder.setMessage(R.string.msg_buggy);
103           builder.setPositiveButton(R.string.button_ok, groupsListener);
104           builder.setNegativeButton(R.string.button_cancel, null);
105           builder.create().show();
106           break;
107         }
108       }
109     }
110   }
111
112   @Override
113   protected void onSaveInstanceState(Bundle state) {
114     webView.saveState(state);
115   }
116
117   @Override
118   public boolean onKeyDown(int keyCode, KeyEvent event) {
119     if (keyCode == KeyEvent.KEYCODE_BACK) {
120       if (webView.canGoBack()) {
121         webView.goBack();
122         return true;
123       }
124     }
125     return super.onKeyDown(keyCode, event);
126   }
127
128   private final class HelpClient extends WebViewClient {
129     @Override
130     public void onPageFinished(WebView view, String url) {
131       setTitle(view.getTitle());
132       backButton.setEnabled(view.canGoBack());
133     }
134   }
135
136 }