1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-05 02:39:23 +02:00

AuthenticationDialog -> AuthenticationActivity (in progress)

This commit is contained in:
Nikolay Pultsin 2011-04-24 07:25:42 +01:00
parent b4d5c5b825
commit d203aa606c
6 changed files with 184 additions and 118 deletions

View file

@ -39,12 +39,17 @@ public class AuthenticationActivity extends Activity {
final static String ERROR_KEY = "error";
final static String SHOW_SIGNUP_LINK_KEY = "showSignupLink";
final static int CANCEL_RESULT_CODE = 0;
final static int OK_RESULT_CODE = 1;
final static int SIGNUP_RESULT_CODE = 2;
private ZLResource myResource;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Thread.setDefaultUncaughtExceptionHandler(new org.geometerplus.zlibrary.ui.android.library.UncaughtExceptionHandler(this));
setResult(CANCEL_RESULT_CODE);
setContentView(R.layout.authentication);
final Intent intent = getIntent();
@ -93,8 +98,7 @@ public class AuthenticationActivity extends Activity {
signupView.setText(myResource.getResource("register").getValue());
signupView.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// TODO
//setResult();
setResult(SIGNUP_RESULT_CODE);
finish();
}
});
@ -117,7 +121,7 @@ public class AuthenticationActivity extends Activity {
PASSWORD_KEY,
findTextView(R.id.authentication_password).getText().toString()
);
setResult(0, data);
setResult(OK_RESULT_CODE, data);
finish();
}
});

View file

@ -91,7 +91,7 @@ class AuthenticationDialog {
intent.putExtra(AuthenticationActivity.SHOW_SIGNUP_LINK_KEY, true);
}
//getDialog().showInternal(activity, link, onSuccessRunnable);
activity.startActivity(intent);
activity.startActivityForResult(intent, NetworkBaseActivity.CUSTOM_AUTHENTICATION_CODE);
}
private void showInternal(Activity activity, INetworkLink link, Runnable onSuccessRunnable) {

View file

@ -21,10 +21,6 @@ package org.geometerplus.android.fbreader.network;
import java.net.*;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import android.app.*;
import android.os.Bundle;
import android.view.*;
@ -35,11 +31,9 @@ import android.graphics.Bitmap;
import org.geometerplus.zlibrary.ui.android.R;
import org.geometerplus.zlibrary.core.options.ZLStringOption;
import org.geometerplus.zlibrary.core.resources.ZLResource;
import org.geometerplus.zlibrary.core.image.ZLImage;
import org.geometerplus.zlibrary.core.image.ZLLoadableImage;
import org.geometerplus.zlibrary.core.network.ZLNetworkManager;
import org.geometerplus.zlibrary.ui.android.image.ZLAndroidImageManager;
import org.geometerplus.zlibrary.ui.android.image.ZLAndroidImageData;
@ -53,7 +47,9 @@ import org.geometerplus.fbreader.network.tree.SearchItemTree;
import org.geometerplus.android.fbreader.tree.ZLAndroidTree;
abstract class NetworkBaseActivity extends ListActivity implements NetworkView.EventListener {
protected static final int AUTHENTICATION_CODE = 1;
protected static final int BASIC_AUTHENTICATION_CODE = 1;
protected static final int CUSTOM_AUTHENTICATION_CODE = 2;
protected static final int SIGNUP_CODE = 3;
protected final ZLResource myResource = ZLResource.resource("networkView");
@ -85,52 +81,11 @@ abstract class NetworkBaseActivity extends ListActivity implements NetworkView.E
NetworkView.Instance().addEventListener(this);
}
private final MyCredentialsCreator myCredentialsCreator = new MyCredentialsCreator();
private class MyCredentialsCreator implements ZLNetworkManager.CredentialsCreator {
private volatile String myUsername;
private volatile String myPassword;
public Credentials createCredentials(AuthScope scope) {
if (!"basic".equalsIgnoreCase(scope.getScheme())) {
return null;
}
final Intent intent = new Intent();
final String host = scope.getHost();
final String area = scope.getRealm();
final ZLStringOption option = new ZLStringOption("username", host + ":" + area, "");
intent.setClass(NetworkBaseActivity.this, AuthenticationActivity.class);
intent.putExtra(AuthenticationActivity.HOST_KEY, host);
intent.putExtra(AuthenticationActivity.AREA_KEY, area);
intent.putExtra(AuthenticationActivity.SCHEME_KEY, scope.getScheme());
intent.putExtra(AuthenticationActivity.USERNAME_KEY, option.getValue());
startActivityForResult(intent, AUTHENTICATION_CODE);
synchronized (this) {
try {
wait();
} catch (InterruptedException e) {
}
}
Credentials creds = null;
if (myUsername != null && myPassword != null) {
option.setValue(myUsername);
creds = new UsernamePasswordCredentials(myUsername, myPassword);
}
myUsername = null;
myPassword = null;
return creds;
}
}
@Override
public void onResume() {
super.onResume();
getListView().setOnCreateContextMenuListener(this);
onModelChanged(); // do the same update actions as upon onModelChanged
ZLNetworkManager.Instance().setCredentialsCreator(myCredentialsCreator);
}
@Override
@ -148,19 +103,6 @@ abstract class NetworkBaseActivity extends ListActivity implements NetworkView.E
super.onDestroy();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == AUTHENTICATION_CODE) {
synchronized (myCredentialsCreator) {
if (data != null) {
myCredentialsCreator.myUsername = data.getStringExtra(AuthenticationActivity.USERNAME_KEY);
myCredentialsCreator.myPassword = data.getStringExtra(AuthenticationActivity.PASSWORD_KEY);
}
myCredentialsCreator.notify();
}
}
}
// method from NetworkView.EventListener
public void onModelChanged() {
}
@ -318,28 +260,6 @@ abstract class NetworkBaseActivity extends ListActivity implements NetworkView.E
}
}
@Override
protected Dialog onCreateDialog(int id) {
if (!NetworkView.Instance().isInitialized()) {
return null;
}
final AuthenticationDialog dlg = AuthenticationDialog.getDialog();
if (dlg != null) {
return dlg.createDialog(this);
}
return null;
}
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
super.onPrepareDialog(id, dialog);
final AuthenticationDialog dlg = AuthenticationDialog.getDialog();
if (dlg != null) {
dlg.prepareDialog(this, dialog);
}
}
@Override
public boolean onSearchRequested() {
return false;

View file

@ -19,17 +19,28 @@
package org.geometerplus.android.fbreader.network;
import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.widget.BaseAdapter;
import android.content.Intent;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.geometerplus.zlibrary.core.network.ZLNetworkException;
import org.geometerplus.zlibrary.core.network.ZLNetworkManager;
import org.geometerplus.zlibrary.core.options.ZLStringOption;
import org.geometerplus.zlibrary.core.resources.ZLResource;
import org.geometerplus.fbreader.network.*;
import org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager;
import org.geometerplus.fbreader.network.tree.*;
import org.geometerplus.fbreader.tree.FBTree;
import org.geometerplus.android.util.UIUtil;
public class NetworkCatalogActivity extends NetworkBaseActivity implements UserRegistrationConstants {
private NetworkTree myTree;
private volatile boolean myInProgress;
@ -89,26 +100,154 @@ public class NetworkCatalogActivity extends NetworkBaseActivity implements UserR
return super.onContextItemSelected(item);
}
private final MyCredentialsCreator myCredentialsCreator = new MyCredentialsCreator();
private class MyCredentialsCreator implements ZLNetworkManager.CredentialsCreator {
private volatile String myUsername;
private volatile String myPassword;
public Credentials createCredentials(AuthScope scope) {
if (!"basic".equalsIgnoreCase(scope.getScheme())) {
return null;
}
final Intent intent = new Intent();
final String host = scope.getHost();
final String area = scope.getRealm();
final ZLStringOption option = new ZLStringOption("username", host + ":" + area, "");
intent.setClass(NetworkCatalogActivity.this, AuthenticationActivity.class);
intent.putExtra(AuthenticationActivity.HOST_KEY, host);
intent.putExtra(AuthenticationActivity.AREA_KEY, area);
intent.putExtra(AuthenticationActivity.SCHEME_KEY, scope.getScheme());
intent.putExtra(AuthenticationActivity.USERNAME_KEY, option.getValue());
startActivityForResult(intent, BASIC_AUTHENTICATION_CODE);
synchronized (this) {
try {
wait();
} catch (InterruptedException e) {
}
}
Credentials creds = null;
if (myUsername != null && myPassword != null) {
option.setValue(myUsername);
creds = new UsernamePasswordCredentials(myUsername, myPassword);
}
myUsername = null;
myPassword = null;
return creds;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
default:
super.onActivityResult(requestCode, resultCode, data);
break;
case USER_REGISTRATION_REQUEST_CODE:
if (myTree instanceof NetworkCatalogTree &&
resultCode == RESULT_OK &&
data != null) {
try {
Util.runAfterRegistration(
((NetworkCatalogTree)myTree).Item.Link.authenticationManager(),
data
);
} catch (ZLNetworkException e) {
// TODO: show an error message
case BASIC_AUTHENTICATION_CODE:
synchronized (myCredentialsCreator) {
if (resultCode == AuthenticationActivity.OK_RESULT_CODE && data != null) {
myCredentialsCreator.myUsername =
data.getStringExtra(AuthenticationActivity.USERNAME_KEY);
myCredentialsCreator.myPassword =
data.getStringExtra(AuthenticationActivity.PASSWORD_KEY);
}
myCredentialsCreator.notify();
}
break;
case CUSTOM_AUTHENTICATION_CODE:
processCustomAuthentication(
this, ((NetworkCatalogTree)myTree).Item.Link, resultCode, data
);
break;
case SIGNUP_CODE:
processSignup(((NetworkCatalogTree)myTree).Item.Link, resultCode, data);
break;
}
}
static void runAuthenticationDialog(Activity activity, INetworkLink link, String error) {
final NetworkAuthenticationManager mgr = link.authenticationManager();
final Intent intent = new Intent(activity, AuthenticationActivity.class);
intent.putExtra(AuthenticationActivity.USERNAME_KEY, mgr.UserNameOption.getValue());
if (Util.isRegistrationSupported(activity, link)) {
intent.putExtra(AuthenticationActivity.SHOW_SIGNUP_LINK_KEY, true);
}
intent.putExtra(AuthenticationActivity.ERROR_KEY, error);
activity.startActivityForResult(intent, NetworkBaseActivity.CUSTOM_AUTHENTICATION_CODE);
}
static void processCustomAuthentication(final Activity activity, final INetworkLink link, int resultCode, Intent data) {
switch (resultCode) {
case AuthenticationActivity.CANCEL_RESULT_CODE:
UIUtil.wait(
"signOut",
new Runnable() {
public void run() {
final NetworkAuthenticationManager mgr =
link.authenticationManager();
if (mgr.mayBeAuthorised(false)) {
mgr.logOut();
}
final NetworkLibrary library = NetworkLibrary.Instance();
library.invalidateVisibility();
library.synchronize();
NetworkView.Instance().fireModelChanged();
}
},
activity
);
break;
case AuthenticationActivity.OK_RESULT_CODE:
{
final ZLResource resource =
ZLResource.resource("dialog").getResource("AuthenticationDialog");
final String username =
data.getStringExtra(AuthenticationActivity.USERNAME_KEY);
final String password =
data.getStringExtra(AuthenticationActivity.PASSWORD_KEY);
if (username.length() == 0) {
runAuthenticationDialog(
activity, link,
resource.getResource("loginIsEmpty").getValue()
);
}
final NetworkAuthenticationManager mgr = link.authenticationManager();
mgr.UserNameOption.setValue(username);
final Runnable runnable = new Runnable() {
public void run() {
try {
mgr.authorise(password);
if (mgr.needsInitialization()) {
mgr.initialize();
}
} catch (ZLNetworkException e) {
mgr.logOut();
runAuthenticationDialog(activity, link, e.getMessage());
return;
}
final NetworkLibrary library = NetworkLibrary.Instance();
library.invalidateVisibility();
library.synchronize();
NetworkView.Instance().fireModelChanged();
}
};
UIUtil.wait("authentication", runnable, activity);
break;
}
case AuthenticationActivity.SIGNUP_RESULT_CODE:
Util.runRegistrationDialog(activity, link);
break;
}
}
static void processSignup(INetworkLink link, int resultCode, Intent data) {
if (resultCode == RESULT_OK && data != null) {
try {
Util.runAfterRegistration(link.authenticationManager(), data);
} catch (ZLNetworkException e) {
// TODO: show an error message
}
}
}
@ -139,6 +278,7 @@ public class NetworkCatalogActivity extends NetworkBaseActivity implements UserR
@Override
public void onResume() {
super.onResume();
ZLNetworkManager.Instance().setCredentialsCreator(myCredentialsCreator);
}
private final class CatalogAdapter extends BaseAdapter {
@ -182,20 +322,24 @@ public class NetworkCatalogActivity extends NetworkBaseActivity implements UserR
@Override
public void onModelChanged() {
final NetworkView networkView = NetworkView.Instance();
final NetworkTree.Key key = getLoadableNetworkTreeKey(myTree);
myInProgress = key != null && networkView.isInitialized() && networkView.containsItemsLoadingRunnable(key);
getListView().invalidateViews();
/*
* getListAdapter() always returns CatalogAdapter because onModelChanged()
* can be called only after Activity's onStart() method (where NetworkView's
* addEventListener() is called). Therefore CatalogAdapter will be set as
* adapter in onCreate() method before any calls to onModelChanged().
*/
((CatalogAdapter) getListAdapter()).onModelChanged();
setupTitle();
runOnUiThread(new Runnable() {
public void run() {
final NetworkView networkView = NetworkView.Instance();
final NetworkTree.Key key = getLoadableNetworkTreeKey(myTree);
myInProgress = key != null && networkView.isInitialized() && networkView.containsItemsLoadingRunnable(key);
getListView().invalidateViews();
/*
* getListAdapter() always returns CatalogAdapter because onModelChanged()
* can be called only after Activity's onStart() method (where NetworkView's
* addEventListener() is called). Therefore CatalogAdapter will be set as
* adapter in onCreate() method before any calls to onModelChanged().
*/
((CatalogAdapter)getListAdapter()).onModelChanged();
setupTitle();
}
});
}
@Override

View file

@ -20,8 +20,6 @@
package org.geometerplus.android.fbreader.network;
public interface UserRegistrationConstants {
int USER_REGISTRATION_REQUEST_CODE = 12345;
String USER_REGISTRATION_USERNAME = "userName";
String USER_REGISTRATION_PASSWORD = "password";
String USER_REGISTRATION_EMAIL = "eMail";

View file

@ -68,7 +68,7 @@ abstract class Util implements UserRegistrationConstants {
activity.startActivityForResult(new Intent(
REGISTRATION_ACTION,
Uri.parse(link.getUrl(UrlInfo.Type.SignUp))
), USER_REGISTRATION_REQUEST_CODE);
), NetworkBaseActivity.SIGNUP_CODE);
}
} catch (ActivityNotFoundException e) {
}