====== Differences ====== This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
script_customviewlog [2016/07/07 12:50] cdfa |
script_customviewlog [2016/11/18 15:24] (current) cdfa |
||
---|---|---|---|
Line 3: | Line 3: | ||
* Author : cdfa | * Author : cdfa | ||
* Link : https://plus.google.com/100430440392224314007/posts/ht4aZS8LSWg | * Link : https://plus.google.com/100430440392224314007/posts/ht4aZS8LSWg | ||
- | * Version: 1.0.3 | + | * Version: 1.0.6.3 |
====== How to use the script ====== | ====== How to use the script ====== | ||
Line 11: | Line 11: | ||
====== (copy/paste) ====== | ====== (copy/paste) ====== | ||
<sxh javascript> | <sxh javascript> | ||
- | var logScript = LL.getScriptByName('logScript'); | + | function log(){}try{return(function(){var logScript=getScriptByName('logScript');if(logScript){eval('function run(){'+logScript.getText()+'}');return run();}})();}catch(e){alert("At line "+e.lineNumber+": "+e);}/*logScriptEnd*/ |
- | if(logScript!=null){ return eval('(function() {' + logScript.getText() + '}())'); }else{throw("logScript not found!");} //logScriptEnd | + | |
</sxh> | </sxh> | ||
- | (before this gets executed you can't use the log function, errors won't get caught) | + | (before this gets executed you can't use the log function and errors won't get caught) |
- | log function: | + | usage: |
- | log(text, logLevel) | + | log(text, logLevel, replacements..) |
- | the logLevel can be anything you want, but only "ERROR" and "debug" have custom colors. (you can add your own if you modify the script just a bit) | + | Loglevels are accessed through logScript.logLevel. |
+ | If you need some number or something to be inside the the text you can enter "{}" where the text should be and it will get replaced by the extra argument you passed. So for example: | ||
+ | |||
+ | log("This {} has id: {}", logScript.logLevel.NORMAL, item.getType(), item.getId()) | ||
+ | |||
+ | will write: This Shortcut had id: 18894. | ||
please report any bugs/feature suggestion in the google plus post | please report any bugs/feature suggestion in the google plus post | ||
Line 33: | Line 37: | ||
1.0.3 | 1.0.3 | ||
* fixed slow writing speed which caused duplicates and wrong order of logs | * fixed slow writing speed which caused duplicates and wrong order of logs | ||
- | |||
- | ===== Things to keep in mind ===== | ||
- | * your code is executed in eval(), so some weird stuff might happen | ||
- | |||
- | * The log won't catch errors thrown by the compiler | ||
1.0.4 | 1.0.4 | ||
Line 43: | Line 42: | ||
* You can now reduce your whole script to one line without the line that starts this script breaking your script | * You can now reduce your whole script to one line without the line that starts this script breaking your script | ||
- | ====== logScript ====== | + | 1.0.5 |
- | <sxh javascript> | + | * The logs are now saved on pause of the custom view instead of after the script has finished running. (But for some reason the paused event doesn't get fired when you restart the app, so keep that in mind) |
- | /*----------- | + | * you can now pass replacements to the log function (see above) |
- | put this in script to use logScript | + | * Some Errors in the logscript won't cause your script to break anymore. |
+ | * Be sure to update the line that enables logscript. | ||
- | var logScript = LL.getScriptByName('logScript'); | + | 1.0.5.1 |
- | if(logScript!=null){ return eval('(function() {' + logScript.getText() + '}())'); }else{throw("logScript not found!");} //logScriptEnd | + | * Small fix for the log getting too big. |
+ | * You can now run the script from an item to create a custom view with the same dimensions as the item. | ||
- | -----------*/ | + | 1.0.6 |
+ | * Your code isn't executed in eval anymore! | ||
- | var logScript = { | + | 1.0.6.1 |
- | logFilePath: "/storage/sdcard0/LightningLauncher/script/logScriptLog.html" | + | * Fixed a bug with the script name |
- | , lineWrapping: true // might require app restart | + | |
- | , bgColor: 0xff191919 | + | |
- | , textColors: ["#d0d0d0", "#ee7600"] // the script iterates through these colors with each new "session" | + | |
- | }; | + | |
- | LL.bindClass("java.io.File"); | + | 1.0.6.2 |
- | LL.bindClass("java.io.FileWriter"); | + | * Fixed a few bugs related to unreachable logfile |
- | LL.bindClass("java.io.BufferedWriter"); | + | |
- | LL.bindClass("android.widget.TextView"); | + | |
- | LL.bindClass("android.text.method.ScrollingMovementMethod"); | + | |
- | LL.bindClass("android.text.Html"); | + | |
- | LL.bindClass("java.io.BufferedReader"); | + | |
- | LL.bindClass("java.io.FileReader"); | + | |
- | LL.bindClass("android.widget.ScrollView"); | + | |
- | LL.bindClass("android.view.View"); | + | |
- | LL.bindClass("java.lang.Runnable"); | + | |
- | function log(text, channel) { | + | 1.0.6.3 |
- | var date = new Date(); | + | * Fixed date |
- | var month = date.getMonth() + 1; | + | |
- | var scriptName = LL.getCurrentScript().getName(); | + | |
- | if (channel == null) channel = "normalLog"; | + | |
- | switch (channel) { | + | |
- | case "ERROR": | + | |
- | var channelColor = "#ff0000"; | + | |
- | break; | + | |
- | case "debug": | + | |
- | var channelColor = "#0000ff" | + | |
- | break; | + | |
- | default: | + | |
- | var channelColor = "#ffffff"; | + | |
- | break; | + | |
- | } | + | |
- | var logText = "<br><font color=#add8e6>" + date.getDate() + "-" + month + | + | |
- | "</font>" + " <font color=#ffff00>" + date.toTimeString().slice(0, 8) + | + | |
- | "</font>" + " <font color=#00ff00>" + scriptName + "</font>" + | + | |
- | " <font color=" + channelColor + ">" + channel + "</font>" + ": " + | + | |
- | " <font color=" + logScript.textColors[logScript.ind] + ">" + text + | + | |
- | "</font>"; | + | |
- | logScript.v.append(Html.fromHtml(logText)); | + | |
- | scrollDown(logScript.sv); | + | |
- | try { | + | |
- | if (bfWriter == null) | + | |
- | var bfWriter = new BufferedWriter(new FileWriter(new File(logScript.logFilePath), true)); | + | |
- | bfWriter.append(logText); | + | |
- | } catch (e) { | + | |
- | alert(e); | + | |
- | } | + | |
- | bfWriter.flush() | + | |
- | } | + | |
- | function read(filePath) { | + | ====== Things to keep in mind ====== |
- | try { | + | * The log won't catch syntax errors |
- | var r = new BufferedReader(new FileReader(filePath)); | + | * For some reason the paused event doesn't get fired when you restart the app, so new logs since the last pause won't be saved |
- | var s = ""; | + | |
- | var l; | + | |
- | while ((l = r.readLine()) != null) s += (l + "\n"); | + | |
- | return s; | + | |
- | } catch (e) { | + | |
- | return ""; | + | |
- | } | + | |
- | } | + | |
- | function scrollDown(sv){ | + | ====== logScript ====== |
- | sv.post(new Runnable() { | + | <sxh javascript> |
- | run: function() { | + | /*----------- |
- | sv.fullScroll(ScrollView.FOCUS_DOWN); | + | |
- | } | + | |
- | }); | + | |
- | } | + | |
- | + | ||
- | if (LL.getCurrentScript().getName() == "logScript") { | + | |
- | var script = LL.getCurrentScript(); | + | |
- | + | ||
- | try { | + | |
- | typeof item; | + | |
- | } catch (e) { | + | |
- | // in create | + | |
- | logScript.cv = item; | + | |
- | var context = LL.getContext(); | + | |
- | logScript.v = new TextView(context); | + | |
- | var sv = new ScrollView(context); | + | |
- | sv.addView(logScript.v); | + | |
- | + | ||
- | logScript.v.setText(Html.fromHtml(read(logScript.logFilePath))); | + | |
- | logScript.v.setMovementMethod(new ScrollingMovementMethod()); | + | |
- | logScript.cv.setVerticalGrab(true); | + | |
- | logScript.v.setVerticalScrollBarEnabled(true); | + | |
- | if (!logScript.lineWrapping) { | + | |
- | logScript.v.setHorizontallyScrolling(true); | + | |
- | logScript.cv.setHorizontalGrab(true); | + | |
- | } | + | |
- | scrollDown(sv); | + | |
- | return sv; | + | |
- | } | + | |
- | + | ||
- | var e = LL.getEvent(); | + | |
- | var src = e.getSource(); | + | |
- | if (src == "MENU_APP") { | + | |
- | //in container | + | |
- | var scriptId = script.getId(); | + | |
- | var e = LL.getEvent(); | + | |
- | var c = e.getContainer(); | + | |
- | var cv = c.addCustomView(e.getTouchX(), e.getTouchY()); | + | |
- | var prop = cv.getProperties().edit(); | + | |
- | prop.getBox("i.box").setColor("c", "n", logScript.bgColor); | + | |
- | prop.setString("v.onCreate", scriptId); | + | |
- | prop.setEventHandler("i.resumed", EventHandler.RUN_SCRIPT, scriptId); | + | |
- | prop.commit(); | + | |
- | script.setTag("cvId", cv.getId()); | + | |
- | } else { | + | |
- | var cv = LL.getItemById(script.getTag("cvId")); | + | |
- | if (cv == null) { | + | |
- | script.setTag("cvId", null); | + | |
- | } else { | + | |
- | cv.getView().getChildAt(0).setText(Html.fromHtml(read(logScript.logFilePath))); | + | |
- | } | + | |
- | } | + | |
- | } else { | + | |
- | //in eval() | + | |
- | logScript.cv = LL.getItemById(LL.getScriptByName("logScript").getTag("cvId")); | + | |
- | if (logScript.cv == null) throw ("No custom view found!") | + | |
- | logScript.ind = parseInt(logScript.cv.getTag("prevColorIndex") || "0"); | + | |
- | logScript.sv = logScript.cv.getView(); | + | |
- | if(logScript.sv == null){ | + | |
- | Toast.makeText(LL.getContext(), "custom view not loaded", Toast.LENGTH_SHORT); | + | |
- | function log(text, channel) {} | + | |
- | }else{ | + | |
- | logScript.v = logScript.sv.getChildAt(0); | + | |
- | } | + | |
- | logScript.text = LL.getCurrentScript().getText(); | + | |
- | logScript.returns = ""; | + | |
- | for(var i=0;i<logScript.text.substr(0, logScript.text.search("var logScript")).split("\n").length; i++){ | + | |
- | logScript.returns += "\n"; | + | |
- | } | + | |
- | logScript.text = logScript.returns + logScript.text.substr(logScript.text.search("logScriptEnd") + 12); | + | |
- | try { | + | |
- | return eval('(function() {' + logScript.text + '}())'); | + | |
- | } catch (e) { | + | |
- | log("At line " + e.lineNumber + ": " + e, "ERROR"); | + | |
- | } finally { | + | |
- | logScript.cv.setTag("prevColorIndex", logScript.ind == logScript.textColors.length - | + | |
- | 1 ? 0 : logScript.ind + 1); | + | |
- | if(typeof bfWriter != "undefined") bfWriter.flush(); | + | |
- | } | + | |
- | }/*----------- | + | |
put this in script to use logScript | put this in script to use logScript | ||
- | var logScript = LL.getScriptByName('logScript'); if(logScript!=null){ return eval('(function() {' + logScript.getText() + '}())'); }else{throw("logScript not found!");} /*logScriptEnd*/ | + | function log(){}try{return(function(){var logScript=getScriptByName('logScript');if(logScript){eval('function run(){'+logScript.getText()+'}');return run();}})();}catch(e){alert("At line "+e.lineNumber+": "+e);}/*logScriptEnd*/ |
//CONFIG | //CONFIG | ||
var logScript = { | var logScript = { | ||
- | logFilePath: "/storage/sdcard0/LightningLauncher/script/logScriptLog.html" | + | logFilePath: "/storage/sdcard0/LightningLauncher/script/logScriptLog.html" |
- | , lineWrapping: true // might require app restart | + | , lineWrapping: true // might require app restart |
- | , bgColor: 0xff191919 | + | , bgColor: 0xff191919 |
- | , textColors: ["#d0d0d0", "#ee7600"] // the script iterates through these colors with each new "session" | + | , textColors: ["#d0d0d0", "#ee7600"] // the script iterates through these colors with each new "session" |
- | // END CONFIG | + | , logLevel: { |
- | , scrollDown : function(sv){ | + | NORMAL: { |
+ | name: "[NORMAL]" | ||
+ | , color: "#ffffff" | ||
+ | } | ||
+ | , DEBUG: { | ||
+ | name: "[DEBUG]" | ||
+ | , color: "#0000ff" | ||
+ | } | ||
+ | , ERROR: { | ||
+ | name: "[ERROR]" | ||
+ | , color: "#ff0000" | ||
+ | } | ||
+ | } | ||
+ | , defaultLogLevel: "NORMAL" | ||
+ | , saveMode: true | ||
+ | // END CONFIG | ||
+ | , currentScript: getCurrentScript() | ||
+ | , getScript: function() { | ||
+ | return this.script = this.script || getScriptByName("logScript"); | ||
+ | } | ||
+ | , scrollDown: function(sv) { | ||
sv.post(new Runnable() { | sv.post(new Runnable() { | ||
run: function() { | run: function() { | ||
Line 219: | Line 108: | ||
}); | }); | ||
} | } | ||
- | , initCv: function(){ | + | , getCVItem: function() { |
- | if(this.cv == null) | + | if(!this.cvItem) { |
- | this.cv = LL.getItemById(LL.getScriptByName("logScript").getTag("cvId")); | + | this.cvItem = getActiveScreen().getAllItemsById(this.getScript().getTag("cvId"))[0]; |
- | if (this.cv == null) throw ("No custom view found!") | + | if(!this.cvItem) throw new Error("Custom view not found!") |
+ | } | ||
+ | return this.cvItem | ||
} | } | ||
- | , initSv: function(){ | + | , getCV: function() { |
- | if(this.cv == null) | + | if(!this.cv) { |
- | this.initCv(); | + | this.cv = this.getCVItem().getView() |
- | var cv = this.cv.getView(); | + | if(!this.cv) throw new Error("Custom view not loaded!"); |
- | //if(cv != null){} | + | } |
- | for(var i = 0; i < cv.getChildCount(); i++){ | + | return this.cv |
+ | } | ||
+ | , getSV: function() { | ||
+ | if(!this.sv) { | ||
+ | var cv = this.getCV() | ||
+ | for(var i = 0; i < cv.getChildCount(); i++) { | ||
this.sv = cv.getChildAt(i); | this.sv = cv.getChildAt(i); | ||
- | if(this.sv.getTag()=="sv") | + | if(this.sv.getTag() == "sv") |
break; | break; | ||
} | } | ||
- | //} | + | } |
+ | return this.sv | ||
} | } | ||
- | , initTv: function(){ | + | , getTV: function() { |
- | if(this.sv == null) | + | if(!this.tv) |
- | this.initSv() | + | this.tv = this.getSV().getChildAt(0); |
- | this.tv = this.sv.getChildAt(0); | + | return this.tv |
} | } | ||
- | }; | + | , createCVItem: function(c) { |
+ | var script = getCurrentScript(); | ||
+ | var scriptId = script.getId(); | ||
+ | var cv = c.addCustomView(0, 0); | ||
+ | var prop = cv.getProperties().edit(); | ||
+ | script.setTag("cvId", cv.getId()); | ||
+ | prop.getBox("i.box").setColor("c", "n", logScript.bgColor); | ||
+ | prop.setString("v.onCreate", scriptId).commit(); | ||
+ | prop.setEventHandler("i.resumed", EventHandler.RUN_SCRIPT, scriptId); | ||
+ | prop.setEventHandler("i.paused", EventHandler.RUN_SCRIPT, scriptId); | ||
+ | prop.commit(); | ||
+ | return cv; | ||
+ | } | ||
+ | , isTVAvailable: function() { | ||
+ | try { | ||
+ | logScript.getTV() | ||
+ | return true; | ||
+ | } catch(e) { | ||
+ | if(e.message != "Custom view not found!" && e.message != "Custom view not loaded!") { | ||
+ | throw e; | ||
+ | } | ||
+ | return false; | ||
+ | } | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | bindClass("java.io.File"); | ||
+ | bindClass("java.io.FileWriter"); | ||
+ | bindClass("java.io.PrintWriter"); | ||
+ | bindClass("java.io.BufferedWriter"); | ||
+ | bindClass("android.widget.TextView"); | ||
+ | bindClass("android.text.method.ScrollingMovementMethod"); | ||
+ | bindClass("android.text.Html"); | ||
+ | bindClass("java.io.BufferedReader"); | ||
+ | bindClass("java.io.FileReader"); | ||
+ | bindClass("android.widget.ScrollView"); | ||
+ | bindClass("android.view.View"); | ||
+ | bindClass("java.lang.Runnable"); | ||
+ | bindClass("android.widget.Button"); | ||
+ | bindClass("android.widget.FrameLayout"); | ||
+ | bindClass("android.view.Gravity"); | ||
- | LL.bindClass("java.io.File"); | + | function log(text, logLevel /**/ ) { |
- | LL.bindClass("java.io.FileWriter"); | + | logLevel = logLevel || logScript.logLevel[logScript.defaultLogLevel]; |
- | LL.bindClass("java.io.BufferedWriter"); | + | |
- | LL.bindClass("android.widget.TextView"); | + | |
- | LL.bindClass("android.text.method.ScrollingMovementMethod"); | + | |
- | LL.bindClass("android.text.Html"); | + | |
- | LL.bindClass("java.io.BufferedReader"); | + | |
- | LL.bindClass("java.io.FileReader"); | + | |
- | LL.bindClass("android.widget.ScrollView"); | + | |
- | LL.bindClass("android.view.View"); | + | |
- | LL.bindClass("java.lang.Runnable"); | + | |
- | LL.bindClass("android.widget.Button"); | + | |
- | LL.bindClass("android.widget.FrameLayout"); | + | |
- | LL.bindClass("android.view.Gravity"); | + | |
- | function log(text, channel) { | + | for(var i = 2; i < arguments.length; i++) { |
- | var date = new Date(); | + | var newText = text.replace("{}", arguments[i]); |
- | var month = date.getMonth() + 1; | + | if(newText == text) { |
- | var scriptName = LL.getCurrentScript().getName(); | + | break; |
- | if (channel == null) channel = "normalLog"; | + | } else { |
- | switch (channel) { | + | text = newText; |
- | case "ERROR": | + | } |
- | var channelColor = "#ff0000"; | + | } |
- | break; | + | |
- | case "debug": | + | var date = new Date(); |
- | var channelColor = "#0000ff" | + | var logText = "<font color=#add8e6>" + date.getDate() + "-" + (date.getMonth() + 1) + |
- | break; | + | "</font>" + " <font color=#ffff00>" + date.toTimeString().slice(0, 8) + |
- | default: | + | "</font>" + " <font color=#00ff00>" + logScript.currentScript.getName() + "</font>" + |
- | var channelColor = "#ffffff"; | + | " <font color=" + logLevel.color + ">" + logLevel.name + "</font>" + ": " + |
- | break; | + | " <font color=" + logScript.textColors[logScript.ind] + ">" + text + |
- | } | + | "</font>"; |
- | var logText = "<br><font color=#add8e6>" + date.getDate() + "-" + month + | + | |
- | "</font>" + " <font color=#ffff00>" + date.toTimeString().slice(0, 8) + | + | if(logScript.isTVAvailable()) { |
- | "</font>" + " <font color=#00ff00>" + scriptName + "</font>" + | + | var tv = logScript.getTV() |
- | " <font color=" + channelColor + ">" + channel + "</font>" + ": " + | + | var tvOldLength = tv.length() |
- | " <font color=" + logScript.textColors[logScript.ind] + ">" + text + | + | tv.append(Html.fromHtml(logText).append("\n")); |
- | "</font>"; | + | logScript.scrollDown(logScript.getSV()); |
- | + | if(logScript.saveMode) { | |
- | if(logScript.sv != null){ | + | var script = logScript.getScript() |
- | logScript.tv.append(Html.fromHtml(logText)); | + | var prevLogInd = script.getTag("prevLogInd") || -1 |
- | logScript.scrollDown(logScript.sv); | + | var prevLog = JSON.parse(script.getTag("log" + prevLogInd)) |
+ | var tvNewLength = tv.length() | ||
+ | if(prevLog && prevLog[1] == tvOldLength) { | ||
+ | prevLog[1] = tv.length() | ||
+ | script.setTag("log" + prevLogInd, JSON.stringify(prevLog)); | ||
+ | } else { | ||
+ | prevLogInd++ | ||
+ | script.setTag("log" + prevLogInd, JSON.stringify([tvOldLength, tvNewLength])) | ||
+ | script.setTag("prevLogInd", prevLogInd) | ||
+ | } | ||
+ | } | ||
+ | } else { | ||
+ | logScript.logText += logText; | ||
} | } | ||
- | | ||
- | try { | ||
- | if (logScript.bfWriter == null) | ||
- | logScript.bfWriter = new BufferedWriter(new FileWriter(new File(logScript.logFilePath), true)); | ||
- | logScript.bfWriter.append(logText); | ||
- | } catch (e) { | ||
- | alert(e); | ||
- | } | ||
} | } | ||
- | function read(filePath) { | + | function cleanEval(text) { |
- | try { | + | eval('function execute() {' + text + '}'); |
- | var r = new BufferedReader(new FileReader(filePath)); | + | execute(); |
- | var s = ""; | + | |
- | var l; | + | |
- | while ((l = r.readLine()) != null) s += (l + "\n"); | + | |
- | return s; | + | |
- | } catch (e) { | + | |
- | return ""; | + | |
- | } | + | |
} | } | ||
- | if (LL.getCurrentScript().getName() == "logScript") { | + | return(function() { |
- | var script = LL.getCurrentScript(); | + | function read(filePath) { |
+ | try { | ||
+ | var r = new BufferedReader(new FileReader(filePath)); | ||
+ | var s = ""; | ||
+ | var l; | ||
+ | while((l = r.readLine()) != null) s += (l + "\n"); | ||
+ | return s; | ||
+ | } catch(e) { | ||
+ | return ""; | ||
+ | } | ||
+ | } | ||
- | try { | + | if(logScript.currentScript.getName() == "logScript") { |
- | typeof item; | + | try { |
- | } catch (e) { | + | typeof item; |
- | // in create | + | } catch(e) { |
- | var cv = item; | + | // in create |
- | var context = LL.getContext(); | + | var cv = item; |
- | var tv = new TextView(context); | + | var context = getActiveScreen().getContext(); |
- | var sv = new ScrollView(context); | + | var tv = new TextView(context); |
- | sv.addView(tv); | + | var sv = new ScrollView(context); |
- | sv.setTag("sv"); | + | sv.addView(tv); |
+ | sv.setTag("sv"); | ||
- | tv.setText(Html.fromHtml(read(logScript.logFilePath))); | + | tv.setMovementMethod(new ScrollingMovementMethod()); |
- | tv.setMovementMethod(new ScrollingMovementMethod()); | + | tv.setTextColor(0xffffffff) |
- | + | ||
- | cv.setVerticalGrab(true); | + | |
- | tv.setVerticalScrollBarEnabled(true); | + | |
- | + | ||
- | if (!logScript.lineWrapping) { | + | |
- | tv.setHorizontallyScrolling(true); | + | |
- | cv.setHorizontalGrab(true); | + | |
- | } | + | |
- | + | ||
- | var button = new Button(context); | + | |
- | button.setText("clear log"); | + | |
- | button.setOnClickListener(new View.OnClickListener() { | + | |
- | onClick: function(v) { | + | |
- | new File(logScript.logFilePath).delete(); | + | |
- | try{ | + | |
- | logScript.initTv(); | + | |
- | tv.setText(""); | + | |
- | }catch(e){} | + | |
- | } | + | |
- | }); | + | |
- | button.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.TOP | Gravity.RIGHT)); | + | |
- | + | ||
- | var layout = new FrameLayout(context); | + | |
- | layout.addView(sv); | + | |
- | layout.addView(button); | + | |
- | + | ||
- | logScript.scrollDown(sv); | + | |
- | return layout; | + | |
- | } | + | |
- | var e = LL.getEvent(); | + | cv.setVerticalGrab(true); |
- | var src = e.getSource(); | + | tv.setVerticalScrollBarEnabled(true); |
- | if (src == "MENU_APP") { | + | |
- | //in container | + | if(!logScript.lineWrapping) { |
- | var scriptId = script.getId(); | + | tv.setHorizontallyScrolling(true); |
- | var e = LL.getEvent(); | + | cv.setHorizontalGrab(true); |
- | var c = e.getContainer(); | + | } |
- | var cv = c.addCustomView(e.getTouchX(), e.getTouchY()); | + | |
- | var prop = cv.getProperties().edit(); | + | var button = new Button(context); |
- | prop.getBox("i.box").setColor("c", "n", logScript.bgColor); | + | button.setText("clear log"); |
- | prop.setString("v.onCreate", scriptId); | + | button.setOnClickListener(new View.OnClickListener() { |
- | prop.setEventHandler("i.resumed", EventHandler.RUN_SCRIPT, scriptId); | + | onClick: function(v) { |
- | prop.commit(); | + | new File(logScript.logFilePath).delete(); |
- | script.setTag("cvId", cv.getId()); | + | try { |
- | } else { | + | logScript.getTV(); |
- | // in resume | + | tv.setText(""); |
- | try{ | + | logScript.getScript().setTag("prevLogInd", null) |
- | logScript.initTv(); | + | } catch(e) {} |
- | logScript.tv.setText(Html.fromHtml(read(logScript.logFilePath))); | + | } |
- | }catch(e){} | + | }); |
- | } | + | button.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.TOP | Gravity.RIGHT)); |
- | } else { | + | |
- | //in eval() | + | var layout = new FrameLayout(context); |
- | try{logScript.initTv();}catch(e){} | + | layout.addView(sv); |
- | logScript.ind = parseInt(logScript.cv.getTag("prevColorIndex") || "0"); | + | layout.addView(button); |
- | + | ||
- | logScript.text = LL.getCurrentScript().getText(); | + | logScript.scrollDown(sv); |
- | logScript.returns = ""; | + | return layout; |
- | for(var i=0;i<logScript.text.substr(0, logScript.text.search("var logScript")).split("\n").length; i++){ | + | } |
- | logScript.returns += "\n"; | + | |
- | } | + | var e = getEvent(); |
- | logScript.text = logScript.text.substr(logScript.text.search("logScriptEnd") + 12); | + | var src = e.getSource(); |
- | if(logScript.text.charAt(0) == "*") | + | switch(src) { |
- | logScript.text = logScript.text.slice(2, logScript.text.length); | + | case "MENU_APP": |
- | logScript.text = logScript.returns + logScript.text; | + | //in container |
- | + | var cv = logScript.createCVItem(e.getContainer()) | |
- | alert(logScript.text); | + | cv.setPosition(e.getTouchX(), e.getTouchY()) |
- | try { | + | break; |
- | return eval('(function() {' + logScript.text + '}())'); | + | case "MENU_ITEM": |
- | } catch (e) { | + | var it = e.getItem(); |
- | log("At line " + e.lineNumber + ": " + e, "ERROR"); | + | var c = it.getParent(); |
- | } finally { | + | var cv = logScript.createCVItem(c); |
- | logScript.cv.setTag("prevColorIndex", logScript.ind == logScript.textColors.length - | + | cv.setSize(it.getWidth(), it.getHeight()); |
- | 1 ? 0 : logScript.ind + 1); | + | cv.setPosition(it.getPositionX(), it.getPositionY()) |
- | if(typeof logScript.bfWriter != "undefined") logScript.bfWriter.flush(); | + | c.removeItem(it); |
- | } | + | break; |
- | } | + | case "I_RESUMED": |
+ | // in resume | ||
+ | var tv = logScript.getTV(); | ||
+ | try { | ||
+ | var logFile = new File(logScript.logFilePath); | ||
+ | } catch(e) {} | ||
+ | |||
+ | if(logFile !== null && tv.getText().length() == 0 || logFile.lastModified() > logScript.getScript().getTag("lastLoaded")) { | ||
+ | var savedLogText = read(logScript.logFilePath) | ||
+ | |||
+ | if(savedLogText.length > 30000) { | ||
+ | savedLogText = savedLogText.substring(savedLogText.length - 20000, savedLogText.length) | ||
+ | savedLogText = savedLogText.substring(savedLogText.indexOf("<br>"), savedLogText.length); | ||
+ | } | ||
+ | tv.setText(Html.fromHtml(savedLogText)); | ||
+ | try { | ||
+ | var pw = new PrintWriter(new File(logScript.logFilePath)); | ||
+ | pw.print(""); | ||
+ | pw.write(savedLogText); | ||
+ | } catch(e) {} finally { | ||
+ | if(pw) pw.close(); | ||
+ | } | ||
+ | logScript.getScript().setTag("lastLoaded", new Date().getTime()) | ||
+ | } | ||
+ | logScript.scrollDown(logScript.getSV()); | ||
+ | break; | ||
+ | case "I_PAUSED": | ||
+ | try { | ||
+ | var bfWriter = new BufferedWriter(new FileWriter(new File(logScript.logFilePath), true)); | ||
+ | var unsavedLog; | ||
+ | var i = 0; | ||
+ | var script = logScript.getScript() | ||
+ | while(unsavedLog = script.getTag("log" + i)) { | ||
+ | var unsavedLog = JSON.parse(unsavedLog) | ||
+ | var fullText = logScript.getTV().getEditableText(); | ||
+ | if(fullText && fullText.length() != 0 && fullText.length() >= unsavedLog[1]) { | ||
+ | var unsavedLogTextString = Html.toHtml(fullText.subSequence(unsavedLog[0], unsavedLog[1])); | ||
+ | bfWriter.append(unsavedLogTextString.substring(unsavedLogTextString.indexOf('>') + 1, unsavedLogTextString.lastIndexOf('<'))); | ||
+ | } | ||
+ | script.setTag("log" + i, null) | ||
+ | i++; | ||
+ | } | ||
+ | script.setTag("prevLogInd", null); | ||
+ | } catch(e) {} finally { | ||
+ | if(bfWriter) bfWriter.close(); | ||
+ | } | ||
+ | break; | ||
+ | } | ||
+ | } else { | ||
+ | //in eval() | ||
+ | logScript.ind = parseInt(logScript.getScript().getTag("prevColorIndex") || "0"); | ||
+ | var tvAvailable = logScript.isTVAvailable() | ||
+ | if(!tvAvailable) | ||
+ | logScript.logText = ""; | ||
+ | |||
+ | var text = logScript.currentScript.getText(); | ||
+ | var returns = ""; | ||
+ | for(var i = 0; i < text.substr(0, text.search("var logScript")).split("\n").length; i++) { | ||
+ | returns += "\n"; | ||
+ | } | ||
+ | text = text.substr(text.search("logScriptEnd") + 12); | ||
+ | if(text.charAt(0) == "*") { | ||
+ | text = text.slice(2, text.length); | ||
+ | returns = returns.slice(1, returns.length); | ||
+ | } | ||
+ | var counter = 0; | ||
+ | for(var i = 0; i < text.length; i++) { | ||
+ | if(text.charAt(i) == "{") | ||
+ | counter++; | ||
+ | else if(text.charAt(i) == "}") | ||
+ | counter--; | ||
+ | |||
+ | if(counter < 0) { | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | text = returns + text.slice(0, i); | ||
+ | |||
+ | try { | ||
+ | return cleanEval(text); | ||
+ | } catch(e) { | ||
+ | log("At line " + e.lineNumber + ": " + e, logScript.logLevel.ERROR); | ||
+ | } finally { | ||
+ | logScript.getScript().setTag("prevColorIndex", logScript.ind == logScript.textColors.length - 1 ? 0 : logScript.ind + 1); | ||
+ | } | ||
+ | |||
+ | if(!tvAvailable) { | ||
+ | try { | ||
+ | var bfWriter = new BufferedWriter(new FileWriter(new File(logScript.logFilePath), true)); | ||
+ | bfWriter.append(logScript.logText) | ||
+ | } catch(e) { | ||
+ | alert("At line " + e.lineNumber + ": " + e); | ||
+ | } finally { | ||
+ | if(bfWriter) bfWriter.close(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | })(); | ||
</sxh> | </sxh> |