1
0
Fork 0
mirror of https://github.com/gsantner/dandelion synced 2024-11-25 22:02:08 +01: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,122 +36,131 @@ 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);
getPreferenceManager().setSharedPreferencesName("app"); getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();
addPreferencesFromResource(R.xml.preferences);
sharedPreferences = getPreferenceScreen().getSharedPreferences();
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
setPreferenceSummaries();
sharedPreferences.edit().putBoolean(getString(R.string.pref_key__proxy_was_enabled),
sharedPreferences.getBoolean(getString(R.string.pref_key__proxy_enabled), false)).apply();
} }
private void setPreferenceSummaries() { public void setActivityRestartRequired(boolean b) {
String[] editTextKeys = new String[]{ this.activityRestartRequired = b;
getString(R.string.pref_key__proxy_host), getString(R.string.pref_key__proxy_port)
};
for (String key : editTextKeys) {
EditTextPreference p = (EditTextPreference) findPreference(key);
p.setSummary(p.getText());
}
} }
@Override public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { private SharedPreferences sharedPreferences;
updatePreference(findPreference(key), key);
}
private void updatePreference(Preference preference, String key) { public void onCreate(Bundle savedInstances) {
if (preference == null) { super.onCreate(savedInstances);
return; getPreferenceManager().setSharedPreferencesName("app");
addPreferencesFromResource(R.xml.preferences);
sharedPreferences = getPreferenceScreen().getSharedPreferences();
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
setPreferenceSummaries();
sharedPreferences.edit().putBoolean(getString(R.string.pref_key__proxy_was_enabled),
sharedPreferences.getBoolean(getString(R.string.pref_key__proxy_enabled), false)).apply();
} }
if (preference instanceof EditTextPreference) {
EditTextPreference textPref = (EditTextPreference) preference;
textPref.setSummary(textPref.getText());
return;
}
if (preference instanceof ListPreference) {
ListPreference listPref = (ListPreference) preference;
listPref.setSummary(listPref.getEntry());
return;
}
}
@Override private void setPreferenceSummaries() {
public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) { String[] editTextKeys = new String[]{
Intent intent = new Intent(this, MainActivity.class); getString(R.string.pref_key__proxy_host), getString(R.string.pref_key__proxy_port)
String podDomain = ((App) getApplication()).getSettings().getPodDomain(); };
switch (preference.getTitleRes()) { for (String key : editTextKeys) {
case R.string.pref_title__personal_settings: { EditTextPreference p = (EditTextPreference) findPreference(key);
intent.setAction(MainActivity.ACTION_OPEN_URL); p.setSummary(p.getText());
intent.putExtra(MainActivity.URL_MESSAGE, "https://" + podDomain + "/user/edit");
break;
} }
case R.string.pref_title__manage_tags: { }
intent.setAction(MainActivity.ACTION_OPEN_URL);
intent.putExtra(MainActivity.URL_MESSAGE, "https://" + podDomain + "/tag_followings/manage"); @Override
break; public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
updatePreference(findPreference(key));
}
private void updatePreference(Preference preference) {
if (preference == null) {
return;
} }
case R.string.pref_title__manage_contacts: { if (preference instanceof EditTextPreference) {
intent.setAction(MainActivity.ACTION_OPEN_URL); EditTextPreference textPref = (EditTextPreference) preference;
intent.putExtra(MainActivity.URL_MESSAGE, "https://" + podDomain + "/contacts"); textPref.setSummary(textPref.getText());
break; return;
} }
case R.string.pref_title__change_account: { if (preference instanceof ListPreference) {
new AlertDialog.Builder(SettingsActivity.this) ListPreference listPref = (ListPreference) preference;
.setTitle(getString(R.string.confirmation)) listPref.setSummary(listPref.getEntry());
.setMessage(getString(R.string.pref_warning__change_account)) }
.setNegativeButton(android.R.string.no, null) }
.setPositiveButton(android.R.string.yes,
new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int id) { public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
Intent intent = new Intent(SettingsActivity.this, MainActivity.class); Intent intent = new Intent(getActivity(), MainActivity.class);
intent.setAction(MainActivity.ACTION_CHANGE_ACCOUNT); String podDomain = ((App) getActivity().getApplication()).getSettings().getPodDomain();
startActivity(intent); switch (preference.getTitleRes()) {
finish(); case R.string.pref_title__personal_settings: {
} intent.setAction(MainActivity.ACTION_OPEN_URL);
}) intent.putExtra(MainActivity.URL_MESSAGE, "https://" + podDomain + "/user/edit");
.show(); break;
}
case R.string.pref_title__manage_tags: {
intent.setAction(MainActivity.ACTION_OPEN_URL);
intent.putExtra(MainActivity.URL_MESSAGE, "https://" + podDomain + "/tag_followings/manage");
break;
}
case R.string.pref_title__manage_contacts: {
intent.setAction(MainActivity.ACTION_OPEN_URL);
intent.putExtra(MainActivity.URL_MESSAGE, "https://" + podDomain + "/contacts");
break;
}
case R.string.pref_title__change_account: {
new AlertDialog.Builder(getActivity())
.setTitle(getString(R.string.confirmation))
.setMessage(getString(R.string.pref_warning__change_account))
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(getActivity(), MainActivity.class);
intent.setAction(MainActivity.ACTION_CHANGE_ACCOUNT);
startActivity(intent);
getActivity().finish();
}
})
.show();
return true;
}
case R.string.pref_title__clear_cache: {
intent.setAction(MainActivity.ACTION_CLEAR_CACHE);
break;
}
case R.string.pref_title__intellihide_toolbars: {
((SettingsActivity) getActivity()).setActivityRestartRequired(true);
return true;
}
default: {
intent = null;
break;
}
}
if (preference.getKey() != null && preference.getKey().startsWith("pref_key__visibility_nav__")) {
((SettingsActivity) getActivity()).setActivityRestartRequired(true);
return true; return true;
} }
case R.string.pref_title__clear_cache: { if (intent != null) {
intent.setAction(MainActivity.ACTION_CLEAR_CACHE); startActivity(intent);
break; getActivity().finish();
}
case R.string.pref_title__intellihide_toolbars: {
activityRestartRequired = true;
return true; return true;
} }
return super.onPreferenceTreeClick(screen, preference);
default: {
intent = null;
break;
}
} }
if(preference.getKey() != null && preference.getKey().startsWith("pref_key__visibility_nav__")) {
activityRestartRequired = true;
return true;
}
if (intent != null) {
startActivity(intent);
finish();
return true;
}
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);
intent.setAction(MainActivity.ACTION_RELOAD_ACTIVITY); intent.setAction(MainActivity.ACTION_RELOAD_ACTIVITY);
startActivity(intent); startActivity(intent);