diff --git a/src/org/geometerplus/android/fbreader/api/Api.java b/src/org/geometerplus/android/fbreader/api/Api.java index f09260d38..6027b6a31 100644 --- a/src/org/geometerplus/android/fbreader/api/Api.java +++ b/src/org/geometerplus/android/fbreader/api/Api.java @@ -7,12 +7,15 @@ package org.geometerplus.android.fbreader.api; import java.util.List; public interface Api { - void connect(); - void disconnect(); - - // fbreader information + // information about fbreader String getFBReaderVersion() throws ApiException; + // preferences information + List getOptionGroups() throws ApiException; + List getOptionNames(String group) throws ApiException; + String getOptionValue(String group, String name) throws ApiException; + void setOptionValue(String group, String name, String value) throws ApiException; + // book information String getBookLanguage() throws ApiException; String getBookTitle() throws ApiException; diff --git a/src/org/geometerplus/android/fbreader/api/ApiClientImplementation.java b/src/org/geometerplus/android/fbreader/api/ApiClientImplementation.java index acc972fb1..18247154c 100644 --- a/src/org/geometerplus/android/fbreader/api/ApiClientImplementation.java +++ b/src/org/geometerplus/android/fbreader/api/ApiClientImplementation.java @@ -133,14 +133,42 @@ public class ApiClientImplementation implements ServiceConnection, Api, ApiMetho private static final ApiObject[] EMPTY_PARAMETERS = new ApiObject[0]; + private static ApiObject[] envelope(String value) { + return new ApiObject[] { ApiObject.envelope(value) }; + } + private static ApiObject[] envelope(int value) { return new ApiObject[] { ApiObject.envelope(value) }; } + // information about fbreader public String getFBReaderVersion() throws ApiException { return requestString(GET_FBREADER_VERSION, EMPTY_PARAMETERS); } + // preferences information + public List getOptionGroups() throws ApiException { + return requestStringList(GET_OPTION_GROUPS, EMPTY_PARAMETERS); + } + + public List getOptionNames(String group) throws ApiException { + return requestStringList(GET_OPTION_NAMES, envelope(group)); + } + + public String getOptionValue(String group, String name) throws ApiException { + return requestString( + GET_OPTION_VALUE, + new ApiObject[] { ApiObject.envelope(group), ApiObject.envelope(name) } + ); + } + + public void setOptionValue(String group, String name, String value) throws ApiException { + request( + SET_OPTION_VALUE, + new ApiObject[] { ApiObject.envelope(group), ApiObject.envelope(name), ApiObject.envelope(value) } + ); + } + public String getBookLanguage() throws ApiException { return requestString(GET_BOOK_LANGUAGE, EMPTY_PARAMETERS); } @@ -154,7 +182,7 @@ public class ApiClientImplementation implements ServiceConnection, Api, ApiMetho } public String getBookFileName() throws ApiException { - return requestString(GET_BOOK_FILENAME, EMPTY_PARAMETERS); + return requestString(GET_BOOK_FILE_NAME, EMPTY_PARAMETERS); } public TextPosition getPageStart() throws ApiException { diff --git a/src/org/geometerplus/android/fbreader/api/ApiMethods.java b/src/org/geometerplus/android/fbreader/api/ApiMethods.java index d8a770e6f..510282622 100644 --- a/src/org/geometerplus/android/fbreader/api/ApiMethods.java +++ b/src/org/geometerplus/android/fbreader/api/ApiMethods.java @@ -15,13 +15,17 @@ interface ApiMethods { // bookmarks information // preferences + int GET_OPTION_GROUPS = 401; + int GET_OPTION_NAMES = 402; + int GET_OPTION_VALUE = 403; + int SET_OPTION_VALUE = 404; // book information int GET_BOOK_LANGUAGE = 501; int GET_BOOK_TITLE = 502; int GET_BOOK_AUTHORS = 503; int GET_BOOK_TAGS = 504; - int GET_BOOK_FILENAME = 505; + int GET_BOOK_FILE_NAME = 505; // text information int GET_PARAGRAPHS_NUMBER = 601; diff --git a/src/org/geometerplus/android/fbreader/api/ApiServerImplementation.java b/src/org/geometerplus/android/fbreader/api/ApiServerImplementation.java index bdc1339a5..b60707e32 100644 --- a/src/org/geometerplus/android/fbreader/api/ApiServerImplementation.java +++ b/src/org/geometerplus/android/fbreader/api/ApiServerImplementation.java @@ -27,23 +27,41 @@ import org.geometerplus.zlibrary.text.view.*; import org.geometerplus.fbreader.fbreader.FBReaderApp; -public class ApiServerImplementation extends ApiInterface.Stub implements ApiMethods { +public class ApiServerImplementation extends ApiInterface.Stub implements Api, ApiMethods { private final FBReaderApp myReader = (FBReaderApp)FBReaderApp.Instance(); private ApiObject.Error unsupportedMethodError(int method) { return new ApiObject.Error("Unsupported method code: " + method); } + private ApiObject.Error exceptionInMethodError(int method, Throwable e) { + return new ApiObject.Error("Exception in method " + method + ": " + e); + } + @Override public ApiObject request(int method, ApiObject[] parameters) { try { switch (method) { case GET_FBREADER_VERSION: - return ApiObject.envelope(ZLibrary.Instance().getVersionName()); + return ApiObject.envelope(getFBReaderVersion()); + case GET_OPTION_VALUE: + return ApiObject.envelope(getOptionValue( + ((ApiObject.String)parameters[0]).Value, + ((ApiObject.String)parameters[1]).Value + )); + case SET_OPTION_VALUE: + setOptionValue( + ((ApiObject.String)parameters[0]).Value, + ((ApiObject.String)parameters[1]).Value, + ((ApiObject.String)parameters[2]).Value + ); + return ApiObject.Void.Instance; case GET_BOOK_LANGUAGE: - return ApiObject.envelope(myReader.Model.Book.getLanguage()); + return ApiObject.envelope(getBookLanguage()); case GET_BOOK_TITLE: - return ApiObject.envelope(myReader.Model.Book.getTitle()); + return ApiObject.envelope(getBookTitle()); + case GET_BOOK_FILE_NAME: + return ApiObject.envelope(getBookFileName()); case GET_PARAGRAPHS_NUMBER: return ApiObject.envelope(getParagraphsNumber()); case GET_ELEMENTS_NUMBER: @@ -55,9 +73,9 @@ public class ApiServerImplementation extends ApiInterface.Stub implements ApiMet ((ApiObject.Integer)parameters[0]).Value )); case GET_PAGE_START: - return getTextPosition(myReader.getTextView().getStartCursor()); + return getPageStart(); case GET_PAGE_END: - return getTextPosition(myReader.getTextView().getEndCursor()); + return getPageEnd(); case IS_PAGE_END_OF_SECTION: return ApiObject.envelope(isPageEndOfSection()); case IS_PAGE_END_OF_TEXT: @@ -67,14 +85,11 @@ public class ApiServerImplementation extends ApiInterface.Stub implements ApiMet return ApiObject.Void.Instance; case HIGHLIGHT_AREA: { - myReader.getTextView().highlight( - getZLTextPosition((TextPosition)parameters[0]), - getZLTextPosition((TextPosition)parameters[1]) - ); + highlightArea((TextPosition)parameters[0], (TextPosition)parameters[1]); return ApiObject.Void.Instance; } case CLEAR_HIGHLIGHTING: - myReader.getTextView().clearHighlighting(); + clearHighlighting(); return ApiObject.Void.Instance; default: return unsupportedMethodError(method); @@ -86,13 +101,95 @@ public class ApiServerImplementation extends ApiInterface.Stub implements ApiMet @Override public List requestList(int method, ApiObject[] parameters) { - return Collections.singletonList(unsupportedMethodError(method)); + try { + switch (method) { + case GET_OPTION_GROUPS: + case GET_OPTION_NAMES: + case GET_BOOK_TAGS: + default: + return Collections.singletonList(unsupportedMethodError(method)); + } + } catch (Throwable e) { + return Collections.singletonList(exceptionInMethodError(method, e)); + } + } + + private Map errorMap(ApiObject.Error error) { + return Collections.singletonMap(error, error); } @Override public Map requestMap(int method, ApiObject[] parameters) { - final ApiObject error = unsupportedMethodError(method); - return Collections.singletonMap(error, error); + try { + switch (method) { + default: + return errorMap(unsupportedMethodError(method)); + } + } catch (Throwable e) { + return errorMap(exceptionInMethodError(method, e)); + } + } + + // information about fbreader + public String getFBReaderVersion() { + return ZLibrary.Instance().getVersionName(); + } + + // preferences information + public List getOptionGroups() { + // TODO: implement + return Collections.emptyList(); + } + + public List getOptionNames(String group) { + // TODO: implement + return Collections.emptyList(); + } + + public String getOptionValue(String group, String name) { + // TODO: implement + return null; + } + + public void setOptionValue(String group, String name, String value) { + // TODO: implement + } + + public String getBookLanguage() { + return myReader.Model.Book.getLanguage(); + } + + public String getBookTitle() { + return myReader.Model.Book.getTitle(); + } + + public List getBookTags() { + // TODO: implement + return Collections.emptyList(); + } + + public String getBookFileName() { + // TODO: implement + return null; + } + + // page information + public TextPosition getPageStart() { + return getTextPosition(myReader.getTextView().getStartCursor()); + } + + public TextPosition getPageEnd() { + return getTextPosition(myReader.getTextView().getEndCursor()); + } + + public boolean isPageEndOfSection() { + final ZLTextWordCursor cursor = myReader.getTextView().getEndCursor(); + return cursor.isEndOfParagraph() && cursor.getParagraphCursor().isEndOfSection(); + } + + public boolean isPageEndOfText() { + final ZLTextWordCursor cursor = myReader.getTextView().getEndCursor(); + return cursor.isEndOfParagraph() && cursor.getParagraphCursor().isLast(); } private TextPosition getTextPosition(ZLTextWordCursor cursor) { @@ -111,33 +208,35 @@ public class ApiServerImplementation extends ApiInterface.Stub implements ApiMet ); } - private boolean isPageEndOfSection() { - final ZLTextWordCursor cursor = myReader.getTextView().getEndCursor(); - return cursor.isEndOfParagraph() && cursor.getParagraphCursor().isEndOfSection(); - } - - private boolean isPageEndOfText() { - final ZLTextWordCursor cursor = myReader.getTextView().getEndCursor(); - return cursor.isEndOfParagraph() && cursor.getParagraphCursor().isLast(); - } - - private void setPageStart(TextPosition position) { + // manage view + public void setPageStart(TextPosition position) { myReader.getTextView().gotoPosition(position.ParagraphIndex, position.ElementIndex, position.CharIndex); myReader.getViewWidget().repaint(); } - private int getParagraphsNumber() { + public void highlightArea(TextPosition start, TextPosition end) { + myReader.getTextView().highlight( + getZLTextPosition(start), + getZLTextPosition(end) + ); + } + + public void clearHighlighting() { + myReader.getTextView().clearHighlighting(); + } + + public int getParagraphsNumber() { return myReader.Model.BookTextModel.getParagraphsNumber(); } - private int getElementsNumber(int paragraphIndex) { + public int getElementsNumber(int paragraphIndex) { final ZLTextWordCursor cursor = new ZLTextWordCursor(myReader.getTextView().getStartCursor()); cursor.moveToParagraph(paragraphIndex); cursor.moveToParagraphEnd(); return cursor.getElementIndex(); } - private String getParagraphText(int paragraphIndex) { + public String getParagraphText(int paragraphIndex) { final StringBuffer sb = new StringBuffer(); final ZLTextWordCursor cursor = new ZLTextWordCursor(myReader.getTextView().getStartCursor()); cursor.moveToParagraph(paragraphIndex);