diff --git a/src/org/geometerplus/android/fbreader/api/ApiImplementation.java b/src/org/geometerplus/android/fbreader/api/ApiImplementation.java index 8486d421f..47ffa024d 100644 --- a/src/org/geometerplus/android/fbreader/api/ApiImplementation.java +++ b/src/org/geometerplus/android/fbreader/api/ApiImplementation.java @@ -26,16 +26,34 @@ import org.geometerplus.fbreader.fbreader.FBReaderApp; public class ApiImplementation extends ApiInterface.Stub { private final FBReaderApp myReader = (FBReaderApp)FBReaderApp.Instance(); - @Override - public int getPageStartParagraphIndex() { - // TODO: check for NPE - return myReader.getTextView().getStartCursor().getParagraphIndex(); + private TextPosition getTextPosition(ZLTextWordCursor cursor) { + return new TextPosition( + cursor.getParagraphIndex(), + cursor.getElementIndex(), + cursor.getCharIndex() + ); } @Override - public int getMaxParagraphIndex() { + 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.getViewWidget().repaint(); + } + + @Override + public int getParagraphsNumber() { // TODO: check for NPE - return myReader.Model.BookTextModel.getParagraphsNumber() - 1; + return myReader.Model.BookTextModel.getParagraphsNumber(); } @Override diff --git a/src/org/geometerplus/android/fbreader/api/ApiInterface.aidl b/src/org/geometerplus/android/fbreader/api/ApiInterface.aidl index 9873005fc..218c94d5c 100644 --- a/src/org/geometerplus/android/fbreader/api/ApiInterface.aidl +++ b/src/org/geometerplus/android/fbreader/api/ApiInterface.aidl @@ -19,10 +19,15 @@ package org.geometerplus.android.fbreader.api; -interface ApiInterface { - int getPageStartParagraphIndex(); +import org.geometerplus.android.fbreader.api.TextPosition; - int getMaxParagraphIndex(); +interface ApiInterface { + int getParagraphsNumber(); + + TextPosition getPageStart(); + TextPosition getPageEnd(); + + void setPageStart(in TextPosition position); String getParagraphText(int paragraphIndex); } diff --git a/src/org/geometerplus/android/fbreader/api/ApiServiceConnection.java b/src/org/geometerplus/android/fbreader/api/ApiServiceConnection.java index 571107515..4a7f1585e 100644 --- a/src/org/geometerplus/android/fbreader/api/ApiServiceConnection.java +++ b/src/org/geometerplus/android/fbreader/api/ApiServiceConnection.java @@ -39,6 +39,16 @@ public class ApiServiceConnection implements ServiceConnection { } } + public synchronized void disconnect() { + if (myInterface != null) { + try { + myContext.unbindService(this); + } catch (IllegalArgumentException e) { + } + myInterface = null; + } + } + public synchronized void onServiceConnected(ComponentName className, IBinder service) { System.err.println("onServiceConnected call"); myInterface = ApiInterface.Stub.asInterface(service); @@ -51,23 +61,41 @@ public class ApiServiceConnection implements ServiceConnection { private void checkConnection() throws ApiException { if (myInterface == null) { - throw new ApiException("No connection with FBReader"); + throw new ApiException("Not connected to FBReader"); } } - public synchronized int getPageStartParagraphIndex() throws ApiException { + public synchronized TextPosition getPageStart() throws ApiException { checkConnection(); try { - return myInterface.getPageStartParagraphIndex(); + return myInterface.getPageStart(); } catch (android.os.RemoteException e) { throw new ApiException(e); } } - public synchronized int getMaxParagraphIndex() throws ApiException { + public synchronized TextPosition getPageEnd() throws ApiException { checkConnection(); try { - return myInterface.getMaxParagraphIndex(); + return myInterface.getPageEnd(); + } catch (android.os.RemoteException e) { + throw new ApiException(e); + } + } + + public synchronized void setPageStart(TextPosition position) throws ApiException { + checkConnection(); + try { + myInterface.setPageStart(position); + } catch (android.os.RemoteException e) { + throw new ApiException(e); + } + } + + public synchronized int getParagraphsNumber() throws ApiException { + checkConnection(); + try { + return myInterface.getParagraphsNumber(); } catch (android.os.RemoteException e) { throw new ApiException(e); } diff --git a/src/org/geometerplus/android/fbreader/api/TextPosition.aidl b/src/org/geometerplus/android/fbreader/api/TextPosition.aidl new file mode 100644 index 000000000..bcb57e2c7 --- /dev/null +++ b/src/org/geometerplus/android/fbreader/api/TextPosition.aidl @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2010-2011 Geometer Plus + * + * 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; diff --git a/src/org/geometerplus/android/fbreader/api/TextPosition.java b/src/org/geometerplus/android/fbreader/api/TextPosition.java new file mode 100644 index 000000000..707498b0d --- /dev/null +++ b/src/org/geometerplus/android/fbreader/api/TextPosition.java @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2010-2011 Geometer Plus + * + * 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; + +import android.os.Parcel; +import android.os.Parcelable; + +public final class TextPosition implements Parcelable { + public final int ParagraphIndex; + public final int ElementIndex; + public final int CharIndex; + + public TextPosition(int paragraphIndex, int elementIndex, int charIndex) { + ParagraphIndex = paragraphIndex; + ElementIndex = elementIndex; + CharIndex = charIndex; + } + + public int describeContents() { + return 0; + } + + public void writeToParcel(Parcel parcel, int flags) { + parcel.writeInt(ParagraphIndex); + parcel.writeInt(ElementIndex); + parcel.writeInt(CharIndex); + } + + public static final Parcelable.Creator CREATOR = + new Parcelable.Creator() { + public TextPosition createFromParcel(Parcel parcel) { + return new TextPosition(parcel.readInt(), parcel.readInt(), parcel.readInt()); + } + + public TextPosition[] newArray(int size) { + return new TextPosition[size]; + } + }; +}