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

View file

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

View file

@ -2517,6 +2517,23 @@ public class DBUtils {
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 )
{
appendLog( XWApp.getContext(), msg );

View file

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

View file

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