1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-04 18:29:23 +02:00

translation action has been implemented

This commit is contained in:
Nikolay Pultsin 2010-12-12 16:30:59 +00:00
parent 07c9f691b0
commit ec4398d411
10 changed files with 127 additions and 134 deletions

View file

@ -3,6 +3,6 @@
<binding key="&lt;VolumeDown&gt;" action="volumeKeyScrollForward"/>
<binding key="&lt;VolumeUp&gt;" action="volumeKeyScrollBackward"/>
<binding key="&lt;Back&gt;" action="cancel"/>
<binding key="&lt;Enter&gt;" action="followHyperlink"/>
<binding key="&lt;PadCenter&gt;" action="followHyperlink"/>
<binding key="&lt;Enter&gt;" action="processHyperlink"/>
<binding key="&lt;PadCenter&gt;" action="processHyperlink"/>
</keymap>

View file

@ -118,7 +118,7 @@ public final class FBReader extends ZLAndroidActivity {
fbReader.addAction(ActionCode.SHOW_NAVIGATION, new ShowNavigationAction(this, fbReader));
fbReader.addAction(ActionCode.SEARCH, new SearchAction(this, fbReader));
fbReader.addAction(ActionCode.TRANSLATE, new TranslateAction(this, fbReader));
fbReader.addAction(ActionCode.PROCESS_HYPERLINK, new ProcessHyperlinkAction(this, fbReader));
}
@Override

View file

@ -0,0 +1,110 @@
/*
* Copyright (C) 2010 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 org.geometerplus.zlibrary.core.resources.ZLResource;
import org.geometerplus.zlibrary.core.library.ZLibrary;
import org.geometerplus.zlibrary.text.view.ZLTextView;
import org.geometerplus.zlibrary.text.view.ZLTextHyperlink;
import org.geometerplus.fbreader.fbreader.FBAction;
import org.geometerplus.fbreader.fbreader.FBReaderApp;
import org.geometerplus.fbreader.bookmodel.FBHyperlinkType;
import android.app.SearchManager;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Intent;
import android.widget.Toast;
class ProcessHyperlinkAction extends FBAction {
private final FBReader myBaseActivity;
ProcessHyperlinkAction(FBReader baseActivity, FBReaderApp fbreader) {
super(fbreader);
myBaseActivity = baseActivity;
}
public boolean isEnabled() {
final ZLTextView view = Reader.getTextView();
return
view.getSelectedText() != null ||
view.getCurrentHyperlink() != null;
}
public void run() {
final ZLTextHyperlink hyperlink = Reader.getTextView().getCurrentHyperlink();
if (hyperlink != null) {
switch (hyperlink.Type) {
case FBHyperlinkType.EXTERNAL:
ZLibrary.Instance().openInBrowser(hyperlink.Id);
break;
case FBHyperlinkType.INTERNAL:
Reader.tryOpenFootnote(hyperlink.Id);
break;
}
return;
}
final String text = Reader.getTextView().getSelectedText();
if (text != null) {
int start = 0;
int end = text.length();
for (; start < end && !Character.isLetterOrDigit(text.charAt(start)); ++start);
for (; start < end && !Character.isLetterOrDigit(text.charAt(end - 1)); --end);
if (start == end) {
return;
}
Intent intent = new Intent(Intent.ACTION_SEARCH);
intent.setComponent(new ComponentName(
"com.socialnmobile.colordict",
"com.socialnmobile.colordict.activity.Main"
));
intent.putExtra(SearchManager.QUERY, text.substring(start, end));
try {
myBaseActivity.startActivity(intent);
} catch(ActivityNotFoundException e){
Toast.makeText(
myBaseActivity,
ZLResource.resource("errorMessage").getResource("dictNotInstalled").getValue(),
Toast.LENGTH_LONG
).show();
}
}
}
/*
final Intent intent = new Intent(Intent.ACTION_VIEW);
boolean externalUrl = true;
if (BookDownloader.acceptsUri(Uri.parse(reference))) {
intent.setClass(myActivity, BookDownloader.class);
intent.putExtra(BookDownloaderService.SHOW_NOTIFICATIONS_KEY, BookDownloaderService.Notifications.ALL);
externalUrl = false;
}
final NetworkLibrary nLibrary = NetworkLibrary.Instance();
try {
nLibrary.initialize();
} catch (ZLNetworkException e) {
}
reference = NetworkLibrary.Instance().rewriteUrl(reference, externalUrl);
intent.setData(Uri.parse(reference));
myActivity.startActivity(intent);
*/
}

View file

@ -1,74 +0,0 @@
/*
* Copyright (C) 2010 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 org.geometerplus.zlibrary.core.resources.ZLResource;
import org.geometerplus.zlibrary.ui.android.library.ZLAndroidApplication;
import org.geometerplus.zlibrary.ui.android.library.ZLAndroidLibrary;
import org.geometerplus.zlibrary.ui.android.view.ZLAndroidWidget;
import org.geometerplus.fbreader.fbreader.FBAction;
import org.geometerplus.fbreader.fbreader.FBReaderApp;
import android.app.SearchManager;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Intent;
import android.widget.Toast;
class TranslateAction extends FBAction {
private final FBReader myBaseActivity;
TranslateAction(FBReader baseActivity, FBReaderApp fbreader) {
super(fbreader);
myBaseActivity = baseActivity;
}
/*
private ZLAndroidWidget getWidget() {
return ((ZLAndroidLibrary)ZLAndroidLibrary.Instance()).getWidget();
}
*/
public boolean isVisible() {
return true;
//return Reader.Model != null;// && getWidget().myLongPressWord != "";
}
public void run() {
Intent intent = new Intent(Intent.ACTION_SEARCH);
intent.setComponent(new ComponentName(
"com.socialnmobile.colordict",
"com.socialnmobile.colordict.activity.Main"
));
//intent.putExtra(SearchManager.QUERY, getWidget().myLongPressWord);
intent.putExtra(SearchManager.QUERY, "biology");
try {
myBaseActivity.startActivity(intent);
}
catch(ActivityNotFoundException e){
Toast.makeText(
myBaseActivity,
ZLResource.resource("errorMessage").getResource("dictNotInstalled").getValue(),
Toast.LENGTH_LONG
).show();
}
}
}

View file

@ -52,5 +52,5 @@ public interface ActionCode {
String CLEAR_SELECTION = "clearSelection";
String TRANSLATE = "translate";
String FOLLOW_HYPERLINK = "followHyperlink";
};
String PROCESS_HYPERLINK = "processHyperlink";
}

View file

@ -109,7 +109,6 @@ public final class FBReaderApp extends ZLApplication {
//addAction(ActionCode.COPY_SELECTED_TEXT_TO_CLIPBOARD, new DummyAction(this));
//addAction(ActionCode.OPEN_SELECTED_TEXT_IN_DICTIONARY, new DummyAction(this));
//addAction(ActionCode.CLEAR_SELECTION, new DummyAction(this));
addAction(ActionCode.FOLLOW_HYPERLINK, new FollowHyperlinkAction(this));
addAction(ActionCode.SWITCH_TO_DAY_PROFILE, new SwitchProfileAction(this, ColorProfile.DAY));
addAction(ActionCode.SWITCH_TO_NIGHT_PROFILE, new SwitchProfileAction(this, ColorProfile.NIGHT));
@ -170,7 +169,7 @@ public final class FBReaderApp extends ZLApplication {
return (FBView)getCurrentView();
}
void tryOpenFootnote(String id) {
public void tryOpenFootnote(String id) {
if (Model != null) {
BookModel.Label label = Model.getLabel(id);
if (label != null) {

View file

@ -69,17 +69,6 @@ public final class FBView extends ZLTextView {
}
}
void followHyperlink(ZLTextHyperlink hyperlink) {
switch (hyperlink.Type) {
case FBHyperlinkType.EXTERNAL:
ZLibrary.Instance().openInBrowser(hyperlink.Id);
break;
case FBHyperlinkType.INTERNAL:
myReader.tryOpenFootnote(hyperlink.Id);
break;
}
}
private int myStartX;
private int myStartY;
private boolean myIsManualScrollingActive;
@ -107,7 +96,8 @@ public final class FBView extends ZLTextView {
if (hyperlink != null) {
selectHyperlink(hyperlink);
myReader.repaintView();
followHyperlink(hyperlink);
myReader.doAction(ActionCode.PROCESS_HYPERLINK);
//followHyperlink(hyperlink);
return true;
}

View file

@ -1,36 +0,0 @@
/*
* Copyright (C) 2009-2010 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.fbreader.fbreader;
public class FollowHyperlinkAction extends FBAction {
FollowHyperlinkAction(FBReaderApp fbreader) {
super(fbreader);
}
public boolean isEnabled() {
FBView view = Reader.getTextView();
return (view != null) && (view.getCurrentHyperlink() != null);
}
public void run() {
FBView view = Reader.getTextView();
view.followHyperlink(view.getCurrentHyperlink());
}
}

View file

@ -1306,6 +1306,14 @@ public abstract class ZLTextView extends ZLTextViewBase {
return (area instanceof ZLTextHyperlinkRegion) ? ((ZLTextHyperlinkRegion)area).Hyperlink : null;
}
public String getSelectedText() {
final ZLTextElementRegion area = getCurrentElementRegion(myCurrentPage);
if (area instanceof ZLTextWordRegion) {
return ((ZLTextWordRegion)area).Word.toString();
}
return null;
}
protected ZLTextHyperlink findHyperlink(int x, int y, int maxDistance) {
ZLTextHyperlinkRegion hyperlinkRegion = null;
int distance = maxDistance + 1;

View file

@ -98,10 +98,6 @@ public final class ZLTextWord extends ZLTextElement {
}
public String toString() {
StringBuffer sb = new StringBuffer();
for (int i = Offset; i < Offset + Length; i++) {
sb.append(Data[i]);
}
return sb.toString();
return new String(Data, Offset, Length);
}
}