Skip to content

Commit becb620

Browse files
Fix widget configuration issue and crashes
1 parent 47b57dc commit becb620

File tree

6 files changed

+27
-7
lines changed

6 files changed

+27
-7
lines changed

app/src/main/kotlin/eu/fliegendewurst/triliumdroid/database/Cache.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,8 @@ object Cache {
315315
while (it.moveToNext()) {
316316
val id = NoteId(it.getString(0))
317317
val title = it.getString(1)
318-
val attrName = it.getString(2)
319-
val attrValue = it.getString(3)
318+
val attrName = it.getStringOrNull(2)
319+
val attrValue = it.getStringOrNull(3)
320320
val attrIdRaw = it.getStringOrNull(4)
321321
val attrId = if (attrIdRaw != null) {
322322
AttributeId(attrIdRaw)
@@ -342,8 +342,9 @@ object Cache {
342342
BlobId("INVALID")
343343
)
344344
}
345-
if (attrValue != null && !attrValue.startsWith('_') &&
346-
!attrName.startsWith("child:") && attrId != null
345+
if (attrId != null && attrName != null && attrValue != null &&
346+
!attrValue.startsWith('_') &&
347+
!attrName.startsWith("child:")
347348
) {
348349
relations.add(Triple(id, NoteId(attrValue), Pair(attrName, attrId)))
349350
}

app/src/main/kotlin/eu/fliegendewurst/triliumdroid/database/CacheDbHelper.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class CacheDbHelper(context: Context, private val sql: String) :
1414
SQLiteOpenHelper(context, Versions.DATABASE_NAME, null, Versions.DATABASE_VERSION) {
1515
companion object {
1616
private const val TAG = "CacheDbHelper"
17+
const val MAX_MIGRATION = 2
1718
}
1819

1920
override fun onCreate(db: SQLiteDatabase) {
@@ -40,7 +41,7 @@ class CacheDbHelper(context: Context, private val sql: String) :
4041
}
4142
}
4243
// DB migrations are only for fixups
43-
Preferences.setDatabaseMigration(2)
44+
Preferences.setDatabaseMigration(MAX_MIGRATION)
4445
} catch (t: Throwable) {
4546
Log.e(TAG, "fatal error creating database", t)
4647
}

app/src/main/kotlin/eu/fliegendewurst/triliumdroid/database/DB.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.content.ContentValues
44
import android.content.Context
55
import android.database.AbstractWindowedCursor
66
import android.database.Cursor
7+
import android.database.Cursor.FIELD_TYPE_STRING
78
import android.database.CursorWindow
89
import android.database.sqlite.SQLiteCursorDriver
910
import android.database.sqlite.SQLiteDatabase
@@ -13,6 +14,7 @@ import android.util.Log
1314
import androidx.core.database.getStringOrNull
1415
import eu.fliegendewurst.triliumdroid.R
1516
import eu.fliegendewurst.triliumdroid.database.Branches.branches
17+
import eu.fliegendewurst.triliumdroid.database.CacheDbHelper.Companion.MAX_MIGRATION
1618
import eu.fliegendewurst.triliumdroid.database.Notes.notes
1719
import eu.fliegendewurst.triliumdroid.util.Preferences
1820
import eu.fliegendewurst.triliumdroid.util.Unreachable
@@ -188,6 +190,9 @@ object DB {
188190
).use {
189191
while (it.moveToNext()) {
190192
val id = it.getString(0)
193+
if (it.getType(1) != FIELD_TYPE_STRING) {
194+
continue
195+
}
191196
val content = it.getStringOrNull(1)
192197
if (content != null) {
193198
decoded.add(Pair(id, Base64.decode(content)))
@@ -203,7 +208,7 @@ object DB {
203208
if (migrationLevel < 2) {
204209
Blobs.fixupBrokenBlobIDs()
205210
}
206-
Preferences.setDatabaseMigration(2)
211+
Preferences.setDatabaseMigration(MAX_MIGRATION)
207212
}
208213

209214
fun nukeDatabase(context: Context) {
@@ -219,7 +224,7 @@ object DB {
219224
branches.clear()
220225
lastSync = null
221226
// DB migrations are only for fixups
222-
Preferences.setDatabaseMigration(2)
227+
Preferences.setDatabaseMigration(MAX_MIGRATION)
223228
}
224229

225230
fun closeDatabase() {

app/src/main/kotlin/eu/fliegendewurst/triliumdroid/util/Preferences.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import androidx.preference.PreferenceManager
77
import eu.fliegendewurst.triliumdroid.activity.main.HistoryItem
88
import eu.fliegendewurst.triliumdroid.data.CanvasNoteViewport
99
import eu.fliegendewurst.triliumdroid.data.NoteId
10+
import eu.fliegendewurst.triliumdroid.database.CacheDbHelper.Companion.MAX_MIGRATION
1011
import eu.fliegendewurst.triliumdroid.service.Util
1112
import eu.fliegendewurst.triliumdroid.widget.parseWidgetAction
1213

@@ -33,6 +34,12 @@ object Preferences {
3334

3435
fun init(applicationContext: Context) {
3536
prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext)
37+
if (!prefs.contains(DATABASE_VERSION) && !prefs.contains(HOSTNAME)) {
38+
// This is relevant if the app is uninstalled and reinstalled,
39+
// in which case the database may persist. We assume it doesn't
40+
// need fixups in that case.
41+
setDatabaseMigration(MAX_MIGRATION)
42+
}
3643
}
3744

3845
fun hostname(): String? = prefs.getString(HOSTNAME, null)

app/src/main/kotlin/eu/fliegendewurst/triliumdroid/widget/NoteWidget.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class NoteWidget : AppWidgetProvider() {
4848
appWidgetIds: IntArray
4949
) {
5050
Preferences.init(context.applicationContext)
51+
DB.applicationContext = context.applicationContext
5152
if (DB.haveDatabase(context)) {
5253
runBlocking {
5354
// There may be multiple widgets active, so update all of them
@@ -65,6 +66,7 @@ class NoteWidget : AppWidgetProvider() {
6566
newOptions: Bundle?
6667
) {
6768
Preferences.init(context.applicationContext)
69+
DB.applicationContext = context.applicationContext
6870
if (DB.haveDatabase(context)) {
6971
runBlocking {
7072
updateAppWidget(context, appWidgetManager, appWidgetId, newOptions)
@@ -74,6 +76,7 @@ class NoteWidget : AppWidgetProvider() {
7476

7577
override fun onEnabled(context: Context) {
7678
Preferences.init(context.applicationContext)
79+
DB.applicationContext = context.applicationContext
7780
if (DB.haveDatabase(context)) {
7881
runBlocking {
7982
DB.initializeDatabase(context)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Bugfixes
2+
- Fix many bugs/crashes with widgets
3+
- Fixed crash when re-installing the app without deleting app data

0 commit comments

Comments
 (0)