@@ -646,13 +646,15 @@ private String saveProject() {
646646 if (oldFileName != null ) {
647647 File tempFile = new File (oldFileName );
648648 tempFile .delete ();
649+ oldFileName = null ;
649650 }
650651 String saveFileName = title + " by " + author + ".buildmlearn" ;
651652 saveFileName = saveFileName .replaceAll (" " , "-" );
652653
653654
654655 FileUtils .saveXmlFile (toolkit .getSavedDir (), saveFileName , doc );
655656
657+ oldFileName = toolkit .getSavedDir () + saveFileName ;
656658
657659 return toolkit .getSavedDir () + saveFileName ;
658660 } catch (ParserConfigurationException e ) {
@@ -662,6 +664,96 @@ private String saveProject() {
662664 return null ;
663665 }
664666
667+ @ Override
668+ public void onBackPressed () {
669+ super .onBackPressed ();
670+ if (saveDraft ()!=null )
671+ Toast .makeText (getApplicationContext (),"Saved in Draft!" ,Toast .LENGTH_SHORT ).show ();
672+
673+ }
674+
675+ /**
676+ * Converts the current TemplateInterface object into a xml file. Xml file is saved in DRAFT
677+ * Directory (defined in constants). File name is of the format: draft<0-X>.buildmlearn
678+ *
679+ * @return Absolute path of the saved file. Null if there is some error.
680+ * @brief Saves the current project into a .buildmlearn file.
681+ */
682+ protected String saveDraft () {
683+
684+ String author = ((EditText ) findViewById (R .id .author_name )).getText ().toString ();
685+ String title = ((EditText ) findViewById (R .id .template_title )).getText ().toString ();
686+
687+
688+ DocumentBuilderFactory docFactory = DocumentBuilderFactory .newInstance ();
689+ DocumentBuilder docBuilder ;
690+ try {
691+
692+ docBuilder = docFactory .newDocumentBuilder ();
693+ Document doc = docBuilder .newDocument ();
694+ Element rootElement = doc .createElement ("buildmlearn_application" );
695+ Attr attr = doc .createAttribute ("type" );
696+ attr .setValue (getResources ().getString (template .getType ()));
697+ rootElement .setAttributeNode (attr );
698+
699+ Element authorElement = doc .createElement ("author" );
700+ rootElement .appendChild (authorElement );
701+
702+ Element nameElement = doc .createElement ("name" );
703+ nameElement .appendChild (doc .createTextNode (author ));
704+
705+ authorElement .appendChild (nameElement );
706+
707+ Element titleElement = doc .createElement ("title" );
708+ titleElement .appendChild (doc .createTextNode (title ));
709+ rootElement .appendChild (titleElement );
710+
711+ doc .appendChild (rootElement );
712+ Element dataElement = doc .createElement ("data" );
713+ rootElement .appendChild (dataElement );
714+ if (selectedTemplate .getItems (doc ).size () == 0 ) {
715+ Toast .makeText (this , "Unable to perform action: No Data" , Toast .LENGTH_SHORT ).show ();
716+ return null ;
717+ }
718+ for (Element item : selectedTemplate .getItems (doc )) {
719+ dataElement .appendChild (item );
720+ }
721+
722+ int draftFileIndex = 0 ;
723+ File draftDir = new File (toolkit .getDraftDir ());
724+ String probableFileName = "draft" +draftFileIndex + ".buildmlearn" ;
725+ File probableFile = new File (draftDir , probableFileName );
726+
727+ while (probableFile .exists ()) {
728+ draftFileIndex ++;
729+ probableFileName = "draft" +draftFileIndex + ".buildmlearn" ;
730+ probableFile = new File (draftDir , probableFileName );
731+ }
732+
733+ //Create temporary File, if that file content matches with OldContent
734+ // Then Don't make Draft
735+ File tempFile = new File (toolkit .getDraftDir (), ".temp" );
736+ File oldFile = null ;
737+ if (oldFileName !=null )
738+ oldFile = new File (oldFileName );
739+
740+ FileUtils .saveXmlFile (toolkit .getDraftDir (), ".temp" , doc );
741+ if (oldFile == null || !FileUtils .equalContent (tempFile ,oldFile )) {
742+ tempFile .renameTo (probableFile );
743+ return toolkit .getDraftDir () + probableFileName ;
744+ } else {
745+ File newFile = new File (toolkit .getDraftDir (), ".temp" );
746+ newFile .delete ();
747+ }
748+ return null ;
749+
750+ } catch (ParserConfigurationException e ) {
751+ e .printStackTrace ();
752+ }
753+ return null ;
754+ }
755+
756+
665757 /**
666758 * @brief Start the simulator activity
667759 * <p/>
@@ -720,6 +812,11 @@ private void parseSavedFile(String path) {
720812 if (templateId == 5 ) {
721813 populateMetaView (selectedTemplate .loadProjectMetaEditor (this , doc ));
722814 }
815+ File draftDir = new File (toolkit .getDraftDir ());
816+ if (fXmlFile .getParentFile ().compareTo (draftDir ) ==0 ) {
817+ //If Draft File
818+ fXmlFile .delete ();
819+ }
723820 setUpActionBar ();
724821 updateHeaderDetails (name , title );
725822
0 commit comments