2 * Copyright © 2018 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.Cursor;
25 import android.database.sqlite.SQLiteDatabase;
26 import android.database.sqlite.SQLiteOpenHelper;
28 public class AdConsentDatabaseHelper extends SQLiteOpenHelper {
29 private static final int SCHEMA_VERSION = 1;
30 private static final String AD_CONSENT_DATABASE = "ad_consent.db";
31 private static final String AD_CONSENT_TABLE = "ad_consent";
33 private static final String _ID = "_id";
34 private static final String AD_CONSENT = "ad_consent";
36 private static final String CREATE_AD_CONSENT_TABLE = "CREATE TABLE " + AD_CONSENT_TABLE + " (" +
37 _ID + " INTEGER PRIMARY KEY, " +
38 AD_CONSENT + " BOOLEAN)";
40 // Initialize the database. The lint warnings for the unused parameters are suppressed.
41 public AdConsentDatabaseHelper(Context context, @SuppressWarnings("UnusedParameters") String name, SQLiteDatabase.CursorFactory cursorFactory, @SuppressWarnings("UnusedParameters") int version) {
42 super(context, AD_CONSENT_DATABASE, cursorFactory, SCHEMA_VERSION);
46 public void onCreate(SQLiteDatabase adConsentDatabase) {
47 // Create the ad consent database.
48 adConsentDatabase.execSQL(CREATE_AD_CONSENT_TABLE);
50 // Create an ad consent ContentValues.
51 ContentValues adConsentContentValues = new ContentValues();
53 // Populate the ad consent content values.
54 adConsentContentValues.put(AD_CONSENT, false);
56 // Insert a new row. The second argument is `null`, which makes it so that a completely null row cannot be created.
57 adConsentDatabase.insert(AD_CONSENT_TABLE, null, adConsentContentValues);
61 public void onUpgrade(SQLiteDatabase adConsentDatabase, int oldVersion, int newVersion) {
62 // Code for upgrading the database will be added here if the schema version ever increases above 1.
65 // Check to see if ad consent has been granted.
66 public boolean isGranted() {
67 // Get a readable database handle.
68 SQLiteDatabase adConsentDatabase = this.getReadableDatabase();
70 // Get the ad consent cursor.
71 Cursor adConsentCursor = adConsentDatabase.rawQuery("SELECT * FROM " + AD_CONSENT_TABLE, null);
73 // Move to the first entry.
74 adConsentCursor.moveToFirst();
76 // Get the ad consent boolean.
77 boolean adConsent = (adConsentCursor.getInt(adConsentCursor.getColumnIndex(AD_CONSENT)) == 1);
80 adConsentCursor.close();
82 // Close the database.
83 adConsentDatabase.close();
85 // Return the ad consent boolean.
89 // Update the ad consent.
90 public void updateAdConsent(boolean adConsent) {
91 // Get a writable database handle.
92 SQLiteDatabase adConsentDatabase = this.getWritableDatabase();
94 // Create an ad consent integer.
97 // Set the ad consent integer according to the boolean.
104 // Update the ad consent in the database.
105 adConsentDatabase.execSQL("UPDATE " + AD_CONSENT_TABLE + " SET " + AD_CONSENT + " = " + adConsentInt);
107 // Close the database.
108 adConsentDatabase.close();