2 * Copyright 2016-2022 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser Android <https://www.stoutner.com/privacy-browser-android>.
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.
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.
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/>.
20 package com.stoutner.privacybrowser.helpers
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
29 // Define the class constants.
30 private const val SCHEMA_VERSION = 1
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.
35 // Define the public database constants.
36 const val BOOKMARKS_DATABASE = "bookmarks.db"
37 const val BOOKMARKS_TABLE = "bookmarks"
39 // Define the public schema constants.
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"
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)"
59 override fun onCreate(bookmarksDatabase: SQLiteDatabase) {
60 // Create the bookmarks table.
61 bookmarksDatabase.execSQL(CREATE_BOOKMARKS_TABLE)
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.
68 // Get a cursor of all the folders.
69 val allFolders: Cursor
71 // Get a readable database handle.
72 val bookmarksDatabase = this.readableDatabase
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)
78 // Get a cursor for all bookmarks and folders.
79 val allBookmarks: Cursor
81 // Get a readable database handle.
82 val bookmarksDatabase = this.readableDatabase
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)
88 // Get a cursor for all bookmarks and folders ordered by display order.
89 val allBookmarksByDisplayOrder: Cursor
91 // Get a readable database handle.
92 val bookmarksDatabase = this.readableDatabase
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)
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()
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)
111 // Get a writable database handle.
112 val bookmarksDatabase = this.writableDatabase
115 bookmarksDatabase.insert(BOOKMARKS_TABLE, null, bookmarkContentValues)
117 // Close the database handle.
118 bookmarksDatabase.close()
121 // Create a bookmark from content values.
122 fun createBookmark(contentValues: ContentValues) {
123 // Get a writable database.
124 val bookmarksDatabase = this.writableDatabase
127 bookmarksDatabase.insert(BOOKMARKS_TABLE, null, contentValues)
129 // Close the database handle.
130 bookmarksDatabase.close()
134 fun createFolder(folderName: String, parentFolder: String, favoriteIcon: ByteArray) {
135 // Store the bookmark folder data in a content values.
136 val bookmarkFolderContentValues = ContentValues()
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)
145 // Get a writable database handle.
146 val bookmarksDatabase = this.writableDatabase
148 // Insert the new folder.
149 bookmarksDatabase.insert(BOOKMARKS_TABLE, null, bookmarkFolderContentValues)
151 // Close the database handle.
152 bookmarksDatabase.close()
155 // Delete one bookmark.
156 fun deleteBookmark(databaseId: Int) {
157 // Get a writable database handle.
158 val bookmarksDatabase = this.writableDatabase
160 // Deletes the row with the given database ID.
161 bookmarksDatabase.delete(BOOKMARKS_TABLE, "$ID = $databaseId", null)
163 // Close the database handle.
164 bookmarksDatabase.close()
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
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)
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
181 // Prepare a string builder to contain the comma-separated list of IDs not to get.
182 val idsNotToGetStringBuilder = StringBuilder()
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(",")
192 // Add the new number to the builder.
193 idsNotToGetStringBuilder.append(databaseIdLong)
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)
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
205 // Prepare a string builder to contain the comma-separated list of IDs not to get.
206 val idsNotToGetStringBuilder = StringBuilder()
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(",")
216 // Add the new number to the builder.
217 idsNotToGetStringBuilder.append(databaseIdLong)
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)
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
229 // SQL escape the folder name.
230 val sqlEscapedFolderName = DatabaseUtils.sqlEscapeString(folderName)
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)
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
241 // SQL escape the folder name.
242 val sqlEscapedFolderName = DatabaseUtils.sqlEscapeString(folderName)
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)
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
253 // SQL escape the folder name.
254 val sqlEscapedFolderName = DatabaseUtils.sqlEscapeString(folderName)
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)
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
265 // Prepare a string builder to contain the comma-separated list of IDs not to get.
266 val idsNotToGetStringBuilder = StringBuilder()
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(",")
276 // Add the new number to the builder.
277 idsNotToGetStringBuilder.append(databaseIdLong)
280 // SQL escape the folder name.
281 val sqlEscapedFolderName = DatabaseUtils.sqlEscapeString(folderName)
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",
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
294 // Prepare a string builder to contain the comma-separated list of IDs not to get.
295 val idsNotToGetStringBuilder = StringBuilder()
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(",")
305 // Add the new number to the builder.
306 idsNotToGetStringBuilder.append(databaseIdLong)
309 // SQL escape the folder name.
310 val sqlEscapedFolderName = DatabaseUtils.sqlEscapeString(folderName)
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)
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
321 // SQL escape the folder name.
322 val sqlEscapedFolderName = DatabaseUtils.sqlEscapeString(folderName)
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)
328 fun getFolderBookmarks(folderDatabaseId: Int): Cursor {
329 // Get the folder name.
330 val folderName = getFolderName(folderDatabaseId)
332 // SQL escape the folder name.
333 val sqlEscapedFolderName = DatabaseUtils.sqlEscapeString(folderName)
335 // Get a readable database handle.
336 val bookmarksDatabase = this.readableDatabase
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)
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
347 // SQL escape the folder name.
348 val sqlEscapedFolderName = DatabaseUtils.sqlEscapeString(folderName)
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)
353 // Move to the first record.
354 folderCursor.moveToFirst()
356 // Get the database ID.
357 val databaseId = folderCursor.getInt(folderCursor.getColumnIndexOrThrow(ID))
359 // Close the cursor and the database handle.
361 bookmarksDatabase.close()
363 // Return the database ID.
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
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)
375 // Move to the first record.
376 folderCursor.moveToFirst()
378 // Get the folder name.
379 val folderName = folderCursor.getString(folderCursor.getColumnIndexOrThrow(BOOKMARK_NAME))
381 // Close the cursor and the database handle.
383 bookmarksDatabase.close()
385 // Return the folder name.
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
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)
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
403 // SQL escape the current folder.
404 val sqlEscapedCurrentFolder = DatabaseUtils.sqlEscapeString(currentFolder)
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)
409 // Move to the first record.
410 bookmarkCursor.moveToFirst()
412 // Store the name of the parent folder.
413 val parentFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndexOrThrow(PARENT_FOLDER))
415 // Close the cursor and the database.
416 bookmarkCursor.close()
417 bookmarksDatabase.close()
419 // Return the parent folder string.
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
428 // Get a cursor for the specified database ID.
429 val bookmarkCursor = bookmarksDatabase.rawQuery("SELECT * FROM $BOOKMARKS_TABLE WHERE $ID = $databaseId", null)
431 // Move to the first record.
432 bookmarkCursor.moveToFirst()
434 // Store the name of the parent folder.
435 val parentFolder = bookmarkCursor.getString(bookmarkCursor.getColumnIndexOrThrow(PARENT_FOLDER))
437 // Close the cursor and the database.
438 bookmarkCursor.close()
439 bookmarksDatabase.close()
441 // Return the parent folder string.
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
450 // SQL escape the current folder.
451 val sqlEscapedCurrentFolder = DatabaseUtils.sqlEscapeString(currentFolder)
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)
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
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)
465 // Move to the first record.
466 folderCursor.moveToFirst()
468 // Ascertain if this database ID is a folder.
469 val isFolder = folderCursor.getInt(folderCursor.getColumnIndexOrThrow(IS_FOLDER)) == 1
471 // Close the cursor and the database handle.
473 bookmarksDatabase.close()
475 // Return the folder status.
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
484 // SQL escape the new folder name.
485 val sqlEscapedNewFolder = DatabaseUtils.sqlEscapeString(newFolder)
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)
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()
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`.
503 newFolderCursor.close()
505 // Create a content values.
506 val bookmarkContentValues = ContentValues()
508 // Store the new values.
509 bookmarkContentValues.put(DISPLAY_ORDER, displayOrder)
510 bookmarkContentValues.put(PARENT_FOLDER, newFolder)
512 // Update the database.
513 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, "$ID = $databaseId", null)
515 // Close the database handle.
516 bookmarksDatabase.close()
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()
524 // Store the updated values.
525 bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName)
526 bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl)
528 // Get a writable database handle.
529 val bookmarksDatabase = this.writableDatabase
531 // Update the bookmark.
532 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, "$ID = $databaseId", null)
534 // Close the database handle.
535 bookmarksDatabase.close()
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()
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)
549 // Get a writable database handle.
550 val bookmarksDatabase = this.writableDatabase
552 // Update the bookmark.
553 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, "$ID = $databaseId", null)
555 // Close the database handle.
556 bookmarksDatabase.close()
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()
564 // Store the updated values.
565 bookmarkContentValues.put(BOOKMARK_NAME, bookmarkName)
566 bookmarkContentValues.put(BOOKMARK_URL, bookmarkUrl)
567 bookmarkContentValues.put(FAVORITE_ICON, favoriteIcon)
569 // Get a writable database handle.
570 val bookmarksDatabase = this.writableDatabase
572 // Update the bookmark.
573 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, "$ID = $databaseId", null)
575 // Close the database handle.
576 bookmarksDatabase.close()
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()
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)
591 // Get a writable database handle.
592 val bookmarksDatabase = this.writableDatabase
594 // Update the bookmark.
595 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, "$ID = $databaseId", null)
597 // Close the database handle.
598 bookmarksDatabase.close()
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
606 // Create a content values.
607 val bookmarkContentValues = ContentValues()
609 // Store the new display order.
610 bookmarkContentValues.put(DISPLAY_ORDER, displayOrder)
612 // Update the database.
613 bookmarksDatabase.update(BOOKMARKS_TABLE, bookmarkContentValues, "$ID = $databaseId", null)
615 // Close the database handle.
616 bookmarksDatabase.close()
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
624 // Create a folder content values.
625 val folderContentValues = ContentValues()
627 // Store the new folder name.
628 folderContentValues.put(BOOKMARK_NAME, newFolderName)
630 // Run the update on the folder.
631 bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, "$ID = $databaseId", null)
633 // Create a bookmark content values.
634 val bookmarkContentValues = ContentValues()
636 // Store the new parent folder name.
637 bookmarkContentValues.put(PARENT_FOLDER, newFolderName)
639 // SQL escape the old folder name.
640 val sqlEscapedOldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName)
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)
645 // Close the database handle.
646 bookmarksDatabase.close()
649 // Update the folder icon.
650 fun updateFolder(databaseId: Int, folderIcon: ByteArray) {
651 // Get a writable database handle.
652 val bookmarksDatabase = this.writableDatabase
654 // Create a content values.
655 val folderContentValues = ContentValues()
657 // Store the updated icon.
658 folderContentValues.put(FAVORITE_ICON, folderIcon)
660 // Run the update on the folder.
661 bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, "$ID = $databaseId", null)
663 // Close the database handle.
664 bookmarksDatabase.close()
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
672 // Create a folder content values.
673 val folderContentValues = ContentValues()
675 // Store the new folder values.
676 folderContentValues.put(BOOKMARK_NAME, newFolderName)
677 folderContentValues.put(PARENT_FOLDER, parentFolder)
678 folderContentValues.put(DISPLAY_ORDER, displayOrder)
680 // Run the update on the folder.
681 bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, "$ID = $databaseId", null)
683 // Create a bookmark content values.
684 val bookmarkContentValues = ContentValues()
686 // Store the new parent folder name.
687 bookmarkContentValues.put(PARENT_FOLDER, newFolderName)
689 // SQL escape the old folder name.
690 val sqlEscapedOldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName)
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)
695 // Close the database handle.
696 bookmarksDatabase.close()
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
704 // Create a folder content values.
705 val folderContentValues = ContentValues()
707 // Store the updated values.
708 folderContentValues.put(BOOKMARK_NAME, newFolderName)
709 folderContentValues.put(FAVORITE_ICON, folderIcon)
711 // Run the update on the folder.
712 bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, "$ID = $databaseId", null)
714 // Create a bookmark content values.
715 val bookmarkContentValues = ContentValues()
717 // Store the new parent folder name.
718 bookmarkContentValues.put(PARENT_FOLDER, newFolderName)
720 // SQL escape the old folder name.
721 val sqlEscapedOldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName)
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)
726 // Close the database handle.
727 bookmarksDatabase.close()
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
735 // Create a folder content values.
736 val folderContentValues = ContentValues()
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)
744 // Run the update on the folder.
745 bookmarksDatabase.update(BOOKMARKS_TABLE, folderContentValues, "$ID = $databaseId", null)
747 // Create a bookmark content values.
748 val bookmarkContentValues = ContentValues()
750 // Store the new parent folder name.
751 bookmarkContentValues.put(PARENT_FOLDER, newFolderName)
753 // SQL escape the old folder name.
754 val sqlEscapedOldFolderName = DatabaseUtils.sqlEscapeString(oldFolderName)
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)
759 // Close the database handle.
760 bookmarksDatabase.close()