fix crash after lookup of word when language has only one URL

(e.g. Dutch) by not skipping the LookupAlert dialog in that case.
This commit is contained in:
Eric House 2014-06-22 11:12:31 -07:00
parent e9608a867e
commit 8783822bdc
2 changed files with 43 additions and 17 deletions

View file

@ -168,7 +168,6 @@ public class DlgDelegate {
public Dialog createDialog( int id ) public Dialog createDialog( int id )
{ {
// DbgUtils.logf("createDialog(id=%d)", id );
Dialog dialog = null; Dialog dialog = null;
DlgID dlgID = DlgID.values()[id]; DlgID dlgID = DlgID.values()[id];
DlgState state = findForID( dlgID ); DlgState state = findForID( dlgID );
@ -337,9 +336,13 @@ public class DlgDelegate {
public void launchLookup( String[] words, int lang, boolean noStudyOption ) public void launchLookup( String[] words, int lang, boolean noStudyOption )
{ {
if ( LookupAlert.needAlert( m_activity, words, lang ) ) {
Bundle params = LookupAlert.makeParams( words, lang, noStudyOption ); Bundle params = LookupAlert.makeParams( words, lang, noStudyOption );
addState( new DlgState( DlgID.LOOKUP, new Object[]{params} ) ); addState( new DlgState( DlgID.LOOKUP, new Object[]{params} ) );
showDialog( DlgID.LOOKUP ); showDialog( DlgID.LOOKUP );
} else {
LookupAlert.launchWordLookup( m_activity, words[0], lang );
}
} }
public void startProgress( int id ) public void startProgress( int id )
@ -451,7 +454,7 @@ public class DlgDelegate {
{ {
DlgState state = findForID( DlgID.LOOKUP ); DlgState state = findForID( DlgID.LOOKUP );
Bundle bundle = (Bundle)state.m_params[0]; Bundle bundle = (Bundle)state.m_params[0];
return LookupAlert.createDialog( m_activity, bundle ); return LookupAlert.makeDialog( m_activity, bundle );
} }
private Dialog createOKDialog( DlgState state, DlgID dlgID ) private Dialog createOKDialog( DlgState state, DlgID dlgID )

View file

@ -136,7 +136,7 @@ public class LookupAlert extends LinearLayout
{ {
m_parent = activity; m_parent = activity;
m_words = params.getStringArray( WORDS ); m_words = params.getStringArray( WORDS );
setLang( params.getInt( LANG, -1 ) ); setLang( activity, params.getInt( LANG, -1 ) );
m_forceList = params.getBoolean( FORCELIST, false ); m_forceList = params.getBoolean( FORCELIST, false );
m_studyOn = XWPrefs.getStudyEnabled( m_context ); m_studyOn = XWPrefs.getStudyEnabled( m_context );
if ( m_studyOn ) { if ( m_studyOn ) {
@ -186,7 +186,9 @@ public class LookupAlert extends LinearLayout
// } // }
// } // }
/* View.OnClickListener -- just the Done button */ //////////////////////////////////////////////////////////////////////
// View.OnClickListener
//////////////////////////////////////////////////////////////////////
public void onClick( View view ) public void onClick( View view )
{ {
if ( view == m_doneButton ) { if ( view == m_doneButton ) {
@ -200,8 +202,10 @@ public class LookupAlert extends LinearLayout
} }
} }
/* AdapterView.OnItemClickListener */ //////////////////////////////////////////////////////////////////////
public void onItemClick( AdapterView<?> parent, View view, // AdapterView.OnItemClickListener
//////////////////////////////////////////////////////////////////////
public void onItemClick( AdapterView<?> parentView, View view,
int position, long id ) int position, long id )
{ {
if ( STATE_WORDS == m_state ) { if ( STATE_WORDS == m_state ) {
@ -265,7 +269,8 @@ public class LookupAlert extends LinearLayout
} }
break; break;
case STATE_LOOKUP: case STATE_LOOKUP:
lookupWord( m_words[m_wordIndex], s_lookupUrls[m_urlIndex] ); lookupWord( m_context, m_words[m_wordIndex],
s_lookupUrls[m_urlIndex] );
switchState( -1 ); switchState( -1 );
break; break;
default: default:
@ -274,7 +279,7 @@ public class LookupAlert extends LinearLayout
} }
} // switchState } // switchState
private void lookupWord( String word, String fmt ) private static void lookupWord( Context context, String word, String fmt )
{ {
if ( false ) { if ( false ) {
DbgUtils.logf( "skipping lookupWord(%s)", word ); DbgUtils.logf( "skipping lookupWord(%s)", word );
@ -286,21 +291,21 @@ public class LookupAlert extends LinearLayout
intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK ); intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
try { try {
m_context.startActivity( intent ); context.startActivity( intent );
} catch ( android.content.ActivityNotFoundException anfe ) { } catch ( android.content.ActivityNotFoundException anfe ) {
DbgUtils.loge( anfe ); DbgUtils.loge( anfe );
} }
} }
} // lookupWord } // lookupWord
private void setLang( int lang ) private static void setLang( Context context, int lang )
{ {
if ( null == s_langCodes ) { if ( null == s_langCodes ) {
s_langCodes = getResources().getStringArray( R.array.language_codes ); s_langCodes = context.getResources().getStringArray( R.array.language_codes );
} }
if ( s_lang != lang ) { if ( s_lang != lang ) {
String[] urls = getResources().getStringArray( R.array.lookup_urls ); String[] urls = context.getResources().getStringArray( R.array.lookup_urls );
ArrayList<String> tmpUrls = new ArrayList<String>(); ArrayList<String> tmpUrls = new ArrayList<String>();
ArrayList<String> tmpNames = new ArrayList<String>(); ArrayList<String> tmpNames = new ArrayList<String>();
String langCode = String.format( ":%s:", s_langCodes[lang] ); String langCode = String.format( ":%s:", s_langCodes[lang] );
@ -313,7 +318,7 @@ public class LookupAlert extends LinearLayout
} }
s_lookupNames = tmpNames.toArray( new String[tmpNames.size()] ); s_lookupNames = tmpNames.toArray( new String[tmpNames.size()] );
s_lookupUrls = tmpUrls.toArray( new String[tmpUrls.size()] ); s_lookupUrls = tmpUrls.toArray( new String[tmpUrls.size()] );
s_urlsAdapter = new ArrayAdapter<String>( m_context, LIST_LAYOUT, s_urlsAdapter = new ArrayAdapter<String>( context, LIST_LAYOUT,
s_lookupNames ); s_lookupNames );
s_lang = lang; s_lang = lang;
} }
@ -344,6 +349,17 @@ public class LookupAlert extends LinearLayout
return handled; return handled;
} }
public static boolean needAlert( Context context, String[] words,
int langCode )
{
boolean result = 1 < words.length;
if ( !result ) {
setLang( context, langCode );
result = 1 < s_lookupUrls.length;
}
return result;
}
public static Bundle makeParams( String[] words, int lang, public static Bundle makeParams( String[] words, int lang,
boolean noStudyOption ) boolean noStudyOption )
{ {
@ -354,7 +370,7 @@ public class LookupAlert extends LinearLayout
return bundle; return bundle;
} }
public static Dialog createDialog( Activity parent, Bundle bundle ) public static Dialog makeDialog( Activity parent, Bundle bundle )
{ {
LookupAlert view = (LookupAlert) LookupAlert view = (LookupAlert)
LocUtils.inflate( parent, R.layout.lookup ); LocUtils.inflate( parent, R.layout.lookup );
@ -367,4 +383,11 @@ public class LookupAlert extends LinearLayout
result.setOnKeyListener( view ); result.setOnKeyListener( view );
return result; return result;
} }
protected static void launchWordLookup( Context context, String word,
int langCode )
{
setLang( context, langCode );
lookupWord( context, word, s_lookupUrls[0] );
}
} }