add import database function

bla

remove unused restart function

add allert dialog and add time to filename
This commit is contained in:
Christian Schabesberger 2018-01-28 21:03:01 +01:00
parent 7146719393
commit 6e8593af91
4 changed files with 185 additions and 82 deletions

View File

@ -1,9 +1,12 @@
package org.schabi.newpipe; package org.schabi.newpipe;
import android.app.AlarmManager;
import android.app.Application; import android.app.Application;
import android.app.NotificationChannel; import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.os.Build; import android.os.Build;
import android.util.Log; import android.util.Log;
@ -116,7 +119,6 @@ public class App extends Application {
}); });
} }
private void initACRA() { private void initACRA() {
try { try {
final ACRAConfiguration acraConfig = new ConfigurationBuilder(this) final ACRAConfiguration acraConfig = new ConfigurationBuilder(this)
@ -149,4 +151,5 @@ public class App extends Application {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.createNotificationChannel(mChannel); mNotificationManager.createNotificationChannel(mChannel);
} }
} }

View File

@ -1,6 +1,8 @@
package org.schabi.newpipe.settings; package org.schabi.newpipe.settings;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.preference.ListPreference; import android.support.v7.preference.ListPreference;
@ -19,93 +21,102 @@ import org.schabi.newpipe.util.FilePickerActivityHelper;
import org.schabi.newpipe.util.KioskTranslator; import org.schabi.newpipe.util.KioskTranslator;
import org.schabi.newpipe.util.ZipHelper; import org.schabi.newpipe.util.ZipHelper;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
import javax.annotation.Nonnull;
public class ContentSettingsFragment extends BasePreferenceFragment { public class ContentSettingsFragment extends BasePreferenceFragment {
private static final int REQUEST_IMPORT_PATH = 80945; private static final int REQUEST_IMPORT_PATH = 8945;
private static final int REQUEST_EXPORT_PATH = 30945; private static final int REQUEST_EXPORT_PATH = 30945;
private String homeDir;
private File databasesDir;
private File newpipe_db;
private File newpipe_db_journal;
@Override @Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
homeDir = getActivity().getApplicationInfo().dataDir;
databasesDir = new File(homeDir + "/databases");
newpipe_db = new File(homeDir + "/databases/newpipe.db");
newpipe_db_journal = new File(homeDir + "/databases/newpipe.db-journal");
addPreferencesFromResource(R.xml.content_settings); addPreferencesFromResource(R.xml.content_settings);
final ListPreference mainPageContentPref = (ListPreference) findPreference(getString(R.string.main_page_content_key)); final ListPreference mainPageContentPref = (ListPreference) findPreference(getString(R.string.main_page_content_key));
mainPageContentPref.setOnPreferenceChangeListener((Preference preference, Object newValueO) -> { mainPageContentPref.setOnPreferenceChangeListener((Preference preference, Object newValueO) -> {
final String newValue = newValueO.toString(); final String newValue = newValueO.toString();
final String mainPrefOldValue = final String mainPrefOldValue =
defaultPreferences.getString(getString(R.string.main_page_content_key), "blank_page"); defaultPreferences.getString(getString(R.string.main_page_content_key), "blank_page");
final String mainPrefOldSummary = getMainPagePrefSummery(mainPrefOldValue, mainPageContentPref); final String mainPrefOldSummary = getMainPagePrefSummery(mainPrefOldValue, mainPageContentPref);
if(newValue.equals(getString(R.string.kiosk_page_key))) { if(newValue.equals(getString(R.string.kiosk_page_key))) {
SelectKioskFragment selectKioskFragment = new SelectKioskFragment(); SelectKioskFragment selectKioskFragment = new SelectKioskFragment();
selectKioskFragment.setOnSelectedLisener(new SelectKioskFragment.OnSelectedLisener() { selectKioskFragment.setOnSelectedLisener((String kioskId, int service_id) -> {
@Override defaultPreferences.edit()
public void onKioskSelected(String kioskId, int service_id) { .putInt(getString(R.string.main_page_selected_service), service_id).apply();
defaultPreferences.edit() defaultPreferences.edit()
.putInt(getString(R.string.main_page_selected_service), service_id).apply(); .putString(getString(R.string.main_page_selectd_kiosk_id), kioskId).apply();
defaultPreferences.edit() String serviceName = "";
.putString(getString(R.string.main_page_selectd_kiosk_id), kioskId).apply(); try {
String serviceName = ""; serviceName = NewPipe.getService(service_id).getServiceInfo().name;
try { } catch (ExtractionException e) {
serviceName = NewPipe.getService(service_id).getServiceInfo().name; onError(e);
} catch (ExtractionException e) { }
onError(e); String kioskName = KioskTranslator.getTranslatedKioskName(kioskId,
} getContext());
String kioskName = KioskTranslator.getTranslatedKioskName(kioskId,
getContext());
String summary = String summary =
String.format(getString(R.string.service_kiosk_string), String.format(getString(R.string.service_kiosk_string),
serviceName, serviceName,
kioskName); kioskName);
mainPageContentPref.setSummary(summary); mainPageContentPref.setSummary(summary);
} });
}); selectKioskFragment.setOnCancelListener(() -> {
selectKioskFragment.setOnCancelListener(new SelectKioskFragment.OnCancelListener() { mainPageContentPref.setSummary(mainPrefOldSummary);
@Override mainPageContentPref.setValue(mainPrefOldValue);
public void onCancel() { });
mainPageContentPref.setSummary(mainPrefOldSummary); selectKioskFragment.show(getFragmentManager(), "select_kiosk");
mainPageContentPref.setValue(mainPrefOldValue); } else if(newValue.equals(getString(R.string.channel_page_key))) {
} SelectChannelFragment selectChannelFragment = new SelectChannelFragment();
}); selectChannelFragment.setOnSelectedLisener((String url, String name, int service) -> {
selectKioskFragment.show(getFragmentManager(), "select_kiosk"); defaultPreferences.edit()
} else if(newValue.equals(getString(R.string.channel_page_key))) { .putInt(getString(R.string.main_page_selected_service), service).apply();
SelectChannelFragment selectChannelFragment = new SelectChannelFragment(); defaultPreferences.edit()
selectChannelFragment.setOnSelectedLisener(new SelectChannelFragment.OnSelectedLisener() { .putString(getString(R.string.main_page_selected_channel_url), url).apply();
@Override defaultPreferences.edit()
public void onChannelSelected(String url, String name, int service) { .putString(getString(R.string.main_page_selected_channel_name), name).apply();
defaultPreferences.edit()
.putInt(getString(R.string.main_page_selected_service), service).apply();
defaultPreferences.edit()
.putString(getString(R.string.main_page_selected_channel_url), url).apply();
defaultPreferences.edit()
.putString(getString(R.string.main_page_selected_channel_name), name).apply();
mainPageContentPref.setSummary(name); mainPageContentPref.setSummary(name);
} });
}); selectChannelFragment.setOnCancelListener(() -> {
selectChannelFragment.setOnCancelListener(new SelectChannelFragment.OnCancelListener() { mainPageContentPref.setSummary(mainPrefOldSummary);
@Override mainPageContentPref.setValue(mainPrefOldValue);
public void onCancel() { });
mainPageContentPref.setSummary(mainPrefOldSummary); selectChannelFragment.show(getFragmentManager(), "select_channel");
mainPageContentPref.setValue(mainPrefOldValue); } else {
} mainPageContentPref.setSummary(getMainPageSummeryByKey(newValue));
}); }
selectChannelFragment.show(getFragmentManager(), "select_channel");
} else {
mainPageContentPref.setSummary(getMainPageSummeryByKey(newValue));
}
defaultPreferences.edit().putBoolean(Constants.KEY_MAIN_PAGE_CHANGE, true).apply(); defaultPreferences.edit().putBoolean(Constants.KEY_MAIN_PAGE_CHANGE, true).apply();
return true; return true;
}); });
Preference importDataPreference = findPreference(getString(R.string.import_data)); Preference importDataPreference = findPreference(getString(R.string.import_data));
importDataPreference.setOnPreferenceClickListener((Preference p) -> { importDataPreference.setOnPreferenceClickListener((Preference p) -> {
@ -113,7 +124,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
.putExtra(FilePickerActivityHelper.EXTRA_ALLOW_MULTIPLE, false) .putExtra(FilePickerActivityHelper.EXTRA_ALLOW_MULTIPLE, false)
.putExtra(FilePickerActivityHelper.EXTRA_ALLOW_CREATE_DIR, false) .putExtra(FilePickerActivityHelper.EXTRA_ALLOW_CREATE_DIR, false)
.putExtra(FilePickerActivityHelper.EXTRA_MODE, FilePickerActivityHelper.MODE_FILE); .putExtra(FilePickerActivityHelper.EXTRA_MODE, FilePickerActivityHelper.MODE_FILE);
startActivityForResult(i, REQUEST_EXPORT_PATH); startActivityForResult(i, REQUEST_IMPORT_PATH);
return true; return true;
}); });
@ -129,7 +140,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
} }
@Override @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { public void onActivityResult(int requestCode, int resultCode, @Nonnull Intent data) {
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);
if (DEBUG) { if (DEBUG) {
Log.d(TAG, "onActivityResult() called with: requestCode = [" + requestCode + "], resultCode = [" + resultCode + "], data = [" + data + "]"); Log.d(TAG, "onActivityResult() called with: requestCode = [" + requestCode + "], resultCode = [" + resultCode + "], data = [" + data + "]");
@ -137,12 +148,19 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
if ((requestCode == REQUEST_IMPORT_PATH || requestCode == REQUEST_EXPORT_PATH) if ((requestCode == REQUEST_IMPORT_PATH || requestCode == REQUEST_EXPORT_PATH)
&& resultCode == Activity.RESULT_OK) { && resultCode == Activity.RESULT_OK) {
String path = data.getData().getPath(); String path = data.getData().getPath();
if(requestCode == REQUEST_EXPORT_PATH) { if (requestCode == REQUEST_EXPORT_PATH) {
exportDatabase(path + "/NewPipeData.zip"); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US);
} else { exportDatabase(path + "/NewPipeData-" + sdf.format(new Date()) + ".zip");
importDatabase(path); } else {
} AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.override_current_data)
.setPositiveButton(android.R.string.ok,
(DialogInterface d, int id) -> importDatabase(path))
.setNegativeButton(android.R.string.cancel,
(DialogInterface d, int id) -> d.cancel());
builder.create().show();
}
} }
} }
@ -151,21 +169,56 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
ZipOutputStream outZip = new ZipOutputStream( ZipOutputStream outZip = new ZipOutputStream(
new BufferedOutputStream( new BufferedOutputStream(
new FileOutputStream(path))); new FileOutputStream(path)));
final String homeDir = getActivity().getApplicationInfo().dataDir; ZipHelper.addFileToZip(outZip, newpipe_db.getPath(), "newpipe.db");
ZipHelper.addFileToZip(outZip, homeDir + "/databases/newpipe.db", "newpipe.db"); ZipHelper.addFileToZip(outZip, newpipe_db_journal.getPath(), "newpipe.db-journal");
ZipHelper.addFileToZip(outZip, homeDir + "/databases/newpipe.db-journal", "newpipe.db-journal");
outZip.close(); outZip.close();
Toast.makeText(getContext(), getString(R.string.export_complete_toast), Toast.LENGTH_SHORT) Toast.makeText(getContext(), R.string.export_complete_toast, Toast.LENGTH_SHORT)
.show(); .show();
} catch (Exception e) { } catch (Exception e) {
onError(e); onError(e);
} }
} }
private void importDatabase(String path) { private void importDatabase(String filePath) {
// check if file is supported
ZipFile zipFile = null;
try {
zipFile = new ZipFile(filePath);
} catch (IOException ioe) {
Toast.makeText(getContext(), R.string.no_valid_zip_file, Toast.LENGTH_SHORT)
.show();
return;
} finally {
try {
zipFile.close();
} catch (Exception e){}
}
try {
ZipInputStream zipIn = new ZipInputStream(
new BufferedInputStream(
new FileInputStream(filePath)));
if (!databasesDir.exists() && !databasesDir.mkdir()) {
throw new Exception("Could not create databases dir");
}
if(!(ZipHelper.extractFileFromZip(zipIn, newpipe_db.getPath(), "newpipe.db")
&& ZipHelper.extractFileFromZip(zipIn, newpipe_db_journal.getPath(), "newpipe.db-journal"))) {
Toast.makeText(getContext(), R.string.could_not_import_all_files, Toast.LENGTH_LONG)
.show();
}
zipIn.close();
// restart app to properly load db
//App.restart(getContext());
System.exit(0);
} catch (Exception e) {
onError(e);
}
} }
@Override @Override
@ -186,8 +239,8 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
getString(R.string.main_page_selected_service), 0)); getString(R.string.main_page_selected_service), 0));
String kioskName = KioskTranslator.getTranslatedKioskName( String kioskName = KioskTranslator.getTranslatedKioskName(
defaultPreferences.getString( defaultPreferences.getString(
getString(R.string.main_page_selectd_kiosk_id), "Trending"), getString(R.string.main_page_selectd_kiosk_id), "Trending"),
getContext()); getContext());
String summary = String summary =

View File

@ -1,8 +1,11 @@
package org.schabi.newpipe.util; package org.schabi.newpipe.util;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
/** /**
@ -30,7 +33,8 @@ public class ZipHelper {
private static final int BUFFER_SIZE = 2048; private static final int BUFFER_SIZE = 2048;
/** /**
* This function helps to create zip files * This function helps to create zip files.
* Caution this will override the original file.
* @param outZip The ZipOutputStream where the data should be stored in * @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 file The path of the file that should be added to zip.
* @param name The path of the file inside the zip. * @param name The path of the file inside the zip.
@ -48,4 +52,43 @@ public class ZipHelper {
} }
inputStream.close(); inputStream.close();
} }
/**
* This will extract data from Zipfiles.
* Caution this will override the original file.
* @param inZip The ZipOutputStream where the data is stored in
* @param file The path of the file on the disk where the data should be extracted to.
* @param name The path of the file inside the zip.
* @return will return true if the file was found within the zip file
* @throws Exception
*/
public static boolean extractFileFromZip(ZipInputStream inZip, String file, String name) throws Exception {
byte data[] = new byte[BUFFER_SIZE];
boolean found = false;
ZipEntry ze;
while((ze = inZip.getNextEntry()) != null) {
if(ze.getName().equals(name)) {
found = true;
// delete old file first
File oldFile = new File(file);
if(oldFile.exists()) {
if(!oldFile.delete()) {
throw new Exception("Could not delete " + file);
}
}
FileOutputStream outFile = new FileOutputStream(file);
int count = 0;
while((count = inZip.read(data)) != -1) {
outFile.write(data, 0, count);
}
outFile.close();
inZip.closeEntry();
}
}
return true;
}
} }

View File

@ -322,6 +322,10 @@
<string name="no_channel_subscribed_yet">No channel subscribed yet</string> <string name="no_channel_subscribed_yet">No channel subscribed yet</string>
<string name="select_a_kiosk">Select a kiosk</string> <string name="select_a_kiosk">Select a kiosk</string>
<string name="export_complete_toast">Export complete</string> <string name="export_complete_toast">Export complete</string>
<string name="import_complete_toast">Import complete</string>
<string name="no_valid_zip_file">No valid Zip file</string>
<string name="could_not_import_all_files">WARNING: Could not import all files.</string>
<string name="override_current_data">This will override your current setup.</string>
<!-- Kiosk Names --> <!-- Kiosk Names -->
<string name="kiosk">Kiosk</string> <string name="kiosk">Kiosk</string>