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.Context;
22import android.content.Intent;
23import android.mtp.MtpDevice;
24import android.mtp.MtpDeviceInfo;
25import android.os.Bundle;
26import android.util.Log;
27import android.view.LayoutInflater;
28import android.view.View;
29import android.view.ViewGroup;
30import android.widget.BaseAdapter;
31import android.widget.ListView;
32import android.widget.TextView;
33import android.widget.TwoLineListItem;
34
35import java.util.List;
36
37 /**
38 * A list view displaying all connected cameras.
39 */
40public class CameraBrowser extends ListActivity implements MtpClient.Listener {
41
42    private static final String TAG = "CameraBrowser";
43
44    private MtpClient mClient;
45    private List<MtpDevice> mDeviceList;
46
47    private static final int MODEL_COLUMN = 0;
48    private static final int MANUFACTURER_COLUMN = 1;
49    private static final int COLUMN_COUNT = 2;
50
51    private class CameraAdapter extends BaseAdapter {
52        private final Context mContext;
53        private final LayoutInflater mInflater;
54
55        public CameraAdapter(Context c) {
56            mContext = c;
57            mInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
58        }
59
60        public int getCount() {
61            return mDeviceList.size();
62        }
63
64        public Object getItem(int position) {
65            return mDeviceList.get(position);
66        }
67
68        public long getItemId(int position) {
69            return position;
70        }
71
72        public View getView(int position, View convertView, ViewGroup parent) {
73            TwoLineListItem view;
74            if (convertView == null) {
75                view = (TwoLineListItem)mInflater.inflate(
76                        android.R.layout.simple_list_item_2, parent, false);
77            } else {
78                view = (TwoLineListItem)convertView;
79            }
80
81            TextView textView1 = (TextView)view.findViewById(android.R.id.text1);
82            TextView textView2 = (TextView)view.findViewById(android.R.id.text2);
83            MtpDevice device = mDeviceList.get(position);
84            MtpDeviceInfo info = device.getDeviceInfo();
85            if (info != null) {
86                textView1.setText(info.getManufacturer());
87                textView2.setText(info.getModel());
88            } else {
89                textView1.setText("???");
90                textView2.setText("???");
91            }
92            return view;
93        }
94    }
95
96    @Override
97    protected void onCreate(Bundle savedInstanceState) {
98        super.onCreate(savedInstanceState);
99        mClient = ((CameraBrowserApplication)getApplication()).getMtpClient();
100        mClient.addListener(this);
101        mDeviceList = mClient.getDeviceList();
102    }
103
104    @Override
105    protected void onResume() {
106        super.onResume();
107        reload();
108    }
109
110    @Override
111    protected void onDestroy() {
112        mClient.removeListener(this);
113        super.onDestroy();
114    }
115
116    @Override
117    protected void onListItemClick(ListView l, View v, int position, long id) {
118        Intent intent = new Intent(this, StorageBrowser.class);
119        intent.putExtra("device", mDeviceList.get(position).getDeviceName());
120        startActivity(intent);
121    }
122
123    private void reload() {
124        setListAdapter(new CameraAdapter(this));
125    }
126
127    public void deviceAdded(MtpDevice device) {
128        Log.d(TAG, "deviceAdded: " + device.getDeviceName());
129        mDeviceList = mClient.getDeviceList();
130        reload();
131    }
132
133    public void deviceRemoved(MtpDevice device) {
134        Log.d(TAG, "deviceRemoved: " + device.getDeviceName());
135        mDeviceList = mClient.getDeviceList();
136        reload();
137    }
138}
139