2 * Copyright 2017 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 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";
34 public static final String DOMAIN = "domain";
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);
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, " +
49 // Create the `domains` table if it doesn't exist.
50 domainsDatabase.execSQL(CREATE_DOMAINS_TABLE);
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.
58 public Cursor getCursorOrderedByDomain() {
59 // Get a readable database handle.
60 SQLiteDatabase domainsDatabase = this.getReadableDatabase();
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";
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);
70 public Cursor getCursorForId(int databaseId) {
71 // Get a readable database handle.
72 SQLiteDatabase domainsDatabase = this.getReadableDatabase();
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;
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);
82 public void addDomain(String domainName) {
83 // We need to store the domain data in a `ContentValues`.
84 ContentValues domainContentValues = new ContentValues();
86 // ID is created automatically.
87 domainContentValues.put(DOMAIN, domainName);
89 // Get a writable database handle.
90 SQLiteDatabase domainsDatabase = this.getWritableDatabase();
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);
95 // Close the database handle.
96 domainsDatabase.close();