Merge branch 'android_branch' into send_in_background

This commit is contained in:
eehouse@eehouse.org 2011-10-06 06:51:05 -07:00 committed by Andy2
commit 6bf8d5abeb
6 changed files with 274 additions and 173 deletions

View file

@ -83,6 +83,11 @@
>
</activity>
<!-- android:theme="@android:style/Theme.Dialog" -->
<activity android:name="LookupActivity"
android:configChanges="keyboardHidden|orientation"
/>
<receiver android:name="RelayReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>

View file

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" -->
<!-- android:layout_width="fill_parent" -->
<!-- android:layout_height="fill_parent" -->
<!-- android:orientation="vertical" -->
<!-- > -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
android:layout_weight="1"
/>
<Button android:id="@+id/button_done"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<!-- android:text="@string/button_done" -->
</LinearLayout>
<!-- </ScrollView> -->

View file

@ -2,6 +2,9 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/board_menu_lookup"
android:title="@string/button_lookup" />
<group android:id="@+id/group_done">
<!-- title set in BoardActivity -->
<item android:id="@+id/board_menu_done"
@ -30,8 +33,8 @@
<item android:title="@string/board_submenu_game">
<menu>
<item android:id="@+id/board_menu_lookup"
android:title="@string/button_lookup" />
<!-- <item android:id="@+id/board_menu_lookup" -->
<!-- android:title="@string/button_lookup" /> -->
<item android:id="@+id/board_menu_game_counts"
android:title="@string/board_menu_game_counts" />
<item android:id="@+id/board_menu_game_left"

View file

@ -1769,6 +1769,7 @@
<string name="button_lookupf">Look up %s</string>
<string name="title_lookup">Tap to look up</string>
<string name="button_done">Done</string>
<string name="button_donef">Done with %s</string>
<string name="site_spinner_label">Pick a site</string>
<string name="site_spinner_prompt">Pick a site</string>

View file

@ -43,12 +43,10 @@ import android.content.DialogInterface.OnDismissListener;
import android.content.DialogInterface.OnCancelListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
@ -80,8 +78,6 @@ public class BoardActivity extends XWActivity
private static final int DLG_DELETED = DLG_OKONLY + 8;
private static final int DLG_INVITE = DLG_OKONLY + 9;
private static final int DLG_SCORES_BLK = DLG_OKONLY + 10;
private static final int DLG_WORDPICK = DLG_OKONLY + 11;
private static final int DLG_URLPICK = DLG_OKONLY + 12;
private static final int CHAT_REQUEST = 1;
private static final int SCREEN_ON_TIME = 10 * 60 * 1000; // 10 mins
@ -148,11 +144,6 @@ public class BoardActivity extends XWActivity
private String m_room;
private String m_toastStr;
private String[] m_words;
private String[] m_wordsWaiting;
private String m_word;
private String[] m_langCodes;
private String[] m_lookupUrls;
private String[] m_lookupNames;
private int m_missing;
private boolean m_haveInvited = false;
@ -217,10 +208,6 @@ public class BoardActivity extends XWActivity
waitCloseGame( false );
GameUtils.deleteGame( BoardActivity.this,
m_rowid, false );
// Intent intent = new Intent();
// intent.putExtra( "delete", true );
// intent.putExtra( "path", m_path );
// setResult( Activity.RESULT_OK, intent );
finish();
}
};
@ -240,8 +227,6 @@ public class BoardActivity extends XWActivity
public void onClick( DialogInterface dialog,
int whichButton ) {
m_resultCode = 1;
m_words = null; // in case it's DLG_SCORES_BLK
m_wordsWaiting = null;
}
};
ab.setPositiveButton( QUERY_REQUEST_BLK == id ?
@ -256,20 +241,19 @@ public class BoardActivity extends XWActivity
};
ab.setNegativeButton( R.string.button_no, lstnr );
} else if ( DLG_SCORES_BLK == id ) {
if ( null != m_wordsWaiting && m_wordsWaiting.length > 0 ) {
if ( null != m_words && m_words.length > 0 ) {
String buttonTxt;
if ( m_wordsWaiting.length == 1 ) {
if ( m_words.length == 1 ) {
buttonTxt = Utils.format( this,
R.string.button_lookupf,
m_wordsWaiting[0] );
m_words[0] );
} else {
buttonTxt = getString( R.string.button_lookup );
}
lstnr = new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dialog,
int whichButton ) {
m_words = m_wordsWaiting;
lookupWord();
launchLookup( m_words );
}
};
ab.setNegativeButton( buttonTxt, lstnr );
@ -344,66 +328,6 @@ public class BoardActivity extends XWActivity
.create();
}
break;
case DLG_WORDPICK:
initLookup();
lstnr = new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dialog,
int item ) {
lookupWord( m_words[item] );
}
};
doneLstnr = new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dlg,
int item ) {
wordPickDone();
}
};
dialog = new AlertDialog.Builder( this )
.setTitle( R.string.title_lookup )
.setItems( m_words, lstnr )
.setNegativeButton( R.string.button_done, doneLstnr )
.setOnCancelListener( new OnCancelListener() {
public void onCancel( DialogInterface dialog ) {
wordPickDone();
}
} )
.create();
Utils.setRemoveOnDismiss( this, dialog, id );
break;
case DLG_URLPICK:
DialogInterface.OnClickListener itemLstnr =
new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dialog,
int item ) {
lookupWord( m_word, m_lookupUrls[item] );
post( new Runnable() {
public void run() {
showDialog( DLG_URLPICK );
}
} );
}
};
doneLstnr = new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dialog,
int item ) {
urlPickDone();
}
};
String fmt = getString( R.string.pick_url_titlef );
String title = String.format( fmt, m_word );
dialog = new AlertDialog.Builder( this )
.setTitle( title )
.setItems( m_lookupNames, itemLstnr )
.setNegativeButton( R.string.button_done, doneLstnr )
.setOnCancelListener( new OnCancelListener() {
public void onCancel( DialogInterface dialog ) {
urlPickDone();
}
} )
.create();
Utils.setRemoveOnDismiss( this, dialog, id );
break;
default:
// just drop it; super.onCreateDialog likely failed
@ -492,8 +416,7 @@ public class BoardActivity extends XWActivity
outState.putString( DLG_BYTES, m_dlgBytes );
outState.putString( ROOM, m_room );
outState.putString( TOASTSTR, m_toastStr );
outState.putStringArray( WORDS, m_wordsWaiting );
outState.putString( LOOKUPITEM, m_word );
outState.putStringArray( WORDS, m_words );
}
private void getBundledData( Bundle bundle )
@ -504,8 +427,7 @@ public class BoardActivity extends XWActivity
m_dlgBytes = bundle.getString( DLG_BYTES );
m_room = bundle.getString( ROOM );
m_toastStr = bundle.getString( TOASTSTR );
m_wordsWaiting = bundle.getStringArray( WORDS );
m_word = bundle.getString( LOOKUPITEM );
m_words = bundle.getStringArray( WORDS );
}
}
@ -537,8 +459,6 @@ public class BoardActivity extends XWActivity
// in case of change...
setBackgroundColor();
setKeepScreenOn();
} else {
lookupWord();
}
}
}
@ -1240,8 +1160,7 @@ public class BoardActivity extends XWActivity
{
m_dlgBytes = expl;
m_dlgTitle = R.string.info_title;
m_wordsWaiting = wordsToMWords( words );
Assert.assertNull( m_words );
m_words = wordsToArray( words );
waitBlockingDialog( DLG_SCORES_BLK, 0 );
}
@ -1366,8 +1285,7 @@ public class BoardActivity extends XWActivity
}
break;
case JNIThread.GOT_WORDS:
m_words = wordsToMWords( (String)msg.obj );
lookupWord();
launchLookup( wordsToArray((String)msg.obj) );
break;
}
}
@ -1641,94 +1559,23 @@ public class BoardActivity extends XWActivity
}
}
private void lookupWord()
{
initLookup();
if ( null == m_words || 0 == m_words.length ) {
// drop it
} else if ( null != m_word ) {
lookupWord( m_word );
} else {
showDialog( DLG_WORDPICK );
}
}
private void lookupWord( String word )
{
m_word = word;
if ( 1 == m_lookupUrls.length ) {
lookupWord( word, m_lookupUrls[0] );
} else {
showDialog( DLG_URLPICK );
}
}
private void lookupWord( String word, String fmt )
{
if ( false ) {
Utils.logf( "skipping lookupWord(%s)", word );
} else {
String langCode = m_langCodes[m_gi.dictLang];
String dict_url = String.format( fmt, langCode, word );
Uri uri = Uri.parse( dict_url );
Intent intent = new Intent( Intent.ACTION_VIEW, uri );
intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
try {
startActivity( intent );
} catch ( android.content.ActivityNotFoundException anfe ) {
Utils.logf( "%s", anfe.toString() );
}
}
} // lookupWord
private void initLookup()
{
if ( null == m_langCodes ) {
Resources res = getResources();
m_langCodes = res.getStringArray( R.array.language_codes );
String[] urls = res.getStringArray( R.array.lookup_urls );
ArrayList<String> tmpUrls = new ArrayList<String>();
ArrayList<String> tmpNames = new ArrayList<String>();
String langCode =
String.format( ":%s:", m_langCodes[m_gi.dictLang] );
for ( int ii = 0; ii < urls.length; ii += 3 ) {
String codes = urls[ii+1];
if ( 0 == codes.length() || codes.contains( langCode ) ) {
tmpNames.add( urls[ii] );
tmpUrls.add( urls[ii+2] );
}
}
m_lookupNames = tmpNames.toArray( new String[tmpNames.size()] );
m_lookupUrls = tmpUrls.toArray( new String[tmpUrls.size()] );
}
} // initLookup
private void urlPickDone() {
m_word = null;
if ( null != m_words && 1 >= m_words.length ) {
m_words = null;
m_wordsWaiting = null;
}
}
private void wordPickDone() {
m_words = null;
m_wordsWaiting = null;
}
private String[] wordsToMWords( String words )
private String[] wordsToArray( String words )
{
String[] tmp = TextUtils.split( words, "\n" );
String[] wordsArray = new String[tmp.length];
for ( int ii = 0, jj = tmp.length; ii < tmp.length; ++ii, --jj ) {
wordsArray[ii] = tmp[jj-1];
}
if ( 1 == wordsArray.length ) {
m_word = wordsArray[0];
}
return wordsArray;
}
private void launchLookup( String[] words )
{
Intent intent = new Intent( this, LookupActivity.class );
intent.putExtra( LookupActivity.WORDS, words );
intent.putExtra( LookupActivity.LANG, m_gi.dictLang );
startActivity( intent );
}
} // class BoardActivity

View file

@ -0,0 +1,216 @@
/* -*- compile-command: "cd ../../../../../; ant install"; -*- */
/*
* Copyright 2009-2011 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.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
import junit.framework.Assert;
public class LookupActivity extends XWListActivity
implements View.OnClickListener,
AdapterView.OnItemClickListener {
public static final String WORDS = "WORDS";
public static final String LANG = "LANG";
private static String[] s_langCodes;
private static String[] s_lookupNames;
private static String[] s_lookupUrls;
private static ArrayAdapter<String> s_urlsAdapter;
private static final int LIST_LAYOUT =
// android.R.layout.simple_spinner_item;
// android.R.layout.select_dialog_item
android.R.layout.simple_list_item_1
;
private static int s_lang = -1;
private String[] m_words;
private static int m_lang;
private int m_wordIndex = 0;
private int m_urlIndex = 0;
private int m_state;
private ArrayAdapter<String> m_wordsAdapter;
private ArrayAdapter<String> m_shown;
private Button m_doneButton;
@Override
protected void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
getBundledData( savedInstanceState );
Intent intent = getIntent();
m_words = intent.getStringArrayExtra( WORDS );
m_lang = intent.getIntExtra( LANG, -1 );
setLang( m_lang );
setContentView( R.layout.lookup );
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_state = 0;
adjustForState();
}
@Override
protected void onSaveInstanceState( Bundle outState )
{
super.onSaveInstanceState( outState );
// if ( null != m_words ) {
// outState.putStringArray( WORDS, m_words );
// }
}
/* View.OnClickListener -- just the Done button */
public void onClick( View view )
{
--m_state;
adjustForState();
}
/* AdapterView.OnItemClickListener */
public void onItemClick( AdapterView<?> parent, View view,
int position, long id )
{
if ( m_shown == m_wordsAdapter ) {
m_wordIndex = position;
Utils.logf( "%s selected", m_words[position] );
} else if ( m_shown == s_urlsAdapter ) {
m_urlIndex = position;
Utils.logf( "%s selected", s_lookupUrls[position] );
} else {
Assert.fail();
}
++m_state;
adjustForState();
}
private void getBundledData( Bundle bundle )
{
// if ( null != bundle ) {
// m_words = bundle.getStringArray( WORDS );
// }
}
private void adjustForState()
{
if ( 0 > m_state ) {
finish();
} else {
switch( m_state ) {
case 0:
if ( 1 < m_words.length ) {
m_shown = m_wordsAdapter;
getListView().setAdapter( m_wordsAdapter );
setTitle( R.string.title_lookup );
m_doneButton.setText( R.string.button_done );
break;
}
case 1:
if ( 1 < s_lookupUrls.length ) {
m_shown = s_urlsAdapter;
getListView().setAdapter( s_urlsAdapter );
setTitle( m_words[m_wordIndex] );
String txt = Utils.format( this, R.string.button_donef,
m_words[m_wordIndex] );
m_doneButton.setText( txt );
break;
}
case 2:
lookupWord( m_words[m_wordIndex], s_lookupUrls[m_urlIndex] );
if ( 0 >= --m_state ) {
finish();
}
break;
}
}
}
private void lookupWord( String word, String fmt )
{
if ( false ) {
Utils.logf( "skipping lookupWord(%s)", word );
} else {
String langCode = s_langCodes[s_lang];
String dict_url = String.format( fmt, langCode, word );
Uri uri = Uri.parse( dict_url );
Intent intent = new Intent( Intent.ACTION_VIEW, uri );
intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
try {
startActivity( intent );
} catch ( android.content.ActivityNotFoundException anfe ) {
Utils.logf( "%s", anfe.toString() );
}
}
} // lookupWord
public void setLang( int lang )
{
if ( null == s_langCodes ) {
s_langCodes = getResources().getStringArray( R.array.language_codes );
}
if ( s_lang != lang ) {
String[] urls = getResources().getStringArray( R.array.lookup_urls );
ArrayList<String> tmpUrls = new ArrayList<String>();
ArrayList<String> tmpNames = new ArrayList<String>();
String langCode = String.format( ":%s:", s_langCodes[lang] );
for ( int ii = 0; ii < urls.length; ii += 3 ) {
String codes = urls[ii+1];
if ( 0 == codes.length() || codes.contains( langCode ) ) {
tmpNames.add( urls[ii] );
tmpUrls.add( urls[ii+2] );
}
}
s_lookupNames = tmpNames.toArray( new String[tmpNames.size()] );
s_lookupUrls = tmpUrls.toArray( new String[tmpUrls.size()] );
s_urlsAdapter = new ArrayAdapter<String>( this, LIST_LAYOUT,
s_lookupNames );
s_lang = lang;
} // initLookup
}
private void setTitle( String word )
{
String title = Utils.format( this, R.string.pick_url_titlef, word );
super.setTitle( title );
}
}