Added device playlist to renderer fragment.

This commit is contained in:
Felix Ableitner 2013-06-08 15:19:38 +02:00
parent 43df4e21db
commit 2d38870dfe
7 changed files with 80 additions and 20 deletions

View file

@ -29,10 +29,21 @@
android:layout_height="wrap_content" > android:layout_height="wrap_content" >
<Button <Button
android:id="@+id/playpause" android:id="@+id/previous"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:onClick="playPauseClicked" /> android:text="@string/previous" />
<Button
android:id="@+id/playpause"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/next" />
</LinearLayout> </LinearLayout>

View file

@ -7,5 +7,7 @@
<string name="play">Play</string> <string name="play">Play</string>
<string name="pause">Pause</string> <string name="pause">Pause</string>
<string name="exit_renderer">Do you really want to exit the renderer? Playback will be stopped.</string> <string name="exit_renderer">Do you really want to exit the renderer? Playback will be stopped.</string>
<string name="previous">Previous</string>
<string name="next">Next</string>
</resources> </resources>

View file

@ -1,5 +1,7 @@
package com.github.nutomic.controldlna; package com.github.nutomic.controldlna;
import java.util.Collection;
import org.teleal.cling.model.meta.Device; import org.teleal.cling.model.meta.Device;
import org.teleal.cling.model.meta.LocalDevice; import org.teleal.cling.model.meta.LocalDevice;
import org.teleal.cling.model.meta.RemoteDevice; import org.teleal.cling.model.meta.RemoteDevice;
@ -121,4 +123,13 @@ public class DeviceArrayAdapter extends ArrayAdapter<Device<?, ?, ?>>
} }
}); });
} }
/**
* Not implemented on lower API levels.
*/
@Override
public void addAll(Collection<? extends Device<?, ?, ?>> collection) {
for (Device<?, ?, ?> d : collection)
add(d);
}
} }

View file

@ -1,5 +1,7 @@
package com.github.nutomic.controldlna; package com.github.nutomic.controldlna;
import java.util.Collection;
import org.teleal.cling.support.model.DIDLObject; import org.teleal.cling.support.model.DIDLObject;
import android.content.Context; import android.content.Context;
@ -27,4 +29,13 @@ public class FileArrayAdapter extends ArrayAdapter<DIDLObject> {
return convertView; return convertView;
} }
/**
* Not implemented on lower API levels.
*/
@Override
public void addAll(Collection<? extends DIDLObject> collection) {
for (DIDLObject d : collection)
add(d);
}
} }

View file

@ -1,5 +1,7 @@
package com.github.nutomic.controldlna; package com.github.nutomic.controldlna;
import java.util.List;
import org.teleal.cling.support.model.item.Item; import org.teleal.cling.support.model.item.Item;
import android.os.Bundle; import android.os.Bundle;
@ -138,7 +140,7 @@ public class MainActivity extends SherlockFragmentActivity implements
/** /**
* Utility function to call RendererFragment.play from ServerFragment. * Utility function to call RendererFragment.play from ServerFragment.
*/ */
public void play(Item[] playlist, int start) { public void play(List<Item> playlist, int start) {
getSupportActionBar().selectTab(getSupportActionBar().getTabAt(0)); getSupportActionBar().selectTab(getSupportActionBar().getTabAt(0));
mRendererFragment.setPlaylist(playlist, start); mRendererFragment.setPlaylist(playlist, start);
} }

View file

@ -1,5 +1,7 @@
package com.github.nutomic.controldlna; package com.github.nutomic.controldlna;
import java.util.Collection;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.teleal.cling.android.AndroidUpnpService; import org.teleal.cling.android.AndroidUpnpService;
@ -72,7 +74,9 @@ public class RendererFragment extends Fragment implements
private boolean mPlaying = false; private boolean mPlaying = false;
private Item[] mPlaylist; private int mCurrentTrack;
private List<Item> mPlaylist;
/** /**
* ListView adapter of media renderers. * ListView adapter of media renderers.
@ -103,14 +107,14 @@ public class RendererFragment extends Fragment implements
*/ */
private ServiceConnection mServiceConnection= new ServiceConnection() { private ServiceConnection mServiceConnection= new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) { @SuppressWarnings("unchecked")
public void onServiceConnected(ComponentName className, IBinder service) {
mUpnpService = (AndroidUpnpService) service; mUpnpService = (AndroidUpnpService) service;
Log.i(TAG, "Starting device search"); Log.i(TAG, "Starting device search");
mUpnpService.getRegistry().addListener(mRendererAdapter); mUpnpService.getRegistry().addListener(mRendererAdapter);
mUpnpService.getControlPoint().search(); mUpnpService.getControlPoint().search();
for (Device<?, ?, ?> d : mUpnpService mRendererAdapter.addAll((Collection<? extends Device<?, ?, ?>>)
.getControlPoint().getRegistry().getDevices()) mUpnpService.getControlPoint().getRegistry().getDevices());
mRendererAdapter.add(d);
} }
public void onServiceDisconnected(ComponentName className) { public void onServiceDisconnected(ComponentName className) {
@ -131,6 +135,7 @@ public class RendererFragment extends Fragment implements
public void onActivityCreated(Bundle savedInstanceState) { public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
mListView = (ListView) getView().findViewById(R.id.listview); mListView = (ListView) getView().findViewById(R.id.listview);
mPlaylistAdapter = new FileArrayAdapter(getActivity());
mRendererAdapter = new DeviceArrayAdapter( mRendererAdapter = new DeviceArrayAdapter(
getActivity(), DeviceArrayAdapter.RENDERER); getActivity(), DeviceArrayAdapter.RENDERER);
mListView.setAdapter(mRendererAdapter); mListView.setAdapter(mRendererAdapter);
@ -141,6 +146,8 @@ public class RendererFragment extends Fragment implements
mPlayPause = (Button) getView().findViewById(R.id.playpause); mPlayPause = (Button) getView().findViewById(R.id.playpause);
mPlayPause.setOnClickListener(this); mPlayPause.setOnClickListener(this);
mPlayPause.setText(R.string.play); mPlayPause.setText(R.string.play);
getView().findViewById(R.id.previous).setOnClickListener(this);
getView().findViewById(R.id.next).setOnClickListener(this);
getActivity().getApplicationContext().bindService( getActivity().getApplicationContext().bindService(
new Intent(getActivity(), AndroidUpnpServiceImpl.class), new Intent(getActivity(), AndroidUpnpServiceImpl.class),
@ -203,8 +210,10 @@ public class RendererFragment extends Fragment implements
/** /**
* Sets the new playlist and starts playing it (if a renderer is selected). * Sets the new playlist and starts playing it (if a renderer is selected).
*/ */
public void setPlaylist(Item[] playlist, int start) { public void setPlaylist(List<Item> playlist, int start) {
mPlaylist = playlist; mPlaylist = playlist;
mPlaylistAdapter.clear();
mPlaylistAdapter.addAll(playlist);
playTrack(start); playTrack(start);
} }
@ -215,11 +224,12 @@ public class RendererFragment extends Fragment implements
private void playTrack(int track) { private void playTrack(int track) {
if (mCurrentRenderer != null) { if (mCurrentRenderer != null) {
mListView.setAdapter(mPlaylistAdapter); mListView.setAdapter(mPlaylistAdapter);
mCurrentTrack = track;
final Service<?, ?> service = mCurrentRenderer.findService( final Service<?, ?> service = mCurrentRenderer.findService(
new ServiceType("schemas-upnp-org", "AVTransport")); new ServiceType("schemas-upnp-org", "AVTransport"));
DIDLParser parser = new DIDLParser(); DIDLParser parser = new DIDLParser();
DIDLContent didl = new DIDLContent(); DIDLContent didl = new DIDLContent();
didl.addItem(mPlaylist[track]); didl.addItem(mPlaylist.get(track));
String metadata; String metadata;
try { try {
metadata = parser.generate(didl, true); metadata = parser.generate(didl, true);
@ -229,7 +239,7 @@ public class RendererFragment extends Fragment implements
metadata = "NO METADATA"; metadata = "NO METADATA";
} }
mUpnpService.getControlPoint().execute(new SetAVTransportURI(service, mUpnpService.getControlPoint().execute(new SetAVTransportURI(service,
mPlaylist[track].getFirstResource().getValue(), metadata) { mPlaylist.get(track).getFirstResource().getValue(), metadata) {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
@Override @Override
public void failure(ActionInvocation invocation, public void failure(ActionInvocation invocation,
@ -337,6 +347,8 @@ public class RendererFragment extends Fragment implements
mListView.setAdapter(mPlaylistAdapter); mListView.setAdapter(mPlaylistAdapter);
mControls.setVisibility(View.VISIBLE); mControls.setVisibility(View.VISIBLE);
} }
else if (mListView.getAdapter() == mPlaylistAdapter)
playTrack(position);
} }
/** /**
@ -373,7 +385,7 @@ public class RendererFragment extends Fragment implements
mSubscriptionCallback.end(); mSubscriptionCallback.end();
mListView.setAdapter(mRendererAdapter); mListView.setAdapter(mRendererAdapter);
mControls.setVisibility(View.GONE); mControls.setVisibility(View.GONE);
} }
/** /**
@ -387,6 +399,15 @@ public class RendererFragment extends Fragment implements
pause(); pause();
else else
play(); play();
break;
case R.id.previous:
if (mCurrentTrack != 0 && !mPlaylist.isEmpty())
playTrack(--mCurrentTrack);
break;
case R.id.next:
if (mPlaylist.size() > mCurrentTrack + 1)
playTrack(++mCurrentTrack);
break;
} }
} }

View file

@ -1,5 +1,8 @@
package com.github.nutomic.controldlna; package com.github.nutomic.controldlna;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Stack; import java.util.Stack;
import org.teleal.cling.android.AndroidUpnpService; import org.teleal.cling.android.AndroidUpnpService;
@ -73,14 +76,14 @@ public class ServerFragment extends ListFragment implements OnBackPressedListene
*/ */
private ServiceConnection mServiceConnection= new ServiceConnection() { private ServiceConnection mServiceConnection= new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) { @SuppressWarnings("unchecked")
public void onServiceConnected(ComponentName className, IBinder service) {
mUpnpService = (AndroidUpnpService) service; mUpnpService = (AndroidUpnpService) service;
Log.i(TAG, "Starting device search"); Log.i(TAG, "Starting device search");
mUpnpService.getRegistry().addListener(mServerAdapter); mUpnpService.getRegistry().addListener(mServerAdapter);
mUpnpService.getControlPoint().search(); mUpnpService.getControlPoint().search();
for (Device<?, ?, ?> d : mUpnpService mServerAdapter.addAll((Collection<? extends Device<?, ?, ?>>)
.getControlPoint().getRegistry().getDevices()) mUpnpService.getControlPoint().getRegistry().getDevices());
mServerAdapter.add(d);
} }
public void onServiceDisconnected(ComponentName className) { public void onServiceDisconnected(ComponentName className) {
@ -133,11 +136,10 @@ public class ServerFragment extends ListFragment implements OnBackPressedListene
getFiles(((Container) mFileAdapter.getItem(position)).getId()); getFiles(((Container) mFileAdapter.getItem(position)).getId());
} }
else { else {
Item[] playlist = new Item[mFileAdapter.getCount()]; List<Item> playlist = new ArrayList<Item>();
for (int i = 0; i < mFileAdapter.getCount(); i++) { for (int i = 0; i < mFileAdapter.getCount(); i++) {
if (mFileAdapter.getItem(i) instanceof Item) { if (mFileAdapter.getItem(i) instanceof Item)
playlist[i] = (Item) mFileAdapter.getItem(position); playlist.add((Item) mFileAdapter.getItem(i));
}
} }
MainActivity activity = (MainActivity) getActivity(); MainActivity activity = (MainActivity) getActivity();
activity.play(playlist, position); activity.play(playlist, position);