Merge branch 'TomHennen-move-to-top' into develop

This commit is contained in:
daniel oeh 2013-09-12 13:23:29 +02:00
commit 455b6c7172
5 changed files with 136 additions and 26 deletions

View File

@ -67,6 +67,5 @@
android:showAsAction="collapseActionView"
android:title="@string/support_label">
</item>
</menu>

View File

@ -15,6 +15,8 @@
<item name="organize_queue_item" type="id"/>
<item name="drag_handle" type="id"/>
<item name="skip_episode_item" type="id"/>
<item name="move_to_top_item" type="id"/>
<item name="move_to_bottom_item" type="id"/>
<item name="image_disk_cache_key" type="id"/>
<item name="imageloader_key" type="id"/>
<item name="notification_gpodnet_sync_error" type="id"/>

View File

@ -133,6 +133,8 @@
<string name="organize_queue_label">Organize queue</string>
<string name="undo">Undo</string>
<string name="removed_from_queue">Item removed</string>
<string name="move_to_top_label">Move to top</string>
<string name="move_to_bottom_label">Move to bottom</string>
<!-- Flattr -->
<string name="flattr_auth_label">Flattr sign-in</string>

View File

@ -222,6 +222,12 @@ public class EpisodesFragment extends Fragment {
}
}, selectedItem, false, QueueAccess.ItemListAccess(queue));
// check to see if the item is in the queue, if so add queue menu items
int itemIndex = queue.indexOf(selectedItem);
if (itemIndex != -1) {
addQueueOnlyMenus(menu, itemIndex);
}
} else if (selectedGroupId == ExternalEpisodesListAdapter.GROUP_POS_QUEUE) {
menu.add(Menu.NONE, R.id.organize_queue_item, Menu.NONE,
R.string.organize_queue_label);
@ -237,6 +243,24 @@ public class EpisodesFragment extends Fragment {
}
}
/**
* Adds submenus to the ContextMenu if the item selected is in the queue.
* @param menu the ContextMenu to add the submenus to
* @param itemIndex the index of the selected item within the queue.
*/
private void addQueueOnlyMenus(ContextMenu menu, int itemIndex) {
if (itemIndex != 0) {
// don't add move to top if this item is already on the top
menu.add(Menu.NONE, R.id.move_to_top_item, Menu.NONE, getActivity()
.getString(R.string.move_to_top_label));
}
if (itemIndex != queue.size() - 1) {
// don't add move to bottom if this item is already on the bottom
menu.add(Menu.NONE, R.id.move_to_bottom_item, Menu.NONE, getActivity()
.getString(R.string.move_to_bottom_label));
}
}
@Override
public boolean onContextItemSelected(android.view.MenuItem item) {
boolean handled = false;
@ -249,7 +273,19 @@ public class EpisodesFragment extends Fragment {
DownloadRequestErrorDialogCreator.newRequestErrorDialog(
getActivity(), e.getMessage());
}
if (!handled) {
// if it wasn't handled by the FeedItemMenuHandler it might be one of ours
switch (item.getItemId()) {
case R.id.move_to_top_item:
DBWriter.moveQueueItemToTop(getActivity(), selectedItem.getId(), true);
handled = true;
break;
case R.id.move_to_bottom_item:
DBWriter.moveQueueItemToBottom(getActivity(), selectedItem.getId(), true);
handled = true;
break;
}
}
} else if (selectedGroupId == ExternalEpisodesListAdapter.GROUP_POS_QUEUE) {
handled = true;
switch (item.getItemId()) {

View File

@ -466,7 +466,61 @@ public class DBWriter {
});
}
/**
* Moves the specified item to the top of the queue.
*
* @param context A context that is used for opening a database connection.
* @param itemId The item to move to the top of the queue
* @param broadcastUpdate true if this operation should trigger a QueueUpdateBroadcast. This option should be set to
* false if the caller wants to avoid unexpected updates of the GUI.
*/
public static Future<?> moveQueueItemToTop(final Context context, final long itemId, final boolean broadcastUpdate) {
return dbExec.submit(new Runnable() {
@Override
public void run() {
List<Long> queueIdList = DBReader.getQueueIDList(context);
int currentLocation = 0;
for (long id : queueIdList) {
if (id == itemId) {
moveQueueItemHelper(context, currentLocation, 0, broadcastUpdate);
return;
}
currentLocation++;
}
Log.e(TAG, "moveQueueItemToTop: item not found");
}
});
}
/**
* Moves the specified item to the bottom of the queue.
*
* @param context A context that is used for opening a database connection.
* @param itemId The item to move to the bottom of the queue
* @param broadcastUpdate true if this operation should trigger a QueueUpdateBroadcast. This option should be set to
* false if the caller wants to avoid unexpected updates of the GUI.
*/
public static Future<?> moveQueueItemToBottom(final Context context, final long itemId,
final boolean broadcastUpdate) {
return dbExec.submit(new Runnable() {
@Override
public void run() {
List<Long> queueIdList = DBReader.getQueueIDList(context);
int currentLocation = 0;
for (long id : queueIdList) {
if (id == itemId) {
moveQueueItemHelper(context, currentLocation, queueIdList.size() - 1,
broadcastUpdate);
return;
}
currentLocation++;
}
Log.e(TAG, "moveQueueItemToBottom: item not found");
}
});
}
/**
* Changes the position of a FeedItem in the queue.
*
@ -483,33 +537,50 @@ public class DBWriter {
@Override
public void run() {
final PodDBAdapter adapter = new PodDBAdapter(context);
adapter.open();
final List<FeedItem> queue = DBReader
.getQueue(context, adapter);
if (queue != null) {
if (from >= 0 && from < queue.size() && to >= 0
&& to < queue.size()) {
final FeedItem item = queue.remove(from);
queue.add(to, item);
adapter.setQueue(queue);
if (broadcastUpdate) {
EventDistributor.getInstance()
.sendQueueUpdateBroadcast();
}
}
} else {
Log.e(TAG, "moveQueueItem: Could not load queue");
}
adapter.close();
moveQueueItemHelper(context, from, to, broadcastUpdate);
}
});
}
/**
* Changes the position of a FeedItem in the queue.
*
* This function must be run using the ExecutorService (dbExec).
*
* @param context A context that is used for opening a database connection.
* @param from Source index. Must be in range 0..queue.size()-1.
* @param to Destination index. Must be in range 0..queue.size()-1.
* @param broadcastUpdate true if this operation should trigger a QueueUpdateBroadcast. This option should be set to
* false if the caller wants to avoid unexpected updates of the GUI.
* @throws IndexOutOfBoundsException if (to < 0 || to >= queue.size()) || (from < 0 || from >= queue.size())
*/
private static void moveQueueItemHelper(final Context context, final int from,
final int to, final boolean broadcastUpdate) {
final PodDBAdapter adapter = new PodDBAdapter(context);
adapter.open();
final List<FeedItem> queue = DBReader
.getQueue(context, adapter);
if (queue != null) {
if (from >= 0 && from < queue.size() && to >= 0
&& to < queue.size()) {
final FeedItem item = queue.remove(from);
queue.add(to, item);
adapter.setQueue(queue);
if (broadcastUpdate) {
EventDistributor.getInstance()
.sendQueueUpdateBroadcast();
}
}
} else {
Log.e(TAG, "moveQueueItemHelper: Could not load queue");
}
adapter.close();
}
/**
* Sets the 'read'-attribute of a FeedItem to the specified value.
*