ObjectBrowser.java revision 0809c0e4c61fb1f64925081c811df86c73f7070f
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.Intent; 21import android.database.Cursor; 22import android.net.Uri; 23import android.os.Bundle; 24import android.provider.Mtp; 25import android.util.Log; 26import android.view.View; 27import android.widget.ListAdapter; 28import android.widget.ListView; 29import android.widget.SimpleCursorAdapter; 30 31 /** 32 * A list view displaying all objects within a container (folder or storage unit). 33 */ 34public class ObjectBrowser extends ListActivity { 35 36 private static final String TAG = "ObjectBrowser"; 37 38 private ListAdapter mAdapter; 39 private int mDeviceID; 40 private int mStorageID; 41 private int mObjectID; 42 43 private static final String[] OBJECT_COLUMNS = 44 new String[] { Mtp.Object._ID, Mtp.Object.NAME, Mtp.Object.FORMAT }; 45 46 @Override 47 protected void onCreate(Bundle savedInstanceState) { 48 super.onCreate(savedInstanceState); 49 } 50 51 @Override 52 protected void onResume() { 53 super.onResume(); 54 55 mDeviceID = getIntent().getIntExtra("device", 0); 56 mStorageID = getIntent().getIntExtra("storage", 0); 57 mObjectID = getIntent().getIntExtra("object", 0); 58 if (mDeviceID != 0 && mStorageID != 0) { 59 Cursor c; 60 if (mObjectID == 0) { 61 c = getContentResolver().query( 62 Mtp.Object.getContentUriForStorageChildren(mDeviceID, mStorageID), 63 OBJECT_COLUMNS, null, null, null); 64 } else { 65 c = getContentResolver().query( 66 Mtp.Object.getContentUriForObjectChildren(mDeviceID, mObjectID), 67 OBJECT_COLUMNS, null, null, null); 68 } 69 Log.d(TAG, "query returned " + c); 70 startManagingCursor(c); 71 72 // Map Cursor columns to views defined in simple_list_item_1.xml 73 mAdapter = new SimpleCursorAdapter(this, 74 android.R.layout.simple_list_item_1, c, 75 new String[] { Mtp.Object.NAME }, 76 new int[] { android.R.id.text1, android.R.id.text2 }); 77 setListAdapter(mAdapter); 78 } 79 } 80 81 @Override 82 protected void onListItemClick(ListView l, View v, int position, long id) { 83 int rowID = (int)mAdapter.getItemId(position); 84 Cursor c = getContentResolver().query( 85 Mtp.Object.getContentUri(mDeviceID, rowID), 86 OBJECT_COLUMNS, null, null, null); 87 Log.d(TAG, "query returned " + c + " count: " + c.getCount()); 88 long format = 0; 89 if (c != null && c.getCount() == 1) { 90 c.moveToFirst(); 91 long rowId = c.getLong(0); 92 String name = c.getString(1); 93 format = c.getLong(2); 94 Log.d(TAG, "rowId: " + rowId + " name: " + name + " format: " + format); 95 } 96 if (format == Mtp.Object.FORMAT_JFIF) { 97 Intent intent = new Intent(this, ObjectViewer.class); 98 intent.putExtra("device", mDeviceID); 99 intent.putExtra("storage", mStorageID); 100 intent.putExtra("object",rowID); 101 startActivity(intent); 102 } else { 103 Intent intent = new Intent(this, ObjectBrowser.class); 104 intent.putExtra("device", mDeviceID); 105 intent.putExtra("storage", mStorageID); 106 intent.putExtra("object", rowID); 107 startActivity(intent); 108 } 109 } 110} 111