+// 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.util.List;
+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 {
- List<DictionaryConfig> dictionaryConfigs;
- private DictionaryConfig dictionaryConfig;
+ static final String LOG = "QuickDic";
- public static final String DICT_INDEX = "dictIndex";
+ 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. */
- @SuppressWarnings("unchecked")
@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);
- dictionaryConfigs = (List<DictionaryConfig>) PersistentObjectCache.init(
- this).read(DictionaryListActivity.DICTIONARY_CONFIGS);
+ 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.
- final Intent intent = getIntent();
- dictionaryConfig = dictionaryConfigs.get(intent.getIntExtra(DICT_INDEX, 0));
+ ((EditText) findViewById(R.id.dictionaryName))
+ .setText(dictionaryInfo.name);
+ ((EditText) findViewById(R.id.localFile))
+ .setText(dictionaryInfo.localFile);
- // Write stuff from object into fields.
+ 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);
- ((EditText) findViewById(R.id.dictionaryName)).setText(dictionaryConfig.name);
- ((EditText) findViewById(R.id.localFile)).setText(dictionaryConfig.localFile);
- ((EditText) findViewById(R.id.wordListFile)).setText(dictionaryConfig.wordList);
- ((EditText) findViewById(R.id.downloadUrl)).setText(dictionaryConfig.downloadUrl);
+ 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.
- dictionaryConfig.name = ((EditText) findViewById(R.id.dictionaryName)).getText().toString();
- dictionaryConfig.localFile = ((EditText) findViewById(R.id.localFile)).getText().toString();
- dictionaryConfig.wordList = ((EditText) findViewById(R.id.wordListFile)).getText().toString();
- dictionaryConfig.downloadUrl = ((EditText) findViewById(R.id.downloadUrl)).getText().toString();
-
- PersistentObjectCache.getInstance().write(
- DictionaryListActivity.DICTIONARY_CONFIGS, dictionaryConfigs);
+ 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);
+ final MenuItem newDictionaryMenuItem = menu
+ .add(R.string.downloadDictionary);
newDictionaryMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
public boolean onMenuItemClick(final MenuItem menuItem) {
- startDownloadDictActivity(DictionaryEditActivity.this, dictionaryConfig);
+ 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;
}
- static void startDownloadDictActivity(final Context context, final DictionaryConfig dictionaryConfig) {
+ 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);
+ intent.putExtra(DownloadActivity.DEST, dictionaryConfig.localFile + ".zip");
context.startActivity(intent);
}
-
-
+
}