Add a Mastodon entry to About > Links. https://redmine.stoutner.com/issues/573
[PrivacyBrowser.git] / app / src / main / java / com / stoutner / privacybrowser / helpers / ImportExportDatabaseHelper.java
1 /*
2  * Copyright © 2018-2020 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.content.SharedPreferences;
25 import android.database.Cursor;
26 import android.database.DatabaseUtils;
27 import android.database.sqlite.SQLiteDatabase;
28 import android.preference.PreferenceManager;
29
30 import com.stoutner.privacybrowser.R;
31
32 import java.io.File;
33 import java.io.FileInputStream;
34 import java.io.FileOutputStream;
35 import java.io.InputStream;
36 import java.io.OutputStream;
37
38 public class ImportExportDatabaseHelper {
39     // Declare the public constants.
40     public static final String EXPORT_SUCCESSFUL = "Export Successful";
41     public static final String IMPORT_SUCCESSFUL = "Import Successful";
42
43     // Declare the class constants.
44     private static final int SCHEMA_VERSION = 12;
45     private static final String PREFERENCES_TABLE = "preferences";
46
47     // Declare the preferences constants.
48     private static final String _ID = "_id";
49     private static final String JAVASCRIPT = "javascript";
50     private static final String FIRST_PARTY_COOKIES = "first_party_cookies";
51     private static final String THIRD_PARTY_COOKIES = "third_party_cookies";
52     private static final String DOM_STORAGE = "dom_storage";
53     private static final String SAVE_FORM_DATA = "save_form_data";
54     private static final String USER_AGENT = "user_agent";
55     private static final String CUSTOM_USER_AGENT = "custom_user_agent";
56     private static final String INCOGNITO_MODE = "incognito_mode";
57     private static final String DO_NOT_TRACK = "do_not_track";
58     private static final String ALLOW_SCREENSHOTS = "allow_screenshots";
59     private static final String EASYLIST = "easylist";
60     private static final String EASYPRIVACY = "easyprivacy";
61     private static final String FANBOYS_ANNOYANCE_LIST = "fanboys_annoyance_list";
62     private static final String FANBOYS_SOCIAL_BLOCKING_LIST = "fanboys_social_blocking_list";
63     private static final String ULTRALIST = "ultralist";
64     private static final String ULTRAPRIVACY = "ultraprivacy";
65     private static final String BLOCK_ALL_THIRD_PARTY_REQUESTS = "block_all_third_party_requests";
66     private static final String GOOGLE_ANALYTICS = "google_analytics";
67     private static final String FACEBOOK_CLICK_IDS = "facebook_click_ids";
68     private static final String TWITTER_AMP_REDIRECTS = "twitter_amp_redirects";
69     private static final String SEARCH = "search";
70     private static final String SEARCH_CUSTOM_URL = "search_custom_url";
71     private static final String PROXY = "proxy";
72     private static final String PROXY_CUSTOM_URL = "proxy_custom_url";
73     private static final String FULL_SCREEN_BROWSING_MODE = "full_screen_browsing_mode";
74     private static final String HIDE_APP_BAR = "hide_app_bar";
75     private static final String CLEAR_EVERYTHING = "clear_everything";
76     private static final String CLEAR_COOKIES = "clear_cookies";
77     private static final String CLEAR_DOM_STORAGE = "clear_dom_storage";
78     private static final String CLEAR_FORM_DATA = "clear_form_data";
79     private static final String CLEAR_LOGCAT = "clear_logcat";
80     private static final String CLEAR_CACHE = "clear_cache";
81     private static final String HOMEPAGE = "homepage";
82     private static final String DOWNLOAD_LOCATION = "download_location";
83     private static final String DOWNLOAD_CUSTOM_LOCATION = "download_custom_location";
84     private static final String FONT_SIZE = "font_size";
85     private static final String OPEN_INTENTS_IN_NEW_TAB = "open_intents_in_new_tab";
86     private static final String SWIPE_TO_REFRESH = "swipe_to_refresh";
87     private static final String SCROLL_APP_BAR = "scroll_app_bar";
88     private static final String DISPLAY_ADDITIONAL_APP_BAR_ICONS = "display_additional_app_bar_icons";
89     private static final String APP_THEME = "app_theme";
90     private static final String WEBVIEW_THEME = "webview_theme";
91     private static final String WIDE_VIEWPORT = "wide_viewport";
92     private static final String DISPLAY_WEBPAGE_IMAGES = "display_webpage_images";
93
94     public String exportUnencrypted(File exportFile, Context context) {
95         try {
96             // Delete the current file if it exists.
97             if (exportFile.exists()) {
98                 //noinspection ResultOfMethodCallIgnored
99                 exportFile.delete();
100             }
101
102             // Create the export database.
103             SQLiteDatabase exportDatabase = SQLiteDatabase.openOrCreateDatabase(exportFile, null);
104
105             // Set the export database version number.
106             exportDatabase.setVersion(SCHEMA_VERSION);
107
108             // Create the export database domains table.
109             exportDatabase.execSQL(DomainsDatabaseHelper.CREATE_DOMAINS_TABLE);
110
111
112             // Create the export database bookmarks table.
113             exportDatabase.execSQL(BookmarksDatabaseHelper.CREATE_BOOKMARKS_TABLE);
114
115             // Open the bookmarks database.  The `0` specifies the database version, but that is ignored and set instead using a constant in `BookmarksDatabaseHelper`.
116             BookmarksDatabaseHelper bookmarksDatabaseHelper = new BookmarksDatabaseHelper(context, null, null, 0);
117
118             // Get a full bookmarks cursor.
119             Cursor bookmarksCursor = bookmarksDatabaseHelper.getAllBookmarks();
120
121             // Move to the first bookmark.
122             bookmarksCursor.moveToFirst();
123
124             // Copy the data from the bookmarks cursor into the export database.
125             for (int i = 0; i < bookmarksCursor.getCount(); i++) {
126                 // Extract the record from the cursor and store the data in a ContentValues.
127                 ContentValues bookmarksContentValues = new ContentValues();
128                 bookmarksContentValues.put(BookmarksDatabaseHelper.BOOKMARK_NAME, bookmarksCursor.getString(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME)));
129                 bookmarksContentValues.put(BookmarksDatabaseHelper.BOOKMARK_URL, bookmarksCursor.getString(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_URL)));
130                 bookmarksContentValues.put(BookmarksDatabaseHelper.PARENT_FOLDER, bookmarksCursor.getString(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.PARENT_FOLDER)));
131                 bookmarksContentValues.put(BookmarksDatabaseHelper.DISPLAY_ORDER, bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.DISPLAY_ORDER)));
132                 bookmarksContentValues.put(BookmarksDatabaseHelper.IS_FOLDER, bookmarksCursor.getInt(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)));
133                 bookmarksContentValues.put(BookmarksDatabaseHelper.FAVORITE_ICON, bookmarksCursor.getBlob(bookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON)));
134
135                 // Insert the record into the export database.
136                 exportDatabase.insert(BookmarksDatabaseHelper.BOOKMARKS_TABLE, null, bookmarksContentValues);
137
138                 // Advance to the next record.
139                 bookmarksCursor.moveToNext();
140             }
141
142             // Close the bookmarks database.
143             bookmarksCursor.close();
144             bookmarksDatabaseHelper.close();
145
146
147             // Open the domains database.  The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
148             DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(context, null, null, 0);
149
150             // Get a full domains database cursor.
151             Cursor domainsCursor = domainsDatabaseHelper.getCompleteCursorOrderedByDomain();
152
153             // Move to the first domain.
154             domainsCursor.moveToFirst();
155
156             // Copy the data from the domains cursor into the export database.
157             for (int i = 0; i < domainsCursor.getCount(); i++) {
158                 // Extract the record from the cursor and store the data in a ContentValues.
159                 ContentValues domainsContentValues = new ContentValues();
160                 domainsContentValues.put(DomainsDatabaseHelper.DOMAIN_NAME, domainsCursor.getString(domainsCursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME)));
161                 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_JAVASCRIPT, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_JAVASCRIPT)));
162                 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_FIRST_PARTY_COOKIES, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FIRST_PARTY_COOKIES)));
163                 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_THIRD_PARTY_COOKIES, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_THIRD_PARTY_COOKIES)));
164                 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_DOM_STORAGE, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_DOM_STORAGE)));
165                 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_FORM_DATA, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FORM_DATA)));
166                 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_EASYLIST, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_EASYLIST)));
167                 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_EASYPRIVACY, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_EASYPRIVACY)));
168                 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_FANBOYS_ANNOYANCE_LIST, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FANBOYS_ANNOYANCE_LIST)));
169                 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST)));
170                 domainsContentValues.put(DomainsDatabaseHelper.ULTRALIST, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.ULTRALIST)));
171                 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_ULTRAPRIVACY, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_ULTRAPRIVACY)));
172                 domainsContentValues.put(DomainsDatabaseHelper.BLOCK_ALL_THIRD_PARTY_REQUESTS, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.BLOCK_ALL_THIRD_PARTY_REQUESTS)));
173                 domainsContentValues.put(DomainsDatabaseHelper.USER_AGENT, domainsCursor.getString(domainsCursor.getColumnIndex(DomainsDatabaseHelper.USER_AGENT)));
174                 domainsContentValues.put(DomainsDatabaseHelper.FONT_SIZE, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.FONT_SIZE)));
175                 domainsContentValues.put(DomainsDatabaseHelper.SWIPE_TO_REFRESH, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.SWIPE_TO_REFRESH)));
176                 domainsContentValues.put(DomainsDatabaseHelper.WEBVIEW_THEME, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.WEBVIEW_THEME)));
177                 domainsContentValues.put(DomainsDatabaseHelper.WIDE_VIEWPORT, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.WIDE_VIEWPORT)));
178                 domainsContentValues.put(DomainsDatabaseHelper.DISPLAY_IMAGES, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.DISPLAY_IMAGES)));
179                 domainsContentValues.put(DomainsDatabaseHelper.PINNED_SSL_CERTIFICATE, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.PINNED_SSL_CERTIFICATE)));
180                 domainsContentValues.put(DomainsDatabaseHelper.SSL_ISSUED_TO_COMMON_NAME, domainsCursor.getString(domainsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_TO_COMMON_NAME)));
181                 domainsContentValues.put(DomainsDatabaseHelper.SSL_ISSUED_TO_ORGANIZATION, domainsCursor.getString(domainsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_TO_ORGANIZATION)));
182                 domainsContentValues.put(DomainsDatabaseHelper.SSL_ISSUED_TO_ORGANIZATIONAL_UNIT, domainsCursor.getString(domainsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_TO_ORGANIZATIONAL_UNIT)));
183                 domainsContentValues.put(DomainsDatabaseHelper.SSL_ISSUED_BY_COMMON_NAME, domainsCursor.getString(domainsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_BY_COMMON_NAME)));
184                 domainsContentValues.put(DomainsDatabaseHelper.SSL_ISSUED_BY_ORGANIZATION, domainsCursor.getString(domainsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_BY_ORGANIZATION)));
185                 domainsContentValues.put(DomainsDatabaseHelper.SSL_ISSUED_BY_ORGANIZATIONAL_UNIT, domainsCursor.getString(domainsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_BY_ORGANIZATIONAL_UNIT)));
186                 domainsContentValues.put(DomainsDatabaseHelper.SSL_START_DATE, domainsCursor.getLong(domainsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_START_DATE)));
187                 domainsContentValues.put(DomainsDatabaseHelper.SSL_END_DATE, domainsCursor.getLong(domainsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_END_DATE)));
188                 domainsContentValues.put(DomainsDatabaseHelper.PINNED_IP_ADDRESSES, domainsCursor.getInt(domainsCursor.getColumnIndex(DomainsDatabaseHelper.PINNED_IP_ADDRESSES)));
189                 domainsContentValues.put(DomainsDatabaseHelper.IP_ADDRESSES, domainsCursor.getString(domainsCursor.getColumnIndex(DomainsDatabaseHelper.IP_ADDRESSES)));
190
191                 // Insert the record into the export database.
192                 exportDatabase.insert(DomainsDatabaseHelper.DOMAINS_TABLE, null, domainsContentValues);
193
194                 // Advance to the next record.
195                 domainsCursor.moveToNext();
196             }
197
198             // Close the domains database.
199             domainsCursor.close();
200             domainsDatabaseHelper.close();
201
202
203             // Prepare the preferences table SQL creation string.
204             String CREATE_PREFERENCES_TABLE = "CREATE TABLE " + PREFERENCES_TABLE + " (" +
205                     _ID + " INTEGER PRIMARY KEY, " +
206                     JAVASCRIPT + " BOOLEAN, " +
207                     FIRST_PARTY_COOKIES + " BOOLEAN, " +
208                     THIRD_PARTY_COOKIES + " BOOLEAN, " +
209                     DOM_STORAGE + " BOOLEAN, " +
210                     SAVE_FORM_DATA + " BOOLEAN, " +
211                     USER_AGENT + " TEXT, " +
212                     CUSTOM_USER_AGENT + " TEXT, " +
213                     INCOGNITO_MODE + " BOOLEAN, " +
214                     DO_NOT_TRACK + " BOOLEAN, " +
215                     ALLOW_SCREENSHOTS + " BOOLEAN, " +
216                     EASYLIST + " BOOLEAN, " +
217                     EASYPRIVACY + " BOOLEAN, " +
218                     FANBOYS_ANNOYANCE_LIST + " BOOLEAN, " +
219                     FANBOYS_SOCIAL_BLOCKING_LIST + " BOOLEAN, " +
220                     ULTRALIST + " BOOLEAN, " +
221                     ULTRAPRIVACY + " BOOLEAN, " +
222                     BLOCK_ALL_THIRD_PARTY_REQUESTS + " BOOLEAN, " +
223                     GOOGLE_ANALYTICS + " BOOLEAN, " +
224                     FACEBOOK_CLICK_IDS + " BOOLEAN, " +
225                     TWITTER_AMP_REDIRECTS + " BOOLEAN, " +
226                     SEARCH + " TEXT, " +
227                     SEARCH_CUSTOM_URL + " TEXT, " +
228                     PROXY + " TEXT, " +
229                     PROXY_CUSTOM_URL + " TEXT, " +
230                     FULL_SCREEN_BROWSING_MODE + " BOOLEAN, " +
231                     HIDE_APP_BAR + " BOOLEAN, " +
232                     CLEAR_EVERYTHING + " BOOLEAN, " +
233                     CLEAR_COOKIES + " BOOLEAN, " +
234                     CLEAR_DOM_STORAGE + " BOOLEAN, " +
235                     CLEAR_FORM_DATA + " BOOLEAN, " +
236                     CLEAR_LOGCAT + " BOOLEAN, " +
237                     CLEAR_CACHE + " BOOLEAN, " +
238                     HOMEPAGE + " TEXT, " +
239                     DOWNLOAD_LOCATION + " TEXT, " +
240                     DOWNLOAD_CUSTOM_LOCATION + " TEXT, " +
241                     FONT_SIZE + " TEXT, " +
242                     OPEN_INTENTS_IN_NEW_TAB + " BOOLEAN, " +
243                     SWIPE_TO_REFRESH + " BOOLEAN, " +
244                     SCROLL_APP_BAR + " BOOLEAN, " +
245                     DISPLAY_ADDITIONAL_APP_BAR_ICONS + " BOOLEAN, " +
246                     APP_THEME + " TEXT, " +
247                     WEBVIEW_THEME + " TEXT, " +
248                     WIDE_VIEWPORT + " BOOLEAN, " +
249                     DISPLAY_WEBPAGE_IMAGES + " BOOLEAN)";
250
251             // Create the export database preferences table.
252             exportDatabase.execSQL(CREATE_PREFERENCES_TABLE);
253
254             // Get a handle for the shared preference.
255             SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
256
257             // Create a ContentValues with the preferences information.
258             ContentValues preferencesContentValues = new ContentValues();
259             preferencesContentValues.put(JAVASCRIPT, sharedPreferences.getBoolean(JAVASCRIPT, false));
260             preferencesContentValues.put(FIRST_PARTY_COOKIES, sharedPreferences.getBoolean(FIRST_PARTY_COOKIES, false));
261             preferencesContentValues.put(THIRD_PARTY_COOKIES, sharedPreferences.getBoolean(THIRD_PARTY_COOKIES, false));
262             preferencesContentValues.put(DOM_STORAGE, sharedPreferences.getBoolean(DOM_STORAGE, false));
263             preferencesContentValues.put(SAVE_FORM_DATA, sharedPreferences.getBoolean(SAVE_FORM_DATA, false));  // Save form data can be removed once the minimum API >= 26.
264             preferencesContentValues.put(USER_AGENT, sharedPreferences.getString(USER_AGENT, context.getString(R.string.user_agent_default_value)));
265             preferencesContentValues.put(CUSTOM_USER_AGENT, sharedPreferences.getString(CUSTOM_USER_AGENT, context.getString(R.string.custom_user_agent_default_value)));
266             preferencesContentValues.put(INCOGNITO_MODE, sharedPreferences.getBoolean(INCOGNITO_MODE, false));
267             preferencesContentValues.put(DO_NOT_TRACK, sharedPreferences.getBoolean(DO_NOT_TRACK, false));
268             preferencesContentValues.put(ALLOW_SCREENSHOTS, sharedPreferences.getBoolean(ALLOW_SCREENSHOTS, false));
269             preferencesContentValues.put(EASYLIST, sharedPreferences.getBoolean(EASYLIST, true));
270             preferencesContentValues.put(EASYPRIVACY, sharedPreferences.getBoolean(EASYPRIVACY, true));
271             preferencesContentValues.put(FANBOYS_ANNOYANCE_LIST, sharedPreferences.getBoolean(FANBOYS_ANNOYANCE_LIST, true));
272             preferencesContentValues.put(FANBOYS_SOCIAL_BLOCKING_LIST, sharedPreferences.getBoolean(FANBOYS_SOCIAL_BLOCKING_LIST, true));
273             preferencesContentValues.put(ULTRALIST, sharedPreferences.getBoolean(ULTRALIST, true));
274             preferencesContentValues.put(ULTRAPRIVACY, sharedPreferences.getBoolean(ULTRAPRIVACY, true));
275             preferencesContentValues.put(BLOCK_ALL_THIRD_PARTY_REQUESTS, sharedPreferences.getBoolean(BLOCK_ALL_THIRD_PARTY_REQUESTS, false));
276             preferencesContentValues.put(GOOGLE_ANALYTICS, sharedPreferences.getBoolean(GOOGLE_ANALYTICS, true));
277             preferencesContentValues.put(FACEBOOK_CLICK_IDS, sharedPreferences.getBoolean(FACEBOOK_CLICK_IDS, true));
278             preferencesContentValues.put(TWITTER_AMP_REDIRECTS, sharedPreferences.getBoolean(TWITTER_AMP_REDIRECTS, true));
279             preferencesContentValues.put(SEARCH, sharedPreferences.getString(SEARCH, context.getString(R.string.search_default_value)));
280             preferencesContentValues.put(SEARCH_CUSTOM_URL, sharedPreferences.getString(SEARCH_CUSTOM_URL, context.getString(R.string.search_custom_url_default_value)));
281             preferencesContentValues.put(PROXY, sharedPreferences.getString(PROXY, context.getString(R.string.proxy_default_value)));
282             preferencesContentValues.put(PROXY_CUSTOM_URL, sharedPreferences.getString(PROXY_CUSTOM_URL, context.getString(R.string.proxy_custom_url_default_value)));
283             preferencesContentValues.put(FULL_SCREEN_BROWSING_MODE, sharedPreferences.getBoolean(FULL_SCREEN_BROWSING_MODE, false));
284             preferencesContentValues.put(HIDE_APP_BAR, sharedPreferences.getBoolean(HIDE_APP_BAR, true));
285             preferencesContentValues.put(CLEAR_EVERYTHING, sharedPreferences.getBoolean(CLEAR_EVERYTHING, true));
286             preferencesContentValues.put(CLEAR_COOKIES, sharedPreferences.getBoolean(CLEAR_COOKIES, true));
287             preferencesContentValues.put(CLEAR_DOM_STORAGE, sharedPreferences.getBoolean(CLEAR_DOM_STORAGE, true));
288             preferencesContentValues.put(CLEAR_FORM_DATA, sharedPreferences.getBoolean(CLEAR_FORM_DATA, true));  // Clear form data can be removed once the minimum API >= 26.
289             preferencesContentValues.put(CLEAR_LOGCAT, sharedPreferences.getBoolean(CLEAR_LOGCAT, true));
290             preferencesContentValues.put(CLEAR_CACHE, sharedPreferences.getBoolean(CLEAR_CACHE, true));
291             preferencesContentValues.put(HOMEPAGE, sharedPreferences.getString(HOMEPAGE, context.getString(R.string.homepage_default_value)));
292             preferencesContentValues.put(DOWNLOAD_LOCATION, sharedPreferences.getString(DOWNLOAD_LOCATION, context.getString(R.string.download_location_default_value)));
293             preferencesContentValues.put(DOWNLOAD_CUSTOM_LOCATION, sharedPreferences.getString(DOWNLOAD_CUSTOM_LOCATION, context.getString(R.string.download_custom_location_default_value)));
294             preferencesContentValues.put(FONT_SIZE, sharedPreferences.getString(FONT_SIZE, context.getString(R.string.font_size_default_value)));
295             preferencesContentValues.put(OPEN_INTENTS_IN_NEW_TAB, sharedPreferences.getBoolean(OPEN_INTENTS_IN_NEW_TAB, true));
296             preferencesContentValues.put(SWIPE_TO_REFRESH, sharedPreferences.getBoolean(SWIPE_TO_REFRESH, true));
297             preferencesContentValues.put(SCROLL_APP_BAR, sharedPreferences.getBoolean(SCROLL_APP_BAR, true));
298             preferencesContentValues.put(DISPLAY_ADDITIONAL_APP_BAR_ICONS, sharedPreferences.getBoolean(DISPLAY_ADDITIONAL_APP_BAR_ICONS, false));
299             preferencesContentValues.put(APP_THEME, sharedPreferences.getString(APP_THEME, context.getString(R.string.app_theme_default_value)));
300             preferencesContentValues.put(WEBVIEW_THEME, sharedPreferences.getString(WEBVIEW_THEME, context.getString(R.string.webview_theme_default_value)));
301             preferencesContentValues.put(WIDE_VIEWPORT, sharedPreferences.getBoolean(WIDE_VIEWPORT, true));
302             preferencesContentValues.put(DISPLAY_WEBPAGE_IMAGES, sharedPreferences.getBoolean(DISPLAY_WEBPAGE_IMAGES, true));
303
304             // Insert the preferences into the export database.
305             exportDatabase.insert(PREFERENCES_TABLE, null, preferencesContentValues);
306
307             // Close the export database.
308             exportDatabase.close();
309
310             // Convert the database file to a string.
311             String exportFileString = exportFile.toString();
312
313             // Create strings for the temporary database files.
314             String journalFileString = exportFileString + "-journal";
315
316             // Get `Files` for the temporary database files.
317             File journalFile = new File(journalFileString);
318
319             // Delete the Journal file if it exists.
320             if (journalFile.exists()) {
321                 //noinspection ResultOfMethodCallIgnored
322                 journalFile.delete();
323             }
324
325             // Export successful.
326             return EXPORT_SUCCESSFUL;
327         } catch (Exception exception) {
328             // Return the export error.
329             return exception.toString();
330         }
331     }
332
333     public String importUnencrypted(File importFile, Context context){
334         try {
335             // Create a temporary import file string.
336             String temporaryImportFileString = context.getCacheDir() + "/" + "temporary_import_file";
337
338             // Get a handle for a temporary import file.
339             File temporaryImportFile = new File(temporaryImportFileString);
340
341             // Delete the temporary import file if it already exists.
342             if (temporaryImportFile.exists()) {
343                 //noinspection ResultOfMethodCallIgnored
344                 temporaryImportFile.delete();
345             }
346
347             // Create input and output streams.
348             InputStream importFileInputStream = new FileInputStream(importFile);
349             OutputStream temporaryImportFileOutputStream = new FileOutputStream(temporaryImportFile);
350
351             // Create a byte array.
352             byte[] transferByteArray = new byte[1024];
353
354             // Create an integer to track the number of bytes read.
355             int bytesRead;
356
357             // Copy the import file to the temporary import file.  Once the minimum API >= 26 `Files.copy` can be used instead.
358             while ((bytesRead = importFileInputStream.read(transferByteArray)) > 0) {
359                 temporaryImportFileOutputStream.write(transferByteArray, 0, bytesRead);
360             }
361
362             // Close the file streams.
363             importFileInputStream.close();
364             temporaryImportFileOutputStream.close();
365
366
367             // Get a handle for the shared preference.
368             SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
369
370             // Open the import database.  Once the minimum API >= 27 the file can be opened directly without using the string.
371             SQLiteDatabase importDatabase = SQLiteDatabase.openDatabase(temporaryImportFileString, null, SQLiteDatabase.OPEN_READWRITE);
372
373             // Get the database version.
374             int importDatabaseVersion = importDatabase.getVersion();
375
376             // Upgrade the database if needed.
377             if (importDatabaseVersion < SCHEMA_VERSION) {
378                 switch (importDatabaseVersion){
379                     // Upgrade from schema version 1, Privacy Browser 2.13.
380                     case 1:
381                         // Previously this upgrade added `download_with_external_app` to the Preferences table.  But that is now removed in schema version 10.
382
383                     // Upgrade from schema version 2, Privacy Browser 2.14.
384                     case 2:
385                         // 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
386                         // https://developer.android.com/reference/android/database/sqlite/package-summary
387                         // 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.
388
389                         // Get a cursor with the current `default_font_size` value.
390                         Cursor importDatabasePreferenceCursor = importDatabase.rawQuery("SELECT default_font_size FROM " + PREFERENCES_TABLE, null);
391
392                         // Move to the beginning fo the cursor.
393                         importDatabasePreferenceCursor.moveToFirst();
394
395                         // Get the current value in `default_font_size`.
396                         String fontSize = importDatabasePreferenceCursor.getString(importDatabasePreferenceCursor.getColumnIndex("default_font_size"));
397
398                         // SQL escape the font size.
399                         fontSize = DatabaseUtils.sqlEscapeString(fontSize);
400
401                         // Close the cursor.
402                         importDatabasePreferenceCursor.close();
403
404                         // Create the new font size column.
405                         importDatabase.execSQL("ALTER TABLE " + PREFERENCES_TABLE + " ADD COLUMN " + FONT_SIZE + " TEXT");
406
407                         // Populate the preferences table with the current font size value.
408                         importDatabase.execSQL("UPDATE " + PREFERENCES_TABLE + " SET " + FONT_SIZE + " = '" + fontSize + "'");
409
410                     // Upgrade from schema version 3, Privacy Browser 2.15.
411                     case 3:
412                         // Add the Pinned IP Addresses columns to the domains table.
413                         importDatabase.execSQL("ALTER TABLE " + DomainsDatabaseHelper.DOMAINS_TABLE + " ADD COLUMN " + DomainsDatabaseHelper.PINNED_IP_ADDRESSES + " BOOLEAN");
414                         importDatabase.execSQL("ALTER TABLE " + DomainsDatabaseHelper.DOMAINS_TABLE + " ADD COLUMN " + DomainsDatabaseHelper.IP_ADDRESSES + " TEXT");
415
416                     // Upgrade from schema version 4, Privacy Browser 2.16.
417                     case 4:
418                         // Add the hide and scroll app bar columns to the preferences table.
419                         importDatabase.execSQL("ALTER TABLE " + PREFERENCES_TABLE + " ADD COLUMN " + HIDE_APP_BAR + " BOOLEAN");
420                         importDatabase.execSQL("ALTER TABLE " + PREFERENCES_TABLE + " ADD COLUMN " + SCROLL_APP_BAR + " BOOLEAN");
421
422                         // Get the current hide and scroll app bar settings.
423                         boolean hideAppBar = sharedPreferences.getBoolean(HIDE_APP_BAR, true);
424                         boolean scrollAppBar = sharedPreferences.getBoolean(SCROLL_APP_BAR, true);
425
426                         // Populate the preferences table with the current app bar values.
427                         if (hideAppBar) {
428                             importDatabase.execSQL("UPDATE " + PREFERENCES_TABLE + " SET " + HIDE_APP_BAR + " = " + 1);
429                         } else {
430                             importDatabase.execSQL("UPDATE " + PREFERENCES_TABLE + " SET " + HIDE_APP_BAR + " = " + 0);
431                         }
432
433                         if (scrollAppBar) {
434                             importDatabase.execSQL("UPDATE " + PREFERENCES_TABLE + " SET " + SCROLL_APP_BAR + " = " + 1);
435                         } else {
436                             importDatabase.execSQL("UPDATE " + PREFERENCES_TABLE + " SET " + SCROLL_APP_BAR + " = " + 0);
437                         }
438
439                     // Upgrade from schema version 5, Privacy Browser 2.17.
440                     case 5:
441                         // Add the open intents in new tab column to the preferences table.
442                         importDatabase.execSQL("ALTER TABLE " + PREFERENCES_TABLE + " ADD COLUMN " + OPEN_INTENTS_IN_NEW_TAB + " BOOLEAN");
443
444                         // Get the current open intents in new tab preference.
445                         boolean openIntentsInNewTab = sharedPreferences.getBoolean(OPEN_INTENTS_IN_NEW_TAB, true);
446
447                         // Populate the preferences table with the current open intents value.
448                         if (openIntentsInNewTab) {
449                             importDatabase.execSQL("UPDATE " + PREFERENCES_TABLE + " SET " + OPEN_INTENTS_IN_NEW_TAB + " = " + 1);
450                         } else {
451                             importDatabase.execSQL("UPDATE " + PREFERENCES_TABLE + " SET " + OPEN_INTENTS_IN_NEW_TAB + " = " + 0);
452                         }
453
454                     // Upgrade from schema version 6, Privacy Browser 3.0.
455                     case 6:
456                         // Add the wide viewport column to the domains table.
457                         importDatabase.execSQL("ALTER TABLE " + DomainsDatabaseHelper.DOMAINS_TABLE + " ADD COLUMN " + DomainsDatabaseHelper.WIDE_VIEWPORT + " INTEGER");
458
459                         // Add the Google Analytics, Facebook Click IDs, Twitter AMP redirects, and wide viewport columns to the preferences table.
460                         importDatabase.execSQL("ALTER TABLE " + PREFERENCES_TABLE + " ADD COLUMN " + GOOGLE_ANALYTICS + " BOOLEAN");
461                         importDatabase.execSQL("ALTER TABLE " + PREFERENCES_TABLE + " ADD COLUMN " + FACEBOOK_CLICK_IDS + " BOOLEAN");
462                         importDatabase.execSQL("ALTER TABLE " + PREFERENCES_TABLE + " ADD COLUMN " + TWITTER_AMP_REDIRECTS + " BOOLEAN");
463                         importDatabase.execSQL("ALTER TABLE " + PREFERENCES_TABLE + " ADD COLUMN " + WIDE_VIEWPORT + " BOOLEAN");
464
465                         // Get the current preference values.
466                         boolean googleAnalytics = sharedPreferences.getBoolean(GOOGLE_ANALYTICS, true);
467                         boolean facebookClickIds = sharedPreferences.getBoolean(FACEBOOK_CLICK_IDS, true);
468                         boolean twitterAmpRedirects = sharedPreferences.getBoolean(TWITTER_AMP_REDIRECTS, true);
469                         boolean wideViewport = sharedPreferences.getBoolean(WIDE_VIEWPORT, true);
470
471                         // Populate the preferences with the current Google Analytics value.
472                         if (googleAnalytics) {
473                             importDatabase.execSQL("UPDATE " + PREFERENCES_TABLE + " SET " + GOOGLE_ANALYTICS + " = " + 1);
474                         } else {
475                             importDatabase.execSQL("UPDATE " + PREFERENCES_TABLE + " SET " + GOOGLE_ANALYTICS + " = " + 0);
476                         }
477
478                         // Populate the preferences with the current Facebook Click IDs value.
479                         if (facebookClickIds) {
480                             importDatabase.execSQL("UPDATE " + PREFERENCES_TABLE + " SET " + FACEBOOK_CLICK_IDS + " = " + 1);
481                         } else {
482                             importDatabase.execSQL("UPDATE " + PREFERENCES_TABLE + " SET " + FACEBOOK_CLICK_IDS + " = " + 0);
483                         }
484
485                         // Populate the preferences table with the current Twitter AMP redirects value.
486                         if (twitterAmpRedirects) {
487                             importDatabase.execSQL("UPDATE " + PREFERENCES_TABLE + " SET " + TWITTER_AMP_REDIRECTS + " = " + 1);
488                         } else {
489                             importDatabase.execSQL("UPDATE " + PREFERENCES_TABLE + " SET " + TWITTER_AMP_REDIRECTS + " = " + 0);
490                         }
491
492                         // Populate the preferences table with the current wide viewport value.
493                         if (wideViewport) {
494                             importDatabase.execSQL("UPDATE " + PREFERENCES_TABLE + " SET " + WIDE_VIEWPORT + " = " + 1);
495                         } else {
496                             importDatabase.execSQL("UPDATE " + PREFERENCES_TABLE + " SET " + WIDE_VIEWPORT + " = " + 0);
497                         }
498
499                     // Upgrade from schema version 7, Privacy Browser 3.1.
500                     case 7:
501                         // Add the UltraList column to the domains table.
502                         importDatabase.execSQL("ALTER TABLE " + DomainsDatabaseHelper.DOMAINS_TABLE + " ADD COLUMN " + DomainsDatabaseHelper.ULTRALIST + " BOOLEAN");
503
504                         // Add the UltraList column to the preferences table.
505                         importDatabase.execSQL("ALTER TABLE " + PREFERENCES_TABLE + " ADD COLUMN " + ULTRALIST + " BOOLEAN");
506
507                         // Get the current preference values.
508                         boolean ultraList = sharedPreferences.getBoolean(ULTRALIST, true);
509
510                         // Populate the preferences tables with the current UltraList value.
511                         if (ultraList) {
512                             importDatabase.execSQL("UPDATE " + DomainsDatabaseHelper.DOMAINS_TABLE + " SET " + DomainsDatabaseHelper.ULTRALIST + " = " + 1);
513                             importDatabase.execSQL("UPDATE " + PREFERENCES_TABLE + " SET " + ULTRALIST + " = " + 1);
514                         } else {
515                             importDatabase.execSQL("UPDATE " + DomainsDatabaseHelper.DOMAINS_TABLE + " SET " + DomainsDatabaseHelper.ULTRALIST + " = " + 0);
516                             importDatabase.execSQL("UPDATE " + PREFERENCES_TABLE + " SET " + ULTRALIST + " = " + 0);
517                         }
518
519                     // Upgrade from schema version 8, Privacy Browser 3.2.
520                     case 8:
521                         // Add the new proxy columns to the preferences table.
522                         importDatabase.execSQL("ALTER TABLE " + PREFERENCES_TABLE + " ADD COLUMN " + PROXY + " TEXT");
523                         importDatabase.execSQL("ALTER TABLE " + PREFERENCES_TABLE + " ADD COLUMN " + PROXY_CUSTOM_URL + " TEXT");
524
525                         // Get the current proxy values.
526                         String proxy = sharedPreferences.getString(PROXY, context.getString(R.string.proxy_default_value));
527                         String proxyCustomUrl = sharedPreferences.getString(PROXY_CUSTOM_URL, context.getString(R.string.proxy_custom_url_default_value));
528
529                         // SQL escape the proxy custom URL string.
530                         proxyCustomUrl = DatabaseUtils.sqlEscapeString(proxyCustomUrl);
531
532                         // Populate the preferences table with the current proxy values. The proxy custom URL does not need to be surrounded by `'` because it was SLQ escaped above.
533                         importDatabase.execSQL("UPDATE " + PREFERENCES_TABLE + " SET " + PROXY + " = '" + proxy + "'");
534                         importDatabase.execSQL("UPDATE " + PREFERENCES_TABLE + " SET " + PROXY_CUSTOM_URL + " = " + proxyCustomUrl);
535
536                     // Upgrade from schema version 9, Privacy Browser 3.3.
537                     case 9:
538                         // Add the download location columns to the preferences table.
539                         importDatabase.execSQL("ALTER TABLE " + PREFERENCES_TABLE + " ADD COLUMN " + DOWNLOAD_LOCATION + " TEXT");
540                         importDatabase.execSQL("ALTER TABLE " + PREFERENCES_TABLE + " ADD COLUMN " + DOWNLOAD_CUSTOM_LOCATION + " TEXT");
541
542                         // Get the current download location values.
543                         String downloadLocation = sharedPreferences.getString(DOWNLOAD_LOCATION, context.getString(R.string.download_location_default_value));
544                         String downloadCustomLocation = sharedPreferences.getString(DOWNLOAD_CUSTOM_LOCATION, context.getString(R.string.download_custom_location_default_value));
545
546                         // Populate the preferences table with the current download location values.
547                         importDatabase.execSQL("UPDATE " + PREFERENCES_TABLE + " SET " + DOWNLOAD_LOCATION + " = '" + downloadLocation + "'");
548                         importDatabase.execSQL("UPDATE " + PREFERENCES_TABLE + " SET " + DOWNLOAD_CUSTOM_LOCATION + " = '" + downloadCustomLocation + "'");
549
550                     // Upgrade from schema version 10, Privacy Browser 3.4.
551                     case 10:
552                         // Add the app theme column to the preferences table.
553                         importDatabase.execSQL("ALTER TABLE " + PREFERENCES_TABLE + " ADD COLUMN " + APP_THEME + " TEXT");
554
555                         // Get a cursor for the dark theme preference.
556                         Cursor darkThemePreferencesCursor = importDatabase.rawQuery("SELECT dark_theme FROM " + PREFERENCES_TABLE, null);
557
558                         // Move to the first preference.
559                         darkThemePreferencesCursor.moveToFirst();
560
561                         // Get the old dark theme value, which is in column 0.
562                         int darkTheme = darkThemePreferencesCursor.getInt(0);
563
564                         // Close the dark theme preference cursor.
565                         darkThemePreferencesCursor.close();
566
567                         // Populate the app theme according to the old dark theme preference.
568                         if (darkTheme == 0) {  // A light theme was selected.
569                             // Set the app theme to be the system default.
570                             importDatabase.execSQL("UPDATE " + PREFERENCES_TABLE + " SET " + APP_THEME + " = 'System default'");
571                         } else {  // A dark theme was selected.
572                             // Set the app theme to be dark.
573                             importDatabase.execSQL("UPDATE " + PREFERENCES_TABLE + " SET " + APP_THEME + " = 'Dark'");
574                         }
575
576                         // Add the WebView theme to the domains table.  This defaults to 0, which is `System default`, so a separate step isn't needed to populate the database.
577                         importDatabase.execSQL("ALTER TABLE " + DomainsDatabaseHelper.DOMAINS_TABLE + " ADD COLUMN " + DomainsDatabaseHelper.WEBVIEW_THEME + " INTEGER");
578
579                         // Add the WebView theme to the preferences table.
580                         importDatabase.execSQL("ALTER TABLE " + PREFERENCES_TABLE + " ADD COLUMN " + WEBVIEW_THEME + " TEXT");
581
582                         // Get the WebView theme default value string.
583                         String webViewThemeDefaultValue = context.getString(R.string.webview_theme_default_value);
584
585                         // Set the WebView theme in the preferences table to be the default.
586                         importDatabase.execSQL("UPDATE " + PREFERENCES_TABLE + " SET " + WEBVIEW_THEME + " = " + "'" + webViewThemeDefaultValue + "'");
587
588                     // Upgrade from schema version 11, Privacy Browser 3.5.
589                     case 11:
590                         // Add the clear logcat column to the preferences table.
591                         importDatabase.execSQL("ALTER TABLE " + PREFERENCES_TABLE + " ADD COLUMN " + CLEAR_LOGCAT + " BOOLEAN");
592
593                         // Get the current clear logcat value.
594                         boolean clearLogcat = sharedPreferences.getBoolean(CLEAR_LOGCAT, true);
595
596                         // Populate the preference table with the current clear logcat value.
597                         if (clearLogcat) {
598                             importDatabase.execSQL("UPDATE " + PREFERENCES_TABLE + " SET " + CLEAR_LOGCAT + " = " + 1);
599                         } else {
600                             importDatabase.execSQL("UPDATE " + PREFERENCES_TABLE + " SET " + CLEAR_LOGCAT + " = " + 0);
601                         }
602                 }
603             }
604
605
606             // Get a cursor for the bookmarks table.
607             Cursor importBookmarksCursor = importDatabase.rawQuery("SELECT * FROM " + BookmarksDatabaseHelper.BOOKMARKS_TABLE, null);
608
609             // Delete the current bookmarks database.
610             context.deleteDatabase(BookmarksDatabaseHelper.BOOKMARKS_DATABASE);
611
612             // Create a new bookmarks database.  The `0` specifies the database version, but that is ignored and set instead using a constant in `BookmarksDatabaseHelper`.
613             BookmarksDatabaseHelper bookmarksDatabaseHelper = new BookmarksDatabaseHelper(context, null, null, 0);
614
615             // Move to the first bookmark.
616             importBookmarksCursor.moveToFirst();
617
618             // Copy the data from the import bookmarks cursor into the bookmarks database.
619             for (int i = 0; i < importBookmarksCursor.getCount(); i++) {
620                 // Extract the record from the cursor and store the data in a ContentValues.
621                 ContentValues bookmarksContentValues = new ContentValues();
622                 bookmarksContentValues.put(BookmarksDatabaseHelper.BOOKMARK_NAME, importBookmarksCursor.getString(importBookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_NAME)));
623                 bookmarksContentValues.put(BookmarksDatabaseHelper.BOOKMARK_URL, importBookmarksCursor.getString(importBookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.BOOKMARK_URL)));
624                 bookmarksContentValues.put(BookmarksDatabaseHelper.PARENT_FOLDER, importBookmarksCursor.getString(importBookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.PARENT_FOLDER)));
625                 bookmarksContentValues.put(BookmarksDatabaseHelper.DISPLAY_ORDER, importBookmarksCursor.getInt(importBookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.DISPLAY_ORDER)));
626                 bookmarksContentValues.put(BookmarksDatabaseHelper.IS_FOLDER, importBookmarksCursor.getInt(importBookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.IS_FOLDER)));
627                 bookmarksContentValues.put(BookmarksDatabaseHelper.FAVORITE_ICON, importBookmarksCursor.getBlob(importBookmarksCursor.getColumnIndex(BookmarksDatabaseHelper.FAVORITE_ICON)));
628
629                 // Insert the record into the export database.
630                 bookmarksDatabaseHelper.createBookmark(bookmarksContentValues);
631
632                 // Advance to the next record.
633                 importBookmarksCursor.moveToNext();
634             }
635
636             // Close the bookmarks cursor.
637             importBookmarksCursor.close();
638
639             // Close the bookmarks database.
640             bookmarksDatabaseHelper.close();
641
642
643             // Get a cursor for the domains table.
644             Cursor importDomainsCursor = importDatabase.rawQuery("SELECT * FROM " + DomainsDatabaseHelper.DOMAINS_TABLE + " ORDER BY " + DomainsDatabaseHelper.DOMAIN_NAME + " ASC", null);
645
646             // Delete the current domains database.
647             context.deleteDatabase(DomainsDatabaseHelper.DOMAINS_DATABASE);
648
649             // Create a new domains database.  The `0` specifies the database version, but that is ignored and set instead using a constant in `DomainsDatabaseHelper`.
650             DomainsDatabaseHelper domainsDatabaseHelper = new DomainsDatabaseHelper(context, null, null, 0);
651
652             // Move to the first domain.
653             importDomainsCursor.moveToFirst();
654
655             // Copy the data from the import domains cursor into the domains database.
656             for (int i = 0; i < importDomainsCursor.getCount(); i++) {
657                 // Extract the record from the cursor and store the data in a ContentValues.
658                 ContentValues domainsContentValues = new ContentValues();
659                 domainsContentValues.put(DomainsDatabaseHelper.DOMAIN_NAME, importDomainsCursor.getString(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.DOMAIN_NAME)));
660                 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_JAVASCRIPT, importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_JAVASCRIPT)));
661                 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_FIRST_PARTY_COOKIES, importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FIRST_PARTY_COOKIES)));
662                 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_THIRD_PARTY_COOKIES, importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_THIRD_PARTY_COOKIES)));
663                 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_DOM_STORAGE, importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_DOM_STORAGE)));
664                 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_FORM_DATA, importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FORM_DATA)));
665                 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_EASYLIST, importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_EASYLIST)));
666                 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_EASYPRIVACY, importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_EASYPRIVACY)));
667                 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_FANBOYS_ANNOYANCE_LIST,
668                         importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FANBOYS_ANNOYANCE_LIST)));
669                 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST,
670                         importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_FANBOYS_SOCIAL_BLOCKING_LIST)));
671                 domainsContentValues.put(DomainsDatabaseHelper.ULTRALIST, importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.ULTRALIST)));
672                 domainsContentValues.put(DomainsDatabaseHelper.ENABLE_ULTRAPRIVACY, importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.ENABLE_ULTRAPRIVACY)));
673                 domainsContentValues.put(DomainsDatabaseHelper.BLOCK_ALL_THIRD_PARTY_REQUESTS,
674                         importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.BLOCK_ALL_THIRD_PARTY_REQUESTS)));
675                 domainsContentValues.put(DomainsDatabaseHelper.USER_AGENT, importDomainsCursor.getString(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.USER_AGENT)));
676                 domainsContentValues.put(DomainsDatabaseHelper.FONT_SIZE, importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.FONT_SIZE)));
677                 domainsContentValues.put(DomainsDatabaseHelper.SWIPE_TO_REFRESH, importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.SWIPE_TO_REFRESH)));
678                 domainsContentValues.put(DomainsDatabaseHelper.WEBVIEW_THEME, importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.WEBVIEW_THEME)));
679                 domainsContentValues.put(DomainsDatabaseHelper.WIDE_VIEWPORT, importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.WIDE_VIEWPORT)));
680                 domainsContentValues.put(DomainsDatabaseHelper.DISPLAY_IMAGES, importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.DISPLAY_IMAGES)));
681                 domainsContentValues.put(DomainsDatabaseHelper.PINNED_SSL_CERTIFICATE, importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.PINNED_SSL_CERTIFICATE)));
682                 domainsContentValues.put(DomainsDatabaseHelper.SSL_ISSUED_TO_COMMON_NAME, importDomainsCursor.getString(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_TO_COMMON_NAME)));
683                 domainsContentValues.put(DomainsDatabaseHelper.SSL_ISSUED_TO_ORGANIZATION, importDomainsCursor.getString(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_TO_ORGANIZATION)));
684                 domainsContentValues.put(DomainsDatabaseHelper.SSL_ISSUED_TO_ORGANIZATIONAL_UNIT,
685                         importDomainsCursor.getString(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_TO_ORGANIZATIONAL_UNIT)));
686                 domainsContentValues.put(DomainsDatabaseHelper.SSL_ISSUED_BY_COMMON_NAME, importDomainsCursor.getString(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_BY_COMMON_NAME)));
687                 domainsContentValues.put(DomainsDatabaseHelper.SSL_ISSUED_BY_ORGANIZATION, importDomainsCursor.getString(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_BY_ORGANIZATION)));
688                 domainsContentValues.put(DomainsDatabaseHelper.SSL_ISSUED_BY_ORGANIZATIONAL_UNIT,
689                         importDomainsCursor.getString(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_ISSUED_BY_ORGANIZATIONAL_UNIT)));
690                 domainsContentValues.put(DomainsDatabaseHelper.SSL_START_DATE, importDomainsCursor.getLong(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_START_DATE)));
691                 domainsContentValues.put(DomainsDatabaseHelper.SSL_END_DATE, importDomainsCursor.getLong(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.SSL_END_DATE)));
692                 domainsContentValues.put(DomainsDatabaseHelper.PINNED_IP_ADDRESSES, importDomainsCursor.getInt(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.PINNED_IP_ADDRESSES)));
693                 domainsContentValues.put(DomainsDatabaseHelper.IP_ADDRESSES, importDomainsCursor.getString(importDomainsCursor.getColumnIndex(DomainsDatabaseHelper.IP_ADDRESSES)));
694
695                 // Insert the record into the export database.
696                 domainsDatabaseHelper.addDomain(domainsContentValues);
697
698                 // Advance to the next record.
699                 importDomainsCursor.moveToNext();
700             }
701
702             // Close the domains cursor.
703             importDomainsCursor.close();
704
705             // Close the domains database.
706             domainsDatabaseHelper.close();
707
708
709             // Get a cursor for the preferences table.
710             Cursor importPreferencesCursor = importDatabase.rawQuery("SELECT * FROM " + PREFERENCES_TABLE, null);
711
712             // Move to the first preference.
713             importPreferencesCursor.moveToFirst();
714
715             // Import the preference data.
716             sharedPreferences.edit()
717                     .putBoolean(JAVASCRIPT, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(JAVASCRIPT)) == 1)
718                     .putBoolean(FIRST_PARTY_COOKIES, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(FIRST_PARTY_COOKIES)) == 1)
719                     .putBoolean(THIRD_PARTY_COOKIES, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(THIRD_PARTY_COOKIES)) == 1)
720                     .putBoolean(DOM_STORAGE, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(DOM_STORAGE)) == 1)
721                     // Save form data can be removed once the minimum API >= 26.
722                     .putBoolean(SAVE_FORM_DATA, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(SAVE_FORM_DATA)) == 1)
723                     .putString(USER_AGENT, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndex(USER_AGENT)))
724                     .putString(CUSTOM_USER_AGENT, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndex(CUSTOM_USER_AGENT)))
725                     .putBoolean(INCOGNITO_MODE, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(INCOGNITO_MODE)) == 1)
726                     .putBoolean(DO_NOT_TRACK, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(DO_NOT_TRACK)) == 1)
727                     .putBoolean(ALLOW_SCREENSHOTS, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(ALLOW_SCREENSHOTS)) == 1)
728                     .putBoolean(EASYLIST, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(EASYLIST)) == 1)
729                     .putBoolean(EASYPRIVACY, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(EASYPRIVACY)) == 1)
730                     .putBoolean(FANBOYS_ANNOYANCE_LIST, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(FANBOYS_ANNOYANCE_LIST)) == 1)
731                     .putBoolean(FANBOYS_SOCIAL_BLOCKING_LIST, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(FANBOYS_SOCIAL_BLOCKING_LIST)) == 1)
732                     .putBoolean(ULTRALIST, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(ULTRALIST)) == 1)
733                     .putBoolean(ULTRAPRIVACY, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(ULTRAPRIVACY)) == 1)
734                     .putBoolean(BLOCK_ALL_THIRD_PARTY_REQUESTS, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(BLOCK_ALL_THIRD_PARTY_REQUESTS)) == 1)
735                     .putBoolean(GOOGLE_ANALYTICS, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(GOOGLE_ANALYTICS)) == 1)
736                     .putBoolean(FACEBOOK_CLICK_IDS, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(FACEBOOK_CLICK_IDS)) == 1)
737                     .putBoolean(TWITTER_AMP_REDIRECTS, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(TWITTER_AMP_REDIRECTS)) == 1)
738                     .putString(SEARCH, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndex(SEARCH)))
739                     .putString(SEARCH_CUSTOM_URL, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndex(SEARCH_CUSTOM_URL)))
740                     .putString(PROXY, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndex(PROXY)))
741                     .putString(PROXY_CUSTOM_URL, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndex(PROXY_CUSTOM_URL)))
742                     .putBoolean(FULL_SCREEN_BROWSING_MODE, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(FULL_SCREEN_BROWSING_MODE)) == 1)
743                     .putBoolean(HIDE_APP_BAR, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(HIDE_APP_BAR)) == 1)
744                     .putBoolean(CLEAR_EVERYTHING, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(CLEAR_EVERYTHING)) == 1)
745                     .putBoolean(CLEAR_COOKIES, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(CLEAR_COOKIES)) == 1)
746                     .putBoolean(CLEAR_DOM_STORAGE, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(CLEAR_DOM_STORAGE)) == 1)
747                     // Clear form data can be removed once the minimum API >= 26.
748                     .putBoolean(CLEAR_FORM_DATA, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(CLEAR_FORM_DATA)) == 1)
749                     .putBoolean(CLEAR_LOGCAT, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(CLEAR_LOGCAT)) == 1)
750                     .putBoolean(CLEAR_CACHE, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(CLEAR_CACHE)) == 1)
751                     .putString(HOMEPAGE, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndex(HOMEPAGE)))
752                     .putString(DOWNLOAD_LOCATION, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndex(DOWNLOAD_LOCATION)))
753                     .putString(DOWNLOAD_CUSTOM_LOCATION, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndex(DOWNLOAD_CUSTOM_LOCATION)))
754                     .putString(FONT_SIZE, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndex(FONT_SIZE)))
755                     .putBoolean(OPEN_INTENTS_IN_NEW_TAB, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(OPEN_INTENTS_IN_NEW_TAB)) == 1)
756                     .putBoolean(SWIPE_TO_REFRESH, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(SWIPE_TO_REFRESH)) == 1)
757                     .putBoolean(SCROLL_APP_BAR, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(SCROLL_APP_BAR)) == 1)
758                     .putBoolean(DISPLAY_ADDITIONAL_APP_BAR_ICONS, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(DISPLAY_ADDITIONAL_APP_BAR_ICONS)) == 1)
759                     .putString(APP_THEME, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndex(APP_THEME)))
760                     .putString(WEBVIEW_THEME, importPreferencesCursor.getString(importPreferencesCursor.getColumnIndex(WEBVIEW_THEME)))
761                     .putBoolean(WIDE_VIEWPORT, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(WIDE_VIEWPORT)) == 1)
762                     .putBoolean(DISPLAY_WEBPAGE_IMAGES, importPreferencesCursor.getInt(importPreferencesCursor.getColumnIndex(DISPLAY_WEBPAGE_IMAGES)) == 1)
763                     .apply();
764
765             // Close the preferences cursor.
766             importPreferencesCursor.close();
767
768
769             // Close the import database.
770             importDatabase.close();
771
772             // Create strings for the temporary database files.
773             String shmFileString = temporaryImportFileString + "-shm";
774             String walFileString = temporaryImportFileString + "-wal";
775             String journalFileString = temporaryImportFileString + "-journal";
776
777             // Get `Files` for the temporary database files.
778             File shmFile = new File(shmFileString);
779             File walFile = new File(walFileString);
780             File journalFile = new File(journalFileString);
781
782             // Delete the Shared Memory file if it exists.
783             if (shmFile.exists()) {
784                 //noinspection ResultOfMethodCallIgnored
785                 shmFile.delete();
786             }
787
788             // Delete the Write Ahead Log file if it exists.
789             if (walFile.exists()) {
790                 //noinspection ResultOfMethodCallIgnored
791                 walFile.delete();
792             }
793
794             // Delete the Journal file if it exists.
795             if (journalFile.exists()) {
796                 //noinspection ResultOfMethodCallIgnored
797                 journalFile.delete();
798             }
799
800             // Delete the temporary import file.
801             //noinspection ResultOfMethodCallIgnored
802             temporaryImportFile.delete();
803
804             // Import successful.
805             return IMPORT_SUCCESSFUL;
806         } catch (Exception exception) {
807             // Return the import error.
808             return exception.toString();
809         }
810     }
811 }