]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/free/java/com/stoutner/privacybrowser/helpers/AdConsentDatabaseHelper.kt
Release 3.9.
[PrivacyBrowserAndroid.git] / app / src / free / java / com / stoutner / privacybrowser / helpers / AdConsentDatabaseHelper.kt
1 /*
2  * Copyright © 2018,2021 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 package com.stoutner.privacybrowser.helpers
21
22 import android.content.ContentValues
23 import android.content.Context
24 import android.database.sqlite.SQLiteDatabase
25 import android.database.sqlite.SQLiteOpenHelper
26
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)"
34
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)
39
40         // Create an ad consent content values.
41         val adConsentContentValues = ContentValues()
42
43         // Populate the ad consent content values with the default data.
44         adConsentContentValues.put(AD_CONSENT, false)
45
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)
48     }
49
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.
52     }
53
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
58
59         // Get the ad consent cursor.
60         val adConsentCursor = adConsentDatabase.rawQuery("SELECT * FROM $AD_CONSENT_TABLE", null)
61
62         // Move the cursor to the first entry.
63         adConsentCursor.moveToFirst()
64
65         // Get the ad consent boolean.
66         val adConsent = adConsentCursor.getInt(adConsentCursor.getColumnIndex(AD_CONSENT)) == 1
67
68         // Close the cursor.
69         adConsentCursor.close()
70
71         // Close the database.
72         adConsentDatabase.close()
73
74         // Return the ad consent boolean.
75         return adConsent
76     }
77
78     // Update the ad consent.
79     fun updateAdConsent(adConsent: Boolean) {
80         // Get a writable database handle.
81         val adConsentDatabase = this.writableDatabase
82
83         // Set the ad consent integer according to the boolean.
84         val adConsentInt = if (adConsent) 1 else 0
85
86         // Update the ad consent in the database.
87         adConsentDatabase.execSQL("UPDATE $AD_CONSENT_TABLE SET $AD_CONSENT = $adConsentInt")
88
89         // Close the database.
90         adConsentDatabase.close()
91     }
92 }