2016-12-07 18:34:39 +01:00
|
|
|
package org.schabi.newpipe;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.Intent;
|
2017-01-02 17:30:35 +01:00
|
|
|
import android.graphics.Bitmap;
|
2016-12-07 18:34:39 +01:00
|
|
|
import android.os.Build;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.support.v4.app.NavUtils;
|
|
|
|
import android.support.v7.app.ActionBar;
|
|
|
|
import android.support.v7.app.AppCompatActivity;
|
2017-04-26 21:32:20 +02:00
|
|
|
import android.support.v7.widget.Toolbar;
|
2016-12-07 18:34:39 +01:00
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.webkit.CookieManager;
|
|
|
|
import android.webkit.ValueCallback;
|
|
|
|
import android.webkit.WebSettings;
|
|
|
|
import android.webkit.WebView;
|
|
|
|
import android.webkit.WebViewClient;
|
|
|
|
|
2017-09-03 08:04:18 +02:00
|
|
|
/*
|
2016-12-07 18:34:39 +01:00
|
|
|
* Created by beneth <bmauduit@beneth.fr> on 06.12.16.
|
|
|
|
*
|
|
|
|
* Copyright (C) Christian Schabesberger 2015 <chris.schabesberger@mailbox.org>
|
|
|
|
* ReCaptchaActivity.java is part of NewPipe.
|
|
|
|
*
|
|
|
|
* NewPipe is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* NewPipe is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
public class ReCaptchaActivity extends AppCompatActivity {
|
2017-01-02 17:30:35 +01:00
|
|
|
public static final int RECAPTCHA_REQUEST = 10;
|
|
|
|
|
2016-12-07 18:34:39 +01:00
|
|
|
public static final String TAG = ReCaptchaActivity.class.toString();
|
|
|
|
public static final String YT_URL = "https://www.youtube.com";
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_recaptcha);
|
|
|
|
|
2017-01-02 17:30:35 +01:00
|
|
|
// Set return to Cancel by default
|
|
|
|
setResult(RESULT_CANCELED);
|
|
|
|
|
2017-09-03 08:04:18 +02:00
|
|
|
Toolbar toolbar = findViewById(R.id.toolbar);
|
2017-04-26 21:32:20 +02:00
|
|
|
setSupportActionBar(toolbar);
|
|
|
|
|
2016-12-07 18:34:39 +01:00
|
|
|
ActionBar actionBar = getSupportActionBar();
|
2017-04-26 21:32:20 +02:00
|
|
|
if (actionBar != null) {
|
|
|
|
actionBar.setDisplayHomeAsUpEnabled(true);
|
|
|
|
actionBar.setTitle(R.string.reCaptcha_title);
|
|
|
|
actionBar.setDisplayShowTitleEnabled(true);
|
|
|
|
}
|
2016-12-07 18:34:39 +01:00
|
|
|
|
2017-09-03 08:04:18 +02:00
|
|
|
WebView myWebView = findViewById(R.id.reCaptchaWebView);
|
2016-12-07 18:34:39 +01:00
|
|
|
|
|
|
|
// Enable Javascript
|
|
|
|
WebSettings webSettings = myWebView.getSettings();
|
|
|
|
webSettings.setJavaScriptEnabled(true);
|
|
|
|
|
|
|
|
ReCaptchaWebViewClient webClient = new ReCaptchaWebViewClient(this);
|
|
|
|
myWebView.setWebViewClient(webClient);
|
|
|
|
|
|
|
|
// Cleaning cache, history and cookies from webView
|
|
|
|
myWebView.clearCache(true);
|
|
|
|
myWebView.clearHistory();
|
|
|
|
android.webkit.CookieManager cookieManager = CookieManager.getInstance();
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
|
cookieManager.removeAllCookies(new ValueCallback<Boolean>() {
|
|
|
|
@Override
|
|
|
|
public void onReceiveValue(Boolean aBoolean) {}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
cookieManager.removeAllCookie();
|
|
|
|
}
|
|
|
|
|
|
|
|
myWebView.loadUrl(YT_URL);
|
|
|
|
}
|
|
|
|
|
|
|
|
private class ReCaptchaWebViewClient extends WebViewClient {
|
2018-08-28 20:02:25 +02:00
|
|
|
private final Activity context;
|
2016-12-07 18:34:39 +01:00
|
|
|
private String mCookies;
|
|
|
|
|
|
|
|
ReCaptchaWebViewClient(Activity ctx) {
|
|
|
|
context = ctx;
|
|
|
|
}
|
|
|
|
|
2017-01-02 17:30:35 +01:00
|
|
|
@Override
|
|
|
|
public void onPageStarted(WebView view, String url, Bitmap favicon) {
|
|
|
|
// TODO: Start Loader
|
|
|
|
super.onPageStarted(view, url, favicon);
|
|
|
|
}
|
|
|
|
|
2016-12-07 18:34:39 +01:00
|
|
|
@Override
|
|
|
|
public void onPageFinished(WebView view, String url) {
|
|
|
|
String cookies = CookieManager.getInstance().getCookie(url);
|
|
|
|
|
2017-01-02 17:30:35 +01:00
|
|
|
// TODO: Stop Loader
|
|
|
|
|
2016-12-07 18:34:39 +01:00
|
|
|
// find cookies : s_gl & goojf and Add cookies to Downloader
|
|
|
|
if (find_access_cookies(cookies)) {
|
|
|
|
// Give cookies to Downloader class
|
2018-02-21 12:05:23 +01:00
|
|
|
Downloader.getInstance().setCookies(mCookies);
|
2016-12-07 18:34:39 +01:00
|
|
|
|
2017-01-02 17:30:35 +01:00
|
|
|
// Closing activity and return to parent
|
|
|
|
setResult(RESULT_OK);
|
|
|
|
finish();
|
2016-12-07 18:34:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
int id = item.getItemId();
|
|
|
|
switch (id) {
|
|
|
|
case android.R.id.home: {
|
|
|
|
Intent intent = new Intent(this, org.schabi.newpipe.MainActivity.class);
|
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
|
|
NavUtils.navigateUpTo(this, intent);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|