ZipXmlThemeResourceProvider: add method to just scan zip for xml themes (#807)
This commit is contained in:
parent
22ed9653ec
commit
ddf94ae2ca
@ -238,10 +238,9 @@ public class MapsforgeActivity extends MapActivity {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Uri uri = data.getData();
|
final Uri uri = data.getData();
|
||||||
|
|
||||||
final ZipXmlThemeResourceProvider resourceProvider = new ZipXmlThemeResourceProvider(new ZipInputStream(new BufferedInputStream(getContentResolver().openInputStream(uri))));
|
final List<String> xmlThemes = ZipXmlThemeResourceProvider.scanXmlThemes(new ZipInputStream(new BufferedInputStream(getContentResolver().openInputStream(uri))));
|
||||||
final List<String> xmlThemes = resourceProvider.getXmlThemes();
|
|
||||||
if (xmlThemes.isEmpty())
|
if (xmlThemes.isEmpty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@ -250,13 +249,17 @@ public class MapsforgeActivity extends MapActivity {
|
|||||||
builder.setSingleChoiceItems(xmlThemes.toArray(new String[0]), -1, new DialogInterface.OnClickListener() {
|
builder.setSingleChoiceItems(xmlThemes.toArray(new String[0]), -1, new DialogInterface.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(DialogInterface dialog, int which) {
|
public void onClick(DialogInterface dialog, int which) {
|
||||||
|
try {
|
||||||
dialog.dismiss();
|
dialog.dismiss();
|
||||||
ThemeFile theme = new ZipRenderTheme(xmlThemes.get(which), resourceProvider);
|
ThemeFile theme = new ZipRenderTheme(xmlThemes.get(which), new ZipXmlThemeResourceProvider(new ZipInputStream(new BufferedInputStream(getContentResolver().openInputStream(uri)))));
|
||||||
if (mTheme != null)
|
if (mTheme != null)
|
||||||
mTheme.dispose();
|
mTheme.dispose();
|
||||||
mTheme = mMap.setTheme(theme);
|
mTheme = mMap.setTheme(theme);
|
||||||
mapsforgeTheme(mTheme);
|
mapsforgeTheme(mTheme);
|
||||||
mMenu.findItem(R.id.theme_external_archive).setChecked(true);
|
mMenu.findItem(R.id.theme_external_archive).setChecked(true);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
builder.show();
|
builder.show();
|
||||||
|
@ -83,4 +83,18 @@ public class ZipXmlThemeResourceProviderTest {
|
|||||||
public void openEmpty() throws IOException {
|
public void openEmpty() throws IOException {
|
||||||
Assert.assertTrue(new ZipXmlThemeResourceProvider(null).getXmlThemes().isEmpty());
|
Assert.assertTrue(new ZipXmlThemeResourceProvider(null).getXmlThemes().isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void scanZipForXmlThemes() throws IOException {
|
||||||
|
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(ZipXmlThemeResourceProviderTest.class.getResourceAsStream("/xmlthemetest.zip")));
|
||||||
|
Assert.assertNotNull(zis);
|
||||||
|
|
||||||
|
List<String> xmlThemes = ZipXmlThemeResourceProvider.scanXmlThemes(zis);
|
||||||
|
|
||||||
|
Assert.assertEquals(4, xmlThemes.size());
|
||||||
|
Assert.assertTrue(xmlThemes.contains("one.xml"));
|
||||||
|
Assert.assertTrue(xmlThemes.contains("two.xml"));
|
||||||
|
Assert.assertTrue(xmlThemes.contains("res/three.xml"));
|
||||||
|
Assert.assertTrue(xmlThemes.contains("res/sub/four.xml"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -60,12 +60,9 @@ public class ZipXmlThemeResourceProvider implements XmlThemeResourceProvider {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
byte[] entry = streamToBytes(zipInputStream, (int) zipEntry.getSize());
|
byte[] entry = streamToBytes(zipInputStream, (int) zipEntry.getSize());
|
||||||
String fileName = zipEntry.getName();
|
String fileName = zipEntryName(zipEntry.getName());
|
||||||
if (fileName.startsWith("/")) {
|
|
||||||
fileName = fileName.substring(1);
|
|
||||||
}
|
|
||||||
files.put(fileName, entry);
|
files.put(fileName, entry);
|
||||||
if (fileName.toLowerCase(Locale.ROOT).endsWith(".xml")) {
|
if (isXmlTheme(fileName)) {
|
||||||
xmlThemes.add(fileName);
|
xmlThemes.add(fileName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -112,6 +109,45 @@ public class ZipXmlThemeResourceProvider implements XmlThemeResourceProvider {
|
|||||||
return xmlThemes;
|
return xmlThemes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean isXmlTheme(String fileName) {
|
||||||
|
return fileName.toLowerCase(Locale.ROOT).endsWith(".xml");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scans a given zip stream for contained xml themes without actually reading and storing its content in memory.
|
||||||
|
* <p>
|
||||||
|
* This method is useful to find out which xml themes are available across multiple zip files
|
||||||
|
* without actually have to read them all into memory.
|
||||||
|
*
|
||||||
|
* @param zipInputStream zip stream to read resources from
|
||||||
|
* @return the XML theme paths in the archive
|
||||||
|
* @throws IOException if a problem occurs reading the stream
|
||||||
|
*/
|
||||||
|
public static List<String> scanXmlThemes(ZipInputStream zipInputStream) throws IOException {
|
||||||
|
if (zipInputStream == null) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
|
List<String> xmlThemes = new ArrayList<>();
|
||||||
|
|
||||||
|
try {
|
||||||
|
ZipEntry zipEntry;
|
||||||
|
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
|
||||||
|
if (zipEntry.isDirectory()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String fileName = zipEntryName(zipEntry.getName());
|
||||||
|
if (isXmlTheme(fileName)) {
|
||||||
|
xmlThemes.add(fileName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
IOUtils.closeQuietly(zipInputStream);
|
||||||
|
}
|
||||||
|
|
||||||
|
return xmlThemes;
|
||||||
|
}
|
||||||
|
|
||||||
private static byte[] streamToBytes(InputStream in, int size) throws IOException {
|
private static byte[] streamToBytes(InputStream in, int size) throws IOException {
|
||||||
byte[] bytes = new byte[size];
|
byte[] bytes = new byte[size];
|
||||||
int count, offset = 0;
|
int count, offset = 0;
|
||||||
@ -121,4 +157,11 @@ public class ZipXmlThemeResourceProvider implements XmlThemeResourceProvider {
|
|||||||
}
|
}
|
||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String zipEntryName(String name) {
|
||||||
|
if (name.startsWith("/")) {
|
||||||
|
return name.substring(1);
|
||||||
|
}
|
||||||
|
return name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user