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