Home | History | Annotate | Download | only in opp
      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 android.content.ContentResolver;
     36 import android.content.Context;
     37 import android.content.res.AssetFileDescriptor;
     38 import android.database.Cursor;
     39 import android.database.sqlite.SQLiteException;
     40 import android.net.Uri;
     41 import android.provider.OpenableColumns;
     42 import android.util.Log;
     43 
     44 import java.io.File;
     45 import java.io.FileInputStream;
     46 import java.io.FileNotFoundException;
     47 import java.io.IOException;
     48 
     49 /**
     50  * This class stores information about a single sending file It will only be
     51  * used for outbound share.
     52  */
     53 public class BluetoothOppSendFileInfo {
     54     private static final String TAG = "BluetoothOppSendFileInfo";
     55 
     56     private static final boolean D = Constants.DEBUG;
     57 
     58     private static final boolean V = Constants.VERBOSE;
     59 
     60     /** Reusable SendFileInfo for error status. */
     61     static final BluetoothOppSendFileInfo SEND_FILE_INFO_ERROR = new BluetoothOppSendFileInfo(
     62             null, null, 0, null, BluetoothShare.STATUS_FILE_ERROR);
     63 
     64     /** readable media file name */
     65     public final String mFileName;
     66 
     67     /** media file input stream */
     68     public final FileInputStream mInputStream;
     69 
     70     /** vCard string data */
     71     public final String mData;
     72 
     73     public final int mStatus;
     74 
     75     public final String mMimetype;
     76 
     77     public final long mLength;
     78 
     79     /** for media file */
     80     public BluetoothOppSendFileInfo(String fileName, String type, long length,
     81             FileInputStream inputStream, int status) {
     82         mFileName = fileName;
     83         mMimetype = type;
     84         mLength = length;
     85         mInputStream = inputStream;
     86         mStatus = status;
     87         mData = null;
     88     }
     89 
     90     /** for vCard, or later for vCal, vNote. Not used currently */
     91     public BluetoothOppSendFileInfo(String data, String type, long length, int status) {
     92         mFileName = null;
     93         mInputStream = null;
     94         mData = data;
     95         mMimetype = type;
     96         mLength = length;
     97         mStatus = status;
     98     }
     99 
    100     public static BluetoothOppSendFileInfo generateFileInfo(Context context, Uri uri,
    101             String type) {
    102         ContentResolver contentResolver = context.getContentResolver();
    103         String scheme = uri.getScheme();
    104         String fileName = null;
    105         String contentType;
    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 ("content".equals(scheme)) {
    111             contentType = contentResolver.getType(uri);
    112             Cursor metadataCursor;
    113             try {
    114                 metadataCursor = contentResolver.query(uri, new String[] {
    115                         OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE
    116                 }, null, null, null);
    117             } catch (SQLiteException e) {
    118                 // some content providers don't support the DISPLAY_NAME or SIZE columns
    119                 metadataCursor = null;
    120             }
    121             if (metadataCursor != null) {
    122                 try {
    123                     if (metadataCursor.moveToFirst()) {
    124                         fileName = metadataCursor.getString(0);
    125                         length = metadataCursor.getInt(1);
    126                         if (D) Log.d(TAG, "fileName = " + fileName + " length = " + length);
    127                     }
    128                 } finally {
    129                     metadataCursor.close();
    130                 }
    131             }
    132             if (fileName == null) {
    133                 // use last segment of URI if DISPLAY_NAME query fails
    134                 fileName = uri.getLastPathSegment();
    135             }
    136         } else if ("file".equals(scheme)) {
    137             fileName = uri.getLastPathSegment();
    138             contentType = type;
    139             File f = new File(uri.getPath());
    140             length = f.length();
    141         } else {
    142             // currently don't accept other scheme
    143             return SEND_FILE_INFO_ERROR;
    144         }
    145         FileInputStream is = null;
    146         if (scheme.equals("content")) {
    147             try {
    148                 // We've found that content providers don't always have the
    149                 // right size in _OpenableColumns.SIZE
    150                 // As a second source of getting the correct file length,
    151                 // get a file descriptor and get the stat length
    152                 AssetFileDescriptor fd = contentResolver.openAssetFileDescriptor(uri, "r");
    153                 long statLength = fd.getLength();
    154                 if (length != statLength && statLength > 0) {
    155                     Log.e(TAG, "Content provider length is wrong (" + Long.toString(length) +
    156                             "), using stat length (" + Long.toString(statLength) + ")");
    157                     length = statLength;
    158                 }
    159                 try {
    160                     // This creates an auto-closing input-stream, so
    161                     // the file descriptor will be closed whenever the InputStream
    162                     // is closed.
    163                     is = fd.createInputStream();
    164                 } catch (IOException e) {
    165                     try {
    166                         fd.close();
    167                     } catch (IOException e2) {
    168                         // Ignore
    169                     }
    170                 }
    171             } catch (FileNotFoundException e) {
    172                 // Ignore
    173             }
    174         }
    175         if (is == null) {
    176             try {
    177                 is = (FileInputStream) contentResolver.openInputStream(uri);
    178             } catch (FileNotFoundException e) {
    179                 return SEND_FILE_INFO_ERROR;
    180             }
    181         }
    182         // If we can not get file length from content provider, we can try to
    183         // get the length via the opened stream.
    184         if (length == 0) {
    185             try {
    186                 length = is.available();
    187                 if (V) Log.v(TAG, "file length is " + length);
    188             } catch (IOException e) {
    189                 Log.e(TAG, "Read stream exception: ", e);
    190                 return SEND_FILE_INFO_ERROR;
    191             }
    192         }
    193 
    194         return new BluetoothOppSendFileInfo(fileName, contentType, length, is, 0);
    195     }
    196 }
    197