mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-23 12:42:56 -04:00
#1449 migrate from original Bookmark to BookmarkEntity
This commit is contained in:
parent
84bdbec722
commit
941f02eefd
@ -32,6 +32,7 @@ import dagger.android.HasActivityInjector;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import javax.inject.Inject;
|
||||
import org.kiwix.kiwixmobile.data.local.KiwixDatabase;
|
||||
import org.kiwix.kiwixmobile.di.components.ApplicationComponent;
|
||||
import org.kiwix.kiwixmobile.di.components.DaggerApplicationComponent;
|
||||
|
||||
@ -46,7 +47,8 @@ public class KiwixApplication extends MultiDexApplication implements HasActivity
|
||||
|
||||
@Inject
|
||||
DispatchingAndroidInjector<Activity> activityInjector;
|
||||
private File logFile;
|
||||
@Inject
|
||||
KiwixDatabase kiwixDatabase;
|
||||
|
||||
public static KiwixApplication getInstance() {
|
||||
return application;
|
||||
@ -73,41 +75,36 @@ public class KiwixApplication extends MultiDexApplication implements HasActivity
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
AndroidThreeTen.init(this);
|
||||
writeLogFile();
|
||||
applicationComponent.inject(this);
|
||||
kiwixDatabase.forceMigration();
|
||||
if (BuildConfig.DEBUG) {
|
||||
StrictMode.setThreadPolicy(buildThreadPolicy(new StrictMode.ThreadPolicy.Builder()));
|
||||
StrictMode.setVmPolicy(buildVmPolicy(new StrictMode.VmPolicy.Builder()));
|
||||
}
|
||||
}
|
||||
|
||||
private void writeLogFile() {
|
||||
if (isExternalStorageWritable()) {
|
||||
File appDirectory = new File(Environment.getExternalStorageDirectory() + "/Kiwix");
|
||||
logFile = new File(appDirectory, "logcat.txt");
|
||||
File logFile = new File(appDirectory, "logcat.txt");
|
||||
Log.d("KIWIX", "Writing all logs into [" + logFile.getPath() + "]");
|
||||
|
||||
// create app folder
|
||||
if (!appDirectory.exists()) {
|
||||
appDirectory.mkdir();
|
||||
}
|
||||
|
||||
// create log folder
|
||||
if (!appDirectory.exists()) {
|
||||
appDirectory.mkdir();
|
||||
}
|
||||
|
||||
if (logFile.exists() && logFile.isFile()) {
|
||||
logFile.delete();
|
||||
}
|
||||
|
||||
// clear the previous logcat and then write the new one to the file
|
||||
try {
|
||||
logFile.createNewFile();
|
||||
Process process = Runtime.getRuntime().exec("logcat -c");
|
||||
process = Runtime.getRuntime().exec("logcat -f " + logFile.getPath() + " -s kiwix");
|
||||
Runtime.getRuntime().exec("logcat -c");
|
||||
Runtime.getRuntime().exec("logcat -f " + logFile.getPath() + " -s kiwix");
|
||||
} catch (IOException e) {
|
||||
Log.e("KIWIX", "Error while writing logcat.txt", e);
|
||||
}
|
||||
}
|
||||
|
||||
Log.d("KIWIX", "Started KiwixApplication");
|
||||
applicationComponent.inject(this);
|
||||
if (BuildConfig.DEBUG) {
|
||||
StrictMode.setThreadPolicy(buildThreadPolicy(new StrictMode.ThreadPolicy.Builder()));
|
||||
StrictMode.setVmPolicy(buildVmPolicy(new StrictMode.VmPolicy.Builder()));
|
||||
}
|
||||
}
|
||||
|
||||
private StrictMode.ThreadPolicy buildThreadPolicy(StrictMode.ThreadPolicy.Builder builder) {
|
||||
|
@ -7,10 +7,10 @@ data class BookmarkItem(
|
||||
val databaseId: Long = 0L,
|
||||
val zimId: String,
|
||||
val zimName: String,
|
||||
val zimFilePath: String,
|
||||
val zimFilePath: String?,
|
||||
val bookmarkUrl: String,
|
||||
val bookmarkTitle: String,
|
||||
val favicon: String
|
||||
val favicon: String?
|
||||
) {
|
||||
constructor(entity: BookmarkEntity) : this(
|
||||
entity.id,
|
||||
|
@ -29,7 +29,6 @@ import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.List;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
import org.kiwix.kiwixmobile.data.ZimContentProvider;
|
||||
@ -46,7 +45,6 @@ import org.kiwix.kiwixmobile.database.newdb.dao.NewBookDao;
|
||||
import org.kiwix.kiwixmobile.database.newdb.dao.NewBookmarksDao;
|
||||
import org.kiwix.kiwixmobile.database.newdb.dao.NewLanguagesDao;
|
||||
import org.kiwix.kiwixmobile.database.newdb.dao.NewRecentSearchDao;
|
||||
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity;
|
||||
import org.kiwix.kiwixmobile.utils.UpdateUtils;
|
||||
|
||||
import static org.kiwix.kiwixmobile.utils.Constants.TAG_KIWIX;
|
||||
@ -54,8 +52,8 @@ import static org.kiwix.kiwixmobile.utils.Constants.TAG_KIWIX;
|
||||
@Singleton
|
||||
public class KiwixDatabase extends SquidDatabase {
|
||||
|
||||
private static final int TWO_POINT_FIVE_POINT_THREE = 16;
|
||||
private static final int FINAL = 17;//3.0.0
|
||||
private static final int VERSION = FINAL;
|
||||
private final Context context;
|
||||
private final NewBookDao bookDao;
|
||||
private final NewLanguagesDao languagesDao;
|
||||
@ -81,11 +79,17 @@ public class KiwixDatabase extends SquidDatabase {
|
||||
|
||||
@Override
|
||||
protected Table[] getTables() {
|
||||
return new Table[] {};
|
||||
return new Table[] {
|
||||
RecentSearch.TABLE,
|
||||
Bookmark.TABLE,
|
||||
BookDatabaseEntity.TABLE,
|
||||
NetworkLanguageDatabaseEntity.TABLE
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean onUpgrade(SQLiteDatabaseWrapper db, int oldVersion, int newVersion) {
|
||||
Log.e("UPGRADE", "oldversion: " + oldVersion + " newVersion: " + newVersion);
|
||||
switch (oldVersion) {
|
||||
case 1:
|
||||
case 2:
|
||||
@ -127,9 +131,6 @@ public class KiwixDatabase extends SquidDatabase {
|
||||
tryCreateTable(BookDatabaseEntity.TABLE);
|
||||
case 14:
|
||||
case 15:
|
||||
tryAddColumn(Bookmark.ZIM_FILE_PATH);
|
||||
tryAddColumn(Bookmark.FAVICON);
|
||||
migrateBookmarksVersion16();
|
||||
try {
|
||||
bookDao.migrationInsert(new BookDao(this).getBooks());
|
||||
} catch (Exception e) {
|
||||
@ -143,11 +144,11 @@ public class KiwixDatabase extends SquidDatabase {
|
||||
tryDropTable(BookDatabaseEntity.TABLE);
|
||||
tryDropTable(NetworkLanguageDatabaseEntity.TABLE);
|
||||
tryDropTable(LibraryDatabaseEntity.TABLE);
|
||||
case 16:
|
||||
case TWO_POINT_FIVE_POINT_THREE:
|
||||
try {
|
||||
final BookmarksDao oldBookmarksDao = new BookmarksDao(this);
|
||||
oldBookmarksDao.processBookmark(UpdateUtils::reformatProviderUrl);
|
||||
this.bookmarksDao.migrationInsert(oldBookmarksDao.getBookmarks(false));
|
||||
this.bookmarksDao.migrationInsert(oldBookmarksDao.getBookmarks(false), bookDao);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -162,27 +163,9 @@ public class KiwixDatabase extends SquidDatabase {
|
||||
return true;
|
||||
}
|
||||
|
||||
private void migrateBookmarksVersion16() {
|
||||
BookmarksDao bookmarksDao = new BookmarksDao(this);
|
||||
BookDao bookDao = new BookDao(this);
|
||||
List<Bookmark> bookmarks = bookmarksDao.getBookmarks(false);
|
||||
List<LibraryNetworkEntity.Book> books = bookDao.getBooks();
|
||||
for (Bookmark bookmark : bookmarks) {
|
||||
if (bookmark.getZimId() != null) {
|
||||
for (LibraryNetworkEntity.Book book : books) {
|
||||
if (bookmark.getZimId().equals(book.getId())) {
|
||||
bookmark.setZimFilePath(book.getUrl()).setFavicon(book.getFavicon());
|
||||
bookmarksDao.saveBookmark(bookmark);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int getVersion() {
|
||||
return VERSION;
|
||||
return FINAL;
|
||||
}
|
||||
|
||||
public void migrateBookmarksVersion6() {
|
||||
@ -216,6 +199,14 @@ public class KiwixDatabase extends SquidDatabase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Now that the database is no longer used
|
||||
* we need to make a migration happen with an explicit call
|
||||
*/
|
||||
public void forceMigration() {
|
||||
beginTransaction();
|
||||
endTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -40,19 +40,6 @@ public class BookmarksDao {
|
||||
this.kiwixDatabase = kiwixDatabase;
|
||||
}
|
||||
|
||||
public void saveBookmark(Bookmark bookmark) {
|
||||
kiwixDatabase.deleteWhere(Bookmark.class, Bookmark.BOOKMARK_URL.eq(bookmark.getBookmarkUrl())
|
||||
.and(Bookmark.ZIM_ID.eq(bookmark.getZimId())));
|
||||
|
||||
kiwixDatabase.persist(new Bookmark()
|
||||
.setZimId(bookmark.getZimId())
|
||||
.setZimName(bookmark.getZimName())
|
||||
.setZimFilePath(bookmark.getZimFilePath())
|
||||
.setFavicon(bookmark.getFavicon())
|
||||
.setBookmarkUrl(bookmark.getBookmarkUrl())
|
||||
.setBookmarkTitle(bookmark.getBookmarkTitle()));
|
||||
}
|
||||
|
||||
public List<Bookmark> getBookmarks(boolean fromCurrentBook) {
|
||||
ArrayList<Bookmark> bookmarks = new ArrayList<>();
|
||||
Query query = Query.select();
|
||||
@ -63,14 +50,10 @@ public class BookmarksDao {
|
||||
.query(Bookmark.class, query.orderBy(Bookmark.BOOKMARK_TITLE.asc()))) {
|
||||
while (squidCursor.moveToNext()) {
|
||||
Bookmark bookmark = new Bookmark();
|
||||
|
||||
bookmark.setZimId(squidCursor.get(Bookmark.ZIM_ID));
|
||||
bookmark.setZimName(squidCursor.get(Bookmark.ZIM_NAME));
|
||||
bookmark.setZimFilePath(squidCursor.get(Bookmark.ZIM_FILE_PATH));
|
||||
bookmark.setFavicon(squidCursor.get(Bookmark.FAVICON));
|
||||
bookmark.setBookmarkTitle(squidCursor.get(Bookmark.BOOKMARK_TITLE));
|
||||
bookmark.setBookmarkUrl(squidCursor.get(Bookmark.BOOKMARK_URL));
|
||||
|
||||
bookmarks.add(bookmark);
|
||||
}
|
||||
}
|
||||
|
@ -28,8 +28,6 @@ public class BookmarksSpec {
|
||||
@ColumnSpec(constraints = "NOT NULL")
|
||||
public String ZimId;
|
||||
public String ZimName;
|
||||
public String zimFilePath;
|
||||
public String bookmarkUrl;
|
||||
public String bookmarkTitle;
|
||||
public String favicon;
|
||||
}
|
||||
|
@ -20,6 +20,7 @@ package org.kiwix.kiwixmobile.database.newdb.dao
|
||||
import io.objectbox.Box
|
||||
import io.objectbox.kotlin.inValues
|
||||
import io.objectbox.kotlin.query
|
||||
import org.kiwix.kiwixmobile.data.local.entity.Bookmark
|
||||
import org.kiwix.kiwixmobile.database.newdb.entities.BookOnDiskEntity
|
||||
import org.kiwix.kiwixmobile.database.newdb.entities.BookOnDiskEntity_
|
||||
import org.kiwix.kiwixmobile.library.entity.LibraryNetworkEntity.Book
|
||||
@ -62,4 +63,11 @@ class NewBookDao @Inject constructor(private val box: Box<BookOnDiskEntity>) {
|
||||
private fun delete(books: List<BookOnDiskEntity>) {
|
||||
box.remove(books)
|
||||
}
|
||||
|
||||
fun getFavIconAndZimFile(it: Bookmark): Pair<String?, String?> {
|
||||
val bookOnDiskEntity = box.query {
|
||||
equal(BookOnDiskEntity_.bookId, it.zimId)
|
||||
}.find().getOrNull(0)
|
||||
return bookOnDiskEntity?.let { Pair(it.favIcon, it.file.path) } ?: Pair(null, null)
|
||||
}
|
||||
}
|
||||
|
@ -60,7 +60,10 @@ class NewBookmarksDao @Inject constructor(val box: Box<BookmarkEntity>) {
|
||||
}.remove()
|
||||
}
|
||||
|
||||
fun migrationInsert(bookmarks: MutableList<Bookmark>) {
|
||||
box.put(bookmarks.map(::BookmarkEntity))
|
||||
fun migrationInsert(
|
||||
bookmarks: MutableList<Bookmark>,
|
||||
bookDao: NewBookDao
|
||||
) {
|
||||
box.put(bookmarks.zip(bookmarks.map { bookDao.getFavIconAndZimFile(it) }).map(::BookmarkEntity))
|
||||
}
|
||||
}
|
||||
|
@ -27,10 +27,10 @@ data class BookmarkEntity(
|
||||
@Id var id: Long = 0,
|
||||
val zimId: String,
|
||||
var zimName: String,
|
||||
var zimFilePath: String,
|
||||
var zimFilePath: String?,
|
||||
var bookmarkUrl: String,
|
||||
var bookmarkTitle: String,
|
||||
var favicon: String
|
||||
var favicon: String?
|
||||
) {
|
||||
constructor(item: BookmarkItem) : this(
|
||||
item.databaseId,
|
||||
@ -42,13 +42,19 @@ data class BookmarkEntity(
|
||||
item.favicon
|
||||
)
|
||||
|
||||
constructor(bookmark: Bookmark) : this(
|
||||
private constructor(bookmark: Bookmark, zimFilePath: String?, favicon: String?) : this(
|
||||
0,
|
||||
bookmark.zimId,
|
||||
bookmark.zimName,
|
||||
bookmark.zimFilePath,
|
||||
zimFilePath,
|
||||
bookmark.bookmarkUrl,
|
||||
bookmark.bookmarkTitle,
|
||||
bookmark.favicon
|
||||
favicon
|
||||
)
|
||||
|
||||
constructor(bookmarkWithFavIconAndFile: Pair<Bookmark, Pair<String?, String?>>) : this(
|
||||
bookmarkWithFavIconAndFile.first,
|
||||
bookmarkWithFavIconAndFile.second.first,
|
||||
bookmarkWithFavIconAndFile.second.second
|
||||
)
|
||||
}
|
||||
|
@ -15,6 +15,6 @@ fun ImageView.setBitmap(base64String: Base64String) {
|
||||
|
||||
// methods that accept inline classes as parameters are not allowed to be called from java
|
||||
// hence this facade
|
||||
fun ImageView.setBitmapFromString(string: String) {
|
||||
fun ImageView.setBitmapFromString(string: String?) {
|
||||
setBitmap(Base64String(string))
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user