hook up context menus, and add a delete item to replace the

checkboxes.  Still need to disable it when dict is built-in.  And
maybe add a different menu for languages.
This commit is contained in:
Andy2 2011-05-09 18:59:51 -07:00
parent c990900913
commit 085748e201
2 changed files with 78 additions and 44 deletions

View file

@ -7,6 +7,9 @@
<item android:id="@+id/dicts_item_select_robot" <item android:id="@+id/dicts_item_select_robot"
android:title="@string/dicts_item_select_robot" android:title="@string/dicts_item_select_robot"
/> />
<item android:id="@+id/dicts_item_delete"
android:title="@string/dicts_item_delete"
/>
<item android:id="@+id/dicts_item_details" <item android:id="@+id/dicts_item_details"
android:title="@string/dicts_item_details" android:title="@string/dicts_item_details"
/> />

View file

@ -42,6 +42,8 @@ import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MenuInflater; import android.view.MenuInflater;
import android.widget.ExpandableListAdapter; import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.net.Uri; import android.net.Uri;
import junit.framework.Assert; import junit.framework.Assert;
@ -51,27 +53,26 @@ import org.eehouse.android.xw4.jni.JNIUtilsImpl;
import org.eehouse.android.xw4.jni.CommonPrefs; import org.eehouse.android.xw4.jni.CommonPrefs;
public class DictsActivity extends ExpandableListActivity public class DictsActivity extends ExpandableListActivity
implements View.OnClickListener, implements View.OnClickListener {
XWListItem.DeleteCallback {
private static final String DICT_DOLAUNCH = "do_launch"; private static final String DICT_DOLAUNCH = "do_launch";
private static final String DICT_LANG_EXTRA = "use_lang"; private static final String DICT_LANG_EXTRA = "use_lang";
private static final String DICT_NAME_EXTRA = "use_dict"; private static final String DICT_NAME_EXTRA = "use_dict";
private String[] m_dicts;
private static final int PICK_STORAGE = DlgDelegate.DIALOG_LAST + 1; private static final int PICK_STORAGE = DlgDelegate.DIALOG_LAST + 1;
private int m_lang = 0; private int m_lang = 0;
private String[] m_langs;
private String m_name = null; private String m_name = null;
private String m_download; private String m_download;
private DlgDelegate m_delegate;
private class DictListAdapter implements ExpandableListAdapter { private class DictListAdapter implements ExpandableListAdapter {
private Context m_context; private Context m_context;
private String[] m_langs;
public DictListAdapter( Context context ) { public DictListAdapter( Context context ) {
//super( context, m_dicts.length ); //super( context, m_dicts.length );
m_context = context; m_context = context;
m_langs = DictLangCache.listLangs( m_context );
} }
public boolean areAllItemsEnabled() { return false; } public boolean areAllItemsEnabled() { return false; }
@ -183,37 +184,48 @@ public class DictsActivity extends ExpandableListActivity
@Override @Override
protected Dialog onCreateDialog( int id ) protected Dialog onCreateDialog( int id )
{ {
Dialog dialog = super.onCreateDialog( id ); Dialog dialog = m_delegate.onCreateDialog( id );
switch( id ) { if ( null != dialog ) {
case PICK_STORAGE: switch( id ) {
DialogInterface.OnClickListener lstnrSD; case PICK_STORAGE:
DialogInterface.OnClickListener lstnrSD;
lstnrSD = new DialogInterface.OnClickListener() { lstnrSD = new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dlg, int item ) { public void onClick( DialogInterface dlg, int item ) {
startDownload( m_lang, m_name, item != startDownload( m_lang, m_name, item !=
DialogInterface.BUTTON_POSITIVE ); DialogInterface.BUTTON_POSITIVE );
} }
}; };
dialog = new AlertDialog.Builder( this ) dialog = new AlertDialog.Builder( this )
.setTitle( R.string.storeWhereTitle ) .setTitle( R.string.storeWhereTitle )
.setMessage( R.string.storeWhereMsg ) .setMessage( R.string.storeWhereMsg )
.setPositiveButton( R.string.button_internal, lstnrSD ) .setPositiveButton( R.string.button_internal, lstnrSD )
.setNegativeButton( R.string.button_sd, lstnrSD ) .setNegativeButton( R.string.button_sd, lstnrSD )
.create(); .create();
break; break;
}
} }
return dialog; return dialog;
} }
@Override
public void onPrepareDialog( int id, Dialog dialog )
{
m_delegate.onPrepareDialog( id, dialog );
}
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState ); super.onCreate( savedInstanceState );
m_download = getString( R.string.download_dicts ); m_delegate = new DlgDelegate( this );
m_download = getString( R.string.download_dicts );
m_langs = DictLangCache.listLangs( this );
setContentView( R.layout.dict_browse ); setContentView( R.layout.dict_browse );
//registerForContextMenu( getListView() ); registerForContextMenu( getExpandableListView() );
Button download = (Button)findViewById( R.id.download ); Button download = (Button)findViewById( R.id.download );
download.setOnClickListener( this ); download.setOnClickListener( this );
@ -252,33 +264,50 @@ public class DictsActivity extends ExpandableListActivity
{ {
super.onCreateContextMenu( menu, view, menuInfo ); super.onCreateContextMenu( menu, view, menuInfo );
MenuInflater inflater = getMenuInflater(); ExpandableListView.ExpandableListContextMenuInfo info
inflater.inflate( R.menu.dicts_item_menu, menu ); = (ExpandableListView.ExpandableListContextMenuInfo)menuInfo;
long packedPosition = info.packedPosition;
int childPosition = ExpandableListView.
getPackedPositionChild( packedPosition );
// int groupPosition = ExpandableListView.
// getPackedPositionGroup( packedPosition );
// Utils.logf( "onCreateContextMenu: group: %d; child: %d",
// groupPosition, childPosition );
AdapterView.AdapterContextMenuInfo info // We don't have a menu yet for languages, just for their dict
= (AdapterView.AdapterContextMenuInfo)menuInfo; // children
if ( childPosition >= 0 ) {
MenuInflater inflater = getMenuInflater();
inflater.inflate( R.menu.dicts_item_menu, menu );
}
} }
@Override @Override
public boolean onContextItemSelected( MenuItem item ) public boolean onContextItemSelected( MenuItem item )
{ {
boolean handled = false; boolean handled = false;
AdapterView.AdapterContextMenuInfo info; ExpandableListContextMenuInfo info = null;
try { try {
info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); info = (ExpandableListContextMenuInfo)item.getMenuInfo();
} catch (ClassCastException e) { } catch (ClassCastException e) {
Utils.logf( "bad menuInfo:" + e.toString() ); Utils.logf( "bad menuInfo:" + e.toString() );
return false; return false;
} }
TextView text = (TextView)info.targetView;
int id = item.getItemId(); int id = item.getItemId();
int position = info.position;
switch( id ) { switch( id ) {
case R.id.dicts_item_select_human: case R.id.dicts_item_select_human:
setDefault( R.string.key_default_dict, position ); setDefault( R.string.key_default_dict, text );
break; break;
case R.id.dicts_item_select_robot: case R.id.dicts_item_select_robot:
setDefault( R.string.key_default_robodict, position ); setDefault( R.string.key_default_robodict, text );
break;
case R.id.dicts_item_delete:
long packedPosition = info.packedPosition;
int groupPosition = ExpandableListView.
getPackedPositionGroup( packedPosition );
deleteDict( groupPosition, text );
break; break;
case R.id.dicts_item_details: case R.id.dicts_item_details:
Utils.notImpl( this ); Utils.notImpl( this );
@ -288,22 +317,25 @@ public class DictsActivity extends ExpandableListActivity
return handled; return handled;
} }
private void setDefault( int keyId, int position ) private void setDefault( int keyId, final TextView text )
{ {
SharedPreferences sp SharedPreferences sp
= PreferenceManager.getDefaultSharedPreferences( this ); = PreferenceManager.getDefaultSharedPreferences( this );
SharedPreferences.Editor editor = sp.edit(); SharedPreferences.Editor editor = sp.edit();
String key = getString( keyId ); String key = getString( keyId );
editor.putString( key, m_dicts[position] ); String name = text.getText().toString();
editor.putString( key, name );
editor.commit(); editor.commit();
} }
// DeleteCallback interface // DeleteCallback interface
public void deleteCalled( final int myPosition ) private void deleteDict( int group, TextView text )
{ {
final String dict = m_dicts[myPosition]; final String dict = text.getText().toString();
int lang = DictLangCache.getDictLangCode( this, dict ); Utils.logf( "deleteDict(%s)", dict );
int nGames = DBUtils.countGamesUsing( this, lang ); String lang = m_langs[group];
int code = DictLangCache.getLangLangCode( this, lang );
int nGames = DBUtils.countGamesUsing( this, code );
String msg = String.format( getString( R.string.confirm_delete_dictf ), String msg = String.format( getString( R.string.confirm_delete_dictf ),
dict ); dict );
DialogInterface.OnClickListener action = DialogInterface.OnClickListener action =
@ -315,16 +347,15 @@ public class DictsActivity extends ExpandableListActivity
if ( nGames > 0 ) { if ( nGames > 0 ) {
int fmt; int fmt;
if ( 1 == DictLangCache.getHaveLang( this, lang ).length ) { if ( 1 == DictLangCache.getHaveLang( this, code ).length ) {
fmt = R.string.confirm_deleteonly_dictf; fmt = R.string.confirm_deleteonly_dictf;
} else { } else {
fmt = R.string.confirm_deletemore_dictf; fmt = R.string.confirm_deletemore_dictf;
} }
String langName = DictLangCache.getLangName( this, lang ); msg += String.format( getString(fmt), lang );
msg += String.format( getString(fmt), langName );
} }
// showConfirmThen( msg, action ); m_delegate.showConfirmThen( msg, action );
} }
private void deleteDict( String dict ) private void deleteDict( String dict )
@ -353,7 +384,7 @@ public class DictsActivity extends ExpandableListActivity
private void mkListAdapter() private void mkListAdapter()
{ {
m_dicts = GameUtils.dictList( this ); m_langs = DictLangCache.listLangs( this );
ExpandableListAdapter adapter = new DictListAdapter( this ); ExpandableListAdapter adapter = new DictListAdapter( this );
setListAdapter( adapter ); setListAdapter( adapter );
} }