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

CredentialProvider has been moved from NetworkCatalogActivity to AuthenticationActivity

This commit is contained in:
Nikolay Pultsin 2011-07-15 20:12:38 +01:00
parent 1f8c4a6f54
commit f0a082c3f9
2 changed files with 70 additions and 60 deletions

View file

@ -26,20 +26,79 @@ import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.geometerplus.zlibrary.core.resources.ZLResource;
import org.geometerplus.zlibrary.core.network.ZLNetworkManager;
import org.geometerplus.zlibrary.core.options.ZLStringOption;
import org.geometerplus.zlibrary.ui.android.R;
public class AuthenticationActivity extends Activity {
final static String AREA_KEY = "area";
final static String HOST_KEY = "host";
final static String SCHEME_KEY = "scheme";
final static String USERNAME_KEY = "username";
final static String PASSWORD_KEY = "password";
final static String ERROR_KEY = "error";
final static String SHOW_SIGNUP_LINK_KEY = "showSignupLink";
private static final String AREA_KEY = "area";
private static final String HOST_KEY = "host";
static final String SCHEME_KEY = "scheme";
static final String USERNAME_KEY = "username";
static final String PASSWORD_KEY = "password";
static final String ERROR_KEY = "error";
static final String SHOW_SIGNUP_LINK_KEY = "showSignupLink";
final static int RESULT_SIGNUP = RESULT_FIRST_USER;
static final int RESULT_SIGNUP = RESULT_FIRST_USER;
static class CredentialsCreator implements ZLNetworkManager.CredentialsCreator {
private final Activity myActivity;
private final int myCode;
private volatile String myUsername;
private volatile String myPassword;
CredentialsCreator(Activity activity, int code) {
myActivity = activity;
myCode = code;
}
synchronized void onDataReceived(int resultCode, Intent data) {
if (resultCode == RESULT_OK && data != null) {
myUsername = data.getStringExtra(USERNAME_KEY);
myPassword = data.getStringExtra(PASSWORD_KEY);
}
notify();
}
public Credentials createCredentials(String scheme, 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(myActivity, AuthenticationActivity.class);
intent.putExtra(HOST_KEY, host);
intent.putExtra(AREA_KEY, area);
intent.putExtra(SCHEME_KEY, scheme);
intent.putExtra(USERNAME_KEY, option.getValue());
myActivity.startActivityForResult(intent, myCode);
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;
}
}
private ZLResource myResource;

View file

@ -24,12 +24,7 @@ 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.ZLNetworkManager;
import org.geometerplus.zlibrary.core.options.ZLStringOption;
import org.geometerplus.fbreader.network.*;
import org.geometerplus.fbreader.network.tree.*;
@ -99,58 +94,14 @@ 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(String scheme, 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, scheme);
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;
}
}
private final AuthenticationActivity.CredentialsCreator myCredentialsCreator =
new AuthenticationActivity.CredentialsCreator(this, BASIC_AUTHENTICATION_CODE);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case BASIC_AUTHENTICATION_CODE:
synchronized (myCredentialsCreator) {
if (resultCode == AuthenticationActivity.RESULT_OK && data != null) {
myCredentialsCreator.myUsername =
data.getStringExtra(AuthenticationActivity.USERNAME_KEY);
myCredentialsCreator.myPassword =
data.getStringExtra(AuthenticationActivity.PASSWORD_KEY);
}
myCredentialsCreator.notify();
}
myCredentialsCreator.onDataReceived(resultCode, data);
break;
case CUSTOM_AUTHENTICATION_CODE:
Util.processCustomAuthentication(