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.app.Activity; 20 import android.app.AlertDialog; 21 import android.bluetooth.BluetoothDevice; 22 import android.content.BroadcastReceiver; 23 import android.content.Context; 24 import android.content.DialogInterface; 25 import android.content.Intent; 26 import android.content.IntentFilter; 27 import android.content.res.Resources; 28 import android.os.Bundle; 29 30 import com.android.nfc.R; 31 32 public class ConfirmConnectActivity extends Activity { 33 BluetoothDevice mDevice; 34 AlertDialog mAlert = null; 35 @Override 36 protected void onCreate(Bundle savedInstanceState) { 37 super.onCreate(savedInstanceState); 38 AlertDialog.Builder builder = new AlertDialog.Builder(this, 39 AlertDialog.THEME_DEVICE_DEFAULT_LIGHT); 40 Intent launchIntent = getIntent(); 41 mDevice = launchIntent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 42 if (mDevice == null) finish(); 43 Resources res = getResources(); 44 String confirmString = String.format(res.getString(R.string.confirm_pairing), 45 launchIntent.getStringExtra(BluetoothDevice.EXTRA_NAME)); 46 builder.setMessage(confirmString) 47 .setCancelable(false) 48 .setPositiveButton(res.getString(R.string.pair_yes), 49 new DialogInterface.OnClickListener() { 50 public void onClick(DialogInterface dialog, int id) { 51 Intent allowIntent = new Intent(BluetoothPeripheralHandover.ACTION_ALLOW_CONNECT); 52 allowIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice); 53 sendBroadcast(allowIntent); 54 ConfirmConnectActivity.this.mAlert = null; 55 ConfirmConnectActivity.this.finish(); 56 } 57 }) 58 .setNegativeButton(res.getString(R.string.pair_no), 59 new DialogInterface.OnClickListener() { 60 public void onClick(DialogInterface dialog, int id) { 61 Intent denyIntent = new Intent(BluetoothPeripheralHandover.ACTION_DENY_CONNECT); 62 denyIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice); 63 sendBroadcast(denyIntent); 64 ConfirmConnectActivity.this.mAlert = null; 65 ConfirmConnectActivity.this.finish(); 66 } 67 }); 68 mAlert = builder.create(); 69 mAlert.show(); 70 71 registerReceiver(mReceiver, 72 new IntentFilter(BluetoothPeripheralHandover.ACTION_TIMEOUT_CONNECT)); 73 } 74 75 @Override 76 protected void onDestroy() { 77 unregisterReceiver(mReceiver); 78 if (mAlert != null) { 79 mAlert.dismiss(); 80 Intent denyIntent = new Intent(BluetoothPeripheralHandover.ACTION_DENY_CONNECT); 81 denyIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice); 82 sendBroadcast(denyIntent); 83 mAlert = null; 84 } 85 super.onDestroy(); 86 } 87 88 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 89 @Override 90 public void onReceive(Context context, Intent intent) { 91 if (BluetoothPeripheralHandover.ACTION_TIMEOUT_CONNECT.equals(intent.getAction())) { 92 finish(); 93 } 94 } 95 }; 96 } 97