]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/DictionaryEditActivity.java
499feb2465af5a70d15eab0cd9beac5d4360053e
[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.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;
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 DictionaryInfo dictionaryInfo;
49   
50   final Handler uiHandler = new Handler();
51
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);
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     final String dictFile = intent.getStringExtra(C.DICT_FILE);
70       
71     PersistentObjectCache.init(this);
72     try {
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);
78       finish();
79       startActivity(DictionaryManagerActivity.getLaunchIntent());
80       return;
81     }
82     
83     // Write stuff from object into fields.
84
85     ((EditText) findViewById(R.id.dictionaryName))
86         .setText(dictionaryInfo.name);
87     ((EditText) findViewById(R.id.localFile))
88         .setText(dictionaryInfo.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(dictionaryInfo.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             dictionaryInfo);
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.getLaunchIntent(dictFile, 0, "");
127         startActivity(intent);
128       }
129     });
130     
131     // Don't show the keyboard when this opens up:
132     getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
133
134   }
135   
136   protected void onResume() {
137     super.onResume();
138
139     updateDictInfo();
140
141     // Focus the download button so the keyboard doesn't pop up.
142     final Button downloadButton = (Button) findViewById(R.id.downloadButton);
143     downloadButton.requestFocus();
144   }
145
146   @Override
147   protected void onPause() {
148     super.onPause();
149
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();
157
158     PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS,
159         quickDicConfig);
160   }
161
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,
168                 dictionaryInfo);
169             return false;
170           }
171         });
172     
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());
177         return false;
178       }
179     });
180
181
182     return true;
183   }
184
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();
190
191     final Button downloadButton = (Button) findViewById(R.id.downloadButton);
192     downloadButton.setEnabled(downloadUrl.length() > 0 && localFile.length() > 0);
193
194     final Button openButton = (Button) findViewById(R.id.openButton);
195     openButton.setEnabled(false);
196
197     final TextView dictInfo = (TextView) findViewById(R.id.dictionaryInfo);
198     if (!new File(localFile).canRead()) {
199       dictInfo.setText(getString(R.string.fileNotFound, localFile));
200       return;
201     }
202
203     try {
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");
208       builder.append(
209           getString(R.string.numPairEntries, dict.pairEntries.size())).append(
210           "\n");
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()))
216             .append("\n");
217         builder.append("  ").append(
218             getString(R.string.numRows, index.rows.size())).append("\n");
219       }
220       raf.close();
221       dictInfo.setText(builder.toString());
222       openButton.setEnabled(true);
223       
224     } catch (IOException e) {
225       dictInfo.setText(getString(R.string.invalidDictionary, localFile, e
226           .toString()));
227     }
228   }
229
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);
236   }
237   
238 }