mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-04 10:19:33 +02:00
LibraryActivity uses BookCollectionShadow
This commit is contained in:
parent
b8d50dc537
commit
42a38f3ab9
6 changed files with 90 additions and 88 deletions
|
@ -41,7 +41,7 @@ import org.geometerplus.fbreader.tree.FBTree;
|
||||||
|
|
||||||
import org.geometerplus.android.util.UIUtil;
|
import org.geometerplus.android.util.UIUtil;
|
||||||
import org.geometerplus.android.fbreader.*;
|
import org.geometerplus.android.fbreader.*;
|
||||||
import org.geometerplus.android.fbreader.libraryService.SQLiteBooksDatabase;
|
import org.geometerplus.android.fbreader.libraryService.BookCollectionShadow;
|
||||||
import org.geometerplus.android.fbreader.tree.TreeActivity;
|
import org.geometerplus.android.fbreader.tree.TreeActivity;
|
||||||
|
|
||||||
public class LibraryActivity extends TreeActivity implements MenuItem.OnMenuItemClickListener, View.OnCreateContextMenuListener, Library.ChangeListener {
|
public class LibraryActivity extends TreeActivity implements MenuItem.OnMenuItemClickListener, View.OnCreateContextMenuListener, Library.ChangeListener {
|
||||||
|
@ -57,14 +57,8 @@ public class LibraryActivity extends TreeActivity implements MenuItem.OnMenuItem
|
||||||
super.onCreate(icicle);
|
super.onCreate(icicle);
|
||||||
|
|
||||||
if (myLibrary == null) {
|
if (myLibrary == null) {
|
||||||
BooksDatabase db = SQLiteBooksDatabase.Instance();
|
myLibrary = new Library(new BookCollectionShadow(this));
|
||||||
if (db == null) {
|
|
||||||
db = new SQLiteBooksDatabase(this, "LIBRARY");
|
|
||||||
}
|
|
||||||
final BookCollection collection = new BookCollection(db);
|
|
||||||
myLibrary = new Library(collection);
|
|
||||||
myLibrary.addChangeListener(this);
|
myLibrary.addChangeListener(this);
|
||||||
collection.startBuild();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mySelectedBook =
|
mySelectedBook =
|
||||||
|
@ -78,6 +72,16 @@ public class LibraryActivity extends TreeActivity implements MenuItem.OnMenuItem
|
||||||
getListView().setOnCreateContextMenuListener(this);
|
getListView().setOnCreateContextMenuListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onStart() {
|
||||||
|
super.onStart();
|
||||||
|
((BookCollectionShadow)myLibrary.Collection).bindToService(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
myLibrary.init();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onNewIntent(Intent intent) {
|
protected void onNewIntent(Intent intent) {
|
||||||
if (START_SEARCH_ACTION.equals(intent.getAction())) {
|
if (START_SEARCH_ACTION.equals(intent.getAction())) {
|
||||||
|
@ -102,6 +106,12 @@ public class LibraryActivity extends TreeActivity implements MenuItem.OnMenuItem
|
||||||
return key != null ? myLibrary.getLibraryTree(key) : myLibrary.getRootTree();
|
return key != null ? myLibrary.getLibraryTree(key) : myLibrary.getRootTree();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onStop() {
|
||||||
|
((BookCollectionShadow)myLibrary.Collection).unbind();
|
||||||
|
super.onStop();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onDestroy() {
|
protected void onDestroy() {
|
||||||
myLibrary.removeChangeListener(this);
|
myLibrary.removeChangeListener(this);
|
||||||
|
|
|
@ -82,11 +82,7 @@ public class LibraryService extends Service {
|
||||||
private final List<FileObserver> myFileObservers = new LinkedList<FileObserver>();
|
private final List<FileObserver> myFileObservers = new LinkedList<FileObserver>();
|
||||||
|
|
||||||
LibraryImplementation() {
|
LibraryImplementation() {
|
||||||
BooksDatabase database = SQLiteBooksDatabase.Instance();
|
myCollection = new BookCollection(SQLiteBooksDatabase.Instance(LibraryService.this));
|
||||||
if (database == null) {
|
|
||||||
database = new SQLiteBooksDatabase(LibraryService.this, "LIBRARY SERVICE");
|
|
||||||
}
|
|
||||||
myCollection = new BookCollection(database);
|
|
||||||
for (String path : myCollection.bookDirectories()) {
|
for (String path : myCollection.bookDirectories()) {
|
||||||
final Observer observer = new Observer(path);
|
final Observer observer = new Observer(path);
|
||||||
observer.startWatching();
|
observer.startWatching();
|
||||||
|
|
|
@ -37,17 +37,23 @@ import org.geometerplus.zlibrary.text.view.ZLTextFixedPosition;
|
||||||
|
|
||||||
import org.geometerplus.fbreader.book.*;
|
import org.geometerplus.fbreader.book.*;
|
||||||
|
|
||||||
import org.geometerplus.android.util.UIUtil;
|
|
||||||
import org.geometerplus.android.util.SQLiteUtil;
|
import org.geometerplus.android.util.SQLiteUtil;
|
||||||
|
|
||||||
public final class SQLiteBooksDatabase extends BooksDatabase {
|
final class SQLiteBooksDatabase extends BooksDatabase {
|
||||||
private final String myInstanceId;
|
private static BooksDatabase ourInstance;
|
||||||
|
|
||||||
|
static BooksDatabase Instance(Context context) {
|
||||||
|
if (ourInstance == null) {
|
||||||
|
ourInstance = new SQLiteBooksDatabase(context);
|
||||||
|
}
|
||||||
|
return ourInstance;
|
||||||
|
}
|
||||||
|
|
||||||
private final SQLiteDatabase myDatabase;
|
private final SQLiteDatabase myDatabase;
|
||||||
|
|
||||||
public SQLiteBooksDatabase(Context context, String instanceId) {
|
private SQLiteBooksDatabase(Context context) {
|
||||||
myInstanceId = instanceId;
|
|
||||||
myDatabase = context.openOrCreateDatabase("books.db", Context.MODE_PRIVATE, null);
|
myDatabase = context.openOrCreateDatabase("books.db", Context.MODE_PRIVATE, null);
|
||||||
migrate(context);
|
migrate();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void executeAsATransaction(Runnable actions) {
|
protected void executeAsATransaction(Runnable actions) {
|
||||||
|
@ -69,14 +75,12 @@ public final class SQLiteBooksDatabase extends BooksDatabase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void migrate(Context context) {
|
private void migrate() {
|
||||||
final int version = myDatabase.getVersion();
|
final int version = myDatabase.getVersion();
|
||||||
final int currentVersion = 20;
|
final int currentVersion = 20;
|
||||||
if (version >= currentVersion) {
|
if (version >= currentVersion) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
UIUtil.wait(version == 0 ? "creatingBooksDatabase" : "updatingBooksDatabase", new Runnable() {
|
|
||||||
public void run() {
|
|
||||||
myDatabase.beginTransaction();
|
myDatabase.beginTransaction();
|
||||||
|
|
||||||
switch (myDatabase.getVersion()) {
|
switch (myDatabase.getVersion()) {
|
||||||
|
@ -127,8 +131,6 @@ public final class SQLiteBooksDatabase extends BooksDatabase {
|
||||||
|
|
||||||
myDatabase.execSQL("VACUUM");
|
myDatabase.execSQL("VACUUM");
|
||||||
}
|
}
|
||||||
}, context);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Book loadBook(long bookId) {
|
protected Book loadBook(long bookId) {
|
||||||
|
|
|
@ -35,6 +35,7 @@ public class BookCollection extends AbstractBookCollection {
|
||||||
Collections.synchronizedMap(new LinkedHashMap<ZLFile,Book>());
|
Collections.synchronizedMap(new LinkedHashMap<ZLFile,Book>());
|
||||||
private final Map<Long,Book> myBooksById =
|
private final Map<Long,Book> myBooksById =
|
||||||
Collections.synchronizedMap(new HashMap<Long,Book>());
|
Collections.synchronizedMap(new HashMap<Long,Book>());
|
||||||
|
|
||||||
private static enum BuildStatus {
|
private static enum BuildStatus {
|
||||||
NotStarted,
|
NotStarted,
|
||||||
Started,
|
Started,
|
||||||
|
@ -264,7 +265,7 @@ public class BookCollection extends AbstractBookCollection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
builder.setPriority((Thread.MIN_PRIORITY + Thread.NORM_PRIORITY) / 2);
|
builder.setPriority(Thread.MIN_PRIORITY);
|
||||||
builder.start();
|
builder.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,16 +26,6 @@ import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
||||||
import org.geometerplus.zlibrary.text.view.ZLTextPosition;
|
import org.geometerplus.zlibrary.text.view.ZLTextPosition;
|
||||||
|
|
||||||
public abstract class BooksDatabase {
|
public abstract class BooksDatabase {
|
||||||
private static BooksDatabase ourInstance;
|
|
||||||
|
|
||||||
public static BooksDatabase Instance() {
|
|
||||||
return ourInstance;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected BooksDatabase() {
|
|
||||||
ourInstance = this;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Book createBook(long id, long fileId, String title, String encoding, String language) {
|
protected Book createBook(long id, long fileId, String title, String encoding, String language) {
|
||||||
final FileInfoSet infos = new FileInfoSet(this, fileId);
|
final FileInfoSet infos = new FileInfoSet(this, fileId);
|
||||||
return createBook(id, infos.getFile(fileId), title, encoding, language);
|
return createBook(id, infos.getFile(fileId), title, encoding, language);
|
||||||
|
|
|
@ -113,12 +113,6 @@ public final class Library {
|
||||||
switch (event) {
|
switch (event) {
|
||||||
case Started:
|
case Started:
|
||||||
Library.this.fireModelChangedEvent(ChangeListener.Code.StatusChanged);
|
Library.this.fireModelChangedEvent(ChangeListener.Code.StatusChanged);
|
||||||
for (Book book : Collection.recentBooks()) {
|
|
||||||
new BookTree(getFirstLevelTree(ROOT_RECENT), book, true);
|
|
||||||
}
|
|
||||||
for (Book book : Collection.favorites()) {
|
|
||||||
new BookTree(getFirstLevelTree(ROOT_FAVORITES), book, true);
|
|
||||||
}
|
|
||||||
setStatus(myStatusMask | STATUS_LOADING);
|
setStatus(myStatusMask | STATUS_LOADING);
|
||||||
break;
|
break;
|
||||||
case Completed:
|
case Completed:
|
||||||
|
@ -137,6 +131,15 @@ public final class Library {
|
||||||
new FileFirstLevelTree(collection, myRootTree, ROOT_FILE_TREE);
|
new FileFirstLevelTree(collection, myRootTree, ROOT_FILE_TREE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void init() {
|
||||||
|
for (Book book : Collection.recentBooks()) {
|
||||||
|
new BookTree(getFirstLevelTree(ROOT_RECENT), book, true);
|
||||||
|
}
|
||||||
|
for (Book book : Collection.favorites()) {
|
||||||
|
new BookTree(getFirstLevelTree(ROOT_FAVORITES), book, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public LibraryTree getRootTree() {
|
public LibraryTree getRootTree() {
|
||||||
return myRootTree;
|
return myRootTree;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue