+ public boolean onCreateOptionsMenu(final Menu menu) {
+ if ("true".equals(Settings.System.getString(getContentResolver(), "firebase.test.lab")))
+ {
+ return false; // testing the menu is not very interesting
+ }
+ final MenuItem sort = menu.add(getString(R.string.sortDicts));
+ MenuItemCompat.setShowAsAction(sort, MenuItem.SHOW_AS_ACTION_NEVER);
+ sort.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
+ public boolean onMenuItemClick(final MenuItem menuItem) {
+ application.sortDictionaries();
+ setMyListAdapater();
+ return true;
+ }
+ });
+
+ final MenuItem browserDownload = menu.add(getString(R.string.browserDownload));
+ MenuItemCompat.setShowAsAction(browserDownload, MenuItem.SHOW_AS_ACTION_NEVER);
+ browserDownload.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
+ public boolean onMenuItemClick(final MenuItem menuItem) {
+ final Intent intent = new Intent(Intent.ACTION_VIEW);
+ intent.setData(Uri
+ .parse("https://github.com/rdoeffinger/Dictionary/releases/v0.2-dictionaries"));
+ startActivity(intent);
+ return false;
+ }
+ });
+
+ DictionaryApplication.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 dictionaryInfo, boolean onDevice) {
+ this.dictionaryInfo = dictionaryInfo;
+ 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;
+ }
+
+ @Override
+ public int getViewTypeCount() {
+ return 3;
+ }
+
+ @Override
+ public int getItemViewType(int position) {
+ final Row row = getItem(position);
+ if (row.dictionaryInfo == null) {
+ return row.onDevice ? 0 : 1;
+ }
+ assert row.dictionaryInfo.indexInfos.size() <= 2;
+ return 2;
+ }
+
+ @Override
+ public View getView(int position, View convertView, ViewGroup parent) {
+ if (convertView == dictionariesOnDeviceHeaderRow ||
+ convertView == downloadableDictionariesHeaderRow) {
+ return convertView;
+ }
+
+ final Row row = getItem(position);
+
+ if (row.dictionaryInfo == null) {
+ assert convertView == null;
+ return row.onDevice ? dictionariesOnDeviceHeaderRow : downloadableDictionariesHeaderRow;
+ }
+ return createDictionaryRow(row.dictionaryInfo, parent, convertView, row.onDevice);
+ }
+
+ }
+
+ private void setMyListAdapater() {
+ final String filter = filterSearchView == null ? "" : filterSearchView.getQuery()
+ .toString();
+ final String[] filters = filter.trim().toLowerCase().split("(\\s|-)+");
+ setListAdapter(new MyListAdapter(filters));
+ }
+
+ private View createDictionaryRow(final DictionaryInfo dictionaryInfo,
+ final ViewGroup parent, View row, boolean canLaunch) {
+
+ if (row == null) {
+ row = LayoutInflater.from(parent.getContext()).inflate(
+ R.layout.dictionary_manager_row, parent, false);
+ }
+ final TextView name = row.findViewById(R.id.dictionaryName);
+ final TextView details = row.findViewById(R.id.dictionaryDetails);
+ name.setText(application.getDictionaryName(dictionaryInfo.uncompressedFilename));
+
+ final boolean updateAvailable = application.updateAvailable(dictionaryInfo);
+ final Button downloadButton = row.findViewById(R.id.downloadButton);
+ final DictionaryInfo downloadable = application.getDownloadable(dictionaryInfo.uncompressedFilename);
+ boolean broken = false;
+ if (!dictionaryInfo.isValid()) {
+ broken = true;
+ canLaunch = false;
+ }
+ if (downloadable != null && (!canLaunch || updateAvailable)) {
+ downloadButton
+ .setText(getString(
+ R.string.downloadButton,
+ downloadable.zipBytes / 1024.0 / 1024.0));
+ downloadButton.setMinWidth(application.languageButtonPixels * 3 / 2);
+ downloadButton.setOnClickListener(new OnClickListener() {
+ @Override
+ public void onClick(View arg0) {
+ downloadDictionary(downloadable.downloadUrl, downloadable.zipBytes, downloadButton);
+ }
+ });
+ downloadButton.setVisibility(View.VISIBLE);
+ } else {
+ downloadButton.setVisibility(View.GONE);
+ }
+
+ LinearLayout buttons = row.findViewById(R.id.dictionaryLauncherButtons);
+
+ final List<IndexInfo> sortedIndexInfos = application
+ .sortedIndexInfos(dictionaryInfo.indexInfos);
+ final StringBuilder builder = new StringBuilder();
+ if (updateAvailable) {
+ builder.append(getString(R.string.updateAvailable));
+ }
+ assert buttons.getChildCount() == 4;
+ for (int i = 0; i < 2; i++) {
+ final Button textButton = (Button)buttons.getChildAt(2*i);
+ final ImageButton imageButton = (ImageButton)buttons.getChildAt(2*i + 1);
+ if (i >= sortedIndexInfos.size()) {
+ textButton.setVisibility(View.GONE);
+ imageButton.setVisibility(View.GONE);
+ continue;
+ }
+ final IndexInfo indexInfo = sortedIndexInfos.get(i);
+ final View button = IsoUtils.INSTANCE.setupButton(textButton, imageButton, dictionaryInfo,
+ indexInfo, application.languageButtonPixels);
+
+ if (canLaunch) {
+ button.setOnClickListener(
+ new IntentLauncher(buttons.getContext(),
+ DictionaryActivity.getLaunchIntent(getApplicationContext(),
+ application.getPath(dictionaryInfo.uncompressedFilename),
+ indexInfo.shortName, "")));
+
+ }
+ button.setEnabled(canLaunch);
+ button.setFocusable(canLaunch);
+ if (builder.length() != 0) {
+ builder.append("; ");
+ }
+ builder.append(getString(R.string.indexInfo, indexInfo.shortName,
+ indexInfo.mainTokenCount));
+ }
+ builder.append("; ");
+ builder.append(getString(R.string.downloadButton, dictionaryInfo.uncompressedBytes / 1024.0 / 1024.0));
+ if (broken) {
+ name.setText("Broken: " + application.getDictionaryName(dictionaryInfo.uncompressedFilename));
+ builder.append("; Cannot be used, redownload, check hardware/file system");
+ }
+ details.setText(builder.toString());
+
+ if (canLaunch) {
+ row.setOnClickListener(new IntentLauncher(parent.getContext(),
+ DictionaryActivity.getLaunchIntent(getApplicationContext(),
+ application.getPath(dictionaryInfo.uncompressedFilename),
+ dictionaryInfo.indexInfos.get(0).shortName, "")));
+ // do not setFocusable, for keyboard navigation
+ // offering only the index buttons is better.
+ }
+ row.setClickable(canLaunch);
+ // Allow deleting, even if we cannot open
+ row.setLongClickable(broken || canLaunch);
+ row.setBackgroundResource(android.R.drawable.menuitem_background);
+
+ return row;
+ }
+
+ private synchronized void downloadDictionary(final String downloadUrl, long bytes, Button downloadButton) {
+ String destFile;
+ try {
+ destFile = new File(new URL(downloadUrl).getPath()).getName();
+ } catch (MalformedURLException e) {
+ throw new RuntimeException("Invalid download URL!", e);
+ }
+ DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
+ final DownloadManager.Query query = new DownloadManager.Query();
+ query.setFilterByStatus(DownloadManager.STATUS_PAUSED | DownloadManager.STATUS_PENDING | DownloadManager.STATUS_RUNNING);
+ final Cursor cursor = downloadManager.query(query);
+
+ // Due to a bug, cursor is null instead of empty when
+ // the download manager is disabled.
+ if (cursor == null) {
+ String msg = getString(R.string.downloadManagerQueryFailed);
+ new AlertDialog.Builder(DictionaryManagerActivity.this).setTitle(getString(R.string.error))
+ .setMessage(getString(R.string.downloadFailed, msg))
+ .setNeutralButton("Close", null).show();
+ return;
+ }
+
+ while (cursor.moveToNext()) {
+ if (downloadUrl.equals(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_URI))))
+ break;
+ if (destFile.equals(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_TITLE))))
+ break;
+ }
+ if (!cursor.isAfterLast()) {
+ downloadManager.remove(cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_ID)));
+ downloadButton
+ .setText(getString(
+ R.string.downloadButton,
+ bytes / 1024.0 / 1024.0));
+ cursor.close();
+ return;
+ }
+ cursor.close();
+ Request request = new Request(
+ Uri.parse(downloadUrl));
+
+ Log.d(LOG, "Downloading to: " + destFile);
+ request.setTitle(destFile);
+
+ File destFilePath = new File(application.getDictDir(), destFile);
+ destFilePath.delete();
+ try {
+ request.setDestinationUri(Uri.fromFile(destFilePath));
+ } catch (Exception e) {
+ }
+
+ try {
+ downloadManager.enqueue(request);
+ } catch (SecurityException e) {
+ request = new Request(Uri.parse(downloadUrl));
+ request.setTitle(destFile);
+ downloadManager.enqueue(request);
+ }
+ Log.w(LOG, "Download started: " + destFile);
+ downloadButton.setText("X");
+ }