mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-04 02:09:35 +02:00
API implementation (in progress)
This commit is contained in:
parent
c48df4b0f0
commit
92929e6752
5 changed files with 143 additions and 14 deletions
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
22
src/org/geometerplus/android/fbreader/api/TextPosition.aidl
Normal file
22
src/org/geometerplus/android/fbreader/api/TextPosition.aidl
Normal file
|
@ -0,0 +1,22 @@
|
|||
/*
|
||||
* 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;
|
56
src/org/geometerplus/android/fbreader/api/TextPosition.java
Normal file
56
src/org/geometerplus/android/fbreader/api/TextPosition.java
Normal file
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* 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;
|
||||
|
||||
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<TextPosition> CREATOR =
|
||||
new Parcelable.Creator<TextPosition>() {
|
||||
public TextPosition createFromParcel(Parcel parcel) {
|
||||
return new TextPosition(parcel.readInt(), parcel.readInt(), parcel.readInt());
|
||||
}
|
||||
|
||||
public TextPosition[] newArray(int size) {
|
||||
return new TextPosition[size];
|
||||
}
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue