Guest User

Provider

a guest
May 24th, 2021
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.64 KB | None | 0 0
  1. import android.content.ContentProvider;
  2. import android.content.ContentUris;
  3. import android.content.ContentValues;
  4. import android.content.UriMatcher;
  5. import android.database.Cursor;
  6. import android.database.sqlite.SQLiteDatabase;
  7. import android.net.Uri;
  8.  
  9.  
  10. public class Provider extends ContentProvider {
  11.  
  12.     public static final int COINS = 100;
  13.     public static final int COINS_ID = 101;
  14.     public static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
  15.     static {
  16.         sUriMatcher.addURI(Moneta.CONTENT_AUTHORITY, Moneta.PATH_COINS, COINS);
  17.         sUriMatcher.addURI(Moneta.CONTENT_AUTHORITY, Moneta.PATH_COINS + "/#", COINS_ID);
  18.  
  19.     }
  20.  
  21.     public Dbsqlite mDbsqlite;
  22.  
  23.  
  24.     @Override
  25.     public boolean onCreate() {
  26.         mDbsqlite = new Dbsqlite(getContext());
  27.         return false;
  28.     }
  29.  
  30.     @Override
  31.     public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
  32.         SQLiteDatabase database = mDbsqlite.getReadableDatabase();
  33.  
  34.         Cursor cursor;
  35.  
  36.         int match = sUriMatcher.match(uri);
  37.         switch (match) {
  38.             case COINS:
  39.                 cursor = database.query(Moneta.CoinEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
  40.                 break;
  41.  
  42.             case COINS_ID:
  43.  
  44.                 selection = Moneta.CoinEntry._ID + "=?";
  45.                 selectionArgs = new String[]{String.valueOf(ContentUris.parseId(uri))};
  46.                 cursor = database.query(Moneta.CoinEntry.TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
  47.                 break;
  48.  
  49.             default:
  50.                 throw new IllegalArgumentException("Выберите" + uri);
  51.  
  52.         }
  53.  
  54.         cursor.setNotificationUri(getContext().getContentResolver(), uri);
  55.         return cursor;
  56.     }
  57.  
  58.     @Override
  59.     public String getType(Uri uri) {
  60.         return null;
  61.     }
  62.  
  63.     @Override
  64.     public Uri insert(Uri uri, ContentValues values) {
  65.  
  66.         int math = sUriMatcher.match(uri);
  67.         switch (math) {
  68.             case COINS:;
  69.                 return insertCoin(uri, values);
  70.  
  71.             default:
  72.                 throw new IllegalArgumentException("Невозможно вставить" + uri);
  73.         }
  74.     }
  75.  
  76.     private Uri insertCoin(Uri uri, ContentValues values) {
  77.         String type = values.getAsString(Moneta.CoinEntry.COLUMN_NOMINAL);
  78.         if (type == null || !Moneta.CoinEntry.isValidType(type)) {
  79.             throw new IllegalArgumentException("Выберите значение");
  80.         }
  81.  
  82.         String type2 = values.getAsString(Moneta.CoinEntry.COLUMN_CONDITION);
  83.         if (type2 == null || !Moneta.CoinEntry.isValidType2(type2)) {
  84.             throw new IllegalArgumentException("Выберите значение");
  85.         }
  86.  
  87.         String type3 = values.getAsString(Moneta.CoinEntry.COLUMN_MAGNET);
  88.         if (type3 == null || !Moneta.CoinEntry.isValidType3(type3)) {
  89.             throw new IllegalArgumentException("Выберите значение");
  90.         }
  91.  
  92.         String type4 = values.getAsString(Moneta.CoinEntry.COLUMN_STAMP);
  93.         if (type4 == null || !Moneta.CoinEntry.isValidType4(type4)) {
  94.             throw new IllegalArgumentException("Выберите значение");
  95.         }
  96.  
  97.  
  98.         String year = values.getAsString(Moneta.CoinEntry.COLUMN_YEAR);
  99.         if (year == null) {
  100.             throw new IllegalArgumentException("Введите год");
  101.         }
  102.  
  103.         String price = values.getAsString(Moneta.CoinEntry.COLUMN_PRICE);
  104.         if (price == null) {
  105.             throw new IllegalArgumentException("Введите цену или поставье '-'");
  106.         }
  107.  
  108.         String description = values.getAsString(Moneta.CoinEntry.COLUMN_DESCRIPTION);
  109.         if (description == null) {
  110.             throw new IllegalArgumentException("Введите описание");
  111.         }
  112.  
  113.         SQLiteDatabase database = mDbsqlite.getWritableDatabase();
  114.         long id = database.insert(Moneta.CoinEntry.TABLE_NAME, null, values);
  115.  
  116.         if (id == -1) {
  117.             return null;
  118.         }
  119.         getContext().getContentResolver().notifyChange(uri, null);
  120.         return ContentUris.withAppendedId(uri, id);
  121.     }
  122.  
  123.     @Override
  124.     public int delete(Uri uri, String selection, String[] selectionArgs) {
  125.          int rowsDeleted;
  126.          SQLiteDatabase database = mDbsqlite.getWritableDatabase();
  127.          int match = sUriMatcher.match(uri);
  128.          switch (match) {
  129.              case COINS:
  130.                  rowsDeleted = database.delete(Moneta.CoinEntry.TABLE_NAME, selection, selectionArgs);
  131.                  break;
  132.  
  133.              case COINS_ID:
  134.                  selection = Moneta.CoinEntry._ID + "=?";
  135.                  selectionArgs = new String[]{String.valueOf(ContentUris.parseId(uri))};
  136.                  rowsDeleted = database.delete(Moneta.CoinEntry.TABLE_NAME, selection, selectionArgs);
  137.                  break;
  138.  
  139.              default:
  140.                  throw new IllegalArgumentException("Невозможно удалить" + uri);
  141.  
  142.  
  143.          }
  144.  
  145.          if (rowsDeleted!=0) {
  146.              getContext().getContentResolver().notifyChange(uri, null);
  147.          }
  148.          return rowsDeleted;
  149.     }
  150.  
  151.     @Override
  152.     public int update(Uri uri, ContentValues values, String selection,  String[] selectionArgs) {
  153.         int match = sUriMatcher.match(uri);
  154.         switch (match) {
  155.             case COINS:
  156.                 return updateCoin(uri, values, selection, selectionArgs);
  157.  
  158.             case COINS_ID:
  159.  
  160.                 selection = Moneta.CoinEntry._ID + "=?";
  161.                 selectionArgs = new String[]{String.valueOf(ContentUris.parseId(uri))};
  162.                 return updateCoin(uri, values, selection, selectionArgs);
  163.  
  164.             default:
  165.                 throw new IllegalArgumentException("Невозможно обновить");
  166.  
  167.         }
  168.  
  169.     }
  170.  
  171.     private int updateCoin(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
  172.  
  173.         if (values.containsKey(Moneta.CoinEntry.COLUMN_NOMINAL)) {
  174.             String type = values.getAsString(Moneta.CoinEntry.COLUMN_NOMINAL);
  175.             if (type == null || !Moneta.CoinEntry.isValidType(type)) {
  176.                 throw new IllegalArgumentException("Выберите значение");
  177.             }
  178.         }
  179.  
  180.         if (values.containsKey(Moneta.CoinEntry.COLUMN_CONDITION)) {
  181.             String type2 = values.getAsString(Moneta.CoinEntry.COLUMN_CONDITION);
  182.             if (type2 == null || !Moneta.CoinEntry.isValidType2(type2)) {
  183.                 throw new IllegalArgumentException("Выберите значение");
  184.             }
  185.         }
  186.  
  187.         if (values.containsKey(Moneta.CoinEntry.COLUMN_MAGNET)) {
  188.             String type3 = values.getAsString(Moneta.CoinEntry.COLUMN_MAGNET);
  189.             if (type3 == null || !Moneta.CoinEntry.isValidType3(type3)) {
  190.                 throw new IllegalArgumentException("Выберите значение");
  191.             }
  192.         }
  193.  
  194.         if (values.containsKey(Moneta.CoinEntry.COLUMN_STAMP)) {
  195.             String type4 = values.getAsString(Moneta.CoinEntry.COLUMN_STAMP);
  196.             if (type4 == null || !Moneta.CoinEntry.isValidType4(type4)) {
  197.                 throw new IllegalArgumentException("Выберите значение");
  198.             }
  199.         }
  200.  
  201.  
  202.         if (values.containsKey(Moneta.CoinEntry.COLUMN_YEAR)) {
  203.             String year = values.getAsString(Moneta.CoinEntry.COLUMN_YEAR);
  204.             if (year == null) {
  205.                 throw new IllegalArgumentException("Введите год");
  206.             }
  207.         }
  208.  
  209.         if (values.containsKey(Moneta.CoinEntry.COLUMN_PRICE)) {
  210.             String price = values.getAsString(Moneta.CoinEntry.COLUMN_PRICE);
  211.             if (price == null) {
  212.                 throw new IllegalArgumentException("Введите цену или поставье '-'");
  213.             }
  214.         }
  215.  
  216.         if (values.containsKey(Moneta.CoinEntry.COLUMN_DESCRIPTION)) {
  217.             String description = values.getAsString(Moneta.CoinEntry.COLUMN_DESCRIPTION);
  218.             if (description == null) {
  219.                 throw new IllegalArgumentException("Введите описание");
  220.             }
  221.         }
  222.  
  223.         if (values.size() == 0) {
  224.             return 0;
  225.         }
  226.         SQLiteDatabase database = mDbsqlite.getWritableDatabase();
  227.         int roesUpdated = database.update(Moneta.CoinEntry.TABLE_NAME, values, selection, selectionArgs);
  228.  
  229.         if (roesUpdated !=0) {
  230.             getContext().getContentResolver().notifyChange(uri, null);
  231.         }
  232.         return roesUpdated;
  233.      }
  234.  
  235. }
  236.  
Advertisement
Add Comment
Please, Sign In to add comment