2 * Copyright © 2018-2019 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.content.SharedPreferences;
25 import android.database.Cursor;
26 import android.database.sqlite.SQLiteDatabase;
27 import android.preference.PreferenceManager;
29 import com.stoutner.privacybrowser.R;
32 import java.io.FileInputStream;
33 import java.io.FileOutputStream;
34 import java.io.InputStream;
35 import java.io.OutputStream;
37 public class ImportExportDatabaseHelper {
38 public static final String EXPORT_SUCCESSFUL = "Export Successful";
39 public static final String IMPORT_SUCCESSFUL = "Import Successful";
41 private static final int SCHEMA_VERSION = 3;
42 private static final String PREFERENCES_TABLE = "preferences";
44 // The preferences constants.
45 private static final String _ID = "_id";
46 private static final String JAVASCRIPT = "javascript";
47 private static final String FIRST_PARTY_COOKIES = "first_party_cookies";
48 private static final String THIRD_PARTY_COOKIES = "third_party_cookies";
49 private static final String DOM_STORAGE = "dom_storage";
50 private static final String SAVE_FORM_DATA = "save_form_data";
51 private static final String USER_AGENT = "user_agent";
52 private static final String CUSTOM_USER_AGENT = "custom_user_agent";
53 private static final String INCOGNITO_MODE = "incognito_mode";
54 private static final String DO_NOT_TRACK = "do_not_track";
55 private static final String ALLOW_SCREENSHOTS = "allow_screenshots";
56 private static final String EASYLIST = "easylist";
57 private static final String EASYPRIVACY = "easyprivacy";
58 private static final String FANBOYS_ANNOYANCE_LIST = "fanboys_annoyance_list";
59 private static final String FANBOYS_SOCIAL_BLOCKING_LIST = "fanboys_social_blocking_list";
60 private static final String ULTRAPRIVACY = "ultraprivacy";
61 private static final String BLOCK_ALL_THIRD_PARTY_REQUESTS = "block_all_third_party_requests";
62 private static final String PROXY_THROUGH_ORBOT = "proxy_through_orbot";
63 private static final String TOR_HOMEPAGE = "tor_homepage";
64 private static final String TOR_SEARCH = "tor_search";
65 private static final String TOR_SEARCH_CUSTOM_URL = "tor_search_custom_url";
66 private static final String SEARCH = "search";
67 private static final String SEARCH_CUSTOM_URL = "search_custom_url";
68 private static final String FULL_SCREEN_BROWSING_MODE = "full_screen_browsing_mode";
69 private static final String HIDE_SYSTEM_BARS = "hide_system_bars";
70 private static final String TRANSLUCENT_NAVIGATION_BAR = "translucent_navigation_bar";
71 private static final String CLEAR_EVERYTHING = "clear_everything";
72 private static final String CLEAR_COOKIES = "clear_cookies";
73 private static final String CLEAR_DOM_STORAGE = "clear_dom_storage";
74 private static final String CLEAR_FORM_DATA = "clear_form_data";
75 private static final String CLEAR_CACHE = "clear_cache";
76 private static final String HOMEPAGE = "homepage";
77 private static final String FONT_SIZE = "font_size";
78 private static final String SWIPE_TO_REFRESH = "swipe_to_refresh";
79 private static final String DOWNLOAD_WITH_EXTERNAL_APP = "download_with_external_app";
80 private static final String DISPLAY_ADDITIONAL_APP_BAR_ICONS = "display_additional_app_bar_icons";
81 private static final String DARK_THEME = "dark_theme";
82 private static final String NIGHT_MODE = "night_mode";
83 private static final String DISPLAY_WEBPAGE_IMAGES = "display_webpage_images";
85 public String exportUnencrypted(File exportFile, Context context) {
87 // Delete the current file if it exists.
88 if (exportFile.exists()) {
89 //noinspection ResultOfMethodCallIgnored
93 // Create the export database.
94 SQLiteDatabase exportDatabase = SQLiteDatabase.openOrCreateDatabase(exportFile, null);
96 // Set the export database version number.
97 exportDatabase.setVersion(SCHEMA_VERSION);
99 // Create the export database domains table.
100 exportDatabase.execSQL(DomainsDatabaseHelper.CREATE_DOMAINS_TABLE);
102 // Open the domains database. The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
103 DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(context, null, null, 0);
105 // Get a full domains database cursor.
106 Cursor domainsCursor = domainsDatabaseHelper.getCompleteCursorOrderedByDomain();
108 // Move to the first domain.
109 domainsCursor.moveToFirst();
111 // Copy the data from the domains cursor into the export database.
112 for (int i = 0; i < domainsCursor.getCount(); i++) {
113 // Extract the record from the cursor and store the data in a ContentValues.
114 ContentValues domainsContentValues = new ContentValues();
115 domainsContentValues.put(DomainsDatabaseHelper.DOMAIN_NAME, domainsCursor.getString(domainsCursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME)));
116 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_JAVASCRIPT, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_JAVASCRIPT)));
117 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_FIRST_PARTY_COOKIES, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FIRST_PARTY_COOKIES)));
118 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_THIRD_PARTY_COOKIES, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_THIRD_PARTY_COOKIES)));
119 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_DOM_STORAGE, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_DOM_STORAGE)));
120 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_FORM_DATA, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FORM_DATA)));
121 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_EASYLIST, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_EASYLIST)));
122 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_EASYPRIVACY, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_EASYPRIVACY)));
123 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_FANBOYS_ANNOYANCE_LIST, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FANBOYS_ANNOYANCE_LIST)));
124 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST)));
125 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_ULTRAPRIVACY, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_ULTRAPRIVACY)));
126 domainsContentValues.put(DomainsDatabaseHelper.BLOCK_ALL_THIRD_PARTY_REQUESTS, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.BLOCK_ALL_THIRD_PARTY_REQUESTS)));
127 domainsContentValues.put(DomainsDatabaseHelper.USER_AGENT, domainsCursor.getString(domainsCursor.getColumnIndex(DomainsDatabaseHelper.USER_AGENT)));
128 domainsContentValues.put(DomainsDatabaseHelper.FONT_SIZE, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.FONT_SIZE)));
129 domainsContentValues.put(DomainsDatabaseHelper.SWIPE_TO_REFRESH, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.SWIPE_TO_REFRESH)));
130 domainsContentValues.put(DomainsDatabaseHelper.NIGHT_MODE, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.NIGHT_MODE)));
131 domainsContentValues.put(DomainsDatabaseHelper.DISPLAY_IMAGES, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.DISPLAY_IMAGES)));
132 domainsContentValues.put(DomainsDatabaseHelper.PINNED_SSL_CERTIFICATE, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.PINNED_SSL_CERTIFICATE)));
133 domainsContentValues.put(DomainsDatabaseHelper.SSL_ISSUED_TO_COMMON_NAME, domainsCursor.getString(domainsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_TO_COMMON_NAME)));
134 domainsContentValues.put(DomainsDatabaseHelper.SSL_ISSUED_TO_ORGANIZATION, domainsCursor.getString(domainsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_TO_ORGANIZATION)));
135 domainsContentValues.put(DomainsDatabaseHelper.SSL_ISSUED_TO_ORGANIZATIONAL_UNIT, domainsCursor.getString(domainsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_TO_ORGANIZATIONAL_UNIT)));
136 domainsContentValues.put(DomainsDatabaseHelper.SSL_ISSUED_BY_COMMON_NAME, domainsCursor.getString(domainsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_BY_COMMON_NAME)));
137 domainsContentValues.put(DomainsDatabaseHelper.SSL_ISSUED_BY_ORGANIZATION, domainsCursor.getString(domainsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_BY_ORGANIZATION)));
138 domainsContentValues.put(DomainsDatabaseHelper.SSL_ISSUED_BY_ORGANIZATIONAL_UNIT, domainsCursor.getString(domainsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_BY_ORGANIZATIONAL_UNIT)));
139 domainsContentValues.put(DomainsDatabaseHelper.SSL_START_DATE, domainsCursor.getLong(domainsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_START_DATE)));
140 domainsContentValues.put(DomainsDatabaseHelper.SSL_END_DATE, domainsCursor.getLong(domainsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_END_DATE)));
142 // Insert the record into the export database.
143 exportDatabase.insert(DomainsDatabaseHelper.DOMAINS_TABLE, null, domainsContentValues);
145 // Advance to the next record.
146 domainsCursor.moveToNext();
149 // Close the domains database.
150 domainsCursor.close();
151 domainsDatabaseHelper.close();
154 // Create the export database bookmarks table.
155 exportDatabase.execSQL(BookmarksDatabaseHelper.CREATE_BOOKMARKS_TABLE);
157 // Open the bookmarks database. The `0` specifies the database version, but that is ignored and set instead using a constant in `BookmarksDatabaseHelper`.
158 BookmarksDatabaseHelper bookmarksDatabaseHelper = new BookmarksDatabaseHelper(context, null, null, 0);
160 // Get a full bookmarks cursor.
161 Cursor bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarks();
163 // Move to the first bookmark.
164 bookmarksCursor.moveToFirst();
166 // Copy the data from the bookmarks cursor into the export database.
167 for (int i = 0; i < bookmarksCursor.getCount(); i++) {
168 // Extract the record from the cursor and store the data in a ContentValues.
169 ContentValues bookmarksContentValues = new ContentValues();
170 bookmarksContentValues.put(BookmarksDatabaseHelper.BOOKMARK_NAME, bookmarksCursor.getString(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME)));
171 bookmarksContentValues.put(BookmarksDatabaseHelper.BOOKMARK_URL, bookmarksCursor.getString(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_URL)));
172 bookmarksContentValues.put(BookmarksDatabaseHelper.PARENT_FOLDER, bookmarksCursor.getString(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.PARENT_FOLDER)));
173 bookmarksContentValues.put(BookmarksDatabaseHelper.DISPLAY_ORDER, bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.DISPLAY_ORDER)));
174 bookmarksContentValues.put(BookmarksDatabaseHelper.IS_FOLDER, bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)));
175 bookmarksContentValues.put(BookmarksDatabaseHelper.FAVORITE_ICON, bookmarksCursor.getBlob(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON)));
177 // Insert the record into the export database.
178 exportDatabase.insert(BookmarksDatabaseHelper.BOOKMARKS_TABLE, null, bookmarksContentValues);
180 // Advance to the next record.
181 bookmarksCursor.moveToNext();
184 // Close the bookmarks database.
185 bookmarksCursor.close();
186 bookmarksDatabaseHelper.close();
189 // Prepare the preferences table SQL creation string.
190 String CREATE_PREFERENCES_TABLE = "CREATE TABLE " + PREFERENCES_TABLE + " (" +
191 _ID + " INTEGER PRIMARY KEY, " +
192 JAVASCRIPT + " BOOLEAN, " +
193 FIRST_PARTY_COOKIES + " BOOLEAN, " +
194 THIRD_PARTY_COOKIES + " BOOLEAN, " +
195 DOM_STORAGE + " BOOLEAN, " +
196 SAVE_FORM_DATA + " BOOLEAN, " +
197 USER_AGENT + " TEXT, " +
198 CUSTOM_USER_AGENT + " TEXT, " +
199 INCOGNITO_MODE + " BOOLEAN, " +
200 DO_NOT_TRACK + " BOOLEAN, " +
201 ALLOW_SCREENSHOTS + " BOOLEAN, " +
202 EASYLIST + " BOOLEAN, " +
203 EASYPRIVACY + " BOOLEAN, " +
204 FANBOYS_ANNOYANCE_LIST + " BOOLEAN, " +
205 FANBOYS_SOCIAL_BLOCKING_LIST + " BOOLEAN, " +
206 ULTRAPRIVACY + " BOOLEAN, " +
207 BLOCK_ALL_THIRD_PARTY_REQUESTS + " BOOLEAN, " +
208 PROXY_THROUGH_ORBOT + " BOOLEAN, " +
209 TOR_HOMEPAGE + " TEXT, " +
210 TOR_SEARCH + " TEXT, " +
211 TOR_SEARCH_CUSTOM_URL + " TEXT, " +
213 SEARCH_CUSTOM_URL + " TEXT, " +
214 FULL_SCREEN_BROWSING_MODE + " BOOLEAN, " +
215 HIDE_SYSTEM_BARS + " BOOLEAN, " +
216 TRANSLUCENT_NAVIGATION_BAR + " BOOLEAN, " +
217 CLEAR_EVERYTHING + " BOOLEAN, " +
218 CLEAR_COOKIES + " BOOLEAN, " +
219 CLEAR_DOM_STORAGE + " BOOLEAN, " +
220 CLEAR_FORM_DATA + " BOOLEAN, " +
221 CLEAR_CACHE + " BOOLEAN, " +
222 HOMEPAGE + " TEXT, " +
223 FONT_SIZE + " TEXT, " +
224 SWIPE_TO_REFRESH + " BOOLEAN, " +
225 DOWNLOAD_WITH_EXTERNAL_APP + " BOOLEAN, " +
226 DISPLAY_ADDITIONAL_APP_BAR_ICONS + " BOOLEAN, " +
227 DARK_THEME + " BOOLEAN, " +
228 NIGHT_MODE + " BOOLEAN, " +
229 DISPLAY_WEBPAGE_IMAGES + " BOOLEAN)";
231 // Create the export database preferences table.
232 exportDatabase.execSQL(CREATE_PREFERENCES_TABLE);
234 // Get a handle for the shared preference.
235 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
237 // Create a ContentValues with the preferences information.
238 ContentValues preferencesContentValues = new ContentValues();
239 preferencesContentValues.put(JAVASCRIPT, sharedPreferences.getBoolean(JAVASCRIPT, false));
240 preferencesContentValues.put(FIRST_PARTY_COOKIES, sharedPreferences.getBoolean(FIRST_PARTY_COOKIES, false));
241 preferencesContentValues.put(THIRD_PARTY_COOKIES, sharedPreferences.getBoolean(THIRD_PARTY_COOKIES, false));
242 preferencesContentValues.put(DOM_STORAGE, sharedPreferences.getBoolean(DOM_STORAGE, false));
243 preferencesContentValues.put(SAVE_FORM_DATA, sharedPreferences.getBoolean(SAVE_FORM_DATA, false)); // Save form data can be removed once the minimum API >= 26.
244 preferencesContentValues.put(USER_AGENT, sharedPreferences.getString(USER_AGENT, context.getString(R.string.user_agent_default_value)));
245 preferencesContentValues.put(CUSTOM_USER_AGENT, sharedPreferences.getString(CUSTOM_USER_AGENT, context.getString(R.string.custom_user_agent_default_value)));
246 preferencesContentValues.put(INCOGNITO_MODE, sharedPreferences.getBoolean(INCOGNITO_MODE, false));
247 preferencesContentValues.put(DO_NOT_TRACK, sharedPreferences.getBoolean(DO_NOT_TRACK, false));
248 preferencesContentValues.put(ALLOW_SCREENSHOTS, sharedPreferences.getBoolean(ALLOW_SCREENSHOTS, false));
249 preferencesContentValues.put(EASYLIST, sharedPreferences.getBoolean(EASYLIST, true));
250 preferencesContentValues.put(EASYPRIVACY, sharedPreferences.getBoolean(EASYPRIVACY, true));
251 preferencesContentValues.put(FANBOYS_ANNOYANCE_LIST, sharedPreferences.getBoolean(FANBOYS_ANNOYANCE_LIST, true));
252 preferencesContentValues.put(FANBOYS_SOCIAL_BLOCKING_LIST, sharedPreferences.getBoolean(FANBOYS_SOCIAL_BLOCKING_LIST, true));
253 preferencesContentValues.put(ULTRAPRIVACY, sharedPreferences.getBoolean(ULTRAPRIVACY, true));
254 preferencesContentValues.put(BLOCK_ALL_THIRD_PARTY_REQUESTS, sharedPreferences.getBoolean(BLOCK_ALL_THIRD_PARTY_REQUESTS, false));
255 preferencesContentValues.put(PROXY_THROUGH_ORBOT, sharedPreferences.getBoolean(PROXY_THROUGH_ORBOT, false));
256 preferencesContentValues.put(TOR_HOMEPAGE, sharedPreferences.getString(TOR_HOMEPAGE, context.getString(R.string.tor_homepage_default_value)));
257 preferencesContentValues.put(TOR_SEARCH, sharedPreferences.getString(TOR_SEARCH, context.getString(R.string.tor_search_default_value)));
258 preferencesContentValues.put(TOR_SEARCH_CUSTOM_URL, sharedPreferences.getString(TOR_SEARCH_CUSTOM_URL, context.getString(R.string.tor_search_custom_url_default_value)));
259 preferencesContentValues.put(SEARCH, sharedPreferences.getString(SEARCH, context.getString(R.string.search_default_value)));
260 preferencesContentValues.put(SEARCH_CUSTOM_URL, sharedPreferences.getString(SEARCH_CUSTOM_URL, context.getString(R.string.search_custom_url_default_value)));
261 preferencesContentValues.put(FULL_SCREEN_BROWSING_MODE, sharedPreferences.getBoolean(FULL_SCREEN_BROWSING_MODE, false));
262 preferencesContentValues.put(HIDE_SYSTEM_BARS, sharedPreferences.getBoolean(HIDE_SYSTEM_BARS, false));
263 preferencesContentValues.put(TRANSLUCENT_NAVIGATION_BAR, sharedPreferences.getBoolean(TRANSLUCENT_NAVIGATION_BAR, true));
264 preferencesContentValues.put(CLEAR_EVERYTHING, sharedPreferences.getBoolean(CLEAR_EVERYTHING, true));
265 preferencesContentValues.put(CLEAR_COOKIES, sharedPreferences.getBoolean(CLEAR_COOKIES, true));
266 preferencesContentValues.put(CLEAR_DOM_STORAGE, sharedPreferences.getBoolean(CLEAR_DOM_STORAGE, true));
267 preferencesContentValues.put(CLEAR_FORM_DATA, sharedPreferences.getBoolean(CLEAR_FORM_DATA, true)); // Clear form data can be removed once the minimum API >= 26.
268 preferencesContentValues.put(CLEAR_CACHE, sharedPreferences.getBoolean(CLEAR_CACHE, true));
269 preferencesContentValues.put(HOMEPAGE, sharedPreferences.getString(HOMEPAGE, context.getString(R.string.homepage_default_value)));
270 preferencesContentValues.put(FONT_SIZE, sharedPreferences.getString(FONT_SIZE, context.getString(R.string.font_size_default_value)));
271 preferencesContentValues.put(SWIPE_TO_REFRESH, sharedPreferences.getBoolean(SWIPE_TO_REFRESH, true));
272 preferencesContentValues.put(DOWNLOAD_WITH_EXTERNAL_APP, sharedPreferences.getBoolean(DOWNLOAD_WITH_EXTERNAL_APP, false));
273 preferencesContentValues.put(DISPLAY_ADDITIONAL_APP_BAR_ICONS, sharedPreferences.getBoolean(DISPLAY_ADDITIONAL_APP_BAR_ICONS, false));
274 preferencesContentValues.put(DARK_THEME, sharedPreferences.getBoolean(DARK_THEME, false));
275 preferencesContentValues.put(NIGHT_MODE, sharedPreferences.getBoolean(NIGHT_MODE, false));
276 preferencesContentValues.put(DISPLAY_WEBPAGE_IMAGES, sharedPreferences.getBoolean(DISPLAY_WEBPAGE_IMAGES, true));
278 // Insert the preferences into the export database.
279 exportDatabase.insert(PREFERENCES_TABLE, null, preferencesContentValues);
281 // Close the export database.
282 exportDatabase.close();
284 // Convert the database file to a string.
285 String exportFileString = exportFile.toString();
287 // Create strings for the temporary database files.
288 String journalFileString = exportFileString + "-journal";
290 // Get `Files` for the temporary database files.
291 File journalFile = new File(journalFileString);
293 // Delete the Journal file if it exists.
294 if (journalFile.exists()) {
295 //noinspection ResultOfMethodCallIgnored
296 journalFile.delete();
299 // Export successful.
300 return EXPORT_SUCCESSFUL;
301 } catch (Exception exception) {
302 // Return the export error.
303 return exception.toString();
307 public String importUnencrypted(File importFile, Context context){
309 // Create a temporary import file string.
310 String temporaryImportFileString = context.getCacheDir() + "/" + "temporary_import_file";
312 // Get a handle for a temporary import file.
313 File temporaryImportFile = new File(temporaryImportFileString);
315 // Delete the temporary import file if it already exists.
316 if (temporaryImportFile.exists()) {
317 //noinspection ResultOfMethodCallIgnored
318 temporaryImportFile.delete();
321 // Create input and output streams.
322 InputStream importFileInputStream = new FileInputStream(importFile);
323 OutputStream temporaryImportFileOutputStream = new FileOutputStream(temporaryImportFile);
325 // Create a byte array.
326 byte[] transferByteArray = new byte[1024];
328 // Create an integer to track the number of bytes read.
331 // Copy the import file to the temporary import file. Once API >= 26 `Files.copy` can be used instead.
332 while ((bytesRead = importFileInputStream.read(transferByteArray)) > 0) {
333 temporaryImportFileOutputStream.write(transferByteArray, 0, bytesRead);
336 // Close the file streams.
337 importFileInputStream.close();
338 temporaryImportFileOutputStream.close();
341 // Get a handle for the shared preference.
342 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
344 // Open the import database. Once API >= 27 the file can be opened directly without using the string.
345 SQLiteDatabase importDatabase = SQLiteDatabase.openDatabase(temporaryImportFileString, null, SQLiteDatabase.OPEN_READWRITE);
347 // Get the database version.
348 int importDatabaseVersion = importDatabase.getVersion();
350 // Upgrade the database if needed.
351 if (importDatabaseVersion < SCHEMA_VERSION) {
352 switch (importDatabaseVersion){
353 // Upgrade from schema version 1.
355 // Add the download with external app preference.
356 importDatabase.execSQL("ALTER TABLE " + PREFERENCES_TABLE + " ADD COLUMN " + DOWNLOAD_WITH_EXTERNAL_APP + " BOOLEAN");
358 // Get the current setting for downloading with an external app.
359 boolean downloadWithExternalApp = sharedPreferences.getBoolean("download_with_external_app", false);
361 // Set the download with external app preference to the current default.
362 if (downloadWithExternalApp) {
363 importDatabase.execSQL("UPDATE " + PREFERENCES_TABLE + " SET " + DOWNLOAD_WITH_EXTERNAL_APP + " = " + 1);
365 importDatabase.execSQL("UPDATE " + PREFERENCES_TABLE + " SET " + DOWNLOAD_WITH_EXTERNAL_APP + " = " + 0);
368 // Upgrade from schema version 2.
370 // Once the SQLite version is >= 3.25.0 `ALTER TABLE RENAME COLUMN` can be used. https://www.sqlite.org/lang_altertable.html https://www.sqlite.org/changes.html
371 // https://developer.android.com/reference/android/database/sqlite/package-summary
372 // In the meantime, a new column must be created with the new name. There is no need to delete the old column on the temporary import database.
374 // Get a cursor with the current `default_font_size` value.
375 Cursor importDatabasePreferenceCursor = importDatabase.rawQuery("SELECT default_font_size FROM " + PREFERENCES_TABLE, null);
377 // Move to the beginning fo the cursor.
378 importDatabasePreferenceCursor.moveToFirst();
380 // Get the current value in `default_font_size`.
381 String fontSize = importDatabasePreferenceCursor.getString(importDatabasePreferenceCursor.getColumnIndex("default_font_size"));
384 importDatabasePreferenceCursor.close();
386 // Create a new column named `font_size`.
387 importDatabase.execSQL("ALTER TABLE " + PREFERENCES_TABLE + " ADD COLUMN " + FONT_SIZE + " TEXT");
389 // Place the font size string in the new column.
390 importDatabase.execSQL("UPDATE " + PREFERENCES_TABLE + " SET " + FONT_SIZE + " = " + fontSize);
394 // Get a cursor for the domains table.
395 Cursor importDomainsCursor = importDatabase.rawQuery("SELECT * FROM " + DomainsDatabaseHelper.DOMAINS_TABLE + " ORDER BY " + DomainsDatabaseHelper.DOMAIN_NAME + " ASC", null);
397 // Delete the current domains database.
398 context.deleteDatabase(DomainsDatabaseHelper.DOMAINS_DATABASE);
400 // Create a new domains database. The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
401 DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(context, null, null, 0);
403 // Move to the first domain.
404 importDomainsCursor.moveToFirst();
406 // Copy the data from the import domains cursor into the domains database.
407 for (int i = 0; i < importDomainsCursor.getCount(); i++) {
408 // Extract the record from the cursor and store the data in a ContentValues.
409 ContentValues domainsContentValues = new ContentValues();
410 domainsContentValues.put(DomainsDatabaseHelper.DOMAIN_NAME, importDomainsCursor.getString(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME)));
411 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_JAVASCRIPT, importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_JAVASCRIPT)));
412 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_FIRST_PARTY_COOKIES, importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FIRST_PARTY_COOKIES)));
413 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_THIRD_PARTY_COOKIES, importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_THIRD_PARTY_COOKIES)));
414 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_DOM_STORAGE, importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_DOM_STORAGE)));
415 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_FORM_DATA, importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FORM_DATA)));
416 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_EASYLIST, importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_EASYLIST)));
417 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_EASYPRIVACY, importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_EASYPRIVACY)));
418 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_FANBOYS_ANNOYANCE_LIST,
419 importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FANBOYS_ANNOYANCE_LIST)));
420 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST,
421 importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST)));
422 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_ULTRAPRIVACY, importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_ULTRAPRIVACY)));
423 domainsContentValues.put(DomainsDatabaseHelper.BLOCK_ALL_THIRD_PARTY_REQUESTS,
424 importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.BLOCK_ALL_THIRD_PARTY_REQUESTS)));
425 domainsContentValues.put(DomainsDatabaseHelper.USER_AGENT, importDomainsCursor.getString(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.USER_AGENT)));
426 domainsContentValues.put(DomainsDatabaseHelper.FONT_SIZE, importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.FONT_SIZE)));
427 domainsContentValues.put(DomainsDatabaseHelper.SWIPE_TO_REFRESH, importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.SWIPE_TO_REFRESH)));
428 domainsContentValues.put(DomainsDatabaseHelper.NIGHT_MODE, importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.NIGHT_MODE)));
429 domainsContentValues.put(DomainsDatabaseHelper.DISPLAY_IMAGES, importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.DISPLAY_IMAGES)));
430 domainsContentValues.put(DomainsDatabaseHelper.PINNED_SSL_CERTIFICATE, importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.PINNED_SSL_CERTIFICATE)));
431 domainsContentValues.put(DomainsDatabaseHelper.SSL_ISSUED_TO_COMMON_NAME, importDomainsCursor.getString(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_TO_COMMON_NAME)));
432 domainsContentValues.put(DomainsDatabaseHelper.SSL_ISSUED_TO_ORGANIZATION, importDomainsCursor.getString(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_TO_ORGANIZATION)));
433 domainsContentValues.put(DomainsDatabaseHelper.SSL_ISSUED_TO_ORGANIZATIONAL_UNIT,
434 importDomainsCursor.getString(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_TO_ORGANIZATIONAL_UNIT)));
435 domainsContentValues.put(DomainsDatabaseHelper.SSL_ISSUED_BY_COMMON_NAME, importDomainsCursor.getString(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_BY_COMMON_NAME)));
436 domainsContentValues.put(DomainsDatabaseHelper.SSL_ISSUED_BY_ORGANIZATION, importDomainsCursor.getString(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_BY_ORGANIZATION)));
437 domainsContentValues.put(DomainsDatabaseHelper.SSL_ISSUED_BY_ORGANIZATIONAL_UNIT,
438 importDomainsCursor.getString(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_BY_ORGANIZATIONAL_UNIT)));
439 domainsContentValues.put(DomainsDatabaseHelper.SSL_START_DATE, importDomainsCursor.getLong(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_START_DATE)));
440 domainsContentValues.put(DomainsDatabaseHelper.SSL_END_DATE, importDomainsCursor.getLong(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_END_DATE)));
442 // Insert the record into the export database.
443 domainsDatabaseHelper.addDomain(domainsContentValues);
445 // Advance to the next record.
446 importDomainsCursor.moveToNext();
449 // Close the domains cursor.
450 importDomainsCursor.close();
452 // Close the domains database.
453 domainsDatabaseHelper.close();
456 // Get a cursor for the bookmarks table.
457 Cursor importBookmarksCursor = importDatabase.rawQuery("SELECT * FROM " + BookmarksDatabaseHelper.BOOKMARKS_TABLE, null);
459 // Delete the current bookmarks database.
460 context.deleteDatabase(BookmarksDatabaseHelper.BOOKMARKS_DATABASE);
462 // Create a new bookmarks database. The `0` specifies the database version, but that is ignored and set instead using a constant in `BookmarksDatabaseHelper`.
463 BookmarksDatabaseHelper bookmarksDatabaseHelper = new BookmarksDatabaseHelper(context, null, null, 0);
465 // Move to the first bookmark.
466 importBookmarksCursor.moveToFirst();
468 // Copy the data from the import bookmarks cursor into the bookmarks database.
469 for (int i = 0; i < importBookmarksCursor.getCount(); i++) {
470 // Extract the record from the cursor and store the data in a ContentValues.
471 ContentValues bookmarksContentValues = new ContentValues();
472 bookmarksContentValues.put(BookmarksDatabaseHelper.BOOKMARK_NAME, importBookmarksCursor.getString(importBookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME)));
473 bookmarksContentValues.put(BookmarksDatabaseHelper.BOOKMARK_URL, importBookmarksCursor.getString(importBookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_URL)));
474 bookmarksContentValues.put(BookmarksDatabaseHelper.PARENT_FOLDER, importBookmarksCursor.getString(importBookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.PARENT_FOLDER)));
475 bookmarksContentValues.put(BookmarksDatabaseHelper.DISPLAY_ORDER, importBookmarksCursor.getInt(importBookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.DISPLAY_ORDER)));
476 bookmarksContentValues.put(BookmarksDatabaseHelper.IS_FOLDER, importBookmarksCursor.getInt(importBookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)));
477 bookmarksContentValues.put(BookmarksDatabaseHelper.FAVORITE_ICON, importBookmarksCursor.getBlob(importBookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON)));
479 // Insert the record into the export database.
480 bookmarksDatabaseHelper.createBookmark(bookmarksContentValues);
482 // Advance to the next record.
483 importBookmarksCursor.moveToNext();
486 // Close the bookmarks cursor.
487 importBookmarksCursor.close();
489 // Close the bookmarks database.
490 bookmarksDatabaseHelper.close();
493 // Get a cursor for the bookmarks table.
494 Cursor importPreferencesCursor = importDatabase.rawQuery("SELECT * FROM " + PREFERENCES_TABLE, null);
496 // Move to the first preference.
497 importPreferencesCursor.moveToFirst();
499 // Import the preference data.
500 sharedPreferences.edit()
501 .putBoolean(JAVASCRIPT, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(JAVASCRIPT)) == 1)
502 .putBoolean(FIRST_PARTY_COOKIES, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(FIRST_PARTY_COOKIES)) == 1)
503 .putBoolean(THIRD_PARTY_COOKIES, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(THIRD_PARTY_COOKIES)) == 1)
504 .putBoolean(DOM_STORAGE, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(DOM_STORAGE)) == 1)
505 // Save form data can be removed once the minimum API >= 26.
506 .putBoolean(SAVE_FORM_DATA, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(SAVE_FORM_DATA)) == 1)
507 .putString(USER_AGENT, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndex(USER_AGENT)))
508 .putString(CUSTOM_USER_AGENT, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndex(CUSTOM_USER_AGENT)))
509 .putBoolean(INCOGNITO_MODE, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(INCOGNITO_MODE)) == 1)
510 .putBoolean(DO_NOT_TRACK, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(DO_NOT_TRACK)) == 1)
511 .putBoolean(ALLOW_SCREENSHOTS, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(ALLOW_SCREENSHOTS)) == 1)
512 .putBoolean(EASYLIST, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(EASYLIST)) == 1)
513 .putBoolean(EASYPRIVACY, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(EASYPRIVACY)) == 1)
514 .putBoolean(FANBOYS_ANNOYANCE_LIST, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(FANBOYS_ANNOYANCE_LIST)) == 1)
515 .putBoolean(FANBOYS_SOCIAL_BLOCKING_LIST, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(FANBOYS_SOCIAL_BLOCKING_LIST)) == 1)
516 .putBoolean(ULTRAPRIVACY, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(ULTRAPRIVACY)) == 1)
517 .putBoolean(BLOCK_ALL_THIRD_PARTY_REQUESTS, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(BLOCK_ALL_THIRD_PARTY_REQUESTS)) == 1)
518 .putBoolean(PROXY_THROUGH_ORBOT, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(PROXY_THROUGH_ORBOT)) == 1)
519 .putString(TOR_HOMEPAGE, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndex(TOR_HOMEPAGE)))
520 .putString(TOR_SEARCH, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndex(TOR_SEARCH)))
521 .putString(TOR_SEARCH_CUSTOM_URL, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndex(TOR_SEARCH_CUSTOM_URL)))
522 .putString(SEARCH, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndex(SEARCH)))
523 .putString(SEARCH_CUSTOM_URL, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndex(SEARCH_CUSTOM_URL)))
524 .putBoolean(FULL_SCREEN_BROWSING_MODE, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(FULL_SCREEN_BROWSING_MODE)) == 1)
525 .putBoolean(HIDE_SYSTEM_BARS, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(HIDE_SYSTEM_BARS)) == 1)
526 .putBoolean(TRANSLUCENT_NAVIGATION_BAR, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(TRANSLUCENT_NAVIGATION_BAR)) == 1)
527 .putBoolean(CLEAR_EVERYTHING, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(CLEAR_EVERYTHING)) == 1)
528 .putBoolean(CLEAR_COOKIES, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(CLEAR_COOKIES)) == 1)
529 .putBoolean(CLEAR_DOM_STORAGE, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(CLEAR_DOM_STORAGE)) == 1)
530 // Clear form data can be removed once the minimum API >= 26.
531 .putBoolean(CLEAR_FORM_DATA, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(CLEAR_FORM_DATA)) == 1)
532 .putBoolean(CLEAR_CACHE, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(CLEAR_CACHE)) == 1)
533 .putString(HOMEPAGE, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndex(HOMEPAGE)))
534 .putString(FONT_SIZE, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndex(FONT_SIZE)))
535 .putBoolean(SWIPE_TO_REFRESH, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(SWIPE_TO_REFRESH)) == 1)
536 .putBoolean(DOWNLOAD_WITH_EXTERNAL_APP, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(DOWNLOAD_WITH_EXTERNAL_APP)) == 1)
537 .putBoolean(DISPLAY_ADDITIONAL_APP_BAR_ICONS, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(DISPLAY_ADDITIONAL_APP_BAR_ICONS)) == 1)
538 .putBoolean(DARK_THEME, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(DARK_THEME)) == 1)
539 .putBoolean(NIGHT_MODE, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(NIGHT_MODE)) == 1)
540 .putBoolean(DISPLAY_WEBPAGE_IMAGES, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(DISPLAY_WEBPAGE_IMAGES)) == 1)
543 // Close the preferences cursor.
544 importPreferencesCursor.close();
547 // Close the import database.
548 importDatabase.close();
550 // Create strings for the temporary database files.
551 String shmFileString = temporaryImportFileString + "-shm";
552 String walFileString = temporaryImportFileString + "-wal";
553 String journalFileString = temporaryImportFileString + "-journal";
555 // Get `Files` for the temporary database files.
556 File shmFile = new File(shmFileString);
557 File walFile = new File(walFileString);
558 File journalFile = new File(journalFileString);
560 // Delete the Shared Memory file if it exists.
561 if (shmFile.exists()) {
562 //noinspection ResultOfMethodCallIgnored
566 // Delete the Write Ahead Log file if it exists.
567 if (walFile.exists()) {
568 //noinspection ResultOfMethodCallIgnored
572 // Delete the Journal file if it exists.
573 if (journalFile.exists()) {
574 //noinspection ResultOfMethodCallIgnored
575 journalFile.delete();
578 // Delete the temporary import file.
579 //noinspection ResultOfMethodCallIgnored
580 temporaryImportFile.delete();
582 // Import successful.
583 return IMPORT_SUCCESSFUL;
584 } catch (Exception exception) {
585 // Return the import error.
586 return exception.toString();