· 6 years ago · May 03, 2019, 01:20 AM
1package com.example.MyApplication;
2
3import java.util.HashMap;
4
5import android.content.ContentProvider;
6import android.content.ContentUris;
7import android.content.ContentValues;
8import android.content.Context;
9import android.content.UriMatcher;
10
11import android.database.Cursor;
12import android.database.SQLException;
13
14import android.database.sqlite.SQLiteDatabase;
15import android.database.sqlite.SQLiteOpenHelper;
16import android.database.sqlite.SQLiteQueryBuilder;
17
18import android.net.Uri;
19import android.text.TextUtils;
20
21public class StudentsProvider extends ContentProvider {
22 static final String PROVIDER_NAME = "com.example.MyApplication.StudentsProvider";
23 static final String URL = "content://" + PROVIDER_NAME + "/students";
24 static final Uri CONTENT_URI = Uri.parse(URL);
25
26 static final String _ID = "_id";
27 static final String NAME = "name";
28 static final String GRADE = "grade";
29
30 private static HashMap<String, String> STUDENTS_PROJECTION_MAP;
31
32 static final int STUDENTS = 1;
33 static final int STUDENT_ID = 2;
34
35 static final UriMatcher uriMatcher;
36 static{
37 uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
38 uriMatcher.addURI(PROVIDER_NAME, "students", STUDENTS);
39 uriMatcher.addURI(PROVIDER_NAME, "students/#", STUDENT_ID);
40 }
41
42 /**
43 * Database specific constant declarations
44 */
45
46 private SQLiteDatabase db;
47 static final String DATABASE_NAME = "College";
48 static final String STUDENTS_TABLE_NAME = "students";
49 static final int DATABASE_VERSION = 1;
50 static final String CREATE_DB_TABLE =
51 " CREATE TABLE " + STUDENTS_TABLE_NAME +
52 " (_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
53 " name TEXT NOT NULL, " +
54 " grade TEXT NOT NULL);";
55
56 /**
57 * Helper class that actually creates and manages
58 * the provider's underlying data repository.
59 */
60
61 private static class DatabaseHelper extends SQLiteOpenHelper {
62 DatabaseHelper(Context context){
63 super(context, DATABASE_NAME, null, DATABASE_VERSION);
64 }
65
66 @Override
67 public void onCreate(SQLiteDatabase db) {
68 db.execSQL(CREATE_DB_TABLE);
69 }
70
71 @Override
72 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
73 db.execSQL("DROP TABLE IF EXISTS " + STUDENTS_TABLE_NAME);
74 onCreate(db);
75 }
76 }
77
78 @Override
79 public boolean onCreate() {
80 Context context = getContext();
81 DatabaseHelper dbHelper = new DatabaseHelper(context);
82
83 /**
84 * Create a write able database which will trigger its
85 * creation if it doesn't already exist.
86 */
87
88 db = dbHelper.getWritableDatabase();
89 return (db == null)? false:true;
90 }
91
92 @Override
93 public Uri insert(Uri uri, ContentValues values) {
94 /**
95 * Add a new student record
96 */
97 long rowID = db.insert( STUDENTS_TABLE_NAME, "", values);
98
99 /**
100 * If record is added successfully
101 */
102 if (rowID > 0) {
103 Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
104 getContext().getContentResolver().notifyChange(_uri, null);
105 return _uri;
106 }
107
108 throw new SQLException("Failed to add a record into " + uri);
109 }
110
111 @Override
112 public Cursor query(Uri uri, String[] projection,
113 String selection,String[] selectionArgs, String sortOrder) {
114 SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
115 qb.setTables(STUDENTS_TABLE_NAME);
116
117 switch (uriMatcher.match(uri)) {
118 case STUDENTS:
119 qb.setProjectionMap(STUDENTS_PROJECTION_MAP);
120 break;
121
122 case STUDENT_ID:
123 qb.appendWhere( _ID + "=" + uri.getPathSegments().get(1));
124 break;
125
126 default:
127 }
128
129 if (sortOrder == null || sortOrder == ""){
130 /**
131 * By default sort on student names
132 */
133 sortOrder = NAME;
134 }
135
136 Cursor c = qb.query(db, projection, selection,
137 selectionArgs,null, null, sortOrder);
138 /**
139 * register to watch a content URI for changes
140 */
141 c.setNotificationUri(getContext().getContentResolver(), uri);
142 return c;
143 }
144
145 @Override
146 public int delete(Uri uri, String selection, String[] selectionArgs) {
147 int count = 0;
148 switch (uriMatcher.match(uri)){
149 case STUDENTS:
150 count = db.delete(STUDENTS_TABLE_NAME, selection, selectionArgs);
151 break;
152
153 case STUDENT_ID:
154 String id = uri.getPathSegments().get(1);
155 count = db.delete( STUDENTS_TABLE_NAME, _ID + " = " + id +
156 (!TextUtils.isEmpty(selection) ? "
157 AND (" + selection + ')' : ""), selectionArgs);
158 break;
159 default:
160 throw new IllegalArgumentException("Unknown URI " + uri);
161 }
162
163 getContext().getContentResolver().notifyChange(uri, null);
164 return count;
165 }
166
167 @Override
168 public int update(Uri uri, ContentValues values,
169 String selection, String[] selectionArgs) {
170 int count = 0;
171 switch (uriMatcher.match(uri)) {
172 case STUDENTS:
173 count = db.update(STUDENTS_TABLE_NAME, values, selection, selectionArgs);
174 break;
175
176 case STUDENT_ID:
177 count = db.update(STUDENTS_TABLE_NAME, values,
178 _ID + " = " + uri.getPathSegments().get(1) +
179 (!TextUtils.isEmpty(selection) ? "
180 AND (" +selection + ')' : ""), selectionArgs);
181 break;
182 default:
183 throw new IllegalArgumentException("Unknown URI " + uri );
184 }
185
186 getContext().getContentResolver().notifyChange(uri, null);
187 return count;
188 }
189
190 @Override
191 public String getType(Uri uri) {
192 switch (uriMatcher.match(uri)){
193 /**
194 * Get all student records
195 */
196 case STUDENTS:
197 return "vnd.android.cursor.dir/vnd.example.students";
198 /**
199 * Get a particular student
200 */
201 case STUDENT_ID:
202 return "vnd.android.cursor.item/vnd.example.students";
203 default:
204 throw new IllegalArgumentException("Unsupported URI: " + uri);
205 }
206 }
207}