📄 script.hcl
字号:
# The default script: set script {proc AddOne {num} { return [+ $num 1]}set res [AddOne 41]alert "Result: $res"set res} set editor [edittext -new $context -text $script -layoutparams $layoutparams] $layout addview $editor set eval [button -new $context -text "Eval" -layoutparams $layoutparams] $layout addview $eval set results [textview -new $context -text "Results:" -layoutparams $layoutparams] $layout addview $results set callback [callback -new [list [list EditCallback $editor $results]]] $eval setonclicklistener $callback [activity] setcontentview $layout}# EditCallback --## Run the script and display the results.proc EditCallback {editor results button} { set res "Results: [eval [$editor gettext]]" $results settext $res}# Contacts --## Display the phone's contact list.CreateActivity Contacts "Contacts" { set context [activity] set layoutparams [linearlayoutparams -new {FILL_PARENT WRAP_CONTENT}] set layout [linearlayout -new $context] $layout setorientation VERTICAL $layout addview [textview -new $context \ -layoutparams $layoutparams \ -text "Contacts:" -textsize 32.0] set cursor [contentQuery content://contacts/people/] set i 0 while { $cursor next } { set name [$cursor getstring [$cursor getcolumnindex name]] set number [$cursor getstring [$cursor getcolumnindex number]] $layout addview [textview -new $context \ -layoutparams $layoutparams \ -text "Who: $name Number: $number"] incr $i } if { = $i 0 } { $layout addview [textview -new $context \ -layoutparams $layoutparams \ -text "No contacts"] } [activity] setcontentview $layout}# TaskList --## Display a list of taks, and let the user switch between them.CreateActivity TaskList "Task List" { set context [activity] set layoutparams [linearlayoutparams -new {FILL_PARENT WRAP_CONTENT}] set layout [linearlayout -new $context] $layout setorientation VERTICAL # Create ourselves some commands to access Android internals. java android.app.ActivityManagerNative activitymanagernative java android.app.IActivityManager iactivitymanager java android.content.ComponentName componentname java {android.app.ActivityManager$RunningTaskInfo} runningtaskinfo # Utilized to contain the result of gettasks set am [activitymanagernative getdefault] set tasks [$am gettasks 10 0 [null]] set tasklist {} set taskIdList {} foreach task $tasks { set baseactivity [$task -field baseactivity] lappend $tasklist "Task: [$baseactivity getpackagename]" lappend $taskIdList [$task -field id] } androidlog "IDs: $taskIdList" $layout addview [textview -new $context -layoutparams $layoutparams \ -text "Currently running tasks. Click to switch:"] set lview [basiclist $context $tasklist -layoutparams $layoutparams] $layout addview $lview $lview requestfocus set callback [callback -new [list [list SelectTask $taskIdList]]] $lview setonitemclicklistener $callback [activity] setcontentview $layout}proc SelectTask {taskIdList parent view position id} { [activitymanagernative getdefault] movetasktofront [lindex $taskIdList $position]}# SelectScripts --## Display the available Hecl scripts in permanent storage.CreateActivity SelectScripts "Hecl Scripts" { set context [activity] set layoutparams [linearlayoutparams -new {FILL_PARENT WRAP_CONTENT}] set layout [linearlayout -new $context -layoutparams $layoutparams] $layout setorientation VERTICAL set cursor [contentQuery content://org.hecl.android.Scripts/scripts] $layout addview [textview -new $context -layoutparams $layoutparams -text "Available scripts:" -textsize 30.0] set scriptlist [list] while { $cursor next } { lappend $scriptlist [$cursor getstring 1] } set lview [basiclist $context $scriptlist -layoutparams $layoutparams] $layout addview $lview $lview requestfocus [activity] setcontentview $layout}# HeclServer --## Create a Hecl command-line server. Listen for connections and# execute commands.CreateActivity HeclServer "Hecl Server" { set context [activity] set layoutparams [linearlayoutparams -new {FILL_PARENT WRAP_CONTENT}] set layout [linearlayout -new $context -layoutparams $layoutparams] $layout setorientation VERTICAL set port 7405 $layout addview [textview -new $context \ -layoutparams $layoutparams \ -text "Running Hecl server on port ${port}. You can telnet to this port to interact with Hecl after running this command: adb forward tcp:7405 tcp:7405\nSince the interpreter that you are accessing is a sub-interpreter, to run commands in the main interpreter, which has access to the Android GUI thread, you have to pass them with the maineval command, like this: maineval { ... code ... }"] set recvcode [textview -new $context -layoutparams $layoutparams] $layout addview $recvcode [activity] setcontentview $layout # We currently run the server in its own interpreter/thread, which # has some consequences: we can't do GUI stuff, or interact with # the rest of the running program in a meaningful way. We use a # handler to do that via the maineval proc. set newi [interp -new [list]] [activity] loadlibs $newi set hh [[activity] getHandler] $newi setVar "handler" $hh $newi setVar "port" $port $newi evalAsync { java java.io.InputStream inputstream java java.io.OutputStream outputstream java java.io.InputStreamReader inputstreamreader java java.io.OutputStreamWriter outputstreamwriter java java.io.BufferedReader bufferedreader java java.net.ServerSocket serversock java java.net.Socket socket java org.hecl.android.HeclHandler heclhandler # Create a server socket. set serverSock [serversock -new [list $port]] set sock [$serverSock accept] # Create some line-oriented IO streams. set is [$sock getinputstream] set isr [inputstreamreader -new [list $is]] set br [bufferedreader -new [list $isr]] set os [$sock getoutputstream] set osw [outputstreamwriter -new [list $os]] global osw # Print something back to the socket. proc put {str} { global osw $osw write [s ${str}] $osw flush } proc puts {str} { put "${str}\n" } # Evaluate a command in the main interpreter - note that we # don't get the results back. proc maineval {code} { global handler set msg [message -new [list]] $msg -field obj $code $handler sendmessage $msg } java android.os.Message message # Main server loop. androidlog "Server operational on port $port" while { true } { if { = 1 [catch { put "> " puts [eval [$br readline]] } err] } { set errstr "HeclServer error: $err" androidlog $errstr puts $errstr } } }}CreateActivity SendGTalk "Send GTalk Message" { set context [activity] set layoutparams [linearlayoutparams -new {FILL_PARENT WRAP_CONTENT}] set layout [linearlayout -new $context -layoutparams $layoutparams] $layout setorientation VERTICAL $layout addview [textview -new $context \ -layoutparams $layoutparams \ -text "Sending GTalk message to David Welton, Hecl creator:"] set msgtext [edittext -new $context -layoutparams $layoutparams] set sendbutton [button -new $context -layoutparams $layoutparams -text "Send"] set callback [callback -new [list [list SendMessage $msgtext]]] $sendbutton setonclicklistener $callback set answers [textview -new $context -layoutparams $layoutparams -text ""] java org.hecl.Interp interp java org.hecl.android.HeclServiceConnection hserviceconnection java android.content.Intent intent java com.google.android.gtalkservice.IGTalkService gtalkservice java com.google.android.gtalkservice.IGTalkSession gtalksession java {com.google.android.gtalkservice.IGTalkService$Stub} gtalkstub java {com.google.android.gtalkservice.GTalkServiceConstants} gtalkconstants java com.google.android.gtalkservice.IChatSession chatsession $layout addview $msgtext $layout addview $sendbutton $layout addview $answers $context setcontentview $layout set conn [hserviceconnection -new [list [thisinterp]]] proc RecvMessage {args} [code { set answers $a set oldtext [$answers gettext] gui [list $answers settext "${oldtext}\n[lindex $args 1]"] } [list a $answers]] proc onserviceconnected {classname service} { set gtalkservice [gtalkstub asInterface $service] global GtalkSession set GtalkSession [[$gtalkservice getDefaultSession] createChatSession "davidnwelton@gmail.com"] set hcb [heclchatlistener -new [list [thisinterp]]] $hcb -field newMessageReceived [list RecvMessage] $GtalkSession addRemoteChatListener $hcb } $conn -field onserviceconnected onserviceconnected set i [intent -new [list]] set i [$i setcomponent [gtalkconstants -field GTALK_SERVICE_COMPONENT]] $context bindservice $i $conn 0}# SendMessage --## Send GTalk message. This currently doesn't deal with the# responsejava org.hecl.android.HeclChatListener heclchatlistener;proc SendMessage {msgtext sendbutton} { set message_txt [$msgtext gettext] global GtalkSession $GtalkSession sendTextMessage "Android Hecl User says: $message_txt"}# SelectDemo --## Select which demo to display.proc SelectDemo {parent view position id} { set dest [$view gettext] global titles_names set procname [hget $titles_names $dest] if { eq $procname "" } { # If the procname doesn't appear in the lookup table, it's one # of the following, and doesn't require its own activity. if {eq $dest "Date Picker"} { DatePicker } elseif {eq $dest "Progress Dialog"} { ProgressDialog } elseif {eq $dest "Time Picker"} { TimePicker } } else { global context newActivity $context $procname }}# viewCode --## View the code of the proc that is currently displayed.proc viewCode {} { global procname global context set layoutparams [linearlayoutparams -new {FILL_PARENT WRAP_CONTENT}] set layout [linearlayout -new $context -layoutparams $layoutparams] $layout setorientation VERTICAL set saverun [button -new $context -text "Save and Run" \ -layoutparams $layoutparams] $layout addview $saverun set scroll [scrollview -new $context -layoutparams $layoutparams] set text [intro proccode $procname] set editor [edittext -new $context \ -text [s $text] \ -layoutparams $layoutparams] $scroll addview $editor $layout addview $scroll [activity] setcontentview $layout set callback [callback -new [list [list SaveCode $editor $context $procname]]] $saverun setonclicklistener $callback set procname viewCode}# SaveCode --## Save the code and run it.proc SaveCode {editor context procname button} { set txt [$editor gettext] proc $procname {} $txt newActivity $context $procname}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -