Created
October 8, 2015 17:42
-
-
Save TristanWiley/649b09468fa44ca728e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.novaapps.booktrackr.Database; | |
import android.provider.BaseColumns; | |
/** | |
* Created by tristan.wiley on 10/6/2015. | |
*/ | |
public final class BookContract { | |
// To prevent someone from accidentally instantiating the contract class, | |
// give it an empty constructor. | |
public BookContract() {} | |
/* Inner class that defines the table contents */ | |
public static abstract class FeedEntry implements BaseColumns { | |
public static final String TABLE_NAME = "book"; | |
public static final String COLUMN_NAME_ENTRY_ID = "entryid"; | |
public static final String COLUMN_NAME_TITLE = "title"; | |
public static final String COLUMN_NAME_AUTHOR = "author"; | |
public static final String COLUMN_NAME_RATING = "rating"; | |
public static final String COLUMN_NAME_ISBN = "isbn"; | |
public static final String COLUMN_NAME_DESCRIPTION = "description"; | |
public static final String COLUMN_NAME_THUMBNAIL = "image"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.novaapps.booktrackr.Database; | |
import android.content.Context; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.database.sqlite.SQLiteOpenHelper; | |
/** | |
* Created by tristan.wiley on 10/6/2015. | |
*/ | |
public class BookDbHelper extends SQLiteOpenHelper { | |
public static final int DATABASE_VERSION = 1; | |
public static final String DATABASE_NAME = "BookDatabase.db"; | |
private static final String TEXT_TYPE = " TEXT"; | |
private static final String COMMA_SEP = ","; | |
private static final String SQL_CREATE_ENTRIES = | |
"CREATE TABLE " + BookContract.FeedEntry.TABLE_NAME + " (" + | |
BookContract.FeedEntry._ID + " INTEGER PRIMARY KEY," + | |
BookContract.FeedEntry.COLUMN_NAME_ENTRY_ID + TEXT_TYPE + COMMA_SEP + | |
BookContract.FeedEntry.COLUMN_NAME_TITLE + TEXT_TYPE + COMMA_SEP + BookContract.FeedEntry.COLUMN_NAME_AUTHOR + TEXT_TYPE + COMMA_SEP | |
+ BookContract.FeedEntry.COLUMN_NAME_RATING + TEXT_TYPE + COMMA_SEP + BookContract.FeedEntry.COLUMN_NAME_ISBN + TEXT_TYPE + COMMA_SEP + | |
BookContract.FeedEntry.COLUMN_NAME_DESCRIPTION + TEXT_TYPE + COMMA_SEP + BookContract.FeedEntry.COLUMN_NAME_THUMBNAIL + TEXT_TYPE + ")"; | |
private static final String SQL_DELETE_ENTRIES = | |
"DROP TABLE IF EXISTS " + BookContract.FeedEntry.TABLE_NAME; | |
public BookDbHelper(Context context){ | |
super(context, DATABASE_NAME, null, DATABASE_VERSION); | |
} | |
@Override | |
public void onCreate(SQLiteDatabase db) { | |
db.execSQL(SQL_CREATE_ENTRIES); | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | |
// This database is only a cache for online data, so its upgrade policy is | |
// to simply to discard the data and start over | |
db.execSQL(SQL_DELETE_ENTRIES); | |
onCreate(db); | |
} | |
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { | |
onUpgrade(db, oldVersion, newVersion); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.novaapps.booktrackr; | |
import android.content.ContentValues; | |
import android.content.DialogInterface; | |
import android.content.Intent; | |
import android.database.Cursor; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.os.AsyncTask; | |
import android.os.Bundle; | |
import android.support.design.widget.Snackbar; | |
import android.support.v7.app.AlertDialog; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.LinearLayoutManager; | |
import android.support.v7.widget.RecyclerView; | |
import android.support.v7.widget.Toolbar; | |
import android.view.LayoutInflater; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.Toast; | |
import com.google.android.gms.common.api.CommonStatusCodes; | |
import com.novaapps.booktrackr.Database.BookContract; | |
import com.novaapps.booktrackr.Database.BookDbHelper; | |
import com.novaapps.booktrackr.Scanning.BarcodeCaptureActivity; | |
import com.novaapps.booktrackr.Scanning.BarcodeGraphic; | |
import com.novaapps.booktrackr.Scanning.GraphicOverlay; | |
import java.io.BufferedInputStream; | |
import java.io.BufferedReader; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class MainActivity extends AppCompatActivity { | |
private GraphicOverlay<BarcodeGraphic> mGraphicOverlay; | |
private List<Book> books; | |
RecyclerView rv; | |
String title; | |
String author; | |
String imageUrl; | |
BookDbHelper mDbHelper; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); | |
setSupportActionBar(toolbar); | |
rv = (RecyclerView) findViewById(R.id.rv); | |
rv.setHasFixedSize(true); | |
mDbHelper = new BookDbHelper(getApplicationContext()); | |
mGraphicOverlay = (GraphicOverlay<BarcodeGraphic>) findViewById(R.id.graphicOverlay); | |
LinearLayoutManager llm = new LinearLayoutManager(this); | |
rv.setLayoutManager(llm); | |
rv.setHasFixedSize(true); | |
books = new ArrayList<>(); | |
initializeAdapter(); | |
Intent i = new Intent(MainActivity.this, ScrollingActivity.class); | |
startActivity(i); | |
} | |
private void initializeAdapter() { | |
CardAdapter adapter = new CardAdapter(getApplicationContext(), books); | |
rv.setAdapter(adapter); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.menu_main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
//noinspection SimplifiableIfStatement | |
if (id == R.id.action_add) { | |
// get prompts.xml view | |
LayoutInflater li = LayoutInflater.from(getApplicationContext()); | |
View promptsView = li.inflate(R.layout.add_dialog, null); | |
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this); | |
// set prompts.xml to alertdialog builder | |
alertDialogBuilder.setView(promptsView); | |
final EditText userInput = (EditText) promptsView | |
.findViewById(R.id.bookIsbn); | |
final Button barcodeButton = (Button) promptsView.findViewById(R.id.button); | |
barcodeButton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Intent intent = new Intent(MainActivity.this, BarcodeCaptureActivity.class); | |
intent.putExtra(BarcodeCaptureActivity.AutoFocus, true); | |
intent.putExtra(BarcodeCaptureActivity.UseFlash, false); | |
startActivityForResult(intent, 9001); | |
} | |
}); | |
alertDialogBuilder | |
.setCancelable(false) | |
.setPositiveButton("OK", | |
new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int id) { | |
new getJsonFromUrl().execute("https://www.googleapis.com/books/v1/volumes?q=isbn:" + userInput.getText()); | |
} | |
}) | |
.setNegativeButton("Cancel", | |
new DialogInterface.OnClickListener() { | |
public void onClick(DialogInterface dialog, int id) { | |
dialog.cancel(); | |
} | |
}); | |
// create alert dialog | |
AlertDialog alertDialog = alertDialogBuilder.create(); | |
// show it | |
alertDialog.show(); | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
switch(requestCode) { | |
case 9001: | |
if (resultCode == CommonStatusCodes.SUCCESS) { | |
Bundle extras = data.getExtras(); | |
String isbn = extras.getString("Barcode"); | |
new getJsonFromUrl().execute("https://www.googleapis.com/books/v1/volumes?q=isbn:" + isbn); | |
} | |
break; | |
} | |
} | |
public class getJsonFromUrl extends AsyncTask<String, String, String> { | |
HttpURLConnection urlConnection; | |
@Override | |
protected String doInBackground(String... args) { | |
StringBuilder result = new StringBuilder(); | |
try { | |
URL url = new URL(args[0]); | |
urlConnection = (HttpURLConnection) url.openConnection(); | |
InputStream in = new BufferedInputStream(urlConnection.getInputStream()); | |
BufferedReader reader = new BufferedReader(new InputStreamReader(in)); | |
String line; | |
while ((line = reader.readLine()) != null) { | |
result.append(line); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally { | |
urlConnection.disconnect(); | |
} | |
return result.toString(); | |
} | |
@Override | |
protected void onPostExecute(String result) { | |
if(result != null) { | |
GetJson getJson = new GetJson(); | |
getJson.getJson(result); | |
title = getJson.getTitle(); | |
author = getJson.getAuthor(); | |
imageUrl = getJson.getImage(); | |
String isbn = getJson.getIsbn(); | |
String description = getJson.getDescription(); | |
books.add(new Book(imageUrl, title, author, 5)); | |
addBookToDatabase(books.size(), title, author, 5, isbn, description, imageUrl); | |
initializeAdapter(); | |
}else{ | |
Snackbar.make(findViewById(R.id.main_activity), "Result not found, try again?", Snackbar.LENGTH_SHORT); | |
} | |
} | |
} | |
public long addBookToDatabase(int id, String title, String author, int rating, String isbn, String description, String thumbnail){ | |
// Gets the data repository in write mode | |
SQLiteDatabase db = mDbHelper.getWritableDatabase(); | |
// Create a new map of values, where column names are the keys | |
ContentValues values = new ContentValues(); | |
values.put(BookContract.FeedEntry.COLUMN_NAME_ENTRY_ID, id); | |
values.put(BookContract.FeedEntry.COLUMN_NAME_TITLE, title); | |
values.put(BookContract.FeedEntry.COLUMN_NAME_AUTHOR, author); | |
values.put(BookContract.FeedEntry.COLUMN_NAME_RATING, rating); | |
values.put(BookContract.FeedEntry.COLUMN_NAME_ISBN, isbn); | |
values.put(BookContract.FeedEntry.COLUMN_NAME_DESCRIPTION, description); | |
values.put(BookContract.FeedEntry.COLUMN_NAME_THUMBNAIL, thumbnail); | |
// Insert the new row, returning the primary key value of the new row | |
long newRowId; | |
newRowId = db.insert(BookContract.FeedEntry.TABLE_NAME, null, values); | |
return newRowId; | |
} | |
public void readDatabase(){ | |
SQLiteDatabase db = mDbHelper.getReadableDatabase(); | |
// Define a projection that specifies which columns from the database | |
// you will actually use after this query. | |
String[] projection = { | |
BookContract.FeedEntry._ID, | |
BookContract.FeedEntry.COLUMN_NAME_TITLE, | |
BookContract.FeedEntry.COLUMN_NAME_AUTHOR, | |
BookContract.FeedEntry.COLUMN_NAME_RATING, | |
BookContract.FeedEntry.COLUMN_NAME_ISBN, | |
BookContract.FeedEntry.COLUMN_NAME_DESCRIPTION, | |
BookContract.FeedEntry.COLUMN_NAME_THUMBNAIL | |
}; | |
// How you want the results sorted in the resulting Cursor | |
String sortOrder = | |
BookContract.FeedEntry.COLUMN_NAME_ENTRY_ID + " DESC"; | |
Cursor c = db.query( | |
BookContract.FeedEntry.TABLE_NAME, // The table to query | |
projection, // The columns to return | |
BookContract.FeedEntry.COLUMN_NAME_TITLE, // The columns for the WHERE clause | |
new String[] {BookContract.FeedEntry.COLUMN_NAME_TITLE}, // The values for the WHERE clause | |
null, // don't group the rows | |
null, // don't filter by row groups | |
sortOrder // The sort order | |
); | |
Toast.makeText(getApplicationContext(), c.getString(0), Toast.LENGTH_SHORT).show(); | |
c.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment