diff --git a/.gitignore b/.gitignore index c445568dfe..cd46af12d7 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,5 @@ bundletool.jar .keystore/arcticonskeystore.jks *.eml freedesktop-theme/arcticons* +/app/src/main/assets/contributors/downloaded +app/src/main/res/xml/contributors.xml diff --git a/app/src/main/assets/contributors/face_1.png b/app/src/main/assets/contributors/face_1.png new file mode 100644 index 0000000000..cb6c9d7006 Binary files /dev/null and b/app/src/main/assets/contributors/face_1.png differ diff --git a/app/src/main/assets/contributors/face_2.png b/app/src/main/assets/contributors/face_2.png new file mode 100644 index 0000000000..9e0d63add1 Binary files /dev/null and b/app/src/main/assets/contributors/face_2.png differ diff --git a/app/src/main/assets/contributors/face_3.png b/app/src/main/assets/contributors/face_3.png new file mode 100644 index 0000000000..19a1502f22 Binary files /dev/null and b/app/src/main/assets/contributors/face_3.png differ diff --git a/app/src/main/res/xml/contributors.xml b/generated/contributors.xml similarity index 100% rename from app/src/main/res/xml/contributors.xml rename to generated/contributors.xml diff --git a/preparehelper/build.gradle b/preparehelper/build.gradle index 042a3017c4..f864ce94d6 100644 --- a/preparehelper/build.gradle +++ b/preparehelper/build.gradle @@ -39,7 +39,7 @@ task rundayNight(type: JavaExec) { dependencies { - implementation("com.android.tools:sdk-common:31.1.4") + implementation("com.android.tools:sdk-common:31.5.1") implementation("org.dom4j:dom4j:2.1.4") implementation("commons-io:commons-io:2.15.0") } diff --git a/preparehelper/src/main/java/com/donnnno/arcticons/helper/ContributorImage.java b/preparehelper/src/main/java/com/donnnno/arcticons/helper/ContributorImage.java new file mode 100644 index 0000000000..68e9e83a60 --- /dev/null +++ b/preparehelper/src/main/java/com/donnnno/arcticons/helper/ContributorImage.java @@ -0,0 +1,134 @@ +package com.donnnno.arcticons.helper; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import java.awt.image.BufferedImage; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.Random; + +import javax.imageio.ImageIO; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +public class ContributorImage { + public static void start(String assetsDir, String contributorsXml, String xmlFilePath) throws IOException { + + StringBuilder output = new StringBuilder("\n\n"); + xmlFilePath = xmlFilePath + "/contributors.xml"; + extractImageUrls(output, contributorsXml, assetsDir); + output.append("\n"); + writeOutput(xmlFilePath, output); + } + + + public static void writeOutput(String pathXml, StringBuilder output) throws IOException { + // Write to drawable.xml in res directory with UTF-8 encoding + try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(pathXml), StandardCharsets.UTF_8))) { + writer.write(output.toString()); + } + } + + public static BufferedImage downloadImages(String imageUrl) { + try { + URL url = new URL(imageUrl); + BufferedImage img = ImageIO.read(url); + if (img != null) { + System.out.println("Downloaded image from: " + imageUrl); + return img; + } else { + System.err.println("Failed to download image from: " + imageUrl); + } + } catch (IOException e) { + System.err.println("Failed to download image from: " + imageUrl + " " + e.getMessage()); + } + return null; + } + + public static void saveImage(BufferedImage image, String imageName, String imagePath) throws IOException { + // Create the directory if it doesn't exist + try { + File directory = new File(imagePath + "/" + imageName).getParentFile(); + if (!directory.exists()) { + boolean good = directory.mkdirs(); + if (!good) { + throw new IOException("Failed to create directory: " + directory.getAbsolutePath()); + } + } + } catch (SecurityException e) { + throw new IOException("Failed to create directory: " + e.getMessage(), e); + } + try { + ImageIO.write(image, "png", new File(imagePath + "/" + imageName)); + } catch (IOException e) { + System.out.println("Error occurred: " + e.getMessage()); + } + } + + private static void appendCategory(StringBuilder output, String name, String contribution, String image, String link) { + output.append("\n\t"); + } + + public static String setPlaceholderImage() { + Random random = new Random(); + int randomImage = random.nextInt(1, 3); + return "assets://contributors/face_" + randomImage + ".png"; + + } + + public static void extractImageUrls(StringBuilder output, String contributorsXml, String assetsDir) { + try { + File inputFile = new File(contributorsXml); + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); + Document doc = dBuilder.parse(inputFile); + doc.getDocumentElement().normalize(); + + NodeList nList = doc.getElementsByTagName("contributor"); + + for (int temp = 0; temp < nList.getLength(); temp++) { + Node nNode = nList.item(temp); + if (nNode.getNodeType() == Node.ELEMENT_NODE) { + Element eElement = (Element) nNode; + String name = eElement.getAttribute("name"); + String contribution = eElement.getAttribute("contribution"); + String imageURL = eElement.getAttribute("image"); + String link = eElement.getAttribute("link"); + if (link.startsWith("https://github.com/") && imageURL.isEmpty()) { + imageURL = link + ".png"; + } + if (imageURL.isEmpty()) { + imageURL = setPlaceholderImage(); + appendCategory(output, name, contribution, imageURL, link); + } else if (link.startsWith("assets://")) { + appendCategory(output, name, contribution, imageURL, link); + } else { + BufferedImage image = downloadImages(imageURL); + if (image != null) { + String imageName = "contributors/downloaded/contributor_" + temp + ".png"; + imageURL = "assets://" + imageName; + saveImage(image, imageName, assetsDir); + appendCategory(output, name, contribution, imageURL, link); + } else { + imageURL = setPlaceholderImage(); + appendCategory(output, name, contribution, imageURL, link); + } + } + } + } + } catch (Exception e) { + System.out.println("Error occurred: " + e.getMessage()); + } + } +} diff --git a/preparehelper/src/main/java/com/donnnno/arcticons/helper/Start.java b/preparehelper/src/main/java/com/donnnno/arcticons/helper/Start.java index 74a0d84e65..19d1338f40 100644 --- a/preparehelper/src/main/java/com/donnnno/arcticons/helper/Start.java +++ b/preparehelper/src/main/java/com/donnnno/arcticons/helper/Start.java @@ -12,7 +12,7 @@ public static void main(String[] args) { Path rootPath = Paths.get(rootDir); // Get the name of the root directory String rootDirName = rootPath.getFileName().toString(); - if (rootDirName.equals("preparehelper")){ + if (rootDirName.equals("preparehelper")) { rootDir = ".."; } String sourceDir = rootDir + "/icons/white"; @@ -69,14 +69,21 @@ public static void main(String[] args) { categoryGamesXml = rootDir+"/generated/games.xml"; assetsDir = rootDir + "/app/src/main/assets"; appFilter = rootDir + "/newicons/appfilter.xml"; + String contributorsXml = rootDir + "/generated/contributors.xml"; - try { - XMLCreator.mergeNewDrawables(xmlDir+"/drawable.xml",newXML,categoryGamesXml,assetsDir,sourceDir,xmlDir,appFilter); - System.out.println("XML task completed"); - } catch (Exception e) { - e.printStackTrace(); - } + try { + XMLCreator.mergeNewDrawables(xmlDir+"/drawable.xml",newXML,categoryGamesXml,assetsDir,sourceDir,xmlDir,appFilter); + System.out.println("XML task completed"); + } catch (Exception e) { + System.out.println("Error occurred: " + e.getMessage()); + } + try { + ContributorImage.start(assetsDir, contributorsXml, xmlDir); + System.out.println("Contributor Image task completed"); + } catch (Exception e) { + System.out.println("Error occurred: " + e.getMessage()); + } } } } \ No newline at end of file