From 74eb6890b938aa6f3300e601ed534971e8ff9328 Mon Sep 17 00:00:00 2001 From: Eric House Date: Wed, 8 Oct 2014 19:12:00 -0700 Subject: [PATCH] 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. --- .../org/eehouse/android/xw4/DictUtils.java | 2 +- .../eehouse/android/xw4/DictsDelegate.java | 63 ++++++++----- .../eehouse/android/xw4/DwnldDelegate.java | 91 +++++++++---------- .../android/xw4/GameConfigDelegate.java | 57 ++++++------ .../android/xw4/GamesListDelegate.java | 5 +- .../android/xw4/UpdateCheckReceiver.java | 2 + .../src/org/eehouse/android/xw4/Utils.java | 26 ++---- 7 files changed, 128 insertions(+), 118 deletions(-) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictUtils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictUtils.java index 1b142bbdc..7bced6666 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictUtils.java @@ -553,7 +553,7 @@ public class DictUtils { return str; } - private static String addDictExtn( String str ) + public static String addDictExtn( String str ) { if ( ! str.endsWith( XWConstants.DICT_EXTN ) ) { str += XWConstants.DICT_EXTN; diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictsDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictsDelegate.java index da384f1e2..a660c685d 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictsDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictsDelegate.java @@ -30,6 +30,7 @@ import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; +import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.preference.PreferenceManager; @@ -99,8 +100,7 @@ public class DictsDelegate extends ListDelegateBase private String m_origTitle; private boolean m_showRemote = false; private String m_filterLang; - private Map m_needUpdates; - private HashMap m_curDownloads; + private Map m_needUpdates; private String m_onServerStr; private String m_lastLang; private String m_lastDict; @@ -578,22 +578,24 @@ public class DictsDelegate extends ListDelegateBase clearSelections(); break; case R.id.dicts_download: - String[] urls = new String[countNeedDownload()]; + Uri[] uris = new Uri[countNeedDownload()]; + String[] names = new String[uris.length]; int count = 0; - m_curDownloads = new HashMap(); for ( Iterator iter = m_selDicts.values().iterator(); iter.hasNext(); ) { XWListItem litm = iter.next(); Object cached = litm.getCached(); if ( cached instanceof DictInfo ) { DictInfo info = (DictInfo)cached; - String url = Utils.makeDictUrl( m_activity, info.m_lang, - litm.getText() ); - urls[count++] = url; - m_curDownloads.put( url, litm ); + String name = litm.getText(); + Uri uri = Utils.makeDictUri( m_activity, info.m_lang, + name ); + uris[count] = uri; + names[count] = name; + ++count; } } - DwnldDelegate.downloadDictsInBack( m_activity, urls, this ); + DwnldDelegate.downloadDictsInBack( m_activity, uris, names, this ); break; default: handled = false; @@ -635,9 +637,12 @@ public class DictsDelegate extends ListDelegateBase { int loci = intent.getIntExtra( UpdateCheckReceiver.NEW_DICT_LOC, 0 ); if ( 0 < loci ) { + String name = + intent.getStringExtra( UpdateCheckReceiver.NEW_DICT_NAME ); String 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(); } } @@ -801,9 +806,17 @@ public class DictsDelegate extends ListDelegateBase mkListAdapter(); break; case UPDATE_DICTS_ACTION: - String[] urls = m_needUpdates.values(). - toArray( new String[m_needUpdates.size()] ); - DwnldDelegate.downloadDictsInBack( m_activity, urls, this ); + Uri[] uris = new Uri[m_needUpdates.size()]; + String[] names = new String[uris.length]; + int count = 0; + for ( Iterator 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; default: Assert.fail(); @@ -944,12 +957,12 @@ public class DictsDelegate extends ListDelegateBase { Assert.fail(); return null; - // String dict_url = Utils.makeDictUrl( context, lang, dict ); + // String dict_url = Utils.makeDictUri( context, lang, dict ); // return mkDownloadIntent( context, dict_url ); } - public static void launchForResult( Activity activity, int requestCode, - int lang, String name ) + public static void downloadForResult( Activity activity, int requestCode, + int lang, String name ) { Intent intent = new Intent( activity, DictsActivity.class ); intent.putExtra( DICT_SHOWREMOTE, true ); @@ -964,15 +977,15 @@ public class DictsDelegate extends ListDelegateBase activity.startActivityForResult( intent, requestCode ); } - public static void launchForResult( Activity activity, int requestCode, - int lang ) + public static void downloadForResult( Activity activity, int requestCode, + 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; JSONArray langs = null; - m_needUpdates = new HashMap(); + m_needUpdates = new HashMap(); if ( null != jsonData ) { Set closedLangs = new HashSet(); final Set curLangs = @@ -1211,10 +1224,10 @@ public class DictsDelegate extends ListDelegateBase } } if ( !matches ) { - String url = - Utils.makeDictUrl( m_activity, + Uri uri = + Utils.makeDictUri( m_activity, langName, name ); - m_needUpdates.put( name, url ); + m_needUpdates.put( name, uri ); } } } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DwnldDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DwnldDelegate.java index c41c3b223..1a66b0add 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DwnldDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DwnldDelegate.java @@ -27,6 +27,7 @@ import android.content.Intent; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; +import android.os.Parcelable; import android.text.TextUtils; import android.view.View; import android.view.ViewGroup; @@ -46,8 +47,8 @@ import java.security.MessageDigest; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; +import java.util.List; import java.util.Map; - import junit.framework.Assert; public class DwnldDelegate extends ListDelegateBase { @@ -72,16 +73,18 @@ public class DwnldDelegate extends ListDelegateBase { // Track callbacks for downloads. 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_lstnr = lstnr; } + public Uri m_uri; public String m_name; public DownloadFinishedListener m_lstnr; } - private static Map s_listeners = - new HashMap(); + private static Map s_listeners = + new HashMap(); private class DownloadFilesTask extends AsyncTask implements DictUtils.DownProgListener { @@ -240,7 +243,7 @@ public class DwnldDelegate extends ListDelegateBase { { m_dfts = new ArrayList(); DownloadFilesTask dft = null; - String[] urls = null; + Uri[] uris = null; LinearLayout item = null; Intent intent = getIntent(); @@ -249,16 +252,19 @@ public class DwnldDelegate extends ListDelegateBase { String appUrl = intent.getStringExtra( APK_EXTRA ); boolean isApp = null != appUrl; if ( isApp ) { - urls = new String[] { appUrl }; + uris = new Uri[] { Uri.parse( appUrl ) }; } 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(); - for ( int ii = 0; ii < urls.length; ++ii ) { + for ( int ii = 0; ii < uris.length; ++ii ) { item = (LinearLayout)inflate( R.layout.import_dict_item ); - m_dfts.add( new DownloadFilesTask( Uri.parse( urls[ii] ), item, - isApp ) ); + m_dfts.add( new DownloadFilesTask( uris[ii], item, isApp )); m_views.add( item ); } } @@ -280,11 +286,11 @@ public class DwnldDelegate extends ListDelegateBase { if ( 0 == m_dfts.size() ) { finish(); } else { - Assert.assertTrue( m_dfts.size() == urls.length ); + Assert.assertTrue( m_dfts.size() == uris.length ); mkListAdapter(); - for ( int ii = 0; ii < urls.length; ++ii ) { - String showName = basename( Uri.parse( urls[ii] ).getPath() ); + for ( int ii = 0; ii < uris.length; ++ii ) { + String showName = basename( uris[ii].getPath() ); showName = DictUtils.removeDictExtn( showName ); String msg = getString( R.string.downloading_dict_fmt, showName ); @@ -328,45 +334,35 @@ public class DwnldDelegate extends ListDelegateBase { return new File(path).getName(); } - private static String langFromUrl( String url ) + private static String langFromUri( Uri uri ) { - String[] parts = TextUtils.split( url, "/" ); - String result = parts[parts.length - 2]; - // DbgUtils.logf( "langFromUrl(%s) => %s", url, result ); + List segs = uri.getPathSegments(); + String result = segs.get( segs.size() - 2 ); return result; } - private static void rememberListener( String url, String name, + private static void rememberListener( Uri uri, String name, DownloadFinishedListener lstnr ) { - ListenerData ld = new ListenerData( name, lstnr ); + ListenerData ld = new ListenerData( uri, name, lstnr ); synchronized( s_listeners ) { - s_listeners.put( url, ld ); - } - } - - private static void rememberListener( String url, DownloadFinishedListener lstnr ) - { - ListenerData ld = new ListenerData( url, lstnr ); - synchronized( s_listeners ) { - s_listeners.put( url, ld ); + s_listeners.put( uri, ld ); } } private static void callListener( Uri uri, boolean success ) { if ( null != uri ) { - String url = uri.toString(); ListenerData ld; synchronized( s_listeners ) { - ld = s_listeners.get( url ); + ld = s_listeners.get( uri ); if ( null != ld ) { - s_listeners.remove( url ); + s_listeners.remove( uri ); } } if ( null != ld ) { String name = ld.m_name; - String lang = langFromUrl( url ); + String lang = langFromUri( uri ); if ( null == name ) { name = uri.toString(); } @@ -379,39 +375,40 @@ public class DwnldDelegate extends ListDelegateBase { String name, DownloadFinishedListener lstnr ) { - String url = Utils.makeDictUrl( context, langName, name ); - // DbgUtils.logf( "downloadDictInBack(lang=%s): url=%s", langName, url ); - downloadDictInBack( context, url, lstnr ); + Uri uri = Utils.makeDictUri( context, langName, name ); + downloadDictInBack( context, uri, name, lstnr ); } public static void downloadDictInBack( Context context, int lang, String name, DownloadFinishedListener lstnr ) { - String url = Utils.makeDictUrl( context, lang, name ); - // DbgUtils.logf( "downloadDictInBack(lang=%d): url=%s", lang, url ); - downloadDictInBack( context, url, lstnr ); + Uri uri = Utils.makeDictUri( context, lang, name ); + downloadDictInBack( context, uri, name, lstnr ); } - public static void downloadDictsInBack( Context context, String[] urls, - DownloadFinishedListener lstnr ) + public static void downloadDictsInBack( Context context, Uri[] uris, + String[] names, + DownloadFinishedListener lstnr ) { if ( null != lstnr ) { - for ( String url : urls ) { - rememberListener( url, lstnr ); + for ( int ii = 0; ii < uris.length; ++ii ) { + rememberListener( uris[ii], names[ii], lstnr ); } } Intent intent = new Intent( context, DwnldActivity.class ); - intent.putExtra( DICTS_EXTRA, urls ); + intent.putExtra( DICTS_EXTRA, uris ); // uris implement Parcelable context.startActivity( intent ); } - public static void downloadDictInBack( Context context, String url, + public static void downloadDictInBack( Context context, Uri uri, + String name, DownloadFinishedListener lstnr ) { - String[] urls = new String[] { url }; - downloadDictsInBack( context, urls, lstnr ); + Uri[] uris = new Uri[] { uri }; + String[] names = new String[] { name }; + downloadDictsInBack( context, uris, names, lstnr ); } public static Intent makeAppDownloadIntent( Context context, String url ) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameConfigDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameConfigDelegate.java index 1a25432b5..1876a17ff 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameConfigDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameConfigDelegate.java @@ -23,6 +23,7 @@ package org.eehouse.android.xw4; import android.app.Activity; import android.content.Intent; import android.os.Bundle; +import android.net.Uri; import java.io.File; import java.util.ArrayList; import java.util.Arrays; @@ -45,6 +46,8 @@ import android.widget.LinearLayout; import android.widget.ListView; import android.widget.SpinnerAdapter; +import java.util.List; + import junit.framework.Assert; import org.eehouse.android.xw4.DlgDelegate.Action; @@ -462,7 +465,8 @@ public class GameConfigDelegate extends DelegateBase switch( requestCode ) { case REQUEST_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; case REQUEST_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, String curDict ) { - String langName = DictLangCache.getLangName( m_activity, lang ); - dictsSpinner.setPrompt( getString( R.string.dicts_list_prompt_fmt, - langName ) ); + if ( null != dictsSpinner ) { + String langName = DictLangCache.getLangName( m_activity, lang ); + dictsSpinner.setPrompt( getString( R.string.dicts_list_prompt_fmt, + langName ) ); - OnItemSelectedListener onSel = - new OnItemSelectedListener() { - @Override - public void onItemSelected( AdapterView parentView, - View selectedItemView, - int position, long id ) { - String chosen = - (String)parentView.getItemAtPosition( position ); + OnItemSelectedListener onSel = + new OnItemSelectedListener() { + @Override + public void onItemSelected( AdapterView parentView, + View selectedItemView, + int position, long id ) { + String chosen = + (String)parentView.getItemAtPosition( position ); - if ( chosen.equals( m_browseText ) ) { - DictsDelegate.launchForResult( m_activity, REQUEST_DICT, - m_gi.dictLang ); + if ( chosen.equals( m_browseText ) ) { + DictsDelegate.downloadForResult( m_activity, + REQUEST_DICT, + m_gi.dictLang ); + } } - } - @Override - public void onNothingSelected(AdapterView parentView) {} - }; + @Override + public void onNothingSelected(AdapterView parentView) {} + }; - ArrayAdapter adapter = - DictLangCache.getDictsAdapter( m_activity, lang ); + ArrayAdapter adapter = + DictLangCache.getDictsAdapter( m_activity, lang ); - configSpinnerWDownload( dictsSpinner, adapter, onSel, curDict ); + configSpinnerWDownload( dictsSpinner, adapter, onSel, curDict ); + } } private void configLangSpinner() @@ -793,7 +800,7 @@ public class GameConfigDelegate extends DelegateBase String chosen = (String)parentView.getItemAtPosition( position ); if ( chosen.equals( m_browseText ) ) { - DictsDelegate.launchForResult( m_activity, REQUEST_LANG ); + DictsDelegate.downloadForResult( m_activity, REQUEST_LANG ); } else { selLangChanged( chosen ); } @@ -813,9 +820,7 @@ public class GameConfigDelegate extends DelegateBase { m_gi.setLang( DictLangCache.getLangLangCode( m_activity, chosen ) ); 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, diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesListDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesListDelegate.java index 08a9f4c4e..8e304a39c 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesListDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesListDelegate.java @@ -563,8 +563,8 @@ public class GamesListDelegate extends ListDelegateBase public void onClick( DialogInterface dlg, int item ) { // no name, so user must pick if ( null == m_missingDictName ) { - DictsDelegate.launchForResult( m_activity, REQUEST_LANG, - m_missingDictLang ); + DictsDelegate.downloadForResult( m_activity, REQUEST_LANG, + m_missingDictLang ); } else { DwnldDelegate .downloadDictInBack( m_activity, @@ -584,6 +584,7 @@ public class GamesListDelegate extends ListDelegateBase message = getString( R.string.invite_dict_missing_body_noname_fmt, null, m_missingDictName, langName ); } else { + // WARN_NODICT_SUBST message = getString( R.string.no_dict_subst_fmt, gameName, m_missingDictName, langName ); } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/UpdateCheckReceiver.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/UpdateCheckReceiver.java index 5ef592c8e..c3c4e2cfd 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/UpdateCheckReceiver.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/UpdateCheckReceiver.java @@ -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_LOC = "NEW_DICT_LOC"; + public static final String NEW_DICT_NAME = "NEW_DICT_NAME"; public static final String NEW_XLATION_CBK = "NEW_XLATION_CBK"; // weekly @@ -384,6 +385,7 @@ public class UpdateCheckReceiver extends BroadcastReceiver { Intent intent = new Intent( m_context, DictsActivity.class ); intent.putExtra( NEW_DICT_URL, url ); + intent.putExtra( NEW_DICT_NAME, dal.name ); intent.putExtra( NEW_DICT_LOC, dal.loc.ordinal() ); String body = LocUtils.getString( m_context, diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/Utils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/Utils.java index 39fa7f35d..b3e5a1561 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/Utils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/Utils.java @@ -389,35 +389,27 @@ public class Utils { return result; } - public static String dictFromURL( Context context, String url ) + public static Uri makeDictUri( Context context, String langName, String name ) { - String result = null; - int indx = url.lastIndexOf( "/" ); - 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 ); + String dictUrl = CommonPrefs.getDefaultDictURL( context ); + Uri.Builder builder = Uri.parse( dictUrl ).buildUpon(); if ( null != langName ) { - dict_url += "/" + langName; + builder.appendPath( langName ); } 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; if ( 0 < lang ) { langName = DictLangCache.getLangName( context, lang ); } - return makeDictUrl( context, langName, name ); + return makeDictUri( context, langName, name ); } public static int getAppVersion( Context context )