From e590db5f3ff4cd0b2f3b609488d3400d46eb3cf0 Mon Sep 17 00:00:00 2001 From: Eric House Date: Mon, 26 Nov 2012 08:05:05 -0800 Subject: [PATCH] merge in the db part of 82c39489f0b8b755b5e53f38ae1ca6b205c446fe (android_groups branch, local only right now), but not the UI part since it won't make the next release. --- .../XWords4/res/values/common_rsrc.xml | 1 + .../android/XWords4/res/values/strings.xml | 3 ++ .../src/org/eehouse/android/xw4/DBHelper.java | 47 ++++++++++++++-- .../src/org/eehouse/android/xw4/DBUtils.java | 53 +++++++++---------- .../src/org/eehouse/android/xw4/XWPrefs.java | 30 +++++++++++ 5 files changed, 103 insertions(+), 31 deletions(-) diff --git a/xwords4/android/XWords4/res/values/common_rsrc.xml b/xwords4/android/XWords4/res/values/common_rsrc.xml index 80c92487a..c7369a101 100644 --- a/xwords4/android/XWords4/res/values/common_rsrc.xml +++ b/xwords4/android/XWords4/res/values/common_rsrc.xml @@ -71,6 +71,7 @@ key_gcmvers_regid key_relay_regid key_checked_sms + key_default_group key_notagain_sync key_notagain_chat diff --git a/xwords4/android/XWords4/res/values/strings.xml b/xwords4/android/XWords4/res/values/strings.xml index f3da657ee..5dd2c83ef 100644 --- a/xwords4/android/XWords4/res/values/strings.xml +++ b/xwords4/android/XWords4/res/values/strings.xml @@ -2137,4 +2137,7 @@ (Not in external/sdcard memory) Downloads Directory + + My games + New games diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBHelper.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBHelper.java index 24e406b87..b44021f29 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBHelper.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBHelper.java @@ -20,9 +20,10 @@ package org.eehouse.android.xw4; +import android.content.ContentValues; import android.content.Context; -import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; public class DBHelper extends SQLiteOpenHelper { @@ -30,8 +31,9 @@ public class DBHelper extends SQLiteOpenHelper { public static final String TABLE_NAME_OBITS = "obits"; public static final String TABLE_NAME_DICTBROWSE = "dictbrowse"; public static final String TABLE_NAME_DICTINFO = "dictinfo"; + public static final String TABLE_NAME_GROUPS = "groups"; private static final String DB_NAME = "xwdb"; - private static final int DB_VERSION = 14; + private static final int DB_VERSION = 15; public static final String GAME_NAME = "GAME_NAME"; public static final String NUM_MOVES = "NUM_MOVES"; @@ -60,7 +62,7 @@ public class DBHelper extends SQLiteOpenHelper { public static final String SEED = "SEED"; public static final String SMSPHONE = "SMSPHONE"; public static final String LASTMOVE = "LASTMOVE"; - + public static final String GROUPID = "GROUPID"; public static final String DICTNAME = "DICTNAME"; public static final String MD5SUM = "MD5SUM"; @@ -76,6 +78,11 @@ public class DBHelper extends SQLiteOpenHelper { public static final String CREATE_TIME = "CREATE_TIME"; public static final String LASTPLAY_TIME = "LASTPLAY_TIME"; + public static final String GROUPNAME = "GROUPNAME"; + public static final String EXPANDED = "EXPANDED"; + + private Context m_context; + private static final String[] s_summaryColsAndTypes = { GAME_NAME, "TEXT" ,NUM_MOVES, "INTEGER" @@ -99,6 +106,7 @@ public class DBHelper extends SQLiteOpenHelper { ,GAMEID, "INTEGER" ,REMOTEDEVS, "TEXT" ,LASTMOVE, "INTEGER DEFAULT 0" + ,GROUPID, "INTEGER" // HASMSGS: sqlite doesn't have bool; use 0 and 1 ,HASMSGS, "INTEGER DEFAULT 0" ,CONTRACTED, "INTEGER DEFAULT 0" @@ -131,9 +139,15 @@ public class DBHelper extends SQLiteOpenHelper { ,ITERPREFIX, "TEXT" }; + private static final String[] s_groupsSchema = { + GROUPNAME, "TEXT" + ,EXPANDED, "INTEGER(1)" + }; + public DBHelper( Context context ) { super( context, DB_NAME, null, DB_VERSION ); + m_context = context; } public static String getDBName() @@ -148,6 +162,7 @@ public class DBHelper extends SQLiteOpenHelper { createTable( db, TABLE_NAME_OBITS, s_obitsColsAndTypes ); createTable( db, TABLE_NAME_DICTINFO, s_dictInfoColsAndTypes ); createTable( db, TABLE_NAME_DICTBROWSE, s_dictBrowseColsAndTypes ); + createGroupsTable( db ); } @Override @@ -177,9 +192,11 @@ public class DBHelper extends SQLiteOpenHelper { case 12: createTable( db, TABLE_NAME_DICTINFO, s_dictInfoColsAndTypes ); createTable( db, TABLE_NAME_DICTBROWSE, s_dictBrowseColsAndTypes ); - case 13: addSumColumn( db, LASTMOVE ); + case 14: + addSumColumn( db, GROUPID ); + createGroupsTable( db ); // nothing yet break; default: @@ -221,4 +238,26 @@ public class DBHelper extends SQLiteOpenHelper { db.execSQL( query.toString() ); } + private void createGroupsTable( SQLiteDatabase db ) + { + createTable( db, TABLE_NAME_GROUPS, s_groupsSchema ); + + // Create an empty group name + ContentValues values = new ContentValues(); + values.put( GROUPNAME, m_context.getString(R.string.group_cur_games) ); + values.put( EXPANDED, 1 ); + long curGroup = db.insert( TABLE_NAME_GROUPS, null, values ); + values = new ContentValues(); + values.put( GROUPNAME, m_context.getString(R.string.group_new_games) ); + values.put( EXPANDED, 0 ); + long newGroup = db.insert( TABLE_NAME_GROUPS, null, values ); + + // place all existing games in the initial unnamed group + values = new ContentValues(); + values.put( GROUPID, curGroup ); + db.update( DBHelper.TABLE_NAME_SUM, values, null, null ); + + XWPrefs.setDefaultNewGameGroup( m_context, newGroup ); + } + } 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 8b9473d5f..72b8338e4 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java @@ -364,18 +364,9 @@ public class DBUtils { private static void setInt( long rowid, String column, int value ) { - synchronized( s_dbHelper ) { - SQLiteDatabase db = s_dbHelper.getWritableDatabase(); - - String selection = String.format( ROW_ID_FMT, rowid ); - ContentValues values = new ContentValues(); - values.put( column, value ); - - int result = db.update( DBHelper.TABLE_NAME_SUM, - values, selection, null ); - Assert.assertTrue( result == 1 ); - db.close(); - } + ContentValues values = new ContentValues(); + values.put( column, value ); + updateRow( null, DBHelper.TABLE_NAME_SUM, rowid, values ); } public static void setMsgFlags( long rowid, int flags ) @@ -693,6 +684,8 @@ public class DBUtils { long timestamp = new Date().getTime(); values.put( DBHelper.CREATE_TIME, timestamp ); values.put( DBHelper.LASTPLAY_TIME, timestamp ); + values.put( DBHelper.GROUPID, + XWPrefs.getDefaultNewGameGroup( context ) ); long rowid = db.insert( DBHelper.TABLE_NAME_SUM, null, values ); @@ -840,21 +833,9 @@ public class DBUtils { public static void setName( Context context, long rowid, String name ) { - 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.GAME_NAME, name ); - - int result = db.update( DBHelper.TABLE_NAME_SUM, - values, selection, null ); - db.close(); - if ( 0 == result ) { - DbgUtils.logf( "setName(%d,%s) failed", rowid, name ); - } - } + ContentValues values = new ContentValues(); + values.put( DBHelper.GAME_NAME, name ); + updateRow( context, DBHelper.TABLE_NAME_SUM, rowid, values ); } public static HistoryPair[] getChatHistory( Context context, long rowid ) @@ -874,6 +855,23 @@ 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; @@ -1220,6 +1218,7 @@ public class DBUtils { private static void initDB( Context context ) { if ( null == s_dbHelper ) { + Assert.assertNotNull( context ); s_dbHelper = new DBHelper( context ); // force any upgrade s_dbHelper.getWritableDatabase().close(); diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWPrefs.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWPrefs.java index 4ac4561c0..c6ef8481a 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWPrefs.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWPrefs.java @@ -151,6 +151,25 @@ public class XWPrefs { editor.commit(); } + public static long getPrefsLong( Context context, int keyID, + long defaultValue ) + { + String key = context.getString( keyID ); + SharedPreferences sp = PreferenceManager + .getDefaultSharedPreferences( context ); + return sp.getLong( key, defaultValue ); + } + + public static void setPrefsLong( Context context, int keyID, long newVal ) + { + SharedPreferences sp = PreferenceManager + .getDefaultSharedPreferences( context ); + SharedPreferences.Editor editor = sp.edit(); + String key = context.getString( keyID ); + editor.putLong( key, newVal ); + editor.commit(); + } + public static void setClosedLangs( Context context, String[] langs ) { setPrefsString( context, R.string.key_closed_langs, @@ -275,6 +294,17 @@ public class XWPrefs { return getPrefsBoolean( context, R.string.key_default_loc, true ); } + public static long getDefaultNewGameGroup( Context context ) + { + return getPrefsLong( context, R.string.key_default_group, + DBUtils.ROWID_NOTFOUND ); + } + + public static void setDefaultNewGameGroup( Context context, long val ) + { + setPrefsLong( context, R.string.key_default_group, val ); + } + protected static String getPrefsString( Context context, int keyID ) { String key = context.getString( keyID );