mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2025-01-25 16:15:55 +01:00
import export SharedPreferences (#600)
Import and export SharedPreferences i add function for import and export SharedPreferences setting when export or import db execute export or import SharedPreferences for blacklist i preload HashSet
This commit is contained in:
parent
f48729cc64
commit
45eb14684b
@ -20,8 +20,10 @@ package nodomain.freeyourgadget.gadgetbridge.activities;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.SharedPreferences;
|
||||
import android.database.sqlite.SQLiteOpenHelper;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.v4.app.NavUtils;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
@ -33,6 +35,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
||||
import nodomain.freeyourgadget.gadgetbridge.R;
|
||||
@ -40,10 +43,13 @@ import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
||||
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.ImportExportSharedPreferences;
|
||||
|
||||
|
||||
public class DbManagementActivity extends GBActivity {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(DbManagementActivity.class);
|
||||
private static SharedPreferences sharedPrefs;
|
||||
private ImportExportSharedPreferences shared_file = new ImportExportSharedPreferences();
|
||||
|
||||
private Button exportDBButton;
|
||||
private Button importDBButton;
|
||||
@ -95,6 +101,8 @@ public class DbManagementActivity extends GBActivity {
|
||||
deleteActivityDatabase();
|
||||
}
|
||||
});
|
||||
|
||||
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
}
|
||||
|
||||
private boolean hasOldActivityDatabase() {
|
||||
@ -110,8 +118,33 @@ public class DbManagementActivity extends GBActivity {
|
||||
return getString(R.string.dbmanagementactivvity_cannot_access_export_path);
|
||||
}
|
||||
|
||||
private void exportShared() {
|
||||
// BEGIN EXAMPLE
|
||||
File myPath = null;
|
||||
try {
|
||||
myPath = FileUtils.getExternalFilesDir();
|
||||
File myFile = new File(myPath, "Export_preference");
|
||||
shared_file.exportToFile(sharedPrefs,myFile,null);
|
||||
} catch (IOException ex) {
|
||||
GB.toast(this, getString(R.string.dbmanagementactivity_error_exporting_shared, ex.getMessage()), Toast.LENGTH_LONG, GB.ERROR, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private void importShared() {
|
||||
// BEGIN EXAMPLE
|
||||
File myPath = null;
|
||||
try {
|
||||
myPath = FileUtils.getExternalFilesDir();
|
||||
File myFile = new File(myPath, "Export_preference");
|
||||
shared_file.importFromFile(sharedPrefs,myFile );
|
||||
} catch (Exception ex) {
|
||||
GB.toast(DbManagementActivity.this, getString(R.string.dbmanagementactivity_error_importing_db, ex.getMessage()), Toast.LENGTH_LONG, GB.ERROR, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private void exportDB() {
|
||||
try (DBHandler dbHandler = GBApplication.acquireDB()) {
|
||||
exportShared();
|
||||
DBHelper helper = new DBHelper(this);
|
||||
File dir = FileUtils.getExternalFilesDir();
|
||||
File destFile = helper.exportDB(dbHandler, dir);
|
||||
@ -130,6 +163,7 @@ public class DbManagementActivity extends GBActivity {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
try (DBHandler dbHandler = GBApplication.acquireDB()) {
|
||||
importShared();
|
||||
DBHelper helper = new DBHelper(DbManagementActivity.this);
|
||||
File dir = FileUtils.getExternalFilesDir();
|
||||
SQLiteOpenHelper sqLiteOpenHelper = dbHandler.getHelper();
|
||||
|
@ -0,0 +1,131 @@
|
||||
package nodomain.freeyourgadget.gadgetbridge.util;
|
||||
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
import org.xmlpull.v1.XmlSerializer;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
import android.util.Xml;
|
||||
|
||||
import static nodomain.freeyourgadget.gadgetbridge.GBApplication.blacklist;
|
||||
|
||||
public class ImportExportSharedPreferences {
|
||||
|
||||
private static final String BOOLEAN = Boolean.class.getSimpleName();
|
||||
private static final String FLOAT = Float.class.getSimpleName();
|
||||
private static final String INTEGER = Integer.class.getSimpleName();
|
||||
private static final String LONG = Long.class.getSimpleName();
|
||||
private static final String STRING = String.class.getSimpleName();
|
||||
private static final String HASTSET = HashSet.class.getSimpleName();
|
||||
|
||||
private static final String NAME = "name";
|
||||
private static final String PREFERENCES = "preferences";
|
||||
|
||||
public static void exportToFile(SharedPreferences sharedPreferences, File outFile,
|
||||
Set<String> doNotExport) throws IOException {
|
||||
export(sharedPreferences, new FileWriter(outFile), doNotExport);
|
||||
}
|
||||
|
||||
|
||||
public static void export(SharedPreferences sharedPreferences, Writer writer,
|
||||
Set<String> doNotExport) throws IOException {
|
||||
XmlSerializer serializer = Xml.newSerializer();
|
||||
serializer.setOutput(writer);
|
||||
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
|
||||
serializer.startDocument("UTF-8", true);
|
||||
serializer.startTag("", PREFERENCES);
|
||||
for (Map.Entry<String, ?> entry : sharedPreferences.getAll().entrySet()) {
|
||||
String key = entry.getKey();
|
||||
if (doNotExport != null && doNotExport.contains(key)) continue;
|
||||
|
||||
Object valueObject = entry.getValue();
|
||||
// Skip this entry if the value is null;
|
||||
if (valueObject == null) continue;
|
||||
|
||||
String valueType = valueObject.getClass().getSimpleName();
|
||||
String value = valueObject.toString();
|
||||
serializer.startTag("", valueType);
|
||||
serializer.attribute("", NAME, key);
|
||||
serializer.text(value);
|
||||
serializer.endTag("", valueType);
|
||||
|
||||
}
|
||||
serializer.endTag("", PREFERENCES);
|
||||
serializer.endDocument();
|
||||
writer.close();
|
||||
}
|
||||
|
||||
public static boolean importFromFile(SharedPreferences sharedPreferences, File inFile)
|
||||
throws Exception {
|
||||
return importFromReader(sharedPreferences, new FileReader(inFile));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param sharedPreferences
|
||||
* @param in
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static boolean importFromReader(SharedPreferences sharedPreferences, Reader in)
|
||||
throws Exception {
|
||||
SharedPreferences.Editor editor = sharedPreferences.edit();
|
||||
editor.clear();
|
||||
XmlPullParser parser = Xml.newPullParser();
|
||||
parser.setInput(in);
|
||||
int eventType = parser.getEventType();
|
||||
String name = null;
|
||||
String key = null;
|
||||
while (eventType != XmlPullParser.END_DOCUMENT) {
|
||||
switch (eventType) {
|
||||
case XmlPullParser.START_TAG:
|
||||
name = parser.getName();
|
||||
key = parser.getAttributeValue("", NAME);
|
||||
break;
|
||||
case XmlPullParser.TEXT:
|
||||
// The parser is reading text outside an element if name is null,
|
||||
// so simply ignore this text part (which is usually something like '\n')
|
||||
if (name == null) break;
|
||||
String text = parser.getText();
|
||||
if (BOOLEAN.equals(name)) {
|
||||
editor.putBoolean(key, Boolean.parseBoolean(text));
|
||||
} else if (FLOAT.equals(name)) {
|
||||
editor.putFloat(key, Float.parseFloat(text));
|
||||
} else if (INTEGER.equals(name)) {
|
||||
editor.putInt(key, Integer.parseInt(text));
|
||||
} else if (LONG.equals(name)) {
|
||||
editor.putLong(key, Long.parseLong(text));
|
||||
} else if (STRING.equals(name)) {
|
||||
editor.putString(key, text);
|
||||
} else if (HASTSET.equals(name)) {
|
||||
if (key.equals("package_blacklist")) {
|
||||
blacklist.clear();
|
||||
text=text.replace("[","").replace("]","");
|
||||
for (int z=0;z<text.split(",").length;z++){
|
||||
blacklist.add(text.split(",")[z].trim());
|
||||
}
|
||||
editor.putStringSet(key, blacklist);
|
||||
}
|
||||
} else if (!PREFERENCES.equals(name)) {
|
||||
throw new Exception("Unkown type " + name);
|
||||
}
|
||||
break;
|
||||
case XmlPullParser.END_TAG:
|
||||
name = null;
|
||||
break;
|
||||
}
|
||||
eventType = parser.next();
|
||||
}
|
||||
return editor.commit();
|
||||
}
|
||||
}
|
@ -370,10 +370,12 @@
|
||||
<string name="dbmanagementactivvity_cannot_access_export_path">Cannot access export path. Please contact the developers.</string>
|
||||
<string name="dbmanagementactivity_exported_to">Exported to: %1$s</string>
|
||||
<string name="dbmanagementactivity_error_exporting_db">"Error exporting DB: %1$s"</string>
|
||||
<string name="dbmanagementactivity_error_exporting_shared">"Error exporting preference: %1$s"</string>
|
||||
<string name="dbmanagementactivity_import_data_title">Import Data?</string>
|
||||
<string name="dbmanagementactivity_overwrite_database_confirmation">Really overwrite the current database? All your current activity data (if any) will be lost.</string>
|
||||
<string name="dbmanagementactivity_import_successful">Import successful.</string>
|
||||
<string name="dbmanagementactivity_error_importing_db">"Error importing DB: %1$s"</string>
|
||||
<string name="dbmanagementactivity_error_importing_shared">"Error importing preference: %1$s"</string>
|
||||
<string name="dbmanagementactivity_delete_activity_data_title">Delete Activity Data?</string>
|
||||
<string name="dbmanagementactivity_really_delete_entire_db">Really delete the entire database? All your activity data and information about your devices will be lost.</string>
|
||||
<string name="dbmanagementactivity_database_successfully_deleted">Data successfully deleted.</string>
|
||||
|
Loading…
Reference in New Issue
Block a user