1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-04 02:09:35 +02:00

API inteface has been changed; API is now extendable

This commit is contained in:
Nikolay Pultsin 2011-06-18 21:40:51 +01:00
parent 726c28111d
commit 61da8d3c66
5 changed files with 100 additions and 100 deletions

View file

@ -23,12 +23,43 @@ import org.geometerplus.zlibrary.text.view.*;
import org.geometerplus.fbreader.fbreader.FBReaderApp;
public class ApiImplementation extends ApiInterface.Stub {
public class ApiImplementation extends ApiInterface.Stub implements ApiMethods {
private final FBReaderApp myReader = (FBReaderApp)FBReaderApp.Instance();
@Override
public String getBookLanguage() {
// TODO: check for NPE
public ApiObject request(int method, ApiObject[] parameters) {
try {
switch (method) {
case GET_BOOK_LANGUAGE:
return ApiObject.envelope(getBookLanguage());
case GET_PARAGRAPHS_NUMBER:
return ApiObject.envelope(getParagraphsNumber());
case GET_ELEMENTS_NUMBER:
return ApiObject.envelope(getElementsNumber(
((ApiObject.Integer)parameters[0]).Value
));
case GET_PARAGRAPH_TEXT:
return ApiObject.envelope(getParagraphText(
((ApiObject.Integer)parameters[0]).Value
));
case GET_PAGE_START:
return getTextPosition(myReader.getTextView().getStartCursor());
case GET_PAGE_END:
return getTextPosition(myReader.getTextView().getEndCursor());
case SET_PAGE_START:
setPageStart(
(TextPosition)parameters[0]
);
return ApiObject.Void.Instance;
default:
return new ApiObject.Error("Unsupported method code: " + method);
}
} catch (Throwable e) {
return new ApiObject.Error("Exception in method " + method + ": " + e.getMessage());
}
}
private String getBookLanguage() {
return myReader.Model.Book.getLanguage();
}
@ -40,38 +71,23 @@ public class ApiImplementation extends ApiInterface.Stub {
);
}
@Override
public TextPosition getPageStart() {
return getTextPosition(myReader.getTextView().getStartCursor());
}
@Override
public TextPosition getPageEnd() {
return getTextPosition(myReader.getTextView().getEndCursor());
}
@Override
public void setPageStart(TextPosition position) {
private void setPageStart(TextPosition position) {
myReader.getTextView().gotoPosition(position.ParagraphIndex, position.ElementIndex, position.CharIndex);
myReader.getViewWidget().repaint();
}
@Override
public int getParagraphsNumber() {
// TODO: check for NPE
private int getParagraphsNumber() {
return myReader.Model.BookTextModel.getParagraphsNumber();
}
@Override
public int getElementsNumber(int paragraphIndex) {
private int getElementsNumber(int paragraphIndex) {
final ZLTextWordCursor cursor = new ZLTextWordCursor(myReader.getTextView().getStartCursor());
cursor.moveToParagraph(paragraphIndex);
cursor.moveToParagraphEnd();
return cursor.getElementIndex();
}
@Override
public String getParagraphText(int paragraphIndex) {
private String getParagraphText(int paragraphIndex) {
final StringBuffer sb = new StringBuffer();
final ZLTextWordCursor cursor = new ZLTextWordCursor(myReader.getTextView().getStartCursor());
cursor.moveToParagraph(paragraphIndex);

View file

@ -19,20 +19,8 @@
package org.geometerplus.android.fbreader.api;
import org.geometerplus.android.fbreader.api.TextPosition;
import org.geometerplus.android.fbreader.api.ApiObject;
interface ApiInterface {
//ApiObject request(String name, ApiObject[] parameters);
int getParagraphsNumber();
int getElementsNumber(int paragraphIndex);
String getBookLanguage();
TextPosition getPageStart();
TextPosition getPageEnd();
void setPageStart(in TextPosition position);
String getParagraphText(int paragraphIndex);
ApiObject request(int method, in ApiObject[] parameters);
}

View file

@ -22,7 +22,7 @@ package org.geometerplus.android.fbreader.api;
import android.content.*;
import android.os.IBinder;
public class ApiServiceConnection implements ServiceConnection {
public class ApiServiceConnection implements ServiceConnection, ApiMethods {
private static String ACTION_API = "android.fbreader.action.API";
private final Context myContext;
@ -59,63 +59,77 @@ public class ApiServiceConnection implements ServiceConnection {
myInterface = null;
}
private void checkConnection() throws ApiException {
private synchronized ApiObject request(int method, ApiObject[] params) throws ApiException {
if (myInterface == null) {
throw new ApiException("Not connected to FBReader");
}
}
public synchronized String getBookLanguage() throws ApiException {
checkConnection();
final ApiObject object;
try {
return myInterface.getBookLanguage();
object = myInterface.request(method, params);
} catch (android.os.RemoteException e) {
throw new ApiException(e);
}
if (object instanceof ApiObject.Error) {
throw new ApiException(((ApiObject.Error)object).Message);
}
return object;
}
public synchronized TextPosition getPageStart() throws ApiException {
checkConnection();
try {
return myInterface.getPageStart();
} catch (android.os.RemoteException e) {
throw new ApiException(e);
private String requestString(int method, ApiObject[] params) throws ApiException {
final ApiObject object = request(method, params);
if (!(object instanceof ApiObject.String)) {
throw new ApiException("Cannot cast return type of method " + method + " to String");
}
return ((ApiObject.String)object).Value;
}
public synchronized TextPosition getPageEnd() throws ApiException {
checkConnection();
try {
return myInterface.getPageEnd();
} catch (android.os.RemoteException e) {
throw new ApiException(e);
private int requestInt(int method, ApiObject[] params) throws ApiException {
final ApiObject object = request(method, params);
if (!(object instanceof ApiObject.Integer)) {
throw new ApiException("Cannot cast return type of method " + method + " to int");
}
return ((ApiObject.Integer)object).Value;
}
public synchronized void setPageStart(TextPosition position) throws ApiException {
checkConnection();
try {
myInterface.setPageStart(position);
} catch (android.os.RemoteException e) {
throw new ApiException(e);
private TextPosition requestTextPosition(int method, ApiObject[] params) throws ApiException {
final ApiObject object = request(method, params);
if (!(object instanceof TextPosition)) {
throw new ApiException("Cannot cast return type of method " + method + " to TextPosition");
}
return (TextPosition)object;
}
public synchronized int getParagraphsNumber() throws ApiException {
checkConnection();
try {
return myInterface.getParagraphsNumber();
} catch (android.os.RemoteException e) {
throw new ApiException(e);
}
private static final ApiObject[] EMPTY_PARAMETERS = new ApiObject[0];
private static ApiObject[] envelope(int value) {
return new ApiObject[] { ApiObject.envelope(value) };
}
public synchronized String getParagraphText(int paragraphIndex) throws ApiException {
checkConnection();
try {
return myInterface.getParagraphText(paragraphIndex);
} catch (android.os.RemoteException e) {
throw new ApiException(e);
}
public String getBookLanguage() throws ApiException {
return requestString(GET_BOOK_LANGUAGE, EMPTY_PARAMETERS);
}
public TextPosition getPageStart() throws ApiException {
return requestTextPosition(GET_PAGE_START, EMPTY_PARAMETERS);
}
public TextPosition getPageEnd() throws ApiException {
return requestTextPosition(GET_PAGE_END, EMPTY_PARAMETERS);
}
public void setPageStart(TextPosition position) throws ApiException {
request(SET_PAGE_START, new ApiObject[] { position });
}
public int getParagraphsNumber() throws ApiException {
return requestInt(GET_PARAGRAPHS_NUMBER, EMPTY_PARAMETERS);
}
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));
}
}

View file

@ -1,22 +0,0 @@
/*
* Copyright (C) 2010-2011 Geometer Plus <contact@geometerplus.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
package org.geometerplus.android.fbreader.api;
parcelable TextPosition;

View file

@ -22,7 +22,7 @@ package org.geometerplus.android.fbreader.api;
import android.os.Parcel;
import android.os.Parcelable;
public final class TextPosition implements Parcelable {
public final class TextPosition extends ApiObject {
public final int ParagraphIndex;
public final int ElementIndex;
public final int CharIndex;
@ -33,11 +33,14 @@ public final class TextPosition implements Parcelable {
CharIndex = charIndex;
}
public int describeContents() {
return 0;
@Override
protected int type() {
return Type.TEXT_POSITION;
}
@Override
public void writeToParcel(Parcel parcel, int flags) {
super.writeToParcel(parcel, flags);
parcel.writeInt(ParagraphIndex);
parcel.writeInt(ElementIndex);
parcel.writeInt(CharIndex);
@ -46,6 +49,7 @@ public final class TextPosition implements Parcelable {
public static final Parcelable.Creator<TextPosition> CREATOR =
new Parcelable.Creator<TextPosition>() {
public TextPosition createFromParcel(Parcel parcel) {
parcel.readInt();
return new TextPosition(parcel.readInt(), parcel.readInt(), parcel.readInt());
}