Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contributer image #2190

Merged
merged 11 commits into from
Jul 20, 2024
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Binary file added app/src/main/assets/contributors/face_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/assets/contributors/face_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/src/main/assets/contributors/face_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
2 changes: 1 addition & 1 deletion preparehelper/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Original file line number Diff line number Diff line change
@@ -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("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<resources>\n");
xmlFilePath = xmlFilePath + "/contributors.xml";
extractImageUrls(output, contributorsXml, assetsDir);
output.append("\n</resources>");
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<contributor\n\t\tname=\"").append(name)
.append("\"\n\t\tcontribution=\"").append(contribution)
.append("\"\n\t\timage=\"").append(image)
.append("\"\n\t\tlink=\"").append(link).append("\" />");
}

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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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());
}
}
}
}