mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-04 02:09:35 +02:00
API enhancements
This commit is contained in:
parent
37924e0072
commit
9296cef55e
6 changed files with 95 additions and 30 deletions
|
@ -19,6 +19,8 @@
|
||||||
|
|
||||||
package org.geometerplus.android.fbreader.api;
|
package org.geometerplus.android.fbreader.api;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public interface Api {
|
public interface Api {
|
||||||
void connect();
|
void connect();
|
||||||
void disconnect();
|
void disconnect();
|
||||||
|
@ -29,8 +31,8 @@ public interface Api {
|
||||||
// book information
|
// book information
|
||||||
String getBookLanguage() throws ApiException;
|
String getBookLanguage() throws ApiException;
|
||||||
String getBookTitle() throws ApiException;
|
String getBookTitle() throws ApiException;
|
||||||
//String getBookAuthors() throws ApiException;
|
//List<String> getBookAuthors() throws ApiException;
|
||||||
//String getBookTags() throws ApiException;
|
List<String> getBookTags() throws ApiException;
|
||||||
String getBookFileName() throws ApiException;
|
String getBookFileName() throws ApiException;
|
||||||
|
|
||||||
// text information
|
// text information
|
||||||
|
@ -46,4 +48,6 @@ public interface Api {
|
||||||
|
|
||||||
// manage view
|
// manage view
|
||||||
void setPageStart(TextPosition position) throws ApiException;
|
void setPageStart(TextPosition position) throws ApiException;
|
||||||
|
void highlightArea(TextPosition start, TextPosition end) throws ApiException;
|
||||||
|
void clearHighlighting() throws ApiException;
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,16 +19,18 @@
|
||||||
|
|
||||||
package org.geometerplus.android.fbreader.api;
|
package org.geometerplus.android.fbreader.api;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
import android.content.*;
|
import android.content.*;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
|
|
||||||
public class ApiServiceConnection implements ServiceConnection, Api, ApiMethods {
|
public class ApiClientImplementation implements ServiceConnection, Api, 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;
|
||||||
private volatile ApiInterface myInterface;
|
private volatile ApiInterface myInterface;
|
||||||
|
|
||||||
public ApiServiceConnection(Context context) {
|
public ApiClientImplementation(Context context) {
|
||||||
myContext = context;
|
myContext = context;
|
||||||
connect();
|
connect();
|
||||||
}
|
}
|
||||||
|
@ -59,20 +61,38 @@ public class ApiServiceConnection implements ServiceConnection, Api, ApiMethods
|
||||||
myInterface = null;
|
myInterface = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized ApiObject request(int method, ApiObject[] params) throws ApiException {
|
private synchronized void checkConnection() 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;
|
|
||||||
try {
|
|
||||||
object = myInterface.request(method, params);
|
|
||||||
} catch (android.os.RemoteException e) {
|
|
||||||
throw new ApiException(e);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private synchronized ApiObject request(int method, ApiObject[] params) throws ApiException {
|
||||||
|
checkConnection();
|
||||||
|
try {
|
||||||
|
final ApiObject object = myInterface.request(method, params);
|
||||||
if (object instanceof ApiObject.Error) {
|
if (object instanceof ApiObject.Error) {
|
||||||
throw new ApiException(((ApiObject.Error)object).Message);
|
throw new ApiException(((ApiObject.Error)object).Message);
|
||||||
}
|
}
|
||||||
return object;
|
return object;
|
||||||
|
} catch (android.os.RemoteException e) {
|
||||||
|
throw new ApiException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private synchronized List<ApiObject> requestList(int method, ApiObject[] params) throws ApiException {
|
||||||
|
checkConnection();
|
||||||
|
try {
|
||||||
|
final List<ApiObject> list = myInterface.requestList(method, params);
|
||||||
|
for (ApiObject object : list) {
|
||||||
|
if (object instanceof ApiObject.Error) {
|
||||||
|
throw new ApiException(((ApiObject.Error)object).Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
} catch (android.os.RemoteException e) {
|
||||||
|
throw new ApiException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private String requestString(int method, ApiObject[] params) throws ApiException {
|
private String requestString(int method, ApiObject[] params) throws ApiException {
|
||||||
|
@ -107,6 +127,18 @@ public class ApiServiceConnection implements ServiceConnection, Api, ApiMethods
|
||||||
return (TextPosition)object;
|
return (TextPosition)object;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private List<String> requestStringList(int method, ApiObject[] params) throws ApiException {
|
||||||
|
final List<ApiObject> list = requestList(method, params);
|
||||||
|
final ArrayList<String> stringList = new ArrayList<String>(list.size());
|
||||||
|
for (ApiObject object : list) {
|
||||||
|
if (!(object instanceof ApiObject.String)) {
|
||||||
|
throw new ApiException("Cannot cast an element returned from method " + method + " to String");
|
||||||
|
}
|
||||||
|
stringList.add(((ApiObject.String)object).Value);
|
||||||
|
}
|
||||||
|
return stringList;
|
||||||
|
}
|
||||||
|
|
||||||
private static final ApiObject[] EMPTY_PARAMETERS = new ApiObject[0];
|
private static final ApiObject[] EMPTY_PARAMETERS = new ApiObject[0];
|
||||||
|
|
||||||
private static ApiObject[] envelope(int value) {
|
private static ApiObject[] envelope(int value) {
|
||||||
|
@ -125,6 +157,10 @@ public class ApiServiceConnection implements ServiceConnection, Api, ApiMethods
|
||||||
return requestString(GET_BOOK_TITLE, EMPTY_PARAMETERS);
|
return requestString(GET_BOOK_TITLE, EMPTY_PARAMETERS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<String> getBookTags() throws ApiException {
|
||||||
|
return requestStringList(GET_BOOK_TAGS, EMPTY_PARAMETERS);
|
||||||
|
}
|
||||||
|
|
||||||
public String getBookFileName() throws ApiException {
|
public String getBookFileName() throws ApiException {
|
||||||
return requestString(GET_BOOK_FILENAME, EMPTY_PARAMETERS);
|
return requestString(GET_BOOK_FILENAME, EMPTY_PARAMETERS);
|
||||||
}
|
}
|
||||||
|
@ -145,10 +181,6 @@ public class ApiServiceConnection implements ServiceConnection, Api, ApiMethods
|
||||||
return requestBoolean(IS_PAGE_END_OF_TEXT, EMPTY_PARAMETERS);
|
return requestBoolean(IS_PAGE_END_OF_TEXT, EMPTY_PARAMETERS);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPageStart(TextPosition position) throws ApiException {
|
|
||||||
request(SET_PAGE_START, new ApiObject[] { position });
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getParagraphsNumber() throws ApiException {
|
public int getParagraphsNumber() throws ApiException {
|
||||||
return requestInt(GET_PARAGRAPHS_NUMBER, EMPTY_PARAMETERS);
|
return requestInt(GET_PARAGRAPHS_NUMBER, EMPTY_PARAMETERS);
|
||||||
}
|
}
|
||||||
|
@ -160,4 +192,16 @@ public class ApiServiceConnection implements ServiceConnection, Api, ApiMethods
|
||||||
public int getElementsNumber(int paragraphIndex) throws ApiException {
|
public int getElementsNumber(int paragraphIndex) throws ApiException {
|
||||||
return requestInt(GET_ELEMENTS_NUMBER, envelope(paragraphIndex));
|
return requestInt(GET_ELEMENTS_NUMBER, envelope(paragraphIndex));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setPageStart(TextPosition position) throws ApiException {
|
||||||
|
request(SET_PAGE_START, new ApiObject[] { position });
|
||||||
|
}
|
||||||
|
|
||||||
|
public void highlightArea(TextPosition start, TextPosition end) throws ApiException {
|
||||||
|
request(HIGHLIGHT_AREA, new ApiObject[] { start, end });
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clearHighlighting() throws ApiException {
|
||||||
|
request(CLEAR_HIGHLIGHTING, EMPTY_PARAMETERS);
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -23,4 +23,6 @@ import org.geometerplus.android.fbreader.api.ApiObject;
|
||||||
|
|
||||||
interface ApiInterface {
|
interface ApiInterface {
|
||||||
ApiObject request(int method, in ApiObject[] parameters);
|
ApiObject request(int method, in ApiObject[] parameters);
|
||||||
|
List<ApiObject> requestList(int method, in ApiObject[] parameters);
|
||||||
|
Map requestMap(int method, in ApiObject[] parameters);
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,6 +49,8 @@ interface ApiMethods {
|
||||||
int IS_PAGE_END_OF_TEXT = 703;
|
int IS_PAGE_END_OF_TEXT = 703;
|
||||||
int IS_PAGE_END_OF_SECTION = 704;
|
int IS_PAGE_END_OF_SECTION = 704;
|
||||||
|
|
||||||
// view change
|
// view management
|
||||||
int SET_PAGE_START = 801;
|
int SET_PAGE_START = 801;
|
||||||
|
int HIGHLIGHT_AREA = 802;
|
||||||
|
int CLEAR_HIGHLIGHTING = 803;
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,31 +19,31 @@
|
||||||
|
|
||||||
package org.geometerplus.android.fbreader.api;
|
package org.geometerplus.android.fbreader.api;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
import org.geometerplus.zlibrary.core.library.ZLibrary;
|
import org.geometerplus.zlibrary.core.library.ZLibrary;
|
||||||
|
|
||||||
import org.geometerplus.zlibrary.text.view.*;
|
import org.geometerplus.zlibrary.text.view.*;
|
||||||
|
|
||||||
import org.geometerplus.fbreader.fbreader.FBReaderApp;
|
import org.geometerplus.fbreader.fbreader.FBReaderApp;
|
||||||
|
|
||||||
public class ApiImplementation extends ApiInterface.Stub implements ApiMethods {
|
public class ApiServerImplementation extends ApiInterface.Stub implements ApiMethods {
|
||||||
private final FBReaderApp myReader = (FBReaderApp)FBReaderApp.Instance();
|
private final FBReaderApp myReader = (FBReaderApp)FBReaderApp.Instance();
|
||||||
|
|
||||||
|
private ApiObject.Error unsupportedMethodError(int method) {
|
||||||
|
return new ApiObject.Error("Unsupported method code: " + method);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ApiObject request(int method, ApiObject[] parameters) {
|
public ApiObject request(int method, ApiObject[] parameters) {
|
||||||
try {
|
try {
|
||||||
switch (method) {
|
switch (method) {
|
||||||
case GET_FBREADER_VERSION:
|
case GET_FBREADER_VERSION:
|
||||||
return ApiObject.envelope(
|
return ApiObject.envelope(ZLibrary.Instance().getVersionName());
|
||||||
ZLibrary.Instance().getVersionName()
|
|
||||||
);
|
|
||||||
case GET_BOOK_LANGUAGE:
|
case GET_BOOK_LANGUAGE:
|
||||||
return ApiObject.envelope(
|
return ApiObject.envelope(myReader.Model.Book.getLanguage());
|
||||||
myReader.Model.Book.getLanguage()
|
|
||||||
);
|
|
||||||
case GET_BOOK_TITLE:
|
case GET_BOOK_TITLE:
|
||||||
return ApiObject.envelope(
|
return ApiObject.envelope(myReader.Model.Book.getTitle());
|
||||||
myReader.Model.Book.getTitle()
|
|
||||||
);
|
|
||||||
case GET_PARAGRAPHS_NUMBER:
|
case GET_PARAGRAPHS_NUMBER:
|
||||||
return ApiObject.envelope(getParagraphsNumber());
|
return ApiObject.envelope(getParagraphsNumber());
|
||||||
case GET_ELEMENTS_NUMBER:
|
case GET_ELEMENTS_NUMBER:
|
||||||
|
@ -63,18 +63,31 @@ public class ApiImplementation extends ApiInterface.Stub implements ApiMethods {
|
||||||
case IS_PAGE_END_OF_TEXT:
|
case IS_PAGE_END_OF_TEXT:
|
||||||
return ApiObject.envelope(isPageEndOfText());
|
return ApiObject.envelope(isPageEndOfText());
|
||||||
case SET_PAGE_START:
|
case SET_PAGE_START:
|
||||||
setPageStart(
|
setPageStart((TextPosition)parameters[0]);
|
||||||
(TextPosition)parameters[0]
|
return ApiObject.Void.Instance;
|
||||||
);
|
case HIGHLIGHT_AREA:
|
||||||
|
return ApiObject.Void.Instance;
|
||||||
|
case CLEAR_HIGHLIGHTING:
|
||||||
return ApiObject.Void.Instance;
|
return ApiObject.Void.Instance;
|
||||||
default:
|
default:
|
||||||
return new ApiObject.Error("Unsupported method code: " + method);
|
return unsupportedMethodError(method);
|
||||||
}
|
}
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
return new ApiObject.Error("Exception in method " + method + ": " + e);
|
return new ApiObject.Error("Exception in method " + method + ": " + e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<ApiObject> requestList(int method, ApiObject[] parameters) {
|
||||||
|
return Collections.<ApiObject>singletonList(unsupportedMethodError(method));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<ApiObject,ApiObject> requestMap(int method, ApiObject[] parameters) {
|
||||||
|
final ApiObject error = unsupportedMethodError(method);
|
||||||
|
return Collections.singletonMap(error, error);
|
||||||
|
}
|
||||||
|
|
||||||
private TextPosition getTextPosition(ZLTextWordCursor cursor) {
|
private TextPosition getTextPosition(ZLTextWordCursor cursor) {
|
||||||
return new TextPosition(
|
return new TextPosition(
|
||||||
cursor.getParagraphIndex(),
|
cursor.getParagraphIndex(),
|
|
@ -26,6 +26,6 @@ import android.os.IBinder;
|
||||||
public class ApiService extends Service {
|
public class ApiService extends Service {
|
||||||
@Override
|
@Override
|
||||||
public IBinder onBind(Intent intent) {
|
public IBinder onBind(Intent intent) {
|
||||||
return new ApiImplementation();
|
return new ApiServerImplementation();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue