123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- package de.tudarmstadt.informatik.hostage.provider;
- import android.content.ContentProvider;
- import android.content.ContentUris;
- import android.content.ContentValues;
- import android.content.UriMatcher;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteQueryBuilder;
- import android.net.Uri;
- import android.text.TextUtils;
- import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.AttackEntry;
- import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.NetworkEntry;
- import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.PacketEntry;
- import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
- public class HostageContentProvider extends ContentProvider {
- public static final String AUTHORITY = "de.tudarmstadt.informatik.hostage";
- public static final Uri CONTENT_URI_NETWORK = Uri.parse("content://" + AUTHORITY + "/network");
- public static final Uri CONTENT_URI_ATTACK = Uri.parse("content://" + AUTHORITY + "/attack");
- public static final Uri CONTENT_URI_PACKET = Uri.parse("content://" + AUTHORITY + "/packet");
- private static final int NETWORK_ALL = 1;
- private static final int NETWORK_ONE = 2;
- private static final int ATTACK_ALL = 3;
- private static final int ATTACK_ONE = 4;
- private static final int PACKET_ALL = 5;
- private static final int PACKET_ONE = 6;
- private static final UriMatcher uriMatcher;
- static {
- uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
- uriMatcher.addURI(AUTHORITY, "network", NETWORK_ALL);
- uriMatcher.addURI(AUTHORITY, "network/#", NETWORK_ONE);
- uriMatcher.addURI(AUTHORITY, "attack", ATTACK_ALL);
- uriMatcher.addURI(AUTHORITY, "attack/#", ATTACK_ONE);
- uriMatcher.addURI(AUTHORITY, "packet", PACKET_ALL);
- uriMatcher.addURI(AUTHORITY, "packet/#", PACKET_ONE);
- }
- private HostageDBOpenHelper mDBOpenHelper;
- @Override
- public boolean onCreate() {
- mDBOpenHelper = new HostageDBOpenHelper(getContext());
- return true;
- }
- @Override
- public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
- SQLiteDatabase db = mDBOpenHelper.getWritableDatabase();
- SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
- int uriMatch = uriMatcher.match(uri);
- if (isNetworkUriMatch(uriMatch)) {
- queryBuilder.setTables(NetworkEntry.TABLE_NAME);
- } else if (isAttackUriMatch(uriMatch)) {
- queryBuilder.setTables(AttackEntry.TABLE_NAME);
- } else if (isPacketUriMatch(uriMatch)) {
- queryBuilder.setTables(PacketEntry.TABLE_NAME);
- }
- if (uriMatch == NETWORK_ONE) {
- String rowID = uri.getPathSegments().get(1);
- queryBuilder.appendWhere(NetworkEntry.KEY_ID + "=" + rowID);
- } else if (uriMatch == ATTACK_ONE) {
- String rowID = uri.getPathSegments().get(1);
- queryBuilder.appendWhere(AttackEntry.KEY_ID + "=" + rowID);
- } else if (uriMatch == PACKET_ONE) {
- String rowID = uri.getPathSegments().get(1);
- queryBuilder.appendWhere(PacketEntry.KEY_ID + "=" + rowID);
- }
- Cursor cursor = queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
- return cursor;
- }
- @Override
- public int delete(Uri uri, String selection, String[] selectionArgs) {
- SQLiteDatabase db = mDBOpenHelper.getWritableDatabase();
- int uriMatch = uriMatcher.match(uri);
- if (uriMatch == NETWORK_ONE) {
- String rowID = uri.getPathSegments().get(1);
- selection = NetworkEntry.KEY_ID + "=" + rowID + (!TextUtils.isEmpty(selection) ? "AND (" + selection + ")" : "");
- } else if (uriMatch == ATTACK_ONE) {
- String rowID = uri.getPathSegments().get(1);
- selection = AttackEntry.KEY_ID + "=" + rowID + (!TextUtils.isEmpty(selection) ? "AND (" + selection + ")" : "");
- } else if (uriMatch == PACKET_ONE) {
- String rowID = uri.getPathSegments().get(1);
- selection = PacketEntry.KEY_ID + "=" + rowID + (!TextUtils.isEmpty(selection) ? "AND (" + selection + ")" : "");
- }
- if (selection == null) {
- selection = "1";
- }
- int deleteCount = 0;
- if (isNetworkUriMatch(uriMatch)) {
- deleteCount = db.delete(NetworkEntry.TABLE_NAME, selection, selectionArgs);
- } else if (isAttackUriMatch(uriMatch)) {
- deleteCount = db.delete(AttackEntry.TABLE_NAME, selection, selectionArgs);
- } else if (isPacketUriMatch(uriMatch)) {
- deleteCount = db.delete(PacketEntry.TABLE_NAME, selection, selectionArgs);
- }
- getContext().getContentResolver().notifyChange(uri, null);
- return deleteCount;
- }
- @Override
- public Uri insert(Uri uri, ContentValues values) {
- SQLiteDatabase db = mDBOpenHelper.getWritableDatabase();
- int uriMatch = uriMatcher.match(uri);
- long id = -1;
- Uri insertedId = null;
- if (isNetworkUriMatch(uriMatch)) {
- id = db.insert(NetworkEntry.TABLE_NAME, null, values);
- if (id > -1) {
- insertedId = ContentUris.withAppendedId(CONTENT_URI_NETWORK, id);
- }
- } else if (isAttackUriMatch(uriMatch)) {
- id = db.insert(AttackEntry.TABLE_NAME, null, values);
- if (id > -1) {
- insertedId = ContentUris.withAppendedId(CONTENT_URI_NETWORK, id);
- }
- } else if (isPacketUriMatch(uriMatch)) {
- id = db.insert(PacketEntry.TABLE_NAME, null, values);
- if (id > -1) {
- insertedId = ContentUris.withAppendedId(CONTENT_URI_NETWORK, id);
- }
- }
- if (id > -1) {
- getContext().getContentResolver().notifyChange(insertedId, null);
- return insertedId;
- }
- return null;
- }
- @Override
- public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
- SQLiteDatabase db = mDBOpenHelper.getWritableDatabase();
- int uriMatch = uriMatcher.match(uri);
- if (uriMatch == NETWORK_ONE) {
- String rowID = uri.getPathSegments().get(1);
- selection = NetworkEntry.KEY_ID + "=" + rowID + (!TextUtils.isEmpty(selection) ? "AND (" + selection + ")" : "");
- } else if (uriMatch == ATTACK_ONE) {
- String rowID = uri.getPathSegments().get(1);
- selection = AttackEntry.KEY_ID + "=" + rowID + (!TextUtils.isEmpty(selection) ? "AND (" + selection + ")" : "");
- } else if (uriMatch == PACKET_ONE) {
- String rowID = uri.getPathSegments().get(1);
- selection = PacketEntry.KEY_ID + "=" + rowID + (!TextUtils.isEmpty(selection) ? "AND (" + selection + ")" : "");
- }
- int updateCount = 0;
- if (isNetworkUriMatch(uriMatch)) {
- updateCount = db.update(NetworkEntry.TABLE_NAME, values, selection, selectionArgs);
- } else if (isAttackUriMatch(uriMatch)) {
- updateCount = db.update(AttackEntry.TABLE_NAME, values, selection, selectionArgs);
- } else if (isPacketUriMatch(uriMatch)) {
- updateCount = db.update(PacketEntry.TABLE_NAME, values, selection, selectionArgs);
- }
- getContext().getContentResolver().notifyChange(uri, null);
- return updateCount;
- }
- @Override
- public String getType(Uri uri) {
- int uriMatch = uriMatcher.match(uri);
- if (isNetworkUriMatch(uriMatch)) {
- if (uriMatch == NETWORK_ONE) {
- return "vnd.android.cursor.item/vnd.de.tudarmstadt.informatik.hostage.network";
- }
- return "vnd.android.cursor.dir/vnd.de.tudarmstadt.informatik.hostage.network";
- } else if (isAttackUriMatch(uriMatch)) {
- if (uriMatch == ATTACK_ONE) {
- return "vnd.android.cursor.item/vnd.de.tudarmstadt.informatik.hostage.attack";
- }
- return "vnd.android.cursor.dir/vnd.de.tudarmstadt.informatik.hostage.attack";
- } else if (isPacketUriMatch(uriMatch)) {
- if (uriMatch == PACKET_ONE) {
- return "vnd.android.cursor.item/vnd.de.tudarmstadt.informatik.hostage.packet";
- }
- return "vnd.android.cursor.dir/vnd.de.tudarmstadt.informatik.hostage.packet";
- }
- return null;
- }
- private boolean isNetworkUriMatch(int uriMatch) {
- if (uriMatch == NETWORK_ALL || uriMatch == NETWORK_ONE) {
- return true;
- }
- return false;
- }
- private boolean isAttackUriMatch(int uriMatch) {
- if (uriMatch == ATTACK_ALL || uriMatch == ATTACK_ONE) {
- return true;
- }
- return false;
- }
- private boolean isPacketUriMatch(int uriMatch) {
- if (uriMatch == PACKET_ALL || uriMatch == PACKET_ONE) {
- return true;
- }
- return false;
- }
- }
|