Search in sources :

Example 71 with ContentProviderOperation

use of android.content.ContentProviderOperation in project robolectric by robolectric.

the class ShadowContentProviderOperationTest method reflectionShouldWork.

@Test
public void reflectionShouldWork() {
    final Uri uri = Uri.parse("content://authority/path");
    ContentProviderOperation op = ContentProviderOperation.newInsert(uri).withValue("insertKey", "insertValue").withValueBackReference("backKey", 2).build();
    // insert and values back references
    assertThat(op.getUri()).isEqualTo(uri);
    ShadowContentProviderOperation shadow = Shadows.shadowOf(op);
    assertThat(shadow.getType()).isEqualTo(ShadowContentProviderOperation.TYPE_INSERT);
    assertThat(shadow.getContentValues().getAsString("insertKey")).isEqualTo("insertValue");
    assertThat(shadow.getValuesBackReferences().getAsInteger("backKey")).isEqualTo(2);
    // update and selection back references
    op = ContentProviderOperation.newUpdate(uri).withValue("updateKey", "updateValue").withSelection("a=? and b=?", new String[] { "abc" }).withSelectionBackReference(1, 3).build();
    assertThat(op.getUri()).isEqualTo(uri);
    shadow = Shadows.shadowOf(op);
    assertThat(shadow.getType()).isEqualTo(ShadowContentProviderOperation.TYPE_UPDATE);
    assertThat(shadow.getContentValues().getAsString("updateKey")).isEqualTo("updateValue");
    assertThat(shadow.getSelection()).isEqualTo("a=? and b=?");
    assertThat(shadow.getSelectionArgs()).containsExactly("abc");
    assertThat(shadow.getSelectionArgsBackReferences()).isEqualTo(Collections.<Integer, Integer>singletonMap(1, 3));
    // delete and expected count
    op = ContentProviderOperation.newDelete(uri).withExpectedCount(1).build();
    assertThat(op.getUri()).isEqualTo(uri);
    shadow = Shadows.shadowOf(op);
    assertThat(shadow.getType()).isEqualTo(ShadowContentProviderOperation.TYPE_DELETE);
    assertThat(shadow.getExpectedCount()).isEqualTo(1);
}
Also used : ContentProviderOperation(android.content.ContentProviderOperation) Uri(android.net.Uri) Test(org.junit.Test)

Example 72 with ContentProviderOperation

use of android.content.ContentProviderOperation in project android_frameworks_base by ResurrectionRemix.

the class TvInputManagerService method registerBroadcastReceivers.

private void registerBroadcastReceivers() {
    PackageMonitor monitor = new PackageMonitor() {

        private void buildTvInputList(String[] packages) {
            synchronized (mLock) {
                if (mCurrentUserId == getChangingUserId()) {
                    buildTvInputListLocked(mCurrentUserId, packages);
                    buildTvContentRatingSystemListLocked(mCurrentUserId);
                }
            }
        }

        @Override
        public void onPackageUpdateFinished(String packageName, int uid) {
            if (DEBUG)
                Slog.d(TAG, "onPackageUpdateFinished(packageName=" + packageName + ")");
            // This callback is invoked when the TV input is reinstalled.
            // In this case, isReplacing() always returns true.
            buildTvInputList(new String[] { packageName });
        }

        @Override
        public void onPackagesAvailable(String[] packages) {
            if (DEBUG) {
                Slog.d(TAG, "onPackagesAvailable(packages=" + Arrays.toString(packages) + ")");
            }
            // available.
            if (isReplacing()) {
                buildTvInputList(packages);
            }
        }

        @Override
        public void onPackagesUnavailable(String[] packages) {
            // unavailable.
            if (DEBUG) {
                Slog.d(TAG, "onPackagesUnavailable(packages=" + Arrays.toString(packages) + ")");
            }
            if (isReplacing()) {
                buildTvInputList(packages);
            }
        }

        @Override
        public void onSomePackagesChanged() {
            // the TV inputs.
            if (DEBUG)
                Slog.d(TAG, "onSomePackagesChanged()");
            if (isReplacing()) {
                if (DEBUG)
                    Slog.d(TAG, "Skipped building TV input list due to replacing");
                // methods instead.
                return;
            }
            buildTvInputList(null);
        }

        @Override
        public boolean onPackageChanged(String packageName, int uid, String[] components) {
            // the update can be handled in {@link #onSomePackagesChanged}.
            return true;
        }

        @Override
        public void onPackageRemoved(String packageName, int uid) {
            synchronized (mLock) {
                UserState userState = getOrCreateUserStateLocked(getChangingUserId());
                if (!userState.packageSet.contains(packageName)) {
                    // Not a TV input package.
                    return;
                }
            }
            ArrayList<ContentProviderOperation> operations = new ArrayList<>();
            String selection = TvContract.BaseTvColumns.COLUMN_PACKAGE_NAME + "=?";
            String[] selectionArgs = { packageName };
            operations.add(ContentProviderOperation.newDelete(TvContract.Channels.CONTENT_URI).withSelection(selection, selectionArgs).build());
            operations.add(ContentProviderOperation.newDelete(TvContract.Programs.CONTENT_URI).withSelection(selection, selectionArgs).build());
            operations.add(ContentProviderOperation.newDelete(TvContract.WatchedPrograms.CONTENT_URI).withSelection(selection, selectionArgs).build());
            ContentProviderResult[] results = null;
            try {
                ContentResolver cr = getContentResolverForUser(getChangingUserId());
                results = cr.applyBatch(TvContract.AUTHORITY, operations);
            } catch (RemoteException | OperationApplicationException e) {
                Slog.e(TAG, "error in applyBatch", e);
            }
            if (DEBUG) {
                Slog.d(TAG, "onPackageRemoved(packageName=" + packageName + ", uid=" + uid + ")");
                Slog.d(TAG, "results=" + results);
            }
        }
    };
    monitor.register(mContext, null, UserHandle.ALL, true);
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(Intent.ACTION_USER_SWITCHED);
    intentFilter.addAction(Intent.ACTION_USER_REMOVED);
    mContext.registerReceiverAsUser(new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (Intent.ACTION_USER_SWITCHED.equals(action)) {
                switchUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
            } else if (Intent.ACTION_USER_REMOVED.equals(action)) {
                removeUser(intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0));
            }
        }
    }, UserHandle.ALL, intentFilter, null, null);
}
Also used : Context(android.content.Context) ContentProviderResult(android.content.ContentProviderResult) IntentFilter(android.content.IntentFilter) ContentProviderOperation(android.content.ContentProviderOperation) ArrayList(java.util.ArrayList) Intent(android.content.Intent) BroadcastReceiver(android.content.BroadcastReceiver) PackageMonitor(com.android.internal.content.PackageMonitor) ContentResolver(android.content.ContentResolver) RemoteException(android.os.RemoteException) OperationApplicationException(android.content.OperationApplicationException)

Example 73 with ContentProviderOperation

use of android.content.ContentProviderOperation in project Etar-Calendar by Etar-Group.

the class EditEventHelperTest method testUpdatePastEvents.

@Smoke
@SmallTest
public void testUpdatePastEvents() {
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    ArrayList<ContentProviderOperation> expectedOps = new ArrayList<ContentProviderOperation>();
    // Sep 3, 2016, 12AM UTC time
    long initialBeginTime = 1472864400000L;
    mValues = new ContentValues();
    mModel1 = buildTestModel();
    mModel1.mUri = (AUTHORITY_URI + TEST_EVENT_ID);
    mActivity = buildTestContext();
    mHelper = new EditEventHelper(mActivity, null);
    // yyyymmddThhmmssZ
    mValues.put(Events.RRULE, "FREQ=DAILY;UNTIL=20160903;WKST=SU");
    mValues.put(Events.DTSTART, TEST_START);
    ContentProviderOperation.Builder b = ContentProviderOperation.newUpdate(Uri.parse(mModel1.mUri)).withValues(mValues);
    expectedOps.add(b.build());
    mHelper.updatePastEvents(ops, mModel1, initialBeginTime);
    assertEquals(expectedOps, ops);
    mModel1.mAllDay = false;
    // yyyymmddThhmmssZ
    mValues.put(Events.RRULE, "FREQ=DAILY;UNTIL=20160903T005959Z;WKST=SU");
    expectedOps.clear();
    b = ContentProviderOperation.newUpdate(Uri.parse(mModel1.mUri)).withValues(mValues);
    expectedOps.add(b.build());
    ops.clear();
    mHelper.updatePastEvents(ops, mModel1, initialBeginTime);
    assertEquals(expectedOps, ops);
}
Also used : ContentValues(android.content.ContentValues) ContentProviderOperation(android.content.ContentProviderOperation) ArrayList(java.util.ArrayList) SmallTest(android.test.suitebuilder.annotation.SmallTest) Smoke(android.test.suitebuilder.annotation.Smoke)

Example 74 with ContentProviderOperation

use of android.content.ContentProviderOperation in project Etar-Calendar by Etar-Group.

the class EditEventHelperTest method addExpectedMinutes.

private void addExpectedMinutes(ArrayList<ContentProviderOperation> expectedOps) {
    ContentProviderOperation.Builder b;
    mValues = new ContentValues();
    mValues.clear();
    mValues.put(Reminders.MINUTES, 5);
    mValues.put(Reminders.METHOD, Reminders.METHOD_DEFAULT);
    mValues.put(Reminders.EVENT_ID, TEST_EVENT_ID);
    b = ContentProviderOperation.newInsert(Reminders.CONTENT_URI).withValues(mValues);
    expectedOps.add(b.build());
    mValues.clear();
    mValues.put(Reminders.MINUTES, 10);
    mValues.put(Reminders.METHOD, Reminders.METHOD_DEFAULT);
    mValues.put(Reminders.EVENT_ID, TEST_EVENT_ID);
    b = ContentProviderOperation.newInsert(Reminders.CONTENT_URI).withValues(mValues);
    expectedOps.add(b.build());
    mValues.clear();
    mValues.put(Reminders.MINUTES, 15);
    mValues.put(Reminders.METHOD, Reminders.METHOD_DEFAULT);
    mValues.put(Reminders.EVENT_ID, TEST_EVENT_ID);
    b = ContentProviderOperation.newInsert(Reminders.CONTENT_URI).withValues(mValues);
    expectedOps.add(b.build());
}
Also used : ContentValues(android.content.ContentValues) ContentProviderOperation(android.content.ContentProviderOperation)

Example 75 with ContentProviderOperation

use of android.content.ContentProviderOperation in project Etar-Calendar by Etar-Group.

the class EditEventHelperTest method addExpectedMinutesWithBackRef.

private void addExpectedMinutesWithBackRef(ArrayList<ContentProviderOperation> expectedOps) {
    ContentProviderOperation.Builder b;
    mValues = new ContentValues();
    mValues.clear();
    mValues.put(Reminders.MINUTES, 5);
    mValues.put(Reminders.METHOD, Reminders.METHOD_DEFAULT);
    b = ContentProviderOperation.newInsert(Reminders.CONTENT_URI).withValues(mValues);
    b.withValueBackReference(Reminders.EVENT_ID, TEST_EVENT_INDEX_ID);
    expectedOps.add(b.build());
    mValues.clear();
    mValues.put(Reminders.MINUTES, 10);
    mValues.put(Reminders.METHOD, Reminders.METHOD_DEFAULT);
    b = ContentProviderOperation.newInsert(Reminders.CONTENT_URI).withValues(mValues);
    b.withValueBackReference(Reminders.EVENT_ID, TEST_EVENT_INDEX_ID);
    expectedOps.add(b.build());
    mValues.clear();
    mValues.put(Reminders.MINUTES, 15);
    mValues.put(Reminders.METHOD, Reminders.METHOD_DEFAULT);
    b = ContentProviderOperation.newInsert(Reminders.CONTENT_URI).withValues(mValues);
    b.withValueBackReference(Reminders.EVENT_ID, TEST_EVENT_INDEX_ID);
    expectedOps.add(b.build());
}
Also used : ContentValues(android.content.ContentValues) ContentProviderOperation(android.content.ContentProviderOperation)

Aggregations

ContentProviderOperation (android.content.ContentProviderOperation)75 ArrayList (java.util.ArrayList)53 OperationApplicationException (android.content.OperationApplicationException)34 Uri (android.net.Uri)27 ContentValues (android.content.ContentValues)25 RemoteException (android.os.RemoteException)22 Cursor (android.database.Cursor)14 ContentProviderResult (android.content.ContentProviderResult)13 ContentResolver (android.content.ContentResolver)9 LinkedList (java.util.LinkedList)8 IOException (java.io.IOException)7 JSONArray (org.json.JSONArray)6 JSONException (org.json.JSONException)6 JSONObject (org.json.JSONObject)6 Intent (android.content.Intent)5 List (java.util.List)5 SuppressLint (android.annotation.SuppressLint)4 BroadcastReceiver (android.content.BroadcastReceiver)4 Context (android.content.Context)4 IntentFilter (android.content.IntentFilter)4