markusfisch / ScreenTime

Displays your screen time in a permanent notification.
The Unlicense
114 stars 10 forks source link

Could not open database #22

Closed MatsG23 closed 1 year ago

MatsG23 commented 1 year ago

When I boot up my phone, the app crashes. It says that the sqlite Database could not be opened. Contrary to that, the app works as expected when I manually open the app after the crash - there is no data loss and the permanent notification showing the screen time of the day shows.

I am using a Pixel 6a with GrapheneOS 2023080800 (based on Android 13).

type: crash
osVersion: google/bluejay/bluejay:13/TQ3A.230805.001/2023080800:user/release-keys
package: de.markusfisch.android.screentime:12
process: de.markusfisch.android.screentime
processUptime: 236 + 354 ms
installer: dev.imranr.obtainium

java.lang.RuntimeException: Unable to create application de.markusfisch.android.screentime.app.ScreenTimeTrackerApp: android.database.sqlite.SQLiteCantOpenDatabaseException: Cannot open database '/data/user/0/de.markusfisch.android.screentime/databases/events.db' with flags 0x10000000: Directory /data/user/0/de.markusfisch.android.screentime/databases doesn't exist
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6832)
at android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2139)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7940)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ExecInit.main(ExecInit.java:49)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:355)
Caused by: android.database.sqlite.SQLiteCantOpenDatabaseException: Cannot open database '/data/user/0/de.markusfisch.android.screentime/databases/events.db' with flags 0x10000000: Directory /data/user/0/de.markusfisch.android.screentime/databases doesn't exist
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:262)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:205)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:512)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:210)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:202)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:1085)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:1065)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:929)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:918)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:380)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:323)
at c.a.f(Unknown Source:10)
at de.markusfisch.android.screentime.app.ScreenTimeTrackerApp.onCreate(Unknown Source:7)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1282)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6827)
... 11 more
Caused by: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14 SQLITE_CANTOPEN): Could not open database
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:224)
... 25 more
markusfisch commented 1 year ago

Hm, that's strange πŸ€” Thanks for filing an issue! πŸ‘

The reason seems to be that the "databases" directory doesn't exist (or is inaccessible) at boot time:

Cannot open database '/data/user/0/de.markusfisch.android.screentime/databases/events.db' with flags 0x10000000: Directory /data/user/0/de.markusfisch.android.screentime/databases doesn't exist

But this doesn't make any sense to me since the app is running and this directory should exist then πŸ€”

Also, if the app works after booting up is complete, the directory must exist at the next boot. I assume your data is still present after the boot?

After googling this a bit, it seems that this is happening to other apps too. And I even found some occourences in the crash logs on Google Play 😬 So it seems you're not the only one affected, even if I have never heard of this before.

MatsG23 commented 1 year ago

Yes, I can boot as many times as I want without losing any data.

markusfisch commented 1 year ago

Fixed in https://github.com/markusfisch/ScreenTime/commit/d833f0168b3f6b5772c7acec0ca7caff1f9bd588

The reason for this was that credential encrypted storage is not available until after the user is unlocked, so the initialization of the database (and shared preferences) must be delayed.

Thanks again for filing an issue about that! πŸ‘