Add new class to cache lang codes for dicts to prevent expensive

operation of loading them each time.  Will have other uses later.
This commit is contained in:
Andy2 2010-08-26 18:14:14 -07:00
parent 40d8aede88
commit 0313421634
2 changed files with 50 additions and 3 deletions

View file

@ -0,0 +1,48 @@
/* -*- compile-command: "cd ../../../../../; ant install"; -*- */
/*
* Copyright 2010 by Eric House (xwords@eehouse.org). All rights
* reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.eehouse.android.xw4;
import android.content.Context;
import java.util.HashMap;
import org.eehouse.android.xw4.jni.JNIUtilsImpl;
import org.eehouse.android.xw4.jni.XwJNI;
public class DictLangCache {
private static HashMap<String,Integer> s_nameToLang;
public static int getLangCode( Context context, String name )
{
if ( null == s_nameToLang ) {
s_nameToLang = new HashMap<String,Integer>();
}
int code;
if ( s_nameToLang.containsKey( name ) ) {
code = s_nameToLang.get( name );
} else {
byte[] dict = GameUtils.openDict( context, name );
code = XwJNI.dict_getLanguageCode( dict, JNIUtilsImpl.get() );
s_nameToLang.put( name, new Integer(code) );
}
return code;
}
}

View file

@ -66,9 +66,8 @@ public class DictsActivity extends ListActivity
view.setPosition( position );
// append language code
byte[] dict = GameUtils.openDict( DictsActivity.this,
int code = DictLangCache.getLangCode( DictsActivity.this,
m_dicts[position] );
int code = XwJNI.dict_getLanguageCode( dict, JNIUtilsImpl.get() );
view.setText( m_dicts[position] + " (" + m_langNames[code] + ")" );
if ( !GameUtils.dictIsBuiltin( DictsActivity.this,