Add Sign in by WebView

This commit is contained in:
seven332 committed 2015-08-26 00:43:25 +08:00
1 parent 0ae6d48a7d
commit 59c14245ee
8 files changed
+218 -5

No files matched your search

@@ -34,6 +34,7 @@ public final class Constants {
public static final int MESSENGER_ID_MODIFY_DOWNLOAD_LABEL_FROM_MANAGER;
public static final int MESSENGER_ID_MODIFY_DOWNLOAD_LABEL_FROM_SCENE;
public static final int MESSENGER_ID_UPDATE_HISTORY;
public static final int MESSENGER_ID_LOG_IN_VIEW_WEBVIEW;
static {
Messenger messenger = Messenger.getInstance();
@@ -51,5 +52,6 @@ public final class Constants {
MESSENGER_ID_MODIFY_DOWNLOAD_LABEL_FROM_MANAGER = messenger.newId();
MESSENGER_ID_MODIFY_DOWNLOAD_LABEL_FROM_SCENE = messenger.newId();
MESSENGER_ID_UPDATE_HISTORY = messenger.newId();
MESSENGER_ID_LOG_IN_VIEW_WEBVIEW = messenger.newId();
}
}
@@ -16,12 +16,14 @@
package com.hippo.ehviewer.client;
import com.hippo.yorozuya.Say;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SignInParser {
static final Pattern NAME_PATTERN = Pattern.compile("<p>You are now logged in as: (.+?)<br />");
static final Pattern NAME_PATTERN = Pattern.compile("<p>You are now logged in as: (.+?)<");
static final Pattern ERROR_PATTERN = Pattern.compile(
"(?:<h4>The error returned was:</h4>\\s*<p>(.+?)</p>)"
+ "|(?:<span class=\"postcolor\">(.+?)</span>)");
@@ -29,6 +31,8 @@ public class SignInParser {
public static String parse(String body) throws Exception {
Matcher m;
Say.f("TAG", body);
m = NAME_PATTERN.matcher(body);
if (m.find()) {
return m.group(1);
@@ -0,0 +1,111 @@
/*
* Copyright 2015 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hippo.ehviewer.ui;
import android.os.Bundle;
import android.webkit.CookieManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import com.hippo.app.StatsActivity;
import com.hippo.ehviewer.Constants;
import com.hippo.ehviewer.R;
import com.hippo.ehviewer.client.SignInParser;
import com.hippo.ehviewer.util.Settings;
import com.hippo.httpclient.Cookie;
import com.hippo.yorozuya.Messenger;
public class WebViewSignInActivity extends StatsActivity {
private static final String SIGN_IN_URL = "http://forums.e-hentai.org/index.php?act=Login&CODE=01";
private static final String TITLE_LOGIN_SUCCESS = "Please stand by...";
private static final String KEY_IPD_MEMBER_ID = "ipb_member_id";
private static final String KEY_IPD_PASS_HASH = "ipb_pass_hash";
private static final String KEY_IGNEOUS = "igneous";
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Clear cookie
CookieManager.getInstance().removeAllCookie();
mWebView = new WebView(this);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new LoginWebViewHelper(), "HtmlViewer");
mWebView.setWebViewClient(new LoginWebViewHelper());
mWebView.loadUrl(SIGN_IN_URL);
setContentView(mWebView);
}
@Override
protected void onDestroy() {
super.onDestroy();
mWebView.destroy();
}
private class LoginWebViewHelper extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) {
if (TITLE_LOGIN_SUCCESS.equals(view.getTitle())) {
// Login OK
Cookie cookie = new Cookie();
cookie.putRaw(CookieManager.getInstance().getCookie(url));
String igneous = cookie.get(KEY_IGNEOUS);
String ipdMemberId = cookie.get(KEY_IPD_MEMBER_ID);
String ipdPassHash = cookie.get(KEY_IPD_PASS_HASH);
if (igneous != null) {
Settings.putIgneous(igneous);
}
if (ipdMemberId != null && ipdPassHash != null) {
Settings.putIpdMemberId(ipdMemberId);
Settings.putIpdPassHash(ipdPassHash);
view.loadUrl("javascript:window.HtmlViewer.showHTML" +
"('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');");
}
}
}
@android.webkit.JavascriptInterface
public void showHTML(String html) {
try {
String displayname = SignInParser.parse(html);
Settings.putSignIn(true);
Settings.putDisplayName(displayname);
Messenger.getInstance().notify(Constants.MESSENGER_ID_SIGN_IN_OR_OUT, displayname);
finish();
Messenger.getInstance().notify(Constants.MESSENGER_ID_LOG_IN_VIEW_WEBVIEW, null);
Toast.makeText(WebViewSignInActivity.this, R.string.sign_in_successfully, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
@@ -16,47 +16,72 @@
package com.hippo.ehviewer.ui.scene;
import android.content.Intent;
import android.net.Uri;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.hippo.ehviewer.Constants;
import com.hippo.ehviewer.EhApplication;
import com.hippo.ehviewer.R;
import com.hippo.ehviewer.client.EhClient;
import com.hippo.ehviewer.client.EhRequest;
import com.hippo.ehviewer.ui.WebViewSignInActivity;
import com.hippo.ehviewer.util.Settings;
import com.hippo.rippleold.RippleSalon;
import com.hippo.scene.ProgressDialog;
import com.hippo.scene.Scene;
import com.hippo.util.ExceptionUtils;
import com.hippo.yorozuya.Messenger;
public final class SignInScene extends Scene implements View.OnClickListener,
TextView.OnEditorActionListener {
TextView.OnEditorActionListener, Messenger.Receiver {
private View mMainView;
private EditText mUsername;
private EditText mPassword;
private TextView mWebview;
private TextView mSignUp;
private TextView mSignIn;
private ProgressDialog mProgressDialog;
private EhClient.EhListener<String> mSignInListener = new EhClient.EhListener<String>() {
@Override
public void onSuccess(String displayname) {
Settings.putSignIn(true);
Settings.putDisplayName(displayname);
Messenger.getInstance().notify(Constants.MESSENGER_ID_SIGN_IN_OR_OUT, displayname);
Toast.makeText(getContext(), R.string.sign_in_successfully, Toast.LENGTH_SHORT).show();
if (mProgressDialog != null) {
mProgressDialog.finish();
mProgressDialog = null;
finish();
}
}
@Override
public void onFailure(Exception e) {
e.printStackTrace();
Toast.makeText(getContext(), ExceptionUtils.getReadableString(getContext(), e), Toast.LENGTH_SHORT).show();
if (mProgressDialog != null) {
mProgressDialog.finish();
mProgressDialog = null;
}
}
@Override
public void onCanceled() {
if (mProgressDialog != null) {
mProgressDialog.finish();
mProgressDialog = null;
}
}
};
@@ -65,6 +90,20 @@ public final class SignInScene extends Scene implements View.OnClickListener,
return LAUNCH_MODE_SINGLE_TOP;
}
@Override
protected void onInit() {
super.onInit();
Messenger.getInstance().register(Constants.MESSENGER_ID_LOG_IN_VIEW_WEBVIEW, this);
}
@Override
protected void onDie() {
super.onDie();
Messenger.getInstance().unregister(Constants.MESSENGER_ID_LOG_IN_VIEW_WEBVIEW, this);
}
@Override
protected void onCreate(boolean rebirth) {
super.onCreate(rebirth);
@@ -73,12 +112,15 @@ public final class SignInScene extends Scene implements View.OnClickListener,
mMainView = findViewById(R.id.sigin_in_main);
mUsername = (EditText) mMainView.findViewById(R.id.username);
mPassword = (EditText) mMainView.findViewById(R.id.password);
mWebview = (TextView) mMainView.findViewById(R.id.webview);
mSignUp = (TextView) mMainView.findViewById(R.id.sign_up);
mSignIn = (TextView) mMainView.findViewById(R.id.sign_in);
mPassword.setOnEditorActionListener(this);
RippleSalon.addRipple(mWebview, false);
RippleSalon.addRipple(mSignUp, false);
RippleSalon.addRipple(mSignIn, false);
mWebview.setOnClickListener(this);
mSignUp.setOnClickListener(this);
mSignIn.setOnClickListener(this);
}
@@ -92,7 +134,14 @@ public final class SignInScene extends Scene implements View.OnClickListener,
@Override
public void onClick(View v) {
if (v == mSignIn) {
if (v == mWebview) {
Intent intent = new Intent(getStageActivity(), WebViewSignInActivity.class);
getStageActivity(). startActivity(intent);
} else if (v == mSignUp) {
Uri uri = Uri.parse("http://forums.e-hentai.org/index.php?act=Reg");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
getStageActivity().startActivity(intent);
} else if (v == mSignIn) {
String username = mUsername.getText().toString();
String password = mPassword.getText().toString();
EhClient client = EhApplication.getEhClient(getStageActivity());
@@ -102,6 +151,8 @@ public final class SignInScene extends Scene implements View.OnClickListener,
request.setEhListener(mSignInListener);
request.setArgs(username, password);
client.execute(request);
mProgressDialog = (ProgressDialog) new ProgressDialog.Builder(getContext()).setTitle(R.string.signin).show(this);
}
}
@@ -115,4 +166,11 @@ public final class SignInScene extends Scene implements View.OnClickListener,
}
return false;
}
@Override
public void onReceive(int id, Object obj) {
if (id == Constants.MESSENGER_ID_LOG_IN_VIEW_WEBVIEW) {
finish();
}
}
}
@@ -190,6 +190,13 @@ public class SimpleDialog extends SceneDialog implements View.OnClickListener,
mSceneDialogView.setOnClickOutOfDialogListener(this);
}
@Override
public void onBackPressed() {
if (mBuilder.mCancelable) {
finish();
}
}
@Override
protected SceneView createSceneView(Context context) {
return new SceneDialogView(context);