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:
parent
726c28111d
commit
61da8d3c66
5 changed files with 100 additions and 100 deletions
|
@ -23,12 +23,43 @@ import org.geometerplus.zlibrary.text.view.*;
|
||||||
|
|
||||||
import org.geometerplus.fbreader.fbreader.FBReaderApp;
|
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();
|
private final FBReaderApp myReader = (FBReaderApp)FBReaderApp.Instance();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getBookLanguage() {
|
public ApiObject request(int method, ApiObject[] parameters) {
|
||||||
// TODO: check for NPE
|
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();
|
return myReader.Model.Book.getLanguage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,38 +71,23 @@ public class ApiImplementation extends ApiInterface.Stub {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private void setPageStart(TextPosition position) {
|
||||||
public TextPosition getPageStart() {
|
|
||||||
return getTextPosition(myReader.getTextView().getStartCursor());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public TextPosition getPageEnd() {
|
|
||||||
return getTextPosition(myReader.getTextView().getEndCursor());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setPageStart(TextPosition position) {
|
|
||||||
myReader.getTextView().gotoPosition(position.ParagraphIndex, position.ElementIndex, position.CharIndex);
|
myReader.getTextView().gotoPosition(position.ParagraphIndex, position.ElementIndex, position.CharIndex);
|
||||||
myReader.getViewWidget().repaint();
|
myReader.getViewWidget().repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private int getParagraphsNumber() {
|
||||||
public int getParagraphsNumber() {
|
|
||||||
// TODO: check for NPE
|
|
||||||
return myReader.Model.BookTextModel.getParagraphsNumber();
|
return myReader.Model.BookTextModel.getParagraphsNumber();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private int getElementsNumber(int paragraphIndex) {
|
||||||
public int getElementsNumber(int paragraphIndex) {
|
|
||||||
final ZLTextWordCursor cursor = new ZLTextWordCursor(myReader.getTextView().getStartCursor());
|
final ZLTextWordCursor cursor = new ZLTextWordCursor(myReader.getTextView().getStartCursor());
|
||||||
cursor.moveToParagraph(paragraphIndex);
|
cursor.moveToParagraph(paragraphIndex);
|
||||||
cursor.moveToParagraphEnd();
|
cursor.moveToParagraphEnd();
|
||||||
return cursor.getElementIndex();
|
return cursor.getElementIndex();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private String getParagraphText(int paragraphIndex) {
|
||||||
public String getParagraphText(int paragraphIndex) {
|
|
||||||
final StringBuffer sb = new StringBuffer();
|
final StringBuffer sb = new StringBuffer();
|
||||||
final ZLTextWordCursor cursor = new ZLTextWordCursor(myReader.getTextView().getStartCursor());
|
final ZLTextWordCursor cursor = new ZLTextWordCursor(myReader.getTextView().getStartCursor());
|
||||||
cursor.moveToParagraph(paragraphIndex);
|
cursor.moveToParagraph(paragraphIndex);
|
||||||
|
|
|
@ -19,20 +19,8 @@
|
||||||
|
|
||||||
package org.geometerplus.android.fbreader.api;
|
package org.geometerplus.android.fbreader.api;
|
||||||
|
|
||||||
import org.geometerplus.android.fbreader.api.TextPosition;
|
import org.geometerplus.android.fbreader.api.ApiObject;
|
||||||
|
|
||||||
interface ApiInterface {
|
interface ApiInterface {
|
||||||
//ApiObject request(String name, ApiObject[] parameters);
|
ApiObject request(int method, in ApiObject[] parameters);
|
||||||
|
|
||||||
int getParagraphsNumber();
|
|
||||||
int getElementsNumber(int paragraphIndex);
|
|
||||||
|
|
||||||
String getBookLanguage();
|
|
||||||
|
|
||||||
TextPosition getPageStart();
|
|
||||||
TextPosition getPageEnd();
|
|
||||||
|
|
||||||
void setPageStart(in TextPosition position);
|
|
||||||
|
|
||||||
String getParagraphText(int paragraphIndex);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,7 @@ package org.geometerplus.android.fbreader.api;
|
||||||
import android.content.*;
|
import android.content.*;
|
||||||
import android.os.IBinder;
|
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 static String ACTION_API = "android.fbreader.action.API";
|
||||||
|
|
||||||
private final Context myContext;
|
private final Context myContext;
|
||||||
|
@ -59,63 +59,77 @@ public class ApiServiceConnection implements ServiceConnection {
|
||||||
myInterface = null;
|
myInterface = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkConnection() throws ApiException {
|
private synchronized ApiObject request(int method, ApiObject[] params) throws ApiException {
|
||||||
if (myInterface == null) {
|
if (myInterface == null) {
|
||||||
throw new ApiException("Not connected to FBReader");
|
throw new ApiException("Not connected to FBReader");
|
||||||
}
|
}
|
||||||
}
|
final ApiObject object;
|
||||||
|
|
||||||
public synchronized String getBookLanguage() throws ApiException {
|
|
||||||
checkConnection();
|
|
||||||
try {
|
try {
|
||||||
return myInterface.getBookLanguage();
|
object = myInterface.request(method, params);
|
||||||
} catch (android.os.RemoteException e) {
|
} catch (android.os.RemoteException e) {
|
||||||
throw new ApiException(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 {
|
private String requestString(int method, ApiObject[] params) throws ApiException {
|
||||||
checkConnection();
|
final ApiObject object = request(method, params);
|
||||||
try {
|
if (!(object instanceof ApiObject.String)) {
|
||||||
return myInterface.getPageStart();
|
throw new ApiException("Cannot cast return type of method " + method + " to String");
|
||||||
} catch (android.os.RemoteException e) {
|
|
||||||
throw new ApiException(e);
|
|
||||||
}
|
}
|
||||||
|
return ((ApiObject.String)object).Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized TextPosition getPageEnd() throws ApiException {
|
private int requestInt(int method, ApiObject[] params) throws ApiException {
|
||||||
checkConnection();
|
final ApiObject object = request(method, params);
|
||||||
try {
|
if (!(object instanceof ApiObject.Integer)) {
|
||||||
return myInterface.getPageEnd();
|
throw new ApiException("Cannot cast return type of method " + method + " to int");
|
||||||
} catch (android.os.RemoteException e) {
|
|
||||||
throw new ApiException(e);
|
|
||||||
}
|
}
|
||||||
|
return ((ApiObject.Integer)object).Value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void setPageStart(TextPosition position) throws ApiException {
|
private TextPosition requestTextPosition(int method, ApiObject[] params) throws ApiException {
|
||||||
checkConnection();
|
final ApiObject object = request(method, params);
|
||||||
try {
|
if (!(object instanceof TextPosition)) {
|
||||||
myInterface.setPageStart(position);
|
throw new ApiException("Cannot cast return type of method " + method + " to TextPosition");
|
||||||
} catch (android.os.RemoteException e) {
|
|
||||||
throw new ApiException(e);
|
|
||||||
}
|
}
|
||||||
|
return (TextPosition)object;
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized int getParagraphsNumber() throws ApiException {
|
private static final ApiObject[] EMPTY_PARAMETERS = new ApiObject[0];
|
||||||
checkConnection();
|
|
||||||
try {
|
private static ApiObject[] envelope(int value) {
|
||||||
return myInterface.getParagraphsNumber();
|
return new ApiObject[] { ApiObject.envelope(value) };
|
||||||
} catch (android.os.RemoteException e) {
|
|
||||||
throw new ApiException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized String getParagraphText(int paragraphIndex) throws ApiException {
|
public String getBookLanguage() throws ApiException {
|
||||||
checkConnection();
|
return requestString(GET_BOOK_LANGUAGE, EMPTY_PARAMETERS);
|
||||||
try {
|
}
|
||||||
return myInterface.getParagraphText(paragraphIndex);
|
|
||||||
} catch (android.os.RemoteException e) {
|
public TextPosition getPageStart() throws ApiException {
|
||||||
throw new ApiException(e);
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
|
|
@ -22,7 +22,7 @@ package org.geometerplus.android.fbreader.api;
|
||||||
import android.os.Parcel;
|
import android.os.Parcel;
|
||||||
import android.os.Parcelable;
|
import android.os.Parcelable;
|
||||||
|
|
||||||
public final class TextPosition implements Parcelable {
|
public final class TextPosition extends ApiObject {
|
||||||
public final int ParagraphIndex;
|
public final int ParagraphIndex;
|
||||||
public final int ElementIndex;
|
public final int ElementIndex;
|
||||||
public final int CharIndex;
|
public final int CharIndex;
|
||||||
|
@ -33,11 +33,14 @@ public final class TextPosition implements Parcelable {
|
||||||
CharIndex = charIndex;
|
CharIndex = charIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int describeContents() {
|
@Override
|
||||||
return 0;
|
protected int type() {
|
||||||
|
return Type.TEXT_POSITION;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void writeToParcel(Parcel parcel, int flags) {
|
public void writeToParcel(Parcel parcel, int flags) {
|
||||||
|
super.writeToParcel(parcel, flags);
|
||||||
parcel.writeInt(ParagraphIndex);
|
parcel.writeInt(ParagraphIndex);
|
||||||
parcel.writeInt(ElementIndex);
|
parcel.writeInt(ElementIndex);
|
||||||
parcel.writeInt(CharIndex);
|
parcel.writeInt(CharIndex);
|
||||||
|
@ -46,6 +49,7 @@ public final class TextPosition implements Parcelable {
|
||||||
public static final Parcelable.Creator<TextPosition> CREATOR =
|
public static final Parcelable.Creator<TextPosition> CREATOR =
|
||||||
new Parcelable.Creator<TextPosition>() {
|
new Parcelable.Creator<TextPosition>() {
|
||||||
public TextPosition createFromParcel(Parcel parcel) {
|
public TextPosition createFromParcel(Parcel parcel) {
|
||||||
|
parcel.readInt();
|
||||||
return new TextPosition(parcel.readInt(), parcel.readInt(), parcel.readInt());
|
return new TextPosition(parcel.readInt(), parcel.readInt(), parcel.readInt());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue