User Tools

Site Tools


sample_code:backup_apps

Differences

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

Link to this comparison view

sample_code:backup_apps [2015/01/11 19:43]
stevegarman created
sample_code:backup_apps [2015/11/15 00:00]
Line 1: Line 1:
-====== Backup your apps ====== 
-Following a reminder from Dave on the discussion group that we should be backing up our apps regularly, I thought I would post a simple way of sending yourself an email archive of your DroidScript folder. 
  
-The following code is intended as a guide to one way you can keep a snapshot of your code at any given time. 
- 
-You may well find the archiving code more useful than the email code. 
- 
-<code JavaScript archive.js> 
-var mygmail = "mymail@gmail.com"; 
-var mypass = "mypassword"; 
- 
-//Called when application is started. 
-function OnStart() 
-{ 
-    //Create a layout with objects vertically centered. 
-    lay = app.CreateLayout( "linear", "FillXY" );     
- 
- 
-    //Create a text label and add it to layout. 
-    txt = app.CreateText( "..." ); 
-    lay.AddChild( txt ); 
- 
-    //Add layout to app.     
-    app.AddLayout( lay ); 
-    var fname = CreateArchive(); 
-    sendGmail( mygmail, mypass, fname); 
-} 
- 
-//Create a backup archive. 
- 
-function CreateArchive( ) 
-{ 
-    var archfolder = "/sdcard/sjgapps/archive/";  
-    app.MakeFolder(archfolder); 
-    //Create project zip file. 
-    var zip = app.CreateZipUtil(); 
-    var fldr = "/sdcard/DroidScript"; 
-    var archname = (new Date).toISOString().slice(0,19 ); 
-    archname = "ds"+archname.replace(/[-:]/gi,""); 
-    archname =  archname.replace(/T/gi,"_"); 
-    var file = archfolder+archname+".zip"; 
-    zip.Create( file ); 
- 
-    app.ShowProgress( "Archiving..." ); 
-    AddFolder( zip, "DroidScript", fldr ); 
-    app.HideProgress(); 
-    zip.Close(); 
- 
-    return file; 
-} 
- 
- 
-//Recursively add folder contents to zip. 
- 
-function AddFolder( zip, name, fldr ) 
- 
-{ 
-    txt.SetText(name); 
-    var list = app.ListFolder( fldr,"",0,"alphasort" ); 
-    for( var i=0; i<list.length; i++ ) 
-    { 
-        var title = list[i]; 
-        if( !app.IsFolder( fldr+"/"+title ) ) 
-            zip.AddFile( name+"/"+title, fldr+"/"+title ); 
-        else 
-            AddFolder( zip, name+"/"+title, fldr+"/"+title ); 
-    } 
-} 
- 
-function sendGmail(addr,pass,attach) 
-{ 
-    //Create email object.  
-    var email = app.CreateEmail( addr, pass );  
-    email.SetSMTP( "smtp.gmail.com", 465 );  
-    email.SetOnStatus( email_OnStatus ); 
- 
-    //Send the email message.  
-    app.ShowProgress( "Sending..." );  
-    email.Send( attach, "DroidScript backup for you.",   
-       addr, addr, attach );  
-} 
- 
-//Handle status messages.  
-function email_OnStatus( status )  
- 
-    app.HideProgress();  
-    app.Alert( status );  
- 
- 
-</code> 
-===== Warnings ===== 
-==== Personalise the code ==== 
-You must change the email address and password in the first two lines 
-==== Version required ==== 
-This code makes the assumption that you are using at least version 1.15 of DroidScript, as the path for your apps is hard coded but older versions used a different path 
-==== Possible Gmail Problem ==== 
-The code used for emailing is pretty simplistic and you may get an authentication error if your gmail account is set to high security. 
-==== Filling your sdcard ==== 
-There is no code included to delete the zip files. 
- 
-Over time, /sdcard/sjgapps/archive will build up all the archives you create. 
- 
-You should probably delete those files manually from time to time, probably after emailing them or placing them in your cloud storage. 
sample_code/backup_apps.txt · Last modified: 2015/11/15 00:00 (external edit)