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