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 java.util.ArrayList;
27 
28 public class BluetoothOppHandoverReceiver extends BroadcastReceiver {
29     public static final String TAG = "BluetoothOppHandoverReceiver";
30     private static final boolean D = Constants.DEBUG;
31 
32     @Override
onReceive(Context context, Intent intent)33     public void onReceive(Context context, Intent intent) {
34         String action = intent.getAction();
35         if (D) Log.d(TAG, "Action :" + action);
36         if (action == null) return;
37         if (action.equals(Constants.ACTION_HANDOVER_SEND) || action.equals(
38                 Constants.ACTION_HANDOVER_SEND_MULTIPLE)) {
39             final BluetoothDevice device =
40                     (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
41             if (device == null) {
42                 if (D) {
43                     Log.d(TAG, "No device attached to handover intent.");
44                 }
45                 return;
46             }
47 
48             final String mimeType = intent.getType();
49             ArrayList<Uri> uris = new ArrayList<Uri>();
50             if (action.equals(Constants.ACTION_HANDOVER_SEND)) {
51                 Uri stream = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
52                 if (stream != null) {
53                     uris.add(stream);
54                 }
55             } else if (action.equals(Constants.ACTION_HANDOVER_SEND_MULTIPLE)) {
56                 uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
57             }
58 
59             if (mimeType != null && uris != null && !uris.isEmpty()) {
60                 final Context finalContext = context;
61                 final ArrayList<Uri> finalUris = uris;
62                 Thread t = new Thread(new Runnable() {
63                     @Override
64                     public void run() {
65                         BluetoothOppManager.getInstance(finalContext)
66                                 .saveSendingFileInfo(mimeType, finalUris, true /* isHandover */,
67                                         true /* fromExternal */);
68                         BluetoothOppManager.getInstance(finalContext).startTransfer(device);
69                     }
70                 });
71                 t.start();
72             } else {
73                 if (D) {
74                     Log.d(TAG, "No mimeType or stream attached to handover request");
75                 }
76                 return;
77             }
78         } else if (action.equals(Constants.ACTION_ACCEPTLIST_DEVICE)) {
79             BluetoothDevice device =
80                     (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
81             if (D) {
82                 Log.d(TAG, "Adding " + device + " to acceptlist");
83             }
84             if (device == null) {
85                 return;
86             }
87             BluetoothOppManager.getInstance(context).addToAcceptlist(device.getAddress());
88         } else if (action.equals(Constants.ACTION_STOP_HANDOVER)) {
89             int id = intent.getIntExtra(Constants.EXTRA_BT_OPP_TRANSFER_ID, -1);
90             if (id != -1) {
91                 Uri contentUri = Uri.parse(BluetoothShare.CONTENT_URI + "/" + id);
92 
93                 if (D) {
94                     Log.d(TAG, "Stopping handover transfer with Uri " + contentUri);
95                 }
96                 context.getContentResolver().delete(contentUri, null, null);
97             }
98         } else {
99             if (D) {
100                 Log.d(TAG, "Unknown action: " + action);
101             }
102         }
103     }
104 
105 }
106