User Tools

Site Tools


sample_code:file_picker

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
sample_code:file_picker [2014/11/22 12:55]
stevegarman [The code]
sample_code:file_picker [2015/03/27 04:03] (current)
Line 1: Line 1:
 ====== Sample File Picker ====== ====== Sample File Picker ======
-This is a simple file choosing dialog which has not yet been proerly tested on DroidScript v1.12+This is a simple file choosing dialog which is offered for use on DroidScript v1.12
  
 When the next version of DroidScript is released, I will tidy it up a bit and add a filter for file extensions. When the next version of DroidScript is released, I will tidy it up a bit and add a filter for file extensions.
  
-In the meantime, I would be grateful if someone would test it on DroidScript v1.12 and comment on wherther it works.+Thanks to Tony Jones for testing it on DroidScript v1.12 and confirming that it works. 
 + 
 +See also [[sample_code:folder_picker|Sample Folder Picker]]
 ===== The code ===== ===== The code =====
-<code js filepicker.js>+<code javascript filepicker.js>
  
-//Called when application is started +//Called when application is started 
-function OnStart(){ +function OnStart(){ 
-    //Create a layout with objects vertically centered+    //Create a layout with objects vertically centered
-    lay = app.CreateLayout( "linear", "Vertical,FillXY" );    +    lay = app.CreateLayout( "linear", "Vertical,FillXY" );    
  
-    //Create a text label and add it to layout+    //Create a text label and add it to layout
-    btn = app.CreateButton( "FilePicker demo" ); +    btn = app.CreateButton( "FilePicker demo" ); 
-    lay.AddChild( btn ); +    lay.AddChild( btn ); 
-    btn.SetOnTouch(btn_OnTouch); +    btn.SetOnTouch(btn_OnTouch); 
-     +    pick = new FilePicker(mycallback); 
-    //Add layout to app    +    // don't show hidden files 
-    app.AddLayout( lay ); +    pick.SetHideFiles(true); 
-}//function Onstart()+    //Add layout to app    
 +    app.AddLayout( lay ); 
 +}//function Onstart()
  
  
-function btn_OnTouch(){ +function btn_OnTouch(){ 
-    pick = new FilePicker(mycallback); +    pick.SetFolder("/sdcard"); 
-    pick.SetFolder("/sdcard"); +    pick.Show();     
-    pick.Show();     +}//function btn_OnTouch()
-}//function btn_OnTouch()+
  
-function mycallback(fullpath){ +function mycallback(fullpath){ 
-    app.Alert("user chose " + fullpath) +  app.Alert("user chose " + fullpath) 
-}//function mycallback()+}//function mycallback()
  
-function FilePicker(Callback,basePath){ +function FilePicker(Callback,basePath){ 
-  var self = this; +  var self = this; 
-  this.basePath basePath || "/sdcard"; +  this.basePath basePath || "/sdcard"; 
-  this.callback Callback || function(){}; +  this.callback Callback || function(){}; 
-  this.FolderPath = this.basePath; +  this.FolderPath = this.basePath; 
-  this.dlg = app.CreateDialog(this.basePath); +  this.hideHiddenFiles = false; 
-  this.lay = app.CreateLayout( "linear", "horizontal,fillxy,left" ); +  this.dlg = app.CreateDialog(this.basePath); 
-  this.lstFolds = app.CreateList("blank" , 0.35, 0.); +  this.lay = app.CreateLayout( "linear", "horizontal,fillxy,left" ); 
-  this.lstFolds.parent = self; +  this.lstFolds = app.CreateList("blank" , 0.35, 0.); 
-  this.lstFolds.SetOnTouch(FilePicker_NewFolder);   +  this.lstFolds.parent = self; 
-  this.lay.AddChild(this.lstFolds); +  this.lstFolds.SetOnTouch(FilePicker_NewFolder);   
-  this.lstFiles = app.CreateList("blank" , 0.35, 0.); +  this.lay.AddChild(this.lstFolds); 
-  this.lstFiles.parent = self; +  this.lstFiles = app.CreateList("blank" , 0.35, 0.); 
-  this.lstFiles.SetOnTouch(FilePicker_NewFile);  +  this.lstFiles.parent = self; 
-  this.lay.AddChild(this.lstFiles); +  this.lstFiles.SetOnTouch(FilePicker_NewFile);  
-  this.dlg.AddLayout(this.lay);+  this.lay.AddChild(this.lstFiles); 
 +  this.dlg.AddLayout(this.lay);
  
-  this.Show = function(){ +  this.Show = function(){ 
-    self.dlg.Show(); +    self.dlg.Show(); 
-  +  
-  this.Hide = function(){ +  this.SetHideFiles=function(val){ 
-    self.dlg.Hide(); +    if(val == undefined) val = true; 
-  +    self.hideHiddenFiles = val; 
-  this.SetFilter = function(filter){ +  } 
-      self.fileFilter = filter; +  this.Hide = function(){ 
-  +    self.dlg.Hide(); 
-  this.GetFolder = function(){ +  
-    return self.FolderPath; +  this.SetFilter = function(filter){ 
-  +      self.fileFilter = filter; 
-  this.SetFolder = function(folderPath){ +  
-    self.FolderPath = folderPath; +  this.GetFolder = function(){ 
-    self.dlg.SetTitle(folderPath); +    return self.FolderPath; 
-    app.ShowProgress( "Loading..." ); +  
-    var lst = app.ListFolder(folderPath); +  this.SetFolder = function(folderPath){ 
-    lst.sort(function(x,y){return (x.toLowerCase() > y.toLowerCase())?1:-1}); +    self.FolderPath = folderPath; 
-    var dirlist=[]+    self.dlg.SetTitle(folderPath); 
-    if( self.FolderPath != self.basePath dirlist = [".."]+    app.ShowProgress( "Loading..." ); 
-    var fillist = []; +    var lst = app.ListFolder(folderPath); 
-    var ths = lst.shift(); +    lst.sort(function(x,y){return (x.toLowerCase() > y.toLowerCase())?1:-1}); 
-    while (undefined != ths) { +    var ths lst.shift()
-      var pth folderPath + "/" + ths; +    self.lstFolds.SetList(""); 
-      if (app.IsFolder(pth)) +    if( self.FolderPath != self.basePath ) 
-         dirlist.push(ths); +      self.lstFolds.AddItem("..",null,"folder")
-      else +    self.lstFiles.SetList(""); 
-         fillist.push(ths); +    while (undefined != ths) { 
-      ths = lst.shift(); +      if ((! self.hideHiddenFiles) || (ths.indexOf(".") !0)){ 
-    +        var pth = folderPath + "/" + ths; 
-    self.lstFolds.SetList(dirlist); +        if (app.IsFolder(pth)) 
-    self.lstFiles.SetList(fillist); +           self.lstFolds.AddItem(ths,null,"folder"
-    app.HideProgress(); +        else 
-    +           self.lstFiles.AddItem(ths) 
-}//function FilePicker()+      } 
 +      ths = lst.shift(); 
 +    
 +    app.HideProgress(); 
 +    
 +}//function FilePicker()
  
-function FilePicker_NewFolder(fil){ +function FilePicker_NewFolder(fil){ 
-  var par = this.parent; +  var par = this.parent; 
-  var pth = par.GetFolder(); +  var pth = par.GetFolder(); 
-  if (fil != "..") { +  if (fil != "..") { 
-      pth += "/" + fil +      pth += "/" + fil 
-  +  
-  else{ +  else{ 
-     if( pth == par.basePath || pth == "/" ){ +     if( pth == par.basePath || pth == "/" ){ 
-       par.Hide() +       par.Hide() 
-       return; +       return; 
-     +     
-     var tst = pth.split("/"); +     var tst = pth.split("/"); 
-     tmp = tst.pop(); +     tmp = tst.pop(); 
-     pth = (tst.join("/")); +     pth = (tst.join("/")); 
-  +     if(pth=="") pth = "/"; 
-   +  
-  this.parent.SetFolder(pth); +   
-}//function FilePicker_NewFolder()+  par.SetFolder(pth); 
 +}//function FilePicker_NewFolder()
  
-function FilePicker_NewFile(fil){ +function FilePicker_NewFile(fil){ 
-  var par = this.parent; +  var par = this.parent; 
-  var pth = par.GetFolder(); +  var pth = par.GetFolder(); 
-  par.Hide(); +  par.Hide(); 
-  par.callback(pth += "/" + fil); +  par.callback(pth += "/" + fil); 
-}//function FilePicker_NewFile()+}//function FilePicker_NewFile()
  
 </code> </code>
 +
 +===== How to use it =====
 +You need three function from the code above
 +  
 +<code>function FilePicker</code>
 +
 +<code>function FilePicker_NewFolder</code>
 + 
 +<code>function FilePicker_NewFile</code>
 +    
 +    
 +In your main code, you need something like
 +
 +<code>    pick = new FilePicker(myCallBack);
 +    pick.SetFolder("/sdcard");
 +    pick.Show();</code>    
 +
 +where myCallBack is a function which is called when a file is chosen. It is defined like
 +
 +<code>function myCallBack(fullpath){
 +    app.ShowPopup("user chose " + fullpath)
 +}</code>
 +
 +and it passes you the full path of the file that is chosen.
sample_code/file_picker.1416660922.txt.gz · Last modified: 2014/11/22 20:55 (external edit)