Java Code Examples for android.content.ContentResolver#unregisterContentObserver()

The following examples show how to use android.content.ContentResolver#unregisterContentObserver() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CalendarTracker.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private void setRegistered(boolean registered) {
    if (mRegistered == registered) return;
    final ContentResolver cr = mSystemContext.getContentResolver();
    final int userId = mUserContext.getUserId();
    if (mRegistered) {
        if (DEBUG) Log.d(TAG, "unregister content observer u=" + userId);
        cr.unregisterContentObserver(mObserver);
    }
    mRegistered = registered;
    if (DEBUG) Log.d(TAG, "mRegistered = " + registered + " u=" + userId);
    if (mRegistered) {
        if (DEBUG) Log.d(TAG, "register content observer u=" + userId);
        cr.registerContentObserver(Instances.CONTENT_URI, true, mObserver, userId);
        cr.registerContentObserver(Events.CONTENT_URI, true, mObserver, userId);
        cr.registerContentObserver(Calendars.CONTENT_URI, true, mObserver, userId);
    }
}
 
Example 2
Source File: ReadActivity.java    From NovelReader with MIT License 6 votes vote down vote up
private void registerBrightObserver() {
    try {
        if (mBrightObserver != null) {
            if (!isRegistered) {
                final ContentResolver cr = getContentResolver();
                cr.unregisterContentObserver(mBrightObserver);
                cr.registerContentObserver(BRIGHTNESS_MODE_URI, false, mBrightObserver);
                cr.registerContentObserver(BRIGHTNESS_URI, false, mBrightObserver);
                cr.registerContentObserver(BRIGHTNESS_ADJ_URI, false, mBrightObserver);
                isRegistered = true;
            }
        }
    } catch (Throwable throwable) {
        LogUtils.e(TAG, "register mBrightObserver error! " + throwable);
    }
}
 
Example 3
Source File: MainActivity.java    From SyncManagerAndroid-DemoGoogleTasks with MIT License 5 votes vote down vote up
private void unregisterContentObservers() {
	ContentResolver cr = getContentResolver();
	if (mTaskContentObserver != null) { // just paranoia
		cr.unregisterContentObserver(mTaskContentObserver);
		mTaskContentObserver = null;
		handler = null;
	}
}
 
Example 4
Source File: LauncherAppState.java    From LB-Launcher with Apache License 2.0 5 votes vote down vote up
/**
 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
 */
public void onTerminate() {
    sContext.unregisterReceiver(mModel);
    final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
    launcherApps.removeOnAppsChangedCallback(mModel);
    PackageInstallerCompat.getInstance(sContext).onStop();

    ContentResolver resolver = sContext.getContentResolver();
    resolver.unregisterContentObserver(mFavoritesObserver);
}
 
Example 5
Source File: ContentObserver.java    From Camera-Roll-Android-App with Apache License 2.0 4 votes vote down vote up
public void unregister(Context context) {
    ContentResolver resolver = context.getContentResolver();
    resolver.unregisterContentObserver(this);
}
 
Example 6
Source File: TestWeatherProvider.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * This test deletes all records from the weather table using the ContentProvider. It also
 * verifies that registered ContentObservers receive onChange callbacks when data is deleted.
 * <p>
 * It finally queries the ContentProvider to make sure that the table has been successfully
 * cleared.
 * <p>
 * NOTE: This does not delete the table itself. It just deletes the rows of data contained
 * within the table.
 * <p>
 * Potential causes for failure:
 * <p>
 *   1) Within {@link WeatherProvider#delete(Uri, String, String[])}, you didn't call
 *    getContext().getContentResolver().notifyChange(uri, null) after performing a deletion.
 * <p>
 *   2) The cursor returned from the query was null
 * <p>
 *   3) After the attempted deletion, the ContentProvider still provided weather data
 */
@Test
public void testDeleteAllRecordsFromProvider() {

    /*
     * Ensure there are records to delete from the database. Due to our setUp method, the
     * database will not have any records in it prior to this method being run.
     */
    testBulkInsert();

    /*
     * TestContentObserver allows us to test weather or not notifyChange was called
     * appropriately. We will use that here to make sure that notifyChange is called when a
     * deletion occurs.
     */
    TestUtilities.TestContentObserver weatherObserver = TestUtilities.getTestContentObserver();

    /*
     * A ContentResolver provides us access to the content model. We can use it to perform
     * deletions and queries at our CONTENT_URI
     */
    ContentResolver contentResolver = mContext.getContentResolver();

    /* Register a content observer to be notified of changes to data at a given URI (weather) */
    contentResolver.registerContentObserver(
            /* URI that we would like to observe changes to */
            WeatherContract.WeatherEntry.CONTENT_URI,
            /* Whether or not to notify us if descendants of this URI change */
            true,
            /* The observer to register (that will receive notifyChange callbacks) */
            weatherObserver);

    /* Delete all of the rows of data from the weather table */
    contentResolver.delete(
            WeatherContract.WeatherEntry.CONTENT_URI,
            /* Columns; leaving this null returns every column in the table */
            null,
            /* Optional specification for columns in the "where" clause above */
            null);

    /* Perform a query of the data that we've just deleted. This should be empty. */
    Cursor shouldBeEmptyCursor = contentResolver.query(
            WeatherContract.WeatherEntry.CONTENT_URI,
            /* Columns; leaving this null returns every column in the table */
            null,
            /* Optional specification for columns in the "where" clause above */
            null,
            /* Values for "where" clause */
            null,
            /* Sort order to return in Cursor */
            null);

    /*
     * If this fails, it's likely you didn't call notifyChange in your delete method from
     * your ContentProvider.
     */
    weatherObserver.waitForNotificationOrFail();

    /*
     * waitForNotificationOrFail is synchronous, so after that call, we are done observing
     * changes to content and should therefore unregister this observer.
     */
    contentResolver.unregisterContentObserver(weatherObserver);

    /* In some cases, the cursor can be null. That's actually a failure case here. */
    String cursorWasNull = "Cursor was null.";
    assertNotNull(cursorWasNull, shouldBeEmptyCursor);

    /* If the count of the cursor is not zero, all records weren't deleted */
    String allRecordsWereNotDeleted =
            "Error: All records were not deleted from weather table during delete";
    assertEquals(allRecordsWereNotDeleted,
            0,
            shouldBeEmptyCursor.getCount());

    /* Always close your cursor */
    shouldBeEmptyCursor.close();
}
 
Example 7
Source File: AndroidWordLevelSpellCheckerSession.java    From Indic-Keyboard with Apache License 2.0 4 votes vote down vote up
@Override
public void onClose() {
    final ContentResolver cres = mService.getContentResolver();
    cres.unregisterContentObserver(mObserver);
}
 
Example 8
Source File: StayAwakeTile.java    From GravityBox with Apache License 2.0 4 votes vote down vote up
void unobserve() {
    ContentResolver resolver = mContext.getContentResolver();
    resolver.unregisterContentObserver(this);
}
 
Example 9
Source File: TestWeatherProvider.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * This test deletes all records from the weather table using the ContentProvider. It also
 * verifies that registered ContentObservers receive onChange callbacks when data is deleted.
 * <p>
 * It finally queries the ContentProvider to make sure that the table has been successfully
 * cleared.
 * <p>
 * NOTE: This does not delete the table itself. It just deletes the rows of data contained
 * within the table.
 * <p>
 * Potential causes for failure:
 * <p>
 *   1) Within {@link WeatherProvider#delete(Uri, String, String[])}, you didn't call
 *    getContext().getContentResolver().notifyChange(uri, null) after performing a deletion.
 * <p>
 *   2) The cursor returned from the query was null
 * <p>
 *   3) After the attempted deletion, the ContentProvider still provided weather data
 */
@Test
public void testDeleteAllRecordsFromProvider() {

    /*
     * Ensure there are records to delete from the database. Due to our setUp method, the
     * database will not have any records in it prior to this method being run.
     */
    testBulkInsert();

    /*
     * TestContentObserver allows us to test weather or not notifyChange was called
     * appropriately. We will use that here to make sure that notifyChange is called when a
     * deletion occurs.
     */
    TestUtilities.TestContentObserver weatherObserver = TestUtilities.getTestContentObserver();

    /*
     * A ContentResolver provides us access to the content model. We can use it to perform
     * deletions and queries at our CONTENT_URI
     */
    ContentResolver contentResolver = mContext.getContentResolver();

    /* Register a content observer to be notified of changes to data at a given URI (weather) */
    contentResolver.registerContentObserver(
            /* URI that we would like to observe changes to */
            WeatherContract.WeatherEntry.CONTENT_URI,
            /* Whether or not to notify us if descendants of this URI change */
            true,
            /* The observer to register (that will receive notifyChange callbacks) */
            weatherObserver);

    /* Delete all of the rows of data from the weather table */
    contentResolver.delete(
            WeatherContract.WeatherEntry.CONTENT_URI,
            /* Columns; leaving this null returns every column in the table */
            null,
            /* Optional specification for columns in the "where" clause above */
            null);

    /* Perform a query of the data that we've just deleted. This should be empty. */
    Cursor shouldBeEmptyCursor = contentResolver.query(
            WeatherContract.WeatherEntry.CONTENT_URI,
            /* Columns; leaving this null returns every column in the table */
            null,
            /* Optional specification for columns in the "where" clause above */
            null,
            /* Values for "where" clause */
            null,
            /* Sort order to return in Cursor */
            null);

    /*
     * If this fails, it's likely you didn't call notifyChange in your delete method from
     * your ContentProvider.
     */
    weatherObserver.waitForNotificationOrFail();

    /*
     * waitForNotificationOrFail is synchronous, so after that call, we are done observing
     * changes to content and should therefore unregister this observer.
     */
    contentResolver.unregisterContentObserver(weatherObserver);

    /* In some cases, the cursor can be null. That's actually a failure case here. */
    String cursorWasNull = "Cursor was null.";
    assertNotNull(cursorWasNull, shouldBeEmptyCursor);

    /* If the count of the cursor is not zero, all records weren't deleted */
    String allRecordsWereNotDeleted =
            "Error: All records were not deleted from weather table during delete";
    assertEquals(allRecordsWereNotDeleted,
            0,
            shouldBeEmptyCursor.getCount());

    /* Always close your cursor */
    shouldBeEmptyCursor.close();
}
 
Example 10
Source File: TestTaskContentProvider.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * Tests deleting a single row of data via a ContentResolver
 */
@Test
public void testDelete() {
    /* Access writable database */
    TaskDbHelper helper = new TaskDbHelper(InstrumentationRegistry.getTargetContext());
    SQLiteDatabase database = helper.getWritableDatabase();

    /* Create a new row of task data */
    ContentValues testTaskValues = new ContentValues();
    testTaskValues.put(TaskContract.TaskEntry.COLUMN_DESCRIPTION, "Test description");
    testTaskValues.put(TaskContract.TaskEntry.COLUMN_PRIORITY, 1);

    /* Insert ContentValues into database and get a row ID back */
    long taskRowId = database.insert(
            /* Table to insert values into */
            TaskContract.TaskEntry.TABLE_NAME,
            null,
            /* Values to insert into table */
            testTaskValues);

    /* Always close the database when you're through with it */
    database.close();

    String insertFailed = "Unable to insert into the database";
    assertTrue(insertFailed, taskRowId != -1);


    /* TestContentObserver allows us to test if notifyChange was called appropriately */
    TestUtilities.TestContentObserver taskObserver = TestUtilities.getTestContentObserver();

    ContentResolver contentResolver = mContext.getContentResolver();

    /* Register a content observer to be notified of changes to data at a given URI (tasks) */
    contentResolver.registerContentObserver(
            /* URI that we would like to observe changes to */
            TaskContract.TaskEntry.CONTENT_URI,
            /* Whether or not to notify us if descendants of this URI change */
            true,
            /* The observer to register (that will receive notifyChange callbacks) */
            taskObserver);



    /* The delete method deletes the previously inserted row with id = 1 */
    Uri uriToDelete = TaskContract.TaskEntry.CONTENT_URI.buildUpon().appendPath("1").build();
    int tasksDeleted = contentResolver.delete(uriToDelete, null, null);

    String deleteFailed = "Unable to delete item in the database";
    assertTrue(deleteFailed, tasksDeleted != 0);

    /*
     * If this fails, it's likely you didn't call notifyChange in your delete method from
     * your ContentProvider.
     */
    taskObserver.waitForNotificationOrFail();

    /*
     * waitForNotificationOrFail is synchronous, so after that call, we are done observing
     * changes to content and should therefore unregister this observer.
     */
    contentResolver.unregisterContentObserver(taskObserver);
}
 
Example 11
Source File: TestWeatherProvider.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * This test deletes all records from the weather table using the ContentProvider. It also
 * verifies that registered ContentObservers receive onChange callbacks when data is deleted.
 * <p>
 * It finally queries the ContentProvider to make sure that the table has been successfully
 * cleared.
 * <p>
 * NOTE: This does not delete the table itself. It just deletes the rows of data contained
 * within the table.
 * <p>
 * Potential causes for failure:
 * <p>
 *   1) Within {@link WeatherProvider#delete(Uri, String, String[])}, you didn't call
 *    getContext().getContentResolver().notifyChange(uri, null) after performing a deletion.
 * <p>
 *   2) The cursor returned from the query was null
 * <p>
 *   3) After the attempted deletion, the ContentProvider still provided weather data
 */
@Test
public void testDeleteAllRecordsFromProvider() {

    /*
     * Ensure there are records to delete from the database. Due to our setUp method, the
     * database will not have any records in it prior to this method being run.
     */
    testBulkInsert();

    /*
     * TestContentObserver allows us to test weather or not notifyChange was called
     * appropriately. We will use that here to make sure that notifyChange is called when a
     * deletion occurs.
     */
    TestUtilities.TestContentObserver weatherObserver = TestUtilities.getTestContentObserver();

    /*
     * A ContentResolver provides us access to the content model. We can use it to perform
     * deletions and queries at our CONTENT_URI
     */
    ContentResolver contentResolver = mContext.getContentResolver();

    /* Register a content observer to be notified of changes to data at a given URI (weather) */
    contentResolver.registerContentObserver(
            /* URI that we would like to observe changes to */
            WeatherContract.WeatherEntry.CONTENT_URI,
            /* Whether or not to notify us if descendants of this URI change */
            true,
            /* The observer to register (that will receive notifyChange callbacks) */
            weatherObserver);

    /* Delete all of the rows of data from the weather table */
    contentResolver.delete(
            WeatherContract.WeatherEntry.CONTENT_URI,
            /* Columns; leaving this null returns every column in the table */
            null,
            /* Optional specification for columns in the "where" clause above */
            null);

    /* Perform a query of the data that we've just deleted. This should be empty. */
    Cursor shouldBeEmptyCursor = contentResolver.query(
            WeatherContract.WeatherEntry.CONTENT_URI,
            /* Columns; leaving this null returns every column in the table */
            null,
            /* Optional specification for columns in the "where" clause above */
            null,
            /* Values for "where" clause */
            null,
            /* Sort order to return in Cursor */
            null);

    /*
     * If this fails, it's likely you didn't call notifyChange in your delete method from
     * your ContentProvider.
     */
    weatherObserver.waitForNotificationOrFail();

    /*
     * waitForNotificationOrFail is synchronous, so after that call, we are done observing
     * changes to content and should therefore unregister this observer.
     */
    contentResolver.unregisterContentObserver(weatherObserver);

    /* In some cases, the cursor can be null. That's actually a failure case here. */
    String cursorWasNull = "Cursor was null.";
    assertNotNull(cursorWasNull, shouldBeEmptyCursor);

    /* If the count of the cursor is not zero, all records weren't deleted */
    String allRecordsWereNotDeleted =
            "Error: All records were not deleted from weather table during delete";
    assertEquals(allRecordsWereNotDeleted,
            0,
            shouldBeEmptyCursor.getCount());

    /* Always close your cursor */
    shouldBeEmptyCursor.close();
}
 
Example 12
Source File: TestTaskContentProvider.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * Tests inserting a single row of data via a ContentResolver
 */
@Test
public void testInsert() {

    /* Create values to insert */
    ContentValues testTaskValues = new ContentValues();
    testTaskValues.put(TaskContract.TaskEntry.COLUMN_DESCRIPTION, "Test description");
    testTaskValues.put(TaskContract.TaskEntry.COLUMN_PRIORITY, 1);

    /* TestContentObserver allows us to test if notifyChange was called appropriately */
    TestUtilities.TestContentObserver taskObserver = TestUtilities.getTestContentObserver();

    ContentResolver contentResolver = mContext.getContentResolver();

    /* Register a content observer to be notified of changes to data at a given URI (tasks) */
    contentResolver.registerContentObserver(
            /* URI that we would like to observe changes to */
            TaskContract.TaskEntry.CONTENT_URI,
            /* Whether or not to notify us if descendants of this URI change */
            true,
            /* The observer to register (that will receive notifyChange callbacks) */
            taskObserver);


    Uri uri = contentResolver.insert(TaskContract.TaskEntry.CONTENT_URI, testTaskValues);


    Uri expectedUri = ContentUris.withAppendedId(TaskContract.TaskEntry.CONTENT_URI, 1);

    String insertProviderFailed = "Unable to insert item through Provider";
    assertEquals(insertProviderFailed, uri, expectedUri);

    /*
     * If this fails, it's likely you didn't call notifyChange in your insert method from
     * your ContentProvider.
     */
    taskObserver.waitForNotificationOrFail();

    /*
     * waitForNotificationOrFail is synchronous, so after that call, we are done observing
     * changes to content and should therefore unregister this observer.
     */
    contentResolver.unregisterContentObserver(taskObserver);
}
 
Example 13
Source File: TestWeatherProvider.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * This test deletes all records from the weather table using the ContentProvider. It also
 * verifies that registered ContentObservers receive onChange callbacks when data is deleted.
 * <p>
 * It finally queries the ContentProvider to make sure that the table has been successfully
 * cleared.
 * <p>
 * NOTE: This does not delete the table itself. It just deletes the rows of data contained
 * within the table.
 * <p>
 * Potential causes for failure:
 * <p>
 *   1) Within {@link WeatherProvider#delete(Uri, String, String[])}, you didn't call
 *    getContext().getContentResolver().notifyChange(uri, null) after performing a deletion.
 * <p>
 *   2) The cursor returned from the query was null
 * <p>
 *   3) After the attempted deletion, the ContentProvider still provided weather data
 */
@Test
public void testDeleteAllRecordsFromProvider() {

    /*
     * Ensure there are records to delete from the database. Due to our setUp method, the
     * database will not have any records in it prior to this method being run.
     */
    testBulkInsert();

    /*
     * TestContentObserver allows us to test weather or not notifyChange was called
     * appropriately. We will use that here to make sure that notifyChange is called when a
     * deletion occurs.
     */
    TestUtilities.TestContentObserver weatherObserver = TestUtilities.getTestContentObserver();

    /*
     * A ContentResolver provides us access to the content model. We can use it to perform
     * deletions and queries at our CONTENT_URI
     */
    ContentResolver contentResolver = mContext.getContentResolver();

    /* Register a content observer to be notified of changes to data at a given URI (weather) */
    contentResolver.registerContentObserver(
            /* URI that we would like to observe changes to */
            WeatherContract.WeatherEntry.CONTENT_URI,
            /* Whether or not to notify us if descendants of this URI change */
            true,
            /* The observer to register (that will receive notifyChange callbacks) */
            weatherObserver);

    /* Delete all of the rows of data from the weather table */
    contentResolver.delete(
            WeatherContract.WeatherEntry.CONTENT_URI,
            /* Columns; leaving this null returns every column in the table */
            null,
            /* Optional specification for columns in the "where" clause above */
            null);

    /* Perform a query of the data that we've just deleted. This should be empty. */
    Cursor shouldBeEmptyCursor = contentResolver.query(
            WeatherContract.WeatherEntry.CONTENT_URI,
            /* Columns; leaving this null returns every column in the table */
            null,
            /* Optional specification for columns in the "where" clause above */
            null,
            /* Values for "where" clause */
            null,
            /* Sort order to return in Cursor */
            null);

    /*
     * If this fails, it's likely you didn't call notifyChange in your delete method from
     * your ContentProvider.
     */
    weatherObserver.waitForNotificationOrFail();

    /*
     * waitForNotificationOrFail is synchronous, so after that call, we are done observing
     * changes to content and should therefore unregister this observer.
     */
    contentResolver.unregisterContentObserver(weatherObserver);

    /* In some cases, the cursor can be null. That's actually a failure case here. */
    String cursorWasNull = "Cursor was null.";
    assertNotNull(cursorWasNull, shouldBeEmptyCursor);

    /* If the count of the cursor is not zero, all records weren't deleted */
    String allRecordsWereNotDeleted =
            "Error: All records were not deleted from weather table during delete";
    assertEquals(allRecordsWereNotDeleted,
            0,
            shouldBeEmptyCursor.getCount());

    /* Always close your cursor */
    shouldBeEmptyCursor.close();
}
 
Example 14
Source File: TestWeatherProvider.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * This test deletes all records from the weather table using the ContentProvider. It also
 * verifies that registered ContentObservers receive onChange callbacks when data is deleted.
 * <p>
 * It finally queries the ContentProvider to make sure that the table has been successfully
 * cleared.
 * <p>
 * NOTE: This does not delete the table itself. It just deletes the rows of data contained
 * within the table.
 * <p>
 * Potential causes for failure:
 * <p>
 *   1) Within {@link WeatherProvider#delete(Uri, String, String[])}, you didn't call
 *    getContext().getContentResolver().notifyChange(uri, null) after performing a deletion.
 * <p>
 *   2) The cursor returned from the query was null
 * <p>
 *   3) After the attempted deletion, the ContentProvider still provided weather data
 */
@Test
public void testDeleteAllRecordsFromProvider() {

    /*
     * Ensure there are records to delete from the database. Due to our setUp method, the
     * database will not have any records in it prior to this method being run.
     */
    testBulkInsert();

    /*
     * TestContentObserver allows us to test weather or not notifyChange was called
     * appropriately. We will use that here to make sure that notifyChange is called when a
     * deletion occurs.
     */
    TestUtilities.TestContentObserver weatherObserver = TestUtilities.getTestContentObserver();

    /*
     * A ContentResolver provides us access to the content model. We can use it to perform
     * deletions and queries at our CONTENT_URI
     */
    ContentResolver contentResolver = mContext.getContentResolver();

    /* Register a content observer to be notified of changes to data at a given URI (weather) */
    contentResolver.registerContentObserver(
            /* URI that we would like to observe changes to */
            WeatherContract.WeatherEntry.CONTENT_URI,
            /* Whether or not to notify us if descendants of this URI change */
            true,
            /* The observer to register (that will receive notifyChange callbacks) */
            weatherObserver);

    /* Delete all of the rows of data from the weather table */
    contentResolver.delete(
            WeatherContract.WeatherEntry.CONTENT_URI,
            /* Columns; leaving this null returns every column in the table */
            null,
            /* Optional specification for columns in the "where" clause above */
            null);

    /* Perform a query of the data that we've just deleted. This should be empty. */
    Cursor shouldBeEmptyCursor = contentResolver.query(
            WeatherContract.WeatherEntry.CONTENT_URI,
            /* Columns; leaving this null returns every column in the table */
            null,
            /* Optional specification for columns in the "where" clause above */
            null,
            /* Values for "where" clause */
            null,
            /* Sort order to return in Cursor */
            null);

    /*
     * If this fails, it's likely you didn't call notifyChange in your delete method from
     * your ContentProvider.
     */
    weatherObserver.waitForNotificationOrFail();

    /*
     * waitForNotificationOrFail is synchronous, so after that call, we are done observing
     * changes to content and should therefore unregister this observer.
     */
    contentResolver.unregisterContentObserver(weatherObserver);

    /* In some cases, the cursor can be null. That's actually a failure case here. */
    String cursorWasNull = "Cursor was null.";
    assertNotNull(cursorWasNull, shouldBeEmptyCursor);

    /* If the count of the cursor is not zero, all records weren't deleted */
    String allRecordsWereNotDeleted =
            "Error: All records were not deleted from weather table during delete";
    assertEquals(allRecordsWereNotDeleted,
            0,
            shouldBeEmptyCursor.getCount());

    /* Always close your cursor */
    shouldBeEmptyCursor.close();
}
 
Example 15
Source File: AndroidWordLevelSpellCheckerSession.java    From Android-Keyboard with Apache License 2.0 4 votes vote down vote up
@Override
public void onClose() {
    final ContentResolver cres = mService.getContentResolver();
    cres.unregisterContentObserver(mObserver);
}
 
Example 16
Source File: TestWeatherProvider.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * This test deletes all records from the weather table using the ContentProvider. It also
 * verifies that registered ContentObservers receive onChange callbacks when data is deleted.
 * <p>
 * It finally queries the ContentProvider to make sure that the table has been successfully
 * cleared.
 * <p>
 * NOTE: This does not delete the table itself. It just deletes the rows of data contained
 * within the table.
 * <p>
 * Potential causes for failure:
 * <p>
 *   1) Within {@link WeatherProvider#delete(Uri, String, String[])}, you didn't call
 *    getContext().getContentResolver().notifyChange(uri, null) after performing a deletion.
 * <p>
 *   2) The cursor returned from the query was null
 * <p>
 *   3) After the attempted deletion, the ContentProvider still provided weather data
 */
@Test
public void testDeleteAllRecordsFromProvider() {

    /*
     * Ensure there are records to delete from the database. Due to our setUp method, the
     * database will not have any records in it prior to this method being run.
     */
    testBulkInsert();

    /*
     * TestContentObserver allows us to test weather or not notifyChange was called
     * appropriately. We will use that here to make sure that notifyChange is called when a
     * deletion occurs.
     */
    TestUtilities.TestContentObserver weatherObserver = TestUtilities.getTestContentObserver();

    /*
     * A ContentResolver provides us access to the content model. We can use it to perform
     * deletions and queries at our CONTENT_URI
     */
    ContentResolver contentResolver = mContext.getContentResolver();

    /* Register a content observer to be notified of changes to data at a given URI (weather) */
    contentResolver.registerContentObserver(
            /* URI that we would like to observe changes to */
            WeatherContract.WeatherEntry.CONTENT_URI,
            /* Whether or not to notify us if descendants of this URI change */
            true,
            /* The observer to register (that will receive notifyChange callbacks) */
            weatherObserver);

    /* Delete all of the rows of data from the weather table */
    contentResolver.delete(
            WeatherContract.WeatherEntry.CONTENT_URI,
            /* Columns; leaving this null returns every column in the table */
            null,
            /* Optional specification for columns in the "where" clause above */
            null);

    /* Perform a query of the data that we've just deleted. This should be empty. */
    Cursor shouldBeEmptyCursor = contentResolver.query(
            WeatherContract.WeatherEntry.CONTENT_URI,
            /* Columns; leaving this null returns every column in the table */
            null,
            /* Optional specification for columns in the "where" clause above */
            null,
            /* Values for "where" clause */
            null,
            /* Sort order to return in Cursor */
            null);

    /*
     * If this fails, it's likely you didn't call notifyChange in your delete method from
     * your ContentProvider.
     */
    weatherObserver.waitForNotificationOrFail();

    /*
     * waitForNotificationOrFail is synchronous, so after that call, we are done observing
     * changes to content and should therefore unregister this observer.
     */
    contentResolver.unregisterContentObserver(weatherObserver);

    /* In some cases, the cursor can be null. That's actually a failure case here. */
    String cursorWasNull = "Cursor was null.";
    assertNotNull(cursorWasNull, shouldBeEmptyCursor);

    /* If the count of the cursor is not zero, all records weren't deleted */
    String allRecordsWereNotDeleted =
            "Error: All records were not deleted from weather table during delete";
    assertEquals(allRecordsWereNotDeleted,
            0,
            shouldBeEmptyCursor.getCount());

    /* Always close your cursor */
    shouldBeEmptyCursor.close();
}
 
Example 17
Source File: AndroidWordLevelSpellCheckerSession.java    From openboard with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onClose() {
    final ContentResolver cres = mService.getContentResolver();
    cres.unregisterContentObserver(mObserver);
}
 
Example 18
Source File: TextClock.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private void unregisterObserver() {
    if (mFormatChangeObserver != null) {
        final ContentResolver resolver = getContext().getContentResolver();
        resolver.unregisterContentObserver(mFormatChangeObserver);
    }
}
 
Example 19
Source File: TestWeatherProvider.java    From android-dev-challenge with Apache License 2.0 4 votes vote down vote up
/**
 * This test deletes all records from the weather table using the ContentProvider. It also
 * verifies that registered ContentObservers receive onChange callbacks when data is deleted.
 * <p>
 * It finally queries the ContentProvider to make sure that the table has been successfully
 * cleared.
 * <p>
 * NOTE: This does not delete the table itself. It just deletes the rows of data contained
 * within the table.
 * <p>
 * Potential causes for failure:
 * <p>
 *   1) Within {@link WeatherProvider#delete(Uri, String, String[])}, you didn't call
 *    getContext().getContentResolver().notifyChange(uri, null) after performing a deletion.
 * <p>
 *   2) The cursor returned from the query was null
 * <p>
 *   3) After the attempted deletion, the ContentProvider still provided weather data
 */
@Test
public void testDeleteAllRecordsFromProvider() {

    /*
     * Ensure there are records to delete from the database. Due to our setUp method, the
     * database will not have any records in it when this method is run
     */
    testBulkInsert();

    /*
     * TestContentObserver allows us to test weather or not notifyChange was called
     * appropriately. We will use that here to make sure that notifyChange is called when a
     * deletion occurs.
     */
    TestUtilities.TestContentObserver weatherObserver = TestUtilities.getTestContentObserver();

    /*
     * A ContentResolver provides us access to the content model. We can use it to perform
     * deletions and queries at our CONTENT_URI
     */
    ContentResolver contentResolver = mContext.getContentResolver();

    /* Register a content observer to be notified of changes to data at a given URI (weather) */
    contentResolver.registerContentObserver(
            /* URI that we would like to observe changes to */
            WeatherContract.WeatherEntry.CONTENT_URI,
            /* Whether or not to notify us if descendants of this URI change */
            true,
            /* The observer to register (that will receive notifyChange callbacks) */
            weatherObserver);

    /* Delete all of the rows of data from the weather table */
    contentResolver.delete(
            WeatherContract.WeatherEntry.CONTENT_URI,
            /* Columns; leaving this null returns every column in the table */
            null,
            /* Optional specification for columns in the "where" clause above */
            null);

    /* Perform a query of the data that we've just deleted. This should be empty. */
    Cursor shouldBeEmptyCursor = contentResolver.query(
            WeatherContract.WeatherEntry.CONTENT_URI,
            /* Columns; leaving this null returns every column in the table */
            null,
            /* Optional specification for columns in the "where" clause above */
            null,
            /* Values for "where" clause */
            null,
            /* Sort order to return in Cursor */
            null);

    /*
     * If this fails, it's likely you didn't call notifyChange in your delete method from
     * your ContentProvider.
     */
    weatherObserver.waitForNotificationOrFail();

    /*
     * waitForNotificationOrFail is synchronous, so after that call, we are done observing
     * changes to content and should therefore unregister this observer.
     */
    contentResolver.unregisterContentObserver(weatherObserver);

    /* In some cases, the cursor can be null. That's actually a failure case here. */
    String cursorWasNull = "Cursor was null.";
    assertNotNull(cursorWasNull, shouldBeEmptyCursor);

    /* If the count of the cursor is not zero, all records weren't deleted */
    String allRecordsWereNotDeleted =
            "Error: All records were not deleted from weather table during delete";
    assertEquals(allRecordsWereNotDeleted,
            0,
            shouldBeEmptyCursor.getCount());

    /* Always close your cursor */
    shouldBeEmptyCursor.close();
}
 
Example 20
Source File: Launcher.java    From LaunchEnr with GNU General Public License v3.0 3 votes vote down vote up
static void removeContentObserver(Activity activity) {

            ContentResolver contentResolver = activity.getContentResolver();

            contentResolver.unregisterContentObserver(new ContactsObserver(new Handler(), activity));

        }