From 0c9d33da3fad1cdab17ae4a3677d8a2f92207f4c Mon Sep 17 00:00:00 2001 From: Andy2 Date: Tue, 26 Apr 2011 18:59:11 -0700 Subject: [PATCH] Add infrastructure to sort dicts by wordcount so robot can have smallest and human largest. Sorting works. Still need to fix when new dicts get assigned after lang change -- too often all players wind up inheriting the default. --- .../eehouse/android/xw4/DictLangCache.java | 61 +++++++++++++++---- .../org/eehouse/android/xw4/jni/DictInfo.java | 1 + 2 files changed, 49 insertions(+), 13 deletions(-) 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 029515b3d..48127a87c 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictLangCache.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictLangCache.java @@ -104,34 +104,67 @@ public class DictLangCache { return count; } - private static String[] getHaveLang( Context context, int code, - boolean withCounts ) + private static DictInfo[] getInfosHaveLang( Context context, int code ) { - ArrayList al = new ArrayList(); + ArrayList al = new ArrayList(); String[] dicts = GameUtils.dictList( context ); - String fmt = "%s (%d)"; // must match stripCount below for ( String dict : dicts ) { DictInfo info = getInfo( context, dict ); if ( code == info.langCode ) { - if ( withCounts ) { - dict = String.format( fmt, dict, info.wordCount ); - } - al.add( dict ); + al.add( info ); } } + DictInfo[] result = al.toArray( new DictInfo[al.size()] ); + return result; + } + + private static String[] getHaveLang( Context context, int code, + Comparator comp, + boolean withCounts ) + { + DictInfo[] infos = getInfosHaveLang( context, code ); + + if ( null != comp ) { + Arrays.sort( infos, comp ); + } + + ArrayList al = new ArrayList(); + String fmt = "%s (%d)"; // must match stripCount below + for ( DictInfo info : infos ) { + String name = info.name; + if ( withCounts ) { + name = String.format( fmt, name, info.wordCount ); + } + al.add( name ); + } String[] result = al.toArray( new String[al.size()] ); - Arrays.sort( result ); + if ( null == comp ) { + Arrays.sort( result ); + } return result; } public static String[] getHaveLang( Context context, int code ) { - return getHaveLang( context, code, false ); + return getHaveLang( context, code, null, false ); + } + + private static Comparator s_ByCount = + new Comparator() { + public int compare( DictInfo di1, DictInfo di2 ) + { + return di2.wordCount - di1.wordCount; + } + }; + + public static String[] getHaveLangByCount( Context context, int code ) + { + return getHaveLang( context, code, s_ByCount, false ); } public static String[] getHaveLangCounts( Context context, int code ) { - return getHaveLang( context, code, true ); + return getHaveLang( context, code, null, true ); } public static String stripCount( String nameWithCount ) @@ -212,9 +245,10 @@ public class DictLangCache { String dict = human? CommonPrefs.getDefaultHumanDict( context ) : CommonPrefs.getDefaultRobotDict( context ); if ( lang != DictLangCache.getDictLangCode( context, dict ) ) { - String dicts[] = getHaveLang( context, lang ); + String dicts[] = getHaveLangByCount( context, lang ); if ( dicts.length > 0 ) { - dict = dicts[0]; + // Human gets biggest; robot gets smallest + dict = dicts[ human ? 0 : dicts.length-1 ]; } else { dict = null; } @@ -284,6 +318,7 @@ public class DictLangCache { byte[] dict = GameUtils.openDict( context, name ); info = new DictInfo(); XwJNI.dict_getInfo( dict, JNIUtilsImpl.get(), info ); + info.name = name; s_nameToLang.put( name, info ); } return info; diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/DictInfo.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/DictInfo.java index 825f9ecf9..bdd366df4 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/DictInfo.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/jni/DictInfo.java @@ -23,4 +23,5 @@ package org.eehouse.android.xw4.jni; public class DictInfo { public int langCode; public int wordCount; + public String name; };