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.Menu;
30 import android.view.MenuItem;
31 import android.view.MenuItem.OnMenuItemClickListener;
32 import android.view.View;
33 import android.view.View.OnClickListener;
34 import android.view.WindowManager;
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 DictionaryInfo dictionaryInfo;
50 final Handler uiHandler = new Handler();
52 public static Intent getLaunchIntent(final String dictFile) {
53 final Intent intent = new Intent();
54 intent.setClassName(DictionaryEditActivity.class.getPackage().getName(),
55 DictionaryEditActivity.class.getName());
56 intent.putExtra(C.DICT_FILE, dictFile);
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();
69 final String dictFile = intent.getStringExtra(C.DICT_FILE);
71 PersistentObjectCache.init(this);
73 quickDicConfig = PersistentObjectCache.init(this).read(
74 C.DICTIONARY_CONFIGS, QuickDicConfig.class);
75 dictionaryInfo = quickDicConfig.getDictionaryInfoByFile(dictFile);
76 } catch (Exception e) {
77 Log.e(LOG, "Failed to read QuickDicConfig.", e);
79 startActivity(DictionaryManagerActivity.getLaunchIntent());
83 // Write stuff from object into fields.
85 ((EditText) findViewById(R.id.dictionaryName))
86 .setText(dictionaryInfo.name);
87 ((EditText) findViewById(R.id.localFile))
88 .setText(dictionaryInfo.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(dictionaryInfo.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.getLaunchIntent(dictFile, 0, "");
127 startActivity(intent);
131 // Don't show the keyboard when this opens up:
132 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
136 protected void onResume() {
141 // Focus the download button so the keyboard doesn't pop up.
142 final Button downloadButton = (Button) findViewById(R.id.downloadButton);
143 downloadButton.requestFocus();
147 protected void onPause() {
150 // Read stuff from fields into object.
151 dictionaryInfo.name = ((EditText) findViewById(R.id.dictionaryName))
152 .getText().toString();
153 dictionaryInfo.localFile = ((EditText) findViewById(R.id.localFile))
154 .getText().toString();
155 dictionaryInfo.downloadUrl = ((EditText) findViewById(R.id.downloadUrl))
156 .getText().toString();
158 PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS,
162 public boolean onCreateOptionsMenu(final Menu menu) {
163 final MenuItem newDictionaryMenuItem = menu
164 .add(R.string.downloadDictionary);
165 newDictionaryMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
166 public boolean onMenuItemClick(final MenuItem menuItem) {
167 startDownloadDictActivity(DictionaryEditActivity.this,
173 final MenuItem dictionaryList = menu.add(getString(R.string.dictionaryManager));
174 dictionaryList.setOnMenuItemClickListener(new OnMenuItemClickListener() {
175 public boolean onMenuItemClick(final MenuItem menuItem) {
176 startActivity(DictionaryManagerActivity.getLaunchIntent());
185 void updateDictInfo() {
186 final String downloadUrl = ((EditText) findViewById(R.id.downloadUrl))
187 .getText().toString();
188 final String localFile = ((EditText) findViewById(R.id.localFile))
189 .getText().toString();
191 final Button downloadButton = (Button) findViewById(R.id.downloadButton);
192 downloadButton.setEnabled(downloadUrl.length() > 0 && localFile.length() > 0);
194 final Button openButton = (Button) findViewById(R.id.openButton);
195 openButton.setEnabled(false);
197 final TextView dictInfo = (TextView) findViewById(R.id.dictionaryInfo);
198 if (!new File(localFile).canRead()) {
199 dictInfo.setText(getString(R.string.fileNotFound, localFile));
204 final RandomAccessFile raf = new RandomAccessFile(localFile, "r");
205 final Dictionary dict = new Dictionary(raf);
206 final StringBuilder builder = new StringBuilder();
207 builder.append(dict.dictInfo).append("\n");
209 getString(R.string.numPairEntries, dict.pairEntries.size())).append(
211 for (final Index index : dict.indices) {
212 builder.append("\n");
213 builder.append(index.longName).append("\n");
214 builder.append(" ").append(
215 getString(R.string.numTokens, index.sortedIndexEntries.size()))
217 builder.append(" ").append(
218 getString(R.string.numRows, index.rows.size())).append("\n");
221 dictInfo.setText(builder.toString());
222 openButton.setEnabled(true);
224 } catch (IOException e) {
225 dictInfo.setText(getString(R.string.invalidDictionary, localFile, e
230 static void startDownloadDictActivity(final Context context,
231 final DictionaryInfo dictionaryConfig) {
232 final Intent intent = new Intent(context, DownloadActivity.class);
233 intent.putExtra(DownloadActivity.SOURCE, dictionaryConfig.downloadUrl);
234 intent.putExtra(DownloadActivity.DEST, dictionaryConfig.localFile + ".zip");
235 context.startActivity(intent);