Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Method025: Imports user acc settings from a file on a specified path.
- public void importFile() {
- //The file variable to be imported.
- File file;
- try {
- //Used to access settings.
- TinyDB database = new TinyDB(getApplicationContext());
- //Sets the file equal to the file found at the specified path.
- String strfilePath = database.getString("FilePath");
- file = new File(strfilePath);
- //To be used to arrange the imported information.
- ArrayList<String> strAcc = new ArrayList<>();
- ArrayList<String> strUser = new ArrayList<>();
- ArrayList<String> strPass = new ArrayList<>();
- ArrayList<String> strAdditionalInfo = new ArrayList<>();
- //To be used to store all the information for additional info variables. This is
- //due to its multi-line nature requiring a slightly different method of
- //importation, the other variables are expected to be one line.
- String strExtraInfo = "";
- //Goes through the file and adds info to arrays for each corresponding variable.
- //If the line does not have an identifier, it assumes it to be an additional
- //info line, and will be processed later.
- try (BufferedReader br = new BufferedReader(new FileReader(file))) {
- String line;
- String strLine = br.readLine();
- //Decodes the line from Base64 and converts it to a string.
- byte[] decodedContent = Base64.decode(strLine.getBytes(), Base64.DEFAULT);
- strLine = decodedContent.toString();
- while ((line = br.readLine()) != null) {
- if (strLine.contains("[Acc]")) {
- strLine = strLine.replace("[Acc]","");
- strAcc.add(strLine);
- } else if (strLine.contains("[User]")) {
- strLine = strLine.replace("[User]", "");
- strUser.add(strLine);
- } else if (strLine.contains("[Pass]")) {
- strLine = strLine.replace("[Pass]", "");
- strPass.add(strLine);
- } else {
- strExtraInfo += strLine;
- }
- }
- }
- //Gets the list of accounts.
- ArrayList<String> savedInfo = new ArrayList<>(database.getListString("allSaved"));
- //To be used to get the AdditionalInfo variables one line at a time.
- String strSubInfo;
- //Gets rid of any erroneous spaces.
- while (strExtraInfo.contains(" ")) {
- strExtraInfo = strExtraInfo.replace(" ", " ");
- }
- //Uses the number of account names as the number of additional info vars to be
- //expected. For each iteration, it uses the identifiers to get the substring
- //that corresponds to one variable. Stores it, then runs again.
- for (String strName : strAcc) {
- //Used to store positions.
- Integer endPos;
- Integer startPos;
- //Finds the start and end of the first var in the overall string, then
- //puts it into a substring.
- endPos = strExtraInfo.indexOf("[ExtraStart]");
- startPos = strExtraInfo.indexOf("[ExtraEnd]");
- strSubInfo = strExtraInfo.substring(endPos, startPos);
- //Removes the substring, so that it wont be added twice. This will only
- //remove the specific occurrence, as no two can have the same acc name.
- //Thus, no two vars will be exactly the same.
- strExtraInfo = strExtraInfo.replace(strSubInfo, "");
- //Gets rid of the identifiers, then adds the variable to the array of
- //additional info variables.
- strSubInfo = strSubInfo.replace("[ExtraStart]", "");
- strSubInfo = strSubInfo.replace("[ExtraEnd]", "");
- strAdditionalInfo.add(strSubInfo);
- }
- //Arranges the information.
- for (String name : strAcc) {
- savedInfo.add(name);
- ArrayList<String> allInfo = new ArrayList<>();
- //Gets the info then adds it to database.
- allInfo.add(strUser.get(0));
- allInfo.add(strPass.get(0));
- allInfo.add(strAdditionalInfo.get(0));
- database.putListString(name,allInfo);
- //Deletes the old information.
- strUser.remove(0);
- strPass.remove(0);
- strAdditionalInfo.remove(0);
- }
- //Adds all the new account names to the database.
- database.putListString("allSaved", savedInfo);
- //Lets the user know the process is finished.
- Toast.makeText(MainActivity.this, "The backup has been restored.", Toast.LENGTH_SHORT).show();
- } catch (IOException e) {
- Toast.makeText(MainActivity.this, "Sorry, there's been an error.", Toast.LENGTH_SHORT).show();
- e.printStackTrace();
- }
- }
Add Comment
Please, Sign In to add comment