Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/fragments/SettingsFragment.java
Show All 36 Lines | |||||
public class SettingsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener { | public class SettingsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener { | ||||
private static final String TAG = SettingsFragment.class.getName(); | private static final String TAG = SettingsFragment.class.getName(); | ||||
private static final String DIALOG_FRAGMENT_TAG = | private static final String DIALOG_FRAGMENT_TAG = | ||||
"androidx.preference.PreferenceFragment.DIALOG"; | "androidx.preference.PreferenceFragment.DIALOG"; | ||||
//private static final | //private static final | ||||
Handler mHandler; | Handler mHandler; | ||||
public final static String PREF_KEY_STARTUP_SCREEN="startup_screen_to_show"; | |||||
private boolean setSummaryStartupPref = false; | |||||
@Override | @Override | ||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | ||||
mHandler = new Handler(); | mHandler = new Handler(); | ||||
return super.onCreateView(inflater, container, savedInstanceState); | return super.onCreateView(inflater, container, savedInstanceState); | ||||
} | } | ||||
@Override | @Override | ||||
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { | public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { | ||||
//getPreferenceManager().setSharedPreferencesName(getString(R.string.mainSharedPreferences)); | //getPreferenceManager().setSharedPreferencesName(getString(R.string.mainSharedPreferences)); | ||||
convertStringPrefToIntIfNeeded(getString(R.string.pref_key_num_recents), getContext()); | convertStringPrefToIntIfNeeded(getString(R.string.pref_key_num_recents), getContext()); | ||||
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); | getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); | ||||
setPreferencesFromResource(R.xml.preferences,rootKey); | setPreferencesFromResource(R.xml.preferences,rootKey); | ||||
/*EditTextPreference editPref = findPreference(getString(R.string.pref_key_num_recents)); | /*EditTextPreference editPref = findPreference(getString(R.string.pref_key_num_recents)); | ||||
editPref.setOnBindEditTextListener(editText -> { | editPref.setOnBindEditTextListener(editText -> { | ||||
editText.setInputType(InputType.TYPE_CLASS_NUMBER); | editText.setInputType(InputType.TYPE_CLASS_NUMBER); | ||||
editText.setSelection(0,editText.getText().length()); | editText.setSelection(0,editText.getText().length()); | ||||
}); | }); | ||||
*/ | */ | ||||
//ListPreference preference = findPreference(R.string.arrival_times) | ListPreference startupScreenPref = findPreference(PREF_KEY_STARTUP_SCREEN); | ||||
if(startupScreenPref !=null){ | |||||
if (startupScreenPref.getValue()==null){ | |||||
startupScreenPref.setSummary(getString(R.string.nav_arrivals_text)); | |||||
setSummaryStartupPref = true; | |||||
} | |||||
} | |||||
//Log.d("BusTO-PrefFrag","startup screen pref is "+startupScreenPref.getValue()); | |||||
Preference dbUpdateNow = findPreference("pref_db_update_now"); | Preference dbUpdateNow = findPreference("pref_db_update_now"); | ||||
if (dbUpdateNow!=null) | if (dbUpdateNow!=null) | ||||
dbUpdateNow.setOnPreferenceClickListener( | dbUpdateNow.setOnPreferenceClickListener( | ||||
new Preference.OnPreferenceClickListener() { | new Preference.OnPreferenceClickListener() { | ||||
@Override | @Override | ||||
public boolean onPreferenceClick(@NonNull Preference preference) { | public boolean onPreferenceClick(@NonNull Preference preference) { | ||||
//trigger update | //trigger update | ||||
Show All 13 Lines | public class SettingsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener { | ||||
} | } | ||||
@Override | @Override | ||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { | public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { | ||||
Preference pref = findPreference(key); | Preference pref = findPreference(key); | ||||
Log.d(TAG,"Preference key "+key+" changed"); | Log.d(TAG,"Preference key "+key+" changed"); | ||||
//sometimes this happens | //sometimes this happens | ||||
if(getContext()==null) return; | if(getContext()==null) return; | ||||
if(key.equals(PREF_KEY_STARTUP_SCREEN) && setSummaryStartupPref && pref !=null){ | |||||
ListPreference listPref = (ListPreference) pref; | |||||
pref.setSummary(listPref.getEntry()); | |||||
} | |||||
/* | /* | ||||
THIS CODE STAYS COMMENTED FOR FUTURE REFERENCES | THIS CODE STAYS COMMENTED FOR FUTURE REFERENCES | ||||
if (key.equals(getString(R.string.pref_key_num_recents))){ | if (key.equals(getString(R.string.pref_key_num_recents))){ | ||||
//check that is it an int | //check that is it an int | ||||
String value = sharedPreferences.getString(key,""); | String value = sharedPreferences.getString(key,""); | ||||
boolean valid = value.length() != 0; | boolean valid = value.length() != 0; | ||||
try{ | try{ | ||||
Show All 18 Lines | public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { | ||||
*/ | */ | ||||
Log.d("BusTO Settings", "changed "+key+"\n "+sharedPreferences.getAll()); | Log.d("BusTO Settings", "changed "+key+"\n "+sharedPreferences.getAll()); | ||||
} | } | ||||
private void convertStringPrefToIntIfNeeded(String preferenceKey, Context con){ | private void convertStringPrefToIntIfNeeded(String preferenceKey, Context con){ | ||||
if (con == null) return; | if (con == null) return; | ||||
SharedPreferences defaultSharedPref = PreferenceManager.getDefaultSharedPreferences(getContext()); | SharedPreferences defaultSharedPref = PreferenceManager.getDefaultSharedPreferences(con); | ||||
try{ | try{ | ||||
Integer val = defaultSharedPref.getInt(preferenceKey, 0); | Integer val = defaultSharedPref.getInt(preferenceKey, 0); | ||||
} catch (NumberFormatException | ClassCastException ex){ | } catch (NumberFormatException | ClassCastException ex){ | ||||
//convert the preference | //convert the preference | ||||
//final String preferenceNumRecents = getString(R.string.pref_key_num_recents); | //final String preferenceNumRecents = getString(R.string.pref_key_num_recents); | ||||
Log.d("Preference - BusTO", "Converting to integer the string preference "+preferenceKey); | Log.d("Preference - BusTO", "Converting to integer the string preference "+preferenceKey); | ||||
String currentValue = defaultSharedPref.getString(preferenceKey, "10"); | String currentValue = defaultSharedPref.getString(preferenceKey, "10"); | ||||
Show All 29 Lines |
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register