mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-04 18:29:23 +02:00
code simplification
This commit is contained in:
parent
1f56a9d831
commit
1ef43b40e2
7 changed files with 67 additions and 58 deletions
|
@ -35,12 +35,16 @@ import org.apache.http.auth.UsernamePasswordCredentials;
|
||||||
|
|
||||||
import org.geometerplus.zlibrary.core.resources.ZLResource;
|
import org.geometerplus.zlibrary.core.resources.ZLResource;
|
||||||
import org.geometerplus.zlibrary.core.network.ZLNetworkManager;
|
import org.geometerplus.zlibrary.core.network.ZLNetworkManager;
|
||||||
|
import org.geometerplus.zlibrary.core.network.ZLNetworkException;
|
||||||
import org.geometerplus.zlibrary.core.options.ZLStringOption;
|
import org.geometerplus.zlibrary.core.options.ZLStringOption;
|
||||||
|
|
||||||
import org.geometerplus.fbreader.network.INetworkLink;
|
import org.geometerplus.fbreader.network.INetworkLink;
|
||||||
|
import org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager;
|
||||||
|
|
||||||
import org.geometerplus.zlibrary.ui.android.R;
|
import org.geometerplus.zlibrary.ui.android.R;
|
||||||
|
|
||||||
|
import org.geometerplus.android.util.UIUtil;
|
||||||
|
|
||||||
public class AuthenticationActivity extends Activity {
|
public class AuthenticationActivity extends Activity {
|
||||||
private static final String AREA_KEY = "area";
|
private static final String AREA_KEY = "area";
|
||||||
private static final String HOST_KEY = "host";
|
private static final String HOST_KEY = "host";
|
||||||
|
@ -49,6 +53,7 @@ public class AuthenticationActivity extends Activity {
|
||||||
static final String PASSWORD_KEY = "password";
|
static final String PASSWORD_KEY = "password";
|
||||||
static final String ERROR_KEY = "error";
|
static final String ERROR_KEY = "error";
|
||||||
static final String SHOW_SIGNUP_LINK_KEY = "showSignupLink";
|
static final String SHOW_SIGNUP_LINK_KEY = "showSignupLink";
|
||||||
|
static final String CUSTOM_AUTH_KEY = "customAuth";
|
||||||
|
|
||||||
static final int RESULT_SIGNUP = RESULT_FIRST_USER;
|
static final int RESULT_SIGNUP = RESULT_FIRST_USER;
|
||||||
|
|
||||||
|
@ -110,6 +115,7 @@ public class AuthenticationActivity extends Activity {
|
||||||
private Button myOkButton;
|
private Button myOkButton;
|
||||||
private Timer myOkButtonUpdater;
|
private Timer myOkButtonUpdater;
|
||||||
private TextView myUsernameView;
|
private TextView myUsernameView;
|
||||||
|
private boolean myCustomAuthentication;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle icicle) {
|
public void onCreate(Bundle icicle) {
|
||||||
|
@ -130,6 +136,7 @@ public class AuthenticationActivity extends Activity {
|
||||||
final String username = intent.getStringExtra(USERNAME_KEY);
|
final String username = intent.getStringExtra(USERNAME_KEY);
|
||||||
final String error = intent.getStringExtra(ERROR_KEY);
|
final String error = intent.getStringExtra(ERROR_KEY);
|
||||||
final boolean showSignupLink = intent.getBooleanExtra(SHOW_SIGNUP_LINK_KEY, false);
|
final boolean showSignupLink = intent.getBooleanExtra(SHOW_SIGNUP_LINK_KEY, false);
|
||||||
|
myCustomAuthentication = intent.getBooleanExtra(CUSTOM_AUTH_KEY, false);
|
||||||
|
|
||||||
myResource = ZLResource.resource("dialog").getResource("AuthenticationDialog");
|
myResource = ZLResource.resource("dialog").getResource("AuthenticationDialog");
|
||||||
|
|
||||||
|
@ -156,13 +163,7 @@ public class AuthenticationActivity extends Activity {
|
||||||
myUsernameView = findTextView(R.id.authentication_username);
|
myUsernameView = findTextView(R.id.authentication_username);
|
||||||
myUsernameView.setText(username);
|
myUsernameView.setText(username);
|
||||||
|
|
||||||
final TextView errorView = findTextView(R.id.authentication_error);
|
setError(error);
|
||||||
if (error != null && !"".equals(error)) {
|
|
||||||
errorView.setVisibility(View.VISIBLE);
|
|
||||||
errorView.setText(error);
|
|
||||||
} else {
|
|
||||||
errorView.setVisibility(View.GONE);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (showSignupLink) {
|
if (showSignupLink) {
|
||||||
findViewById(R.id.authentication_signup_box).setVisibility(View.VISIBLE);
|
findViewById(R.id.authentication_signup_box).setVisibility(View.VISIBLE);
|
||||||
|
@ -184,17 +185,13 @@ public class AuthenticationActivity extends Activity {
|
||||||
myOkButton.setText(buttonResource.getResource("ok").getValue());
|
myOkButton.setText(buttonResource.getResource("ok").getValue());
|
||||||
myOkButton.setOnClickListener(new Button.OnClickListener() {
|
myOkButton.setOnClickListener(new Button.OnClickListener() {
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
final Intent data = Util.intentByLink(new Intent(), myLink);
|
final String username = myUsernameView.getText().toString();
|
||||||
data.putExtra(
|
final String password = findTextView(R.id.authentication_password).getText().toString();
|
||||||
USERNAME_KEY,
|
if (myCustomAuthentication) {
|
||||||
myUsernameView.getText().toString()
|
runCustomAuthentication(username, password);
|
||||||
);
|
} else {
|
||||||
data.putExtra(
|
finishOk(username, password);
|
||||||
PASSWORD_KEY,
|
}
|
||||||
findTextView(R.id.authentication_password).getText().toString()
|
|
||||||
);
|
|
||||||
setResult(RESULT_OK, data);
|
|
||||||
finish();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -207,6 +204,48 @@ public class AuthenticationActivity extends Activity {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setError(String error) {
|
||||||
|
final TextView errorView = findTextView(R.id.authentication_error);
|
||||||
|
if (error != null && !"".equals(error)) {
|
||||||
|
errorView.setVisibility(View.VISIBLE);
|
||||||
|
errorView.setText(error);
|
||||||
|
} else {
|
||||||
|
errorView.setVisibility(View.GONE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
finish();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void runCustomAuthentication(final String username, final String password) {
|
||||||
|
final NetworkAuthenticationManager mgr = myLink.authenticationManager();
|
||||||
|
mgr.UserNameOption.setValue(username);
|
||||||
|
final Runnable runnable = new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
mgr.authorise(password);
|
||||||
|
if (mgr.needsInitialization()) {
|
||||||
|
mgr.initialize();
|
||||||
|
}
|
||||||
|
finishOk(username, password);
|
||||||
|
} catch (final ZLNetworkException e) {
|
||||||
|
mgr.logOut();
|
||||||
|
runOnUiThread(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
setError(e.getMessage());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
UIUtil.wait("authentication", runnable, this);
|
||||||
|
}
|
||||||
|
|
||||||
private TextView findTextView(int resourceId) {
|
private TextView findTextView(int resourceId) {
|
||||||
return (TextView)findViewById(resourceId);
|
return (TextView)findViewById(resourceId);
|
||||||
}
|
}
|
||||||
|
|
|
@ -432,7 +432,7 @@ class NetworkBookActions extends NetworkTreeActions {
|
||||||
}
|
}
|
||||||
} catch (ZLNetworkException e) {
|
} catch (ZLNetworkException e) {
|
||||||
}
|
}
|
||||||
Util.runAuthenticationDialog(activity, book.Link, null, buyOnUiRunnable);
|
Util.runAuthenticationDialog(activity, book.Link, buyOnUiRunnable);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void doBuyInBrowser(Activity activity, final NetworkBookItem book) {
|
private static void doBuyInBrowser(Activity activity, final NetworkBookItem book) {
|
||||||
|
|
|
@ -42,9 +42,6 @@ public class NetworkCatalogActions {
|
||||||
|
|
||||||
final NetworkCatalogItem item = catalogTree.Item;
|
final NetworkCatalogItem item = catalogTree.Item;
|
||||||
switch (actionCode) {
|
switch (actionCode) {
|
||||||
case ActionCode.SIGNIN:
|
|
||||||
Util.runAuthenticationDialog(activity, item.Link, null, null);
|
|
||||||
return true;
|
|
||||||
case ActionCode.BASKET_CLEAR:
|
case ActionCode.BASKET_CLEAR:
|
||||||
item.Link.basket().clear();
|
item.Link.basket().clear();
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -259,7 +259,7 @@ public class NetworkLibraryActivity extends BaseActivity implements NetworkView.
|
||||||
action.run(tree);
|
action.run(tree);
|
||||||
break;
|
break;
|
||||||
case B3_UNDEFINED:
|
case B3_UNDEFINED:
|
||||||
Util.runAuthenticationDialog(this, item.Link, null, new Runnable() {
|
Util.runAuthenticationDialog(this, item.Link, new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
if (item.getVisibility() != ZLBoolean3.B3_TRUE) {
|
if (item.getVisibility() != ZLBoolean3.B3_TRUE) {
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -156,13 +156,7 @@ public class TopupMenuActivity extends ListActivity implements AdapterView.OnIte
|
||||||
if (mgr.mayBeAuthorised(false)) {
|
if (mgr.mayBeAuthorised(false)) {
|
||||||
action.run();
|
action.run();
|
||||||
} else {
|
} else {
|
||||||
Util.runAuthenticationDialog(this, myLink, null, new Runnable() {
|
Util.runAuthenticationDialog(this, myLink, action);
|
||||||
public void run() {
|
|
||||||
if (mgr.mayBeAuthorised(false)) {
|
|
||||||
runOnUiThread(action);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -82,7 +82,7 @@ public abstract class Util implements UserRegistrationConstants {
|
||||||
private static final Map<Activity,Runnable> ourAfterRegisrationMap =
|
private static final Map<Activity,Runnable> ourAfterRegisrationMap =
|
||||||
new HashMap<Activity,Runnable>();
|
new HashMap<Activity,Runnable>();
|
||||||
|
|
||||||
public static void runAuthenticationDialog(Activity activity, INetworkLink link, String error, Runnable onSuccess) {
|
public static void runAuthenticationDialog(Activity activity, INetworkLink link, Runnable onSuccess) {
|
||||||
final NetworkAuthenticationManager mgr = link.authenticationManager();
|
final NetworkAuthenticationManager mgr = link.authenticationManager();
|
||||||
|
|
||||||
final Intent intent = intentByLink(new Intent(activity, AuthenticationActivity.class), link);
|
final Intent intent = intentByLink(new Intent(activity, AuthenticationActivity.class), link);
|
||||||
|
@ -91,7 +91,7 @@ public abstract class Util implements UserRegistrationConstants {
|
||||||
intent.putExtra(AuthenticationActivity.SHOW_SIGNUP_LINK_KEY, true);
|
intent.putExtra(AuthenticationActivity.SHOW_SIGNUP_LINK_KEY, true);
|
||||||
}
|
}
|
||||||
intent.putExtra(AuthenticationActivity.SCHEME_KEY, "https");
|
intent.putExtra(AuthenticationActivity.SCHEME_KEY, "https");
|
||||||
intent.putExtra(AuthenticationActivity.ERROR_KEY, error);
|
intent.putExtra(AuthenticationActivity.CUSTOM_AUTH_KEY, true);
|
||||||
if (onSuccess != null) {
|
if (onSuccess != null) {
|
||||||
ourAfterRegisrationMap.put(activity, onSuccess);
|
ourAfterRegisrationMap.put(activity, onSuccess);
|
||||||
}
|
}
|
||||||
|
@ -128,39 +128,18 @@ public abstract class Util implements UserRegistrationConstants {
|
||||||
);
|
);
|
||||||
break;
|
break;
|
||||||
case AuthenticationActivity.RESULT_OK:
|
case AuthenticationActivity.RESULT_OK:
|
||||||
{
|
activity.runOnUiThread(new Runnable() {
|
||||||
final ZLResource resource =
|
|
||||||
ZLResource.resource("dialog").getResource("AuthenticationDialog");
|
|
||||||
final String username =
|
|
||||||
data.getStringExtra(AuthenticationActivity.USERNAME_KEY);
|
|
||||||
final String password =
|
|
||||||
data.getStringExtra(AuthenticationActivity.PASSWORD_KEY);
|
|
||||||
final NetworkAuthenticationManager mgr = link.authenticationManager();
|
|
||||||
mgr.UserNameOption.setValue(username);
|
|
||||||
final Runnable runnable = new Runnable() {
|
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
if (onSuccess != null) {
|
||||||
mgr.authorise(password);
|
onSuccess.run();
|
||||||
if (mgr.needsInitialization()) {
|
|
||||||
mgr.initialize();
|
|
||||||
}
|
|
||||||
if (onSuccess != null) {
|
|
||||||
onSuccess.run();
|
|
||||||
}
|
|
||||||
} catch (ZLNetworkException e) {
|
|
||||||
mgr.logOut();
|
|
||||||
runAuthenticationDialog(activity, link, e.getMessage(), onSuccess);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
final NetworkLibrary library = NetworkLibrary.Instance();
|
final NetworkLibrary library = NetworkLibrary.Instance();
|
||||||
library.invalidateVisibility();
|
library.invalidateVisibility();
|
||||||
library.synchronize();
|
library.synchronize();
|
||||||
NetworkView.Instance().fireModelChanged();
|
NetworkView.Instance().fireModelChanged();
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
UIUtil.wait("authentication", runnable, activity);
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
case AuthenticationActivity.RESULT_SIGNUP:
|
case AuthenticationActivity.RESULT_SIGNUP:
|
||||||
Util.runRegistrationDialog(activity, link);
|
Util.runRegistrationDialog(activity, link);
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -47,6 +47,6 @@ public class SignInAction extends Action {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(NetworkTree tree) {
|
public void run(NetworkTree tree) {
|
||||||
Util.runAuthenticationDialog(myActivity, ((NetworkCatalogTree)tree).Item.Link, null, null);
|
Util.runAuthenticationDialog(myActivity, ((NetworkCatalogTree)tree).Item.Link, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue