2017-09-03 06:04:18 +00:00
|
|
|
package org.schabi.newpipe.settings;
|
|
|
|
|
2019-12-01 05:34:34 +00:00
|
|
|
import android.content.ActivityNotFoundException;
|
2018-04-08 20:58:18 +00:00
|
|
|
import android.content.Intent;
|
2017-09-03 06:04:18 +00:00
|
|
|
import android.os.Bundle;
|
2018-04-08 20:58:18 +00:00
|
|
|
import android.provider.Settings;
|
2019-12-01 05:34:34 +00:00
|
|
|
import android.widget.Toast;
|
2020-03-31 17:20:15 +00:00
|
|
|
|
2020-12-15 06:21:26 +00:00
|
|
|
import androidx.core.app.ActivityCompat;
|
2019-10-04 12:59:08 +00:00
|
|
|
import androidx.preference.Preference;
|
2017-09-03 06:04:18 +00:00
|
|
|
|
|
|
|
import org.schabi.newpipe.R;
|
|
|
|
import org.schabi.newpipe.util.Constants;
|
2021-03-26 14:12:20 +00:00
|
|
|
import org.schabi.newpipe.util.ThemeHelper;
|
2017-09-03 06:04:18 +00:00
|
|
|
|
|
|
|
public class AppearanceSettingsFragment extends BasePreferenceFragment {
|
|
|
|
|
|
|
|
@Override
|
2021-03-22 20:38:36 +00:00
|
|
|
public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) {
|
2021-12-24 20:35:15 +00:00
|
|
|
addPreferencesFromResourceRegistry();
|
2020-10-11 11:16:22 +00:00
|
|
|
|
2020-08-16 08:24:58 +00:00
|
|
|
final String themeKey = getString(R.string.theme_key);
|
2020-10-11 11:16:22 +00:00
|
|
|
// the key of the active theme when settings were opened (or recreated after theme change)
|
|
|
|
final String startThemeKey = defaultPreferences
|
2020-03-31 17:20:15 +00:00
|
|
|
.getString(themeKey, getString(R.string.default_theme_value));
|
2020-10-11 11:16:22 +00:00
|
|
|
final String autoDeviceThemeKey = getString(R.string.auto_device_theme_key);
|
|
|
|
findPreference(themeKey).setOnPreferenceChangeListener((preference, newValue) -> {
|
|
|
|
if (newValue.toString().equals(autoDeviceThemeKey)) {
|
|
|
|
Toast.makeText(getContext(), getString(R.string.select_night_theme_toast),
|
|
|
|
Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
|
|
|
|
applyThemeChange(startThemeKey, themeKey, newValue);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
final String nightThemeKey = getString(R.string.night_theme_key);
|
|
|
|
if (startThemeKey.equals(autoDeviceThemeKey)) {
|
|
|
|
final String startNightThemeKey = defaultPreferences
|
|
|
|
.getString(nightThemeKey, getString(R.string.default_night_theme_value));
|
2018-04-08 20:58:18 +00:00
|
|
|
|
2020-10-11 11:16:22 +00:00
|
|
|
findPreference(nightThemeKey).setOnPreferenceChangeListener((preference, newValue) -> {
|
|
|
|
applyThemeChange(startNightThemeKey, nightThemeKey, newValue);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
} else {
|
2022-12-01 12:01:58 +00:00
|
|
|
// disable the night theme selection
|
|
|
|
final Preference preference = findPreference(nightThemeKey);
|
|
|
|
if (preference != null) {
|
|
|
|
preference.setEnabled(false);
|
|
|
|
preference.setSummary(getString(R.string.night_theme_available));
|
|
|
|
}
|
2020-10-11 11:16:22 +00:00
|
|
|
}
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
|
|
|
|
2018-04-08 20:58:18 +00:00
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
public boolean onPreferenceTreeClick(final Preference preference) {
|
2022-03-19 21:12:58 +00:00
|
|
|
if (getString(R.string.caption_settings_key).equals(preference.getKey())) {
|
2019-12-01 05:34:34 +00:00
|
|
|
try {
|
|
|
|
startActivity(new Intent(Settings.ACTION_CAPTIONING_SETTINGS));
|
2020-08-16 08:24:58 +00:00
|
|
|
} catch (final ActivityNotFoundException e) {
|
2019-12-01 05:34:34 +00:00
|
|
|
Toast.makeText(getActivity(), R.string.general_error, Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
2018-04-08 20:58:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return super.onPreferenceTreeClick(preference);
|
|
|
|
}
|
2020-10-11 11:16:22 +00:00
|
|
|
|
|
|
|
private void applyThemeChange(final String beginningThemeKey,
|
|
|
|
final String themeKey,
|
|
|
|
final Object newValue) {
|
|
|
|
defaultPreferences.edit().putBoolean(Constants.KEY_THEME_CHANGE, true).apply();
|
|
|
|
defaultPreferences.edit().putString(themeKey, newValue.toString()).apply();
|
|
|
|
|
2022-07-06 21:46:20 +00:00
|
|
|
ThemeHelper.setDayNightMode(requireContext(), newValue.toString());
|
2021-03-26 14:12:20 +00:00
|
|
|
|
2020-10-11 11:16:22 +00:00
|
|
|
if (!newValue.equals(beginningThemeKey) && getActivity() != null) {
|
|
|
|
// if it's not the current theme
|
|
|
|
ActivityCompat.recreate(getActivity());
|
|
|
|
}
|
|
|
|
}
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|