Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. @Override
  2. public void onPause()
  3. {
  4. super.onPause();
  5.  
  6. // save sheet when user is leaving activity
  7. SaveTask task = new SaveTask(PlayCharacterActivity.this);
  8.  
  9. task.execute();
  10. }
  11.  
  12. private class SaveTask extends AsyncTask <Void, Void, Void> {
  13. private ProgressDialog dialog;
  14.  
  15. public SaveTask(PlayCharacterActivity activity) {
  16.  
  17. }
  18.  
  19. @Override
  20. protected void onPreExecute() {
  21. super.onPreExecute();
  22. dialog = new ProgressDialog(PlayCharacterActivity.this);
  23. dialog.setMessage("Saving...");
  24. dialog.show();
  25. dialog.setIndeterminate(true);
  26. dialog.setCancelable(false);
  27.  
  28. }
  29.  
  30. @Override
  31. protected void onPostExecute(Void result) {
  32. if (dialog.isShowing()) {
  33. dialog.dismiss();
  34. }
  35. super.onPostExecute(result);
  36. }
  37.  
  38. @Override
  39. protected Void doInBackground(Void... params) {
  40. try {
  41. //Log.d("plch:OnPause", "starting save");
  42. Sheet.getInstance().Save();
  43. //Log.d("plch:OnPause", "finished save");
  44. } catch (RuntimeException e) {
  45. e.printStackTrace();
  46. }
  47.  
  48. return null;
  49. }
  50.  
  51. }
  52.  
  53. public void Save()
  54. {
  55. long dtMili = System.currentTimeMillis();
  56. Date d = new Date(dtMili);
  57. CharSequence s = DateFormat.format("hh:mm:ss", d.getTime());
  58.  
  59. // use this flag to know whether to back up the saved file or not
  60. boolean saveSuccessful = false;
  61.  
  62. //_xml = new String("");
  63. dtMili = System.currentTimeMillis();
  64. d = new Date(dtMili);
  65. Log.d("Load/Save", "started serialising: " + DateFormat.format("hh:mm:ss", d.getTime()));
  66. _xml = _instance.Serialise();
  67. dtMili = System.currentTimeMillis();
  68. d = new Date(dtMili);
  69. Log.d("Load/Save", "finished serialising: " + DateFormat.format("hh:mm:ss", d.getTime()));
  70. try
  71. {
  72. //---SD Card Storage---
  73. File sdCard = Environment.getExternalStorageDirectory();
  74. File directory = new File (sdCard.getAbsolutePath() + "/RPGenius");
  75. directory.mkdirs();
  76. File file = new File(directory, _name + ".rpg");
  77. FileOutputStream fOut = new FileOutputStream(file);
  78. Log.d("Saving to: ", file.getAbsolutePath());
  79.  
  80. //---write the string to the file---
  81. OutputStreamWriter osw = new OutputStreamWriter(fOut);
  82. //DebugHelper.DebugMessage("File contents: " + _xml);
  83.  
  84. dtMili = System.currentTimeMillis();
  85. d = new Date(dtMili);
  86. Log.d("Load/Save", "started writing file: " + DateFormat.format("hh:mm:ss", d.getTime()));
  87.  
  88. osw.write(_xml);
  89. osw.flush();
  90. osw.close();
  91.  
  92. dtMili = System.currentTimeMillis();
  93. d = new Date(dtMili);
  94. Log.d("Load/Save", "finished writing file: " + DateFormat.format("hh:mm:ss", d.getTime()));
  95.  
  96. saveSuccessful = true;
  97. }
  98. catch (NullPointerException npe)
  99. {
  100. npe.printStackTrace();
  101. }
  102. catch (IOException ioe)
  103. {
  104. ioe.printStackTrace();
  105. }
  106.  
  107. // if the save was completely successfully, back it up
  108. if (saveSuccessful)
  109. {
  110. File sdCard = Environment.getExternalStorageDirectory();
  111. File directory = new File (sdCard.getAbsolutePath() + "/RPGenius");
  112. File file = new File(directory, _name + ".rpg");
  113.  
  114. if (file.exists())
  115. {
  116. // locate backup directory and create if not present
  117. File backupDirectory = new File (sdCard.getAbsolutePath() + "/RPGenius/Backups");
  118. backupDirectory.mkdirs();
  119.  
  120. // create target file location/name
  121. File backupFile = new File(backupDirectory, _name + ".rpg");
  122.  
  123. // attempt to copy file
  124. try {
  125. FileInputStream inStream = new FileInputStream(file);
  126. FileOutputStream outStream = new FileOutputStream(backupFile);
  127. FileChannel inChannel = inStream.getChannel();
  128. FileChannel outChannel = outStream.getChannel();
  129. inChannel.transferTo(0, inChannel.size(), outChannel);
  130. inStream.close();
  131. outStream.close();
  132. } catch (FileNotFoundException e) {
  133. // TODO Auto-generated catch block
  134. e.printStackTrace();
  135. }
  136. catch (IOException e) {
  137. // TODO Auto-generated catch block
  138. e.printStackTrace();
  139. }
  140.  
  141. }
  142. }
  143. }
  144.  
  145. @Override
  146. public String Serialise() {
  147. // produce xml string to represent this object
  148. _indents = 0;
  149. _xml = new String("");
  150. StartXmlItem("Sheet");
  151. //AddSimpleXmlItem("Name", _name);
  152.  
  153. StartXmlItem("Stats");
  154. for (XmlSerialisable stat: _stats)
  155. {
  156. AddComplexXmlItem(stat);
  157. }
  158. EndXmlItem("Stats");
  159.  
  160. StartXmlItem("Effects");
  161. for (XmlSerialisable effect: _effects)
  162. {
  163. AddComplexXmlItem(effect);
  164. }
  165. EndXmlItem("Effects");
  166.  
  167. StartXmlItem("Pages");
  168. for (XmlSerialisable page: _pages)
  169. {
  170. AddComplexXmlItem(page);
  171. }
  172. EndXmlItem("Pages");
  173.  
  174. EndXmlItem("Sheet");
  175.  
  176. return _xml;
  177. }
  178.  
  179. private ProgressDialog dialog; //should be declared as field in Activity
  180.  
  181. public SaveTask(PlayCharacterActivity activity) { //don't pass the activity to AsyncTask
  182.  
  183. }
  184.  
  185. @Override
  186. protected void onPreExecute() {
  187. showDialog(); //call a method in your Activity that shows your dialog as you want
  188. }
  189.  
  190. dialog = ProgressDialog.show(...)
  191.  
  192. task.execute();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement