Home | History | Annotate | Download | only in beam
      1 /*
      2 * Copyright (C) 2008 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 package com.android.nfc.beam;
     17 
     18 import com.android.nfc.NfcService;
     19 import com.android.nfc.handover.HandoverDataParser;
     20 
     21 import android.bluetooth.BluetoothDevice;
     22 import android.content.Context;
     23 import android.content.Intent;
     24 import android.net.Uri;
     25 import android.os.Handler;
     26 import android.os.Looper;
     27 import android.os.Message;
     28 import android.os.Messenger;
     29 import android.os.UserHandle;
     30 import android.util.Log;
     31 
     32 /**
     33  * Manager for starting and stopping Beam transfers. Prevents more than one transfer from
     34  * happening at a time.
     35  */
     36 public class BeamManager implements Handler.Callback {
     37     private static final String TAG = "BeamManager";
     38     private static final boolean DBG = false;
     39 
     40     private static final String ACTION_WHITELIST_DEVICE =
     41             "android.btopp.intent.action.WHITELIST_DEVICE";
     42     public static final int MSG_BEAM_COMPLETE = 0;
     43 
     44     private final Object mLock;
     45 
     46     private boolean mBeamInProgress;
     47     private final Handler mCallback;
     48 
     49     private NfcService mNfcService;
     50 
     51     private static final class Singleton {
     52         public static final BeamManager INSTANCE = new BeamManager();
     53     }
     54 
     55     private BeamManager() {
     56         mLock = new Object();
     57         mBeamInProgress = false;
     58         mCallback = new Handler(Looper.getMainLooper(), this);
     59         mNfcService = NfcService.getInstance();
     60     }
     61 
     62     public static BeamManager getInstance() {
     63         return Singleton.INSTANCE;
     64     }
     65 
     66     public boolean isBeamInProgress() {
     67         synchronized (mLock) {
     68             return mBeamInProgress;
     69         }
     70     }
     71 
     72     public boolean startBeamReceive(Context context,
     73                                  HandoverDataParser.BluetoothHandoverData handoverData) {
     74         synchronized (mLock) {
     75             if (mBeamInProgress) {
     76                 return false;
     77             } else {
     78                 mBeamInProgress = true;
     79             }
     80         }
     81 
     82         BeamTransferRecord transferRecord =
     83                 BeamTransferRecord.forBluetoothDevice(
     84                         handoverData.device, handoverData.carrierActivating, null);
     85 
     86         Intent receiveIntent = new Intent(context.getApplicationContext(),
     87                 BeamReceiveService.class);
     88         receiveIntent.putExtra(BeamReceiveService.EXTRA_BEAM_TRANSFER_RECORD, transferRecord);
     89         receiveIntent.putExtra(BeamReceiveService.EXTRA_BEAM_COMPLETE_CALLBACK,
     90                 new Messenger(mCallback));
     91         whitelistOppDevice(context, handoverData.device);
     92         context.startServiceAsUser(receiveIntent, UserHandle.CURRENT);
     93         return true;
     94     }
     95 
     96     public boolean startBeamSend(Context context,
     97                                HandoverDataParser.BluetoothHandoverData outgoingHandoverData,
     98                                Uri[] uris, UserHandle userHandle) {
     99         synchronized (mLock) {
    100             if (mBeamInProgress) {
    101                 return false;
    102             } else {
    103                 mBeamInProgress = true;
    104             }
    105         }
    106 
    107         BeamTransferRecord transferRecord = BeamTransferRecord.forBluetoothDevice(
    108                 outgoingHandoverData.device, outgoingHandoverData.carrierActivating,
    109                 uris);
    110         Intent sendIntent = new Intent(context.getApplicationContext(),
    111                 BeamSendService.class);
    112         sendIntent.putExtra(BeamSendService.EXTRA_BEAM_TRANSFER_RECORD, transferRecord);
    113         sendIntent.putExtra(BeamSendService.EXTRA_BEAM_COMPLETE_CALLBACK,
    114                 new Messenger(mCallback));
    115         context.startServiceAsUser(sendIntent, userHandle);
    116         return true;
    117     }
    118 
    119     @Override
    120     public boolean handleMessage(Message msg) {
    121         if (msg.what == MSG_BEAM_COMPLETE) {
    122             synchronized (mLock) {
    123                 mBeamInProgress = false;
    124             }
    125 
    126             boolean success = msg.arg1 == 1;
    127             if (success) {
    128                 mNfcService.playSound(NfcService.SOUND_END);
    129             }
    130             return true;
    131         }
    132         return false;
    133     }
    134 
    135     void whitelistOppDevice(Context context, BluetoothDevice device) {
    136         if (DBG) Log.d(TAG, "Whitelisting " + device + " for BT OPP");
    137         Intent intent = new Intent(ACTION_WHITELIST_DEVICE);
    138         intent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
    139         context.sendBroadcastAsUser(intent, UserHandle.CURRENT);
    140     }
    141 
    142 }
    143