File size: 13,177 Bytes
b2a0e4a
 
8e389a6
b2a0e4a
35c5808
b2a0e4a
35c5808
b2a0e4a
8e389a6
 
 
35c5808
8e389a6
fe82d72
67b521d
aa3b34e
8e389a6
7624539
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e389a6
aa3b34e
67b521d
35c5808
aa3b34e
 
 
67b521d
 
8e389a6
67b521d
8e389a6
67b521d
 
7624539
8e389a6
 
 
7624539
 
 
 
 
 
 
 
 
 
 
 
 
 
8e389a6
 
 
 
7624539
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e389a6
 
 
 
 
 
 
 
 
67b521d
 
8e389a6
67b521d
 
 
 
8e389a6
 
 
 
 
67b521d
 
 
 
8e389a6
 
 
7624539
8e389a6
 
 
 
67b521d
 
 
8e389a6
67b521d
 
8e389a6
 
67b521d
8e389a6
67b521d
 
 
 
 
 
 
 
 
 
 
 
 
 
8e389a6
 
 
 
 
 
 
 
 
 
 
67b521d
 
 
 
 
 
 
7624539
67b521d
 
 
 
7624539
67b521d
 
 
 
 
 
 
 
 
 
 
7624539
67b521d
 
 
 
 
 
 
 
 
 
7624539
67b521d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7624539
67b521d
 
 
 
7624539
67b521d
 
 
8e389a6
 
 
67b521d
8e389a6
 
7624539
8e389a6
 
 
 
7624539
67b521d
8e389a6
 
 
67b521d
8e389a6
67b521d
7624539
67b521d
 
7624539
8e389a6
 
 
7624539
 
 
 
67b521d
 
 
8e389a6
67b521d
7624539
67b521d
 
 
7624539
67b521d
 
 
7624539
 
 
 
67b521d
 
 
7624539
 
 
 
 
 
 
 
 
 
 
 
 
 
67b521d
 
7624539
67b521d
 
 
 
8e389a6
 
 
 
67b521d
8e389a6
67b521d
7624539
 
 
 
 
 
67b521d
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!/bin/bash

export DISPLAY=:99
export ANDROID_SDK_ROOT=/opt/android-sdk
export ANDROID_HOME=/opt/android-sdk
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
export PATH=$PATH:$ANDROID_HOME/cmdline-tools/latest/bin:$ANDROID_HOME/platform-tools:/opt/android-studio/bin

# Clean locks
rm -f /tmp/.X99-lock /tmp/.X11-unix/X99

echo "Starting Xvfb..."
Xvfb :99 -screen 0 1280x800x16 -ac &

sleep 3

echo "Starting Openbox..."
# Create a minimal menu file to avoid the warning
mkdir -p /var/lib/openbox
cat > /var/lib/openbox/debian-menu.xml << 'EOF'
<?xml version="1.0" encoding="UTF-8"?>
<openbox_menu>
    <menu id="root-menu" label="Openbox">
        <item label="Android Studio">
            <action name="Execute">
                <command>/opt/android-studio/bin/studio.sh</command>
            </action>
        </item>
        <item label="Terminal">
            <action name="Execute">
                <command>xterm</command>
            </action>
        </item>
    </menu>
</openbox_menu>
EOF

openbox --replace &

sleep 2

echo "Starting Android Studio..."
/opt/android-studio/bin/studio.sh &

sleep 5

echo "Starting VNC..."
x11vnc -display :99 -nopw -forever -shared -rfbport 5900 -defer 10 -wait 10 -q &

sleep 3

# Create a simple landing page that redirects to vnc.html
cat > /usr/share/novnc/index.html << 'EOF'
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="refresh" content="0; url=/vnc.html">
    <title>Redirecting to Android Studio VNC</title>
</head>
<body>
    Redirecting to <a href="/vnc.html">Android Studio VNC</a>...
</body>
</html>
EOF

# Create a custom vnc.html that works with HF Spaces
cat > /usr/share/novnc/vnc.html << 'EOF'
<!DOCTYPE html>
<html>
<head>
    <title>Android Studio on Hugging Face</title>
    <meta charset="utf-8">
    <style>
        body {
            margin: 0;
            padding: 0;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: #1e1e1e;
            color: #fff;
            height: 100vh;
            display: flex;
            flex-direction: column;
        }
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 10px 20px;
            background: #2d2d2d;
            border-bottom: 2px solid #3ddc84;
        }
        .title h1 {
            margin: 0;
            font-size: 1.3rem;
            color: #3ddc84;
        }
        .badge {
            background: #3ddc84;
            color: #000;
            padding: 2px 8px;
            border-radius: 12px;
            font-size: 0.7rem;
            font-weight: bold;
            margin-left: 10px;
        }
        .controls {
            display: flex;
            gap: 8px;
            align-items: center;
        }
        button {
            background: #3ddc84;
            color: #000;
            border: none;
            padding: 6px 12px;
            border-radius: 4px;
            cursor: pointer;
            font-weight: bold;
            font-size: 0.8rem;
        }
        button.secondary {
            background: transparent;
            color: #3ddc84;
            border: 1px solid #3ddc84;
        }
        button:disabled {
            opacity: 0.5;
            cursor: not-allowed;
        }
        .vnc-container {
            flex: 1;
            background: #000;
            position: relative;
            overflow: hidden;
        }
        #vnc {
            width: 100%;
            height: 100%;
        }
        .status {
            display: flex;
            align-items: center;
            gap: 5px;
            margin-right: 15px;
            font-size: 0.8rem;
        }
        .status-dot {
            width: 8px;
            height: 8px;
            border-radius: 50%;
            display: inline-block;
        }
        .status-dot.connected {
            background: #3ddc84;
            animation: pulse 2s infinite;
        }
        .status-dot.connecting {
            background: #ffaa00;
        }
        .status-dot.disconnected {
            background: #ff4444;
        }
        @keyframes pulse {
            0% { box-shadow: 0 0 0 0 rgba(61, 220, 132, 0.7); }
            70% { box-shadow: 0 0 0 6px rgba(61, 220, 132, 0); }
        }
        .loading {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            text-align: center;
            z-index: 10;
            background: rgba(30,30,30,0.9);
            padding: 20px;
            border-radius: 8px;
            border: 1px solid #3ddc84;
        }
        .spinner {
            border: 3px solid #f3f3f3;
            border-top: 3px solid #3ddc84;
            border-radius: 50%;
            width: 30px;
            height: 30px;
            animation: spin 1s linear infinite;
            margin: 0 auto 10px;
        }
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        .footer {
            text-align: center;
            padding: 5px;
            background: #2d2d2d;
            font-size: 0.7rem;
            color: #888;
        }
        .footer a {
            color: #3ddc84;
            text-decoration: none;
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="title">
            <span class="badge">HF Space</span>
            <h1>🤖 Android Studio</h1>
        </div>
        <div class="controls">
            <div class="status">
                <span class="status-dot disconnected" id="status-dot"></span>
                <span id="status-text">Disconnected</span>
            </div>
            <button onclick="toggleViewOnly()" class="secondary" id="viewOnlyBtn" disabled>View Only: OFF</button>
            <button onclick="sendCtrlAltDel()" class="secondary" id="ctrlAltDelBtn" disabled>Ctrl+Alt+Del</button>
            <button onclick="reconnect()" id="reconnectBtn">Reconnect</button>
        </div>
    </div>
    
    <div class="vnc-container">
        <div id="vnc"></div>
        <div class="loading" id="loading">
            <div class="spinner"></div>
            <div id="loading-text">Connecting to Android Studio...</div>
        </div>
    </div>
    
    <div class="footer">
        ⚡ Running on Hugging Face Spaces | <a href="#" onclick="toggleFullscreen()">Fullscreen</a>
    </div>

    <script>
        let rfb;
        let retryCount = 0;
        const maxRetries = 10;
        
        const loading = document.getElementById('loading');
        const loadingText = document.getElementById('loading-text');
        const statusDot = document.getElementById('status-dot');
        const statusText = document.getElementById('status-text');
        const viewOnlyBtn = document.getElementById('viewOnlyBtn');
        const ctrlAltDelBtn = document.getElementById('ctrlAltDelBtn');
        
        function setStatus(state, message) {
            statusDot.className = 'status-dot ' + state;
            statusText.textContent = message;
        }
        
        function toggleViewOnly() {
            if (rfb) {
                rfb.viewOnly = !rfb.viewOnly;
                viewOnlyBtn.textContent = 'View Only: ' + (rfb.viewOnly ? 'ON' : 'OFF');
            }
        }
        
        function sendCtrlAltDel() {
            if (rfb) rfb.sendCtrlAltDel();
        }
        
        function toggleFullscreen() {
            const container = document.querySelector('.vnc-container');
            if (document.fullscreenElement) {
                document.exitFullscreen();
            } else {
                container.requestFullscreen();
            }
        }
        
        function reconnect() {
            if (rfb) rfb.disconnect();
            setTimeout(connect, 1000);
        }
        
        function connect() {
            loading.style.display = 'block';
            loadingText.textContent = 'Connecting to Android Studio...';
            setStatus('connecting', 'Connecting...');
            viewOnlyBtn.disabled = true;
            ctrlAltDelBtn.disabled = true;
            
            // Hugging Face Spaces WebSocket endpoint
            const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
            const host = window.location.host;
            const wsUrl = `${protocol}//${host}/proxy/7860/websockify`;
            
            console.log('Connecting to:', wsUrl);
            
            try {
                rfb = new RFB(document.getElementById('vnc'), wsUrl, {
                    credentials: { password: '' },
                    shared: true,
                    viewOnly: false,
                    resizeSession: true
                });
                
                rfb.addEventListener('connect', () => {
                    loading.style.display = 'none';
                    setStatus('connected', 'Connected');
                    viewOnlyBtn.disabled = false;
                    ctrlAltDelBtn.disabled = false;
                    retryCount = 0;
                });
                
                rfb.addEventListener('disconnect', () => {
                    loading.style.display = 'block';
                    setStatus('disconnected', 'Disconnected');
                    viewOnlyBtn.disabled = true;
                    ctrlAltDelBtn.disabled = true;
                    
                    if (retryCount < maxRetries) {
                        retryCount++;
                        const delay = Math.min(1000 * retryCount, 10000);
                        loadingText.textContent = `Reconnecting in ${delay/1000}s... (${retryCount}/${maxRetries})`;
                        setTimeout(connect, delay);
                    } else {
                        loadingText.textContent = 'Failed to connect. Click Reconnect.';
                    }
                });
                
                rfb.addEventListener('securityfailure', (e) => {
                    console.error('Security failure:', e.detail);
                    loadingText.textContent = 'Security error';
                });
                
            } catch (error) {
                console.error('Connection error:', error);
                loadingText.textContent = 'Connection error';
            }
        }
        
        window.onload = connect;
        
        window.addEventListener('resize', () => {
            if (rfb) setTimeout(() => rfb.resizeSession(), 100);
        });
    </script>
    <script src="vnc.js"></script>
</body>
</html>
EOF

# Create diagnostic page
cat > /usr/share/novnc/diagnostic.html << 'EOF'
<!DOCTYPE html>
<html>
<head>
    <title>HF Space Diagnostic</title>
    <style>
        body { background: #1e1e1e; color: #fff; font-family: monospace; padding: 20px; }
        pre { background: #2d2d2d; padding: 15px; border-radius: 5px; }
        .success { color: #3ddc84; }
        .error { color: #ff4444; }
        button { background: #3ddc84; color: #000; border: none; padding: 10px; border-radius: 4px; cursor: pointer; margin: 5px; }
    </style>
</head>
<body>
    <h1>🔧 Diagnostic Tool</h1>
    <button onclick="testWebSocket()">Test WebSocket</button>
    <button onclick="window.location.href='/vnc.html'">Go to VNC</button>
    <pre id="output"></pre>
    
    <script>
        const output = document.getElementById('output');
        
        async function testWebSocket() {
            output.textContent = 'Testing WebSocket connections...\n';
            
            const endpoints = [
                `/proxy/7860/websockify`,
                `/websockify`
            ];
            
            for (const endpoint of endpoints) {
                const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
                const url = `${protocol}//${window.location.host}${endpoint}`;
                
                output.textContent += `\nTesting: ${url}\n`;
                
                try {
                    const ws = new WebSocket(url);
                    await new Promise((resolve, reject) => {
                        ws.onopen = () => {
                            output.textContent += '✅ SUCCESS\n';
                            ws.close();
                            resolve();
                        };
                        ws.onerror = () => {
                            output.textContent += '❌ FAILED\n';
                            reject();
                        };
                        setTimeout(() => {
                            output.textContent += '⏱️ TIMEOUT\n';
                            reject();
                        }, 3000);
                    });
                } catch (e) {
                    output.textContent += `❌ ERROR: ${e.message}\n`;
                }
            }
        }
    </script>
</body>
</html>
EOF

echo "Starting noVNC with HF Spaces configuration..."
cd /usr/share/novnc

# Ensure vnc.js is available
if [ ! -f "vnc.js" ]; then
    ln -sf /usr/share/novnc/vnc.js . 2>/dev/null || true
fi

# Start websockify
websockify --web=/usr/share/novnc 0.0.0.0:7860 localhost:5900