]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/helpers/DomainsDatabaseHelper.java
1a5c066b94baecf270ca0aba1a730f1ac936084e
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / helpers / DomainsDatabaseHelper.java
1 /*
2  * Copyright 2017 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 DomainsDatabaseHelper extends SQLiteOpenHelper {
29     private static final int SCHEMA_VERSION = 1;
30     private static final String DOMAINS_DATABASE = "domains.db";
31     private static final String DOMAINS_TABLE = "domains";
32     private static final String _ID = "_id";
33
34     public static final String DOMAIN = "domain";
35
36
37     // Initialize the database.  The lint warnings for the unused parameters are suppressed.
38     public DomainsDatabaseHelper(Context context, @SuppressWarnings("UnusedParameters") String name, SQLiteDatabase.CursorFactory cursorFactory, @SuppressWarnings("UnusedParameters") int version) {
39         super(context, DOMAINS_DATABASE, cursorFactory, SCHEMA_VERSION);
40     }
41
42     @Override
43     public void onCreate(SQLiteDatabase domainsDatabase) {
44         // Setup the SQL string to create the `domains` table.
45         final String CREATE_DOMAINS_TABLE = "CREATE TABLE " + DOMAINS_TABLE + " (" +
46                 _ID + " integer primary key, " +
47                 DOMAIN + " text);";
48
49         // Create the `domains` table if it doesn't exist.
50         domainsDatabase.execSQL(CREATE_DOMAINS_TABLE);
51     }
52
53     @Override
54     public void onUpgrade(SQLiteDatabase domainsDatabase, int oldVersion, int newVersion) {
55         // Code for upgrading the database will be added here when the schema version > 1.
56     }
57
58     public Cursor getCursorOrderedByDomain() {
59         // Get a readable database handle.
60         SQLiteDatabase domainsDatabase = this.getReadableDatabase();
61
62         // Get everything in `DOMAINS_TABLE` ordered by `DOMAIN`.
63         final String GET_CURSOR_SORTED_BY_DOMAIN = "Select * FROM " + DOMAINS_TABLE +
64                 " ORDER BY " + DOMAIN + " ASC";
65
66         // Return the results as a `Cursor`.  The second argument is `null` because there are no `selectionArgs`.  We can't close the `Cursor` because we need to use it in the parent activity.
67         return domainsDatabase.rawQuery(GET_CURSOR_SORTED_BY_DOMAIN, null);
68     }
69
70     public Cursor getCursorForId(int databaseId) {
71         // Get a readable database handle.
72         SQLiteDatabase domainsDatabase = this.getReadableDatabase();
73
74         // Prepare the SQL statement to ge the `Cursor` for `databaseId`.
75         final String GET_CURSOR_FOR_ID = "Select * FROM " + DOMAINS_TABLE +
76                 " WHERE " + _ID + " = " + databaseId;
77
78         // Return the results as a `Cursor`.  The second argument is `null` because there are no `selectionArgs`.  We can't close the `Cursor` because we need to use it in the parent activity.
79         return domainsDatabase.rawQuery(GET_CURSOR_FOR_ID, null);
80     }
81
82     public void addDomain(String domainName) {
83         // We need to store the domain data in a `ContentValues`.
84         ContentValues domainContentValues = new ContentValues();
85
86         // ID is created automatically.
87         domainContentValues.put(DOMAIN, domainName);
88
89         // Get a writable database handle.
90         SQLiteDatabase domainsDatabase = this.getWritableDatabase();
91
92         // Insert a new row.  The second argument is `null`, which makes it so that a completely null row cannot be created.
93         domainsDatabase.insert(DOMAINS_TABLE, null, domainContentValues);
94
95         // Close the database handle.
96         domainsDatabase.close();
97     }
98 }