Guest User

Untitled

a guest
Nov 28th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. //Method025: Imports user acc settings from a file on a specified path.
  2. public void importFile() {
  3. //The file variable to be imported.
  4. File file;
  5.  
  6. try {
  7. //Used to access settings.
  8. TinyDB database = new TinyDB(getApplicationContext());
  9.  
  10. //Sets the file equal to the file found at the specified path.
  11. String strfilePath = database.getString("FilePath");
  12. file = new File(strfilePath);
  13.  
  14. //To be used to arrange the imported information.
  15. ArrayList<String> strAcc = new ArrayList<>();
  16. ArrayList<String> strUser = new ArrayList<>();
  17. ArrayList<String> strPass = new ArrayList<>();
  18. ArrayList<String> strAdditionalInfo = new ArrayList<>();
  19.  
  20. //To be used to store all the information for additional info variables. This is
  21. //due to its multi-line nature requiring a slightly different method of
  22. //importation, the other variables are expected to be one line.
  23. String strExtraInfo = "";
  24.  
  25. //Goes through the file and adds info to arrays for each corresponding variable.
  26. //If the line does not have an identifier, it assumes it to be an additional
  27. //info line, and will be processed later.
  28. try (BufferedReader br = new BufferedReader(new FileReader(file))) {
  29. String line;
  30.  
  31. String strLine = br.readLine();
  32. //Decodes the line from Base64 and converts it to a string.
  33. byte[] decodedContent = Base64.decode(strLine.getBytes(), Base64.DEFAULT);
  34. strLine = decodedContent.toString();
  35.  
  36. while ((line = br.readLine()) != null) {
  37. if (strLine.contains("[Acc]")) {
  38. strLine = strLine.replace("[Acc]","");
  39. strAcc.add(strLine);
  40. } else if (strLine.contains("[User]")) {
  41. strLine = strLine.replace("[User]", "");
  42. strUser.add(strLine);
  43. } else if (strLine.contains("[Pass]")) {
  44. strLine = strLine.replace("[Pass]", "");
  45. strPass.add(strLine);
  46. } else {
  47. strExtraInfo += strLine;
  48. }
  49. }
  50. }
  51.  
  52. //Gets the list of accounts.
  53. ArrayList<String> savedInfo = new ArrayList<>(database.getListString("allSaved"));
  54.  
  55. //To be used to get the AdditionalInfo variables one line at a time.
  56. String strSubInfo;
  57.  
  58. //Gets rid of any erroneous spaces.
  59. while (strExtraInfo.contains(" ")) {
  60. strExtraInfo = strExtraInfo.replace(" ", " ");
  61. }
  62.  
  63. //Uses the number of account names as the number of additional info vars to be
  64. //expected. For each iteration, it uses the identifiers to get the substring
  65. //that corresponds to one variable. Stores it, then runs again.
  66. for (String strName : strAcc) {
  67.  
  68. //Used to store positions.
  69. Integer endPos;
  70. Integer startPos;
  71.  
  72. //Finds the start and end of the first var in the overall string, then
  73. //puts it into a substring.
  74. endPos = strExtraInfo.indexOf("[ExtraStart]");
  75. startPos = strExtraInfo.indexOf("[ExtraEnd]");
  76. strSubInfo = strExtraInfo.substring(endPos, startPos);
  77.  
  78. //Removes the substring, so that it wont be added twice. This will only
  79. //remove the specific occurrence, as no two can have the same acc name.
  80. //Thus, no two vars will be exactly the same.
  81. strExtraInfo = strExtraInfo.replace(strSubInfo, "");
  82.  
  83. //Gets rid of the identifiers, then adds the variable to the array of
  84. //additional info variables.
  85. strSubInfo = strSubInfo.replace("[ExtraStart]", "");
  86. strSubInfo = strSubInfo.replace("[ExtraEnd]", "");
  87. strAdditionalInfo.add(strSubInfo);
  88.  
  89. }
  90.  
  91. //Arranges the information.
  92. for (String name : strAcc) {
  93. savedInfo.add(name);
  94.  
  95. ArrayList<String> allInfo = new ArrayList<>();
  96.  
  97. //Gets the info then adds it to database.
  98. allInfo.add(strUser.get(0));
  99. allInfo.add(strPass.get(0));
  100. allInfo.add(strAdditionalInfo.get(0));
  101. database.putListString(name,allInfo);
  102.  
  103. //Deletes the old information.
  104. strUser.remove(0);
  105. strPass.remove(0);
  106. strAdditionalInfo.remove(0);
  107. }
  108.  
  109. //Adds all the new account names to the database.
  110. database.putListString("allSaved", savedInfo);
  111.  
  112. //Lets the user know the process is finished.
  113. Toast.makeText(MainActivity.this, "The backup has been restored.", Toast.LENGTH_SHORT).show();
  114.  
  115. } catch (IOException e) {
  116. Toast.makeText(MainActivity.this, "Sorry, there's been an error.", Toast.LENGTH_SHORT).show();
  117. e.printStackTrace();
  118. }
  119. }
Add Comment
Please, Sign In to add comment