1
+ package fr .free .nrw .commons .recentlanguages ;
2
+
3
+ import static fr .free .nrw .commons .recentlanguages .RecentLanguagesDao .Table .COLUMN_NAME ;
4
+ import static fr .free .nrw .commons .recentlanguages .RecentLanguagesDao .Table .TABLE_NAME ;
5
+
6
+ import android .content .ContentValues ;
7
+ import android .database .Cursor ;
8
+ import android .database .sqlite .SQLiteDatabase ;
9
+ import android .database .sqlite .SQLiteQueryBuilder ;
10
+ import android .net .Uri ;
11
+ import android .text .TextUtils ;
12
+ import androidx .annotation .NonNull ;
13
+ import fr .free .nrw .commons .BuildConfig ;
14
+ import fr .free .nrw .commons .data .DBOpenHelper ;
15
+ import fr .free .nrw .commons .di .CommonsDaggerContentProvider ;
16
+ import javax .inject .Inject ;
17
+ import timber .log .Timber ;
18
+
19
+ /**
20
+ * Content provider of recently used languages
21
+ */
22
+ public class RecentLanguagesContentProvider extends CommonsDaggerContentProvider {
23
+
24
+ private static final String BASE_PATH = "recent_languages" ;
25
+ public static final Uri BASE_URI =
26
+ Uri .parse ("content://" + BuildConfig .RECENT_LANGUAGE_AUTHORITY + "/" + BASE_PATH );
27
+
28
+
29
+ /**
30
+ * Append language code to the base uri
31
+ * @param languageCode Code of a language
32
+ */
33
+ public static Uri uriForCode (final String languageCode ) {
34
+ return Uri .parse (BASE_URI + "/" + languageCode );
35
+ }
36
+
37
+ @ Inject
38
+ DBOpenHelper dbOpenHelper ;
39
+
40
+ @ Override
41
+ public String getType (@ NonNull final Uri uri ) {
42
+ return null ;
43
+ }
44
+
45
+ /**
46
+ * Queries the SQLite database for the recently used languages
47
+ * @param uri : contains the uri for recently used languages
48
+ * @param projection : contains the all fields of the table
49
+ * @param selection : handles Where
50
+ * @param selectionArgs : the condition of Where clause
51
+ * @param sortOrder : ascending or descending
52
+ */
53
+ @ Override
54
+ public Cursor query (@ NonNull final Uri uri , final String [] projection , final String selection ,
55
+ final String [] selectionArgs , final String sortOrder ) {
56
+ final SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder ();
57
+ queryBuilder .setTables (TABLE_NAME );
58
+ final SQLiteDatabase db = dbOpenHelper .getReadableDatabase ();
59
+ final Cursor cursor = queryBuilder .query (db , projection , selection ,
60
+ selectionArgs , null , null , sortOrder );
61
+ cursor .setNotificationUri (getContext ().getContentResolver (), uri );
62
+ return cursor ;
63
+ }
64
+
65
+ /**
66
+ * Handles the update query of local SQLite Database
67
+ * @param uri : contains the uri for recently used languages
68
+ * @param contentValues : new values to be entered to db
69
+ * @param selection : handles Where
70
+ * @param selectionArgs : the condition of Where clause
71
+ */
72
+ @ Override
73
+ public int update (@ NonNull final Uri uri , final ContentValues contentValues ,
74
+ final String selection , final String [] selectionArgs ) {
75
+ final SQLiteDatabase sqlDB = dbOpenHelper .getWritableDatabase ();
76
+ final int rowsUpdated ;
77
+ if (TextUtils .isEmpty (selection )) {
78
+ final int id = Integer .parseInt (uri .getLastPathSegment ());
79
+ rowsUpdated = sqlDB .update (TABLE_NAME ,
80
+ contentValues ,
81
+ COLUMN_NAME + " = ?" ,
82
+ new String []{String .valueOf (id )});
83
+ } else {
84
+ throw new IllegalArgumentException (
85
+ "Parameter `selection` should be empty when updating an ID" );
86
+ }
87
+
88
+ getContext ().getContentResolver ().notifyChange (uri , null );
89
+ return rowsUpdated ;
90
+ }
91
+
92
+ /**
93
+ * Handles the insertion of new recently used languages record to local SQLite Database
94
+ * @param uri : contains the uri for recently used languages
95
+ * @param contentValues : new values to be entered to db
96
+ */
97
+ @ Override
98
+ public Uri insert (@ NonNull final Uri uri , final ContentValues contentValues ) {
99
+ final SQLiteDatabase sqlDB = dbOpenHelper .getWritableDatabase ();
100
+ final long id = sqlDB .insert (TABLE_NAME , null , contentValues );
101
+ getContext ().getContentResolver ().notifyChange (uri , null );
102
+ return Uri .parse (BASE_URI + "/" + id );
103
+ }
104
+
105
+ /**
106
+ * Handles the deletion of new recently used languages record to local SQLite Database
107
+ * @param uri : contains the uri for recently used languages
108
+ */
109
+ @ Override
110
+ public int delete (@ NonNull final Uri uri , final String s , final String [] strings ) {
111
+ final int rows ;
112
+ final SQLiteDatabase db = dbOpenHelper .getReadableDatabase ();
113
+ Timber .d ("Deleting recently used language %s" , uri .getLastPathSegment ());
114
+ rows = db .delete (
115
+ TABLE_NAME ,
116
+ "language_code = ?" ,
117
+ new String []{uri .getLastPathSegment ()}
118
+ );
119
+ getContext ().getContentResolver ().notifyChange (uri , null );
120
+ return rows ;
121
+ }
122
+ }
0 commit comments