BluetoothOppSendFileInfo.java revision 151f41949621e082e127fe468bedd28289bc3a19
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.IOException; 37import java.io.FileInputStream; 38import java.io.FileNotFoundException; 39 40import android.util.Log; 41import android.content.ContentResolver; 42import android.content.Context; 43import android.database.Cursor; 44import android.net.Uri; 45import android.provider.OpenableColumns; 46import android.provider.ContactsContract.Contacts; 47 48/** 49 * This class stores information about a single sending file It will only be 50 * used for outbound share. 51 */ 52public class BluetoothOppSendFileInfo { 53 private static final String TAG = "BluetoothOppSendFileInfo"; 54 55 private static final boolean D = Constants.DEBUG; 56 57 private static final boolean V = Constants.VERBOSE; 58 59 /** readable media file name */ 60 public final String mFileName; 61 62 /** media file input stream */ 63 public final FileInputStream mInputStream; 64 65 /** vCard string data */ 66 public final String mData; 67 68 public final int mStatus; 69 70 public final String mMimetype; 71 72 public final long mLength; 73 74 public final String mDestAddr; 75 76 /** for media file */ 77 public BluetoothOppSendFileInfo(String fileName, String type, long length, 78 FileInputStream inputStream, int status, String dest) { 79 mFileName = fileName; 80 mMimetype = type; 81 mLength = length; 82 mInputStream = inputStream; 83 mStatus = status; 84 mDestAddr = dest; 85 mData = null; 86 } 87 88 /** for vCard, or later for vCal, vNote. Not used currently */ 89 public BluetoothOppSendFileInfo(String data, String type, long length, int status, 90 String dest) { 91 mFileName = null; 92 mInputStream = null; 93 mData = data; 94 mMimetype = type; 95 mLength = length; 96 mStatus = status; 97 mDestAddr = dest; 98 } 99 100 public static BluetoothOppSendFileInfo generateFileInfo(Context context, String uri, 101 String type, String dest) { 102 ContentResolver contentResolver = context.getContentResolver(); 103 Uri u = Uri.parse(uri); 104 String scheme = u.getScheme(); 105 String fileName = null; 106 String contentType = null; 107 long length = 0; 108 // Support all Uri with "content" scheme 109 // This will allow more 3rd party applications to share files via 110 // bluetooth 111 if (scheme.equals("content")) { 112 contentType = contentResolver.getType(u); 113 Cursor metadataCursor = contentResolver.query(u, new String[] { 114 OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE 115 }, null, null, null); 116 if (metadataCursor != null) { 117 try { 118 if (metadataCursor.moveToFirst()) { 119 fileName = metadataCursor.getString(0); 120 length = metadataCursor.getInt(1); 121 if (D) Log.d(TAG, "fileName = " + fileName + " length = " + length); 122 } 123 } finally { 124 metadataCursor.close(); 125 } 126 } 127 } else if (scheme.equals("file")) { 128 fileName = u.getLastPathSegment(); 129 contentType = type; 130 File f = new File(u.getPath()); 131 length = f.length(); 132 } else { 133 // currently don't accept other scheme 134 return new BluetoothOppSendFileInfo(null, null, 0, null, 135 BluetoothShare.STATUS_FILE_ERROR, dest); 136 } 137 FileInputStream is; 138 try { 139 is = (FileInputStream)contentResolver.openInputStream(u); 140 } catch (FileNotFoundException e) { 141 return new BluetoothOppSendFileInfo(null, null, 0, null, 142 BluetoothShare.STATUS_FILE_ERROR, dest); 143 } 144 145 // If we can not get file length from content provider, we can try to 146 // get the length via the opened stream. 147 if (length == 0) { 148 try { 149 length = is.available(); 150 if (V) Log.v(TAG, "file length is " + length); 151 } catch (IOException e) { 152 Log.e(TAG, "Read stream exception: ", e); 153 return new BluetoothOppSendFileInfo(null, null, 0, null, 154 BluetoothShare.STATUS_FILE_ERROR, dest); 155 } 156 } 157 158 return new BluetoothOppSendFileInfo(fileName, contentType, length, is, 0, dest); 159 } 160} 161