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