Search in sources :

Example 66 with PopupMenu

use of android.support.v7.widget.PopupMenu in project Osmand by osmandapp.

the class AvailableGPXFragment method openPopUpMenu.

private void openPopUpMenu(View v, final GpxInfo gpxInfo) {
    IconsCache iconsCache = getMyApplication().getIconsCache();
    final PopupMenu optionsMenu = new PopupMenu(getActivity(), v);
    DirectionsDialogs.setupPopUpMenuIcon(optionsMenu);
    MenuItem item = optionsMenu.getMenu().add(R.string.shared_string_show_on_map).setIcon(iconsCache.getThemedIcon(R.drawable.ic_show_on_map));
    item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            showGpxOnMap(gpxInfo);
            return true;
        }
    });
    GPXTrackAnalysis analysis;
    if ((analysis = getGpxTrackAnalysis(gpxInfo, app)) != null) {
        if (analysis.totalDistance != 0 && !gpxInfo.currentlyRecordingTrack) {
            item = optionsMenu.getMenu().add(R.string.analyze_on_map).setIcon(iconsCache.getThemedIcon(R.drawable.ic_action_info_dark));
            item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    new OpenGpxDetailsTask(gpxInfo).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                    return true;
                }
            });
        }
    }
    item = optionsMenu.getMenu().add(R.string.shared_string_move).setIcon(iconsCache.getThemedIcon(R.drawable.ic_action_folder_stroke));
    item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            moveGpx(gpxInfo);
            return true;
        }
    });
    item = optionsMenu.getMenu().add(R.string.shared_string_rename).setIcon(iconsCache.getThemedIcon(R.drawable.ic_action_edit_dark));
    item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            LocalIndexesFragment.renameFile(getActivity(), gpxInfo.file, new RenameCallback() {

                @Override
                public void renamedTo(File file) {
                    app.getGpxDatabase().rename(gpxInfo.file, file);
                    asyncLoader = new LoadGpxTask();
                    asyncLoader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, getActivity());
                }
            });
            return true;
        }
    });
    item = optionsMenu.getMenu().add(R.string.shared_string_share).setIcon(iconsCache.getThemedIcon(R.drawable.ic_action_gshare_dark));
    item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            final Uri fileUri = Uri.fromFile(gpxInfo.file);
            final Intent sendIntent = new Intent(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
            sendIntent.setType("application/gpx+xml");
            startActivity(sendIntent);
            return true;
        }
    });
    final OsmEditingPlugin osmEditingPlugin = OsmandPlugin.getEnabledPlugin(OsmEditingPlugin.class);
    if (osmEditingPlugin != null && osmEditingPlugin.isActive()) {
        item = optionsMenu.getMenu().add(R.string.shared_string_export).setIcon(iconsCache.getThemedIcon(R.drawable.ic_action_export));
        item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                osmEditingPlugin.sendGPXFiles(getActivity(), AvailableGPXFragment.this, gpxInfo);
                return true;
            }
        });
    }
    item = optionsMenu.getMenu().add(R.string.shared_string_delete).setIcon(iconsCache.getThemedIcon(R.drawable.ic_action_delete_dark));
    item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setMessage(R.string.recording_delete_confirm);
            builder.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    operationTask = new DeleteGpxTask();
                    operationTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, gpxInfo);
                }
            });
            builder.setNegativeButton(R.string.shared_string_cancel, null);
            builder.show();
            return true;
        }
    });
    optionsMenu.show();
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) DialogInterface(android.content.DialogInterface) GPXTrackAnalysis(net.osmand.plus.GPXUtilities.GPXTrackAnalysis) MenuItem(android.view.MenuItem) ContextMenuItem(net.osmand.plus.ContextMenuItem) Intent(android.content.Intent) Uri(android.net.Uri) OsmEditingPlugin(net.osmand.plus.osmedit.OsmEditingPlugin) RenameCallback(net.osmand.plus.download.ui.LocalIndexesFragment.RenameCallback) IconsCache(net.osmand.plus.IconsCache) SelectedGpxFile(net.osmand.plus.GpxSelectionHelper.SelectedGpxFile) GPXFile(net.osmand.plus.GPXUtilities.GPXFile) File(java.io.File) PopupMenu(android.support.v7.widget.PopupMenu)

Example 67 with PopupMenu

use of android.support.v7.widget.PopupMenu in project Osmand by osmandapp.

the class ContextMenuHelper method showPopupLangMenu.

protected static void showPopupLangMenu(final Context ctx, Toolbar tb, final SampleApplication app, final Amenity a, final Dialog dialog, final String langSelected) {
    final PopupMenu optionsMenu = new PopupMenu(ctx, tb, Gravity.RIGHT);
    Set<String> namesSet = new TreeSet<>();
    namesSet.addAll(a.getNames("content", "en"));
    namesSet.addAll(a.getNames("description", "en"));
    Map<String, String> names = new HashMap<>();
    for (String n : namesSet) {
        names.put(n, getLangName(ctx, n));
    }
    String selectedLangName = names.get(langSelected);
    if (selectedLangName != null) {
        names.remove(langSelected);
    }
    Map<String, String> sortedNames = AndroidUtils.sortByValue(names);
    if (selectedLangName != null) {
        MenuItem item = optionsMenu.getMenu().add(selectedLangName);
        item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                dialog.dismiss();
                showWiki(ctx, app, a, langSelected);
                return true;
            }
        });
    }
    for (final Map.Entry<String, String> e : sortedNames.entrySet()) {
        MenuItem item = optionsMenu.getMenu().add(e.getValue());
        item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                dialog.dismiss();
                showWiki(ctx, app, a, e.getKey());
                return true;
            }
        });
    }
    optionsMenu.show();
}
Also used : HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) MenuItem(android.view.MenuItem) HashMap(java.util.HashMap) Map(java.util.Map) PopupMenu(android.support.v7.widget.PopupMenu)

Example 68 with PopupMenu

use of android.support.v7.widget.PopupMenu in project andOTP by andOTP.

the class EntriesCardAdapter method showPopupMenu.

private void showPopupMenu(View view, final int pos) {
    View menuItemView = view.findViewById(R.id.menuButton);
    PopupMenu popup = new PopupMenu(view.getContext(), menuItemView);
    MenuInflater inflate = popup.getMenuInflater();
    inflate.inflate(R.menu.menu_popup, popup.getMenu());
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {
            int id = item.getItemId();
            if (id == R.id.menu_popup_editLabel) {
                editEntryLabel(pos);
                return true;
            } else if (id == R.id.menu_popup_changeImage) {
                changeThumbnail(pos);
                return true;
            } else if (id == R.id.menu_popup_editTags) {
                editEntryTags(pos);
                return true;
            } else if (id == R.id.menu_popup_remove) {
                removeItem(pos);
                return true;
            } else {
                return false;
            }
        }
    });
    popup.show();
}
Also used : MenuInflater(android.view.MenuInflater) MenuItem(android.view.MenuItem) GridView(android.widget.GridView) View(android.view.View) AdapterView(android.widget.AdapterView) RecyclerView(android.support.v7.widget.RecyclerView) PopupMenu(android.support.v7.widget.PopupMenu)

Example 69 with PopupMenu

use of android.support.v7.widget.PopupMenu in project LibreraReader by foobnix.

the class DragingDialogs method preferences.

public static DragingPopup preferences(final FrameLayout anchor, final DocumentController controller, final Runnable onRefresh, final Runnable updateUIRefresh) {
    final int cssHash = BookCSS.get().toCssString().hashCode();
    final int appHash = Objects.hashCode(AppState.get());
    if (ExtUtils.isNotValidFile(controller.getCurrentBook())) {
        DragingPopup dialog = new DragingPopup(R.string.preferences, anchor, PREF_WIDTH, PREF_HEIGHT) {

            @Override
            public View getContentView(final LayoutInflater inflater) {
                TextView txt = new TextView(anchor.getContext());
                txt.setText(R.string.file_not_found);
                return txt;
            }
        };
        return dialog;
    }
    DragingPopup dialog = new DragingPopup(R.string.preferences, anchor, PREF_WIDTH, PREF_HEIGHT) {

        @Override
        public View getContentView(final LayoutInflater inflater) {
            View inflate = inflater.inflate(R.layout.dialog_prefs, null, false);
            // TOP panel start
            View topPanelLine = inflate.findViewById(R.id.topPanelLine);
            View topPanelLineDiv = inflate.findViewById(R.id.topPanelLineDiv);
            // topPanelLine.setVisibility(controller instanceof
            // DocumentControllerHorizontalView ? View.VISIBLE : View.GONE);
            topPanelLine.setVisibility(View.GONE);
            topPanelLineDiv.setVisibility(controller.isTextFormat() ? View.VISIBLE : View.GONE);
            inflate.findViewById(R.id.allBGConfig).setVisibility(Dips.isEInk(inflate.getContext()) ? View.GONE : View.VISIBLE);
            View onRecent = inflate.findViewById(R.id.onRecent);
            onRecent.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    closeDialog();
                    DragingDialogs.recentBooks(anchor, controller);
                }
            });
            final View onRotate = inflate.findViewById(R.id.onRotate);
            onRotate.setOnClickListener(new OnClickListener() {

                @SuppressLint("NewApi")
                @Override
                public void onClick(View v) {
                    if (Build.VERSION.SDK_INT <= 10) {
                        Toast.makeText(anchor.getContext(), R.string.this_function_will_works_in_modern_android, Toast.LENGTH_SHORT).show();
                        return;
                    }
                    // closeDialog();
                    MenuBuilderM.addRotateMenu(onRotate, null, updateUIRefresh).show();
                }
            });
            inflate.findViewById(R.id.onPageFlip).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(final View v) {
                    closeDialog();
                    DragingDialogs.pageFlippingDialog(anchor, controller, onRefresh);
                }
            });
            ImageView brightness = (ImageView) inflate.findViewById(R.id.onBrightness);
            brightness.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(final View v) {
                    AppState.get().isDayNotInvert = !AppState.get().isDayNotInvert;
                    controller.restartActivity();
                }
            });
            brightness.setImageResource(!AppState.get().isDayNotInvert ? R.drawable.glyphicons_232_sun : R.drawable.glyphicons_2_moon);
            final ImageView isCrop = (ImageView) inflate.findViewById(R.id.onCrop);
            // isCrop.setVisibility(controller.isTextFormat() ||
            // AppState.get().isCut ? View.GONE : View.VISIBLE);
            isCrop.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(final View v) {
                    AppState.get().isCrop = !AppState.get().isCrop;
                    SettingsManager.getBookSettings().updateFromAppState();
                    TintUtil.setTintImageWithAlpha(isCrop, !AppState.get().isCrop ? TintUtil.COLOR_TINT_GRAY : Color.LTGRAY);
                    updateUIRefresh.run();
                }
            });
            TintUtil.setTintImageWithAlpha(isCrop, !AppState.get().isCrop ? TintUtil.COLOR_TINT_GRAY : Color.LTGRAY);
            final ImageView bookCut = (ImageView) inflate.findViewById(R.id.bookCut);
            // bookCut.setVisibility(controller.isTextFormat() ? View.GONE :
            // View.VISIBLE);
            bookCut.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(final View v) {
                    closeDialog();
                    DragingDialogs.sliceDialog(anchor, controller, updateUIRefresh, new ResultResponse<Integer>() {

                        @Override
                        public boolean onResultRecive(Integer result) {
                            TintUtil.setTintImageWithAlpha(bookCut, !AppState.get().isCut ? TintUtil.COLOR_TINT_GRAY : Color.LTGRAY);
                            SettingsManager.getBookSettings().updateFromAppState();
                            EventBus.getDefault().post(new InvalidateMessage());
                            return false;
                        }
                    });
                }
            });
            TintUtil.setTintImageWithAlpha(bookCut, !AppState.get().isCut ? TintUtil.COLOR_TINT_GRAY : Color.LTGRAY);
            inflate.findViewById(R.id.onFullScreen).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(final View v) {
                    AppState.get().isFullScreen = !AppState.get().isFullScreen;
                    DocumentController.chooseFullScreen(controller.getActivity(), AppState.get().isFullScreen);
                    if (controller.isTextFormat()) {
                        if (onRefresh != null) {
                            onRefresh.run();
                        }
                        controller.restartActivity();
                    }
                }
            });
            View tts = inflate.findViewById(R.id.onTTS);
            tts.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(final View v) {
                    closeDialog();
                    DragingDialogs.textToSpeachDialog(anchor, controller);
                }
            });
            final ImageView pin = (ImageView) inflate.findViewById(R.id.onPin);
            pin.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(final View v) {
                    AppState.get().isShowToolBar = !AppState.get().isShowToolBar;
                    pin.setImageResource(AppState.get().isShowToolBar ? R.drawable.glyphicons_336_pushpin : R.drawable.glyphicons_200_ban);
                    if (onRefresh != null) {
                        onRefresh.run();
                    }
                }
            });
            pin.setImageResource(AppState.get().isShowToolBar ? R.drawable.glyphicons_336_pushpin : R.drawable.glyphicons_200_ban);
            // TOP panel end
            CheckBox isPreText = (CheckBox) inflate.findViewById(R.id.isPreText);
            isPreText.setChecked(AppState.get().isPreText);
            isPreText.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    AppState.get().isPreText = isChecked;
                }
            });
            isPreText.setVisibility(BookType.TXT.is(controller.getCurrentBook().getPath()) ? View.VISIBLE : View.GONE);
            CheckBox isLineBreaksText = (CheckBox) inflate.findViewById(R.id.isLineBreaksText);
            isLineBreaksText.setChecked(AppState.get().isLineBreaksText);
            isLineBreaksText.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    AppState.get().isLineBreaksText = isChecked;
                }
            });
            isLineBreaksText.setVisibility(BookType.TXT.is(controller.getCurrentBook().getPath()) ? View.VISIBLE : View.GONE);
            // 
            TextView moreSettings = (TextView) inflate.findViewById(R.id.moreSettings);
            moreSettings.setVisibility(controller.isTextFormat() ? View.VISIBLE : View.GONE);
            inflate.findViewById(R.id.moreSettingsDiv).setVisibility(controller.isTextFormat() ? View.VISIBLE : View.GONE);
            // View moreSettingsImage =
            // inflate.findViewById(R.id.moreSettingsImage);
            // moreSettingsImage.setVisibility(controller.isTextFormat() ?
            // View.VISIBLE : View.GONE);
            TxtUtils.underlineTextView(moreSettings).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    moreBookSettings(anchor, controller, onRefresh, updateUIRefresh);
                }
            });
            TextView performanceSettings = TxtUtils.underlineTextView((TextView) inflate.findViewById(R.id.performanceSettigns));
            performanceSettings.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    performanceSettings(anchor, controller, onRefresh, updateUIRefresh);
                }
            });
            TextView statusBarSettings = TxtUtils.underlineTextView((TextView) inflate.findViewById(R.id.statusBarSettings));
            statusBarSettings.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    statusBarSettings(anchor, controller, onRefresh, updateUIRefresh);
                }
            });
            final CustomSeek fontSizeSp = (CustomSeek) inflate.findViewById(R.id.fontSizeSp);
            fontSizeSp.init(10, 70, AppState.get().fontSizeSp);
            fontSizeSp.setOnSeekChanged(new IntegerResponse() {

                @Override
                public boolean onResultRecive(int result) {
                    AppState.get().fontSizeSp = result;
                    return false;
                }
            });
            fontSizeSp.setValueText("" + AppState.get().fontSizeSp);
            inflate.findViewById(R.id.fontSizeLayout).setVisibility(ExtUtils.isTextFomat(controller.getCurrentBook().getPath()) ? View.VISIBLE : View.GONE);
            inflate.findViewById(R.id.fontNameSelectionLayout).setVisibility(ExtUtils.isTextFomat(controller.getCurrentBook().getPath()) ? View.VISIBLE : View.GONE);
            final TextView textFontName = (TextView) inflate.findViewById(R.id.textFontName);
            textFontName.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    final List<FontPack> fontPacks = BookCSS.get().getAllFontsPacks();
                    MyPopupMenu popup = new MyPopupMenu(controller.getActivity(), v);
                    for (final FontPack pack : fontPacks) {
                        LOG.d("pack.normalFont", pack.normalFont);
                        popup.getMenu().add(pack.dispalyName, pack.normalFont).setOnMenuItemClickListener(new OnMenuItemClickListener() {

                            @Override
                            public boolean onMenuItemClick(MenuItem item) {
                                BookCSS.get().resetAll(pack);
                                TxtUtils.underline(textFontName, BookCSS.get().displayFontName);
                                return false;
                            }
                        });
                    }
                    popup.show();
                }
            });
            TxtUtils.underline(textFontName, BookCSS.get().displayFontName);
            final View moreFontSettings = inflate.findViewById(R.id.moreFontSettings);
            moreFontSettings.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    FontDialog.show(controller.getActivity(), new Runnable() {

                        @Override
                        public void run() {
                            TxtUtils.underline(textFontName, BookCSS.get().displayFontName);
                        }
                    });
                }
            });
            final View downloadFonts = inflate.findViewById(R.id.downloadFonts);
            downloadFonts.setVisibility(FontExtractor.hasZipFonts() ? View.GONE : View.VISIBLE);
            downloadFonts.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    FontExtractor.showDownloadFontsDialog(controller.getActivity(), downloadFonts, textFontName);
                }
            });
            // crop
            CheckBox isCropBorders = (CheckBox) inflate.findViewById(R.id.isCropBorders);
            isCropBorders.setChecked(controller.isCropCurrentBook());
            isCropBorders.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    controller.onCrop();
                }
            });
            // volume
            final CheckBox isReverseKyes = (CheckBox) inflate.findViewById(R.id.isReverseKyes);
            isReverseKyes.setChecked(AppState.get().isReverseKeys);
            isReverseKyes.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    AppState.get().isReverseKeys = isChecked;
                }
            });
            isReverseKyes.setEnabled(AppState.get().isUseVolumeKeys ? true : false);
            CheckBox isUseVolumeKeys = (CheckBox) inflate.findViewById(R.id.isUseVolumeKeys);
            isUseVolumeKeys.setChecked(AppState.get().isUseVolumeKeys);
            isUseVolumeKeys.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    AppState.get().isUseVolumeKeys = isChecked;
                    isReverseKyes.setEnabled(AppState.get().isUseVolumeKeys ? true : false);
                }
            });
            // orientation begin
            final TextView screenOrientation = (TextView) inflate.findViewById(R.id.screenOrientation);
            screenOrientation.setText(DocumentController.getRotationText());
            TxtUtils.underlineTextView(screenOrientation);
            screenOrientation.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    PopupMenu menu = new PopupMenu(v.getContext(), v);
                    for (int i = 0; i < DocumentController.orientationIds.size(); i++) {
                        final int j = i;
                        final int name = DocumentController.orientationTexts.get(i);
                        menu.getMenu().add(name).setOnMenuItemClickListener(new OnMenuItemClickListener() {

                            @Override
                            public boolean onMenuItemClick(MenuItem item) {
                                AppState.get().orientation = DocumentController.orientationIds.get(j);
                                screenOrientation.setText(DocumentController.orientationTexts.get(j));
                                TxtUtils.underlineTextView(screenOrientation);
                                DocumentController.doRotation(controller.getActivity());
                                return false;
                            }
                        });
                    }
                    menu.show();
                }
            });
            // orientation end
            BrightnessHelper.showBlueLigthDialogAndBrightness(controller.getActivity(), inflate, onRefresh);
            // brightness end
            // dicts
            final TextView selectedDictionaly = (TextView) inflate.findViewById(R.id.selectedDictionaly);
            selectedDictionaly.setText(DialogTranslateFromTo.getSelectedDictionaryUnderline());
            selectedDictionaly.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    DialogTranslateFromTo.show(controller.getActivity(), new Runnable() {

                        @Override
                        public void run() {
                            selectedDictionaly.setText(DialogTranslateFromTo.getSelectedDictionaryUnderline());
                        }
                    });
                }
            });
            ((CheckBox) inflate.findViewById(R.id.isRememberDictionary)).setChecked(AppState.get().isRememberDictionary);
            ((CheckBox) inflate.findViewById(R.id.isRememberDictionary)).setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
                    AppState.get().isRememberDictionary = isChecked;
                }
            });
            // Colors
            TextView textCustomizeFontBGColor = (TextView) inflate.findViewById(R.id.textCustomizeFontBGColor);
            if (AppState.get().isCustomizeBgAndColors || controller.isTextFormat()) {
                textCustomizeFontBGColor.setText(R.string.customize_font_background_colors);
            } else {
                textCustomizeFontBGColor.setText(R.string.customize_background_color);
            }
            final ImageView onDayColorImage = (ImageView) inflate.findViewById(R.id.onDayColorImage);
            final TextView textDayColor = TxtUtils.underlineTextView((TextView) inflate.findViewById(R.id.onDayColor));
            textDayColor.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    boolean isSolid = !AppState.get().isUseBGImageDay;
                    new ColorsDialog((FragmentActivity) controller.getActivity(), true, AppState.get().colorDayText, AppState.get().colorDayBg, false, isSolid, new ColorsDialogResult() {

                        @Override
                        public void onChooseColor(int colorText, int colorBg) {
                            textDayColor.setTextColor(colorText);
                            textDayColor.setBackgroundColor(colorBg);
                            TintUtil.setTintImageWithAlpha(onDayColorImage, colorText);
                            AppState.get().colorDayText = colorText;
                            AppState.get().colorDayBg = colorBg;
                            ImageLoader.getInstance().clearDiskCache();
                            ImageLoader.getInstance().clearMemoryCache();
                            if (AppState.get().isUseBGImageDay) {
                                textDayColor.setBackgroundDrawable(MagicHelper.getBgImageDayDrawable(true));
                            }
                        }
                    });
                }
            });
            final ImageView onNigthColorImage = (ImageView) inflate.findViewById(R.id.onNigthColorImage);
            final TextView textNigthColor = TxtUtils.underlineTextView((TextView) inflate.findViewById(R.id.onNigthColor));
            textNigthColor.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    boolean isSolid = !AppState.get().isUseBGImageNight;
                    new ColorsDialog((FragmentActivity) controller.getActivity(), false, AppState.get().colorNigthText, AppState.get().colorNigthBg, false, isSolid, new ColorsDialogResult() {

                        @Override
                        public void onChooseColor(int colorText, int colorBg) {
                            textNigthColor.setTextColor(colorText);
                            textNigthColor.setBackgroundColor(colorBg);
                            TintUtil.setTintImageWithAlpha(onNigthColorImage, colorText);
                            AppState.get().colorNigthText = colorText;
                            AppState.get().colorNigthBg = colorBg;
                            if (AppState.get().isUseBGImageNight) {
                                textNigthColor.setBackgroundDrawable(MagicHelper.getBgImageNightDrawable(true));
                            }
                        }
                    });
                }
            });
            final LinearLayout lc = (LinearLayout) inflate.findViewById(R.id.preColors);
            TintUtil.setTintImageWithAlpha(onDayColorImage, AppState.get().colorDayText);
            TintUtil.setTintImageWithAlpha(onNigthColorImage, AppState.get().colorNigthText);
            textNigthColor.setTextColor(AppState.get().colorNigthText);
            textNigthColor.setBackgroundColor(AppState.get().colorNigthBg);
            textDayColor.setTextColor(AppState.get().colorDayText);
            textDayColor.setBackgroundColor(AppState.get().colorDayBg);
            if (AppState.get().isUseBGImageDay) {
                textDayColor.setTextColor(Color.BLACK);
                textDayColor.setBackgroundDrawable(MagicHelper.getBgImageDayDrawable(true));
            }
            if (AppState.get().isUseBGImageNight) {
                textNigthColor.setTextColor(Color.WHITE);
                textNigthColor.setBackgroundDrawable(MagicHelper.getBgImageNightDrawable(true));
            }
            // lc.setVisibility(controller.isTextFormat() ||
            // AppState.get().isCustomizeBgAndColors ? View.VISIBLE :
            // View.GONE);
            final int padding = Dips.dpToPx(3);
            final Runnable colorsLine = new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    lc.removeAllViews();
                    for (String line : AppState.get().readColors.split(";")) {
                        if (TxtUtils.isEmpty(line)) {
                            continue;
                        }
                        String[] split = line.split(",");
                        LOG.d("Split colors", split[0], split[1], split[2]);
                        String name = split[0];
                        final int bg = Color.parseColor(split[1]);
                        final int text = Color.parseColor(split[2]);
                        final boolean isDay = split[3].equals("0");
                        BorderTextView t1 = new BorderTextView(controller.getActivity());
                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(Dips.dpToPx(30), Dips.dpToPx(30));
                        params.setMargins(padding, padding, padding, padding);
                        t1.setLayoutParams(params);
                        t1.setGravity(Gravity.CENTER);
                        t1.setBackgroundColor(bg);
                        if (controller.isTextFormat() || AppState.get().isCustomizeBgAndColors) {
                            t1.setText(name);
                            t1.setTextColor(text);
                            t1.setTypeface(null, Typeface.BOLD);
                        }
                        t1.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                if (isDay) {
                                    if (controller.isTextFormat() || AppState.get().isCustomizeBgAndColors) {
                                        AppState.get().colorDayText = text;
                                        textDayColor.setTextColor(text);
                                    }
                                    AppState.get().colorDayBg = bg;
                                    textDayColor.setBackgroundColor(bg);
                                    AppState.get().isUseBGImageDay = false;
                                } else {
                                    if (controller.isTextFormat() || AppState.get().isCustomizeBgAndColors) {
                                        AppState.get().colorNigthText = text;
                                        textNigthColor.setTextColor(text);
                                    }
                                    AppState.get().colorNigthBg = bg;
                                    textNigthColor.setBackgroundColor(bg);
                                    AppState.get().isUseBGImageNight = false;
                                }
                                TintUtil.setTintImageWithAlpha(onDayColorImage, AppState.get().colorDayText);
                                TintUtil.setTintImageWithAlpha(onNigthColorImage, AppState.get().colorNigthText);
                            }
                        });
                        lc.addView(t1);
                    }
                    // add DayBG
                    {
                        ImageView t1 = new ImageView(controller.getActivity());
                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(Dips.dpToPx(30), Dips.dpToPx(30));
                        params.setMargins(padding, padding, padding, padding);
                        t1.setLayoutParams(params);
                        t1.setScaleType(ScaleType.FIT_XY);
                        t1.setImageDrawable(MagicHelper.getBgImageDayDrawable(false));
                        t1.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                AppState.get().colorDayText = AppState.COLOR_BLACK;
                                AppState.get().colorDayBg = AppState.COLOR_WHITE;
                                textDayColor.setTextColor(Color.BLACK);
                                textDayColor.setBackgroundDrawable(MagicHelper.getBgImageDayDrawable(false));
                                AppState.get().isUseBGImageDay = true;
                                TintUtil.setTintImageWithAlpha(onDayColorImage, AppState.get().colorDayText);
                            }
                        });
                        lc.addView(t1, AppState.get().readColors.split(";").length / 2);
                    }
                    // add Night
                    {
                        ImageView t2 = new ImageView(controller.getActivity());
                        LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(Dips.dpToPx(30), Dips.dpToPx(30));
                        params2.setMargins(padding, padding, padding, padding);
                        t2.setLayoutParams(params2);
                        t2.setScaleType(ScaleType.FIT_XY);
                        t2.setImageDrawable(MagicHelper.getBgImageNightDrawable(false));
                        t2.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                AppState.get().colorNigthText = AppState.COLOR_WHITE;
                                AppState.get().colorNigthBg = AppState.COLOR_BLACK;
                                textNigthColor.setTextColor(Color.WHITE);
                                textNigthColor.setBackgroundDrawable(MagicHelper.getBgImageNightDrawable(false));
                                AppState.get().isUseBGImageNight = true;
                                TintUtil.setTintImageWithAlpha(onNigthColorImage, AppState.get().colorNigthText);
                            }
                        });
                        lc.addView(t2);
                    }
                }
            };
            colorsLine.run();
            TxtUtils.underlineTextView((TextView) inflate.findViewById(R.id.onDefaultColor)).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    AlertDialogs.showOkDialog(controller.getActivity(), controller.getString(R.string.restore_defaults_full), new Runnable() {

                        @Override
                        public void run() {
                            AppState.get().readColors = AppState.READ_COLORS_DEAFAUL;
                            AppState.get().isUseBGImageDay = false;
                            AppState.get().isUseBGImageNight = false;
                            AppState.get().bgImageDayTransparency = AppState.DAY_TRANSPARENCY;
                            AppState.get().bgImageDayPath = MagicHelper.IMAGE_BG_1;
                            AppState.get().bgImageNightTransparency = AppState.NIGHT_TRANSPARENCY;
                            AppState.get().bgImageNightPath = MagicHelper.IMAGE_BG_1;
                            AppState.get().isCustomizeBgAndColors = false;
                            AppState.get().colorDayText = AppState.COLOR_BLACK;
                            AppState.get().colorDayBg = AppState.COLOR_WHITE;
                            textDayColor.setTextColor(AppState.COLOR_BLACK);
                            textDayColor.setBackgroundColor(AppState.COLOR_WHITE);
                            AppState.get().colorNigthText = AppState.COLOR_WHITE;
                            AppState.get().colorNigthBg = AppState.COLOR_BLACK;
                            textNigthColor.setTextColor(AppState.COLOR_WHITE);
                            textNigthColor.setBackgroundColor(AppState.COLOR_BLACK);
                            TintUtil.setTintImageWithAlpha(onDayColorImage, AppState.get().colorDayText);
                            TintUtil.setTintImageWithAlpha(onNigthColorImage, AppState.get().colorNigthText);
                            AppState.get().statusBarColorDay = AppState.TEXT_COLOR_DAY;
                            AppState.get().statusBarColorNight = AppState.TEXT_COLOR_NIGHT;
                            colorsLine.run();
                        }
                    });
                }
            });
            inflate.findViewById(R.id.moreReadColorSettings).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    final AlertDialog.Builder builder = new AlertDialog.Builder(controller.getActivity());
                    builder.setTitle(R.string.customize);
                    final LinearLayout root = new LinearLayout(controller.getActivity());
                    root.setOrientation(LinearLayout.VERTICAL);
                    for (String line : AppState.get().readColors.split(";")) {
                        if (TxtUtils.isEmpty(line)) {
                            continue;
                        }
                        final String[] split = line.split(",");
                        LOG.d("Split colors", split[0], split[1], split[2]);
                        final String name = split[0];
                        final int bg = Color.parseColor(split[1]);
                        final int text = Color.parseColor(split[2]);
                        final boolean isDay = split[3].equals("0");
                        final LinearLayout child = new LinearLayout(controller.getActivity());
                        child.setOrientation(LinearLayout.HORIZONTAL);
                        child.setTag(line);
                        final TextView t1Img = new TextView(controller.getActivity());
                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(Dips.dpToPx(60), Dips.dpToPx(30));
                        params.setMargins(padding, padding, padding, padding);
                        t1Img.setLayoutParams(params);
                        t1Img.setGravity(Gravity.CENTER);
                        t1Img.setBackgroundColor(bg);
                        t1Img.setText(name);
                        t1Img.setTextColor(text);
                        t1Img.setTypeface(null, Typeface.BOLD);
                        t1Img.setTag(isDay);
                        TextView t0 = new TextView(controller.getActivity());
                        t0.setEms(1);
                        TextView t00 = new TextView(controller.getActivity());
                        t00.setEms(2);
                        final TextView t2BG = new TextView(controller.getActivity());
                        t2BG.setText(TxtUtils.underline(split[1]));
                        t2BG.setEms(5);
                        t2BG.setTag(bg);
                        final TextView t3Text = new TextView(controller.getActivity());
                        t3Text.setText(TxtUtils.underline(split[2]));
                        t3Text.setEms(5);
                        t3Text.setTag(text);
                        child.addView(t0);
                        child.addView(t1Img);
                        child.addView(t00);
                        child.addView(t2BG);
                        child.addView(t3Text);
                        child.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                new ColorsDialog((FragmentActivity) controller.getActivity(), (Boolean) t1Img.getTag(), (Integer) t3Text.getTag(), (Integer) t2BG.getTag(), true, true, new ColorsDialogResult() {

                                    @Override
                                    public void onChooseColor(int colorText, int colorBg) {
                                        t1Img.setTextColor(colorText);
                                        t1Img.setBackgroundColor(colorBg);
                                        t2BG.setText(TxtUtils.underline(MagicHelper.colorToString(colorBg)));
                                        t3Text.setText(TxtUtils.underline(MagicHelper.colorToString(colorText)));
                                        t2BG.setTag(colorBg);
                                        t3Text.setTag(colorText);
                                        String line = name + "," + MagicHelper.colorToString(colorBg) + "," + MagicHelper.colorToString(colorText) + "," + split[3];
                                        child.setTag(line);
                                    }
                                });
                            }
                        });
                        root.addView(child);
                    }
                    builder.setView(root);
                    builder.setNegativeButton(R.string.apply, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String res = "";
                            for (int i = 0; i < root.getChildCount(); i++) {
                                View childAt = root.getChildAt(i);
                                String line = (String) childAt.getTag();
                                res = res + line + ";";
                            }
                            AppState.get().readColors = res;
                            LOG.d("SAVE readColors", AppState.get().readColors);
                            colorsLine.run();
                        }
                    });
                    builder.setPositiveButton(R.string.cancel, new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    builder.show();
                }
            });
            return inflate;
        }
    }.show(DragingPopup.PREF + "_preferences").setOnCloseListener(new Runnable() {

        @Override
        public void run() {
            if (// 
            // 
            appHash != Objects.hashCode(AppState.get()) || (controller.isTextFormat() && cssHash != BookCSS.get().toCssString().hashCode())) {
                if (onRefresh != null) {
                    onRefresh.run();
                }
                controller.restartActivity();
            }
        }
    });
    return dialog;
}
Also used : ResultResponse(com.foobnix.android.utils.ResultResponse) AlertDialog(android.app.AlertDialog) DialogInterface(android.content.DialogInterface) OnMenuItemClickListener(android.view.MenuItem.OnMenuItemClickListener) FontPack(com.foobnix.pdf.info.model.BookCSS.FontPack) TextView(android.widget.TextView) ArrayList(java.util.ArrayList) List(java.util.List) ImageView(android.widget.ImageView) OnCheckedChangeListener(android.widget.CompoundButton.OnCheckedChangeListener) IntegerResponse(com.foobnix.android.utils.IntegerResponse) OnClickListener(android.view.View.OnClickListener) MenuItem(android.view.MenuItem) ImageView(android.widget.ImageView) RecyclerView(android.support.v7.widget.RecyclerView) GridView(android.widget.GridView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) AbsListView(android.widget.AbsListView) SuppressLint(android.annotation.SuppressLint) FragmentActivity(android.support.v4.app.FragmentActivity) CheckBox(android.widget.CheckBox) LayoutInflater(android.view.LayoutInflater) OnClickListener(android.view.View.OnClickListener) SuppressLint(android.annotation.SuppressLint) ColorsDialog(com.foobnix.pdf.info.widget.ColorsDialog) ColorsDialogResult(com.foobnix.pdf.info.widget.ColorsDialog.ColorsDialogResult) CompoundButton(android.widget.CompoundButton) LinearLayout(android.widget.LinearLayout) InvalidateMessage(com.foobnix.pdf.search.activity.msg.InvalidateMessage) PopupMenu(android.widget.PopupMenu)

Example 70 with PopupMenu

use of android.support.v7.widget.PopupMenu in project LibreraReader by foobnix.

the class DragingDialogs method selectTextMenu.

public static DragingPopup selectTextMenu(final FrameLayout anchor, final DocumentController controller, final boolean withAnnotation, final Runnable reloadUI) {
    return new DragingPopup(R.string.text, anchor, 300, 400) {

        @Override
        public View getContentView(LayoutInflater inflater) {
            final View view = inflater.inflate(R.layout.dialog_selected_text, null, false);
            final LinearLayout linearLayoutColor = (LinearLayout) view.findViewById(R.id.colorsLine);
            linearLayoutColor.removeAllViews();
            List<String> colors = new ArrayList<String>(AppState.get().COLORS);
            colors.remove(0);
            colors.remove(0);
            final ImageView underLine = (ImageView) view.findViewById(R.id.onUnderline);
            final ImageView strike = (ImageView) view.findViewById(R.id.onStrike);
            final ImageView selection = (ImageView) view.findViewById(R.id.onSelection);
            final ImageView onAddCustom = (ImageView) view.findViewById(R.id.onAddCustom);
            final LinearLayout customsLayout = (LinearLayout) view.findViewById(R.id.customsLayout);
            final Runnable updateConfigRunnable = new Runnable() {

                @Override
                public void run() {
                    customsLayout.removeAllViews();
                    for (final String line : AppState.get().customConfigColors.split(",")) {
                        if (TxtUtils.isEmpty(line)) {
                            continue;
                        }
                        final ImageView image = new ImageView(controller.getActivity());
                        if (line.startsWith("H")) {
                            image.setImageResource(R.drawable.glyphicons_607_te_background);
                        } else if (line.startsWith("U")) {
                            image.setImageResource(R.drawable.glyphicons_104_te_underline);
                        } else if (line.startsWith("S")) {
                            image.setImageResource(R.drawable.glyphicons_105_te_strike);
                        }
                        String color = line.substring(1);
                        final int colorInt = Color.parseColor(color);
                        TintUtil.setTintImageWithAlpha(image, colorInt);
                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(Dips.dpToPx(35), Dips.dpToPx(35));
                        int pd = Dips.dpToPx(5);
                        params.leftMargin = pd;
                        image.setPadding(pd, pd, pd, pd);
                        customsLayout.addView(image, params);
                        image.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                if (line.startsWith("H")) {
                                    controller.underlineText(colorInt, 2.0f, AnnotationType.HIGHLIGHT);
                                } else if (line.startsWith("U")) {
                                    controller.underlineText(colorInt, 2.0f, AnnotationType.UNDERLINE);
                                } else if (line.startsWith("S")) {
                                    controller.underlineText(colorInt, 2.0f, AnnotationType.STRIKEOUT);
                                }
                                closeDialog();
                                controller.saveAnnotationsToFile();
                            }
                        });
                        image.setOnLongClickListener(new OnLongClickListener() {

                            @Override
                            public boolean onLongClick(View v) {
                                AppState.get().customConfigColors = AppState.get().customConfigColors.replace(line, "");
                                customsLayout.removeView(image);
                                return true;
                            }
                        });
                    }
                }
            };
            updateConfigRunnable.run();
            onAddCustom.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    PopupMenu menu = new PopupMenu(v.getContext(), v);
                    Drawable highlight = controller.getActivity().getResources().getDrawable(R.drawable.glyphicons_607_te_background);
                    highlight.setColorFilter(Color.parseColor(AppState.get().annotationTextColor), Mode.SRC_ATOP);
                    Drawable underline = controller.getActivity().getResources().getDrawable(R.drawable.glyphicons_104_te_underline);
                    underline.setColorFilter(Color.parseColor(AppState.get().annotationTextColor), Mode.SRC_ATOP);
                    Drawable strikeout = controller.getActivity().getResources().getDrawable(R.drawable.glyphicons_105_te_strike);
                    strikeout.setColorFilter(Color.parseColor(AppState.get().annotationTextColor), Mode.SRC_ATOP);
                    menu.getMenu().add(R.string.highlight_of_text).setIcon(highlight).setOnMenuItemClickListener(new OnMenuItemClickListener() {

                        @Override
                        public boolean onMenuItemClick(MenuItem item) {
                            AppState.get().customConfigColors += "H" + AppState.get().annotationTextColor + ",";
                            updateConfigRunnable.run();
                            return false;
                        }
                    });
                    menu.getMenu().add(R.string.underline_of_text).setIcon(underline).setOnMenuItemClickListener(new OnMenuItemClickListener() {

                        @Override
                        public boolean onMenuItemClick(MenuItem item) {
                            AppState.get().customConfigColors += "U" + AppState.get().annotationTextColor + ",";
                            updateConfigRunnable.run();
                            return false;
                        }
                    });
                    menu.getMenu().add(R.string.strikethrough_of_text).setIcon(strikeout).setOnMenuItemClickListener(new OnMenuItemClickListener() {

                        @Override
                        public boolean onMenuItemClick(MenuItem item) {
                            AppState.get().customConfigColors += "S" + AppState.get().annotationTextColor + ",";
                            updateConfigRunnable.run();
                            return false;
                        }
                    });
                    menu.show();
                    PopupHelper.initIcons(menu, Color.parseColor(AppState.get().annotationTextColor));
                }
            });
            underLine.setColorFilter(Color.parseColor(AppState.get().annotationTextColor));
            strike.setColorFilter(Color.parseColor(AppState.get().annotationTextColor));
            selection.setColorFilter(Color.parseColor(AppState.get().annotationTextColor));
            for (final String colorName : colors) {
                final View inflate = LayoutInflater.from(linearLayoutColor.getContext()).inflate(R.layout.item_color, linearLayoutColor, false);
                inflate.setBackgroundResource(R.drawable.bg_border_2_lines);
                final View img = inflate.findViewById(R.id.itColor);
                final int colorId = Color.parseColor(colorName);
                img.setBackgroundColor(colorId);
                inflate.setTag(colorName);
                linearLayoutColor.addView(inflate);
                inflate.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // Views.unselectChilds(linearLayoutColor);
                        // v.setSelected(true);
                        AppState.get().annotationTextColor = colorName;
                        underLine.setColorFilter(Color.parseColor(colorName));
                        strike.setColorFilter(Color.parseColor(colorName));
                        selection.setColorFilter(Color.parseColor(colorName));
                    }
                });
            }
            final EditText editText = (EditText) view.findViewById(R.id.editText);
            final String selectedText = AppState.get().selectedText;
            // AppState.get().selectedText = null;
            editText.setText(selectedText);
            final View onTranslate = view.findViewById(R.id.onTranslate);
            onTranslate.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    anchor.removeAllViews();
                    final PopupMenu popupMenu = new PopupMenu(v.getContext(), v);
                    final Map<String, String> providers = AppState.getDictionaries(editText.getText().toString().trim());
                    for (final String name : providers.keySet()) {
                        popupMenu.getMenu().add(name).setOnMenuItemClickListener(new OnMenuItemClickListener() {

                            @Override
                            public boolean onMenuItemClick(MenuItem item) {
                                Urls.open(anchor.getContext(), providers.get(name).trim());
                                return false;
                            }
                        });
                    }
                    popupMenu.show();
                }
            });
            view.findViewById(R.id.onAddToBookmark).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    closeDialog();
                    ListBoxHelper.showAddDialog(controller, null, null, editText.getText().toString().trim());
                }
            });
            view.findViewById(R.id.readTTS).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    TTSEngine.get().stop();
                    TTSEngine.get().speek(editText.getText().toString().trim());
                }
            });
            view.findViewById(R.id.readTTSNext).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    TTSEngine.get().stop();
                    TTSService.playBookPage(controller.getCurentPageFirst1() - 1, controller.getCurrentBook().getPath(), editText.getText().toString().trim(), controller.getBookWidth(), controller.getBookHeight(), AppState.get().fontSizeSp);
                }
            });
            view.findViewById(R.id.onShare).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    closeDialog();
                    final Intent intent = new Intent(Intent.ACTION_SEND);
                    intent.setType("text/plain");
                    String txt = "\"" + editText.getText().toString().trim() + "\" (" + controller.getBookFileMetaName() + ")";
                    intent.putExtra(Intent.EXTRA_TEXT, txt);
                    controller.getActivity().startActivity(Intent.createChooser(intent, controller.getString(R.string.share)));
                }
            });
            view.findViewById(R.id.onCopy).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Context c = anchor.getContext();
                    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {
                        android.text.ClipboardManager clipboard = (android.text.ClipboardManager) c.getSystemService(Context.CLIPBOARD_SERVICE);
                        clipboard.setText(editText.getText().toString().trim());
                    } else {
                        android.content.ClipboardManager clipboard = (android.content.ClipboardManager) c.getSystemService(Context.CLIPBOARD_SERVICE);
                        android.content.ClipData clip = android.content.ClipData.newPlainText("Copied Text", editText.getText().toString().trim());
                        clipboard.setPrimaryClip(clip);
                    }
                    closeDialog();
                }
            });
            view.findViewById(R.id.onGoogle).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    closeDialog();
                    Urls.open(anchor.getContext(), "http://www.google.com/search?q=" + editText.getText().toString().trim());
                }
            });
            TextView onBookSearch = (TextView) view.findViewById(R.id.onBookSearch);
            // onBookSearch.setText(controller.getString(R.string.search_in_the_book)
            // + " \"" + AppState.get().selectedText + "\"");
            onBookSearch.setVisibility(selectedText.contains(" ") ? View.GONE : View.VISIBLE);
            onBookSearch.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    searchMenu(anchor, controller, selectedText);
                }
            });
            LinearLayout dictLayout = (LinearLayout) view.findViewById(R.id.dictionaryLine);
            dictLayout.removeAllViews();
            final Intent intentProccessText = new Intent();
            if (Build.VERSION.SDK_INT >= 23) {
                intentProccessText.setAction(Intent.ACTION_PROCESS_TEXT);
            }
            intentProccessText.setType("text/plain");
            final Intent intentSearch = new Intent();
            intentSearch.setAction(Intent.ACTION_SEARCH);
            final Intent intentSend = new Intent();
            intentSend.setAction(Intent.ACTION_SEND);
            intentSend.setType("text/plain");
            final Intent intentCustom = new Intent("colordict.intent.action.SEARCH");
            PackageManager pm = anchor.getContext().getPackageManager();
            final List<ResolveInfo> proccessTextList = pm.queryIntentActivities(intentProccessText, 0);
            final List<ResolveInfo> searchList = pm.queryIntentActivities(intentSearch, 0);
            final List<ResolveInfo> sendList = pm.queryIntentActivities(intentSend, 0);
            final List<ResolveInfo> customList = pm.queryIntentActivities(intentCustom, 0);
            final List<ResolveInfo> all = new ArrayList<ResolveInfo>();
            if (Build.VERSION.SDK_INT >= 23) {
                all.addAll(proccessTextList);
            }
            all.addAll(customList);
            all.addAll(searchList);
            all.addAll(sendList);
            final SharedPreferences sp = anchor.getContext().getSharedPreferences("lastDict", Context.MODE_PRIVATE);
            final String lastID = sp.getString("last", "");
            List<String> cache = new ArrayList<String>();
            for (final ResolveInfo app : all) {
                for (final String pkgKey : AppState.appDictionariesKeys) {
                    if (app.activityInfo.packageName.toLowerCase().contains(pkgKey)) {
                        if (cache.contains(app.activityInfo.name)) {
                            continue;
                        }
                        cache.add(app.activityInfo.name);
                        LOG.d("Add APP", app.activityInfo.name);
                        try {
                            ImageView image = new ImageView(anchor.getContext());
                            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Dips.dpToPx(44), Dips.dpToPx(44));
                            layoutParams.rightMargin = Dips.dpToPx(8);
                            image.setLayoutParams(layoutParams);
                            Drawable icon = anchor.getContext().getPackageManager().getApplicationIcon(app.activityInfo.packageName);
                            image.setImageDrawable(icon);
                            image.setOnClickListener(new OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    String selecteText = editText.getText().toString().trim();
                                    closeDialog();
                                    final ActivityInfo activity = app.activityInfo;
                                    final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
                                    if (customList.contains(app)) {
                                        intentCustom.addCategory(Intent.CATEGORY_LAUNCHER);
                                        intentCustom.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                                        intentCustom.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                                        intentCustom.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                                        intentCustom.setComponent(name);
                                        intentCustom.putExtra("EXTRA_QUERY", selecteText);
                                        intentCustom.putExtra("EXTRA_HEIGHT", Dips.screenHeight() / 2);
                                        if (AppState.get().isDouble || Dips.screenWidth() > Dips.screenHeight()) {
                                            intentCustom.putExtra("EXTRA_HEIGHT", Dips.screenHeight() * 2 / 3);
                                            if (TempHolder.get().textFromPage == 1) {
                                                intentCustom.putExtra("EXTRA_GRAVITY", Gravity.BOTTOM | Gravity.RIGHT);
                                            } else if (TempHolder.get().textFromPage == 2) {
                                                intentCustom.putExtra("EXTRA_GRAVITY", Gravity.BOTTOM | Gravity.LEFT);
                                            } else {
                                                intentCustom.putExtra("EXTRA_GRAVITY", Gravity.BOTTOM | Gravity.CENTER);
                                            }
                                            intentCustom.putExtra("EXTRA_WIDTH", Dips.screenWidth() / 2);
                                        } else {
                                            intentCustom.putExtra("EXTRA_GRAVITY", Gravity.BOTTOM);
                                        }
                                        controller.getActivity().startActivity(intentCustom);
                                    } else if (proccessTextList.contains(app)) {
                                        intentProccessText.addCategory(Intent.CATEGORY_LAUNCHER);
                                        intentProccessText.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                                        intentProccessText.setComponent(name);
                                        intentProccessText.putExtra(Intent.EXTRA_TEXT, selecteText);
                                        intentProccessText.putExtra(Intent.EXTRA_PROCESS_TEXT, selecteText);
                                        intentProccessText.putExtra(Intent.EXTRA_PROCESS_TEXT_READONLY, selecteText);
                                        controller.getActivity().startActivity(intentProccessText);
                                    } else if (searchList.contains(app)) {
                                        intentSearch.addCategory(Intent.CATEGORY_LAUNCHER);
                                        intentSearch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                                        intentSearch.setComponent(name);
                                        intentSearch.putExtra(SearchManager.QUERY, selecteText);
                                        intentSearch.putExtra(Intent.EXTRA_TEXT, selecteText);
                                        controller.getActivity().startActivity(intentSearch);
                                    } else if (sendList.contains(app)) {
                                        intentSend.addCategory(Intent.CATEGORY_LAUNCHER);
                                        intentSend.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                                        intentSend.setComponent(name);
                                        intentSend.putExtra(Intent.EXTRA_TEXT, selecteText);
                                        controller.getActivity().startActivity(intentSend);
                                    }
                                    sp.edit().putString("last", app.activityInfo.name).commit();
                                }
                            });
                            if (app.activityInfo.name.equals(lastID)) {
                                dictLayout.addView(image, 0);
                            } else {
                                dictLayout.addView(image);
                            }
                        } catch (PackageManager.NameNotFoundException e) {
                            LOG.d(e);
                        }
                    }
                }
            }
            view.findViewById(R.id.onUnderline).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    controller.underlineText(Color.parseColor(AppState.get().annotationTextColor), 2.0f, AnnotationType.UNDERLINE);
                    closeDialog();
                    controller.saveAnnotationsToFile();
                }
            });
            view.findViewById(R.id.onStrike).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    controller.underlineText(Color.parseColor(AppState.get().annotationTextColor), 2.0f, AnnotationType.STRIKEOUT);
                    closeDialog();
                    controller.saveAnnotationsToFile();
                }
            });
            view.findViewById(R.id.onSelection).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    controller.underlineText(Color.parseColor(AppState.get().annotationTextColor), 2.0f, AnnotationType.HIGHLIGHT);
                    closeDialog();
                    controller.saveAnnotationsToFile();
                }
            });
            if (!BookType.PDF.is(controller.getCurrentBook().getPath()) || !withAnnotation || controller.getActivity() instanceof HorizontalViewActivity || controller.isPasswordProtected()) {
                linearLayoutColor.setVisibility(View.GONE);
                view.findViewById(R.id.onUnderline).setVisibility(View.GONE);
                view.findViewById(R.id.onStrike).setVisibility(View.GONE);
                view.findViewById(R.id.onSelection).setVisibility(View.GONE);
                onAddCustom.setVisibility(View.GONE);
                customsLayout.setVisibility(View.GONE);
            }
            return view;
        }
    }.show("text", true).setOnCloseListener(new Runnable() {

        @Override
        public void run() {
            // controller.clearSelectedText();
            AppState.get().selectedText = null;
        }
    });
}
Also used : OnMenuItemClickListener(android.view.MenuItem.OnMenuItemClickListener) ArrayList(java.util.ArrayList) ResolveInfo(android.content.pm.ResolveInfo) PackageManager(android.content.pm.PackageManager) OnLongClickListener(android.view.View.OnLongClickListener) TextView(android.widget.TextView) ComponentName(android.content.ComponentName) ImageView(android.widget.ImageView) Context(android.content.Context) HorizontalViewActivity(com.foobnix.pdf.search.activity.HorizontalViewActivity) EditText(android.widget.EditText) Context(android.content.Context) ActivityInfo(android.content.pm.ActivityInfo) AppSharedPreferences(com.foobnix.pdf.info.AppSharedPreferences) SharedPreferences(android.content.SharedPreferences) ColorDrawable(android.graphics.drawable.ColorDrawable) Drawable(android.graphics.drawable.Drawable) MenuItem(android.view.MenuItem) OnClickListener(android.view.View.OnClickListener) Intent(android.content.Intent) ImageView(android.widget.ImageView) RecyclerView(android.support.v7.widget.RecyclerView) GridView(android.widget.GridView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) AbsListView(android.widget.AbsListView) SuppressLint(android.annotation.SuppressLint) LayoutInflater(android.view.LayoutInflater) OnClickListener(android.view.View.OnClickListener) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) LinearLayout(android.widget.LinearLayout) PopupMenu(android.widget.PopupMenu)

Aggregations

PopupMenu (android.support.v7.widget.PopupMenu)142 MenuItem (android.view.MenuItem)116 View (android.view.View)86 TextView (android.widget.TextView)63 RecyclerView (android.support.v7.widget.RecyclerView)54 ImageView (android.widget.ImageView)40 Intent (android.content.Intent)32 PopupMenu (android.widget.PopupMenu)31 List (java.util.List)27 ArrayList (java.util.ArrayList)26 MenuInflater (android.view.MenuInflater)23 Menu (android.view.Menu)22 Bundle (android.os.Bundle)20 LayoutInflater (android.view.LayoutInflater)20 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)20 Context (android.content.Context)18 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)18 AdapterView (android.widget.AdapterView)17 DialogInterface (android.content.DialogInterface)16 ViewGroup (android.view.ViewGroup)15