Moved RegistryListener into DeviceArrayAdapter.

This commit is contained in:
Felix Ableitner 2013-06-07 16:12:28 +02:00
parent 3ec4032105
commit 43df4e21db
3 changed files with 112 additions and 172 deletions

View file

@ -1,18 +1,47 @@
package com.github.nutomic.controldlna; package com.github.nutomic.controldlna;
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.RemoteDevice;
import org.teleal.cling.registry.Registry;
import org.teleal.cling.registry.RegistryListener;
import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.TextView; import android.widget.TextView;
public class DeviceArrayAdapter extends ArrayAdapter<Device<?, ?, ?>> { /**
* Displays the devices that are inserted through the RegistryListener (either
* of type RENDERER or SERVER).
*
* @author Felix
*
*/
public class DeviceArrayAdapter extends ArrayAdapter<Device<?, ?, ?>>
implements RegistryListener {
public DeviceArrayAdapter(Context context) { private static final String TAG = "DeviceArrayAdapter";
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 @Override
@ -27,4 +56,69 @@ public class DeviceArrayAdapter extends ArrayAdapter<Device<?, ?, ?>> {
return convertView; 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);
}
});
}
} }

View file

@ -10,13 +10,9 @@ import org.teleal.cling.model.gena.CancelReason;
import org.teleal.cling.model.gena.GENASubscription; import org.teleal.cling.model.gena.GENASubscription;
import org.teleal.cling.model.message.UpnpResponse; import org.teleal.cling.model.message.UpnpResponse;
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.RemoteDevice;
import org.teleal.cling.model.meta.Service; import org.teleal.cling.model.meta.Service;
import org.teleal.cling.model.state.StateVariableValue; import org.teleal.cling.model.state.StateVariableValue;
import org.teleal.cling.model.types.ServiceType; 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.GetPositionInfo;
import org.teleal.cling.support.avtransport.callback.Play; import org.teleal.cling.support.avtransport.callback.Play;
import org.teleal.cling.support.avtransport.callback.Seek; import org.teleal.cling.support.avtransport.callback.Seek;
@ -110,8 +106,11 @@ public class RendererFragment extends Fragment implements
public void onServiceConnected(ComponentName className, IBinder service) { 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(mRegistryListener); mUpnpService.getRegistry().addListener(mRendererAdapter);
mUpnpService.getControlPoint().search(); mUpnpService.getControlPoint().search();
for (Device<?, ?, ?> d : mUpnpService
.getControlPoint().getRegistry().getDevices())
mRendererAdapter.add(d);
} }
public void onServiceDisconnected(ComponentName className) { public void onServiceDisconnected(ComponentName className) {
@ -119,82 +118,6 @@ public class RendererFragment extends Fragment implements
} }
}; };
/**
* 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 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
@ -207,8 +130,9 @@ public class RendererFragment extends Fragment implements
@Override @Override
public void onActivityCreated(Bundle savedInstanceState) { public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
mRendererAdapter = new DeviceArrayAdapter(getActivity());
mListView = (ListView) getView().findViewById(R.id.listview); mListView = (ListView) getView().findViewById(R.id.listview);
mRendererAdapter = new DeviceArrayAdapter(
getActivity(), DeviceArrayAdapter.RENDERER);
mListView.setAdapter(mRendererAdapter); mListView.setAdapter(mRendererAdapter);
mListView.setOnItemClickListener(this); mListView.setOnItemClickListener(this);
mControls = getView().findViewById(R.id.controls); mControls = getView().findViewById(R.id.controls);
@ -272,7 +196,7 @@ public class RendererFragment extends Fragment implements
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
if (mUpnpService != null) if (mUpnpService != null)
mUpnpService.getRegistry().removeListener(mRegistryListener); mUpnpService.getRegistry().removeListener(mRendererAdapter);
getActivity().getApplicationContext().unbindService(mServiceConnection); getActivity().getApplicationContext().unbindService(mServiceConnection);
} }

View file

@ -7,12 +7,8 @@ import org.teleal.cling.android.AndroidUpnpServiceImpl;
import org.teleal.cling.model.action.ActionInvocation; import org.teleal.cling.model.action.ActionInvocation;
import org.teleal.cling.model.message.UpnpResponse; import org.teleal.cling.model.message.UpnpResponse;
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.RemoteDevice;
import org.teleal.cling.model.meta.Service; import org.teleal.cling.model.meta.Service;
import org.teleal.cling.model.types.ServiceType; 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.contentdirectory.callback.Browse;
import org.teleal.cling.support.model.BrowseFlag; import org.teleal.cling.support.model.BrowseFlag;
import org.teleal.cling.support.model.DIDLContent; import org.teleal.cling.support.model.DIDLContent;
@ -80,8 +76,11 @@ public class ServerFragment extends ListFragment implements OnBackPressedListene
public void onServiceConnected(ComponentName className, IBinder service) { 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(registryListener); mUpnpService.getRegistry().addListener(mServerAdapter);
mUpnpService.getControlPoint().search(); mUpnpService.getControlPoint().search();
for (Device<?, ?, ?> d : mUpnpService
.getControlPoint().getRegistry().getDevices())
mServerAdapter.add(d);
} }
public void onServiceDisconnected(ComponentName className) { public void onServiceDisconnected(ComponentName className) {
@ -89,93 +88,16 @@ public class ServerFragment extends ListFragment implements OnBackPressedListene
} }
}; };
/**
* 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. * Initializes ListView adapters, launches Cling UPNP service.
*/ */
@Override @Override
public void onActivityCreated(Bundle savedInstanceState) { public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
mServerAdapter = new DeviceArrayAdapter(getActivity());
mFileAdapter = new FileArrayAdapter(getActivity()); mFileAdapter = new FileArrayAdapter(getActivity());
mServerAdapter = new DeviceArrayAdapter(
getActivity(), DeviceArrayAdapter.SERVER);
setListAdapter(mServerAdapter); setListAdapter(mServerAdapter);
getActivity().getApplicationContext().bindService( getActivity().getApplicationContext().bindService(
@ -192,7 +114,7 @@ public class ServerFragment extends ListFragment implements OnBackPressedListene
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
if (mUpnpService != null) if (mUpnpService != null)
mUpnpService.getRegistry().removeListener(registryListener); mUpnpService.getRegistry().removeListener(mServerAdapter);
getActivity().getApplicationContext().unbindService(mServiceConnection); getActivity().getApplicationContext().unbindService(mServiceConnection);
} }