/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.android.pets;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.example.android.pets.data.PetContract;
import com.example.android.pets.data.PetDbHelper;

/**
 * Displays list of pets that were entered and stored in the app.
 */
public class CatalogActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_catalog);

        // Setup FAB to open EditorActivity
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(CatalogActivity.this, EditorActivity.class);
                startActivity(intent);
            }
        });

        displayDatabaseInfo();

    }
    private void displayDatabaseInfo() {
        // To access our database, we instantiate our subclass of SQLiteOpenHelper
        // and pass the context, which is the current activity.
        PetDbHelper mDbHelper = new PetDbHelper(this);

        // Create and/or open a database to read from it
        SQLiteDatabase db = mDbHelper.getReadableDatabase();

        // Perform this raw SQL query "SELECT * FROM pets"
        // to get a Cursor that contains all rows from the pets table.
        Cursor cursor = db.rawQuery("SELECT * FROM " + PetContract.PetEntry.TABLE_NAME, null);
        try {
            // Display the number of rows in the Cursor (which reflects the number of rows in the
            // pets table in the database).
            TextView displayView = (TextView) findViewById(R.id.text_view_pet);
            displayView.setText("Number of rows in pets database table: " + cursor.getCount());
        } finally {
            // Always close the cursor when you're done reading from it. This releases all its
            // resources and makes it invalid.
            cursor.close();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu options from the res/menu/menu_catalog.xml file.
        // This adds menu items to the app bar.
        getMenuInflater().inflate(R.menu.menu_catalog, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // User clicked on a menu option in the app bar overflow menu
        switch (item.getItemId()) {
            // Respond to a click on the "Insert dummy data" menu option
            case R.id.action_insert_dummy_data:
                // Do nothing for now
                return true;
            // Respond to a click on the "Delete all entries" menu option
            case R.id.action_delete_all_entries:
                // Do nothing for now
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

@@ -648,16 +648,54 @@
 .pets;%0A%0A
+import android.content.ContentValues;%0A
 import a
@@ -717,16 +717,16 @@
 Intent;%0A
-
 import a
@@ -796,16 +796,65 @@
 tabase;%0A
+import android.database.sqlite.SQLiteOpenHelper;%0A
 import a
@@ -979,16 +979,41 @@
 tivity;%0A
+import android.util.Log;%0A
 import a
@@ -1218,16 +1218,75 @@
 bHelper;
+%0Aimport com.example.android.pets.data.PetContract.PetEntry;
 %0A%0A/**%0A *
@@ -1411,16 +1411,52 @@
 vity %7B%0A%0A
+    private PetDbHelper mDbHelper;%0A%0A
     @Ove
@@ -2017,24 +2017,68 @@
        %7D);%0A%0A
+%0A        mDbHelper= new PetDbHelper(this);%0A%0A
         disp
@@ -3503,32 +3503,581 @@
 rn true;%0A    %7D%0A%0A
+    private void insertPet()%7B%0A        //Get the data repository in write mode%0A        SQLiteDatabase db=mDbHelper.getWritableDatabase();%0A%0A        ContentValues values= new ContentValues();%0A        values.put(PetEntry.COLUMN_PET_NAME,%22Toto%22);%0A        values.put(PetEntry.COLUMN_PET_BREED,%22Terrier%22);%0A        values.put(PetEntry.COLUMN_PET_GENDER,PetEntry.GENDER_MALE);%0A        values.put(PetEntry.COLUMN_PET_WEIGHT,7);%0A%0A        long newrowId=db.insert(PetEntry.TABLE_NAME,null,values);%0A%0A        Log.v(%22CatalogActivity%22,%22New Row Id%22+newrowId);%0A    %7D%0A%0A
     @Override%0A  
@@ -4375,37 +4375,67 @@
         
-// Do nothing for now
+insertPet();%0A                displayDatabaseInfo();
 %0A       

Add dummy pet to the db