ObjectBrowser.java revision 99b3452f404e1574508665ebb71362766d362603
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.net.Uri; 26import android.os.Bundle; 27import android.provider.Mtp; 28import android.util.Log; 29import android.view.View; 30import android.widget.ListAdapter; 31import android.widget.ImageView; 32import android.widget.ListView; 33import android.widget.ResourceCursorAdapter; 34import android.widget.TextView; 35 36 /** 37 * A list view displaying all objects within a container (folder or storage unit). 38 */ 39public class ObjectBrowser extends ListActivity { 40 41 private static final String TAG = "ObjectBrowser"; 42 43 private ListAdapter mAdapter; 44 private int mDeviceID; 45 private int mStorageID; 46 private int mObjectID; 47 48 private static final String[] OBJECT_COLUMNS = 49 new String[] { Mtp.Object._ID, Mtp.Object.NAME, Mtp.Object.FORMAT, Mtp.Object.THUMB }; 50 51 static final int ID_COLUMN = 0; 52 static final int NAME_COLUMN = 1; 53 static final int FORMAT_COLUMN = 2; 54 static final int THUMB_COLUMN = 3; 55 56 @Override 57 protected void onCreate(Bundle savedInstanceState) { 58 super.onCreate(savedInstanceState); 59 } 60 61 @Override 62 protected void onResume() { 63 super.onResume(); 64 65 mDeviceID = getIntent().getIntExtra("device", 0); 66 mStorageID = getIntent().getIntExtra("storage", 0); 67 mObjectID = getIntent().getIntExtra("object", 0); 68 if (mDeviceID != 0 && mStorageID != 0) { 69 Cursor c; 70 if (mObjectID == 0) { 71 c = getContentResolver().query( 72 Mtp.Object.getContentUriForStorageChildren(mDeviceID, mStorageID), 73 OBJECT_COLUMNS, null, null, null); 74 } else { 75 c = getContentResolver().query( 76 Mtp.Object.getContentUriForObjectChildren(mDeviceID, mObjectID), 77 OBJECT_COLUMNS, null, null, null); 78 } 79 Log.d(TAG, "query returned " + c); 80 startManagingCursor(c); 81 82 // Map Cursor columns to views defined in simple_list_item_1.xml 83 mAdapter = new ObjectCursorAdapter(this, c); 84 setListAdapter(mAdapter); 85 } 86 } 87 88 @Override 89 protected void onListItemClick(ListView l, View v, int position, long id) { 90 int rowID = (int)mAdapter.getItemId(position); 91 Cursor c = getContentResolver().query( 92 Mtp.Object.getContentUri(mDeviceID, rowID), 93 OBJECT_COLUMNS, null, null, null); 94 Log.d(TAG, "query returned " + c + " count: " + c.getCount()); 95 long format = 0; 96 if (c != null && c.getCount() == 1) { 97 c.moveToFirst(); 98 long rowId = c.getLong(ID_COLUMN); 99 String name = c.getString(NAME_COLUMN); 100 format = c.getLong(FORMAT_COLUMN); 101 Log.d(TAG, "rowId: " + rowId + " name: " + name + " format: " + format); 102 } 103 if (format == Mtp.Object.FORMAT_JFIF) { 104 Intent intent = new Intent(this, ObjectViewer.class); 105 intent.putExtra("device", mDeviceID); 106 intent.putExtra("storage", mStorageID); 107 intent.putExtra("object",rowID); 108 startActivity(intent); 109 } else { 110 Intent intent = new Intent(this, ObjectBrowser.class); 111 intent.putExtra("device", mDeviceID); 112 intent.putExtra("storage", mStorageID); 113 intent.putExtra("object", rowID); 114 startActivity(intent); 115 } 116 } 117 118 private class ObjectCursorAdapter extends ResourceCursorAdapter { 119 120 public ObjectCursorAdapter(Context context, Cursor c) { 121 super(context, R.layout.object_list, c); 122 } 123 124 @Override 125 public void bindView(View view, Context context, Cursor cursor) { 126 ImageView thumbView = (ImageView)view.findViewById(R.id.thumbnail); 127 TextView nameView = (TextView)view.findViewById(R.id.name); 128 129 // get the thumbnail 130 byte[] thumbnail = cursor.getBlob(THUMB_COLUMN); 131 if (thumbnail != null) { 132 Bitmap bitmap = BitmapFactory.decodeByteArray(thumbnail, 0, thumbnail.length); 133 if (bitmap != null) { 134 thumbView.setImageBitmap(bitmap); 135 } 136 } 137 138 // get the name 139 String name = cursor.getString(NAME_COLUMN); 140 if (name == null) { 141 name = ""; 142 } 143 nameView.setText(name); 144 } 145 } 146} 147