diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardDelegate.java index 35b3c6fae..800d96a95 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardDelegate.java @@ -547,8 +547,10 @@ public class BoardDelegate extends DelegateBase waitCloseGame( true ); } + @Override protected void onResume() { + super.onResume(); m_handler = new Handler(); m_blockingDlgID = DlgID.NONE; diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DelegateBase.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DelegateBase.java index d4a51be38..120b2c4b5 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DelegateBase.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DelegateBase.java @@ -61,11 +61,15 @@ public class DelegateBase implements DlgDelegate.DlgClickNotify, public boolean onPrepareOptionsMenu( Menu menu ) { return false; } public boolean onOptionsItemSelected( MenuItem item ) { return false; } protected void onStart() {} - protected void onResume() {} protected void onPause() {} protected void onStop() {} protected void onDestroy() {} + protected void onResume() + { + LocUtils.setLatestContext( m_activity ); + } + // public boolean onOptionsItemSelected( MenuItem item ) // { // } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictBrowseDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictBrowseDelegate.java index 263a3e70a..8c851e391 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictBrowseDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictBrowseDelegate.java @@ -238,8 +238,10 @@ public class DictBrowseDelegate extends ListDelegateBase } } + @Override protected void onResume() { + super.onResume(); if ( null == m_browseState ) { m_browseState = DBUtils.dictsGetOffset( m_activity, m_name, m_loc ); } 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 8e692fae8..32cf90b0e 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictsDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictsDelegate.java @@ -408,8 +408,11 @@ public class DictsDelegate extends DelegateBase m_origTitle = getTitle(); } // onCreate + @Override protected void onResume() { + super.onResume(); + MountEventReceiver.register( this ); mkListAdapter(); 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 9016059df..b98a46b71 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameConfigDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameConfigDelegate.java @@ -433,8 +433,10 @@ public class GameConfigDelegate extends DelegateBase loadGame(); } + @Override protected void onResume() { + super.onResume(); loadGame(); } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/NewGameDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/NewGameDelegate.java index 87f37a6de..0dad3aafa 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/NewGameDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/NewGameDelegate.java @@ -162,7 +162,9 @@ public class NewGameDelegate extends DelegateBase { } } + @Override protected void onResume() { + super.onResume(); checkEnableBT( false ); } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/PrefsDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/PrefsDelegate.java index 2be45e4c1..352389ed2 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/PrefsDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/PrefsDelegate.java @@ -156,10 +156,12 @@ public class PrefsDelegate extends DelegateBase } ); } + @Override protected void onResume() { + super.onResume(); getSharedPreferences().registerOnSharedPreferenceChangeListener(this); - } + } protected void onPause() { diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocSearcher.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocSearcher.java index e66640aca..70d32c15c 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocSearcher.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocSearcher.java @@ -96,6 +96,7 @@ public class LocSearcher { FilterFunc proc = s_falseProc; switch ( showBy ) { case LOC_FILTERS_SCREEN: + proc = s_screenProc; break; case LOC_FILTERS_MENU: proc = s_menuProc; @@ -157,9 +158,15 @@ public class LocSearcher { } }; + private static FilterFunc s_screenProc = new FilterFunc() { + public boolean passes( Context context, Pair pair ) { + return LocUtils.inLatestScreen( pair.getKey() ); + } + }; + private static FilterFunc s_menuProc = new FilterFunc() { public boolean passes( Context context, Pair pair ) { - return LocUtils.inLatestMenu( context, pair.getKey() ); + return LocUtils.inLatestMenu( pair.getKey() ); } }; diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocUtils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocUtils.java index 65fa6051f..ae79844e9 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/LocUtils.java @@ -81,27 +81,11 @@ public class LocUtils { private static WeakReference s_latestMenuRef; private static HashMap, HashSet > s_menuSets = new HashMap, HashSet >(); - - public interface LocIface { - void setText( CharSequence text ); - } - - // public static void loadStrings( Context context, AttributeSet as, LocIface view ) - // { - // // There should be a way to look up the index of "strid" but I don't - // // have it yet. This got things working. - // int count = as.getAttributeCount(); - // for ( int ii = 0; ii < count; ++ii ) { - // if ( "strid".equals( as.getAttributeName(ii) ) ) { - // String value = as.getAttributeValue(ii); - // Assert.assertTrue( '@' == value.charAt(0) ); - // int id = Integer.parseInt( value.substring(1) ); - // view.setText( getString( context, id ) ); - // break; - // } - // } - // } - + private static String s_latestContextName; + private static String s_newContextName; + private static HashMap > s_contextSets + = new HashMap >(); + public static void localeChanged( Context context, String newLocale ) { saveLocalData( context ); @@ -111,6 +95,14 @@ public class LocUtils { s_enabled = null; } + public static void setLatestContext( Context context ) + { + String newName = context.getClass().getName(); + s_latestContextName = s_newContextName; + s_newContextName = newName; + DbgUtils.logf( "setLatestContext(%s): now %s", newName, s_latestContextName ); + } + public static View inflate( Context context, int resID ) { LayoutInflater factory = LayoutInflater.from( context ); @@ -140,8 +132,9 @@ public class LocUtils { public static void xlateView( Context context, View view ) { - // DbgUtils.logf( "xlateView() top level" ); - xlateView( context, view, 0 ); + // DbgUtils.logf( "xlateView(%s, %s)", context.getClass().getName(), + // view.getClass().getName() ); + xlateView( context, context.getClass().getName(), view, 0 ); } public static void xlateMenu( Activity activity, Menu menu ) @@ -160,9 +153,13 @@ public class LocUtils { return result; } - public static String xlateString( Context context, String str ) + private static String xlateString( Context context, String str, + boolean associate ) { if ( LocIDs.getS_MAP( context ).containsKey( str ) ) { + if ( associate ) { + associateContextString( context, str ); + } String xlation = getXlation( context, str, true ); if ( null != xlation ) { str = xlation; @@ -171,6 +168,11 @@ public class LocUtils { return str; } + public static String xlateString( Context context, String str ) + { + return xlateString( context, str, true ); + } + public static CharSequence[] xlateStrings( Context context, CharSequence[] strs ) { CharSequence[] result = new CharSequence[strs.length]; @@ -206,6 +208,7 @@ public class LocUtils { String result = null; String key = keyForID( context, id ); if ( null != key ) { + associateContextString( context, key ); result = getXlation( context, key, canUseDB ); } @@ -390,7 +393,7 @@ public class LocUtils { if ( LocIDs.getS_MAP( activity ).containsKey(title) ) { associateMenuString( rootRef, title ); - title = xlateString( activity, title ); + title = xlateString( activity, title, false ); item.setTitle( title ); } } @@ -478,25 +481,28 @@ public class LocUtils { return s_idsToKeys.get( id ); } - private static void xlateView( Context context, View view, int depth ) + private static void xlateView( Context context, String contextName, + View view, int depth ) { // DbgUtils.logf( "xlateView(depth=%d, view=%s, canRecurse=%b)", depth, // view.getClass().getName(), view instanceof ViewGroup ); if ( view instanceof Button ) { Button button = (Button)view; - String str = xlateString( context, button.getText().toString() ); - button.setText( str ); + String str = xlateAndStore( context, button.getText() ); + if ( null != str ) { + button.setText( str ); + } } else if ( view instanceof TextView ) { TextView tv = (TextView)view; - tv.setText( xlateString( context, tv.getText().toString() ) ); + String str = xlateAndStore( context, tv.getText() ); + if ( null != str ) { + tv.setText( str ); + } } else if ( view instanceof Spinner ) { Spinner sp = (Spinner)view; - CharSequence prompt = sp.getPrompt(); - if ( null != prompt ) { - String xlation = xlateString( context, prompt.toString() ); - if ( null != xlation ) { - sp.setPrompt( xlation ); - } + String str = xlateAndStore( context, sp.getPrompt() ); + if ( null != str ) { + sp.setPrompt( str ); } SpinnerAdapter adapter = sp.getAdapter(); if ( null != adapter ) { @@ -510,7 +516,7 @@ public class LocUtils { int count = asGroup.getChildCount(); for ( int ii = 0; ii < count; ++ii ) { View child = asGroup.getChildAt( ii ); - xlateView( context, child, depth + 1 ); + xlateView( context, contextName, child, depth + 1 ); } } } @@ -565,7 +571,7 @@ public class LocUtils { } } - public static boolean inLatestMenu( Context context, String key ) + public static boolean inLatestMenu( String key ) { boolean result = false; if ( null != s_latestMenuRef ) { @@ -577,6 +583,36 @@ public class LocUtils { return result; } + public static boolean inLatestScreen( String key ) + { + boolean result = false; + Assert.assertNotNull( s_latestContextName ); + HashSet keys = s_contextSets.get( s_latestContextName ); + Assert.assertNotNull( keys ); // failing + result = keys.contains( key ); + // DbgUtils.logf( "inLatestScreen(%s [in %s])=>%b", key, s_latestContextName, result ); + return result; + } + + private static String xlateAndStore( Context context, CharSequence cs ) + { + String result = null; + if ( null == cs ) { + // DbgUtils.logf( "xlateAndStore: cs null" ); + } else if ( 0 == cs.length() ) { + Assert.assertTrue( 0 == cs.toString().length() ); + // DbgUtils.logf( "xlateAndStore: cs 0 len" ); + } else { + String key = cs.toString(); + // DbgUtils.logf( "xlateAndStore: key=%s", key ); + result = xlateString( context, key ); + if ( null != result ) { + associateContextString( context, key ); + } + } + return result; + } + // This is for testing, but the ability to pull the formatters will be // critical for validating local transations of strings containing // formatters. @@ -621,11 +657,27 @@ public class LocUtils { if ( null == keys ) { keys = new HashSet(); s_menuSets.put( ref, keys ); - // Watch for leaking -- currently nothing ever removed from s_menuSets } keys.add( key ); } + private static void associateContextString( Context context, final String key ) + { + associateContextString( context.getClass().getName(), key ); + } + + private static void associateContextString( String contextName, final String key ) + { + HashSet keys = s_contextSets.get( contextName ); + if ( null == keys ) { + keys = new HashSet(); + s_contextSets.put( contextName, keys ); + // DbgUtils.logf( "adding keys hash to %s", contextName ); + } + keys.add( key ); + // DbgUtils.logf( "associated with context %s string '%s'", contextName, key ); + } + private static Pattern s_patUnicode = Pattern.compile("(\\\\[Uu][0-9a-fA-F]{4})"); private static Pattern s_patCr = Pattern.compile("\\\\n"); diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/XlatingSpinnerAdapter.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/XlatingSpinnerAdapter.java index 6e2638aa8..1a1eda4ac 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/XlatingSpinnerAdapter.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/loc/XlatingSpinnerAdapter.java @@ -41,7 +41,6 @@ public class XlatingSpinnerAdapter implements SpinnerAdapter { public View getDropDownView( int position, View convertView, ViewGroup parent ) { View view = m_adapter.getDropDownView( position, convertView, parent ); - // DbgUtils.logf( "getDropDownView()=>%s", view.getClass().getName() ); LocUtils.xlateView( m_context, view ); return view; } @@ -49,7 +48,6 @@ public class XlatingSpinnerAdapter implements SpinnerAdapter { public View getView( int position, View convertView, ViewGroup parent ) { View view = m_adapter.getView( position, convertView, parent ); - // DbgUtils.logf( "getView()=>%s", view.getClass().getName() ); LocUtils.xlateView( m_context, view ); return view; }