1 // Copyright 2011 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 package com.hughes.android.dictionary;
17 import android.Manifest;
18 import android.app.AlertDialog;
19 import android.app.DownloadManager;
20 import android.app.DownloadManager.Request;
21 import android.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.IntentFilter;
25 import android.content.SharedPreferences;
26 import android.content.SharedPreferences.Editor;
27 import android.content.pm.PackageManager;
28 import android.database.Cursor;
29 import android.net.Uri;
30 import android.os.Build;
31 import android.os.Bundle;
32 import android.os.Environment;
33 import android.os.Handler;
34 import android.support.v7.preference.PreferenceManager;
35 import android.provider.Settings;
36 import android.support.annotation.NonNull;
37 import android.support.v4.app.ActivityCompat;
38 import android.support.v4.content.ContextCompat;
39 import android.support.v4.view.MenuItemCompat;
40 import android.support.v7.app.ActionBar;
41 import android.support.v7.app.AppCompatActivity;
42 import android.support.v7.widget.SearchView;
43 import android.support.v7.widget.SearchView.OnQueryTextListener;
44 import android.support.v7.widget.Toolbar;
45 import android.text.InputType;
46 import android.util.Log;
47 import android.view.ContextMenu;
48 import android.view.ContextMenu.ContextMenuInfo;
49 import android.view.LayoutInflater;
50 import android.view.Menu;
51 import android.view.MenuItem;
52 import android.view.View;
53 import android.view.View.OnClickListener;
54 import android.view.ViewGroup;
55 import android.view.inputmethod.EditorInfo;
56 import android.widget.AdapterView.AdapterContextMenuInfo;
57 import android.widget.BaseAdapter;
58 import android.widget.Button;
59 import android.widget.CompoundButton;
60 import android.widget.CompoundButton.OnCheckedChangeListener;
61 import android.widget.FrameLayout;
62 import android.widget.ImageButton;
63 import android.widget.LinearLayout;
64 import android.widget.ListAdapter;
65 import android.widget.ListView;
66 import android.widget.TextView;
67 import android.widget.Toast;
68 import android.widget.ToggleButton;
70 import com.hughes.android.dictionary.DictionaryInfo.IndexInfo;
71 import com.hughes.android.util.IntentLauncher;
73 import java.io.BufferedInputStream;
75 import java.io.FileInputStream;
76 import java.io.FileOutputStream;
77 import java.io.IOException;
78 import java.io.InputStream;
79 import java.net.MalformedURLException;
81 import java.nio.ByteBuffer;
82 import java.nio.channels.FileChannel;
83 import java.util.Collections;
84 import java.util.HashSet;
85 import java.util.List;
87 import java.util.regex.Pattern;
88 import java.util.zip.ZipEntry;
89 import java.util.zip.ZipInputStream;
92 // Delete, move to top.
94 public class DictionaryManagerActivity extends AppCompatActivity {
96 private static final String LOG = "QuickDic";
97 private static boolean blockAutoLaunch = false;
99 private ListView listView;
100 private ListView getListView() {
101 if (listView == null) {
102 listView = (ListView)findViewById(android.R.id.list);
106 private void setListAdapter(ListAdapter adapter) {
107 getListView().setAdapter(adapter);
109 private ListAdapter getListAdapter() {
110 return getListView().getAdapter();
113 // For DownloadManager bug workaround
114 private final Set<Long> finishedDownloadIds = new HashSet<>();
116 private DictionaryApplication application;
118 private SearchView filterSearchView;
119 private ToggleButton showDownloadable;
121 private LinearLayout dictionariesOnDeviceHeaderRow;
122 private LinearLayout downloadableDictionariesHeaderRow;
124 private Handler uiHandler;
126 private final Runnable dictionaryUpdater = new Runnable() {
129 if (uiHandler == null) {
132 uiHandler.post(new Runnable() {
141 private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
143 public synchronized void onReceive(Context context, Intent intent) {
144 final String action = intent.getAction();
146 if (DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(action)) {
147 startActivity(getLaunchIntent(getApplicationContext()).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP));
149 if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
150 final long downloadId = intent.getLongExtra(
151 DownloadManager.EXTRA_DOWNLOAD_ID, 0);
152 if (finishedDownloadIds.contains(downloadId)) return; // ignore double notifications
153 final DownloadManager.Query query = new DownloadManager.Query();
154 query.setFilterById(downloadId);
155 final DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
156 final Cursor cursor = downloadManager.query(query);
158 if (cursor == null || !cursor.moveToFirst()) {
159 Log.e(LOG, "Couldn't find download.");
163 final String dest = cursor
164 .getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
165 final int status = cursor
167 .getColumnIndex(DownloadManager.COLUMN_STATUS));
168 if (DownloadManager.STATUS_SUCCESSFUL != status) {
169 final int reason = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_REASON));
171 "Download failed: status=" + status +
172 ", reason=" + reason);
173 String msg = Integer.toString(reason);
175 case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
178 case DownloadManager.ERROR_FILE_ERROR:
181 case DownloadManager.ERROR_INSUFFICIENT_SPACE:
182 msg = "Not enough space";
185 new AlertDialog.Builder(context).setTitle(getString(R.string.error)).setMessage(getString(R.string.downloadFailed, msg)).setNeutralButton("Close", null).show();
189 Log.w(LOG, "Download finished: " + dest + " Id: " + downloadId);
191 Toast.makeText(context, getString(R.string.unzippingDictionary, dest),
192 Toast.LENGTH_LONG).show();
194 if (unzipInstall(context, Uri.parse(dest), dest, true)) {
195 finishedDownloadIds.add(downloadId);
196 Log.w(LOG, "Unzipping finished: " + dest + " Id: " + downloadId);
202 private boolean unzipInstall(Context context, Uri zipUri, String dest, boolean delete) {
203 File localZipFile = null;
204 InputStream zipFileStream = null;
205 ZipInputStream zipFile = null;
206 FileOutputStream zipOut = null;
207 boolean result = false;
209 if (zipUri.getScheme().equals("content")) {
210 zipFileStream = context.getContentResolver().openInputStream(zipUri);
213 localZipFile = new File(zipUri.getPath());
215 zipFileStream = new FileInputStream(localZipFile);
216 } catch (Exception e) {
217 if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
218 ActivityCompat.requestPermissions(this,
219 new String[] {Manifest.permission.READ_EXTERNAL_STORAGE,
220 Manifest.permission.WRITE_EXTERNAL_STORAGE
227 zipFile = new ZipInputStream(new BufferedInputStream(zipFileStream));
229 while ((zipEntry = zipFile.getNextEntry()) != null) {
230 // Note: this check prevents security issues like accidental path
231 // traversal, which unfortunately ZipInputStream has no protection against.
232 // So take extra care when changing it.
233 if (!Pattern.matches("[-A-Za-z]+\\.quickdic", zipEntry.getName())) {
234 Log.w(LOG, "Invalid zip entry: " + zipEntry.getName());
237 Log.d(LOG, "Unzipping entry: " + zipEntry.getName());
238 File targetFile = new File(application.getDictDir(), zipEntry.getName());
239 if (targetFile.exists()) {
240 targetFile.renameTo(new File(targetFile.getAbsolutePath().replace(".quickdic", ".bak.quickdic")));
241 targetFile = new File(application.getDictDir(), zipEntry.getName());
243 zipOut = new FileOutputStream(targetFile);
244 copyStream(zipFile, zipOut);
246 application.backgroundUpdateDictionaries(dictionaryUpdater);
248 Toast.makeText(context, getString(R.string.installationFinished, dest),
249 Toast.LENGTH_LONG).show();
251 } catch (Exception e) {
252 String msg = getString(R.string.unzippingFailed, dest + ": " + e.getMessage());
253 File dir = application.getDictDir();
254 if (!dir.canWrite() || !DictionaryApplication.checkFileCreate(dir)) {
255 msg = getString(R.string.notWritable, dir.getAbsolutePath());
257 new AlertDialog.Builder(context).setTitle(getString(R.string.error)).setMessage(msg).setNeutralButton("Close", null).show();
258 Log.e(LOG, "Failed to unzip.", e);
261 if (zipOut != null) zipOut.close();
262 } catch (IOException ignored) {}
264 if (zipFile != null) zipFile.close();
265 } catch (IOException ignored) {}
267 if (zipFileStream != null) zipFileStream.close();
268 } catch (IOException ignored) {}
269 if (localZipFile != null && delete) //noinspection ResultOfMethodCallIgnored
270 localZipFile.delete();
275 public static Intent getLaunchIntent(Context c) {
276 final Intent intent = new Intent(c, DictionaryManagerActivity.class);
277 intent.putExtra(C.CAN_AUTO_LAUNCH_DICT, false);
281 private void readableCheckAndError(boolean requestPermission) {
282 final File dictDir = application.getDictDir();
283 if (dictDir.canRead() && dictDir.canExecute()) return;
284 blockAutoLaunch = true;
285 if (requestPermission &&
286 ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
287 ActivityCompat.requestPermissions(this,
288 new String[] {Manifest.permission.READ_EXTERNAL_STORAGE,
289 Manifest.permission.WRITE_EXTERNAL_STORAGE
293 blockAutoLaunch = true;
295 AlertDialog.Builder builder = new AlertDialog.Builder(getListView().getContext());
296 builder.setTitle(getString(R.string.error));
297 builder.setMessage(getString(
298 R.string.unableToReadDictionaryDir,
299 dictDir.getAbsolutePath(),
300 Environment.getExternalStorageDirectory()));
301 builder.setNeutralButton("Close", null);
302 builder.create().show();
306 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
307 readableCheckAndError(false);
309 application.backgroundUpdateDictionaries(dictionaryUpdater);
315 public void onCreate(Bundle savedInstanceState) {
316 DictionaryApplication.INSTANCE.init(getApplicationContext());
317 application = DictionaryApplication.INSTANCE;
318 // This must be first, otherwise the action bar doesn't get
320 setTheme(application.getSelectedTheme().themeId);
322 super.onCreate(savedInstanceState);
323 Log.d(LOG, "onCreate:" + this);
325 setTheme(application.getSelectedTheme().themeId);
327 blockAutoLaunch = false;
330 setContentView(R.layout.dictionary_manager_activity);
332 dictionariesOnDeviceHeaderRow = (LinearLayout) LayoutInflater.from(
333 getListView().getContext()).inflate(
334 R.layout.dictionary_manager_header_row_on_device, getListView(), false);
336 downloadableDictionariesHeaderRow = (LinearLayout) LayoutInflater.from(
337 getListView().getContext()).inflate(
338 R.layout.dictionary_manager_header_row_downloadable, getListView(), false);
340 showDownloadable = downloadableDictionariesHeaderRow
341 .findViewById(R.id.hideDownloadable);
342 showDownloadable.setOnCheckedChangeListener(new OnCheckedChangeListener() {
344 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
345 onShowDownloadableChanged();
350 Disable version update notification, I don't maintain the text really
351 and I don't think it is very useful.
352 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
353 final String thanksForUpdatingLatestVersion = getString(R.string.thanksForUpdatingVersion);
354 if (!prefs.getString(C.THANKS_FOR_UPDATING_VERSION, "").equals(
355 thanksForUpdatingLatestVersion)) {
356 blockAutoLaunch = true;
357 startActivity(HtmlDisplayActivity.getWhatsNewLaunchIntent(getApplicationContext()));
358 prefs.edit().putString(C.THANKS_FOR_UPDATING_VERSION, thanksForUpdatingLatestVersion)
362 IntentFilter downloadManagerIntents = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
363 downloadManagerIntents.addAction(DownloadManager.ACTION_NOTIFICATION_CLICKED);
364 registerReceiver(broadcastReceiver, downloadManagerIntents);
367 registerForContextMenu(getListView());
368 getListView().setItemsCanFocus(true);
370 readableCheckAndError(true);
372 onCreateSetupActionBar();
374 final Intent intent = getIntent();
375 if (intent != null && intent.getAction() != null &&
376 intent.getAction().equals(Intent.ACTION_VIEW)) {
377 blockAutoLaunch = true;
378 Uri uri = intent.getData();
379 unzipInstall(this, uri, uri.getLastPathSegment(), false);
383 private void onCreateSetupActionBar() {
384 ActionBar actionBar = getSupportActionBar();
385 actionBar.setDisplayShowTitleEnabled(false);
386 actionBar.setDisplayShowHomeEnabled(false);
387 actionBar.setDisplayHomeAsUpEnabled(false);
389 filterSearchView = new SearchView(getSupportActionBar().getThemedContext());
390 filterSearchView.setIconifiedByDefault(false);
391 // filterSearchView.setIconified(false); // puts the magnifying glass in
394 filterSearchView.setQueryHint(getString(R.string.searchText));
395 filterSearchView.setSubmitButtonEnabled(false);
396 FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
397 FrameLayout.LayoutParams.WRAP_CONTENT);
398 filterSearchView.setLayoutParams(lp);
399 filterSearchView.setInputType(InputType.TYPE_CLASS_TEXT);
400 filterSearchView.setImeOptions(
401 EditorInfo.IME_ACTION_DONE |
402 EditorInfo.IME_FLAG_NO_EXTRACT_UI |
403 // EditorInfo.IME_FLAG_NO_FULLSCREEN | // Requires API
405 EditorInfo.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
407 filterSearchView.setOnQueryTextListener(new OnQueryTextListener() {
409 public boolean onQueryTextSubmit(String query) {
410 filterSearchView.clearFocus();
415 public boolean onQueryTextChange(String filterText) {
420 filterSearchView.setFocusable(true);
422 actionBar.setCustomView(filterSearchView);
423 actionBar.setDisplayShowCustomEnabled(true);
425 // Avoid wasting space on large left inset
426 Toolbar tb = (Toolbar)filterSearchView.getParent();
427 tb.setContentInsetsRelative(0, 0);
431 public void onDestroy() {
433 unregisterReceiver(broadcastReceiver);
436 private static void copyStream(final InputStream ins, final FileOutputStream outs)
438 ByteBuffer buf = ByteBuffer.allocateDirect(1024 * 64);
439 FileChannel out = outs.getChannel();
442 final byte[] bytes = new byte[1024 * 64];
444 bytesRead = ins.read(bytes, pos, bytes.length - pos);
445 if (bytesRead != -1) pos += bytesRead;
446 if (bytesRead == -1 ? pos != 0 : 2*pos >= bytes.length) {
447 buf.put(bytes, 0, pos);
450 while (buf.hasRemaining()) out.write(buf);
453 } while (bytesRead != -1);
457 protected void onStart() {
459 uiHandler = new Handler();
463 protected void onStop() {
469 protected void onResume() {
472 if (PreferenceActivity.prefsMightHaveChanged) {
473 PreferenceActivity.prefsMightHaveChanged = false;
475 startActivity(getIntent());
478 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
479 showDownloadable.setChecked(prefs.getBoolean(C.SHOW_DOWNLOADABLE, true));
481 if (!blockAutoLaunch &&
482 getIntent().getBooleanExtra(C.CAN_AUTO_LAUNCH_DICT, true) &&
483 prefs.contains(C.DICT_FILE) &&
484 prefs.contains(C.INDEX_SHORT_NAME)) {
485 Log.d(LOG, "Skipping DictionaryManager, going straight to dictionary.");
486 startActivity(DictionaryActivity.getLaunchIntent(getApplicationContext(),
487 new File(prefs.getString(C.DICT_FILE, "")),
488 prefs.getString(C.INDEX_SHORT_NAME, ""),
494 // Remove the active dictionary from the prefs so we won't autolaunch
496 prefs.edit().remove(C.DICT_FILE).remove(C.INDEX_SHORT_NAME).commit();
498 application.backgroundUpdateDictionaries(dictionaryUpdater);
504 public boolean onCreateOptionsMenu(final Menu menu) {
505 if ("true".equals(Settings.System.getString(getContentResolver(), "firebase.test.lab")))
507 return false; // testing the menu is not very interesting
509 final MenuItem sort = menu.add(getString(R.string.sortDicts));
510 MenuItemCompat.setShowAsAction(sort, MenuItem.SHOW_AS_ACTION_NEVER);
511 sort.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
512 public boolean onMenuItemClick(final MenuItem menuItem) {
513 application.sortDictionaries();
519 final MenuItem browserDownload = menu.add(getString(R.string.browserDownload));
520 MenuItemCompat.setShowAsAction(browserDownload, MenuItem.SHOW_AS_ACTION_NEVER);
521 browserDownload.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
522 public boolean onMenuItemClick(final MenuItem menuItem) {
523 final Intent intent = new Intent(Intent.ACTION_VIEW);
525 .parse("https://github.com/rdoeffinger/Dictionary/releases/v0.3-dictionaries"));
526 startActivity(intent);
531 DictionaryApplication.onCreateGlobalOptionsMenu(this, menu);
536 public void onCreateContextMenu(final ContextMenu menu, final View view,
537 final ContextMenuInfo menuInfo) {
538 super.onCreateContextMenu(menu, view, menuInfo);
539 Log.d(LOG, "onCreateContextMenu, " + menuInfo);
541 final AdapterContextMenuInfo adapterContextMenuInfo =
542 (AdapterContextMenuInfo) menuInfo;
543 final int position = adapterContextMenuInfo.position;
544 final MyListAdapter.Row row = (MyListAdapter.Row) getListAdapter().getItem(position);
546 if (row.dictionaryInfo == null) {
550 if (position > 0 && row.onDevice) {
551 final android.view.MenuItem moveToTopMenuItem =
552 menu.add(R.string.moveToTop);
553 moveToTopMenuItem.setOnMenuItemClickListener(new
554 android.view.MenuItem.OnMenuItemClickListener() {
556 public boolean onMenuItemClick(android.view.MenuItem item) {
557 application.moveDictionaryToTop(row.dictionaryInfo);
565 final android.view.MenuItem deleteMenuItem = menu.add(R.string.deleteDictionary);
567 .setOnMenuItemClickListener(new android.view.MenuItem.OnMenuItemClickListener() {
569 public boolean onMenuItemClick(android.view.MenuItem item) {
570 application.deleteDictionary(row.dictionaryInfo);
578 private void onShowDownloadableChanged() {
580 Editor prefs = PreferenceManager.getDefaultSharedPreferences(this).edit();
581 prefs.putBoolean(C.SHOW_DOWNLOADABLE, showDownloadable.isChecked());
585 class MyListAdapter extends BaseAdapter {
587 final List<DictionaryInfo> dictionariesOnDevice;
588 final List<DictionaryInfo> downloadableDictionaries;
591 final DictionaryInfo dictionaryInfo;
592 final boolean onDevice;
594 private Row(DictionaryInfo dictionaryInfo, boolean onDevice) {
595 this.dictionaryInfo = dictionaryInfo;
596 this.onDevice = onDevice;
600 private MyListAdapter(final String[] filters) {
601 dictionariesOnDevice = application.getDictionariesOnDevice(filters);
602 if (showDownloadable.isChecked()) {
603 downloadableDictionaries = application.getDownloadableDictionaries(filters);
605 downloadableDictionaries = Collections.emptyList();
610 public int getCount() {
611 return 2 + dictionariesOnDevice.size() + downloadableDictionaries.size();
615 public Row getItem(int position) {
617 return new Row(null, true);
621 if (position < dictionariesOnDevice.size()) {
622 return new Row(dictionariesOnDevice.get(position), true);
624 position -= dictionariesOnDevice.size();
627 return new Row(null, false);
631 assert position < downloadableDictionaries.size();
632 return new Row(downloadableDictionaries.get(position), false);
636 public long getItemId(int position) {
641 public int getViewTypeCount() {
646 public int getItemViewType(int position) {
647 final Row row = getItem(position);
648 if (row.dictionaryInfo == null) {
649 return row.onDevice ? 0 : 1;
651 assert row.dictionaryInfo.indexInfos.size() <= 2;
656 public View getView(int position, View convertView, ViewGroup parent) {
657 if (convertView == dictionariesOnDeviceHeaderRow ||
658 convertView == downloadableDictionariesHeaderRow) {
662 final Row row = getItem(position);
664 if (row.dictionaryInfo == null) {
665 assert convertView == null;
666 return row.onDevice ? dictionariesOnDeviceHeaderRow : downloadableDictionariesHeaderRow;
668 return createDictionaryRow(row.dictionaryInfo, parent, convertView, row.onDevice);
673 private void setMyListAdapter() {
674 final String filter = filterSearchView == null ? "" : filterSearchView.getQuery()
676 final String[] filters = filter.trim().toLowerCase().split("(\\s|-)+");
677 setListAdapter(new MyListAdapter(filters));
680 private boolean isDownloadActive(String downloadUrl, boolean cancel) {
681 DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
682 final DownloadManager.Query query = new DownloadManager.Query();
683 query.setFilterByStatus(DownloadManager.STATUS_PAUSED | DownloadManager.STATUS_PENDING | DownloadManager.STATUS_RUNNING);
684 final Cursor cursor = downloadManager.query(query);
686 // Due to a bug, cursor is null instead of empty when
687 // the download manager is disabled.
688 if (cursor == null) {
690 String msg = getString(R.string.downloadManagerQueryFailed);
691 new AlertDialog.Builder(this).setTitle(getString(R.string.error))
692 .setMessage(getString(R.string.downloadFailed, msg))
693 .setNeutralButton("Close", null).show();
700 destFile = new File(new URL(downloadUrl).getPath()).getName();
701 } catch (MalformedURLException e) {
702 throw new RuntimeException("Invalid download URL!", e);
704 while (cursor.moveToNext()) {
705 if (downloadUrl.equals(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_URI))))
707 if (destFile.equals(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_TITLE))))
710 boolean active = !cursor.isAfterLast();
711 if (active && cancel) {
712 long downloadId = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_ID));
713 finishedDownloadIds.add(downloadId);
714 downloadManager.remove(downloadId);
720 private View createDictionaryRow(final DictionaryInfo dictionaryInfo,
721 final ViewGroup parent, View row, boolean canLaunch) {
724 row = LayoutInflater.from(parent.getContext()).inflate(
725 R.layout.dictionary_manager_row, parent, false);
727 final TextView name = row.findViewById(R.id.dictionaryName);
728 final TextView details = row.findViewById(R.id.dictionaryDetails);
729 name.setText(application.getDictionaryName(dictionaryInfo.uncompressedFilename));
731 final boolean updateAvailable = application.updateAvailable(dictionaryInfo);
732 final Button downloadButton = row.findViewById(R.id.downloadButton);
733 final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
734 boolean broken = false;
735 if (!dictionaryInfo.isValid()) {
739 if (downloadable != null && (!canLaunch || updateAvailable)) {
742 R.string.downloadButton,
743 downloadable.zipBytes / 1024.0 / 1024.0));
744 downloadButton.setMinWidth(application.languageButtonPixels * 3 / 2);
745 downloadButton.setOnClickListener(new OnClickListener() {
747 public void onClick(View arg0) {
748 downloadDictionary(downloadable.downloadUrl, downloadable.zipBytes, downloadButton);
751 downloadButton.setVisibility(View.VISIBLE);
753 if (isDownloadActive(downloadable.downloadUrl, false))
754 downloadButton.setText("X");
756 downloadButton.setVisibility(View.GONE);
759 LinearLayout buttons = row.findViewById(R.id.dictionaryLauncherButtons);
761 final List<IndexInfo> sortedIndexInfos = application
762 .sortedIndexInfos(dictionaryInfo.indexInfos);
763 final StringBuilder builder = new StringBuilder();
764 if (updateAvailable) {
765 builder.append(getString(R.string.updateAvailable));
767 assert buttons.getChildCount() == 4;
768 for (int i = 0; i < 2; i++) {
769 final Button textButton = (Button)buttons.getChildAt(2*i);
770 final ImageButton imageButton = (ImageButton)buttons.getChildAt(2*i + 1);
771 if (i >= sortedIndexInfos.size()) {
772 textButton.setVisibility(View.GONE);
773 imageButton.setVisibility(View.GONE);
776 final IndexInfo indexInfo = sortedIndexInfos.get(i);
777 final View button = IsoUtils.INSTANCE.setupButton(textButton, imageButton,
781 button.setOnClickListener(
782 new IntentLauncher(buttons.getContext(),
783 DictionaryActivity.getLaunchIntent(getApplicationContext(),
784 application.getPath(dictionaryInfo.uncompressedFilename),
785 indexInfo.shortName, "")));
788 button.setEnabled(canLaunch);
789 button.setFocusable(canLaunch);
790 if (builder.length() != 0) {
791 builder.append("; ");
793 builder.append(getString(R.string.indexInfo, indexInfo.shortName,
794 indexInfo.mainTokenCount));
796 builder.append("; ");
797 builder.append(getString(R.string.downloadButton, dictionaryInfo.uncompressedBytes / 1024.0 / 1024.0));
799 name.setText("Broken: " + application.getDictionaryName(dictionaryInfo.uncompressedFilename));
800 builder.append("; Cannot be used, redownload, check hardware/file system");
802 details.setText(builder.toString());
805 row.setOnClickListener(new IntentLauncher(parent.getContext(),
806 DictionaryActivity.getLaunchIntent(getApplicationContext(),
807 application.getPath(dictionaryInfo.uncompressedFilename),
808 dictionaryInfo.indexInfos.get(0).shortName, "")));
809 // do not setFocusable, for keyboard navigation
810 // offering only the index buttons is better.
812 row.setClickable(canLaunch);
813 // Allow deleting, even if we cannot open
814 row.setLongClickable(broken || canLaunch);
815 row.setBackgroundResource(android.R.drawable.menuitem_background);
820 private synchronized void downloadDictionary(final String downloadUrl, long bytes, Button downloadButton) {
821 if (isDownloadActive(downloadUrl, true)) {
824 R.string.downloadButton,
825 bytes / 1024.0 / 1024.0));
828 // API 19 and earlier have issues with github URLs, both http and https.
829 // Really old (~API 10) DownloadManager cannot handle https at all.
830 // Work around both with in one.
831 String altUrl = downloadUrl.replace("https://github.com/rdoeffinger/Dictionary/releases/download/v0.2-dictionaries/", "http://ffmpeg.org/~reimar/dict/");
832 altUrl = altUrl.replace("https://github.com/rdoeffinger/Dictionary/releases/download/v0.3-dictionaries/", "http://ffmpeg.org/~reimar/dict/");
833 Request request = new Request(Uri.parse(Build.VERSION.SDK_INT < 21 ? altUrl : downloadUrl));
837 destFile = new File(new URL(downloadUrl).getPath()).getName();
838 } catch (MalformedURLException e) {
839 throw new RuntimeException("Invalid download URL!", e);
841 Log.d(LOG, "Downloading to: " + destFile);
842 request.setTitle(destFile);
843 File destFilePath = new File(application.getDictDir(), destFile);
844 destFilePath.delete();
846 request.setDestinationUri(Uri.fromFile(destFilePath));
847 } catch (Exception e) {
850 DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
852 if (downloadManager == null) {
853 String msg = getString(R.string.downloadManagerQueryFailed);
854 new AlertDialog.Builder(this).setTitle(getString(R.string.error))
855 .setMessage(getString(R.string.downloadFailed, msg))
856 .setNeutralButton("Close", null).show();
861 downloadManager.enqueue(request);
862 } catch (SecurityException e) {
863 request = new Request(Uri.parse(downloadUrl));
864 request.setTitle(destFile);
865 downloadManager.enqueue(request);
867 Log.w(LOG, "Download started: " + destFile);
868 downloadButton.setText("X");