]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/DictionaryEditActivity.java
f1f206719c0c182f33637ab42d6ef85fcb86d1dd
[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
139   @Override
140   protected void onPause() {
141     super.onPause();
142
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();
150
151     PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS,
152         quickDicConfig);
153   }
154
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,
161                 dictionaryConfig);
162             return false;
163           }
164         });
165     
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));
170         return false;
171       }
172     });
173
174
175     return true;
176   }
177
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();
183
184     final Button downloadButton = (Button) findViewById(R.id.downloadButton);
185     downloadButton.setEnabled(downloadUrl.length() > 0 && localFile.length() > 0);
186
187     final Button openButton = (Button) findViewById(R.id.openButton);
188     openButton.setEnabled(false);
189
190     final TextView dictInfo = (TextView) findViewById(R.id.dictionaryInfo);
191     if (!new File(localFile).canRead()) {
192       dictInfo.setText(getString(R.string.fileNotFound, localFile));
193       return;
194     }
195
196     try {
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");
201       builder.append(
202           getString(R.string.numPairEntries, dict.pairEntries.size())).append(
203           "\n");
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()))
209             .append("\n");
210         builder.append("  ").append(
211             getString(R.string.numRows, index.rows.size())).append("\n");
212       }
213       raf.close();
214       dictInfo.setText(builder.toString());
215       openButton.setEnabled(true);
216       
217     } catch (IOException e) {
218       dictInfo.setText(getString(R.string.invalidDictionary, localFile, e
219           .toString()));
220     }
221   }
222
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);
229   }
230   
231   @Override
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);
236     }
237     return super.onKeyDown(keyCode, event);
238   }
239
240 }