try, again, making lookup dialog an alert rather than an activity. It's a much better experience on a nexus 5 but I've lost my records of why I made it an activity in the first place -- what phones it sucked on as an alert. Still probably doesn't bundle itself correctly and is missing a title.

This commit is contained in:
Eric House 2014-03-14 18:41:54 -07:00
parent d4930e157b
commit be440c8abf
4 changed files with 163 additions and 67 deletions

View file

@ -1,10 +1,11 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <org.eehouse.android.xw4.LookupActivity
android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:orientation="vertical"
android:layout_height="fill_parent" android:layout_width="fill_parent"
> android:layout_height="fill_parent"
>
<TextView android:id="@+id/summary" <TextView android:id="@+id/summary"
android:layout_height="wrap_content" android:layout_height="wrap_content"
@ -28,4 +29,4 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
/> />
</LinearLayout> </org.eehouse.android.xw4.LookupActivity>

View file

@ -89,9 +89,10 @@ public class DlgDelegate {
Iterator<DlgState> iter = m_dlgStates.values().iterator(); Iterator<DlgState> iter = m_dlgStates.values().iterator();
while ( iter.hasNext() ) { while ( iter.hasNext() ) {
DlgState state = iter.next(); 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 ); outState.putParcelable( key, state );
ids[indx++] = state.m_id.ordinal(); ids[indx++] = id;
} }
} }
outState.putIntArray( IDS, ids ); outState.putIntArray( IDS, ids );
@ -104,6 +105,9 @@ public class DlgDelegate {
DlgID dlgID = DlgID.values()[id]; DlgID dlgID = DlgID.values()[id];
DlgState state = findForID( dlgID ); DlgState state = findForID( dlgID );
switch( dlgID ) { switch( dlgID ) {
case LOOKUP:
dialog = createLookupDialog();
break;
case DIALOG_ABOUT: case DIALOG_ABOUT:
dialog = createAboutDialog(); dialog = createAboutDialog();
break; break;
@ -252,7 +256,9 @@ public class DlgDelegate {
public void launchLookup( String[] words, int lang, boolean noStudyOption ) 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 ) public void startProgress( int id )
@ -351,6 +357,13 @@ public class DlgDelegate {
.create(); .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 ) private Dialog createOKDialog( DlgState state, DlgID dlgID )
{ {
Dialog dialog = new AlertDialog.Builder( m_activity ) Dialog dialog = new AlertDialog.Builder( m_activity )

View file

@ -70,6 +70,11 @@ public class DlgState implements Parcelable {
this( dlgID, null, 0, cbckID, 0 ); this( dlgID, null, 0, cbckID, 0 );
} }
public DlgState( DlgID dlgID, Object[] params )
{
this( dlgID, null, 0, 0, 0, params );
}
public int describeContents() { public int describeContents() {
return 0; return 0;
} }

View file

@ -1,7 +1,7 @@
/* -*- compile-command: "find-and-ant.sh debug install"; -*- */ /* -*- compile-command: "find-and-ant.sh debug install"; -*- */
/* /*
* Copyright 2009-2011 by Eric House (xwords@eehouse.org). All * Copyright 2009-2014 by Eric House (xwords@eehouse.org). All rights
* rights reserved. * reserved.
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as * modify it under the terms of the GNU General Public License as
@ -21,23 +21,35 @@
package org.eehouse.android.xw4; package org.eehouse.android.xw4;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.res.Resources; import android.content.res.Resources;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View; import android.view.View;
import android.view.Window; import android.view.Window;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.Button; import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
import java.util.ArrayList; import java.util.ArrayList;
import junit.framework.Assert; import junit.framework.Assert;
public class LookupActivity extends XWListActivity public class LookupActivity extends LinearLayout
implements View.OnClickListener, implements View.OnClickListener, Dialog.OnKeyListener,
AdapterView.OnItemClickListener { AdapterView.OnItemClickListener {
public static final String WORDS = "WORDS"; public static final String WORDS = "WORDS";
@ -58,10 +70,13 @@ public class LookupActivity extends XWListActivity
private static String[] s_lookupUrls; private static String[] s_lookupUrls;
private static ArrayAdapter<String> s_urlsAdapter; private static ArrayAdapter<String> s_urlsAdapter;
private static final int LIST_LAYOUT = android.R.layout.simple_list_item_1; 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; 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 String[] m_words;
private boolean m_forceList; private boolean m_forceList;
private boolean m_studyOn; private boolean m_studyOn;
@ -73,28 +88,66 @@ public class LookupActivity extends XWListActivity
private Button m_studyButton; private Button m_studyButton;
private TextView m_summary; private TextView m_summary;
@Override public LookupActivity( Context context, AttributeSet as ) {
protected void onCreate( Bundle savedInstanceState ) super( context, as );
{ m_context = context;
super.onCreate( savedInstanceState ); }
requestWindowFeature( Window.FEATURE_NO_TITLE );
setContentView( R.layout.lookup );
Intent intent = getIntent(); // @Override
m_words = intent.getStringArrayExtra( WORDS ); // protected void onCreate( Bundle savedInstanceState )
setLang( intent.getIntExtra( LANG, -1 ) ); // {
m_forceList = intent.getBooleanExtra( FORCELIST, false ); // super.onCreate( savedInstanceState );
m_studyOn = XWPrefs.getStudyEnabled( this ); // 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<String>( 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 ) { if ( m_studyOn ) {
m_studyOn = !intent.getBooleanExtra( NOSTUDY, false ); m_studyOn = !params.getBoolean( NOSTUDY, false );
} }
m_state = STATE_DONE; m_state = STATE_DONE;
adjustState( 1 ); adjustState( 1 );
m_wordsAdapter = new ArrayAdapter<String>( this, LIST_LAYOUT, m_wordsAdapter = new ArrayAdapter<String>( m_context, LIST_LAYOUT,
m_words ); 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 = (Button)findViewById( R.id.button_done );
m_doneButton.setOnClickListener( this ); m_doneButton.setOnClickListener( this );
@ -110,26 +163,26 @@ public class LookupActivity extends XWListActivity
switchState(); switchState();
} }
@Override // @Override
protected void onSaveInstanceState( Bundle outState ) // protected void onSaveInstanceState( Bundle outState )
{ // {
super.onSaveInstanceState( outState ); // super.onSaveInstanceState( outState );
outState.putInt( STATE, m_state ); // outState.putInt( STATE, m_state );
outState.putInt( WORDINDEX, m_wordIndex ); // outState.putInt( WORDINDEX, m_wordIndex );
outState.putInt( URLINDEX, m_urlIndex ); // outState.putInt( URLINDEX, m_urlIndex );
} // }
private void getBundledData( Bundle bundle ) // private void getBundledData( Bundle bundle )
{ // {
if ( null == bundle ) { // if ( null == bundle ) {
m_state = STATE_DONE; // m_state = STATE_DONE;
adjustState( 1 ); // adjustState( 1 );
} else { // } else {
m_state = bundle.getInt( STATE ); // m_state = bundle.getInt( STATE );
m_wordIndex = bundle.getInt( WORDINDEX ); // m_wordIndex = bundle.getInt( WORDINDEX );
m_urlIndex = bundle.getInt( URLINDEX ); // m_urlIndex = bundle.getInt( URLINDEX );
} // }
} // }
/* View.OnClickListener -- just the Done button */ /* View.OnClickListener -- just the Done button */
public void onClick( View view ) public void onClick( View view )
@ -138,10 +191,10 @@ public class LookupActivity extends XWListActivity
switchState( -1 ); switchState( -1 );
} else if ( view == m_studyButton ) { } else if ( view == m_studyButton ) {
String word = m_words[m_wordIndex]; 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 ); String msg = m_context.getString( R.string.add_donef, word );
Utils.showToast( this, msg ); Utils.showToast( m_context, msg );
} }
} }
@ -187,23 +240,23 @@ public class LookupActivity extends XWListActivity
{ {
switch( m_state ) { switch( m_state ) {
case STATE_DONE: case STATE_DONE:
finish(); m_parent.removeDialog( DlgID.LOOKUP.ordinal() );
break; break;
case STATE_WORDS: case STATE_WORDS:
getListView().setAdapter( m_wordsAdapter ); m_listView.setAdapter( m_wordsAdapter );
setSummary( m_studyOn ? setSummary( m_studyOn ?
R.string.title_lookup_study : R.string.title_lookup ); R.string.title_lookup_study : R.string.title_lookup );
m_doneButton.setText( R.string.button_done ); m_doneButton.setText( R.string.button_done );
m_studyButton.setVisibility( View.GONE ); m_studyButton.setVisibility( View.GONE );
break; break;
case STATE_URLS: case STATE_URLS:
getListView().setAdapter( s_urlsAdapter ); m_listView.setAdapter( s_urlsAdapter );
setSummary( m_words[m_wordIndex] ); 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_words[m_wordIndex] );
m_doneButton.setText( txt ); m_doneButton.setText( txt );
txt = Utils.format( this, R.string.add_to_studyf, txt = m_context.getString( R.string.add_to_studyf,
m_words[m_wordIndex] ); m_words[m_wordIndex] );
if ( m_studyOn ) { if ( m_studyOn ) {
m_studyButton.setVisibility( View.VISIBLE ); m_studyButton.setVisibility( View.VISIBLE );
m_studyButton.setText( txt ); m_studyButton.setText( txt );
@ -231,7 +284,7 @@ public class LookupActivity extends XWListActivity
intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK ); intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
try { try {
startActivity( intent ); m_context.startActivity( intent );
} catch ( android.content.ActivityNotFoundException anfe ) { } catch ( android.content.ActivityNotFoundException anfe ) {
DbgUtils.loge( anfe ); DbgUtils.loge( anfe );
} }
@ -258,7 +311,7 @@ public class LookupActivity extends XWListActivity
} }
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>( this, LIST_LAYOUT, s_urlsAdapter = new ArrayAdapter<String>( m_context, LIST_LAYOUT,
s_lookupNames ); s_lookupNames );
s_lang = lang; s_lang = lang;
} }
@ -266,23 +319,47 @@ public class LookupActivity extends XWListActivity
private void setSummary( int id ) private void setSummary( int id )
{ {
m_summary.setText( getString( id ) ); m_summary.setText( m_context.getString( id ) );
} }
private void setSummary( String word ) 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 ); 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 ); boolean handled = keyCode == KeyEvent.KEYCODE_BACK
intent.putExtra( WORDS, words ); && KeyEvent.ACTION_UP == event.getAction();
intent.putExtra( LANG, lang ); if ( handled ) {
intent.putExtra( NOSTUDY, noStudyOption ); 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;
} }
} }