ObjectViewer.java revision f1f5361e328c13be80e3760d8cc2170d70c9d0ba
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 16package com.android.camerabrowser; 17 18import android.app.Activity; 19import android.content.Intent; 20import android.database.Cursor; 21import android.graphics.Bitmap; 22import android.graphics.BitmapFactory; 23import android.net.Uri; 24import android.os.Bundle; 25import android.os.Environment; 26import android.os.FileUtils; 27import android.os.ParcelFileDescriptor; 28import android.os.Process; 29import android.provider.Mtp; 30import android.util.Log; 31import android.view.Menu; 32import android.view.MenuInflater; 33import android.view.MenuItem; 34import android.view.View; 35import android.widget.ImageView; 36import android.widget.TextView; 37import android.widget.Toast; 38 39import java.io.File; 40import java.io.FileDescriptor; 41import java.io.FileInputStream; 42import java.io.FileOutputStream; 43import java.util.Calendar; 44import java.util.Date; 45 46/** 47 * A view to display the properties of an object. 48 */ 49public class ObjectViewer extends Activity { 50 51 private static final String TAG = "ObjectViewer"; 52 53 private int mDeviceID; 54 private long mStorageID; 55 private long mObjectID; 56 57 private static final String[] OBJECT_COLUMNS = 58 new String[] { Mtp.Object._ID, 59 Mtp.Object.NAME, 60 Mtp.Object.SIZE, 61 Mtp.Object.THUMB_WIDTH, 62 Mtp.Object.THUMB_HEIGHT, 63 Mtp.Object.THUMB_SIZE, 64 Mtp.Object.IMAGE_WIDTH, 65 Mtp.Object.IMAGE_HEIGHT, 66 Mtp.Object.IMAGE_DEPTH, 67 Mtp.Object.SEQUENCE_NUMBER, 68 Mtp.Object.DATE_CREATED, 69 Mtp.Object.DATE_MODIFIED, 70 Mtp.Object.KEYWORDS, 71 Mtp.Object.THUMB, 72 Mtp.Object.FORMAT, 73 }; 74 75 @Override 76 protected void onCreate(Bundle savedInstanceState) { 77 super.onCreate(savedInstanceState); 78 79 setContentView(R.layout.object_info); 80 } 81 82 @Override 83 protected void onResume() { 84 super.onResume(); 85 86 mDeviceID = getIntent().getIntExtra("device", 0); 87 mStorageID = getIntent().getLongExtra("storage", 0); 88 mObjectID = getIntent().getLongExtra("object", 0); 89 90 if (mDeviceID != 0 && mObjectID != 0) { 91 Cursor c = getContentResolver().query( 92 Mtp.Object.getContentUri(mDeviceID, mObjectID), 93 OBJECT_COLUMNS, null, null, null); 94 c.moveToFirst(); 95 TextView view = (TextView)findViewById(R.id.name); 96 view.setText(c.getString(1)); 97 view = (TextView)findViewById(R.id.size); 98 view.setText(Long.toString(c.getLong(2))); 99 view = (TextView)findViewById(R.id.thumb_width); 100 view.setText(Long.toString(c.getLong(3))); 101 view = (TextView)findViewById(R.id.thumb_height); 102 view.setText(Long.toString(c.getLong(4))); 103 view = (TextView)findViewById(R.id.thumb_size); 104 view.setText(Long.toString(c.getLong(5))); 105 view = (TextView)findViewById(R.id.width); 106 view.setText(Long.toString(c.getLong(6))); 107 view = (TextView)findViewById(R.id.height); 108 view.setText(Long.toString(c.getLong(7))); 109 view = (TextView)findViewById(R.id.depth); 110 view.setText(Long.toString(c.getLong(8))); 111 view = (TextView)findViewById(R.id.sequence); 112 view.setText(Long.toString(c.getLong(9))); 113 view = (TextView)findViewById(R.id.created); 114 Date date = new Date(c.getLong(10) * 1000); 115 view.setText(date.toString()); 116 view = (TextView)findViewById(R.id.modified); 117 date = new Date(c.getLong(11) * 1000); 118 view.setText(date.toString()); 119 view = (TextView)findViewById(R.id.keywords); 120 view.setText(c.getString(12)); 121 byte[] thumbnail = c.getBlob(13); 122 if (thumbnail != null) { 123 ImageView thumbView = (ImageView)findViewById(R.id.thumbnail); 124 Bitmap bitmap = BitmapFactory.decodeByteArray(thumbnail, 0, thumbnail.length); 125 if (bitmap != null) { 126 thumbView.setImageBitmap(bitmap); 127 } 128 } 129 view = (TextView)findViewById(R.id.format); 130 view.setText(Long.toHexString(c.getLong(14)).toUpperCase()); 131 } 132 } 133 134 @Override 135 public boolean onCreateOptionsMenu(Menu menu) { 136 MenuInflater inflater = getMenuInflater(); 137 inflater.inflate(R.menu.object_menu, menu); 138 return true; 139 } 140 141 @Override 142 public boolean onPrepareOptionsMenu(Menu menu) { 143 MenuItem item = menu.findItem(R.id.save); 144 item.setEnabled(true); 145 item = menu.findItem(R.id.delete); 146 item.setEnabled(true); 147 return true; 148 } 149 150 @Override 151 public boolean onOptionsItemSelected(MenuItem item) { 152 switch (item.getItemId()) { 153 case R.id.save: 154 save(); 155 return true; 156 case R.id.delete: 157 delete(); 158 return true; 159 } 160 return false; 161 } 162 163 private static String getTimestamp() { 164 Calendar c = Calendar.getInstance(); 165 c.setTimeInMillis(System.currentTimeMillis()); 166 return String.format("%tY-%tm-%td-%tH-%tM-%tS", c, c, c, c, c, c); 167 } 168 169 private void save() { 170 boolean success = false; 171 Uri uri = Mtp.Object.getContentUri(mDeviceID, mObjectID); 172 File destFile = null; 173 ParcelFileDescriptor pfd = null; 174 FileInputStream fis = null; 175 FileOutputStream fos = null; 176 177 try { 178 pfd = getContentResolver().openFileDescriptor(uri, "r"); 179 Log.d(TAG, "save got pfd " + pfd); 180 if (pfd != null) { 181 fis = new FileInputStream(pfd.getFileDescriptor()); 182 Log.d(TAG, "save got fis " + fis); 183 File destDir = Environment.getExternalStoragePublicDirectory( 184 Environment.DIRECTORY_DCIM); 185 destDir.mkdirs(); 186 destFile = new File(destDir, "CameraBrowser-" + getTimestamp() + ".jpeg"); 187 188 189 Log.d(TAG, "save got destFile " + destFile); 190 191 if (destFile.exists()) { 192 destFile.delete(); 193 } 194 fos = new FileOutputStream(destFile); 195 196 byte[] buffer = new byte[65536]; 197 int bytesRead; 198 while ((bytesRead = fis.read(buffer)) >= 0) { 199 fos.write(buffer, 0, bytesRead); 200 } 201 202 // temporary workaround until we straighten out permissions in /data/media 203 // 1015 is AID_SDCARD_RW 204 FileUtils.setPermissions(destDir.getPath(), 0775, Process.myUid(), 1015); 205 FileUtils.setPermissions(destFile.getPath(), 0664, Process.myUid(), 1015); 206 207 success = true; 208 } 209 } catch (Exception e) { 210 Log.e(TAG, "Exception in ObjectView.save", e); 211 } finally { 212 if (fis != null) { 213 try { 214 fis.close(); 215 } catch (Exception e) { 216 } 217 } 218 if (fos != null) { 219 try { 220 fos.close(); 221 } catch (Exception e) { 222 } 223 } 224 if (pfd != null) { 225 try { 226 pfd.close(); 227 } catch (Exception e) { 228 } 229 } 230 } 231 232 if (success) { 233 Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 234 intent.setData(Uri.fromFile(destFile)); 235 sendBroadcast(intent); 236 Toast.makeText(this, R.string.object_saved_message, Toast.LENGTH_SHORT).show(); 237 } else { 238 Toast.makeText(this, R.string.save_failed_message, Toast.LENGTH_SHORT).show(); 239 } 240 } 241 242 private void delete() { 243 Uri uri = Mtp.Object.getContentUri(mDeviceID, mObjectID); 244 245 Log.d(TAG, "deleting " + uri); 246 247 int result = getContentResolver().delete(uri, null, null); 248 if (result > 0) { 249 Toast.makeText(this, R.string.object_deleted_message, Toast.LENGTH_SHORT).show(); 250 finish(); 251 } else { 252 Toast.makeText(this, R.string.delete_failed_message, Toast.LENGTH_SHORT).show(); 253 } 254 } 255} 256