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