49530df8ac375f1492a29bec32741ecaff6ce021
[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.os.Bundle;
21 import android.view.View;
22 import android.view.KeyEvent;
23 import android.webkit.WebView;
24 import android.webkit.WebViewClient;
25 import android.widget.Button;
26
27 /**
28  * An HTML-based help screen with Back and Done buttons at the bottom.
29  *
30  * @author dswitkin@google.com (Daniel Switkin)
31  */
32 public final class HelpActivity extends Activity {
33   private static final String DEFAULT_URL = "file:///android_asset/html/index.html";
34
35   private WebView webView;
36   private Button backButton;
37
38   private final Button.OnClickListener backListener = new Button.OnClickListener() {
39     public void onClick(View view) {
40       webView.goBack();
41     }
42   };
43
44   private final Button.OnClickListener doneListener = new Button.OnClickListener() {
45     public void onClick(View view) {
46       finish();
47     }
48   };
49
50   @Override
51   protected void onCreate(Bundle icicle) {
52     super.onCreate(icicle);
53     setContentView(R.layout.help);
54
55     webView = (WebView)findViewById(R.id.help_contents);
56     webView.setWebViewClient(new HelpClient());
57     if (icicle != null) {
58       webView.restoreState(icicle);
59     } else {
60       webView.loadUrl(DEFAULT_URL);
61     }
62
63     backButton = (Button)findViewById(R.id.back_button);
64     backButton.setOnClickListener(backListener);
65
66     Button doneButton = (Button)findViewById(R.id.done_button);
67     doneButton.setOnClickListener(doneListener);
68   }
69
70   @Override
71   public void onResume() {
72     super.onResume();
73   }
74
75   @Override
76   protected void onSaveInstanceState(Bundle state) {
77     webView.saveState(state);
78   }
79
80   @Override
81   public boolean onKeyDown(int keyCode, KeyEvent event) {
82     if (keyCode == KeyEvent.KEYCODE_BACK) {
83       if (webView.canGoBack()) {
84         webView.goBack();
85         return true;
86       }
87     }
88     return super.onKeyDown(keyCode, event);
89   }
90
91   private final class HelpClient extends WebViewClient {
92     @Override
93     public void onPageFinished(WebView view, String url) {
94       setTitle(view.getTitle());
95       backButton.setEnabled(view.canGoBack());
96     }
97   }
98
99 }