mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-03 09:49:19 +02:00
fixed possible NPEs in selection operations
This commit is contained in:
parent
4112c4f1b9
commit
c788564d22
7 changed files with 58 additions and 16 deletions
|
@ -48,6 +48,9 @@ public class SelectionBookmarkAction extends FBAndroidAction {
|
|||
} else {
|
||||
bookmark = Reader.addSelectionBookmark();
|
||||
}
|
||||
if (bookmark == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final SuperActivityToast toast =
|
||||
new SuperActivityToast(BaseActivity, SuperToast.Type.BUTTON);
|
||||
|
|
|
@ -25,6 +25,8 @@ import android.text.ClipboardManager;
|
|||
import org.geometerplus.zlibrary.core.resources.ZLResource;
|
||||
|
||||
import org.geometerplus.fbreader.fbreader.FBReaderApp;
|
||||
import org.geometerplus.fbreader.fbreader.FBView;
|
||||
import org.geometerplus.fbreader.util.TextSnippet;
|
||||
|
||||
import org.geometerplus.android.util.UIMessageUtil;
|
||||
|
||||
|
@ -35,8 +37,14 @@ public class SelectionCopyAction extends FBAndroidAction {
|
|||
|
||||
@Override
|
||||
protected void run(Object ... params) {
|
||||
final String text = Reader.getTextView().getSelectedSnippet().getText();
|
||||
Reader.getTextView().clearSelection();
|
||||
final FBView fbview = Reader.getTextView();
|
||||
final TextSnippet snippet = fbview.getSelectedSnippet();
|
||||
if (snippet == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final String text = snippet.getText();
|
||||
fbview.clearSelection();
|
||||
|
||||
final ClipboardManager clipboard =
|
||||
(ClipboardManager)BaseActivity.getApplication().getSystemService(Application.CLIPBOARD_SERVICE);
|
||||
|
|
|
@ -24,6 +24,8 @@ import android.content.Intent;
|
|||
import org.geometerplus.zlibrary.core.resources.ZLResource;
|
||||
|
||||
import org.geometerplus.fbreader.fbreader.FBReaderApp;
|
||||
import org.geometerplus.fbreader.fbreader.FBView;
|
||||
import org.geometerplus.fbreader.util.TextSnippet;
|
||||
|
||||
public class SelectionShareAction extends FBAndroidAction {
|
||||
SelectionShareAction(FBReader baseActivity, FBReaderApp fbreader) {
|
||||
|
@ -32,9 +34,15 @@ public class SelectionShareAction extends FBAndroidAction {
|
|||
|
||||
@Override
|
||||
protected void run(Object ... params) {
|
||||
final String text = Reader.getTextView().getSelectedSnippet().getText();
|
||||
final FBView fbview = Reader.getTextView();
|
||||
final TextSnippet snippet = fbview.getSelectedSnippet();
|
||||
if (snippet == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final String text = snippet.getText();
|
||||
final String title = Reader.getCurrentBook().getTitle();
|
||||
Reader.getTextView().clearSelection();
|
||||
fbview.clearSelection();
|
||||
|
||||
final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
|
||||
intent.setType("text/plain");
|
||||
|
|
|
@ -20,6 +20,8 @@
|
|||
package org.geometerplus.android.fbreader;
|
||||
|
||||
import org.geometerplus.fbreader.fbreader.*;
|
||||
import org.geometerplus.fbreader.util.TextSnippet;
|
||||
|
||||
import org.geometerplus.android.fbreader.dict.DictionaryUtil;
|
||||
|
||||
public class SelectionTranslateAction extends FBAndroidAction {
|
||||
|
@ -30,10 +32,16 @@ public class SelectionTranslateAction extends FBAndroidAction {
|
|||
@Override
|
||||
protected void run(Object ... params) {
|
||||
final FBView fbview = Reader.getTextView();
|
||||
final DictionaryHighlighting dictionaryHilite = new DictionaryHighlighting(fbview);
|
||||
final DictionaryHighlighting dictionaryHilite = DictionaryHighlighting.get(fbview);
|
||||
final TextSnippet snippet = fbview.getSelectedSnippet();
|
||||
|
||||
if (dictionaryHilite == null || snippet == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
DictionaryUtil.openTextInDictionary(
|
||||
BaseActivity,
|
||||
fbview.getSelectedSnippet().getText(),
|
||||
snippet.getText(),
|
||||
fbview.getCountOfSelectedWords() == 1,
|
||||
fbview.getSelectionStartY(),
|
||||
fbview.getSelectionEndY(),
|
||||
|
|
|
@ -23,12 +23,23 @@ import org.geometerplus.zlibrary.core.util.ZLColor;
|
|||
import org.geometerplus.zlibrary.text.view.*;
|
||||
|
||||
public final class DictionaryHighlighting extends ZLTextSimpleHighlighting {
|
||||
public DictionaryHighlighting(ZLTextView view) {
|
||||
this(view, view.getSelectionHighlighting());
|
||||
public static DictionaryHighlighting get(ZLTextView view) {
|
||||
final ZLTextHighlighting hilite = view.getSelectionHighlighting();
|
||||
if (hilite == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final ZLTextPosition start = hilite.getStartPosition();
|
||||
final ZLTextPosition end = hilite.getEndPosition();
|
||||
if (start == null || end == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new DictionaryHighlighting(view, start, end);
|
||||
}
|
||||
|
||||
private DictionaryHighlighting(ZLTextView view, ZLTextHighlighting selection) {
|
||||
super(view, selection.getStartPosition(), selection.getEndPosition());
|
||||
private DictionaryHighlighting(ZLTextView view, ZLTextPosition start, ZLTextPosition end) {
|
||||
super(view, start, end);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -35,8 +35,7 @@ import org.geometerplus.fbreader.bookmodel.*;
|
|||
import org.geometerplus.fbreader.fbreader.options.*;
|
||||
import org.geometerplus.fbreader.formats.*;
|
||||
import org.geometerplus.fbreader.network.sync.SyncData;
|
||||
import org.geometerplus.fbreader.util.AutoTextSnippet;
|
||||
import org.geometerplus.fbreader.util.EmptyTextSnippet;
|
||||
import org.geometerplus.fbreader.util.*;
|
||||
|
||||
public final class FBReaderApp extends ZLApplication {
|
||||
public interface ExternalFileOpener {
|
||||
|
@ -269,12 +268,16 @@ public final class FBReaderApp extends ZLApplication {
|
|||
|
||||
public Bookmark addSelectionBookmark() {
|
||||
final FBView fbView = getTextView();
|
||||
final TextSnippet snippet = fbView.getSelectedSnippet();
|
||||
if (snippet == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final Bookmark bookmark = new Bookmark(
|
||||
Collection,
|
||||
Model.Book,
|
||||
fbView.getModel().getId(),
|
||||
fbView.getSelectedSnippet(),
|
||||
snippet,
|
||||
true
|
||||
);
|
||||
Collection.saveBookmark(bookmark);
|
||||
|
|
|
@ -763,10 +763,11 @@ public final class FBView extends ZLTextView {
|
|||
public TextSnippet getSelectedSnippet() {
|
||||
final ZLTextPosition start = getSelectionStartPosition();
|
||||
final ZLTextPosition end = getSelectionEndPosition();
|
||||
final TextBuildTraverser traverser = new TextBuildTraverser(this);
|
||||
if (!isSelectionEmpty()) {
|
||||
traverser.traverse(start, end);
|
||||
if (start == null || end == null) {
|
||||
return null;
|
||||
}
|
||||
final TextBuildTraverser traverser = new TextBuildTraverser(this);
|
||||
traverser.traverse(start, end);
|
||||
return new FixedTextSnippet(start, end, traverser.getText());
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue