completed web sms provider curd.
authorTrillGates <williamtrillgates@gmail.com>
Thu, 8 Jul 2021 13:14:01 +0000 (21:14 +0800)
committerTrillGates <williamtrillgates@gmail.com>
Thu, 8 Jul 2021 13:14:01 +0000 (21:14 +0800)
app/src/main/java/com/enqualcomm/support/provider/WebSmsProvider.java

index 34a1b46..76a2992 100644 (file)
@@ -2,24 +2,58 @@ package com.enqualcomm.support.provider;
 
 import android.content.ContentProvider;
 import android.content.ContentValues;
+import android.content.UriMatcher;
 import android.database.Cursor;
+import android.database.sqlite.SQLiteDatabase;
 import android.net.Uri;
 import android.support.annotation.NonNull;
 import android.support.annotation.Nullable;
 
 /**
- * 云消息内容提供者
+ * 云消息内容提供者,由后台下发的云短信。
  */
 public class WebSmsProvider extends ContentProvider {
 
+    public static final String PATH = "record";
+    //com.ic.web.sms
+    public static final String AUTHORITY = "com.ic.web.sms";
+    public static final String WEB_MSG = "content://" + AUTHORITY + "/" + PATH;
+
+
+    private static UriMatcher sUriMatcher;
+
+    private static final int MATH_CODE_TABLE = 1;
+
+
+    static {
+        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
+        sUriMatcher.addURI(AUTHORITY, PATH, MATH_CODE_TABLE);
+    }
+
+
+    private ICDatabaseHelper mDatabase;
+
+
     @Override
     public boolean onCreate() {
+        mDatabase = new ICDatabaseHelper(getContext());
         return false;
     }
 
     @Nullable
     @Override
     public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
+        //查询警告消息列表
+        //匹配规则
+        int match = sUriMatcher.match(uri);
+        SQLiteDatabase readableDatabase = mDatabase.getReadableDatabase();
+        if (match == MATH_CODE_TABLE) {
+            return readableDatabase.query(ICDatabaseHelper.tb_web_sms,
+                    projection,
+                    selection,
+                    selectionArgs,
+                    null, null, sortOrder);
+        }
         return null;
     }
 
@@ -32,16 +66,38 @@ public class WebSmsProvider extends ContentProvider {
     @Nullable
     @Override
     public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
+        //插入数据
+        int match = sUriMatcher.match(uri);
+        SQLiteDatabase readableDatabase = mDatabase.getReadableDatabase();
+        if (match == MATH_CODE_TABLE) {
+            long insert = readableDatabase.insert(ICDatabaseHelper.tb_web_sms, null, values);
+            //通知UI更新
+            getContext().getContentResolver().notifyChange(uri, null);
+            return Uri.parse(AUTHORITY + "/" + PATH + "/" + insert);
+        }
         return null;
     }
 
     @Override
     public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
+        int match = sUriMatcher.match(uri);
+        if (match == MATH_CODE_TABLE) {
+            SQLiteDatabase writableDatabase = mDatabase.getWritableDatabase();
+            return writableDatabase.delete(ICDatabaseHelper.tb_web_sms, selection, selectionArgs);
+        }
         return 0;
     }
 
     @Override
     public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
+        int match = sUriMatcher.match(uri);
+        if (match == MATH_CODE_TABLE) {
+            SQLiteDatabase writableDatabase = mDatabase.getWritableDatabase();
+            int update = writableDatabase.update(ICDatabaseHelper.tb_web_sms,
+                    values, selection, selectionArgs);
+            writableDatabase.close();
+            return update;
+        }
         return 0;
     }
 }