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