1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-03 17:59:33 +02:00

preferences API (stub implementation)

This commit is contained in:
Nikolay Pultsin 2011-06-23 18:26:01 +01:00
parent eb46450995
commit 47fc0d3be1
4 changed files with 168 additions and 34 deletions

View file

@ -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<String> getOptionGroups() throws ApiException;
List<String> 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;

View file

@ -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<String> getOptionGroups() throws ApiException {
return requestStringList(GET_OPTION_GROUPS, EMPTY_PARAMETERS);
}
public List<String> 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 {

View file

@ -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;

View file

@ -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<ApiObject> requestList(int method, ApiObject[] parameters) {
return Collections.<ApiObject>singletonList(unsupportedMethodError(method));
try {
switch (method) {
case GET_OPTION_GROUPS:
case GET_OPTION_NAMES:
case GET_BOOK_TAGS:
default:
return Collections.<ApiObject>singletonList(unsupportedMethodError(method));
}
} catch (Throwable e) {
return Collections.<ApiObject>singletonList(exceptionInMethodError(method, e));
}
}
private Map<ApiObject,ApiObject> errorMap(ApiObject.Error error) {
return Collections.<ApiObject,ApiObject>singletonMap(error, error);
}
@Override
public Map<ApiObject,ApiObject> 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<String> getOptionGroups() {
// TODO: implement
return Collections.emptyList();
}
public List<String> 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<String> 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);