Crash debug version when doing I/O on main thread (#8064)

This commit is contained in:
Hans-Peter Lehmann
2025-11-02 08:33:00 +01:00
committed by GitHub
parent 04dfbc1d5d
commit 41edd00b39
2 changed files with 24 additions and 3 deletions

View File

@ -21,12 +21,14 @@ public class PodcastApp extends Application {
if (BuildConfig.DEBUG) {
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects()
.penaltyDeath()
.penaltyLog()
.penaltyDropBox()
.detectLeakedSqlLiteObjects()
.detectActivityLeaks()
.detectLeakedClosableObjects()
.detectLeakedRegistrationObjects();
if (android.os.Build.VERSION.SDK_INT >= 26) {
builder.detectContentUriWithoutPermission();
}
StrictMode.setVmPolicy(builder.build());
}

View File

@ -10,6 +10,8 @@ import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Build;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
@ -387,7 +389,24 @@ public class PodDBAdapter {
return newDb;
}
private boolean isTest() {
if ("robolectric".equals(Build.FINGERPRINT)) {
return true;
}
try {
Class.forName("org.junit.Test");
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
public synchronized PodDBAdapter open() {
if (BuildConfig.DEBUG) {
if (Looper.myLooper() == Looper.getMainLooper() && !isTest()) {
throw new RuntimeException("I/O on main thread");
}
}
// do nothing
return this;
}