Advertisement
Guest User

shortcuts.java

a guest
Nov 22nd, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.59 KB | None | 0 0
  1. /*
  2.  
  3. * Copyright (C) 2014 Arpit Khurana <arpitkh96@gmail.com>, Vishal Nehra <vishalmeham2@gmail.com>
  4.  
  5. *
  6.  
  7. * This file is part of Amaze File Manager.
  8.  
  9. *
  10.  
  11. * Amaze File Manager is free software: you can redistribute it and/or modify
  12.  
  13. * it under the terms of the GNU General Public License as published by
  14.  
  15. * the Free Software Foundation, either version 3 of the License, or
  16.  
  17. * (at your option) any later version.
  18.  
  19. *
  20.  
  21. * This program is distributed in the hope that it will be useful,
  22.  
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24.  
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26.  
  27. * GNU General Public License for more details.
  28.  
  29. *
  30.  
  31. * You should have received a copy of the GNU General Public License
  32.  
  33. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  34.  
  35. */
  36.  
  37.  
  38. package com.amaze.filemanager.utils;
  39.  
  40.  
  41. import android.app.Activity;
  42.  
  43. import android.content.Context;
  44.  
  45. import android.os.Environment;
  46.  
  47. import android.widget.Toast;
  48.  
  49.  
  50. import org.w3c.dom.Document;
  51.  
  52. import org.w3c.dom.Element;
  53.  
  54. import org.w3c.dom.Node;
  55.  
  56. import org.w3c.dom.NodeList;
  57.  
  58. import org.xml.sax.SAXException;
  59.  
  60.  
  61. import java.io.File;
  62.  
  63. import java.io.FileNotFoundException;
  64.  
  65. import java.io.IOException;
  66.  
  67. import java.util.ArrayList;
  68.  
  69.  
  70. import javax.xml.parsers.DocumentBuilder;
  71.  
  72. import javax.xml.parsers.DocumentBuilderFactory;
  73.  
  74. import javax.xml.parsers.ParserConfigurationException;
  75.  
  76. import javax.xml.transform.Transformer;
  77.  
  78. import javax.xml.transform.TransformerException;
  79.  
  80. import javax.xml.transform.TransformerFactory;
  81.  
  82. import javax.xml.transform.dom.DOMSource;
  83.  
  84. import javax.xml.transform.stream.StreamResult;
  85.  
  86.  
  87.  
  88. public class Shortcuts {
  89. Context context;
  90.     public Shortcuts(Context context) {
  91. this.context=context;
  92.     }
  93.  
  94.  
  95.     public void makeS() throws ParserConfigurationException, TransformerException {
  96.  
  97.         String sd = Environment.getExternalStorageDirectory() + "/";
  98.  
  99.         String[] a = new String[]{sd + Environment.DIRECTORY_DCIM, sd + Environment.DIRECTORY_DOWNLOADS, sd + Environment.DIRECTORY_MOVIES, sd + Environment.DIRECTORY_MUSIC, sd + Environment.DIRECTORY_PICTURES};
  100.  
  101.         File g = new File(context.getFilesDir()+"/shortcut.xml");
  102.  
  103.         if (!g.exists()) {
  104.  
  105.             DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
  106.  
  107.             DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
  108.  
  109.  
  110. // root elements
  111.  
  112.             Document doc = docBuilder.newDocument();
  113.  
  114.             Element rootElement = doc.createElement("shortcut");
  115.  
  116.             doc.appendChild(rootElement);
  117.  
  118.             for (int i = 0; i < a.length; i++) {
  119.  
  120.                 Element staff = doc.createElement("path");
  121.  
  122.                 staff.appendChild(doc.createTextNode(a[i]));
  123.  
  124.                 rootElement.appendChild(staff);
  125.  
  126.             }
  127.  
  128.             TransformerFactory transformerFactory = TransformerFactory.newInstance();
  129.  
  130.             Transformer transformer = transformerFactory.newTransformer();
  131.  
  132.             DOMSource source = new DOMSource(doc);
  133.  
  134.             StreamResult result = new StreamResult(g);
  135.  
  136.             transformer.transform(source, result);
  137.  
  138.         }
  139.  
  140.     }
  141.  
  142.  
  143.     public void addS(File f) throws Exception {
  144.  
  145.         File g = new File( context.getFilesDir()+"/shortcut.xml" );
  146.  
  147.         DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
  148.  
  149.         DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
  150.  
  151.  
  152. // root elements
  153.  
  154.         Document doc = docBuilder.parse(g);
  155.  
  156.         NodeList nList = doc.getElementsByTagName("shortcut");
  157.  
  158.         Node nNode = nList.item(0);
  159.  
  160.         Element eElement = (Element) nNode;
  161.  
  162.  
  163.  
  164. // f elements
  165.  
  166.         Element staff = doc.createElement("path");
  167.  
  168.         staff.appendChild(doc.createTextNode(f.getPath()));
  169.  
  170.         eElement.appendChild(staff);
  171.  
  172.  
  173.  
  174.         TransformerFactory transformerFactory = TransformerFactory.newInstance();
  175.  
  176.         Transformer transformer = transformerFactory.newTransformer();
  177.  
  178.         DOMSource source = new DOMSource(doc);
  179.  
  180.         StreamResult result = new StreamResult(new File( context.getFilesDir()+"/shortcut.xml" ));
  181.  
  182.         transformer.transform(source, result);
  183.  
  184.  
  185.     }
  186.  
  187.  
  188.     public ArrayList<File> readS() throws FileNotFoundException, IOException, SAXException, ParserConfigurationException {
  189.  
  190.         ArrayList<File> f = new ArrayList<File>();
  191.  
  192.         File g = new File( context.getFilesDir()+"/shortcut.xml" );
  193.  
  194.         DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
  195.  
  196.         DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
  197.  
  198.  
  199. // root elements
  200.  
  201.         Document doc = docBuilder.parse(g);
  202.  
  203.         NodeList nList = doc.getElementsByTagName("path");
  204.  
  205.         for (int temp = 0; temp < nList.getLength(); temp++) {
  206.  
  207.             Node nNode = nList.item(temp);
  208.  
  209.             if (nNode.getNodeType() == Node.ELEMENT_NODE) {
  210.  
  211.  
  212.                 f.add(new File(nNode.getTextContent()));
  213.  
  214.             }
  215.  
  216.         }
  217.  
  218.         return f;
  219.  
  220.     }
  221.  
  222.  
  223.     public boolean isShortcut(File f) throws IOException, ParserConfigurationException, SAXException {
  224.  
  225.         boolean b = false;
  226.  
  227.         ArrayList<File> x = readS();
  228.  
  229.         for (int i = 0; i < x.size(); i++) {
  230.  
  231.             if (x.get(i) == f) {
  232.  
  233.                 b = true;
  234.  
  235.             } else {
  236.  
  237.                 b = false;
  238.  
  239.             }
  240.  
  241.         }
  242.  
  243.         return b;
  244.  
  245.     }
  246.  
  247.  
  248.     public void removeS(File f1, Activity s) throws IOException, SAXException, ParserConfigurationException, TransformerException {
  249.  
  250.         //  ArrayList<File> f=new ArrayList<File>();
  251.  
  252.         File g = new File( context.getFilesDir()+"/shortcut.xml" );
  253.  
  254.         DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
  255.  
  256.         DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
  257.  
  258.  
  259. // root elements
  260.  
  261.         Document doc = docBuilder.parse(g);
  262.  
  263.         NodeList List = doc.getElementsByTagName("shortcut");
  264.  
  265.         Node n = List.item(0);
  266.  
  267.  
  268.         NodeList nList = n.getChildNodes();
  269.  
  270.  
  271.         for (int temp = 0; temp < nList.getLength(); temp++) {
  272.  
  273.             Node nNode = nList.item(temp);
  274.  
  275.  
  276.             if (nNode.getTextContent().equals(f1.getPath())) {
  277.  
  278.                 //Element e=(Element)nNode;
  279.  
  280.                 n.removeChild(nNode);
  281.  
  282.  
  283.             }
  284.  
  285.         }
  286.  
  287.         TransformerFactory transformerFactory = TransformerFactory.newInstance();
  288.  
  289.         Transformer transformer = transformerFactory.newTransformer();
  290.  
  291.         DOMSource source = new DOMSource(doc);
  292.  
  293.         StreamResult result = new StreamResult(new File( context.getFilesDir()+"/shortcut.xml" ));
  294.  
  295.         transformer.transform(source, result);
  296.  
  297.         Toast.makeText(s, "Successful", Toast.LENGTH_LONG).show();
  298.  
  299.     }
  300.  
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement