Advertisement
globiws

DroidScript vNote

Jun 3rd, 2019
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var noteContent = "";
  2. var datFile = "";
  3. var options = {};
  4. options.Confirm = true;
  5. options.CurlUrl = "https://secure.globiflow.com/catch/";
  6.  
  7. //Called when application is started.
  8. function OnStart()
  9. {
  10.     noteContent = "Lorem Ipsum";
  11.     var datDir = "/sdcard/." + app.GetAppName();
  12.     if ( ! app.FolderExists( datDir ) ) app.MakeFolder( datDir );
  13.     datFile = datDir+"/gwsvnote.jsn";
  14.     LoadOptions();
  15.    
  16.     //Create recognition object and set callbacks.
  17.     speech = app.CreateSpeechRec();
  18.     speech.SetOnReady( speech_OnReady );
  19.     speech.SetOnResult( speech_OnResult );
  20.     speech.SetOnError( speech_OnError );
  21.  
  22.     Home();
  23.    
  24.     if ( options.CurlUrl.replace("https://secure.globiflow.com/catch/", "") == "" ) {
  25.         Options();
  26.     } else {
  27.         //Start recognizing.
  28.         speech.Recognize();
  29.     }
  30.  
  31. }
  32.  
  33. function Home()
  34. {
  35.     homeLay = app.CreateLayout("Linear", "VCenter,FillXY");
  36.    
  37.     txt = app.CreateText( "VNOTE" );
  38.     txt.SetTextSize(22);
  39.     homeLay.AddChild(txt);
  40.     txt = app.CreateText( "Quickly take voice notes" );
  41.     homeLay.AddChild(txt);
  42.     txt = app.CreateText( "and send them to a webhook" );
  43.     homeLay.AddChild(txt);
  44.    
  45.     layRow = app.CreateLayout("Linear", "Horizontal");
  46.     homeLay.AddChild(layRow);
  47.  
  48.     txt = app.CreateText( "Android should start listening automatically" );
  49.     homeLay.AddChild(txt);
  50.  
  51.     btnConfig = app.CreateButton("Config", -1, -1, "Custom");
  52.     btnConfig.SetStyle("#00ffffff", "#00ffffff", 0, "#0000ff", 0);
  53.     btnConfig.SetOnTouch(Options);
  54.     layRow.AddChild(btnConfig);
  55.    
  56.     btnQuit = app.CreateButton( "Quit", -1, -1, "" );
  57.     btnQuit.SetStyle("#666666", "#666666");
  58.     btnQuit.SetOnTouch(function(){
  59.         app.Exit();
  60.     });
  61.     layRow.AddChild(btnQuit);
  62.  
  63.     btnStart = app.CreateButton("Start", -1, -1, "Custom");
  64.     btnStart.SetStyle("#4285F4", "#4285F4");
  65.     btnStart.SetOnTouch(function(){
  66.         //Start recognizing.
  67.         speech.Recognize();
  68.     });
  69.     layRow.AddChild(btnStart);
  70.    
  71.     app.AddLayout(homeLay);
  72. }
  73.  
  74.  
  75. function Confirm()
  76. {
  77.     dlgConf = app.CreateDialog("Send Note");
  78.     mainLay = app.CreateLayout("Linear", "VCenter,FillXY");
  79.     mainLay.SetMargins(0.05, 0.05, 0.05, 0.05);
  80.     dlgConf.AddLayout(mainLay);
  81.  
  82.    
  83.     edtNote = app.CreateTextEdit( noteContent,0.5 );  
  84.     mainLay.AddChild(edtNote);  
  85.    
  86.     layRow = app.CreateLayout("Linear", "Horizontal");
  87.     mainLay.AddChild(layRow);
  88.    
  89.     btnConfig = app.CreateButton("Config", -1, -1, "Custom");
  90.     btnConfig.SetStyle("#00ffffff", "#00ffffff", 0, "#0000ff", 0);
  91.     btnConfig.SetOnTouch(Options);
  92.     layRow.AddChild(btnConfig);
  93.    
  94.     btnClose = app.CreateButton( "Cancel", -1, -1, "" );
  95.     btnClose.SetStyle("#666666", "#666666");
  96.     btnClose.SetOnTouch(function(){
  97.         app.Exit();
  98.     });
  99.     layRow.AddChild(btnClose);
  100.    
  101.     btnSend = app.CreateButton("OK", -1, -1, "Custom");
  102.     btnSend.SetStyle("#4285F4", "#4285F4");
  103.     btnSend.SetOnTouch(function(){
  104.         noteContent = edtNote.GetText();
  105.         SendNote();
  106.     });
  107.     layRow.AddChild(btnSend);
  108.  
  109.     //app.AddLayout(mainLay);
  110.     dlgConf.Show();
  111. }
  112.  
  113. function SendNote() {
  114.     SendRequest(options.CurlUrl, {v:noteContent});
  115. }
  116.  
  117. function Options() {
  118.     dlg = app.CreateDialog("App Options");
  119.    
  120.     lay = app.CreateLayout("Linear", "VCenter,FillX,Left");
  121.     lay.SetMargins(0.05, 0.05, 0.05, 0.05);
  122.     dlg.AddLayout(lay);
  123.    
  124.     txt = app.CreateText("POST URL");
  125.     lay.AddChild(txt, "Left");
  126.    
  127.     edtUrl = app.CreateTextEdit(options.CurlUrl);
  128.     edtUrl.SetMargins(0, 0, 0, 0.02);
  129.     lay.AddChild(edtUrl);
  130.  
  131.     txt = app.CreateText("Before Sending Text to URL");
  132.     lay.AddChild(txt, "Left");
  133.    
  134.     chkConf = app.CreateCheckBox("Allow Confirmation?");
  135.     chkConf.SetChecked(options.Confirm);
  136.     chkConf.SetMargins(0, 0, 0, 0.05);
  137.     lay.AddChild(chkConf, "Left");
  138.    
  139.     layRow = app.CreateLayout( "Linear", "Horizontal,FillX,Right" );
  140.     lay.AddChild(layRow, "Right");
  141.  
  142.     btnCancel = app.CreateButton("Cancel", -1, -1, "Custom");
  143.     btnCancel.SetStyle("#666666", "#666666");
  144.     btnCancel.SetOnTouch(function(){
  145.         dlg.Hide();
  146.     });
  147.     layRow.AddChild(btnCancel);
  148.    
  149.     btnOK = app.CreateButton("OK", -1, -1, "Custom");
  150.     btnOK.SetStyle("#4285F4", "#4285F4");
  151.     btnOK.SetOnTouch(function(){
  152.         options.CurlUrl = edtUrl.GetText();
  153.         options.Confirm = chkConf.GetChecked();
  154.         SaveOptions();
  155.         dlg.Hide();
  156.         //Start recognizing.
  157.         speech.Recognize();
  158.     });
  159.     layRow.AddChild(btnOK);
  160.    
  161.     //app.AddLayout( lay );
  162.     dlg.Show();
  163. }
  164.  
  165. function LoadOptions() {
  166.     var opts = {};
  167.     if ( app.FileExists(datFile) ) {
  168.         var data = app.ReadFile(datFile);
  169.         try {
  170.             opts = JSON.parse(data);
  171.         } catch(e) {
  172.             opts = {};
  173.         }
  174.     }
  175.     if ( "CurlUrl" in opts ) options.CurlUrl = opts.CurlUrl;
  176.     if ( "Confirm" in opts ) options.Confirm = opts.Confirm;
  177. }
  178.  
  179. function SaveOptions() {
  180.     if ( ! app.FileExists(datFile) ) {
  181.         app.CreateFile(datFile);
  182.     }
  183.     var data = JSON.stringify(options);
  184.     app.WriteFile(datFile, data);
  185. }
  186.  
  187. //Send an http post request.
  188. function SendRequest( url, params )
  189. {
  190.     var httpRequest = new XMLHttpRequest();
  191.     httpRequest.onreadystatechange = function() { HandleReply(httpRequest); };
  192.     httpRequest.open("POST", url, true);
  193.     httpRequest.send(JSON.stringify(params));
  194. }
  195.  
  196. //Handle the server's reply (a json object).
  197. function HandleReply( httpRequest ){
  198.     if( httpRequest.readyState==4 ) {
  199.         //If we got a valid response.
  200.         if( httpRequest.status==200 ){
  201.             app.Exit();
  202.         }
  203.         //An error occurred
  204.         else{
  205.           alert( "Error: " + httpRequest.status + httpRequest.responseText);
  206.         }
  207.     }
  208.     app.HideProgress();
  209. }
  210.  
  211. //Called when speech engine is ready.
  212. function speech_OnReady()
  213. {
  214.     app.ShowPopup( "Listening...", "Short" );
  215. }
  216.  
  217. //Called with the recognition result(s).
  218. function speech_OnResult( results )
  219. {
  220.     //An array of recognition results is returned
  221.     //here, with the most probable at the front
  222.     //of the array.
  223.    
  224.     //Show the top result.
  225.     noteContent = results[0];
  226.     if ( options.Confirm ) {
  227.         Confirm();
  228.     } else {
  229.         SendNote();
  230.     }
  231.    
  232. }
  233.  
  234. //Called if recognition fails.
  235. function speech_OnError()
  236. {
  237.     app.ShowPopup( "Please speak more clearly!" );
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement