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