Make BatteryDatabaseManager singleton

In BatteryTipLoader, two threads may access BatteryDatabaseManager
simultaneously. In this case thread A may close the database thread B
holds, then settings will crash.

In this cl, we make the BatteryDatabaseManager as singleton and
synchronize all the database related method. Then it shouldn't have
the crash anymore.

Bug: 73346734
Test: RunSettingsRoboTests
Change-Id: Ib53b2894b25155cca0c6ec60d1a816663d27a578
This commit is contained in:
jackqdyulei
2018-02-14 12:43:09 -08:00
parent 1bbe424e6e
commit c76bb78758
5 changed files with 35 additions and 9 deletions

View File

@@ -19,6 +19,7 @@ package com.android.settings.testutils;
import android.content.Context;
import com.android.settings.fuelgauge.batterytip.AnomalyDatabaseHelper;
import com.android.settings.fuelgauge.batterytip.BatteryDatabaseManager;
import com.android.settings.search.IndexDatabaseHelper;
import com.android.settings.slices.SlicesDatabaseHelper;
@@ -30,6 +31,7 @@ public class DatabaseTestUtils {
clearSearchDb(context);
clearSlicesDb(context);
clearAnomalyDb(context);
clearAnomalyDbManager();
}
private static void clearSlicesDb(Context context) {
@@ -76,4 +78,16 @@ public class DatabaseTestUtils {
throw new RuntimeException();
}
}
private static void clearAnomalyDbManager() {
Field instance;
Class clazz = BatteryDatabaseManager.class;
try {
instance = clazz.getDeclaredField("sSingleton");
instance.setAccessible(true);
instance.set(null, null);
} catch (Exception e) {
throw new RuntimeException();
}
}
}