Call CardDatabaseHelper.getInstance() when used and not in onCreate

Providers can be created before onCreate is called on the application.
Calling CardDatabaseHelper.getInstance() too early will cause
ContextualCardLoader to call getAppllicationContext before application
has been fully started.

Postpone calling CardDatabaseHelper.getInstance() until database is
accessed.

Test: Start device with sim card inserted and Pin enabled on sim.
  Wait some time before entering sim pin. After entering pin, open
  settings. Verify that settings does not crash.
  Verify that there is no contentProvider exceptions from settings in log.
Bug: 154076590
Change-Id: Id0b6294ca2aeebdc71076299928c4dea5145ba33
This commit is contained in:
Håkan Kvist
2019-09-20 13:46:19 +02:00
parent 27b4578002
commit 6f1b6e3693

View File

@@ -65,11 +65,8 @@ public class CardContentProvider extends ContentProvider {
URI_MATCHER.addURI(CARD_AUTHORITY, CardDatabaseHelper.CARD_TABLE, MATCH_CARDS);
}
private CardDatabaseHelper mDBHelper;
@Override
public boolean onCreate() {
mDBHelper = CardDatabaseHelper.getInstance(getContext());
return true;
}
@@ -84,7 +81,8 @@ public class CardContentProvider extends ContentProvider {
public int bulkInsert(Uri uri, ContentValues[] values) {
final StrictMode.ThreadPolicy oldPolicy = StrictMode.getThreadPolicy();
int numInserted = 0;
final SQLiteDatabase database = mDBHelper.getWritableDatabase();
final CardDatabaseHelper DBHelper = CardDatabaseHelper.getInstance(getContext());
final SQLiteDatabase database = DBHelper.getWritableDatabase();
final boolean keepDismissalTimestampBeforeDeletion = getContext().getResources()
.getBoolean(R.bool.config_keep_contextual_card_dismissal_timestamp);
final Map<String, Long> dismissedTimeMap = new ArrayMap<>();
@@ -168,7 +166,8 @@ public class CardContentProvider extends ContentProvider {
final SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
final String table = getTableFromMatch(uri);
queryBuilder.setTables(table);
final SQLiteDatabase database = mDBHelper.getReadableDatabase();
final CardDatabaseHelper DBHelper = CardDatabaseHelper.getInstance(getContext());
final SQLiteDatabase database = DBHelper.getReadableDatabase();
final Cursor cursor = queryBuilder.query(database,
projection, selection, selectionArgs, null /* groupBy */, null /* having */,
sortOrder);