mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-04 02:09:35 +02:00
updated credential creator logic
This commit is contained in:
parent
10a3765835
commit
1b97558c55
4 changed files with 45 additions and 59 deletions
|
@ -22,6 +22,7 @@ package org.geometerplus.android.fbreader.network;
|
|||
import java.util.*;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
|
@ -66,34 +67,30 @@ public class AuthenticationActivity extends Activity {
|
|||
static final String ERROR_KEY = "error";
|
||||
static final String CUSTOM_AUTH_KEY = "customAuth";
|
||||
|
||||
static class CredentialsCreator extends ZLNetworkManager.BasicCredentialsCreator {
|
||||
private final Activity myActivity;
|
||||
private final int myCode;
|
||||
|
||||
CredentialsCreator(Activity activity, int code) {
|
||||
myActivity = activity;
|
||||
myCode = code;
|
||||
static void initCredentialsCreator(Context context) {
|
||||
final ZLNetworkManager manager = ZLNetworkManager.Instance();
|
||||
if (manager.getCredentialsCreator() == null) {
|
||||
manager.setCredentialsCreator(new CredentialsCreator(context));
|
||||
}
|
||||
}
|
||||
|
||||
synchronized void onDataReceived(int resultCode, Intent data) {
|
||||
if (resultCode == RESULT_OK && data != null) {
|
||||
setCredentials(
|
||||
data.getStringExtra(USERNAME_KEY),
|
||||
data.getStringExtra(PASSWORD_KEY)
|
||||
);
|
||||
}
|
||||
notify();
|
||||
static class CredentialsCreator extends ZLNetworkManager.BasicCredentialsCreator {
|
||||
private final Context myContext;
|
||||
|
||||
CredentialsCreator(Context context) {
|
||||
myContext = context.getApplicationContext();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void startAuthenticationDialog(String host, String area, String scheme, String username) {
|
||||
final Intent intent = new Intent();
|
||||
intent.setClass(myActivity, AuthenticationActivity.class);
|
||||
intent.setClass(myContext, AuthenticationActivity.class);
|
||||
intent.putExtra(HOST_KEY, host);
|
||||
intent.putExtra(AREA_KEY, area);
|
||||
intent.putExtra(SCHEME_KEY, scheme);
|
||||
intent.putExtra(USERNAME_KEY, username);
|
||||
myActivity.startActivityForResult(intent, myCode);
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
myContext.startActivity(intent);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -211,10 +208,11 @@ public class AuthenticationActivity extends Activity {
|
|||
}
|
||||
|
||||
private void finishOk(String username, String password) {
|
||||
final Intent data = Util.intentByLink(new Intent(), myLink);
|
||||
data.putExtra(USERNAME_KEY, username);
|
||||
data.putExtra(PASSWORD_KEY, password);
|
||||
setResult(RESULT_OK, data);
|
||||
final ZLNetworkManager.CredentialsCreator creator =
|
||||
ZLNetworkManager.Instance().getCredentialsCreator();
|
||||
if (creator instanceof CredentialsCreator) {
|
||||
((CredentialsCreator)creator).setCredentials(username, password);
|
||||
}
|
||||
finish();
|
||||
}
|
||||
|
||||
|
@ -277,4 +275,14 @@ public class AuthenticationActivity extends Activity {
|
|||
}
|
||||
super.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
final ZLNetworkManager.CredentialsCreator creator =
|
||||
ZLNetworkManager.Instance().getCredentialsCreator();
|
||||
if (creator instanceof CredentialsCreator) {
|
||||
((CredentialsCreator)creator).release();
|
||||
}
|
||||
super.onStop();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue