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