1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package com.android.camerabrowser; 17 18 import android.app.Activity; 19 import android.content.ActivityNotFoundException; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.graphics.Bitmap; 23 import android.graphics.BitmapFactory; 24 import android.media.MediaScannerConnection; 25 import android.media.MediaScannerConnection.MediaScannerConnectionClient; 26 import android.mtp.MtpConstants; 27 import android.mtp.MtpObjectInfo; 28 import android.net.Uri; 29 import android.os.Bundle; 30 import android.os.Environment; 31 import android.util.Log; 32 import android.view.View; 33 import android.widget.Button; 34 import android.widget.ImageView; 35 import android.widget.TextView; 36 import android.widget.Toast; 37 38 import java.io.File; 39 import java.util.Date; 40 import java.util.Locale; 41 42 /** 43 * A view to display the properties of an object. 44 */ 45 public class ObjectViewer extends Activity implements View.OnClickListener { 46 47 private static final String TAG = "ObjectViewer"; 48 49 private MtpClient mClient; 50 private String mDeviceName; 51 private int mStorageID; 52 private int mObjectID; 53 private String mFileName; 54 private Button mImportButton; 55 private Button mDeleteButton; 56 private DeviceDisconnectedReceiver mDisconnectedReceiver; 57 58 private final class ScannerClient implements MediaScannerConnectionClient { 59 private final Context mContext; 60 private String mPath; 61 ScannerClient(Context context)62 public ScannerClient(Context context) { 63 mContext = context; 64 } 65 setScanPath(String path)66 public void setScanPath(String path) { 67 mPath = path; 68 } 69 70 @Override onMediaScannerConnected()71 public void onMediaScannerConnected() { 72 mScannerConnection.scanFile(mPath, null); 73 } 74 75 @Override onScanCompleted(String path, Uri uri)76 public void onScanCompleted(String path, Uri uri) { 77 mScannerConnection.disconnect(); 78 79 // try to start an activity to view the file 80 Intent intent = new Intent(Intent.ACTION_VIEW, uri); 81 try { 82 mContext.startActivity(intent); 83 } catch (ActivityNotFoundException e) { 84 Toast.makeText(mContext, R.string.start_activity_failed_message, 85 Toast.LENGTH_SHORT).show(); 86 } 87 } 88 } 89 90 private MediaScannerConnection mScannerConnection; 91 private ScannerClient mScannerClient; 92 93 @Override onCreate(Bundle savedInstanceState)94 protected void onCreate(Bundle savedInstanceState) { 95 super.onCreate(savedInstanceState); 96 97 mClient = ((CameraBrowserApplication)getApplication()).getMtpClient(); 98 99 setContentView(R.layout.object_info); 100 101 mImportButton = (Button)findViewById(R.id.import_button); 102 mImportButton.setOnClickListener(this); 103 mDeleteButton = (Button)findViewById(R.id.delete_button); 104 mDeleteButton.setOnClickListener(this); 105 106 mDeviceName = getIntent().getStringExtra("device"); 107 mStorageID = getIntent().getIntExtra("storage", 0); 108 mObjectID = getIntent().getIntExtra("object", 0); 109 mDisconnectedReceiver = new DeviceDisconnectedReceiver(this, mDeviceName); 110 mScannerClient = new ScannerClient(this); 111 mScannerConnection = new MediaScannerConnection(this, mScannerClient); 112 } 113 114 @Override onResume()115 protected void onResume() { 116 super.onResume(); 117 118 MtpObjectInfo info = mClient.getObjectInfo(mDeviceName, mObjectID); 119 if (info != null) { 120 TextView view = (TextView)findViewById(R.id.name); 121 mFileName = info.getName(); 122 view.setText(mFileName); 123 view = (TextView)findViewById(R.id.format); 124 view.setText(Integer.toHexString(info.getFormat()).toUpperCase(Locale.ROOT)); 125 view = (TextView)findViewById(R.id.size); 126 view.setText(Long.toString(info.getCompressedSize())); 127 view = (TextView)findViewById(R.id.thumb_width); 128 view.setText(Long.toString(info.getThumbPixWidth())); 129 view = (TextView)findViewById(R.id.thumb_height); 130 view.setText(Long.toString(info.getThumbPixHeight())); 131 view = (TextView)findViewById(R.id.thumb_size); 132 view.setText(Long.toString(info.getThumbCompressedSize())); 133 view = (TextView)findViewById(R.id.width); 134 view.setText(Long.toString(info.getImagePixWidth())); 135 view = (TextView)findViewById(R.id.height); 136 view.setText(Long.toString(info.getImagePixHeight())); 137 view = (TextView)findViewById(R.id.depth); 138 view.setText(Long.toString(info.getImagePixDepth())); 139 view = (TextView)findViewById(R.id.sequence); 140 view.setText(Long.toString(info.getSequenceNumber())); 141 view = (TextView)findViewById(R.id.created); 142 Date date = new Date(info.getDateCreated() * 1000); 143 view.setText(date.toString()); 144 view = (TextView)findViewById(R.id.modified); 145 date = new Date(info.getDateModified() * 1000); 146 view.setText(date.toString()); 147 view = (TextView)findViewById(R.id.keywords); 148 view.setText(info.getKeywords()); 149 int thumbFormat = info.getThumbFormat(); 150 if (thumbFormat == MtpConstants.FORMAT_EXIF_JPEG 151 || thumbFormat == MtpConstants.FORMAT_JFIF) { 152 byte[] thumbnail = mClient.getThumbnail(mDeviceName, info.getObjectHandle()); 153 if (thumbnail != null) { 154 Bitmap bitmap = BitmapFactory.decodeByteArray(thumbnail, 0, thumbnail.length); 155 if (bitmap != null) { 156 ImageView thumbView = (ImageView)findViewById(R.id.thumbnail); 157 thumbView.setImageBitmap(bitmap); 158 } 159 } 160 } 161 } 162 } 163 164 @Override onDestroy()165 protected void onDestroy() { 166 unregisterReceiver(mDisconnectedReceiver); 167 super.onDestroy(); 168 } 169 importObject()170 private void importObject() { 171 // copy file to /mnt/sdcard/imported/<filename> 172 File dest = Environment.getExternalStorageDirectory(); 173 dest = new File(dest, "imported"); 174 dest.mkdirs(); 175 dest = new File(dest, mFileName); 176 177 if (mClient.importFile(mDeviceName, mObjectID, dest.getAbsolutePath())) { 178 Toast.makeText(this, R.string.object_saved_message, Toast.LENGTH_SHORT).show(); 179 180 mScannerClient.setScanPath(dest.getAbsolutePath()); 181 mScannerConnection.connect(); 182 } else { 183 Toast.makeText(this, R.string.save_failed_message, Toast.LENGTH_SHORT).show(); 184 } 185 } 186 deleteObject()187 private void deleteObject() { 188 if (mClient.deleteObject(mDeviceName, mObjectID)) { 189 Toast.makeText(this, R.string.object_deleted_message, Toast.LENGTH_SHORT).show(); 190 finish(); 191 } else { 192 Toast.makeText(this, R.string.delete_failed_message, Toast.LENGTH_SHORT).show(); 193 } 194 } 195 onClick(View v)196 public void onClick(View v) { 197 if (v == mImportButton) { 198 importObject(); 199 } else if (v == mDeleteButton) { 200 deleteObject(); 201 } 202 } 203 } 204