diff --git a/src/com/github/nutomic/controldlna/DeviceArrayAdapter.java b/src/com/github/nutomic/controldlna/DeviceArrayAdapter.java index d19b80f..754ef03 100644 --- a/src/com/github/nutomic/controldlna/DeviceArrayAdapter.java +++ b/src/com/github/nutomic/controldlna/DeviceArrayAdapter.java @@ -1,18 +1,47 @@ package com.github.nutomic.controldlna; import org.teleal.cling.model.meta.Device; +import org.teleal.cling.model.meta.LocalDevice; +import org.teleal.cling.model.meta.RemoteDevice; +import org.teleal.cling.registry.Registry; +import org.teleal.cling.registry.RegistryListener; +import android.app.Activity; import android.content.Context; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; -public class DeviceArrayAdapter extends ArrayAdapter> { +/** + * Displays the devices that are inserted through the RegistryListener (either + * of type RENDERER or SERVER). + * + * @author Felix + * + */ +public class DeviceArrayAdapter extends ArrayAdapter> + implements RegistryListener { + + private static final String TAG = "DeviceArrayAdapter"; - public DeviceArrayAdapter(Context context) { - super(context, android.R.layout.simple_list_item_1); + public static final String RENDERER = "MediaRenderer"; + + public static final String SERVER = "MediaServer"; + + private Activity mActivity; + + private String mDeviceType; + + /** + * @param deviceType One of RENDERER or SERVER. + */ + public DeviceArrayAdapter(Activity activity, String deviceType) { + super(activity, android.R.layout.simple_list_item_1); + mActivity = activity; + mDeviceType = deviceType; } @Override @@ -27,4 +56,69 @@ public class DeviceArrayAdapter extends ArrayAdapter> { return convertView; } + @Override + public void afterShutdown() { + } + + @Override + public void beforeShutdown(Registry registry) { + } + + @Override + public void localDeviceAdded(Registry registry, LocalDevice device) { + deviceAdded(device); + } + + @Override + public void localDeviceRemoved(Registry registry, LocalDevice device) { + deviceRemoved(device); + } + + @Override + public void remoteDeviceAdded(Registry registry, RemoteDevice device) { + deviceAdded(device); + } + + @Override + public void remoteDeviceDiscoveryFailed(Registry registry, + RemoteDevice device, Exception exception) { + Log.w(TAG, "Device discovery failed", exception); + } + + @Override + public void remoteDeviceDiscoveryStarted(Registry registry, + RemoteDevice device) { + } + + @Override + public void remoteDeviceRemoved(Registry registry, RemoteDevice device) { + deviceRemoved(device); + } + + @Override + public void remoteDeviceUpdated(Registry registry, RemoteDevice device) { + deviceRemoved(device); + deviceAdded(device); + } + + private void deviceAdded(final Device device) { + mActivity.runOnUiThread(new Runnable() { + + @Override + public void run() { + if (device.getType().getType().equals(mDeviceType)) + add(device); + } + }); + } + + private void deviceRemoved(final Device device) { + mActivity.runOnUiThread(new Runnable() { + + @Override + public void run() { + remove(device); + } + }); + } } diff --git a/src/com/github/nutomic/controldlna/RendererFragment.java b/src/com/github/nutomic/controldlna/RendererFragment.java index 650a5b8..95c179c 100644 --- a/src/com/github/nutomic/controldlna/RendererFragment.java +++ b/src/com/github/nutomic/controldlna/RendererFragment.java @@ -10,13 +10,9 @@ import org.teleal.cling.model.gena.CancelReason; import org.teleal.cling.model.gena.GENASubscription; import org.teleal.cling.model.message.UpnpResponse; import org.teleal.cling.model.meta.Device; -import org.teleal.cling.model.meta.LocalDevice; -import org.teleal.cling.model.meta.RemoteDevice; import org.teleal.cling.model.meta.Service; import org.teleal.cling.model.state.StateVariableValue; import org.teleal.cling.model.types.ServiceType; -import org.teleal.cling.registry.Registry; -import org.teleal.cling.registry.RegistryListener; import org.teleal.cling.support.avtransport.callback.GetPositionInfo; import org.teleal.cling.support.avtransport.callback.Play; import org.teleal.cling.support.avtransport.callback.Seek; @@ -110,90 +106,17 @@ public class RendererFragment extends Fragment implements public void onServiceConnected(ComponentName className, IBinder service) { mUpnpService = (AndroidUpnpService) service; Log.i(TAG, "Starting device search"); - mUpnpService.getRegistry().addListener(mRegistryListener); + mUpnpService.getRegistry().addListener(mRendererAdapter); mUpnpService.getControlPoint().search(); + for (Device d : mUpnpService + .getControlPoint().getRegistry().getDevices()) + mRendererAdapter.add(d); } public void onServiceDisconnected(ComponentName className) { mUpnpService = null; } }; - - /** - * Receives updates when devices are added or removed. - */ - private RegistryListener mRegistryListener = new RegistryListener() { - - @Override - public void remoteDeviceUpdated(Registry registry, RemoteDevice device) { - } - - @Override - public void remoteDeviceRemoved(Registry registry, RemoteDevice device) { - remove(device); - } - - @Override - public void remoteDeviceDiscoveryStarted(Registry registry, - RemoteDevice device) { - } - - @Override - public void remoteDeviceDiscoveryFailed(Registry registry, - RemoteDevice device, Exception exception) { - Log.w(TAG, "Device discovery failed" + exception.getMessage()); - } - - @Override - public void remoteDeviceAdded(Registry registry, RemoteDevice device) { - add(device); - } - - @Override - public void localDeviceRemoved(Registry registry, LocalDevice device) { - remove(device); - } - - @Override - public void localDeviceAdded(Registry registry, LocalDevice device) { - add(device); - } - - @Override - public void beforeShutdown(Registry registry) { - } - - @Override - public void afterShutdown() { - } - - /** - * Add a device to the ListView. - */ - private void add(final Device device) { - getActivity().runOnUiThread(new Runnable() { - - @Override - public void run() { - if (device.getType().getType().equals("MediaRenderer")) - mRendererAdapter.add(device); - } - }); - } - - /** - * Remove a device from the ListView. - */ - private void remove(final Device device) { - getActivity().runOnUiThread(new Runnable() { - - @Override - public void run() { - mRendererAdapter.remove(device); - } - }); - } - }; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, @@ -207,8 +130,9 @@ public class RendererFragment extends Fragment implements @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); - mRendererAdapter = new DeviceArrayAdapter(getActivity()); mListView = (ListView) getView().findViewById(R.id.listview); + mRendererAdapter = new DeviceArrayAdapter( + getActivity(), DeviceArrayAdapter.RENDERER); mListView.setAdapter(mRendererAdapter); mListView.setOnItemClickListener(this); mControls = getView().findViewById(R.id.controls); @@ -272,7 +196,7 @@ public class RendererFragment extends Fragment implements public void onDestroy() { super.onDestroy(); if (mUpnpService != null) - mUpnpService.getRegistry().removeListener(mRegistryListener); + mUpnpService.getRegistry().removeListener(mRendererAdapter); getActivity().getApplicationContext().unbindService(mServiceConnection); } diff --git a/src/com/github/nutomic/controldlna/ServerFragment.java b/src/com/github/nutomic/controldlna/ServerFragment.java index 1d3c202..4d13df9 100644 --- a/src/com/github/nutomic/controldlna/ServerFragment.java +++ b/src/com/github/nutomic/controldlna/ServerFragment.java @@ -7,12 +7,8 @@ import org.teleal.cling.android.AndroidUpnpServiceImpl; import org.teleal.cling.model.action.ActionInvocation; import org.teleal.cling.model.message.UpnpResponse; import org.teleal.cling.model.meta.Device; -import org.teleal.cling.model.meta.LocalDevice; -import org.teleal.cling.model.meta.RemoteDevice; import org.teleal.cling.model.meta.Service; import org.teleal.cling.model.types.ServiceType; -import org.teleal.cling.registry.Registry; -import org.teleal.cling.registry.RegistryListener; import org.teleal.cling.support.contentdirectory.callback.Browse; import org.teleal.cling.support.model.BrowseFlag; import org.teleal.cling.support.model.DIDLContent; @@ -80,92 +76,17 @@ public class ServerFragment extends ListFragment implements OnBackPressedListene public void onServiceConnected(ComponentName className, IBinder service) { mUpnpService = (AndroidUpnpService) service; Log.i(TAG, "Starting device search"); - mUpnpService.getRegistry().addListener(registryListener); + mUpnpService.getRegistry().addListener(mServerAdapter); mUpnpService.getControlPoint().search(); + for (Device d : mUpnpService + .getControlPoint().getRegistry().getDevices()) + mServerAdapter.add(d); } public void onServiceDisconnected(ComponentName className) { mUpnpService = null; } }; - - /** - * Receives updates when devices are added or removed. - */ - private RegistryListener registryListener = new RegistryListener() { - - @Override - public void remoteDeviceUpdated(Registry registry, RemoteDevice device) { - if (device == mCurrentServer) - getFiles(); - } - - @Override - public void remoteDeviceRemoved(Registry registry, RemoteDevice device) { - remove(device); - } - - @Override - public void remoteDeviceDiscoveryStarted(Registry registry, - RemoteDevice device) { - } - - @Override - public void remoteDeviceDiscoveryFailed(Registry registry, - RemoteDevice device, Exception exception) { - Log.w(TAG, "Device discovery failed" + exception.getMessage()); - } - - @Override - public void remoteDeviceAdded(Registry registry, RemoteDevice device) { - add(device); - } - - @Override - public void localDeviceRemoved(Registry registry, LocalDevice device) { - remove(device); - } - - @Override - public void localDeviceAdded(Registry registry, LocalDevice device) { - add(device); - } - - @Override - public void beforeShutdown(Registry registry) { - } - - @Override - public void afterShutdown() { - } - - /** - * Add a device to the ListView. - */ - private void add(final Device device) { - getActivity().runOnUiThread(new Runnable() { - - @Override - public void run() { - if (device.getType().getType().equals("MediaServer")) - mServerAdapter.add(device); - } - }); - } - - /** - * Remove a device from the ListView. - */ - private void remove(final Device device) { - getActivity().runOnUiThread(new Runnable() { - - @Override - public void run() { - mServerAdapter.remove(device); - } - }); - } - }; /** * Initializes ListView adapters, launches Cling UPNP service. @@ -173,9 +94,10 @@ public class ServerFragment extends ListFragment implements OnBackPressedListene @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); - mServerAdapter = new DeviceArrayAdapter(getActivity()); mFileAdapter = new FileArrayAdapter(getActivity()); - + + mServerAdapter = new DeviceArrayAdapter( + getActivity(), DeviceArrayAdapter.SERVER); setListAdapter(mServerAdapter); getActivity().getApplicationContext().bindService( @@ -192,7 +114,7 @@ public class ServerFragment extends ListFragment implements OnBackPressedListene public void onDestroy() { super.onDestroy(); if (mUpnpService != null) - mUpnpService.getRegistry().removeListener(registryListener); + mUpnpService.getRegistry().removeListener(mServerAdapter); getActivity().getApplicationContext().unbindService(mServiceConnection); }