CameraBrowser.java revision a31560598af25d5116effc2a6af0d9be12a0628d
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.ContentResolver;
21import android.content.Intent;
22import android.database.ContentObserver;
23import android.database.Cursor;
24import android.net.Uri;
25import android.os.Bundle;
26import android.os.Handler;
27import android.provider.Ptp;
28import android.util.Log;
29import android.view.View;
30import android.widget.ListAdapter;
31import android.widget.ListView;
32import android.widget.SimpleCursorAdapter;
33
34 /**
35 * A list view displaying all connected cameras.
36 */
37public class CameraBrowser extends ListActivity {
38
39    private static final String TAG = "CameraBrowser";
40
41    private ListAdapter mAdapter;
42    private ContentResolver mResolver;
43    private DeviceObserver mDeviceObserver;
44    private Cursor mCursor;
45
46    private class DeviceObserver extends ContentObserver {
47        DeviceObserver(Handler handler) {
48            super(handler);
49        }
50
51        @Override
52        public void onChange(boolean selfChange) {
53            Log.d(TAG, "DeviceObserver.onChange");
54            if (mCursor != null) {
55                mCursor.requery();
56            }
57        }
58    }
59
60    private static final String[] DEVICE_COLUMNS =
61         new String[] { Ptp.Device._ID, Ptp.Device.MANUFACTURER, Ptp.Device.MODEL };
62
63    @Override
64    protected void onCreate(Bundle savedInstanceState) {
65        super.onCreate(savedInstanceState);
66        mResolver = getContentResolver();
67        mDeviceObserver = new DeviceObserver(new Handler());
68    }
69
70    @Override
71    protected void onResume() {
72        super.onResume();
73
74        Cursor c = getContentResolver().query(Ptp.Device.CONTENT_URI,
75                DEVICE_COLUMNS, null, null, null);
76        Log.d(TAG, "query returned " + c);
77        startManagingCursor(c);
78        mCursor = c;
79
80        // Map Cursor columns to views defined in simple_list_item_2.xml
81        mAdapter = new SimpleCursorAdapter(this,
82                android.R.layout.simple_list_item_2, c,
83                        new String[] { Ptp.Device.MANUFACTURER, Ptp.Device.MODEL },
84                        new int[] { android.R.id.text1, android.R.id.text2 });
85        setListAdapter(mAdapter);
86
87        // register for changes to the device list
88        mResolver.registerContentObserver(Ptp.Device.CONTENT_URI, true, mDeviceObserver);
89    }
90
91    @Override
92    protected void onPause() {
93        super.onPause();
94        mResolver.unregisterContentObserver(mDeviceObserver);
95    }
96
97    @Override
98    protected void onListItemClick(ListView l, View v, int position, long id) {
99        Intent intent = new Intent(this, StorageBrowser.class);
100        intent.putExtra("device", (int)mAdapter.getItemId(position));
101        startActivity(intent);
102    }
103}
104