diff options
author | Scott Jackson <daneren2005@gmail.com> | 2015-11-27 11:53:21 -0800 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2015-11-27 11:53:21 -0800 |
commit | 32e04ec3166362b3232d72232a66f2cc1f0ab0a3 (patch) | |
tree | be5ff34f945e84aae4d5546fbf678f6879508457 /app/src/main/java | |
parent | e09c70e5d112bd6b7142892b25697212a745e963 (diff) | |
download | dsub-32e04ec3166362b3232d72232a66f2cc1f0ab0a3.tar.gz dsub-32e04ec3166362b3232d72232a66f2cc1f0ab0a3.tar.bz2 dsub-32e04ec3166362b3232d72232a66f2cc1f0ab0a3.zip |
#580, #596 Make setting open up a normal new fragment
Diffstat (limited to 'app/src/main/java')
3 files changed, 206 insertions, 138 deletions
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 632b4c55..e63692f6 100644 --- a/app/src/main/java/github/daneren2005/dsub/activity/SubsonicFragmentActivity.java +++ b/app/src/main/java/github/daneren2005/dsub/activity/SubsonicFragmentActivity.java @@ -611,7 +611,12 @@ public class SubsonicFragmentActivity extends SubsonicActivity implements Downlo sessionInitialized = true; } private void loadSettings() { - PreferenceManager.setDefaultValues(this, R.xml.settings, false); + PreferenceManager.setDefaultValues(this, R.xml.settings_appearance, false); + PreferenceManager.setDefaultValues(this, R.xml.settings_cache, false); + PreferenceManager.setDefaultValues(this, R.xml.settings_drawer, false); + PreferenceManager.setDefaultValues(this, R.xml.settings_sync, false); + PreferenceManager.setDefaultValues(this, R.xml.settings_playback, false); + SharedPreferences prefs = Util.getPreferences(this); if (!prefs.contains(Constants.PREFERENCES_KEY_CACHE_LOCATION) || prefs.getString(Constants.PREFERENCES_KEY_CACHE_LOCATION, null) == null) { resetCacheLocation(prefs); 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 9f413b3b..201a1fdb 100644 --- a/app/src/main/java/github/daneren2005/dsub/fragments/PreferenceCompatFragment.java +++ b/app/src/main/java/github/daneren2005/dsub/fragments/PreferenceCompatFragment.java @@ -29,6 +29,7 @@ import android.preference.Preference; import android.preference.PreferenceFragment; import android.preference.PreferenceManager; import android.preference.PreferenceScreen; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -40,7 +41,8 @@ import java.lang.reflect.Method; import github.daneren2005.dsub.R; import github.daneren2005.dsub.util.Constants; -public class PreferenceCompatFragment extends SubsonicFragment { +public abstract class PreferenceCompatFragment extends SubsonicFragment { + private static final String TAG = PreferenceCompatFragment.class.getSimpleName(); private static final int FIRST_REQUEST_CODE = 100; private static final int MSG_BIND_PREFERENCES = 1; private static final String PREFERENCES_TAG = "android:preferences"; @@ -114,10 +116,25 @@ public class PreferenceCompatFragment extends SubsonicFragment { setPreferenceScreen(screen); } - public void addPreferencesFromResource(int resId) { + public PreferenceScreen addPreferencesFromResource(int resId) { requirePreferenceManager(); PreferenceScreen screen = inflateFromResource(getActivity(), resId, getPreferenceScreen()); setPreferenceScreen(screen); + + for(int i = 0; i < screen.getPreferenceCount(); i++) { + Preference preference = screen.getPreference(i); + if(preference instanceof PreferenceScreen && preference.getKey() != null) { + preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + onStartNewFragment(preference.getKey()); + return false; + } + }); + } + } + + return screen; } public Preference findPreference(CharSequence key) { @@ -139,7 +156,7 @@ public class PreferenceCompatFragment extends SubsonicFragment { @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); - getListView().setScrollBarStyle(0); + getListView().setScrollBarStyle(View.SCROLLBAR_POSITION_DEFAULT); if (mHavePrefs) { bindPreferences(); } @@ -168,7 +185,8 @@ public class PreferenceCompatFragment extends SubsonicFragment { int res = this.getArguments().getInt(Constants.INTENT_EXTRA_FRAGMENT_TYPE, 0); if(res != 0) { - addPreferencesFromResource(res); + PreferenceScreen preferenceScreen = addPreferencesFromResource(res); + onInitPreferences(preferenceScreen); } } @@ -310,4 +328,7 @@ public class PreferenceCompatFragment extends SubsonicFragment { } return preferenceScreen; } + + protected abstract void onInitPreferences(PreferenceScreen preferenceScreen); + protected abstract void onStartNewFragment(String name); } 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 925d69d7..0030ea4a 100644 --- a/app/src/main/java/github/daneren2005/dsub/fragments/SettingsFragment.java +++ b/app/src/main/java/github/daneren2005/dsub/fragments/SettingsFragment.java @@ -102,16 +102,6 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared } @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) { - View root = super.onCreateView(inflater, container, bundle); - - this.setTitle(getResources().getString(R.string.settings_title)); - initSettings(); - - return root; - } - - @Override public void onDestroy() { super.onDestroy(); @@ -120,6 +110,33 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared } @Override + protected void onStartNewFragment(String name) { + SettingsFragment newFragment = new SettingsFragment(); + Bundle args = new Bundle(); + + int xml = 0; + if("appearance".equals(name)) { + xml = R.xml.settings_appearance; + } else if("drawer".equals(name)) { + xml = R.xml.settings_drawer; + } else if("cache".equals(name)) { + xml = R.xml.settings_cache; + } else if("sync".equals(name)) { + xml = R.xml.settings_sync; + } else if("playback".equals(name)) { + xml = R.xml.settings_playback; + } else if("servers".equals(name)) { + xml = R.xml.settings_servers; + } + + if(xml != 0) { + args.putInt(Constants.INTENT_EXTRA_FRAGMENT_TYPE, xml); + newFragment.setArguments(args); + replaceFragment(newFragment); + } + } + + @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { // Random error I have no idea how to reproduce if(sharedPreferences == null) { @@ -162,9 +179,12 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared scheduleBackup(); } - private void initSettings() { + @Override + protected void onInitPreferences(PreferenceScreen preferenceScreen) { + this.setTitle(preferenceScreen.getTitle()); + internalSSID = Util.getSSID(context); - if(internalSSID == null) { + if (internalSSID == null) { internalSSID = ""; } internalSSIDDisplay = context.getResources().getString(R.string.settings_server_local_network_ssid_hint, internalSSID); @@ -200,87 +220,93 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared settings = Util.getPreferences(context); serverCount = settings.getInt(Constants.PREFERENCES_KEY_SERVER_COUNT, 1); - this.findPreference("clearCache").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference preference) { - Util.confirmDialog(context, R.string.common_delete, R.string.common_confirm_message_cache, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - new LoadingTask<Void>(context, false) { - @Override - protected Void doInBackground() throws Throwable { - FileUtil.deleteMusicDirectory(context); - FileUtil.deleteSerializedCache(context); - FileUtil.deleteArtworkCache(context); - FileUtil.deleteAvatarCache(context); - return null; - } - - @Override - protected void done(Void result) { - Util.toast(context, R.string.settings_cache_clear_complete); - } - - @Override - protected void error(Throwable error) { - Util.toast(context, getErrorMessage(error), false); - } - }.execute(); - } - }); - return false; - } - }); + if(cacheSize != null) { + this.findPreference("clearCache").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + Util.confirmDialog(context, R.string.common_delete, R.string.common_confirm_message_cache, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + new LoadingTask<Void>(context, false) { + @Override + protected Void doInBackground() throws Throwable { + FileUtil.deleteMusicDirectory(context); + FileUtil.deleteSerializedCache(context); + FileUtil.deleteArtworkCache(context); + FileUtil.deleteAvatarCache(context); + return null; + } + + @Override + protected void done(Void result) { + Util.toast(context, R.string.settings_cache_clear_complete); + } + + @Override + protected void error(Throwable error) { + Util.toast(context, getErrorMessage(error), false); + } + }.execute(); + } + }); + return false; + } + }); + } - addServerPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference preference) { - serverCount++; - String instance = String.valueOf(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.commit(); + if(syncEnabled != null) { + this.findPreference(Constants.PREFERENCES_KEY_SYNC_ENABLED).setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + Boolean syncEnabled = (Boolean) newValue; - serverSettings.put(instance, new ServerSettings(instance)); + Account account = new Account(Constants.SYNC_ACCOUNT_NAME, Constants.SYNC_ACCOUNT_TYPE); + ContentResolver.setSyncAutomatically(account, Constants.SYNC_ACCOUNT_PLAYLIST_AUTHORITY, syncEnabled); + ContentResolver.setSyncAutomatically(account, Constants.SYNC_ACCOUNT_PODCAST_AUTHORITY, syncEnabled); - return true; - } - }); + return true; + } + }); + syncInterval.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + Integer syncInterval = Integer.parseInt(((String) newValue)); - this.findPreference(Constants.PREFERENCES_KEY_SYNC_ENABLED).setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - Boolean syncEnabled = (Boolean) newValue; + Account account = new Account(Constants.SYNC_ACCOUNT_NAME, Constants.SYNC_ACCOUNT_TYPE); + ContentResolver.addPeriodicSync(account, Constants.SYNC_ACCOUNT_PLAYLIST_AUTHORITY, new Bundle(), 60L * syncInterval); + ContentResolver.addPeriodicSync(account, Constants.SYNC_ACCOUNT_PODCAST_AUTHORITY, new Bundle(), 60L * syncInterval); - Account account = new Account(Constants.SYNC_ACCOUNT_NAME, Constants.SYNC_ACCOUNT_TYPE); - ContentResolver.setSyncAutomatically(account, Constants.SYNC_ACCOUNT_PLAYLIST_AUTHORITY, syncEnabled); - ContentResolver.setSyncAutomatically(account, Constants.SYNC_ACCOUNT_PODCAST_AUTHORITY, syncEnabled); + return true; + } + }); + } - return true; - } - }); - syncInterval.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object newValue) { - Integer syncInterval = Integer.parseInt(((String) newValue)); + if(serversCategory != null) { + addServerPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(Preference preference) { + serverCount++; + String instance = String.valueOf(serverCount); + serversCategory.addPreference(addServer(serverCount)); - Account account = new Account(Constants.SYNC_ACCOUNT_NAME, Constants.SYNC_ACCOUNT_TYPE); - ContentResolver.addPeriodicSync(account, Constants.SYNC_ACCOUNT_PLAYLIST_AUTHORITY, new Bundle(), 60L * syncInterval); - ContentResolver.addPeriodicSync(account, Constants.SYNC_ACCOUNT_PODCAST_AUTHORITY, new Bundle(), 60L * syncInterval); + 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.commit(); - return true; - } - }); + serverSettings.put(instance, new ServerSettings(instance)); + + return true; + } + }); - 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)); + 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)); + } } SharedPreferences prefs = Util.getPreferences(context); @@ -309,61 +335,77 @@ public class SettingsFragment extends PreferenceCompatFragment implements Shared return; } - theme.setSummary(theme.getEntry()); - maxBitrateWifi.setSummary(maxBitrateWifi.getEntry()); - maxBitrateMobile.setSummary(maxBitrateMobile.getEntry()); - maxVideoBitrateWifi.setSummary(maxVideoBitrateWifi.getEntry()); - maxVideoBitrateMobile.setSummary(maxVideoBitrateMobile.getEntry()); - networkTimeout.setSummary(networkTimeout.getEntry()); - cacheLocation.setSummary(cacheLocation.getText()); - preloadCountWifi.setSummary(preloadCountWifi.getEntry()); - preloadCountMobile.setSummary(preloadCountMobile.getEntry()); - keepPlayedCount.setSummary(keepPlayedCount.getEntry()); - tempLoss.setSummary(tempLoss.getEntry()); - pauseDisconnect.setSummary(pauseDisconnect.getEntry()); - videoPlayer.setSummary(videoPlayer.getEntry()); - syncInterval.setSummary(syncInterval.getEntry()); - openToTab.setSummary(openToTab.getEntry()); - try { - if(megabyteFromat == null) { - megabyteFromat = new DecimalFormat(getResources().getString(R.string.util_bytes_format_megabyte)); - } - - cacheSize.setSummary(megabyteFromat.format((double) Integer.parseInt(cacheSize.getText())).replace(".00", "")); - } catch(Exception e) { - Log.e(TAG, "Failed to format cache size", e); - cacheSize.setSummary(cacheSize.getText()); + if(theme != null) { + theme.setSummary(theme.getEntry()); + openToTab.setSummary(openToTab.getEntry()); } - if(syncEnabled.isChecked()) { - if(!syncInterval.isEnabled()) { - syncInterval.setEnabled(true); - syncWifi.setEnabled(true); - syncNotification.setEnabled(true); - syncStarred.setEnabled(true); - syncMostRecent.setEnabled(true); + + if(cacheSize != null) { + maxBitrateWifi.setSummary(maxBitrateWifi.getEntry()); + maxBitrateMobile.setSummary(maxBitrateMobile.getEntry()); + maxVideoBitrateWifi.setSummary(maxVideoBitrateWifi.getEntry()); + maxVideoBitrateMobile.setSummary(maxVideoBitrateMobile.getEntry()); + networkTimeout.setSummary(networkTimeout.getEntry()); + cacheLocation.setSummary(cacheLocation.getText()); + preloadCountWifi.setSummary(preloadCountWifi.getEntry()); + preloadCountMobile.setSummary(preloadCountMobile.getEntry()); + + try { + if(megabyteFromat == null) { + megabyteFromat = new DecimalFormat(getResources().getString(R.string.util_bytes_format_megabyte)); + } + + cacheSize.setSummary(megabyteFromat.format((double) Integer.parseInt(cacheSize.getText())).replace(".00", "")); + } catch(Exception e) { + Log.e(TAG, "Failed to format cache size", e); + cacheSize.setSummary(cacheSize.getText()); } - } else { - if(syncInterval.isEnabled()) { - syncInterval.setEnabled(false); - syncWifi.setEnabled(false); - syncNotification.setEnabled(false); - syncStarred.setEnabled(false); - syncMostRecent.setEnabled(false); + } + + if(keepPlayedCount != null) { + keepPlayedCount.setSummary(keepPlayedCount.getEntry()); + tempLoss.setSummary(tempLoss.getEntry()); + pauseDisconnect.setSummary(pauseDisconnect.getEntry()); + videoPlayer.setSummary(videoPlayer.getEntry()); + + if(replayGain.isChecked()) { + replayGainType.setEnabled(true); + replayGainBump.setEnabled(true); + replayGainUntagged.setEnabled(true); + } else { + replayGainType.setEnabled(false); + replayGainBump.setEnabled(false); + replayGainUntagged.setEnabled(false); } + replayGainType.setSummary(replayGainType.getEntry()); } - if(replayGain.isChecked()) { - replayGainType.setEnabled(true); - replayGainBump.setEnabled(true); - replayGainUntagged.setEnabled(true); - } else { - replayGainType.setEnabled(false); - replayGainBump.setEnabled(false); - replayGainUntagged.setEnabled(false); + + if(syncEnabled != null) { + syncInterval.setSummary(syncInterval.getEntry()); + + if(syncEnabled.isChecked()) { + if(!syncInterval.isEnabled()) { + syncInterval.setEnabled(true); + syncWifi.setEnabled(true); + syncNotification.setEnabled(true); + syncStarred.setEnabled(true); + syncMostRecent.setEnabled(true); + } + } else { + if(syncInterval.isEnabled()) { + syncInterval.setEnabled(false); + syncWifi.setEnabled(false); + syncNotification.setEnabled(false); + syncStarred.setEnabled(false); + syncMostRecent.setEnabled(false); + } + } } - replayGainType.setSummary(replayGainType.getEntry()); - for (ServerSettings ss : serverSettings.values()) { - ss.update(); + if(serversCategory != null) { + for (ServerSettings ss : serverSettings.values()) { + ss.update(); + } } } |