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