mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-04 10:19:33 +02:00
updated credential creator logic
This commit is contained in:
parent
10a3765835
commit
1b97558c55
4 changed files with 45 additions and 59 deletions
|
@ -30,7 +30,6 @@ import android.widget.TextView;
|
||||||
|
|
||||||
import org.geometerplus.zlibrary.core.resources.ZLResource;
|
import org.geometerplus.zlibrary.core.resources.ZLResource;
|
||||||
import org.geometerplus.zlibrary.core.network.ZLNetworkException;
|
import org.geometerplus.zlibrary.core.network.ZLNetworkException;
|
||||||
import org.geometerplus.zlibrary.core.network.ZLNetworkManager;
|
|
||||||
|
|
||||||
import org.geometerplus.zlibrary.ui.android.R;
|
import org.geometerplus.zlibrary.ui.android.R;
|
||||||
|
|
||||||
|
@ -43,8 +42,6 @@ import org.geometerplus.android.util.UIUtil;
|
||||||
public class AddCustomCatalogActivity extends Activity {
|
public class AddCustomCatalogActivity extends Activity {
|
||||||
public static String EDIT_KEY = "EditNotAdd";
|
public static String EDIT_KEY = "EditNotAdd";
|
||||||
|
|
||||||
protected static final int BASIC_AUTHENTICATION_CODE = 1;
|
|
||||||
|
|
||||||
private ZLResource myResource;
|
private ZLResource myResource;
|
||||||
private volatile ICustomNetworkLink myLink;
|
private volatile ICustomNetworkLink myLink;
|
||||||
private boolean myEditNotAdd;
|
private boolean myEditNotAdd;
|
||||||
|
@ -53,6 +50,9 @@ public class AddCustomCatalogActivity extends Activity {
|
||||||
public void onCreate(Bundle icicle) {
|
public void onCreate(Bundle icicle) {
|
||||||
super.onCreate(icicle);
|
super.onCreate(icicle);
|
||||||
Thread.setDefaultUncaughtExceptionHandler(new org.geometerplus.zlibrary.ui.android.library.UncaughtExceptionHandler(this));
|
Thread.setDefaultUncaughtExceptionHandler(new org.geometerplus.zlibrary.ui.android.library.UncaughtExceptionHandler(this));
|
||||||
|
|
||||||
|
AuthenticationActivity.initCredentialsCreator(this);
|
||||||
|
|
||||||
setContentView(R.layout.add_custom_catalog);
|
setContentView(R.layout.add_custom_catalog);
|
||||||
|
|
||||||
myResource = ZLResource.resource("dialog").getResource("CustomCatalogDialog");
|
myResource = ZLResource.resource("dialog").getResource("CustomCatalogDialog");
|
||||||
|
@ -263,22 +263,4 @@ public class AddCustomCatalogActivity extends Activity {
|
||||||
};
|
};
|
||||||
UIUtil.wait("loadingCatalogInfo", loadInfoRunnable, this);
|
UIUtil.wait("loadingCatalogInfo", loadInfoRunnable, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private final AuthenticationActivity.CredentialsCreator myCredentialsCreator =
|
|
||||||
new AuthenticationActivity.CredentialsCreator(this, BASIC_AUTHENTICATION_CODE);
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
|
|
||||||
switch (requestCode) {
|
|
||||||
case BASIC_AUTHENTICATION_CODE:
|
|
||||||
myCredentialsCreator.onDataReceived(resultCode, intent);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onResume() {
|
|
||||||
super.onResume();
|
|
||||||
ZLNetworkManager.Instance().setCredentialsCreator(myCredentialsCreator);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,7 @@ package org.geometerplus.android.fbreader.network;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
@ -66,34 +67,30 @@ public class AuthenticationActivity extends Activity {
|
||||||
static final String ERROR_KEY = "error";
|
static final String ERROR_KEY = "error";
|
||||||
static final String CUSTOM_AUTH_KEY = "customAuth";
|
static final String CUSTOM_AUTH_KEY = "customAuth";
|
||||||
|
|
||||||
|
static void initCredentialsCreator(Context context) {
|
||||||
|
final ZLNetworkManager manager = ZLNetworkManager.Instance();
|
||||||
|
if (manager.getCredentialsCreator() == null) {
|
||||||
|
manager.setCredentialsCreator(new CredentialsCreator(context));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static class CredentialsCreator extends ZLNetworkManager.BasicCredentialsCreator {
|
static class CredentialsCreator extends ZLNetworkManager.BasicCredentialsCreator {
|
||||||
private final Activity myActivity;
|
private final Context myContext;
|
||||||
private final int myCode;
|
|
||||||
|
|
||||||
CredentialsCreator(Activity activity, int code) {
|
CredentialsCreator(Context context) {
|
||||||
myActivity = activity;
|
myContext = context.getApplicationContext();
|
||||||
myCode = code;
|
|
||||||
}
|
|
||||||
|
|
||||||
synchronized void onDataReceived(int resultCode, Intent data) {
|
|
||||||
if (resultCode == RESULT_OK && data != null) {
|
|
||||||
setCredentials(
|
|
||||||
data.getStringExtra(USERNAME_KEY),
|
|
||||||
data.getStringExtra(PASSWORD_KEY)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
notify();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void startAuthenticationDialog(String host, String area, String scheme, String username) {
|
protected void startAuthenticationDialog(String host, String area, String scheme, String username) {
|
||||||
final Intent intent = new Intent();
|
final Intent intent = new Intent();
|
||||||
intent.setClass(myActivity, AuthenticationActivity.class);
|
intent.setClass(myContext, AuthenticationActivity.class);
|
||||||
intent.putExtra(HOST_KEY, host);
|
intent.putExtra(HOST_KEY, host);
|
||||||
intent.putExtra(AREA_KEY, area);
|
intent.putExtra(AREA_KEY, area);
|
||||||
intent.putExtra(SCHEME_KEY, scheme);
|
intent.putExtra(SCHEME_KEY, scheme);
|
||||||
intent.putExtra(USERNAME_KEY, username);
|
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) {
|
private void finishOk(String username, String password) {
|
||||||
final Intent data = Util.intentByLink(new Intent(), myLink);
|
final ZLNetworkManager.CredentialsCreator creator =
|
||||||
data.putExtra(USERNAME_KEY, username);
|
ZLNetworkManager.Instance().getCredentialsCreator();
|
||||||
data.putExtra(PASSWORD_KEY, password);
|
if (creator instanceof CredentialsCreator) {
|
||||||
setResult(RESULT_OK, data);
|
((CredentialsCreator)creator).setCredentials(username, password);
|
||||||
|
}
|
||||||
finish();
|
finish();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -277,4 +275,14 @@ public class AuthenticationActivity extends Activity {
|
||||||
}
|
}
|
||||||
super.onPause();
|
super.onPause();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onStop() {
|
||||||
|
final ZLNetworkManager.CredentialsCreator creator =
|
||||||
|
ZLNetworkManager.Instance().getCredentialsCreator();
|
||||||
|
if (creator instanceof CredentialsCreator) {
|
||||||
|
((CredentialsCreator)creator).release();
|
||||||
|
}
|
||||||
|
super.onStop();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,8 +48,6 @@ import org.geometerplus.android.util.UIUtil;
|
||||||
public class NetworkLibraryActivity extends TreeActivity implements NetworkLibrary.ChangeListener {
|
public class NetworkLibraryActivity extends TreeActivity implements NetworkLibrary.ChangeListener {
|
||||||
static final String OPEN_CATALOG_ACTION = "android.fbreader.action.OPEN_NETWORK_CATALOG";
|
static final String OPEN_CATALOG_ACTION = "android.fbreader.action.OPEN_NETWORK_CATALOG";
|
||||||
|
|
||||||
protected static final int BASIC_AUTHENTICATION_CODE = 1;
|
|
||||||
|
|
||||||
BookDownloaderServiceConnection Connection;
|
BookDownloaderServiceConnection Connection;
|
||||||
|
|
||||||
final List<Action> myOptionsMenuActions = new ArrayList<Action>();
|
final List<Action> myOptionsMenuActions = new ArrayList<Action>();
|
||||||
|
@ -62,6 +60,8 @@ public class NetworkLibraryActivity extends TreeActivity implements NetworkLibra
|
||||||
public void onCreate(Bundle icicle) {
|
public void onCreate(Bundle icicle) {
|
||||||
super.onCreate(icicle);
|
super.onCreate(icicle);
|
||||||
|
|
||||||
|
AuthenticationActivity.initCredentialsCreator(this);
|
||||||
|
|
||||||
SQLiteCookieDatabase.init(this);
|
SQLiteCookieDatabase.init(this);
|
||||||
|
|
||||||
Connection = new BookDownloaderServiceConnection();
|
Connection = new BookDownloaderServiceConnection();
|
||||||
|
@ -109,7 +109,6 @@ public class NetworkLibraryActivity extends TreeActivity implements NetworkLibra
|
||||||
super.onResume();
|
super.onResume();
|
||||||
getListView().setOnCreateContextMenuListener(this);
|
getListView().setOnCreateContextMenuListener(this);
|
||||||
NetworkLibrary.Instance().fireModelChangedEvent(NetworkLibrary.ChangeListener.Code.SomeCode);
|
NetworkLibrary.Instance().fireModelChangedEvent(NetworkLibrary.ChangeListener.Code.SomeCode);
|
||||||
ZLNetworkManager.Instance().setCredentialsCreator(myCredentialsCreator);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -276,18 +275,6 @@ public class NetworkLibraryActivity extends TreeActivity implements NetworkLibra
|
||||||
listView.showContextMenuForChild(view);
|
listView.showContextMenuForChild(view);
|
||||||
}
|
}
|
||||||
|
|
||||||
private final AuthenticationActivity.CredentialsCreator myCredentialsCreator =
|
|
||||||
new AuthenticationActivity.CredentialsCreator(this, BASIC_AUTHENTICATION_CODE);
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
|
|
||||||
switch (requestCode) {
|
|
||||||
case BASIC_AUTHENTICATION_CODE:
|
|
||||||
myCredentialsCreator.onDataReceived(resultCode, intent);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
super.onCreateOptionsMenu(menu);
|
super.onCreateOptionsMenu(menu);
|
||||||
|
|
|
@ -60,9 +60,14 @@ public class ZLNetworkManager {
|
||||||
private volatile String myUsername;
|
private volatile String myUsername;
|
||||||
private volatile String myPassword;
|
private volatile String myPassword;
|
||||||
|
|
||||||
synchronized protected void setCredentials(String username, String password) {
|
synchronized public void setCredentials(String username, String password) {
|
||||||
myUsername = username;
|
myUsername = username;
|
||||||
myPassword = password;
|
myPassword = password;
|
||||||
|
release();
|
||||||
|
}
|
||||||
|
|
||||||
|
synchronized public void release() {
|
||||||
|
notifyAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Credentials createCredentials(String scheme, AuthScope scope) {
|
public Credentials createCredentials(String scheme, AuthScope scope) {
|
||||||
|
@ -95,7 +100,7 @@ public class ZLNetworkManager {
|
||||||
abstract protected void startAuthenticationDialog(String host, String area, String scheme, String username);
|
abstract protected void startAuthenticationDialog(String host, String area, String scheme, String username);
|
||||||
}
|
}
|
||||||
|
|
||||||
private CredentialsCreator myCredentialsCreator;
|
private volatile CredentialsCreator myCredentialsCreator;
|
||||||
|
|
||||||
private class MyCredentialsProvider extends BasicCredentialsProvider {
|
private class MyCredentialsProvider extends BasicCredentialsProvider {
|
||||||
private final HttpUriRequest myRequest;
|
private final HttpUriRequest myRequest;
|
||||||
|
@ -211,6 +216,10 @@ public class ZLNetworkManager {
|
||||||
myCredentialsCreator = creator;
|
myCredentialsCreator = creator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public CredentialsCreator getCredentialsCreator() {
|
||||||
|
return myCredentialsCreator;
|
||||||
|
}
|
||||||
|
|
||||||
public void perform(ZLNetworkRequest request) throws ZLNetworkException {
|
public void perform(ZLNetworkRequest request) throws ZLNetworkException {
|
||||||
boolean success = false;
|
boolean success = false;
|
||||||
DefaultHttpClient httpClient = null;
|
DefaultHttpClient httpClient = null;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue