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; 36 37import android.app.NotificationManager; 38import android.bluetooth.BluetoothAdapter; 39import android.bluetooth.BluetoothDevice; 40import android.bluetooth.BluetoothDevicePicker; 41import android.content.BroadcastReceiver; 42import android.content.ContentUris; 43import android.content.ContentValues; 44import android.content.Context; 45import android.content.Intent; 46import android.database.Cursor; 47import android.net.Uri; 48import android.util.Log; 49import android.widget.Toast; 50 51/** 52 * Receives and handles: system broadcasts; Intents from other applications; 53 * Intents from OppService; Intents from modules in Opp application layer. 54 */ 55public class BluetoothOppReceiver extends BroadcastReceiver { 56 private static final String TAG = "BluetoothOppReceiver"; 57 private static final boolean D = Constants.DEBUG; 58 private static final boolean V = Constants.VERBOSE; 59 60 @Override 61 public void onReceive(Context context, Intent intent) { 62 String action = intent.getAction(); 63 64 if (action.equals(BluetoothDevicePicker.ACTION_DEVICE_SELECTED)) { 65 BluetoothOppManager mOppManager = BluetoothOppManager.getInstance(context); 66 67 BluetoothDevice remoteDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 68 69 if (D) Log.d(TAG, "Received BT device selected intent, bt device: " + remoteDevice); 70 71 if (remoteDevice == null) { 72 mOppManager.cleanUpSendingFileInfo(); 73 return; 74 } 75 // Insert transfer session record to database 76 mOppManager.startTransfer(remoteDevice); 77 78 // Display toast message 79 String deviceName = mOppManager.getDeviceName(remoteDevice); 80 String toastMsg; 81 int batchSize = mOppManager.getBatchSize(); 82 if (mOppManager.mMultipleFlag) { 83 toastMsg = context.getString(R.string.bt_toast_5, Integer.toString(batchSize), 84 deviceName); 85 } else { 86 toastMsg = context.getString(R.string.bt_toast_4, deviceName); 87 } 88 Toast.makeText(context, toastMsg, Toast.LENGTH_SHORT).show(); 89 } else if (action.equals(Constants.ACTION_INCOMING_FILE_CONFIRM)) { 90 if (V) Log.v(TAG, "Receiver ACTION_INCOMING_FILE_CONFIRM"); 91 92 Uri uri = intent.getData(); 93 Intent in = new Intent(context, BluetoothOppIncomingFileConfirmActivity.class); 94 in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 95 in.setDataAndNormalize(uri); 96 context.startActivity(in); 97 98 } else if (action.equals(Constants.ACTION_DECLINE)) { 99 if (V) Log.v(TAG, "Receiver ACTION_DECLINE"); 100 101 Uri uri = intent.getData(); 102 ContentValues values = new ContentValues(); 103 values.put(BluetoothShare.USER_CONFIRMATION, BluetoothShare.USER_CONFIRMATION_DENIED); 104 context.getContentResolver().update(uri, values, null, null); 105 cancelNotification(context, BluetoothOppNotification.NOTIFICATION_ID_PROGRESS); 106 107 } else if (action.equals(Constants.ACTION_ACCEPT)) { 108 if (V) Log.v(TAG, "Receiver ACTION_ACCEPT"); 109 110 Uri uri = intent.getData(); 111 ContentValues values = new ContentValues(); 112 values.put(BluetoothShare.USER_CONFIRMATION, BluetoothShare.USER_CONFIRMATION_CONFIRMED); 113 context.getContentResolver().update(uri, values, null, null); 114 } else if (action.equals(Constants.ACTION_OPEN) || action.equals(Constants.ACTION_LIST)) { 115 if (V) { 116 if (action.equals(Constants.ACTION_OPEN)) { 117 Log.v(TAG, "Receiver open for " + intent.getData()); 118 } else { 119 Log.v(TAG, "Receiver list for " + intent.getData()); 120 } 121 } 122 123 BluetoothOppTransferInfo transInfo = new BluetoothOppTransferInfo(); 124 Uri uri = intent.getData(); 125 transInfo = BluetoothOppUtility.queryRecord(context, uri); 126 if (transInfo == null) { 127 Log.e(TAG, "Error: Can not get data from db"); 128 return; 129 } 130 131 if (transInfo.mDirection == BluetoothShare.DIRECTION_INBOUND 132 && BluetoothShare.isStatusSuccess(transInfo.mStatus)) { 133 // if received file successfully, open this file 134 BluetoothOppUtility.openReceivedFile(context, transInfo.mFileName, 135 transInfo.mFileType, transInfo.mTimeStamp, uri); 136 BluetoothOppUtility.updateVisibilityToHidden(context, uri); 137 } else { 138 Intent in = new Intent(context, BluetoothOppTransferActivity.class); 139 in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 140 in.setDataAndNormalize(uri); 141 context.startActivity(in); 142 } 143 144 } else if (action.equals(Constants.ACTION_OPEN_OUTBOUND_TRANSFER)) { 145 if (V) Log.v(TAG, "Received ACTION_OPEN_OUTBOUND_TRANSFER."); 146 147 Intent in = new Intent(context, BluetoothOppTransferHistory.class); 148 in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); 149 in.putExtra("direction", BluetoothShare.DIRECTION_OUTBOUND); 150 context.startActivity(in); 151 } else if (action.equals(Constants.ACTION_OPEN_INBOUND_TRANSFER)) { 152 if (V) Log.v(TAG, "Received ACTION_OPEN_INBOUND_TRANSFER."); 153 154 Intent in = new Intent(context, BluetoothOppTransferHistory.class); 155 in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); 156 in.putExtra("direction", BluetoothShare.DIRECTION_INBOUND); 157 context.startActivity(in); 158 } else if (action.equals(Constants.ACTION_OPEN_RECEIVED_FILES)) { 159 if (V) Log.v(TAG, "Received ACTION_OPEN_RECEIVED_FILES."); 160 161 Intent in = new Intent(context, BluetoothOppTransferHistory.class); 162 in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); 163 in.putExtra("direction", BluetoothShare.DIRECTION_INBOUND); 164 in.putExtra(Constants.EXTRA_SHOW_ALL_FILES, true); 165 context.startActivity(in); 166 } else if (action.equals(Constants.ACTION_HIDE)) { 167 if (V) Log.v(TAG, "Receiver hide for " + intent.getData()); 168 Cursor cursor = context.getContentResolver().query(intent.getData(), null, null, null, 169 null); 170 if (cursor != null) { 171 if (cursor.moveToFirst()) { 172 int visibilityColumn = cursor.getColumnIndexOrThrow(BluetoothShare.VISIBILITY); 173 int visibility = cursor.getInt(visibilityColumn); 174 int userConfirmationColumn = cursor 175 .getColumnIndexOrThrow(BluetoothShare.USER_CONFIRMATION); 176 int userConfirmation = cursor.getInt(userConfirmationColumn); 177 if (((userConfirmation == BluetoothShare.USER_CONFIRMATION_PENDING)) 178 && visibility == BluetoothShare.VISIBILITY_VISIBLE) { 179 ContentValues values = new ContentValues(); 180 values.put(BluetoothShare.VISIBILITY, BluetoothShare.VISIBILITY_HIDDEN); 181 context.getContentResolver().update(intent.getData(), values, null, null); 182 if (V) Log.v(TAG, "Action_hide received and db updated"); 183 } 184 } 185 cursor.close(); 186 } 187 } else if (action.equals(Constants.ACTION_COMPLETE_HIDE)) { 188 if (V) Log.v(TAG, "Receiver ACTION_COMPLETE_HIDE"); 189 ContentValues updateValues = new ContentValues(); 190 updateValues.put(BluetoothShare.VISIBILITY, BluetoothShare.VISIBILITY_HIDDEN); 191 context.getContentResolver().update(BluetoothShare.CONTENT_URI, updateValues, 192 BluetoothOppNotification.WHERE_COMPLETED, null); 193 } else if (action.equals(BluetoothShare.TRANSFER_COMPLETED_ACTION)) { 194 if (V) Log.v(TAG, "Receiver Transfer Complete Intent for " + intent.getData()); 195 196 String toastMsg = null; 197 BluetoothOppTransferInfo transInfo = new BluetoothOppTransferInfo(); 198 transInfo = BluetoothOppUtility.queryRecord(context, intent.getData()); 199 if (transInfo == null) { 200 Log.e(TAG, "Error: Can not get data from db"); 201 return; 202 } 203 204 if (transInfo.mHandoverInitiated) { 205 // Deal with handover-initiated transfers separately 206 Intent handoverIntent = new Intent(Constants.ACTION_BT_OPP_TRANSFER_DONE); 207 if (transInfo.mDirection == BluetoothShare.DIRECTION_INBOUND) { 208 handoverIntent.putExtra(Constants.EXTRA_BT_OPP_TRANSFER_DIRECTION, 209 Constants.DIRECTION_BLUETOOTH_INCOMING); 210 } else { 211 handoverIntent.putExtra(Constants.EXTRA_BT_OPP_TRANSFER_DIRECTION, 212 Constants.DIRECTION_BLUETOOTH_OUTGOING); 213 } 214 handoverIntent.putExtra(Constants.EXTRA_BT_OPP_TRANSFER_ID, transInfo.mID); 215 handoverIntent.putExtra(Constants.EXTRA_BT_OPP_ADDRESS, transInfo.mDestAddr); 216 217 if (BluetoothShare.isStatusSuccess(transInfo.mStatus)) { 218 handoverIntent.putExtra(Constants.EXTRA_BT_OPP_TRANSFER_STATUS, 219 Constants.HANDOVER_TRANSFER_STATUS_SUCCESS); 220 handoverIntent.putExtra(Constants.EXTRA_BT_OPP_TRANSFER_URI, 221 transInfo.mFileName); 222 handoverIntent.putExtra(Constants.EXTRA_BT_OPP_TRANSFER_MIMETYPE, 223 transInfo.mFileType); 224 } else { 225 handoverIntent.putExtra(Constants.EXTRA_BT_OPP_TRANSFER_STATUS, 226 Constants.HANDOVER_TRANSFER_STATUS_FAILURE); 227 } 228 context.sendBroadcast(handoverIntent, Constants.HANDOVER_STATUS_PERMISSION); 229 return; 230 } 231 232 if (BluetoothShare.isStatusSuccess(transInfo.mStatus)) { 233 if (transInfo.mDirection == BluetoothShare.DIRECTION_OUTBOUND) { 234 toastMsg = context.getString(R.string.notification_sent, transInfo.mFileName); 235 } else if (transInfo.mDirection == BluetoothShare.DIRECTION_INBOUND) { 236 toastMsg = context.getString(R.string.notification_received, 237 transInfo.mFileName); 238 } 239 240 } else if (BluetoothShare.isStatusError(transInfo.mStatus)) { 241 if (transInfo.mDirection == BluetoothShare.DIRECTION_OUTBOUND) { 242 toastMsg = context.getString(R.string.notification_sent_fail, 243 transInfo.mFileName); 244 } else if (transInfo.mDirection == BluetoothShare.DIRECTION_INBOUND) { 245 toastMsg = context.getString(R.string.download_fail_line1); 246 } 247 } 248 if (V) Log.v(TAG, "Toast msg == " + toastMsg); 249 if (toastMsg != null) { 250 Toast.makeText(context, toastMsg, Toast.LENGTH_SHORT).show(); 251 } 252 } 253 } 254 255 private void cancelNotification(Context context, int id) { 256 NotificationManager notMgr = (NotificationManager)context 257 .getSystemService(Context.NOTIFICATION_SERVICE); 258 if (notMgr == null) return; 259 notMgr.cancel(id); 260 if (V) Log.v(TAG, "notMgr.cancel called"); 261 } 262} 263