content: package com.example.android.pets.Data;
  
  import android.content.ContentProvider;
  import android.content.ContentUris;
  import android.content.ContentValues;
  import android.content.UriMatcher;
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.net.Uri;
  import android.util.Log;
  import com.example.android.pets.Data.PetContract.PetEntry;
  
  /**
   * {@link ContentProvider} for Pets app.
   */
  public class PetProvider extends ContentProvider {
  
      /** URI matcher code for the content URI for the pets table */
      private static final int PETS = 100;
  
      /** URI matcher code for the content URI for a single pet in the pets table */
      private static final int PET_ID = 101;
  
      /**
       * UriMatcher object to match a content URI to a corresponding code.
       * The input passed into the constructor represents the code to return for the root URI.
       * It's common to use NO_MATCH as the input for this case.
       */
      private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
  
      // Static initializer. This is run the first time anything is called from this class.
      static {
          // The calls to addURI() go here, for all of the content URI patterns that the provider
          // should recognize. All paths added to the UriMatcher have a corresponding code to return
          // when a match is found.
  
          // TODO: Add 2 content URIs to URI matcher
          sUriMatcher.addURI(PetContract.CONTENT_AUTHORITY, PetContract.PATH_PETS,PETS);
          sUriMatcher.addURI(PetContract.CONTENT_AUTHORITY, PetContract.PATH_PETS + "/#",PET_ID);
      }
  
      /** Tag for the log messages */
      public static final String LOG_TAG = PetProvider.class.getSimpleName();
  
      /**
       * Initialize the provider and the database helper object.
       */
      PetDbHelper mDbHelper;
      @Override
      public boolean onCreate() {
          mDbHelper = new PetDbHelper(getContext());
          return true;
      }
  
      /**
       * Perform the query for the given URI. Use the given projection, selection, selection arguments, and sort order.
       */
      @Override
      public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
          // Get readable database
          SQLiteDatabase db = mDbHelper.getReadableDatabase();
  
          // This cursor will hold the result of the query
          Cursor cursor = null;
  
          // Figure out if the URI matcher can match the URI to a specific code
          int match = sUriMatcher.match(uri);
          switch (match) {
              case PETS:
                  // For the PETS code, query the pets table directly with the given
                  // projection, selection, selection arguments, and sort order. The cursor
                  // could contain multiple rows of the pets table.
  
                  cursor = db.query(PetContract.PetEntry.TABLE_NAME, projection, null,null,null,null,null);
                  break;
              case PET_ID:
                  // For the PET_ID code, extract out the ID from the URI.
                  // For an example URI such as "content://com.example.android.pets/pets/3",
                  // the selection will be "_id=?" and the selection argument will be a
                  // String array containing the actual ID of 3 in this case.
                  //
                  // For every "?" in the selection, we need to have an element in the selection
                  // arguments that will fill in the "?". Since we have 1 question mark in the
                  // selection, we have 1 String in the selection arguments' String array.
                  selection = PetContract.PetEntry._ID + "=?";
                  selectionArgs = new String[] { String.valueOf(ContentUris.parseId(uri)) };
  
                  // This will perform a query on the pets table where the _id equals 3 to return a
                  // Cursor containing that row of the table.
                  cursor = db.query(PetContract.PetEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
                  break;
              default:
                  throw new IllegalArgumentException("Cannot query unknown URI " + uri);
          }
          return cursor;
      }
  
      /**
       * Insert new data into the provider with the given ContentValues.
       */
      @Override
      public Uri insert(Uri uri, ContentValues contentValues) {
          final int match = sUriMatcher.match(uri);
          switch (match) {
              case PETS:
                  return insertPet(uri, contentValues);
              default:
                  throw new IllegalArgumentException("Insertion is not supported for " + uri);
          }
      }
  
      /**
       * Insert a pet into the database with the given content values. Return the new content URI
       * for that specific row in the database.
       */
      private Uri insertPet(Uri uri, ContentValues values) {
          //Get writeable Database
          SQLiteDatabase db = mDbHelper.getWritableDatabase();
  
          long id = db.insert(PetContract.PetEntry.TABLE_NAME,null, values);
  
          // If the ID is -1, then the insertion failed. Log an error and return null.
          if (id == -1) {
              Log.e(LOG_TAG, "Failed to insert row for " + uri);
              return null;
          }
  
          // Once we know the ID of the new row in the table,
          // return the new URI with the ID appended to the end of it
          return ContentUris.withAppendedId(uri, id);
      }
  
      /**
       * Updates the data at the given selection and selection arguments, with the new ContentValues.
       */
      @Override
      public int update(Uri uri, ContentValues contentValues, String selection,
                        String[] selectionArgs) {
          final int match = sUriMatcher.match(uri);
          switch (match) {
              case PETS:
                  return updatePet(uri, contentValues, selection, selectionArgs);
              case PET_ID:
                  // For the PET_ID code, extract out the ID from the URI,
                  // so we know which row to update. Selection will be "_id=?" and selection
                  // arguments will be a String array containing the actual ID.
                  selection = PetContract.PetEntry._ID + "=?";
                  selectionArgs = new String[] { String.valueOf(ContentUris.parseId(uri)) };
                  return updatePet(uri, contentValues, selection, selectionArgs);
              default:
                  throw new IllegalArgumentException("Update is not supported for " + uri);
          }
      }
  
      /**
       * Update pets in the database with the given content values. Apply the changes to the rows
       * specified in the selection and selection arguments (which could be 0 or 1 or more pets).
       * Return the number of rows that were successfully updated.
       */
      private int updatePet(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
          //Get writeable Database
          SQLiteDatabase db = mDbHelper.getWritableDatabase();
  
          return db.update(PetContract.PetEntry.TABLE_NAME, values, selection, selectionArgs);
      }
  
      /**
       * Delete the data at the given selection and selection arguments.
       */
      @Override
      public int delete(Uri uri, String selection, String[] selectionArgs) {
          // Get writeable database
          SQLiteDatabase database = mDbHelper.getWritableDatabase();
  
          final int match = sUriMatcher.match(uri);
          switch (match) {
              case PETS:
                  // Delete all rows that match the selection and selection args
                  return database.delete(PetEntry.TABLE_NAME, selection, selectionArgs);
              case PET_ID:
                  // Delete a single row given by the ID in the URI
                  selection = PetEntry._ID + "=?";
                  selectionArgs = new String[]{String.valueOf(ContentUris.parseId(uri))};
                  return database.delete(PetEntry.TABLE_NAME, selection, selectionArgs);
              default:
                  throw new IllegalArgumentException("Deletion is not supported for " + uri);
          }
      }
  
      /**
       * Returns the MIME type of data for the content URI.
       */
      @Override
      public String getType(Uri uri) {
          final int match = sUriMatcher.match(uri);
          switch (match) {
              case PETS:
                  return PetEntry.CONTENT_LIST_TYPE;
              case PET_ID:
                  return PetEntry.CONTENT_ITEM_TYPE;
              default:
                  throw new IllegalStateException("Unknown URI " + uri + " with match " + match);
          }
      }
  }
  