2 * Copyright © 2018,2021 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
6 * Privacy Browser 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 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. 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.sqlite.SQLiteDatabase
25 import android.database.sqlite.SQLiteOpenHelper
27 // Define the class constants.
28 private const val SCHEMA_VERSION = 1
29 private const val AD_CONSENT_DATABASE = "ad_consent.db"
30 private const val AD_CONSENT_TABLE = "ad_consent"
31 private const val ID = "_id"
32 private const val AD_CONSENT = "ad_consent"
33 private const val CREATE_AD_CONSENT_TABLE = "CREATE TABLE $AD_CONSENT_TABLE ($ID INTEGER PRIMARY KEY, $AD_CONSENT BOOLEAN)"
35 class AdConsentDatabaseHelper (context: Context) : SQLiteOpenHelper(context, AD_CONSENT_DATABASE, null, SCHEMA_VERSION) {
36 override fun onCreate(adConsentDatabase: SQLiteDatabase) {
37 // Create the ad consent table.
38 adConsentDatabase.execSQL(CREATE_AD_CONSENT_TABLE)
40 // Create an ad consent content values.
41 val adConsentContentValues = ContentValues()
43 // Populate the ad consent content values with the default data.
44 adConsentContentValues.put(AD_CONSENT, false)
46 // Insert a new row. The second argument is `null`, which makes it so that a completely null row cannot be created.
47 adConsentDatabase.insert(AD_CONSENT_TABLE, null, adConsentContentValues)
50 override fun onUpgrade(adConsentDatabase: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
51 // Code for upgrading the database will be added here if the schema version ever increases above 1.
54 // Check to see if ad consent has been granted.
55 val isGranted: Boolean get() {
56 // Get a readable database handle.
57 val adConsentDatabase = this.readableDatabase
59 // Get the ad consent cursor.
60 val adConsentCursor = adConsentDatabase.rawQuery("SELECT * FROM $AD_CONSENT_TABLE", null)
62 // Move the cursor to the first entry.
63 adConsentCursor.moveToFirst()
65 // Get the ad consent boolean.
66 val adConsent = adConsentCursor.getInt(adConsentCursor.getColumnIndex(AD_CONSENT)) == 1
69 adConsentCursor.close()
71 // Close the database.
72 adConsentDatabase.close()
74 // Return the ad consent boolean.
78 // Update the ad consent.
79 fun updateAdConsent(adConsent: Boolean) {
80 // Get a writable database handle.
81 val adConsentDatabase = this.writableDatabase
83 // Set the ad consent integer according to the boolean.
84 val adConsentInt = if (adConsent) 1 else 0
86 // Update the ad consent in the database.
87 adConsentDatabase.execSQL("UPDATE $AD_CONSENT_TABLE SET $AD_CONSENT = $adConsentInt")
89 // Close the database.
90 adConsentDatabase.close()