From 4aa68e74e099f7a18ee339a70d8a7ec0fdb6dffa Mon Sep 17 00:00:00 2001 From: Martin Fietz Date: Fri, 17 Apr 2015 13:16:14 +0200 Subject: [PATCH] Bux fixes, tests --- .../test/antennapod/ui/MainActivityTest.java | 72 +++++++++++++++++-- .../antennapod/activity/MainActivity.java | 28 +++++--- .../antennapod/adapter/NavListAdapter.java | 28 ++++---- app/src/main/res/layout/nav_list.xml | 10 ++- .../core/preferences/UserPreferences.java | 2 +- 5 files changed, 105 insertions(+), 35 deletions(-) diff --git a/app/src/androidTest/java/de/test/antennapod/ui/MainActivityTest.java b/app/src/androidTest/java/de/test/antennapod/ui/MainActivityTest.java index bbcc4ce5c..b7acd1d28 100644 --- a/app/src/androidTest/java/de/test/antennapod/ui/MainActivityTest.java +++ b/app/src/androidTest/java/de/test/antennapod/ui/MainActivityTest.java @@ -7,11 +7,21 @@ import android.widget.ListView; import com.robotium.solo.Solo; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + import de.danoeh.antennapod.R; import de.danoeh.antennapod.activity.DefaultOnlineFeedViewActivity; import de.danoeh.antennapod.activity.MainActivity; import de.danoeh.antennapod.core.feed.Feed; +import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.storage.PodDBAdapter; +import de.danoeh.antennapod.fragment.AllEpisodesFragment; +import de.danoeh.antennapod.fragment.DownloadsFragment; +import de.danoeh.antennapod.fragment.NewEpisodesFragment; +import de.danoeh.antennapod.fragment.PlaybackHistoryFragment; +import de.danoeh.antennapod.fragment.QueueFragment; import de.danoeh.antennapod.preferences.PreferenceController; /** @@ -71,11 +81,11 @@ public class MainActivityTest extends ActivityInstrumentationTestCase2()); + openNavDrawer(); + solo.clickLongOnText(solo.getString(R.string.queue_label)); + solo.waitForDialogToOpen(); + solo.clickOnText(solo.getString(R.string.all_episodes_label)); + solo.clickOnText(solo.getString(R.string.playback_history_label)); + solo.clickOnText(solo.getString(R.string.confirm_label)); + solo.waitForDialogToClose(); + List hidden = UserPreferences.getHiddenDrawerItems(); + assertEquals(2, hidden.size()); + assertTrue(hidden.contains(AllEpisodesFragment.TAG)); + assertTrue(hidden.contains(PlaybackHistoryFragment.TAG)); + } + + public void testDrawerPreferencesUnhideSomeElements() { + List hidden = Arrays.asList(NewEpisodesFragment.TAG, DownloadsFragment.TAG); + UserPreferences.setHiddenDrawerItems(getInstrumentation().getTargetContext(), hidden); + openNavDrawer(); + solo.clickLongOnText(solo.getString(R.string.queue_label)); + solo.waitForDialogToOpen(); + solo.clickOnText(solo.getString(R.string.downloads_label)); + solo.clickOnText(solo.getString(R.string.queue_label)); + solo.clickOnText(solo.getString(R.string.confirm_label)); + solo.waitForDialogToClose(); + hidden = UserPreferences.getHiddenDrawerItems(); + assertEquals(2, hidden.size()); + assertTrue(hidden.contains(QueueFragment.TAG)); + assertTrue(hidden.contains(NewEpisodesFragment.TAG)); + } + + public void testDrawerPreferencesHideAllElements() { + UserPreferences.setHiddenDrawerItems(getInstrumentation().getTargetContext(), new ArrayList()); + String[] titles = getInstrumentation().getTargetContext().getResources().getStringArray(R.array.nav_drawer_titles); + + openNavDrawer(); + solo.clickLongOnText(solo.getString(R.string.queue_label)); + solo.waitForDialogToOpen(); + for(String title : titles) { + solo.clickOnText(title); + } + solo.clickOnText(solo.getString(R.string.confirm_label)); + solo.waitForDialogToClose(); + List hidden = UserPreferences.getHiddenDrawerItems(); + assertEquals(6, hidden.size()); + for(String tag : MainActivity.NAV_DRAWER_TAGS) { + assertTrue(hidden.contains(tag)); + } + } } diff --git a/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java index 052955130..2b0bf72f1 100644 --- a/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java +++ b/app/src/main/java/de/danoeh/antennapod/activity/MainActivity.java @@ -111,7 +111,6 @@ public class MainActivity extends ActionBarActivity implements NavDrawerActivity drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); navList = (ListView) findViewById(R.id.nav_list); navDrawer = findViewById(R.id.nav_layout); - Log.i(TAG, ""); drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_open, R.string.drawer_close) { @Override public void onDrawerOpened(View drawerView) { @@ -254,24 +253,27 @@ public class MainActivity extends ActionBarActivity implements NavDrawerActivity } public void loadFragment(int index, Bundle args) { - int numTags = navAdapter.getTags().size(); - if (index <= numTags) { + if (index < navAdapter.getSubscriptionOffset()) { String tag = navAdapter.getTags().get(index); loadFragment(tag, args); } else { - int pos = index - numTags; + int pos = index - navAdapter.getSubscriptionOffset(); loadFeedFragmentByPosition(pos, args); } } public void loadFragment(final String tag, Bundle args) { + Log.d(TAG, "loadFragment(\"" + tag + "\", " + args + ")"); Fragment fragment = null; switch (tag) { + case QueueFragment.TAG: + fragment = new QueueFragment(); + break; case NewEpisodesFragment.TAG: fragment = new NewEpisodesFragment(); break; - case QueueFragment.TAG: - fragment = new QueueFragment(); + case AllEpisodesFragment.TAG: + fragment = new AllEpisodesFragment(); break; case DownloadsFragment.TAG: fragment = new DownloadsFragment(); @@ -283,8 +285,8 @@ public class MainActivity extends ActionBarActivity implements NavDrawerActivity fragment = new AddFeedFragment(); break; } - String title = navAdapter.getLabel(tag); - getSupportActionBar().setTitle(title); + currentTitle = navAdapter.getLabel(tag); + getSupportActionBar().setTitle(currentTitle); selectedNavListIndex = navAdapter.getTags().indexOf(tag); if (args != null) { fragment.setArguments(args); @@ -299,7 +301,8 @@ public class MainActivity extends ActionBarActivity implements NavDrawerActivity Feed feed = itemAccess.getItem(relPos); Fragment fragment = ItemlistFragment.newInstance(feed.getId()); selectedNavListIndex = navAdapter.getSubscriptionOffset() + relPos; - getSupportActionBar().setTitle(""); + currentTitle = ""; + getSupportActionBar().setTitle(currentTitle); loadFragment(fragment); } @@ -420,7 +423,8 @@ public class MainActivity extends ActionBarActivity implements NavDrawerActivity StorageUtils.checkStorageAvailability(this); Intent intent = getIntent(); - if (navDrawerData != null && intent.hasExtra(EXTRA_NAV_INDEX) && intent.hasExtra(EXTRA_NAV_TYPE)) { + if (navDrawerData != null && intent.hasExtra(EXTRA_NAV_TYPE) && + (intent.hasExtra(EXTRA_NAV_INDEX) || intent.hasExtra(EXTRA_FRAGMENT_TAG))) { handleNavIntent(); } @@ -538,8 +542,10 @@ public class MainActivity extends ActionBarActivity implements NavDrawerActivity }; private void handleNavIntent() { + Log.d(TAG, "handleNavIntent()"); Intent intent = getIntent(); - if (intent.hasExtra(EXTRA_NAV_INDEX) && intent.hasExtra(EXTRA_NAV_TYPE)) { + if (intent.hasExtra(EXTRA_NAV_TYPE) && + intent.hasExtra(EXTRA_NAV_INDEX) || intent.hasExtra(EXTRA_FRAGMENT_TAG)) { int index = intent.getIntExtra(EXTRA_NAV_INDEX, -1); String tag = intent.getStringExtra(EXTRA_FRAGMENT_TAG); Bundle args = intent.getBundleExtra(EXTRA_FRAGMENT_ARGS); diff --git a/app/src/main/java/de/danoeh/antennapod/adapter/NavListAdapter.java b/app/src/main/java/de/danoeh/antennapod/adapter/NavListAdapter.java index c763c7b61..907093ad6 100644 --- a/app/src/main/java/de/danoeh/antennapod/adapter/NavListAdapter.java +++ b/app/src/main/java/de/danoeh/antennapod/adapter/NavListAdapter.java @@ -36,7 +36,8 @@ import de.danoeh.antennapod.fragment.QueueFragment; /** * BaseAdapter for the navigation drawer */ -public class NavListAdapter extends BaseAdapter { +public class NavListAdapter extends BaseAdapter + implements SharedPreferences.OnSharedPreferenceChangeListener { public static final int VIEW_TYPE_COUNT = 3; public static final int VIEW_TYPE_NAV = 0; public static final int VIEW_TYPE_SECTION_DIVIDER = 1; @@ -48,17 +49,6 @@ public class NavListAdapter extends BaseAdapter { private ItemAccess itemAccess; private Context context; - private SharedPreferences.OnSharedPreferenceChangeListener listener = - new SharedPreferences.OnSharedPreferenceChangeListener() { - @Override - public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { - if(key.equals(UserPreferences.PREF_HIDDEN_DRAWER_ITEMS)) { - loadItems(); - } - } - }; - - public NavListAdapter(ItemAccess itemAccess, Context context) { this.itemAccess = itemAccess; this.context = context; @@ -67,7 +57,13 @@ public class NavListAdapter extends BaseAdapter { loadItems(); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - prefs.registerOnSharedPreferenceChangeListener(listener); + prefs.registerOnSharedPreferenceChangeListener(this); + } + + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + if (key.equals(UserPreferences.PREF_HIDDEN_DRAWER_ITEMS)) { + loadItems(); + } } private void loadItems() { @@ -122,7 +118,7 @@ public class NavListAdapter extends BaseAdapter { @Override public int getCount() { - return tags.size() + itemAccess.getCount() + (tags.size() > 0 ? 1 : 0); + return getSubscriptionOffset() + itemAccess.getCount(); } @Override @@ -146,7 +142,7 @@ public class NavListAdapter extends BaseAdapter { public int getItemViewType(int position) { if (0 <= position && position < tags.size()) { return VIEW_TYPE_NAV; - } else if (position < tags.size() + 1) { + } else if (position < getSubscriptionOffset()) { return VIEW_TYPE_SECTION_DIVIDER; } else { return VIEW_TYPE_SUBSCRIPTION; @@ -159,7 +155,7 @@ public class NavListAdapter extends BaseAdapter { } public int getSubscriptionOffset() { - return tags.size() + 1; + return tags.size() > 0 ? tags.size() + 1 : 0; } diff --git a/app/src/main/res/layout/nav_list.xml b/app/src/main/res/layout/nav_list.xml index e366a65b2..2807cf15f 100644 --- a/app/src/main/res/layout/nav_list.xml +++ b/app/src/main/res/layout/nav_list.xml @@ -66,4 +66,12 @@ - \ No newline at end of file + + + diff --git a/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java b/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java index 34cc91e63..2c863d378 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java +++ b/core/src/main/java/de/danoeh/antennapod/core/preferences/UserPreferences.java @@ -546,12 +546,12 @@ public class UserPreferences implements public static void setHiddenDrawerItems(Context context, List items) { instanceAvailable(); + instance.hiddenDrawerItems = items; String str = StringUtils.join(items, ','); PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()) .edit() .putString(PREF_HIDDEN_DRAWER_ITEMS, str) .commit(); - instance.hiddenDrawerItems = items; }