mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-05 19:42:17 +02:00
synchronization with library-service branch
This commit is contained in:
parent
ff38a7b0cb
commit
2872059088
5 changed files with 546 additions and 26 deletions
|
@ -0,0 +1,328 @@
|
|||
/*
|
||||
* Copyright (C) 2007-2013 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.libraryService;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import android.content.*;
|
||||
import android.os.IBinder;
|
||||
import android.os.RemoteException;
|
||||
|
||||
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
||||
|
||||
import org.geometerplus.zlibrary.text.view.ZLTextFixedPosition;
|
||||
import org.geometerplus.zlibrary.text.view.ZLTextPosition;
|
||||
|
||||
import org.geometerplus.fbreader.book.*;
|
||||
|
||||
import org.geometerplus.android.fbreader.api.TextPosition;
|
||||
|
||||
public class BookCollectionShadow extends AbstractBookCollection implements ServiceConnection {
|
||||
private Context myContext;
|
||||
private volatile LibraryInterface myInterface;
|
||||
private Runnable myOnBindAction;
|
||||
|
||||
private final BroadcastReceiver myReceiver = new BroadcastReceiver() {
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (!hasListeners()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
final String type = intent.getStringExtra("type");
|
||||
if (LibraryService.BOOK_EVENT_ACTION.equals(intent.getAction())) {
|
||||
final Book book = SerializerUtil.deserializeBook(intent.getStringExtra("book"));
|
||||
fireBookEvent(Listener.BookEvent.valueOf(type), book);
|
||||
} else {
|
||||
fireBuildEvent(Listener.BuildEvent.valueOf(type));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public synchronized void bindToService(Context context, Runnable onBindAction) {
|
||||
if (myInterface != null && myContext == context) {
|
||||
if (onBindAction != null) {
|
||||
onBindAction.run();
|
||||
}
|
||||
} else {
|
||||
if (onBindAction != null) {
|
||||
myOnBindAction = onBindAction;
|
||||
}
|
||||
context.bindService(
|
||||
new Intent(context, LibraryService.class),
|
||||
this,
|
||||
LibraryService.BIND_AUTO_CREATE
|
||||
);
|
||||
myContext = context;
|
||||
}
|
||||
}
|
||||
|
||||
public void unbind() {
|
||||
if (myContext != null && myInterface != null) {
|
||||
myContext.unregisterReceiver(myReceiver);
|
||||
myContext.unbindService(this);
|
||||
myInterface = null;
|
||||
myContext = null;
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized int size() {
|
||||
if (myInterface == null) {
|
||||
return 0;
|
||||
}
|
||||
try {
|
||||
return myInterface.size();
|
||||
} catch (RemoteException e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized List<Book> books() {
|
||||
if (myInterface == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
try {
|
||||
return SerializerUtil.deserializeBookList(myInterface.books());
|
||||
} catch (RemoteException e) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized List<Book> books(String pattern) {
|
||||
if (myInterface == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
try {
|
||||
return SerializerUtil.deserializeBookList(myInterface.booksForPattern(pattern));
|
||||
} catch (RemoteException e) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized List<Book> recentBooks() {
|
||||
if (myInterface == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
try {
|
||||
return SerializerUtil.deserializeBookList(myInterface.recentBooks());
|
||||
} catch (RemoteException e) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized List<Book> favorites() {
|
||||
if (myInterface == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
try {
|
||||
return SerializerUtil.deserializeBookList(myInterface.favorites());
|
||||
} catch (RemoteException e) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized Book getRecentBook(int index) {
|
||||
if (myInterface == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return SerializerUtil.deserializeBook(myInterface.getRecentBook(index));
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized Book getBookByFile(ZLFile file) {
|
||||
if (myInterface == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return SerializerUtil.deserializeBook(myInterface.getBookByFile(file.getPath()));
|
||||
} catch (RemoteException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized Book getBookById(long id) {
|
||||
if (myInterface == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return SerializerUtil.deserializeBook(myInterface.getBookById(id));
|
||||
} catch (RemoteException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized boolean saveBook(Book book, boolean force) {
|
||||
if (myInterface == null) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
return myInterface.saveBook(SerializerUtil.serialize(book), force);
|
||||
} catch (RemoteException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void removeBook(Book book, boolean deleteFromDisk) {
|
||||
if (myInterface != null) {
|
||||
try {
|
||||
myInterface.removeBook(SerializerUtil.serialize(book), deleteFromDisk);
|
||||
} catch (RemoteException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void addBookToRecentList(Book book) {
|
||||
if (myInterface != null) {
|
||||
try {
|
||||
myInterface.addBookToRecentList(SerializerUtil.serialize(book));
|
||||
} catch (RemoteException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void setBookFavorite(Book book, boolean favorite) {
|
||||
if (myInterface != null) {
|
||||
try {
|
||||
myInterface.setBookFavorite(SerializerUtil.serialize(book), favorite);
|
||||
} catch (RemoteException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized ZLTextPosition getStoredPosition(long bookId) {
|
||||
if (myInterface == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
final TextPosition position = myInterface.getStoredPosition(bookId);
|
||||
if (position == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new ZLTextFixedPosition(
|
||||
position.ParagraphIndex, position.ElementIndex, position.CharIndex
|
||||
);
|
||||
} catch (RemoteException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void storePosition(long bookId, ZLTextPosition position) {
|
||||
if (position != null && myInterface != null) {
|
||||
try {
|
||||
myInterface.storePosition(bookId, new TextPosition(
|
||||
position.getParagraphIndex(), position.getElementIndex(), position.getCharIndex()
|
||||
));
|
||||
} catch (RemoteException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized boolean isHyperlinkVisited(Book book, String linkId) {
|
||||
if (myInterface == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
return myInterface.isHyperlinkVisited(SerializerUtil.serialize(book), linkId);
|
||||
} catch (RemoteException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void markHyperlinkAsVisited(Book book, String linkId) {
|
||||
if (myInterface != null) {
|
||||
try {
|
||||
myInterface.markHyperlinkAsVisited(SerializerUtil.serialize(book), linkId);
|
||||
} catch (RemoteException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized List<Bookmark> invisibleBookmarks(Book book) {
|
||||
if (myInterface == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
try {
|
||||
return SerializerUtil.deserializeBookmarkList(
|
||||
myInterface.invisibleBookmarks(SerializerUtil.serialize(book))
|
||||
);
|
||||
} catch (RemoteException e) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized List<Bookmark> allBookmarks() {
|
||||
if (myInterface == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
try {
|
||||
return SerializerUtil.deserializeBookmarkList(myInterface.allBookmarks());
|
||||
} catch (RemoteException e) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void saveBookmark(Bookmark bookmark) {
|
||||
if (myInterface != null) {
|
||||
// try {
|
||||
// bookmark.update(SerializerUtil.deserializeBookmark(
|
||||
// myInterface.saveBookmark(SerializerUtil.serialize(bookmark))
|
||||
// ));
|
||||
// } catch (RemoteException e) {
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void deleteBookmark(Bookmark bookmark) {
|
||||
if (myInterface != null) {
|
||||
try {
|
||||
myInterface.deleteBookmark(SerializerUtil.serialize(bookmark));
|
||||
} catch (RemoteException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// method from ServiceConnection interface
|
||||
public synchronized void onServiceConnected(ComponentName name, IBinder service) {
|
||||
myInterface = LibraryInterface.Stub.asInterface(service);
|
||||
if (myOnBindAction != null) {
|
||||
myOnBindAction.run();
|
||||
myOnBindAction = null;
|
||||
}
|
||||
if (myContext != null) {
|
||||
myContext.registerReceiver(myReceiver, new IntentFilter(LibraryService.BOOK_EVENT_ACTION));
|
||||
myContext.registerReceiver(myReceiver, new IntentFilter(LibraryService.BUILD_EVENT_ACTION));
|
||||
}
|
||||
}
|
||||
|
||||
// method from ServiceConnection interface
|
||||
public synchronized void onServiceDisconnected(ComponentName name) {
|
||||
}
|
||||
}
|
|
@ -4,6 +4,32 @@
|
|||
|
||||
package org.geometerplus.android.fbreader.libraryService;
|
||||
|
||||
import java.util.List;
|
||||
import org.geometerplus.android.fbreader.api.TextPosition;
|
||||
|
||||
interface LibraryInterface {
|
||||
boolean isUpToDate();
|
||||
int size();
|
||||
List<String> books();
|
||||
List<String> booksForPattern(in String pattern);
|
||||
List<String> recentBooks();
|
||||
List<String> favorites();
|
||||
String getBookByFile(in String file);
|
||||
String getBookById(in long id);
|
||||
String getRecentBook(in int index);
|
||||
|
||||
boolean saveBook(in String book, in boolean force);
|
||||
void removeBook(in String book, in boolean deleteFromDisk);
|
||||
void addBookToRecentList(in String book);
|
||||
void setBookFavorite(in String book, in boolean favorite);
|
||||
|
||||
TextPosition getStoredPosition(in long bookId);
|
||||
void storePosition(in long bookId, in TextPosition position);
|
||||
|
||||
boolean isHyperlinkVisited(in String book, in String linkId);
|
||||
void markHyperlinkAsVisited(in String book, in String linkId);
|
||||
|
||||
List<String> invisibleBookmarks(in String book);
|
||||
List<String> allBookmarks();
|
||||
String saveBookmark(in String bookmark);
|
||||
void deleteBookmark(in String bookmark);
|
||||
}
|
||||
|
|
|
@ -19,32 +19,197 @@
|
|||
|
||||
package org.geometerplus.android.fbreader.libraryService;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.LinkedList;
|
||||
|
||||
import android.app.Service;
|
||||
import android.content.Intent;
|
||||
import android.os.IBinder;
|
||||
import android.os.FileObserver;
|
||||
|
||||
import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
||||
|
||||
import org.geometerplus.zlibrary.text.view.ZLTextPosition;
|
||||
import org.geometerplus.zlibrary.text.view.ZLTextFixedPosition;
|
||||
|
||||
import org.geometerplus.fbreader.book.*;
|
||||
import org.geometerplus.fbreader.library.Library;
|
||||
|
||||
public class LibraryService extends Service implements Library.ChangeListener {
|
||||
public final class LibraryImplementation extends LibraryInterface.Stub {
|
||||
private final Library myBaseLibrary;
|
||||
import org.geometerplus.android.fbreader.api.TextPosition;
|
||||
|
||||
LibraryImplementation() {
|
||||
BooksDatabase database = SQLiteBooksDatabase.Instance();
|
||||
if (database == null) {
|
||||
database = new SQLiteBooksDatabase(LibraryService.this, "LIBRARY SERVICE");
|
||||
}
|
||||
myBaseLibrary = new Library(database);
|
||||
((Library)myBaseLibrary).startBuild();
|
||||
public class LibraryService extends Service {
|
||||
static String BOOK_EVENT_ACTION = "fbreader.library-service.book-event";
|
||||
static String BUILD_EVENT_ACTION = "fbreader.library-service.build-event";
|
||||
|
||||
private static final class Observer extends FileObserver {
|
||||
private static final int MASK =
|
||||
MOVE_SELF | MOVED_TO | MOVED_FROM | DELETE_SELF | DELETE | CLOSE_WRITE | ATTRIB;
|
||||
|
||||
public Observer(String path) {
|
||||
super(path, MASK);
|
||||
}
|
||||
|
||||
public boolean isUpToDate() {
|
||||
return myBaseLibrary.isUpToDate();
|
||||
@Override
|
||||
public void onEvent(int event, String path) {
|
||||
event = event & ALL_EVENTS;
|
||||
System.err.println("Event " + event + " on " + path);
|
||||
switch (event) {
|
||||
case MOVE_SELF:
|
||||
// TODO: File(path) removed; stop watching (?)
|
||||
break;
|
||||
case MOVED_TO:
|
||||
// TODO: File(path) removed; File(path) added
|
||||
break;
|
||||
case MOVED_FROM:
|
||||
case DELETE:
|
||||
// TODO: File(path) removed
|
||||
break;
|
||||
case DELETE_SELF:
|
||||
// TODO: File(path) removed; watching is stopped automatically (?)
|
||||
break;
|
||||
case CLOSE_WRITE:
|
||||
case ATTRIB:
|
||||
// TODO: File(path) changed (added, removed?)
|
||||
break;
|
||||
default:
|
||||
System.err.println("Unexpected event " + event + " on " + path);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private LibraryImplementation myLibrary;
|
||||
public final class LibraryImplementation extends LibraryInterface.Stub {
|
||||
private final BookCollection myCollection;
|
||||
private final List<FileObserver> myFileObservers = new LinkedList<FileObserver>();
|
||||
|
||||
LibraryImplementation() {
|
||||
myCollection = null;//new BookCollection(SQLiteBooksDatabase.Instance(LibraryService.this));
|
||||
//for (String path : myCollection.bookDirectories()) {
|
||||
// final Observer observer = new Observer(path);
|
||||
// observer.startWatching();
|
||||
// myFileObservers.add(observer);
|
||||
//}
|
||||
|
||||
myCollection.addListener(new BookCollection.Listener() {
|
||||
public void onBookEvent(BookEvent event, Book book) {
|
||||
final Intent intent = new Intent(BOOK_EVENT_ACTION);
|
||||
intent.putExtra("type", event.toString());
|
||||
intent.putExtra("book", SerializerUtil.serialize(book));
|
||||
sendBroadcast(intent);
|
||||
}
|
||||
|
||||
public void onBuildEvent(BuildEvent event) {
|
||||
final Intent intent = new Intent(BUILD_EVENT_ACTION);
|
||||
intent.putExtra("type", event.toString());
|
||||
sendBroadcast(intent);
|
||||
}
|
||||
});
|
||||
//myCollection.startBuild();
|
||||
}
|
||||
|
||||
public void deactivate() {
|
||||
for (FileObserver observer : myFileObservers) {
|
||||
observer.stopWatching();
|
||||
}
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return myCollection.size();
|
||||
}
|
||||
|
||||
public List<String> books() {
|
||||
return SerializerUtil.serializeBookList(myCollection.books());
|
||||
}
|
||||
|
||||
public List<String> booksForPattern(String pattern) {
|
||||
return SerializerUtil.serializeBookList(myCollection.books(pattern));
|
||||
}
|
||||
|
||||
public List<String> recentBooks() {
|
||||
return SerializerUtil.serializeBookList(myCollection.recentBooks());
|
||||
}
|
||||
|
||||
public List<String> favorites() {
|
||||
return SerializerUtil.serializeBookList(myCollection.favorites());
|
||||
}
|
||||
|
||||
public String getRecentBook(int index) {
|
||||
return SerializerUtil.serialize(myCollection.getRecentBook(index));
|
||||
}
|
||||
|
||||
public String getBookByFile(String file) {
|
||||
return SerializerUtil.serialize(myCollection.getBookByFile(ZLFile.createFileByPath(file)));
|
||||
}
|
||||
|
||||
public String getBookById(long id) {
|
||||
return SerializerUtil.serialize(myCollection.getBookById(id));
|
||||
}
|
||||
|
||||
public boolean saveBook(String book, boolean force) {
|
||||
return myCollection.saveBook(SerializerUtil.deserializeBook(book), force);
|
||||
}
|
||||
|
||||
public void removeBook(String book, boolean deleteFromDisk) {
|
||||
myCollection.removeBook(SerializerUtil.deserializeBook(book), deleteFromDisk);
|
||||
}
|
||||
|
||||
public void addBookToRecentList(String book) {
|
||||
myCollection.addBookToRecentList(SerializerUtil.deserializeBook(book));
|
||||
}
|
||||
|
||||
public void setBookFavorite(String book, boolean favorite) {
|
||||
myCollection.setBookFavorite(SerializerUtil.deserializeBook(book), favorite);
|
||||
}
|
||||
|
||||
public TextPosition getStoredPosition(long bookId) {
|
||||
final ZLTextPosition position = myCollection.getStoredPosition(bookId);
|
||||
if (position == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new TextPosition(
|
||||
position.getParagraphIndex(), position.getElementIndex(), position.getCharIndex()
|
||||
);
|
||||
}
|
||||
|
||||
public void storePosition(long bookId, TextPosition position) {
|
||||
if (position == null) {
|
||||
return;
|
||||
}
|
||||
myCollection.storePosition(bookId, new ZLTextFixedPosition(
|
||||
position.ParagraphIndex, position.ElementIndex, position.CharIndex
|
||||
));
|
||||
}
|
||||
|
||||
public boolean isHyperlinkVisited(String book, String linkId) {
|
||||
return myCollection.isHyperlinkVisited(SerializerUtil.deserializeBook(book), linkId);
|
||||
}
|
||||
|
||||
public void markHyperlinkAsVisited(String book, String linkId) {
|
||||
myCollection.markHyperlinkAsVisited(SerializerUtil.deserializeBook(book), linkId);
|
||||
}
|
||||
|
||||
public List<String> invisibleBookmarks(String book) {
|
||||
return SerializerUtil.serializeBookmarkList(
|
||||
myCollection.invisibleBookmarks(SerializerUtil.deserializeBook(book))
|
||||
);
|
||||
}
|
||||
|
||||
public List<String> allBookmarks() {
|
||||
return SerializerUtil.serializeBookmarkList(myCollection.allBookmarks());
|
||||
}
|
||||
|
||||
public String saveBookmark(String serialized) {
|
||||
final Bookmark bookmark = SerializerUtil.deserializeBookmark(serialized);
|
||||
myCollection.saveBookmark(bookmark);
|
||||
return SerializerUtil.serialize(bookmark);
|
||||
}
|
||||
|
||||
public void deleteBookmark(String serialized) {
|
||||
myCollection.deleteBookmark(SerializerUtil.deserializeBookmark(serialized));
|
||||
}
|
||||
}
|
||||
|
||||
private volatile LibraryImplementation myLibrary;
|
||||
|
||||
@Override
|
||||
public void onStart(Intent intent, int startId) {
|
||||
|
@ -53,33 +218,26 @@ public class LibraryService extends Service implements Library.ChangeListener {
|
|||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
System.err.println("LibraryService started for intent " + intent);
|
||||
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
System.err.println("LibraryService binded for intent " + intent);
|
||||
return myLibrary;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
System.err.println("LibraryService.onCreate()");
|
||||
super.onCreate();
|
||||
myLibrary = new LibraryImplementation();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
System.err.println("LibraryService.onDestroy()");
|
||||
myLibrary = null;
|
||||
if (myLibrary != null) {
|
||||
myLibrary.deactivate();
|
||||
myLibrary = null;
|
||||
}
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
public void onLibraryChanged(final Code code) {
|
||||
// TODO: implement signal sending
|
||||
System.err.println("LibraryService.onLibraryChanged(" + code + "): " + myLibrary.isUpToDate());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue