blob: 75310e1762e250a891fd769f143e3651db1d41f7 [file] [log] [blame]
nxpandroid64fd68c2015-09-23 16:45:15 +05301/*
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
17package com.android.nfc.handover;
18
19import android.app.Activity;
20import android.app.AlertDialog;
21import android.bluetooth.BluetoothDevice;
nxpandroid6fd9cdb2017-07-12 18:25:41 +053022import android.content.BroadcastReceiver;
23import android.content.Context;
nxpandroid64fd68c2015-09-23 16:45:15 +053024import android.content.DialogInterface;
25import android.content.Intent;
nxpandroid6fd9cdb2017-07-12 18:25:41 +053026import android.content.IntentFilter;
nxpandroid64fd68c2015-09-23 16:45:15 +053027import android.content.res.Resources;
28import android.os.Bundle;
29
30import com.android.nfc.R;
31
32public class ConfirmConnectActivity extends Activity {
33 BluetoothDevice mDevice;
nxpandroid1153eb32015-11-06 18:46:58 +053034 AlertDialog mAlert = null;
nxpandroid64fd68c2015-09-23 16:45:15 +053035 @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();
Nikhil Chhabra0bb13512018-01-08 21:00:37 +053044 String confirmString = String.format(res.getString(R.string.confirm_pairing),
45 launchIntent.getStringExtra(BluetoothDevice.EXTRA_NAME));
nxpandroid64fd68c2015-09-23 16:45:15 +053046 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);
nxpandroid1153eb32015-11-06 18:46:58 +053054 ConfirmConnectActivity.this.mAlert = null;
nxpandroid64fd68c2015-09-23 16:45:15 +053055 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);
nxpandroid1153eb32015-11-06 18:46:58 +053064 ConfirmConnectActivity.this.mAlert = null;
nxpandroid64fd68c2015-09-23 16:45:15 +053065 ConfirmConnectActivity.this.finish();
66 }
67 });
nxpandroid1153eb32015-11-06 18:46:58 +053068 mAlert = builder.create();
69 mAlert.show();
nxpandroid6fd9cdb2017-07-12 18:25:41 +053070
71 registerReceiver(mReceiver,
72 new IntentFilter(BluetoothPeripheralHandover.ACTION_TIMEOUT_CONNECT));
nxpandroid1153eb32015-11-06 18:46:58 +053073 }
74
75 @Override
nxpandroid6fd9cdb2017-07-12 18:25:41 +053076 protected void onDestroy() {
77 unregisterReceiver(mReceiver);
nxpandroid1153eb32015-11-06 18:46:58 +053078 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);
nxpandroid6fd9cdb2017-07-12 18:25:41 +053083 mAlert = null;
nxpandroid1153eb32015-11-06 18:46:58 +053084 }
nxpandroid6fd9cdb2017-07-12 18:25:41 +053085 super.onDestroy();
nxpandroid64fd68c2015-09-23 16:45:15 +053086 }
nxpandroid6fd9cdb2017-07-12 18:25:41 +053087
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 };
nxpandroid64fd68c2015-09-23 16:45:15 +053096}