/ocr/translate/src/com/google/marvin/translate/TranslateActivity.java

http://eyes-free.googlecode.com/ · Java · 139 lines · 85 code · 25 blank · 29 comment · 5 complexity · ae3de669a5cae6aba4547b995ed65dab 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.translate;
  17. import android.app.Activity;
  18. import android.content.Intent;
  19. import android.os.Bundle;
  20. import android.util.Log;
  21. import android.view.KeyEvent;
  22. import android.view.View;
  23. import android.widget.Button;
  24. import android.widget.TextView;
  25. import com.google.marvin.translate.GoogleTranslate.TranslationListener;
  26. /**
  27. * Displays un-translated text and runs a Google Translate call in the
  28. * background. Closes automatically when translation finishes.
  29. *
  30. * @author alanv@google.com (Alan Viverette)
  31. */
  32. public class TranslateActivity extends Activity implements Button.OnClickListener {
  33. private static final String TAG = "TranslateActivity";
  34. public static final String EXTRA_RESULTS = "results";
  35. public static final String EXTRA_CODE = "code";
  36. public static final String EXTRA_ERROR = "error";
  37. public static final String EXTRA_SOURCE = "source";
  38. public static final String EXTRA_TARGET = "target";
  39. public static final String EXTRA_QUERY = "query";
  40. private TextView mTextView;
  41. private Button mCancel;
  42. @Override
  43. public void onCreate(Bundle savedInstanceState) {
  44. super.onCreate(savedInstanceState);
  45. setContentView(R.layout.translate);
  46. mTextView = (TextView) findViewById(R.id.translate);
  47. mCancel = (Button) findViewById(R.id.cancelTranslate);
  48. mCancel.setOnClickListener(this);
  49. processIntent(getIntent());
  50. }
  51. private void processIntent(Intent intent) {
  52. String query = intent.getStringExtra(EXTRA_QUERY);
  53. String source = intent.getStringExtra(EXTRA_SOURCE);
  54. String target = intent.getStringExtra(EXTRA_TARGET);
  55. Log.i(TAG, "Translating from " + source + " to " + target + "...");
  56. mTextView.append("Translating from " + source + " to " + target + "\n\n");
  57. mTextView.append(query);
  58. processResults(query, source, target);
  59. }
  60. private void processResults(String query, String source, String target) {
  61. TranslationListener onComplete = new TranslationListener() {
  62. @Override
  63. public void onComplete(String result) {
  64. TranslateActivity.this.onComplete(result);
  65. }
  66. @Override
  67. public void onError(int code, String error) {
  68. TranslateActivity.this.onError(code, error);
  69. }
  70. };
  71. GoogleTranslate translate = new GoogleTranslate(query, source, target, onComplete);
  72. translate.start();
  73. }
  74. /**
  75. * Since Google Translate returns HTML-encoded text, we have to convert some
  76. * character representations in the string back to characters. This isn't a
  77. * complete list, but it works for most text.
  78. *
  79. * @param input string with html-encoded characters
  80. * @return string with html-encoded characters decoded
  81. */
  82. private String fixHtml(String input) {
  83. input = input.replaceAll(">", ">");
  84. input = input.replaceAll("&lt;", "<");
  85. input = input.replaceAll("&quot;", "\"");
  86. input = input.replaceAll("&#39;", "'");
  87. input = input.replaceAll("&amp;", "&");
  88. return input;
  89. }
  90. public void onComplete(String result) {
  91. if (result == null) {
  92. Log.i(TAG, "Translation returned null results");
  93. setResult(RESULT_CANCELED);
  94. } else {
  95. result = fixHtml(result);
  96. Intent data = new Intent();
  97. data.putExtra(EXTRA_RESULTS, result);
  98. setResult(RESULT_OK, data);
  99. }
  100. finish();
  101. }
  102. public void onError(int code, String error) {
  103. Log.e(TAG, "Error " + code + ": " + error);
  104. setResult(RESULT_CANCELED);
  105. finish();
  106. }
  107. @Override
  108. public void onClick(View v) {
  109. KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK);
  110. if (v == mCancel) {
  111. onKeyDown(KeyEvent.KEYCODE_BACK, event);
  112. }
  113. }
  114. }