Validate icon packs

This commit is contained in:
MrLetsplay 2023-10-01 20:52:20 +02:00
parent 92d8708ada
commit d25799719e
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
5 changed files with 38 additions and 6 deletions

View File

@ -60,7 +60,7 @@ public class MainActivity extends BaseActivity {
private Consumer<Uri> pickBackupFileLoadCallback;
private ActivityResultLauncher<String[]> pickIconPackFileLoad;
private ActivityResultLauncher<String[]> pickIconPackFile;
private ActivityResultLauncher<PickVisualMediaRequest> pickIconImage;
@ -141,12 +141,23 @@ public class MainActivity extends BaseActivity {
}
});
pickIconPackFileLoad = registerForActivityResult(new ActivityResultContracts.OpenDocument(), doc -> {
pickIconPackFile = registerForActivityResult(new ActivityResultContracts.OpenDocument(), doc -> {
lockOnStop = true;
try {
if(doc == null) return;
IconPackMetadata meta = IconUtil.importIconPack(this, doc); // TODO: check if pack contains icons
IconPackMetadata meta = IconUtil.importIconPack(this, doc);
if(!meta.validate()) {
DialogUtil.showErrorDialog(this, getString(R.string.error_icon_pack_invalid));
return;
}
if(meta.getIcons().length == 0) {
DialogUtil.showErrorDialog(this, getString(R.string.error_icon_pack_empty));
return;
}
DialogUtil.showErrorDialog(this, "Icon pack contains " + meta.getIcons().length + " icons");
} catch (IconPackException e) {
DialogUtil.showErrorDialog(this, "Failed to import icon pack", e);
@ -250,7 +261,10 @@ public class MainActivity extends BaseActivity {
if(!(fragment instanceof HomeFragment)) {
NavigationUtil.navigate(this, HomeFragment.class, null);
return;
}
finishAffinity();
}
public void openSettings(MenuItem item) {
@ -392,9 +406,9 @@ public class MainActivity extends BaseActivity {
pickBackupFileLoad.launch(new String[]{"application/json", "*/*"});
}
public void promptPickIconPackLoad() {
public void promptPickIconPackFile() {
this.lockOnStop = false;
pickIconPackFileLoad.launch(new String[]{"application/zip", "*/*"});
pickIconPackFile.launch(new String[]{"application/zip", "*/*"});
}
public void promptPickIconImage(Consumer<Uri> callback) {

View File

@ -261,7 +261,7 @@ public class SettingsFragment extends NamedFragment {
});
});
binding.settingsLoadIconPack.setOnClickListener(v -> ((MainActivity) requireActivity()).promptPickIconPackLoad());
binding.settingsLoadIconPack.setOnClickListener(v -> ((MainActivity) requireActivity()).promptPickIconPackFile());
binding.settingsManageIconPacks.setOnClickListener(v -> {
List<IconPack> packs = IconUtil.loadAllIconPacks(requireContext());

View File

@ -30,4 +30,9 @@ public class IconMetadata {
public String[] getIssuer() {
return issuer;
}
public boolean validate() {
return filename != null && category != null && issuer != null;
}
}

View File

@ -24,4 +24,15 @@ public class IconPackMetadata {
public IconMetadata[] getIcons() {
return icons;
}
public boolean validate() {
if(uuid == null || name == null || icons == null) return false;
for(IconMetadata i : icons) {
if(!i.validate()) return false;
}
return true;
}
}

View File

@ -121,4 +121,6 @@
<string name="changelog_link" translatable="false">https://git.cringe-studios.com/CringeStudios/Code-Guard</string>
<string name="documentation_link" translatable="false">https://git.cringe-studios.com/CringeStudios/Code-Guard</string>
<string name="patreon_link" translatable="false">https://git.cringe-studios.com/CringeStudios/Code-Guard</string>
<string name="error_icon_pack_empty">The icon pack doesn\'t contain any icons</string>
<string name="error_icon_pack_invalid">Pack contains invalid metadata. Make sure you selected the correct file</string>
</resources>