]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/helpers/DomainsDatabaseHelper.java
Add controls for displaying webpage images. Implements https://redmine.stoutner...
[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 = 2;
30     private static final String DOMAINS_DATABASE = "domains.db";
31     private static final String DOMAINS_TABLE = "domains";
32
33     public static final String _ID = "_id";
34     public static final String DOMAIN_NAME = "domainname";
35     public static final String ENABLE_JAVASCRIPT = "enablejavascript";
36     public static final String ENABLE_FIRST_PARTY_COOKIES = "enablefirstpartycookies";
37     public static final String ENABLE_THIRD_PARTY_COOKIES = "enablethirdpartycookies";
38     public static final String ENABLE_DOM_STORAGE = "enabledomstorage";
39     public static final String ENABLE_FORM_DATA = "enableformdata";
40     public static final String USER_AGENT = "useragent";
41     public static final String FONT_SIZE = "fontsize";
42     public static final String DISPLAY_IMAGES = "displayimages";
43
44     public static final int DISPLAY_WEBPAGE_IMAGES_SYSTEM_DEFAULT = 0;
45     public static final int DISPLAY_WEBPAGE_IMAGES_ENABLED = 1;
46     public static final int DISPLAY_WEBPAGE_IMAGES_DISABLED = 2;
47
48     // Initialize the database.  The lint warnings for the unused parameters are suppressed.
49     public DomainsDatabaseHelper(Context context, @SuppressWarnings("UnusedParameters") String name, SQLiteDatabase.CursorFactory cursorFactory, @SuppressWarnings("UnusedParameters") int version) {
50         super(context, DOMAINS_DATABASE, cursorFactory, SCHEMA_VERSION);
51     }
52
53     @Override
54     public void onCreate(SQLiteDatabase domainsDatabase) {
55         // Setup the SQL string to create the `domains` table.
56         final String CREATE_DOMAINS_TABLE = "CREATE TABLE " + DOMAINS_TABLE + " (" +
57                 _ID + " INTEGER PRIMARY KEY, " +
58                 DOMAIN_NAME + " TEXT, " +
59                 ENABLE_JAVASCRIPT + " BOOLEAN, " +
60                 ENABLE_FIRST_PARTY_COOKIES + " BOOLEAN, " +
61                 ENABLE_THIRD_PARTY_COOKIES + " BOOLEAN, " +
62                 ENABLE_DOM_STORAGE + " BOOLEAN, " +
63                 ENABLE_FORM_DATA + " BOOLEAN, " +
64                 USER_AGENT + " TEXT, " +
65                 FONT_SIZE + " INTEGER, " +
66                 DISPLAY_IMAGES + " INTEGER);";
67
68         // Create the `domains` table if it doesn't exist.
69         domainsDatabase.execSQL(CREATE_DOMAINS_TABLE);
70     }
71
72     @Override
73     public void onUpgrade(SQLiteDatabase domainsDatabase, int oldVersion, int newVersion) {
74         // Upgrade `DOMAINS_TABLE`.
75         switch (oldVersion) {
76             // Upgrade from `SCHEMA_VERSION` 1.
77             case 1:
78                 // Add the `DISPLAY_IMAGES` column.
79                 domainsDatabase.execSQL("ALTER TABLE " + DOMAINS_TABLE + " ADD COLUMN " + DISPLAY_IMAGES + " INTEGER");
80         }
81     }
82
83     public Cursor getDomainNameCursorOrderedByDomain() {
84         // Get a readable database handle.
85         SQLiteDatabase domainsDatabase = this.getReadableDatabase();
86
87         // Get everything in `DOMAINS_TABLE` ordered by `DOMAIN_NAME`.
88         final String GET_CURSOR_ORDERED_BY_DOMAIN = "SELECT " + _ID + ", " + DOMAIN_NAME +
89                 " FROM " + DOMAINS_TABLE +
90                 " ORDER BY " + DOMAIN_NAME + " ASC";
91
92         // 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.
93         return domainsDatabase.rawQuery(GET_CURSOR_ORDERED_BY_DOMAIN, null);
94     }
95
96     public Cursor getDomainNameCursorOrderedByDomainExcept(int databaseId) {
97         // Get a readable database handle.
98         SQLiteDatabase domainsDatabase = this.getReadableDatabase();
99
100         // Prepare the SQL statement to select all rows except that with `databaseId`.
101         final String GET_CURSOR_ORDERED_BY_DOMAIN_EXCEPT = "SELECT " + _ID + ", " + DOMAIN_NAME +
102                 " FROM " + DOMAINS_TABLE +
103                 " WHERE " + _ID + " IS NOT " + databaseId +
104                 " ORDER BY " + DOMAIN_NAME + " ASC";
105
106         // 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 calling activity.
107         return domainsDatabase.rawQuery(GET_CURSOR_ORDERED_BY_DOMAIN_EXCEPT, null);
108     }
109
110     public Cursor getCursorForId(int databaseId) {
111         // Get a readable database handle.
112         SQLiteDatabase domainsDatabase = this.getReadableDatabase();
113
114         // Prepare the SQL statement to get the `Cursor` for `databaseId`.
115         final String GET_CURSOR_FOR_ID = "SELECT * FROM " + DOMAINS_TABLE +
116                 " WHERE " + _ID + " = " + databaseId;
117
118         // 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 calling activity.
119         return domainsDatabase.rawQuery(GET_CURSOR_FOR_ID, null);
120     }
121
122     public Cursor getCursorForDomainName(String domainName) {
123         // Get a readable database handle.
124         SQLiteDatabase domainsDatabase = this.getReadableDatabase();
125
126         // Prepare the SQL statement to get the `Cursor` for `domainName`.
127         final String GET_CURSOR_FOR_DOMAIN_NAME = "SELECT * FROM " + DOMAINS_TABLE +
128                 " WHERE " + DOMAIN_NAME + " = " + "\"" + domainName + "\"";
129
130         // 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 us it in the calling activity.
131         return domainsDatabase.rawQuery(GET_CURSOR_FOR_DOMAIN_NAME, null);
132     }
133
134     public void addDomain(String domainName) {
135         // Store the domain data in a `ContentValues`.
136         ContentValues domainContentValues = new ContentValues();
137
138         // Create entries for each field in the database.  The ID is created automatically.
139         domainContentValues.put(DOMAIN_NAME, domainName);
140         domainContentValues.put(ENABLE_JAVASCRIPT, false);
141         domainContentValues.put(ENABLE_FIRST_PARTY_COOKIES, false);
142         domainContentValues.put(ENABLE_THIRD_PARTY_COOKIES, false);
143         domainContentValues.put(ENABLE_DOM_STORAGE, false);
144         domainContentValues.put(ENABLE_FORM_DATA, false);
145         domainContentValues.put(USER_AGENT, "PrivacyBrowser/1.0");
146         domainContentValues.put(FONT_SIZE, "100");
147         domainContentValues.put(DISPLAY_IMAGES, 0);
148
149         // Get a writable database handle.
150         SQLiteDatabase domainsDatabase = this.getWritableDatabase();
151
152         // Insert a new row.  The second argument is `null`, which makes it so that a completely null row cannot be created.
153         domainsDatabase.insert(DOMAINS_TABLE, null, domainContentValues);
154
155         // Close the database handle.
156         domainsDatabase.close();
157     }
158
159     public void saveDomain(int databaseId, String domainName, boolean javaScriptEnabled, boolean firstPartyCookiesEnabled, boolean thirdPartyCookiesEnabled, boolean domStorageEnabled, boolean formDataEnabled, String userAgent, int fontSize,
160                            int displayImages) {
161         // Store the domain data in a `ContentValues`.
162         ContentValues domainContentValues = new ContentValues();
163
164         // Add entries for each field in the database.
165         domainContentValues.put(DOMAIN_NAME, domainName);
166         domainContentValues.put(ENABLE_JAVASCRIPT, javaScriptEnabled);
167         domainContentValues.put(ENABLE_FIRST_PARTY_COOKIES, firstPartyCookiesEnabled);
168         domainContentValues.put(ENABLE_THIRD_PARTY_COOKIES, thirdPartyCookiesEnabled);
169         domainContentValues.put(ENABLE_DOM_STORAGE, domStorageEnabled);
170         domainContentValues.put(ENABLE_FORM_DATA, formDataEnabled);
171         domainContentValues.put(USER_AGENT, userAgent);
172         domainContentValues.put(FONT_SIZE, fontSize);
173         domainContentValues.put(DISPLAY_IMAGES, displayImages);
174
175         // Get a writable database handle.
176         SQLiteDatabase domainsDatabase = this.getWritableDatabase();
177
178         // Update the row for `databaseId`.  The last argument is `null` because there are no `whereArgs`.
179         domainsDatabase.update(DOMAINS_TABLE, domainContentValues, _ID + " = " + databaseId, null);
180
181         // Close the database handle.
182         domainsDatabase.close();
183     }
184
185     public void deleteDomain(int databaseId) {
186         // Get a writable database handle.
187         SQLiteDatabase domainsDatabase = this.getWritableDatabase();
188
189         // Delete the row for `databaseId`.  The last argument is `null` because we don't need additional parameters.
190         domainsDatabase.delete(DOMAINS_TABLE, _ID + " = " + databaseId, null);
191
192         // Close the database handle.
193         domainsDatabase.close();
194     }
195 }