add DBUtils utility for saving/restoring Serializables

This commit is contained in:
Eric House 2019-05-22 18:51:16 -07:00
parent 95bea0a13f
commit 53e10ebafb
5 changed files with 28 additions and 24 deletions

View file

@ -314,8 +314,7 @@ public class BTInviteDelegate extends InviteDelegate {
{ {
if ( null == sPersisted ) { if ( null == sPersisted ) {
try { try {
String str64 = DBUtils.getStringFor( context, KEY_PERSIST, null ); sPersisted = (Persisted)DBUtils.getSerializableFor( context, KEY_PERSIST );
sPersisted = (Persisted)Utils.string64ToSerializable( str64 );
} catch ( Exception ex ) {} // NPE, de-serialization problems, etc. } catch ( Exception ex ) {} // NPE, de-serialization problems, etc.
if ( null == sPersisted ) { if ( null == sPersisted ) {
@ -326,9 +325,7 @@ public class BTInviteDelegate extends InviteDelegate {
private synchronized static void store( Context context ) private synchronized static void store( Context context )
{ {
String str64 = sPersisted == null DBUtils.setSerializableFor( context, KEY_PERSIST, sPersisted );
? "" : Utils.serializableToString64( sPersisted );
DBUtils.setStringFor( context, KEY_PERSIST, str64 );
} }
// DlgDelegate.DlgClickNotify interface // DlgDelegate.DlgClickNotify interface

View file

@ -43,6 +43,7 @@ import java.util.Map;
public class ConnStatusHandler { public class ConnStatusHandler {
private static final String TAG = ConnStatusHandler.class.getSimpleName(); private static final String TAG = ConnStatusHandler.class.getSimpleName();
private static final String RECS_KEY = TAG + "/recs";
public interface ConnStatusCBacks { public interface ConnStatusCBacks {
public void invalidateParent(); public void invalidateParent();
@ -355,14 +356,10 @@ public class ConnStatusHandler {
{ {
synchronized( ConnStatusHandler.class ) { synchronized( ConnStatusHandler.class ) {
if ( s_records == null ) { if ( s_records == null ) {
String as64 = XWPrefs.getPrefsString( context, s_records = (HashMap<CommsConnType, SuccessRecord[]>)
R.string.key_connstat_data ); DBUtils.getSerializableFor( context, RECS_KEY );
if ( null != as64 && 0 < as64.length() ) {
s_records = (HashMap<CommsConnType,SuccessRecord[]>)Utils.
string64ToSerializable(as64);
}
if ( null == s_records ) { if ( null == s_records ) {
s_records = new HashMap<CommsConnType,SuccessRecord[]>(); s_records = new HashMap<>();
} }
} }
} }
@ -472,9 +469,7 @@ public class ConnStatusHandler {
private static void doSave( Context context ) private static void doSave( Context context )
{ {
synchronized( ConnStatusHandler.class ) { synchronized( ConnStatusHandler.class ) {
String as64 = Utils.serializableToString64( getRecords( context ) ); DBUtils.setSerializableFor( context, RECS_KEY, getRecords( context ) );
XWPrefs.setPrefsString( context, R.string.key_connstat_data,
as64 );
s_needsSave = false; s_needsSave = false;
} }
} }

View file

@ -2517,6 +2517,23 @@ public class DBUtils {
return bytes; return bytes;
} }
public static Serializable getSerializableFor( Context context, String key )
{
Serializable value = null;
String str64 = getStringFor( context, key, "" );
if ( str64 != null ) {
value = (Serializable)Utils.string64ToSerializable( str64 );
}
return value;
}
public static void setSerializableFor( Context context, String key,
Serializable value )
{
String str64 = null == value ? "" : Utils.serializableToString64( value );
setStringFor( context, key, str64 );
}
public static void appendLog( String tag, String msg ) public static void appendLog( String tag, String msg )
{ {
appendLog( XWApp.getContext(), msg ); appendLog( XWApp.getContext(), msg );

View file

@ -471,20 +471,15 @@ public class RelayInviteDelegate extends InviteDelegate {
private void getSavedState() private void getSavedState()
{ {
String dataString = DBUtils.getStringFor( m_activity, RECS_KEY, null ); m_devIDRecs = (ArrayList<DevIDRec>)DBUtils.getSerializableFor( m_activity, RECS_KEY );
if ( null != dataString ) {
m_devIDRecs = (ArrayList<DevIDRec>)Utils.string64ToSerializable( dataString );
}
if ( null == m_devIDRecs ) { if ( null == m_devIDRecs ) {
m_devIDRecs = new ArrayList<DevIDRec>(); m_devIDRecs = new ArrayList<>();
} }
} }
private void saveAndRebuild() private void saveAndRebuild()
{ {
String as64 = Utils.serializableToString64( m_devIDRecs ); DBUtils.setSerializableFor( m_activity, RECS_KEY, m_devIDRecs );
DBUtils.setStringFor( m_activity, RECS_KEY, as64 );
rebuildList( false ); rebuildList( false );
} }

View file

@ -618,7 +618,7 @@ public class Utils {
new ObjectInputStream( new ByteArrayInputStream(bytes) ); new ObjectInputStream( new ByteArrayInputStream(bytes) );
result = ois.readObject(); result = ois.readObject();
} catch ( Exception ex ) { } catch ( Exception ex ) {
Log.d( TAG, ex.getMessage() ); Log.d( TAG, "%s", ex.getMessage() );
} }
return result; return result;
} }