From d412253327b0009b1da083503bb91f2512f6a307 Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Thu, 11 Apr 2013 18:36:47 -0700 Subject: Move to keeping track of which menus should be shown manually, fragment manager sucks at it --- .../daneren2005/dsub/activity/MainActivity.java | 126 ++--------------- .../dsub/activity/SubsonicActivity.java | 156 ++++++++++++++++++++- .../daneren2005/dsub/fragments/MainFragment.java | 2 +- .../dsub/fragments/SelectArtistFragment.java | 2 +- .../dsub/fragments/SelectDirectoryFragment.java | 2 +- .../dsub/fragments/SelectPlaylistFragment.java | 2 +- .../dsub/fragments/SubsonicTabFragment.java | 12 +- 7 files changed, 174 insertions(+), 128 deletions(-) diff --git a/subsonic-android/src/github/daneren2005/dsub/activity/MainActivity.java b/subsonic-android/src/github/daneren2005/dsub/activity/MainActivity.java index c64d87a8..06925e8b 100644 --- a/subsonic-android/src/github/daneren2005/dsub/activity/MainActivity.java +++ b/subsonic-android/src/github/daneren2005/dsub/activity/MainActivity.java @@ -2,33 +2,22 @@ package github.daneren2005.dsub.activity; import android.app.AlertDialog; import android.content.DialogInterface; -import com.actionbarsherlock.app.ActionBar; -import com.actionbarsherlock.app.ActionBar.Tab; -import com.actionbarsherlock.app.ActionBar.TabListener; -import com.actionbarsherlock.app.SherlockFragmentActivity; import android.content.Intent; import android.os.Bundle; -import com.actionbarsherlock.app.SherlockFragment; -import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentManager; -import android.support.v4.app.FragmentPagerAdapter; -import android.support.v4.app.FragmentTransaction; +import com.actionbarsherlock.app.ActionBar; +import com.actionbarsherlock.view.Menu; import android.support.v4.view.ViewPager; +import android.util.Log; import github.daneren2005.dsub.R; import github.daneren2005.dsub.fragments.MainFragment; import github.daneren2005.dsub.fragments.SelectArtistFragment; import github.daneren2005.dsub.fragments.SelectPlaylistFragment; -import github.daneren2005.dsub.fragments.SubsonicTabFragment; import github.daneren2005.dsub.service.DownloadServiceImpl; import github.daneren2005.dsub.util.Util; -import java.util.ArrayList; -import java.util.List; public class MainActivity extends SubsonicActivity { private static final String TAG = MainActivity.class.getSimpleName(); private static boolean infoDialogDisplayed; - private MainActivityPagerAdapter pagerAdapter; - private ViewPager viewPager; @Override public void onCreate(Bundle savedInstanceState) { @@ -36,7 +25,7 @@ public class MainActivity extends SubsonicActivity { setContentView(R.layout.main); viewPager = (ViewPager) findViewById(R.id.pager); - pagerAdapter = new MainActivityPagerAdapter(this, viewPager); + pagerAdapter = new TabPagerAdapter(this, viewPager); viewPager.setAdapter(pagerAdapter); viewPager.setOnPageChangeListener(pagerAdapter); @@ -84,19 +73,19 @@ public class MainActivity extends SubsonicActivity { } } else { super.onBackPressed(); + pagerAdapter.removeCurrent(); } } - - protected void addTab(int titleRes, Class fragmentClass, Bundle args) { - pagerAdapter.addTab(getString(titleRes), fragmentClass, args); - } - protected void addTab(CharSequence title, Class fragmentClass, Bundle args) { - pagerAdapter.addTab(title, fragmentClass, args); + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + com.actionbarsherlock.view.MenuInflater menuInflater = getSupportMenuInflater(); + pagerAdapter.onCreateOptionsMenu(menu, menuInflater); + return true; } - - private void exit() { - stopService(new Intent(this, DownloadServiceImpl.class)); - finish(); + @Override + public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) { + return pagerAdapter.onOptionsItemSelected(item); } private void showInfoDialog() { @@ -107,91 +96,4 @@ public class MainActivity extends SubsonicActivity { } } } - - - - public class MainActivityPagerAdapter extends FragmentPagerAdapter implements TabListener, ViewPager.OnPageChangeListener { - private SherlockFragmentActivity activity; - private ViewPager pager; - private ActionBar actionBar; - private SubsonicTabFragment currentFragment; - private List tabs = new ArrayList(); - private List frags = new ArrayList(); - - public MainActivityPagerAdapter(SherlockFragmentActivity activity, ViewPager pager) { - super(activity.getSupportFragmentManager()); - this.activity = activity; - this.actionBar = activity.getSupportActionBar(); - this.pager = pager; - } - - @Override - public Fragment getItem(int i) { - final TabInfo tabInfo = (TabInfo)tabs.get(i); - SherlockFragment frag = (SherlockFragment) Fragment.instantiate(activity, tabInfo.fragmentClass.getName(), tabInfo.args); - frags.add(i, frag); - if(currentFragment == null) { - currentFragment = (SubsonicTabFragment) frag; - currentFragment.setPrimaryFragment(true); - } - return frag; - } - - @Override - public int getCount() { - return tabs.size(); - } - - public void onTabSelected(Tab tab, FragmentTransaction ft) { - TabInfo tabInfo = (TabInfo) tab.getTag(); - for (int i = 0; i < tabs.size(); i++) { - if ( tabs.get(i) == tabInfo ) { - pager.setCurrentItem(i); - break; - } - } - } - - public void onTabUnselected(Tab tab, FragmentTransaction ft) {} - - public void onTabReselected(Tab tab, FragmentTransaction ft) {} - - public void onPageScrollStateChanged(int arg0) {} - - public void onPageScrolled(int arg0, float arg1, int arg2) {} - - public void onPageSelected(int position) { - actionBar.setSelectedNavigationItem(position); - if(currentFragment != null) { - currentFragment.setPrimaryFragment(false); - } - currentFragment = (SubsonicTabFragment) frags.get(position); - if(currentFragment != null) { - currentFragment.setPrimaryFragment(true); - } - } - - public void addTab(CharSequence title, Class fragmentClass, Bundle args) { - final TabInfo tabInfo = new TabInfo(fragmentClass, args); - - Tab tab = actionBar.newTab(); - tab.setText(title); - tab.setTabListener(this); - tab.setTag(tabInfo); - - tabs.add(tabInfo); - - actionBar.addTab(tab); - notifyDataSetChanged(); - } - - private class TabInfo { - public final Class fragmentClass; - public final Bundle args; - public TabInfo(Class fragmentClass, Bundle args) { - this.fragmentClass = fragmentClass; - this.args = args; - } - } - } } diff --git a/subsonic-android/src/github/daneren2005/dsub/activity/SubsonicActivity.java b/subsonic-android/src/github/daneren2005/dsub/activity/SubsonicActivity.java index bada0da0..61a8c18c 100644 --- a/subsonic-android/src/github/daneren2005/dsub/activity/SubsonicActivity.java +++ b/subsonic-android/src/github/daneren2005/dsub/activity/SubsonicActivity.java @@ -7,14 +7,21 @@ import android.media.AudioManager; import android.os.Build; import android.os.Bundle; import android.os.Environment; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentPagerAdapter; +import android.support.v4.app.FragmentTransaction; +import android.support.v4.view.ViewPager; import android.util.Log; import android.view.KeyEvent; import com.actionbarsherlock.app.ActionBar; +import com.actionbarsherlock.app.ActionBar.Tab; +import com.actionbarsherlock.app.ActionBar.TabListener; import com.actionbarsherlock.app.SherlockFragmentActivity; +import com.actionbarsherlock.app.SherlockFragment; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuItem; -import com.actionbarsherlock.view.Window; import github.daneren2005.dsub.R; +import github.daneren2005.dsub.fragments.SubsonicTabFragment; import github.daneren2005.dsub.service.DownloadService; import github.daneren2005.dsub.service.DownloadServiceImpl; import github.daneren2005.dsub.updates.Updater; @@ -22,12 +29,16 @@ import github.daneren2005.dsub.util.ImageLoader; import github.daneren2005.dsub.util.Util; import java.io.File; import java.io.PrintWriter; +import java.util.ArrayList; +import java.util.List; public class SubsonicActivity extends SherlockFragmentActivity { private static final String TAG = SubsonicActivity.class.getSimpleName(); private static ImageLoader IMAGE_LOADER; protected static String theme; private boolean destroyed = false; + protected TabPagerAdapter pagerAdapter; + protected ViewPager viewPager; @Override protected void onCreate(Bundle bundle) { @@ -75,6 +86,13 @@ public class SubsonicActivity extends SherlockFragmentActivity { } return super.onKeyDown(keyCode, event); } + + protected void addTab(int titleRes, Class fragmentClass, Bundle args) { + pagerAdapter.addTab(getString(titleRes), fragmentClass, args); + } + protected void addTab(CharSequence title, Class fragmentClass, Bundle args) { + pagerAdapter.addTab(title, fragmentClass, args); + } protected void restart() { Intent intent = new Intent(this, this.getClass()); @@ -132,6 +150,13 @@ public class SubsonicActivity extends SherlockFragmentActivity { } return DownloadServiceImpl.getInstance(); } + + public ViewPager getViewPager() { + return viewPager; + } + public TabPagerAdapter getPagerAdapter() { + return pagerAdapter; + } private void setUncaughtExceptionHandler() { Thread.UncaughtExceptionHandler handler = Thread.getDefaultUncaughtExceptionHandler(); @@ -191,4 +216,133 @@ public class SubsonicActivity extends SherlockFragmentActivity { } } + + public class TabPagerAdapter extends FragmentPagerAdapter implements TabListener, ViewPager.OnPageChangeListener { + private SherlockFragmentActivity activity; + private ViewPager pager; + private ActionBar actionBar; + private SubsonicTabFragment currentFragment; + private List tabs = new ArrayList(); + private List frags = new ArrayList(); + private int currentPosition; + + public TabPagerAdapter(SherlockFragmentActivity activity, ViewPager pager) { + super(activity.getSupportFragmentManager()); + this.activity = activity; + this.actionBar = activity.getSupportActionBar(); + this.pager = pager; + this.currentPosition = 0; + } + + @Override + public Fragment getItem(int i) { + final TabInfo tabInfo = (TabInfo)tabs.get(i); + SherlockFragment frag = (SherlockFragment) Fragment.instantiate(activity, tabInfo.fragmentClass.getName(), tabInfo.args); + List fragStack = new ArrayList(); + fragStack.add(frag); + frags.add(i, fragStack); + if(currentFragment == null) { + currentFragment = (SubsonicTabFragment) frag; + currentFragment.setPrimaryFragment(true); + } + return frag; + } + + @Override + public int getCount() { + return tabs.size(); + } + + public void onCreateOptionsMenu(Menu menu, com.actionbarsherlock.view.MenuInflater menuInflater) { + if(currentFragment != null) { + currentFragment.onCreateOptionsMenu(menu, menuInflater); + } + } + public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) { + if(currentFragment != null) { + return currentFragment.onOptionsItemSelected(item); + } else { + return false; + } + } + + public void onTabSelected(Tab tab, FragmentTransaction ft) { + TabInfo tabInfo = (TabInfo) tab.getTag(); + for (int i = 0; i < tabs.size(); i++) { + if ( tabs.get(i) == tabInfo ) { + pager.setCurrentItem(i); + break; + } + } + } + + public void onTabUnselected(Tab tab, FragmentTransaction ft) {} + + public void onTabReselected(Tab tab, FragmentTransaction ft) {} + + public void onPageScrollStateChanged(int arg0) {} + + public void onPageScrolled(int arg0, float arg1, int arg2) {} + + public void onPageSelected(int position) { + currentPosition = position; + actionBar.setSelectedNavigationItem(position); + if(currentFragment != null) { + currentFragment.setPrimaryFragment(false); + } + List fragStack = (List)frags.get(position); + currentFragment = (SubsonicTabFragment) fragStack.get(fragStack.size() - 1); + if(currentFragment != null) { + currentFragment.setPrimaryFragment(true); + } + activity.invalidateOptionsMenu(); + } + + public void addTab(CharSequence title, Class fragmentClass, Bundle args) { + final TabInfo tabInfo = new TabInfo(fragmentClass, args); + + Tab tab = actionBar.newTab(); + tab.setText(title); + tab.setTabListener(this); + tab.setTag(tabInfo); + + tabs.add(tabInfo); + + actionBar.addTab(tab); + notifyDataSetChanged(); + } + + public void replaceCurrent(SubsonicTabFragment fragment) { + if(currentFragment != null) { + currentFragment.setPrimaryFragment(false); + } + List fragStack = (List)frags.get(currentPosition); + fragStack.add(fragment); + + currentFragment = fragment; + currentFragment.setPrimaryFragment(true); + activity.invalidateOptionsMenu(); + } + + public void removeCurrent() { + if(currentFragment != null) { + currentFragment.setPrimaryFragment(false); + } + List fragStack = (List)frags.get(currentPosition); + fragStack.remove(fragStack.size() - 1); + + currentFragment = (SubsonicTabFragment) fragStack.get(fragStack.size() - 1); + currentFragment.setPrimaryFragment(true); + activity.invalidateOptionsMenu(); + } + + private class TabInfo { + public final Class fragmentClass; + public final Bundle args; + public TabInfo(Class fragmentClass, Bundle args) { + this.fragmentClass = fragmentClass; + this.args = args; + } + } + } } diff --git a/subsonic-android/src/github/daneren2005/dsub/fragments/MainFragment.java b/subsonic-android/src/github/daneren2005/dsub/fragments/MainFragment.java index 8f3682bd..ae382cd1 100644 --- a/subsonic-android/src/github/daneren2005/dsub/fragments/MainFragment.java +++ b/subsonic-android/src/github/daneren2005/dsub/fragments/MainFragment.java @@ -64,7 +64,7 @@ public class MainFragment extends LibraryFunctionsFragment { } @Override - public void onSupportCreateOptionsMenu(Menu menu, MenuInflater menuInflater) { + public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) { menuInflater.inflate(R.menu.main, menu); } diff --git a/subsonic-android/src/github/daneren2005/dsub/fragments/SelectArtistFragment.java b/subsonic-android/src/github/daneren2005/dsub/fragments/SelectArtistFragment.java index 0efac370..4e8d26e9 100644 --- a/subsonic-android/src/github/daneren2005/dsub/fragments/SelectArtistFragment.java +++ b/subsonic-android/src/github/daneren2005/dsub/fragments/SelectArtistFragment.java @@ -63,7 +63,7 @@ public class SelectArtistFragment extends LibraryFunctionsFragment implements Ad } @Override - public void onSupportCreateOptionsMenu(Menu menu, com.actionbarsherlock.view.MenuInflater menuInflater) { + public void onCreateOptionsMenu(Menu menu, com.actionbarsherlock.view.MenuInflater menuInflater) { menuInflater.inflate(R.menu.select_artist, menu); } diff --git a/subsonic-android/src/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java b/subsonic-android/src/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java index cba310a3..92155fe1 100644 --- a/subsonic-android/src/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java +++ b/subsonic-android/src/github/daneren2005/dsub/fragments/SelectDirectoryFragment.java @@ -108,7 +108,7 @@ public class SelectDirectoryFragment extends LibraryFunctionsFragment implements } @Override - public void onSupportCreateOptionsMenu(com.actionbarsherlock.view.Menu menu, com.actionbarsherlock.view.MenuInflater menuInflater) { + public void onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu, com.actionbarsherlock.view.MenuInflater menuInflater) { if(licenseValid == null) { menuInflater.inflate(R.menu.empty, menu); } diff --git a/subsonic-android/src/github/daneren2005/dsub/fragments/SelectPlaylistFragment.java b/subsonic-android/src/github/daneren2005/dsub/fragments/SelectPlaylistFragment.java index 35684be9..b09fe802 100644 --- a/subsonic-android/src/github/daneren2005/dsub/fragments/SelectPlaylistFragment.java +++ b/subsonic-android/src/github/daneren2005/dsub/fragments/SelectPlaylistFragment.java @@ -57,7 +57,7 @@ public class SelectPlaylistFragment extends LibraryFunctionsFragment implements } @Override - public void onSupportCreateOptionsMenu(com.actionbarsherlock.view.Menu menu, com.actionbarsherlock.view.MenuInflater menuInflater) { + public void onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu, com.actionbarsherlock.view.MenuInflater menuInflater) { menuInflater.inflate(R.menu.select_playlist, menu); } diff --git a/subsonic-android/src/github/daneren2005/dsub/fragments/SubsonicTabFragment.java b/subsonic-android/src/github/daneren2005/dsub/fragments/SubsonicTabFragment.java index 639284b8..017b2c89 100644 --- a/subsonic-android/src/github/daneren2005/dsub/fragments/SubsonicTabFragment.java +++ b/subsonic-android/src/github/daneren2005/dsub/fragments/SubsonicTabFragment.java @@ -45,7 +45,6 @@ public class SubsonicTabFragment extends SherlockFragment { @Override public void onCreate(Bundle bundle) { super.onCreate(bundle); - setHasOptionsMenu(true); } @Override @@ -64,16 +63,6 @@ public class SubsonicTabFragment extends SherlockFragment { context = (SubsonicActivity)activity; } - @Override - public void onCreateOptionsMenu(Menu menu, com.actionbarsherlock.view.MenuInflater menuInflater) { - if(primaryFragment) { - onSupportCreateOptionsMenu(menu, menuInflater); - } - } - public void onSupportCreateOptionsMenu(Menu menu, com.actionbarsherlock.view.MenuInflater menuInflater) { - - } - public DownloadService getDownloadService() { return context != null ? context.getDownloadService() : null; } @@ -85,6 +74,7 @@ public class SubsonicTabFragment extends SherlockFragment { public void replaceFragment(SubsonicTabFragment fragment, int id) { this.setPrimaryFragment(false); fragment.setPrimaryFragment(true); + context.getPagerAdapter().replaceCurrent(fragment); FragmentTransaction trans = getFragmentManager().beginTransaction(); trans.replace(id, fragment); -- cgit v1.2.3