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.mtp.MtpConstants; 26import android.mtp.MtpDevice; 27import android.mtp.MtpObjectInfo; 28import android.os.Bundle; 29import android.util.Log; 30import android.view.LayoutInflater; 31import android.view.View; 32import android.view.ViewGroup; 33import android.widget.BaseAdapter; 34import android.widget.ImageView; 35import android.widget.ListView; 36import android.widget.TextView; 37 38import java.util.List; 39 40 /** 41 * A list view displaying all objects within a container (folder or storage unit). 42 */ 43public class ObjectBrowser extends ListActivity { 44 45 private static final String TAG = "ObjectBrowser"; 46 47 private MtpClient mClient; 48 private List<MtpObjectInfo> mObjectList; 49 private String mDeviceName; 50 private int mStorageID; 51 private int mObjectID; 52 private DeviceDisconnectedReceiver mDisconnectedReceiver; 53 54 private class ObjectAdapter extends BaseAdapter { 55 private final Context mContext; 56 private final LayoutInflater mInflater; 57 58 public ObjectAdapter(Context c) { 59 mContext = c; 60 mInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 61 } 62 63 public int getCount() { 64 if (mObjectList == null) { 65 return 0; 66 } else { 67 return mObjectList.size(); 68 } 69 } 70 71 public Object getItem(int position) { 72 return mObjectList.get(position); 73 } 74 75 public long getItemId(int position) { 76 return position; 77 } 78 79 public View getView(int position, View convertView, ViewGroup parent) { 80 View view; 81 if (convertView == null) { 82 view = mInflater.inflate(R.layout.object_list, parent, false); 83 } else { 84 view = convertView; 85 } 86 87 TextView nameView = (TextView)view.findViewById(R.id.name); 88 MtpObjectInfo info = mObjectList.get(position); 89 nameView.setText(info.getName()); 90 91 int thumbFormat = info.getThumbFormat(); 92 if (thumbFormat == MtpConstants.FORMAT_EXIF_JPEG 93 || thumbFormat == MtpConstants.FORMAT_JFIF) { 94 byte[] thumbnail = mClient.getThumbnail(mDeviceName, info.getObjectHandle()); 95 if (thumbnail != null) { 96 Bitmap bitmap = BitmapFactory.decodeByteArray(thumbnail, 0, thumbnail.length); 97 if (bitmap != null) { 98 ImageView thumbView = (ImageView)view.findViewById(R.id.thumbnail); 99 thumbView.setImageBitmap(bitmap); 100 } 101 } 102 } 103 return view; 104 } 105 } 106 107 @Override 108 protected void onCreate(Bundle savedInstanceState) { 109 super.onCreate(savedInstanceState); 110 111 mClient = ((CameraBrowserApplication)getApplication()).getMtpClient(); 112 mDeviceName = getIntent().getStringExtra("device"); 113 mStorageID = getIntent().getIntExtra("storage", 0); 114 mObjectID = getIntent().getIntExtra("object", 0); 115 mDisconnectedReceiver = new DeviceDisconnectedReceiver(this, mDeviceName); 116 } 117 118 @Override 119 protected void onResume() { 120 super.onResume(); 121 122 mObjectList = mClient.getObjectList(mDeviceName, mStorageID, mObjectID); 123 setListAdapter(new ObjectAdapter(this)); 124 } 125 126 @Override 127 protected void onDestroy() { 128 unregisterReceiver(mDisconnectedReceiver); 129 super.onDestroy(); 130 } 131 132 @Override 133 protected void onListItemClick(ListView l, View v, int position, long id) { 134 MtpObjectInfo info = mObjectList.get(position); 135 Intent intent; 136 if (info.getFormat() == MtpConstants.FORMAT_ASSOCIATION) { 137 intent = new Intent(this, ObjectBrowser.class); 138 } else { 139 intent = new Intent(this, ObjectViewer.class); 140 } 141 intent.putExtra("device", mDeviceName); 142 intent.putExtra("storage", mStorageID); 143 intent.putExtra("object", info.getObjectHandle()); 144 startActivity(intent); 145 } 146} 147