-
Notifications
You must be signed in to change notification settings - Fork 7
/
DroidScript.js
5033 lines (4299 loc) · 166 KB
/
DroidScript.js
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// DroidScript Main Application
//
// Copyright droidscript.org
//
// (Note: This source code is provided unobfuscated and commented in
// the hope that it is useful for educational purposes. It remains the
// copyright of droidscript.org)
//
//General globals.
var language = "English";
var layBack,layFront,laySamp,layDoc,scrollIcons,dlgShop;
var textSize=12, timer=0, lastLine=0;
var textChanged=false, lastProg="";
var tipCount=0, sharedApp=null;
var g_debugParams="", fix=1, dirty=false;
var isWebIDE=false; imgGrad = null;
var ignoreIcons=false, isSample = false;
var sharedFiles, sharedText, startedByShare;
var plgApk=null, playStore=null, purchases=[];
var pluginVersions=[], crypt=null;
var g_tester=false, g_sdk=false, hashCode="";
var tablet=false, lowRes=false, orient="Portrait";
var isHtml = false; var isSBC = false; var isEspruino = false;
var useADB = false; var premium = false;
var net=null, port=19800, udpTimer=0;
var usePass = true, password="",deviceName="";
var curProgram = null, lstDlgCopy=null;
var useSoftKeys=true, useYoyo=true, stayAwake=true, autoHelp=true, lastCursorLine=0;
var headless=false,useAce=false,txtDebug=null,forceOrient="",noGUI=false;
var useDarkTheme=false, usingAce=false;
var lastCursorPos = [], curProgTitle=null;
var espruino=null, term=null, webAce=null;
//Called when application is started.
function OnStart()
{
app.SetDebugEnabled( false );
app.ShowProgress("");
//Detect OS/hardware.
model = app.GetModel();
isChrome = app.IsChrome();
isThings = app.IsThings();
//Detect OS type (and switch to chrome mode if required).
var product = app.SysExec("getprop ro.build.product").toLowerCase();
isRemix = (product.indexOf("remix")>-1 || product.indexOf("nexbox")>-1 || product.indexOf("vim")>-1 );
if( isRemix ) isChrome = true;
//Set appropriate default text size and screen.
dens = app.GetScreenDensity();
var defTextSize = 12;
if( app.IsTablet() && dens < 200 ) defTextSize = 13;
//Load settings.
headless = (isChrome || isRemix || isThings);
textSize = app.LoadNumber( "TextSize", defTextSize );
useADB = app.LoadBoolean( "UseADB" );
useSoftKeys = app.LoadBoolean( "UseSoftKeys", headless?false:true );
useYoyo = app.LoadBoolean( "UseYoyo", headless?false:true );
autoHelp = app.LoadBoolean( "AutoHelp", true );
stayAwake = app.LoadBoolean( "StayAwake", true );
useDarkTheme = app.LoadBoolean( "UseDarkTheme", false );
usePass = app.LoadBoolean( "UsePass", headless?false:true );
password = app.LoadText( "Password", Math.random().toString(36).substr(4,4) );
deviceName = app.LoadText( "DeviceName", app.GetModel() );
language = app.LoadText( "Language", "English" );
autoWifi = app.LoadBoolean( "_AutoWifi", false, "spremote" );
autoStart = app.LoadText( "_AutoStart", null, "spremote" );
wifiSSID = app.LoadText( "WifiSSID", null );
wifiKey = app.LoadText( "WifiKey", null );
//Override settings with config file if found.
try {
var configFile = "/sdcard/DroidScript/config.json";
if( app.FileExists( configFile ) )
{
var conFile = app.ReadFile( configFile );
var config = JSON.parse( conFile );
if( config.useADB!=null ) useADB = config.useADB;
if( config.useSoftKeys!=null ) useSoftKeys = config.useSoftKeys;
if( config.useYoyo!=null ) useYoyo = config.useYoyo;
if( config.autoHelp!=null ) autoHelp = config.autoHelp;
if( config.useDarkTheme!=null ) useDarkTheme = config.useDarkTheme;
if( config.stayAwake!=null ) stayAwake = config.stayAwake;
if( config.usePass!=null ) usePass = config.usePass;
if( config.password!=null ) password = config.password;
if( config.deviceName!=null ) deviceName = config.deviceName;
if( config.language!=null ) language = config.language;
if( config.autoWifi!=null ) autoWifi = config.autoWifi;
if( config.autoStart!=null ) autoStart = config.autoStart;
if( config.wifiSSID!=null ) wifiSSID = config.wifiSSID;
if( config.wifiKey!=null ) wifiKey = config.wifiKey;
if( config.useAce!=null ) { useAce = config.useAce; if(useAce) headless=true; }
if( config.useChrome!=null ) { isChrome = config.useChrome; if(isChrome) headless=true; }
if( config.orientation!=null ) forceOrient = config.orientation;
if( config.noGUI!=null ) { noGUI = config.noGUI; if(noGUI) headless=true; }
}
}
catch(e){}
//Set T() translation language.
app.SetLanguage( language );
//Save password and device name for IDE web server use.
app.SaveBoolean( "UsePass", usePass );
app.SaveText( "Password", password );
app.SaveText( "DeviceName", deviceName );
//Get app name and path.
appName = app.GetName();
appPath = "/sdcard/" + appName;
//Extract samples and Wifi editor to private folder if not done.
if( app.IsNewVersion() )
{
app.ExtractAssets( "sdcard/"+app.Language2Code(), appPath, true );
var editDir = appPath+"/.edit";
app.ExtractAssets( "edit", editDir, true );
}
//Delete demo and temp folders.
app.DeleteFolder( appPath+"/.demo" );
tempFldr = "/sdcard/.DroidScript/Temp";
app.DeleteFolder( tempFldr );
app.MakeFolder( tempFldr );
//Keep screen awake if required.
if( stayAwake ) app.PreventScreenLock( "Full" );
//Connect network if a specific ssid is set.
if( wifiSSID ) app.WifiConnect( wifiSSID, wifiKey );
//Lock screen orientation to Portrait at startup (if phone app).
startOrient = app.GetOrientation();
if( headless || forceOrient.toLowerCase()=="landscape" ) OnRotate();
else app.SetOrientation( "Portrait", OnRotate );
//Use material style theme for chrome.
if( isChrome && !noGUI ) CreateTheme();
}
//Called when forced rotation is complete.
function OnRotate()
{
orient = app.GetOrientation();
isPortrait = (orient=="Portrait");
console.log( orient );
//Get screen dimensions.
sw = app.GetScreenWidth();
sh = app.GetScreenHeight();
tablet = app.IsTablet(); //( (sw > 1200 || sh > 1200) && dens < 320 );
lowRes = ( sw < 800 && sh < 800 );
//Get display dimensions and aspect ratio
dw = app.GetDisplayWidth();
dh = app.GetDisplayHeight();
aspect = dw / dh;
console.log( aspect );
//Set theme.
var theme = app.CreateTheme();
theme.SetBackColor( "#bb000000" );
theme.SetDialogColor( "#bb000000" );
theme.SetTitleHeight( 42 );
app.SetTheme( theme );
//Set menus.
menus = "New:Add.png,Connect:Connect.png,Exit:Exit.png";
SetMenus( menus, "/assets/Img" );
//Disable normal back key functionality
//(unless we are launched from another app).
//if( !sharedFiles && !sharedText )
app.EnableBackKey( false );
//Register for error broadcasts.
app.SetOnBroadcast( app_OnBroadcast );
//Check for test mode and SDK mode.
if( app.FileExists( "/sdcard/DroidScript/_beta_") ) g_tester = true;
if( app.FileExists( "/sdcard/DroidScript/_sdk_") ) g_sdk = true;
//Set or unlock orientation.
if( model.indexOf("ODROID") > -1 || forceOrient.toLowerCase()=="landscape" || headless )
{
if( !isThings ) setTimeout( function(){app.SetOrientation( "Landscape" )},100 );
else app.SetOrientation( "Landscape" );
isSBC = true;
}
else setTimeout( function(){app.SetOrientation("")},100 );
//Create the main graphical layout.
if( !noGUI && (isRemix || isChrome || useAce) ) CreateAceLayout();
else if( headless ) CreateHeadlessLayout( true );
else CreateLayout();
//Detect keyboard showing and set focus to editor.
app.SetOnShowKeyboard( app_OnShowKeyBoard );
//Check for plugin licenses and handle plugins.
CheckLicenses();
ConvertApkPlugins();
ImportUserPlugins();
ListPlugins();
//Check for auto wifi or chrome connect.
if( autoWifi || headless ) Connect( false );
//Get samples list for chrome etc.
if( headless ) GetSamples();
app.HideProgress();
//Check for auto-start app.
if( autoStart && autoStart!="null" && autoStart!="false" )
LaunchApp( autoStart, "", "remote" );
}
//Called when shared data is received.
function OnData( isStartUp )
{
//Check for any shared files from other apps
//and pass on to user's programs.
sharedFiles = app.GetSharedFiles();
sharedText = app.GetSharedText();
sharedIntent = app.GetIntent();
app.SaveText( "_SharedFiles", sharedFiles, "spremote" );
app.SaveText( "_SharedText", sharedText, "spremote" );
//Handle spk files.
if( sharedFiles && sharedFiles[0].indexOf(".spk")>-1 )
{
spkFile = sharedFiles[0];
spkTitle = spkFile.substr( spkFile.lastIndexOf("/")+1 );
var s = spkTitle;
if( s=="download.spk" ) s = "this package";
var installDiag = app.CreateYesNoDialog( "Script Package Installer:\n\nDo you trust the source of "+s+"?" );
installDiag.SetOnTouch( installDiag_OnTouch );
installDiag.Show();
}
//Check if we need to auto-launch user app.
else if( sharedFiles || sharedText )
{
sharedApp = app.LoadText( "_SharedApp", null, "spremote" );
if( sharedApp )
{
startedByShare = isStartUp;
LaunchApp( sharedApp, "" );
}
}
//Check for pebble app running inside IDE.
var intent = app.GetIntent();
if( intent ) {
for( var key in intent.extras ) {
if( key=="Pebble_AppName" ) LaunchApp( intent.extras[key], "" );
}
}
//else share general intent data.
//**** Not sure we need this now, as it is best to call NewActivity directly ******
/*else {
//if( sharedIntent && sharedIntent.extras.app )
// LaunchApp( sharedIntent.extras.app, JSON.stringify(sharedIntent) );
}*/
}
//Called when back button is pressed.
function OnBack()
{
if( txtRemote.IsVisible() ) {
txtRemote_OnTouchDown();
return;
}
if( headless ) return;
if( layMenu.GetVisibility()=="Show" ) {
btnMenu_OnTouch();
}
else if( layDoc.GetVisibility()=="Show" ) {
if( app.GetData("CurWebDoc")=="Documentation" ) btnDocs_OnTouch();
else webDocs.Execute( "history.back();" );
}
else if( laySamp.GetVisibility()=="Show" )
{
if( layEditSamp.GetVisibility()=="Show" ) layEditSamp.SetVisibility( "Hide" );
else btnSamp_OnTouch();
HideCodingMenus();
}
else if( layFile.IsVisible() )
{
btnFiles_OnTouch();
edit.Focus();
}
else if( laySrch.IsVisible() || layCC.IsVisible() )
{
HideCodingMenus( true );
edit.Focus();
}
else if( layEdit.GetVisibility()=="Show" )
{
//Save user's changes and hide menus.
SaveFile();
btnFiles.Hide();
layFile.Hide();
HideCodingMenus();
if( layCopy.GetVisibility()=="Show" ) edit_OnDoubleTap();
//Animate flip and stop timer.
layFlip.Animate( "Flip", null, 350 );
clearTimeout( timer );
}
else {
//if( !sharedFiles && !sharedText ) {
var yesNo = app.CreateYesNoDialog( T("ExitDroidScript") );
yesNo.SetOnTouch( yesNoExit_OnTouch );
yesNo.Show();
//}
}
}
//Called when application is paused.
function OnPause()
{
}
//Called when application is resumed.
function OnResume()
{
if( sharedApp && startedByShare ) app.Exit();
}
//Handle Exit app choice.
function yesNoExit_OnTouch( result )
{
if( result=="Yes" ) app.Exit();
}
//Set current language.
function SetLanguage()
{
//Reset T() translation language.
app.SetLanguage( language );
//Load docs.
var code = "-" + app.Language2Code( language );
if( code=="-en" ) code = "";
docsPath = "/sdcard/DroidScript/.edit/docs" + code;
webDocs.LoadUrl( "file://" + docsPath + "/Docs.htm" );
//Re-load sample list.
lstSamp.SetList( GetSamples() );
//Re-load menus.
LoadMenus();
//Re-Extract the 'Hello World' sample.
app.ExtractAssets( "sdcard/"+app.Language2Code(), appPath, true );
//Save language code (use by ide server).
app.SaveText( "LanguageCode", app.Language2Code() );
}
//Handle spk installs.
function installDiag_OnTouch( result )
{
if( result=="Yes" ) CheckPackage( spkFile );
else app.ShowPopup( "Package rejected" );
}
//Set spacing of title and margins.
function SetTitleMargins()
{
var left,right,top,bottom,width,height;
//Set title bar size.
layBar.SetSize( -1, topBarHeight );
//Set title image size and margins.
if( orient=="Portrait" ) {
width = (tablet?0.34:0.34); height = (tablet?0.040:0.055);
left = (tablet?0.28:0.21); right = (tablet?0.28:0.21);
top = (lowRes?0.018:(tablet?0.012:0.014)); bottom = (lowRes?0.018:(tablet?0.012:0.014));
if( tablet ) scale=0.63; else scale=0.24;
}
else {
width = (tablet?0.24:0.24); height = (tablet?0.065:0.100);
left = (tablet?0.46:0.340); right = (tablet?0.46:0.340);
top = (lowRes?0.028:(tablet?0.022:0.025)); bottom = (lowRes?0.028:(tablet?0.022:0.025));
if( tablet ) scale=0.53; else scale=0.72;
}
//imgTitle.SetSize( width, height );
//imgTitle.SetMargins( left*scale,top,right*scale,bottom );
btnFiles.SetSize( width, height ); //Note: Test these margins on multiple tablets!!
if( orient=="Portrait" )
btnFiles.SetMargins( tablet?(aspect>0.67?0.14:0.17):8,0,tablet?0.14:8,0,tablet?"":"dip" );
else btnFiles.SetMargins( tablet?(aspect>0.67?0.26:0.28):0.2, 0, tablet?0.26:0.2, 0 );
//Set title button sizes.
if( orient=="Portrait" ) { width = (tablet?48:48); height = (tablet?36:36); }
else { width = (tablet?48:48); height = (tablet?36:36); }
btnDocs.SetSize( width, height, "dip" );
btnConnect.SetSize( width, height, "dip" );
btnFiles.SetSize( -1, height, "dip" );
btnSamp.SetSize( width, height, "dip" );
btnMenu.SetSize( width, height, "dip" );
}
//Re-set spacings and sizes of controls.
function AdjustLayout()
{
var left,right,top,bottom,width,height;
//Set samples and docs top padding (so we can see title bar).
// if( orient=="Portrait" ) top = (lowRes?0.104:(tablet?0.066:0.080));
//else top =topBarHeight
laySamp.SetPadding( 0, topBarHeight, 0, 0 );
layDoc.SetPadding( 0, topBarHeight, 0, 0 );
//Reset programs list and gradient cover layer size.
width = 1.0; height = 0.94;
//lst.SetSize( width, height );
//lst.SetPadding( (tablet?0.2:0.12), 0.01, (tablet?0.2:0.12), 0.01 );
//imgGrad.SetSize( width, height );
//Reset code editor dimensions.
if( orient=="Portrait" ) height = (lowRes?0.83:(tablet?0.88:0.858));
else height = (lowRes?0.83:(tablet?0.81:0.79));
//xx scrollEdit.SetSize( 1.0, height );
//xx if( orient=="Portrait" ) edit.SetSize( 1.5, -1 );
//xx else edit.SetSize( 1.0, -1 );
//edit.SetSize( 1.0, height );
var barh = (orient=="Portrait"? bottomBarHeight : bottomBarHeight );
edit.SetSize( 1.0, 1-topBarHeight-barh );
layEditBtns.SetSize( 1.0, bottomBarHeight);
//Set code editor button height.
if( orient=="Portrait" ) height = (tablet?0.04:0.052);
else height = (tablet?0.06:0.082);
var width = 0.135;
btnUndo.SetSize( width, height );
btnRedo.SetSize( width, height );
btnNew.SetSize( width, height );
btnAsset.SetSize( width, height );
btnDbg.SetSize( width, height );
btnExec.SetSize( width, height );
//Reset sample editor dimensions.
if( orient=="Portrait" ) height = (lowRes?0.83:(tablet?0.88:0.86));
else height = (lowRes?0.83:(tablet?0.81:0.76));
//xx scroll.SetSize( 1.0, height );
//xx if( orient=="Portrait" ) editSamp.SetSize( 1.5, -1 );
//xx else editSamp.SetSize( 1.0, -1 );
editSamp.SetSize( 1.0, height );
//Set sample edtor button height.
if( orient=="Portrait" ) height = (tablet?0.044:0.052);
else height = (tablet?0.066:0.082);
btnCopy.SetSize( 0.26, height );
btnRun.SetSize( 0.26, height );
//Set documentation size.
webDocs.SetSize( 1.0, 1-topBarHeight );
//Set menu position.
if( orient=="Portrait" ) layMenu.SetPosition( tablet?0.6:0.5, topBarHeight );
else layMenu.SetPosition( 0.72, topBarHeight );
//Set file list position.
layFile.SetPosition( isPortrait?0.3:0.385, topBarHeight );
}
//Called when configuration changes.
function OnConfig()
{
if( headless ) return;
try
{
//Resize terminal if visible.
if( term ) term.Resize();
//Get new orientation.
orient = app.GetOrientation();
isPortrait = (orient=="Portrait");
//Calculate scaling val to keep objects same size.
if( orient=="Portrait" ) fix = 1;
else fix = dw/dh;
//Re-position controls.
PrepareCodingMenus();
SetTitleMargins();
AdjustLayout();
ResizeCodingMenus();
ShowIcons();
ignoreIcons = false;
}
catch(e) {}
}
//Handle wifi connect button.
function btnConnect_OnTouch()
{
HandleMenu( "Connect" );
}
//Load the main menus.
function LoadMenus()
{
var sdk = (g_sdk || premium ? ",SDK": "" );
lstMenu.SetList( T("New")+","+T("Plugins")+","+T("Settings")+","+T("Demos")
+","+T("News")+","+T("Premium")+sdk+","+T("Shop")+","+T("About") );
}
//Set menus.
function SetMenus( list, images )
{
//Do nothing.
//app.SetMenu( list );
}
//Handle custom menu.
function lstMenu_OnTouch( item )
{
btnMenu_OnTouch();
HandleMenu( item );
}
//Called when user selects a menu item.
function OnMenu( name )
{
if( name==null ) btnMenu_OnTouch();
//HandleMenu( name );
}
//Start wifi edit server.
function Connect( prompt )
{
if( useADB ) {
if( prompt ) app.Alert( T("UseADB"), "ADB Connect" );
}
else {
if( !app.IsConnected() ) {
if( prompt ) { app.ShowPopup( T("ActivateWifi") ); return; }
}
if( prompt ) app.Alert( T("UseWifi") + app.GetIPAddress()+":8088\n\n" +
(usePass?T("Password")+": "+password :""), T("WifiConnect"), "" );
}
app.StartDebugServer();
if( !headless ) btnConnect.SetTextColor("#97C024");
//Allow discovery via UDP.
if( app.IsAPK() )
{
if( !net ) net = app.CreateNetClient( "UDP" );
address = net.GetBroadcastAddress();
SendDiscoveredMessage();
clearInterval( udpTimer );
udpTimer = setInterval( CheckForMsg, 1000 );
}
//Start browse server if premium.
if( premium ) StartBrowseServer();
}
//Broadcast our Datagram (UDP) packet.
function SendDiscoveredMessage()
{
var jsonData = {};
jsonData["appname"] = "DroidScript";
jsonData["devicename"] = deviceName;
jsonData["macaddress"] = app.GetMacAddress();
jsonData["version"] = app.GetVersion();
jsonData["usepass"] = usePass;
net.SendDatagram( JSON.stringify(jsonData), "UTF-8", address, port );
}
//Called by our interval timer.
function CheckForMsg()
{
//Try to read a packet for 1 millisec.
var packet = net.ReceiveDatagram( "UTF-8", port, 1 );
if( packet ) {
if( packet.localeCompare("DISCOVER_DSSERVER_REQUEST") == 0 )
SendDiscoveredMessage();
}
}
//Called when user selects a menu item.
function HandleMenu( name )
{
if( name==T("New") ) {
ShowTextDialog( T("NewApp"), "", "Native,HTML,Espruino", "OnAdd" );
}
else if( name=="Connect" ) {
Connect( true );
}
else if( name=="Exit" ) {
app.Exit( true );
}
else if( name==T("About") ) {
ShowAbout();
}
else if( name==T("Shop") ) {
ShowShop();
}
else if( name==T("Settings") ) {
ShowSettings();
}
else if( name==T("Plugins") ) {
ShowPlugins();
}
else if( name==T("Demos") ) {
ShowDemos();
}
else if( name==T("News") ) {
ShowNews();
}
else if( name==T("Premium") ) {
ShowPremium();
}
else if( name=="SDK" ) {
ShowSDKDialog();
}
else if( name=="Build APK" ) {
lstOps_Select( name );
}
}
//Show custom slide out menu.
function btnMenu_OnTouch()
{
if( layMenu.GetVisibility()=="Hide" ) {
if( !usingAce ) {
HideCodingMenus();
if( layFile.IsVisible()) layFile.Animate( "ScaleToTop" );
}
layMenu.Animate( "ScaleFromTop" );
}
else {
layMenu.Animate( "ScaleToTop" );
if( !usingAce )
{
//Show info bar and start code completion if editing.
if( layEdit.GetVisibility()=="Show" ) {
setTimeout( "layInfo.SetVisibility('Show')", 250 );
ccTimer = setInterval( CheckForCodeSuggestions, 500 );
}
}
}
}
//Show slide out files menu.
function btnFiles_OnTouch()
{
if( layFile.GetVisibility()=="Hide" ) {
if( !usingAce ) {
HideCodingMenus();
if( layMenu.IsVisible() ) layMenu.Animate( "ScaleToTop" );
}
layFile.Animate( "ScaleFromTop" );
}
else {
layFile.Animate( "ScaleToTop" );
if( !usingAce )
{
//Show info bar and start code completion if editing.
if( layEdit.GetVisibility()=="Show" ) {
setTimeout( "layInfo.SetVisibility('Show')", 250 );
ccTimer = setInterval( CheckForCodeSuggestions, 500 );
}
}
}
}
//Create the graphical layout.
function CreateLayout()
{
//Remove old layouts if they exist.
if( layBack ) app.RemoveLayout( layBack );
if( layFront ) app.RemoveLayout( layFront );
if( laySamp ) app.RemoveLayout( laySamp );
if( layDoc ) app.RemoveLayout( layDoc );
//Prepare popup editing and coding menus.
PrepareCodingMenus();
//--- Background screen -----
layBack = app.CreateLayout( "linear", "vcenter,fillxy" );
//Create text for remote connection message.
txtRemote = app.CreateText( "Hello",-1,-1,"fontawesome" );
txtRemote.SetTextSize( 22 );
txtRemote.SetOnTouchDown( txtRemote_OnTouchDown );
txtRemote.Gone();
layBack.AddChild( txtRemote );
//--- Main Screen ----------
//Create main layout for buttons etc.
layFront = app.CreateLayout( "linear", "vertical,fillxy,touchthrough" );
//Create title bar and buttons.
layBar = app.CreateLayout( "Linear", "horizontal,vcenter,fillx" );
layBar.SetBackground( "/res/drawable/bar_gray" );
layBar.SetSize( -1, topBarHeight );
btnDocs = app.CreateButton( "[fa-book]", (tablet?0.16:0.23), (tablet?0.038:0.04), "bar-gray,fontawesome" );
btnDocs.SetOnTouch( btnDocs_OnTouch );
btnDocs.SetTextSize("16", "dip");
btnDocs.SetTextColor("#dddddd");
layBar.AddChild( btnDocs );
btnConnect = app.CreateButton( "[fa-wifi]", (tablet?0.16:0.23), (tablet?0.038:0.04), "bar-gray,fontawesome" );
//btnConnect.SetSize( (tablet?0.08:0.13), (tablet?0.040:0.056) );
btnConnect.SetOnTouch( btnConnect_OnTouch );
btnConnect.SetMargins(0.02,0,0,0);
btnConnect.SetTextSize("16", "dip");
btnConnect.SetTextColor("#999999");
//btnConnect.SetPadding(0,0,0,4,"dip");
layBar.AddChild( btnConnect );
//imgTitle = app.CreateImage( "/assets/Img/AScript.png", (tablet?0.052:0.07), (lowRes?0.05:(tablet?0.033:0.039)), "" );
//layBar.AddChild( imgTitle );
btnFiles = app.CreateButton( "", (tablet?0.08:0.13), (tablet?0.040:0.055), "bar-gray,singleline" );
//btnFiles.SetSize( (tablet?0.16:0.26), (tablet?0.040:0.056) );
btnFiles.SetOnTouch( btnFiles_OnTouch );
btnFiles.SetMargins(0.04,0,0.04,0);
btnFiles.SetTextSize( lowRes?12:14, "dip");
btnFiles.SetTextColor("#dddddd");
btnFiles.SetEllipsize( "end" );
//btnFiles.SetPadding(0,0,0,0.014);
btnFiles.Hide();
layBar.AddChild( btnFiles );
btnMenu = app.CreateButton( "[fa-ellipsis-v]", (tablet?0.08:0.13), (tablet?0.040:0.055), "bar-gray,fontawesome" );
//btnMenu.SetSize( (tablet?0.08:0.13), (tablet?0.040:0.056) );
btnMenu.SetOnTouch( btnMenu_OnTouch );
btnMenu.SetMargins(0,0,0.02,0);
btnMenu.SetTextSize("14", "dip");
btnMenu.SetTextColor("#dddddd");
//btnMenu.SetPadding(0,0,0,0.014);
layBar.AddChild( btnMenu );
btnSamp = app.CreateButton( "[fa-rocket]", (tablet?0.16:0.23), (tablet?0.038:0.04), "bar-gray,fontawesome" );
btnSamp.SetOnTouch( btnSamp_OnTouch );
btnSamp.SetTextSize("16", "dip");
btnSamp.SetTextColor("#dddddd");
layBar.AddChild( btnSamp );
layFront.AddChild( layBar );
SetTitleMargins();
//Create layout to allow flipping of list/code.
layFlip = app.CreateLayout( "Frame", "" );
//Create TextEdit control to edit code (invisible at first).
layEdit = app.CreateLayout( "Linear", "Vertical,FillXY" );
layEdit.SetVisibility( "Hide" );
edit = app.CreateCodeEdit( "", 1.0, (lowRes?0.83:(tablet?0.88:0.86)), "" );
if( useDarkTheme ) edit.SetColorScheme( "Dark" );
edit.SetTextSize( textSize );
edit.SetPadding( 0.02,0,0,0 );
edit.SetOnChange( edit_OnChange );
edit.SetOnKey( edit_OnKey );
edit.SetOnDoubleTap( edit_OnDoubleTap );
edit.SetUseKeyboard( useSoftKeys );
edit.SetNavigationMethod( useYoyo ? "Yoyo" : "Touch" );
layEdit.AddChild( edit );
//Create editing buttons.
layEditBtns = app.CreateLayout( "Linear", "Horizontal,VCenter,FillX" );
//layEditBtns.SetPadding( 0,0.006,0,0.006 );
layEditBtns.SetBackColor( "#ff777777" );
layEditBtns.SetSize( 1.0, bottomBarHeight);
btnUndo = app.CreateButton( "[fa-undo]", 0.14, (tablet?0.04:0.06), "gray,fontawesome" );
btnUndo.SetOnTouch( btnUndo_OnTouch );
btnUndo.SetTextSize( 14, "pl");
layEditBtns.AddChild( btnUndo );
btnNew = app.CreateButton( "[fa-file-text]", 0.14, (tablet?0.04:0.06), "gray,fontawesome" );
btnNew.SetMargins( 0.03,0,0,0 );
btnNew.SetOnTouch( btnNew_OnTouch );
btnNew.SetTextSize( 14, "pl");
layEditBtns.AddChild( btnNew );
btnAsset = app.CreateButton( "[fa-picture-o]", 0.14, (tablet?0.04:0.06), "gray,fontawesome" );
btnAsset.SetMargins( 0.03,0,0,0 );
btnAsset.SetOnTouch( btnAsset_OnTouch );
btnAsset.SetTextSize( 14, "pl");
layEditBtns.AddChild( btnAsset );
btnDbg = app.CreateButton( "[fa-bug]", 0.14, (tablet?0.04:0.06), "gray,fontawesome" );
btnDbg.SetMargins( 0.03,0,0.03,0 );
btnDbg.SetTextSize( 14, "pl");
btnDbg.SetOnTouch( btnDbg_OnTouch );
layEditBtns.AddChild( btnDbg );
btnExec = app.CreateButton( "[fa-play]", 0.14, (tablet?0.04:0.06), "gray,fontawesome" );
btnExec.SetMargins( 0,0,0.03,0 );
btnExec.SetTextSize( 14, "pl");
btnExec.SetOnTouch( btnExec_OnTouch );
layEditBtns.AddChild( btnExec );
btnRedo = app.CreateButton( "[fa-repeat]", 0.14, (tablet?0.04:0.06), "gray,fontawesome" );
btnRedo.SetOnTouch( btnRedo_OnTouch );
btnRedo.SetTextSize( 14, "pl");
layEditBtns.AddChild( btnRedo );
layEdit.AddChild( layEditBtns );
layFlip.AddChild( layEdit );
//Create program list control.
layLst = app.CreateLayout( "Frame", "FillXY" );
if( useDarkTheme ) layLst.SetBackground( "/res/drawable/android_dark" );
else layLst.SetBackground( "/res/drawable/android" );
//Show user's program icons if starting in portrait
//mode, else it's done in OnConfig.
if( startOrient=="Portrait" ) ShowIcons();
//Create translucent gradient image above list.
//imgGrad = app.CreateImage( null, 1.0, 0.94, "" );
//imgGrad.SetBackGradientRadial( 0.5, 0.2, 2.0, "#00000000", "#88000000" );
//imgGrad.SetTouchable( false );
//layLst.AddChild( imgGrad );
layFlip.AddChild( layLst );
layFront.AddChild( layFlip );
//--- Custom slide down options layout -----
layMenus = app.CreateLayout( "Absolute", "fillxy,touchthrough" );
layMenu = app.CreateLayout( "Linear" );
//layMenu.SetMargins( 0.15, (tablet?0.065:0.076), 0,0 );
layMenu.SetPosition( 0.50, (tablet?0.065:0.076) );
layMenu.SetVisibility( "Hide" );
layMenus.AddChild( layMenu );
lstMenu = app.CreateList( "", 0.3,-1, "FillXY,normal" );
lstMenu.SetPadding( 0,0.005,0,0,0 );
lstMenu.SetBackColor( "#ee333333" );
lstMenu.SetTextSize( 18, "dip" );
lstMenu.SetOnTouch( lstMenu_OnTouch );
layMenu.AddChild( lstMenu );
LoadMenus();
//--- Slide down file list -----
layFiles = app.CreateLayout( "Absolute", "fillxy,touchthrough" );
layFile = app.CreateLayout( "Linear" );
//layFile.SetMargins( 0.15, (tablet?0.065:0.076), 0,0 );
layFile.SetPosition( isPortrait?0.3:0.385, topBarHeight );
layFile.SetVisibility( "Hide" );
layFiles.AddChild( layFile );
lstFiles = app.CreateList( "", 0.4,-1, "FillXY,normal" );
lstFiles.SetPadding( 0,0.005,0,0,0 );
lstFiles.SetBackColor( "#ee333333" );
lstFiles.SetTextSize( lowRes?16:18, "dip" );
lstFiles.SetOnTouch( lstFiles_OnTouch );
lstFiles.SetOnLongTouch( lstFiles_OnLongTouch );
layFile.AddChild( lstFiles );
//--- Samples Screen -------
//Create an (initially invisible) layout to show samples.
laySamp = app.CreateLayout( "Absolute", "fillxy,touchthrough" );
laySamp.SetPadding( 0, (lowRes?0.104:(tablet?0.066:0.080)), 0, 0 );
laySamp.SetVisibility( "Hide" );
layExamp = app.CreateLayout( "Frame", "vertical,fillxy" );
layExamp.SetBackColor( "#ff222222" );
//Create list of samples.
var listArray = GetSamples();
lstSamp = app.CreateList( listArray, -1, -1, "FillXY,WhiteGrad,Html" );
lstSamp.SetTextColor1( "#ff555558" );
lstSamp.SetTextColor2( "#ff555558" );
lstSamp.SetTextMargins( 0.04, 0, 0, 0 );
lstSamp.SetOnTouch( lstSamp_OnTouch );
lstSamp.SetOnLongTouch( lstSamp_OnLongTouch );
layExamp.AddChild( lstSamp );
//Create text control showing code.
layEditSamp = app.CreateLayout( "Linear", "Vertical,FillXY" );
layEditSamp.SetVisibility( "Hide" );
editSamp = app.CreateCodeEdit( "", 1, (lowRes?0.83:(tablet?0.88:0.86)), "NoSpell,NoKeyboard" );
editSamp.SetTextSize( textSize );
editSamp.SetPadding( 0.02,0,0,0 ); //<-needed for keyboard space.
editSamp.SetOnDoubleTap( edit_OnDoubleTap );
editSamp.SetNavigationMethod( useYoyo ? "Yoyo" : "Touch" );
layEditSamp.AddChild( editSamp );
//Create horizontal layout for buttons.
layBtns = app.CreateLayout( "Linear", "Horizontal,VCenter,FillX" );
layBtns.SetBackColor( "#ff777777" );
layBtns.SetPadding( 0,0.006,0,0.006 );
//Create button to zoom out.
// btnZoomOut = app.CreateButton( "-", 0.16, 0.05, "gray" );
// btnZoomOut.SetOnTouch( btnZoomOut_OnTouch );
//layBtns.AddChild( btnZoomOut );
//Create button to copy sample.
btnCopy = app.CreateButton( "[fa-copy]", 0.26, (tablet?0.04:0.06), "gray,fontawesome" );
btnCopy.SetMargins( 0.08,0,0.08,0 );
btnCopy.SetTextSize( 14, "pl");
btnCopy.SetOnTouch( btnCopy_OnTouch );
layBtns.AddChild( btnCopy );
//Create button to launch sample.
btnRun = app.CreateButton( "[fa-play]", 0.26, (tablet?0.04:0.06), "gray,fontawesome" );
btnRun.SetMargins( 0.08,0,0.08,0 );
btnRun.SetTextSize( 14, "pl");
btnRun.SetOnTouch( btnRun_OnTouch );
layBtns.AddChild( btnRun );
//Create button to zoom in.
// btnZoomIn = app.CreateButton( "+", 0.16, 0.05, "gray" );
// btnZoomIn.SetOnTouch( btnZoomIn_OnTouch );
// layBtns.AddChild( btnZoomIn );
layEditSamp.AddChild( layBtns );
layExamp.AddChild( layEditSamp );
laySamp.AddChild( layExamp );
//--- Documentation Screen -------
//Create an (initially invisible) layout to show docs.
layDoc = app.CreateLayout( "Absolute", "fillxy,touchthrough" );
layDoc.SetPadding( 0, topBarHeight, 0, 0 );
layWeb = app.CreateLayout( "Linear", "vertical,fillxy" );
layWeb.SetBackGradient( "#ffdddddd", "#ffaaaaaa" );
layDoc.SetVisibility( "Hide" );
webDocs = app.CreateWebView( 1.0, 1-topBarHeight-bottomBarHeight, "NoLongTouch,IgnoreErrors,ScrollFade" );
//webDocs.LoadUrl( "file:///android_asset/edit/docs/Docs.htm" );
//var docsPath = "file:///sdcard/DroidScript/.edit/docs-"+language.toLowerCase();
//webDocs.LoadUrl( docsPath + "/Docs.htm" );
layWeb.AddChild( webDocs );
layDoc.AddChild( layWeb );
//Set language.
SetLanguage();
//---------------------------------
//Adjust layout according to orientation.
AdjustLayout();
//Hide main layout if required.
if( sharedApp ) layFront.SetVisibility("Hide");
//Add main layouts to app.
app.AddLayout( layBack );
app.AddLayout( layFront );
app.AddLayout( laySamp );
app.AddLayout( layDoc );
//Create popup coding menus.
CreateCodingMenus();
}
//Create basic layout when running in chrome.
function CreateHeadlessLayout( debug )
{
var msg;
if( useADB ) msg = "Run 'DroidScript Connect' and type the following" +
" address into your web browser:\n127.0.0.1:8088";
else msg = "Type the following address into your web browser:\n" + app.GetIPAddress()+":8088";
var lay = app.CreateLayout( "linear", "VCenter,FillXY" );
lay.SetPadding( 0.04,0,0.04,0 );
if( debug ) txtDebug = app.CreateText( msg,-1,-1,"FillXY,Log" );
else txtDebug = app.CreateText( msg,-1,-1,"MultiLine" );
txtDebug.SetTextSize( debug?12:22 );
lay.AddChild( txtDebug );
app.AddLayout( lay );
//Capture remote logging.
if( debug ) app.SetOnDebug( OnDebug );
}
//Create a theme for all controls and dialogs.
function CreateTheme()
{
theme = app.CreateTheme( "Light" );