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