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.Toast;
import com.cradle.iitc_mobile.IITC_Mobile.ResponseHandler;
/**
* 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
*/
@ -126,6 +128,7 @@ public class IITC_DeviceAccountLogin implements AccountManagerCallback<Bundle> {
/**
* called by IITC_Mobile when the authentication activity has finished.
*/
@Override
public void onActivityResult(int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK)
// 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
// authentication token. (Could be user confirmation or something else. Whatever,
// we have to start it) IITC_Mobile will call it using startActivityForResult
mActivity.startLoginActivity(launch);
mActivity.startActivityForResult(launch, this);
return;
}

View File

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