PageRenderTime 25ms CodeModel.GetById 12ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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