From b57b6b1b31169c38d9a83e9a0609e014562b3508 Mon Sep 17 00:00:00 2001 From: Andy2 Date: Mon, 28 Feb 2011 05:41:02 -0800 Subject: [PATCH] replace array[1] with HashSet to support more than one db change listener --- .../src/org/eehouse/android/xw4/DBUtils.java | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java index 188ddc342..90566f583 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java @@ -28,6 +28,8 @@ import java.util.StringTokenizer; import android.content.ContentValues; import java.util.ArrayList; import java.util.Date; +import java.util.HashSet; +import java.util.Iterator; import junit.framework.Assert; import org.eehouse.android.xw4.jni.*; @@ -38,8 +40,8 @@ public class DBUtils { public static interface DBChangeListener { public void pathSaved( String path ); } - private static DBChangeListener[] s_listeners = - new DBChangeListener[]{ null }; + private static HashSet s_listeners = + new HashSet(); private static SQLiteOpenHelper s_dbHelper = null; @@ -569,8 +571,16 @@ public class DBUtils { public static void setDBChangeListener( DBChangeListener listener ) { synchronized( s_listeners ) { - Assert.assertTrue( listener != s_listeners[0] ); - s_listeners[0] = listener; + Assert.assertNotNull( listener ); + s_listeners.add( listener ); + } + } + + public static void clearDBChangeListener( DBChangeListener listener ) + { + synchronized( s_listeners ) { + Assert.assertTrue( s_listeners.contains( listener ) ); + s_listeners.remove( listener ); } } @@ -633,8 +643,9 @@ public class DBUtils { private static void notifyListeners( String path ) { synchronized( s_listeners ) { - if ( null != s_listeners[0] ) { - s_listeners[0].pathSaved( path ); + Iterator iter = s_listeners.iterator(); + while ( iter.hasNext() ) { + iter.next().pathSaved( path ); } } }