]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/DictionaryEditActivity.java
UI changes, (experiments), Arabic rendering changes, changing the way
[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.view.WindowManager;
36 import android.widget.Button;
37 import android.widget.EditText;
38 import android.widget.TextView;
39
40 import com.hughes.android.dictionary.engine.Dictionary;
41 import com.hughes.android.dictionary.engine.Index;
42 import com.hughes.android.util.PersistentObjectCache;
43
44 public class DictionaryEditActivity extends Activity {
45
46   static final String LOG = "QuickDic";
47
48   QuickDicConfig quickDicConfig;
49   private DictionaryInfo dictionaryConfig;
50   
51   final Handler uiHandler = new Handler();
52
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);
58     return intent;
59   }
60
61   /** Called when the activity is first created. */
62   @Override
63   public void onCreate(final Bundle savedInstanceState) {
64     ((DictionaryApplication)getApplication()).applyTheme(this);
65
66     super.onCreate(savedInstanceState);
67     setContentView(R.layout.edit_activity);
68
69     final Intent intent = getIntent();
70
71     final int dictIndex = intent.getIntExtra(C.DICT_INDEX, 0);
72       
73     PersistentObjectCache.init(this);
74     try {
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);
82     }
83     
84     // Write stuff from object into fields.
85
86     ((EditText) findViewById(R.id.dictionaryName))
87         .setText(dictionaryConfig.name);
88     ((EditText) findViewById(R.id.localFile))
89         .setText(dictionaryConfig.localFile);
90
91     final TextWatcher textWatcher = new TextWatcher() {
92       @Override
93       public void onTextChanged(CharSequence s, int start, int before,
94           int count) {
95       }
96
97       @Override
98       public void beforeTextChanged(CharSequence s, int start, int count,
99           int after) {
100       }
101
102       @Override
103       public void afterTextChanged(Editable s) {
104         updateDictInfo();
105       }
106     };
107
108     ((EditText) findViewById(R.id.localFile)).addTextChangedListener(textWatcher);
109
110     final EditText downloadUrl = (EditText) findViewById(R.id.downloadUrl);
111     downloadUrl.setText(dictionaryConfig.downloadUrl);
112     downloadUrl.addTextChangedListener(textWatcher);
113     
114     final Button downloadButton = (Button) findViewById(R.id.downloadButton);
115     downloadButton.setOnClickListener(new OnClickListener() {
116       @Override
117       public void onClick(View v) {
118         startDownloadDictActivity(DictionaryEditActivity.this,
119             dictionaryConfig);
120       }
121     });
122
123     final Button openButton = (Button) findViewById(R.id.openButton);
124     openButton.setOnClickListener(new OnClickListener() {
125       @Override
126       public void onClick(View v) {
127         final Intent intent = DictionaryActivity.getIntent(DictionaryEditActivity.this, dictIndex, 0, "");
128         startActivity(intent);
129       }
130     });
131     
132     // Don't show the keyboard when this opens up:
133     getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
134
135   }
136   
137   protected void onResume() {
138     super.onResume();
139
140     updateDictInfo();
141
142     // Focus the download button so the keyboard doesn't pop up.
143     final Button downloadButton = (Button) findViewById(R.id.downloadButton);
144     downloadButton.requestFocus();
145   }
146
147   @Override
148   protected void onPause() {
149     super.onPause();
150
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();
158
159     PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS,
160         quickDicConfig);
161   }
162
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,
169                 dictionaryConfig);
170             return false;
171           }
172         });
173     
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));
178         return false;
179       }
180     });
181
182
183     return true;
184   }
185
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();
191
192     final Button downloadButton = (Button) findViewById(R.id.downloadButton);
193     downloadButton.setEnabled(downloadUrl.length() > 0 && localFile.length() > 0);
194
195     final Button openButton = (Button) findViewById(R.id.openButton);
196     openButton.setEnabled(false);
197
198     final TextView dictInfo = (TextView) findViewById(R.id.dictionaryInfo);
199     if (!new File(localFile).canRead()) {
200       dictInfo.setText(getString(R.string.fileNotFound, localFile));
201       return;
202     }
203
204     try {
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");
209       builder.append(
210           getString(R.string.numPairEntries, dict.pairEntries.size())).append(
211           "\n");
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()))
217             .append("\n");
218         builder.append("  ").append(
219             getString(R.string.numRows, index.rows.size())).append("\n");
220       }
221       raf.close();
222       dictInfo.setText(builder.toString());
223       openButton.setEnabled(true);
224       
225     } catch (IOException e) {
226       dictInfo.setText(getString(R.string.invalidDictionary, localFile, e
227           .toString()));
228     }
229   }
230
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);
237   }
238   
239 }