NewPipe/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java

319 lines
13 KiB
Java
Raw Normal View History

package org.schabi.newpipe.settings;
2017-10-09 14:22:27 +02:00
import android.app.Activity;
import android.app.AlertDialog;
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;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
2019-10-04 14:59:08 +02:00
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
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;
import androidx.preference.PreferenceManager;
import com.nononsenseapps.filepicker.Utils;
import com.nostra13.universalimageloader.core.ImageLoader;
import org.schabi.newpipe.DownloaderImpl;
2020-02-21 13:37:19 +01:00
import org.schabi.newpipe.NewPipeDatabase;
import org.schabi.newpipe.R;
import org.schabi.newpipe.ReCaptchaActivity;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.localization.ContentCountry;
import org.schabi.newpipe.extractor.localization.Localization;
2017-10-09 14:22:27 +02:00
import org.schabi.newpipe.report.ErrorActivity;
import org.schabi.newpipe.report.ErrorInfo;
2017-10-09 14:22:27 +02:00
import org.schabi.newpipe.report.UserAction;
2018-01-28 19:02:34 +01:00
import org.schabi.newpipe.util.FilePickerActivityHelper;
import org.schabi.newpipe.util.ZipHelper;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
2018-04-19 01:31:25 +02:00
import java.io.ObjectInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
2018-04-19 01:31:25 +02:00
import java.util.Map;
import java.util.zip.ZipFile;
import static org.schabi.newpipe.util.Localization.assureCorrectAppLanguage;
public class ContentSettingsFragment extends BasePreferenceFragment {
private static final int REQUEST_IMPORT_PATH = 8945;
2018-01-28 19:02:34 +01:00
private static final int REQUEST_EXPORT_PATH = 30945;
private ContentSettingsManager manager;
private File databasesDir;
private File newpipeDb;
private File newpipeDbJournal;
private File newpipeDbShm;
private File newpipeDbWal;
private File newpipeSettings;
private String thumbnailLoadToggleKey;
private String youtubeRestrictedModeEnabledKey;
private Localization initialSelectedLocalization;
private ContentCountry initialSelectedContentCountry;
private String initialLanguage;
@Override
public void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
thumbnailLoadToggleKey = getString(R.string.download_thumbnail_key);
youtubeRestrictedModeEnabledKey = getString(R.string.youtube_restricted_mode_enabled);
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");
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);
}
}
@Override
public boolean onPreferenceTreeClick(final Preference preference) {
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();
}
if (preference.getKey().equals(youtubeRestrictedModeEnabledKey)) {
2020-08-16 10:24:58 +02:00
final Context context = getContext();
if (context != null) {
DownloaderImpl.getInstance().updateYoutubeRestrictedModeCookies(context);
} else {
Log.w(TAG, "onPreferenceTreeClick: null context");
}
}
return super.onPreferenceTreeClick(preference);
}
@Override
public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) {
2020-12-15 03:32:25 +01:00
final File homeDir = ContextCompat.getDataDir(requireContext());
databasesDir = new File(homeDir, "/databases");
newpipeDb = new File(homeDir, "/databases/newpipe.db");
newpipeDbJournal = new File(homeDir, "/databases/newpipe.db-journal");
newpipeDbShm = new File(homeDir, "/databases/newpipe.db-shm");
newpipeDbWal = new File(homeDir, "/databases/newpipe.db-wal");
newpipeSettings = new File(homeDir, "/databases/newpipe.settings");
newpipeSettings.delete();
manager = new ContentSettingsManager(homeDir);
addPreferencesFromResource(R.xml.content_settings);
2017-09-26 17:29:38 +02:00
2020-08-16 10:24:58 +02:00
final Preference importDataPreference = findPreference(getString(R.string.import_data));
2020-11-20 00:54:27 +01:00
importDataPreference.setOnPreferenceClickListener(p -> {
2020-08-16 10:24:58 +02:00
final Intent i = new Intent(getActivity(), FilePickerActivityHelper.class)
2018-01-28 19:02:34 +01:00
.putExtra(FilePickerActivityHelper.EXTRA_ALLOW_MULTIPLE, false)
.putExtra(FilePickerActivityHelper.EXTRA_ALLOW_CREATE_DIR, false)
.putExtra(FilePickerActivityHelper.EXTRA_MODE,
FilePickerActivityHelper.MODE_FILE);
startActivityForResult(i, REQUEST_IMPORT_PATH);
2018-01-28 19:02:34 +01:00
return true;
});
2020-08-16 10:24:58 +02:00
final Preference exportDataPreference = findPreference(getString(R.string.export_data));
2020-11-20 00:54:27 +01:00
exportDataPreference.setOnPreferenceClickListener(p -> {
2020-08-16 10:24:58 +02:00
final Intent i = new Intent(getActivity(), FilePickerActivityHelper.class)
2018-01-28 19:02:34 +01:00
.putExtra(FilePickerActivityHelper.EXTRA_ALLOW_MULTIPLE, false)
.putExtra(FilePickerActivityHelper.EXTRA_ALLOW_CREATE_DIR, true)
.putExtra(FilePickerActivityHelper.EXTRA_MODE,
FilePickerActivityHelper.MODE_DIR);
2018-01-28 19:02:34 +01:00
startActivityForResult(i, REQUEST_EXPORT_PATH);
return true;
});
}
2018-10-05 16:31:23 +02:00
@Override
public void onDestroy() {
super.onDestroy();
2018-10-05 16:31:23 +02:00
final Localization selectedLocalization = org.schabi.newpipe.util.Localization
.getPreferredLocalization(requireContext());
final ContentCountry selectedContentCountry = org.schabi.newpipe.util.Localization
.getPreferredContentCountry(requireContext());
final String selectedLanguage = PreferenceManager
2020-08-27 22:55:57 +02:00
.getDefaultSharedPreferences(requireContext()).getString("app_language_key", "en");
if (!selectedLocalization.equals(initialSelectedLocalization)
|| !selectedContentCountry.equals(initialSelectedContentCountry)
|| !selectedLanguage.equals(initialLanguage)) {
Toast.makeText(requireContext(), R.string.localization_changes_requires_app_restart,
Toast.LENGTH_LONG).show();
NewPipe.setupLocalization(selectedLocalization, selectedContentCountry);
}
2018-01-28 19:02:34 +01:00
}
@Override
public void onActivityResult(final int requestCode, final int resultCode,
@NonNull final Intent data) {
assureCorrectAppLanguage(getContext());
2018-01-28 19:02:34 +01:00
super.onActivityResult(requestCode, resultCode, data);
if (DEBUG) {
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)
&& resultCode == Activity.RESULT_OK && data.getData() != null) {
2020-08-16 10:24:58 +02:00
final String path = Utils.getFileForUri(data.getData()).getAbsolutePath();
2018-04-19 01:31:25 +02:00
if (requestCode == REQUEST_EXPORT_PATH) {
2020-08-16 10:24:58 +02:00
final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US);
2018-04-19 01:31:25 +02:00
exportDatabase(path + "/NewPipeData-" + sdf.format(new Date()) + ".zip");
} else {
2020-08-16 10:24:58 +02:00
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
2018-04-19 01:31:25 +02:00
builder.setMessage(R.string.override_current_data)
.setPositiveButton(getString(R.string.finish),
2020-11-20 00:54:27 +01:00
(d, id) -> importDatabase(path))
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
}
}
private void exportDatabase(final String path) {
2018-01-28 19:02:34 +01:00
try {
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
final SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(requireContext());
manager.exportDatabase(preferences, path);
2018-01-28 19:02:34 +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) {
2018-01-28 19:02:34 +01:00
onError(e);
}
}
private void importDatabase(final String filePath) {
// check if file is supported
2020-11-06 01:46:13 +01:00
try (ZipFile zipFile = new ZipFile(filePath)) {
2020-08-16 10:24:58 +02:00
} catch (final IOException ioe) {
Toast.makeText(getContext(), R.string.no_valid_zip_file, Toast.LENGTH_SHORT)
.show();
return;
}
try {
if (!databasesDir.exists() && !databasesDir.mkdir()) {
throw new Exception("Could not create databases dir");
}
2018-01-28 19:02:34 +01:00
final boolean isDbFileExtracted = ZipHelper.extractFileFromZip(filePath,
newpipeDb.getPath(), "newpipe.db");
if (isDbFileExtracted) {
newpipeDbJournal.delete();
newpipeDbWal.delete();
newpipeDbShm.delete();
} else {
2018-04-19 01:31:25 +02:00
Toast.makeText(getContext(), R.string.could_not_import_all_files, Toast.LENGTH_LONG)
.show();
}
2018-04-19 01:31:25 +02:00
//If settings file exist, ask if it should be imported.
if (ZipHelper.extractFileFromZip(filePath, newpipeSettings.getPath(),
"newpipe.settings")) {
2020-08-16 10:24:58 +02:00
final AlertDialog.Builder alert = new AlertDialog.Builder(getContext());
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();
// restart app to properly load db
System.exit(0);
2018-04-19 01:31:25 +02:00
});
alert.setPositiveButton(getString(R.string.finish), (dialog, which) -> {
2018-05-11 17:17:07 +02:00
dialog.dismiss();
loadSharedPreferences(newpipeSettings);
2018-05-11 17:17:07 +02:00
// restart app to properly load db
System.exit(0);
2018-04-19 01:31:25 +02:00
});
alert.show();
} else {
// restart app to properly load db
System.exit(0);
}
2020-08-16 10:24:58 +02:00
} catch (final Exception e) {
onError(e);
}
2017-09-26 17:29:38 +02:00
}
private void loadSharedPreferences(final File src) {
2020-11-06 01:46:13 +01:00
try (ObjectInputStream input = new ObjectInputStream(new FileInputStream(src))) {
2020-08-16 10:24:58 +02:00
final SharedPreferences.Editor prefEdit = PreferenceManager
2020-08-27 22:55:57 +02:00
.getDefaultSharedPreferences(requireContext()).edit();
2018-04-19 01:31:25 +02:00
prefEdit.clear();
2020-08-16 10:24:58 +02:00
final Map<String, ?> entries = (Map<String, ?>) input.readObject();
for (final Map.Entry<String, ?> entry : entries.entrySet()) {
final Object v = entry.getValue();
final String key = entry.getKey();
2018-04-19 01:31:25 +02:00
if (v instanceof Boolean) {
2018-08-28 20:06:47 +02:00
prefEdit.putBoolean(key, (Boolean) v);
} else if (v instanceof Float) {
2018-08-28 20:06:47 +02:00
prefEdit.putFloat(key, (Float) v);
} else if (v instanceof Integer) {
2018-08-28 20:06:47 +02:00
prefEdit.putInt(key, (Integer) v);
} else if (v instanceof Long) {
2018-08-28 20:06:47 +02:00
prefEdit.putLong(key, (Long) v);
} else if (v instanceof String) {
prefEdit.putString(key, (String) v);
}
2018-04-19 01:31:25 +02:00
}
2019-08-04 17:27:56 +02:00
prefEdit.commit();
2020-08-16 10:24:58 +02:00
} catch (final IOException | ClassNotFoundException e) {
2018-04-19 01:31:25 +02:00
e.printStackTrace();
}
}
2017-10-09 14:22:27 +02:00
/*//////////////////////////////////////////////////////////////////////////
// Error
//////////////////////////////////////////////////////////////////////////*/
protected void onError(final Throwable e) {
2017-10-09 14:22:27 +02:00
final Activity activity = getActivity();
ErrorActivity.reportError(activity, e,
activity.getClass(),
null,
ErrorInfo.make(UserAction.UI_ERROR,
2017-10-09 14:22:27 +02:00
"none", "", R.string.app_ui_crash));
}
}