fix posting of dialogfragments from preferences

I can't get preference fragments working in my world (mostly because
nested PreferenceScreens don't open when clicked on and the workarounds
for that are too complex) so next best thing is to fix alerts to work on
top of prefs activity. That involves creating a new transparent activity
that subclasses XWActivity and has a DelegateBase subclass. Launching an
alert is then just a matter of passing DlgState into the new activity
via an Intent, at which point everything Just Works. Button clicks are
returned via an intent as well.
This commit is contained in:
Eric House 2017-02-24 08:56:31 -08:00
parent ff99e89e9d
commit 48bd5d7f1d
9 changed files with 230 additions and 17 deletions

View file

@ -73,6 +73,10 @@
</intent-filter>
</activity>
<activity android:name="HostActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
/>
<activity android:name="DictsActivity"
android:label="@string/title_dicts_list"
android:configChanges="keyboardHidden|orientation|screenSize"

View file

@ -436,6 +436,32 @@ public class DelegateBase implements DlgClickNotify,
show( fragment );
}
protected void show( DlgState state )
{
DlgDelegateAlert df = null;
switch ( state.m_id ) {
case CONFIRM_THEN:
df = ConfirmThenAlert.newInstance( state );
break;
case DIALOG_ENABLESMS:
df = EnableSMSAlert.newInstance( state );
break;
case DIALOG_OKONLY:
df = OkOnlyAlert.newInstance( state );
break;
case DIALOG_NOTAGAIN:
df = NotAgainAlert.newInstance( state );
break;
case INVITE_CHOICES_THEN:
df = InviteChoicesAlert.newInstance( state );
break;
default:
Assert.fail();
}
show( df );
}
protected void show( DialogFragment df )
{
if ( m_activity instanceof XWActivity ) {
@ -580,9 +606,9 @@ public class DelegateBase implements DlgClickNotify,
m_dlgDelegate.stopProgress();
}
protected void showSMSEnableDialog( Action action, Object... params )
protected void showSMSEnableDialog( Action action )
{
m_dlgDelegate.showSMSEnableDialog( action, params );
m_dlgDelegate.showSMSEnableDialog( action );
}
protected boolean isVisible() { return m_isVisible; }
@ -653,7 +679,7 @@ public class DelegateBase implements DlgClickNotify,
action.toString() );
switch( action ) {
case ENABLE_SMS_ASK:
showSMSEnableDialog( Action.ENABLE_SMS_DO, params );
showSMSEnableDialog( Action.ENABLE_SMS_DO );
break;
case ENABLE_SMS_DO:
XWPrefs.setSMSEnabled( m_activity, true );

View file

@ -406,19 +406,18 @@ public class DlgDelegate {
.setParams( params )
.setTitle( titleId )
.setAction(action);
m_dlgt.show( OkOnlyAlert.newInstance( state ) );
m_dlgt.show( state );
}
// Puts up alert asking to choose a reason to enable SMS, and on dismiss
// calls onPosButton/onNegButton with the action and in params a Boolean
// indicating whether enabling is now ok.
public void showSMSEnableDialog( Action action, Object... params )
public void showSMSEnableDialog( Action action )
{
DlgState state = new DlgState( DlgID.DIALOG_ENABLESMS )
.setAction( action )
.setParams( params );
.setAction( action );
m_dlgt.show( EnableSMSAlert.newInstance( state ) );
m_dlgt.show( state );
}
private void showNotAgainDlgThen( String msg, int prefsKey,
@ -443,7 +442,7 @@ public class DlgDelegate {
.setAction( action )
.setActionPair( more )
.setParams( params );
m_dlgt.show( NotAgainAlert.newInstance( state ) );
m_dlgt.show( state );
}
}
@ -460,7 +459,7 @@ public class DlgDelegate {
.setAction( action )
.setTitle( titleId )
.setParams( params );
m_dlgt.show( ConfirmThenAlert.newInstance( state ) );
m_dlgt.show( state );
}
}
@ -475,7 +474,7 @@ public class DlgDelegate {
DlgState state = new DlgState( DlgID.INVITE_CHOICES_THEN )
.setAction( action )
.setParams( info );
m_dlgt.show( InviteChoicesAlert.newInstance( state ) );
m_dlgt.show( state );
} else {
post( new Runnable() {
public void run() {

View file

@ -29,6 +29,7 @@ import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import org.eehouse.android.xw4.DlgDelegate.Action;
import org.eehouse.android.xw4.DlgDelegate.ActionPair;
import org.eehouse.android.xw4.DlgDelegate.DlgClickNotify;
import junit.framework.Assert;
@ -68,6 +69,17 @@ public class DlgDelegateAlert extends DialogFragment {
bundle.putParcelable( STATE_KEY, m_state );
}
@Override
public void onDismiss( DialogInterface dif )
{
Activity activity = getActivity();
if ( activity instanceof DlgClickNotify ) {
((DlgClickNotify)activity)
.onDismissed( m_state.m_action, m_state.m_params );
}
super.onDismiss( dif );
}
protected void checkNotAgainCheck( DlgState state, NotAgainView naView )
{
if ( null != naView && naView.getChecked() ) {
@ -75,8 +87,8 @@ public class DlgDelegateAlert extends DialogFragment {
XWPrefs.setPrefsBoolean( getActivity(), m_state.m_prefsKey,
true );
} else if ( null != state.m_onNAChecked ) {
XWActivity activity = (XWActivity)getActivity();
activity.onPosButton( m_state.m_onNAChecked, null );
DlgClickNotify notify = (DlgClickNotify)getActivity();
notify.onPosButton( m_state.m_onNAChecked, null );
}
}
}
@ -103,14 +115,14 @@ public class DlgDelegateAlert extends DialogFragment {
Activity activity = getActivity();
if ( Action.SKIP_CALLBACK != m_state.m_action
&& activity instanceof XWActivity ) {
XWActivity xwact = (XWActivity)activity;
&& activity instanceof DlgClickNotify ) {
DlgClickNotify notify = (DlgClickNotify)activity;
switch ( button ) {
case AlertDialog.BUTTON_POSITIVE:
xwact.onPosButton( m_state.m_action, m_state.m_params );
notify.onPosButton( m_state.m_action, m_state.m_params );
break;
case AlertDialog.BUTTON_NEGATIVE:
xwact.onNegButton( m_state.m_action, m_state.m_params );
notify.onNegButton( m_state.m_action, m_state.m_params );
break;
default:
DbgUtils.loge( TAG, "unexpected button %d",

View file

@ -0,0 +1,36 @@
/* -*- compile-command: "find-and-gradle.sh installXw4Debug"; -*- */
/*
* Copyright 2009-2010 by Eric House (xwords@eehouse.org). All
* rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.eehouse.android.xw4;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
public class HostActivity extends XWActivity {
private HostDelegate m_dlgt;
@Override
public void onCreate( Bundle sis ) {
m_dlgt = new HostDelegate( this, sis );
super.onCreate( sis, m_dlgt );
}
}

View file

@ -0,0 +1,97 @@
/* -*- compile-command: "find-and-gradle.sh installXw4Debug"; -*- */
/*
* Copyright 2014-2016 by Eric House (xwords@eehouse.org). All rights
* reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.eehouse.android.xw4;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import org.eehouse.android.xw4.DlgDelegate.Action;
class HostDelegate extends DelegateBase {
private static final String ACTION = "ACTION";
private static final String IS_POS_BUTTON = "POS_BUTTON";
private static final String STATE = "STATE";
private Bundle mArgs;
public HostDelegate( Delegator delegator, Bundle sis )
{
super( delegator, sis, 0 );
mArgs = delegator.getArguments();
}
@Override
protected void init( Bundle savedInstanceState )
{
show( (DlgState)mArgs.getParcelable( STATE ) );
}
@Override
public boolean onPosButton( Action action, Object[] params )
{
setResult( action, true );
return true;
}
@Override
public boolean onNegButton( Action action, Object[] params )
{
setResult( action, false );
return true;
}
@Override
public boolean onDismissed( Action action, Object[] params )
{
finish();
return true;
}
private void setResult( Action action, boolean wasPos )
{
Intent intent = new Intent();
intent.putExtra( ACTION, action.ordinal() );
intent.putExtra( IS_POS_BUTTON, wasPos );
setResult( Activity.RESULT_OK, intent );
finish();
}
protected static void showForResult( Activity parent, DlgState state )
{
Intent intent = new Intent( parent, HostActivity.class );
intent.putExtra( STATE, state );
parent.startActivityForResult( intent,
RequestCode.HOST_DIALOG.ordinal() );
}
protected static void resultReceived( DlgDelegate.DlgClickNotify target,
RequestCode requestCode,
Intent data )
{
Action action = Action.values()[data.getIntExtra(ACTION, -1)];
if ( data.getBooleanExtra( IS_POS_BUTTON, false ) ) {
target.onPosButton( action, null );
} else {
target.onNegButton( action, null );
}
}
}

View file

@ -22,6 +22,7 @@ package org.eehouse.android.xw4;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.support.v4.app.DialogFragment;
@ -102,6 +103,14 @@ public class PrefsActivity extends PreferenceActivity
m_dlgt.prepareDialog( DlgID.values()[id], dialog );
}
@Override
protected void onActivityResult( int requestCode, int resultCode,
Intent data )
{
RequestCode rc = RequestCode.values()[requestCode];
m_dlgt.onActivityResult( rc, resultCode, data );
}
public OkOnlyBuilder makeOkOnlyBuilder( int msgId )
{
return m_dlgt.makeOkOnlyBuilder( msgId );

View file

@ -20,6 +20,7 @@
package org.eehouse.android.xw4;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
@ -46,6 +47,7 @@ import java.util.Map;
public class PrefsDelegate extends DelegateBase
implements SharedPreferences.OnSharedPreferenceChangeListener {
private static final String TAG = PrefsDelegate.class.getSimpleName();
private PreferenceActivity m_activity;
private static int[] s_keys = {
@ -262,6 +264,31 @@ public class PrefsDelegate extends DelegateBase
return handled;
}
@Override
protected void show( DlgState state )
{
Assert.assertNotNull( state );
switch ( state.m_id ) {
case CONFIRM_THEN:
case DIALOG_OKONLY:
case DIALOG_ENABLESMS:
HostDelegate.showForResult( m_activity, state );
break;
default:
Assert.fail();
}
}
@Override
protected void onActivityResult( RequestCode requestCode, int resultCode,
Intent data )
{
if ( Activity.RESULT_CANCELED != resultCode ) {
HostDelegate.resultReceived( this, requestCode, data );
}
}
private void relaunch()
{
PreferenceManager.setDefaultValues( m_activity, R.xml.xwprefs,

View file

@ -43,4 +43,7 @@ public enum RequestCode {
// SMSInviteDelegate
GET_CONTACT,
// HostDelegate
HOST_DIALOG,
}