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