forked from Sketchware-Pro/Sketchware-Pro
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Move heavy tasks to its own classes
- Loading branch information
Showing
4 changed files
with
194 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
app/src/main/java/dev/aldi/sayuti/editor/manage/LocalLibrariesUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package dev.aldi.sayuti.editor.manage; | ||
|
||
import static pro.sketchware.utility.FileUtil.deleteFile; | ||
import static pro.sketchware.utility.FileUtil.getExternalStorageDir; | ||
import static pro.sketchware.utility.FileUtil.isExistFile; | ||
import static pro.sketchware.utility.FileUtil.listDirAsFile; | ||
import static pro.sketchware.utility.FileUtil.readFile; | ||
import static pro.sketchware.utility.FileUtil.writeFile; | ||
|
||
import com.google.gson.Gson; | ||
|
||
import mod.hey.studios.util.Helper; | ||
|
||
import java.io.File; | ||
import java.util.ArrayList; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.HashMap; | ||
|
||
public class LocalLibrariesUtil { | ||
private static String localLibsPath = getExternalStorageDir().concat("/.sketchware/libs/local_libs/"); | ||
|
||
public static List<LocalLibrary> getAllLocalLibraries() { | ||
ArrayList<File> localLibraryFiles = new ArrayList<>(); | ||
listDirAsFile(localLibsPath, localLibraryFiles); | ||
localLibraryFiles.sort(new LocalLibrariesComparator()); | ||
|
||
List<LocalLibrary> localLibraries = new LinkedList<>(); | ||
for (File libraryFile : localLibraryFiles) { | ||
if (libraryFile.isDirectory()) { | ||
localLibraries.add(LocalLibrary.fromFile(libraryFile)); | ||
} | ||
} | ||
|
||
return localLibraries; | ||
} | ||
|
||
public static ArrayList<HashMap<String, Object>> getLocalLibraries(String scId) { | ||
File localLibFile = getLocalLibFile(scId); | ||
String fileContent; | ||
if (!localLibFile.exists() || (fileContent = readFile(localLibFile.getAbsolutePath())).isEmpty()) { | ||
writeFile(localLibFile.getAbsolutePath(), "[]"); | ||
return new ArrayList<>(); | ||
} | ||
return new Gson().fromJson(fileContent, Helper.TYPE_MAP_LIST); | ||
} | ||
|
||
public static void deleteSelectedLocalLibraries(List<LocalLibrary> localLibraries) { | ||
for (LocalLibrary library : localLibraries) { | ||
if (library.isSelected()) { | ||
deleteFile(localLibsPath.concat(library.getName())); | ||
} | ||
} | ||
} | ||
|
||
public static File getLocalLibFile(String scId) { | ||
return new File(getExternalStorageDir().concat("/.sketchware/data/").concat(scId.concat("/local_library"))); | ||
} | ||
|
||
public static void rewriteLocalLibFile(String scId, String newContent) { | ||
writeFile(getLocalLibFile(scId).getAbsolutePath(), newContent); | ||
} | ||
|
||
public static HashMap<String, Object> createLibraryMap(String name, String dependency) { | ||
String configPath = localLibsPath + name + "/config"; | ||
String resPath = localLibsPath + name + "/res"; | ||
String jarPath = localLibsPath + name + "/classes.jar"; | ||
String dexPath = localLibsPath + name + "/classes.dex"; | ||
String manifestPath = localLibsPath + name + "/AndroidManifest.xml"; | ||
String pgRulesPath = localLibsPath + name + "/proguard.txt"; | ||
String assetsPath = localLibsPath + name + "/assets"; | ||
|
||
HashMap<String, Object> localLibrary = new HashMap<>(); | ||
localLibrary.put("name", name); | ||
if (dependency != null) { | ||
localLibrary.put("dependency", dependency); | ||
} | ||
if (isExistFile(configPath)) { | ||
localLibrary.put("packageName", readFile(configPath)); | ||
} | ||
if (isExistFile(resPath)) { | ||
localLibrary.put("resPath", resPath); | ||
} | ||
if (isExistFile(jarPath)) { | ||
localLibrary.put("jarPath", jarPath); | ||
} | ||
if (isExistFile(dexPath)) { | ||
localLibrary.put("dexPath", dexPath); | ||
} | ||
if (isExistFile(manifestPath)) { | ||
localLibrary.put("manifestPath", manifestPath); | ||
} | ||
if (isExistFile(pgRulesPath)) { | ||
localLibrary.put("pgRulesPath", pgRulesPath); | ||
} | ||
if (isExistFile(assetsPath)) { | ||
localLibrary.put("assetsPath", assetsPath); | ||
} | ||
return localLibrary; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
app/src/main/java/dev/aldi/sayuti/editor/manage/LocalLibrary.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package dev.aldi.sayuti.editor.manage; | ||
|
||
import static pro.sketchware.utility.FileUtil.formatFileSize; | ||
import static pro.sketchware.utility.FileUtil.getFileSize; | ||
|
||
import java.io.File; | ||
|
||
public class LocalLibrary { | ||
private final String name; | ||
private final String size; | ||
private boolean isSelected; | ||
|
||
private LocalLibrary(String name, String size) { | ||
this.name = name; | ||
this.size = size; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getSize() { | ||
return size; | ||
} | ||
|
||
public boolean isSelected() { | ||
return isSelected; | ||
} | ||
|
||
public void setSelected(boolean isSelected) { | ||
this.isSelected = isSelected; | ||
} | ||
|
||
public static LocalLibrary fromFile(File file) { | ||
return new LocalLibrary(file.getName(), formatFileSize(getFileSize(file))); | ||
} | ||
} |
Oops, something went wrong.