Implemented Actionmode for FeedItemlistFragment
This commit is contained in:
parent
f9b0d1d318
commit
4cd84da5a3
Binary file not shown.
After Width: | Height: | Size: 1.4 KiB |
Binary file not shown.
After Width: | Height: | Size: 1.6 KiB |
|
@ -14,12 +14,12 @@
|
|||
android:layout_below="@id/txtvItemname"
|
||||
/>
|
||||
<Button
|
||||
android:id="@+id/butActions"
|
||||
android:id="@+id/butAction"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="match_parent"
|
||||
android:text="More"
|
||||
android:layout_alignParentRight="true"
|
||||
android:focusable="false"
|
||||
android:onClick="onButActionClicked"/>
|
||||
/>
|
||||
|
||||
</RelativeLayout>
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
|
||||
<item android:id="@+id/download_item" android:icon="@drawable/av_download" android:visible="false" android:title="@string/download_label"></item>
|
||||
<item android:id="@+id/remove_item" android:icon="@drawable/content_discard" android:title="@string/remove_label" android:visible="false"></item>
|
||||
<item android:id="@+id/play_item" android:title="@string/play_label" android:visible="false" android:icon="@drawable/av_play"></item>
|
||||
<item android:id="@+id/cancel_download_item" android:icon="@drawable/navigation_cancel" android:title="@string/cancel_download_label" android:visible="false"></item>
|
||||
|
||||
|
||||
</menu>
|
|
@ -8,6 +8,7 @@
|
|||
<string name="settings_label">Settings</string>
|
||||
<string name="add_new_feed_label">Add a new Feed</string>
|
||||
<string name="downloads_label">Downloads</string>
|
||||
<string name="cancel_download_label">Cancel Download</string>
|
||||
|
||||
<!-- -->
|
||||
<string name="confirm_label">Confirm</string>
|
||||
|
|
|
@ -44,8 +44,8 @@ public class FeedItemlistActivity extends SherlockFragmentActivity {
|
|||
fT.commit();
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
public void onButActionClicked(View v) {
|
||||
Log.d(TAG, "Button clicked");
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
|
|
@ -62,19 +62,7 @@ public class ItemviewActivity extends SherlockActivity {
|
|||
butPlay.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
// Start playback Service
|
||||
Intent launchIntent = new Intent(v.getContext(),
|
||||
PlaybackService.class);
|
||||
launchIntent.putExtra(PlaybackService.EXTRA_MEDIA_ID, item
|
||||
.getMedia().getId());
|
||||
launchIntent.putExtra(PlaybackService.EXTRA_FEED_ID, item
|
||||
.getFeed().getId());
|
||||
v.getContext().startService(launchIntent);
|
||||
|
||||
// Launch Mediaplayer
|
||||
Intent playerIntent = new Intent(v.getContext(),
|
||||
MediaplayerActivity.class);
|
||||
v.getContext().startActivity(playerIntent);
|
||||
manager.playMedia(v.getContext(), item.getMedia());
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -6,18 +6,21 @@ import de.podfetcher.feed.FeedItem;
|
|||
import de.podfetcher.util.Converter;
|
||||
import de.podfetcher.R;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.view.ViewGroup;
|
||||
import android.content.Context;
|
||||
|
||||
public class FeedItemlistAdapter extends ArrayAdapter<FeedItem> {
|
||||
|
||||
private OnClickListener onButActionClicked;
|
||||
|
||||
public FeedItemlistAdapter(Context context,
|
||||
int textViewResourceId, List<FeedItem> objects) {
|
||||
int textViewResourceId, List<FeedItem> objects, OnClickListener onButActionClicked) {
|
||||
super(context, textViewResourceId, objects);
|
||||
this.onButActionClicked = onButActionClicked;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -31,6 +34,7 @@ public class FeedItemlistAdapter extends ArrayAdapter<FeedItem> {
|
|||
convertView = inflater.inflate(R.layout.feeditemlist_item, null);
|
||||
holder.title = (TextView) convertView.findViewById(R.id.txtvItemname);
|
||||
holder.size = (TextView) convertView.findViewById(R.id.txtvItemsize);
|
||||
holder.butAction = (Button) convertView.findViewById(R.id.butAction);
|
||||
|
||||
convertView.setTag(holder);
|
||||
} else {
|
||||
|
@ -39,6 +43,7 @@ public class FeedItemlistAdapter extends ArrayAdapter<FeedItem> {
|
|||
|
||||
holder.title.setText(item.getTitle());
|
||||
holder.size.setText(Converter.byteToString(item.getMedia().getSize()));
|
||||
holder.butAction.setOnClickListener(onButActionClicked);
|
||||
return convertView;
|
||||
|
||||
}
|
||||
|
@ -46,5 +51,6 @@ public class FeedItemlistAdapter extends ArrayAdapter<FeedItem> {
|
|||
static class Holder {
|
||||
TextView title;
|
||||
TextView size;
|
||||
Button butAction;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,8 +4,11 @@ import java.io.File;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
|
||||
import de.podfetcher.activity.MediaplayerActivity;
|
||||
import de.podfetcher.service.PlaybackService;
|
||||
import de.podfetcher.storage.*;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.database.Cursor;
|
||||
import android.util.Log;
|
||||
|
||||
|
@ -37,6 +40,21 @@ public class FeedManager {
|
|||
return singleton;
|
||||
}
|
||||
|
||||
/** Play FeedMedia and start the playback service + launch Mediaplayer Activity. */
|
||||
public void playMedia(Context context, FeedMedia media) {
|
||||
// Start playback Service
|
||||
Intent launchIntent = new Intent(context,
|
||||
PlaybackService.class);
|
||||
launchIntent.putExtra(PlaybackService.EXTRA_MEDIA_ID, media.getId());
|
||||
launchIntent.putExtra(PlaybackService.EXTRA_FEED_ID, media.getItem().getFeed().getId());
|
||||
context.startService(launchIntent);
|
||||
|
||||
// Launch Mediaplayer
|
||||
Intent playerIntent = new Intent(context,
|
||||
MediaplayerActivity.class);
|
||||
context.startActivity(playerIntent);
|
||||
}
|
||||
|
||||
/** Remove media item that has been downloaded. */
|
||||
public boolean deleteFeedMedia(Context context, FeedMedia media) {
|
||||
boolean result = false;
|
||||
|
|
|
@ -6,14 +6,21 @@ import android.content.Intent;
|
|||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.widget.ListView;
|
||||
|
||||
import com.actionbarsherlock.app.SherlockListFragment;
|
||||
import com.actionbarsherlock.view.ActionMode;
|
||||
import com.actionbarsherlock.view.Menu;
|
||||
import com.actionbarsherlock.view.MenuInflater;
|
||||
import com.actionbarsherlock.view.MenuItem;
|
||||
|
||||
import de.podfetcher.R;
|
||||
import de.podfetcher.activity.ItemviewActivity;
|
||||
import de.podfetcher.adapter.FeedItemlistAdapter;
|
||||
import de.podfetcher.feed.FeedItem;
|
||||
import de.podfetcher.feed.FeedManager;
|
||||
import de.podfetcher.storage.DownloadRequester;
|
||||
|
||||
public class FeedItemlistFragment extends SherlockListFragment {
|
||||
private static final String TAG = "FeedItemlistFragment";
|
||||
|
@ -21,36 +28,126 @@ public class FeedItemlistFragment extends SherlockListFragment {
|
|||
|
||||
private FeedItemlistAdapter fila;
|
||||
private FeedManager manager;
|
||||
private DownloadRequester requester;
|
||||
|
||||
/** The feed which the activity displays */
|
||||
private ArrayList<FeedItem> items;
|
||||
|
||||
private FeedItem selectedItem;
|
||||
private ActionMode mActionMode;
|
||||
|
||||
public FeedItemlistFragment(ArrayList<FeedItem> items) {
|
||||
super();
|
||||
this.items = items;
|
||||
manager = FeedManager.getInstance();
|
||||
requester = DownloadRequester.getInstance();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
fila = new FeedItemlistAdapter(getActivity(), 0, items);
|
||||
fila = new FeedItemlistAdapter(getActivity(), 0, items,
|
||||
onButActionClicked);
|
||||
setListAdapter(fila);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
if (mActionMode != null) {
|
||||
mActionMode.finish();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onListItemClick(ListView l, View v, int position, long id) {
|
||||
FeedItem selection = fila.getItem(position);
|
||||
Intent showItem = new Intent(getActivity(), ItemviewActivity.class);
|
||||
showItem.putExtra(FeedlistFragment.EXTRA_SELECTED_FEED, selection.getFeed().getId());
|
||||
showItem.putExtra(FeedlistFragment.EXTRA_SELECTED_FEED, selection
|
||||
.getFeed().getId());
|
||||
showItem.putExtra(EXTRA_SELECTED_FEEDITEM, selection.getId());
|
||||
|
||||
startActivity(showItem);
|
||||
}
|
||||
|
||||
public void onButActionClicked(View v) {
|
||||
Log.d(TAG, "Button clicked");
|
||||
}
|
||||
private final OnClickListener onButActionClicked = new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
int index = getListView().getPositionForView(v);
|
||||
if (index != ListView.INVALID_POSITION) {
|
||||
FeedItem newSelectedItem = items.get(index);
|
||||
if (newSelectedItem != selectedItem) {
|
||||
if (mActionMode != null) {
|
||||
mActionMode.finish();
|
||||
}
|
||||
|
||||
selectedItem = newSelectedItem;
|
||||
mActionMode = getSherlockActivity().startActionMode(
|
||||
mActionModeCallback);
|
||||
} else {
|
||||
mActionMode.finish();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
|
||||
|
||||
@Override
|
||||
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyActionMode(ActionMode mode) {
|
||||
mActionMode = null;
|
||||
selectedItem = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
|
||||
MenuInflater inflater = mode.getMenuInflater();
|
||||
inflater.inflate(R.menu.feeditemlist, menu);
|
||||
mode.setTitle(selectedItem.getTitle());
|
||||
|
||||
if (selectedItem.getMedia().isDownloaded()) {
|
||||
menu.findItem(R.id.play_item).setVisible(true);
|
||||
menu.findItem(R.id.remove_item).setVisible(true);
|
||||
} else if (selectedItem.getMedia().getFile_url() == null) {
|
||||
menu.findItem(R.id.download_item).setVisible(true);
|
||||
} else {
|
||||
menu.findItem(R.id.cancel_download_item).setVisible(true);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case R.id.download_item:
|
||||
requester.downloadMedia(getSherlockActivity(),
|
||||
selectedItem.getMedia());
|
||||
break;
|
||||
case R.id.play_item:
|
||||
manager.playMedia(getSherlockActivity(),
|
||||
selectedItem.getMedia());
|
||||
break;
|
||||
case R.id.remove_item:
|
||||
manager.deleteFeedMedia(getSherlockActivity(),
|
||||
selectedItem.getMedia());
|
||||
break;
|
||||
case R.id.cancel_download_item:
|
||||
requester.cancelDownload(getSherlockActivity(), selectedItem
|
||||
.getMedia().getDownloadId());
|
||||
break;
|
||||
|
||||
}
|
||||
mode.finish();
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue