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;
|
|
|
|
import android.os.Build;
|
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
|
|
|
|
2019-10-04 12:59:08 +00:00
|
|
|
import androidx.annotation.Nullable;
|
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;
|
|
|
|
|
|
|
|
public class AppearanceSettingsFragment extends BasePreferenceFragment {
|
2020-03-31 17:20:15 +00:00
|
|
|
private static final boolean CAPTIONING_SETTINGS_ACCESSIBLE =
|
2018-04-08 20:58:18 +00:00
|
|
|
Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
|
|
|
|
|
|
|
|
private String captionSettingsKey;
|
2017-09-03 06:04:18 +00:00
|
|
|
|
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
public void onCreate(@Nullable final Bundle savedInstanceState) {
|
2017-09-03 06:04:18 +00:00
|
|
|
super.onCreate(savedInstanceState);
|
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 {
|
|
|
|
removePreference(nightThemeKey);
|
|
|
|
}
|
2020-07-03 16:44:34 +00:00
|
|
|
|
2018-04-08 20:58:18 +00:00
|
|
|
captionSettingsKey = getString(R.string.caption_settings_key);
|
2020-03-31 17:20:15 +00:00
|
|
|
if (!CAPTIONING_SETTINGS_ACCESSIBLE) {
|
2020-10-11 11:16:22 +00:00
|
|
|
removePreference(captionSettingsKey);
|
2018-04-08 20:58:18 +00:00
|
|
|
}
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) {
|
2017-09-03 06:04:18 +00:00
|
|
|
addPreferencesFromResource(R.xml.appearance_settings);
|
|
|
|
}
|
|
|
|
|
2018-04-08 20:58:18 +00:00
|
|
|
@Override
|
2020-03-31 17:20:15 +00:00
|
|
|
public boolean onPreferenceTreeClick(final Preference preference) {
|
2018-04-08 20:58:18 +00:00
|
|
|
if (preference.getKey().equals(captionSettingsKey) && CAPTIONING_SETTINGS_ACCESSIBLE) {
|
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 removePreference(final String preferenceKey) {
|
|
|
|
final Preference preference = findPreference(preferenceKey);
|
|
|
|
if (preference != null) {
|
|
|
|
getPreferenceScreen().removePreference(preference);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
if (!newValue.equals(beginningThemeKey) && getActivity() != null) {
|
|
|
|
// if it's not the current theme
|
|
|
|
ActivityCompat.recreate(getActivity());
|
|
|
|
}
|
|
|
|
}
|
2017-09-03 06:04:18 +00:00
|
|
|
}
|