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.network.ZLNetworkManager;
|
||||
import org.geometerplus.zlibrary.core.network.ZLNetworkException;
|
||||
import org.geometerplus.zlibrary.core.options.ZLStringOption;
|
||||
|
||||
import org.geometerplus.fbreader.network.INetworkLink;
|
||||
import org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager;
|
||||
|
||||
import org.geometerplus.zlibrary.ui.android.R;
|
||||
|
||||
import org.geometerplus.android.util.UIUtil;
|
||||
|
||||
public class AuthenticationActivity extends Activity {
|
||||
private static final String AREA_KEY = "area";
|
||||
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 ERROR_KEY = "error";
|
||||
static final String SHOW_SIGNUP_LINK_KEY = "showSignupLink";
|
||||
static final String CUSTOM_AUTH_KEY = "customAuth";
|
||||
|
||||
static final int RESULT_SIGNUP = RESULT_FIRST_USER;
|
||||
|
||||
|
@ -110,6 +115,7 @@ public class AuthenticationActivity extends Activity {
|
|||
private Button myOkButton;
|
||||
private Timer myOkButtonUpdater;
|
||||
private TextView myUsernameView;
|
||||
private boolean myCustomAuthentication;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle icicle) {
|
||||
|
@ -130,6 +136,7 @@ public class AuthenticationActivity extends Activity {
|
|||
final String username = intent.getStringExtra(USERNAME_KEY);
|
||||
final String error = intent.getStringExtra(ERROR_KEY);
|
||||
final boolean showSignupLink = intent.getBooleanExtra(SHOW_SIGNUP_LINK_KEY, false);
|
||||
myCustomAuthentication = intent.getBooleanExtra(CUSTOM_AUTH_KEY, false);
|
||||
|
||||
myResource = ZLResource.resource("dialog").getResource("AuthenticationDialog");
|
||||
|
||||
|
@ -156,13 +163,7 @@ public class AuthenticationActivity extends Activity {
|
|||
myUsernameView = findTextView(R.id.authentication_username);
|
||||
myUsernameView.setText(username);
|
||||
|
||||
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);
|
||||
}
|
||||
setError(error);
|
||||
|
||||
if (showSignupLink) {
|
||||
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.setOnClickListener(new Button.OnClickListener() {
|
||||
public void onClick(View v) {
|
||||
final Intent data = Util.intentByLink(new Intent(), myLink);
|
||||
data.putExtra(
|
||||
USERNAME_KEY,
|
||||
myUsernameView.getText().toString()
|
||||
);
|
||||
data.putExtra(
|
||||
PASSWORD_KEY,
|
||||
findTextView(R.id.authentication_password).getText().toString()
|
||||
);
|
||||
setResult(RESULT_OK, data);
|
||||
finish();
|
||||
final String username = myUsernameView.getText().toString();
|
||||
final String password = findTextView(R.id.authentication_password).getText().toString();
|
||||
if (myCustomAuthentication) {
|
||||
runCustomAuthentication(username, password);
|
||||
} else {
|
||||
finishOk(username, password);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -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) {
|
||||
return (TextView)findViewById(resourceId);
|
||||
}
|
||||
|
|
|
@ -432,7 +432,7 @@ class NetworkBookActions extends NetworkTreeActions {
|
|||
}
|
||||
} 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) {
|
||||
|
|
|
@ -42,9 +42,6 @@ public class NetworkCatalogActions {
|
|||
|
||||
final NetworkCatalogItem item = catalogTree.Item;
|
||||
switch (actionCode) {
|
||||
case ActionCode.SIGNIN:
|
||||
Util.runAuthenticationDialog(activity, item.Link, null, null);
|
||||
return true;
|
||||
case ActionCode.BASKET_CLEAR:
|
||||
item.Link.basket().clear();
|
||||
return true;
|
||||
|
|
|
@ -259,7 +259,7 @@ public class NetworkLibraryActivity extends BaseActivity implements NetworkView.
|
|||
action.run(tree);
|
||||
break;
|
||||
case B3_UNDEFINED:
|
||||
Util.runAuthenticationDialog(this, item.Link, null, new Runnable() {
|
||||
Util.runAuthenticationDialog(this, item.Link, new Runnable() {
|
||||
public void run() {
|
||||
if (item.getVisibility() != ZLBoolean3.B3_TRUE) {
|
||||
return;
|
||||
|
|
|
@ -156,13 +156,7 @@ public class TopupMenuActivity extends ListActivity implements AdapterView.OnIte
|
|||
if (mgr.mayBeAuthorised(false)) {
|
||||
action.run();
|
||||
} else {
|
||||
Util.runAuthenticationDialog(this, myLink, null, new Runnable() {
|
||||
public void run() {
|
||||
if (mgr.mayBeAuthorised(false)) {
|
||||
runOnUiThread(action);
|
||||
}
|
||||
}
|
||||
});
|
||||
Util.runAuthenticationDialog(this, myLink, action);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -82,7 +82,7 @@ public abstract class Util implements UserRegistrationConstants {
|
|||
private static final Map<Activity,Runnable> ourAfterRegisrationMap =
|
||||
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 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.SCHEME_KEY, "https");
|
||||
intent.putExtra(AuthenticationActivity.ERROR_KEY, error);
|
||||
intent.putExtra(AuthenticationActivity.CUSTOM_AUTH_KEY, true);
|
||||
if (onSuccess != null) {
|
||||
ourAfterRegisrationMap.put(activity, onSuccess);
|
||||
}
|
||||
|
@ -128,39 +128,18 @@ public abstract class Util implements UserRegistrationConstants {
|
|||
);
|
||||
break;
|
||||
case AuthenticationActivity.RESULT_OK:
|
||||
{
|
||||
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() {
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
mgr.authorise(password);
|
||||
if (mgr.needsInitialization()) {
|
||||
mgr.initialize();
|
||||
}
|
||||
if (onSuccess != null) {
|
||||
onSuccess.run();
|
||||
}
|
||||
} catch (ZLNetworkException e) {
|
||||
mgr.logOut();
|
||||
runAuthenticationDialog(activity, link, e.getMessage(), onSuccess);
|
||||
return;
|
||||
if (onSuccess != null) {
|
||||
onSuccess.run();
|
||||
}
|
||||
final NetworkLibrary library = NetworkLibrary.Instance();
|
||||
library.invalidateVisibility();
|
||||
library.synchronize();
|
||||
NetworkView.Instance().fireModelChanged();
|
||||
}
|
||||
};
|
||||
UIUtil.wait("authentication", runnable, activity);
|
||||
});
|
||||
break;
|
||||
}
|
||||
case AuthenticationActivity.RESULT_SIGNUP:
|
||||
Util.runRegistrationDialog(activity, link);
|
||||
break;
|
||||
|
|
|
@ -47,6 +47,6 @@ public class SignInAction extends Action {
|
|||
|
||||
@Override
|
||||
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