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 fcdbff254..26e570c4d 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBHelper.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBHelper.java @@ -28,7 +28,8 @@ public class DBHelper extends SQLiteOpenHelper { public static final String TABLE_NAME_SUM = "summaries"; public static final String TABLE_NAME_OBITS = "obits"; - public static final String TABLE_NAME_DICTS = "dicts"; + public static final String TABLE_NAME_DICTBROWSE = "dictbrowse"; + public static final String TABLE_NAME_DICTINFO = "dictinfo"; private static final String DB_NAME = "xwdb"; private static final int DB_VERSION = 13; @@ -138,13 +139,18 @@ public class DBHelper extends SQLiteOpenHelper { private void onCreateDictsDB( SQLiteDatabase db ) { - db.execSQL( "CREATE TABLE " + TABLE_NAME_DICTS + " (" + db.execSQL( "CREATE TABLE " + TABLE_NAME_DICTINFO + " (" + DICTNAME + " TEXT," + MD5SUM + " TEXT(32)," + WORDCOUNT + " INTEGER," - + WORDCOUNTS + " TEXT," + LANGCODE + " INTEGER," - + LOC + " INTEGER(2)," + + LOC + " INTEGER(2)" + + ");" ); + + db.execSQL( "CREATE TABLE " + TABLE_NAME_DICTBROWSE + " (" + + DICTNAME + " TEXT," + + WORDCOUNT + " INTEGER," + + WORDCOUNTS + " TEXT," + ITERMIN + " INTEGER(4)," + ITERMAX + " INTEGER(4)," + ITERPOS + " INTEGER," 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 a23db96d8..d732e94dc 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java @@ -964,7 +964,7 @@ public class DBUtils { DBHelper.WORDCOUNTS, DBHelper.WORDCOUNT, DBHelper.ITERPREFIX }; String selection = String.format( NAME_FMT, DBHelper.DICTNAME, name ); - Cursor cursor = db.query( DBHelper.TABLE_NAME_DICTS, columns, + Cursor cursor = db.query( DBHelper.TABLE_NAME_DICTBROWSE, columns, selection, null, null, null, null ); if ( 1 == cursor.getCount() && cursor.moveToFirst() ) { result = new DictBrowseState(); @@ -1021,16 +1021,83 @@ public class DBUtils { } values.put( DBHelper.WORDCOUNTS, TextUtils.join( ":", nums ) ); } - int result = db.update( DBHelper.TABLE_NAME_DICTS, + int result = db.update( DBHelper.TABLE_NAME_DICTBROWSE, values, selection, null ); if ( 0 == result ) { values.put( DBHelper.DICTNAME, name ); - db.insert( DBHelper.TABLE_NAME_DICTS, null, values ); + db.insert( DBHelper.TABLE_NAME_DICTBROWSE, null, values ); } db.close(); } } + public static DictInfo dictsGetInfo( Context context, String name ) + { + DictInfo result = null; + initDB( context ); + synchronized( s_dbHelper ) { + SQLiteDatabase db = s_dbHelper.getReadableDatabase(); + String[] columns = { DBHelper.LANGCODE, + DBHelper.WORDCOUNT, + DBHelper.MD5SUM, + DBHelper.LOC }; + String selection = String.format( NAME_FMT, DBHelper.DICTNAME, name ); + Cursor cursor = db.query( DBHelper.TABLE_NAME_DICTINFO, columns, + selection, null, null, null, null ); + if ( 1 == cursor.getCount() && cursor.moveToFirst() ) { + result = new DictInfo(); + result.name = name; + result.langCode = + cursor.getInt( cursor.getColumnIndex(DBHelper.LANGCODE)); + result.wordCount = + cursor.getInt( cursor.getColumnIndex(DBHelper.WORDCOUNT)); + result.md5Sum = + cursor.getString( cursor.getColumnIndex(DBHelper.MD5SUM)); + } + cursor.close(); + db.close(); + } + return result; + } + + public static void dictsSetInfo( Context context, DictUtils.DictAndLoc dal, + DictInfo info ) + { + initDB( context ); + synchronized( s_dbHelper ) { + SQLiteDatabase db = s_dbHelper.getWritableDatabase(); + String selection = + String.format( NAME_FMT, DBHelper.DICTNAME, dal.name ); + ContentValues values = new ContentValues(); + + values.put( DBHelper.LANGCODE, info.langCode ); + values.put( DBHelper.WORDCOUNT, info.wordCount ); + values.put( DBHelper.MD5SUM, info.md5Sum ); + values.put( DBHelper.LOC, dal.loc.ordinal() ); + + int result = db.update( DBHelper.TABLE_NAME_DICTINFO, + values, selection, null ); + if ( 0 == result ) { + values.put( DBHelper.DICTNAME, dal.name ); + db.insert( DBHelper.TABLE_NAME_DICTINFO, null, values ); + } + db.close(); + } + } + + public static void dictsRemoveInfo( Context context, + DictUtils.DictAndLoc dal ) + { + initDB( context ); + synchronized( s_dbHelper ) { + SQLiteDatabase db = s_dbHelper.getWritableDatabase(); + String selection = + String.format( NAME_FMT, DBHelper.DICTNAME, dal.name ); + db.delete( DBHelper.TABLE_NAME_DICTINFO, selection, null ); + db.close(); + } + } + private static void copyGameDB( Context context, boolean toSDCard ) { String name = DBHelper.getDBName(); diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictLangCache.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictLangCache.java index d07fa8b83..c6ecddf96 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictLangCache.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictLangCache.java @@ -39,8 +39,6 @@ import org.eehouse.android.xw4.jni.DictInfo; import org.eehouse.android.xw4.jni.CommonPrefs; public class DictLangCache { - private static final HashMap s_nameToLang = - new HashMap(); private static String[] s_langNames; private static int m_adaptedLang = -1; @@ -230,7 +228,8 @@ public class DictLangCache { DictUtils.DictLoc loc, boolean added ) { DictAndLoc dal = new DictAndLoc( name, loc ); - s_nameToLang.remove( dal ); + DBUtils.dictsRemoveInfo( context, dal ); + if ( added ) { getInfo( context, dal ); } @@ -352,15 +351,7 @@ public class DictLangCache { private static DictInfo getInfo( Context context, String name ) { - DictInfo result = null; - Set keys = s_nameToLang.keySet(); - for ( DictAndLoc key : keys ) { - if ( key.name.equals(name) ) { - result = s_nameToLang.get( key ); - break; - } - } - + DictInfo result = DBUtils.dictsGetInfo( context, name ); if ( null == result ) { DictUtils.DictLoc loc = DictUtils.getDictLoc( context, name ); result = getInfo( context, new DictAndLoc( name, loc ) ); @@ -370,28 +361,19 @@ public class DictLangCache { private static DictInfo getInfo( Context context, DictAndLoc dal ) { - DictInfo info; - if ( s_nameToLang.containsKey( dal ) ) { - info = s_nameToLang.get( dal ); - } else { + DictInfo info = DBUtils.dictsGetInfo( context, dal.name ); + if ( null == info ) { String[] names = { dal.name }; DictUtils.DictPairs pairs = DictUtils.openDicts( context, names ); info = new DictInfo(); - - // It should not be possible for dict_getInfo to fail - // unless DictUtils.dictList() isn't doing its job and - // puts unchecked dicts on the list. Should probably - // assert that this returns true. Open question: do I - // always trust dicts in the BUILTIN and INTERNAL - // locations? Files can get damaged.... if ( XwJNI.dict_getInfo( pairs.m_bytes[0], pairs.m_paths[0], JNIUtilsImpl.get(), DictUtils.DictLoc.DOWNLOAD == dal.loc, info ) ) { info.name = dal.name; - s_nameToLang.put( dal, info ); + DBUtils.dictsSetInfo( context, dal, info ); } else { info = null; DbgUtils.logf( "getInfo(): unable to open dict %s", dal.name );