mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-03 17:59: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.fbreader.*;
|
||||
import org.geometerplus.android.fbreader.libraryService.SQLiteBooksDatabase;
|
||||
import org.geometerplus.android.fbreader.libraryService.BookCollectionShadow;
|
||||
import org.geometerplus.android.fbreader.tree.TreeActivity;
|
||||
|
||||
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);
|
||||
|
||||
if (myLibrary == null) {
|
||||
BooksDatabase db = SQLiteBooksDatabase.Instance();
|
||||
if (db == null) {
|
||||
db = new SQLiteBooksDatabase(this, "LIBRARY");
|
||||
}
|
||||
final BookCollection collection = new BookCollection(db);
|
||||
myLibrary = new Library(collection);
|
||||
myLibrary = new Library(new BookCollectionShadow(this));
|
||||
myLibrary.addChangeListener(this);
|
||||
collection.startBuild();
|
||||
}
|
||||
|
||||
mySelectedBook =
|
||||
|
@ -78,6 +72,16 @@ public class LibraryActivity extends TreeActivity implements MenuItem.OnMenuItem
|
|||
getListView().setOnCreateContextMenuListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
((BookCollectionShadow)myLibrary.Collection).bindToService(new Runnable() {
|
||||
public void run() {
|
||||
myLibrary.init();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onNewIntent(Intent intent) {
|
||||
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();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
((BookCollectionShadow)myLibrary.Collection).unbind();
|
||||
super.onStop();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
myLibrary.removeChangeListener(this);
|
||||
|
|
|
@ -82,11 +82,7 @@ public class LibraryService extends Service {
|
|||
private final List<FileObserver> myFileObservers = new LinkedList<FileObserver>();
|
||||
|
||||
LibraryImplementation() {
|
||||
BooksDatabase database = SQLiteBooksDatabase.Instance();
|
||||
if (database == null) {
|
||||
database = new SQLiteBooksDatabase(LibraryService.this, "LIBRARY SERVICE");
|
||||
}
|
||||
myCollection = new BookCollection(database);
|
||||
myCollection = new BookCollection(SQLiteBooksDatabase.Instance(LibraryService.this));
|
||||
for (String path : myCollection.bookDirectories()) {
|
||||
final Observer observer = new Observer(path);
|
||||
observer.startWatching();
|
||||
|
|
|
@ -37,17 +37,23 @@ import org.geometerplus.zlibrary.text.view.ZLTextFixedPosition;
|
|||
|
||||
import org.geometerplus.fbreader.book.*;
|
||||
|
||||
import org.geometerplus.android.util.UIUtil;
|
||||
import org.geometerplus.android.util.SQLiteUtil;
|
||||
|
||||
public final class SQLiteBooksDatabase extends BooksDatabase {
|
||||
private final String myInstanceId;
|
||||
final class SQLiteBooksDatabase extends BooksDatabase {
|
||||
private static BooksDatabase ourInstance;
|
||||
|
||||
static BooksDatabase Instance(Context context) {
|
||||
if (ourInstance == null) {
|
||||
ourInstance = new SQLiteBooksDatabase(context);
|
||||
}
|
||||
return ourInstance;
|
||||
}
|
||||
|
||||
private final SQLiteDatabase myDatabase;
|
||||
|
||||
public SQLiteBooksDatabase(Context context, String instanceId) {
|
||||
myInstanceId = instanceId;
|
||||
private SQLiteBooksDatabase(Context context) {
|
||||
myDatabase = context.openOrCreateDatabase("books.db", Context.MODE_PRIVATE, null);
|
||||
migrate(context);
|
||||
migrate();
|
||||
}
|
||||
|
||||
protected void executeAsATransaction(Runnable actions) {
|
||||
|
@ -69,65 +75,61 @@ public final class SQLiteBooksDatabase extends BooksDatabase {
|
|||
}
|
||||
}
|
||||
|
||||
private void migrate(Context context) {
|
||||
private void migrate() {
|
||||
final int version = myDatabase.getVersion();
|
||||
final int currentVersion = 20;
|
||||
if (version >= currentVersion) {
|
||||
return;
|
||||
}
|
||||
UIUtil.wait(version == 0 ? "creatingBooksDatabase" : "updatingBooksDatabase", new Runnable() {
|
||||
public void run() {
|
||||
myDatabase.beginTransaction();
|
||||
myDatabase.beginTransaction();
|
||||
|
||||
switch (myDatabase.getVersion()) {
|
||||
case 0:
|
||||
createTables();
|
||||
case 1:
|
||||
updateTables1();
|
||||
case 2:
|
||||
updateTables2();
|
||||
case 3:
|
||||
updateTables3();
|
||||
case 4:
|
||||
updateTables4();
|
||||
case 5:
|
||||
updateTables5();
|
||||
case 6:
|
||||
updateTables6();
|
||||
case 7:
|
||||
updateTables7();
|
||||
case 8:
|
||||
updateTables8();
|
||||
case 9:
|
||||
updateTables9();
|
||||
case 10:
|
||||
updateTables10();
|
||||
case 11:
|
||||
updateTables11();
|
||||
case 12:
|
||||
updateTables12();
|
||||
case 13:
|
||||
updateTables13();
|
||||
case 14:
|
||||
updateTables14();
|
||||
case 15:
|
||||
updateTables15();
|
||||
case 16:
|
||||
updateTables16();
|
||||
case 17:
|
||||
updateTables17();
|
||||
case 18:
|
||||
updateTables18();
|
||||
case 19:
|
||||
updateTables19();
|
||||
}
|
||||
myDatabase.setTransactionSuccessful();
|
||||
myDatabase.setVersion(currentVersion);
|
||||
myDatabase.endTransaction();
|
||||
switch (myDatabase.getVersion()) {
|
||||
case 0:
|
||||
createTables();
|
||||
case 1:
|
||||
updateTables1();
|
||||
case 2:
|
||||
updateTables2();
|
||||
case 3:
|
||||
updateTables3();
|
||||
case 4:
|
||||
updateTables4();
|
||||
case 5:
|
||||
updateTables5();
|
||||
case 6:
|
||||
updateTables6();
|
||||
case 7:
|
||||
updateTables7();
|
||||
case 8:
|
||||
updateTables8();
|
||||
case 9:
|
||||
updateTables9();
|
||||
case 10:
|
||||
updateTables10();
|
||||
case 11:
|
||||
updateTables11();
|
||||
case 12:
|
||||
updateTables12();
|
||||
case 13:
|
||||
updateTables13();
|
||||
case 14:
|
||||
updateTables14();
|
||||
case 15:
|
||||
updateTables15();
|
||||
case 16:
|
||||
updateTables16();
|
||||
case 17:
|
||||
updateTables17();
|
||||
case 18:
|
||||
updateTables18();
|
||||
case 19:
|
||||
updateTables19();
|
||||
}
|
||||
myDatabase.setTransactionSuccessful();
|
||||
myDatabase.setVersion(currentVersion);
|
||||
myDatabase.endTransaction();
|
||||
|
||||
myDatabase.execSQL("VACUUM");
|
||||
}
|
||||
}, context);
|
||||
myDatabase.execSQL("VACUUM");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -35,6 +35,7 @@ public class BookCollection extends AbstractBookCollection {
|
|||
Collections.synchronizedMap(new LinkedHashMap<ZLFile,Book>());
|
||||
private final Map<Long,Book> myBooksById =
|
||||
Collections.synchronizedMap(new HashMap<Long,Book>());
|
||||
|
||||
private static enum BuildStatus {
|
||||
NotStarted,
|
||||
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();
|
||||
}
|
||||
|
||||
|
|
|
@ -26,16 +26,6 @@ import org.geometerplus.zlibrary.core.filesystem.ZLFile;
|
|||
import org.geometerplus.zlibrary.text.view.ZLTextPosition;
|
||||
|
||||
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) {
|
||||
final FileInfoSet infos = new FileInfoSet(this, fileId);
|
||||
return createBook(id, infos.getFile(fileId), title, encoding, language);
|
||||
|
|
|
@ -113,12 +113,6 @@ public final class Library {
|
|||
switch (event) {
|
||||
case Started:
|
||||
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);
|
||||
break;
|
||||
case Completed:
|
||||
|
@ -137,6 +131,15 @@ public final class Library {
|
|||
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() {
|
||||
return myRootTree;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue