diff options
author | Scott Jackson <daneren2005@gmail.com> | 2015-11-27 15:29:01 -0800 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2015-11-27 15:29:01 -0800 |
commit | 5071050e612f940d6b97bcfaa218f361f69ccfa4 (patch) | |
tree | 606a5ef16d4b2eccb3ea3a62d7a208e0968406aa /app/src | |
parent | 2eff8611ac2acfe13eb46d9757d169e1203dcfe8 (diff) | |
download | dsub-5071050e612f940d6b97bcfaa218f361f69ccfa4.tar.gz dsub-5071050e612f940d6b97bcfaa218f361f69ccfa4.tar.bz2 dsub-5071050e612f940d6b97bcfaa218f361f69ccfa4.zip |
#580, #596 Switch fragments for server settings as well
Diffstat (limited to 'app/src')
5 files changed, 137 insertions, 65 deletions
diff --git a/app/src/main/java/github/daneren2005/dsub/activity/SubsonicActivity.java b/app/src/main/java/github/daneren2005/dsub/activity/SubsonicActivity.java index ba7a32b8..edcb7486 100644 --- a/app/src/main/java/github/daneren2005/dsub/activity/SubsonicActivity.java +++ b/app/src/main/java/github/daneren2005/dsub/activity/SubsonicActivity.java @@ -681,7 +681,7 @@ public class SubsonicActivity extends AppCompatActivity implements OnItemSelecte } } - protected SubsonicFragment getCurrentFragment() { + public SubsonicFragment getCurrentFragment() { return this.currentFragment; } diff --git a/app/src/main/java/github/daneren2005/dsub/activity/SubsonicFragmentActivity.java b/app/src/main/java/github/daneren2005/dsub/activity/SubsonicFragmentActivity.java index e63692f6..87a67e80 100644 --- a/app/src/main/java/github/daneren2005/dsub/activity/SubsonicFragmentActivity.java +++ b/app/src/main/java/github/daneren2005/dsub/activity/SubsonicFragmentActivity.java @@ -458,7 +458,7 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo } @Override - protected SubsonicFragment getCurrentFragment() { + public SubsonicFragment getCurrentFragment() { if(slideUpPanel.getPanelState() == SlidingUpPanelLayout.PanelState.EXPANDED) { if(secondaryFragment == null) { return nowPlayingFragment; diff --git a/app/src/main/java/github/daneren2005/dsub/fragments/PreferenceCompatFragment.java b/app/src/main/java/github/daneren2005/dsub/fragments/PreferenceCompatFragment.java index 201a1fdb..d3b5ea3f 100644 --- a/app/src/main/java/github/daneren2005/dsub/fragments/PreferenceCompatFragment.java +++ b/app/src/main/java/github/daneren2005/dsub/fragments/PreferenceCompatFragment.java @@ -248,7 +248,7 @@ public abstract class PreferenceCompatFragment extends SubsonicFragment { } } - private void setPreferenceScreen(PreferenceScreen preferenceScreen) { + protected void setPreferenceScreen(PreferenceScreen preferenceScreen) { try { Method m = PreferenceManager.class.getDeclaredMethod("setPreferences", PreferenceScreen.class); m.setAccessible(true); diff --git a/app/src/main/java/github/daneren2005/dsub/fragments/SettingsFragment.java b/app/src/main/java/github/daneren2005/dsub/fragments/SettingsFragment.java index 0030ea4a..e288b7cb 100644 --- a/app/src/main/java/github/daneren2005/dsub/fragments/SettingsFragment.java +++ b/app/src/main/java/github/daneren2005/dsub/fragments/SettingsFragment.java @@ -60,6 +60,7 @@ import github.daneren2005.dsub.view.ErrorDialog; public class SettingsFragment extends PreferenceCompatFragment implements SharedPreferences.OnSharedPreferenceChangeListener { private final static String TAG = SettingsFragment.class.getSimpleName(); + private final Map<String, ServerSettings> serverSettings = new LinkedHashMap<String, ServerSettings>(); private boolean testingConnection; private ListPreference theme; @@ -99,6 +100,15 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared @Override public void onCreate(Bundle bundle) { super.onCreate(bundle); + + int instance = this.getArguments().getInt(Constants.PREFERENCES_KEY_SERVER_INSTANCE, -1); + if (instance != -1) { + PreferenceScreen preferenceScreen = expandServer(instance); + setPreferenceScreen(preferenceScreen); + + serverSettings.put(Integer.toString(instance), new ServerSettings(instance)); + onInitPreferences(preferenceScreen); + } } @Override @@ -286,16 +296,20 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared @Override public boolean onPreferenceClick(Preference preference) { serverCount++; - String instance = String.valueOf(serverCount); + int instance = serverCount; serversCategory.addPreference(addServer(serverCount)); SharedPreferences.Editor editor = settings.edit(); editor.putInt(Constants.PREFERENCES_KEY_SERVER_COUNT, serverCount); // Reset set folder ID editor.putString(Constants.PREFERENCES_KEY_MUSIC_FOLDER_ID + instance, null); + editor.putString(Constants.PREFERENCES_KEY_SERVER_URL + instance, "http://yourhost"); + editor.putString(Constants.PREFERENCES_KEY_SERVER_NAME + instance, getResources().getString(R.string.settings_server_unused)); editor.commit(); - serverSettings.put(instance, new ServerSettings(instance)); + ServerSettings ss = new ServerSettings(instance); + serverSettings.put(String.valueOf(instance), ss); + ss.update(); return true; } @@ -303,9 +317,8 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared serversCategory.setOrderingAsAdded(false); for (int i = 1; i <= serverCount; i++) { - String instance = String.valueOf(i); serversCategory.addPreference(addServer(i)); - serverSettings.put(instance, new ServerSettings(instance)); + serverSettings.put(String.valueOf(i), new ServerSettings(i)); } } @@ -402,15 +415,39 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared } } - if(serversCategory != null) { - for (ServerSettings ss : serverSettings.values()) { - ss.update(); + for (ServerSettings ss : serverSettings.values()) { + if(!ss.update()) { + if(serversCategory != null) { + serversCategory.removePreference(ss.getScreen()); + } } } } private PreferenceScreen addServer(final int instance) { final PreferenceScreen screen = this.getPreferenceManager().createPreferenceScreen(context); + screen.setKey(Constants.PREFERENCES_KEY_SERVER_KEY + instance); + screen.setOrder(instance); + + screen.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + SettingsFragment newFragment = new SettingsFragment(); + + Bundle args = new Bundle(); + args.putInt(Constants.PREFERENCES_KEY_SERVER_INSTANCE, instance); + newFragment.setArguments(args); + + replaceFragment(newFragment); + return false; + } + }); + + return screen; + } + + private PreferenceScreen expandServer(final int instance) { + final PreferenceScreen screen = this.getPreferenceManager().createPreferenceScreen(context); screen.setTitle(R.string.settings_server_unused); screen.setKey(Constants.PREFERENCES_KEY_SERVER_KEY + instance); @@ -533,8 +570,13 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared editor.putInt(Constants.PREFERENCES_KEY_SERVER_COUNT, serverCount); editor.commit(); - serversCategory.removePreference(screen); - screen.getDialog().dismiss(); + removeCurrent(); + + SubsonicFragment parentFragment = context.getCurrentFragment(); + if(parentFragment instanceof SettingsFragment) { + SettingsFragment serverSelectionFragment = (SettingsFragment) parentFragment; + serverSelectionFragment.update(); + } } }); @@ -566,8 +608,6 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared screen.addPreference(serverOpenBrowser); screen.addPreference(serverRemoveServerPreference); - screen.setOrder(instance); - return screen; } @@ -691,6 +731,7 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared } private class ServerSettings { + private int instance; private EditTextPreference serverName; private EditTextPreference serverUrl; private EditTextPreference serverLocalNetworkSSID; @@ -698,73 +739,101 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared private EditTextPreference username; private PreferenceScreen screen; - private ServerSettings(String instance) { - screen = (PreferenceScreen) SettingsFragment.this.findPreference("server" + instance); + private ServerSettings(int instance) { + this.instance = instance; + screen = (PreferenceScreen) SettingsFragment.this.findPreference(Constants.PREFERENCES_KEY_SERVER_KEY + instance); serverName = (EditTextPreference) SettingsFragment.this.findPreference(Constants.PREFERENCES_KEY_SERVER_NAME + instance); serverUrl = (EditTextPreference) SettingsFragment.this.findPreference(Constants.PREFERENCES_KEY_SERVER_URL + instance); serverLocalNetworkSSID = (EditTextPreference) SettingsFragment.this.findPreference(Constants.PREFERENCES_KEY_SERVER_LOCAL_NETWORK_SSID + instance); serverInternalUrl = (EditTextPreference) SettingsFragment.this.findPreference(Constants.PREFERENCES_KEY_SERVER_INTERNAL_URL + instance); username = (EditTextPreference) SettingsFragment.this.findPreference(Constants.PREFERENCES_KEY_USERNAME + instance); - serverUrl.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object value) { - try { - String url = (String) value; - new URL(url); - if (url.contains(" ") || url.contains("@") || url.contains("_")) { - throw new Exception(); + if(serverName != null) { + serverUrl.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object value) { + try { + String url = (String) value; + new URL(url); + if (url.contains(" ") || url.contains("@") || url.contains("_")) { + throw new Exception(); + } + } catch (Exception x) { + new ErrorDialog(context, R.string.settings_invalid_url, false); + return false; } - } catch (Exception x) { - new ErrorDialog(context, R.string.settings_invalid_url, false); - return false; + return true; } - return true; - } - }); - serverInternalUrl.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object value) { - try { - String url = (String) value; - // Allow blank internal IP address - if("".equals(url) || url == null) { - return true; + }); + serverInternalUrl.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object value) { + try { + String url = (String) value; + // Allow blank internal IP address + if ("".equals(url) || url == null) { + return true; + } + + new URL(url); + if (url.contains(" ") || url.contains("@") || url.contains("_")) { + throw new Exception(); + } + } catch (Exception x) { + new ErrorDialog(context, R.string.settings_invalid_url, false); + return false; } + return true; + } + }); - new URL(url); - if (url.contains(" ") || url.contains("@") || url.contains("_")) { - throw new Exception(); + username.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object value) { + String username = (String) value; + if (username == null || !username.equals(username.trim())) { + new ErrorDialog(context, R.string.settings_invalid_username, false); + return false; } - } catch (Exception x) { - new ErrorDialog(context, R.string.settings_invalid_url, false); - return false; + return true; } - return true; + }); + } + } + + public PreferenceScreen getScreen() { + return screen; + } + + public boolean update() { + SharedPreferences prefs = Util.getPreferences(context); + + if(prefs.contains(Constants.PREFERENCES_KEY_SERVER_NAME + instance)) { + if (serverName != null) { + serverName.setSummary(serverName.getText()); + serverUrl.setSummary(serverUrl.getText()); + serverLocalNetworkSSID.setSummary(serverLocalNetworkSSID.getText()); + serverInternalUrl.setSummary(serverInternalUrl.getText()); + username.setSummary(username.getText()); } - }); - username.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object value) { - String username = (String) value; - if (username == null || !username.equals(username.trim())) { - new ErrorDialog(context, R.string.settings_invalid_username, false); - return false; - } - return true; + + String title = prefs.getString(Constants.PREFERENCES_KEY_SERVER_NAME + instance, null); + String summary = prefs.getString(Constants.PREFERENCES_KEY_SERVER_URL + instance, null); + + if (title != null) { + screen.setTitle(title); + } else { + screen.setTitle(R.string.settings_server_unused); + } + if (summary != null) { + screen.setSummary(summary); } - }); - } - public void update() { - serverName.setSummary(serverName.getText()); - serverUrl.setSummary(serverUrl.getText()); - serverLocalNetworkSSID.setSummary(serverLocalNetworkSSID.getText()); - serverInternalUrl.setSummary(serverInternalUrl.getText()); - username.setSummary(username.getText()); - screen.setSummary(serverUrl.getText()); - screen.setTitle(serverName.getText()); + return true; + } else { + return false; + } } } } diff --git a/app/src/main/java/github/daneren2005/dsub/fragments/SubsonicFragment.java b/app/src/main/java/github/daneren2005/dsub/fragments/SubsonicFragment.java index 33ce0b20..6d25bc0b 100644 --- a/app/src/main/java/github/daneren2005/dsub/fragments/SubsonicFragment.java +++ b/app/src/main/java/github/daneren2005/dsub/fragments/SubsonicFragment.java @@ -456,6 +456,9 @@ public class SubsonicFragment extends Fragment implements SwipeRefreshLayout.OnR public void replaceExistingFragment(SubsonicFragment fragment) { context.replaceExistingFragment(fragment, fragment.getSupportTag()); } + public void removeCurrent() { + context.removeCurrent(); + } public int getRootId() { return rootView.getId(); |