Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

Commit

Permalink
bug/syntax fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
schordas committed Jul 27, 2015
1 parent ca8fc33 commit bbff4cc
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions android-content-provider.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id="android-content-provider" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="java-gradle" name="Java-Gradle">
<configuration>
<option name="BUILD_FOLDER_PATH" value="$MODULE_DIR$/build" />
<option name="BUILDABLE" value="false" />
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
3 changes: 1 addition & 2 deletions app/app.iml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id=":app" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="AndroidFlavors" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
<module external.linked.project.id=":app" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="android-content-provider" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="android-gradle" name="Android-Gradle">
<configuration>
Expand Down Expand Up @@ -84,7 +84,6 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Android API 22 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sam_chordas.android.androidflavors;
package com.sam_chordas.android.androidflavors.data;

import android.content.ContentResolver;
import android.content.ContentUris;
Expand All @@ -9,7 +9,7 @@ public class FlavorsContract{

public static final String CONTENT_AUTHORITY = "com.sam_chordas.android.androidflavors.app";

public static final BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);
public static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);

public static final String FLAVOR = "flavors";

Expand All @@ -22,8 +22,8 @@ public static final class FlavorEntry implements BaseColumns{
public static final String COLUMN_VERSION_NUMBER = "version_number";

public static final Uri CONTENT_URI = BASE_CONTENT_URI.buildUpon()
.appendPath(PATH_MOVIES).build();
public static final String CONTENT_TYPE =
.appendPath(FLAVOR).build();
public static final String CONTENT_DIR_TYPE =
ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + FLAVOR;
public static final String CONTENT_ITEM_TYPE =
ContentResolver.CURSOR_ITEM_BASE_TYPE +"/" + CONTENT_AUTHORITY + "/" + FLAVOR;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
package com.sam_chordas.android.androidflavors;
package com.sam_chordas.android.androidflavors.data;

import android.content.Context
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class FlavorsDBHelper extends SQLiteOpenHelper{
public static final String LOg_TAG = FlavorsDBHelper.class.getSimpleName();


public class FlavorsDBHelper extends SQLiteOpenHelper {
public static final String LOG_TAG = FlavorsDBHelper.class.getSimpleName();

//name & version
private static final String DATABASE_NAME = "flavors.db";
private static final int DATABASE_VERSION = 1;

public FlavorsDBHelper(Context context){
public FlavorsDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase){
public void onCreate(SQLiteDatabase sqLiteDatabase) {
final String SQL_CREATE_MOVIE_TABLE = "CREATE TABLE " +
TABLE_FLAVORS + "(" + _ID +
" INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_VERSION_NAME +
+ " TEXT NOT NULL, " + COLUMN_ICON + " BLOB NOT NULL, " +
COLUMN_VERSION_NUMBER + " TEXT NOT NULL);";
FlavorsContract.FlavorEntry.TABLE_FLAVORS + "(" + FlavorsContract.FlavorEntry._ID +
" INTEGER PRIMARY KEY AUTOINCREMENT, " +
FlavorsContract.FlavorEntry.COLUMN_VERSION_NAME
+ " TEXT NOT NULL, " + FlavorsContract.FlavorEntry.COLUMN_ICON +
" BLOB NOT NULL, " +
FlavorsContract.FlavorEntry.COLUMN_VERSION_NUMBER + " TEXT NOT NULL);";

sqLiteDatabase.execSQL(SQL_CREATE_MOVIE_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion){
Log.w(LOG_TAG, "Upgrading database from version " + oldVersion + " to " +
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
Log.w(LOG_TAG, "Upgrading database from version " + oldVersion + " to " +
newVersion + ". OLD DATA WILL BE DESTROYED");

sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_FLAVORS);
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + FlavorsContract.FlavorEntry.TABLE_FLAVORS);

onCreate(sqLiteDatabase);
}
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
package com.sam_chordas.android.androidflavors;
package com.sam_chordas.android.androidflavors.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.SQLiteConstraintException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.util.Log;


public class FlavorsProvider extends ContentProvider{
private static final String LOG_TAG = FlavorsProvider.class.getSimpleName();
private static final UriMatcher sUriMatcher = buildUriMatcher();
private FlavorsDBhelper mOpenHelper;
private FlavorsDBHelper mOpenHelper;

private static final int FLAVOR = 100;
private static final int FLAVOR_WITH_ID = 200;

private static UriMatcher buildUriMatcher(){
final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
final String authority = FlavorContract.CONTENT_AUTHORITY;
final String authority = FlavorsContract.CONTENT_AUTHORITY;

matcher.addURI(authority, FlavorEntry.TABLE_FLAVORS, FLAVOR);
matcher.addURI(authority, FlavorEntry.TABLE_FLAVORS + "/#", FLAVOR_WITH_ID);
matcher.addURI(authority, FlavorsContract.FlavorEntry.TABLE_FLAVORS, FLAVOR);
matcher.addURI(authority, FlavorsContract.FlavorEntry.TABLE_FLAVORS + "/#", FLAVOR_WITH_ID);

return matcher;
}

@Override
public boolean onCreate(){
mOpenHelper = new FlavorDBHelper(getContext());
mOpenHelper = new FlavorsDBHelper(getContext());

return true;
}
Expand All @@ -40,10 +42,10 @@ public String getType(Uri uri){

switch (match){
case FLAVOR:{
return FlavorEntry.CONTENT_DIR_TYPE;
return FlavorsContract.FlavorEntry.CONTENT_DIR_TYPE;
}
case FLAVOR_WITH_ID:{
return FlavorEntry.CONTENT_ITEM_TYPE;
return FlavorsContract.FlavorEntry.CONTENT_ITEM_TYPE;
}
default:{
throw new UnsupportedOperationException("Unknown uri: " + uri);
Expand All @@ -54,9 +56,10 @@ public String getType(Uri uri){
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder){
Cursor retCursor;
switch(sUriMatcher.match(uri){
switch(sUriMatcher.match(uri)){
case FLAVOR:{
retCursor = mOpenHelper.getReadableDatabase().query(FlavorEntry.TABLE_FLAVORS,
retCursor = mOpenHelper.getReadableDatabase().query(
FlavorsContract.FlavorEntry.TABLE_FLAVORS,
projection,
selection,
selectionArgs,
Expand All @@ -66,66 +69,73 @@ public Cursor query(Uri uri, String[] projection, String selection, String[] sel
return retCursor;
}
case FLAVOR_WITH_ID:{
retCursor = mOpenHelper.getReadableDatabase().query(FlavorEntry.TABLE_FLAVORS,
retCursor = mOpenHelper.getReadableDatabase().query(
FlavorsContract.FlavorEntry.TABLE_FLAVORS,
projection,
FlavorEntry._ID + " = ?",
FlavorsContract.FlavorEntry._ID + " = ?",
new String[] {String.valueOf(ContentUris.parseId(uri))},
null,
null,
sortOrder);
return retCursor;
}
default:{
default :{
throw new UnsupportedOperationException("Unknown uri: " + uri);
}
}
}

@Override
public Uri insert(Uri uri, ContentValues values){
final SQLitedatabase db = mOpenHelper.getWritableDatabase();
case FLAVOR:{
long _id = db.insert(FlavorEntry.TABLE_FLAVORS, null, values);
if(_id > 0){
returnUri = FlavorEntry.buildFlavorsUri(_id);
} else{
throw new android.database.SQLException("Failed to insert row into: " + uri);
final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
Uri returnUri;
switch (sUriMatcher.match(uri)) {
case FLAVOR: {
long _id = db.insert(FlavorsContract.FlavorEntry.TABLE_FLAVORS, null, values);
if (_id > 0) {
returnUri = FlavorsContract.FlavorEntry.buildFlavorsUri(_id);
} else {
throw new android.database.SQLException("Failed to insert row into: " + uri);
}
break;
}
break;
}

default: {
throw new UnsupportedOperationException("Unknown uri: " + uri);
default: {
throw new UnsupportedOperationException("Unknown uri: " + uri);

}
}
getContext().getContentResolver().notifyChange(uri, null);
return returnUri;
}

@Override
public Uri delete(Uri uri, String selection, String[] selectionArgs){
final SQLitedatabase db = mOpenHelper.getWritableDatabase();
public int delete(Uri uri, String selection, String[] selectionArgs){
final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
final int match = sUriMatcher.match(uri);
int numDeleted;
switch(match){
case FLAVOR:
numDeleted = db.delete(
FlavorEntry.TABLE_FLAVORS, selection, selectionArgs);
FlavorsContract.FlavorEntry.TABLE_FLAVORS, selection, selectionArgs);
// reset _ID
db.execSQL("DELETE FROM SQLITE_SEQUENCE WHERE NAME = '" +
FlavorEntry.TABLE_FLAVORS + "'");
db.execSQL("DELETE FROM SQLITE_SEQUENCE WHERE NAME = '" +
FlavorsContract.FlavorEntry.TABLE_FLAVORS + "'");
break;
case FLAVOR_WITH_ID:
numDeleted = db.delete(FlavorEntry.TABLE_FLAVORS, FlavorEntry._ID + " = ?",
new String[]{String.valueOf(ContentsUris.parseId(uri))});
numDeleted = db.delete(FlavorsContract.FlavorEntry.TABLE_FLAVORS,
FlavorsContract.FlavorEntry._ID + " = ?",
new String[]{String.valueOf(ContentUris.parseId(uri))});
// reset _ID
db.execSQL("DELETE FROM SQLITE_SEQUENCE WHERE NAME = '" +
FlavorEntry.TABLE_FLAVORS + "'");
FlavorsContract.FlavorEntry.TABLE_FLAVORS + "'");

break;
default:
throw new UnsupportedOperationException("Unknown uri: " + uri);
}

return numDeleted;
}

@Override
Expand All @@ -145,9 +155,12 @@ public int bulkInsert(Uri uri, ContentValues[] values){
}
long _id = -1;
try{
-id = db.insertOrThrow(FlavorEntry.TABLE_FLAVORS, null, value);
_id = db.insertOrThrow(FlavorsContract.FlavorEntry.TABLE_FLAVORS,
null, value);
}catch(SQLiteConstraintException e) {
Log.w(LOG_TAG, "Attempting to insert " + value.getAsString(FlavorEntry.COLUMN_VERSION_NAME)
Log.w(LOG_TAG, "Attempting to insert " +
value.getAsString(
FlavorsContract.FlavorEntry.COLUMN_VERSION_NAME)
+ " but value is already in database.");
}
if (_id != 1){
Expand Down Expand Up @@ -175,18 +188,18 @@ public int update(Uri uri, ContentValues contentValues, String selection, String
throw new IllegalArgumentException("Cannot have null content values");
}

switch(sUrimatcher.match(uri)){
switch(sUriMatcher.match(uri)){
case FLAVOR:{
numUpdated = db.update(FlavorEntry.TABLE_FLAVORS,
numUpdated = db.update(FlavorsContract.FlavorEntry.TABLE_FLAVORS,
contentValues,
selection,
selectionArgs);
break;
}
case FLAVOR_WITH_ID: {
numUpdated = db.update(FlavorEntry.TABLE_FLAVORS,
numUpdated = db.update(FlavorsContract.FlavorEntry.TABLE_FLAVORS,
contentValues,
FlavorEntry._ID + " = ?",
FlavorsContract.FlavorEntry._ID + " = ?",
new String[] {String.valueOf(ContentUris.parseId(uri))});
break;
}
Expand All @@ -196,7 +209,7 @@ public int update(Uri uri, ContentValues contentValues, String selection, String
}

if (numUpdated > 0){
getContext().getContentResolver.notifyChange(uri, null);
getContext().getContentResolver().notifyChange(uri, null);
}

return numUpdated;
Expand Down

0 comments on commit bbff4cc

Please sign in to comment.