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