示例#1
0
  /*
     This test uses the database directly to insert and then uses the ContentProvider to
     read out the data.  Uncomment this test to see if your location queries are
     performing correctly.
  */
  public void testBasicLocationQueries() {
    // insert our test records into the database
    WeatherDatabaseHelper dbHelper = new WeatherDatabaseHelper(mContext);
    SQLiteDatabase db = dbHelper.getWritableDatabase();

    ContentValues testValues = TestUtilities.createNorthPoleLocationValues();
    long locationRowId = TestUtilities.insertNorthPoleLocationValues(mContext);

    // Test the basic content provider query
    Cursor locationCursor =
        mContext.getContentResolver().query(LocationEntry.CONTENT_URI, null, null, null, null);

    // Make sure we get the correct cursor out of the database
    TestUtilities.validateCursor(
        "testBasicLocationQueries, location query", locationCursor, testValues);

    // Has the NotificationUri been set correctly? --- we can only test this easily against API
    // level 19 or greater because getNotificationUri was added in API level 19.
    if (Build.VERSION.SDK_INT >= 19) {
      assertEquals(
          "Error: Location Query did not properly set NotificationUri",
          locationCursor.getNotificationUri(),
          LocationEntry.CONTENT_URI);
    }
  }
  private Loader getInstanceUncheckedLocked(String documentId) throws FileNotFoundException {
    try {
      final ParsedDocumentId id = ParsedDocumentId.fromDocumentId(documentId, mIdDelimiter);
      if (mArchives.get(id.mArchiveId) != null) {
        return mArchives.get(id.mArchiveId);
      }

      final Cursor cursor =
          mProvider.queryDocument(
              id.mArchiveId, new String[] {Document.COLUMN_MIME_TYPE, COLUMN_LOCAL_FILE_PATH});
      cursor.moveToFirst();
      final String mimeType = cursor.getString(cursor.getColumnIndex(Document.COLUMN_MIME_TYPE));
      Preconditions.checkArgument(isSupportedArchiveType(mimeType), "Unsupported archive type.");
      final int columnIndex = cursor.getColumnIndex(COLUMN_LOCAL_FILE_PATH);
      final String localFilePath = columnIndex != -1 ? cursor.getString(columnIndex) : null;
      final File localFile = localFilePath != null ? new File(localFilePath) : null;
      final Uri notificationUri = cursor.getNotificationUri();
      final Loader loader = new Loader(mProvider, localFile, id, mIdDelimiter, notificationUri);

      // Remove the instance from mArchives collection once the archive file changes.
      if (notificationUri != null) {
        final LruCache<String, Loader> finalArchives = mArchives;
        mProvider
            .getContext()
            .getContentResolver()
            .registerContentObserver(
                notificationUri,
                false,
                new ContentObserver(null) {
                  @Override
                  public void onChange(boolean selfChange, Uri uri) {
                    synchronized (mArchives) {
                      final Loader currentLoader = mArchives.get(id.mArchiveId);
                      if (currentLoader == loader) {
                        mArchives.remove(id.mArchiveId);
                      }
                    }
                  }
                });
      }

      mArchives.put(id.mArchiveId, loader);
      return loader;
    } catch (IOException e) {
      // DocumentsProvider doesn't use IOException. For consistency convert it to
      // IllegalStateException.
      throw new IllegalStateException(e);
    }
  }
 private void copyNotificationUri(MatrixCursor result, Cursor cursor) {
   result.setNotificationUri(getContext().getContentResolver(), cursor.getNotificationUri());
 }
 public Uri getNotificationUri() {
   return mDatabaseCursor.getNotificationUri();
 }