]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/free/java/com/stoutner/privacybrowser/helpers/AdConsentDatabaseHelper.java
bbe480c1af167cd24b817b031ad26fa3fb9ffc4e
[PrivacyBrowserAndroid.git] / app / src / free / java / com / stoutner / privacybrowser / helpers / AdConsentDatabaseHelper.java
1 /*
2  * Copyright © 2018 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.Cursor;
25 import android.database.sqlite.SQLiteDatabase;
26 import android.database.sqlite.SQLiteOpenHelper;
27
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";
32
33     private static final String _ID = "_id";
34     private static final String AD_CONSENT = "ad_consent";
35
36     private static final String CREATE_AD_CONSENT_TABLE = "CREATE TABLE " + AD_CONSENT_TABLE + " (" +
37             _ID + " INTEGER PRIMARY KEY, " +
38             AD_CONSENT + " BOOLEAN)";
39
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);
43     }
44
45     @Override
46     public void onCreate(SQLiteDatabase adConsentDatabase) {
47         // Create the ad consent database.
48         adConsentDatabase.execSQL(CREATE_AD_CONSENT_TABLE);
49
50         // Create an ad consent ContentValues.
51         ContentValues adConsentContentValues = new ContentValues();
52
53         // Populate the ad consent content values.
54         adConsentContentValues.put(AD_CONSENT, false);
55
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);
58     }
59
60     @Override
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.
63     }
64
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();
69
70         // Get the ad consent cursor.
71         Cursor adConsentCursor = adConsentDatabase.rawQuery("SELECT * FROM " + AD_CONSENT_TABLE, null);
72
73         // Move to the first entry.
74         adConsentCursor.moveToFirst();
75
76         // Get the ad consent boolean.
77         boolean adConsent = (adConsentCursor.getInt(adConsentCursor.getColumnIndex(AD_CONSENT)) == 1);
78
79         // Close the cursor.
80         adConsentCursor.close();
81
82         // Close the database.
83         adConsentDatabase.close();
84
85         // Return the ad consent boolean.
86         return adConsent;
87     }
88
89     // Update the ad consent.
90     public void updateAdConsent(boolean adConsent) {
91         // Get a writable database handle.
92         SQLiteDatabase adConsentDatabase = this.getWritableDatabase();
93
94         // Create an ad consent integer.
95         int adConsentInt;
96
97         // Set the ad consent integer according to the boolean.
98         if (adConsent) {
99             adConsentInt = 1;
100         } else {
101             adConsentInt = 0;
102         }
103
104         // Update the ad consent in the database.
105         adConsentDatabase.execSQL("UPDATE " + AD_CONSENT_TABLE + " SET " + AD_CONSENT + " = " + adConsentInt);
106
107         // Close the database.
108         adConsentDatabase.close();
109     }
110 }