diff --git a/xwords4/android/XWords4/res/layout/lookup.xml b/xwords4/android/XWords4/res/layout/lookup.xml index ba5c89b13..7eaf8e62f 100644 --- a/xwords4/android/XWords4/res/layout/lookup.xml +++ b/xwords4/android/XWords4/res/layout/lookup.xml @@ -1,10 +1,11 @@ - + - + diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DlgDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DlgDelegate.java index ad1b42427..e0d3b2977 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DlgDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DlgDelegate.java @@ -89,9 +89,10 @@ public class DlgDelegate { Iterator iter = m_dlgStates.values().iterator(); while ( iter.hasNext() ) { DlgState state = iter.next(); - String key = String.format( STATE_KEYF, state.m_id ); + int id = state.m_id.ordinal(); + String key = String.format( STATE_KEYF, id ); outState.putParcelable( key, state ); - ids[indx++] = state.m_id.ordinal(); + ids[indx++] = id; } } outState.putIntArray( IDS, ids ); @@ -104,6 +105,9 @@ public class DlgDelegate { DlgID dlgID = DlgID.values()[id]; DlgState state = findForID( dlgID ); switch( dlgID ) { + case LOOKUP: + dialog = createLookupDialog(); + break; case DIALOG_ABOUT: dialog = createAboutDialog(); break; @@ -252,7 +256,9 @@ public class DlgDelegate { public void launchLookup( String[] words, int lang, boolean noStudyOption ) { - LookupActivity.launch( m_activity, words, lang, noStudyOption ); + Bundle params = LookupActivity.makeParams( words, lang, noStudyOption ); + addState( new DlgState( DlgID.LOOKUP, new Object[]{params} ) ); + m_activity.showDialog( DlgID.LOOKUP.ordinal() ); } public void startProgress( int id ) @@ -351,6 +357,13 @@ public class DlgDelegate { .create(); } + private Dialog createLookupDialog() + { + DlgState state = findForID( DlgID.LOOKUP ); + Bundle bundle = (Bundle)state.m_params[0]; + return LookupActivity.createDialog( m_activity, bundle ); + } + private Dialog createOKDialog( DlgState state, DlgID dlgID ) { Dialog dialog = new AlertDialog.Builder( m_activity ) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DlgState.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DlgState.java index 1e5ce3a33..0e2dc3c22 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DlgState.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DlgState.java @@ -70,6 +70,11 @@ public class DlgState implements Parcelable { this( dlgID, null, 0, cbckID, 0 ); } + public DlgState( DlgID dlgID, Object[] params ) + { + this( dlgID, null, 0, 0, 0, params ); + } + public int describeContents() { return 0; } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/LookupActivity.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/LookupActivity.java index 9e06fd44f..e52bf815a 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/LookupActivity.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/LookupActivity.java @@ -1,7 +1,7 @@ /* -*- compile-command: "find-and-ant.sh debug install"; -*- */ /* - * Copyright 2009-2011 by Eric House (xwords@eehouse.org). All - * rights reserved. + * Copyright 2009-2014 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 @@ -21,23 +21,35 @@ package org.eehouse.android.xw4; import android.app.Activity; +import android.app.AlertDialog; +import android.app.Dialog; + import android.content.Context; +import android.content.DialogInterface.OnCancelListener; +import android.content.DialogInterface; import android.content.Intent; import android.content.res.Resources; + import android.net.Uri; import android.os.Bundle; +import android.util.AttributeSet; +import android.view.KeyEvent; + import android.view.View; import android.view.Window; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; +import android.widget.LinearLayout; +import android.widget.ListView; import android.widget.TextView; + import java.util.ArrayList; import junit.framework.Assert; -public class LookupActivity extends XWListActivity - implements View.OnClickListener, +public class LookupActivity extends LinearLayout + implements View.OnClickListener, Dialog.OnKeyListener, AdapterView.OnItemClickListener { public static final String WORDS = "WORDS"; @@ -58,10 +70,13 @@ public class LookupActivity extends XWListActivity private static String[] s_lookupUrls; private static ArrayAdapter s_urlsAdapter; private static final int LIST_LAYOUT = android.R.layout.simple_list_item_1; - // android.R.layout.select_dialog_item; private static int s_lang = -1; + // These two are probably always the same object + private Context m_context; + private Activity m_parent; + private ListView m_listView; private String[] m_words; private boolean m_forceList; private boolean m_studyOn; @@ -73,28 +88,66 @@ public class LookupActivity extends XWListActivity private Button m_studyButton; private TextView m_summary; - @Override - protected void onCreate( Bundle savedInstanceState ) - { - super.onCreate( savedInstanceState ); - requestWindowFeature( Window.FEATURE_NO_TITLE ); - setContentView( R.layout.lookup ); + public LookupActivity( Context context, AttributeSet as ) { + super( context, as ); + m_context = context; + } - Intent intent = getIntent(); - m_words = intent.getStringArrayExtra( WORDS ); - setLang( intent.getIntExtra( LANG, -1 ) ); - m_forceList = intent.getBooleanExtra( FORCELIST, false ); - m_studyOn = XWPrefs.getStudyEnabled( this ); + // @Override + // protected void onCreate( Bundle savedInstanceState ) + // { + // super.onCreate( savedInstanceState ); + // requestWindowFeature( Window.FEATURE_NO_TITLE ); + // setContentView( R.layout.lookup ); + + // Intent intent = getIntent(); + // m_words = intent.getStringArrayExtra( WORDS ); + // setLang( intent.getIntExtra( LANG, -1 ) ); + // m_forceList = intent.getBooleanExtra( FORCELIST, false ); + // m_studyOn = XWPrefs.getStudyEnabled( this ); + // if ( m_studyOn ) { + // m_studyOn = !intent.getBooleanExtra( NOSTUDY, false ); + // } + + // m_state = STATE_DONE; + // adjustState( 1 ); + + // m_wordsAdapter = new ArrayAdapter( this, LIST_LAYOUT, + // m_words ); + // getListView().setOnItemClickListener( this ); + + // m_doneButton = (Button)findViewById( R.id.button_done ); + // m_doneButton.setOnClickListener( this ); + // m_studyButton = (Button)findViewById( R.id.button_study ); + // if ( m_studyOn ) { + // m_studyButton.setOnClickListener( this ); + // } else { + // m_studyButton.setVisibility( View.GONE ); + // } + + // m_summary = (TextView)findViewById( R.id.summary ); + + // switchState(); + // } + + private void init( Activity activity, Bundle params ) + { + m_parent = activity; + m_words = params.getStringArray( WORDS ); + setLang( params.getInt( LANG, -1 ) ); + m_forceList = params.getBoolean( FORCELIST, false ); + m_studyOn = XWPrefs.getStudyEnabled( m_context ); if ( m_studyOn ) { - m_studyOn = !intent.getBooleanExtra( NOSTUDY, false ); + m_studyOn = !params.getBoolean( NOSTUDY, false ); } m_state = STATE_DONE; adjustState( 1 ); - m_wordsAdapter = new ArrayAdapter( this, LIST_LAYOUT, + m_wordsAdapter = new ArrayAdapter( m_context, LIST_LAYOUT, m_words ); - getListView().setOnItemClickListener( this ); + m_listView = (ListView)findViewById( android.R.id.list ); + m_listView.setOnItemClickListener( this ); m_doneButton = (Button)findViewById( R.id.button_done ); m_doneButton.setOnClickListener( this ); @@ -110,26 +163,26 @@ public class LookupActivity extends XWListActivity switchState(); } - @Override - protected void onSaveInstanceState( Bundle outState ) - { - super.onSaveInstanceState( outState ); - outState.putInt( STATE, m_state ); - outState.putInt( WORDINDEX, m_wordIndex ); - outState.putInt( URLINDEX, m_urlIndex ); - } + // @Override + // protected void onSaveInstanceState( Bundle outState ) + // { + // super.onSaveInstanceState( outState ); + // outState.putInt( STATE, m_state ); + // outState.putInt( WORDINDEX, m_wordIndex ); + // outState.putInt( URLINDEX, m_urlIndex ); + // } - private void getBundledData( Bundle bundle ) - { - if ( null == bundle ) { - m_state = STATE_DONE; - adjustState( 1 ); - } else { - m_state = bundle.getInt( STATE ); - m_wordIndex = bundle.getInt( WORDINDEX ); - m_urlIndex = bundle.getInt( URLINDEX ); - } - } + // private void getBundledData( Bundle bundle ) + // { + // if ( null == bundle ) { + // m_state = STATE_DONE; + // adjustState( 1 ); + // } else { + // m_state = bundle.getInt( STATE ); + // m_wordIndex = bundle.getInt( WORDINDEX ); + // m_urlIndex = bundle.getInt( URLINDEX ); + // } + // } /* View.OnClickListener -- just the Done button */ public void onClick( View view ) @@ -138,10 +191,10 @@ public class LookupActivity extends XWListActivity switchState( -1 ); } else if ( view == m_studyButton ) { String word = m_words[m_wordIndex]; - DBUtils.addToStudyList( this, word, s_lang ); + DBUtils.addToStudyList( m_context, word, s_lang ); - String msg = getString( R.string.add_donef, word ); - Utils.showToast( this, msg ); + String msg = m_context.getString( R.string.add_donef, word ); + Utils.showToast( m_context, msg ); } } @@ -187,23 +240,23 @@ public class LookupActivity extends XWListActivity { switch( m_state ) { case STATE_DONE: - finish(); + m_parent.removeDialog( DlgID.LOOKUP.ordinal() ); break; case STATE_WORDS: - getListView().setAdapter( m_wordsAdapter ); + m_listView.setAdapter( m_wordsAdapter ); setSummary( m_studyOn ? R.string.title_lookup_study : R.string.title_lookup ); m_doneButton.setText( R.string.button_done ); m_studyButton.setVisibility( View.GONE ); break; case STATE_URLS: - getListView().setAdapter( s_urlsAdapter ); + m_listView.setAdapter( s_urlsAdapter ); setSummary( m_words[m_wordIndex] ); - String txt = Utils.format( this, R.string.button_donef, + String txt = Utils.format( m_context, R.string.button_donef, m_words[m_wordIndex] ); m_doneButton.setText( txt ); - txt = Utils.format( this, R.string.add_to_studyf, - m_words[m_wordIndex] ); + txt = m_context.getString( R.string.add_to_studyf, + m_words[m_wordIndex] ); if ( m_studyOn ) { m_studyButton.setVisibility( View.VISIBLE ); m_studyButton.setText( txt ); @@ -231,7 +284,7 @@ public class LookupActivity extends XWListActivity intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK ); try { - startActivity( intent ); + m_context.startActivity( intent ); } catch ( android.content.ActivityNotFoundException anfe ) { DbgUtils.loge( anfe ); } @@ -258,7 +311,7 @@ public class LookupActivity extends XWListActivity } s_lookupNames = tmpNames.toArray( new String[tmpNames.size()] ); s_lookupUrls = tmpUrls.toArray( new String[tmpUrls.size()] ); - s_urlsAdapter = new ArrayAdapter( this, LIST_LAYOUT, + s_urlsAdapter = new ArrayAdapter( m_context, LIST_LAYOUT, s_lookupNames ); s_lang = lang; } @@ -266,23 +319,47 @@ public class LookupActivity extends XWListActivity private void setSummary( int id ) { - m_summary.setText( getString( id ) ); + m_summary.setText( m_context.getString( id ) ); } private void setSummary( String word ) { - String title = Utils.format( this, R.string.pick_url_titlef, word ); + String title = m_context.getString( R.string.pick_url_titlef, word ); m_summary.setText( title ); } - public static void launch( Activity activity, String[] words, int lang, - boolean noStudyOption ) + ////////////////////////////////////////////////////////////////////// + // Dialog.OnKeyListener interface + ////////////////////////////////////////////////////////////////////// + public boolean onKey( DialogInterface arg0, int keyCode, KeyEvent event ) { - Intent intent = new Intent( activity, LookupActivity.class ); - intent.putExtra( WORDS, words ); - intent.putExtra( LANG, lang ); - intent.putExtra( NOSTUDY, noStudyOption ); + boolean handled = keyCode == KeyEvent.KEYCODE_BACK + && KeyEvent.ACTION_UP == event.getAction(); + if ( handled ) { + switchState( -1 ); + } + return handled; + } - activity.startActivity( intent ); + public static Bundle makeParams( String[] words, int lang, + boolean noStudyOption ) + { + Bundle bundle = new Bundle(); + bundle.putStringArray( WORDS, words ); + bundle.putInt( LANG, lang ); + bundle.putBoolean( NOSTUDY, noStudyOption ); + return bundle; + } + + public static Dialog createDialog( Activity parent, Bundle bundle ) + { + LookupActivity view = (LookupActivity)Utils.inflate( parent, R.layout.lookup ); + view.init( parent, bundle ); + + Dialog result = new AlertDialog.Builder( parent ) + .setView( view ) + .create(); + result.setOnKeyListener( view ); + return result; } }