From 0394579b7c478d14c8222b8e07728d3b9fb1b076 Mon Sep 17 00:00:00 2001 From: Eric House Date: Fri, 30 Nov 2012 19:58:23 -0800 Subject: [PATCH] cleanup (and use factored-out updateRow a couple of additional places) --- .../src/org/eehouse/android/xw4/DBUtils.java | 96 +++++++------------ .../org/eehouse/android/xw4/RelayService.java | 2 - .../src/org/eehouse/android/xw4/Utils.java | 1 - 3 files changed, 35 insertions(+), 64 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 bc96d8db4..f3504932f 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java @@ -554,10 +554,9 @@ public class DBUtils { Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns, selection, null, null, null, DBHelper.CREATE_TIME + " DESC" ); // order by - while ( cursor.moveToNext() ) { + if ( cursor.moveToNext() ) { int indx = cursor.getColumnIndex( DBHelper.CREATE_TIME ); result = new Date( cursor.getLong( indx ) ); - break; } cursor.close(); db.close(); @@ -710,34 +709,20 @@ public class DBUtils { { Assert.assertTrue( lock.canWrite() ); long rowid = lock.getRowid(); - initDB( context ); - synchronized( s_dbHelper ) { - SQLiteDatabase db = s_dbHelper.getWritableDatabase(); - String selection = String.format( ROW_ID_FMT, rowid ); - ContentValues values = new ContentValues(); - values.put( DBHelper.SNAPSHOT, bytes ); + ContentValues values = new ContentValues(); + values.put( DBHelper.SNAPSHOT, bytes ); - long timestamp = new Date().getTime(); - if ( setCreate ) { - values.put( DBHelper.CREATE_TIME, timestamp ); - } - values.put( DBHelper.LASTPLAY_TIME, timestamp ); - - int result = db.update( DBHelper.TABLE_NAME_SUM, - values, selection, null ); - if ( 0 == result ) { - Assert.fail(); - // values.put( DBHelper.FILE_NAME, path ); - // rowid = db.insert( DBHelper.TABLE_NAME_SUM, null, values ); - // DbgUtils.logf( "insert=>%d", rowid ); - // Assert.assertTrue( row >= 0 ); - } - db.close(); + long timestamp = new Date().getTime(); + if ( setCreate ) { + values.put( DBHelper.CREATE_TIME, timestamp ); } - setCached( rowid, null ); // force reread + values.put( DBHelper.LASTPLAY_TIME, timestamp ); - if ( -1 != rowid ) { + updateRow( context, DBHelper.TABLE_NAME_SUM, rowid, values ); + + setCached( rowid, null ); // force reread + if ( -1 != rowid ) { // Is this possible? PENDING notifyListeners( rowid ); } return rowid; @@ -861,23 +846,6 @@ public class DBUtils { return result; } - private static void updateRow( Context context, String table, - long rowid, ContentValues values ) - { - initDB( context ); - synchronized( s_dbHelper ) { - SQLiteDatabase db = s_dbHelper.getWritableDatabase(); - - String selection = String.format( ROW_ID_FMT, rowid ); - - int result = db.update( table, values, selection, null ); - db.close(); - if ( 0 == result ) { - DbgUtils.logf( "updateRow failed" ); - } - } - } - private static String getChatHistoryStr( Context context, long rowid ) { String result = null; @@ -1200,25 +1168,14 @@ public class DBUtils { private static void saveChatHistory( Context context, long rowid, String history ) { - initDB( context ); - synchronized( s_dbHelper ) { - SQLiteDatabase db = s_dbHelper.getWritableDatabase(); - - String selection = String.format( ROW_ID_FMT, rowid ); - ContentValues values = new ContentValues(); - if ( null != history ) { - values.put( DBHelper.CHAT_HISTORY, history ); - } else { - values.putNull( DBHelper.CHAT_HISTORY ); - } - - long timestamp = new Date().getTime(); - values.put( DBHelper.LASTPLAY_TIME, timestamp ); - - int result = db.update( DBHelper.TABLE_NAME_SUM, - values, selection, null ); - db.close(); + ContentValues values = new ContentValues(); + if ( null != history ) { + values.put( DBHelper.CHAT_HISTORY, history ); + } else { + values.putNull( DBHelper.CHAT_HISTORY ); } + values.put( DBHelper.LASTPLAY_TIME, new Date().getTime() ); + updateRow( context, DBHelper.TABLE_NAME_SUM, rowid, values ); } private static void initDB( Context context ) @@ -1231,6 +1188,23 @@ public class DBUtils { } } + private static void updateRow( Context context, String table, + long rowid, ContentValues values ) + { + initDB( context ); + synchronized( s_dbHelper ) { + SQLiteDatabase db = s_dbHelper.getWritableDatabase(); + + String selection = String.format( ROW_ID_FMT, rowid ); + + int result = db.update( table, values, selection, null ); + db.close(); + if ( 0 == result ) { + DbgUtils.logf( "updateRow failed" ); + } + } + } + private static void notifyListeners( long rowid ) { synchronized( s_listeners ) { diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayService.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayService.java index 19dcf3e7c..c312a8fc8 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayService.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayService.java @@ -112,9 +112,7 @@ public class RelayService extends Service { if ( 0 < idsWMsgs.size() ) { String[] relayIDs = new String[idsWMsgs.size()]; idsWMsgs.toArray( relayIDs ); - // if ( !DispatchNotify.tryHandle( relayIDs ) ) { setupNotification( relayIDs ); - // } } sink.send( this ); } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/Utils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/Utils.java index 5585407e9..4744e48ab 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/Utils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/Utils.java @@ -444,7 +444,6 @@ public class Utils { pm.queryIntentActivities( intent, PackageManager.MATCH_DEFAULT_ONLY ); result = 0 < doers.size(); - DbgUtils.logf( "canInstall()=>%b", result ); return result; }