From 103fb635d16f270796adde0e6f3d212a671b7214 Mon Sep 17 00:00:00 2001 From: Martin Fietz Date: Sat, 2 May 2015 21:04:30 +0200 Subject: [PATCH] Add ActionBar icon to enable/disable list drag (reorder, swipe) --- .../antennapod/adapter/QueueListAdapter.java | 18 +++++++++++++- .../antennapod/fragment/QueueFragment.java | 24 ++++++++++++++++++- .../antennapod/menuhandler/MenuItemUtils.java | 16 +++++++++++++ app/src/main/res/layout/queue_listitem.xml | 6 +++-- app/src/main/res/menu/queue.xml | 6 +++++ .../core/preferences/UserPreferences.java | 19 +++++++++++++++ core/src/main/res/values/strings.xml | 2 ++ 7 files changed, 87 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/de/danoeh/antennapod/adapter/QueueListAdapter.java b/app/src/main/java/de/danoeh/antennapod/adapter/QueueListAdapter.java index a256dc129..bba5a00a9 100644 --- a/app/src/main/java/de/danoeh/antennapod/adapter/QueueListAdapter.java +++ b/app/src/main/java/de/danoeh/antennapod/adapter/QueueListAdapter.java @@ -14,9 +14,9 @@ import android.widget.TextView; import com.squareup.picasso.Picasso; import de.danoeh.antennapod.R; -import de.danoeh.antennapod.core.feed.EventDistributor; import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.feed.FeedMedia; +import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.storage.DownloadRequester; import de.danoeh.antennapod.core.util.Converter; @@ -31,6 +31,8 @@ public class QueueListAdapter extends BaseAdapter { private final ActionButtonCallback actionButtonCallback; private final ActionButtonUtils actionButtonUtils; + private boolean locked; + public QueueListAdapter(Context context, ItemAccess itemAccess, ActionButtonCallback actionButtonCallback) { super(); @@ -38,6 +40,12 @@ public class QueueListAdapter extends BaseAdapter { this.itemAccess = itemAccess; this.actionButtonUtils = new ActionButtonUtils(context); this.actionButtonCallback = actionButtonCallback; + locked = UserPreferences.isQueueLocked(); + } + + public void setLocked(boolean locked) { + this.locked = locked; + notifyDataSetChanged(); } @Override @@ -67,6 +75,7 @@ public class QueueListAdapter extends BaseAdapter { .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.queue_listitem, parent, false); + holder.dragHandle = (ImageView) convertView.findViewById(R.id.drag_handle); holder.imageView = (ImageView) convertView.findViewById(R.id.imgvImage); holder.title = (TextView) convertView.findViewById(R.id.txtvTitle); holder.pubDate = (TextView) convertView.findViewById(R.id.txtvPubDate); @@ -83,6 +92,12 @@ public class QueueListAdapter extends BaseAdapter { holder = (Holder) convertView.getTag(); } + if(locked) { + holder.dragHandle.setVisibility(View.GONE); + } else { + holder.dragHandle.setVisibility(View.VISIBLE); + } + holder.title.setText(item.getTitle()); FeedMedia media = item.getMedia(); @@ -143,6 +158,7 @@ public class QueueListAdapter extends BaseAdapter { static class Holder { + ImageView dragHandle; ImageView imageView; TextView title; TextView pubDate; diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java index f1ed6c656..6f81177db 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/QueueFragment.java @@ -4,6 +4,7 @@ import android.app.Activity; import android.content.Context; import android.content.DialogInterface; import android.content.SharedPreferences; +import android.content.res.TypedArray; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; @@ -48,7 +49,6 @@ import de.danoeh.antennapod.core.util.QueueSorter; import de.danoeh.antennapod.core.util.gui.FeedItemUndoToken; import de.danoeh.antennapod.core.util.gui.UndoBarController; import de.danoeh.antennapod.menuhandler.MenuItemUtils; -import de.danoeh.antennapod.menuhandler.NavDrawerActivity; import de.greenrobot.event.EventBus; /** @@ -67,6 +67,8 @@ public class QueueFragment extends Fragment { private TextView txtvEmpty; private ProgressBar progLoading; + private MenuItem queueLock; + private UndoBarController undoBarController; private List queue; @@ -221,6 +223,9 @@ public class QueueFragment extends Fragment { return false; } }); + + MenuItemUtils.refreshLockItem(getActivity(), menu, queueLock); + isUpdatingFeeds = MenuItemUtils.updateRefreshMenuItem(menu, R.id.refresh_item, updateRefreshMenuItemChecker); } } @@ -229,6 +234,17 @@ public class QueueFragment extends Fragment { public boolean onOptionsItemSelected(MenuItem item) { if (!super.onOptionsItemSelected(item)) { switch (item.getItemId()) { + case R.id.queue_lock: + boolean locked = !UserPreferences.isQueueLocked(); + if(locked) { + listView.setDragEnabled(false); + } else { + listView.setDragEnabled(true); + } + UserPreferences.setQueueLocked(locked); + getActivity().supportInvalidateOptionsMenu(); + listAdapter.setLocked(locked); + return true; case R.id.refresh_item: List feeds = ((MainActivity) getActivity()).getFeeds(); if (feeds != null) { @@ -330,6 +346,12 @@ public class QueueFragment extends Fragment { progLoading = (ProgressBar) root.findViewById(R.id.progLoading); listView.setEmptyView(txtvEmpty); + if(UserPreferences.isQueueLocked()) { + listView.setDragEnabled(false); + } else { + listView.setDragEnabled(true); + } + listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { diff --git a/app/src/main/java/de/danoeh/antennapod/menuhandler/MenuItemUtils.java b/app/src/main/java/de/danoeh/antennapod/menuhandler/MenuItemUtils.java index b78bfd01a..b2e3f8968 100644 --- a/app/src/main/java/de/danoeh/antennapod/menuhandler/MenuItemUtils.java +++ b/app/src/main/java/de/danoeh/antennapod/menuhandler/MenuItemUtils.java @@ -1,11 +1,14 @@ package de.danoeh.antennapod.menuhandler; +import android.content.Context; +import android.content.res.TypedArray; import android.support.v4.view.MenuItemCompat; import android.support.v7.widget.SearchView; import android.view.Menu; import android.view.MenuItem; import de.danoeh.antennapod.core.R; +import de.danoeh.antennapod.core.preferences.UserPreferences; /** * Utilities for menu items @@ -19,4 +22,17 @@ public class MenuItemUtils extends de.danoeh.antennapod.core.menuhandler.MenuIte return item; } + public static void refreshLockItem(Context context, Menu menu, MenuItem queueLock) { + queueLock = menu.findItem(de.danoeh.antennapod.R.id.queue_lock); + int[] lockIcons = new int[] { de.danoeh.antennapod.R.attr.ic_lock_open, de.danoeh.antennapod.R.attr.ic_lock_closed }; + TypedArray ta = context.obtainStyledAttributes(lockIcons); + if (UserPreferences.isQueueLocked()) { + queueLock.setTitle(de.danoeh.antennapod.R.string.unlock_queue); + queueLock.setIcon(ta.getDrawable(1)); + } else { + queueLock.setTitle(de.danoeh.antennapod.R.string.lock_queue); + queueLock.setIcon(ta.getDrawable(0)); + } + } + } diff --git a/app/src/main/res/layout/queue_listitem.xml b/app/src/main/res/layout/queue_listitem.xml index bc5b951a2..39e9c72a5 100644 --- a/app/src/main/res/layout/queue_listitem.xml +++ b/app/src/main/res/layout/queue_listitem.xml @@ -1,17 +1,19 @@ - + + hiddenDrawerItems; + private boolean queueLocked; private UserPreferences(Context context) { this.context = context; @@ -172,6 +174,7 @@ public class UserPreferences implements } persistNotify = sp.getBoolean(PREF_PERSISTENT_NOTIFICATION, false); hiddenDrawerItems = Arrays.asList(StringUtils.split(sp.getString(PREF_HIDDEN_DRAWER_ITEMS, ""), ',')); + queueLocked = sp.getBoolean(PREF_QUEUE_LOCKED, false); } private int readThemeValue(String valueFromPrefs) { @@ -395,6 +398,11 @@ public class UserPreferences implements return instance.isFreshInstall; } + public static boolean isQueueLocked() { + instanceAvailable(); + return instance.queueLocked; + } + @Override public void onSharedPreferenceChanged(SharedPreferences sp, String key) { Log.d(TAG, "Registered change of user preferences. Key: " + key); @@ -468,6 +476,8 @@ public class UserPreferences implements persistNotify = sp.getBoolean(PREF_PERSISTENT_NOTIFICATION, false); } else if (key.equals(PREF_HIDDEN_DRAWER_ITEMS)) { hiddenDrawerItems = Arrays.asList(StringUtils.split(sp.getString(PREF_HIDDEN_DRAWER_ITEMS, ""), ',')); + } else if(key.equals(PREF_QUEUE_LOCKED)) { + queueLocked = sp.getBoolean(PREF_QUEUE_LOCKED, false); } } @@ -554,6 +564,15 @@ public class UserPreferences implements .commit(); } + public static void setQueueLocked(boolean locked) { + instanceAvailable(); + instance.queueLocked = locked; + PreferenceManager.getDefaultSharedPreferences(instance.context) + .edit() + .putBoolean(PREF_QUEUE_LOCKED, locked) + .commit(); + } + /** * Return the folder where the app stores all of its data. This method will diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index c0c2f06de..3421d3c52 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -161,6 +161,8 @@ AntennaPod - Unknown media key: %1$d + Lock queue + Unlock queue Clear queue Undo Item removed