summaryrefslogtreecommitdiff
path: root/app/src/main/java/com/javispedro/vndroid/ServerService.java
blob: 96420df3a294d6ee23572d21efcd35662f1a3398 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package com.javispedro.vndroid;

import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.content.res.Configuration;
import android.media.Image;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

import com.javispedro.vndroid.keymaps.AndroidKeyHandler;
import com.javispedro.vndroid.keymaps.SpanishKeyHandler;

import java.lang.ref.WeakReference;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;

public class ServerService extends Service {
    private static final String TAG = ServerService.class.getSimpleName();

    public static final String ACTION_START_SERVER = "ACTION_START_SERVER";
    public static final String ACTION_STOP_SERVER = "ACTION_STOP_SERVER";

    public class ServerBinder extends Binder {
        ServerService getService() {
            return ServerService.this;
        }
    }
    public interface ServerStatusCallback {
        void onServerStatusChanged();
        void onNumClientChanged();
    }

    private ServerBinder binder;
    private WeakReference<ServerStatusCallback> callback;

    private ScreenGrabber screenGrabber;
    private RFBServer rfbServer;
    private PointerEventOutput pointerOut;
    private KeyEventOutput keyOut;

    @Override
    public void onCreate() {
        Log.d(TAG, "onCreate");
        binder = new ServerBinder();
        ServerRunningNotification.initNotificationChannel(this);
        System.loadLibrary("native-lib");
    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "onDestroy");
        cleanupServer();
        binder = null;
        callback = null;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand intent=" + intent);
        switch (intent.getAction()) {
            case ACTION_START_SERVER:
                startServer();
                return START_REDELIVER_INTENT;
            case ACTION_STOP_SERVER:
                stopServer();
                return START_NOT_STICKY;
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (screenGrabber != null && screenGrabber.hasSizeChanged()) {
            Log.d(TAG, "screen size has changed");
            screenGrabber.updateScreenSize();
        }
    }

    public void setServerStatusCallback(ServerStatusCallback callback) {
        if (callback != null) {
            this.callback = new WeakReference<>(callback);
        } else {
            this.callback = null;
        }
    }

    public void startServer() {
        if (rfbServer != null) {
            Log.w(TAG, "cannot start, already started");
            return;
        }

        Log.d(TAG, "starting");

        Notification notification = ServerRunningNotification.build(this);
        startForeground(1, notification);

        if (!ControlService.isServiceStarted()) {
            Toast toast = Toast.makeText(this, R.string.toast_no_input_service, Toast.LENGTH_SHORT);
            toast.show();
        }

        pointerOut = new PointerEventOutput();
        keyOut = new KeyEventOutput();
        keyOut.addHandler(new AndroidKeyHandler());
        keyOut.addHandler(new SpanishKeyHandler());

        if (screenGrabber == null) {
            screenGrabber = new ScreenVirtualGrabber(this);
        }

        rfbServer = new RFBServer();
        rfbServer.setEventCallback(new EventCallback());
        rfbServer.start();

        screenGrabber.setCallback(new ScreenGrabberCallback());
        screenGrabber.start();

        notifyServerStatusChanged();
    }

    public void stopServer() {
        Log.d(TAG, "stopping");

        cleanupServer();

        stopForeground(true);
        stopSelf();

        notifyServerStatusChanged();
    }

    public boolean isServerActive() {
        return rfbServer != null;
    }

    public void setMediaProjectionResult(int resultCode, Intent data) {
        if (screenGrabber != null) {
            Log.w(TAG, "already have an screen grabber");
        }

        screenGrabber = new ScreenMirrorGrabber(this, resultCode, data);
    }

    public int getListeningDisplay() {
        return 0;
    }

    public List<String> getListeningIPAddresses() {
        LinkedList<String> result = new LinkedList<String>();
        try {
            for (Enumeration<NetworkInterface> netif_it = NetworkInterface.getNetworkInterfaces(); netif_it.hasMoreElements(); ) {
                NetworkInterface netif = netif_it.nextElement();

                for (Enumeration<InetAddress> addr_it = netif.getInetAddresses(); addr_it.hasMoreElements(); ) {
                    InetAddress addr = addr_it.nextElement();

                    if (addr.isLoopbackAddress()) continue;

                    result.add(addr.getHostAddress());
                }
            }
        } catch (Exception ex) {
            Log.w(TAG, "While enumerating IP addresses: " + ex.toString());
        }
        return result;
    }

    public int getNumClients() {
        return rfbServer.getNumClients();
    }

    private void cleanupServer() {
        if (rfbServer != null) {
            rfbServer.stop();
            rfbServer = null;
        }
        if (screenGrabber != null) {
            screenGrabber.stop();
            screenGrabber = null;
        }
        if (pointerOut != null) {
            pointerOut = null;
        }
        if (keyOut != null) {
            keyOut = null;
        }
    }

    private ServerStatusCallback getStatusCallback() {
        return callback == null ? null : callback.get();
    }

    private void notifyServerStatusChanged() {
        ServerStatusCallback cb = getStatusCallback();
        if (cb == null) return;
        cb.onServerStatusChanged();
    }

    private void notifyNumClientChanged() {
        ServerStatusCallback cb = getStatusCallback();
        if (cb == null) return;
        cb.onNumClientChanged();
    }

    private class ScreenGrabberCallback implements ScreenGrabber.Callback {
        @Override
        public void onImage(Image image) {
            if (rfbServer != null) {
                rfbServer.putImage(image);
            } else {
                image.close();
            }
        }
    }

    private class EventCallback implements RFBServer.EventCallback {
        @Override
        public void onPointerEvent(int buttonMask, int x, int y) {
            try {
                x = screenGrabber.scaleInputX(x);
                y = screenGrabber.scaleInputY(y);
                pointerOut.postPointerEvent((byte) buttonMask, x, y);
            } catch (Exception e) {
                Log.e(TAG, "Exception on pointer EventCallback: " + e.toString());
                e.printStackTrace();
                // Need to suppress the exception, otherwise we'll crash JNI
            }
        }

        @Override
        public void onKeyEvent(int key, boolean state) {
            try {
                keyOut.postKeyEvent(key, state);
            } catch (Exception e) {
                Log.e(TAG, "Exception on key EventCallback: " + e.toString());
                e.printStackTrace();
            }
        }

        @Override
        public void onClientEvent() {
            try {
                notifyNumClientChanged();
            } catch (Exception e) {
                Log.e(TAG, "Exception on client EventCallback: " + e.toString());
                e.printStackTrace();
            }
        }
    }
}