mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-04 18:29:23 +02:00
runnable map has been moved from Util to AuthenticationActivity
This commit is contained in:
parent
1ef43b40e2
commit
b4a219aefe
2 changed files with 40 additions and 42 deletions
|
@ -39,6 +39,7 @@ 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.NetworkLibrary;
|
||||
import org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager;
|
||||
|
||||
import org.geometerplus.zlibrary.ui.android.R;
|
||||
|
@ -46,8 +47,24 @@ import org.geometerplus.zlibrary.ui.android.R;
|
|||
import org.geometerplus.android.util.UIUtil;
|
||||
|
||||
public class AuthenticationActivity extends Activity {
|
||||
private static final Map<Long,Runnable> ourOnSuccessRunnableMap =
|
||||
Collections.synchronizedMap(new HashMap<Long,Runnable>());
|
||||
private static volatile long ourNextCode;
|
||||
|
||||
static Intent registerRunnable(Intent intent, Runnable action) {
|
||||
synchronized (ourOnSuccessRunnableMap) {
|
||||
if (action != null) {
|
||||
ourOnSuccessRunnableMap.put(ourNextCode, action);
|
||||
intent.putExtra(RUNNABLE_KEY, ourNextCode);
|
||||
++ourNextCode;
|
||||
}
|
||||
}
|
||||
return intent;
|
||||
}
|
||||
|
||||
private static final String AREA_KEY = "area";
|
||||
private static final String HOST_KEY = "host";
|
||||
private static final String RUNNABLE_KEY = "onSuccess";
|
||||
static final String SCHEME_KEY = "scheme";
|
||||
static final String USERNAME_KEY = "username";
|
||||
static final String PASSWORD_KEY = "password";
|
||||
|
@ -116,6 +133,7 @@ public class AuthenticationActivity extends Activity {
|
|||
private Timer myOkButtonUpdater;
|
||||
private TextView myUsernameView;
|
||||
private boolean myCustomAuthentication;
|
||||
private Runnable myOnSuccessRunnable;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle icicle) {
|
||||
|
@ -137,6 +155,7 @@ public class AuthenticationActivity extends Activity {
|
|||
final String error = intent.getStringExtra(ERROR_KEY);
|
||||
final boolean showSignupLink = intent.getBooleanExtra(SHOW_SIGNUP_LINK_KEY, false);
|
||||
myCustomAuthentication = intent.getBooleanExtra(CUSTOM_AUTH_KEY, false);
|
||||
myOnSuccessRunnable = ourOnSuccessRunnableMap.remove(intent.getLongExtra(RUNNABLE_KEY, -1));
|
||||
|
||||
myResource = ZLResource.resource("dialog").getResource("AuthenticationDialog");
|
||||
|
||||
|
@ -199,6 +218,18 @@ public class AuthenticationActivity extends Activity {
|
|||
cancelButton.setText(buttonResource.getResource("cancel").getValue());
|
||||
cancelButton.setOnClickListener(new Button.OnClickListener() {
|
||||
public void onClick(View v) {
|
||||
runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
final NetworkAuthenticationManager mgr = myLink.authenticationManager();
|
||||
if (mgr.mayBeAuthorised(false)) {
|
||||
mgr.logOut();
|
||||
}
|
||||
final NetworkLibrary library = NetworkLibrary.Instance();
|
||||
library.invalidateVisibility();
|
||||
library.synchronize();
|
||||
NetworkView.Instance().fireModelChanged();
|
||||
}
|
||||
});
|
||||
finish();
|
||||
}
|
||||
});
|
||||
|
@ -233,6 +264,13 @@ public class AuthenticationActivity extends Activity {
|
|||
mgr.initialize();
|
||||
}
|
||||
finishOk(username, password);
|
||||
if (myOnSuccessRunnable != null) {
|
||||
myOnSuccessRunnable.run();
|
||||
}
|
||||
final NetworkLibrary library = NetworkLibrary.Instance();
|
||||
library.invalidateVisibility();
|
||||
library.synchronize();
|
||||
NetworkView.Instance().fireModelChanged();
|
||||
} catch (final ZLNetworkException e) {
|
||||
mgr.logOut();
|
||||
runOnUiThread(new Runnable() {
|
||||
|
|
|
@ -79,22 +79,17 @@ 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, Runnable onSuccess) {
|
||||
final NetworkAuthenticationManager mgr = link.authenticationManager();
|
||||
|
||||
final Intent intent = intentByLink(new Intent(activity, AuthenticationActivity.class), link);
|
||||
AuthenticationActivity.registerRunnable(intent, onSuccess);
|
||||
intent.putExtra(AuthenticationActivity.USERNAME_KEY, mgr.UserNameOption.getValue());
|
||||
if (isRegistrationSupported(activity, link)) {
|
||||
intent.putExtra(AuthenticationActivity.SHOW_SIGNUP_LINK_KEY, true);
|
||||
}
|
||||
intent.putExtra(AuthenticationActivity.SCHEME_KEY, "https");
|
||||
intent.putExtra(AuthenticationActivity.CUSTOM_AUTH_KEY, true);
|
||||
if (onSuccess != null) {
|
||||
ourAfterRegisrationMap.put(activity, onSuccess);
|
||||
}
|
||||
activity.startActivityForResult(intent, NetworkLibraryActivity.CUSTOM_AUTHENTICATION_CODE);
|
||||
}
|
||||
|
||||
|
@ -104,44 +99,9 @@ public abstract class Util implements UserRegistrationConstants {
|
|||
return;
|
||||
}
|
||||
|
||||
final Runnable onSuccess = ourAfterRegisrationMap.get(activity);
|
||||
ourAfterRegisrationMap.remove(activity);
|
||||
|
||||
switch (resultCode) {
|
||||
case AuthenticationActivity.RESULT_CANCELED:
|
||||
UIUtil.wait(
|
||||
"signOut",
|
||||
new Runnable() {
|
||||
public void run() {
|
||||
final NetworkAuthenticationManager mgr =
|
||||
link.authenticationManager();
|
||||
if (mgr.mayBeAuthorised(false)) {
|
||||
mgr.logOut();
|
||||
}
|
||||
final NetworkLibrary library = NetworkLibrary.Instance();
|
||||
library.invalidateVisibility();
|
||||
library.synchronize();
|
||||
NetworkView.Instance().fireModelChanged();
|
||||
}
|
||||
},
|
||||
activity
|
||||
);
|
||||
break;
|
||||
case AuthenticationActivity.RESULT_OK:
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
if (onSuccess != null) {
|
||||
onSuccess.run();
|
||||
}
|
||||
final NetworkLibrary library = NetworkLibrary.Instance();
|
||||
library.invalidateVisibility();
|
||||
library.synchronize();
|
||||
NetworkView.Instance().fireModelChanged();
|
||||
}
|
||||
});
|
||||
break;
|
||||
case AuthenticationActivity.RESULT_SIGNUP:
|
||||
Util.runRegistrationDialog(activity, link);
|
||||
runRegistrationDialog(activity, link);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue