2017-09-03 08:04:18 +02:00
|
|
|
package org.schabi.newpipe.settings;
|
|
|
|
|
2021-05-31 12:40:14 +02:00
|
|
|
import android.annotation.SuppressLint;
|
2017-10-09 14:22:27 +02:00
|
|
|
import android.app.Activity;
|
2020-03-15 22:53:29 +01:00
|
|
|
import android.content.Context;
|
2018-01-28 19:02:34 +01:00
|
|
|
import android.content.Intent;
|
2018-04-19 01:31:25 +02:00
|
|
|
import android.content.SharedPreferences;
|
2017-09-03 08:04:18 +02:00
|
|
|
import android.os.Bundle;
|
2020-01-31 22:49:43 +01:00
|
|
|
import android.util.Log;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
2019-10-04 14:59:08 +02:00
|
|
|
import androidx.annotation.NonNull;
|
2021-04-01 21:46:29 +02:00
|
|
|
import androidx.appcompat.app.AlertDialog;
|
2020-12-15 03:32:25 +01:00
|
|
|
import androidx.core.content.ContextCompat;
|
2019-10-04 14:59:08 +02:00
|
|
|
import androidx.preference.Preference;
|
2020-10-08 19:32:47 +02:00
|
|
|
import androidx.preference.PreferenceManager;
|
2017-09-03 08:04:18 +02:00
|
|
|
|
2018-03-08 14:39:24 +01:00
|
|
|
import com.nononsenseapps.filepicker.Utils;
|
2018-03-16 04:07:20 +01:00
|
|
|
import com.nostra13.universalimageloader.core.ImageLoader;
|
2018-03-08 14:39:24 +01:00
|
|
|
|
2020-03-15 22:53:29 +01:00
|
|
|
import org.schabi.newpipe.DownloaderImpl;
|
2020-02-21 13:37:19 +01:00
|
|
|
import org.schabi.newpipe.NewPipeDatabase;
|
2017-09-03 08:04:18 +02:00
|
|
|
import org.schabi.newpipe.R;
|
2020-12-11 14:55:47 +01:00
|
|
|
import org.schabi.newpipe.error.ErrorActivity;
|
2020-12-09 12:42:01 +01:00
|
|
|
import org.schabi.newpipe.error.ReCaptchaActivity;
|
2019-10-20 17:31:46 +02:00
|
|
|
import org.schabi.newpipe.extractor.NewPipe;
|
2019-10-28 03:35:51 +01:00
|
|
|
import org.schabi.newpipe.extractor.localization.ContentCountry;
|
|
|
|
import org.schabi.newpipe.extractor.localization.Localization;
|
2021-05-21 17:51:58 +02:00
|
|
|
import org.schabi.newpipe.util.FilePathUtils;
|
2021-05-31 12:36:21 +02:00
|
|
|
import org.schabi.newpipe.util.FilePickerActivityHelper;
|
2018-01-28 19:02:34 +01:00
|
|
|
import org.schabi.newpipe.util.ZipHelper;
|
|
|
|
|
2018-01-28 21:03:01 +01:00
|
|
|
import java.io.File;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.Locale;
|
2017-09-03 08:04:18 +02:00
|
|
|
|
2020-01-31 22:49:43 +01:00
|
|
|
import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
|
|
|
|
|
2017-09-03 08:04:18 +02:00
|
|
|
public class ContentSettingsFragment extends BasePreferenceFragment {
|
2018-01-28 21:03:01 +01:00
|
|
|
private static final int REQUEST_IMPORT_PATH = 8945;
|
2018-01-28 19:02:34 +01:00
|
|
|
private static final int REQUEST_EXPORT_PATH = 30945;
|
|
|
|
|
2020-12-01 20:12:42 +01:00
|
|
|
private ContentSettingsManager manager;
|
|
|
|
|
2021-05-17 19:27:27 +02:00
|
|
|
private String importExportDataPathKey;
|
|
|
|
|
2018-03-16 04:07:20 +01:00
|
|
|
private String thumbnailLoadToggleKey;
|
2020-04-12 22:13:04 +02:00
|
|
|
private String youtubeRestrictedModeEnabledKey;
|
2018-03-16 04:07:20 +01:00
|
|
|
|
2019-10-28 03:35:51 +01:00
|
|
|
private Localization initialSelectedLocalization;
|
|
|
|
private ContentCountry initialSelectedContentCountry;
|
2020-01-09 09:40:05 +01:00
|
|
|
private String initialLanguage;
|
2019-10-28 03:35:51 +01:00
|
|
|
|
2018-03-16 04:07:20 +01:00
|
|
|
@Override
|
2021-03-22 21:38:36 +01:00
|
|
|
public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) {
|
|
|
|
final File homeDir = ContextCompat.getDataDir(requireContext());
|
|
|
|
manager = new ContentSettingsManager(new NewPipeFileLocator(homeDir));
|
|
|
|
manager.deleteSettingsFile();
|
|
|
|
|
|
|
|
addPreferencesFromResource(R.xml.content_settings);
|
|
|
|
|
2021-05-17 19:27:27 +02:00
|
|
|
importExportDataPathKey = getString(R.string.import_export_data_path);
|
2021-03-22 21:38:36 +01:00
|
|
|
final Preference importDataPreference = findPreference(getString(R.string.import_data));
|
|
|
|
importDataPreference.setOnPreferenceClickListener(p -> {
|
|
|
|
final Intent i = new Intent(getActivity(), FilePickerActivityHelper.class)
|
|
|
|
.putExtra(FilePickerActivityHelper.EXTRA_ALLOW_MULTIPLE, false)
|
|
|
|
.putExtra(FilePickerActivityHelper.EXTRA_ALLOW_CREATE_DIR, false)
|
|
|
|
.putExtra(FilePickerActivityHelper.EXTRA_MODE,
|
|
|
|
FilePickerActivityHelper.MODE_FILE);
|
2021-05-17 19:27:27 +02:00
|
|
|
final String path = defaultPreferences.getString(importExportDataPathKey, "");
|
2021-05-21 17:51:58 +02:00
|
|
|
if (FilePathUtils.isValidDirectoryPath(path)) {
|
2021-05-17 19:27:27 +02:00
|
|
|
i.putExtra(FilePickerActivityHelper.EXTRA_START_PATH, path);
|
|
|
|
}
|
2021-03-22 21:38:36 +01:00
|
|
|
startActivityForResult(i, REQUEST_IMPORT_PATH);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
final Preference exportDataPreference = findPreference(getString(R.string.export_data));
|
|
|
|
exportDataPreference.setOnPreferenceClickListener(p -> {
|
|
|
|
final Intent i = new Intent(getActivity(), FilePickerActivityHelper.class)
|
|
|
|
.putExtra(FilePickerActivityHelper.EXTRA_ALLOW_MULTIPLE, false)
|
|
|
|
.putExtra(FilePickerActivityHelper.EXTRA_ALLOW_CREATE_DIR, true)
|
|
|
|
.putExtra(FilePickerActivityHelper.EXTRA_MODE,
|
|
|
|
FilePickerActivityHelper.MODE_DIR);
|
2021-05-17 19:27:27 +02:00
|
|
|
final String path = defaultPreferences.getString(importExportDataPathKey, "");
|
2021-05-21 17:51:58 +02:00
|
|
|
if (FilePathUtils.isValidDirectoryPath(path)) {
|
2021-05-17 19:27:27 +02:00
|
|
|
i.putExtra(FilePickerActivityHelper.EXTRA_START_PATH, path);
|
|
|
|
}
|
2021-03-22 21:38:36 +01:00
|
|
|
startActivityForResult(i, REQUEST_EXPORT_PATH);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2018-03-16 04:07:20 +01:00
|
|
|
thumbnailLoadToggleKey = getString(R.string.download_thumbnail_key);
|
2020-04-12 22:13:04 +02:00
|
|
|
youtubeRestrictedModeEnabledKey = getString(R.string.youtube_restricted_mode_enabled);
|
2019-10-28 03:35:51 +01:00
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
initialSelectedLocalization = org.schabi.newpipe.util.Localization
|
|
|
|
.getPreferredLocalization(requireContext());
|
|
|
|
initialSelectedContentCountry = org.schabi.newpipe.util.Localization
|
|
|
|
.getPreferredContentCountry(requireContext());
|
|
|
|
initialLanguage = PreferenceManager
|
2020-08-27 22:55:57 +02:00
|
|
|
.getDefaultSharedPreferences(requireContext()).getString("app_language_key", "en");
|
2020-10-08 19:32:47 +02:00
|
|
|
|
|
|
|
final Preference clearCookiePref = findPreference(getString(R.string.clear_cookie_key));
|
|
|
|
|
|
|
|
clearCookiePref.setOnPreferenceClickListener(preference -> {
|
|
|
|
defaultPreferences.edit()
|
|
|
|
.putString(getString(R.string.recaptcha_cookies_key), "").apply();
|
|
|
|
DownloaderImpl.getInstance().setCookie(ReCaptchaActivity.RECAPTCHA_COOKIES_KEY, "");
|
|
|
|
Toast.makeText(getActivity(), R.string.recaptcha_cookies_cleared,
|
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
clearCookiePref.setVisible(false);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (defaultPreferences.getString(getString(R.string.recaptcha_cookies_key), "").isEmpty()) {
|
|
|
|
clearCookiePref.setVisible(false);
|
|
|
|
}
|
2018-03-16 04:07:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-03-31 19:20:15 +02:00
|
|
|
public boolean onPreferenceTreeClick(final Preference preference) {
|
2018-03-16 04:07:20 +01:00
|
|
|
if (preference.getKey().equals(thumbnailLoadToggleKey)) {
|
|
|
|
final ImageLoader imageLoader = ImageLoader.getInstance();
|
|
|
|
imageLoader.stop();
|
|
|
|
imageLoader.clearDiskCache();
|
|
|
|
imageLoader.clearMemoryCache();
|
|
|
|
imageLoader.resume();
|
|
|
|
Toast.makeText(preference.getContext(), R.string.thumbnail_cache_wipe_complete_notice,
|
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
|
2020-04-12 22:13:04 +02:00
|
|
|
if (preference.getKey().equals(youtubeRestrictedModeEnabledKey)) {
|
2020-08-16 10:24:58 +02:00
|
|
|
final Context context = getContext();
|
2020-03-16 02:04:34 +01:00
|
|
|
if (context != null) {
|
2020-04-12 22:13:04 +02:00
|
|
|
DownloaderImpl.getInstance().updateYoutubeRestrictedModeCookies(context);
|
2020-03-16 05:17:13 +01:00
|
|
|
} else {
|
|
|
|
Log.w(TAG, "onPreferenceTreeClick: null context");
|
2020-03-15 22:53:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-16 04:07:20 +01:00
|
|
|
return super.onPreferenceTreeClick(preference);
|
|
|
|
}
|
|
|
|
|
2019-10-28 03:35:51 +01:00
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
2018-10-05 16:31:23 +02:00
|
|
|
|
2019-10-28 03:35:51 +01:00
|
|
|
final Localization selectedLocalization = org.schabi.newpipe.util.Localization
|
|
|
|
.getPreferredLocalization(requireContext());
|
|
|
|
final ContentCountry selectedContentCountry = org.schabi.newpipe.util.Localization
|
|
|
|
.getPreferredContentCountry(requireContext());
|
2020-03-31 19:20:15 +02:00
|
|
|
final String selectedLanguage = PreferenceManager
|
2020-08-27 22:55:57 +02:00
|
|
|
.getDefaultSharedPreferences(requireContext()).getString("app_language_key", "en");
|
2019-10-28 03:35:51 +01:00
|
|
|
|
|
|
|
if (!selectedLocalization.equals(initialSelectedLocalization)
|
2020-03-31 19:20:15 +02:00
|
|
|
|| !selectedContentCountry.equals(initialSelectedContentCountry)
|
|
|
|
|| !selectedLanguage.equals(initialLanguage)) {
|
|
|
|
Toast.makeText(requireContext(), R.string.localization_changes_requires_app_restart,
|
|
|
|
Toast.LENGTH_LONG).show();
|
2019-10-20 17:31:46 +02:00
|
|
|
|
|
|
|
NewPipe.setupLocalization(selectedLocalization, selectedContentCountry);
|
2019-10-28 03:35:51 +01:00
|
|
|
}
|
2018-01-28 19:02:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-03-31 19:20:15 +02:00
|
|
|
public void onActivityResult(final int requestCode, final int resultCode,
|
|
|
|
@NonNull final Intent data) {
|
2020-01-31 22:49:43 +01:00
|
|
|
assureCorrectAppLanguage(getContext());
|
2018-01-28 19:02:34 +01:00
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
|
|
if (DEBUG) {
|
2020-03-31 19:20:15 +02:00
|
|
|
Log.d(TAG, "onActivityResult() called with: "
|
|
|
|
+ "requestCode = [" + requestCode + "], "
|
|
|
|
+ "resultCode = [" + resultCode + "], "
|
|
|
|
+ "data = [" + data + "]");
|
2018-01-28 19:02:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((requestCode == REQUEST_IMPORT_PATH || requestCode == REQUEST_EXPORT_PATH)
|
2018-03-08 14:39:24 +01:00
|
|
|
&& resultCode == Activity.RESULT_OK && data.getData() != null) {
|
2021-05-17 19:27:27 +02:00
|
|
|
final File file = Utils.getFileForUri(data.getData());
|
|
|
|
|
2018-04-19 01:31:25 +02:00
|
|
|
if (requestCode == REQUEST_EXPORT_PATH) {
|
2021-05-31 12:36:21 +02:00
|
|
|
exportDatabase(file);
|
2018-04-19 01:31:25 +02:00
|
|
|
} else {
|
2021-04-01 21:46:29 +02:00
|
|
|
final AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
|
2018-04-19 01:31:25 +02:00
|
|
|
builder.setMessage(R.string.override_current_data)
|
2020-01-31 22:49:43 +01:00
|
|
|
.setPositiveButton(getString(R.string.finish),
|
2021-05-31 12:38:21 +02:00
|
|
|
(d, id) -> importDatabase(file))
|
2018-04-19 01:31:25 +02:00
|
|
|
.setNegativeButton(android.R.string.cancel,
|
2020-11-20 00:54:27 +01:00
|
|
|
(d, id) -> d.cancel());
|
2018-04-19 01:31:25 +02:00
|
|
|
builder.create().show();
|
|
|
|
}
|
2018-01-28 19:02:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-31 15:09:57 +02:00
|
|
|
private void exportDatabase(@NonNull final File folder) {
|
2018-01-28 19:02:34 +01:00
|
|
|
try {
|
2021-05-31 12:36:21 +02:00
|
|
|
final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US);
|
|
|
|
final String path = folder.getAbsolutePath() + "/NewPipeData-"
|
|
|
|
+ sdf.format(new Date()) + ".zip";
|
|
|
|
|
2020-02-21 13:37:19 +01:00
|
|
|
//checkpoint before export
|
2020-02-21 14:13:01 +01:00
|
|
|
NewPipeDatabase.checkpoint();
|
2020-02-21 13:37:19 +01:00
|
|
|
|
2020-12-01 20:12:42 +01:00
|
|
|
final SharedPreferences preferences = PreferenceManager
|
2021-05-31 12:36:21 +02:00
|
|
|
.getDefaultSharedPreferences(requireContext());
|
2020-12-01 20:12:42 +01:00
|
|
|
manager.exportDatabase(preferences, path);
|
2018-01-28 19:02:34 +01:00
|
|
|
|
2021-05-31 12:40:14 +02:00
|
|
|
setImportExportDataPath(folder, false);
|
2021-05-31 12:36:21 +02:00
|
|
|
|
2020-12-01 20:12:42 +01:00
|
|
|
Toast.makeText(getContext(), R.string.export_complete_toast, Toast.LENGTH_SHORT).show();
|
2020-08-16 10:24:58 +02:00
|
|
|
} catch (final Exception e) {
|
2021-01-09 16:33:59 +01:00
|
|
|
ErrorActivity.reportUiErrorInSnackbar(this, "Exporting database", e);
|
2018-01-28 19:02:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-31 15:09:57 +02:00
|
|
|
private void importDatabase(@NonNull final File file) {
|
2021-05-31 12:38:21 +02:00
|
|
|
final String filePath = file.getAbsolutePath();
|
|
|
|
|
2018-01-28 21:03:01 +01:00
|
|
|
// check if file is supported
|
2020-12-19 14:14:48 +01:00
|
|
|
if (!ZipHelper.isValidZipFile(filePath)) {
|
2018-01-28 21:03:01 +01:00
|
|
|
Toast.makeText(getContext(), R.string.no_valid_zip_file, Toast.LENGTH_SHORT)
|
2021-05-31 12:38:21 +02:00
|
|
|
.show();
|
2018-01-28 21:03:01 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2020-12-19 11:22:25 +01:00
|
|
|
if (!manager.ensureDbDirectoryExists()) {
|
2018-01-28 21:03:01 +01:00
|
|
|
throw new Exception("Could not create databases dir");
|
|
|
|
}
|
2018-01-28 19:02:34 +01:00
|
|
|
|
2020-12-19 11:22:25 +01:00
|
|
|
if (!manager.extractDb(filePath)) {
|
2018-04-19 01:31:25 +02:00
|
|
|
Toast.makeText(getContext(), R.string.could_not_import_all_files, Toast.LENGTH_LONG)
|
2021-05-31 12:40:14 +02:00
|
|
|
.show();
|
2018-01-28 21:03:01 +01:00
|
|
|
}
|
|
|
|
|
2018-04-19 01:31:25 +02:00
|
|
|
//If settings file exist, ask if it should be imported.
|
2020-12-19 16:28:10 +01:00
|
|
|
if (manager.extractSettings(filePath)) {
|
2021-04-01 21:46:29 +02:00
|
|
|
final AlertDialog.Builder alert = new AlertDialog.Builder(requireContext());
|
2018-04-19 01:31:25 +02:00
|
|
|
alert.setTitle(R.string.import_settings);
|
|
|
|
|
2018-05-11 17:17:07 +02:00
|
|
|
alert.setNegativeButton(android.R.string.no, (dialog, which) -> {
|
|
|
|
dialog.dismiss();
|
2021-05-31 12:38:21 +02:00
|
|
|
finishImport(file);
|
2018-04-19 01:31:25 +02:00
|
|
|
});
|
2020-01-31 22:49:43 +01:00
|
|
|
alert.setPositiveButton(getString(R.string.finish), (dialog, which) -> {
|
2018-05-11 17:17:07 +02:00
|
|
|
dialog.dismiss();
|
2020-12-19 11:22:25 +01:00
|
|
|
manager.loadSharedPreferences(PreferenceManager
|
2021-05-31 12:38:21 +02:00
|
|
|
.getDefaultSharedPreferences(requireContext()));
|
|
|
|
finishImport(file);
|
2018-04-19 01:31:25 +02:00
|
|
|
});
|
|
|
|
alert.show();
|
|
|
|
} else {
|
2021-05-31 12:38:21 +02:00
|
|
|
finishImport(file);
|
2018-04-19 01:31:25 +02:00
|
|
|
}
|
2020-08-16 10:24:58 +02:00
|
|
|
} catch (final Exception e) {
|
2021-01-09 16:33:59 +01:00
|
|
|
ErrorActivity.reportUiErrorInSnackbar(this, "Importing database", e);
|
2018-01-28 21:03:01 +01:00
|
|
|
}
|
2017-09-26 17:29:38 +02:00
|
|
|
}
|
2021-05-17 19:27:27 +02:00
|
|
|
|
2021-05-31 12:38:21 +02:00
|
|
|
/**
|
|
|
|
* Save import path and restart system.
|
|
|
|
*
|
|
|
|
* @param file The file of the created backup
|
|
|
|
*/
|
2021-05-31 15:09:57 +02:00
|
|
|
private void finishImport(@NonNull final File file) {
|
2021-05-31 12:38:21 +02:00
|
|
|
if (file.getParentFile() != null) {
|
2021-05-31 12:40:14 +02:00
|
|
|
//immediately because app is about to exit
|
|
|
|
setImportExportDataPath(file.getParentFile(), true);
|
2021-05-31 12:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// restart app to properly load db
|
|
|
|
System.exit(0);
|
|
|
|
}
|
|
|
|
|
2021-05-31 12:40:14 +02:00
|
|
|
@SuppressLint("ApplySharedPref")
|
2021-05-31 15:09:57 +02:00
|
|
|
private void setImportExportDataPath(@NonNull final File file, final boolean immediately) {
|
2021-05-17 19:27:27 +02:00
|
|
|
final String directoryPath;
|
2021-05-21 17:54:57 +02:00
|
|
|
if (file.isDirectory()) {
|
|
|
|
directoryPath = file.getAbsolutePath();
|
|
|
|
} else {
|
2021-05-17 19:27:27 +02:00
|
|
|
final File parentFile = file.getParentFile();
|
|
|
|
if (parentFile != null) {
|
|
|
|
directoryPath = parentFile.getAbsolutePath();
|
|
|
|
} else {
|
|
|
|
directoryPath = "";
|
|
|
|
}
|
|
|
|
}
|
2021-05-31 12:40:14 +02:00
|
|
|
final SharedPreferences.Editor editor = defaultPreferences
|
|
|
|
.edit()
|
|
|
|
.putString(importExportDataPathKey, directoryPath);
|
|
|
|
if (immediately) {
|
|
|
|
editor.commit();
|
|
|
|
} else {
|
|
|
|
editor.apply();
|
|
|
|
}
|
2021-05-17 19:27:27 +02:00
|
|
|
}
|
2017-09-03 08:04:18 +02:00
|
|
|
}
|