From 20bc97fc541610a296d12e25570ac3eb6d599673 Mon Sep 17 00:00:00 2001 From: Mike Lockwood Date: Thu, 6 Jan 2011 18:17:48 -0500 Subject: [PATCH] CameraBrowser: new implementation based on new MTP host APIs Change-Id: I81300e3218d6f6fce87432ce506fc282aa4dc4b8 Signed-off-by: Mike Lockwood --- media/tests/CameraBrowser/Android.mk | 10 + media/tests/CameraBrowser/AndroidManifest.xml | 31 +++ .../CameraBrowser/res/layout/object_info.xml | 169 +++++++++++++++ .../CameraBrowser/res/layout/object_list.xml | 33 +++ .../CameraBrowser/res/values/strings.xml | 46 ++++ .../tests/CameraBrowser/res/values/styles.xml | 34 +++ .../android/camerabrowser/CameraBrowser.java | 139 ++++++++++++ .../CameraBrowserApplication.java | 41 ++++ .../DeviceDisconnectedReceiver.java | 52 +++++ .../android/camerabrowser/ObjectBrowser.java | 147 +++++++++++++ .../android/camerabrowser/ObjectViewer.java | 203 ++++++++++++++++++ .../android/camerabrowser/StorageBrowser.java | 121 +++++++++++ .../android/camerabrowser/UsbReceiver.java | 48 +++++ 13 files changed, 1074 insertions(+) create mode 100644 media/tests/CameraBrowser/Android.mk create mode 100644 media/tests/CameraBrowser/AndroidManifest.xml create mode 100644 media/tests/CameraBrowser/res/layout/object_info.xml create mode 100644 media/tests/CameraBrowser/res/layout/object_list.xml create mode 100644 media/tests/CameraBrowser/res/values/strings.xml create mode 100644 media/tests/CameraBrowser/res/values/styles.xml create mode 100644 media/tests/CameraBrowser/src/com/android/camerabrowser/CameraBrowser.java create mode 100644 media/tests/CameraBrowser/src/com/android/camerabrowser/CameraBrowserApplication.java create mode 100644 media/tests/CameraBrowser/src/com/android/camerabrowser/DeviceDisconnectedReceiver.java create mode 100644 media/tests/CameraBrowser/src/com/android/camerabrowser/ObjectBrowser.java create mode 100644 media/tests/CameraBrowser/src/com/android/camerabrowser/ObjectViewer.java create mode 100644 media/tests/CameraBrowser/src/com/android/camerabrowser/StorageBrowser.java create mode 100644 media/tests/CameraBrowser/src/com/android/camerabrowser/UsbReceiver.java diff --git a/media/tests/CameraBrowser/Android.mk b/media/tests/CameraBrowser/Android.mk new file mode 100644 index 0000000000000..1d8112911d3c3 --- /dev/null +++ b/media/tests/CameraBrowser/Android.mk @@ -0,0 +1,10 @@ +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_MODULE_TAGS := tests + +LOCAL_SRC_FILES := $(call all-subdir-java-files) + +LOCAL_PACKAGE_NAME := CameraBrowser + +include $(BUILD_PACKAGE) diff --git a/media/tests/CameraBrowser/AndroidManifest.xml b/media/tests/CameraBrowser/AndroidManifest.xml new file mode 100644 index 0000000000000..f167f4bec542b --- /dev/null +++ b/media/tests/CameraBrowser/AndroidManifest.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/media/tests/CameraBrowser/res/layout/object_info.xml b/media/tests/CameraBrowser/res/layout/object_info.xml new file mode 100644 index 0000000000000..a0499f267bfe1 --- /dev/null +++ b/media/tests/CameraBrowser/res/layout/object_info.xml @@ -0,0 +1,169 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/media/tests/CameraBrowser/res/layout/object_list.xml b/media/tests/CameraBrowser/res/layout/object_list.xml new file mode 100644 index 0000000000000..30c18bb8a0775 --- /dev/null +++ b/media/tests/CameraBrowser/res/layout/object_list.xml @@ -0,0 +1,33 @@ + + + + + + + + + diff --git a/media/tests/CameraBrowser/res/values/strings.xml b/media/tests/CameraBrowser/res/values/strings.xml new file mode 100644 index 0000000000000..932aaecc626ce --- /dev/null +++ b/media/tests/CameraBrowser/res/values/strings.xml @@ -0,0 +1,46 @@ + + + + + Camera Browser + + + Name: + Size: + Format: + Thumb Width: + Thumb Height: + Thumb Size: + Width: + Height: + Depth: + Sequence: + Created: + Modified: + Keywords: + + + Import + Delete + + + Object saved + Could not save object + Object deleted + Could not delete object + Import succeeded, but could not display object + + diff --git a/media/tests/CameraBrowser/res/values/styles.xml b/media/tests/CameraBrowser/res/values/styles.xml new file mode 100644 index 0000000000000..c869985af1d37 --- /dev/null +++ b/media/tests/CameraBrowser/res/values/styles.xml @@ -0,0 +1,34 @@ + + + + + + + + + + diff --git a/media/tests/CameraBrowser/src/com/android/camerabrowser/CameraBrowser.java b/media/tests/CameraBrowser/src/com/android/camerabrowser/CameraBrowser.java new file mode 100644 index 0000000000000..f642d93085365 --- /dev/null +++ b/media/tests/CameraBrowser/src/com/android/camerabrowser/CameraBrowser.java @@ -0,0 +1,139 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.camerabrowser; + +import android.app.ListActivity; +import android.content.ContentResolver; +import android.content.Context; +import android.content.Intent; +import android.mtp.MtpClient; +import android.mtp.MtpDevice; +import android.mtp.MtpDeviceInfo; +import android.os.Bundle; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.ListView; +import android.widget.TextView; +import android.widget.TwoLineListItem; + +import java.util.List; + + /** + * A list view displaying all connected cameras. + */ +public class CameraBrowser extends ListActivity implements MtpClient.Listener { + + private static final String TAG = "CameraBrowser"; + + private MtpClient mClient; + private List mDeviceList; + + private static final int MODEL_COLUMN = 0; + private static final int MANUFACTURER_COLUMN = 1; + private static final int COLUMN_COUNT = 2; + + private class CameraAdapter extends BaseAdapter { + private final Context mContext; + private final LayoutInflater mInflater; + + public CameraAdapter(Context c) { + mContext = c; + mInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + } + + public int getCount() { + return mDeviceList.size(); + } + + public Object getItem(int position) { + return mDeviceList.get(position); + } + + public long getItemId(int position) { + return position; + } + + public View getView(int position, View convertView, ViewGroup parent) { + TwoLineListItem view; + if (convertView == null) { + view = (TwoLineListItem)mInflater.inflate( + android.R.layout.simple_list_item_2, parent, false); + } else { + view = (TwoLineListItem)convertView; + } + + TextView textView1 = (TextView)view.findViewById(com.android.internal.R.id.text1); + TextView textView2 = (TextView)view.findViewById(com.android.internal.R.id.text2); + MtpDevice device = mDeviceList.get(position); + MtpDeviceInfo info = device.getDeviceInfo(); + if (info != null) { + textView1.setText(info.getManufacturer()); + textView2.setText(info.getModel()); + } else { + textView1.setText("???"); + textView2.setText("???"); + } + return view; + } + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + mClient = ((CameraBrowserApplication)getApplication()).getMtpClient(); + mClient.addListener(this); + mDeviceList = mClient.getDeviceList(); + } + + @Override + protected void onResume() { + super.onResume(); + reload(); + } + + @Override + protected void onDestroy() { + mClient.removeListener(this); + super.onDestroy(); + } + + @Override + protected void onListItemClick(ListView l, View v, int position, long id) { + Intent intent = new Intent(this, StorageBrowser.class); + intent.putExtra("device", mDeviceList.get(position).getDeviceName()); + startActivity(intent); + } + + private void reload() { + setListAdapter(new CameraAdapter(this)); + } + + public void deviceAdded(MtpDevice device) { + Log.d(TAG, "deviceAdded: " + device.getDeviceName()); + mDeviceList = mClient.getDeviceList(); + reload(); + } + + public void deviceRemoved(MtpDevice device) { + Log.d(TAG, "deviceRemoved: " + device.getDeviceName()); + mDeviceList = mClient.getDeviceList(); + reload(); + } +} diff --git a/media/tests/CameraBrowser/src/com/android/camerabrowser/CameraBrowserApplication.java b/media/tests/CameraBrowser/src/com/android/camerabrowser/CameraBrowserApplication.java new file mode 100644 index 0000000000000..6f1edfea2a3ed --- /dev/null +++ b/media/tests/CameraBrowser/src/com/android/camerabrowser/CameraBrowserApplication.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.camerabrowser; + +import android.app.Application; +import android.mtp.MtpClient; + + +public class CameraBrowserApplication extends Application { + + private MtpClient mClient; + + @Override + public void onCreate() { + mClient = new MtpClient(this); + } + + @Override + public void onTerminate() { + mClient.close(); + mClient = null; + } + + public MtpClient getMtpClient() { + return mClient; + } +} diff --git a/media/tests/CameraBrowser/src/com/android/camerabrowser/DeviceDisconnectedReceiver.java b/media/tests/CameraBrowser/src/com/android/camerabrowser/DeviceDisconnectedReceiver.java new file mode 100644 index 0000000000000..736af1f506f2c --- /dev/null +++ b/media/tests/CameraBrowser/src/com/android/camerabrowser/DeviceDisconnectedReceiver.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.camerabrowser; + +import android.app.Activity; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.hardware.UsbManager; +import android.util.Log; + +public class DeviceDisconnectedReceiver extends BroadcastReceiver { + + private static final String TAG = "DeviceDisconnectedReceiver"; + + private final Activity mActivity; + private final String mDeviceName; + + public DeviceDisconnectedReceiver(Activity activity, String deviceName) { + mActivity = activity; + mDeviceName = deviceName; + + IntentFilter filter = new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED); + activity.registerReceiver(this, filter); + } + + @Override + public void onReceive(Context context, Intent intent) { + String deviceName = intent.getStringExtra(UsbManager.EXTRA_DEVICE_NAME); + Log.d(TAG, "ACTION_USB_DEVICE_DETACHED " + deviceName); + + // close our activity if the device it is displaying is disconnected + if (deviceName.equals(mDeviceName)) { + mActivity.finish(); + } + } +} \ No newline at end of file diff --git a/media/tests/CameraBrowser/src/com/android/camerabrowser/ObjectBrowser.java b/media/tests/CameraBrowser/src/com/android/camerabrowser/ObjectBrowser.java new file mode 100644 index 0000000000000..82251d9213e58 --- /dev/null +++ b/media/tests/CameraBrowser/src/com/android/camerabrowser/ObjectBrowser.java @@ -0,0 +1,147 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.camerabrowser; + +import android.app.ListActivity; +import android.content.Context; +import android.content.Intent; +import android.database.Cursor; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.mtp.MtpClient; +import android.mtp.MtpConstants; +import android.mtp.MtpDevice; +import android.mtp.MtpObjectInfo; +import android.os.Bundle; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.ImageView; +import android.widget.ListView; +import android.widget.TextView; + +import java.util.List; + + /** + * A list view displaying all objects within a container (folder or storage unit). + */ +public class ObjectBrowser extends ListActivity { + + private static final String TAG = "ObjectBrowser"; + + private MtpClient mClient; + private List mObjectList; + private String mDeviceName; + private int mStorageID; + private int mObjectID; + private DeviceDisconnectedReceiver mDisconnectedReceiver; + + private class ObjectAdapter extends BaseAdapter { + private final Context mContext; + private final LayoutInflater mInflater; + + public ObjectAdapter(Context c) { + mContext = c; + mInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + } + + public int getCount() { + if (mObjectList == null) { + return 0; + } else { + return mObjectList.size(); + } + } + + public Object getItem(int position) { + return mObjectList.get(position); + } + + public long getItemId(int position) { + return position; + } + + public View getView(int position, View convertView, ViewGroup parent) { + View view; + if (convertView == null) { + view = mInflater.inflate(R.layout.object_list, parent, false); + } else { + view = convertView; + } + + TextView nameView = (TextView)view.findViewById(R.id.name); + MtpObjectInfo info = mObjectList.get(position); + nameView.setText(info.getName()); + + int thumbFormat = info.getThumbFormat(); + if (thumbFormat == MtpConstants.FORMAT_EXIF_JPEG + || thumbFormat == MtpConstants.FORMAT_JFIF) { + byte[] thumbnail = mClient.getThumbnail(mDeviceName, info.getObjectHandle()); + if (thumbnail != null) { + Bitmap bitmap = BitmapFactory.decodeByteArray(thumbnail, 0, thumbnail.length); + if (bitmap != null) { + ImageView thumbView = (ImageView)view.findViewById(R.id.thumbnail); + thumbView.setImageBitmap(bitmap); + } + } + } + return view; + } + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + mClient = ((CameraBrowserApplication)getApplication()).getMtpClient(); + mDeviceName = getIntent().getStringExtra("device"); + mStorageID = getIntent().getIntExtra("storage", 0); + mObjectID = getIntent().getIntExtra("object", 0); + mDisconnectedReceiver = new DeviceDisconnectedReceiver(this, mDeviceName); + } + + @Override + protected void onResume() { + super.onResume(); + + mObjectList = mClient.getObjectList(mDeviceName, mStorageID, mObjectID); + setListAdapter(new ObjectAdapter(this)); + } + + @Override + protected void onDestroy() { + unregisterReceiver(mDisconnectedReceiver); + super.onDestroy(); + } + + @Override + protected void onListItemClick(ListView l, View v, int position, long id) { + MtpObjectInfo info = mObjectList.get(position); + Intent intent; + if (info.getFormat() == MtpConstants.FORMAT_ASSOCIATION) { + intent = new Intent(this, ObjectBrowser.class); + } else { + intent = new Intent(this, ObjectViewer.class); + } + intent.putExtra("device", mDeviceName); + intent.putExtra("storage", mStorageID); + intent.putExtra("object", info.getObjectHandle()); + startActivity(intent); + } +} diff --git a/media/tests/CameraBrowser/src/com/android/camerabrowser/ObjectViewer.java b/media/tests/CameraBrowser/src/com/android/camerabrowser/ObjectViewer.java new file mode 100644 index 0000000000000..e9ea9f3a1bd80 --- /dev/null +++ b/media/tests/CameraBrowser/src/com/android/camerabrowser/ObjectViewer.java @@ -0,0 +1,203 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.camerabrowser; + +import android.app.Activity; +import android.content.ActivityNotFoundException; +import android.content.Context; +import android.content.Intent; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.media.MediaScannerConnection; +import android.media.MediaScannerConnection.MediaScannerConnectionClient; +import android.mtp.MtpClient; +import android.mtp.MtpConstants; +import android.mtp.MtpObjectInfo; +import android.net.Uri; +import android.os.Bundle; +import android.os.Environment; +import android.util.Log; +import android.view.View; +import android.widget.Button; +import android.widget.ImageView; +import android.widget.TextView; +import android.widget.Toast; + +import java.io.File; +import java.util.Date; + +/** + * A view to display the properties of an object. + */ +public class ObjectViewer extends Activity implements View.OnClickListener { + + private static final String TAG = "ObjectViewer"; + + private MtpClient mClient; + private String mDeviceName; + private int mStorageID; + private int mObjectID; + private String mFileName; + private Button mImportButton; + private Button mDeleteButton; + private DeviceDisconnectedReceiver mDisconnectedReceiver; + + private final class ScannerClient implements MediaScannerConnectionClient { + private final Context mContext; + private String mPath; + + public ScannerClient(Context context) { + mContext = context; + } + + public void setScanPath(String path) { + mPath = path; + } + + @Override + public void onMediaScannerConnected() { + mScannerConnection.scanFile(mPath, null); + } + + @Override + public void onScanCompleted(String path, Uri uri) { + mScannerConnection.disconnect(); + + // try to start an activity to view the file + Intent intent = new Intent(Intent.ACTION_VIEW, uri); + try { + mContext.startActivity(intent); + } catch (ActivityNotFoundException e) { + Toast.makeText(mContext, R.string.start_activity_failed_message, + Toast.LENGTH_SHORT).show(); + } + } + } + + private MediaScannerConnection mScannerConnection; + private ScannerClient mScannerClient; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + mClient = ((CameraBrowserApplication)getApplication()).getMtpClient(); + + setContentView(R.layout.object_info); + + mImportButton = (Button)findViewById(R.id.import_button); + mImportButton.setOnClickListener(this); + mDeleteButton = (Button)findViewById(R.id.delete_button); + mDeleteButton.setOnClickListener(this); + + mDeviceName = getIntent().getStringExtra("device"); + mStorageID = getIntent().getIntExtra("storage", 0); + mObjectID = getIntent().getIntExtra("object", 0); + mDisconnectedReceiver = new DeviceDisconnectedReceiver(this, mDeviceName); + mScannerClient = new ScannerClient(this); + mScannerConnection = new MediaScannerConnection(this, mScannerClient); + } + + @Override + protected void onResume() { + super.onResume(); + + MtpObjectInfo info = mClient.getObjectInfo(mDeviceName, mObjectID); + if (info != null) { + TextView view = (TextView)findViewById(R.id.name); + mFileName = info.getName(); + view.setText(mFileName); + view = (TextView)findViewById(R.id.format); + view.setText(Integer.toHexString(info.getFormat()).toUpperCase()); + view = (TextView)findViewById(R.id.size); + view.setText(Long.toString(info.getCompressedSize())); + view = (TextView)findViewById(R.id.thumb_width); + view.setText(Long.toString(info.getThumbPixWidth())); + view = (TextView)findViewById(R.id.thumb_height); + view.setText(Long.toString(info.getThumbPixHeight())); + view = (TextView)findViewById(R.id.thumb_size); + view.setText(Long.toString(info.getThumbCompressedSize())); + view = (TextView)findViewById(R.id.width); + view.setText(Long.toString(info.getImagePixWidth())); + view = (TextView)findViewById(R.id.height); + view.setText(Long.toString(info.getImagePixHeight())); + view = (TextView)findViewById(R.id.depth); + view.setText(Long.toString(info.getImagePixDepth())); + view = (TextView)findViewById(R.id.sequence); + view.setText(Long.toString(info.getSequenceNumber())); + view = (TextView)findViewById(R.id.created); + Date date = new Date(info.getDateCreated() * 1000); + view.setText(date.toString()); + view = (TextView)findViewById(R.id.modified); + date = new Date(info.getDateModified() * 1000); + view.setText(date.toString()); + view = (TextView)findViewById(R.id.keywords); + view.setText(info.getKeywords()); + int thumbFormat = info.getThumbFormat(); + if (thumbFormat == MtpConstants.FORMAT_EXIF_JPEG + || thumbFormat == MtpConstants.FORMAT_JFIF) { + byte[] thumbnail = mClient.getThumbnail(mDeviceName, info.getObjectHandle()); + if (thumbnail != null) { + Bitmap bitmap = BitmapFactory.decodeByteArray(thumbnail, 0, thumbnail.length); + if (bitmap != null) { + ImageView thumbView = (ImageView)findViewById(R.id.thumbnail); + thumbView.setImageBitmap(bitmap); + } + } + } + } + } + + @Override + protected void onDestroy() { + unregisterReceiver(mDisconnectedReceiver); + super.onDestroy(); + } + + private void importObject() { + // copy file to /mnt/sdcard/imported/ + File dest = Environment.getExternalStorageDirectory(); + dest = new File(dest, "imported"); + dest.mkdirs(); + dest = new File(dest, mFileName); + + if (mClient.importFile(mDeviceName, mObjectID, dest.getAbsolutePath())) { + Toast.makeText(this, R.string.object_saved_message, Toast.LENGTH_SHORT).show(); + + mScannerClient.setScanPath(dest.getAbsolutePath()); + mScannerConnection.connect(); + } else { + Toast.makeText(this, R.string.save_failed_message, Toast.LENGTH_SHORT).show(); + } + } + + private void deleteObject() { + if (mClient.deleteObject(mDeviceName, mObjectID)) { + Toast.makeText(this, R.string.object_deleted_message, Toast.LENGTH_SHORT).show(); + finish(); + } else { + Toast.makeText(this, R.string.delete_failed_message, Toast.LENGTH_SHORT).show(); + } + } + + public void onClick(View v) { + if (v == mImportButton) { + importObject(); + } else if (v == mDeleteButton) { + deleteObject(); + } + } +} diff --git a/media/tests/CameraBrowser/src/com/android/camerabrowser/StorageBrowser.java b/media/tests/CameraBrowser/src/com/android/camerabrowser/StorageBrowser.java new file mode 100644 index 0000000000000..7d5a5dace7ad4 --- /dev/null +++ b/media/tests/CameraBrowser/src/com/android/camerabrowser/StorageBrowser.java @@ -0,0 +1,121 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.camerabrowser; + +import android.app.ListActivity; +import android.content.Context; +import android.content.Intent; +import android.mtp.MtpClient; +import android.mtp.MtpDevice; +import android.mtp.MtpStorageInfo; +import android.os.Bundle; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.ListView; +import android.widget.TextView; + +import java.util.List; + +/** + * A list view displaying all storage units on a device. + */ +public class StorageBrowser extends ListActivity { + + private static final String TAG = "StorageBrowser"; + + private MtpClient mClient; + private String mDeviceName; + private List mStorageList; + private DeviceDisconnectedReceiver mDisconnectedReceiver; + + private class StorageAdapter extends BaseAdapter { + private final Context mContext; + private final LayoutInflater mInflater; + + public StorageAdapter(Context c) { + mContext = c; + mInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + } + + public int getCount() { + if (mStorageList == null) { + return 0; + } else { + return mStorageList.size(); + } + } + + public Object getItem(int position) { + return mStorageList.get(position); + } + + public long getItemId(int position) { + return position; + } + + public View getView(int position, View convertView, ViewGroup parent) { + TextView view; + if (convertView == null) { + view = (TextView)mInflater.inflate( + android.R.layout.simple_list_item_1, parent, false); + } else { + view = (TextView)convertView; + } + + MtpStorageInfo info = mStorageList.get(position); + if (info != null) { + view.setText(info.getDescription()); + } else { + view.setText("???"); + } + return view; + } + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + mClient = ((CameraBrowserApplication)getApplication()).getMtpClient(); + mDeviceName = getIntent().getStringExtra("device"); + mDisconnectedReceiver = new DeviceDisconnectedReceiver(this, mDeviceName); + } + + @Override + protected void onResume() { + super.onResume(); + mStorageList = mClient.getStorageList(mDeviceName); + setListAdapter(new StorageAdapter(this)); + } + + @Override + protected void onDestroy() { + unregisterReceiver(mDisconnectedReceiver); + super.onDestroy(); + } + + @Override + protected void onListItemClick(ListView l, View v, int position, long id) { + Intent intent = new Intent(this, ObjectBrowser.class); + intent.putExtra("device", mDeviceName); + intent.putExtra("storage", mStorageList.get(position).getStorageId()); + startActivity(intent); + } +} diff --git a/media/tests/CameraBrowser/src/com/android/camerabrowser/UsbReceiver.java b/media/tests/CameraBrowser/src/com/android/camerabrowser/UsbReceiver.java new file mode 100644 index 0000000000000..22d9443af6ee6 --- /dev/null +++ b/media/tests/CameraBrowser/src/com/android/camerabrowser/UsbReceiver.java @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.camerabrowser; + +import android.content.Context; +import android.content.Intent; +import android.content.BroadcastReceiver; +import android.hardware.UsbDevice; +import android.hardware.UsbManager; +import android.mtp.MtpClient; +import android.os.Bundle; +import android.os.Parcelable; +import android.util.Log; + +public class UsbReceiver extends BroadcastReceiver +{ + private static final String TAG = "UsbReceiver"; + + @Override + public void onReceive(Context context, Intent intent) { + Log.d(TAG, "onReceive " + intent); + if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(intent.getAction())) { + UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE); + if (MtpClient.isCamera(device)) { + String deviceName = device.getDeviceName(); + Log.d(TAG, "Got camera: " + deviceName); + intent = new Intent(context, StorageBrowser.class); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + intent.putExtra("device", deviceName); + context.startActivity(intent); + } + } + } +}