package com.seewo.vcommons.system.onoffsetting;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;
import com.seewo.vcommons.data.PropertiesUtils;
import com.seewo.vcommons.system.onoffsetting.Task;

/* JADX INFO: loaded from: classes.dex */
public class TimeSwitchProvider extends ContentProvider {
    public static final String PROVIDER_STRING = "com.seewo.vcommons.onoffsetting";
    private static final int TASK = 1;
    private static final int TASK_ITEM_ID = 2;
    private static final UriMatcher mURLMatcher;
    private SQLiteOpenHelper mOpenHelper;

    static {
        UriMatcher uriMatcher = new UriMatcher(-1);
        mURLMatcher = uriMatcher;
        uriMatcher.addURI(PROVIDER_STRING, "task", 1);
        uriMatcher.addURI(PROVIDER_STRING, "task/#", 2);
    }

    @Override // android.content.ContentProvider
    public boolean onCreate() {
        this.mOpenHelper = PropertiesUtils.getProperty(TimeSwitchSQLBusiness.KEY_SUPPORT_REBOOT_TASK, false) ? new TimeSwitchSQLHelperSupportRebootTask(getContext()) : new TimeSwitchSQLHelper(getContext());
        return true;
    }

    @Override // android.content.ContentProvider
    public Cursor query(Uri uri, String[] strArr, String str, String[] strArr2, String str2) {
        SQLiteQueryBuilder sQLiteQueryBuilder = new SQLiteQueryBuilder();
        int iMatch = mURLMatcher.match(uri);
        if (iMatch == 1) {
            sQLiteQueryBuilder.setTables("task");
        } else if (iMatch == 2) {
            sQLiteQueryBuilder.setTables("task");
            sQLiteQueryBuilder.appendWhere("_id=");
            sQLiteQueryBuilder.appendWhere(uri.getPathSegments().get(1));
        } else {
            throw new IllegalArgumentException("Unknown URL " + uri);
        }
        Cursor cursorQuery = sQLiteQueryBuilder.query(this.mOpenHelper.getReadableDatabase(), strArr, str, strArr2, null, null, str2);
        if (cursorQuery != null) {
            cursorQuery.setNotificationUri(getContext().getContentResolver(), uri);
        }
        return cursorQuery;
    }

    @Override // android.content.ContentProvider
    public Uri insert(Uri uri, ContentValues contentValues) {
        ContentValues contentValues2 = new ContentValues(contentValues);
        SQLiteDatabase writableDatabase = this.mOpenHelper.getWritableDatabase();
        if (mURLMatcher.match(uri) == 1) {
            long jInsert = writableDatabase.insert("task", Task.TaskColumns.MESSAGE, contentValues2);
            if (jInsert < 0) {
                throw new SQLException("Failed to insert row into " + uri);
            }
            Uri uriWithAppendedId = ContentUris.withAppendedId(uri, jInsert);
            getContext().getContentResolver().notifyChange(uriWithAppendedId, null);
            return uriWithAppendedId;
        }
        throw new IllegalArgumentException("Cannot insert into URL: " + uri);
    }

    @Override // android.content.ContentProvider
    public int delete(Uri uri, String str, String[] strArr) {
        int iDelete;
        String str2;
        SQLiteDatabase writableDatabase = this.mOpenHelper.getWritableDatabase();
        int iMatch = mURLMatcher.match(uri);
        if (iMatch == 1) {
            iDelete = writableDatabase.delete("task", str, strArr);
        } else if (iMatch == 2) {
            String str3 = uri.getPathSegments().get(1);
            if (TextUtils.isEmpty(str)) {
                str2 = "_id=" + str3;
            } else {
                str2 = "_id=" + str3 + " AND (" + str + ")";
            }
            iDelete = writableDatabase.delete("task", str2, strArr);
        } else {
            throw new IllegalArgumentException("Cannot delete from URL: " + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return iDelete;
    }

    @Override // android.content.ContentProvider
    public int update(Uri uri, ContentValues contentValues, String str, String[] strArr) {
        int iMatch = mURLMatcher.match(uri);
        SQLiteDatabase writableDatabase = this.mOpenHelper.getWritableDatabase();
        if (iMatch == 2) {
            int iUpdate = writableDatabase.update("task", contentValues, "_id=" + Long.parseLong(uri.getPathSegments().get(1)), null);
            getContext().getContentResolver().notifyChange(uri, null);
            return iUpdate;
        }
        throw new UnsupportedOperationException("Cannot update URL: " + uri);
    }

    @Override // android.content.ContentProvider
    public String getType(Uri uri) {
        int iMatch = mURLMatcher.match(uri);
        if (iMatch == 1) {
            return "vnd.android.cursor.dir/task";
        }
        if (iMatch == 2) {
            return "vnd.android.cursor.item/task";
        }
        throw new IllegalArgumentException("Unknown URL");
    }
}
