You are now logged in as: (.+?)<"); static final Pattern ERROR_PATTERN = Pattern.compile( "(?:
(.+?)
)" + "|(?:(.+?))"); @@ -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); diff --git a/app/src/main/java/com/hippo/ehviewer/ui/WebViewSignInActivity.java b/app/src/main/java/com/hippo/ehviewer/ui/WebViewSignInActivity.java new file mode 100644 index 000000000..4e0190a86 --- /dev/null +++ b/app/src/main/java/com/hippo/ehviewer/ui/WebViewSignInActivity.java @@ -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" + + "(''+document.getElementsByTagName('html')[0].innerHTML+'');"); + } + } + } + + @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(); + } + } + } +} diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/SignInScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/SignInScene.java index 7785d387d..c8aa654f4 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/SignInScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/SignInScene.java @@ -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