HostageContentProvider.java 8.2 KB

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