admin
2024-01-26 c2d382d99ca506932985d1843d4371d6ed0203ff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package com.weikou.beibeivideo.db;
 
import java.util.HashMap;
 
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.SQLiteQueryBuilder;
import android.net.Uri;
import androidx.core.database.DatabaseUtilsCompat;
import android.util.Log;
 
public class WatchHistoryProvider extends ContentProvider {
    
    // A projection map used to select columns from the database
    private final HashMap<String, String> mNotesProjectionMap;
    // Uri matcher to decode incoming URIs.
    private final UriMatcher mUriMatcher;
 
    // The incoming URI matches the main table URI pattern
    private static final int WATCH_HISTORY = 1;
    // The incoming URI matches the main table row ID URI pattern
    private static final int WATCH_HISTORY_ID = 2;
    
    // Handle to a new DatabaseHelper.
    private BeibeiSQLiteOpenHelper mOpenHelper;
 
    /**
     * Global provider initialization.
     */
    public WatchHistoryProvider() {
        // Create and initialize URI matcher.
        mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        mUriMatcher.addURI(WatchHistoryTable.AUTHORITY, "watchhistories", WATCH_HISTORY);
        mUriMatcher.addURI(WatchHistoryTable.AUTHORITY, "watchhistories/#", WATCH_HISTORY_ID);
 
        // Create and initialize projection map for all columns.  This is
        // simply an identity mapping.
        mNotesProjectionMap = new HashMap<>();
        mNotesProjectionMap.put(WatchHistoryTable._ID, WatchHistoryTable._ID);
        mNotesProjectionMap.put(WatchHistoryTable.VIDEO_ID, WatchHistoryTable.VIDEO_ID);
        mNotesProjectionMap.put(WatchHistoryTable.VIDEO_DETAIL_ID, WatchHistoryTable.VIDEO_DETAIL_ID);
        mNotesProjectionMap.put(WatchHistoryTable.VIDEO_THIRD_TYPE, WatchHistoryTable.VIDEO_THIRD_TYPE);
        mNotesProjectionMap.put(WatchHistoryTable.VIDEO_DETAIL, WatchHistoryTable.VIDEO_DETAIL);
        mNotesProjectionMap.put(WatchHistoryTable.WATCH_TIME, WatchHistoryTable.WATCH_TIME);
        mNotesProjectionMap.put(WatchHistoryTable.UPDATE_TIME, WatchHistoryTable.UPDATE_TIME);
        mNotesProjectionMap.put(WatchHistoryTable.CREATE_TIME, WatchHistoryTable.CREATE_TIME);
        mNotesProjectionMap.put(WatchHistoryTable.VIDEO_RESOURCE, WatchHistoryTable.VIDEO_RESOURCE);
        mNotesProjectionMap.put(WatchHistoryTable.VIDEO_RESOURCE_ID, WatchHistoryTable.VIDEO_RESOURCE_ID);
    }
 
    /**
     * Perform provider creation.
     */
    @Override
    public boolean onCreate() {
        mOpenHelper = new BeibeiSQLiteOpenHelper(getContext());
        // Assumes that any failures will be reported by a thrown exception.
        Log.i("mResult", "BeibeiSQLiteOpenHelper");
        return true;
    }
 
    /**
     * Return the MIME type for an known URI in the provider.
     */
    @Override
    public String getType(Uri uri) {
        switch (mUriMatcher.match(uri)) {
            case WATCH_HISTORY:
                return WatchHistoryTable.CONTENT_TYPE;
            case WATCH_HISTORY_ID:
                return WatchHistoryTable.CONTENT_ITEM_TYPE;
            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }
    }
    
    /**
     * Handle incoming queries.
     */
    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        Cursor c;
        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        switch (mUriMatcher.match(uri)) {
            case WATCH_HISTORY: {
                // Constructs a new query builder and sets its table name
                SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
                qb.setTables(getTableName(uri));
                // If the incoming URI is for main table.
                qb.setProjectionMap(mNotesProjectionMap);
                c = qb.query(db, projection, selection, selectionArgs,
                        null /* no group */, null /* no filter */, sortOrder);
            }
            break;
            case WATCH_HISTORY_ID: {
                // Constructs a new query builder and sets its table name
                SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
                qb.setTables(getTableName(uri));
                // The incoming URI is for a single row.
                qb.setProjectionMap(mNotesProjectionMap);
                qb.appendWhere(WatchHistoryTable._ID + "=?");
                selectionArgs = DatabaseUtilsCompat.appendSelectionArgs(selectionArgs,
                        new String[] { uri.getLastPathSegment() });
                c = qb.query(db, projection, selection, selectionArgs,
                        null /* no group */, null /* no filter */, sortOrder);
            }
            break;
            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }
        c.setNotificationUri(getContext().getContentResolver(), uri);
        return c;
    }
 
    /**
     * Handler inserting new data.
     */
    @Override
    public Uri insert(Uri uri, ContentValues initialValues) {
        if (mUriMatcher.match(uri) != WATCH_HISTORY) {
            // Can only insert into to main URI.
            throw new IllegalArgumentException("Unknown URI " + uri);
        }
 
        ContentValues values;
 
        if (initialValues != null) {
            values = new ContentValues(initialValues);
        } else {
            values = new ContentValues();
        }
 
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        long rowId = db.insert(WatchHistoryTable.TABLE_NAME, null, values);
        // If the insert succeeded, the row ID exists.
        if (rowId > 0) {
            Uri noteUri = ContentUris.withAppendedId(WatchHistoryTable.CONTENT_URI, rowId);
            getContext().getContentResolver().notifyChange(noteUri, null);
            return noteUri;
        }
 
        throw new SQLException("Failed to insert row into " + uri);
    }
 
    /**
     * Handle deleting data.
     */
    @Override
    public int delete(Uri uri, String where, String[] whereArgs) {
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        String finalWhere;
 
        int count;
 
        switch (mUriMatcher.match(uri)) {
            case WATCH_HISTORY:
                // If URI is main table, delete uses incoming where clause and args.
                count = db.delete(WatchHistoryTable.TABLE_NAME, where, whereArgs);
                break;
 
                // If the incoming URI matches a single note ID, does the delete based on the
                // incoming data, but modifies the where clause to restrict it to the
                // particular note ID.
            case WATCH_HISTORY_ID:
                // If URI is for a particular row ID, delete is based on incoming
                // data but modified to restrict to the given ID.
                finalWhere = DatabaseUtilsCompat.concatenateWhere(
                        WatchHistoryTable._ID + " = " + uri.getLastPathSegment(), where);
                count = db.delete(WatchHistoryTable.TABLE_NAME, finalWhere, whereArgs);
                break;
 
            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
 
        return count;
    }
 
    /**
     * Handle updating data.
     */
    @Override
    public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        int count;
        String finalWhere;
 
        switch (mUriMatcher.match(uri)) {
            case WATCH_HISTORY:
                // If URI is main table, update uses incoming where clause and args.
                count = db.update(WatchHistoryTable.TABLE_NAME, values, where, whereArgs);
                break;
 
            case WATCH_HISTORY_ID:
                // If URI is for a particular row ID, update is based on incoming
                // data but modified to restrict to the given ID.
                finalWhere = DatabaseUtilsCompat.concatenateWhere(
                        WatchHistoryTable._ID + " = " + uri.getLastPathSegment(), where);
                count = db.update(WatchHistoryTable.TABLE_NAME, values, finalWhere, whereArgs);
                break;
 
            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
 
        return count;
    }
    
    private String getTableName(Uri uri) {
        switch (mUriMatcher.match(uri)) {
        case WATCH_HISTORY:
        case WATCH_HISTORY_ID:
            return WatchHistoryTable.TABLE_NAME;
        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }
    }
 
}