From 87c6b3e0fe3d8fb196316c44f6cf7b8f5ab43748 Mon Sep 17 00:00:00 2001 From: Eric House Date: Wed, 9 Dec 2015 06:10:09 -0800 Subject: [PATCH] use an enum for startActivityForResult requestCode to ensure they're unique across the app. --- .../eehouse/android/xw4/BTInviteDelegate.java | 4 +- .../eehouse/android/xw4/BoardDelegate.java | 16 +++---- .../org/eehouse/android/xw4/DelegateBase.java | 7 +-- .../eehouse/android/xw4/DictsDelegate.java | 8 ++-- .../android/xw4/GameConfigDelegate.java | 16 +++---- .../android/xw4/GamesListDelegate.java | 14 +++--- .../android/xw4/RelayInviteDelegate.java | 4 +- .../org/eehouse/android/xw4/RequestCode.java | 47 +++++++++++++++++++ .../android/xw4/SMSInviteDelegate.java | 10 ++-- .../org/eehouse/android/xw4/XWActivity.java | 3 +- 10 files changed, 87 insertions(+), 42 deletions(-) create mode 100644 xwords4/android/XWords4/src/org/eehouse/android/xw4/RequestCode.java diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BTInviteDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BTInviteDelegate.java index 916d4d015..16337eb9a 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BTInviteDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BTInviteDelegate.java @@ -54,12 +54,12 @@ public class BTInviteDelegate extends InviteDelegate { private BTDevsAdapter m_adapter; public static void launchForResult( Activity activity, int nMissing, - int requestCode ) + RequestCode requestCode ) { Assert.assertTrue( 0 < nMissing ); // don't call if nMissing == 0 Intent intent = new Intent( activity, BTInviteActivity.class ); intent.putExtra( INTENT_KEY_NMISSING, nMissing ); - activity.startActivityForResult( intent, requestCode ); + activity.startActivityForResult( intent, requestCode.ordinal() ); } protected BTInviteDelegate( Delegator delegator, Bundle savedInstanceState ) 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 d5692a1c8..8919ad945 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardDelegate.java @@ -69,11 +69,6 @@ public class BoardDelegate extends DelegateBase public static final String INTENT_KEY_CHAT = "chat"; - private static final int CHAT_REQUEST = 1; - private static final int BT_INVITE_RESULT = 2; - private static final int SMS_INVITE_RESULT = 3; - private static final int RELAY_INVITE_RESULT = 4; - private static final int SCREEN_ON_TIME = 10 * 60 * 1000; // 10 mins private static final String DLG_TITLE = "DLG_TITLE"; @@ -644,7 +639,8 @@ public class BoardDelegate extends DelegateBase } } - protected void onActivityResult( int requestCode, int resultCode, Intent data ) + @Override + protected void onActivityResult( RequestCode requestCode, int resultCode, Intent data ) { if ( Activity.RESULT_CANCELED != resultCode ) { InviteMeans missingMeans = null; @@ -1059,15 +1055,15 @@ public class BoardDelegate extends DelegateBase break; case BLUETOOTH: BTInviteDelegate.launchForResult( m_activity, m_nMissing, - BT_INVITE_RESULT ); + RequestCode.BT_INVITE_RESULT ); break; case SMS: SMSInviteDelegate.launchForResult( m_activity, m_nMissing, - SMS_INVITE_RESULT ); + RequestCode.SMS_INVITE_RESULT ); break; case RELAY: RelayInviteDelegate.launchForResult( m_activity, m_nMissing, - RELAY_INVITE_RESULT ); + RequestCode.RELAY_INVITE_RESULT ); break; case EMAIL: case CLIPBOARD: @@ -2310,7 +2306,7 @@ public class BoardDelegate extends DelegateBase if ( BuildConstants.CHAT_SUPPORTED ) { Intent intent = new Intent( m_activity, ChatActivity.class ); intent.putExtra( GameUtils.INTENT_KEY_ROWID, m_rowid ); - startActivityForResult( intent, CHAT_REQUEST ); + startActivityForResult( intent, RequestCode.CHAT_REQUEST ); } } 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 3ca4960c9..04957cc80 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DelegateBase.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DelegateBase.java @@ -86,7 +86,7 @@ public class DelegateBase implements DlgClickNotify, protected void onDestroy() {} protected void onWindowFocusChanged( boolean hasFocus ) {} protected boolean onBackPressed() { return false; } - protected void onActivityResult( int requestCode, int resultCode, + protected void onActivityResult( RequestCode requestCode, int resultCode, Intent data ) { DbgUtils.logf( "DelegateBase.onActivityResult(): subclass responsibility!!!" ); @@ -179,9 +179,10 @@ public class DelegateBase implements DlgClickNotify, return m_activity.getTitle().toString(); } - protected void startActivityForResult( Intent intent, int requestCode ) + protected void startActivityForResult( Intent intent, + RequestCode requestCode ) { - m_activity.startActivityForResult( intent, requestCode ); + m_activity.startActivityForResult( intent, requestCode.ordinal() ); } protected void setResult( int result, Intent intent ) 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 60a0bae80..077a78b30 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictsDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DictsDelegate.java @@ -984,7 +984,7 @@ public class DictsDelegate extends ListDelegateBase // return mkDownloadIntent( context, dict_url ); } - public static void downloadForResult( Activity activity, int requestCode, + public static void downloadForResult( Activity activity, RequestCode requestCode, int lang, String name ) { Intent intent = new Intent( activity, DictsActivity.class ); @@ -997,16 +997,16 @@ public class DictsDelegate extends ListDelegateBase intent.putExtra( DICT_NAME_EXTRA, name ); } - activity.startActivityForResult( intent, requestCode ); + activity.startActivityForResult( intent, requestCode.ordinal() ); } - public static void downloadForResult( Activity activity, int requestCode, + public static void downloadForResult( Activity activity, RequestCode requestCode, int lang ) { downloadForResult( activity, requestCode, lang, null ); } - public static void downloadForResult( Activity activity, int requestCode ) + public static void downloadForResult( Activity activity, RequestCode requestCode ) { downloadForResult( activity, requestCode, 0, null ); } 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 c34ebbb4f..f9fe90c36 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameConfigDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameConfigDelegate.java @@ -68,8 +68,6 @@ public class GameConfigDelegate extends DelegateBase private static final String INTENT_FORRESULT_ROWID = "forresult"; private static final String WHICH_PLAYER = "WHICH_PLAYER"; - private static final int REQUEST_LANG = 1; - private static final int REQUEST_DICT = 2; private Activity m_activity; private CheckBox m_joinPublicCheck; @@ -518,7 +516,7 @@ public class GameConfigDelegate extends DelegateBase } @Override - protected void onActivityResult( int requestCode, int resultCode, Intent data ) + protected void onActivityResult( RequestCode requestCode, int resultCode, Intent data ) { if ( Activity.RESULT_CANCELED != resultCode ) { loadGame(); @@ -528,7 +526,7 @@ public class GameConfigDelegate extends DelegateBase configDictSpinner( m_dictSpinner, m_gi.dictLang, dictName ); configDictSpinner( m_playerDictSpinner, m_gi.dictLang, dictName ); break; - case REQUEST_LANG: + case REQUEST_LANG_GC: String langName = data.getStringExtra( DictsDelegate.RESULT_LAST_LANG ); selLangChanged( langName ); setLangSpinnerSelection( langName ); @@ -852,7 +850,7 @@ public class GameConfigDelegate extends DelegateBase if ( chosen.equals( m_browseText ) ) { DictsDelegate.downloadForResult( m_activity, - REQUEST_DICT, + RequestCode.REQUEST_DICT, m_gi.dictLang ); } } @@ -885,7 +883,9 @@ public class GameConfigDelegate extends DelegateBase String chosen = (String)parentView.getItemAtPosition( position ); if ( chosen.equals( m_browseText ) ) { - DictsDelegate.downloadForResult( m_activity, REQUEST_LANG ); + DictsDelegate.downloadForResult( m_activity, + RequestCode + .REQUEST_LANG_GC ); } else { String langName = adapter.getLangAtPosition( position ); selLangChanged( langName ); @@ -1196,14 +1196,14 @@ public class GameConfigDelegate extends DelegateBase return DeviceRole.SERVER_STANDALONE == m_giOrig.serverRole; } - public static void editForResult( Activity parent, int requestCode, + public static void editForResult( Activity parent, RequestCode requestCode, long rowID ) { Intent intent = new Intent( parent, GameConfigActivity.class ); intent.setAction( Intent.ACTION_EDIT ); intent.putExtra( GameUtils.INTENT_KEY_ROWID, rowID ); intent.putExtra( INTENT_FORRESULT_ROWID, true ); - parent.startActivityForResult( intent, requestCode ); + parent.startActivityForResult( intent, requestCode.ordinal() ); } private void setConnLabel() 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 952cd4f1d..331a28874 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesListDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GamesListDelegate.java @@ -82,9 +82,6 @@ public class GamesListDelegate extends ListDelegateBase private static final String SAVE_DICTNAMES = "SAVE_DICTNAMES"; private static final String SAVE_NEXTSOLO = "SAVE_NEXTSOLO"; - private static final int REQUEST_LANG = 1; - private static final int CONFIG_GAME = 2; - private static final String RELAYIDS_EXTRA = "relayids"; private static final String ROWID_EXTRA = "rowid"; private static final String GAMEID_EXTRA = "gameid"; @@ -620,7 +617,9 @@ public class GamesListDelegate extends ListDelegateBase public void onClick( DialogInterface dlg, int item ) { // no name, so user must pick if ( null == m_missingDictName ) { - DictsDelegate.downloadForResult( m_activity, REQUEST_LANG, + DictsDelegate.downloadForResult( m_activity, + RequestCode + .REQUEST_LANG_GL, m_missingDictLang ); } else { DwnldDelegate @@ -1279,12 +1278,12 @@ public class GamesListDelegate extends ListDelegateBase } @Override - protected void onActivityResult( int requestCode, int resultCode, + protected void onActivityResult( RequestCode requestCode, int resultCode, Intent data ) { boolean cancelled = Activity.RESULT_CANCELED == resultCode; switch ( requestCode ) { - case REQUEST_LANG: + case REQUEST_LANG_GL: if ( !cancelled ) { DbgUtils.logf( "lang need met" ); if ( checkWarnNoDict( m_missingDictRowId ) ) { @@ -2442,7 +2441,8 @@ public class GamesListDelegate extends ListDelegateBase if ( doConfigure ) { // configure it - GameConfigDelegate.editForResult( m_activity, CONFIG_GAME, rowID ); + GameConfigDelegate.editForResult( m_activity, RequestCode + .CONFIG_GAME, rowID ); } else { // launch it GameUtils.launchGame( m_activity, rowID ); diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayInviteDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayInviteDelegate.java index 09f077907..5b68c2c55 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayInviteDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/RelayInviteDelegate.java @@ -69,11 +69,11 @@ public class RelayInviteDelegate extends InviteDelegate { private Activity m_activity; public static void launchForResult( Activity activity, int nMissing, - int requestCode ) + RequestCode requestCode ) { Intent intent = new Intent( activity, RelayInviteActivity.class ); intent.putExtra( INTENT_KEY_NMISSING, nMissing ); - activity.startActivityForResult( intent, requestCode ); + activity.startActivityForResult( intent, requestCode.ordinal() ); } public RelayInviteDelegate( Delegator delegator, Bundle savedInstanceState ) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/RequestCode.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/RequestCode.java new file mode 100644 index 000000000..dfba107cc --- /dev/null +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/RequestCode.java @@ -0,0 +1,47 @@ +/* -*- compile-command: "find-and-ant.sh debug install"; -*- */ +/* + * Copyright 2015 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; + +// Just make sure all calls to startActivityForResult are using unique codes. + +public enum RequestCode { + __UNUSED, + + // BoardDelegate + CHAT_REQUEST, + BT_INVITE_RESULT, + SMS_INVITE_RESULT, + RELAY_INVITE_RESULT, + + // PermUtils + PERM_REQUEST, + + // GameConfig + REQUEST_LANG_GC, + REQUEST_DICT, + + // Games list + REQUEST_LANG_GL, + CONFIG_GAME, + + // SMSInviteDelegate + GET_CONTACT, +} + diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/SMSInviteDelegate.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/SMSInviteDelegate.java index b7179ac20..3d7589ad2 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/SMSInviteDelegate.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/SMSInviteDelegate.java @@ -52,7 +52,6 @@ import org.eehouse.android.xw4.DlgDelegate.Action; public class SMSInviteDelegate extends InviteDelegate { - private static final int GET_CONTACT = 1; private static final String SAVE_NAME = "SAVE_NAME"; private static final String SAVE_NUMBER = "SAVE_NUMBER"; @@ -65,11 +64,11 @@ public class SMSInviteDelegate extends InviteDelegate { private Activity m_activity; public static void launchForResult( Activity activity, int nMissing, - int requestCode ) + RequestCode requestCode ) { Intent intent = new Intent( activity, SMSInviteActivity.class ); intent.putExtra( INTENT_KEY_NMISSING, nMissing ); - activity.startActivityForResult( intent, requestCode ); + activity.startActivityForResult( intent, requestCode.ordinal() ); } public SMSInviteDelegate( Delegator delegator, Bundle savedInstanceState ) @@ -114,7 +113,8 @@ public class SMSInviteDelegate extends InviteDelegate { } } - protected void onActivityResult( int requestCode, int resultCode, + @Override + protected void onActivityResult( RequestCode requestCode, int resultCode, Intent data ) { // super.onActivityResult( requestCode, resultCode, data ); @@ -167,7 +167,7 @@ public class SMSInviteDelegate extends InviteDelegate { Intent intent = new Intent( Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI ); intent.setType( Phone.CONTENT_TYPE ); - startActivityForResult( intent, GET_CONTACT ); + startActivityForResult( intent, RequestCode.GET_CONTACT ); } protected void clearSelected() diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWActivity.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWActivity.java index d700b8264..b11a826a4 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWActivity.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWActivity.java @@ -180,7 +180,8 @@ public class XWActivity extends Activity implements Delegator { protected void onActivityResult( int requestCode, int resultCode, Intent data ) { - m_dlgt.onActivityResult( requestCode, resultCode, data ); + RequestCode rc = RequestCode.values()[requestCode]; + m_dlgt.onActivityResult( rc, resultCode, data ); } //////////////////////////////////////////////////////////////////////