summaryrefslogtreecommitdiff
path: root/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java
blob: 56036841a867100a705d646f5bae39b6ffc8a532 (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
/* -*- Mode: C++; 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 android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.Messenger;

public class CommunicationService extends Service implements Runnable {

    public enum State {
        DISCONNECTED, SEARCHING, CONNECTING, CONNECTED
    };

    /**
     * Used to protect all writes to mState, mStateDesired, and mServerDesired.
     */
    private Object mConnectionVariableMutex = new Object();

    private State mState = State.DISCONNECTED;

    private State mStateDesired = State.DISCONNECTED;

    private Server mServerDesired = null;

    @Override
    public void run() {
        while (true) {
            // Condition
            try {
                wait();
            } catch (InterruptedException e) {
                // We don't care.
            }
            // Work
            synchronized (mConnectionVariableMutex) {
                if ((mStateDesired == State.CONNECTED && mState == State.CONNECTED)
                                || (mStateDesired == State.DISCONNECTED && mState == State.CONNECTED)) {
                    mClient.closeConnection();
                    mState = State.DISCONNECTED;
                }
                if (mStateDesired == State.CONNECTED) {
                    switch (mServerDesired.getProtocol()) {
                    case NETWORK:
                        mClient = new NetworkClient(
                                        mServerDesired.getAddress(), this);
                        mTransmitter = new Transmitter(mClient);
                        mClient.setReceiver(mReceiver);
                        break;
                    case BLUETOOTH:
                        break;
                    }
                    mState = State.CONNECTED;
                }
            }
        }

    }

    public void startSearching() {
        synchronized (mConnectionVariableMutex) {
            if (mState == State.CONNECTING || mState == State.CONNECTED) {
                disconnect();
            }
            mFinder.startFinding();
            mState = State.SEARCHING;
        }
    }

    public void stopSearching() {
        synchronized (mConnectionVariableMutex) {
            mFinder.stopFinding();
            mState = State.DISCONNECTED;
        }
    }

    public void connectTo(Server aServer) {
        synchronized (mConnectionVariableMutex) {
            if (mState == State.SEARCHING) {
                mFinder.stopFinding();
                mState = State.DISCONNECTED;
            }
            mServerDesired = aServer;
            mStateDesired = State.CONNECTED;
            notify();
        }
        // TODO: connect
    }

    public void disconnect() {
        synchronized (mConnectionVariableMutex) {
            mStateDesired = State.DISCONNECTED;
            notify();
        }
    }

    /**
     * Return the service to clients.
     */
    public class CBinder extends Binder {
        public CommunicationService getService() {
            return CommunicationService.this;
        }
    }

    private final IBinder mBinder = new CBinder();

    public static final int MSG_SLIDESHOW_STARTED = 1;
    public static final int MSG_SLIDE_CHANGED = 2;
    public static final int MSG_SLIDE_PREVIEW = 3;
    public static final int MSG_SLIDE_NOTES = 4;

    public static final String MSG_SERVERLIST_CHANGED = "SERVERLIST_CHANGED";
    public static final String MSG_PAIRING_STARTED = "PAIRING_STARTED";
    public static final String MSG_PAIRING_SUCCESSFUL = "PAIRING_SUCCESSFUL";

    private Transmitter mTransmitter;

    private Client mClient;

    private Receiver mReceiver = new Receiver();

    private ServerFinder mFinder = new ServerFinder(this);

    public void setActivityMessenger(Messenger aActivityMessenger) {
        mReceiver.setActivityMessenger(aActivityMessenger);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return mBinder;
    }

    @Override
    public void onCreate() {
        // TODO Create a notification (if configured).
    }

    @Override
    public void onDestroy() {
        // TODO Destroy the notification (as necessary).
    }

    public Transmitter getTransmitter() {
        return mTransmitter;
    }

    public Server[] getServers() {
        return mFinder.getServerList();
    }

    public void startFindingServers() {
        mFinder.startFinding();
    }

    public void stopFindingServers() {
        mFinder.stopFinding();
    }

    public SlideShow getSlideShow() {
        return mReceiver.getSlideShow();
    }

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