From 2015cd0928f36045a07ef622ff8711497d2e8b9f Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 13 Oct 2013 22:20:34 -0400 Subject: [PATCH] [Android] Implement OnSharedPreferenceChangeListener within PrefsActivity.java. This allows us to immediately save to the ini config when a preference in the front-end is changed, rather than waiting for the settings window to close. This also allows us to remove handling for preferences from CPUSettingsFragment.java and VideoSettingsFragment.java. --- .../settings/CPUSettingsFragment.java | 21 ------ .../dolphinemu/settings/PrefsActivity.java | 65 ++++++++++--------- .../settings/VideoSettingsFragment.java | 9 --- 3 files changed, 33 insertions(+), 62 deletions(-) diff --git a/Source/Android/src/org/dolphinemu/dolphinemu/settings/CPUSettingsFragment.java b/Source/Android/src/org/dolphinemu/dolphinemu/settings/CPUSettingsFragment.java index 309412c2f4..47f0cb7d35 100644 --- a/Source/Android/src/org/dolphinemu/dolphinemu/settings/CPUSettingsFragment.java +++ b/Source/Android/src/org/dolphinemu/dolphinemu/settings/CPUSettingsFragment.java @@ -8,7 +8,6 @@ package org.dolphinemu.dolphinemu.settings; import org.dolphinemu.dolphinemu.R; -import android.app.Activity; import android.os.Build; import android.os.Bundle; import android.preference.ListPreference; @@ -19,8 +18,6 @@ import android.preference.PreferenceFragment; */ public final class CPUSettingsFragment extends PreferenceFragment { - private Activity m_activity; - @Override public void onCreate(Bundle savedInstanceState) { @@ -51,22 +48,4 @@ public final class CPUSettingsFragment extends PreferenceFragment cpuCores.setEntryValues(R.array.emuCoreValuesOther); } } - - @Override - public void onAttach(Activity activity) - { - super.onAttach(activity); - - // Cache the activity instance. - m_activity = activity; - } - - @Override - public void onDestroy() - { - super.onDestroy(); - - // When this fragment is destroyed, force the settings to be saved to the ini file. - UserPreferences.SavePrefsToIni(m_activity); - } } diff --git a/Source/Android/src/org/dolphinemu/dolphinemu/settings/PrefsActivity.java b/Source/Android/src/org/dolphinemu/dolphinemu/settings/PrefsActivity.java index bd1259b8ae..51153866e1 100644 --- a/Source/Android/src/org/dolphinemu/dolphinemu/settings/PrefsActivity.java +++ b/Source/Android/src/org/dolphinemu/dolphinemu/settings/PrefsActivity.java @@ -14,7 +14,10 @@ import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; +import android.content.SharedPreferences; +import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.os.Bundle; +import android.preference.PreferenceManager; import android.support.v13.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; @@ -22,15 +25,8 @@ import android.support.v4.view.ViewPager; * Main activity that manages all of the preference fragments used to display * the settings to the user. */ -public final class PrefsActivity extends Activity implements ActionBar.TabListener +public final class PrefsActivity extends Activity implements ActionBar.TabListener, OnSharedPreferenceChangeListener { - /** - * The {@link FragmentPagerAdapter} that will provide settings - * fragments for each of the sections. This will also keep every - * loaded fragment in memory. - */ - private SectionsPagerAdapter mSectionsPagerAdapter; - /** * The {@link ViewPager} that will host the section contents. */ @@ -40,19 +36,25 @@ public final class PrefsActivity extends Activity implements ActionBar.TabListen protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + + // Set the ViewPager. setContentView(R.layout.prefs_viewpager); - + mViewPager = (ViewPager) findViewById(R.id.pager); + + // Set the ViewPager adapter. + final ViewPagerAdapter mSectionsPagerAdapter = new ViewPagerAdapter(getFragmentManager()); + mViewPager.setAdapter(mSectionsPagerAdapter); + + // Register the preference change listener. + final SharedPreferences sPrefs = PreferenceManager.getDefaultSharedPreferences(this); + sPrefs.registerOnSharedPreferenceChangeListener(this); + // Set up the action bar. final ActionBar actionBar = getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); - - // Create the adapter that will return a fragment for each of the three - // primary sections of the app. - mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager()); - - // Set up the ViewPager with the sections adapter. - mViewPager = (ViewPager) findViewById(R.id.pager); - mViewPager.setAdapter(mSectionsPagerAdapter); + actionBar.addTab(actionBar.newTab().setText(R.string.cpu_settings).setTabListener(this)); + actionBar.addTab(actionBar.newTab().setText(R.string.input_settings).setTabListener(this)); + actionBar.addTab(actionBar.newTab().setText(R.string.video_settings).setTabListener(this)); // When swiping between different sections, select the corresponding // tab. We can also use ActionBar.Tab#select() to do this if we have @@ -65,19 +67,6 @@ public final class PrefsActivity extends Activity implements ActionBar.TabListen actionBar.setSelectedNavigationItem(position); } } ); - - // Create a tab with text corresponding to the page title defined by - // the adapter. Also specify this Activity object, which implements - // the TabListener interface, as the callback (listener) for when - // this tab is selected. - actionBar.addTab(actionBar.newTab().setText(R.string.cpu_settings).setTabListener(this)); - actionBar.addTab(actionBar.newTab().setText(R.string.input_settings).setTabListener(this)); - actionBar.addTab(actionBar.newTab().setText(R.string.video_settings).setTabListener(this)); - } - - public void onTabReselected(Tab tab, FragmentTransaction ft) - { - // Do nothing. } public void onTabSelected(Tab tab, FragmentTransaction ft) @@ -86,18 +75,30 @@ public final class PrefsActivity extends Activity implements ActionBar.TabListen mViewPager.setCurrentItem(tab.getPosition()); } + public void onTabReselected(Tab tab, FragmentTransaction ft) + { + // Do nothing. + } + public void onTabUnselected(Tab tab, FragmentTransaction ft) { // Do nothing. } + + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) + { + // If any change is made to the preferences in the front-end, immediately save them. + UserPreferences.SavePrefsToIni(this); + } /** * A {@link FragmentPagerAdapter} that returns a fragment * corresponding to one of the sections/tabs/pages. */ - public final class SectionsPagerAdapter extends FragmentPagerAdapter + private final class ViewPagerAdapter extends FragmentPagerAdapter { - public SectionsPagerAdapter(FragmentManager fm) + public ViewPagerAdapter(FragmentManager fm) { super(fm); } diff --git a/Source/Android/src/org/dolphinemu/dolphinemu/settings/VideoSettingsFragment.java b/Source/Android/src/org/dolphinemu/dolphinemu/settings/VideoSettingsFragment.java index ef8bafb34c..fffea888a7 100644 --- a/Source/Android/src/org/dolphinemu/dolphinemu/settings/VideoSettingsFragment.java +++ b/Source/Android/src/org/dolphinemu/dolphinemu/settings/VideoSettingsFragment.java @@ -287,13 +287,4 @@ public final class VideoSettingsFragment extends PreferenceFragment // Cache the activity instance. m_activity = activity; } - - @Override - public void onDestroy() - { - super.onDestroy(); - - // When the fragment is done being used, save the settings to the Dolphin ini file. - UserPreferences.SavePrefsToIni(m_activity); - } }