summaryrefslogtreecommitdiff
path: root/android/sdremote/src/org/libreoffice/impressremote/communication/BluetoothServersFinder.java
blob: e696a1ba14efb9e4698a1716f188a4b0526139a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* -*- 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.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.support.v4.content.LocalBroadcastManager;

import org.libreoffice.impressremote.util.BluetoothOperator;
import org.libreoffice.impressremote.util.Intents;

class BluetoothServersFinder extends BroadcastReceiver implements ServersFinder, Runnable {
    private static final int SEARCH_DELAY_IN_SECONDS = 5;

    private final Context mContext;

    private final Map<String, Server> mServers;

    public BluetoothServersFinder(Context aContext) {
        mContext = aContext;

        mServers = new HashMap<String, Server>();
    }

    @Override
    public void startSearch() {
        if (!BluetoothOperator.isAvailable()) {
            return;
        }

        if (BluetoothOperator.getAdapter().isDiscovering()) {
            return;
        }

        setUpBluetoothActionsReceiver();

        if (!BluetoothOperator.getAdapter().isEnabled()) {
            return;
        }

        BluetoothOperator.getAdapter().startDiscovery();
    }

    private void setUpBluetoothActionsReceiver() {
        IntentFilter aBluetoothActionsFilter = new IntentFilter();
        aBluetoothActionsFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        aBluetoothActionsFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        aBluetoothActionsFilter.addAction(BluetoothDevice.ACTION_FOUND);

        mContext.registerReceiver(this, aBluetoothActionsFilter);
    }

    @Override
    public void onReceive(Context aContext, Intent aIntent) {
        if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(aIntent.getAction())) {
            switch (aIntent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0)) {
                case BluetoothAdapter.STATE_ON: {
                    BluetoothOperator.getAdapter().startDiscovery();
                    Intent aNewIntent = Intents.buildBluetoothStateChangedIntent();
                    LocalBroadcastManager.getInstance(mContext).sendBroadcast(aNewIntent);
                    return;
                }

                case BluetoothAdapter.STATE_OFF: {
                    mServers.clear();
                    Intent aNewIntent = Intents.buildBluetoothStateChangedIntent();
                    LocalBroadcastManager.getInstance(mContext).sendBroadcast(aNewIntent);
                    return;
                }

                default:
                    return;
            }
        }

        if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(aIntent.getAction())) {
            startDiscoveryDelayed();
            return;
        }

        if (BluetoothDevice.ACTION_FOUND.equals(aIntent.getAction())) {
            BluetoothDevice aBluetoothDevice = aIntent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

            addServer(buildServer(aBluetoothDevice));
            callUpdatingServersList();
        }
    }

    private void startDiscoveryDelayed() {
        // Start discovery again after a small delay.
        // Check whether device is on in case the user manually
        // disabled Bluetooth.

        if (!BluetoothOperator.getAdapter().isEnabled()) {
            return;
        }

        Handler aDiscoveryHandler = new Handler();
        aDiscoveryHandler.postDelayed(this, TimeUnit.SECONDS.toMillis(SEARCH_DELAY_IN_SECONDS));
    }

    @Override
    public void run() {
        BluetoothOperator.getAdapter().startDiscovery();
    }

    private void addServer(Server aServer) {
        mServers.put(aServer.getAddress(), aServer);
    }

    private Server buildServer(BluetoothDevice aBluetoothDevice) {
        Server.Type aServerType = buildServerType(aBluetoothDevice);
        String aServerAddress = aBluetoothDevice.getAddress();
        String aServerName = aBluetoothDevice.getName();

        return Server.newBluetoothInstance(aServerType, aServerAddress, aServerName);
    }

    private Server.Type buildServerType(BluetoothDevice aBluetoothDevice) {
        int aBluetoothClass = aBluetoothDevice.getBluetoothClass().getMajorDeviceClass();

        switch (aBluetoothClass) {
            case BluetoothClass.Device.Major.COMPUTER:
                return Server.Type.COMPUTER;

            case BluetoothClass.Device.Major.PHONE:
                return Server.Type.PHONE;

            default:
                return Server.Type.UNDEFINED;
        }
    }

    private void callUpdatingServersList() {
        Intent aIntent = Intents.buildServersListChangedIntent();
        LocalBroadcastManager.getInstance(mContext).sendBroadcast(aIntent);
    }

    @Override
    public void stopSearch() {
        if (!BluetoothOperator.isAvailable()) {
            return;
        }

        tearDownBluetoothActionsReceiver();

        BluetoothOperator.getAdapter().cancelDiscovery();
    }

    private void tearDownBluetoothActionsReceiver() {
        try {
            mContext.unregisterReceiver(this);
        } catch (IllegalArgumentException e) {
            // Receiver not registered.
            // Fixed in Honeycomb: Android’s issue #6191.
        }
    }

    @Override
    public List<Server> getServers() {
        return new ArrayList<Server>(mServers.values());
    }
}

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