OCR Configurable  1.0
 Todo Clases Namespaces Archivos Funciones Variables
HelpActivity.java
Ir a la documentación de este archivo.
00001 /*
00002  * Copyright 2008 ZXing authors
00003  * Copyright 2011 Robert Theis
00004  * Copyright 2012 Jaime Navarro Santapau
00005  * 
00006  * Licensed under the Apache License, Version 2.0 (the "License");
00007  * you may not use this file except in compliance with the License.
00008  * You may obtain a copy of the License at
00009  *
00010  *      http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing, software
00013  * distributed under the License is distributed on an "AS IS" BASIS,
00014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  * See the License for the specific language governing permissions and
00016  * limitations under the License.
00017  */
00018 
00027 package edu.uoc.ocr;
00028 
00029 
00030 import java.util.Locale;
00031 
00032 import edu.uoc.ocr.R;
00033 import android.app.Activity;
00034 import android.app.AlertDialog;
00035 import android.content.ActivityNotFoundException;
00036 import android.content.DialogInterface;
00037 import android.content.Intent;
00038 import android.net.MailTo;
00039 import android.net.Uri;
00040 import android.os.Bundle;
00041 import android.util.Log;
00042 import android.view.KeyEvent;
00043 import android.view.View;
00044 import android.webkit.WebView;
00045 import android.webkit.WebViewClient;
00046 import android.widget.Button;
00047 
00053 public final class HelpActivity extends Activity {
00054 
00055         private static final String TAG = HelpActivity.class.getSimpleName();
00056 
00057         private static final String DEFAULT_LANGUAGE = "es";
00058 
00059         private static final String LANGUAGE;
00060 
00061         static {
00062                 Locale locale = Locale.getDefault();
00063                 String language = locale.getLanguage();
00064                 
00065                 if (language.contentEquals("es") 
00066                                 || language.contentEquals("en")
00067                                         || language.contentEquals("ca"))
00068                         LANGUAGE = language;
00069                 else
00070                         LANGUAGE = DEFAULT_LANGUAGE;
00071         }
00072         
00073         // Use this key and one of the values below when launching this activity via
00074         // intent. If not
00075         // present, the default page will be loaded.
00076         public static final String REQUESTED_PAGE_KEY = "requested_page_key";
00077         public static final String DEFAULT_PAGE = "whatsnew.html";
00078         public static final String ABOUT_PAGE = "about.html";
00079         public static final String WHATS_NEW_PAGE = "whatsnew.html";
00080 
00081         private static final String BASE_URL =
00082                       "file:///android_asset/html-" + LANGUAGE + '/';
00083         private static final String WEBVIEW_STATE_PRESENT = "webview_state_present";
00084 
00085         private WebView webView;
00086 
00087         private final Button.OnClickListener doneListener = new Button.OnClickListener() {
00088                 @Override
00089                 public void onClick(View view) {
00090                         finish();
00091                 }
00092         };
00093 
00094         @Override
00095         protected void onCreate(Bundle icicle) {
00096                 super.onCreate(icicle);
00097                 setContentView(R.layout.help);
00098 
00099                 webView = (WebView) findViewById(R.id.help_contents);
00100                 webView.setWebViewClient(new HelpClient(this));
00101 
00102                 Intent intent = getIntent();
00103                 String page = intent.getStringExtra(REQUESTED_PAGE_KEY);
00104 
00105                 // Show an OK button.
00106                 View doneButton = findViewById(R.id.done_button);
00107                 doneButton.setOnClickListener(doneListener);
00108 
00109                 if (page.equals(DEFAULT_PAGE)) {
00110                         doneButton.setVisibility(View.VISIBLE);
00111                 } else {
00112                         doneButton.setVisibility(View.GONE);
00113                 }
00114 
00115                 // Froyo has a bug with calling onCreate() twice in a row, which causes
00116                 // the What's New page
00117                 // that's auto-loaded on first run to appear blank. As a workaround we
00118                 // only call restoreState()
00119                 // if a valid URL was loaded at the time the previous activity was torn
00120                 // down.
00121                 if (icicle != null && icicle.getBoolean(WEBVIEW_STATE_PRESENT, false)) {
00122                         webView.restoreState(icicle);
00123                 } else if (intent != null && page != null && page.length() > 0) {
00124                         webView.loadUrl(BASE_URL + page);
00125                 } else {
00126                         webView.loadUrl(BASE_URL + DEFAULT_PAGE);
00127                 }
00128         }
00129 
00130         @Override
00131         protected void onSaveInstanceState(Bundle state) {
00132                 String url = webView.getUrl();
00133                 if (url != null && url.length() > 0) {
00134                         webView.saveState(state);
00135                         state.putBoolean(WEBVIEW_STATE_PRESENT, true);
00136                 }
00137         }
00138 
00139         @Override
00140         public boolean onKeyDown(int keyCode, KeyEvent event) {
00141                 if (keyCode == KeyEvent.KEYCODE_BACK) {
00142                         if (webView.canGoBack()) {
00143                                 webView.goBack();
00144                                 return true;
00145                         }
00146                 }
00147                 return super.onKeyDown(keyCode, event);
00148         }
00149 
00150         private final class HelpClient extends WebViewClient {
00151                 Activity context;
00152 
00153                 public HelpClient(Activity context) {
00154                         this.context = context;
00155                 }
00156 
00157                 @Override
00158                 public void onPageFinished(WebView view, String url) {
00159                         setTitle(view.getTitle());
00160                 }
00161 
00162                 @Override
00163                 public boolean shouldOverrideUrlLoading(WebView view, String url) {
00164                         if (url.startsWith("file")) {
00165                                 // Keep local assets in this WebView.
00166                                 return false;
00167                         } else if (url.startsWith("mailto:")) {
00168                                 try {
00169                                         MailTo mt = MailTo.parse(url);
00170                                         Intent i = new Intent(Intent.ACTION_SEND);
00171                                         i.setType("message/rfc822");
00172                                         i.putExtra(Intent.EXTRA_EMAIL, new String[] { mt.getTo() });
00173                                         i.putExtra(Intent.EXTRA_SUBJECT, mt.getSubject());
00174                                         context.startActivity(i);
00175                                         view.reload();
00176                                 } catch (ActivityNotFoundException e) {
00177                                         Log.w(TAG, "Problem with Intent.ACTION_SEND", e);
00178                                         new AlertDialog.Builder(context)
00179                                                         .setTitle("Contact Info")
00180                                                         .setMessage(
00181                                                                         "Please send your feedback to: app.ocr@gmail.com")
00182                                                         .setPositiveButton(getString(R.string.boton_done),
00183                                                                         new DialogInterface.OnClickListener() {
00184                                                                                 public void onClick(
00185                                                                                                 DialogInterface dialog,
00186                                                                                                 int which) {
00187                                                                                         Log.d("AlertDialog", "Positive");
00188                                                                                 }
00189                                                                         }).show();
00190                                 }
00191                                 return true;
00192                         } else {
00193                                 // Open external URLs in Browser.
00194                                 startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
00195                                 return true;
00196                         }
00197                 }
00198         }
00199 }
 Todo Clases Namespaces Archivos Funciones Variables