save and restore prefs as part of saving/restoring games

A bit of testing says this works (to allow somebody moving to a new
phone to export and then import sharedpreferences in addition to the
games DB. There may be race conditions, but it probably works better
than nothing.
This commit is contained in:
Eric House 2022-06-06 20:49:58 -07:00
parent 4f3decc1c5
commit 2a6d1a0af2
2 changed files with 44 additions and 1 deletions

View file

@ -1853,11 +1853,16 @@ public class DBUtils {
public static boolean loadDB( Context context )
{
return copyGameDB( context, false );
boolean success = copyGameDB( context, false );
if ( success ) {
PrefsDelegate.loadPrefs( context );
}
return success;
}
public static boolean saveDB( Context context )
{
PrefsDelegate.savePrefs( context );
return copyGameDB( context, true );
}

View file

@ -47,6 +47,7 @@ public class PrefsDelegate extends DelegateBase
implements SharedPreferences.OnSharedPreferenceChangeListener,
View.OnClickListener, PopupMenu.OnMenuItemClickListener {
private static final String TAG = PrefsDelegate.class.getSimpleName();
private static final String PREFS_KEY = TAG + "/prefs";
private XWActivity mActivity;
private PreferenceFragmentCompat mFragment;
@ -365,4 +366,41 @@ public class PrefsDelegate extends DelegateBase
PreferenceManager.setDefaultValues( context, id, mustCheck );
}
}
static void savePrefs( Context context )
{
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences( context );
Map<String, ?> all = prefs.getAll();
HashMap<String, Object> copy = new HashMap<>();
for ( String key : all.keySet() ) {
copy.put( key, all.get(key) );
}
DBUtils.setSerializableFor( context, PREFS_KEY, copy );
}
static void loadPrefs( Context context ) {
HashMap<String, Object> map = (HashMap<String, Object>)DBUtils
.getSerializableFor( context, PREFS_KEY );
if ( null != map ) {
SharedPreferences.Editor editor =
PreferenceManager.getDefaultSharedPreferences( context )
.edit();
for ( String key : map.keySet() ) {
Object value = map.get( key );
if ( value instanceof Boolean ) {
editor.putBoolean( key, (Boolean)value );
} else if ( value instanceof String ) {
editor.putString( key, (String)value );
} else if ( value instanceof Integer ) {
editor.putInt( key, (Integer)value );
} else if ( value instanceof Long ) {
editor.putLong( key, (Long)value );
} else {
Log.d( TAG, "unexpected class: %s", value.getClass().getName() );
Assert.failDbg();
}
}
editor.commit();
}
}
}