Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var noteContent = "";
- var datFile = "";
- var options = {};
- options.Confirm = true;
- options.CurlUrl = "https://secure.globiflow.com/catch/";
- //Called when application is started.
- function OnStart()
- {
- noteContent = "Lorem Ipsum";
- var datDir = "/sdcard/." + app.GetAppName();
- if ( ! app.FolderExists( datDir ) ) app.MakeFolder( datDir );
- datFile = datDir+"/gwsvnote.jsn";
- LoadOptions();
- //Create recognition object and set callbacks.
- speech = app.CreateSpeechRec();
- speech.SetOnReady( speech_OnReady );
- speech.SetOnResult( speech_OnResult );
- speech.SetOnError( speech_OnError );
- Home();
- if ( options.CurlUrl.replace("https://secure.globiflow.com/catch/", "") == "" ) {
- Options();
- } else {
- //Start recognizing.
- speech.Recognize();
- }
- }
- function Home()
- {
- homeLay = app.CreateLayout("Linear", "VCenter,FillXY");
- txt = app.CreateText( "VNOTE" );
- txt.SetTextSize(22);
- homeLay.AddChild(txt);
- txt = app.CreateText( "Quickly take voice notes" );
- homeLay.AddChild(txt);
- txt = app.CreateText( "and send them to a webhook" );
- homeLay.AddChild(txt);
- layRow = app.CreateLayout("Linear", "Horizontal");
- homeLay.AddChild(layRow);
- txt = app.CreateText( "Android should start listening automatically" );
- homeLay.AddChild(txt);
- btnConfig = app.CreateButton("Config", -1, -1, "Custom");
- btnConfig.SetStyle("#00ffffff", "#00ffffff", 0, "#0000ff", 0);
- btnConfig.SetOnTouch(Options);
- layRow.AddChild(btnConfig);
- btnQuit = app.CreateButton( "Quit", -1, -1, "" );
- btnQuit.SetStyle("#666666", "#666666");
- btnQuit.SetOnTouch(function(){
- app.Exit();
- });
- layRow.AddChild(btnQuit);
- btnStart = app.CreateButton("Start", -1, -1, "Custom");
- btnStart.SetStyle("#4285F4", "#4285F4");
- btnStart.SetOnTouch(function(){
- //Start recognizing.
- speech.Recognize();
- });
- layRow.AddChild(btnStart);
- app.AddLayout(homeLay);
- }
- function Confirm()
- {
- dlgConf = app.CreateDialog("Send Note");
- mainLay = app.CreateLayout("Linear", "VCenter,FillXY");
- mainLay.SetMargins(0.05, 0.05, 0.05, 0.05);
- dlgConf.AddLayout(mainLay);
- edtNote = app.CreateTextEdit( noteContent,0.5 );
- mainLay.AddChild(edtNote);
- layRow = app.CreateLayout("Linear", "Horizontal");
- mainLay.AddChild(layRow);
- btnConfig = app.CreateButton("Config", -1, -1, "Custom");
- btnConfig.SetStyle("#00ffffff", "#00ffffff", 0, "#0000ff", 0);
- btnConfig.SetOnTouch(Options);
- layRow.AddChild(btnConfig);
- btnClose = app.CreateButton( "Cancel", -1, -1, "" );
- btnClose.SetStyle("#666666", "#666666");
- btnClose.SetOnTouch(function(){
- app.Exit();
- });
- layRow.AddChild(btnClose);
- btnSend = app.CreateButton("OK", -1, -1, "Custom");
- btnSend.SetStyle("#4285F4", "#4285F4");
- btnSend.SetOnTouch(function(){
- noteContent = edtNote.GetText();
- SendNote();
- });
- layRow.AddChild(btnSend);
- //app.AddLayout(mainLay);
- dlgConf.Show();
- }
- function SendNote() {
- SendRequest(options.CurlUrl, {v:noteContent});
- }
- function Options() {
- dlg = app.CreateDialog("App Options");
- lay = app.CreateLayout("Linear", "VCenter,FillX,Left");
- lay.SetMargins(0.05, 0.05, 0.05, 0.05);
- dlg.AddLayout(lay);
- txt = app.CreateText("POST URL");
- lay.AddChild(txt, "Left");
- edtUrl = app.CreateTextEdit(options.CurlUrl);
- edtUrl.SetMargins(0, 0, 0, 0.02);
- lay.AddChild(edtUrl);
- txt = app.CreateText("Before Sending Text to URL");
- lay.AddChild(txt, "Left");
- chkConf = app.CreateCheckBox("Allow Confirmation?");
- chkConf.SetChecked(options.Confirm);
- chkConf.SetMargins(0, 0, 0, 0.05);
- lay.AddChild(chkConf, "Left");
- layRow = app.CreateLayout( "Linear", "Horizontal,FillX,Right" );
- lay.AddChild(layRow, "Right");
- btnCancel = app.CreateButton("Cancel", -1, -1, "Custom");
- btnCancel.SetStyle("#666666", "#666666");
- btnCancel.SetOnTouch(function(){
- dlg.Hide();
- });
- layRow.AddChild(btnCancel);
- btnOK = app.CreateButton("OK", -1, -1, "Custom");
- btnOK.SetStyle("#4285F4", "#4285F4");
- btnOK.SetOnTouch(function(){
- options.CurlUrl = edtUrl.GetText();
- options.Confirm = chkConf.GetChecked();
- SaveOptions();
- dlg.Hide();
- //Start recognizing.
- speech.Recognize();
- });
- layRow.AddChild(btnOK);
- //app.AddLayout( lay );
- dlg.Show();
- }
- function LoadOptions() {
- var opts = {};
- if ( app.FileExists(datFile) ) {
- var data = app.ReadFile(datFile);
- try {
- opts = JSON.parse(data);
- } catch(e) {
- opts = {};
- }
- }
- if ( "CurlUrl" in opts ) options.CurlUrl = opts.CurlUrl;
- if ( "Confirm" in opts ) options.Confirm = opts.Confirm;
- }
- function SaveOptions() {
- if ( ! app.FileExists(datFile) ) {
- app.CreateFile(datFile);
- }
- var data = JSON.stringify(options);
- app.WriteFile(datFile, data);
- }
- //Send an http post request.
- function SendRequest( url, params )
- {
- var httpRequest = new XMLHttpRequest();
- httpRequest.onreadystatechange = function() { HandleReply(httpRequest); };
- httpRequest.open("POST", url, true);
- httpRequest.send(JSON.stringify(params));
- }
- //Handle the server's reply (a json object).
- function HandleReply( httpRequest ){
- if( httpRequest.readyState==4 ) {
- //If we got a valid response.
- if( httpRequest.status==200 ){
- app.Exit();
- }
- //An error occurred
- else{
- alert( "Error: " + httpRequest.status + httpRequest.responseText);
- }
- }
- app.HideProgress();
- }
- //Called when speech engine is ready.
- function speech_OnReady()
- {
- app.ShowPopup( "Listening...", "Short" );
- }
- //Called with the recognition result(s).
- function speech_OnResult( results )
- {
- //An array of recognition results is returned
- //here, with the most probable at the front
- //of the array.
- //Show the top result.
- noteContent = results[0];
- if ( options.Confirm ) {
- Confirm();
- } else {
- SendNote();
- }
- }
- //Called if recognition fails.
- function speech_OnError()
- {
- app.ShowPopup( "Please speak more clearly!" );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement