@@ -537,13 +537,15 @@ protected String saveProject() {
537537 if (oldFileName != null ) {
538538 File tempFile = new File (oldFileName );
539539 tempFile .delete ();
540+ oldFileName = null ;
540541 }
541542 String saveFileName = title + " by " + author + ".buildmlearn" ;
542543 saveFileName = saveFileName .replaceAll (" " , "-" );
543544
544545
545546 FileUtils .saveXmlFile (toolkit .getSavedDir (), saveFileName , doc );
546547
548+ oldFileName = toolkit .getSavedDir () + saveFileName ;
547549
548550 return toolkit .getSavedDir () + saveFileName ;
549551 } catch (ParserConfigurationException e ) {
@@ -553,6 +555,96 @@ protected String saveProject() {
553555 return null ;
554556 }
555557
558+ @ Override
559+ public void onBackPressed () {
560+ super .onBackPressed ();
561+ if (saveDraft ()!=null )
562+ Toast .makeText (getApplicationContext (),"Saved in Draft!" ,Toast .LENGTH_SHORT ).show ();
563+
564+ }
565+
566+ /**
567+ * Converts the current TemplateInterface object into a xml file. Xml file is saved in DRAFT
568+ * Directory (defined in constants). File name is of the format: draft<0-X>.buildmlearn
569+ *
570+ * @return Absolute path of the saved file. Null if there is some error.
571+ * @brief Saves the current project into a .buildmlearn file.
572+ */
573+ protected String saveDraft () {
574+
575+ String author = ((EditText ) findViewById (R .id .author_name )).getText ().toString ();
576+ String title = ((EditText ) findViewById (R .id .template_title )).getText ().toString ();
577+
578+
579+ DocumentBuilderFactory docFactory = DocumentBuilderFactory .newInstance ();
580+ DocumentBuilder docBuilder ;
581+ try {
582+
583+ docBuilder = docFactory .newDocumentBuilder ();
584+ Document doc = docBuilder .newDocument ();
585+ Element rootElement = doc .createElement ("buildmlearn_application" );
586+ Attr attr = doc .createAttribute ("type" );
587+ attr .setValue (getResources ().getString (template .getType ()));
588+ rootElement .setAttributeNode (attr );
589+
590+ Element authorElement = doc .createElement ("author" );
591+ rootElement .appendChild (authorElement );
592+
593+ Element nameElement = doc .createElement ("name" );
594+ nameElement .appendChild (doc .createTextNode (author ));
595+
596+ authorElement .appendChild (nameElement );
597+
598+ Element titleElement = doc .createElement ("title" );
599+ titleElement .appendChild (doc .createTextNode (title ));
600+ rootElement .appendChild (titleElement );
601+
602+ doc .appendChild (rootElement );
603+ Element dataElement = doc .createElement ("data" );
604+ rootElement .appendChild (dataElement );
605+ if (selectedTemplate .getItems (doc ).size () == 0 ) {
606+ Toast .makeText (this , "Unable to perform action: No Data" , Toast .LENGTH_SHORT ).show ();
607+ return null ;
608+ }
609+ for (Element item : selectedTemplate .getItems (doc )) {
610+ dataElement .appendChild (item );
611+ }
612+
613+ int draftFileIndex = 0 ;
614+ File draftDir = new File (toolkit .getDraftDir ());
615+ String probableFileName = "draft" +draftFileIndex + ".buildmlearn" ;
616+ File probableFile = new File (draftDir , probableFileName );
617+
618+ while (probableFile .exists ()) {
619+ draftFileIndex ++;
620+ probableFileName = "draft" +draftFileIndex + ".buildmlearn" ;
621+ probableFile = new File (draftDir , probableFileName );
622+ }
623+
624+ //Create temporary File, if that file content matches with OldContent
625+ // Then Don't make Draft
626+ File tempFile = new File (toolkit .getDraftDir (), ".temp" );
627+ File oldFile = null ;
628+ if (oldFileName !=null )
629+ oldFile = new File (oldFileName );
630+
631+ FileUtils .saveXmlFile (toolkit .getDraftDir (), ".temp" , doc );
632+ if (oldFile == null || !FileUtils .equalContent (tempFile ,oldFile )) {
633+ tempFile .renameTo (probableFile );
634+ return toolkit .getDraftDir () + probableFileName ;
635+ } else {
636+ File newFile = new File (toolkit .getDraftDir (), ".temp" );
637+ newFile .delete ();
638+ }
639+ return null ;
640+
641+ } catch (ParserConfigurationException e ) {
642+ e .printStackTrace ();
643+ }
644+ return null ;
645+ }
646+
647+
556648 /**
557649 * @brief Start the simulator activity
558650 * <p/>
@@ -608,6 +700,11 @@ protected void parseSavedFile(String path) {
608700 Object templateObject = templateClass .newInstance ();
609701 selectedTemplate = (TemplateInterface ) templateObject ;
610702 populateListView (selectedTemplate .loadProjectTemplateEditor (this , items ));
703+ File draftDir = new File (toolkit .getDraftDir ());
704+ if (fXmlFile .getParentFile ().compareTo (draftDir ) ==0 ) {
705+ //If Draft File
706+ fXmlFile .delete ();
707+ }
611708 setUpActionBar ();
612709 updateHeaderDetails (name , title );
613710
0 commit comments