1
0
Fork 0
mirror of https://github.com/gsantner/dandelion synced 2024-09-20 23:09:36 +02:00

Migrated SettingsActivity to using PreferenceFragment

This commit is contained in:
vanitasvitae 2016-08-13 19:54:16 +02:00
parent 48a7680930
commit 99349fc13a

View file

@ -27,8 +27,8 @@ import android.preference.EditTextPreference;
import android.preference.ListPreference; import android.preference.ListPreference;
import android.preference.Preference; import android.preference.Preference;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen; import android.preference.PreferenceScreen;
import android.util.Log;
import com.github.dfa.diaspora_android.App; import com.github.dfa.diaspora_android.App;
import com.github.dfa.diaspora_android.R; import com.github.dfa.diaspora_android.R;
@ -36,14 +36,24 @@ import com.github.dfa.diaspora_android.R;
/** /**
* @author vanitas * @author vanitas
*/ */
public class SettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener { public class SettingsActivity extends PreferenceActivity {
private boolean activityRestartRequired;
private SharedPreferences sharedPreferences;
private boolean activityRestartRequired = false;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();
}
public void setActivityRestartRequired(boolean b) {
this.activityRestartRequired = b;
}
public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
private SharedPreferences sharedPreferences;
public void onCreate(Bundle savedInstances) {
super.onCreate(savedInstances);
getPreferenceManager().setSharedPreferencesName("app"); getPreferenceManager().setSharedPreferencesName("app");
addPreferencesFromResource(R.xml.preferences); addPreferencesFromResource(R.xml.preferences);
sharedPreferences = getPreferenceScreen().getSharedPreferences(); sharedPreferences = getPreferenceScreen().getSharedPreferences();
@ -65,10 +75,10 @@ public class SettingsActivity extends PreferenceActivity implements SharedPrefer
@Override @Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
updatePreference(findPreference(key), key); updatePreference(findPreference(key));
} }
private void updatePreference(Preference preference, String key) { private void updatePreference(Preference preference) {
if (preference == null) { if (preference == null) {
return; return;
} }
@ -80,14 +90,13 @@ public class SettingsActivity extends PreferenceActivity implements SharedPrefer
if (preference instanceof ListPreference) { if (preference instanceof ListPreference) {
ListPreference listPref = (ListPreference) preference; ListPreference listPref = (ListPreference) preference;
listPref.setSummary(listPref.getEntry()); listPref.setSummary(listPref.getEntry());
return;
} }
} }
@Override @Override
public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) { public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
Intent intent = new Intent(this, MainActivity.class); Intent intent = new Intent(getActivity(), MainActivity.class);
String podDomain = ((App) getApplication()).getSettings().getPodDomain(); String podDomain = ((App) getActivity().getApplication()).getSettings().getPodDomain();
switch (preference.getTitleRes()) { switch (preference.getTitleRes()) {
case R.string.pref_title__personal_settings: { case R.string.pref_title__personal_settings: {
intent.setAction(MainActivity.ACTION_OPEN_URL); intent.setAction(MainActivity.ACTION_OPEN_URL);
@ -105,17 +114,17 @@ public class SettingsActivity extends PreferenceActivity implements SharedPrefer
break; break;
} }
case R.string.pref_title__change_account: { case R.string.pref_title__change_account: {
new AlertDialog.Builder(SettingsActivity.this) new AlertDialog.Builder(getActivity())
.setTitle(getString(R.string.confirmation)) .setTitle(getString(R.string.confirmation))
.setMessage(getString(R.string.pref_warning__change_account)) .setMessage(getString(R.string.pref_warning__change_account))
.setNegativeButton(android.R.string.no, null) .setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes, .setPositiveButton(android.R.string.yes,
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) { public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(SettingsActivity.this, MainActivity.class); Intent intent = new Intent(getActivity(), MainActivity.class);
intent.setAction(MainActivity.ACTION_CHANGE_ACCOUNT); intent.setAction(MainActivity.ACTION_CHANGE_ACCOUNT);
startActivity(intent); startActivity(intent);
finish(); getActivity().finish();
} }
}) })
.show(); .show();
@ -126,7 +135,7 @@ public class SettingsActivity extends PreferenceActivity implements SharedPrefer
break; break;
} }
case R.string.pref_title__intellihide_toolbars: { case R.string.pref_title__intellihide_toolbars: {
activityRestartRequired = true; ((SettingsActivity) getActivity()).setActivityRestartRequired(true);
return true; return true;
} }
@ -136,20 +145,20 @@ public class SettingsActivity extends PreferenceActivity implements SharedPrefer
} }
} }
if (preference.getKey() != null && preference.getKey().startsWith("pref_key__visibility_nav__")) { if (preference.getKey() != null && preference.getKey().startsWith("pref_key__visibility_nav__")) {
activityRestartRequired = true; ((SettingsActivity) getActivity()).setActivityRestartRequired(true);
return true; return true;
} }
if (intent != null) { if (intent != null) {
startActivity(intent); startActivity(intent);
finish(); getActivity().finish();
return true; return true;
} }
return super.onPreferenceTreeClick(screen, preference); return super.onPreferenceTreeClick(screen, preference);
} }
}
@Override @Override
protected void onStop() { protected void onStop() {
Log.d(App.TAG, "Settings onStop" + activityRestartRequired);
super.onStop(); super.onStop();
if (activityRestartRequired) { if (activityRestartRequired) {
Intent intent = new Intent(this, MainActivity.class); Intent intent = new Intent(this, MainActivity.class);