-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGroupMessengerProvider.java
110 lines (98 loc) · 4.04 KB
/
GroupMessengerProvider.java
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package edu.buffalo.cse.cse486586.groupmessenger2;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;
import static edu.buffalo.cse.cse486586.groupmessenger2.GroupMessengerActivity.TAG;
/**
* GroupMessengerProvider is a key-value table. Once again, please note that we do not implement
* full support for SQL as a usual ContentProvider does. We re-purpose ContentProvider's interface
* to use it as a key-value table.
*
* Please read:
*
* http://developer.android.com/guide/topics/providers/content-providers.html
* http://developer.android.com/reference/android/content/ContentProvider.html
*
* before you start to get yourself familiarized with ContentProvider.
*
* There are two methods you need to implement---insert() and query(). Others are optional and
* will not be tested.
*
* @author Shad
*
*/
public class GroupMessengerProvider extends ContentProvider {
private DatabaseHelper mDb;
private static final String DATABASE_NAME = "dictionary";
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// You do not need to implement this.
return 0;
}
@Override
public String getType(Uri uri) {
// You do not need to implement this.
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
/*
* TODO: You need to implement this method. Note that values will have two columns (a key
* column and a value column) and one row that contains the actual (key, value) pair to be
* inserted.
*
* For actual storage, you can use any option. If you know how to use SQL, then you can use
* SQLite. But this is not a requirement. You can use other storage options, such as the
* internal storage option that we used in PA1. If you want to use that option, please
* take a look at the code for PA1.
*/
SQLiteDatabase mydatabase = mDb.getWritableDatabase();
long id = mydatabase.insert(DATABASE_NAME, null, values);
if (id == -1){
Log.e(TAG, String.valueOf(id));
Log.e(TAG,"Failed to insert key");
Log.e(TAG,"Failed to insert key");
return null;
}
Log.v("insert", values.toString());
return uri;
}
@Override
public boolean onCreate() {
// If you need to perform any one-time initialization task, please do it here.
final boolean b;
if (getContext().deleteDatabase(DATABASE_NAME)) b = true; //code taken from stack overflow
else b = false;
mDb = new DatabaseHelper(getContext());
Log.v(TAG,"Created database");
return false;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
// You do not need to implement this.
return 0;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
String sortOrder) {
/*
* TODO: You need to implement this method. Note that you need to return a Cursor object
* with the right format. If the formatting is not correct, then it is not going to work.
*
* If you use SQLite, whatever is returned from SQLite is a Cursor object. However, you
* still need to be careful because the formatting might still be incorrect.
*
* If you use a file storage option, then it is your job to build a Cursor * object. I
* recommend building a MatrixCursor described at:
* http://developer.android.com/reference/android/database/MatrixCursor.html
*/
SQLiteDatabase mydatabase = mDb.getReadableDatabase();
String [] sArgs ={selection};
Cursor cursor = mydatabase.query(DATABASE_NAME,null,"key = ?",sArgs,null,null,null);
Log.v("query", selection);
return cursor;
}
}