move static stuff from activity to delegate

This commit is contained in:
Eric House 2016-03-12 15:46:59 -08:00
parent 08da6e5472
commit a111cf8953
3 changed files with 66 additions and 65 deletions

View file

@ -1020,7 +1020,7 @@ public class BoardDelegate extends DelegateBase
String curDict = m_gi.dictName( m_view.getCurPlayer() );
View button = m_toolbar.getViewFor( Toolbar.BUTTON_BROWSE_DICT );
if ( Action.BUTTON_BROWSEALL_ACTION == action &&
DictsActivity.handleDictsPopup( m_activity, button,
DictsDelegate.handleDictsPopup( m_activity, button,
curDict, m_gi.dictLang ) ){
break;
}

View file

@ -26,7 +26,6 @@ import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import java.util.HashMap;
@ -35,11 +34,6 @@ import org.eehouse.android.xw4.loc.LocUtils;
public class DictsActivity extends XWActivity {
private static interface SafePopup {
public void doPopup( Context context, View button,
String curDict, int lang );
}
private static SafePopup s_safePopup = null;
// I can't provide a subclass of MenuItem to hold DictAndLoc, so
// settle for a hash on the side.
private DictsDelegate m_dlgt;
@ -51,64 +45,6 @@ public class DictsActivity extends XWActivity {
super.onCreate( savedInstanceState, m_dlgt );
} // onCreate
private static class SafePopupImpl implements SafePopup {
public void doPopup( final Context context, View button,
String curDict, int lang ) {
final HashMap<MenuItem, DictAndLoc> itemData
= new HashMap<MenuItem, DictAndLoc>();
MenuItem.OnMenuItemClickListener listener =
new MenuItem.OnMenuItemClickListener() {
public boolean onMenuItemClick( MenuItem item )
{
DictAndLoc dal = itemData.get( item );
DictBrowseDelegate.launch( context, dal.name,
dal.loc );
return true;
}
};
PopupMenu popup = new PopupMenu( context, button );
Menu menu = popup.getMenu();
// Add at top but save until have dal info
MenuItem curItem =
menu.add( LocUtils.getString( context,
R.string.cur_menu_marker_fmt,
curDict ) );
DictAndLoc[] dals = DictLangCache.getDALsHaveLang( context, lang );
for ( DictAndLoc dal : dals ) {
MenuItem item = dal.name.equals(curDict)
? curItem : menu.add( dal.name );
item.setOnMenuItemClickListener( listener );
itemData.put( item, dal );
}
popup.show();
}
}
public static boolean handleDictsPopup( Context context, View button,
String curDict, int lang )
{
int nDicts = DictLangCache.getLangCount( context, lang );
if ( null == s_safePopup && 1 < nDicts ) {
int sdkVersion = Integer.valueOf( android.os.Build.VERSION.SDK );
if ( 11 <= sdkVersion ) {
s_safePopup = new SafePopupImpl();
}
}
boolean canHandle = null != s_safePopup && 1 < nDicts;
if ( canHandle ) {
s_safePopup.doPopup( context, button, curDict, lang );
}
return canHandle;
}
public static void start( Context context )
{
Intent intent = new Intent( context, DictsActivity.class );

View file

@ -44,6 +44,7 @@ import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.TextView;
import java.net.HttpURLConnection;
@ -107,6 +108,52 @@ public class DictsDelegate extends ListDelegateBase
private String m_lastDict;
private String m_noteNone;
private static interface SafePopup {
public void doPopup( Context context, View button,
String curDict, int lang );
}
private static SafePopup s_safePopup = null;
private static class SafePopupImpl implements SafePopup {
public void doPopup( final Context context, View button,
String curDict, int lang ) {
final HashMap<MenuItem, DictAndLoc> itemData
= new HashMap<MenuItem, DictAndLoc>();
MenuItem.OnMenuItemClickListener listener =
new MenuItem.OnMenuItemClickListener() {
public boolean onMenuItemClick( MenuItem item )
{
DictAndLoc dal = itemData.get( item );
DictBrowseDelegate.launch( context, dal.name,
dal.loc );
return true;
}
};
PopupMenu popup = new PopupMenu( context, button );
Menu menu = popup.getMenu();
// Add at top but save until have dal info
MenuItem curItem =
menu.add( LocUtils.getString( context,
R.string.cur_menu_marker_fmt,
curDict ) );
DictAndLoc[] dals = DictLangCache.getDALsHaveLang( context, lang );
for ( DictAndLoc dal : dals ) {
MenuItem item = dal.name.equals(curDict)
? curItem : menu.add( dal.name );
item.setOnMenuItemClickListener( listener );
itemData.put( item, dal );
}
popup.show();
}
}
private static class DictInfo implements Comparable {
public String m_name;
public String m_lang;
@ -1017,6 +1064,24 @@ public class DictsDelegate extends ListDelegateBase
new GetDefaultDictTask( context, lc, lstnr ).execute();
}
public static boolean handleDictsPopup( Context context, View button,
String curDict, int lang )
{
int nDicts = DictLangCache.getLangCount( context, lang );
if ( null == s_safePopup && 1 < nDicts ) {
int sdkVersion = Integer.valueOf( android.os.Build.VERSION.SDK );
if ( 11 <= sdkVersion ) {
s_safePopup = new SafePopupImpl();
}
}
boolean canHandle = null != s_safePopup && 1 < nDicts;
if ( canHandle ) {
s_safePopup.doPopup( context, button, curDict, lang );
}
return canHandle;
}
//////////////////////////////////////////////////////////////////////
// XWListItem.ExpandedListener interface
//////////////////////////////////////////////////////////////////////