diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 78f79d47c..c918af361 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -44,6 +44,15 @@ + + + + You are now logged in as: (.+?)
"); + static final Pattern NAME_PATTERN = Pattern.compile("

You are now logged in as: (.+?)<"); static final Pattern ERROR_PATTERN = Pattern.compile( "(?:

The error returned was:

\\s*

(.+?)

)" + "|(?:(.+?))"); @@ -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 mSignInListener = new EhClient.EhListener() { @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(); + } + } } diff --git a/app/src/main/java/com/hippo/scene/SimpleDialog.java b/app/src/main/java/com/hippo/scene/SimpleDialog.java index 4d4f4e5e9..132886d08 100644 --- a/app/src/main/java/com/hippo/scene/SimpleDialog.java +++ b/app/src/main/java/com/hippo/scene/SimpleDialog.java @@ -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); diff --git a/app/src/main/res/layout/scene_sign_in.xml b/app/src/main/res/layout/scene_sign_in.xml index 39d5035ea..e14b60305 100644 --- a/app/src/main/res/layout/scene_sign_in.xml +++ b/app/src/main/res/layout/scene_sign_in.xml @@ -89,6 +89,23 @@ + + + + + + \"What ?\" \"I lose my panda …\" + Sign in by WebView + Sign in successfully Download server @@ -256,6 +258,9 @@ History list is empty + + Please wait … + Keyword Search Retry