Save and restore cookies to/from preferences

So that the user does not have to solve a recaptcha every time he opens the app
This commit is contained in:
Stypox 2020-04-11 11:51:40 +02:00
parent b8efef7c7a
commit ee5ce0c809
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
4 changed files with 27 additions and 5 deletions

View File

@ -40,8 +40,10 @@ public class DebugApp extends App {
@Override @Override
protected Downloader getDownloader() { protected Downloader getDownloader() {
return DownloaderImpl.init(new OkHttpClient.Builder() DownloaderImpl downloader = DownloaderImpl.init(new OkHttpClient.Builder()
.addNetworkInterceptor(new StethoInterceptor())); .addNetworkInterceptor(new StethoInterceptor()));
setCookiesToDownloader(downloader);
return downloader;
} }
private void initStetho() { private void initStetho() {

View File

@ -5,10 +5,12 @@ import android.app.Application;
import android.app.NotificationChannel; import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build; import android.os.Build;
import android.util.Log; import android.util.Log;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.preference.PreferenceManager;
import com.nostra13.universalimageloader.cache.memory.impl.LRULimitedMemoryCache; import com.nostra13.universalimageloader.cache.memory.impl.LRULimitedMemoryCache;
import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoader;
@ -125,7 +127,16 @@ public class App extends Application {
} }
protected Downloader getDownloader() { protected Downloader getDownloader() {
return DownloaderImpl.init(null); DownloaderImpl downloader = DownloaderImpl.init(null);
setCookiesToDownloader(downloader);
return downloader;
}
protected void setCookiesToDownloader(final DownloaderImpl downloader) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(
getApplicationContext());
final String key = getApplicationContext().getString(R.string.recaptcha_cookies_key);
downloader.setCookies(prefs.getString(key, ""));
} }
private void configureRxJavaErrorHandler() { private void configureRxJavaErrorHandler() {

View File

@ -1,6 +1,7 @@
package org.schabi.newpipe; package org.schabi.newpipe;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
@ -19,6 +20,7 @@ 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 androidx.core.app.NavUtils; import androidx.core.app.NavUtils;
import androidx.preference.PreferenceManager;
import org.schabi.newpipe.util.ThemeHelper; import org.schabi.newpipe.util.ThemeHelper;
@ -159,6 +161,12 @@ public class ReCaptchaActivity extends AppCompatActivity {
} }
if (!foundCookies.isEmpty()) { if (!foundCookies.isEmpty()) {
// save cookies to preferences
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(
getApplicationContext());
final String key = getApplicationContext().getString(R.string.recaptcha_cookies_key);
prefs.edit().putString(key, foundCookies).apply();
// give cookies to Downloader class // give cookies to Downloader class
DownloaderImpl.getInstance().setCookies(foundCookies); DownloaderImpl.getInstance().setCookies(foundCookies);
setResult(RESULT_OK); setResult(RESULT_OK);
@ -170,7 +178,7 @@ public class ReCaptchaActivity extends AppCompatActivity {
} }
private void handleCookiesFromUrl(final @Nullable String url) { private void handleCookiesFromUrl(@Nullable final String url) {
if (MainActivity.DEBUG) { if (MainActivity.DEBUG) {
Log.d(TAG, "handleCookiesFromUrl: url=" + (url == null ? "null" : url)); Log.d(TAG, "handleCookiesFromUrl: url=" + (url == null ? "null" : url));
} }
@ -201,7 +209,7 @@ public class ReCaptchaActivity extends AppCompatActivity {
} }
} }
private void handleCookies(final @Nullable String cookies) { private void handleCookies(@Nullable final String cookies) {
if (MainActivity.DEBUG) { if (MainActivity.DEBUG) {
Log.d(TAG, "handleCookies: cookies=" + (cookies == null ? "null" : cookies)); Log.d(TAG, "handleCookies: cookies=" + (cookies == null ? "null" : cookies));
} }
@ -214,7 +222,7 @@ public class ReCaptchaActivity extends AppCompatActivity {
// add here methods to extract cookies for other services // add here methods to extract cookies for other services
} }
private void addYoutubeCookies(final @NonNull String cookies) { private void addYoutubeCookies(@NonNull final String cookies) {
if (cookies.contains("s_gl=") || cookies.contains("goojf=") if (cookies.contains("s_gl=") || cookies.contains("goojf=")
|| cookies.contains("VISITOR_INFO1_LIVE=") || cookies.contains("VISITOR_INFO1_LIVE=")
|| cookies.contains("GOOGLE_ABUSE_EXEMPTION=")) { || cookies.contains("GOOGLE_ABUSE_EXEMPTION=")) {

View File

@ -1129,4 +1129,5 @@
<item>@string/grid</item> <item>@string/grid</item>
</string-array> </string-array>
<string name="recaptcha_cookies_key" translatable="false">recaptcha_cookies_key</string>
</resources> </resources>