Fix ReCaptchaActivity crash and save cookies correctly
This commit is contained in:
parent
a3d8848825
commit
daa4fd5103
|
@ -1,6 +1,5 @@
|
||||||
package org.schabi.newpipe;
|
package org.schabi.newpipe;
|
||||||
|
|
||||||
import android.app.Activity;
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
|
@ -9,12 +8,18 @@ import androidx.core.app.NavUtils;
|
||||||
import androidx.appcompat.app.ActionBar;
|
import androidx.appcompat.app.ActionBar;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
import androidx.appcompat.widget.Toolbar;
|
import androidx.appcompat.widget.Toolbar;
|
||||||
|
|
||||||
|
import android.util.Log;
|
||||||
|
import android.view.Menu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.webkit.CookieManager;
|
import android.webkit.CookieManager;
|
||||||
import android.webkit.WebSettings;
|
import android.webkit.WebSettings;
|
||||||
import android.webkit.WebView;
|
import android.webkit.WebView;
|
||||||
import android.webkit.WebViewClient;
|
import android.webkit.WebViewClient;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Created by beneth <bmauduit@beneth.fr> on 06.12.16.
|
* Created by beneth <bmauduit@beneth.fr> on 06.12.16.
|
||||||
*
|
*
|
||||||
|
@ -37,18 +42,17 @@ import android.webkit.WebViewClient;
|
||||||
public class ReCaptchaActivity extends AppCompatActivity {
|
public class ReCaptchaActivity extends AppCompatActivity {
|
||||||
public static final int RECAPTCHA_REQUEST = 10;
|
public static final int RECAPTCHA_REQUEST = 10;
|
||||||
public static final String RECAPTCHA_URL_EXTRA = "recaptcha_url_extra";
|
public static final String RECAPTCHA_URL_EXTRA = "recaptcha_url_extra";
|
||||||
|
|
||||||
public static final String TAG = ReCaptchaActivity.class.toString();
|
public static final String TAG = ReCaptchaActivity.class.toString();
|
||||||
public static final String YT_URL = "https://www.youtube.com";
|
public static final String YT_URL = "https://www.youtube.com";
|
||||||
|
|
||||||
private String url;
|
private String foundCookies = "";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_recaptcha);
|
setContentView(R.layout.activity_recaptcha);
|
||||||
|
|
||||||
url = getIntent().getStringExtra(RECAPTCHA_URL_EXTRA);
|
String url = getIntent().getStringExtra(RECAPTCHA_URL_EXTRA);
|
||||||
if (url == null || url.isEmpty()) {
|
if (url == null || url.isEmpty()) {
|
||||||
url = YT_URL;
|
url = YT_URL;
|
||||||
}
|
}
|
||||||
|
@ -60,26 +64,24 @@ public class ReCaptchaActivity extends AppCompatActivity {
|
||||||
Toolbar toolbar = findViewById(R.id.toolbar);
|
Toolbar toolbar = findViewById(R.id.toolbar);
|
||||||
setSupportActionBar(toolbar);
|
setSupportActionBar(toolbar);
|
||||||
|
|
||||||
ActionBar actionBar = getSupportActionBar();
|
|
||||||
if (actionBar != null) {
|
|
||||||
actionBar.setDisplayHomeAsUpEnabled(true);
|
|
||||||
actionBar.setTitle(R.string.reCaptcha_title);
|
|
||||||
actionBar.setDisplayShowTitleEnabled(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
WebView myWebView = findViewById(R.id.reCaptchaWebView);
|
WebView myWebView = findViewById(R.id.reCaptchaWebView);
|
||||||
|
|
||||||
// Enable Javascript
|
// Enable Javascript
|
||||||
WebSettings webSettings = myWebView.getSettings();
|
WebSettings webSettings = myWebView.getSettings();
|
||||||
webSettings.setJavaScriptEnabled(true);
|
webSettings.setJavaScriptEnabled(true);
|
||||||
|
|
||||||
ReCaptchaWebViewClient webClient = new ReCaptchaWebViewClient(this);
|
myWebView.setWebViewClient(new WebViewClient() {
|
||||||
myWebView.setWebViewClient(webClient);
|
@Override
|
||||||
|
public void onPageFinished(WebView view, String url) {
|
||||||
|
super.onPageFinished(view, url);
|
||||||
|
handleCookies(CookieManager.getInstance().getCookie(url));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Cleaning cache, history and cookies from webView
|
// Cleaning cache, history and cookies from webView
|
||||||
myWebView.clearCache(true);
|
myWebView.clearCache(true);
|
||||||
myWebView.clearHistory();
|
myWebView.clearHistory();
|
||||||
android.webkit.CookieManager cookieManager = CookieManager.getInstance();
|
android.webkit.CookieManager cookieManager = CookieManager .getInstance();
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||||
cookieManager.removeAllCookies(aBoolean -> {});
|
cookieManager.removeAllCookies(aBoolean -> {});
|
||||||
} else {
|
} else {
|
||||||
|
@ -89,74 +91,89 @@ public class ReCaptchaActivity extends AppCompatActivity {
|
||||||
myWebView.loadUrl(url);
|
myWebView.loadUrl(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
private class ReCaptchaWebViewClient extends WebViewClient {
|
@Override
|
||||||
private final Activity context;
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
private String mCookies;
|
boolean ret = super.onCreateOptionsMenu(menu);
|
||||||
|
|
||||||
ReCaptchaWebViewClient(Activity ctx) {
|
ActionBar actionBar = getSupportActionBar();
|
||||||
context = ctx;
|
if (actionBar != null) {
|
||||||
|
actionBar.setDisplayHomeAsUpEnabled(true);
|
||||||
|
actionBar.setHomeAsUpIndicator(getResources().getDrawable(R.drawable.ic_done_white_24dp));
|
||||||
|
actionBar.setTitle(R.string.title_activity_recaptcha);
|
||||||
|
actionBar.setSubtitle(R.string.subtitle_activity_recaptcha);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
return ret;
|
||||||
public void onPageStarted(WebView view, String url, Bitmap favicon) {
|
}
|
||||||
// TODO: Start Loader
|
|
||||||
super.onPageStarted(view, url, favicon);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPageFinished(WebView view, String url) {
|
public void onBackPressed() {
|
||||||
String cookies = CookieManager.getInstance().getCookie(url);
|
saveCookiesAndFinish();
|
||||||
|
|
||||||
// TODO: Stop Loader
|
|
||||||
|
|
||||||
// find cookies : s_gl & goojf and Add cookies to Downloader
|
|
||||||
if (find_access_cookies(cookies)) {
|
|
||||||
// Give cookies to Downloader class
|
|
||||||
DownloaderImpl.getInstance().setCookies(mCookies);
|
|
||||||
|
|
||||||
// Closing activity and return to parent
|
|
||||||
setResult(RESULT_OK);
|
|
||||||
finish();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean find_access_cookies(String cookies) {
|
|
||||||
boolean ret = false;
|
|
||||||
String c_s_gl = "";
|
|
||||||
String c_goojf = "";
|
|
||||||
|
|
||||||
String[] parts = cookies.split("; ");
|
|
||||||
for (String part : parts) {
|
|
||||||
if (part.trim().startsWith("s_gl")) {
|
|
||||||
c_s_gl = part.trim();
|
|
||||||
}
|
|
||||||
if (part.trim().startsWith("goojf")) {
|
|
||||||
c_goojf = part.trim();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (c_s_gl.length() > 0 && c_goojf.length() > 0) {
|
|
||||||
ret = true;
|
|
||||||
//mCookies = c_s_gl + "; " + c_goojf;
|
|
||||||
// Youtube seems to also need the other cookies:
|
|
||||||
mCookies = cookies;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
int id = item.getItemId();
|
int id = item.getItemId();
|
||||||
switch (id) {
|
switch (id) {
|
||||||
case android.R.id.home: {
|
case android.R.id.home:
|
||||||
Intent intent = new Intent(this, org.schabi.newpipe.MainActivity.class);
|
saveCookiesAndFinish();
|
||||||
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
||||||
NavUtils.navigateUpTo(this, intent);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void saveCookiesAndFinish() {
|
||||||
|
if (!foundCookies.isEmpty()) {
|
||||||
|
// Give cookies to Downloader class
|
||||||
|
DownloaderImpl.getInstance().setCookies(foundCookies);
|
||||||
|
setResult(RESULT_OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
Intent intent = new Intent(this, org.schabi.newpipe.MainActivity.class);
|
||||||
|
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||||
|
NavUtils.navigateUpTo(this, intent);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private void handleCookies(@Nullable String cookies) {
|
||||||
|
if (MainActivity.DEBUG) Log.d(TAG, "handleCookies: cookies=" + (cookies == null ? "null" : cookies));
|
||||||
|
if (cookies == null) return;
|
||||||
|
|
||||||
|
addYoutubeCookies(cookies);
|
||||||
|
// add other methods to extract cookies here
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addYoutubeCookies(@Nonnull String cookies) {
|
||||||
|
String c_s_gl = "";
|
||||||
|
String c_goojf = "";
|
||||||
|
|
||||||
|
String[] parts = cookies.split(";");
|
||||||
|
for (String part : parts) {
|
||||||
|
String trimmedPart = part.trim();
|
||||||
|
if (trimmedPart.startsWith("s_gl")) {
|
||||||
|
c_s_gl = trimmedPart;
|
||||||
|
}
|
||||||
|
if (trimmedPart.startsWith("goojf")) {
|
||||||
|
c_goojf = trimmedPart;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (c_s_gl.length() > 0 && c_goojf.length() > 0) {
|
||||||
|
// addCookie(c_s_gl);
|
||||||
|
// addCookie(c_goojf);
|
||||||
|
// Youtube seems to also need the other cookies:
|
||||||
|
addCookie(cookies);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addCookie(String cookie) {
|
||||||
|
if (foundCookies.isEmpty() || foundCookies.endsWith("; ")) {
|
||||||
|
foundCookies += cookie;
|
||||||
|
} else if (foundCookies.endsWith(";")) {
|
||||||
|
foundCookies += " " + cookie;
|
||||||
|
} else {
|
||||||
|
foundCookies += "; " + cookie;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue