public void testRestrictedQueryParam() throws Exception {
    final long greyContact = mGrey.createContact(true, GENERIC_NAME);
    final long greyPhone = mGrey.createPhone(greyContact, PHONE_GREY);

    final Uri greyUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, greyContact);
    final Uri redUri =
        greyUri
            .buildUpon()
            .appendQueryParameter(ContactsContract.REQUESTING_PACKAGE_PARAM_KEY, mRed.packageName)
            .build();

    // When calling normally, we have access to protected
    mGrey.ensureCallingPackage();
    EntityIterator iterator = mGrey.resolver.queryEntities(greyUri, null, null, null);
    while (iterator.hasNext()) {
      final Entity entity = iterator.next();
      final long rawContactId = entity.getEntityValues().getAsLong(RawContacts._ID);
      assertTrue(rawContactId == greyContact);
    }

    // When calling on behalf of another package, protected is omitted
    mGrey.ensureCallingPackage();
    iterator = mGrey.resolver.queryEntities(redUri, null, null, null);
    while (iterator.hasNext()) {
      final Entity entity = iterator.next();
      final long rawContactId = entity.getEntityValues().getAsLong(RawContacts._ID);
      assertTrue(rawContactId != greyContact);
    }
  }
    @Override
    public AccountSet loadInBackground() {
      Context context = getContext();
      final AccountTypeManager accountTypes = AccountTypeManager.getInstance(context);
      final ContentResolver resolver = context.getContentResolver();

      final AccountSet accounts = new AccountSet();
      for (AccountWithDataSet account : accountTypes.getAccounts(false)) {
        final AccountType accountType = accountTypes.getAccountTypeForAccount(account);
        if (accountType.isExtension() && !account.hasData(context)) {
          // Extension with no data -- skip.
          continue;
        }

        AccountDisplay accountDisplay =
            new AccountDisplay(resolver, account.name, account.type, account.dataSet);

        final Uri.Builder groupsUri =
            Groups.CONTENT_URI
                .buildUpon()
                .appendQueryParameter(Groups.ACCOUNT_NAME, account.name)
                .appendQueryParameter(Groups.ACCOUNT_TYPE, account.type);
        if (account.dataSet != null) {
          groupsUri.appendQueryParameter(Groups.DATA_SET, account.dataSet).build();
        }
        final Cursor cursor = resolver.query(groupsUri.build(), null, null, null, null);
        if (cursor == null) {
          continue;
        }
        android.content.EntityIterator iterator = ContactsContract.Groups.newEntityIterator(cursor);
        try {
          boolean hasGroups = false;

          // Create entries for each known group
          while (iterator.hasNext()) {
            final ContentValues values = iterator.next().getEntityValues();
            final GroupDelta group = GroupDelta.fromBefore(values);
            accountDisplay.addGroup(group);
            hasGroups = true;
          }
          // Create single entry handling ungrouped status
          accountDisplay.mUngrouped =
              GroupDelta.fromSettings(
                  resolver, account.name, account.type, account.dataSet, hasGroups);
          accountDisplay.addGroup(accountDisplay.mUngrouped);
        } finally {
          iterator.close();
        }

        accounts.add(accountDisplay);
      }

      return accounts;
    }
  public static Event getEvent(Context context, long eventId) {
    Event event = null;

    Uri uri = CalendarContract.Events.CONTENT_URI;
    Builder builder = uri.buildUpon();
    ContentUris.appendId(builder, eventId);
    uri = builder.build();

    // Run query
    ContentResolver cr = context.getContentResolver();
    Cursor cur = cr.query(uri, EVENT_PROJECTION, null, null, null);
    EntityIterator it = CalendarContract.EventsEntity.newEntityIterator(cur, cr);
    if (it.hasNext()) {
      event = new Event(it.next());
    }
    cur.close();

    return event;
  }
  public static ArrayList<Event> getEvents(Context context, long calId) {
    ArrayList<Event> events = new ArrayList<Event>();

    // Run query
    ContentResolver cr = context.getContentResolver();

    String selection = "(" + CalendarContract.Events.CALENDAR_ID + " = ?)";
    String[] selectionArgs = new String[] {calId + ""};
    // Submit the query and get a Cursor object back.
    Cursor cur =
        cr.query(
            CalendarContract.Events.CONTENT_URI, EVENT_PROJECTION, selection, selectionArgs, null);

    EntityIterator it = CalendarContract.EventsEntity.newEntityIterator(cur, cr);
    while (it.hasNext()) {
      Entity entity = it.next();
      events.add(new Event(entity));
    }
    cur.close();

    return events;
  }
 /**
  * Create an {@link EntitySet} based on {@link Contacts} specified by the given query parameters.
  * This closes the {@link EntityIterator} when finished, so it doesn't subscribe to updates.
  */
 public static EntitySet fromQuery(
     ContentResolver resolver, String selection, String[] selectionArgs, String sortOrder) {
   EntityIterator iterator = null;
   final EntitySet state = new EntitySet();
   try {
     // Perform background query to pull contact details
     iterator =
         resolver.queryEntities(RawContacts.CONTENT_URI, selection, selectionArgs, sortOrder);
     while (iterator.hasNext()) {
       // Read all contacts into local deltas to prepare for edits
       final Entity before = iterator.next();
       final EntityDelta entity = EntityDelta.fromBefore(before);
       state.add(entity);
     }
   } catch (RemoteException e) {
     throw new IllegalStateException("Problem querying contact details", e);
   } finally {
     if (iterator != null) {
       iterator.close();
     }
   }
   return state;
 }