package com.asksven.android.common.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Map;
import java.util.Map.Entry;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class SharedPreferencesUtils {
public static boolean saveSharedPreferencesToFile(SharedPreferences prefs, String file)
{
boolean res = false;
ObjectOutputStream output = null;
File dst = null;
try
{
dst = new File(file);
output = new ObjectOutputStream(new FileOutputStream(dst));
output.writeObject(prefs.getAll());
res = true;
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (output != null)
{
output.flush();
output.close();
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
return res;
}
@SuppressWarnings({ "unchecked" })
public static boolean loadSharedPreferencesFromFile(SharedPreferences prefs, String file)
{
boolean res = false;
ObjectInputStream input = null;
File src = null;
try
{
src = new File(file);
input = new ObjectInputStream(new FileInputStream(src));
Editor prefEdit = prefs.edit();
prefEdit.clear();
Map<String, ?> entries = (Map<String, ?>) input.readObject();
for (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();
res = true;
}
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();
}
}
return res;
}
}