HostageContentProvider.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package de.tudarmstadt.informatik.hostage.provider;
  2. import android.content.ContentProvider;
  3. import android.content.ContentUris;
  4. import android.content.ContentValues;
  5. import android.content.UriMatcher;
  6. import android.database.Cursor;
  7. import android.database.sqlite.SQLiteDatabase;
  8. import android.database.sqlite.SQLiteQueryBuilder;
  9. import android.net.Uri;
  10. import android.text.TextUtils;
  11. import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.AttackEntry;
  12. import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.NetworkEntry;
  13. import de.tudarmstadt.informatik.hostage.persistence.HostageDBContract.PacketEntry;
  14. import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
  15. public class HostageContentProvider extends ContentProvider {
  16. public static final String AUTHORITY = "de.tudarmstadt.informatik.hostage";
  17. public static final Uri CONTENT_URI_NETWORK = Uri.parse("content://" + AUTHORITY + "/network");
  18. public static final Uri CONTENT_URI_ATTACK = Uri.parse("content://" + AUTHORITY + "/attack");
  19. public static final Uri CONTENT_URI_PACKET = Uri.parse("content://" + AUTHORITY + "/packet");
  20. private static final int NETWORK_ALL = 1;
  21. private static final int NETWORK_ONE = 2;
  22. private static final int ATTACK_ALL = 3;
  23. private static final int ATTACK_ONE = 4;
  24. private static final int PACKET_ALL = 5;
  25. private static final int PACKET_ONE = 6;
  26. private static final UriMatcher uriMatcher;
  27. static {
  28. uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
  29. uriMatcher.addURI(AUTHORITY, "network", NETWORK_ALL);
  30. uriMatcher.addURI(AUTHORITY, "network/#", NETWORK_ONE);
  31. uriMatcher.addURI(AUTHORITY, "attack", ATTACK_ALL);
  32. uriMatcher.addURI(AUTHORITY, "attack/#", ATTACK_ONE);
  33. uriMatcher.addURI(AUTHORITY, "packet", PACKET_ALL);
  34. uriMatcher.addURI(AUTHORITY, "packet/#", PACKET_ONE);
  35. }
  36. private HostageDBOpenHelper mDBOpenHelper;
  37. @Override
  38. public boolean onCreate() {
  39. mDBOpenHelper = new HostageDBOpenHelper(getContext());
  40. return true;
  41. }
  42. @Override
  43. public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
  44. SQLiteDatabase db = mDBOpenHelper.getWritableDatabase();
  45. SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
  46. int uriMatch = uriMatcher.match(uri);
  47. if (isNetworkUriMatch(uriMatch)) {
  48. queryBuilder.setTables(NetworkEntry.TABLE_NAME);
  49. } else if (isAttackUriMatch(uriMatch)) {
  50. queryBuilder.setTables(AttackEntry.TABLE_NAME);
  51. } else if (isPacketUriMatch(uriMatch)) {
  52. queryBuilder.setTables(PacketEntry.TABLE_NAME);
  53. }
  54. if (uriMatch == NETWORK_ONE) {
  55. String rowID = uri.getPathSegments().get(1);
  56. queryBuilder.appendWhere(NetworkEntry.KEY_ID + "=" + rowID);
  57. } else if (uriMatch == ATTACK_ONE) {
  58. String rowID = uri.getPathSegments().get(1);
  59. queryBuilder.appendWhere(AttackEntry.KEY_ID + "=" + rowID);
  60. } else if (uriMatch == PACKET_ONE) {
  61. String rowID = uri.getPathSegments().get(1);
  62. queryBuilder.appendWhere(PacketEntry.KEY_ID + "=" + rowID);
  63. }
  64. Cursor cursor = queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
  65. return cursor;
  66. }
  67. @Override
  68. public int delete(Uri uri, String selection, String[] selectionArgs) {
  69. SQLiteDatabase db = mDBOpenHelper.getWritableDatabase();
  70. int uriMatch = uriMatcher.match(uri);
  71. if (uriMatch == NETWORK_ONE) {
  72. String rowID = uri.getPathSegments().get(1);
  73. selection = NetworkEntry.KEY_ID + "=" + rowID + (!TextUtils.isEmpty(selection) ? "AND (" + selection + ")" : "");
  74. } else if (uriMatch == ATTACK_ONE) {
  75. String rowID = uri.getPathSegments().get(1);
  76. selection = AttackEntry.KEY_ID + "=" + rowID + (!TextUtils.isEmpty(selection) ? "AND (" + selection + ")" : "");
  77. } else if (uriMatch == PACKET_ONE) {
  78. String rowID = uri.getPathSegments().get(1);
  79. selection = PacketEntry.KEY_ID + "=" + rowID + (!TextUtils.isEmpty(selection) ? "AND (" + selection + ")" : "");
  80. }
  81. if (selection == null) {
  82. selection = "1";
  83. }
  84. int deleteCount = 0;
  85. if (isNetworkUriMatch(uriMatch)) {
  86. deleteCount = db.delete(NetworkEntry.TABLE_NAME, selection, selectionArgs);
  87. } else if (isAttackUriMatch(uriMatch)) {
  88. deleteCount = db.delete(AttackEntry.TABLE_NAME, selection, selectionArgs);
  89. } else if (isPacketUriMatch(uriMatch)) {
  90. deleteCount = db.delete(PacketEntry.TABLE_NAME, selection, selectionArgs);
  91. }
  92. getContext().getContentResolver().notifyChange(uri, null);
  93. return deleteCount;
  94. }
  95. @Override
  96. public Uri insert(Uri uri, ContentValues values) {
  97. SQLiteDatabase db = mDBOpenHelper.getWritableDatabase();
  98. int uriMatch = uriMatcher.match(uri);
  99. long id = -1;
  100. Uri insertedId = null;
  101. if (isNetworkUriMatch(uriMatch)) {
  102. id = db.insert(NetworkEntry.TABLE_NAME, null, values);
  103. if (id > -1) {
  104. insertedId = ContentUris.withAppendedId(CONTENT_URI_NETWORK, id);
  105. }
  106. } else if (isAttackUriMatch(uriMatch)) {
  107. id = db.insert(AttackEntry.TABLE_NAME, null, values);
  108. if (id > -1) {
  109. insertedId = ContentUris.withAppendedId(CONTENT_URI_NETWORK, id);
  110. }
  111. } else if (isPacketUriMatch(uriMatch)) {
  112. id = db.insert(PacketEntry.TABLE_NAME, null, values);
  113. if (id > -1) {
  114. insertedId = ContentUris.withAppendedId(CONTENT_URI_NETWORK, id);
  115. }
  116. }
  117. if (id > -1) {
  118. getContext().getContentResolver().notifyChange(insertedId, null);
  119. return insertedId;
  120. }
  121. return null;
  122. }
  123. @Override
  124. public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
  125. SQLiteDatabase db = mDBOpenHelper.getWritableDatabase();
  126. int uriMatch = uriMatcher.match(uri);
  127. if (uriMatch == NETWORK_ONE) {
  128. String rowID = uri.getPathSegments().get(1);
  129. selection = NetworkEntry.KEY_ID + "=" + rowID + (!TextUtils.isEmpty(selection) ? "AND (" + selection + ")" : "");
  130. } else if (uriMatch == ATTACK_ONE) {
  131. String rowID = uri.getPathSegments().get(1);
  132. selection = AttackEntry.KEY_ID + "=" + rowID + (!TextUtils.isEmpty(selection) ? "AND (" + selection + ")" : "");
  133. } else if (uriMatch == PACKET_ONE) {
  134. String rowID = uri.getPathSegments().get(1);
  135. selection = PacketEntry.KEY_ID + "=" + rowID + (!TextUtils.isEmpty(selection) ? "AND (" + selection + ")" : "");
  136. }
  137. int updateCount = 0;
  138. if (isNetworkUriMatch(uriMatch)) {
  139. updateCount = db.update(NetworkEntry.TABLE_NAME, values, selection, selectionArgs);
  140. } else if (isAttackUriMatch(uriMatch)) {
  141. updateCount = db.update(AttackEntry.TABLE_NAME, values, selection, selectionArgs);
  142. } else if (isPacketUriMatch(uriMatch)) {
  143. updateCount = db.update(PacketEntry.TABLE_NAME, values, selection, selectionArgs);
  144. }
  145. getContext().getContentResolver().notifyChange(uri, null);
  146. return updateCount;
  147. }
  148. @Override
  149. public String getType(Uri uri) {
  150. int uriMatch = uriMatcher.match(uri);
  151. if (isNetworkUriMatch(uriMatch)) {
  152. if (uriMatch == NETWORK_ONE) {
  153. return "vnd.android.cursor.item/vnd.de.tudarmstadt.informatik.hostage.network";
  154. }
  155. return "vnd.android.cursor.dir/vnd.de.tudarmstadt.informatik.hostage.network";
  156. } else if (isAttackUriMatch(uriMatch)) {
  157. if (uriMatch == ATTACK_ONE) {
  158. return "vnd.android.cursor.item/vnd.de.tudarmstadt.informatik.hostage.attack";
  159. }
  160. return "vnd.android.cursor.dir/vnd.de.tudarmstadt.informatik.hostage.attack";
  161. } else if (isPacketUriMatch(uriMatch)) {
  162. if (uriMatch == PACKET_ONE) {
  163. return "vnd.android.cursor.item/vnd.de.tudarmstadt.informatik.hostage.packet";
  164. }
  165. return "vnd.android.cursor.dir/vnd.de.tudarmstadt.informatik.hostage.packet";
  166. }
  167. return null;
  168. }
  169. private boolean isNetworkUriMatch(int uriMatch) {
  170. if (uriMatch == NETWORK_ALL || uriMatch == NETWORK_ONE) {
  171. return true;
  172. }
  173. return false;
  174. }
  175. private boolean isAttackUriMatch(int uriMatch) {
  176. if (uriMatch == ATTACK_ALL || uriMatch == ATTACK_ONE) {
  177. return true;
  178. }
  179. return false;
  180. }
  181. private boolean isPacketUriMatch(int uriMatch) {
  182. if (uriMatch == PACKET_ALL || uriMatch == PACKET_ONE) {
  183. return true;
  184. }
  185. return false;
  186. }
  187. }