BluetoothOppSendFileInfo.java revision 9f20e7cea068dac8cb58d240969f211ed9263aa0
1/* 2 * Copyright (c) 2008-2009, Motorola, Inc. 3 * 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * - Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * 12 * - Redistributions in binary form must reproduce the above copyright notice, 13 * this list of conditions and the following disclaimer in the documentation 14 * and/or other materials provided with the distribution. 15 * 16 * - Neither the name of the Motorola, Inc. nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33package com.android.bluetooth.opp; 34 35import java.io.File; 36import java.io.FileInputStream; 37import java.io.FileNotFoundException; 38 39import android.content.ContentResolver; 40import android.content.Context; 41import android.database.Cursor; 42import android.net.Uri; 43import android.provider.OpenableColumns; 44 45/** 46 * This class stores information about a single sending file It will only be 47 * used for outbound share. 48 */ 49public class BluetoothOppSendFileInfo { 50 /** readable media file name */ 51 public final String mFileName; 52 53 /** media file input stream */ 54 public final FileInputStream mInputStream; 55 56 /** vCard string data */ 57 public final String mData; 58 59 public final int mStatus; 60 61 public final String mMimetype; 62 63 public final long mLength; 64 65 public final String mDestAddr; 66 67 /** for media file */ 68 public BluetoothOppSendFileInfo(String fileName, String type, long length, 69 FileInputStream inputStream, int status, String dest) { 70 mFileName = fileName; 71 mMimetype = type; 72 mLength = length; 73 mInputStream = inputStream; 74 mStatus = status; 75 mDestAddr = dest; 76 mData = null; 77 } 78 79 /** for vCard, or later for vCal, vNote. Not used currently */ 80 public BluetoothOppSendFileInfo(String data, String type, long length, int status, 81 String dest) { 82 mFileName = null; 83 mInputStream = null; 84 mData = data; 85 mMimetype = type; 86 mLength = length; 87 mStatus = status; 88 mDestAddr = dest; 89 } 90 91 public static BluetoothOppSendFileInfo generateFileInfo(Context context, String uri, 92 String type, String dest) { 93 //TODO consider uri is a file:// uri 94 ContentResolver contentResolver = context.getContentResolver(); 95 Uri u = Uri.parse(uri); 96 String scheme = u.getScheme(); 97 String authority = u.getAuthority(); 98 String fileName = null; 99 String contentType = null; 100 long length = 0; 101 if (scheme.equals("content") && authority.equals("media")) { 102 103 contentType = contentResolver.getType(u); 104 Cursor metadataCursor = contentResolver.query(u, new String[] { 105 OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE 106 }, null, null, null); 107 if (metadataCursor != null) { 108 try { 109 if (metadataCursor.moveToFirst()) { 110 fileName = metadataCursor.getString(0); 111 length = metadataCursor.getInt(1); 112 } 113 } finally { 114 metadataCursor.close(); 115 } 116 } 117 } else if (scheme.equals("file")) { 118 fileName = u.getLastPathSegment(); 119 contentType = type; 120 File f = new File(u.getPath()); 121 length = f.length(); 122 } else { 123 // currently don't accept other scheme 124 return new BluetoothOppSendFileInfo(null, null, 0, null, 125 BluetoothShare.STATUS_FILE_ERROR, dest); 126 } 127 FileInputStream is; 128 try { 129 is = (FileInputStream)contentResolver.openInputStream(u); 130 } catch (FileNotFoundException e) { 131 return new BluetoothOppSendFileInfo(null, null, 0, null, 132 BluetoothShare.STATUS_FILE_ERROR, dest); 133 } 134 return new BluetoothOppSendFileInfo(fileName, contentType, length, is, 0, dest); 135 } 136} 137