2016-12-07 18:34:39 +01:00
|
|
|
package org.schabi.newpipe;
|
|
|
|
|
|
|
|
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;
|
2019-10-04 14:59:08 +02:00
|
|
|
import androidx.core.app.NavUtils;
|
|
|
|
import androidx.appcompat.app.ActionBar;
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
import androidx.appcompat.widget.Toolbar;
|
2020-01-29 19:36:57 +01:00
|
|
|
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.Menu;
|
2016-12-07 18:34:39 +01:00
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.webkit.CookieManager;
|
|
|
|
import android.webkit.WebSettings;
|
|
|
|
import android.webkit.WebView;
|
|
|
|
import android.webkit.WebViewClient;
|
|
|
|
|
2020-02-01 17:53:43 +01:00
|
|
|
import org.schabi.newpipe.util.ThemeHelper;
|
|
|
|
|
2020-01-29 19:36:57 +01:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
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;
|
2019-08-17 09:30:42 +02:00
|
|
|
public static final String RECAPTCHA_URL_EXTRA = "recaptcha_url_extra";
|
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";
|
|
|
|
|
2020-02-01 17:59:16 +01:00
|
|
|
private WebView webView;
|
2020-01-29 19:36:57 +01:00
|
|
|
private String foundCookies = "";
|
2019-08-17 09:30:42 +02:00
|
|
|
|
2016-12-07 18:34:39 +01:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
2020-02-01 17:53:43 +01:00
|
|
|
ThemeHelper.setTheme(this);
|
2016-12-07 18:34:39 +01:00
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_recaptcha);
|
2020-01-30 21:05:57 +01:00
|
|
|
Toolbar toolbar = findViewById(R.id.toolbar);
|
|
|
|
setSupportActionBar(toolbar);
|
2016-12-07 18:34:39 +01:00
|
|
|
|
2020-01-29 19:36:57 +01:00
|
|
|
String url = getIntent().getStringExtra(RECAPTCHA_URL_EXTRA);
|
2019-08-17 09:30:42 +02:00
|
|
|
if (url == null || url.isEmpty()) {
|
|
|
|
url = YT_URL;
|
|
|
|
}
|
|
|
|
|
2017-01-02 17:30:35 +01:00
|
|
|
// Set return to Cancel by default
|
|
|
|
setResult(RESULT_CANCELED);
|
|
|
|
|
2017-04-26 21:32:20 +02:00
|
|
|
|
2020-02-01 17:59:16 +01:00
|
|
|
webView = findViewById(R.id.reCaptchaWebView);
|
2016-12-07 18:34:39 +01:00
|
|
|
|
|
|
|
// Enable Javascript
|
2020-02-01 17:59:16 +01:00
|
|
|
WebSettings webSettings = webView.getSettings();
|
2016-12-07 18:34:39 +01:00
|
|
|
webSettings.setJavaScriptEnabled(true);
|
|
|
|
|
2020-02-01 17:59:16 +01:00
|
|
|
webView.setWebViewClient(new WebViewClient() {
|
2020-01-29 19:36:57 +01:00
|
|
|
@Override
|
|
|
|
public void onPageFinished(WebView view, String url) {
|
|
|
|
super.onPageFinished(view, url);
|
2020-02-01 17:59:16 +01:00
|
|
|
handleCookies(url);
|
2020-01-29 19:36:57 +01:00
|
|
|
}
|
|
|
|
});
|
2016-12-07 18:34:39 +01:00
|
|
|
|
|
|
|
// Cleaning cache, history and cookies from webView
|
2020-02-01 17:59:16 +01:00
|
|
|
webView.clearCache(true);
|
|
|
|
webView.clearHistory();
|
2020-01-30 21:05:57 +01:00
|
|
|
android.webkit.CookieManager cookieManager = CookieManager.getInstance();
|
2016-12-07 18:34:39 +01:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
2019-08-17 09:30:42 +02:00
|
|
|
cookieManager.removeAllCookies(aBoolean -> {});
|
2016-12-07 18:34:39 +01:00
|
|
|
} else {
|
|
|
|
cookieManager.removeAllCookie();
|
|
|
|
}
|
|
|
|
|
2020-02-01 17:59:16 +01:00
|
|
|
webView.loadUrl(url);
|
2016-12-07 18:34:39 +01:00
|
|
|
}
|
|
|
|
|
2020-01-29 19:36:57 +01:00
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
2020-02-01 17:53:43 +01:00
|
|
|
getMenuInflater().inflate(R.menu.menu_recaptcha, menu);
|
2016-12-07 18:34:39 +01:00
|
|
|
|
2020-01-29 19:36:57 +01:00
|
|
|
ActionBar actionBar = getSupportActionBar();
|
|
|
|
if (actionBar != null) {
|
2020-02-01 17:53:43 +01:00
|
|
|
actionBar.setDisplayHomeAsUpEnabled(false);
|
2020-01-29 19:36:57 +01:00
|
|
|
actionBar.setTitle(R.string.title_activity_recaptcha);
|
|
|
|
actionBar.setSubtitle(R.string.subtitle_activity_recaptcha);
|
2016-12-07 18:34:39 +01:00
|
|
|
}
|
|
|
|
|
2020-02-01 17:53:43 +01:00
|
|
|
return true;
|
2020-01-29 19:36:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBackPressed() {
|
|
|
|
saveCookiesAndFinish();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
int id = item.getItemId();
|
|
|
|
switch (id) {
|
2020-02-01 17:53:43 +01:00
|
|
|
case R.id.menu_item_done:
|
2020-01-29 19:36:57 +01:00
|
|
|
saveCookiesAndFinish();
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
2017-01-02 17:30:35 +01:00
|
|
|
}
|
2020-01-29 19:36:57 +01:00
|
|
|
}
|
2017-01-02 17:30:35 +01:00
|
|
|
|
2020-01-29 19:36:57 +01:00
|
|
|
private void saveCookiesAndFinish() {
|
2020-02-01 17:59:16 +01:00
|
|
|
handleCookies(webView.getUrl()); // try to get cookies of unclosed page
|
2020-01-29 19:36:57 +01:00
|
|
|
if (!foundCookies.isEmpty()) {
|
|
|
|
// Give cookies to Downloader class
|
|
|
|
DownloaderImpl.getInstance().setCookies(foundCookies);
|
|
|
|
setResult(RESULT_OK);
|
|
|
|
}
|
2016-12-07 18:34:39 +01:00
|
|
|
|
2020-01-29 19:36:57 +01:00
|
|
|
Intent intent = new Intent(this, org.schabi.newpipe.MainActivity.class);
|
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
|
|
NavUtils.navigateUpTo(this, intent);
|
|
|
|
}
|
2017-01-02 17:30:35 +01:00
|
|
|
|
2016-12-07 18:34:39 +01:00
|
|
|
|
|
|
|
|
2020-02-01 17:59:16 +01:00
|
|
|
private void handleCookies(String url) {
|
|
|
|
String cookies = CookieManager.getInstance().getCookie(url);
|
|
|
|
if (MainActivity.DEBUG) Log.d(TAG, "handleCookies: url=" + url + "; cookies=" + (cookies == null ? "null" : cookies));
|
2020-01-29 19:36:57 +01:00
|
|
|
if (cookies == null) return;
|
|
|
|
|
|
|
|
addYoutubeCookies(cookies);
|
|
|
|
// add other methods to extract cookies here
|
|
|
|
}
|
|
|
|
|
|
|
|
private void addYoutubeCookies(@Nonnull String cookies) {
|
2020-02-02 21:33:07 +01:00
|
|
|
if (cookies.contains("s_gl=") || cookies.contains("goojf=") || cookies.contains("VISITOR_INFO1_LIVE=")) {
|
2020-01-29 19:36:57 +01:00
|
|
|
// Youtube seems to also need the other cookies:
|
|
|
|
addCookie(cookies);
|
2016-12-07 18:34:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 19:36:57 +01:00
|
|
|
private void addCookie(String cookie) {
|
2020-02-02 21:33:07 +01:00
|
|
|
if (foundCookies.contains(cookie)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-29 19:36:57 +01:00
|
|
|
if (foundCookies.isEmpty() || foundCookies.endsWith("; ")) {
|
|
|
|
foundCookies += cookie;
|
|
|
|
} else if (foundCookies.endsWith(";")) {
|
|
|
|
foundCookies += " " + cookie;
|
|
|
|
} else {
|
|
|
|
foundCookies += "; " + cookie;
|
2016-12-07 18:34:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|