Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/fragments/SettingsFragment.java
package it.reyboz.bustorino.fragments; | package it.reyboz.bustorino.fragments; | ||||
import android.content.Context; | import android.content.Context; | ||||
import android.content.SharedPreferences; | import android.content.SharedPreferences; | ||||
import android.os.Bundle; | import android.os.Bundle; | ||||
import android.os.Handler; | |||||
import android.util.Log; | import android.util.Log; | ||||
import androidx.preference.Preference; | import android.view.LayoutInflater; | ||||
import androidx.preference.PreferenceFragmentCompat; | import android.view.View; | ||||
import android.view.ViewGroup; | |||||
import androidx.annotation.NonNull; | |||||
import androidx.annotation.Nullable; | |||||
import androidx.preference.*; | |||||
import it.reyboz.bustorino.R; | import it.reyboz.bustorino.R; | ||||
import java.lang.ref.WeakReference; | |||||
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(); | ||||
SharedPreferences preferences; | private static final String DIALOG_FRAGMENT_TAG = | ||||
"androidx.preference.PreferenceFragment.DIALOG"; | |||||
//private static final | |||||
Handler mHandler; | |||||
@Override | |||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | |||||
mHandler = new Handler(); | |||||
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)); | |||||
convertStringPrefToIntIfNeeded(getString(R.string.pref_key_num_recents), getContext()); | |||||
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); | |||||
setPreferencesFromResource(R.xml.preferences,rootKey); | setPreferencesFromResource(R.xml.preferences,rootKey); | ||||
Context con = getContext(); | /*EditTextPreference editPref = findPreference(getString(R.string.pref_key_num_recents)); | ||||
if (con == null){ | editPref.setOnBindEditTextListener(editText -> { | ||||
Log.w("SETTINGS FRAGMENT", "context is null"); | editText.setInputType(InputType.TYPE_CLASS_NUMBER); | ||||
preferences = null; | editText.setSelection(0,editText.getText().length()); | ||||
} | }); | ||||
else | */ | ||||
preferences = getContext().getSharedPreferences(getString(R.string.mainSharedPreferences), Context.MODE_PRIVATE); | |||||
} | } | ||||
@Override | @Override | ||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { | public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { | ||||
Preference pref = findPreference(key); | Preference pref = findPreference(key); | ||||
//non so a cosa serva tutto questo | Log.d(TAG,"Preference key "+key+" changed"); | ||||
//sometimes this happens | |||||
if(getContext()==null) return; | |||||
/* | |||||
THIS CODE STAYS COMMENTED FOR FUTURE REFERENCES | |||||
if (key.equals(getString(R.string.pref_key_num_recents))){ | |||||
//check that is it an int | |||||
String value = sharedPreferences.getString(key,""); | |||||
boolean valid = value.length() != 0; | |||||
try{ | |||||
Integer intValue = Integer.parseInt(value); | |||||
} catch (NumberFormatException ex){ | |||||
valid = false; | |||||
} | |||||
if (!valid){ | |||||
Toast.makeText(getContext(), R.string.invalid_number, Toast.LENGTH_SHORT).show(); | |||||
if(pref instanceof EditTextPreference){ | |||||
EditTextPreference prefEdit = (EditTextPreference) pref; | |||||
//Intent intent = prefEdit.getIntent(); | |||||
Log.d(TAG, "opening preference, dialog showing "+ | |||||
(getParentFragmentManager().findFragmentByTag(DIALOG_FRAGMENT_TAG)!=null) ); | |||||
//getPreferenceManager().showDialog(pref); | |||||
//onDisplayPreferenceDialog(prefEdit); | |||||
mHandler.postDelayed(new DelayedDisplay(prefEdit), 500); | |||||
} | |||||
} | |||||
} | |||||
*/ | |||||
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){ | |||||
if (con == null) return; | |||||
SharedPreferences defaultSharedPref = PreferenceManager.getDefaultSharedPreferences(getContext()); | |||||
try{ | |||||
Integer val = defaultSharedPref.getInt(preferenceKey, 0); | |||||
} catch (NumberFormatException | ClassCastException ex){ | |||||
//convert the preference | |||||
//final String preferenceNumRecents = getString(R.string.pref_key_num_recents); | |||||
Log.d("Preference - BusTO", "Converting to integer the string preference "+preferenceKey); | |||||
String currentValue = defaultSharedPref.getString(preferenceKey, "10"); | |||||
int newValue; | |||||
try{ | |||||
newValue = Integer.parseInt(currentValue); | |||||
} catch (NumberFormatException e){ | |||||
newValue = 10; | |||||
} | |||||
final SharedPreferences.Editor editor = defaultSharedPref.edit(); | |||||
editor.remove(preferenceKey); | |||||
editor.putInt(preferenceKey, newValue); | |||||
editor.apply(); | |||||
} | |||||
} | |||||
class DelayedDisplay implements Runnable{ | |||||
private final WeakReference<DialogPreference> preferenceWeakReference; | |||||
public DelayedDisplay(DialogPreference preference) { | |||||
this.preferenceWeakReference = new WeakReference<>(preference); | |||||
} | |||||
@Override | |||||
public void run() { | |||||
if(preferenceWeakReference.get()==null) | |||||
return; | |||||
getPreferenceManager().showDialog(preferenceWeakReference.get()); | |||||
} | |||||
} | |||||
} | } |
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