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.view.WindowManager;
36 import android.widget.Button;
37 import android.widget.EditText;
38 import android.widget.TextView;
40 import com.hughes.android.dictionary.engine.Dictionary;
41 import com.hughes.android.dictionary.engine.Index;
42 import com.hughes.android.util.PersistentObjectCache;
44 public class DictionaryEditActivity extends Activity {
46 static final String LOG = "QuickDic";
48 QuickDicConfig quickDicConfig;
49 private DictionaryInfo dictionaryConfig;
51 final Handler uiHandler = new Handler();
53 public static Intent getIntent(final int dictIndex) {
54 final Intent intent = new Intent();
55 intent.setClassName(DictionaryEditActivity.class.getPackage().getName(),
56 DictionaryEditActivity.class.getName());
57 intent.putExtra(C.DICT_INDEX, dictIndex);
61 /** Called when the activity is first created. */
63 public void onCreate(final Bundle savedInstanceState) {
64 ((DictionaryApplication)getApplication()).applyTheme(this);
66 super.onCreate(savedInstanceState);
67 setContentView(R.layout.edit_activity);
69 final Intent intent = getIntent();
71 final int dictIndex = intent.getIntExtra(C.DICT_INDEX, 0);
73 PersistentObjectCache.init(this);
75 quickDicConfig = PersistentObjectCache.init(this).read(
76 C.DICTIONARY_CONFIGS, QuickDicConfig.class);
77 dictionaryConfig = quickDicConfig.dictionaryConfigs.get(dictIndex);
78 } catch (Exception e) {
79 Log.e(LOG, "Failed to read QuickDicConfig.", e);
80 quickDicConfig = new QuickDicConfig();
81 dictionaryConfig = quickDicConfig.dictionaryConfigs.get(0);
84 // Write stuff from object into fields.
86 ((EditText) findViewById(R.id.dictionaryName))
87 .setText(dictionaryConfig.name);
88 ((EditText) findViewById(R.id.localFile))
89 .setText(dictionaryConfig.localFile);
91 final TextWatcher textWatcher = new TextWatcher() {
93 public void onTextChanged(CharSequence s, int start, int before,
98 public void beforeTextChanged(CharSequence s, int start, int count,
103 public void afterTextChanged(Editable s) {
108 ((EditText) findViewById(R.id.localFile)).addTextChangedListener(textWatcher);
110 final EditText downloadUrl = (EditText) findViewById(R.id.downloadUrl);
111 downloadUrl.setText(dictionaryConfig.downloadUrl);
112 downloadUrl.addTextChangedListener(textWatcher);
114 final Button downloadButton = (Button) findViewById(R.id.downloadButton);
115 downloadButton.setOnClickListener(new OnClickListener() {
117 public void onClick(View v) {
118 startDownloadDictActivity(DictionaryEditActivity.this,
123 final Button openButton = (Button) findViewById(R.id.openButton);
124 openButton.setOnClickListener(new OnClickListener() {
126 public void onClick(View v) {
127 final Intent intent = DictionaryActivity.getIntent(DictionaryEditActivity.this, dictIndex, 0, "");
128 startActivity(intent);
132 // Don't show the keyboard when this opens up:
133 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
137 protected void onResume() {
142 // Focus the download button so the keyboard doesn't pop up.
143 final Button downloadButton = (Button) findViewById(R.id.downloadButton);
144 downloadButton.requestFocus();
148 protected void onPause() {
151 // Read stuff from fields into object.
152 dictionaryConfig.name = ((EditText) findViewById(R.id.dictionaryName))
153 .getText().toString();
154 dictionaryConfig.localFile = ((EditText) findViewById(R.id.localFile))
155 .getText().toString();
156 dictionaryConfig.downloadUrl = ((EditText) findViewById(R.id.downloadUrl))
157 .getText().toString();
159 PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS,
163 public boolean onCreateOptionsMenu(final Menu menu) {
164 final MenuItem newDictionaryMenuItem = menu
165 .add(R.string.downloadDictionary);
166 newDictionaryMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
167 public boolean onMenuItemClick(final MenuItem menuItem) {
168 startDownloadDictActivity(DictionaryEditActivity.this,
174 final MenuItem dictionaryList = menu.add(getString(R.string.dictionaryList));
175 dictionaryList.setOnMenuItemClickListener(new OnMenuItemClickListener() {
176 public boolean onMenuItemClick(final MenuItem menuItem) {
177 startActivity(DictionaryManagerActivity.getIntent(DictionaryEditActivity.this));
186 void updateDictInfo() {
187 final String downloadUrl = ((EditText) findViewById(R.id.downloadUrl))
188 .getText().toString();
189 final String localFile = ((EditText) findViewById(R.id.localFile))
190 .getText().toString();
192 final Button downloadButton = (Button) findViewById(R.id.downloadButton);
193 downloadButton.setEnabled(downloadUrl.length() > 0 && localFile.length() > 0);
195 final Button openButton = (Button) findViewById(R.id.openButton);
196 openButton.setEnabled(false);
198 final TextView dictInfo = (TextView) findViewById(R.id.dictionaryInfo);
199 if (!new File(localFile).canRead()) {
200 dictInfo.setText(getString(R.string.fileNotFound, localFile));
205 final RandomAccessFile raf = new RandomAccessFile(localFile, "r");
206 final Dictionary dict = new Dictionary(raf);
207 final StringBuilder builder = new StringBuilder();
208 builder.append(dict.dictInfo).append("\n");
210 getString(R.string.numPairEntries, dict.pairEntries.size())).append(
212 for (final Index index : dict.indices) {
213 builder.append("\n");
214 builder.append(index.longName).append("\n");
215 builder.append(" ").append(
216 getString(R.string.numTokens, index.sortedIndexEntries.size()))
218 builder.append(" ").append(
219 getString(R.string.numRows, index.rows.size())).append("\n");
222 dictInfo.setText(builder.toString());
223 openButton.setEnabled(true);
225 } catch (IOException e) {
226 dictInfo.setText(getString(R.string.invalidDictionary, localFile, e
231 static void startDownloadDictActivity(final Context context,
232 final DictionaryInfo dictionaryConfig) {
233 final Intent intent = new Intent(context, DownloadActivity.class);
234 intent.putExtra(DownloadActivity.SOURCE, dictionaryConfig.downloadUrl);
235 intent.putExtra(DownloadActivity.DEST, dictionaryConfig.localFile + ".zip");
236 context.startActivity(intent);