- public View getView(final int position, final View convertView, final ViewGroup parent) {
- final DictionaryInfo dictionaryInfo = getItem(position);
- final LinearLayout result = new LinearLayout(parent.getContext());
- result.setOrientation(LinearLayout.VERTICAL);
-
- final LinearLayout row = new LinearLayout(parent.getContext());
- row.setOrientation(LinearLayout.HORIZONTAL);
- result.addView(row);
-
- {
- final TextView textView = new TextView(parent.getContext());
- final String name = application.getDictionaryName(dictionaryInfo.uncompressedFilename);
- textView.setText(name);
- textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
- row.addView(textView);
- LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
- layoutParams.weight = 1.0f;
- textView.setLayoutParams(layoutParams);
- }
-
- final boolean updateAvailable = application.updateAvailable(dictionaryInfo);
- final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
- if ((!application.isDictionaryOnDevice(dictionaryInfo.uncompressedFilename) || updateAvailable) && downloadable != null) {
- final Button downloadButton = new Button(parent.getContext());
- downloadButton.setText(getString(updateAvailable ? R.string.updateButton : R.string.downloadButton, downloadable.zipBytes / 1024.0 / 1024.0));
- downloadButton.setOnClickListener(new IntentLauncher(parent.getContext(), DownloadActivity
- .getLaunchIntent(downloadable.downloadUrl,
- application.getPath(dictionaryInfo.uncompressedFilename).getPath() + ".zip",
- dictionaryInfo.dictInfo)) {
- @Override
- protected void onGo() {
- application.invalidateDictionaryInfo(dictionaryInfo.uncompressedFilename);
- }
- });
- WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
- layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
- layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
- downloadButton.setLayoutParams(layoutParams);
- row.addView(downloadButton);
- } else {
- final ImageView checkMark = new ImageView(parent.getContext());
- checkMark.setImageResource(R.drawable.btn_check_buttonless_on);
- row.addView(checkMark);
- }
-
- // Add the information about each index.
- final LinearLayout row2 = new LinearLayout(parent.getContext());
- row2.setOrientation(LinearLayout.HORIZONTAL);
- final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
- row2.setLayoutParams(layoutParams);
- result.addView(row2);
- final StringBuilder builder = new StringBuilder();
- for (final IndexInfo indexInfo : dictionaryInfo.indexInfos) {
- if (builder.length() > 0) {
- builder.append(" | ");
+ protected void onResume() {
+ super.onResume();
+
+ if (PreferenceActivity.prefsMightHaveChanged) {
+ PreferenceActivity.prefsMightHaveChanged = false;
+ finish();
+ startActivity(getIntent());
+ }
+
+ final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
+ showDownloadable.setChecked(prefs.getBoolean(C.SHOW_DOWNLOADABLE, true));
+
+ if (!blockAutoLaunch &&
+ getIntent().getBooleanExtra(C.CAN_AUTO_LAUNCH_DICT, true) &&
+ prefs.contains(C.DICT_FILE) &&
+ prefs.contains(C.INDEX_SHORT_NAME)) {
+ Log.d(LOG, "Skipping DictionaryManager, going straight to dictionary.");
+ startActivity(DictionaryActivity.getLaunchIntent(getApplicationContext(),
+ new File(prefs.getString(C.DICT_FILE, "")),
+ prefs.getString(C.INDEX_SHORT_NAME, ""),
+ prefs.getString(C.SEARCH_TOKEN, "")));
+ finish();
+ return;
+ }
+
+ // Remove the active dictionary from the prefs so we won't autolaunch
+ // next time.
+ final Editor editor = prefs.edit();
+ editor.remove(C.DICT_FILE);
+ editor.remove(C.INDEX_SHORT_NAME);
+ editor.remove(C.SEARCH_TOKEN);
+ editor.commit();
+
+ application.backgroundUpdateDictionaries(dictionaryUpdater);
+
+ setMyListAdapater();
+ }
+
+ @Override
+ public boolean onCreateOptionsMenu(final Menu menu) {
+ application.onCreateGlobalOptionsMenu(this, menu);
+ return true;
+ }
+
+ @Override
+ public void onCreateContextMenu(final ContextMenu menu, final View view,
+ final ContextMenuInfo menuInfo) {
+ super.onCreateContextMenu(menu, view, menuInfo);
+ Log.d(LOG, "onCreateContextMenu, " + menuInfo);
+
+ final AdapterContextMenuInfo adapterContextMenuInfo =
+ (AdapterContextMenuInfo) menuInfo;
+ final int position = adapterContextMenuInfo.position;
+ final MyListAdapter.Row row = (MyListAdapter.Row) getListAdapter().getItem(position);
+
+ if (row.dictionaryInfo == null) {
+ return;
+ }
+
+ if (position > 0 && row.onDevice) {
+ final android.view.MenuItem moveToTopMenuItem =
+ menu.add(R.string.moveToTop);
+ moveToTopMenuItem.setOnMenuItemClickListener(new
+ android.view.MenuItem.OnMenuItemClickListener() {
+ @Override
+ public boolean onMenuItemClick(android.view.MenuItem item) {
+ application.moveDictionaryToTop(row.dictionaryInfo);
+ setMyListAdapater();
+ return true;
+ }
+ });
+ }
+
+ if (row.onDevice) {
+ final android.view.MenuItem deleteMenuItem = menu.add(R.string.deleteDictionary);
+ deleteMenuItem
+ .setOnMenuItemClickListener(new android.view.MenuItem.OnMenuItemClickListener() {
+ @Override
+ public boolean onMenuItemClick(android.view.MenuItem item) {
+ application.deleteDictionary(row.dictionaryInfo);
+ setMyListAdapater();
+ return true;
+ }
+ });
+ }
+ }
+
+ private void onShowDownloadableChanged() {
+ setMyListAdapater();
+ Editor prefs = PreferenceManager.getDefaultSharedPreferences(this).edit();
+ prefs.putBoolean(C.SHOW_DOWNLOADABLE, showDownloadable.isChecked());
+ prefs.commit();
+ }
+
+ class MyListAdapter extends BaseAdapter {
+
+ List<DictionaryInfo> dictionariesOnDevice;
+ List<DictionaryInfo> downloadableDictionaries;
+
+ class Row {
+ DictionaryInfo dictionaryInfo;
+ boolean onDevice;
+
+ private Row(DictionaryInfo dictinoaryInfo, boolean onDevice) {
+ this.dictionaryInfo = dictinoaryInfo;
+ this.onDevice = onDevice;
+ }
+ }
+
+ private MyListAdapter(final String[] filters) {
+ dictionariesOnDevice = application.getDictionariesOnDevice(filters);
+ if (showDownloadable.isChecked()) {
+ downloadableDictionaries = application.getDownloadableDictionaries(filters);
+ } else {
+ downloadableDictionaries = Collections.emptyList();
+ }
+ }
+
+ @Override
+ public int getCount() {
+ return 2 + dictionariesOnDevice.size() + downloadableDictionaries.size();
+ }
+
+ @Override
+ public Row getItem(int position) {
+ if (position == 0) {
+ return new Row(null, true);
+ }
+ position -= 1;
+
+ if (position < dictionariesOnDevice.size()) {
+ return new Row(dictionariesOnDevice.get(position), true);
+ }
+ position -= dictionariesOnDevice.size();
+
+ if (position == 0) {
+ return new Row(null, false);
+ }
+ position -= 1;
+
+ assert position < downloadableDictionaries.size();
+ return new Row(downloadableDictionaries.get(position), false);
+ }
+
+ @Override
+ public long getItemId(int position) {
+ return position;