Skip to content

Commit

Permalink
updated API
Browse files Browse the repository at this point in the history
  • Loading branch information
geometer committed Nov 17, 2012
1 parent 0f2b1a8 commit e932ca3
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 11 deletions.
12 changes: 11 additions & 1 deletion src/org/geometerplus/android/fbreader/api/Api.java
Expand Up @@ -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;
List<String> getParagraphWords(int paragraphIndex) throws ApiException;
List<Integer> getParagraphWordIndices(int paragraphIndex) throws ApiException;

// page information
TextPosition getPageStart() throws ApiException;
Expand All @@ -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<String> listActions() throws ApiException;
Expand Down
Expand Up @@ -171,6 +171,18 @@ private List<String> requestStringList(int method, ApiObject[] params) throws Ap
return stringList;
}

private List<Integer> requestIntegerList(int method, ApiObject[] params) throws ApiException {
final List<ApiObject> list = requestList(method, params);
final ArrayList<Integer> intList = new ArrayList<Integer>(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) {
Expand All @@ -189,7 +201,7 @@ private static ApiObject[] envelope(List<String> value) {
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;
}
Expand Down Expand Up @@ -302,8 +314,16 @@ public String getParagraphText(int paragraphIndex) throws ApiException {
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<String> getParagraphWords(int paragraphIndex) throws ApiException {
return requestStringList(GET_PARAGRAPH_WORDS, envelope(paragraphIndex));
}

public List<Integer> getParagraphWordIndices(int paragraphIndex) throws ApiException {
return requestIntegerList(GET_PARAGRAPH_WORD_INDICES, envelope(paragraphIndex));
}

public void setPageStart(TextPosition position) throws ApiException {
Expand All @@ -318,6 +338,38 @@ public void clearHighlighting() throws ApiException {
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[] {
Expand All @@ -335,23 +387,23 @@ public void setKeyAction(int key, boolean longPress, String action) throws ApiEx
}

public List<String> listActions() throws ApiException {
return requestStringList(LIST_ACTIONS, EMPTY_PARAMETERS);
return requestStringList(LIST_ACTIONS, EMPTY_PARAMETERS);
}

public List<String> listActionNames(List<String> actions) throws ApiException {
return requestStringList(LIST_ACTION_NAMES, envelope(actions));
return requestStringList(LIST_ACTION_NAMES, envelope(actions));
}

public List<String> 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 {
Expand Down
12 changes: 11 additions & 1 deletion src/org/geometerplus/android/fbreader/api/ApiMethods.java
Expand Up @@ -32,8 +32,10 @@ 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 = 604;
int GET_PARAGRAPH_WORD_INDICES = 605;

// page information
int GET_PAGE_START = 701;
Expand All @@ -45,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;
Expand Down
10 changes: 9 additions & 1 deletion src/org/geometerplus/android/fbreader/api/ApiObject.java
Expand Up @@ -168,14 +168,22 @@ static ApiObject envelope(java.util.Date value) {
return new Date(value);
}

static List<ApiObject> envelope(List<java.lang.String> values) {
static List<ApiObject> envelopeStringList(List<java.lang.String> values) {
final ArrayList<ApiObject> objects = new ArrayList<ApiObject>(values.size());
for (java.lang.String v : values) {
objects.add(new String(v));
}
return objects;
}

static List<ApiObject> envelopeIntegerList(List<java.lang.Integer> values) {
final ArrayList<ApiObject> objects = new ArrayList<ApiObject>(values.size());
for (java.lang.Integer v : values) {
objects.add(new Integer(v));
}
return objects;
}

abstract protected int type();

public int describeContents() {
Expand Down

0 comments on commit e932ca3

Please sign in to comment.