From cbd74279ac37d588070078feaf187432cdece8a6 Mon Sep 17 00:00:00 2001 From: Nikolay Pultsin Date: Sat, 13 Oct 2012 13:08:57 +0400 Subject: [PATCH] synchronyzation with Greg's branch + code style --- .../android/fbreader/api/Api.java | 12 +- .../fbreader/api/ApiClientImplementation.java | 68 +++++++++-- .../android/fbreader/api/ApiMethods.java | 10 +- .../android/fbreader/api/ApiObject.java | 8 ++ .../fbreader/api/ApiServerImplementation.java | 106 ++++++++++++++++-- 5 files changed, 186 insertions(+), 18 deletions(-) diff --git a/src/org/geometerplus/android/fbreader/api/Api.java b/src/org/geometerplus/android/fbreader/api/Api.java index 41b19f396..de6277eb1 100644 --- a/src/org/geometerplus/android/fbreader/api/Api.java +++ b/src/org/geometerplus/android/fbreader/api/Api.java @@ -42,8 +42,10 @@ public interface Api { // text information int getParagraphsNumber() throws ApiException; - int getElementsNumber(int paragraphIndex) throws ApiException; + int getParagraphElementsCount(int paragraphIndex) throws ApiException; String getParagraphText(int paragraphIndex) throws ApiException; + int getParagraphWordsCount(int paragraphIndex) throws ApiException; + String getParagraphWordIndices(int paragraphIndex) throws ApiException; // page information TextPosition getPageStart() throws ApiException; @@ -55,6 +57,14 @@ public interface Api { void setPageStart(TextPosition position) throws ApiException; void highlightArea(TextPosition start, TextPosition end) throws ApiException; void clearHighlighting() throws ApiException; + int getBottomMargin() throws ApiException; + void setBottomMargin(int value) throws ApiException; + int getTopMargin() throws ApiException; + void setTopMargin(int value) throws ApiException; + int getLeftMargin() throws ApiException; + void setLeftMargin(int value) throws ApiException; + int getRightMargin() throws ApiException; + void setRightMargin(int value) throws ApiException; // action control List listActions() throws ApiException; diff --git a/src/org/geometerplus/android/fbreader/api/ApiClientImplementation.java b/src/org/geometerplus/android/fbreader/api/ApiClientImplementation.java index 059ed825d..c478638e0 100644 --- a/src/org/geometerplus/android/fbreader/api/ApiClientImplementation.java +++ b/src/org/geometerplus/android/fbreader/api/ApiClientImplementation.java @@ -171,6 +171,18 @@ public class ApiClientImplementation implements ServiceConnection, Api, ApiMetho return stringList; } + private List requestIntegerList(int method, ApiObject[] params) throws ApiException { + final List list = requestList(method, params); + final ArrayList intList = new ArrayList(list.size()); + for (ApiObject object : list) { + if (!(object instanceof ApiObject.Integer)) { + throw new ApiException("Cannot cast an element returned from method " + method + " to Integer"); + } + intList.add(((ApiObject.Integer)object).Value); + } + return intList; + } + private static final ApiObject[] EMPTY_PARAMETERS = new ApiObject[0]; private static ApiObject[] envelope(String value) { @@ -189,7 +201,7 @@ public class ApiClientImplementation implements ServiceConnection, Api, ApiMetho final ApiObject[] objects = new ApiObject[value.size()]; int index = 0; for (String s : value) { - objects[index++] = ApiObject.envelope(s); + objects[index++] = ApiObject.envelope(s); } return objects; } @@ -302,8 +314,16 @@ public class ApiClientImplementation implements ServiceConnection, Api, ApiMetho return requestString(GET_PARAGRAPH_TEXT, envelope(paragraphIndex)); } - public int getElementsNumber(int paragraphIndex) throws ApiException { - return requestInt(GET_ELEMENTS_NUMBER, envelope(paragraphIndex)); + public int getParagraphElementsCount(int paragraphIndex) throws ApiException { + return requestInt(GET_PARAGRAPH_ELEMENTS_COUNT, envelope(paragraphIndex)); + } + + public List getParagraphWordsCount(int paragraphIndex) throws ApiException { + return requestStringList(GET_PARAGRAPH_WORDS_COUNT, envelope(paragraphIndex)); + } + + public ArrayList getParagraphWordIndices(int paragraphIndex) throws ApiException { + return requestIntegerList(GET_PARAGRAPH_WORD_INDICES, envelope(paragraphIndex)); } public void setPageStart(TextPosition position) throws ApiException { @@ -318,6 +338,38 @@ public class ApiClientImplementation implements ServiceConnection, Api, ApiMetho request(CLEAR_HIGHLIGHTING, EMPTY_PARAMETERS); } + public int getBottomMargin() throws ApiException { + return requestInt(GET_BOTTOM_MARGIN, EMPTY_PARAMETERS); + } + + public void setBottomMargin(int value) throws ApiException { + request(SET_BOTTOM_MARGIN, new ApiObject[] { ApiObject.envelope(value) }); + } + + public int getTopMargin() throws ApiException { + return requestInt(GET_TOP_MARGIN, EMPTY_PARAMETERS); + } + + public void setTopMargin(int value) throws ApiException { + request(SET_TOP_MARGIN, new ApiObject[] { ApiObject.envelope(value) }); + } + + public int getLeftMargin() throws ApiException { + return requestInt(GET_LEFT_MARGIN, EMPTY_PARAMETERS); + } + + public void setLeftMargin(int value) throws ApiException { + request(SET_LEFT_MARGIN, new ApiObject[] { ApiObject.envelope(value) }); + } + + public int getRightMargin() throws ApiException { + return requestInt(GET_RIGHT_MARGIN, EMPTY_PARAMETERS); + } + + public void setRightMargin(int value) throws ApiException { + request(SET_RIGHT_MARGIN, new ApiObject[] { ApiObject.envelope(value) }); + } + // action control public String getKeyAction(int key, boolean longPress) throws ApiException { return requestString(GET_KEY_ACTION, new ApiObject[] { @@ -335,23 +387,23 @@ public class ApiClientImplementation implements ServiceConnection, Api, ApiMetho } public List listActions() throws ApiException { - return requestStringList(LIST_ACTIONS, EMPTY_PARAMETERS); + return requestStringList(LIST_ACTIONS, EMPTY_PARAMETERS); } public List listActionNames(List actions) throws ApiException { - return requestStringList(LIST_ACTION_NAMES, envelope(actions)); + return requestStringList(LIST_ACTION_NAMES, envelope(actions)); } public List listZoneMaps() throws ApiException { - return requestStringList(LIST_ZONEMAPS, EMPTY_PARAMETERS); + return requestStringList(LIST_ZONEMAPS, EMPTY_PARAMETERS); } public String getZoneMap() throws ApiException { - return requestString(GET_ZONEMAP, EMPTY_PARAMETERS); + return requestString(GET_ZONEMAP, EMPTY_PARAMETERS); } public void setZoneMap(String name) throws ApiException { - request(SET_ZONEMAP, envelope(name)); + request(SET_ZONEMAP, envelope(name)); } public int getZoneMapHeight(String name) throws ApiException { diff --git a/src/org/geometerplus/android/fbreader/api/ApiMethods.java b/src/org/geometerplus/android/fbreader/api/ApiMethods.java index 566d07a91..91c47e785 100644 --- a/src/org/geometerplus/android/fbreader/api/ApiMethods.java +++ b/src/org/geometerplus/android/fbreader/api/ApiMethods.java @@ -32,7 +32,7 @@ interface ApiMethods { // text information int GET_PARAGRAPHS_NUMBER = 601; - int GET_ELEMENTS_NUMBER = 602; + int GET_PARAGRAPH_ELEMENTS_COUNT = 602; int GET_PARAGRAPH_TEXT = 603; int GET_PARAGRAPH_WORDS_COUNT = 604; int GET_PARAGRAPH_WORD_INDICES = 605; @@ -47,6 +47,14 @@ interface ApiMethods { int SET_PAGE_START = 801; int HIGHLIGHT_AREA = 802; int CLEAR_HIGHLIGHTING = 803; + int GET_BOTTOM_MARGIN = 804; + int SET_BOTTOM_MARGIN = 805; + int GET_TOP_MARGIN = 806; + int SET_TOP_MARGIN = 807; + int GET_LEFT_MARGIN = 808; + int SET_LEFT_MARGIN = 809; + int GET_RIGHT_MARGIN = 810; + int SET_RIGHT_MARGIN = 811; // action control int LIST_ACTIONS = 901; diff --git a/src/org/geometerplus/android/fbreader/api/ApiObject.java b/src/org/geometerplus/android/fbreader/api/ApiObject.java index 18438aae5..772d29958 100644 --- a/src/org/geometerplus/android/fbreader/api/ApiObject.java +++ b/src/org/geometerplus/android/fbreader/api/ApiObject.java @@ -176,6 +176,14 @@ public abstract class ApiObject implements Parcelable { return objects; } + static List envelope(List values) { + final ArrayList objects = new ArrayList(values.size()); + for (java.lang.Integer v : values) { + objects.add(new Integer(v)); + } + return objects; + } + abstract protected int type(); public int describeContents() { diff --git a/src/org/geometerplus/android/fbreader/api/ApiServerImplementation.java b/src/org/geometerplus/android/fbreader/api/ApiServerImplementation.java index b91d5b4e5..922bbec6d 100644 --- a/src/org/geometerplus/android/fbreader/api/ApiServerImplementation.java +++ b/src/org/geometerplus/android/fbreader/api/ApiServerImplementation.java @@ -104,8 +104,8 @@ public class ApiServerImplementation extends ApiInterface.Stub implements Api, A } case GET_PARAGRAPHS_NUMBER: return ApiObject.envelope(getParagraphsNumber()); - case GET_ELEMENTS_NUMBER: - return ApiObject.envelope(getElementsNumber( + case GET_PARAGRAPH_ELEMENTS_COUNT: + return ApiObject.envelope(getParagraphElementsCount( ((ApiObject.Integer)parameters[0]).Value )); case GET_PARAGRAPH_TEXT: @@ -131,6 +131,26 @@ public class ApiServerImplementation extends ApiInterface.Stub implements Api, A case CLEAR_HIGHLIGHTING: clearHighlighting(); return ApiObject.Void.Instance; + case GET_BOTTOM_MARGIN: + return ApiObject.envelope(getBottomMargin()); + case SET_BOTTOM_MARGIN: + setBottomMargin(((ApiObject.Integer)parameters[0]).Value); + return ApiObject.Void.Instance; + case GET_TOP_MARGIN: + return ApiObject.envelope(getTopMargin()); + case SET_TOP_MARGIN: + setTopMargin(((ApiObject.Integer)parameters[0]).Value); + return ApiObject.Void.Instance; + case GET_LEFT_MARGIN: + return ApiObject.envelope(getLeftMargin()); + case SET_LEFT_MARGIN: + setLeftMargin(((ApiObject.Integer)parameters[0]).Value); + return ApiObject.Void.Instance; + case GET_RIGHT_MARGIN: + return ApiObject.envelope(getRightMargin()); + case SET_RIGHT_MARGIN: + setRightMargin(((ApiObject.Integer)parameters[0]).Value); + return ApiObject.Void.Instance; case GET_KEY_ACTION: return ApiObject.envelope(getKeyAction( ((ApiObject.Integer)parameters[0]).Value, @@ -144,9 +164,9 @@ public class ApiServerImplementation extends ApiInterface.Stub implements Api, A ); return ApiObject.Void.Instance; case GET_ZONEMAP: - return ApiObject.envelope(getZoneMap()); + return ApiObject.envelope(getZoneMap()); case SET_ZONEMAP: - setZoneMap(((ApiObject.String)parameters[0]).Value); + setZoneMap(((ApiObject.String)parameters[0]).Value); return ApiObject.Void.Instance; case GET_ZONEMAP_HEIGHT: return ApiObject.envelope(getZoneMapHeight(((ApiObject.String)parameters[0]).Value)); @@ -207,12 +227,20 @@ public class ApiServerImplementation extends ApiInterface.Stub implements Api, A { final ArrayList actions = new ArrayList(parameters.length); for (ApiObject o : parameters) { - actions.add(((ApiObject.String)o).Value); + actions.add(((ApiObject.String)o).Value); } return ApiObject.envelope(listActionNames(actions)); } case LIST_ZONEMAPS: return ApiObject.envelope(listZoneMaps()); + case GET_PARAGRAPH_WORDS_COUNT: + return ApiObject.envelope(getParagraphWordsCount( + ((ApiObject.Integer)parameters[0]).Value + )); + case GET_PARAGRAPH_WORD_INDICES: + return ApiObject.envelope(getParagraphWordIndices( + ((ApiObject.Integer)parameters[0]).Value + )); default: return Collections.singletonList(unsupportedMethodError(method)); } @@ -376,11 +404,43 @@ public class ApiServerImplementation extends ApiInterface.Stub implements Api, A myReader.getTextView().clearHighlighting(); } + public int getBottomMargin() { + return myReader.BottomMarginOption.getValue(); + } + + public void setBottomMargin(int value) { + myReader.BottomMarginOption.setValue(value); + } + + public int getTopMargin() { + return myReader.TopMarginOption.getValue(); + } + + public void setTopMargin(int value) { + myReader.TopMarginOption.setValue(value); + } + + public int getLeftMargin() { + return myReader.LeftMarginOption.getValue(); + } + + public void setLeftMargin(int value) { + myReader.LeftMarginOption.setValue(value); + } + + public int getRightMargin() { + return myReader.RightMarginOption.getValue(); + } + + public void setRightMargin(int value) { + myReader.RightMarginOption.setValue(value); + } + public int getParagraphsNumber() { return myReader.Model.getTextModel().getParagraphsNumber(); } - public int getElementsNumber(int paragraphIndex) { + public int getParagraphElementsCount(int paragraphIndex) { final ZLTextWordCursor cursor = new ZLTextWordCursor(myReader.getTextView().getStartCursor()); cursor.moveToParagraph(paragraphIndex); cursor.moveToParagraphEnd(); @@ -402,6 +462,36 @@ public class ApiServerImplementation extends ApiInterface.Stub implements Api, A return sb.toString(); } + public List getParagraphWordsCount(int paragraphIndex) { + final ArrayList words = new ArrayList(); + final ZLTextWordCursor cursor = new ZLTextWordCursor(myReader.getTextView().getStartCursor()); + cursor.moveToParagraph(paragraphIndex); + cursor.moveToParagraphStart(); + while (!cursor.isEndOfParagraph()) { + ZLTextElement element = cursor.getElement(); + if (element instanceof ZLTextWord) { + words.add(element.toString()); + } + cursor.nextWord(); + } + return words; + } + + public ArrayList getParagraphWordIndices(int paragraphIndex) { + final ArrayList indices = new ArrayList(); + final ZLTextWordCursor cursor = new ZLTextWordCursor(myReader.getTextView().getStartCursor()); + cursor.moveToParagraph(paragraphIndex); + cursor.moveToParagraphStart(); + while (!cursor.isEndOfParagraph()) { + ZLTextElement element = cursor.getElement(); + if (element instanceof ZLTextWord) { + indices.add(cursor.getElementIndex()); + } + cursor.nextWord(); + } + return indices; + } + // action control public List listActions() { // TODO: implement @@ -427,11 +517,11 @@ public class ApiServerImplementation extends ApiInterface.Stub implements Api, A } public String getZoneMap() { - return ScrollingPreferences.Instance().TapZoneMapOption.getValue(); + return ScrollingPreferences.Instance().TapZoneMapOption.getValue(); } public void setZoneMap(String name) { - ScrollingPreferences.Instance().TapZoneMapOption.setValue(name); + ScrollingPreferences.Instance().TapZoneMapOption.setValue(name); } public int getZoneMapHeight(String name) {