mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-02-04 20:46:28 +01:00
implement deletion; save with new serialization util
Add new Utils methods that turn Serializable objects into B64-encoded Strings, and vice versa. Use in place of existing code, and use anew to store the array of DeviceID records. Implement the "Delete checked" button.
This commit is contained in:
parent
7eb9458a78
commit
a71d60db63
5 changed files with 92 additions and 77 deletions
|
@ -39,10 +39,6 @@ import org.eehouse.android.xw4.jni.CommsAddrRec.CommsConnTypeSet;
|
|||
import org.eehouse.android.xw4.jni.XwJNI;
|
||||
import org.eehouse.android.xw4.loc.LocUtils;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
@ -123,9 +119,8 @@ public class ConnStatusHandler {
|
|||
|
||||
private ConnStatusHandler() {}
|
||||
|
||||
private static Map<CommsConnType,SuccessRecord[]> s_records =
|
||||
private static HashMap<CommsConnType,SuccessRecord[]> s_records =
|
||||
new HashMap<CommsConnType,SuccessRecord[]>();
|
||||
private static Class s_lockObj = ConnStatusHandler.class;
|
||||
private static boolean s_needsSave = false;
|
||||
|
||||
public static void setRect( int left, int top, int right, int bottom )
|
||||
|
@ -172,7 +167,7 @@ public class ConnStatusHandler {
|
|||
} else {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
String tmp;
|
||||
synchronized( s_lockObj ) {
|
||||
synchronized( ConnStatusHandler.class ) {
|
||||
sb.append( LocUtils.getString( context,
|
||||
R.string.connstat_net_fmt,
|
||||
connTypes.toString( context, true )));
|
||||
|
@ -253,7 +248,7 @@ public class ConnStatusHandler {
|
|||
cbacks = s_cbacks;
|
||||
}
|
||||
|
||||
synchronized( s_lockObj ) {
|
||||
synchronized( ConnStatusHandler.class ) {
|
||||
SuccessRecord record = recordFor( connType, isIn );
|
||||
record.update( success );
|
||||
}
|
||||
|
@ -288,7 +283,7 @@ public class ConnStatusHandler {
|
|||
CommsConnTypeSet connTypes, boolean isSolo )
|
||||
{
|
||||
if ( !isSolo && null != s_rect ) {
|
||||
synchronized( s_lockObj ) {
|
||||
synchronized( ConnStatusHandler.class ) {
|
||||
Rect scratchR = new Rect( s_rect );
|
||||
int quarterHeight = scratchR.height() / 4;
|
||||
|
||||
|
@ -351,20 +346,15 @@ public class ConnStatusHandler {
|
|||
// @SuppressWarnings("unchecked")
|
||||
public static void loadState( Context context )
|
||||
{
|
||||
synchronized( s_lockObj ) {
|
||||
synchronized( ConnStatusHandler.class ) {
|
||||
s_records = null;
|
||||
String as64 = XWPrefs.getPrefsString( context,
|
||||
R.string.key_connstat_data );
|
||||
if ( null != as64 && 0 < as64.length() ) {
|
||||
try {
|
||||
byte[] bytes = Utils.base64Decode( as64 );
|
||||
ObjectInputStream ois =
|
||||
new ObjectInputStream( new ByteArrayInputStream(bytes) );
|
||||
s_records =
|
||||
(HashMap<CommsConnType,SuccessRecord[]>)ois.readObject();
|
||||
} catch ( Exception ex ) {
|
||||
Log.ex( TAG, ex );
|
||||
s_records = new HashMap<CommsConnType,SuccessRecord[]>();
|
||||
}
|
||||
s_records = (HashMap<CommsConnType,SuccessRecord[]>)Utils.string64ToSerializable(as64);
|
||||
}
|
||||
if ( null == s_records ) {
|
||||
s_records = new HashMap<CommsConnType,SuccessRecord[]>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -376,7 +366,7 @@ public class ConnStatusHandler {
|
|||
doSave( context );
|
||||
} else {
|
||||
boolean savePending;
|
||||
synchronized( s_lockObj ) {
|
||||
synchronized( ConnStatusHandler.class ) {
|
||||
savePending = s_needsSave;
|
||||
if ( !savePending ) {
|
||||
s_needsSave = true;
|
||||
|
@ -400,7 +390,7 @@ public class ConnStatusHandler {
|
|||
private static void showSuccess( ConnStatusCBacks cbcks, boolean isIn )
|
||||
{
|
||||
if ( null != cbcks ) {
|
||||
synchronized( s_lockObj ) {
|
||||
synchronized( ConnStatusHandler.class ) {
|
||||
if ( isIn && s_showSuccesses[SUCCESS_IN] ) {
|
||||
// do nothing
|
||||
} else if ( !isIn && s_showSuccesses[SUCCESS_OUT] ) {
|
||||
|
@ -413,7 +403,7 @@ public class ConnStatusHandler {
|
|||
|
||||
Runnable proc = new Runnable() {
|
||||
public void run() {
|
||||
synchronized( s_lockObj ) {
|
||||
synchronized( ConnStatusHandler.class ) {
|
||||
s_showSuccesses[index] = false;
|
||||
invalidateParent();
|
||||
}
|
||||
|
@ -468,21 +458,10 @@ public class ConnStatusHandler {
|
|||
|
||||
private static void doSave( Context context )
|
||||
{
|
||||
synchronized( s_lockObj ) {
|
||||
// DbgUtils.logf( "ConnStatusHandler:doSave() doing save" );
|
||||
ByteArrayOutputStream bas
|
||||
= new ByteArrayOutputStream();
|
||||
try {
|
||||
ObjectOutputStream out
|
||||
= new ObjectOutputStream( bas );
|
||||
out.writeObject( s_records );
|
||||
out.flush();
|
||||
String as64 = Utils.base64Encode( bas.toByteArray() );
|
||||
XWPrefs.setPrefsString( context, R.string.key_connstat_data,
|
||||
as64 );
|
||||
} catch ( java.io.IOException ioe ) {
|
||||
Log.ex( TAG, ioe );
|
||||
}
|
||||
synchronized( ConnStatusHandler.class ) {
|
||||
String as64 = Utils.serializableToString64( s_records );
|
||||
XWPrefs.setPrefsString( context, R.string.key_connstat_data,
|
||||
as64 );
|
||||
s_needsSave = false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -39,12 +39,7 @@ import android.widget.EditText;
|
|||
import android.widget.FrameLayout;
|
||||
import android.widget.Spinner;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.eehouse.android.xw4.DlgDelegate.Action;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
@ -53,8 +48,16 @@ import java.util.HashSet;
|
|||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import org.eehouse.android.xw4.DlgDelegate.Action;
|
||||
|
||||
public class RelayInviteDelegate extends InviteDelegate {
|
||||
private static final String TAG = RelayInviteDelegate.class.getSimpleName();
|
||||
private static final String RECS_KEY = "TAG" + "/recs";
|
||||
|
||||
private static int[] BUTTONIDS = {
|
||||
R.id.button_relay_add,
|
||||
|
@ -132,11 +135,10 @@ public class RelayInviteDelegate extends InviteDelegate {
|
|||
showDialogFragment( DlgID.GET_NUMBER );
|
||||
break;
|
||||
case R.id.button_clear:
|
||||
Utils.notImpl( m_activity );
|
||||
// int count = getChecked().size();
|
||||
// String msg = getQuantityString( R.plurals.confirm_clear_sms_fmt,
|
||||
// count, count );
|
||||
// makeConfirmThenBuilder( msg, Action.CLEAR_ACTION ).show();
|
||||
int count = getChecked().size();
|
||||
String msg = getQuantityString( R.plurals.confirm_clear_relay_fmt,
|
||||
count, count );
|
||||
makeConfirmThenBuilder( msg, Action.CLEAR_ACTION ).show();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -430,25 +432,19 @@ public class RelayInviteDelegate extends InviteDelegate {
|
|||
|
||||
private void getSavedState()
|
||||
{
|
||||
String[] devIDs = XWPrefs.getRelayIDs( m_activity );
|
||||
|
||||
m_devIDRecs = new ArrayList<DevIDRec>(devIDs.length);
|
||||
for ( String devID : devIDs ) {
|
||||
DevIDRec rec = new DevIDRec( "me", devID );
|
||||
m_devIDRecs.add( rec );
|
||||
String dataString = DBUtils.getStringFor( m_activity, RECS_KEY, null );
|
||||
if ( null == dataString ) {
|
||||
m_devIDRecs = new ArrayList<DevIDRec>();
|
||||
} else {
|
||||
m_devIDRecs = (ArrayList<DevIDRec>)Utils
|
||||
.string64ToSerializable( dataString );
|
||||
}
|
||||
}
|
||||
|
||||
private void saveAndRebuild()
|
||||
{
|
||||
String[] devIDs = new String[m_devIDRecs.size()];
|
||||
Iterator<DevIDRec> iter = m_devIDRecs.iterator();
|
||||
for ( int ii = 0; iter.hasNext(); ++ii ) {
|
||||
DevIDRec rec = iter.next();
|
||||
devIDs[ii] = rec.m_devID;
|
||||
}
|
||||
XWPrefs.setRelayIDs( m_activity, devIDs );
|
||||
|
||||
String as64 = Utils.serializableToString64( m_devIDRecs );
|
||||
DBUtils.setStringFor( m_activity, RECS_KEY, as64 );
|
||||
rebuildList( false );
|
||||
}
|
||||
|
||||
|
@ -467,6 +463,16 @@ public class RelayInviteDelegate extends InviteDelegate {
|
|||
|
||||
private void clearSelectedImpl()
|
||||
{
|
||||
Set<InviterItem> checked = getChecked();
|
||||
Iterator<DevIDRec> iter = m_devIDRecs.iterator();
|
||||
for ( ; iter.hasNext(); ) {
|
||||
if ( checked.contains( iter.next() ) ) {
|
||||
iter.remove();
|
||||
}
|
||||
}
|
||||
clearChecked();
|
||||
saveAndRebuild();
|
||||
|
||||
// int count = m_adapter.getCount();
|
||||
// for ( int ii = count - 1; ii >= 0; --ii ) {
|
||||
// if ( m_devIDRecs.get( ii ).m_isChecked ) {
|
||||
|
@ -476,7 +482,7 @@ public class RelayInviteDelegate extends InviteDelegate {
|
|||
// saveAndRebuild();
|
||||
}
|
||||
|
||||
private class DevIDRec implements InviterItem {
|
||||
private static class DevIDRec implements InviterItem, Serializable {
|
||||
public String m_devID;
|
||||
public String m_opponent;
|
||||
public int m_nPlayers;
|
||||
|
|
|
@ -55,7 +55,12 @@ import android.widget.Toast;
|
|||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
|
@ -497,6 +502,37 @@ public class Utils {
|
|||
return Base64.decode( in, Base64.NO_WRAP );
|
||||
}
|
||||
|
||||
public static Object string64ToSerializable( String str64 )
|
||||
{
|
||||
Object result = null;
|
||||
byte[] bytes = base64Decode( str64 );
|
||||
try {
|
||||
ObjectInputStream ois =
|
||||
new ObjectInputStream( new ByteArrayInputStream(bytes) );
|
||||
result = ois.readObject();
|
||||
} catch ( Exception ex ) {
|
||||
Log.ex( TAG, ex );
|
||||
Assert.assertFalse( BuildConfig.DEBUG );
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static String serializableToString64( Serializable obj )
|
||||
{
|
||||
String result = null;
|
||||
ByteArrayOutputStream bas = new ByteArrayOutputStream();
|
||||
try {
|
||||
ObjectOutputStream out = new ObjectOutputStream( bas );
|
||||
out.writeObject( obj );
|
||||
out.flush();
|
||||
result = base64Encode( bas.toByteArray() );
|
||||
} catch ( Exception ex ) {
|
||||
Log.ex( TAG, ex );
|
||||
Assert.assertFalse( BuildConfig.DEBUG );
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void setFirstBootStatics( Context context )
|
||||
{
|
||||
if ( null == s_isFirstBootThisVersion ) {
|
||||
|
|
|
@ -278,17 +278,6 @@ public class XWPrefs {
|
|||
return obj;
|
||||
}
|
||||
|
||||
// Used by RelayInviteDelegate.java
|
||||
public static void setRelayIDs( Context context, String[] names )
|
||||
{
|
||||
setPrefsStringArray( context, R.string.key_relay_ids, names );
|
||||
}
|
||||
|
||||
public static String[] getRelayIDs( Context context )
|
||||
{
|
||||
return getPrefsStringArray( context, R.string.key_relay_ids );
|
||||
}
|
||||
|
||||
public static void setBTAddresses( Context context, String[] addrs )
|
||||
{
|
||||
setPrefsStringArray( context, R.string.key_bt_addrs, addrs );
|
||||
|
|
|
@ -1932,8 +1932,13 @@
|
|||
<item quantity="other">Are you sure you want to delete the
|
||||
%1$d checked phone numbers?</item>
|
||||
</plurals>
|
||||
<string name="confirm_clear_relay">Are you sure you want to delete the
|
||||
checked device[s]?</string>
|
||||
<plurals name="confirm_clear_relay_fmt">
|
||||
<item quantity="one">Are you sure you want to delete the checked
|
||||
RelayID record?</item>
|
||||
<item quantity="other">Are you sure you want to delete the
|
||||
%1$d checked RelayID records?</item>
|
||||
</plurals>
|
||||
|
||||
<!-- -->
|
||||
<string name="summary_conn_sms_fmt">Game in play with %1$s</string>
|
||||
<!-- -->
|
||||
|
|
Loading…
Add table
Reference in a new issue