]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/helpers/BookmarksDatabaseHelper.kt
Open all bookmarks in a folder on long-press. https://redmine.stoutner.com/issues/926
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / helpers / BookmarksDatabaseHelper.kt
1 /*
2  * Copyright 2016-2022 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
5  *
6  * Privacy Browser Android is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Privacy Browser Android is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Privacy Browser Android.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package com.stoutner.privacybrowser.helpers
21
22 import android.content.ContentValues
23 import android.content.Context
24 import android.database.Cursor
25 import android.database.DatabaseUtils
26 import android.database.sqlite.SQLiteDatabase
27 import android.database.sqlite.SQLiteOpenHelper
28
29 // Define the class constants.
30 private const val SCHEMA_VERSION = 1
31
32 class BookmarksDatabaseHelper(context: Context) : SQLiteOpenHelper(context, BOOKMARKS_DATABASE, null, SCHEMA_VERSION) {
33     // Define the public companion object constants.  These can be moved to public class constants once the entire project has migrated to Kotlin.
34     companion object {
35         // Define the public database constants.
36         const val BOOKMARKS_DATABASE = "bookmarks.db"
37         const val BOOKMARKS_TABLE = "bookmarks"
38
39         // Define the public schema constants.
40         const val ID = "_id"
41         const val BOOKMARK_NAME = "bookmarkname"
42         const val BOOKMARK_URL = "bookmarkurl"
43         const val PARENT_FOLDER = "parentfolder"
44         const val DISPLAY_ORDER = "displayorder"
45         const val IS_FOLDER = "isfolder"
46         const val FAVORITE_ICON = "favoriteicon"
47
48         // Define the public table creation constant.
49         const val CREATE_BOOKMARKS_TABLE = "CREATE TABLE $BOOKMARKS_TABLE (" +
50                 "$ID INTEGER PRIMARY KEY, " +
51                 "$BOOKMARK_NAME TEXT, " +
52                 "$BOOKMARK_URL TEXT, " +
53                 "$PARENT_FOLDER TEXT, " +
54                 "$DISPLAY_ORDER INTEGER, " +
55                 "$IS_FOLDER BOOLEAN, " +
56                 "$FAVORITE_ICON BLOB)"
57     }
58
59     override fun onCreate(bookmarksDatabase: SQLiteDatabase) {
60         // Create the bookmarks table.
61         bookmarksDatabase.execSQL(CREATE_BOOKMARKS_TABLE)
62     }
63
64     override fun onUpgrade(bookmarksDatabase: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
65         // Code for upgrading the database will be added here when the schema version > 1.
66     }
67
68     // Get a cursor of all the folders.
69     val allFolders: Cursor
70         get() {
71             // Get a readable database handle.
72             val bookmarksDatabase = this.readableDatabase
73
74             // Return the cursor with the all the folders.  The cursor cannot be closed because it is used in the parent activity.
75             return bookmarksDatabase.rawQuery("SELECT * FROM $BOOKMARKS_TABLE WHERE $IS_FOLDER = 1 ORDER BY $BOOKMARK_NAME ASC", null)
76         }
77
78     // Get a cursor for all bookmarks and folders.
79     val allBookmarks: Cursor
80         get() {
81             // Get a readable database handle.
82             val bookmarksDatabase = this.readableDatabase
83
84             // Return a cursor with the entire contents of the bookmarks table.  The cursor cannot be closed because it is used in the parent activity.
85             return bookmarksDatabase.rawQuery("SELECT * FROM $BOOKMARKS_TABLE", null)
86         }
87
88     // Get a cursor for all bookmarks and folders ordered by display order.
89     val allBookmarksByDisplayOrder: Cursor
90         get() {
91             // Get a readable database handle.
92             val bookmarksDatabase = this.readableDatabase
93
94             // Return a cursor with the entire contents of the bookmarks table ordered by the display order.  The cursor cannot be closed because it is used in the parent activity.
95             return bookmarksDatabase.rawQuery("SELECT * FROM $BOOKMARKS_TABLE ORDER BY $DISPLAY_ORDER ASC", null)
96         }
97
98     // Create a bookmark.
99     fun createBookmark(bookmarkName: String, bookmarkURL: String, parentFolder: String, displayOrder: Int, favoriteIcon: ByteArray) {
100         // Store the bookmark data in a content values.
101         val bookmarkContentValues = ContentValues()
102
103         // The ID is created automatically.
104         bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName)
105         bookmarkContentValues.put(BOOKMARK_URL, bookmarkURL)
106         bookmarkContentValues.put(PARENT_FOLDER, parentFolder)
107         bookmarkContentValues.put(DISPLAY_ORDER, displayOrder)
108         bookmarkContentValues.put(IS_FOLDER, false)
109         bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon)
110
111         // Get a writable database handle.
112         val bookmarksDatabase = this.writableDatabase
113
114         // Insert a new row.
115         bookmarksDatabase.insert(BOOKMARKS_TABLE, null, bookmarkContentValues)
116
117         // Close the database handle.
118         bookmarksDatabase.close()
119     }
120
121     // Create a bookmark from content values.
122     fun createBookmark(contentValues: ContentValues) {
123         // Get a writable database.
124         val bookmarksDatabase = this.writableDatabase
125
126         // Insert a new row.
127         bookmarksDatabase.insert(BOOKMARKS_TABLE, null, contentValues)
128
129         // Close the database handle.
130         bookmarksDatabase.close()
131     }
132
133     // Create a folder.
134     fun createFolder(folderName: String, parentFolder: String, favoriteIcon: ByteArray) {
135         // Store the bookmark folder data in a content values.
136         val bookmarkFolderContentValues = ContentValues()
137
138         // The ID is created automatically.  Folders are always created at the top of the list.
139         bookmarkFolderContentValues.put(BOOKMARK_NAME, folderName)
140         bookmarkFolderContentValues.put(PARENT_FOLDER, parentFolder)
141         bookmarkFolderContentValues.put(DISPLAY_ORDER, 0)
142         bookmarkFolderContentValues.put(IS_FOLDER, true)
143         bookmarkFolderContentValues.put(FAVORITE_ICON, favoriteIcon)
144
145         // Get a writable database handle.
146         val bookmarksDatabase = this.writableDatabase
147
148         // Insert the new folder.
149         bookmarksDatabase.insert(BOOKMARKS_TABLE, null, bookmarkFolderContentValues)
150
151         // Close the database handle.
152         bookmarksDatabase.close()
153     }
154
155     // Delete one bookmark.
156     fun deleteBookmark(databaseId: Int) {
157         // Get a writable database handle.
158         val bookmarksDatabase = this.writableDatabase
159
160         // Deletes the row with the given database ID.
161         bookmarksDatabase.delete(BOOKMARKS_TABLE, "$ID = $databaseId", null)
162
163         // Close the database handle.
164         bookmarksDatabase.close()
165     }
166
167     // Get a cursor for the bookmark with the specified database ID.
168     fun getBookmark(databaseId: Int): Cursor {
169         // Get a readable database handle.
170         val bookmarksDatabase = this.readableDatabase
171
172         // Return the cursor for the database ID.  The cursor can't be closed because it is used in the parent activity.
173         return bookmarksDatabase.rawQuery("SELECT * FROM $BOOKMARKS_TABLE WHERE $ID = $databaseId", null)
174     }
175
176     // Get a cursor for all bookmarks and folders by display order except those with the specified IDs.
177     fun getAllBookmarksByDisplayOrderExcept(exceptIdLongArray: LongArray): Cursor {
178         // Get a readable database handle.
179         val bookmarksDatabase = this.readableDatabase
180
181         // Prepare a string builder to contain the comma-separated list of IDs not to get.
182         val idsNotToGetStringBuilder = StringBuilder()
183
184         // Extract the array of IDs not to get to the string builder.
185         for (databaseIdLong in exceptIdLongArray) {
186             // Check to see if there is already a number in the builder.
187             if (idsNotToGetStringBuilder.isNotEmpty()) {
188                 // This is not the first number, so place a `,` before the new number.
189                 idsNotToGetStringBuilder.append(",")
190             }
191
192             // Add the new number to the builder.
193             idsNotToGetStringBuilder.append(databaseIdLong)
194         }
195
196         // Return a cursor with all the bookmarks except those specified ordered by display order.  The cursor cannot be closed because it is used in the parent activity.
197         return bookmarksDatabase.rawQuery("SELECT * FROM $BOOKMARKS_TABLE WHERE $ID NOT IN ($idsNotToGetStringBuilder) ORDER BY $DISPLAY_ORDER ASC", null)
198     }
199
200     // Get a cursor for all bookmarks and folders except those with the specified IDs.
201     fun getAllBookmarksExcept(exceptIdLongArray: LongArray): Cursor {
202         // Get a readable database handle.
203         val bookmarksDatabase = this.readableDatabase
204
205         // Prepare a string builder to contain the comma-separated list of IDs not to get.
206         val idsNotToGetStringBuilder = StringBuilder()
207
208         // Extract the array of IDs not to get to the string builder.
209         for (databaseIdLong in exceptIdLongArray) {
210             // Check to see if there is already a number in the builder.
211             if (idsNotToGetStringBuilder.isNotEmpty()) {
212                 // This is not the first number, so place a `,` before the new number.
213                 idsNotToGetStringBuilder.append(",")
214             }
215
216             // Add the new number to the builder.
217             idsNotToGetStringBuilder.append(databaseIdLong)
218         }
219
220         // Return a cursor with all the bookmarks except those specified.  The cursor cannot be closed because it is used in the parent activity.
221         return bookmarksDatabase.rawQuery("SELECT * FROM $BOOKMARKS_TABLE WHERE $ID NOT IN ($idsNotToGetStringBuilder)", null)
222     }
223
224     // Get a cursor with just database ID of bookmarks and folders in the specified folder.  This is useful for deleting folders with bookmarks that have favorite icons too large to fit in a cursor.
225     fun getBookmarkIds(folderName: String): Cursor {
226         // Get a readable database handle.
227         val bookmarksDatabase = this.readableDatabase
228
229         // SQL escape the folder name.
230         val sqlEscapedFolderName = DatabaseUtils.sqlEscapeString(folderName)
231
232         // Return a cursor with all the database IDs.  The cursor cannot be closed because it is used in the parent activity.
233         return bookmarksDatabase.rawQuery("SELECT $ID FROM $BOOKMARKS_TABLE WHERE $PARENT_FOLDER = $sqlEscapedFolderName", null)
234     }
235
236     // Get a cursor for bookmarks and folders in the specified folder.
237     fun getBookmarks(folderName: String): Cursor {
238         // Get a readable database handle.
239         val bookmarksDatabase = this.readableDatabase
240
241         // SQL escape the folder name.
242         val sqlEscapedFolderName = DatabaseUtils.sqlEscapeString(folderName)
243
244         // Return a cursor with all the bookmarks in a specified folder.  The cursor cannot be closed because it is used in the parent activity.
245         return bookmarksDatabase.rawQuery("SELECT * FROM $BOOKMARKS_TABLE WHERE $PARENT_FOLDER = $sqlEscapedFolderName", null)
246     }
247
248     // Get a cursor for bookmarks and folders in the specified folder ordered by display order.
249     fun getBookmarksByDisplayOrder(folderName: String): Cursor {
250         // Get a readable database handle.
251         val bookmarksDatabase = this.readableDatabase
252
253         // SQL escape the folder name.
254         val sqlEscapedFolderName = DatabaseUtils.sqlEscapeString(folderName)
255
256         // Return a cursor with all the bookmarks in the specified folder ordered by display order.  The cursor cannot be closed because it is used in the parent activity.
257         return bookmarksDatabase.rawQuery("SELECT * FROM $BOOKMARKS_TABLE WHERE $PARENT_FOLDER = $sqlEscapedFolderName ORDER BY $DISPLAY_ORDER ASC", null)
258     }
259
260     // Get a cursor for bookmarks and folders in the specified folder by display order except those with the specified IDs.
261     fun getBookmarksByDisplayOrderExcept(exceptIdLongArray: LongArray, folderName: String): Cursor {
262         // Get a readable database handle.
263         val bookmarksDatabase = this.readableDatabase
264
265         // Prepare a string builder to contain the comma-separated list of IDs not to get.
266         val idsNotToGetStringBuilder = StringBuilder()
267
268         // Extract the array of IDs not to get to the string builder.
269         for (databaseIdLong in exceptIdLongArray) {
270             // Check to see if there is already a number in the builder.
271             if (idsNotToGetStringBuilder.isNotEmpty()) {
272                 // This is not the first number, so place a `,` before the new number.
273                 idsNotToGetStringBuilder.append(",")
274             }
275
276             // Add the new number to the builder.
277             idsNotToGetStringBuilder.append(databaseIdLong)
278         }
279
280         // SQL escape the folder name.
281         val sqlEscapedFolderName = DatabaseUtils.sqlEscapeString(folderName)
282
283         // Return a cursor with all the bookmarks in the specified folder except for those database IDs specified ordered by display order.
284         // The cursor cannot be closed because it will be used in the parent activity.
285         return bookmarksDatabase.rawQuery("SELECT * FROM $BOOKMARKS_TABLE WHERE $PARENT_FOLDER = $sqlEscapedFolderName AND $ID NOT IN ($idsNotToGetStringBuilder) ORDER BY $DISPLAY_ORDER ASC",
286             null)
287     }
288
289     // Get a cursor for bookmarks and folders in the specified folder except those with the specified IDs.
290     fun getBookmarksExcept(exceptIdLongArray: LongArray, folderName: String): Cursor {
291         // Get a readable database handle.
292         val bookmarksDatabase = this.readableDatabase
293
294         // Prepare a string builder to contain the comma-separated list of IDs not to get.
295         val idsNotToGetStringBuilder = StringBuilder()
296
297         // Extract the array of IDs not to get to the string builder.
298         for (databaseIdLong in exceptIdLongArray) {
299             // Check to see if there is already a number in the builder.
300             if (idsNotToGetStringBuilder.isNotEmpty()) {
301                 // This is not the first number, so place a `,` before the new number.
302                 idsNotToGetStringBuilder.append(",")
303             }
304
305             // Add the new number to the builder.
306             idsNotToGetStringBuilder.append(databaseIdLong)
307         }
308
309         // SQL escape the folder name.
310         val sqlEscapedFolderName = DatabaseUtils.sqlEscapeString(folderName)
311
312         // Return a cursor with all the bookmarks in the specified folder except for those database IDs specified.  The cursor cannot be closed because it is used in the parent activity.
313         return bookmarksDatabase.rawQuery("SELECT * FROM $BOOKMARKS_TABLE WHERE $PARENT_FOLDER = $sqlEscapedFolderName AND $ID NOT IN ($idsNotToGetStringBuilder)", null)
314     }
315
316     // Get a cursor for the specified folder name.
317     fun getFolder(folderName: String): Cursor {
318         // Get a readable database handle.
319         val bookmarksDatabase = this.readableDatabase
320
321         // SQL escape the folder name.
322         val sqlEscapedFolderName = DatabaseUtils.sqlEscapeString(folderName)
323
324         // Return the cursor for the specified folder.  The cursor can't be closed because it is used in the parent activity.
325         return bookmarksDatabase.rawQuery("SELECT * FROM $BOOKMARKS_TABLE WHERE $BOOKMARK_NAME = $sqlEscapedFolderName AND $IS_FOLDER = 1", null)
326     }
327
328     fun getFolderBookmarks(folderDatabaseId: Int): Cursor {
329         // Get the folder name.
330         val folderName = getFolderName(folderDatabaseId)
331
332         // SQL escape the folder name.
333         val sqlEscapedFolderName = DatabaseUtils.sqlEscapeString(folderName)
334
335         // Get a readable database handle.
336         val bookmarksDatabase = this.readableDatabase
337
338         // Return a cursor with all the bookmarks in the folder.  The cursor cannot be closed because it is used in the parent activity.
339         return bookmarksDatabase.rawQuery("SELECT * FROM $BOOKMARKS_TABLE WHERE $PARENT_FOLDER = $sqlEscapedFolderName AND $IS_FOLDER = 0 ORDER BY $DISPLAY_ORDER ASC", null)
340     }
341
342     // Get the database ID for the specified folder name.
343     fun getFolderDatabaseId(folderName: String): Int {
344         // Get a readable database handle.
345         val bookmarksDatabase = this.readableDatabase
346
347         // SQL escape the folder name.
348         val sqlEscapedFolderName = DatabaseUtils.sqlEscapeString(folderName)
349
350         // Get the cursor for the folder with the specified name.
351         val folderCursor = bookmarksDatabase.rawQuery("SELECT * FROM $BOOKMARKS_TABLE WHERE $BOOKMARK_NAME = $sqlEscapedFolderName AND $IS_FOLDER = 1", null)
352
353         // Move to the first record.
354         folderCursor.moveToFirst()
355
356         // Get the database ID.
357         val databaseId = folderCursor.getInt(folderCursor.getColumnIndexOrThrow(ID))
358
359         // Close the cursor and the database handle.
360         folderCursor.close()
361         bookmarksDatabase.close()
362
363         // Return the database ID.
364         return databaseId
365     }
366
367     // Get the folder name for the specified database ID.
368     fun getFolderName(databaseId: Int): String {
369         // Get a readable database handle.
370         val bookmarksDatabase = this.readableDatabase
371
372         // Get the cursor for the folder with the specified database ID.
373         val folderCursor = bookmarksDatabase.rawQuery("SELECT * FROM $BOOKMARKS_TABLE WHERE $ID = $databaseId", null)
374
375         // Move to the first record.
376         folderCursor.moveToFirst()
377
378         // Get the folder name.
379         val folderName = folderCursor.getString(folderCursor.getColumnIndexOrThrow(BOOKMARK_NAME))
380
381         // Close the cursor and the database handle.
382         folderCursor.close()
383         bookmarksDatabase.close()
384
385         // Return the folder name.
386         return folderName
387     }
388
389     // Get a cursor of all the folders except those specified.
390     fun getFoldersExcept(exceptFolders: String): Cursor {
391         // Get a readable database handle.
392         val bookmarksDatabase = this.readableDatabase
393
394         // Return the cursor of all folders except those specified.  Each individual folder in the list has already been SQL escaped.  The cursor can't be closed because it is used in the parent activity.
395         return bookmarksDatabase.rawQuery("SELECT * FROM $BOOKMARKS_TABLE WHERE $IS_FOLDER = 1 AND $BOOKMARK_NAME NOT IN ($exceptFolders) ORDER BY $BOOKMARK_NAME ASC", null)
396     }
397
398     // Get the name of the parent folder.
399     fun getParentFolderName(currentFolder: String): String {
400         // Get a readable database handle.
401         val bookmarksDatabase = this.readableDatabase
402
403         // SQL escape the current folder.
404         val sqlEscapedCurrentFolder = DatabaseUtils.sqlEscapeString(currentFolder)
405
406         // Get a cursor for the current folder.
407         val bookmarkCursor = bookmarksDatabase.rawQuery("SELECT * FROM $BOOKMARKS_TABLE WHERE $IS_FOLDER = 1 AND $BOOKMARK_NAME = $sqlEscapedCurrentFolder", null)
408
409         // Move to the first record.
410         bookmarkCursor.moveToFirst()
411
412         // Store the name of the parent folder.
413         val parentFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndexOrThrow(PARENT_FOLDER))
414
415         // Close the cursor and the database.
416         bookmarkCursor.close()
417         bookmarksDatabase.close()
418
419         // Return the parent folder string.
420         return parentFolder
421     }
422
423     // Get the name of the parent folder.
424     fun getParentFolderName(databaseId: Int): String {
425         // Get a readable database handle.
426         val bookmarksDatabase = this.readableDatabase
427
428         // Get a cursor for the specified database ID.
429         val bookmarkCursor = bookmarksDatabase.rawQuery("SELECT * FROM $BOOKMARKS_TABLE WHERE $ID = $databaseId", null)
430
431         // Move to the first record.
432         bookmarkCursor.moveToFirst()
433
434         // Store the name of the parent folder.
435         val parentFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndexOrThrow(PARENT_FOLDER))
436
437         // Close the cursor and the database.
438         bookmarkCursor.close()
439         bookmarksDatabase.close()
440
441         // Return the parent folder string.
442         return parentFolder
443     }
444
445     // Get a cursor with all the subfolders of the specified folder.
446     fun getSubfolders(currentFolder: String): Cursor {
447         // Get a readable database handle.
448         val bookmarksDatabase = this.readableDatabase
449
450         // SQL escape the current folder.
451         val sqlEscapedCurrentFolder = DatabaseUtils.sqlEscapeString(currentFolder)
452
453         // Return the cursor with the subfolders.  The cursor can't be closed because it is used in the parent activity.
454         return bookmarksDatabase.rawQuery("SELECT * FROM $BOOKMARKS_TABLE WHERE $PARENT_FOLDER = $sqlEscapedCurrentFolder AND $IS_FOLDER = 1", null)
455     }
456
457     // Check if a database ID is a folder.
458     fun isFolder(databaseId: Int): Boolean {
459         // Get a readable database handle.
460         val bookmarksDatabase = this.readableDatabase
461
462         // Get a cursor with the is folder field for the specified database ID.
463         val folderCursor = bookmarksDatabase.rawQuery("SELECT $IS_FOLDER FROM $BOOKMARKS_TABLE WHERE $ID = $databaseId", null)
464
465         // Move to the first record.
466         folderCursor.moveToFirst()
467
468         // Ascertain if this database ID is a folder.
469         val isFolder = folderCursor.getInt(folderCursor.getColumnIndexOrThrow(IS_FOLDER)) == 1
470
471         // Close the cursor and the database handle.
472         folderCursor.close()
473         bookmarksDatabase.close()
474
475         // Return the folder status.
476         return isFolder
477     }
478
479     // Move one bookmark or folder to a new folder.
480     fun moveToFolder(databaseId: Int, newFolder: String) {
481         // Get a writable database handle.
482         val bookmarksDatabase = this.writableDatabase
483
484         // SQL escape the new folder name.
485         val sqlEscapedNewFolder = DatabaseUtils.sqlEscapeString(newFolder)
486
487         // Get a cursor for all the bookmarks in the new folder ordered by display order.
488         val newFolderCursor = bookmarksDatabase.rawQuery("SELECT * FROM $BOOKMARKS_TABLE WHERE $PARENT_FOLDER = $sqlEscapedNewFolder ORDER BY $DISPLAY_ORDER ASC", null)
489
490         // Set the new display order.
491         val displayOrder: Int = if (newFolderCursor.count > 0) {  // There are already bookmarks in the folder.
492             // Move to the last bookmark.
493             newFolderCursor.moveToLast()
494
495             // Set the display order to be one greater that the last bookmark.
496             newFolderCursor.getInt(newFolderCursor.getColumnIndexOrThrow(DISPLAY_ORDER)) + 1
497         } else {  // There are no bookmarks in the new folder.
498             // Set the display order to be `0`.
499             0
500         }
501
502         // Close the cursor.
503         newFolderCursor.close()
504
505         // Create a content values.
506         val bookmarkContentValues = ContentValues()
507
508         // Store the new values.
509         bookmarkContentValues.put(DISPLAY_ORDER, displayOrder)
510         bookmarkContentValues.put(PARENT_FOLDER, newFolder)
511
512         // Update the database.
513         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, "$ID = $databaseId", null)
514
515         // Close the database handle.
516         bookmarksDatabase.close()
517     }
518
519     // Update the bookmark name and URL.
520     fun updateBookmark(databaseId: Int, bookmarkName: String, bookmarkUrl: String) {
521         // Initialize a content values.
522         val bookmarkContentValues = ContentValues()
523
524         // Store the updated values.
525         bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName)
526         bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl)
527
528         // Get a writable database handle.
529         val bookmarksDatabase = this.writableDatabase
530
531         // Update the bookmark.
532         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, "$ID = $databaseId", null)
533
534         // Close the database handle.
535         bookmarksDatabase.close()
536     }
537
538     // Update the bookmark name, URL, parent folder, and display order.
539     fun updateBookmark(databaseId: Int, bookmarkName: String, bookmarkUrl: String, parentFolder: String, displayOrder: Int) {
540         // Initialize a content values.
541         val bookmarkContentValues = ContentValues()
542
543         // Store the updated values.
544         bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName)
545         bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl)
546         bookmarkContentValues.put(PARENT_FOLDER, parentFolder)
547         bookmarkContentValues.put(DISPLAY_ORDER, displayOrder)
548
549         // Get a writable database handle.
550         val bookmarksDatabase = this.writableDatabase
551
552         // Update the bookmark.
553         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, "$ID = $databaseId", null)
554
555         // Close the database handle.
556         bookmarksDatabase.close()
557     }
558
559     // Update the bookmark name, URL, and favorite icon.
560     fun updateBookmark(databaseId: Int, bookmarkName: String, bookmarkUrl: String, favoriteIcon: ByteArray) {
561         // Initialize a content values.
562         val bookmarkContentValues = ContentValues()
563
564         // Store the updated values.
565         bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName)
566         bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl)
567         bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon)
568
569         // Get a writable database handle.
570         val bookmarksDatabase = this.writableDatabase
571
572         // Update the bookmark.
573         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, "$ID = $databaseId", null)
574
575         // Close the database handle.
576         bookmarksDatabase.close()
577     }
578
579     // Update the bookmark name, URL, parent folder, display order, and favorite icon.
580     fun updateBookmark(databaseId: Int, bookmarkName: String, bookmarkUrl: String, parentFolder: String, displayOrder: Int, favoriteIcon: ByteArray) {
581         // Initialize a content values.
582         val bookmarkContentValues = ContentValues()
583
584         // Store the updated values.
585         bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName)
586         bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl)
587         bookmarkContentValues.put(PARENT_FOLDER, parentFolder)
588         bookmarkContentValues.put(DISPLAY_ORDER, displayOrder)
589         bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon)
590
591         // Get a writable database handle.
592         val bookmarksDatabase = this.writableDatabase
593
594         // Update the bookmark.
595         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, "$ID = $databaseId", null)
596
597         // Close the database handle.
598         bookmarksDatabase.close()
599     }
600
601     // Update the display order for one bookmark or folder.
602     fun updateDisplayOrder(databaseId: Int, displayOrder: Int) {
603         // Get a writable database handle.
604         val bookmarksDatabase = this.writableDatabase
605
606         // Create a content values.
607         val bookmarkContentValues = ContentValues()
608
609         // Store the new display order.
610         bookmarkContentValues.put(DISPLAY_ORDER, displayOrder)
611
612         // Update the database.
613         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, "$ID = $databaseId", null)
614
615         // Close the database handle.
616         bookmarksDatabase.close()
617     }
618
619     // Update the folder name.
620     fun updateFolder(databaseId: Int, oldFolderName: String, newFolderName: String) {
621         // Get a writable database handle.
622         val bookmarksDatabase = this.writableDatabase
623
624         // Create a folder content values.
625         val folderContentValues = ContentValues()
626
627         // Store the new folder name.
628         folderContentValues.put(BOOKMARK_NAME, newFolderName)
629
630         // Run the update on the folder.
631         bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, "$ID = $databaseId", null)
632
633         // Create a bookmark content values.
634         val bookmarkContentValues = ContentValues()
635
636         // Store the new parent folder name.
637         bookmarkContentValues.put(PARENT_FOLDER, newFolderName)
638
639         // SQL escape the old folder name.
640         val sqlEscapedOldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName)
641
642         // Run the update on all the bookmarks that currently list the old folder name as their parent folder.
643         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, "$PARENT_FOLDER = $sqlEscapedOldFolderName", null)
644
645         // Close the database handle.
646         bookmarksDatabase.close()
647     }
648
649     // Update the folder icon.
650     fun updateFolder(databaseId: Int, folderIcon: ByteArray) {
651         // Get a writable database handle.
652         val bookmarksDatabase = this.writableDatabase
653
654         // Create a content values.
655         val folderContentValues = ContentValues()
656
657         // Store the updated icon.
658         folderContentValues.put(FAVORITE_ICON, folderIcon)
659
660         // Run the update on the folder.
661         bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, "$ID = $databaseId", null)
662
663         // Close the database handle.
664         bookmarksDatabase.close()
665     }
666
667     // Update the folder name, parent folder, and display order.
668     fun updateFolder(databaseId: Int, oldFolderName: String, newFolderName: String, parentFolder: String, displayOrder: Int) {
669         // Get a writable database handle.
670         val bookmarksDatabase = this.writableDatabase
671
672         // Create a folder content values.
673         val folderContentValues = ContentValues()
674
675         // Store the new folder values.
676         folderContentValues.put(BOOKMARK_NAME, newFolderName)
677         folderContentValues.put(PARENT_FOLDER, parentFolder)
678         folderContentValues.put(DISPLAY_ORDER, displayOrder)
679
680         // Run the update on the folder.
681         bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, "$ID = $databaseId", null)
682
683         // Create a bookmark content values.
684         val bookmarkContentValues = ContentValues()
685
686         // Store the new parent folder name.
687         bookmarkContentValues.put(PARENT_FOLDER, newFolderName)
688
689         // SQL escape the old folder name.
690         val sqlEscapedOldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName)
691
692         // Run the update on all the bookmarks that currently list the old folder name as their parent folder.
693         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, "$PARENT_FOLDER = $sqlEscapedOldFolderName", null)
694
695         // Close the database handle.
696         bookmarksDatabase.close()
697     }
698
699     // Update the folder name and icon.
700     fun updateFolder(databaseId: Int, oldFolderName: String, newFolderName: String, folderIcon: ByteArray) {
701         // Get a writable database handle.
702         val bookmarksDatabase = this.writableDatabase
703
704         // Create a folder content values.
705         val folderContentValues = ContentValues()
706
707         // Store the updated values.
708         folderContentValues.put(BOOKMARK_NAME, newFolderName)
709         folderContentValues.put(FAVORITE_ICON, folderIcon)
710
711         // Run the update on the folder.
712         bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, "$ID = $databaseId", null)
713
714         // Create a bookmark content values.
715         val bookmarkContentValues = ContentValues()
716
717         // Store the new parent folder name.
718         bookmarkContentValues.put(PARENT_FOLDER, newFolderName)
719
720         // SQL escape the old folder name.
721         val sqlEscapedOldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName)
722
723         // Run the update on all the bookmarks that currently list the old folder name as their parent folder.
724         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, "$PARENT_FOLDER = $sqlEscapedOldFolderName", null)
725
726         // Close the database handle.
727         bookmarksDatabase.close()
728     }
729
730     // Update the folder name and icon.
731     fun updateFolder(databaseId: Int, oldFolderName: String, newFolderName: String, parentFolder: String, displayOrder: Int, folderIcon: ByteArray) {
732         // Get a writable database handle.
733         val bookmarksDatabase = this.writableDatabase
734
735         // Create a folder content values.
736         val folderContentValues = ContentValues()
737
738         // Store the updated values.
739         folderContentValues.put(BOOKMARK_NAME, newFolderName)
740         folderContentValues.put(PARENT_FOLDER, parentFolder)
741         folderContentValues.put(DISPLAY_ORDER, displayOrder)
742         folderContentValues.put(FAVORITE_ICON, folderIcon)
743
744         // Run the update on the folder.
745         bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, "$ID = $databaseId", null)
746
747         // Create a bookmark content values.
748         val bookmarkContentValues = ContentValues()
749
750         // Store the new parent folder name.
751         bookmarkContentValues.put(PARENT_FOLDER, newFolderName)
752
753         // SQL escape the old folder name.
754         val sqlEscapedOldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName)
755
756         // Run the update on all the bookmarks that currently list the old folder name as their parent folder.
757         bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, "$PARENT_FOLDER = $sqlEscapedOldFolderName", null)
758
759         // Close the database handle.
760         bookmarksDatabase.close()
761     }
762 }