new dynamic activity result handling

This commit is contained in:
fkloft 2014-01-20 20:33:02 +01:00
parent e63678c2e4
commit 9e210805dc
2 changed files with 26 additions and 17 deletions

View File

@ -18,10 +18,12 @@ import android.widget.BaseAdapter;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import com.cradle.iitc_mobile.IITC_Mobile.ResponseHandler;
/** /**
* this class manages automatic login using the Google account stored on the device * this class manages automatic login using the Google account stored on the device
*/ */
public class IITC_DeviceAccountLogin implements AccountManagerCallback<Bundle> { public class IITC_DeviceAccountLogin implements AccountManagerCallback<Bundle>, ResponseHandler {
/** /**
* Adapter to show available accounts in a ListView. Accounts are read from mAccounts * Adapter to show available accounts in a ListView. Accounts are read from mAccounts
*/ */
@ -126,6 +128,7 @@ public class IITC_DeviceAccountLogin implements AccountManagerCallback<Bundle> {
/** /**
* called by IITC_Mobile when the authentication activity has finished. * called by IITC_Mobile when the authentication activity has finished.
*/ */
@Override
public void onActivityResult(int resultCode, Intent data) { public void onActivityResult(int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) if (resultCode == Activity.RESULT_OK)
// authentication activity succeeded, request token again // authentication activity succeeded, request token again
@ -149,7 +152,7 @@ public class IITC_DeviceAccountLogin implements AccountManagerCallback<Bundle> {
// There is a reason we need to start the given activity if we want an // There is a reason we need to start the given activity if we want an
// authentication token. (Could be user confirmation or something else. Whatever, // authentication token. (Could be user confirmation or something else. Whatever,
// we have to start it) IITC_Mobile will call it using startActivityForResult // we have to start it) IITC_Mobile will call it using startActivityForResult
mActivity.startLoginActivity(launch); mActivity.startActivityForResult(launch, this);
return; return;
} }

View File

@ -42,10 +42,9 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.Stack; import java.util.Stack;
import java.util.Vector;
public class IITC_Mobile extends Activity implements OnSharedPreferenceChangeListener { public class IITC_Mobile extends Activity implements OnSharedPreferenceChangeListener {
private static final int REQUEST_LOGIN = 1;
private static final String mIntelUrl = "https://www.ingress.com/intel"; private static final String mIntelUrl = "https://www.ingress.com/intel";
private SharedPreferences mSharedPrefs; private SharedPreferences mSharedPrefs;
@ -55,6 +54,7 @@ public class IITC_Mobile extends Activity implements OnSharedPreferenceChangeLis
private IITC_NavigationHelper mNavigationHelper; private IITC_NavigationHelper mNavigationHelper;
private IITC_MapSettings mMapSettings; private IITC_MapSettings mMapSettings;
private IITC_DeviceAccountLogin mLogin; private IITC_DeviceAccountLogin mLogin;
private Vector<ResponseHandler> mResponseHandlers = new Vector<ResponseHandler>();
private boolean mDesktopMode = false; private boolean mDesktopMode = false;
private boolean mAdvancedMenu = false; private boolean mAdvancedMenu = false;
private MenuItem mSearchMenuItem; private MenuItem mSearchMenuItem;
@ -572,23 +572,25 @@ public class IITC_Mobile extends Activity implements OnSharedPreferenceChangeLis
return this.mIitcWebView; return this.mIitcWebView;
} }
/** public void startActivityForResult(Intent launch, ResponseHandler handler) {
* It can occur that in order to authenticate, an external activity has to be launched. int index = mResponseHandlers.indexOf(handler);
* (This could for example be a confirmation dialog.) if (index == -1) {
*/ mResponseHandlers.add(handler);
public void startLoginActivity(Intent launch) { index = mResponseHandlers.indexOf(handler);
startActivityForResult(launch, REQUEST_LOGIN); // REQUEST_LOGIN is to recognize the result }
startActivityForResult(launch, RESULT_FIRST_USER + index);
} }
@Override @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) { int index = requestCode - RESULT_FIRST_USER;
case REQUEST_LOGIN:
// authentication activity has returned. mLogin will continue authentication try {
mLogin.onActivityResult(resultCode, data); ResponseHandler handler = mResponseHandlers.get(index);
break; handler.onActivityResult(resultCode, data);
default: } catch (ArrayIndexOutOfBoundsException e) {
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);
} }
} }
@ -764,4 +766,8 @@ public class IITC_Mobile extends Activity implements OnSharedPreferenceChangeLis
public IITC_UserLocation getUserLocation() { public IITC_UserLocation getUserLocation() {
return mUserLocation; return mUserLocation;
} }
public interface ResponseHandler {
void onActivityResult(int resultCode, Intent data);
}
} }