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.DialogInterface;
23 import android.content.Intent;
24 import android.content.res.Resources;
25 import android.os.Bundle;
26 
27 import com.android.nfc.R;
28 
29 public class ConfirmConnectActivity extends Activity {
30     BluetoothDevice mDevice;
31     AlertDialog mAlert = null;
32     @Override
onCreate(Bundle savedInstanceState)33     protected void onCreate(Bundle savedInstanceState) {
34         super.onCreate(savedInstanceState);
35         AlertDialog.Builder builder = new AlertDialog.Builder(this,
36                 AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
37         Intent launchIntent = getIntent();
38         mDevice = launchIntent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
39         if (mDevice == null) finish();
40         Resources res = getResources();
41         String deviceName = mDevice.getName() != null ? mDevice.getName() : "";
42         String confirmString = String.format(res.getString(R.string.confirm_pairing), deviceName);
43         builder.setMessage(confirmString)
44                .setCancelable(false)
45                .setPositiveButton(res.getString(R.string.pair_yes),
46                        new DialogInterface.OnClickListener() {
47                    public void onClick(DialogInterface dialog, int id) {
48                         Intent allowIntent = new Intent(BluetoothPeripheralHandover.ACTION_ALLOW_CONNECT);
49                         allowIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
50                         sendBroadcast(allowIntent);
51                         ConfirmConnectActivity.this.mAlert = null;
52                         ConfirmConnectActivity.this.finish();
53                    }
54                })
55                .setNegativeButton(res.getString(R.string.pair_no),
56                        new DialogInterface.OnClickListener() {
57                    public void onClick(DialogInterface dialog, int id) {
58                        Intent denyIntent = new Intent(BluetoothPeripheralHandover.ACTION_DENY_CONNECT);
59                        denyIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
60                        sendBroadcast(denyIntent);
61                        ConfirmConnectActivity.this.mAlert = null;
62                        ConfirmConnectActivity.this.finish();
63                    }
64                });
65         mAlert = builder.create();
66         mAlert.show();
67     }
68 
69     @Override
onStop()70     protected void onStop() {
71         super.onStop();
72         if (mAlert != null) {
73             mAlert.dismiss();
74             Intent denyIntent = new Intent(BluetoothPeripheralHandover.ACTION_DENY_CONNECT);
75             denyIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice);
76             sendBroadcast(denyIntent);
77         }
78     }
79 }
80