Add login via WebView
This commit is contained in:
1 parent
fd5d07b2f2
commit
1c2aff9bbb
6 files changed
+120
-97
No files matched your search
@@ -16,18 +16,6 @@
|
||||
|
||||
package com.hippo.ehviewer.ehclient;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
@@ -51,6 +39,18 @@ import com.hippo.ehviewer.util.Config;
|
||||
import com.hippo.ehviewer.util.EhUtils;
|
||||
import com.hippo.ehviewer.util.Utils;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class EhClient {
|
||||
@SuppressWarnings("unused")
|
||||
private static final String TAG = EhClient.class.getSimpleName();
|
||||
@@ -66,7 +66,7 @@ public class EhClient {
|
||||
public static final long APIUID = 1363542;
|
||||
public static final String APIKEY = "f4b5407ab1727b9d08d7";
|
||||
|
||||
private static final String LOGIN_URL = "http://forums.e-hentai.org/index.php?act=Login&CODE=01";
|
||||
public static final String LOGIN_URL = "http://forums.e-hentai.org/index.php?act=Login&CODE=01";
|
||||
private static final String FORUMS_URL = "http://forums.e-hentai.org/index.php";
|
||||
|
||||
public static final String HEADER_G = "http://g.e-hentai.org/";
|
||||
|
||||
@@ -148,22 +148,24 @@ public class EhInfo {
|
||||
}
|
||||
|
||||
public void storeCookie(HttpURLConnection conn) {
|
||||
String ipb_member_id;
|
||||
String ipb_pass_hash;
|
||||
|
||||
List<String> cookieList = conn.getHeaderFields().get("Set-Cookie");
|
||||
if (cookieList == null)
|
||||
return;
|
||||
SharedPreferences.Editor editor = mInfoPref.edit();
|
||||
for (String str : cookieList) {
|
||||
ipb_member_id = getCookie(str, KEY_MEMBER_ID);
|
||||
ipb_pass_hash = getCookie(str, KEY_PASS_HASH);
|
||||
|
||||
if (ipb_member_id != null)
|
||||
editor.putString(KEY_MEMBER_ID, ipb_member_id);
|
||||
if (ipb_pass_hash != null)
|
||||
editor.putString(KEY_PASS_HASH, ipb_pass_hash);
|
||||
for (String str : cookieList) {
|
||||
setCookie(str);
|
||||
}
|
||||
}
|
||||
|
||||
public void setCookie(String cookie) {
|
||||
SharedPreferences.Editor editor = mInfoPref.edit();
|
||||
String ipb_member_id = getCookie(cookie, KEY_MEMBER_ID);
|
||||
String ipb_pass_hash = getCookie(cookie, KEY_PASS_HASH);
|
||||
|
||||
if (ipb_member_id != null)
|
||||
editor.putString(KEY_MEMBER_ID, ipb_member_id);
|
||||
if (ipb_pass_hash != null)
|
||||
editor.putString(KEY_PASS_HASH, ipb_pass_hash);
|
||||
editor.apply();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2014 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.receiver;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.ConnectivityManager;
|
||||
|
||||
import com.hippo.ehviewer.network.Network;
|
||||
import com.hippo.ehviewer.util.Secret;
|
||||
|
||||
public class NetworkChangeReceiver extends BroadcastReceiver {
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
|
||||
|
||||
// Update secret image here
|
||||
int state = Network.getNetworkState(context);
|
||||
if (state == Network.NETWORK_STATE_WIFI) {
|
||||
Secret.updateSecretImage(context);
|
||||
}
|
||||
|
||||
// TODO stop download when mobile network
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,15 +16,10 @@
|
||||
|
||||
package com.hippo.ehviewer.ui;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.annotation.TargetApi;
|
||||
import android.app.ActionBar;
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.app.SearchManager;
|
||||
import android.content.Context;
|
||||
@@ -97,6 +92,12 @@ import com.hippo.ehviewer.widget.TagsAdapter;
|
||||
import com.hippo.ehviewer.widget.recyclerview.EasyRecyclerView;
|
||||
import com.hippo.ehviewer.windowsanimate.WindowsAnimate;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
/*
|
||||
*
|
||||
* Footer 显示产生的 refresh 手动 pull 显示产生的 refresh 按键或其他产生的 refresh
|
||||
@@ -130,7 +131,8 @@ public class GalleryListActivity extends AbsTranslucentActivity implements View.
|
||||
@SuppressWarnings("unused")
|
||||
private static final String TAG = GalleryListActivity.class.getSimpleName();
|
||||
|
||||
private static int RESULT_LOAD_SEARCH_IMAGE = 1;
|
||||
private static int REQUST_WEB_WIEW_LOGIN = 0;
|
||||
private static int REQUST_LOAD_SEARCH_IMAGE = 1;
|
||||
|
||||
public static final String ACTION_GALLERY_LIST = "com.hippo.ehviewer.intent.action.GALLERY_LIST";
|
||||
|
||||
@@ -246,25 +248,31 @@ public class GalleryListActivity extends AbsTranslucentActivity implements View.
|
||||
View view = ViewUtils.inflateDialogView(R.layout.login, false);
|
||||
|
||||
return new MaterialAlertDialog.Builder(this).setCancelable(false).setTitle(R.string.login).setView(view, true)
|
||||
.setPositiveButton(android.R.string.ok).setNegativeButton(android.R.string.cancel)
|
||||
.setNeutralButton(R.string.register).setButtonListener(new MaterialAlertDialog.OnClickListener() {
|
||||
.setPositiveButton(android.R.string.ok)
|
||||
.setNegativeButton(android.R.string.cancel)
|
||||
.setNeutralButton(R.string.register)
|
||||
.setActionButton(" ! ")
|
||||
.setButtonListener(new MaterialAlertDialog.OnClickListener() {
|
||||
@Override
|
||||
public boolean onClick(MaterialAlertDialog dialog, int which) {
|
||||
switch (which) {
|
||||
case MaterialAlertDialog.POSITIVE:
|
||||
setUserPanel(WAIT);
|
||||
String username = ((EditText) loginDialog.findViewById(R.id.username)).getText().toString();
|
||||
String password = ((EditText) loginDialog.findViewById(R.id.password)).getText().toString();
|
||||
mClient.login(username, password, GalleryListActivity.this);
|
||||
return true;
|
||||
case MaterialAlertDialog.NEGATIVE:
|
||||
setUserPanel();
|
||||
return true;
|
||||
case MaterialAlertDialog.NEUTRAL:
|
||||
toRegister();
|
||||
return false;
|
||||
default:
|
||||
return false;
|
||||
case MaterialAlertDialog.POSITIVE:
|
||||
setUserPanel(WAIT);
|
||||
String username = ((EditText) loginDialog.findViewById(R.id.username)).getText().toString();
|
||||
String password = ((EditText) loginDialog.findViewById(R.id.password)).getText().toString();
|
||||
mClient.login(username, password, GalleryListActivity.this);
|
||||
return true;
|
||||
case MaterialAlertDialog.NEGATIVE:
|
||||
setUserPanel();
|
||||
return true;
|
||||
case MaterialAlertDialog.NEUTRAL:
|
||||
toRegister();
|
||||
return false;
|
||||
case MaterialAlertDialog.ACTION:
|
||||
Intent intent = new Intent(GalleryListActivity.this, WebViewLoginActivity.class);
|
||||
startActivityForResult(intent, REQUST_WEB_WIEW_LOGIN);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}).create();
|
||||
@@ -300,7 +308,7 @@ public class GalleryListActivity extends AbsTranslucentActivity implements View.
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
|
||||
if (requestCode == RESULT_LOAD_SEARCH_IMAGE && resultCode == RESULT_OK && null != data) {
|
||||
if (requestCode == REQUST_LOAD_SEARCH_IMAGE && resultCode == RESULT_OK && null != data) {
|
||||
Uri selectedImage = data.getData();
|
||||
String[] filePathColumn = { MediaStore.Images.Media.DATA };
|
||||
|
||||
@@ -313,6 +321,11 @@ public class GalleryListActivity extends AbsTranslucentActivity implements View.
|
||||
|
||||
mSearchImageText.setVisibility(View.VISIBLE);
|
||||
mSearchImageText.setText(ImagePath);
|
||||
} else if (REQUST_WEB_WIEW_LOGIN == requestCode) {
|
||||
if (resultCode == Activity.RESULT_OK) {
|
||||
loginDialog.dismiss();
|
||||
onSuccess();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -351,7 +364,7 @@ public class GalleryListActivity extends AbsTranslucentActivity implements View.
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
|
||||
startActivityForResult(i, RESULT_LOAD_SEARCH_IMAGE);
|
||||
startActivityForResult(i, REQUST_LOAD_SEARCH_IMAGE);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.hippo.ehviewer.ui;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.webkit.CookieManager;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
|
||||
import com.hippo.ehviewer.ehclient.EhClient;
|
||||
import com.hippo.ehviewer.ehclient.EhInfo;
|
||||
|
||||
public class WebViewLoginActivity extends AbsActivity {
|
||||
|
||||
private static final String TITLE_LOGIN_SUCCESS = "Please stand by...";
|
||||
|
||||
public static final String KEY_ERROR = "error";
|
||||
|
||||
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
CookieManager.getInstance().removeAllCookie();
|
||||
WebView wv = new WebView(this);
|
||||
wv.setWebViewClient(new LoginWebViewClient());
|
||||
wv.loadUrl(EhClient.LOGIN_URL);
|
||||
setContentView(wv);
|
||||
}
|
||||
|
||||
private class LoginWebViewClient extends WebViewClient {
|
||||
@Override
|
||||
public void onPageFinished(WebView view, String url) {
|
||||
if (TITLE_LOGIN_SUCCESS.equals(view.getTitle())) {
|
||||
// Login OK
|
||||
String cookies = CookieManager.getInstance().getCookie(url);
|
||||
if (cookies != null) {
|
||||
// Store cookie
|
||||
EhInfo ehInfo = EhInfo.getInstance(WebViewLoginActivity.this);
|
||||
ehInfo.setCookie(cookies);
|
||||
ehInfo.login("User", "User");
|
||||
setResult(RESULT_OK);
|
||||
} else {
|
||||
setResult(RESULT_CANCELED);
|
||||
}
|
||||
finish();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user