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