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

354 lines
14 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;
import android.content.DialogInterface;
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;
2018-04-19 01:31:25 +02:00
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;
2019-10-04 14:59:08 +02:00
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.Preference;
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.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.UserAction;
2018-01-28 19:02:34 +01:00
import org.schabi.newpipe.util.FilePickerActivityHelper;
import org.schabi.newpipe.util.ZipHelper;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
2018-04-19 01:31:25 +02:00
import java.io.FileNotFoundException;
2018-01-28 19:02:34 +01:00
import java.io.FileOutputStream;
import java.io.IOException;
2018-04-19 01:31:25 +02:00
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
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;
2018-01-28 19:02:34 +01:00
import java.util.zip.ZipOutputStream;
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 File databasesDir;
private File newpipeDb;
private File newpipeDbJournal;
private File newpipeDbShm;
private File newpipeDbWal;
private File newpipeSettings;
private String thumbnailLoadToggleKey;
2020-04-12 00:26:16 +02:00
private String restrictedModeEnabledKey;
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);
2020-04-12 00:26:16 +02:00
restrictedModeEnabledKey = getString(R.string.restricted_mode_enabled);
initialSelectedLocalization = org.schabi.newpipe.util.Localization
.getPreferredLocalization(requireContext());
initialSelectedContentCountry = org.schabi.newpipe.util.Localization
.getPreferredContentCountry(requireContext());
initialLanguage = PreferenceManager
.getDefaultSharedPreferences(getContext()).getString("app_language_key", "en");
}
@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();
}
2020-04-12 00:26:16 +02:00
if (preference.getKey().equals(restrictedModeEnabledKey)) {
Context context = getContext();
if (context != null) {
2020-04-12 00:26:16 +02:00
DownloaderImpl.getInstance().updateRestrictedModeCookies(context);
} else {
Log.w(TAG, "onPreferenceTreeClick: null context");
}
}
return super.onPreferenceTreeClick(preference);
}
@Override
public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) {
2017-09-28 15:36:15 +02:00
String homeDir = getActivity().getApplicationInfo().dataDir;
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();
addPreferencesFromResource(R.xml.content_settings);
2017-09-26 17:29:38 +02:00
2018-01-28 19:02:34 +01:00
Preference importDataPreference = findPreference(getString(R.string.import_data));
importDataPreference.setOnPreferenceClickListener((Preference p) -> {
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);
startActivityForResult(i, REQUEST_IMPORT_PATH);
2018-01-28 19:02:34 +01:00
return true;
});
Preference exportDataPreference = findPreference(getString(R.string.export_data));
exportDataPreference.setOnPreferenceClickListener((Preference p) -> {
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);
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
.getDefaultSharedPreferences(getContext()).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) {
2018-04-19 01:31:25 +02:00
String path = Utils.getFileForUri(data.getData()).getAbsolutePath();
if (requestCode == REQUEST_EXPORT_PATH) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US);
exportDatabase(path + "/NewPipeData-" + sdf.format(new Date()) + ".zip");
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.override_current_data)
.setPositiveButton(getString(R.string.finish),
2018-04-19 01:31:25 +02:00
(DialogInterface d, int id) -> importDatabase(path))
.setNegativeButton(android.R.string.cancel,
(DialogInterface d, int id) -> d.cancel());
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
2018-01-28 19:02:34 +01:00
ZipOutputStream outZip = new ZipOutputStream(
new BufferedOutputStream(
new FileOutputStream(path)));
ZipHelper.addFileToZip(outZip, newpipeDb.getPath(), "newpipe.db");
saveSharedPreferencesToFile(newpipeSettings);
ZipHelper.addFileToZip(outZip, newpipeSettings.getPath(), "newpipe.settings");
2018-04-19 01:31:25 +02:00
2018-01-28 19:02:34 +01:00
outZip.close();
Toast.makeText(getContext(), R.string.export_complete_toast, Toast.LENGTH_SHORT)
2018-01-28 19:02:34 +01:00
.show();
} catch (Exception e) {
onError(e);
}
}
private void saveSharedPreferencesToFile(final File dst) {
2018-04-19 01:31:25 +02:00
ObjectOutputStream output = null;
try {
output = new ObjectOutputStream(new FileOutputStream(dst));
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getContext());
output.writeObject(pref.getAll());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
2020-02-01 15:35:33 +01:00
} finally {
2018-04-19 01:31:25 +02:00
try {
if (output != null) {
output.flush();
output.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
private void importDatabase(final String filePath) {
// check if file is supported
ZipFile zipFile = null;
try {
zipFile = new ZipFile(filePath);
} catch (IOException ioe) {
Toast.makeText(getContext(), R.string.no_valid_zip_file, Toast.LENGTH_SHORT)
.show();
return;
} finally {
try {
zipFile.close();
} catch (Exception ignored) {
}
}
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")) {
2018-04-19 01:31:25 +02:00
AlertDialog.Builder alert = new AlertDialog.Builder(getContext());
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);
}
} catch (Exception e) {
onError(e);
}
2017-09-26 17:29:38 +02:00
}
private void loadSharedPreferences(final File src) {
2018-04-19 01:31:25 +02:00
ObjectInputStream input = null;
try {
input = new ObjectInputStream(new FileInputStream(src));
SharedPreferences.Editor prefEdit = PreferenceManager
.getDefaultSharedPreferences(getContext()).edit();
2018-04-19 01:31:25 +02:00
prefEdit.clear();
Map<String, ?> entries = (Map<String, ?>) input.readObject();
for (Map.Entry<String, ?> entry : entries.entrySet()) {
Object v = entry.getValue();
String key = entry.getKey();
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();
2018-04-19 01:31:25 +02:00
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
2020-02-01 15:35:33 +01:00
} finally {
2018-04-19 01:31:25 +02:00
try {
if (input != null) {
input.close();
}
} catch (IOException ex) {
ex.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,
ErrorActivity.ErrorInfo.make(UserAction.UI_ERROR,
"none", "", R.string.app_ui_crash));
}
}