mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-04 18:29:23 +02:00
event processing in BookCollectionShadow
This commit is contained in:
parent
d17f318110
commit
315b75e241
4 changed files with 100 additions and 59 deletions
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
* 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.fbreader.book;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public abstract class AbstractBookCollection implements IBookCollection {
|
||||
private final List<Listener> myListeners = Collections.synchronizedList(new LinkedList<Listener>());
|
||||
|
||||
public void addListener(Listener listener) {
|
||||
myListeners.add(listener);
|
||||
}
|
||||
|
||||
public void removeListener(Listener listener) {
|
||||
myListeners.remove(listener);
|
||||
}
|
||||
|
||||
protected boolean hasListeners() {
|
||||
return !myListeners.isEmpty();
|
||||
}
|
||||
|
||||
protected void fireBookEvent(Listener.BookEvent event, Book book) {
|
||||
synchronized (myListeners) {
|
||||
for (Listener l : myListeners) {
|
||||
l.onBookEvent(event, book);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void fireBuildEvent(Listener.BuildEvent event) {
|
||||
synchronized (myListeners) {
|
||||
for (Listener l : myListeners) {
|
||||
l.onBuildEvent(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -29,31 +29,7 @@ import org.geometerplus.zlibrary.text.view.ZLTextPosition;
|
|||
import org.geometerplus.fbreader.Paths;
|
||||
import org.geometerplus.fbreader.bookmodel.BookReadingException;
|
||||
|
||||
public class BookCollection implements IBookCollection {
|
||||
private final List<Listener> myListeners = Collections.synchronizedList(new LinkedList<Listener>());
|
||||
|
||||
public void addListener(Listener listener) {
|
||||
myListeners.add(listener);
|
||||
}
|
||||
|
||||
public void removeListener(Listener listener) {
|
||||
myListeners.remove(listener);
|
||||
}
|
||||
|
||||
protected void fireBookEvent(Listener.BookEvent event, Book book) {
|
||||
synchronized (myListeners) {
|
||||
for (Listener l : myListeners) {
|
||||
l.onBookEvent(event, book);
|
||||
}
|
||||
}
|
||||
}
|
||||
protected void fireBuildEvent(Listener.BuildEvent event) {
|
||||
synchronized (myListeners) {
|
||||
for (Listener l : myListeners) {
|
||||
l.onBuildEvent(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
public class BookCollection extends AbstractBookCollection {
|
||||
private final BooksDatabase myDatabase;
|
||||
private final Map<ZLFile,Book> myBooksByFile =
|
||||
Collections.synchronizedMap(new LinkedHashMap<ZLFile,Book>());
|
||||
|
@ -174,6 +150,17 @@ public class BookCollection implements IBookCollection {
|
|||
}
|
||||
|
||||
public boolean saveBook(Book book, boolean force) {
|
||||
if (book == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
synchronized (myBooksByFile) {
|
||||
final boolean replace = myBooksByFile.remove(book.File) != null;
|
||||
myBooksById.remove(book.getId());
|
||||
myBooksByFile.put(book.File, book);
|
||||
addBookById(book);
|
||||
fireBookEvent(replace ? Listener.BookEvent.Updated : Listener.BookEvent.Added, book);
|
||||
}
|
||||
return book.save(myDatabase, force);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue