Search in sources :

Example 1 with AutoTextSnippet

use of org.geometerplus.fbreader.util.AutoTextSnippet in project FBReaderJ by geometer.

the class ProcessHyperlinkAction method run.

@Override
protected void run(Object... params) {
    final ZLTextRegion region = Reader.getTextView().getOutlinedRegion();
    if (region == null) {
        return;
    }
    final ZLTextRegion.Soul soul = region.getSoul();
    if (soul instanceof ZLTextHyperlinkRegionSoul) {
        Reader.getTextView().hideOutline();
        Reader.getViewWidget().repaint();
        final ZLTextHyperlink hyperlink = ((ZLTextHyperlinkRegionSoul) soul).Hyperlink;
        switch(hyperlink.Type) {
            case FBHyperlinkType.EXTERNAL:
                openInBrowser(hyperlink.Id);
                break;
            case FBHyperlinkType.INTERNAL:
            case FBHyperlinkType.FOOTNOTE:
                {
                    final AutoTextSnippet snippet = Reader.getFootnoteData(hyperlink.Id);
                    if (snippet == null) {
                        break;
                    }
                    Reader.Collection.markHyperlinkAsVisited(Reader.getCurrentBook(), hyperlink.Id);
                    final boolean showToast;
                    switch(Reader.MiscOptions.ShowFootnoteToast.getValue()) {
                        default:
                        case never:
                            showToast = false;
                            break;
                        case footnotesOnly:
                            showToast = hyperlink.Type == FBHyperlinkType.FOOTNOTE;
                            break;
                        case footnotesAndSuperscripts:
                            showToast = hyperlink.Type == FBHyperlinkType.FOOTNOTE || region.isVerticallyAligned();
                            break;
                        case allInternalLinks:
                            showToast = true;
                            break;
                    }
                    if (showToast) {
                        final SuperActivityToast toast;
                        if (snippet.IsEndOfText) {
                            toast = new SuperActivityToast(BaseActivity, SuperToast.Type.STANDARD);
                        } else {
                            toast = new SuperActivityToast(BaseActivity, SuperToast.Type.BUTTON);
                            toast.setButtonIcon(android.R.drawable.ic_menu_more, ZLResource.resource("toast").getResource("more").getValue());
                            toast.setOnClickWrapper(new OnClickWrapper("ftnt", new SuperToast.OnClickListener() {

                                @Override
                                public void onClick(View view, Parcelable token) {
                                    Reader.getTextView().hideOutline();
                                    Reader.tryOpenFootnote(hyperlink.Id);
                                }
                            }));
                        }
                        toast.setText(snippet.getText());
                        toast.setDuration(Reader.MiscOptions.FootnoteToastDuration.getValue().Value);
                        toast.setOnDismissWrapper(new OnDismissWrapper("ftnt", new SuperToast.OnDismissListener() {

                            @Override
                            public void onDismiss(View view) {
                                Reader.getTextView().hideOutline();
                                Reader.getViewWidget().repaint();
                            }
                        }));
                        Reader.getTextView().outlineRegion(region);
                        BaseActivity.showToast(toast);
                    } else {
                        Reader.tryOpenFootnote(hyperlink.Id);
                    }
                    break;
                }
        }
    } else if (soul instanceof ZLTextImageRegionSoul) {
        Reader.getTextView().hideOutline();
        Reader.getViewWidget().repaint();
        final String url = ((ZLTextImageRegionSoul) soul).ImageElement.URL;
        if (url != null) {
            try {
                final Intent intent = new Intent();
                intent.setClass(BaseActivity, ImageViewActivity.class);
                intent.putExtra(ImageViewActivity.URL_KEY, url);
                intent.putExtra(ImageViewActivity.BACKGROUND_COLOR_KEY, Reader.ImageOptions.ImageViewBackground.getValue().intValue());
                OrientationUtil.startActivity(BaseActivity, intent);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    } else if (soul instanceof ZLTextWordRegionSoul) {
        DictionaryUtil.openTextInDictionary(BaseActivity, ((ZLTextWordRegionSoul) soul).Word.getString(), true, region.getTop(), region.getBottom(), new Runnable() {

            public void run() {
                BaseActivity.outlineRegion(soul);
            }
        });
    }
}
Also used : SuperToast(com.github.johnpersano.supertoasts.SuperToast) ImageViewActivity(org.geometerplus.android.fbreader.image.ImageViewActivity) SuperActivityToast(com.github.johnpersano.supertoasts.SuperActivityToast) Parcelable(android.os.Parcelable) Intent(android.content.Intent) View(android.view.View) AutoTextSnippet(org.geometerplus.fbreader.util.AutoTextSnippet) ZLNetworkException(org.geometerplus.zlibrary.core.network.ZLNetworkException) ActivityNotFoundException(android.content.ActivityNotFoundException) OnClickWrapper(com.github.johnpersano.supertoasts.util.OnClickWrapper) OnDismissWrapper(com.github.johnpersano.supertoasts.util.OnDismissWrapper)

Aggregations

ActivityNotFoundException (android.content.ActivityNotFoundException)1 Intent (android.content.Intent)1 Parcelable (android.os.Parcelable)1 View (android.view.View)1 SuperActivityToast (com.github.johnpersano.supertoasts.SuperActivityToast)1 SuperToast (com.github.johnpersano.supertoasts.SuperToast)1 OnClickWrapper (com.github.johnpersano.supertoasts.util.OnClickWrapper)1 OnDismissWrapper (com.github.johnpersano.supertoasts.util.OnDismissWrapper)1 ImageViewActivity (org.geometerplus.android.fbreader.image.ImageViewActivity)1 AutoTextSnippet (org.geometerplus.fbreader.util.AutoTextSnippet)1 ZLNetworkException (org.geometerplus.zlibrary.core.network.ZLNetworkException)1