1. DMCActivity
public class DMCActivity extends Activity {
private RegistryListener listener = new RegistryListener();
private ListView listView;
private ImageView noRemoteDeviceImage;
private TextView noRemoteDeviceText;
private MyAdapter adapter;
ProgressDialog progressDialog;
private LayoutInflater inflater;
DMCApplication mDmcApplication = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dlna_device_list);
mDmcApplication = (DMCApplication) getApplication();
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
listView = (ListView) findViewById(R.id.device_list);
adapter = new MyAdapter(mDmcApplication.arrayList, inflater);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long id) {
Intent intent = new Intent(DMCActivity.this,
ShowDirActivity.class);
intent.putExtra("position", position);
startActivity(intent);
}
});
noRemoteDeviceImage = (ImageView) findViewById(R.id.noitem_nodevice);
noRemoteDeviceText = (TextView) findViewById(R.id.emptytext);
init();
}
@Override
protected void onResume() {
Intent service = new Intent(DMCActivity.this,
AndroidUpnpServiceImpl.class);
this.bindService(service, connection, Context.BIND_AUTO_CREATE);
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onDestroy() {
if (mDmcApplication.upnpService != null) {
mDmcApplication.upnpService.getRegistry().removeListener(listener);
mDmcApplication.upnpService.getRegistry().shutdown();
mDmcApplication.upnpService = null;
}
this.unbindService(connection);
super.onDestroy();
}
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
System.out.println("is connected!");
mDmcApplication.upnpService = (AndroidUpnpService) service;
mDmcApplication.upnpService.getRegistry().addListener(listener);
mDmcApplication.upnpService.getControlPoint().search();
}
};
public class RegistryListener extends DefaultRegistryListener {
@Override
public void deviceAdded(Registry registry, Device device) {
deviceAddToList(device);
super.deviceAdded(registry, device);
}
@Override
public void deviceRemoved(Registry registry, Device device) {
deviceRemoveToList(device);
super.deviceRemoved(registry, device);
}
public void deviceAddToList(final Device device) {
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
if (!mDmcApplication.arrayList.contains(device)) {
mDmcApplication.arrayList.add(device);
}
adapter.notifyDataSetChanged();
} catch (Exception e) {
} finally {
progressDialog.cancel();
}
}
});
}
public void deviceRemoveToList(final Device device) {
runOnUiThread(new Runnable() {
public void run() {
if (mDmcApplication.arrayList.contains(device)) {
mDmcApplication.arrayList.remove(device);
}
adapter.notifyDataSetChanged();
}
});
}
}
public void init() {
progressDialog = new ProgressDialog(DMCActivity.this);
progressDialog.setProgressStyle(progressDialog.STYLE_SPINNER);
progressDialog.setIcon(R.drawable.search);
progressDialog.setTitle(R.string.device_search);
progressDialog.setMessage(getResources().getString(
R.string.progress_conent));
progressDialog.show();
noRemoteDeviceImage.setVisibility(View.GONE);
noRemoteDeviceText.setVisibility(View.GONE);
listView.setVisibility(View.VISIBLE);
}
}
2. DMCAppliaction
public class DMCApplication extends Application {
public AndroidUpnpService upnpService = null;
public ArrayList<Device> arrayList = new ArrayList<Device>();
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onTerminate() {
super.onTerminate();
}
}
3.MyAdapter
public class MyAdapter extends BaseAdapter {
private ArrayList<Device> arrayList;
private LayoutInflater inflater;
public MyAdapter(ArrayList<Device> arrayList, LayoutInflater inflater) {
this.arrayList = arrayList;
this.inflater = inflater;
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int position) {
return arrayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.main_item1, null);
holder = new ViewHolder();
holder.textViewNo = (TextView) convertView
.findViewById(R.id.show_number);
holder.textViewDevice = (TextView) convertView
.findViewById(R.id.device_name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textViewNo.setText(position + "");
holder.textViewDevice.setText(arrayList.get(position).getDetails()
.getFriendlyName().toString());
return convertView;
}
static class ViewHolder {
TextView textViewNo;
TextView textViewDevice;
}
}
4. showDirActivity
public class ShowDirActivity extends ListActivity {
private ArrayAdapter adapter;
private Service service;
private String containerID = "0";
private ArrayList<String> ID = new ArrayList<String>();
ProgressDialog progressDialog;
public List<Item> items;
private ArrayList<String> URLStr = new ArrayList<String>();
private String suffixLast;
DMCApplication mDmcApplication = null;
ArrayList<String> historyPath = new ArrayList<String>();
private boolean flag = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
adapter = new ArrayAdapter<String>(this, R.layout.show_dir_item1,
R.id.item_cotent);
getListView().setBackgroundDrawable(
getResources().getDrawable(R.drawable.dlna_bg));
getListView().setAdapter(adapter);
mDmcApplication = (DMCApplication) getApplication();
int position = getIntent().getIntExtra("position", 0);
service = mDmcApplication.arrayList.get(position).findService(
new ServiceId("upnp-org", "ContentDirectory"));
if (mDmcApplication.upnpService != null && service != null) {
executeBrowser(0);
}
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onDestroy() {
// service = null;
super.onDestroy();
}
public void executeBrowser(int position) {
adapter.clear();
progressDialog = new ProgressDialog(ShowDirActivity.this);
progressDialog.setProgressStyle(progressDialog.STYLE_SPINNER);
progressDialog.setTitle(getResources().getString(R.string.data_load));
progressDialog.setMessage(getResources().getString(
R.string.data_list_load));
progressDialog.setIcon(R.drawable.search);
progressDialog.setCancelable(true);
progressDialog.show();
if (flag) {
if (!ID.isEmpty()) {
containerID = ID.get(position);
ID.clear();
historyPath.add(containerID);
} else {
historyPath.add(containerID);
}
} else {
containerID = historyPath.get(position);
}
mDmcApplication.upnpService.getControlPoint().execute(
new Browse(service, containerID, BrowseFlag.DIRECT_CHILDREN) {
@Override
public void failure(ActionInvocation arg0,
UpnpResponse arg1, String arg2) {
}
@Override
public void updateStatus(Status status) {
switch (status) {
case OK:
runOnUiThread(new Runnable() {
public void run() {
progressDialog.cancel();
}
});
case LOADING:
runOnUiThread(new Runnable() {
public void run() {
}
});
break;
default:
break;
}
}
@Override
public void received(ActionInvocation invocation,
DIDLContent didlContent) {
List<Container> containers = didlContent
.getContainers();
for (int i = 0; i < containers.size(); i++) {
final String title = containers.get(i).getTitle();
ID.add(containers.get(i).getId());
runOnUiThread(new Runnable() {
public void run() {
adapter.add(title);
adapter.notifyDataSetChanged();
}
});
}
items = didlContent.getItems();
if (items.size() > 0) {
handler.sendEmptyMessage(1);
}
for (int i = 0; i < items.size(); i++) {
final String title = items.get(i).getTitle();
final String value = items.get(i)
.getFirstResource().getValue();
final String[] suffix = value.split("\\?")[0]
.split("\\.");
suffixLast = suffix[suffix.length - 1];
if (!(suffixLast.equals("jpeg")
|| suffixLast.equals("jpg")
|| suffixLast.equals("png")
|| suffixLast.equals("mp3")
|| suffixLast.equals("wmv")
|| suffixLast.equals("mp4")
|| suffixLast.equals("3gp") || suffixLast
.equals("wtv"))) {
suffixLast = "";
}
final String titleAll;
if (suffixLast.equals("")) {
titleAll = title;
} else {
titleAll = title + "." + suffixLast;
}
runOnUiThread(new Runnable() {
public void run() {
URLStr.add(value);
adapter.add(titleAll);
adapter.notifyDataSetChanged();
}
});
}
}
});
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
flag = true;
if (items.size() > 0) {
String urlstr = URLStr.get(position);
Item itemType = items.get(position);
if (MusicTrack.CLASS.equals(itemType)) {
Intent mp3Intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(urlstr));
mp3Intent.setDataAndType(Uri.parse(urlstr), "audio/*");
startActivity(mp3Intent);
} else if (Movie.CLASS.equals(itemType)
|| VideoItem.CLASS.equals(itemType)) {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(urlstr));
intent.addFlags(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
intent.setDataAndType(Uri.parse(urlstr), "video/*");
startActivity(intent);
} else if (Photo.CLASS.equals(itemType)) {
Intent pictureIntent = new Intent(ShowDirActivity.this,
ShowPictureActivity.class);
pictureIntent.putStringArrayListExtra("urlstr", URLStr);
pictureIntent.putExtra("position", position);
startActivity(pictureIntent);
}
} else {
executeBrowser(position);
}
super.onListItemClick(l, v, position, id);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
flag = false;
int index = historyPath.size();
if (index >= 1) {
historyPath.remove(historyPath.get(index - 1));
}
if (index >= 2) {
executeBrowser(index - 2);
} else {
flag = true;
this.finish();
}
if (!ID.isEmpty()) {
ID.clear();
}
return false;
};
Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
handler.hasMessages(1);
handler.removeMessages(1);
break;
default:
break;
}
};
};
}
5.showPictureAcitivty
public class ShowPictureActivity extends Activity {
private ImageView imageView;
private String url;
private Bitmap bitmap;
private ArrayList<String> URLStr;
private ProgressDialog progressDialog;
private ViewFlipper flipper;
private GestureDetector detector;
private ImageSwitcher switcher;
// ��ǰ�ļ����µ��ܵ�ͼƬ��
private int totalCount;
// ��ǰͼƬ��λ�á�
private int currentPosition;
// ��������ͼƬ
private ArrayList<Bitmap> pictureList = new ArrayList<Bitmap>();
// ��������URI
private ArrayList<String> uriList = new ArrayList<String>();
private float start;
private boolean firstFlag = false;
private Boolean flag = false;
// for test
private Button preButton;
private Button nexButton;
private MyAsyncTask task;
private Boolean preBoolean = false;
private Boolean nexBoolean = false;
private SoftReference<Bitmap> softBitmap;
private HashMap<String, Bitmap> map;
private static final int NETWORK_INTERREPTED = 0x1001;
private static final int SHOW_DIALOG = 0X1002;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.show_picture_ui);
init();
Intent intent = getIntent();
URLStr = intent.getStringArrayListExtra("urlstr");
totalCount = URLStr.size();
currentPosition = intent.getIntExtra("position", 0);
System.out.println(totalCount + "," + currentPosition);
url = URLStr.get(currentPosition);
switcher = (ImageSwitcher) findViewById(R.id.Switcher);
switcher.setFactory(new MyViewFactory());
new MyAsyncTask().execute(url);
switcher.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (softBitmap != null) {
if (softBitmap.get() != null
&& !softBitmap.get().isRecycled()) {
softBitmap.get().recycle();
softBitmap = null;
}
}
if (!firstFlag) {
start = event.getX();
firstFlag = true;
}
System.out.println("start--->" + start);
if (!flag) {
if (event.getX() - start > 50) {
flag = true;
progressDialog.show();
task = new MyAsyncTask();
if (currentPosition > 0) {
currentPosition--;
} else {
currentPosition = totalCount - 1;
}
url = URLStr.get(currentPosition);
task.execute(url);
} else if (event.getX() - start < -50) {
flag = true;
progressDialog.show();
task = new MyAsyncTask();
if (currentPosition < totalCount - 1) {
currentPosition++;
} else {
currentPosition = 0;
}
url = URLStr.get(currentPosition);
task.execute(url);
}
}
return true;
}
});
super.onCreate(savedInstanceState);
}
@SuppressLint("NewApi")
public class MyAsyncTask extends AsyncTask<String, Integer, Bitmap> {
@Override
protected Bitmap doInBackground(String... params) {
if (params[0] != null) {
if (!uriList.contains(params[0])) {
uriList.add(params[0]);
}
}
try {
HttpGet httpGet = new HttpGet(params[0]);
HttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream inputStream = httpEntity.getContent();
bitmap = BitmapFactory.decodeStream(inputStream);
softBitmap = new SoftReference<Bitmap>(bitmap);
return bitmap;
} catch (Exception e) {
Toast.makeText(ShowPictureActivity.this, "���������쳣",
Toast.LENGTH_SHORT).show();
handler.sendEmptyMessage(NETWORK_INTERREPTED);
return null;
}
}
@Override
protected void onPostExecute(Bitmap result) {
progressDialog.cancel();
nexBoolean = preBoolean = flag = false;
if (result != null) {
switcher.setImageDrawable(new BitmapDrawable(result));
}
}
}
public void init() {
progressDialog = new ProgressDialog(ShowPictureActivity.this);
progressDialog.setProgressStyle(progressDialog.STYLE_SPINNER);
progressDialog
.setTitle(getResources().getString(R.string.picture_load));
progressDialog.setMessage(getResources().getString(
R.string.picture_load_data));
progressDialog.setIcon(R.drawable.search);
progressDialog.setCancelable(true);
progressDialog.show();
}
public class MyViewFactory implements ViewFactory {
@Override
public View makeView() {
ImageView PictureView = new ImageView(ShowPictureActivity.this);
return PictureView;
}
}
Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case NETWORK_INTERREPTED:
showDialog(SHOW_DIALOG);
break;
default:
break;
}
};
};
protected Dialog onCreateDialog(int id) {
Dialog dialog = new AlertDialog.Builder(ShowPictureActivity.this)
.setIcon(R.drawable.dlna_connect_default_image_broken)
.setMessage(getResources().getString(R.string.network_iterrupt))
.setTitle(getResources().getString(R.string.dialog_title))
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create();
return dialog;
};
}
androidManifast:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:name=".DMCApplication"
android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".DMCActivity"
android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ShowDirActivity"
android:configChanges="orientation|keyboardHidden"></activity>
<activity android:name=".ShowPictureActivity"
android:configChanges="orientation|keyboardHidden"
></activity>
<service android:name="org.teleal.cling.android.AndroidUpnpServiceImpl"/>
</application>