Added settings export

This commit is contained in:
Somethingweirdhere 2018-04-19 01:31:25 +02:00
parent d9e616beee
commit dcdb2c323e
3 changed files with 129 additions and 28 deletions

View file

@ -4,7 +4,9 @@ import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v7.preference.ListPreference; import android.support.v7.preference.ListPreference;
@ -30,15 +32,21 @@ import java.io.BufferedInputStream;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import java.util.zip.ZipFile; import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream; import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
import static android.content.Context.MODE_PRIVATE;
public class ContentSettingsFragment extends BasePreferenceFragment { public class ContentSettingsFragment extends BasePreferenceFragment {
private static final int REQUEST_IMPORT_PATH = 8945; private static final int REQUEST_IMPORT_PATH = 8945;
@ -48,6 +56,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
private File databasesDir; private File databasesDir;
private File newpipe_db; private File newpipe_db;
private File newpipe_db_journal; private File newpipe_db_journal;
private File newpipe_settings;
private String thumbnailLoadToggleKey; private String thumbnailLoadToggleKey;
@ -79,6 +88,8 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
databasesDir = new File(homeDir + "/databases"); databasesDir = new File(homeDir + "/databases");
newpipe_db = new File(homeDir + "/databases/newpipe.db"); newpipe_db = new File(homeDir + "/databases/newpipe.db");
newpipe_db_journal = new File(homeDir + "/databases/newpipe.db-journal"); newpipe_db_journal = new File(homeDir + "/databases/newpipe.db-journal");
newpipe_settings = new File(homeDir + "/databases/newpipe.settings");
newpipe_settings.delete();
addPreferencesFromResource(R.xml.content_settings); addPreferencesFromResource(R.xml.content_settings);
@ -197,7 +208,9 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
new FileOutputStream(path))); new FileOutputStream(path)));
ZipHelper.addFileToZip(outZip, newpipe_db.getPath(), "newpipe.db"); ZipHelper.addFileToZip(outZip, newpipe_db.getPath(), "newpipe.db");
ZipHelper.addFileToZip(outZip, newpipe_db_journal.getPath(), "newpipe.db-journal"); ZipHelper.addFileToZip(outZip, newpipe_db_journal.getPath(), "newpipe.db-journal");
//add settings saveSharedPreferencesToFile(newpipe_settings);
ZipHelper.addFileToZip(outZip, newpipe_settings.getPath(), "newpipe.settings");
outZip.close(); outZip.close();
Toast.makeText(getContext(), R.string.export_complete_toast, Toast.LENGTH_SHORT) Toast.makeText(getContext(), R.string.export_complete_toast, Toast.LENGTH_SHORT)
@ -207,6 +220,29 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
} }
} }
private void saveSharedPreferencesToFile(File dst) {
ObjectOutputStream output = null;
try {
output = new ObjectOutputStream(new FileOutputStream(dst));
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getContext());
output.writeObject(pref.getAll());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (output != null) {
output.flush();
output.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
private void importDatabase(String filePath) { private void importDatabase(String filePath) {
// check if file is supported // check if file is supported
ZipFile zipFile = null; ZipFile zipFile = null;
@ -223,30 +259,89 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
} }
try { try {
ZipInputStream zipIn = new ZipInputStream(
new BufferedInputStream(
new FileInputStream(filePath)));
if (!databasesDir.exists() && !databasesDir.mkdir()) { if (!databasesDir.exists() && !databasesDir.mkdir()) {
throw new Exception("Could not create databases dir"); throw new Exception("Could not create databases dir");
} }
if(!(ZipHelper.extractFileFromZip(zipIn, newpipe_db.getPath(), "newpipe.db") if(!(ZipHelper.extractFileFromZip(filePath, newpipe_db.getPath(), "newpipe.db")
&& ZipHelper.extractFileFromZip(zipIn, newpipe_db_journal.getPath(), "newpipe.db-journal"))) { && ZipHelper.extractFileFromZip(filePath, newpipe_db_journal.getPath(), "newpipe.db-journal"))) {
Toast.makeText(getContext(), R.string.could_not_import_all_files, Toast.LENGTH_LONG) Toast.makeText(getContext(), R.string.could_not_import_all_files, Toast.LENGTH_LONG)
.show(); .show();
} }
zipIn.close(); //If settings file exist, ask if it should be imported.
if(ZipHelper.extractFileFromZip(filePath, newpipe_settings.getPath(), "newpipe.settings")) {
AlertDialog.Builder alert = new AlertDialog.Builder(getContext());
alert.setTitle(R.string.import_settings);
alert.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
// restart app to properly load db // restart app to properly load db
//App.restart(getContext());
System.exit(0); System.exit(0);
}
});
alert.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
loadSharedPreferences(newpipe_settings);
// restart app to properly load db
System.exit(0);
}
});
alert.show();
} else {
// restart app to properly load db
System.exit(0);
}
} catch (Exception e) { } catch (Exception e) {
onError(e); onError(e);
} }
} }
private void loadSharedPreferences(File src) {
ObjectInputStream input = null;
try {
input = new ObjectInputStream(new FileInputStream(src));
SharedPreferences.Editor prefEdit = PreferenceManager.getDefaultSharedPreferences(getContext()).edit();
prefEdit.clear();
Map<String, ?> entries = (Map<String, ?>) input.readObject();
for (Map.Entry<String, ?> entry : entries.entrySet()) {
Object v = entry.getValue();
String key = entry.getKey();
if (v instanceof Boolean)
prefEdit.putBoolean(key, ((Boolean) v).booleanValue());
else if (v instanceof Float)
prefEdit.putFloat(key, ((Float) v).floatValue());
else if (v instanceof Integer)
prefEdit.putInt(key, ((Integer) v).intValue());
else if (v instanceof Long)
prefEdit.putLong(key, ((Long) v).longValue());
else if (v instanceof String)
prefEdit.putString(key, ((String) v));
}
prefEdit.commit();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally {
try {
if (input != null) {
input.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();

View file

@ -62,7 +62,12 @@ public class ZipHelper {
* @return will return true if the file was found within the zip file * @return will return true if the file was found within the zip file
* @throws Exception * @throws Exception
*/ */
public static boolean extractFileFromZip(ZipInputStream inZip, String file, String name) throws Exception { public static boolean extractFileFromZip(String filePath, String file, String name) throws Exception {
ZipInputStream inZip = new ZipInputStream(
new BufferedInputStream(
new FileInputStream(filePath)));
byte data[] = new byte[BUFFER_SIZE]; byte data[] = new byte[BUFFER_SIZE];
boolean found = false; boolean found = false;
@ -89,6 +94,6 @@ public class ZipHelper {
inZip.closeEntry(); inZip.closeEntry();
} }
} }
return true; return found;
} }
} }

View file

@ -359,6 +359,7 @@
<string name="no_valid_zip_file">No valid ZIP file</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="could_not_import_all_files">Warning: Could not import all files.</string>
<string name="override_current_data">This will override your current setup.</string> <string name="override_current_data">This will override your current setup.</string>
<string name="import_settings">Do you want to also import settings?</string>
<!-- Kiosk Names --> <!-- Kiosk Names -->
<string name="kiosk">Kiosk</string> <string name="kiosk">Kiosk</string>