1 // Copyright 2011 Google Inc. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 package com.hughes.android.dictionary;
18 import java.io.IOException;
19 import java.io.RandomAccessFile;
21 import android.app.Activity;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.os.Bundle;
25 import android.os.Handler;
26 import android.text.Editable;
27 import android.text.TextWatcher;
28 import android.util.Log;
29 import android.view.KeyEvent;
30 import android.view.Menu;
31 import android.view.MenuItem;
32 import android.view.View;
33 import android.view.MenuItem.OnMenuItemClickListener;
34 import android.view.View.OnClickListener;
35 import android.widget.Button;
36 import android.widget.EditText;
37 import android.widget.TextView;
39 import com.hughes.android.dictionary.engine.Dictionary;
40 import com.hughes.android.dictionary.engine.Index;
41 import com.hughes.android.util.PersistentObjectCache;
43 public class DictionaryEditActivity extends Activity {
45 static final String LOG = "QuickDic";
47 QuickDicConfig quickDicConfig;
48 private DictionaryConfig dictionaryConfig;
50 final Handler uiHandler = new Handler();
52 public static Intent getIntent(final int dictIndex) {
53 final Intent intent = new Intent();
54 intent.setClassName(DictionaryEditActivity.class.getPackage().getName(),
55 DictionaryEditActivity.class.getName());
56 intent.putExtra(C.DICT_INDEX, dictIndex);
60 /** Called when the activity is first created. */
62 public void onCreate(final Bundle savedInstanceState) {
63 ((DictionaryApplication)getApplication()).applyTheme(this);
65 super.onCreate(savedInstanceState);
66 setContentView(R.layout.edit_activity);
68 final Intent intent = getIntent();
70 final int dictIndex = intent.getIntExtra(C.DICT_INDEX, 0);
72 PersistentObjectCache.init(this);
74 quickDicConfig = PersistentObjectCache.init(this).read(
75 C.DICTIONARY_CONFIGS, QuickDicConfig.class);
76 dictionaryConfig = quickDicConfig.dictionaryConfigs.get(dictIndex);
77 } catch (Exception e) {
78 Log.e(LOG, "Failed to read QuickDicConfig.", e);
79 quickDicConfig = new QuickDicConfig();
80 dictionaryConfig = quickDicConfig.dictionaryConfigs.get(0);
83 // Write stuff from object into fields.
85 ((EditText) findViewById(R.id.dictionaryName))
86 .setText(dictionaryConfig.name);
87 ((EditText) findViewById(R.id.localFile))
88 .setText(dictionaryConfig.localFile);
90 final TextWatcher textWatcher = new TextWatcher() {
92 public void onTextChanged(CharSequence s, int start, int before,
97 public void beforeTextChanged(CharSequence s, int start, int count,
102 public void afterTextChanged(Editable s) {
107 ((EditText) findViewById(R.id.localFile)).addTextChangedListener(textWatcher);
109 final EditText downloadUrl = (EditText) findViewById(R.id.downloadUrl);
110 downloadUrl.setText(dictionaryConfig.downloadUrl);
111 downloadUrl.addTextChangedListener(textWatcher);
113 final Button downloadButton = (Button) findViewById(R.id.downloadButton);
114 downloadButton.setOnClickListener(new OnClickListener() {
116 public void onClick(View v) {
117 startDownloadDictActivity(DictionaryEditActivity.this,
122 final Button openButton = (Button) findViewById(R.id.openButton);
123 openButton.setOnClickListener(new OnClickListener() {
125 public void onClick(View v) {
126 final Intent intent = DictionaryActivity.getIntent(DictionaryEditActivity.this, dictIndex, 0, "");
127 startActivity(intent);
133 protected void onResume() {
140 protected void onPause() {
143 // Read stuff from fields into object.
144 dictionaryConfig.name = ((EditText) findViewById(R.id.dictionaryName))
145 .getText().toString();
146 dictionaryConfig.localFile = ((EditText) findViewById(R.id.localFile))
147 .getText().toString();
148 dictionaryConfig.downloadUrl = ((EditText) findViewById(R.id.downloadUrl))
149 .getText().toString();
151 PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS,
155 public boolean onCreateOptionsMenu(final Menu menu) {
156 final MenuItem newDictionaryMenuItem = menu
157 .add(R.string.downloadDictionary);
158 newDictionaryMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
159 public boolean onMenuItemClick(final MenuItem menuItem) {
160 startDownloadDictActivity(DictionaryEditActivity.this,
166 final MenuItem dictionaryList = menu.add(getString(R.string.dictionaryList));
167 dictionaryList.setOnMenuItemClickListener(new OnMenuItemClickListener() {
168 public boolean onMenuItemClick(final MenuItem menuItem) {
169 startActivity(DictionaryListActivity.getIntent(DictionaryEditActivity.this));
178 void updateDictInfo() {
179 final String downloadUrl = ((EditText) findViewById(R.id.downloadUrl))
180 .getText().toString();
181 final String localFile = ((EditText) findViewById(R.id.localFile))
182 .getText().toString();
184 final Button downloadButton = (Button) findViewById(R.id.downloadButton);
185 downloadButton.setEnabled(downloadUrl.length() > 0 && localFile.length() > 0);
187 final Button openButton = (Button) findViewById(R.id.openButton);
188 openButton.setEnabled(false);
190 final TextView dictInfo = (TextView) findViewById(R.id.dictionaryInfo);
191 if (!new File(localFile).canRead()) {
192 dictInfo.setText(getString(R.string.fileNotFound, localFile));
197 final RandomAccessFile raf = new RandomAccessFile(localFile, "r");
198 final Dictionary dict = new Dictionary(raf);
199 final StringBuilder builder = new StringBuilder();
200 builder.append(dict.dictInfo).append("\n");
202 getString(R.string.numPairEntries, dict.pairEntries.size())).append(
204 for (final Index index : dict.indices) {
205 builder.append("\n");
206 builder.append(index.longName).append("\n");
207 builder.append(" ").append(
208 getString(R.string.numTokens, index.sortedIndexEntries.size()))
210 builder.append(" ").append(
211 getString(R.string.numRows, index.rows.size())).append("\n");
214 dictInfo.setText(builder.toString());
215 openButton.setEnabled(true);
217 } catch (IOException e) {
218 dictInfo.setText(getString(R.string.invalidDictionary, localFile, e
223 static void startDownloadDictActivity(final Context context,
224 final DictionaryConfig dictionaryConfig) {
225 final Intent intent = new Intent(context, DownloadActivity.class);
226 intent.putExtra(DownloadActivity.SOURCE, dictionaryConfig.downloadUrl);
227 intent.putExtra(DownloadActivity.DEST, dictionaryConfig.localFile + ".zip");
228 context.startActivity(intent);
232 public boolean onKeyDown(final int keyCode, final KeyEvent event) {
233 if (keyCode == KeyEvent.KEYCODE_BACK) {
234 Log.d(LOG, "Clearing dictionary prefs.");
235 DictionaryActivity.clearDictionaryPrefs(this);
237 return super.onKeyDown(keyCode, event);