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