1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-05 02:39:23 +02:00

event processing in BookCollectionShadow

This commit is contained in:
Nikolay Pultsin 2013-01-13 18:53:04 +04:00
parent d17f318110
commit 315b75e241
4 changed files with 100 additions and 59 deletions

View file

@ -35,11 +35,31 @@ import org.geometerplus.fbreader.book.*;
import org.geometerplus.android.fbreader.api.TextPosition;
public class BookCollectionShadow implements IBookCollection, ServiceConnection {
public class BookCollectionShadow extends AbstractBookCollection implements ServiceConnection {
private final 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 BookCollectionShadow(Context context) {
myContext = context;
}
@ -65,17 +85,10 @@ public class BookCollectionShadow implements IBookCollection, ServiceConnection
if (myInterface != null) {
myContext.unbindService(this);
myInterface = null;
myContext.unregisterReceiver(myReceiver);
}
}
public void addListener(Listener listener) {
// TODO: implement
}
public void removeListener(Listener listener) {
// TODO: implement
}
public synchronized int size() {
if (myInterface == null) {
return 0;
@ -294,6 +307,8 @@ public class BookCollectionShadow implements IBookCollection, ServiceConnection
myOnBindAction.run();
myOnBindAction = null;
}
myContext.registerReceiver(myReceiver, new IntentFilter(LibraryService.BOOK_EVENT_ACTION));
myContext.registerReceiver(myReceiver, new IntentFilter(LibraryService.BUILD_EVENT_ACTION));
}
// method from ServiceConnection interface

View file

@ -37,6 +37,9 @@ import org.geometerplus.fbreader.book.*;
import org.geometerplus.android.fbreader.api.TextPosition;
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;
@ -90,31 +93,18 @@ public class LibraryService extends Service {
myFileObservers.add(observer);
}
final long start = System.currentTimeMillis();
myCollection.addListener(new BookCollection.Listener() {
public void onBookEvent(BookEvent event, Book book) {
switch (event) {
case Added:
System.err.println("Added " + book.getTitle());
break;
}
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) {
switch (event) {
case Started:
System.err.println("Build started");
break;
case Succeeded:
System.err.println("Build succeeded");
break;
case Failed:
System.err.println("Build failed");
break;
case Completed:
System.err.println("Build completed with " + myCollection.size() + " books in " + (System.currentTimeMillis() - start) + " milliseconds");
break;
}
final Intent intent = new Intent(BUILD_EVENT_ACTION);
intent.putExtra("type", event.toString());
sendBroadcast(intent);
}
});
myCollection.startBuild();
@ -228,27 +218,22 @@ public class LibraryService extends Service {
@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()");
if (myLibrary != null) {
myLibrary.deactivate();
myLibrary = null;