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 onCreate(Bundle savedInstanceState)36 protected void onCreate(Bundle savedInstanceState) { 37 super.onCreate(savedInstanceState); 38 AlertDialog.Builder builder = new AlertDialog.Builder(this, 39 R.style.DialogAlertDayNight); 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 allowIntent.setPackage("com.android.nfc"); 54 sendBroadcast(allowIntent); 55 ConfirmConnectActivity.this.mAlert = null; 56 ConfirmConnectActivity.this.finish(); 57 } 58 }) 59 .setNegativeButton(res.getString(R.string.pair_no), 60 new DialogInterface.OnClickListener() { 61 public void onClick(DialogInterface dialog, int id) { 62 Intent denyIntent = new Intent(BluetoothPeripheralHandover.ACTION_DENY_CONNECT); 63 denyIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice); 64 denyIntent.setPackage("com.android.nfc"); 65 sendBroadcast(denyIntent); 66 ConfirmConnectActivity.this.mAlert = null; 67 ConfirmConnectActivity.this.finish(); 68 } 69 }); 70 mAlert = builder.create(); 71 mAlert.show(); 72 73 registerReceiver(mReceiver, 74 new IntentFilter(BluetoothPeripheralHandover.ACTION_TIMEOUT_CONNECT)); 75 } 76 77 @Override onDestroy()78 protected void onDestroy() { 79 unregisterReceiver(mReceiver); 80 if (mAlert != null) { 81 mAlert.dismiss(); 82 Intent denyIntent = new Intent(BluetoothPeripheralHandover.ACTION_DENY_CONNECT); 83 denyIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice); 84 denyIntent.setPackage("com.android.nfc"); 85 sendBroadcast(denyIntent); 86 mAlert = null; 87 } 88 super.onDestroy(); 89 } 90 91 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 92 @Override 93 public void onReceive(Context context, Intent intent) { 94 if (BluetoothPeripheralHandover.ACTION_TIMEOUT_CONNECT.equals(intent.getAction())) { 95 finish(); 96 } 97 } 98 }; 99 } 100