Home | History | Annotate | Download | only in camerabrowser
      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.database.Cursor;
     23 import android.graphics.Bitmap;
     24 import android.graphics.BitmapFactory;
     25 import android.mtp.MtpConstants;
     26 import android.mtp.MtpDevice;
     27 import android.mtp.MtpObjectInfo;
     28 import android.os.Bundle;
     29 import android.util.Log;
     30 import android.view.LayoutInflater;
     31 import android.view.View;
     32 import android.view.ViewGroup;
     33 import android.widget.BaseAdapter;
     34 import android.widget.ImageView;
     35 import android.widget.ListView;
     36 import android.widget.TextView;
     37 
     38 import java.util.List;
     39 
     40  /**
     41  * A list view displaying all objects within a container (folder or storage unit).
     42  */
     43 public 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