Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

4.17 bulkinserts with contentprovider #294

Open
wants to merge 2 commits into
base: 4.16_use_contentprovider_inserts
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/build.gradle
Expand Up @@ -17,6 +17,9 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
buildTypes.each {
it.buildConfigField 'String', 'OPEN_WEATHER_MAP_API_KEY', MyOpenWeatherMapApiKey
}
}

dependencies {
Expand Down
Expand Up @@ -449,67 +449,67 @@ static ContentValues[] createBulkInsertWeatherValues(long locationRowId) {
// in your provider. Note that this test will work with the built-in (default) provider
// implementation, which just inserts records one-at-a-time, so really do implement the
// BulkInsert ContentProvider function.
// public void testBulkInsert() {
// // first, let's create a location value
// ContentValues testValues = TestUtilities.createNorthPoleLocationValues();
// Uri locationUri = mContext.getContentResolver().insert(LocationEntry.CONTENT_URI, testValues);
// long locationRowId = ContentUris.parseId(locationUri);
//
// // Verify we got a row back.
// assertTrue(locationRowId != -1);
//
// // Data's inserted. IN THEORY. Now pull some out to stare at it and verify it made
// // the round trip.
//
// // A cursor is your primary interface to the query results.
// Cursor cursor = mContext.getContentResolver().query(
// LocationEntry.CONTENT_URI,
// null, // leaving "columns" null just returns all the columns.
// null, // cols for "where" clause
// null, // values for "where" clause
// null // sort order
// );
//
// TestUtilities.validateCursor("testBulkInsert. Error validating LocationEntry.",
// cursor, testValues);
//
// // Now we can bulkInsert some weather. In fact, we only implement BulkInsert for weather
// // entries. With ContentProviders, you really only have to implement the features you
// // use, after all.
// ContentValues[] bulkInsertContentValues = createBulkInsertWeatherValues(locationRowId);
//
// // Register a content observer for our bulk insert.
// TestUtilities.TestContentObserver weatherObserver = TestUtilities.getTestContentObserver();
// mContext.getContentResolver().registerContentObserver(WeatherEntry.CONTENT_URI, true, weatherObserver);
//
// int insertCount = mContext.getContentResolver().bulkInsert(WeatherEntry.CONTENT_URI, bulkInsertContentValues);
//
// // Students: If this fails, it means that you most-likely are not calling the
// // getContext().getContentResolver().notifyChange(uri, null); in your BulkInsert
// // ContentProvider method.
// weatherObserver.waitForNotificationOrFail();
// mContext.getContentResolver().unregisterContentObserver(weatherObserver);
//
// assertEquals(insertCount, BULK_INSERT_RECORDS_TO_INSERT);
//
// // A cursor is your primary interface to the query results.
// cursor = mContext.getContentResolver().query(
// WeatherEntry.CONTENT_URI,
// null, // leaving "columns" null just returns all the columns.
// null, // cols for "where" clause
// null, // values for "where" clause
// WeatherEntry.COLUMN_DATE + " ASC" // sort order == by DATE ASCENDING
// );
//
// // we should have as many records in the database as we've inserted
// assertEquals(cursor.getCount(), BULK_INSERT_RECORDS_TO_INSERT);
//
// // and let's make sure they match the ones we created
// cursor.moveToFirst();
// for ( int i = 0; i < BULK_INSERT_RECORDS_TO_INSERT; i++, cursor.moveToNext() ) {
// TestUtilities.validateCurrentRecord("testBulkInsert. Error validating WeatherEntry " + i,
// cursor, bulkInsertContentValues[i]);
// }
// cursor.close();
// }
public void testBulkInsert() {
// first, let's create a location value
ContentValues testValues = TestUtilities.createNorthPoleLocationValues();
Uri locationUri = mContext.getContentResolver().insert(LocationEntry.CONTENT_URI, testValues);
long locationRowId = ContentUris.parseId(locationUri);

// Verify we got a row back.
assertTrue(locationRowId != -1);

// Data's inserted. IN THEORY. Now pull some out to stare at it and verify it made
// the round trip.

// A cursor is your primary interface to the query results.
Cursor cursor = mContext.getContentResolver().query(
LocationEntry.CONTENT_URI,
null, // leaving "columns" null just returns all the columns.
null, // cols for "where" clause
null, // values for "where" clause
null // sort order
);

TestUtilities.validateCursor("testBulkInsert. Error validating LocationEntry.",
cursor, testValues);

// Now we can bulkInsert some weather. In fact, we only implement BulkInsert for weather
// entries. With ContentProviders, you really only have to implement the features you
// use, after all.
ContentValues[] bulkInsertContentValues = createBulkInsertWeatherValues(locationRowId);

// Register a content observer for our bulk insert.
TestUtilities.TestContentObserver weatherObserver = TestUtilities.getTestContentObserver();
mContext.getContentResolver().registerContentObserver(WeatherEntry.CONTENT_URI, true, weatherObserver);

int insertCount = mContext.getContentResolver().bulkInsert(WeatherEntry.CONTENT_URI, bulkInsertContentValues);

// Students: If this fails, it means that you most-likely are not calling the
// getContext().getContentResolver().notifyChange(uri, null); in your BulkInsert
// ContentProvider method.
weatherObserver.waitForNotificationOrFail();
mContext.getContentResolver().unregisterContentObserver(weatherObserver);

assertEquals(insertCount, BULK_INSERT_RECORDS_TO_INSERT);

// A cursor is your primary interface to the query results.
cursor = mContext.getContentResolver().query(
WeatherEntry.CONTENT_URI,
null, // leaving "columns" null just returns all the columns.
null, // cols for "where" clause
null, // values for "where" clause
WeatherEntry.COLUMN_DATE + " ASC" // sort order == by DATE ASCENDING
);

// we should have as many records in the database as we've inserted
assertEquals(cursor.getCount(), BULK_INSERT_RECORDS_TO_INSERT);

// and let's make sure they match the ones we created
cursor.moveToFirst();
for ( int i = 0; i < BULK_INSERT_RECORDS_TO_INSERT; i++, cursor.moveToNext() ) {
TestUtilities.validateCurrentRecord("testBulkInsert. Error validating WeatherEntry " + i,
cursor, bulkInsertContentValues[i]);
}
cursor.close();
}
}
Expand Up @@ -20,6 +20,7 @@
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.net.Uri;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
Expand Down Expand Up @@ -302,7 +303,9 @@ private String[] getWeatherDataFromJson(String forecastJsonStr,

// add to database
if ( cVVector.size() > 0 ) {
// Student: call bulkInsert to add the weatherEntries to the database here
ContentValues[] cvArray = new ContentValues[cVVector.size()];
cVVector.toArray(cvArray);
mContext.getContentResolver().bulkInsert(WeatherEntry.CONTENT_URI, cvArray);
}

// Sort order: Ascending, by date.
Expand All @@ -311,18 +314,17 @@ private String[] getWeatherDataFromJson(String forecastJsonStr,
locationSetting, System.currentTimeMillis());

// Students: Uncomment the next lines to display what what you stored in the bulkInsert

// Cursor cur = mContext.getContentResolver().query(weatherForLocationUri,
// null, null, null, sortOrder);
//
// cVVector = new Vector<ContentValues>(cur.getCount());
// if ( cur.moveToFirst() ) {
// do {
// ContentValues cv = new ContentValues();
// DatabaseUtils.cursorRowToContentValues(cur, cv);
// cVVector.add(cv);
// } while (cur.moveToNext());
// }
Cursor cur = mContext.getContentResolver().query(weatherForLocationUri,
null, null, null, sortOrder);

cVVector = new Vector<ContentValues>(cur.getCount());
if ( cur.moveToFirst() ) {
do {
ContentValues cv = new ContentValues();
DatabaseUtils.cursorRowToContentValues(cur, cv);
cVVector.add(cv);
} while (cur.moveToNext());
}

Log.d(LOG_TAG, "FetchWeatherTask Complete. " + cVVector.size() + " Inserted");

Expand Down Expand Up @@ -367,12 +369,14 @@ protected String[] doInBackground(String... params) {
final String FORMAT_PARAM = "mode";
final String UNITS_PARAM = "units";
final String DAYS_PARAM = "cnt";
final String APPID_PARAM = "APPID";

Uri builtUri = Uri.parse(FORECAST_BASE_URL).buildUpon()
.appendQueryParameter(QUERY_PARAM, params[0])
.appendQueryParameter(FORMAT_PARAM, format)
.appendQueryParameter(UNITS_PARAM, units)
.appendQueryParameter(DAYS_PARAM, Integer.toString(numDays))
.appendQueryParameter(APPID_PARAM, BuildConfig.OPEN_WEATHER_MAP_API_KEY)
.build();

URL url = new URL(builtUri.toString());
Expand Down