completed forbidden provider.
authorTrillGates <williamtrillgates@gmail.com>
Mon, 6 Sep 2021 03:56:30 +0000 (11:56 +0800)
committerTrillGates <williamtrillgates@gmail.com>
Mon, 6 Sep 2021 03:56:30 +0000 (11:56 +0800)
app/src/main/java/com/enqualcomm/support/provider/ICForbiddenProvider.java

index f836123..95d1f9b 100644 (file)
@@ -2,18 +2,39 @@ 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;
 
+import com.enqualcomm.support.utils.ICLogger;
+
 /**
  * 上课禁用时间段的内容提供者
  */
 public class ICForbiddenProvider extends ContentProvider {
 
+    //添加规则
+    public static final String AUTHORITY = "com.ic.forbidden";
+    public static final String PATH = "time";
+    public static final String FORBIDDEN = "content://" + AUTHORITY + "/" + PATH;
+
+
+    private static UriMatcher sUriMatcher;
+    public static final int MATCH_CODE = 0x1;
+
+    static {
+        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
+        sUriMatcher.addURI(AUTHORITY, PATH, MATCH_CODE);
+    }
+
+    private ICDatabaseHelper mDatabase;
+
     @Override
     public boolean onCreate() {
+        mDatabase = new ICDatabaseHelper(getContext());
         return false;
     }
 
@@ -21,6 +42,14 @@ public class ICForbiddenProvider extends ContentProvider {
     @Override
     public Cursor query(@NonNull Uri uri, @Nullable String[] projection,
                         @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
+        int match = sUriMatcher.match(uri);
+        if (match == MATCH_CODE) {
+            SQLiteDatabase readableDatabase = mDatabase.getReadableDatabase();
+            return readableDatabase.query(ICDatabaseHelper.tSettings, projection,
+                    selection,
+                    selectionArgs,
+                    null, null, sortOrder);
+        }
         return null;
     }
 
@@ -33,16 +62,52 @@ public class ICForbiddenProvider extends ContentProvider {
     @Nullable
     @Override
     public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
+        int match = sUriMatcher.match(uri);
+        if (match == MATCH_CODE) {
+            //匹配得上,插入数据
+            SQLiteDatabase writableDatabase = mDatabase.getWritableDatabase();
+            long insert = writableDatabase.insert(ICDatabaseHelper.tb_forbidden_time, null, values);
+            ICLogger.d("insert ==> " + insert);
+            //通知
+            if (getContext() != null) {
+                getContext().getContentResolver().notifyChange(uri, null);
+            }
+            //返回
+            return uri;
+        }
         return null;
     }
 
     @Override
     public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
+        //删除数据
+        int match = sUriMatcher.match(uri);
+        if (match == MATCH_CODE) {
+            SQLiteDatabase readableDatabase = mDatabase.getReadableDatabase();
+            int delete = readableDatabase.delete(ICDatabaseHelper.tb_forbidden_time,
+                    selection, selectionArgs);
+            ICLogger.d("delete ==> " + delete);
+            if (getContext() != null) {
+                getContext().getContentResolver().notifyChange(uri, null);
+            }
+            return delete;
+        }
         return 0;
     }
 
     @Override
-    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
+    public int update(@NonNull Uri uri, @Nullable ContentValues values,
+                      @Nullable String selection, @Nullable String[] selectionArgs) {
+        int match = sUriMatcher.match(uri);
+        if (match == MATCH_CODE) {
+            SQLiteDatabase readableDatabase = mDatabase.getReadableDatabase();
+            int update = readableDatabase.update(ICDatabaseHelper.tb_forbidden_time, values, selection, selectionArgs);
+            ICLogger.d("update ==> " + update);
+            if (getContext() != null) {
+                getContext().getContentResolver().notifyChange(uri, null);
+            }
+            return update;
+        }
         return 0;
     }
 }