nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 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; |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 22 | import android.content.BroadcastReceiver; |
| 23 | import android.content.Context; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 24 | import android.content.DialogInterface; |
| 25 | import android.content.Intent; |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 26 | import android.content.IntentFilter; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 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; |
nxpandroid | 1153eb3 | 2015-11-06 18:46:58 +0530 | [diff] [blame] | 34 | AlertDialog mAlert = null; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 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(); |
Nikhil Chhabra | 0bb1351 | 2018-01-08 21:00:37 +0530 | [diff] [blame] | 44 | String confirmString = String.format(res.getString(R.string.confirm_pairing), |
| 45 | launchIntent.getStringExtra(BluetoothDevice.EXTRA_NAME)); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 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); |
nxpandroid | 1153eb3 | 2015-11-06 18:46:58 +0530 | [diff] [blame] | 54 | ConfirmConnectActivity.this.mAlert = null; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 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); |
nxpandroid | 1153eb3 | 2015-11-06 18:46:58 +0530 | [diff] [blame] | 64 | ConfirmConnectActivity.this.mAlert = null; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 65 | ConfirmConnectActivity.this.finish(); |
| 66 | } |
| 67 | }); |
nxpandroid | 1153eb3 | 2015-11-06 18:46:58 +0530 | [diff] [blame] | 68 | mAlert = builder.create(); |
| 69 | mAlert.show(); |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 70 | |
| 71 | registerReceiver(mReceiver, |
| 72 | new IntentFilter(BluetoothPeripheralHandover.ACTION_TIMEOUT_CONNECT)); |
nxpandroid | 1153eb3 | 2015-11-06 18:46:58 +0530 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Override |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 76 | protected void onDestroy() { |
| 77 | unregisterReceiver(mReceiver); |
nxpandroid | 1153eb3 | 2015-11-06 18:46:58 +0530 | [diff] [blame] | 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); |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 83 | mAlert = null; |
nxpandroid | 1153eb3 | 2015-11-06 18:46:58 +0530 | [diff] [blame] | 84 | } |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 85 | super.onDestroy(); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 86 | } |
nxpandroid | 6fd9cdb | 2017-07-12 18:25:41 +0530 | [diff] [blame] | 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 | }; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 96 | } |