format status timestamps in terms of how long ago (using

getRelativeDateTimeString); save and restore serialized timestamps.
This commit is contained in:
Eric House 2012-06-28 07:20:28 -07:00
parent 52e70bff3d
commit 9b9aec45da
7 changed files with 101 additions and 24 deletions

View file

@ -64,6 +64,7 @@
<string name="key_bt_names">key_bt_names</string> <string name="key_bt_names">key_bt_names</string>
<string name="key_bt_addrs">key_bt_addrs</string> <string name="key_bt_addrs">key_bt_addrs</string>
<string name="key_sms_phones">key_sms_phones</string> <string name="key_sms_phones">key_sms_phones</string>
<string name="key_connstat_data">key_connstat_data</string>
<string name="key_notagain_sync">key_notagain_sync</string> <string name="key_notagain_sync">key_notagain_sync</string>
<string name="key_notagain_chat">key_notagain_chat</string> <string name="key_notagain_chat">key_notagain_chat</string>

View file

@ -882,7 +882,7 @@ public class BoardActivity extends XWActivity
// m_jniThread.handle( JNICmd.CMD_DRAW_BT_STATUS, accepted ); // m_jniThread.handle( JNICmd.CMD_DRAW_BT_STATUS, accepted );
// } // }
ConnStatusHandler. ConnStatusHandler.
updateStatusIn(CommsAddrRec.CommsConnType.COMMS_CONN_BT, updateStatusIn(this, CommsAddrRec.CommsConnType.COMMS_CONN_BT,
MultiService.MultiEvent.MESSAGE_ACCEPTED == event); MultiService.MultiEvent.MESSAGE_ACCEPTED == event);
break; break;
case MESSAGE_NOGAME: case MESSAGE_NOGAME:

View file

@ -217,7 +217,7 @@ public class BoardView extends View implements DrawCtx, BoardHandler,
break; break;
case MotionEvent.ACTION_UP: case MotionEvent.ACTION_UP:
if ( ConnStatusHandler.handleUp( xx, yy ) ) { if ( ConnStatusHandler.handleUp( xx, yy ) ) {
String msg = ConnStatusHandler.getStatusText(); String msg = ConnStatusHandler.getStatusText(m_context);
m_parent.showOKOnlyDialog( msg ); m_parent.showOKOnlyDialog( msg );
} else { } else {
m_jniThread.handle( JNIThread.JNICmd.CMD_PEN_UP, xx, yy ); m_jniThread.handle( JNIThread.JNICmd.CMD_PEN_UP, xx, yy );

View file

@ -407,18 +407,22 @@ public class CommsTransport implements TransportProcs,
case COMMS_RELAYSTATE_UNCONNECTED: case COMMS_RELAYSTATE_UNCONNECTED:
case COMMS_RELAYSTATE_DENIED: case COMMS_RELAYSTATE_DENIED:
case COMMS_RELAYSTATE_CONNECT_PENDING: case COMMS_RELAYSTATE_CONNECT_PENDING:
ConnStatusHandler.updateStatusOut( CommsConnType.COMMS_CONN_RELAY, ConnStatusHandler.updateStatusOut( m_context,
CommsConnType.COMMS_CONN_RELAY,
false ); false );
ConnStatusHandler.updateStatusIn( CommsConnType.COMMS_CONN_RELAY, ConnStatusHandler.updateStatusIn( m_context,
CommsConnType.COMMS_CONN_RELAY,
false ); false );
break; break;
case COMMS_RELAYSTATE_CONNECTED: case COMMS_RELAYSTATE_CONNECTED:
case COMMS_RELAYSTATE_RECONNECTED: case COMMS_RELAYSTATE_RECONNECTED:
ConnStatusHandler.updateStatusOut( CommsConnType.COMMS_CONN_RELAY, ConnStatusHandler.updateStatusOut( m_context,
CommsConnType.COMMS_CONN_RELAY,
true ); true );
break; break;
case COMMS_RELAYSTATE_ALLCONNECTED: case COMMS_RELAYSTATE_ALLCONNECTED:
ConnStatusHandler.updateStatusIn( CommsConnType.COMMS_CONN_RELAY, ConnStatusHandler.updateStatusIn( m_context,
CommsConnType.COMMS_CONN_RELAY,
true ); true );
break; break;
} }

View file

@ -20,6 +20,8 @@
package org.eehouse.android.xw4; package org.eehouse.android.xw4;
import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Paint; import android.graphics.Paint;
@ -27,11 +29,18 @@ import android.graphics.Rect;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
import android.text.format.DateUtils;
import android.text.format.Time; import android.text.format.Time;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap; import java.util.HashMap;
import junit.framework.Assert; import junit.framework.Assert;
import org.eehouse.android.xw4.jni.CommonPrefs;
import org.eehouse.android.xw4.jni.CommsAddrRec.CommsConnType; import org.eehouse.android.xw4.jni.CommsAddrRec.CommsConnType;
import org.eehouse.android.xw4.jni.XwJNI;
public class ConnStatusHandler { public class ConnStatusHandler {
// private static final int GREEN = 0x7F00FF00; // private static final int GREEN = 0x7F00FF00;
@ -45,9 +54,9 @@ public class ConnStatusHandler {
private static Handler s_handler; private static Handler s_handler;
private static Paint s_fillPaint = new Paint( Paint.ANTI_ALIAS_FLAG ); private static Paint s_fillPaint = new Paint( Paint.ANTI_ALIAS_FLAG );
private static class SuccessRecord { private static class SuccessRecord implements java.io.Serializable {
// man strftime for these // man strftime for these
private static final String TIME_FMT = "%X %x"; // private static final String TIME_FMT = "%X %x";
private static final Time s_zero = new Time(); private static final Time s_zero = new Time();
public Time lastSuccess; public Time lastSuccess;
public Time lastFailure; public Time lastFailure;
@ -70,16 +79,17 @@ public class ConnStatusHandler {
return lastSuccess.after( s_zero ); return lastSuccess.after( s_zero );
} }
public String newerStr() public String newerStr( Context context )
{ {
Time which = successNewer? lastSuccess : lastFailure; Time which = successNewer? lastSuccess : lastFailure;
return which.format( TIME_FMT ); return format( context, which );
// return which.format( TIME_FMT );
} }
public String olderStr() public String olderStr( Context context )
{ {
Time which = successNewer? lastFailure : lastSuccess; Time which = successNewer? lastFailure : lastSuccess;
return which.format( TIME_FMT ); return format( context, which );
} }
public void update( boolean success ) public void update( boolean success )
@ -88,10 +98,22 @@ public class ConnStatusHandler {
last.setToNow(); last.setToNow();
successNewer = success; successNewer = success;
} }
private String format( Context context, Time time )
{
CharSequence seq =
DateUtils.getRelativeDateTimeString( context,
time.toMillis(true),
DateUtils.MINUTE_IN_MILLIS,
DateUtils.WEEK_IN_MILLIS,
0 );
return seq.toString();
}
} }
private static HashMap<CommsConnType,SuccessRecord[]> s_records = private static HashMap<CommsConnType,SuccessRecord[]> s_records =
new HashMap<CommsConnType,SuccessRecord[]>(); new HashMap<CommsConnType,SuccessRecord[]>();
private static Object s_lockObj = new Object();
public static void setRect( int left, int top, int right, int bottom ) public static void setRect( int left, int top, int right, int bottom )
{ {
@ -126,20 +148,20 @@ public class ConnStatusHandler {
return s_downOnMe && s_rect.contains( xx, yy ); return s_downOnMe && s_rect.contains( xx, yy );
} }
public static String getStatusText() public static String getStatusText( Context context )
{ {
String msg; String msg;
if ( CommsConnType.COMMS_CONN_NONE == s_connType ) { if ( CommsConnType.COMMS_CONN_NONE == s_connType ) {
msg = "This is a standalone game. There is no network status."; msg = "This is a standalone game. There is no network status.";
} else { } else {
synchronized( s_records ) { synchronized( s_lockObj ) {
msg = "Network status for game connected via " + connType2Str(); msg = "Network status for game connected via " + connType2Str();
msg += ":\n\n"; msg += ":\n\n";
SuccessRecord record = recordFor( s_connType, false ); SuccessRecord record = recordFor( s_connType, false );
msg += msg +=
String.format( "Last send was %s (at %s)\n", String.format( "Last send was %s (at %s)\n",
record.successNewer? "successful":"unsuccessful", record.successNewer? "successful":"unsuccessful",
record.newerStr() ); record.newerStr( context ) );
String fmt = null; String fmt = null;
if ( record.successNewer ) { if ( record.successNewer ) {
@ -152,7 +174,7 @@ public class ConnStatusHandler {
} }
} }
if ( null != fmt ) { if ( null != fmt ) {
msg += String.format( fmt, record.olderStr() ); msg += String.format( fmt, record.olderStr( context ) );
} }
msg += "\n"; msg += "\n";
@ -160,7 +182,7 @@ public class ConnStatusHandler {
if ( record.haveSuccess() ) { if ( record.haveSuccess() ) {
msg += msg +=
String.format( "Last receipt was at %s", String.format( "Last receipt was at %s",
record.newerStr() ); record.newerStr( context ) );
} else { } else {
msg += "No messages have been received."; msg += "No messages have been received.";
} }
@ -176,28 +198,32 @@ public class ConnStatusHandler {
} }
} }
public static void updateStatusIn( CommsConnType connType, boolean success ) public static void updateStatusIn( Context context,
CommsConnType connType, boolean success )
{ {
synchronized( s_records ) { synchronized( s_lockObj ) {
SuccessRecord record = recordFor( connType, true ); SuccessRecord record = recordFor( connType, true );
record.update( success ); record.update( success );
} }
invalidateParent(); invalidateParent();
saveState( context );
} }
public static void updateStatusOut( CommsConnType connType, boolean success ) public static void updateStatusOut( Context context,
CommsConnType connType, boolean success )
{ {
synchronized( s_records ) { synchronized( s_lockObj ) {
SuccessRecord record = recordFor( connType, false ); SuccessRecord record = recordFor( connType, false );
record.update( success ); record.update( success );
} }
invalidateParent(); invalidateParent();
saveState( context );
} }
public static void draw( Canvas canvas, Resources res, public static void draw( Canvas canvas, Resources res,
int offsetX, int offsetY ) int offsetX, int offsetY )
{ {
synchronized( s_records ) { synchronized( s_lockObj ) {
if ( null != s_rect ) { if ( null != s_rect ) {
int iconID; int iconID;
switch( s_connType ) { switch( s_connType ) {
@ -254,6 +280,51 @@ public class ConnStatusHandler {
} }
} }
public static void loadState( Context context )
{
synchronized( s_lockObj ) {
String as64 = CommonPrefs.getPrefsString( context,
R.string.key_connstat_data );
if ( null != as64 && 0 < as64.length() ) {
byte[] bytes = XwJNI.base64Decode( as64 );
try {
ObjectInputStream ois =
new ObjectInputStream( new ByteArrayInputStream(bytes) );
s_records =
(HashMap<CommsConnType,SuccessRecord[]>)ois.readObject();
// } catch ( java.io.StreamCorruptedException sce ) {
// DbgUtils.logf( "loadState: %s", sce.toString() );
// } catch ( java.io.OptionalDataException ode ) {
// DbgUtils.logf( "loadState: %s", ode.toString() );
// } catch ( java.io.IOException ioe ) {
// DbgUtils.logf( "loadState: %s", ioe.toString() );
// } catch ( java.lang.ClassNotFoundException cnfe ) {
// DbgUtils.logf( "loadState: %s", cnfe.toString() );
} catch ( Exception ex ) {
DbgUtils.logf( "loadState: %s", ex.toString() );
}
}
}
}
private static void saveState( Context context )
{
DbgUtils.logf( "saveState called; need to coalesce these!!!" );
synchronized( s_lockObj ) {
ByteArrayOutputStream bas = new ByteArrayOutputStream();
try {
ObjectOutputStream out = new ObjectOutputStream( bas );
out.writeObject(s_records);
out.flush();
String as64 = XwJNI.base64Encode( bas.toByteArray() );
CommonPrefs.setPrefsString( context,
R.string.key_connstat_data, as64 );
} catch ( java.io.IOException ioe ) {
DbgUtils.logf( "loadState: %s", ioe.toString() );
}
}
}
private static void drawIn( Canvas canvas, Resources res, int id, Rect rect ) private static void drawIn( Canvas canvas, Resources res, int id, Rect rect )
{ {
Drawable icon = res.getDrawable( id ); Drawable icon = res.getDrawable( id );

View file

@ -240,7 +240,7 @@ public class SMSService extends Service {
case HANDLE: case HANDLE:
++m_nReceived; ++m_nReceived;
ConnStatusHandler. ConnStatusHandler.
updateStatusIn( CommsConnType.COMMS_CONN_SMS, updateStatusIn( this, CommsConnType.COMMS_CONN_SMS,
true ); true );
if ( s_showToasts ) { if ( s_showToasts ) {
DbgUtils.showf( this, "got %dth msg", m_nReceived ); DbgUtils.showf( this, "got %dth msg", m_nReceived );
@ -544,7 +544,7 @@ public class SMSService extends Service {
ee.toString() ); ee.toString() );
} }
ConnStatusHandler.updateStatusOut( CommsConnType.COMMS_CONN_SMS, ConnStatusHandler.updateStatusOut( this, CommsConnType.COMMS_CONN_SMS,
success ); success );
return success; return success;
} }

View file

@ -50,6 +50,7 @@ public class XWApp extends Application {
RelayReceiver.RestartTimer( this ); RelayReceiver.RestartTimer( this );
BTService.startService( this ); BTService.startService( this );
ConnStatusHandler.loadState( this );
} }
public static UUID getAppUUID() public static UUID getAppUUID()