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.nfc.handover; 18 19 import android.bluetooth.BluetoothDevice; 20 import android.content.ContentResolver; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.net.Uri; 24 import android.os.Handler; 25 import android.os.Message; 26 import android.os.SystemClock; 27 import android.os.UserHandle; 28 import android.util.Log; 29 import android.webkit.MimeTypeMap; 30 import java.util.ArrayList; 31 import java.util.Arrays; 32 33 public class BluetoothOppHandover implements Handler.Callback { 34 static final String TAG = "BluetoothOppHandover"; 35 static final boolean DBG = true; 36 37 static final int STATE_INIT = 0; 38 static final int STATE_TURNING_ON = 1; 39 static final int STATE_WAITING = 2; // Need to wait for remote side turning on BT 40 static final int STATE_COMPLETE = 3; 41 42 static final int MSG_START_SEND = 0; 43 44 static final int REMOTE_BT_ENABLE_DELAY_MS = 5000; 45 46 static final String ACTION_HANDOVER_SEND = 47 "android.nfc.handover.intent.action.HANDOVER_SEND"; 48 49 static final String ACTION_HANDOVER_SEND_MULTIPLE = 50 "android.nfc.handover.intent.action.HANDOVER_SEND_MULTIPLE"; 51 52 final Context mContext; 53 final BluetoothDevice mDevice; 54 55 final Uri[] mUris; 56 final boolean mRemoteActivating; 57 final Handler mHandler; 58 final Long mCreateTime; 59 60 int mState; 61 BluetoothOppHandover(Context context, BluetoothDevice device, Uri[] uris, boolean remoteActivating)62 public BluetoothOppHandover(Context context, BluetoothDevice device, Uri[] uris, 63 boolean remoteActivating) { 64 mContext = context; 65 mDevice = device; 66 mUris = uris; 67 mRemoteActivating = remoteActivating; 68 mCreateTime = SystemClock.elapsedRealtime(); 69 70 mHandler = new Handler(context.getMainLooper(),this); 71 mState = STATE_INIT; 72 } 73 74 /** 75 * Main entry point. This method is usually called after construction, 76 * to begin the BT sequence. Must be called on Main thread. 77 */ start()78 public void start() { 79 if (mRemoteActivating) { 80 Long timeElapsed = SystemClock.elapsedRealtime() - mCreateTime; 81 if (timeElapsed < REMOTE_BT_ENABLE_DELAY_MS) { 82 mHandler.sendEmptyMessageDelayed(MSG_START_SEND, 83 REMOTE_BT_ENABLE_DELAY_MS - timeElapsed); 84 } else { 85 // Already waited long enough for BT to come up 86 // - start send. 87 sendIntent(); 88 } 89 } else { 90 // Remote BT enabled already, start send immediately 91 sendIntent(); 92 } 93 } 94 complete()95 void complete() { 96 mState = STATE_COMPLETE; 97 } 98 sendIntent()99 void sendIntent() { 100 Intent intent = new Intent(); 101 intent.setPackage("com.android.bluetooth"); 102 String mimeType = MimeTypeUtil.getMimeTypeForUri(mContext, mUris[0]); 103 intent.setType(mimeType); 104 intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice); 105 for (Uri uri : mUris) { 106 // TODO we need to transfer our permission grant from NFC 107 // to the Bluetooth process. This works, but we don't have 108 // a good framework API for revoking permission yet. 109 try { 110 mContext.grantUriPermission("com.android.bluetooth", uri, 111 Intent.FLAG_GRANT_READ_URI_PERMISSION); 112 } catch (SecurityException e) { 113 Log.e(TAG, "Failed to transfer permission to Bluetooth process."); 114 } 115 } 116 if (mUris.length == 1) { 117 intent.setAction(ACTION_HANDOVER_SEND); 118 intent.putExtra(Intent.EXTRA_STREAM, mUris[0]); 119 } else { 120 ArrayList<Uri> uris = new ArrayList<Uri>(Arrays.asList(mUris)); 121 intent.setAction(ACTION_HANDOVER_SEND_MULTIPLE); 122 intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); 123 } 124 if (DBG) Log.d(TAG, "Handing off outging transfer to BT"); 125 mContext.sendBroadcast(intent); 126 127 complete(); 128 } 129 130 131 @Override handleMessage(Message msg)132 public boolean handleMessage(Message msg) { 133 if (msg.what == MSG_START_SEND) { 134 sendIntent(); 135 return true; 136 } 137 return false; 138 } 139 } 140