2 * Copyright (c) 2008-2009, Motorola, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
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.
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.
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.
33 package com.android.bluetooth.opp;
36 import java.io.IOException;
37 import java.io.FileInputStream;
38 import java.io.FileNotFoundException;
40 import android.util.Log;
41 import android.content.ContentResolver;
42 import android.content.Context;
43 import android.database.Cursor;
44 import android.net.Uri;
45 import android.provider.OpenableColumns;
46 import android.provider.ContactsContract.Contacts;
49 * This class stores information about a single sending file It will only be
50 * used for outbound share.
52 public class BluetoothOppSendFileInfo {
53 private static final String TAG = "BluetoothOppSendFileInfo";
55 private static final boolean D = Constants.DEBUG;
57 private static final boolean V = Constants.VERBOSE;
59 /** readable media file name */
60 public final String mFileName;
62 /** media file input stream */
63 public final FileInputStream mInputStream;
65 /** vCard string data */
66 public final String mData;
68 public final int mStatus;
70 public final String mMimetype;
72 public final long mLength;
74 public final String mDestAddr;
77 public BluetoothOppSendFileInfo(String fileName, String type, long length,
78 FileInputStream inputStream, int status, String dest) {
82 mInputStream = inputStream;
88 /** for vCard, or later for vCal, vNote. Not used currently */
89 public BluetoothOppSendFileInfo(String data, String type, long length, int status,
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;
108 // Support all Uri with "content" scheme
109 // This will allow more 3rd party applications to share files via
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) {
118 if (metadataCursor.moveToFirst()) {
119 fileName = metadataCursor.getString(0);
120 length = metadataCursor.getInt(1);
121 if (D) Log.d(TAG, "fileName = " + fileName + " length = " + length);
124 metadataCursor.close();
127 } else if (scheme.equals("file")) {
128 fileName = u.getLastPathSegment();
130 File f = new File(u.getPath());
133 // currently don't accept other scheme
134 return new BluetoothOppSendFileInfo(null, null, 0, null,
135 BluetoothShare.STATUS_FILE_ERROR, dest);
139 is = (FileInputStream)contentResolver.openInputStream(u);
140 } catch (FileNotFoundException e) {
141 return new BluetoothOppSendFileInfo(null, null, 0, null,
142 BluetoothShare.STATUS_FILE_ERROR, dest);
145 // If we can not get file length from content provider, we can try to
146 // get the length via the opened stream.
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);
158 return new BluetoothOppSendFileInfo(fileName, contentType, length, is, 0, dest);