mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-05 10:49:24 +02:00
121 lines
4 KiB
Java
121 lines
4 KiB
Java
/*
|
|
* Copyright (C) 2010-2013 Geometer Plus <contact@geometerplus.com>
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
* 02110-1301, USA.
|
|
*/
|
|
|
|
package org.geometerplus.android.fbreader;
|
|
|
|
import android.content.Intent;
|
|
import android.content.ActivityNotFoundException;
|
|
import android.net.Uri;
|
|
|
|
import org.geometerplus.zlibrary.text.view.*;
|
|
|
|
import org.geometerplus.fbreader.fbreader.FBReaderApp;
|
|
import org.geometerplus.fbreader.bookmodel.FBHyperlinkType;
|
|
import org.geometerplus.fbreader.network.NetworkLibrary;
|
|
|
|
import org.geometerplus.android.fbreader.network.BookDownloader;
|
|
import org.geometerplus.android.fbreader.network.BookDownloaderService;
|
|
import org.geometerplus.android.fbreader.image.ImageViewActivity;
|
|
|
|
class ProcessHyperlinkAction extends FBAndroidAction {
|
|
ProcessHyperlinkAction(FBReader baseActivity, FBReaderApp fbreader) {
|
|
super(baseActivity, fbreader);
|
|
}
|
|
|
|
@Override
|
|
public boolean isEnabled() {
|
|
return Reader.getTextView().getSelectedRegion() != null;
|
|
}
|
|
|
|
@Override
|
|
protected void run(Object ... params) {
|
|
final ZLTextRegion region = Reader.getTextView().getSelectedRegion();
|
|
if (region == null) {
|
|
return;
|
|
}
|
|
|
|
final ZLTextRegion.Soul soul = region.getSoul();
|
|
if (soul instanceof ZLTextHyperlinkRegionSoul) {
|
|
Reader.getTextView().hideSelectedRegionBorder();
|
|
Reader.getViewWidget().repaint();
|
|
final ZLTextHyperlink hyperlink = ((ZLTextHyperlinkRegionSoul)soul).Hyperlink;
|
|
switch (hyperlink.Type) {
|
|
case FBHyperlinkType.EXTERNAL:
|
|
openInBrowser(hyperlink.Id);
|
|
break;
|
|
case FBHyperlinkType.INTERNAL:
|
|
Reader.Collection.markHyperlinkAsVisited(Reader.Model.Book, hyperlink.Id);
|
|
Reader.tryOpenFootnote(hyperlink.Id);
|
|
break;
|
|
}
|
|
} else if (soul instanceof ZLTextImageRegionSoul) {
|
|
Reader.getTextView().hideSelectedRegionBorder();
|
|
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.setData(Uri.parse(url));
|
|
intent.putExtra(
|
|
ImageViewActivity.BACKGROUND_COLOR_KEY,
|
|
Reader.ImageViewBackgroundOption.getValue().intValue()
|
|
);
|
|
OrientationUtil.startActivity(BaseActivity, intent);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
} else if (soul instanceof ZLTextWordRegionSoul) {
|
|
DictionaryUtil.openWordInDictionary(
|
|
BaseActivity, ((ZLTextWordRegionSoul)soul).Word, region
|
|
);
|
|
}
|
|
}
|
|
|
|
private void openInBrowser(final String url) {
|
|
final Intent intent = new Intent(Intent.ACTION_VIEW);
|
|
final boolean externalUrl;
|
|
if (BookDownloader.acceptsUri(Uri.parse(url))) {
|
|
intent.setClass(BaseActivity, BookDownloader.class);
|
|
intent.putExtra(BookDownloaderService.SHOW_NOTIFICATIONS_KEY, BookDownloaderService.Notifications.ALL);
|
|
externalUrl = false;
|
|
} else {
|
|
externalUrl = true;
|
|
}
|
|
final NetworkLibrary nLibrary = NetworkLibrary.Instance();
|
|
new Thread(new Runnable() {
|
|
public void run() {
|
|
if (!url.startsWith("fbreader-action:")) {
|
|
nLibrary.initialize();
|
|
}
|
|
intent.setData(Uri.parse(nLibrary.rewriteUrl(url, externalUrl)));
|
|
BaseActivity.runOnUiThread(new Runnable() {
|
|
public void run() {
|
|
try {
|
|
OrientationUtil.startActivity(BaseActivity, intent);
|
|
} catch (ActivityNotFoundException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
}).start();
|
|
}
|
|
}
|