1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-04 18:29:23 +02:00

AuthenticationDialog -> AuthenticationActivity (finished)

This commit is contained in:
Nikolay Pultsin 2011-04-24 08:26:09 +01:00
parent d203aa606c
commit eb8403bdd2
11 changed files with 136 additions and 431 deletions

View file

@ -1,5 +1,6 @@
===== 1.0.10 (??? ??, 2011) =====
* Fixed book downloading service (book-by-link-downloading works again)
* Old authentication dialog has gone
===== 1.0.9 (Apr 23, 2011) =====
* HttpConnection => HttpClient (that fixes a bug in SSL connection)

View file

@ -10,4 +10,4 @@
# Project target.
target=android-8
java.encoding=utf-8
#proguard.config=proguard.cfg
proguard.config=proguard.cfg

View file

@ -1,69 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/network_authentication_dialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:orientation="vertical"
>
<TextView
android:id="@+id/network_authentication_login_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
/>
<EditText
android:id="@+id/network_authentication_login"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
/>
<TextView
android:id="@+id/network_authentication_password_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:layout_marginTop="5dp"
/>
<EditText
android:id="@+id/network_authentication_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:password="true"
/>
<TextView
android:id="@+id/network_authentication_error"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:textColor="#ff0000"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="5dp"
android:paddingBottom="5dp"
>
<TextView
android:id="@+id/network_authentication_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="#0000ff"
android:textStyle="bold"
android:clickable="true"
android:focusable="true"
android:background="@drawable/link"
/>
</LinearLayout>
</LinearLayout>
</ScrollView>

View file

@ -39,9 +39,7 @@ public class AuthenticationActivity extends Activity {
final static String ERROR_KEY = "error";
final static String SHOW_SIGNUP_LINK_KEY = "showSignupLink";
final static int CANCEL_RESULT_CODE = 0;
final static int OK_RESULT_CODE = 1;
final static int SIGNUP_RESULT_CODE = 2;
final static int RESULT_SIGNUP = RESULT_FIRST_USER;
private ZLResource myResource;
@ -49,7 +47,7 @@ public class AuthenticationActivity extends Activity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Thread.setDefaultUncaughtExceptionHandler(new org.geometerplus.zlibrary.ui.android.library.UncaughtExceptionHandler(this));
setResult(CANCEL_RESULT_CODE);
setResult(RESULT_CANCELED);
setContentView(R.layout.authentication);
final Intent intent = getIntent();
@ -98,7 +96,7 @@ public class AuthenticationActivity extends Activity {
signupView.setText(myResource.getResource("register").getValue());
signupView.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
setResult(SIGNUP_RESULT_CODE);
setResult(RESULT_SIGNUP);
finish();
}
});
@ -121,7 +119,7 @@ public class AuthenticationActivity extends Activity {
PASSWORD_KEY,
findTextView(R.id.authentication_password).getText().toString()
);
setResult(OK_RESULT_CODE, data);
setResult(RESULT_OK, data);
finish();
}
});

View file

@ -1,229 +0,0 @@
/*
* Copyright (C) 2010-2011 Geometer Plus <contact@geometerplus.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
package org.geometerplus.android.fbreader.network;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import org.geometerplus.zlibrary.core.resources.ZLResource;
import org.geometerplus.zlibrary.core.network.ZLNetworkException;
import org.geometerplus.zlibrary.ui.android.R;
import org.geometerplus.fbreader.network.INetworkLink;
import org.geometerplus.fbreader.network.NetworkLibrary;
import org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager;
import org.geometerplus.android.util.UIUtil;
class AuthenticationDialog {
private static AuthenticationDialog ourDialog;
public static AuthenticationDialog getDialog() {
if (ourDialog == null) {
ourDialog = new AuthenticationDialog();
}
return ourDialog;
}
private class DialogHandler extends Handler {
@Override
public void handleMessage(Message message) {
if (!NetworkView.Instance().isInitialized()) {
return;
}
final NetworkLibrary library = NetworkLibrary.Instance();
library.invalidateVisibility();
library.synchronize();
NetworkView.Instance().fireModelChanged();
if (message.what == -1) {
myErrorMessage = (String)message.obj;
myActivity.showDialog(0);
} else if (message.what == 1) {
if (myOnSuccessRunnable != null) {
myOnSuccessRunnable.run();
}
}
}
};
private final ZLResource myResource =
ZLResource.resource("dialog").getResource("AuthenticationDialog");
private INetworkLink myLink;
private String myErrorMessage;
private Runnable myOnSuccessRunnable;
private Activity myActivity;
private final DialogHandler myHandler = new DialogHandler();
public static void show(Activity activity, INetworkLink link, Runnable onSuccessRunnable) {
final NetworkAuthenticationManager mgr = link.authenticationManager();
final Intent intent = new Intent(activity, AuthenticationActivity.class);
intent.putExtra(AuthenticationActivity.USERNAME_KEY, mgr.UserNameOption.getValue());
if (Util.isRegistrationSupported(activity, link)) {
intent.putExtra(AuthenticationActivity.SHOW_SIGNUP_LINK_KEY, true);
}
//getDialog().showInternal(activity, link, onSuccessRunnable);
activity.startActivityForResult(intent, NetworkBaseActivity.CUSTOM_AUTHENTICATION_CODE);
}
private void showInternal(Activity activity, INetworkLink link, Runnable onSuccessRunnable) {
myLink = link;
myErrorMessage = null;
myOnSuccessRunnable = onSuccessRunnable;
activity.showDialog(0);
}
private void sendSuccess() {
myHandler.sendMessage(myHandler.obtainMessage(1, null));
}
private void sendCancel() {
myHandler.sendMessage(myHandler.obtainMessage(0, null));
}
private void sendError(String message) {
myHandler.sendMessage(myHandler.obtainMessage(-1, message));
}
private View createLayout() {
final View layout = myActivity.getLayoutInflater().inflate(R.layout.network_authentication_dialog, null);
((TextView)layout.findViewById(R.id.network_authentication_login_text)).setText(myResource.getResource("login").getValue());
((TextView)layout.findViewById(R.id.network_authentication_password_text)).setText(myResource.getResource("password").getValue());
final TextView registerText = (TextView)layout.findViewById(R.id.network_authentication_register);
registerText.setText(myResource.getResource("register").getValue());
registerText.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (Util.isRegistrationSupported(myActivity, myLink)) {
myActivity.dismissDialog(0);
Util.runRegistrationDialog(myActivity, myLink);
}
}
});
return layout;
}
private void onPositive(DialogInterface dialog) {
AlertDialog alert = (AlertDialog)dialog;
final String login = ((TextView)alert.findViewById(R.id.network_authentication_login)).getText().toString().trim();
final String password = ((TextView)alert.findViewById(R.id.network_authentication_password)).getText().toString();
if (login.length() == 0) {
final String err = myResource.getResource("loginIsEmpty").getValue();
sendError(err);
return;
}
final NetworkAuthenticationManager mgr = myLink.authenticationManager();
mgr.UserNameOption.setValue(login);
final Runnable runnable = new Runnable() {
public void run() {
try {
mgr.authorise(password);
if (mgr.needsInitialization()) {
mgr.initialize();
}
} catch (ZLNetworkException e) {
mgr.logOut();
sendError(e.getMessage());
return;
}
sendSuccess();
}
};
UIUtil.wait("authentication", runnable, myActivity);
}
private void onNegative(DialogInterface dialog) {
final NetworkAuthenticationManager mgr = myLink.authenticationManager();
final Runnable runnable = new Runnable() {
public void run() {
if (mgr.mayBeAuthorised(false)) {
mgr.logOut();
sendCancel();
}
}
};
UIUtil.wait("signOut", runnable, myActivity);
}
public final Dialog createDialog(final Activity activity) {
myActivity = activity;
final DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
onPositive(dialog);
} else {
onNegative(dialog);
}
}
};
final View layout = createLayout();
final ZLResource buttonResource = ZLResource.resource("dialog").getResource("button");
return new AlertDialog.Builder(activity)
.setView(layout)
.setTitle(myResource.getResource("title").getValue())
.setPositiveButton(buttonResource.getResource("ok").getValue(), listener)
.setNegativeButton(buttonResource.getResource("cancel").getValue(), listener)
.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
onNegative(dialog);
}
})
.create();
}
public final void prepareDialog(final Activity activity, Dialog dialog) {
myActivity = activity;
final NetworkAuthenticationManager mgr = myLink.authenticationManager();
((TextView)dialog.findViewById(R.id.network_authentication_login)).setText(mgr.UserNameOption.getValue());
((TextView)dialog.findViewById(R.id.network_authentication_password)).setText("");
final TextView error = (TextView)dialog.findViewById(R.id.network_authentication_error);
if (myErrorMessage == null) {
error.setVisibility(View.GONE);
error.setText("");
} else {
error.setVisibility(View.VISIBLE);
error.setText(myErrorMessage);
}
dialog.findViewById(R.id.network_authentication_register).setVisibility(Util.isRegistrationSupported(myActivity, myLink) ? View.VISIBLE : View.GONE);
View dlgView = dialog.findViewById(R.id.network_authentication_dialog);
dlgView.invalidate();
dlgView.requestLayout();
}
}

View file

@ -456,6 +456,11 @@ class NetworkBookActions extends NetworkTreeActions {
.create().show();
}
};
final Runnable buyOnUiRunnable = new Runnable() {
public void run() {
activity.runOnUiThread(buyRunnable);
}
};
try {
if (mgr.isAuthorised(true)) {
@ -464,7 +469,7 @@ class NetworkBookActions extends NetworkTreeActions {
}
} catch (ZLNetworkException e) {
}
AuthenticationDialog.show(activity, book.Link, buyRunnable);
Util.runAuthenticationDialog(activity, book.Link, null, buyOnUiRunnable);
}
private static void doBuyInBrowser(Activity activity, final NetworkBookItem book) {

View file

@ -347,28 +347,22 @@ public class NetworkBookInfoActivity extends Activity implements NetworkView.Eve
}
public void onModelChanged() {
runOnUiThread(new Runnable() {
public void run() {
updateView();
}
@Override
protected Dialog onCreateDialog(int id) {
if (!NetworkView.Instance().isInitialized()) {
return null;
}
final AuthenticationDialog dlg = AuthenticationDialog.getDialog();
if (dlg != null) {
return dlg.createDialog(this);
}
return null;
});
}
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
super.onPrepareDialog(id, dialog);
final AuthenticationDialog dlg = AuthenticationDialog.getDialog();
if (dlg != null) {
dlg.prepareDialog(this, dialog);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case NetworkBaseActivity.CUSTOM_AUTHENTICATION_CODE:
Util.processCustomAuthentication(this, myBook.Link, resultCode, data);
break;
case NetworkBaseActivity.SIGNUP_CODE:
Util.processSignup(myBook.Link, resultCode, data);
break;
}
}
}

View file

@ -213,7 +213,7 @@ class NetworkCatalogActions extends NetworkTreeActions {
case B3_TRUE:
return false;
case B3_UNDEFINED:
AuthenticationDialog.show(activity, item.Link, new Runnable() {
Util.runAuthenticationDialog(activity, item.Link, null, new Runnable() {
public void run() {
if (item.getVisibility() != ZLBoolean3.B3_TRUE) {
return;
@ -258,7 +258,7 @@ class NetworkCatalogActions extends NetworkTreeActions {
doReloadCatalog(activity, catalogTree);
return true;
case SIGNIN_ITEM_ID:
AuthenticationDialog.show(activity, item.Link, null);
Util.runAuthenticationDialog(activity, item.Link, null, null);
return true;
case SIGNUP_ITEM_ID:
Util.runRegistrationDialog(activity, item.Link);

View file

@ -32,15 +32,12 @@ import org.apache.http.auth.UsernamePasswordCredentials;
import org.geometerplus.zlibrary.core.network.ZLNetworkException;
import org.geometerplus.zlibrary.core.network.ZLNetworkManager;
import org.geometerplus.zlibrary.core.options.ZLStringOption;
import org.geometerplus.zlibrary.core.resources.ZLResource;
import org.geometerplus.fbreader.network.*;
import org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager;
import org.geometerplus.fbreader.network.tree.*;
import org.geometerplus.fbreader.tree.FBTree;
import org.geometerplus.android.util.UIUtil;
public class NetworkCatalogActivity extends NetworkBaseActivity implements UserRegistrationConstants {
private NetworkTree myTree;
private volatile boolean myInProgress;
@ -144,7 +141,7 @@ public class NetworkCatalogActivity extends NetworkBaseActivity implements UserR
switch (requestCode) {
case BASIC_AUTHENTICATION_CODE:
synchronized (myCredentialsCreator) {
if (resultCode == AuthenticationActivity.OK_RESULT_CODE && data != null) {
if (resultCode == AuthenticationActivity.RESULT_OK && data != null) {
myCredentialsCreator.myUsername =
data.getStringExtra(AuthenticationActivity.USERNAME_KEY);
myCredentialsCreator.myPassword =
@ -154,103 +151,16 @@ public class NetworkCatalogActivity extends NetworkBaseActivity implements UserR
}
break;
case CUSTOM_AUTHENTICATION_CODE:
processCustomAuthentication(
Util.processCustomAuthentication(
this, ((NetworkCatalogTree)myTree).Item.Link, resultCode, data
);
break;
case SIGNUP_CODE:
processSignup(((NetworkCatalogTree)myTree).Item.Link, resultCode, data);
Util.processSignup(((NetworkCatalogTree)myTree).Item.Link, resultCode, data);
break;
}
}
static void runAuthenticationDialog(Activity activity, INetworkLink link, String error) {
final NetworkAuthenticationManager mgr = link.authenticationManager();
final Intent intent = new Intent(activity, AuthenticationActivity.class);
intent.putExtra(AuthenticationActivity.USERNAME_KEY, mgr.UserNameOption.getValue());
if (Util.isRegistrationSupported(activity, link)) {
intent.putExtra(AuthenticationActivity.SHOW_SIGNUP_LINK_KEY, true);
}
intent.putExtra(AuthenticationActivity.ERROR_KEY, error);
activity.startActivityForResult(intent, NetworkBaseActivity.CUSTOM_AUTHENTICATION_CODE);
}
static void processCustomAuthentication(final Activity activity, final INetworkLink link, int resultCode, Intent data) {
switch (resultCode) {
case AuthenticationActivity.CANCEL_RESULT_CODE:
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.OK_RESULT_CODE:
{
final ZLResource resource =
ZLResource.resource("dialog").getResource("AuthenticationDialog");
final String username =
data.getStringExtra(AuthenticationActivity.USERNAME_KEY);
final String password =
data.getStringExtra(AuthenticationActivity.PASSWORD_KEY);
if (username.length() == 0) {
runAuthenticationDialog(
activity, link,
resource.getResource("loginIsEmpty").getValue()
);
}
final NetworkAuthenticationManager mgr = link.authenticationManager();
mgr.UserNameOption.setValue(username);
final Runnable runnable = new Runnable() {
public void run() {
try {
mgr.authorise(password);
if (mgr.needsInitialization()) {
mgr.initialize();
}
} catch (ZLNetworkException e) {
mgr.logOut();
runAuthenticationDialog(activity, link, e.getMessage());
return;
}
final NetworkLibrary library = NetworkLibrary.Instance();
library.invalidateVisibility();
library.synchronize();
NetworkView.Instance().fireModelChanged();
}
};
UIUtil.wait("authentication", runnable, activity);
break;
}
case AuthenticationActivity.SIGNUP_RESULT_CODE:
Util.runRegistrationDialog(activity, link);
break;
}
}
static void processSignup(INetworkLink link, int resultCode, Intent data) {
if (resultCode == RESULT_OK && data != null) {
try {
Util.runAfterRegistration(link.authenticationManager(), data);
} catch (ZLNetworkException e) {
// TODO: show an error message
}
}
}
private final void setupTitle() {
String title = null;
final NetworkView networkView = NetworkView.Instance();

View file

@ -159,10 +159,10 @@ class TopupActions extends NetworkTreeActions {
if (mgr.mayBeAuthorised(false)) {
action.run();
} else {
AuthenticationDialog.show(activity, link, new Runnable() {
Util.runAuthenticationDialog(activity, link, null, new Runnable() {
public void run() {
if (mgr.mayBeAuthorised(false)) {
action.run();
activity.runOnUiThread(action);
}
}
});

View file

@ -19,7 +19,7 @@
package org.geometerplus.android.fbreader.network;
import java.util.Map;
import java.util.*;
import android.app.Activity;
import android.content.ActivityNotFoundException;
@ -28,12 +28,14 @@ import android.content.Context;
import android.net.Uri;
import org.geometerplus.zlibrary.core.network.ZLNetworkException;
import org.geometerplus.zlibrary.core.resources.ZLResource;
import org.geometerplus.fbreader.network.*;
import org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager;
import org.geometerplus.fbreader.network.tree.NetworkBookTree;
import org.geometerplus.fbreader.network.urlInfo.UrlInfo;
import org.geometerplus.android.util.UIUtil;
import org.geometerplus.android.util.PackageUtil;
abstract class Util implements UserRegistrationConstants {
@ -74,7 +76,96 @@ abstract class Util implements UserRegistrationConstants {
}
}
static void runAfterRegistration(NetworkAuthenticationManager mgr, Intent data) throws ZLNetworkException {
private static final Map<Activity,Runnable> myAfterRegisrationMap =
new HashMap<Activity,Runnable>();
static void runAuthenticationDialog(Activity activity, INetworkLink link, String error, Runnable onSuccess) {
final NetworkAuthenticationManager mgr = link.authenticationManager();
final Intent intent = new Intent(activity, AuthenticationActivity.class);
intent.putExtra(AuthenticationActivity.USERNAME_KEY, mgr.UserNameOption.getValue());
if (isRegistrationSupported(activity, link)) {
intent.putExtra(AuthenticationActivity.SHOW_SIGNUP_LINK_KEY, true);
}
intent.putExtra(AuthenticationActivity.ERROR_KEY, error);
if (onSuccess != null) {
myAfterRegisrationMap.put(activity, onSuccess);
}
activity.startActivityForResult(intent, NetworkBaseActivity.CUSTOM_AUTHENTICATION_CODE);
}
static void processCustomAuthentication(final Activity activity, final INetworkLink link, int resultCode, Intent data) {
final Runnable onSuccess = myAfterRegisrationMap.get(activity);
myAfterRegisrationMap.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:
{
final ZLResource resource =
ZLResource.resource("dialog").getResource("AuthenticationDialog");
final String username =
data.getStringExtra(AuthenticationActivity.USERNAME_KEY);
final String password =
data.getStringExtra(AuthenticationActivity.PASSWORD_KEY);
if (username.length() == 0) {
runAuthenticationDialog(
activity, link,
resource.getResource("loginIsEmpty").getValue(),
onSuccess
);
}
final NetworkAuthenticationManager mgr = link.authenticationManager();
mgr.UserNameOption.setValue(username);
final Runnable runnable = new Runnable() {
public void run() {
try {
mgr.authorise(password);
if (mgr.needsInitialization()) {
mgr.initialize();
}
onSuccess.run();
} catch (ZLNetworkException e) {
mgr.logOut();
runAuthenticationDialog(activity, link, e.getMessage(), onSuccess);
return;
}
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;
}
}
static void processSignup(INetworkLink link, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && data != null) {
try {
final NetworkAuthenticationManager mgr = link.authenticationManager();
final String userName = data.getStringExtra(USER_REGISTRATION_USERNAME);
final String litresSid = data.getStringExtra(USER_REGISTRATION_LITRES_SID);
mgr.initUser(userName, litresSid);
@ -86,6 +177,10 @@ abstract class Util implements UserRegistrationConstants {
throw e;
}
}
} catch (ZLNetworkException e) {
// TODO: show an error message
}
}
}
static boolean isTopupSupported(Activity activity, INetworkLink link) {