1
0
Fork 0
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:
Nikolay Pultsin 2011-12-15 06:02:35 +00:00
parent 10a3765835
commit 1b97558c55
4 changed files with 45 additions and 59 deletions

View file

@ -30,7 +30,6 @@ import android.widget.TextView;
import org.geometerplus.zlibrary.core.resources.ZLResource;
import org.geometerplus.zlibrary.core.network.ZLNetworkException;
import org.geometerplus.zlibrary.core.network.ZLNetworkManager;
import org.geometerplus.zlibrary.ui.android.R;
@ -43,8 +42,6 @@ import org.geometerplus.android.util.UIUtil;
public class AddCustomCatalogActivity extends Activity {
public static String EDIT_KEY = "EditNotAdd";
protected static final int BASIC_AUTHENTICATION_CODE = 1;
private ZLResource myResource;
private volatile ICustomNetworkLink myLink;
private boolean myEditNotAdd;
@ -53,6 +50,9 @@ public class AddCustomCatalogActivity extends Activity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Thread.setDefaultUncaughtExceptionHandler(new org.geometerplus.zlibrary.ui.android.library.UncaughtExceptionHandler(this));
AuthenticationActivity.initCredentialsCreator(this);
setContentView(R.layout.add_custom_catalog);
myResource = ZLResource.resource("dialog").getResource("CustomCatalogDialog");
@ -263,22 +263,4 @@ public class AddCustomCatalogActivity extends Activity {
};
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);
}
}

View file

@ -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 void initCredentialsCreator(Context context) {
final ZLNetworkManager manager = ZLNetworkManager.Instance();
if (manager.getCredentialsCreator() == null) {
manager.setCredentialsCreator(new CredentialsCreator(context));
}
}
static class CredentialsCreator extends ZLNetworkManager.BasicCredentialsCreator {
private final Activity myActivity;
private final int myCode;
private final Context myContext;
CredentialsCreator(Activity activity, int code) {
myActivity = activity;
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();
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();
}
}

View file

@ -48,8 +48,6 @@ import org.geometerplus.android.util.UIUtil;
public class NetworkLibraryActivity extends TreeActivity implements NetworkLibrary.ChangeListener {
static final String OPEN_CATALOG_ACTION = "android.fbreader.action.OPEN_NETWORK_CATALOG";
protected static final int BASIC_AUTHENTICATION_CODE = 1;
BookDownloaderServiceConnection Connection;
final List<Action> myOptionsMenuActions = new ArrayList<Action>();
@ -62,6 +60,8 @@ public class NetworkLibraryActivity extends TreeActivity implements NetworkLibra
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
AuthenticationActivity.initCredentialsCreator(this);
SQLiteCookieDatabase.init(this);
Connection = new BookDownloaderServiceConnection();
@ -109,7 +109,6 @@ public class NetworkLibraryActivity extends TreeActivity implements NetworkLibra
super.onResume();
getListView().setOnCreateContextMenuListener(this);
NetworkLibrary.Instance().fireModelChangedEvent(NetworkLibrary.ChangeListener.Code.SomeCode);
ZLNetworkManager.Instance().setCredentialsCreator(myCredentialsCreator);
}
@Override
@ -276,18 +275,6 @@ public class NetworkLibraryActivity extends TreeActivity implements NetworkLibra
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
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);

View file

@ -60,9 +60,14 @@ public class ZLNetworkManager {
private volatile String myUsername;
private volatile String myPassword;
synchronized protected void setCredentials(String username, String password) {
synchronized public void setCredentials(String username, String password) {
myUsername = username;
myPassword = password;
release();
}
synchronized public void release() {
notifyAll();
}
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);
}
private CredentialsCreator myCredentialsCreator;
private volatile CredentialsCreator myCredentialsCreator;
private class MyCredentialsProvider extends BasicCredentialsProvider {
private final HttpUriRequest myRequest;
@ -211,6 +216,10 @@ public class ZLNetworkManager {
myCredentialsCreator = creator;
}
public CredentialsCreator getCredentialsCreator() {
return myCredentialsCreator;
}
public void perform(ZLNetworkRequest request) throws ZLNetworkException {
boolean success = false;
DefaultHttpClient httpClient = null;