Saturday 2 August 2014

Tool for a bit of introspection

On google groups, Andreas Rozek posted a useful tool,, AppInspector for looking at the functions of androidscript's app object.

I used this a fair bit, so I stole the main bit of the code and expanded the app a bit so that I can look at various control types.
For anyone who wants to play with it, there's an spk at http://hudl.sgarman.net/public/spk/SJGinspect.spk

The code is a bit ropey as it was intended only for home use. It looks like this:

// Introspection lite tool for home use by Steve Garman
// Main code stolen from AppInspector by Andreas Rozek 

var myobj = app;
var ListView;
var lastMenu = "app";
//Called when application is started.
function OnStart()
{
    app.EnableBackKey( false );
    
    //Create a layout with objects vertically centered.
    lay = app.CreateLayout( "linear", "Vertical,FillXY" );  
    add_top_bar( lay );
 
/**** prepare page area ****/
    var PageArea = app.CreateLayout('frame','fillxy');
    lay.AddChild(PageArea);

    control_list = "app,layout,Button,Text,Image"+
      ",CheckBox,ToggleButton,SeekBar,List"+
      ",TextEdit,WebView,CameraView,Sensor";
    menuView = app.CreateList( control_list, -1,-1, 'fillxy');
    menuView.SetOnTouch(menuView_OnTouch);
    PageArea.AddChild(menuView);
    menuView.SetVisibility('Hide');

    ListView = app.CreateList("", -1,-1, 'fillxy');
    prepareFunctionList();

    ListView.SetOnTouch(showAppFunction);
    PageArea.AddChild(ListView);

  /**** prepare detail view ****/

    FunctionView = app.CreateTextEdit('');
    PageArea.AddChild(FunctionView);
    FunctionView.SetVisibility('Hide');
    
    //Add layout to app.    
    app.AddLayout( lay );
    showMenuView();
}

function add_top_bar (lyout, opts){
  //basic top bar layout
  lay_bar = app.CreateLayout( "Linear", "Horizontal,Left, FillX" );
  lay_bar.SetBackGradient( "#444444", "#888888", "#cccccc" );

  //add app name if required - stolen for object/function name in this app
  app_name_txt = app.CreateText( lastMenu , -1, -1 );
  app_name_txt.SetTextSize( 24 );
  app_name_txt.SetPadding( 0.01, 0, 0.01, 0 );
  lay_bar.AddChild( app_name_txt );

  lyout.AddChild( lay_bar );
}

function menuView_OnTouch(name){
  lastMenu = name;
  switch(name) {
    case "layout":
        myobj = lay;
        break;
    case "Button":
        myobj = app.CreateButton("x");
        break;
    case "Text":
        myobj = app_name_txt;
        break;
    case "Image":
        myobj = app.CreateImage( "/Sys/Img/Hello.png" );
        break;
    case "CheckBox":
        myobj = app.CreateCheckBox( "x" );
        break;
    case "ToggleButton":
        myobj = app.CreateToggle( "x" );
        break;
    case "SeekBar":
        myobj = app.CreateSeekBar( "x" );
        break;
    case "List":
        myobj = ListView;
        break;
    case "TextEdit":
        myobj = FunctionView;
        break;
    case "WebView":
        myobj = app.CreateWebView( 0.8,0.8 );
        break;
    case "CameraView":
        myobj = app.CreateCameraView( 0.8,0.8 );
        break;
    case "Sensor":
        myobj = app.CreateSensor( "Accelerometer" );
        break
    default:
        myobj=app;
        lastMenu = "app";
    }   
  prepareFunctionList();
  showOverview();
  app_name_txt.SetText( lastMenu );
}

  function prepareFunctionList(){
/**** prepare list view ****/

    var objFunctionList;
    objFunctionList = [];

    for (var Key in myobj) {
      if (myobj.hasOwnProperty(Key) && (typeof myobj[Key] === 'function')) {
        objFunctionList.push(Key);
      };
    };
    objFunctionList.sort();
    ListView.SetList( objFunctionList.join(','), ",")

  }
  function showMenuView(){
    ListView.SetVisibility('hide');
    FunctionView.SetVisibility('hide');
    menuView.SetVisibility('show');
    app_name_txt.SetText( "Choose Control" );
  }

  function showAppFunction (Name) {
    FunctionView.SetText(String(myobj[Name]));
    app_name_txt.SetText( lastMenu + "." + Name );
    ListView.SetVisibility('hide');
    menuView.SetVisibility('hide');
    FunctionView.SetVisibility('show');
  }

  function showOverview () {
    ListView.SetVisibility('show');
    FunctionView.SetVisibility('hide');
    menuView.SetVisibility('hide');
    app_name_txt.SetText( lastMenu );
  }

  function OnBack(){
    if (menuView.GetVisibility() == "Show") app.Exit();
    else if( ListView.GetVisibility() == "Show" ) showMenuView();
    else showOverview();
  }

3 comments:

  1. I made a couple more amendments so I could output a text file for each control, listing its available functions.
    I put copies of the text files at http://androidscript.sgarman.net/sjgDocs/androidscript/

    ReplyDelete
  2. I have also updated the spk to show the version I used to create the files.

    I haven't changed the text of the blog post, though.

    ReplyDelete
    Replies
    1. The full amended code can be seen at http://androidscript.sgarman.net/sjgDocs/androidscript/SJGinspect.js.txt

      Delete