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