165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/*
265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Copyright (C) 2014 The Android Open Source Project
365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Licensed under the Apache License, Version 2.0 (the "License");
565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * you may not use this file except in compliance with the License.
665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * You may obtain a copy of the License at
765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *      http://www.apache.org/licenses/LICENSE-2.0
965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
1065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Unless required by applicable law or agreed to in writing, software
1165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * distributed under the License is distributed on an "AS IS" BASIS,
1265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * See the License for the specific language governing permissions and
1465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * limitations under the License.
1565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
1665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
1765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepackage com.android.tv.settings.accessories;
1865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
1965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.bluetooth.BluetoothDevice;
2065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.BroadcastReceiver;
2165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.Context;
2265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.Intent;
2365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
2465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/**
2565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * BluetoothPairingRequest is a receiver for any Bluetooth pairing request. It
2665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * starts the Bluetooth Pairing activity, displaying the PIN, the passkey or a
2765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * confirmation entry dialog.
2865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
2965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepublic final class BluetoothPairingRequest extends BroadcastReceiver {
3065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
3165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    @Override
3265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public void onReceive(Context context, Intent intent) {
3365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        String action = intent.getAction();
3465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (!action.equals(BluetoothDevice.ACTION_PAIRING_REQUEST)) {
3565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            return;
3665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
3765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
3865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        // convert broadcast intent into activity intent (same action string)
3965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        BluetoothDevice device =
4065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
4165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT,
4265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                BluetoothDevice.ERROR);
4365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        Intent pairingIntent = new Intent();
4465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        pairingIntent.setClass(context, BluetoothPairingDialog.class);
4565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        pairingIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
4665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        pairingIntent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, type);
4765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        if (type == BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION ||
4865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                type == BluetoothDevice.PAIRING_VARIANT_DISPLAY_PASSKEY ||
4965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                type == BluetoothDevice.PAIRING_VARIANT_DISPLAY_PIN) {
5065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            int pairingKey = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_KEY,
5165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                    BluetoothDevice.ERROR);
5265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            pairingIntent.putExtra(BluetoothDevice.EXTRA_PAIRING_KEY, pairingKey);
5365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
5465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        pairingIntent.setAction(BluetoothDevice.ACTION_PAIRING_REQUEST);
5565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        pairingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
5665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
5765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        // In Canvas, always start the pairing activity when we get the pairing broadcast,
5865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        // as opposed to displaying a notification that will start the pairing activity.
5965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        context.startActivity(pairingIntent);
6065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
6165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane}
62