/ocr/worldreader/src/com/google/marvin/worldreader/ReaderActivity.java

http://eyes-free.googlecode.com/ · Java · 170 lines · 125 code · 23 blank · 22 comment · 21 complexity · 891980d390b3e0f6779509ea79353de6 MD5 · raw file

  1. /*
  2. * Copyright (C) 2009 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * 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, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.google.marvin.worldreader;
  17. import android.app.Activity;
  18. import android.content.Intent;
  19. import android.os.Bundle;
  20. import android.os.Handler;
  21. import android.os.Message;
  22. import android.os.Parcelable;
  23. import android.speech.tts.TextToSpeech;
  24. import android.util.Log;
  25. import android.view.KeyEvent;
  26. import android.view.Window;
  27. import android.view.WindowManager;
  28. import android.widget.Toast;
  29. import com.android.ocr.client.Config;
  30. import com.android.ocr.client.Intents;
  31. /**
  32. * Main activity for WorldReader. Creates a TTS object and launches
  33. * CaptureActivity.
  34. *
  35. * @author alanv@google.com (Alan Viverette)
  36. */
  37. public class ReaderActivity extends Activity {
  38. private static final String TAG = "ReaderActivity";
  39. private static final int REQUEST_CAPTURE = 0;
  40. private static final int REQUEST_RECOGNIZE = 1;
  41. private static final int ACTION_INITIALIZED = 0;
  42. protected static TextToSpeech mTts;
  43. private final Handler mHandler = new Handler() {
  44. @Override
  45. public void handleMessage(Message message) {
  46. switch (message.what) {
  47. case ACTION_INITIALIZED: {
  48. requestCapture();
  49. break;
  50. }
  51. }
  52. }
  53. };
  54. @Override
  55. public void onCreate(Bundle savedInstanceState) {
  56. Log.i(TAG, "Creating ReaderActivity...");
  57. super.onCreate(savedInstanceState);
  58. Window window = getWindow();
  59. window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  60. TextToSpeech.OnInitListener onInit = new TextToSpeech.OnInitListener() {
  61. @Override
  62. public void onInit(int status) {
  63. Message msg = mHandler.obtainMessage(ACTION_INITIALIZED, status, 0);
  64. msg.sendToTarget();
  65. }
  66. };
  67. mTts = new TextToSpeech(this, onInit);
  68. }
  69. @Override
  70. protected void onDestroy() {
  71. if (mTts != null) {
  72. mTts.shutdown();
  73. }
  74. super.onDestroy();
  75. }
  76. @Override
  77. public boolean onKeyDown(int keyCode, KeyEvent event) {
  78. switch (keyCode) {
  79. case KeyEvent.KEYCODE_BACK: {
  80. requestCapture();
  81. return true;
  82. }
  83. }
  84. return super.onKeyDown(keyCode, event);
  85. }
  86. public void requestCapture() {
  87. Intent capture = new Intent(this, CaptureActivity.class);
  88. startActivityForResult(capture, REQUEST_CAPTURE);
  89. }
  90. public void requestRecognize(Intent data) {
  91. Parcelable extra = data.getParcelableExtra(Intents.Capture.CONFIG);
  92. if (!(extra instanceof Config)) {
  93. Log.e(TAG, "requestRecognize received wrong parcelable type (was " + extra + ")");
  94. return;
  95. }
  96. Config config = (Config) extra;
  97. config.pageSegMode = Config.PSM_AUTO;
  98. config.language = "eng";
  99. config.debug = false;
  100. if (config.image != null) {
  101. Intent recognize = new Intent(this, RecognizeActivity.class);
  102. recognize.putExtra(Intents.Recognize.CONFIG, config);
  103. startActivityForResult(recognize, REQUEST_RECOGNIZE);
  104. } else {
  105. Log.e(TAG, "requestRecognize received null image");
  106. }
  107. }
  108. public void handleCompleted(Intent data) {
  109. requestCapture();
  110. }
  111. @Override
  112. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  113. switch (requestCode) {
  114. case REQUEST_CAPTURE: {
  115. if (resultCode == RESULT_OK) {
  116. requestRecognize(data);
  117. } else if (resultCode == RESULT_CANCELED) {
  118. // Maintain the illusion that Capture is the main activity
  119. finish();
  120. } else {
  121. Toast.makeText(this, R.string.capture_failed, 5).show();
  122. Log.e(TAG, "REQUEST_CAPTURE received unexpected resultCode (" + resultCode + ")");
  123. finish();
  124. }
  125. break;
  126. }
  127. case REQUEST_RECOGNIZE: {
  128. if (resultCode == RESULT_OK) {
  129. handleCompleted(data);
  130. } else if (resultCode == RESULT_CANCELED) {
  131. Toast.makeText(this, R.string.recognize_canceled, 3).show();
  132. Log.i(TAG, "REQUEST_RECOGNIZED received RESULT_CANCELED");
  133. requestCapture();
  134. } else {
  135. Toast.makeText(this, R.string.recognize_failed, 5).show();
  136. Log.e(TAG, "REQUEST_RECOGNIZE received unexpected resultCode (" + resultCode + ")");
  137. requestCapture();
  138. }
  139. break;
  140. }
  141. default: {
  142. Log.i(TAG, "Received unknown activity request code (" + requestCode + ")");
  143. super.onActivityResult(requestCode, resultCode, data);
  144. }
  145. }
  146. }
  147. }