-// Copyright 2011 Google Inc. All Rights Reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package com.hughes.android.dictionary;
-
-import java.io.File;
-import java.io.IOException;
-import java.io.RandomAccessFile;
-
-import android.app.Activity;
-import android.content.Context;
-import android.content.Intent;
-import android.os.Bundle;
-import android.os.Handler;
-import android.text.Editable;
-import android.text.TextWatcher;
-import android.util.Log;
-import android.view.Menu;
-import android.view.MenuItem;
-import android.view.MenuItem.OnMenuItemClickListener;
-import android.view.View;
-import android.view.View.OnClickListener;
-import android.view.WindowManager;
-import android.widget.Button;
-import android.widget.EditText;
-import android.widget.TextView;
-
-import com.hughes.android.dictionary.engine.Dictionary;
-import com.hughes.android.dictionary.engine.Index;
-import com.hughes.android.util.PersistentObjectCache;
-
-public class DictionaryEditActivity extends Activity {
-
- static final String LOG = "QuickDic";
-
- QuickDicConfig quickDicConfig;
- private DictionaryInfo dictionaryInfo;
-
- final Handler uiHandler = new Handler();
-
- public static Intent getLaunchIntent(final String dictFile) {
- final Intent intent = new Intent();
- intent.setClassName(DictionaryEditActivity.class.getPackage().getName(),
- DictionaryEditActivity.class.getName());
- intent.putExtra(C.DICT_FILE, dictFile);
- return intent;
- }
-
- /** Called when the activity is first created. */
- @Override
- public void onCreate(final Bundle savedInstanceState) {
- //((DictionaryApplication)getApplication()).applyTheme(this);
-
- super.onCreate(savedInstanceState);
- setContentView(R.layout.edit_activity);
-
- final Intent intent = getIntent();
- final String dictFile = intent.getStringExtra(C.DICT_FILE);
-
- PersistentObjectCache.init(this);
- try {
- quickDicConfig = PersistentObjectCache.init(this).read(
- C.DICTIONARY_CONFIGS, QuickDicConfig.class);
- dictionaryInfo = quickDicConfig.getDictionaryInfoByFile(dictFile);
- } catch (Exception e) {
- Log.e(LOG, "Failed to read QuickDicConfig.", e);
- finish();
- startActivity(DictionaryManagerActivity.getLaunchIntent());
- return;
- }
-
- // Write stuff from object into fields.
-
- ((EditText) findViewById(R.id.dictionaryName))
- .setText(dictionaryInfo.name);
- ((EditText) findViewById(R.id.localFile))
- .setText(dictionaryInfo.localFile);
-
- final TextWatcher textWatcher = new TextWatcher() {
- @Override
- public void onTextChanged(CharSequence s, int start, int before,
- int count) {
- }
-
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count,
- int after) {
- }
-
- @Override
- public void afterTextChanged(Editable s) {
- updateDictInfo();
- }
- };
-
- ((EditText) findViewById(R.id.localFile)).addTextChangedListener(textWatcher);
-
- final EditText downloadUrl = (EditText) findViewById(R.id.downloadUrl);
- downloadUrl.setText(dictionaryInfo.downloadUrl);
- downloadUrl.addTextChangedListener(textWatcher);
-
- final Button downloadButton = (Button) findViewById(R.id.downloadButton);
- downloadButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- startDownloadDictActivity(DictionaryEditActivity.this,
- dictionaryInfo);
- }
- });
-
- final Button openButton = (Button) findViewById(R.id.openButton);
- openButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- final Intent intent = DictionaryActivity.getLaunchIntent(dictFile, 0, "");
- startActivity(intent);
- }
- });
-
- // Don't show the keyboard when this opens up:
- getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
-
- }
-
- protected void onResume() {
- super.onResume();
-
- updateDictInfo();
-
- // Focus the download button so the keyboard doesn't pop up.
- final Button downloadButton = (Button) findViewById(R.id.downloadButton);
- downloadButton.requestFocus();
- }
-
- @Override
- protected void onPause() {
- super.onPause();
-
- // Read stuff from fields into object.
- dictionaryInfo.name = ((EditText) findViewById(R.id.dictionaryName))
- .getText().toString();
- dictionaryInfo.localFile = ((EditText) findViewById(R.id.localFile))
- .getText().toString();
- dictionaryInfo.downloadUrl = ((EditText) findViewById(R.id.downloadUrl))
- .getText().toString();
-
- PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS,
- quickDicConfig);
- }
-
- public boolean onCreateOptionsMenu(final Menu menu) {
- final MenuItem newDictionaryMenuItem = menu
- .add(R.string.downloadDictionary);
- newDictionaryMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
- public boolean onMenuItemClick(final MenuItem menuItem) {
- startDownloadDictActivity(DictionaryEditActivity.this,
- dictionaryInfo);
- return false;
- }
- });
-
- final MenuItem dictionaryList = menu.add(getString(R.string.dictionaryManager));
- dictionaryList.setOnMenuItemClickListener(new OnMenuItemClickListener() {
- public boolean onMenuItemClick(final MenuItem menuItem) {
- startActivity(DictionaryManagerActivity.getLaunchIntent());
- return false;
- }
- });
-
-
- return true;
- }
-
- void updateDictInfo() {
- final String downloadUrl = ((EditText) findViewById(R.id.downloadUrl))
- .getText().toString();
- final String localFile = ((EditText) findViewById(R.id.localFile))
- .getText().toString();
-
- final Button downloadButton = (Button) findViewById(R.id.downloadButton);
- downloadButton.setEnabled(downloadUrl.length() > 0 && localFile.length() > 0);
-
- final Button openButton = (Button) findViewById(R.id.openButton);
- openButton.setEnabled(false);
-
- final TextView dictInfo = (TextView) findViewById(R.id.dictionaryInfo);
- if (!new File(localFile).canRead()) {
- dictInfo.setText(getString(R.string.fileNotFound, localFile));
- return;
- }
-
- try {
- final RandomAccessFile raf = new RandomAccessFile(localFile, "r");
- final Dictionary dict = new Dictionary(raf);
- final StringBuilder builder = new StringBuilder();
- builder.append(dict.dictInfo).append("\n");
- builder.append(
- getString(R.string.numPairEntries, dict.pairEntries.size())).append(
- "\n");
- for (final Index index : dict.indices) {
- builder.append("\n");
- builder.append(index.longName).append("\n");
- builder.append(" ").append(
- getString(R.string.numTokens, index.sortedIndexEntries.size()))
- .append("\n");
- builder.append(" ").append(
- getString(R.string.numRows, index.rows.size())).append("\n");
- }
- raf.close();
- dictInfo.setText(builder.toString());
- openButton.setEnabled(true);
-
- } catch (IOException e) {
- dictInfo.setText(getString(R.string.invalidDictionary, localFile, e
- .toString()));
- }
- }
-
- static void startDownloadDictActivity(final Context context,
- final DictionaryInfo dictionaryConfig) {
- final Intent intent = new Intent(context, DownloadActivity.class);
- intent.putExtra(DownloadActivity.SOURCE, dictionaryConfig.downloadUrl);
- intent.putExtra(DownloadActivity.DEST, dictionaryConfig.localFile + ".zip");
- context.startActivity(intent);
- }
-
-}