-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoiProvider.cs
More file actions
91 lines (79 loc) · 2.89 KB
/
PoiProvider.cs
File metadata and controls
91 lines (79 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using Android.Content;
using Android.Database;
using Android.Database.Sqlite;
using Android.Net;
using Android.Runtime;
using Java.Lang;
namespace SensorLab {
[Register("world.cryville.sensorlab.PoiProvider")]
public class PoiProvider : ContentProvider {
public static readonly Uri Uri = Uri.Parse("content://world.cryville.sensorlab/pois");
static readonly UriMatcher _uriMatcher = new UriMatcher(UriMatcher.NoMatch);
static PoiProvider() {
_uriMatcher.AddURI("world.cryville.sensorlab", "pois", 1);
}
SQLiteDatabase _db;
class OpenHelper : SQLiteOpenHelper {
public OpenHelper(Context context) : base(context, "poi", null, 1) { }
public override void OnCreate(SQLiteDatabase db) {
db.ExecSQL("CREATE TABLE Pois (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, color INTEGER, latitude REAL, longitude REAL)");
}
public override void OnUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }
}
public override bool OnCreate() {
var helper = new OpenHelper(Context);
try {
_db = helper.WritableDatabase;
}
catch (SQLiteException) {
return false;
}
return true;
}
protected override void Dispose(bool disposing) {
base.Dispose(disposing);
if (disposing) _db.Close();
}
public override string GetType(Uri uri) => _uriMatcher.Match(uri) switch {
1 => "vnd.android.cursor.dir/vnd.world.cryville.sensorlab.pois",
_ => throw new IllegalArgumentException("Unknown URI"),
};
public override ICursor Query(Uri uri, string[] projection, string selection, string[] selectionArgs, string sortOrder) {
SQLiteQueryBuilder qb = new SQLiteQueryBuilder {
Tables = "Pois"
};
switch(_uriMatcher.Match(uri)) {
case 1:
var c = qb.Query(_db, projection, selection, selectionArgs, null, null, sortOrder);
c.SetNotificationUri(Context.ContentResolver, uri);
return c;
default: throw new IllegalArgumentException("Unknown URI");
};
}
public override Uri Insert(Uri uri, ContentValues values) {
var rowId = _db.Insert("Pois", null, values);
if (rowId >= 0) {
Uri itemUri = ContentUris.WithAppendedId(Uri, rowId);
Context.ContentResolver.NotifyChange(itemUri, null);
return itemUri;
}
throw new SQLException("Failed to add record");
}
public override int Update(Uri uri, ContentValues values, string selection, string[] selectionArgs) {
int count = _uriMatcher.Match(uri) switch {
1 => _db.Update("Pois", values, selection, selectionArgs),
_ => throw new IllegalArgumentException("Unknown URI"),
};
Context.ContentResolver.NotifyChange(uri, null);
return count;
}
public override int Delete(Uri uri, string selection, string[] selectionArgs) {
int count = _uriMatcher.Match(uri) switch {
1 => _db.Delete("Pois", selection, selectionArgs),
_ => throw new IllegalArgumentException("Unknown URI"),
};
Context.ContentResolver.NotifyChange(uri, null);
return count;
}
}
}