ObjectBrowser.java revision 5367ab6aab2a61aaf534b18576dcdd0bca1b9681
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.camerabrowser; 18 19import android.app.ListActivity; 20import android.content.Context; 21import android.content.Intent; 22import android.database.Cursor; 23import android.graphics.Bitmap; 24import android.graphics.BitmapFactory; 25import android.media.MtpConstants; 26import android.net.Uri; 27import android.os.Bundle; 28import android.provider.Mtp; 29import android.util.Log; 30import android.view.View; 31import android.widget.AdapterView; 32import android.widget.ImageView; 33import android.widget.ListView; 34import android.widget.ResourceCursorAdapter; 35import android.widget.TextView; 36 37 /** 38 * A list view displaying all objects within a container (folder or storage unit). 39 */ 40public class ObjectBrowser extends ListActivity { 41 42 private static final String TAG = "ObjectBrowser"; 43 44 private Cursor mCursor; 45 private ObjectCursorAdapter mAdapter; 46 private int mDeviceID; 47 private int mStorageID; 48 private int mObjectID; 49 50 private static final String[] OBJECT_COLUMNS = 51 new String[] { Mtp.Object._ID, Mtp.Object.NAME, Mtp.Object.FORMAT, Mtp.Object.THUMB }; 52 53 static final int ID_COLUMN = 0; 54 static final int NAME_COLUMN = 1; 55 static final int FORMAT_COLUMN = 2; 56 static final int THUMB_COLUMN = 3; 57 58 @Override 59 protected void onCreate(Bundle savedInstanceState) { 60 super.onCreate(savedInstanceState); 61 } 62 63 @Override 64 protected void onResume() { 65 super.onResume(); 66 67 mDeviceID = getIntent().getIntExtra("device", 0); 68 mStorageID = getIntent().getIntExtra("storage", 0); 69 mObjectID = getIntent().getIntExtra("object", 0); 70 if (mDeviceID != 0 && mStorageID != 0) { 71 Cursor c; 72 Uri uri; 73 if (mObjectID == 0) { 74 uri = Mtp.Object.getContentUriForStorageChildren(mDeviceID, mStorageID); 75 } else { 76 uri = Mtp.Object.getContentUriForObjectChildren(mDeviceID, mObjectID); 77 } 78 Log.d(TAG, "query " + uri); 79 c = getContentResolver().query(uri, OBJECT_COLUMNS, null, null, null); 80 startManagingCursor(c); 81 mCursor = c; 82 83 // Map Cursor columns to views defined in simple_list_item_1.xml 84 mAdapter = new ObjectCursorAdapter(this, c); 85 setListAdapter(mAdapter); 86 } 87 } 88 89 @Override 90 protected void onListItemClick(ListView l, View v, int position, long id) { 91 int rowID = (int)mAdapter.getItemId(position); 92 Cursor c = getContentResolver().query( 93 Mtp.Object.getContentUri(mDeviceID, rowID), 94 OBJECT_COLUMNS, null, null, null); 95 Log.d(TAG, "query returned " + c + " count: " + c.getCount()); 96 long format = 0; 97 if (c != null && c.getCount() == 1) { 98 c.moveToFirst(); 99 long rowId = c.getLong(ID_COLUMN); 100 String name = c.getString(NAME_COLUMN); 101 format = c.getLong(FORMAT_COLUMN); 102 Log.d(TAG, "rowId: " + rowId + " name: " + name + " format: " + format); 103 } 104 if (format == MtpConstants.FORMAT_JFIF) { 105 Intent intent = new Intent(this, ObjectViewer.class); 106 intent.putExtra("device", mDeviceID); 107 intent.putExtra("storage", mStorageID); 108 intent.putExtra("object",rowID); 109 startActivity(intent); 110 } else { 111 Intent intent = new Intent(this, ObjectBrowser.class); 112 intent.putExtra("device", mDeviceID); 113 intent.putExtra("storage", mStorageID); 114 intent.putExtra("object", rowID); 115 startActivity(intent); 116 } 117 } 118 119 private class ObjectCursorAdapter extends ResourceCursorAdapter { 120 121 public ObjectCursorAdapter(Context context, Cursor c) { 122 super(context, R.layout.object_list, c); 123 } 124 125 @Override 126 public void bindView(View view, Context context, Cursor cursor) { 127 ImageView thumbView = (ImageView)view.findViewById(R.id.thumbnail); 128 TextView nameView = (TextView)view.findViewById(R.id.name); 129 130 // get the thumbnail 131 byte[] thumbnail = cursor.getBlob(THUMB_COLUMN); 132 if (thumbnail != null) { 133 Bitmap bitmap = BitmapFactory.decodeByteArray(thumbnail, 0, thumbnail.length); 134 if (bitmap != null) { 135 thumbView.setImageBitmap(bitmap); 136 } 137 } 138 139 // get the name 140 String name = cursor.getString(NAME_COLUMN); 141 if (name == null) { 142 name = ""; 143 } 144 nameView.setText(name); 145 } 146 } 147} 148