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.app.NotificationManager;
     36 import android.bluetooth.BluetoothAdapter;
     37 import android.bluetooth.BluetoothDevice;
     38 import android.content.ActivityNotFoundException;
     39 import android.content.ContentResolver;
     40 import android.content.ContentValues;
     41 import android.content.Context;
     42 import android.content.Intent;
     43 import android.content.pm.PackageManager;
     44 import android.content.pm.ResolveInfo;
     45 import android.database.Cursor;
     46 import android.net.Uri;
     47 import android.os.Environment;
     48 import android.util.Log;
     49 
     50 import com.android.bluetooth.R;
     51 
     52 import com.google.android.collect.Lists;
     53 
     54 import java.io.File;
     55 import java.io.IOException;
     56 import java.math.RoundingMode;
     57 import java.text.DecimalFormat;
     58 import java.util.ArrayList;
     59 import java.util.List;
     60 import java.util.concurrent.ConcurrentHashMap;
     61 
     62 /**
     63  * This class has some utilities for Opp application;
     64  */
     65 public class BluetoothOppUtility {
     66     private static final String TAG = "BluetoothOppUtility";
     67     private static final boolean D = Constants.DEBUG;
     68     private static final boolean V = Constants.VERBOSE;
     69 
     70     private static final ConcurrentHashMap<Uri, BluetoothOppSendFileInfo> sSendFileMap =
     71             new ConcurrentHashMap<Uri, BluetoothOppSendFileInfo>();
     72 
     73     public static boolean isBluetoothShareUri(Uri uri) {
     74         return uri.toString().startsWith(BluetoothShare.CONTENT_URI.toString());
     75     }
     76 
     77     public static BluetoothOppTransferInfo queryRecord(Context context, Uri uri) {
     78         BluetoothOppTransferInfo info = new BluetoothOppTransferInfo();
     79         Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
     80         if (cursor != null) {
     81             if (cursor.moveToFirst()) {
     82                 fillRecord(context, cursor, info);
     83             }
     84             cursor.close();
     85         } else {
     86             info = null;
     87             if (V) {
     88                 Log.v(TAG, "BluetoothOppManager Error: not got data from db for uri:" + uri);
     89             }
     90         }
     91         return info;
     92     }
     93 
     94     public static void fillRecord(Context context, Cursor cursor, BluetoothOppTransferInfo info) {
     95         BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
     96         info.mID = cursor.getInt(cursor.getColumnIndexOrThrow(BluetoothShare._ID));
     97         info.mStatus = cursor.getInt(cursor.getColumnIndexOrThrow(BluetoothShare.STATUS));
     98         info.mDirection = cursor.getInt(cursor.getColumnIndexOrThrow(BluetoothShare.DIRECTION));
     99         info.mTotalBytes = cursor.getLong(cursor.getColumnIndexOrThrow(BluetoothShare.TOTAL_BYTES));
    100         info.mCurrentBytes =
    101                 cursor.getLong(cursor.getColumnIndexOrThrow(BluetoothShare.CURRENT_BYTES));
    102         info.mTimeStamp = cursor.getLong(cursor.getColumnIndexOrThrow(BluetoothShare.TIMESTAMP));
    103         info.mDestAddr = cursor.getString(cursor.getColumnIndexOrThrow(BluetoothShare.DESTINATION));
    104 
    105         info.mFileName = cursor.getString(cursor.getColumnIndexOrThrow(BluetoothShare._DATA));
    106         if (info.mFileName == null) {
    107             info.mFileName =
    108                     cursor.getString(cursor.getColumnIndexOrThrow(BluetoothShare.FILENAME_HINT));
    109         }
    110         if (info.mFileName == null) {
    111             info.mFileName = context.getString(R.string.unknown_file);
    112         }
    113 
    114         info.mFileUri = cursor.getString(cursor.getColumnIndexOrThrow(BluetoothShare.URI));
    115 
    116         if (info.mFileUri != null) {
    117             Uri u = Uri.parse(info.mFileUri);
    118             info.mFileType = context.getContentResolver().getType(u);
    119         } else {
    120             Uri u = Uri.parse(info.mFileName);
    121             info.mFileType = context.getContentResolver().getType(u);
    122         }
    123         if (info.mFileType == null) {
    124             info.mFileType =
    125                     cursor.getString(cursor.getColumnIndexOrThrow(BluetoothShare.MIMETYPE));
    126         }
    127 
    128         BluetoothDevice remoteDevice = adapter.getRemoteDevice(info.mDestAddr);
    129         info.mDeviceName = BluetoothOppManager.getInstance(context).getDeviceName(remoteDevice);
    130 
    131         int confirmationType =
    132                 cursor.getInt(cursor.getColumnIndexOrThrow(BluetoothShare.USER_CONFIRMATION));
    133         info.mHandoverInitiated =
    134                 confirmationType == BluetoothShare.USER_CONFIRMATION_HANDOVER_CONFIRMED;
    135 
    136         if (V) {
    137             Log.v(TAG, "Get data from db:" + info.mFileName + info.mFileType + info.mDestAddr);
    138         }
    139     }
    140 
    141     /**
    142      * Organize Array list for transfers in one batch
    143      */
    144     // This function is used when UI show batch transfer. Currently only show single transfer.
    145     public static ArrayList<String> queryTransfersInBatch(Context context, Long timeStamp) {
    146         ArrayList<String> uris = Lists.newArrayList();
    147         final String where = BluetoothShare.TIMESTAMP + " == " + timeStamp;
    148         Cursor metadataCursor =
    149                 context.getContentResolver().query(BluetoothShare.CONTENT_URI, new String[]{
    150                         BluetoothShare._DATA
    151                 }, where, null, BluetoothShare._ID);
    152 
    153         if (metadataCursor == null) {
    154             return null;
    155         }
    156 
    157         for (metadataCursor.moveToFirst(); !metadataCursor.isAfterLast();
    158                 metadataCursor.moveToNext()) {
    159             String fileName = metadataCursor.getString(0);
    160             Uri path = Uri.parse(fileName);
    161             // If there is no scheme, then it must be a file
    162             if (path.getScheme() == null) {
    163                 path = Uri.fromFile(new File(fileName));
    164             }
    165             uris.add(path.toString());
    166             if (V) {
    167                 Log.d(TAG, "Uri in this batch: " + path.toString());
    168             }
    169         }
    170         metadataCursor.close();
    171         return uris;
    172     }
    173 
    174     /**
    175      * Open the received file with appropriate application, if can not find
    176      * application to handle, display error dialog.
    177      */
    178     public static void openReceivedFile(Context context, String fileName, String mimetype,
    179             Long timeStamp, Uri uri) {
    180         if (fileName == null || mimetype == null) {
    181             Log.e(TAG, "ERROR: Para fileName ==null, or mimetype == null");
    182             return;
    183         }
    184 
    185         if (!isBluetoothShareUri(uri)) {
    186             Log.e(TAG, "Trying to open a file that wasn't transfered over Bluetooth");
    187             return;
    188         }
    189 
    190         File f = new File(fileName);
    191         if (!f.exists()) {
    192             Intent in = new Intent(context, BluetoothOppBtErrorActivity.class);
    193             in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    194             in.putExtra("title", context.getString(R.string.not_exist_file));
    195             in.putExtra("content", context.getString(R.string.not_exist_file_desc));
    196             context.startActivity(in);
    197 
    198             // Due to the file is not existing, delete related info in btopp db
    199             // to prevent this file from appearing in live folder
    200             if (V) {
    201                 Log.d(TAG, "This uri will be deleted: " + uri);
    202             }
    203             context.getContentResolver().delete(uri, null, null);
    204             return;
    205         }
    206 
    207         Uri path = BluetoothOppFileProvider.getUriForFile(context,
    208                 "com.android.bluetooth.opp.fileprovider", f);
    209         if (path == null) {
    210             Log.w(TAG, "Cannot get content URI for the shared file");
    211             return;
    212         }
    213         // If there is no scheme, then it must be a file
    214         if (path.getScheme() == null) {
    215             path = Uri.fromFile(new File(fileName));
    216         }
    217 
    218         if (isRecognizedFileType(context, path, mimetype)) {
    219             Intent activityIntent = new Intent(Intent.ACTION_VIEW);
    220             activityIntent.setDataAndTypeAndNormalize(path, mimetype);
    221 
    222             List<ResolveInfo> resInfoList = context.getPackageManager()
    223                     .queryIntentActivities(activityIntent, PackageManager.MATCH_DEFAULT_ONLY);
    224 
    225             activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    226             activityIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    227 
    228             try {
    229                 if (V) {
    230                     Log.d(TAG, "ACTION_VIEW intent sent out: " + path + " / " + mimetype);
    231                 }
    232                 context.startActivity(activityIntent);
    233             } catch (ActivityNotFoundException ex) {
    234                 if (V) {
    235                     Log.d(TAG, "no activity for handling ACTION_VIEW intent:  " + mimetype, ex);
    236                 }
    237             }
    238         } else {
    239             Intent in = new Intent(context, BluetoothOppBtErrorActivity.class);
    240             in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    241             in.putExtra("title", context.getString(R.string.unknown_file));
    242             in.putExtra("content", context.getString(R.string.unknown_file_desc));
    243             context.startActivity(in);
    244         }
    245     }
    246 
    247     /**
    248      * To judge if the file type supported (can be handled by some app) by phone
    249      * system.
    250      */
    251     public static boolean isRecognizedFileType(Context context, Uri fileUri, String mimetype) {
    252         boolean ret = true;
    253 
    254         if (D) {
    255             Log.d(TAG, "RecognizedFileType() fileUri: " + fileUri + " mimetype: " + mimetype);
    256         }
    257 
    258         Intent mimetypeIntent = new Intent(Intent.ACTION_VIEW);
    259         mimetypeIntent.setDataAndTypeAndNormalize(fileUri, mimetype);
    260         List<ResolveInfo> list = context.getPackageManager()
    261                 .queryIntentActivities(mimetypeIntent, PackageManager.MATCH_DEFAULT_ONLY);
    262 
    263         if (list.size() == 0) {
    264             if (D) {
    265                 Log.d(TAG, "NO application to handle MIME type " + mimetype);
    266             }
    267             ret = false;
    268         }
    269         return ret;
    270     }
    271 
    272     /**
    273      * update visibility to Hidden
    274      */
    275     public static void updateVisibilityToHidden(Context context, Uri uri) {
    276         ContentValues updateValues = new ContentValues();
    277         updateValues.put(BluetoothShare.VISIBILITY, BluetoothShare.VISIBILITY_HIDDEN);
    278         context.getContentResolver().update(uri, updateValues, null, null);
    279     }
    280 
    281     /**
    282      * Helper function to build the progress text.
    283      */
    284     public static String formatProgressText(long totalBytes, long currentBytes) {
    285         DecimalFormat df = new DecimalFormat("0%");
    286         df.setRoundingMode(RoundingMode.DOWN);
    287         double percent = 0.0;
    288         if (totalBytes > 0) {
    289             percent = currentBytes / (double) totalBytes;
    290         }
    291         return df.format(percent);
    292     }
    293 
    294     /**
    295      * Get status description according to status code.
    296      */
    297     public static String getStatusDescription(Context context, int statusCode, String deviceName) {
    298         String ret;
    299         if (statusCode == BluetoothShare.STATUS_PENDING) {
    300             ret = context.getString(R.string.status_pending);
    301         } else if (statusCode == BluetoothShare.STATUS_RUNNING) {
    302             ret = context.getString(R.string.status_running);
    303         } else if (statusCode == BluetoothShare.STATUS_SUCCESS) {
    304             ret = context.getString(R.string.status_success);
    305         } else if (statusCode == BluetoothShare.STATUS_NOT_ACCEPTABLE) {
    306             ret = context.getString(R.string.status_not_accept);
    307         } else if (statusCode == BluetoothShare.STATUS_FORBIDDEN) {
    308             ret = context.getString(R.string.status_forbidden);
    309         } else if (statusCode == BluetoothShare.STATUS_CANCELED) {
    310             ret = context.getString(R.string.status_canceled);
    311         } else if (statusCode == BluetoothShare.STATUS_FILE_ERROR) {
    312             ret = context.getString(R.string.status_file_error);
    313         } else if (statusCode == BluetoothShare.STATUS_ERROR_NO_SDCARD) {
    314             ret = context.getString(R.string.status_no_sd_card);
    315         } else if (statusCode == BluetoothShare.STATUS_CONNECTION_ERROR) {
    316             ret = context.getString(R.string.status_connection_error);
    317         } else if (statusCode == BluetoothShare.STATUS_ERROR_SDCARD_FULL) {
    318             ret = context.getString(R.string.bt_sm_2_1, deviceName);
    319         } else if ((statusCode == BluetoothShare.STATUS_BAD_REQUEST) || (statusCode
    320                 == BluetoothShare.STATUS_LENGTH_REQUIRED) || (statusCode
    321                 == BluetoothShare.STATUS_PRECONDITION_FAILED) || (statusCode
    322                 == BluetoothShare.STATUS_UNHANDLED_OBEX_CODE) || (statusCode
    323                 == BluetoothShare.STATUS_OBEX_DATA_ERROR)) {
    324             ret = context.getString(R.string.status_protocol_error);
    325         } else {
    326             ret = context.getString(R.string.status_unknown_error);
    327         }
    328         return ret;
    329     }
    330 
    331     /**
    332      * Retry the failed transfer: Will insert a new transfer session to db
    333      */
    334     public static void retryTransfer(Context context, BluetoothOppTransferInfo transInfo) {
    335         ContentValues values = new ContentValues();
    336         values.put(BluetoothShare.URI, transInfo.mFileUri);
    337         values.put(BluetoothShare.MIMETYPE, transInfo.mFileType);
    338         values.put(BluetoothShare.DESTINATION, transInfo.mDestAddr);
    339 
    340         final Uri contentUri =
    341                 context.getContentResolver().insert(BluetoothShare.CONTENT_URI, values);
    342         if (V) {
    343             Log.v(TAG,
    344                     "Insert contentUri: " + contentUri + "  to device: " + transInfo.mDeviceName);
    345         }
    346     }
    347 
    348     static Uri originalUri(Uri uri) {
    349         String mUri = uri.toString();
    350         int atIndex = mUri.lastIndexOf("@");
    351         if (atIndex != -1) {
    352             mUri = mUri.substring(0, atIndex);
    353             uri = Uri.parse(mUri);
    354         }
    355         if (V) Log.v(TAG, "originalUri: " + uri);
    356         return uri;
    357     }
    358 
    359     static Uri generateUri(Uri uri, BluetoothOppSendFileInfo sendFileInfo) {
    360         String fileInfo = sendFileInfo.toString();
    361         int atIndex = fileInfo.lastIndexOf("@");
    362         fileInfo = fileInfo.substring(atIndex);
    363         uri = Uri.parse(uri + fileInfo);
    364         if (V) Log.v(TAG, "generateUri: " + uri);
    365         return uri;
    366     }
    367 
    368     static void putSendFileInfo(Uri uri, BluetoothOppSendFileInfo sendFileInfo) {
    369         if (D) {
    370             Log.d(TAG, "putSendFileInfo: uri=" + uri + " sendFileInfo=" + sendFileInfo);
    371         }
    372         if (sendFileInfo == BluetoothOppSendFileInfo.SEND_FILE_INFO_ERROR) {
    373             Log.e(TAG, "putSendFileInfo: bad sendFileInfo, URI: " + uri);
    374         }
    375         sSendFileMap.put(uri, sendFileInfo);
    376     }
    377 
    378     static BluetoothOppSendFileInfo getSendFileInfo(Uri uri) {
    379         if (D) {
    380             Log.d(TAG, "getSendFileInfo: uri=" + uri);
    381         }
    382         BluetoothOppSendFileInfo info = sSendFileMap.get(uri);
    383         return (info != null) ? info : BluetoothOppSendFileInfo.SEND_FILE_INFO_ERROR;
    384     }
    385 
    386     static void closeSendFileInfo(Uri uri) {
    387         if (D) {
    388             Log.d(TAG, "closeSendFileInfo: uri=" + uri);
    389         }
    390         BluetoothOppSendFileInfo info = sSendFileMap.remove(uri);
    391         if (info != null && info.mInputStream != null) {
    392             try {
    393                 info.mInputStream.close();
    394             } catch (IOException ignored) {
    395             }
    396         }
    397     }
    398 
    399     /**
    400      * Checks if the URI is in Environment.getExternalStorageDirectory() as it
    401      * is the only directory that is possibly readable by both the sender and
    402      * the Bluetooth process.
    403      */
    404     static boolean isInExternalStorageDir(Uri uri) {
    405         if (!ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
    406             Log.e(TAG, "Not a file URI: " + uri);
    407             return false;
    408         }
    409         final File file = new File(uri.getCanonicalUri().getPath());
    410         return isSameOrSubDirectory(Environment.getExternalStorageDirectory(), file);
    411     }
    412 
    413     /**
    414      * Checks, whether the child directory is the same as, or a sub-directory of the base
    415      * directory. Neither base nor child should be null.
    416      */
    417     static boolean isSameOrSubDirectory(File base, File child) {
    418         try {
    419             base = base.getCanonicalFile();
    420             child = child.getCanonicalFile();
    421             File parentFile = child;
    422             while (parentFile != null) {
    423                 if (base.equals(parentFile)) {
    424                     return true;
    425                 }
    426                 parentFile = parentFile.getParentFile();
    427             }
    428             return false;
    429         } catch (IOException ex) {
    430             Log.e(TAG, "Error while accessing file", ex);
    431             return false;
    432         }
    433     }
    434 
    435     protected static void cancelNotification(Context ctx) {
    436         NotificationManager nm = (NotificationManager) ctx
    437                 .getSystemService(Context.NOTIFICATION_SERVICE);
    438         nm.cancel(BluetoothOppNotification.NOTIFICATION_ID_PROGRESS);
    439     }
    440 
    441 }
    442