when user while configuring game chooses to download a new wordlist,

make it the new selection in the spinner from which "Download more"
was chosen.
This commit is contained in:
Eric House 2014-10-08 19:12:00 -07:00
parent e4e5d599cd
commit 74eb6890b9
7 changed files with 128 additions and 118 deletions

View file

@ -553,7 +553,7 @@ public class DictUtils {
return str; return str;
} }
private static String addDictExtn( String str ) public static String addDictExtn( String str )
{ {
if ( ! str.endsWith( XWConstants.DICT_EXTN ) ) { if ( ! str.endsWith( XWConstants.DICT_EXTN ) ) {
str += XWConstants.DICT_EXTN; str += XWConstants.DICT_EXTN;

View file

@ -30,6 +30,7 @@ import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
@ -99,8 +100,7 @@ public class DictsDelegate extends ListDelegateBase
private String m_origTitle; private String m_origTitle;
private boolean m_showRemote = false; private boolean m_showRemote = false;
private String m_filterLang; private String m_filterLang;
private Map<String, String> m_needUpdates; private Map<String, Uri> m_needUpdates;
private HashMap<String, XWListItem> m_curDownloads;
private String m_onServerStr; private String m_onServerStr;
private String m_lastLang; private String m_lastLang;
private String m_lastDict; private String m_lastDict;
@ -578,22 +578,24 @@ public class DictsDelegate extends ListDelegateBase
clearSelections(); clearSelections();
break; break;
case R.id.dicts_download: case R.id.dicts_download:
String[] urls = new String[countNeedDownload()]; Uri[] uris = new Uri[countNeedDownload()];
String[] names = new String[uris.length];
int count = 0; int count = 0;
m_curDownloads = new HashMap<String, XWListItem>();
for ( Iterator<XWListItem> iter = m_selDicts.values().iterator(); for ( Iterator<XWListItem> iter = m_selDicts.values().iterator();
iter.hasNext(); ) { iter.hasNext(); ) {
XWListItem litm = iter.next(); XWListItem litm = iter.next();
Object cached = litm.getCached(); Object cached = litm.getCached();
if ( cached instanceof DictInfo ) { if ( cached instanceof DictInfo ) {
DictInfo info = (DictInfo)cached; DictInfo info = (DictInfo)cached;
String url = Utils.makeDictUrl( m_activity, info.m_lang, String name = litm.getText();
litm.getText() ); Uri uri = Utils.makeDictUri( m_activity, info.m_lang,
urls[count++] = url; name );
m_curDownloads.put( url, litm ); uris[count] = uri;
names[count] = name;
++count;
} }
} }
DwnldDelegate.downloadDictsInBack( m_activity, urls, this ); DwnldDelegate.downloadDictsInBack( m_activity, uris, names, this );
break; break;
default: default:
handled = false; handled = false;
@ -635,9 +637,12 @@ public class DictsDelegate extends ListDelegateBase
{ {
int loci = intent.getIntExtra( UpdateCheckReceiver.NEW_DICT_LOC, 0 ); int loci = intent.getIntExtra( UpdateCheckReceiver.NEW_DICT_LOC, 0 );
if ( 0 < loci ) { if ( 0 < loci ) {
String name =
intent.getStringExtra( UpdateCheckReceiver.NEW_DICT_NAME );
String url = String url =
intent.getStringExtra( UpdateCheckReceiver.NEW_DICT_URL ); intent.getStringExtra( UpdateCheckReceiver.NEW_DICT_URL );
DwnldDelegate.downloadDictInBack( m_activity, url, null ); Uri uri = Uri.parse( url );
DwnldDelegate.downloadDictInBack( m_activity, uri, name, null );
finish(); finish();
} }
} }
@ -801,9 +806,17 @@ public class DictsDelegate extends ListDelegateBase
mkListAdapter(); mkListAdapter();
break; break;
case UPDATE_DICTS_ACTION: case UPDATE_DICTS_ACTION:
String[] urls = m_needUpdates.values(). Uri[] uris = new Uri[m_needUpdates.size()];
toArray( new String[m_needUpdates.size()] ); String[] names = new String[uris.length];
DwnldDelegate.downloadDictsInBack( m_activity, urls, this ); int count = 0;
for ( Iterator<String> iter = m_needUpdates.keySet().iterator();
iter.hasNext(); ) {
String name = iter.next();
names[count] = name;
uris[count] = m_needUpdates.get( name );
++count;
}
DwnldDelegate.downloadDictsInBack( m_activity, uris, names, this );
break; break;
default: default:
Assert.fail(); Assert.fail();
@ -944,12 +957,12 @@ public class DictsDelegate extends ListDelegateBase
{ {
Assert.fail(); Assert.fail();
return null; return null;
// String dict_url = Utils.makeDictUrl( context, lang, dict ); // String dict_url = Utils.makeDictUri( context, lang, dict );
// return mkDownloadIntent( context, dict_url ); // return mkDownloadIntent( context, dict_url );
} }
public static void launchForResult( Activity activity, int requestCode, public static void downloadForResult( Activity activity, int requestCode,
int lang, String name ) int lang, String name )
{ {
Intent intent = new Intent( activity, DictsActivity.class ); Intent intent = new Intent( activity, DictsActivity.class );
intent.putExtra( DICT_SHOWREMOTE, true ); intent.putExtra( DICT_SHOWREMOTE, true );
@ -964,15 +977,15 @@ public class DictsDelegate extends ListDelegateBase
activity.startActivityForResult( intent, requestCode ); activity.startActivityForResult( intent, requestCode );
} }
public static void launchForResult( Activity activity, int requestCode, public static void downloadForResult( Activity activity, int requestCode,
int lang ) int lang )
{ {
launchForResult( activity, requestCode, lang, null ); downloadForResult( activity, requestCode, lang, null );
} }
public static void launchForResult( Activity activity, int requestCode ) public static void downloadForResult( Activity activity, int requestCode )
{ {
launchForResult( activity, requestCode, 0, null ); downloadForResult( activity, requestCode, 0, null );
} }
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
@ -1143,7 +1156,7 @@ public class DictsDelegate extends ListDelegateBase
boolean success = false; boolean success = false;
JSONArray langs = null; JSONArray langs = null;
m_needUpdates = new HashMap<String, String>(); m_needUpdates = new HashMap<String, Uri>();
if ( null != jsonData ) { if ( null != jsonData ) {
Set<String> closedLangs = new HashSet<String>(); Set<String> closedLangs = new HashSet<String>();
final Set<String> curLangs = final Set<String> curLangs =
@ -1211,10 +1224,10 @@ public class DictsDelegate extends ListDelegateBase
} }
} }
if ( !matches ) { if ( !matches ) {
String url = Uri uri =
Utils.makeDictUrl( m_activity, Utils.makeDictUri( m_activity,
langName, name ); langName, name );
m_needUpdates.put( name, url ); m_needUpdates.put( name, uri );
} }
} }
} }

View file

@ -27,6 +27,7 @@ import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.Parcelable;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -46,8 +47,8 @@ import java.security.MessageDigest;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List;
import java.util.Map; import java.util.Map;
import junit.framework.Assert; import junit.framework.Assert;
public class DwnldDelegate extends ListDelegateBase { public class DwnldDelegate extends ListDelegateBase {
@ -72,16 +73,18 @@ public class DwnldDelegate extends ListDelegateBase {
// Track callbacks for downloads. // Track callbacks for downloads.
private static class ListenerData { private static class ListenerData {
public ListenerData( String name, DownloadFinishedListener lstnr ) public ListenerData( Uri uri, String name, DownloadFinishedListener lstnr )
{ {
m_uri = uri;
m_name = name; m_name = name;
m_lstnr = lstnr; m_lstnr = lstnr;
} }
public Uri m_uri;
public String m_name; public String m_name;
public DownloadFinishedListener m_lstnr; public DownloadFinishedListener m_lstnr;
} }
private static Map<String,ListenerData> s_listeners = private static Map<Uri,ListenerData> s_listeners =
new HashMap<String,ListenerData>(); new HashMap<Uri,ListenerData>();
private class DownloadFilesTask extends AsyncTask<Void, Void, Void> private class DownloadFilesTask extends AsyncTask<Void, Void, Void>
implements DictUtils.DownProgListener { implements DictUtils.DownProgListener {
@ -240,7 +243,7 @@ public class DwnldDelegate extends ListDelegateBase {
{ {
m_dfts = new ArrayList<DownloadFilesTask>(); m_dfts = new ArrayList<DownloadFilesTask>();
DownloadFilesTask dft = null; DownloadFilesTask dft = null;
String[] urls = null; Uri[] uris = null;
LinearLayout item = null; LinearLayout item = null;
Intent intent = getIntent(); Intent intent = getIntent();
@ -249,16 +252,19 @@ public class DwnldDelegate extends ListDelegateBase {
String appUrl = intent.getStringExtra( APK_EXTRA ); String appUrl = intent.getStringExtra( APK_EXTRA );
boolean isApp = null != appUrl; boolean isApp = null != appUrl;
if ( isApp ) { if ( isApp ) {
urls = new String[] { appUrl }; uris = new Uri[] { Uri.parse( appUrl ) };
} else { } else {
urls = intent.getStringArrayExtra( DICTS_EXTRA ); Parcelable[] parcels = intent.getParcelableArrayExtra( DICTS_EXTRA );
uris = new Uri[parcels.length];
for ( int ii = 0; ii < parcels.length; ++ii ) {
uris[ii] = (Uri)(parcels[ii]);
}
} }
if ( null != urls ) { if ( null != uris ) {
m_views = new ArrayList<LinearLayout>(); m_views = new ArrayList<LinearLayout>();
for ( int ii = 0; ii < urls.length; ++ii ) { for ( int ii = 0; ii < uris.length; ++ii ) {
item = (LinearLayout)inflate( R.layout.import_dict_item ); item = (LinearLayout)inflate( R.layout.import_dict_item );
m_dfts.add( new DownloadFilesTask( Uri.parse( urls[ii] ), item, m_dfts.add( new DownloadFilesTask( uris[ii], item, isApp ));
isApp ) );
m_views.add( item ); m_views.add( item );
} }
} }
@ -280,11 +286,11 @@ public class DwnldDelegate extends ListDelegateBase {
if ( 0 == m_dfts.size() ) { if ( 0 == m_dfts.size() ) {
finish(); finish();
} else { } else {
Assert.assertTrue( m_dfts.size() == urls.length ); Assert.assertTrue( m_dfts.size() == uris.length );
mkListAdapter(); mkListAdapter();
for ( int ii = 0; ii < urls.length; ++ii ) { for ( int ii = 0; ii < uris.length; ++ii ) {
String showName = basename( Uri.parse( urls[ii] ).getPath() ); String showName = basename( uris[ii].getPath() );
showName = DictUtils.removeDictExtn( showName ); showName = DictUtils.removeDictExtn( showName );
String msg = String msg =
getString( R.string.downloading_dict_fmt, showName ); getString( R.string.downloading_dict_fmt, showName );
@ -328,45 +334,35 @@ public class DwnldDelegate extends ListDelegateBase {
return new File(path).getName(); return new File(path).getName();
} }
private static String langFromUrl( String url ) private static String langFromUri( Uri uri )
{ {
String[] parts = TextUtils.split( url, "/" ); List<String> segs = uri.getPathSegments();
String result = parts[parts.length - 2]; String result = segs.get( segs.size() - 2 );
// DbgUtils.logf( "langFromUrl(%s) => %s", url, result );
return result; return result;
} }
private static void rememberListener( String url, String name, private static void rememberListener( Uri uri, String name,
DownloadFinishedListener lstnr ) DownloadFinishedListener lstnr )
{ {
ListenerData ld = new ListenerData( name, lstnr ); ListenerData ld = new ListenerData( uri, name, lstnr );
synchronized( s_listeners ) { synchronized( s_listeners ) {
s_listeners.put( url, ld ); s_listeners.put( uri, ld );
}
}
private static void rememberListener( String url, DownloadFinishedListener lstnr )
{
ListenerData ld = new ListenerData( url, lstnr );
synchronized( s_listeners ) {
s_listeners.put( url, ld );
} }
} }
private static void callListener( Uri uri, boolean success ) private static void callListener( Uri uri, boolean success )
{ {
if ( null != uri ) { if ( null != uri ) {
String url = uri.toString();
ListenerData ld; ListenerData ld;
synchronized( s_listeners ) { synchronized( s_listeners ) {
ld = s_listeners.get( url ); ld = s_listeners.get( uri );
if ( null != ld ) { if ( null != ld ) {
s_listeners.remove( url ); s_listeners.remove( uri );
} }
} }
if ( null != ld ) { if ( null != ld ) {
String name = ld.m_name; String name = ld.m_name;
String lang = langFromUrl( url ); String lang = langFromUri( uri );
if ( null == name ) { if ( null == name ) {
name = uri.toString(); name = uri.toString();
} }
@ -379,39 +375,40 @@ public class DwnldDelegate extends ListDelegateBase {
String name, String name,
DownloadFinishedListener lstnr ) DownloadFinishedListener lstnr )
{ {
String url = Utils.makeDictUrl( context, langName, name ); Uri uri = Utils.makeDictUri( context, langName, name );
// DbgUtils.logf( "downloadDictInBack(lang=%s): url=%s", langName, url ); downloadDictInBack( context, uri, name, lstnr );
downloadDictInBack( context, url, lstnr );
} }
public static void downloadDictInBack( Context context, int lang, public static void downloadDictInBack( Context context, int lang,
String name, String name,
DownloadFinishedListener lstnr ) DownloadFinishedListener lstnr )
{ {
String url = Utils.makeDictUrl( context, lang, name ); Uri uri = Utils.makeDictUri( context, lang, name );
// DbgUtils.logf( "downloadDictInBack(lang=%d): url=%s", lang, url ); downloadDictInBack( context, uri, name, lstnr );
downloadDictInBack( context, url, lstnr );
} }
public static void downloadDictsInBack( Context context, String[] urls, public static void downloadDictsInBack( Context context, Uri[] uris,
DownloadFinishedListener lstnr ) String[] names,
DownloadFinishedListener lstnr )
{ {
if ( null != lstnr ) { if ( null != lstnr ) {
for ( String url : urls ) { for ( int ii = 0; ii < uris.length; ++ii ) {
rememberListener( url, lstnr ); rememberListener( uris[ii], names[ii], lstnr );
} }
} }
Intent intent = new Intent( context, DwnldActivity.class ); Intent intent = new Intent( context, DwnldActivity.class );
intent.putExtra( DICTS_EXTRA, urls ); intent.putExtra( DICTS_EXTRA, uris ); // uris implement Parcelable
context.startActivity( intent ); context.startActivity( intent );
} }
public static void downloadDictInBack( Context context, String url, public static void downloadDictInBack( Context context, Uri uri,
String name,
DownloadFinishedListener lstnr ) DownloadFinishedListener lstnr )
{ {
String[] urls = new String[] { url }; Uri[] uris = new Uri[] { uri };
downloadDictsInBack( context, urls, lstnr ); String[] names = new String[] { name };
downloadDictsInBack( context, uris, names, lstnr );
} }
public static Intent makeAppDownloadIntent( Context context, String url ) public static Intent makeAppDownloadIntent( Context context, String url )

View file

@ -23,6 +23,7 @@ package org.eehouse.android.xw4;
import android.app.Activity; import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.net.Uri;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -45,6 +46,8 @@ import android.widget.LinearLayout;
import android.widget.ListView; import android.widget.ListView;
import android.widget.SpinnerAdapter; import android.widget.SpinnerAdapter;
import java.util.List;
import junit.framework.Assert; import junit.framework.Assert;
import org.eehouse.android.xw4.DlgDelegate.Action; import org.eehouse.android.xw4.DlgDelegate.Action;
@ -462,7 +465,8 @@ public class GameConfigDelegate extends DelegateBase
switch( requestCode ) { switch( requestCode ) {
case REQUEST_DICT: case REQUEST_DICT:
String dictName = data.getStringExtra( DictsDelegate.RESULT_LAST_DICT ); String dictName = data.getStringExtra( DictsDelegate.RESULT_LAST_DICT );
setSpinnerSelection( m_playerDictSpinner, dictName ); configDictSpinner( m_dictSpinner, m_gi.dictLang, dictName );
configDictSpinner( m_playerDictSpinner, m_gi.dictLang, dictName );
break; break;
case REQUEST_LANG: case REQUEST_LANG:
String langName = data.getStringExtra( DictsDelegate.RESULT_LAST_LANG ); String langName = data.getStringExtra( DictsDelegate.RESULT_LAST_LANG );
@ -750,33 +754,36 @@ public class GameConfigDelegate extends DelegateBase
private void configDictSpinner( Spinner dictsSpinner, int lang, private void configDictSpinner( Spinner dictsSpinner, int lang,
String curDict ) String curDict )
{ {
String langName = DictLangCache.getLangName( m_activity, lang ); if ( null != dictsSpinner ) {
dictsSpinner.setPrompt( getString( R.string.dicts_list_prompt_fmt, String langName = DictLangCache.getLangName( m_activity, lang );
langName ) ); dictsSpinner.setPrompt( getString( R.string.dicts_list_prompt_fmt,
langName ) );
OnItemSelectedListener onSel = OnItemSelectedListener onSel =
new OnItemSelectedListener() { new OnItemSelectedListener() {
@Override @Override
public void onItemSelected( AdapterView<?> parentView, public void onItemSelected( AdapterView<?> parentView,
View selectedItemView, View selectedItemView,
int position, long id ) { int position, long id ) {
String chosen = String chosen =
(String)parentView.getItemAtPosition( position ); (String)parentView.getItemAtPosition( position );
if ( chosen.equals( m_browseText ) ) { if ( chosen.equals( m_browseText ) ) {
DictsDelegate.launchForResult( m_activity, REQUEST_DICT, DictsDelegate.downloadForResult( m_activity,
m_gi.dictLang ); REQUEST_DICT,
m_gi.dictLang );
}
} }
}
@Override @Override
public void onNothingSelected(AdapterView<?> parentView) {} public void onNothingSelected(AdapterView<?> parentView) {}
}; };
ArrayAdapter<String> adapter = ArrayAdapter<String> adapter =
DictLangCache.getDictsAdapter( m_activity, lang ); DictLangCache.getDictsAdapter( m_activity, lang );
configSpinnerWDownload( dictsSpinner, adapter, onSel, curDict ); configSpinnerWDownload( dictsSpinner, adapter, onSel, curDict );
}
} }
private void configLangSpinner() private void configLangSpinner()
@ -793,7 +800,7 @@ public class GameConfigDelegate extends DelegateBase
String chosen = String chosen =
(String)parentView.getItemAtPosition( position ); (String)parentView.getItemAtPosition( position );
if ( chosen.equals( m_browseText ) ) { if ( chosen.equals( m_browseText ) ) {
DictsDelegate.launchForResult( m_activity, REQUEST_LANG ); DictsDelegate.downloadForResult( m_activity, REQUEST_LANG );
} else { } else {
selLangChanged( chosen ); selLangChanged( chosen );
} }
@ -813,9 +820,7 @@ public class GameConfigDelegate extends DelegateBase
{ {
m_gi.setLang( DictLangCache.getLangLangCode( m_activity, chosen ) ); m_gi.setLang( DictLangCache.getLangLangCode( m_activity, chosen ) );
loadPlayersList(); loadPlayersList();
if ( null != m_dictSpinner ) { configDictSpinner( m_dictSpinner, m_gi.dictLang, m_gi.dictName );
configDictSpinner( m_dictSpinner, m_gi.dictLang, m_gi.dictName );
}
} }
private void configSpinnerWDownload( Spinner spinner, private void configSpinnerWDownload( Spinner spinner,

View file

@ -563,8 +563,8 @@ public class GamesListDelegate extends ListDelegateBase
public void onClick( DialogInterface dlg, int item ) { public void onClick( DialogInterface dlg, int item ) {
// no name, so user must pick // no name, so user must pick
if ( null == m_missingDictName ) { if ( null == m_missingDictName ) {
DictsDelegate.launchForResult( m_activity, REQUEST_LANG, DictsDelegate.downloadForResult( m_activity, REQUEST_LANG,
m_missingDictLang ); m_missingDictLang );
} else { } else {
DwnldDelegate DwnldDelegate
.downloadDictInBack( m_activity, .downloadDictInBack( m_activity,
@ -584,6 +584,7 @@ public class GamesListDelegate extends ListDelegateBase
message = getString( R.string.invite_dict_missing_body_noname_fmt, message = getString( R.string.invite_dict_missing_body_noname_fmt,
null, m_missingDictName, langName ); null, m_missingDictName, langName );
} else { } else {
// WARN_NODICT_SUBST
message = getString( R.string.no_dict_subst_fmt, gameName, message = getString( R.string.no_dict_subst_fmt, gameName,
m_missingDictName, langName ); m_missingDictName, langName );
} }

View file

@ -53,6 +53,7 @@ public class UpdateCheckReceiver extends BroadcastReceiver {
public static final String NEW_DICT_URL = "NEW_DICT_URL"; public static final String NEW_DICT_URL = "NEW_DICT_URL";
public static final String NEW_DICT_LOC = "NEW_DICT_LOC"; public static final String NEW_DICT_LOC = "NEW_DICT_LOC";
public static final String NEW_DICT_NAME = "NEW_DICT_NAME";
public static final String NEW_XLATION_CBK = "NEW_XLATION_CBK"; public static final String NEW_XLATION_CBK = "NEW_XLATION_CBK";
// weekly // weekly
@ -384,6 +385,7 @@ public class UpdateCheckReceiver extends BroadcastReceiver {
Intent intent = Intent intent =
new Intent( m_context, DictsActivity.class ); new Intent( m_context, DictsActivity.class );
intent.putExtra( NEW_DICT_URL, url ); intent.putExtra( NEW_DICT_URL, url );
intent.putExtra( NEW_DICT_NAME, dal.name );
intent.putExtra( NEW_DICT_LOC, dal.loc.ordinal() ); intent.putExtra( NEW_DICT_LOC, dal.loc.ordinal() );
String body = String body =
LocUtils.getString( m_context, LocUtils.getString( m_context,

View file

@ -389,35 +389,27 @@ public class Utils {
return result; return result;
} }
public static String dictFromURL( Context context, String url ) public static Uri makeDictUri( Context context, String langName, String name )
{ {
String result = null; String dictUrl = CommonPrefs.getDefaultDictURL( context );
int indx = url.lastIndexOf( "/" ); Uri.Builder builder = Uri.parse( dictUrl ).buildUpon();
if ( 0 <= indx ) {
result = url.substring( indx + 1 );
}
return result;
}
public static String makeDictUrl( Context context, String langName, String name )
{
String dict_url = CommonPrefs.getDefaultDictURL( context );
if ( null != langName ) { if ( null != langName ) {
dict_url += "/" + langName; builder.appendPath( langName );
} }
if ( null != name ) { if ( null != name ) {
dict_url += "/" + name + XWConstants.DICT_EXTN; Assert.assertNotNull( langName );
builder.appendPath( DictUtils.addDictExtn( name ) );
} }
return dict_url; return builder.build();
} }
public static String makeDictUrl( Context context, int lang, String name ) public static Uri makeDictUri( Context context, int lang, String name )
{ {
String langName = null; String langName = null;
if ( 0 < lang ) { if ( 0 < lang ) {
langName = DictLangCache.getLangName( context, lang ); langName = DictLangCache.getLangName( context, lang );
} }
return makeDictUrl( context, langName, name ); return makeDictUri( context, langName, name );
} }
public static int getAppVersion( Context context ) public static int getAppVersion( Context context )