1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.bluetooth.opp; 18 19 import android.bluetooth.BluetoothDevice; 20 import android.content.BroadcastReceiver; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.net.Uri; 24 import android.util.Log; 25 26 import com.android.bluetooth.BluetoothMethodProxy; 27 28 import java.util.ArrayList; 29 30 public class BluetoothOppHandoverReceiver extends BroadcastReceiver { 31 public static final String TAG = "BluetoothOppHandoverReceiver"; 32 private static final boolean D = Constants.DEBUG; 33 34 @Override onReceive(Context context, Intent intent)35 public void onReceive(Context context, Intent intent) { 36 String action = intent.getAction(); 37 if (D) Log.d(TAG, "Action :" + action); 38 if (action == null) return; 39 if (action.equals(Constants.ACTION_HANDOVER_SEND) || action.equals( 40 Constants.ACTION_HANDOVER_SEND_MULTIPLE)) { 41 final BluetoothDevice device = 42 (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 43 if (device == null) { 44 if (D) { 45 Log.d(TAG, "No device attached to handover intent."); 46 } 47 return; 48 } 49 50 final String mimeType = intent.getType(); 51 ArrayList<Uri> uris = new ArrayList<Uri>(); 52 if (action.equals(Constants.ACTION_HANDOVER_SEND)) { 53 Uri stream = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM); 54 if (stream != null) { 55 uris.add(stream); 56 } 57 } else if (action.equals(Constants.ACTION_HANDOVER_SEND_MULTIPLE)) { 58 uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM); 59 } 60 61 if (mimeType != null && uris != null && !uris.isEmpty()) { 62 final Context finalContext = context; 63 final ArrayList<Uri> finalUris = uris; 64 Thread t = new Thread(new Runnable() { 65 @Override 66 public void run() { 67 BluetoothOppManager.getInstance(finalContext) 68 .saveSendingFileInfo(mimeType, finalUris, true /* isHandover */, 69 true /* fromExternal */); 70 BluetoothOppManager.getInstance(finalContext).startTransfer(device); 71 } 72 }); 73 t.start(); 74 } else { 75 if (D) { 76 Log.d(TAG, "No mimeType or stream attached to handover request"); 77 } 78 return; 79 } 80 } else if (action.equals(Constants.ACTION_ACCEPTLIST_DEVICE)) { 81 BluetoothDevice device = 82 (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 83 if (device == null) { 84 return; 85 } 86 if (D) { 87 Log.d(TAG, "Adding " + device.getIdentityAddress() + " to acceptlist"); 88 } 89 BluetoothOppManager.getInstance(context).addToAcceptlist(device.getIdentityAddress()); 90 } else if (action.equals(Constants.ACTION_STOP_HANDOVER)) { 91 int id = intent.getIntExtra(Constants.EXTRA_BT_OPP_TRANSFER_ID, -1); 92 if (id != -1) { 93 Uri contentUri = Uri.parse(BluetoothShare.CONTENT_URI + "/" + id); 94 95 if (D) { 96 Log.d(TAG, "Stopping handover transfer with Uri " + contentUri); 97 } 98 BluetoothMethodProxy.getInstance().contentResolverDelete( 99 context.getContentResolver(), contentUri, null, null); 100 } 101 } else { 102 if (D) { 103 Log.d(TAG, "Unknown action: " + action); 104 } 105 } 106 } 107 108 } 109