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 
17 package com.android.camerabrowser;
18 
19 import android.app.ListActivity;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.mtp.MtpDevice;
23 import android.mtp.MtpStorageInfo;
24 import android.os.Bundle;
25 import android.util.Log;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.widget.BaseAdapter;
30 import android.widget.ListView;
31 import android.widget.TextView;
32 
33 import java.util.List;
34 
35 /**
36  * A list view displaying all storage units on a device.
37  */
38 public class StorageBrowser extends ListActivity {
39 
40     private static final String TAG = "StorageBrowser";
41 
42     private MtpClient mClient;
43     private String mDeviceName;
44     private List<MtpStorageInfo> mStorageList;
45     private DeviceDisconnectedReceiver mDisconnectedReceiver;
46 
47     private class StorageAdapter extends BaseAdapter {
48         private final Context mContext;
49         private final LayoutInflater mInflater;
50 
StorageAdapter(Context c)51         public StorageAdapter(Context c) {
52             mContext = c;
53             mInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
54         }
55 
getCount()56         public int getCount() {
57             if (mStorageList == null) {
58                 return 0;
59             } else {
60                 return mStorageList.size();
61             }
62         }
63 
getItem(int position)64         public Object getItem(int position) {
65             return mStorageList.get(position);
66         }
67 
getItemId(int position)68         public long getItemId(int position) {
69             return position;
70         }
71 
getView(int position, View convertView, ViewGroup parent)72         public View getView(int position, View convertView, ViewGroup parent) {
73             TextView view;
74             if (convertView == null) {
75                 view = (TextView)mInflater.inflate(
76                         android.R.layout.simple_list_item_1, parent, false);
77             } else {
78                 view = (TextView)convertView;
79             }
80 
81             MtpStorageInfo info = mStorageList.get(position);
82             if (info != null) {
83                 view.setText(info.getDescription());
84             } else {
85                 view.setText("???");
86             }
87             return view;
88         }
89     }
90 
91     @Override
onCreate(Bundle savedInstanceState)92     protected void onCreate(Bundle savedInstanceState) {
93         super.onCreate(savedInstanceState);
94 
95         mClient = ((CameraBrowserApplication)getApplication()).getMtpClient();
96         mDeviceName = getIntent().getStringExtra("device");
97         mDisconnectedReceiver = new DeviceDisconnectedReceiver(this, mDeviceName);
98     }
99 
100     @Override
onResume()101     protected void onResume() {
102         super.onResume();
103         mStorageList = mClient.getStorageList(mDeviceName);
104         setListAdapter(new StorageAdapter(this));
105     }
106 
107     @Override
onDestroy()108     protected void onDestroy() {
109         unregisterReceiver(mDisconnectedReceiver);
110         super.onDestroy();
111     }
112 
113     @Override
onListItemClick(ListView l, View v, int position, long id)114     protected void onListItemClick(ListView l, View v, int position, long id) {
115         Intent intent = new Intent(this, ObjectBrowser.class);
116         intent.putExtra("device", mDeviceName);
117         intent.putExtra("storage", mStorageList.get(position).getStorageId());
118         startActivity(intent);
119     }
120 }
121