]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/DictionaryEditActivity.java
12d3ae2e8ab44ad3b17302d16ea323fe41bd038d
[Dictionary.git] / src / com / hughes / android / dictionary / DictionaryEditActivity.java
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
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
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 package com.hughes.android.dictionary;
16
17 import java.io.File;
18 import java.io.IOException;
19 import java.io.RandomAccessFile;
20
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;
38
39 import com.hughes.android.dictionary.engine.Dictionary;
40 import com.hughes.android.dictionary.engine.Index;
41 import com.hughes.android.util.PersistentObjectCache;
42
43 public class DictionaryEditActivity extends Activity {
44
45   static final String LOG = "QuickDic";
46
47   QuickDicConfig quickDicConfig;
48   private DictionaryConfig dictionaryConfig;
49   
50   final Handler uiHandler = new Handler();
51
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);
57     return intent;
58   }
59
60   /** Called when the activity is first created. */
61   @Override
62   public void onCreate(final Bundle savedInstanceState) {
63     ((DictionaryApplication)getApplication()).applyTheme(this);
64
65     super.onCreate(savedInstanceState);
66     setContentView(R.layout.edit_activity);
67
68     final Intent intent = getIntent();
69
70     final int dictIndex = intent.getIntExtra(C.DICT_INDEX, 0);
71       
72     PersistentObjectCache.init(this);
73     try {
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);
81     }
82     
83     // Write stuff from object into fields.
84
85     ((EditText) findViewById(R.id.dictionaryName))
86         .setText(dictionaryConfig.name);
87     ((EditText) findViewById(R.id.localFile))
88         .setText(dictionaryConfig.localFile);
89
90     final TextWatcher textWatcher = new TextWatcher() {
91       @Override
92       public void onTextChanged(CharSequence s, int start, int before,
93           int count) {
94       }
95
96       @Override
97       public void beforeTextChanged(CharSequence s, int start, int count,
98           int after) {
99       }
100
101       @Override
102       public void afterTextChanged(Editable s) {
103         updateDictInfo();
104       }
105     };
106
107     ((EditText) findViewById(R.id.localFile)).addTextChangedListener(textWatcher);
108
109     final EditText downloadUrl = (EditText) findViewById(R.id.downloadUrl);
110     downloadUrl.setText(dictionaryConfig.downloadUrl);
111     downloadUrl.addTextChangedListener(textWatcher);
112     
113     final Button downloadButton = (Button) findViewById(R.id.downloadButton);
114     downloadButton.setOnClickListener(new OnClickListener() {
115       @Override
116       public void onClick(View v) {
117         startDownloadDictActivity(DictionaryEditActivity.this,
118             dictionaryConfig);
119       }
120     });
121
122     final Button openButton = (Button) findViewById(R.id.openButton);
123     openButton.setOnClickListener(new OnClickListener() {
124       @Override
125       public void onClick(View v) {
126         final Intent intent = DictionaryActivity.getIntent(DictionaryEditActivity.this, dictIndex, 0, "");
127         startActivity(intent);
128       }
129     });
130
131   }
132   
133   protected void onResume() {
134     super.onResume();
135
136     updateDictInfo();
137
138     // Focus the download button so the keyboard doesn't pop up.
139     final Button downloadButton = (Button) findViewById(R.id.downloadButton);
140     downloadButton.requestFocus();
141   }
142
143   @Override
144   protected void onPause() {
145     super.onPause();
146
147     // Read stuff from fields into object.
148     dictionaryConfig.name = ((EditText) findViewById(R.id.dictionaryName))
149         .getText().toString();
150     dictionaryConfig.localFile = ((EditText) findViewById(R.id.localFile))
151         .getText().toString();
152     dictionaryConfig.downloadUrl = ((EditText) findViewById(R.id.downloadUrl))
153         .getText().toString();
154
155     PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS,
156         quickDicConfig);
157   }
158
159   public boolean onCreateOptionsMenu(final Menu menu) {
160     final MenuItem newDictionaryMenuItem = menu
161         .add(R.string.downloadDictionary);
162     newDictionaryMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
163           public boolean onMenuItemClick(final MenuItem menuItem) {
164             startDownloadDictActivity(DictionaryEditActivity.this,
165                 dictionaryConfig);
166             return false;
167           }
168         });
169     
170     final MenuItem dictionaryList = menu.add(getString(R.string.dictionaryList));
171     dictionaryList.setOnMenuItemClickListener(new OnMenuItemClickListener() {
172       public boolean onMenuItemClick(final MenuItem menuItem) {
173         startActivity(DictionaryListActivity.getIntent(DictionaryEditActivity.this));
174         return false;
175       }
176     });
177
178
179     return true;
180   }
181
182   void updateDictInfo() {
183     final String downloadUrl = ((EditText) findViewById(R.id.downloadUrl))
184         .getText().toString();
185     final String localFile = ((EditText) findViewById(R.id.localFile))
186         .getText().toString();
187
188     final Button downloadButton = (Button) findViewById(R.id.downloadButton);
189     downloadButton.setEnabled(downloadUrl.length() > 0 && localFile.length() > 0);
190
191     final Button openButton = (Button) findViewById(R.id.openButton);
192     openButton.setEnabled(false);
193
194     final TextView dictInfo = (TextView) findViewById(R.id.dictionaryInfo);
195     if (!new File(localFile).canRead()) {
196       dictInfo.setText(getString(R.string.fileNotFound, localFile));
197       return;
198     }
199
200     try {
201       final RandomAccessFile raf = new RandomAccessFile(localFile, "r");
202       final Dictionary dict = new Dictionary(raf);
203       final StringBuilder builder = new StringBuilder();
204       builder.append(dict.dictInfo).append("\n");
205       builder.append(
206           getString(R.string.numPairEntries, dict.pairEntries.size())).append(
207           "\n");
208       for (final Index index : dict.indices) {
209         builder.append("\n");
210         builder.append(index.longName).append("\n");
211         builder.append("  ").append(
212             getString(R.string.numTokens, index.sortedIndexEntries.size()))
213             .append("\n");
214         builder.append("  ").append(
215             getString(R.string.numRows, index.rows.size())).append("\n");
216       }
217       raf.close();
218       dictInfo.setText(builder.toString());
219       openButton.setEnabled(true);
220       
221     } catch (IOException e) {
222       dictInfo.setText(getString(R.string.invalidDictionary, localFile, e
223           .toString()));
224     }
225   }
226
227   static void startDownloadDictActivity(final Context context,
228       final DictionaryConfig dictionaryConfig) {
229     final Intent intent = new Intent(context, DownloadActivity.class);
230     intent.putExtra(DownloadActivity.SOURCE, dictionaryConfig.downloadUrl);
231     intent.putExtra(DownloadActivity.DEST, dictionaryConfig.localFile + ".zip");
232     context.startActivity(intent);
233   }
234   
235   @Override
236   public boolean onKeyDown(final int keyCode, final KeyEvent event) {
237     if (keyCode == KeyEvent.KEYCODE_BACK) {
238       Log.d(LOG, "Clearing dictionary prefs.");
239       DictionaryActivity.clearDictionaryPrefs(this);
240     }
241     return super.onKeyDown(keyCode, event);
242   }
243
244 }