/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package org.libreoffice.impressremote.communication;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;

public class BluetoothClient extends Client {
    // Standard UUID for the Serial Port Profile.
    // https://www.bluetooth.org/en-us/specification/assigned-numbers-overview/service-discovery
    private static final String STANDARD_SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";

    private final boolean mBluetoothWasEnabled;

    private final BluetoothAdapter mBluetoothAdapter;
    private BluetoothSocket mSocket;

    public BluetoothClient(Server aServer, CommunicationService aCommunicationService, Receiver aReceiver, boolean aBluetoothWasEnabled) {
        super(aServer, aCommunicationService, aReceiver);

        mBluetoothWasEnabled = aBluetoothWasEnabled;

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

        if (!mBluetoothWasEnabled) {
            mBluetoothAdapter.enable();
        }
    }

    @Override
    protected void setUpServerConnection() {
        mSocket = buildServerConnection();
    }

    private BluetoothSocket buildServerConnection() {
        try {
            BluetoothDevice aBluetoothServer = mBluetoothAdapter
                .getRemoteDevice(mServer.getAddress());

            mBluetoothAdapter.cancelDiscovery();

            BluetoothSocket aSocket = aBluetoothServer
                .createRfcommSocketToServiceRecord(
                    UUID.fromString(STANDARD_SPP_UUID));

            aSocket.connect();

            return aSocket;
        } catch (IOException e) {
            throw new RuntimeException("Unable to connect to Bluetooth host");
        }
    }

    protected InputStream buildMessagesStream() {
        try {
            return mSocket.getInputStream();
        } catch (IOException e) {
            throw new RuntimeException("Unable to open messages stream.");
        }
    }

    protected OutputStream buildCommandsStream() {
        try {
            return mSocket.getOutputStream();
        } catch (IOException e) {
            throw new RuntimeException("Unable to open commands stream.");
        }
    }

    @Override
    public void closeConnection() {
        try {
            mSocket.close();
        } catch (IOException e) {
            throw new RuntimeException("Unable to close Bluetooth socket.");
        }
    }

    protected void onDisconnect() {
        if (!mBluetoothWasEnabled) {
            mBluetoothAdapter.disable();
        }
    }

    @Override
    public void validating() throws IOException {
        String aMessage = mMessagesReader.readLine();

        if (!aMessage.equals(Protocol.Messages.PAIRED)) {
            return;
        }

        while (mMessagesReader.readLine().length() != 0) {
            // Get rid of extra lines
        }

        callSuccessfulPairing();

        startListening();
    }

    private void callSuccessfulPairing() {
        Intent aSuccessfulPairingIntent = new Intent(
            CommunicationService.MSG_PAIRING_SUCCESSFUL);

        LocalBroadcastManager.getInstance(mCommunicationService)
            .sendBroadcast(aSuccessfulPairingIntent);
    }
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

@@ -1053,62 +1053,8 @@
 d;%0A%0A
-    private final BluetoothAdapter mBluetoothAdapter;%0A
     
@@ -1343,75 +1343,8 @@
 d;%0A%0A
-        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();%0A%0A
     
@@ -1380,33 +1380,32 @@
 ) %7B%0A            
-m
 BluetoothAdapter
@@ -1405,16 +1405,36 @@
 Adapter.
+getDefaultAdapter().
 enable()
@@ -1676,17 +1676,16 @@
 erver = 
-m
 Bluetoot
@@ -1693,16 +1693,53 @@
 Adapter%0A
+                .getDefaultAdapter()%0A
         
@@ -1791,33 +1791,32 @@
 );%0A%0A            
-m
 BluetoothAdapter
@@ -1816,16 +1816,36 @@
 Adapter.
+getDefaultAdapter().
 cancelDi
@@ -2190,16 +2190,17 @@
 oth host
+.
 %22);%0A    
@@ -2982,33 +2982,32 @@
 ) %7B%0A            
-m
 BluetoothAdapter
@@ -3007,16 +3007,36 @@
 Adapter.
+getDefaultAdapter().
 disable(

Fix possible null pointer exception in the BluetoothClient.