Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
InstiApp
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
RAHUL SHARMA
InstiApp
Commits
22ff3399
Commit
22ff3399
authored
Jun 22, 2017
by
Sajal Narang
Committed by
GitHub
Jun 22, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #20 from yvsriram/master
Created a content provider and a map locations database.
parents
b8dc6615
b4a6432f
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
269 additions
and
0 deletions
+269
-0
app/src/main/AndroidManifest.xml
app/src/main/AndroidManifest.xml
+5
-0
app/src/main/java/in/ac/iitb/gymkhana/iitbapp/data/DatabaseContract.java
...va/in/ac/iitb/gymkhana/iitbapp/data/DatabaseContract.java
+23
-0
app/src/main/java/in/ac/iitb/gymkhana/iitbapp/data/IITBAppContentProvider.java
...ac/iitb/gymkhana/iitbapp/data/IITBAppContentProvider.java
+208
-0
app/src/main/java/in/ac/iitb/gymkhana/iitbapp/data/MapDbHelper.java
...in/java/in/ac/iitb/gymkhana/iitbapp/data/MapDbHelper.java
+33
-0
No files found.
app/src/main/AndroidManifest.xml
View file @
22ff3399
...
@@ -55,6 +55,11 @@
...
@@ -55,6 +55,11 @@
android:scheme=
"https"
/>
android:scheme=
"https"
/>
</intent-filter>
</intent-filter>
</activity>
</activity>
<provider
android:name=
"in.ac.iitb.gymkhana.iitbapp.data.IITBAppContentProvider"
android:authorities=
"in.ac.iitb.gymkhana.iitbapp"
android:exported=
"false"
/>
</application>
</application>
</manifest>
</manifest>
\ No newline at end of file
app/src/main/java/in/ac/iitb/gymkhana/iitbapp/data/DatabaseContract.java
0 → 100644
View file @
22ff3399
package
in.ac.iitb.gymkhana.iitbapp.data
;
import
android.net.Uri
;
import
android.provider.BaseColumns
;
public
class
DatabaseContract
{
public
static
final
String
CONTENT_AUTHORITY
=
"in.ac.iitb.gymkhana.iitbapp"
;
public
static
final
Uri
BASE_CONTENT_URI
=
Uri
.
parse
(
"content://"
+
CONTENT_AUTHORITY
);
public
static
final
String
PATH_MAP
=
"map"
;
public
static
final
class
MapEntry
implements
BaseColumns
{
public
static
final
Uri
CONTENT_URI
=
BASE_CONTENT_URI
.
buildUpon
()
.
appendPath
(
PATH_MAP
)
.
build
();
public
static
final
String
TABLE_NAME
=
"map"
;
public
static
final
String
COLUMN_LATITUDE
=
"latitude"
;
public
static
final
String
COLUMN_LONGITUDE
=
"longitude"
;
public
static
final
String
COLUMN_NAME
=
"name"
;
public
static
final
String
COLUMN_TYPE
=
"type"
;
}
}
app/src/main/java/in/ac/iitb/gymkhana/iitbapp/data/IITBAppContentProvider.java
0 → 100644
View file @
22ff3399
package
in.ac.iitb.gymkhana.iitbapp.data
;
import
android.content.ContentProvider
;
import
android.content.ContentUris
;
import
android.content.ContentValues
;
import
android.content.Context
;
import
android.content.UriMatcher
;
import
android.database.Cursor
;
import
android.database.SQLException
;
import
android.database.sqlite.SQLiteDatabase
;
import
android.net.Uri
;
import
android.support.annotation.NonNull
;
import
android.support.annotation.Nullable
;
import
static
in
.
ac
.
iitb
.
gymkhana
.
iitbapp
.
data
.
DatabaseContract
.
MapEntry
.
TABLE_NAME
;
public
class
IITBAppContentProvider
extends
ContentProvider
{
public
static
final
int
LOCS
=
100
;
public
static
final
int
LOC_WITH_ID
=
101
;
private
static
final
UriMatcher
sUriMatcher
=
buildUriMatcher
();
private
MapDbHelper
mapDbHelper
;
public
static
UriMatcher
buildUriMatcher
()
{
final
UriMatcher
matcher
=
new
UriMatcher
(
UriMatcher
.
NO_MATCH
);
matcher
.
addURI
(
DatabaseContract
.
CONTENT_AUTHORITY
,
DatabaseContract
.
PATH_MAP
,
LOCS
);
matcher
.
addURI
(
DatabaseContract
.
CONTENT_AUTHORITY
,
DatabaseContract
.
PATH_MAP
+
"/#"
,
LOC_WITH_ID
);
return
matcher
;
}
@Override
public
boolean
onCreate
()
{
Context
context
=
getContext
();
mapDbHelper
=
new
MapDbHelper
(
context
);
return
true
;
}
@Nullable
@Override
public
Cursor
query
(
@NonNull
Uri
uri
,
@Nullable
String
[]
projection
,
@Nullable
String
selection
,
@Nullable
String
[]
selectionArgs
,
@Nullable
String
sortOrder
)
{
final
SQLiteDatabase
db
=
mapDbHelper
.
getReadableDatabase
();
int
match
=
sUriMatcher
.
match
(
uri
);
Cursor
cursor
;
switch
(
match
)
{
case
LOCS:
cursor
=
db
.
query
(
TABLE_NAME
,
projection
,
selection
,
selectionArgs
,
null
,
null
,
sortOrder
);
break
;
case
LOC_WITH_ID:
String
id
=
uri
.
getPathSegments
().
get
(
1
);
String
[]
selectionArguments
=
new
String
[]{
id
};
cursor
=
db
.
query
(
TABLE_NAME
,
projection
,
"_id=?"
,
selectionArguments
,
null
,
null
,
sortOrder
);
break
;
default
:
throw
new
SQLException
(
"Wrong Uri: "
+
uri
);
}
cursor
.
setNotificationUri
(
getContext
().
getContentResolver
(),
uri
);
return
cursor
;
}
@Nullable
@Override
public
String
getType
(
@NonNull
Uri
uri
)
{
int
match
=
sUriMatcher
.
match
(
uri
);
switch
(
match
)
{
case
LOCS:
return
"vnd.android.cursor.dir"
+
"/"
+
DatabaseContract
.
CONTENT_AUTHORITY
+
"/"
+
DatabaseContract
.
PATH_MAP
;
case
LOC_WITH_ID:
return
"vnd.android.cursor.item"
+
"/"
+
DatabaseContract
.
CONTENT_AUTHORITY
+
"/"
+
DatabaseContract
.
PATH_MAP
;
default
:
throw
new
UnsupportedOperationException
(
"Unknown uri: "
+
uri
);
}
}
@Nullable
@Override
public
Uri
insert
(
@NonNull
Uri
uri
,
@Nullable
ContentValues
values
)
{
final
SQLiteDatabase
db
=
mapDbHelper
.
getWritableDatabase
();
int
match
=
sUriMatcher
.
match
(
uri
);
Uri
returnUri
;
switch
(
match
)
{
case
LOCS:
long
id
=
db
.
insert
(
TABLE_NAME
,
null
,
values
);
if
(
id
>
0
)
{
returnUri
=
ContentUris
.
withAppendedId
(
DatabaseContract
.
MapEntry
.
CONTENT_URI
,
id
);
}
else
throw
new
SQLException
(
"Failed to insert row into "
+
uri
);
break
;
default
:
throw
new
SQLException
(
"Wrong uri: "
+
uri
);
}
getContext
().
getContentResolver
().
notifyChange
(
uri
,
null
);
return
returnUri
;
}
@Override
public
int
bulkInsert
(
@NonNull
Uri
uri
,
@NonNull
ContentValues
[]
values
)
{
final
SQLiteDatabase
db
=
mapDbHelper
.
getWritableDatabase
();
switch
(
sUriMatcher
.
match
(
uri
))
{
case
LOCS:
db
.
beginTransaction
();
int
rowsInserted
=
0
;
try
{
for
(
ContentValues
value
:
values
)
{
long
_id
=
db
.
insert
(
TABLE_NAME
,
null
,
value
);
if
(
_id
!=
-
1
)
{
rowsInserted
++;
}
}
db
.
setTransactionSuccessful
();
}
finally
{
db
.
endTransaction
();
}
if
(
rowsInserted
>
0
)
{
getContext
().
getContentResolver
().
notifyChange
(
uri
,
null
);
}
return
rowsInserted
;
default
:
return
super
.
bulkInsert
(
uri
,
values
);
}
}
@Override
public
int
delete
(
@NonNull
Uri
uri
,
@Nullable
String
selection
,
@Nullable
String
[]
selectionArgs
)
{
int
numRowsDeleted
;
if
(
null
==
selection
)
selection
=
"1"
;
switch
(
sUriMatcher
.
match
(
uri
))
{
case
LOCS:
numRowsDeleted
=
mapDbHelper
.
getWritableDatabase
().
delete
(
TABLE_NAME
,
selection
,
selectionArgs
);
break
;
case
LOC_WITH_ID:
String
id
=
uri
.
getPathSegments
().
get
(
1
);
numRowsDeleted
=
mapDbHelper
.
getWritableDatabase
().
delete
(
TABLE_NAME
,
"_id=?"
,
new
String
[]{
id
});
break
;
default
:
throw
new
UnsupportedOperationException
(
"Unknown uri: "
+
uri
);
}
if
(
numRowsDeleted
!=
0
)
{
getContext
().
getContentResolver
().
notifyChange
(
uri
,
null
);
}
return
numRowsDeleted
;
}
@Override
public
int
update
(
@NonNull
Uri
uri
,
@Nullable
ContentValues
values
,
@Nullable
String
selection
,
@Nullable
String
[]
selectionArgs
)
{
int
itemsUpdated
;
int
match
=
sUriMatcher
.
match
(
uri
);
switch
(
match
)
{
case
LOC_WITH_ID:
String
id
=
uri
.
getPathSegments
().
get
(
1
);
itemsUpdated
=
mapDbHelper
.
getWritableDatabase
().
update
(
TABLE_NAME
,
values
,
"_id=?"
,
new
String
[]{
id
});
break
;
default
:
throw
new
UnsupportedOperationException
(
"Unknown uri: "
+
uri
);
}
if
(
itemsUpdated
!=
0
)
{
getContext
().
getContentResolver
().
notifyChange
(
uri
,
null
);
}
return
itemsUpdated
;
}
}
app/src/main/java/in/ac/iitb/gymkhana/iitbapp/data/MapDbHelper.java
0 → 100644
View file @
22ff3399
package
in.ac.iitb.gymkhana.iitbapp.data
;
import
android.content.Context
;
import
android.database.sqlite.SQLiteDatabase
;
import
android.database.sqlite.SQLiteOpenHelper
;
public
class
MapDbHelper
extends
SQLiteOpenHelper
{
private
static
final
String
DATABASE_NAME
=
"mapDb.db"
;
private
static
final
int
VERSION
=
1
;
MapDbHelper
(
Context
context
)
{
super
(
context
,
DATABASE_NAME
,
null
,
VERSION
);
}
@Override
public
void
onCreate
(
SQLiteDatabase
db
)
{
final
String
CREATE_TABLE
=
"CREATE TABLE "
+
DatabaseContract
.
MapEntry
.
TABLE_NAME
+
" ("
+
DatabaseContract
.
MapEntry
.
_ID
+
" INTEGER PRIMARY KEY, "
+
DatabaseContract
.
MapEntry
.
COLUMN_LATITUDE
+
" DOUBLE NOT NULL, "
+
DatabaseContract
.
MapEntry
.
COLUMN_LONGITUDE
+
" DOUBLE NOT NULL, "
+
DatabaseContract
.
MapEntry
.
COLUMN_NAME
+
" TEXT NOT NULL, "
+
DatabaseContract
.
MapEntry
.
COLUMN_TYPE
+
" TEXT NOT NULL);"
;
db
.
execSQL
(
CREATE_TABLE
);
}
@Override
public
void
onUpgrade
(
SQLiteDatabase
db
,
int
oldVersion
,
int
newVersion
)
{
db
.
execSQL
(
"DROP TABLE IF EXISTS "
+
DatabaseContract
.
MapEntry
.
TABLE_NAME
);
onCreate
(
db
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment