ObjectBrowser.java revision ad0643a330db13c8f11b1a71fbb7262570114f4d
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.AdapterView;
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 Cursor mCursor;
44    private ObjectCursorAdapter mAdapter;
45    private int mDeviceID;
46    private int mStorageID;
47    private int mObjectID;
48
49    private static final String[] OBJECT_COLUMNS =
50        new String[] { Mtp.Object._ID, Mtp.Object.NAME, Mtp.Object.FORMAT, Mtp.Object.THUMB };
51
52    static final int ID_COLUMN = 0;
53    static final int NAME_COLUMN = 1;
54    static final int FORMAT_COLUMN = 2;
55    static final int THUMB_COLUMN = 3;
56
57    @Override
58    protected void onCreate(Bundle savedInstanceState) {
59        super.onCreate(savedInstanceState);
60    }
61
62    @Override
63    protected void onResume() {
64        super.onResume();
65
66        mDeviceID = getIntent().getIntExtra("device", 0);
67        mStorageID = getIntent().getIntExtra("storage", 0);
68        mObjectID = getIntent().getIntExtra("object", 0);
69        if (mDeviceID != 0 && mStorageID != 0) {
70            Cursor c;
71            Uri uri;
72            if (mObjectID == 0) {
73                uri = Mtp.Object.getContentUriForStorageChildren(mDeviceID, mStorageID);
74            } else {
75                uri = Mtp.Object.getContentUriForObjectChildren(mDeviceID, mObjectID);
76            }
77            Log.d(TAG, "query " + uri);
78            c = getContentResolver().query(uri, OBJECT_COLUMNS, null, null, null);
79            startManagingCursor(c);
80            mCursor = 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