From 9b9aec45da964fd53fab195ed47fa4646b1cb152 Mon Sep 17 00:00:00 2001 From: Eric House Date: Thu, 28 Jun 2012 07:20:28 -0700 Subject: [PATCH] format status timestamps in terms of how long ago (using getRelativeDateTimeString); save and restore serialized timestamps. --- .../XWords4/res/values/common_rsrc.xml | 1 + .../eehouse/android/xw4/BoardActivity.java | 2 +- .../org/eehouse/android/xw4/BoardView.java | 2 +- .../eehouse/android/xw4/CommsTransport.java | 12 +- .../android/xw4/ConnStatusHandler.java | 103 +++++++++++++++--- .../org/eehouse/android/xw4/SMSService.java | 4 +- .../src/org/eehouse/android/xw4/XWApp.java | 1 + 7 files changed, 101 insertions(+), 24 deletions(-) diff --git a/xwords4/android/XWords4/res/values/common_rsrc.xml b/xwords4/android/XWords4/res/values/common_rsrc.xml index 716d0f438..c071fb1d7 100644 --- a/xwords4/android/XWords4/res/values/common_rsrc.xml +++ b/xwords4/android/XWords4/res/values/common_rsrc.xml @@ -64,6 +64,7 @@ key_bt_names key_bt_addrs key_sms_phones + key_connstat_data key_notagain_sync key_notagain_chat diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardActivity.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardActivity.java index 50ebaec02..2a9e9e5d9 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardActivity.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardActivity.java @@ -882,7 +882,7 @@ public class BoardActivity extends XWActivity // m_jniThread.handle( JNICmd.CMD_DRAW_BT_STATUS, accepted ); // } ConnStatusHandler. - updateStatusIn(CommsAddrRec.CommsConnType.COMMS_CONN_BT, + updateStatusIn(this, CommsAddrRec.CommsConnType.COMMS_CONN_BT, MultiService.MultiEvent.MESSAGE_ACCEPTED == event); break; case MESSAGE_NOGAME: diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardView.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardView.java index ba4b8a113..2a8cfedfd 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardView.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardView.java @@ -217,7 +217,7 @@ public class BoardView extends View implements DrawCtx, BoardHandler, break; case MotionEvent.ACTION_UP: if ( ConnStatusHandler.handleUp( xx, yy ) ) { - String msg = ConnStatusHandler.getStatusText(); + String msg = ConnStatusHandler.getStatusText(m_context); m_parent.showOKOnlyDialog( msg ); } else { m_jniThread.handle( JNIThread.JNICmd.CMD_PEN_UP, xx, yy ); diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/CommsTransport.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/CommsTransport.java index 27176b867..2bee56a05 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/CommsTransport.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/CommsTransport.java @@ -407,18 +407,22 @@ public class CommsTransport implements TransportProcs, case COMMS_RELAYSTATE_UNCONNECTED: case COMMS_RELAYSTATE_DENIED: case COMMS_RELAYSTATE_CONNECT_PENDING: - ConnStatusHandler.updateStatusOut( CommsConnType.COMMS_CONN_RELAY, + ConnStatusHandler.updateStatusOut( m_context, + CommsConnType.COMMS_CONN_RELAY, false ); - ConnStatusHandler.updateStatusIn( CommsConnType.COMMS_CONN_RELAY, + ConnStatusHandler.updateStatusIn( m_context, + CommsConnType.COMMS_CONN_RELAY, false ); break; case COMMS_RELAYSTATE_CONNECTED: case COMMS_RELAYSTATE_RECONNECTED: - ConnStatusHandler.updateStatusOut( CommsConnType.COMMS_CONN_RELAY, + ConnStatusHandler.updateStatusOut( m_context, + CommsConnType.COMMS_CONN_RELAY, true ); break; case COMMS_RELAYSTATE_ALLCONNECTED: - ConnStatusHandler.updateStatusIn( CommsConnType.COMMS_CONN_RELAY, + ConnStatusHandler.updateStatusIn( m_context, + CommsConnType.COMMS_CONN_RELAY, true ); break; } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/ConnStatusHandler.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/ConnStatusHandler.java index e74e945a7..8dd241637 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/ConnStatusHandler.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/ConnStatusHandler.java @@ -20,6 +20,8 @@ package org.eehouse.android.xw4; + +import android.content.Context; import android.content.res.Resources; import android.graphics.Canvas; import android.graphics.Paint; @@ -27,11 +29,18 @@ import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.os.Handler; import android.os.Message; +import android.text.format.DateUtils; 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 junit.framework.Assert; +import org.eehouse.android.xw4.jni.CommonPrefs; import org.eehouse.android.xw4.jni.CommsAddrRec.CommsConnType; +import org.eehouse.android.xw4.jni.XwJNI; public class ConnStatusHandler { // private static final int GREEN = 0x7F00FF00; @@ -45,9 +54,9 @@ public class ConnStatusHandler { private static Handler s_handler; 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 - private static final String TIME_FMT = "%X %x"; + // private static final String TIME_FMT = "%X %x"; private static final Time s_zero = new Time(); public Time lastSuccess; public Time lastFailure; @@ -70,16 +79,17 @@ public class ConnStatusHandler { return lastSuccess.after( s_zero ); } - public String newerStr() + public String newerStr( Context context ) { 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; - return which.format( TIME_FMT ); + return format( context, which ); } public void update( boolean success ) @@ -88,10 +98,22 @@ public class ConnStatusHandler { last.setToNow(); 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 s_records = new HashMap(); + private static Object s_lockObj = new Object(); 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 ); } - public static String getStatusText() + public static String getStatusText( Context context ) { String msg; if ( CommsConnType.COMMS_CONN_NONE == s_connType ) { msg = "This is a standalone game. There is no network status."; } else { - synchronized( s_records ) { + synchronized( s_lockObj ) { msg = "Network status for game connected via " + connType2Str(); msg += ":\n\n"; SuccessRecord record = recordFor( s_connType, false ); msg += String.format( "Last send was %s (at %s)\n", record.successNewer? "successful":"unsuccessful", - record.newerStr() ); + record.newerStr( context ) ); String fmt = null; if ( record.successNewer ) { @@ -152,7 +174,7 @@ public class ConnStatusHandler { } } if ( null != fmt ) { - msg += String.format( fmt, record.olderStr() ); + msg += String.format( fmt, record.olderStr( context ) ); } msg += "\n"; @@ -160,7 +182,7 @@ public class ConnStatusHandler { if ( record.haveSuccess() ) { msg += String.format( "Last receipt was at %s", - record.newerStr() ); + record.newerStr( context ) ); } else { 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 ); record.update( success ); } 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 ); record.update( success ); } invalidateParent(); + saveState( context ); } public static void draw( Canvas canvas, Resources res, int offsetX, int offsetY ) { - synchronized( s_records ) { + synchronized( s_lockObj ) { if ( null != s_rect ) { int iconID; 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)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 ) { Drawable icon = res.getDrawable( id ); diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/SMSService.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/SMSService.java index 6d051ef83..41cb780d6 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/SMSService.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/SMSService.java @@ -240,7 +240,7 @@ public class SMSService extends Service { case HANDLE: ++m_nReceived; ConnStatusHandler. - updateStatusIn( CommsConnType.COMMS_CONN_SMS, + updateStatusIn( this, CommsConnType.COMMS_CONN_SMS, true ); if ( s_showToasts ) { DbgUtils.showf( this, "got %dth msg", m_nReceived ); @@ -544,7 +544,7 @@ public class SMSService extends Service { ee.toString() ); } - ConnStatusHandler.updateStatusOut( CommsConnType.COMMS_CONN_SMS, + ConnStatusHandler.updateStatusOut( this, CommsConnType.COMMS_CONN_SMS, success ); return success; } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWApp.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWApp.java index bea4a5c8f..161f12fdf 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWApp.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWApp.java @@ -50,6 +50,7 @@ public class XWApp extends Application { RelayReceiver.RestartTimer( this ); BTService.startService( this ); + ConnStatusHandler.loadState( this ); } public static UUID getAppUUID()