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