BluetoothOppReceiver.java revision 52236de777c23788df8147de15912a57e8bc36dd
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.BluetoothDevice; 39import android.bluetooth.BluetoothError; 40import android.bluetooth.BluetoothIntent; 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 58 @Override 59 public void onReceive(Context context, Intent intent) { 60 String action = intent.getAction(); 61 62 if (action.equals(Intent.ACTION_BOOT_COMPLETED)) { 63 64 context.startService(new Intent(context, BluetoothOppService.class)); 65 } else if (action.equals(BluetoothIntent.BLUETOOTH_STATE_CHANGED_ACTION)) { 66 if (BluetoothDevice.BLUETOOTH_STATE_ON == intent.getIntExtra( 67 BluetoothIntent.BLUETOOTH_STATE, BluetoothError.ERROR)) { 68 if (Constants.LOGVV) { 69 Log.v(TAG, "Received BLUETOOTH_STATE_CHANGED_ACTION, BLUETOOTH_STATE_ON"); 70 } 71 context.startService(new Intent(context, BluetoothOppService.class)); 72 73 // If this is within a sending process, continue the handle 74 // logic to display device picker dialog. 75 synchronized (this) { 76 if (BluetoothOppManager.getInstance(context).mSendingFlag) { 77 // reset the flags 78 BluetoothOppManager.getInstance(context).mSendingFlag = false; 79 80 Intent in1 = new Intent(context, BluetoothDevicePickerActivity.class); 81 in1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 82 context.startActivity(in1); 83 } 84 } 85 } 86 } else if (action.equals(BluetoothShare.BLUETOOTH_DEVICE_SELECTED_ACTION)) { 87 BluetoothOppManager mOppManager = BluetoothOppManager.getInstance(context); 88 89 String btAddr = (String)intent.getStringExtra("BT_ADDRESS"); 90 91 if (Constants.LOGVV) { 92 Log.v(TAG, "Received BT device selected intent, bt addr: " + btAddr); 93 } 94 95 // Insert transfer session record to database 96 mOppManager.startTransfer(btAddr); 97 98 // Display toast message 99 String deviceName = mOppManager.getDeviceName(btAddr); 100 String toastMsg; 101 if (mOppManager.mMultipleFlag) { 102 toastMsg = context.getString(R.string.bt_toast_5, Integer 103 .toString(mOppManager.mfileNumInBatch), deviceName); 104 } else { 105 toastMsg = context.getString(R.string.bt_toast_4, deviceName); 106 } 107 Toast.makeText(context, toastMsg, Toast.LENGTH_SHORT).show(); 108 } else if (action.equals(Constants.ACTION_INCOMING_FILE_CONFIRM)) { 109 if (Constants.LOGVV) { 110 Log.v(TAG, "Receiver ACTION_INCOMING_FILE_CONFIRM"); 111 } 112 113 Uri uri = intent.getData(); 114 Intent in = new Intent(context, BluetoothOppIncomingFileConfirmActivity.class); 115 in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 116 in.setData(uri); 117 context.startActivity(in); 118 119 NotificationManager notMgr = (NotificationManager)context 120 .getSystemService(Context.NOTIFICATION_SERVICE); 121 if (notMgr != null) { 122 notMgr.cancel((int)ContentUris.parseId(intent.getData())); 123 Log.v(TAG, "notMgr.cancel called"); 124 } 125 } else if (action.equals(BluetoothShare.INCOMING_FILE_CONFIRMATION_REQUEST_ACTION)) { 126 if (Constants.LOGVV) { 127 Log.v(TAG, "Receiver INCOMING_FILE_NOTIFICATION"); 128 } 129 130 Toast.makeText(context, context.getString(R.string.incoming_file_toast_msg), 131 Toast.LENGTH_SHORT).show(); 132 133 } else if (action.equals(Constants.ACTION_OPEN) || action.equals(Constants.ACTION_LIST)) { 134 if (Constants.LOGVV) { 135 if (action.equals(Constants.ACTION_OPEN)) { 136 Log.v(TAG, "Receiver open for " + intent.getData()); 137 } else { 138 Log.v(TAG, "Receiver list for " + intent.getData()); 139 } 140 } 141 142 BluetoothOppTransferInfo transInfo = new BluetoothOppTransferInfo(); 143 Uri uri = intent.getData(); 144 transInfo = BluetoothOppUtility.queryRecord(context, uri); 145 if (transInfo == null) { 146 if (Constants.LOGVV) { 147 Log.e(TAG, "Error: Can not get data from db"); 148 } 149 return; 150 } 151 152 if (transInfo.mDirection == BluetoothShare.DIRECTION_INBOUND 153 && BluetoothShare.isStatusSuccess(transInfo.mStatus)) { 154 // if received file successfully, open this file 155 BluetoothOppUtility.openReceivedFile(context, transInfo.mFileName, 156 transInfo.mFileType, transInfo.mTimeStamp); 157 BluetoothOppUtility.updateVisibilityToHidden(context, uri); 158 } else { 159 Intent in = new Intent(context, BluetoothOppTransferActivity.class); 160 in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 161 in.setData(uri); 162 context.startActivity(in); 163 } 164 165 NotificationManager notMgr = (NotificationManager)context 166 .getSystemService(Context.NOTIFICATION_SERVICE); 167 if (notMgr != null) { 168 notMgr.cancel((int)ContentUris.parseId(intent.getData())); 169 if (Constants.LOGVV) { 170 Log.v(TAG, "notMgr.cancel called"); 171 } 172 } 173 } else if (action.equals(Constants.ACTION_HIDE)) { 174 if (Constants.LOGVV) { 175 Log.v(TAG, "Receiver hide for " + intent.getData()); 176 } 177 Cursor cursor = context.getContentResolver().query(intent.getData(), null, null, null, 178 null); 179 if (cursor != null) { 180 if (cursor.moveToFirst()) { 181 int statusColumn = cursor.getColumnIndexOrThrow(BluetoothShare.STATUS); 182 int status = cursor.getInt(statusColumn); 183 int visibilityColumn = cursor.getColumnIndexOrThrow(BluetoothShare.VISIBILITY); 184 int visibility = cursor.getInt(visibilityColumn); 185 int userConfirmationColumn = cursor 186 .getColumnIndexOrThrow(BluetoothShare.USER_CONFIRMATION); 187 int userConfirmation = cursor.getInt(userConfirmationColumn); 188 if ((BluetoothShare.isStatusCompleted(status) || (userConfirmation == BluetoothShare.USER_CONFIRMATION_PENDING)) 189 && visibility == BluetoothShare.VISIBILITY_VISIBLE) { 190 ContentValues values = new ContentValues(); 191 values.put(BluetoothShare.VISIBILITY, BluetoothShare.VISIBILITY_HIDDEN); 192 context.getContentResolver().update(intent.getData(), values, null, null); 193 if (Constants.LOGVV) { 194 Log.v(TAG, "Action_hide received and db updated"); 195 } 196 } 197 } 198 cursor.close(); 199 } 200 } else if (action.equals(BluetoothShare.TRANSFER_COMPLETED_ACTION)) { 201 if (Constants.LOGVV) { 202 Log.v(TAG, "Receiver Transfer Complete Intent for " + intent.getData()); 203 } 204 205 String toastMsg = null; 206 BluetoothOppTransferInfo transInfo = new BluetoothOppTransferInfo(); 207 transInfo = BluetoothOppUtility.queryRecord(context, intent.getData()); 208 if (transInfo == null) { 209 if (Constants.LOGVV) { 210 Log.e(TAG, "Error: Can not get data from db"); 211 } 212 return; 213 } 214 215 if (BluetoothShare.isStatusSuccess(transInfo.mStatus)) { 216 if (transInfo.mDirection == BluetoothShare.DIRECTION_OUTBOUND) { 217 toastMsg = context.getString(R.string.notification_sent, transInfo.mFileName); 218 } else if (transInfo.mDirection == BluetoothShare.DIRECTION_INBOUND) { 219 toastMsg = context.getString(R.string.notification_received, 220 transInfo.mFileName); 221 } 222 223 } else if (BluetoothShare.isStatusError(transInfo.mStatus)) { 224 if (transInfo.mDirection == BluetoothShare.DIRECTION_OUTBOUND) { 225 toastMsg = context.getString(R.string.notification_sent_fail, 226 transInfo.mFileName); 227 } else if (transInfo.mDirection == BluetoothShare.DIRECTION_INBOUND) { 228 toastMsg = context.getString(R.string.download_fail_line1); 229 } 230 } 231 if (Constants.LOGVV) { 232 Log.v(TAG, "Toast msg == " + toastMsg); 233 } 234 if (toastMsg != null) { 235 Toast.makeText(context, toastMsg, Toast.LENGTH_SHORT).show(); 236 } 237 } 238 } 239} 240