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