109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly/* 209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * Copyright (c) 2008-2009, Motorola, Inc. 309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * 409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * All rights reserved. 509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * 609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * Redistribution and use in source and binary forms, with or without 709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * modification, are permitted provided that the following conditions are met: 809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * 909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * - Redistributions of source code must retain the above copyright notice, 1009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * this list of conditions and the following disclaimer. 1109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * 1209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * - Redistributions in binary form must reproduce the above copyright notice, 1309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * this list of conditions and the following disclaimer in the documentation 1409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * and/or other materials provided with the distribution. 1509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * 1609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * - Neither the name of the Motorola, Inc. nor the names of its contributors 1709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * may be used to endorse or promote products derived from this software 1809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * without specific prior written permission. 1909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * 2009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 2109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 2409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * POSSIBILITY OF SUCH DAMAGE. 3109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly */ 3209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly 3309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pellypackage com.android.bluetooth.opp; 3409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly 356769b59d715ea98bd72eafcfea9acd2714a887daTao Liejunimport com.android.bluetooth.R; 366769b59d715ea98bd72eafcfea9acd2714a887daTao Liejunimport com.google.android.collect.Lists; 376769b59d715ea98bd72eafcfea9acd2714a887daTao Liejun 3841ef8d494511c040451f2f887cb31c3100746b61Nick Pelly 3941ef8d494511c040451f2f887cb31c3100746b61Nick Pellyimport android.bluetooth.BluetoothAdapter; 4041ef8d494511c040451f2f887cb31c3100746b61Nick Pellyimport android.bluetooth.BluetoothDevice; 4109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pellyimport android.net.Uri; 4209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pellyimport android.content.ContentValues; 4309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pellyimport android.content.Context; 4409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pellyimport android.content.ActivityNotFoundException; 4509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pellyimport android.content.Intent; 4609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pellyimport android.content.pm.PackageManager; 4709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pellyimport android.content.pm.ResolveInfo; 4809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pellyimport android.database.Cursor; 4941ef8d494511c040451f2f887cb31c3100746b61Nick Pellyimport android.util.Log; 5009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly 516769b59d715ea98bd72eafcfea9acd2714a887daTao Liejunimport java.io.File; 52ee52ddf33a0ce2cf89cc028136f60ae600c45de5Jake Hambyimport java.io.IOException; 536769b59d715ea98bd72eafcfea9acd2714a887daTao Liejunimport java.util.ArrayList; 546769b59d715ea98bd72eafcfea9acd2714a887daTao Liejunimport java.util.List; 55ee52ddf33a0ce2cf89cc028136f60ae600c45de5Jake Hambyimport java.util.concurrent.ConcurrentHashMap; 5609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly 5709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly/** 5809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * This class has some utilities for Opp application; 5909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly */ 6009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pellypublic class BluetoothOppUtility { 6109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly private static final String TAG = "BluetoothOppUtility"; 62ce4d93666275df294cb073fe41de5b85932570a8Nick Pelly private static final boolean D = Constants.DEBUG; 63ce4d93666275df294cb073fe41de5b85932570a8Nick Pelly private static final boolean V = Constants.VERBOSE; 6409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly 65ee52ddf33a0ce2cf89cc028136f60ae600c45de5Jake Hamby private static final ConcurrentHashMap<Uri, BluetoothOppSendFileInfo> sSendFileMap 66ee52ddf33a0ce2cf89cc028136f60ae600c45de5Jake Hamby = new ConcurrentHashMap<Uri, BluetoothOppSendFileInfo>(); 67ee52ddf33a0ce2cf89cc028136f60ae600c45de5Jake Hamby 6809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly public static BluetoothOppTransferInfo queryRecord(Context context, Uri uri) { 693a88b20fcd71e42451e402d27374b19eeb2ff0daNick Pelly BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); 7009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly BluetoothOppTransferInfo info = new BluetoothOppTransferInfo(); 7109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly Cursor cursor = context.getContentResolver().query(uri, null, null, null, null); 7209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly if (cursor != null) { 7309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly if (cursor.moveToFirst()) { 7409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly info.mID = cursor.getInt(cursor.getColumnIndexOrThrow(BluetoothShare._ID)); 7509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly info.mStatus = cursor.getInt(cursor.getColumnIndexOrThrow(BluetoothShare.STATUS)); 7609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly info.mDirection = cursor.getInt(cursor 7709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly .getColumnIndexOrThrow(BluetoothShare.DIRECTION)); 7809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly info.mTotalBytes = cursor.getInt(cursor 7909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly .getColumnIndexOrThrow(BluetoothShare.TOTAL_BYTES)); 8009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly info.mCurrentBytes = cursor.getInt(cursor 8109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly .getColumnIndexOrThrow(BluetoothShare.CURRENT_BYTES)); 8209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly info.mTimeStamp = cursor.getLong(cursor 8309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly .getColumnIndexOrThrow(BluetoothShare.TIMESTAMP)); 8409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly info.mDestAddr = cursor.getString(cursor 8509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly .getColumnIndexOrThrow(BluetoothShare.DESTINATION)); 8609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly 8709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly info.mFileName = cursor.getString(cursor 8809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly .getColumnIndexOrThrow(BluetoothShare._DATA)); 8909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly if (info.mFileName == null) { 9009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly info.mFileName = cursor.getString(cursor 9109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly .getColumnIndexOrThrow(BluetoothShare.FILENAME_HINT)); 9209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } 9309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly if (info.mFileName == null) { 9409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly info.mFileName = context.getString(R.string.unknown_file); 9509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } 9609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly 9709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly info.mFileUri = cursor.getString(cursor.getColumnIndexOrThrow(BluetoothShare.URI)); 9809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly 9909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly if (info.mFileUri != null) { 10009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly Uri u = Uri.parse(info.mFileUri); 10109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly info.mFileType = context.getContentResolver().getType(u); 10209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } else { 10309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly Uri u = Uri.parse(info.mFileName); 10409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly info.mFileType = context.getContentResolver().getType(u); 10509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } 10609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly if (info.mFileType == null) { 10709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly info.mFileType = cursor.getString(cursor 10809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly .getColumnIndexOrThrow(BluetoothShare.MIMETYPE)); 10909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } 11009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly 11141ef8d494511c040451f2f887cb31c3100746b61Nick Pelly BluetoothDevice remoteDevice = adapter.getRemoteDevice(info.mDestAddr); 11241ef8d494511c040451f2f887cb31c3100746b61Nick Pelly info.mDeviceName = 11341ef8d494511c040451f2f887cb31c3100746b61Nick Pelly BluetoothOppManager.getInstance(context).getDeviceName(remoteDevice); 11409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly 1158099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen int confirmationType = cursor.getInt( 1168099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen cursor.getColumnIndexOrThrow(BluetoothShare.USER_CONFIRMATION)); 1178099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen info.mHandoverInitiated = 1188099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen confirmationType == BluetoothShare.USER_CONFIRMATION_HANDOVER_CONFIRMED; 1198099f5e7bfa7227ba674b5f0076f331e737bafd7Martijn Coenen 120ce4d93666275df294cb073fe41de5b85932570a8Nick Pelly if (V) Log.v(TAG, "Get data from db:" + info.mFileName + info.mFileType 12109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly + info.mDestAddr); 12209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } 12309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly cursor.close(); 12409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } else { 12509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly info = null; 126ce4d93666275df294cb073fe41de5b85932570a8Nick Pelly if (V) Log.v(TAG, "BluetoothOppManager Error: not got data from db for uri:" + uri); 12709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } 12809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly return info; 12909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } 13009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly 13109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly /** 13209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * Organize Array list for transfers in one batch 13309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly */ 134888485a3f5fe991116c5536bb6d6903d47b63a70Tao Liejun // This function is used when UI show batch transfer. Currently only show single transfer. 13509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly public static ArrayList<String> queryTransfersInBatch(Context context, Long timeStamp) { 13609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly ArrayList<String> uris = Lists.newArrayList(); 13709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly final String WHERE = BluetoothShare.TIMESTAMP + " == " + timeStamp; 13809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly 13909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly Cursor metadataCursor = context.getContentResolver().query(BluetoothShare.CONTENT_URI, 14009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly new String[] { 14109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly BluetoothShare._DATA 14209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly }, WHERE, null, BluetoothShare._ID); 14309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly 14409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly if (metadataCursor == null) { 14509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly return null; 14609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } 14709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly 14809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly for (metadataCursor.moveToFirst(); !metadataCursor.isAfterLast(); metadataCursor 14909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly .moveToNext()) { 15009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly String fileName = metadataCursor.getString(0); 15109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly Uri path = Uri.parse(fileName); 15209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly // If there is no scheme, then it must be a file 15309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly if (path.getScheme() == null) { 15409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly path = Uri.fromFile(new File(fileName)); 15509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } 15609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly uris.add(path.toString()); 157ce4d93666275df294cb073fe41de5b85932570a8Nick Pelly if (V) Log.d(TAG, "Uri in this batch: " + path.toString()); 15809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } 15909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly metadataCursor.close(); 16009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly return uris; 16109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } 16209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly 16309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly /** 16409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * Open the received file with appropriate application, if can not find 16509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * application to handle, display error dialog. 16609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly */ 16709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly public static void openReceivedFile(Context context, String fileName, String mimetype, 1681f0fc26568c1babf0c66d2c75812b72e894eb0deLixin Yue Long timeStamp, Uri uri) { 16909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly if (fileName == null || mimetype == null) { 17009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly Log.e(TAG, "ERROR: Para fileName ==null, or mimetype == null"); 17109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly return; 17209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } 17309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly 17409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly File f = new File(fileName); 17509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly if (!f.exists()) { 17609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly Intent in = new Intent(context, BluetoothOppBtErrorActivity.class); 17709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 17809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly in.putExtra("title", context.getString(R.string.not_exist_file)); 17909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly in.putExtra("content", context.getString(R.string.not_exist_file_desc)); 18009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly context.startActivity(in); 1811f0fc26568c1babf0c66d2c75812b72e894eb0deLixin Yue 1821f0fc26568c1babf0c66d2c75812b72e894eb0deLixin Yue // Due to the file is not existing, delete related info in btopp db 1831f0fc26568c1babf0c66d2c75812b72e894eb0deLixin Yue // to prevent this file from appearing in live folder 1841f0fc26568c1babf0c66d2c75812b72e894eb0deLixin Yue if (V) Log.d(TAG, "This uri will be deleted: " + uri); 1851f0fc26568c1babf0c66d2c75812b72e894eb0deLixin Yue context.getContentResolver().delete(uri, null, null); 18609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly return; 18709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } 18809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly 18909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly Uri path = Uri.parse(fileName); 19009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly // If there is no scheme, then it must be a file 19109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly if (path.getScheme() == null) { 19209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly path = Uri.fromFile(new File(fileName)); 19309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } 19409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly 19509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly if (isRecognizedFileType(context, path, mimetype)) { 19609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly Intent activityIntent = new Intent(Intent.ACTION_VIEW); 19709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly activityIntent.setDataAndType(path, mimetype); 19809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly 19909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 20009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly try { 201ce4d93666275df294cb073fe41de5b85932570a8Nick Pelly if (V) Log.d(TAG, "ACTION_VIEW intent sent out: " + path + " / " + mimetype); 20209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly context.startActivity(activityIntent); 20309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } catch (ActivityNotFoundException ex) { 204ce4d93666275df294cb073fe41de5b85932570a8Nick Pelly if (V) Log.d(TAG, "no activity for handling ACTION_VIEW intent: " + mimetype, ex); 20509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } 20609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } else { 20709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly Intent in = new Intent(context, BluetoothOppBtErrorActivity.class); 20809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 20909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly in.putExtra("title", context.getString(R.string.unknown_file)); 21009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly in.putExtra("content", context.getString(R.string.unknown_file_desc)); 21109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly context.startActivity(in); 21209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } 21309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } 21409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly 21509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly /** 21609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * To judge if the file type supported (can be handled by some app) by phone 21709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * system. 21809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly */ 21909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly public static boolean isRecognizedFileType(Context context, Uri fileUri, String mimetype) { 22009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly boolean ret = true; 22109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly 222ce4d93666275df294cb073fe41de5b85932570a8Nick Pelly if (D) Log.d(TAG, "RecognizedFileType() fileUri: " + fileUri + " mimetype: " + mimetype); 22309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly 22409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly Intent mimetypeIntent = new Intent(Intent.ACTION_VIEW); 22509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly mimetypeIntent.setDataAndType(fileUri, mimetype); 22609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(mimetypeIntent, 22709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly PackageManager.MATCH_DEFAULT_ONLY); 22809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly 22909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly if (list.size() == 0) { 230ce4d93666275df294cb073fe41de5b85932570a8Nick Pelly if (D) Log.d(TAG, "NO application to handle MIME type " + mimetype); 23109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly ret = false; 23209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } 23309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly return ret; 23409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } 23509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly 23609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly /** 23709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * update visibility to Hidden 23809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly */ 23909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly public static void updateVisibilityToHidden(Context context, Uri uri) { 24009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly ContentValues updateValues = new ContentValues(); 24109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly updateValues.put(BluetoothShare.VISIBILITY, BluetoothShare.VISIBILITY_HIDDEN); 24209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly context.getContentResolver().update(uri, updateValues, null, null); 24309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } 24409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly 24509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly /** 24609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * Helper function to build the progress text. 24709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly */ 24809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly public static String formatProgressText(long totalBytes, long currentBytes) { 24909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly if (totalBytes <= 0) { 25009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly return "0%"; 25109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } 25209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly long progress = currentBytes * 100 / totalBytes; 25309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly StringBuilder sb = new StringBuilder(); 25409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly sb.append(progress); 25509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly sb.append('%'); 25609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly return sb.toString(); 25709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } 25809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly 25909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly /** 26009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * Get status description according to status code. 26109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly */ 2621d2222642c372e5635a906b6d6620ac35a304cf9Lixin Yue public static String getStatusDescription(Context context, int statusCode, String deviceName) { 26309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly String ret; 26409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly if (statusCode == BluetoothShare.STATUS_PENDING) { 26509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly ret = context.getString(R.string.status_pending); 26609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } else if (statusCode == BluetoothShare.STATUS_RUNNING) { 26709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly ret = context.getString(R.string.status_running); 26809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } else if (statusCode == BluetoothShare.STATUS_SUCCESS) { 26909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly ret = context.getString(R.string.status_success); 27009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } else if (statusCode == BluetoothShare.STATUS_NOT_ACCEPTABLE) { 27109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly ret = context.getString(R.string.status_not_accept); 27209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } else if (statusCode == BluetoothShare.STATUS_FORBIDDEN) { 27309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly ret = context.getString(R.string.status_forbidden); 27409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } else if (statusCode == BluetoothShare.STATUS_CANCELED) { 27509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly ret = context.getString(R.string.status_canceled); 27609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } else if (statusCode == BluetoothShare.STATUS_FILE_ERROR) { 27709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly ret = context.getString(R.string.status_file_error); 27809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } else if (statusCode == BluetoothShare.STATUS_ERROR_NO_SDCARD) { 27909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly ret = context.getString(R.string.status_no_sd_card); 28009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } else if (statusCode == BluetoothShare.STATUS_CONNECTION_ERROR) { 28109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly ret = context.getString(R.string.status_connection_error); 28209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } else if (statusCode == BluetoothShare.STATUS_ERROR_SDCARD_FULL) { 2831d2222642c372e5635a906b6d6620ac35a304cf9Lixin Yue ret = context.getString(R.string.bt_sm_2_1, deviceName); 28409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } else if ((statusCode == BluetoothShare.STATUS_BAD_REQUEST) 28509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly || (statusCode == BluetoothShare.STATUS_LENGTH_REQUIRED) 28609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly || (statusCode == BluetoothShare.STATUS_PRECONDITION_FAILED) 28709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly || (statusCode == BluetoothShare.STATUS_UNHANDLED_OBEX_CODE) 28809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly || (statusCode == BluetoothShare.STATUS_OBEX_DATA_ERROR)) { 28909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly ret = context.getString(R.string.status_protocol_error); 29009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } else { 29109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly ret = context.getString(R.string.status_unknown_error); 29209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } 29309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly return ret; 29409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } 29509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly 29609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly /** 29709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * Retry the failed transfer: Will insert a new transfer session to db 29809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly */ 29909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly public static void retryTransfer(Context context, BluetoothOppTransferInfo transInfo) { 30009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly ContentValues values = new ContentValues(); 30109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly values.put(BluetoothShare.URI, transInfo.mFileUri); 30209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly values.put(BluetoothShare.MIMETYPE, transInfo.mFileType); 30309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly values.put(BluetoothShare.DESTINATION, transInfo.mDestAddr); 30409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly 30509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly final Uri contentUri = context.getContentResolver().insert(BluetoothShare.CONTENT_URI, 30609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly values); 307ce4d93666275df294cb073fe41de5b85932570a8Nick Pelly if (V) Log.v(TAG, "Insert contentUri: " + contentUri + " to device: " + 308ce4d93666275df294cb073fe41de5b85932570a8Nick Pelly transInfo.mDeviceName); 30909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly } 31009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly 311ee52ddf33a0ce2cf89cc028136f60ae600c45de5Jake Hamby static void putSendFileInfo(Uri uri, BluetoothOppSendFileInfo sendFileInfo) { 312ee52ddf33a0ce2cf89cc028136f60ae600c45de5Jake Hamby if (D) Log.d(TAG, "putSendFileInfo: uri=" + uri + " sendFileInfo=" + sendFileInfo); 313ee52ddf33a0ce2cf89cc028136f60ae600c45de5Jake Hamby sSendFileMap.put(uri, sendFileInfo); 314ee52ddf33a0ce2cf89cc028136f60ae600c45de5Jake Hamby } 315ee52ddf33a0ce2cf89cc028136f60ae600c45de5Jake Hamby 316ee52ddf33a0ce2cf89cc028136f60ae600c45de5Jake Hamby static BluetoothOppSendFileInfo getSendFileInfo(Uri uri) { 317ee52ddf33a0ce2cf89cc028136f60ae600c45de5Jake Hamby if (D) Log.d(TAG, "getSendFileInfo: uri=" + uri); 318ee52ddf33a0ce2cf89cc028136f60ae600c45de5Jake Hamby BluetoothOppSendFileInfo info = sSendFileMap.get(uri); 319ee52ddf33a0ce2cf89cc028136f60ae600c45de5Jake Hamby return (info != null) ? info : BluetoothOppSendFileInfo.SEND_FILE_INFO_ERROR; 320ee52ddf33a0ce2cf89cc028136f60ae600c45de5Jake Hamby } 321ee52ddf33a0ce2cf89cc028136f60ae600c45de5Jake Hamby 322ee52ddf33a0ce2cf89cc028136f60ae600c45de5Jake Hamby static void closeSendFileInfo(Uri uri) { 323ee52ddf33a0ce2cf89cc028136f60ae600c45de5Jake Hamby if (D) Log.d(TAG, "closeSendFileInfo: uri=" + uri); 324ee52ddf33a0ce2cf89cc028136f60ae600c45de5Jake Hamby BluetoothOppSendFileInfo info = sSendFileMap.remove(uri); 325ee52ddf33a0ce2cf89cc028136f60ae600c45de5Jake Hamby if (info != null) { 326ee52ddf33a0ce2cf89cc028136f60ae600c45de5Jake Hamby try { 327ee52ddf33a0ce2cf89cc028136f60ae600c45de5Jake Hamby info.mInputStream.close(); 328ee52ddf33a0ce2cf89cc028136f60ae600c45de5Jake Hamby } catch (IOException ignored) { 329ee52ddf33a0ce2cf89cc028136f60ae600c45de5Jake Hamby } 330ee52ddf33a0ce2cf89cc028136f60ae600c45de5Jake Hamby } 331ee52ddf33a0ce2cf89cc028136f60ae600c45de5Jake Hamby } 33209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly} 333