From 03134216344b32f3459f2b643fb3f7895ca414ab Mon Sep 17 00:00:00 2001 From: Andy2 Date: Thu, 26 Aug 2010 18:14:14 -0700 Subject: [PATCH] Add new class to cache lang codes for dicts to prevent expensive operation of loading them each time. Will have other uses later. --- .../eehouse/android/xw4/DictLangCache.java | 48 +++++++++++++++++++ .../eehouse/android/xw4/DictsActivity.java | 5 +- 2 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 xwords4/android/XWords4/src/org/eehouse/android/xw4/DictLangCache.java diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictLangCache.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictLangCache.java new file mode 100644 index 000000000..8a485cd53 --- /dev/null +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictLangCache.java @@ -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 s_nameToLang; + + public static int getLangCode( Context context, String name ) + { + if ( null == s_nameToLang ) { + s_nameToLang = new HashMap(); + } + + 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; + } +} \ No newline at end of file diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictsActivity.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictsActivity.java index d8d093735..1d151af21 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictsActivity.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictsActivity.java @@ -66,9 +66,8 @@ public class DictsActivity extends ListActivity view.setPosition( position ); // append language code - byte[] dict = GameUtils.openDict( DictsActivity.this, - m_dicts[position] ); - int code = XwJNI.dict_getLanguageCode( dict, JNIUtilsImpl.get() ); + int code = DictLangCache.getLangCode( DictsActivity.this, + m_dicts[position] ); view.setText( m_dicts[position] + " (" + m_langNames[code] + ")" ); if ( !GameUtils.dictIsBuiltin( DictsActivity.this,