From 7146719393b9dda67f00109a878db577c625ae48 Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Sun, 28 Jan 2018 19:02:34 +0100 Subject: [PATCH] add export newipe db function --- .../settings/ContentSettingsFragment.java | 79 +++++++++++++++++-- .../settings/DownloadSettingsFragment.java | 3 +- .../org/schabi/newpipe/util/ZipHelper.java | 51 ++++++++++++ app/src/main/res/values/settings_keys.xml | 2 + app/src/main/res/values/strings.xml | 5 ++ app/src/main/res/xml/content_settings.xml | 10 +++ 6 files changed, 144 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/util/ZipHelper.java diff --git a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java index 2cda95987..31eaeb601 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java @@ -1,9 +1,12 @@ package org.schabi.newpipe.settings; import android.app.Activity; +import android.content.Intent; import android.os.Bundle; import android.support.v7.preference.ListPreference; import android.support.v7.preference.Preference; +import android.util.Log; +import android.widget.Toast; import org.schabi.newpipe.R; import org.schabi.newpipe.extractor.NewPipe; @@ -12,19 +15,26 @@ import org.schabi.newpipe.extractor.exceptions.ExtractionException; import org.schabi.newpipe.report.ErrorActivity; import org.schabi.newpipe.report.UserAction; import org.schabi.newpipe.util.Constants; +import org.schabi.newpipe.util.FilePickerActivityHelper; import org.schabi.newpipe.util.KioskTranslator; +import org.schabi.newpipe.util.ZipHelper; + +import java.io.BufferedOutputStream; +import java.io.FileOutputStream; +import java.util.zip.ZipOutputStream; public class ContentSettingsFragment extends BasePreferenceFragment { + private static final int REQUEST_IMPORT_PATH = 80945; + private static final int REQUEST_EXPORT_PATH = 30945; + @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { addPreferencesFromResource(R.xml.content_settings); final ListPreference mainPageContentPref = (ListPreference) findPreference(getString(R.string.main_page_content_key)); - mainPageContentPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object newValueO) { + mainPageContentPref.setOnPreferenceChangeListener((Preference preference, Object newValueO) -> { final String newValue = newValueO.toString(); final String mainPrefOldValue = @@ -95,8 +105,67 @@ public class ContentSettingsFragment extends BasePreferenceFragment { defaultPreferences.edit().putBoolean(Constants.KEY_MAIN_PAGE_CHANGE, true).apply(); return true; - } - }); + }); + + Preference importDataPreference = findPreference(getString(R.string.import_data)); + importDataPreference.setOnPreferenceClickListener((Preference p) -> { + Intent i = new Intent(getActivity(), FilePickerActivityHelper.class) + .putExtra(FilePickerActivityHelper.EXTRA_ALLOW_MULTIPLE, false) + .putExtra(FilePickerActivityHelper.EXTRA_ALLOW_CREATE_DIR, false) + .putExtra(FilePickerActivityHelper.EXTRA_MODE, FilePickerActivityHelper.MODE_FILE); + startActivityForResult(i, REQUEST_EXPORT_PATH); + return true; + }); + + Preference exportDataPreference = findPreference(getString(R.string.export_data)); + exportDataPreference.setOnPreferenceClickListener((Preference p) -> { + Intent i = new Intent(getActivity(), FilePickerActivityHelper.class) + .putExtra(FilePickerActivityHelper.EXTRA_ALLOW_MULTIPLE, false) + .putExtra(FilePickerActivityHelper.EXTRA_ALLOW_CREATE_DIR, true) + .putExtra(FilePickerActivityHelper.EXTRA_MODE, FilePickerActivityHelper.MODE_DIR); + startActivityForResult(i, REQUEST_EXPORT_PATH); + return true; + }); + } + + @Override + public void onActivityResult(int requestCode, int resultCode, Intent data) { + super.onActivityResult(requestCode, resultCode, data); + if (DEBUG) { + Log.d(TAG, "onActivityResult() called with: requestCode = [" + requestCode + "], resultCode = [" + resultCode + "], data = [" + data + "]"); + } + + if ((requestCode == REQUEST_IMPORT_PATH || requestCode == REQUEST_EXPORT_PATH) + && resultCode == Activity.RESULT_OK) { + String path = data.getData().getPath(); + if(requestCode == REQUEST_EXPORT_PATH) { + exportDatabase(path + "/NewPipeData.zip"); + } else { + importDatabase(path); + } + } + } + + private void exportDatabase(String path) { + try { + ZipOutputStream outZip = new ZipOutputStream( + new BufferedOutputStream( + new FileOutputStream(path))); + final String homeDir = getActivity().getApplicationInfo().dataDir; + ZipHelper.addFileToZip(outZip, homeDir + "/databases/newpipe.db", "newpipe.db"); + ZipHelper.addFileToZip(outZip, homeDir + "/databases/newpipe.db-journal", "newpipe.db-journal"); + + outZip.close(); + + Toast.makeText(getContext(), getString(R.string.export_complete_toast), Toast.LENGTH_SHORT) + .show(); + } catch (Exception e) { + onError(e); + } + } + + private void importDatabase(String path) { + } @Override diff --git a/app/src/main/java/org/schabi/newpipe/settings/DownloadSettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/DownloadSettingsFragment.java index 9a43374a5..9a065d9d8 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/DownloadSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/DownloadSettingsFragment.java @@ -46,7 +46,8 @@ public class DownloadSettingsFragment extends BasePreferenceFragment { Log.d(TAG, "onPreferenceTreeClick() called with: preference = [" + preference + "]"); } - if (preference.getKey().equals(DOWNLOAD_PATH_PREFERENCE) || preference.getKey().equals(DOWNLOAD_PATH_AUDIO_PREFERENCE)) { + if (preference.getKey().equals(DOWNLOAD_PATH_PREFERENCE) + || preference.getKey().equals(DOWNLOAD_PATH_AUDIO_PREFERENCE)) { Intent i = new Intent(getActivity(), FilePickerActivityHelper.class) .putExtra(FilePickerActivityHelper.EXTRA_ALLOW_MULTIPLE, false) .putExtra(FilePickerActivityHelper.EXTRA_ALLOW_CREATE_DIR, true) diff --git a/app/src/main/java/org/schabi/newpipe/util/ZipHelper.java b/app/src/main/java/org/schabi/newpipe/util/ZipHelper.java new file mode 100644 index 000000000..c415c9917 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/util/ZipHelper.java @@ -0,0 +1,51 @@ +package org.schabi.newpipe.util; + +import java.io.BufferedInputStream; +import java.io.FileInputStream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +/** + * Created by Christian Schabesberger on 28.01.18. + * Copyright 2018 Christian Schabesberger + * ZipHelper.java is part of NewPipe + * + * License: GPL-3.0+ + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +public class ZipHelper { + + private static final int BUFFER_SIZE = 2048; + + /** + * This function helps to create zip files + * @param outZip The ZipOutputStream where the data should be stored in + * @param file The path of the file that should be added to zip. + * @param name The path of the file inside the zip. + * @throws Exception + */ + public static void addFileToZip(ZipOutputStream outZip, String file, String name) throws Exception { + byte data[] = new byte[BUFFER_SIZE]; + FileInputStream fi = new FileInputStream(file); + BufferedInputStream inputStream = new BufferedInputStream(fi, BUFFER_SIZE); + ZipEntry entry = new ZipEntry(name); + outZip.putNextEntry(entry); + int count; + while((count = inputStream.read(data, 0, BUFFER_SIZE)) != -1) { + outZip.write(data, 0, count); + } + inputStream.close(); + } +} diff --git a/app/src/main/res/values/settings_keys.xml b/app/src/main/res/values/settings_keys.xml index 7a34d296b..3057eeaca 100644 --- a/app/src/main/res/values/settings_keys.xml +++ b/app/src/main/res/values/settings_keys.xml @@ -131,6 +131,8 @@ main_page_selected_channel_name main_page_selected_channel_url main_page_selectd_kiosk_id + import_data + export_data file_rename diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6d5e48d2d..4368732f4 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -134,6 +134,10 @@ Switch to Popup Switch to Main + Import database + Export database + Will override your current history and subscriptions + Export history, subscriptions and playlists. Error Network error @@ -317,6 +321,7 @@ Select a channel No channel subscribed yet Select a kiosk + Export complete Kiosk diff --git a/app/src/main/res/xml/content_settings.xml b/app/src/main/res/xml/content_settings.xml index 22269eef6..20099d5c0 100644 --- a/app/src/main/res/xml/content_settings.xml +++ b/app/src/main/res/xml/content_settings.xml @@ -37,4 +37,14 @@ android:key="@string/main_page_content_key" android:title="@string/main_page_content" android:summary="%s"/> + + + +