fix(android): StringIndexOutOfBoundsException with invalid drawables (#9563)

* fix(android/application): org.nativescript.widgets.Utils::getDrawable

* chore: fix spacing

* fix(android/application): do not load empty path

Co-authored-by: Igor Randjelovic <rigor789@gmail.com>

* test: Add tests for empty image sources

* chore: add a few more test cases

These make the app crash without the fix in place

Co-authored-by: Igor Randjelovic <rigor789@gmail.com>
Co-authored-by: Nathan Walker <walkerrunpdx@gmail.com>
This commit is contained in:
Sébastien JEAN
2021-09-25 09:54:34 +02:00
committed by GitHub
parent db7b02a418
commit 8e76bbe251
3 changed files with 41 additions and 3 deletions

View File

@ -1,5 +1,5 @@
<Page> <Page>
<GridLayout rows="*,*,*,*,*,*" > <GridLayout rows="*,*,*,*,*,*,*,*,*,*,*,*,*" >
<Button backgroundImage="url('~/resources/images/no-image.png')" <Button backgroundImage="url('~/resources/images/no-image.png')"
borderRadius='125' borderWidth='2' borderColor='black' borderRadius='125' borderWidth='2' borderColor='black'
backgroundRepeat="repeat" backgroundSize="contain" backgroundRepeat="repeat" backgroundSize="contain"
@ -25,5 +25,35 @@
backgroundSize="contain" backgroundSize="contain"
height="80" width="180" height="80" width="180"
/> />
<!-- Test some invalid cases - these should not crash -->
<Button row="6" backgroundImage="url('res://theneverfoundunicorn.png')"
borderRadius='10' borderWidth='2' borderColor='black'
height="80" width="180"
/>
<Button row="7" backgroundImage="url('res://')"
borderRadius='10' borderWidth='2' borderColor='black'
height="80" width="180"
/>
<Button row="8" backgroundImage="url('')"
borderRadius='10' borderWidth='2' borderColor='black'
height="80" width="180"
/>
<Button row="9" backgroundImage="res://theneverfoundunicorn.png"
borderRadius='10' borderWidth='2' borderColor='black'
height="80" width="180"
/>
<Button row="10" backgroundImage="res://"
borderRadius='10' borderWidth='2' borderColor='black'
height="80" width="180"
/>
<Button row="11" backgroundImage=" "
borderRadius='10' borderWidth='2' borderColor='black'
height="80" width="180"
/>
<Button row="12" backgroundImage="bad"
borderRadius='10' borderWidth='2' borderColor='black'
height="80" width="180"
/>
</GridLayout> </GridLayout>
</Page> </Page>

View File

@ -12,6 +12,8 @@
<Image src="res://theneverfoundunicorn.png" margin="1" width="30" height="30" backgroundColor="yellow" /> <Image src="res://theneverfoundunicorn.png" margin="1" width="30" height="30" backgroundColor="yellow" />
<Image src="res://testlogo" margin="1" width="30" height="30" backgroundColor="yellow" /> <Image src="res://testlogo" margin="1" width="30" height="30" backgroundColor="yellow" />
<Image src="res://" margin="1" width="30" height="30" backgroundColor="yellow" />
<Image src="null" margin="1" width="30" height="30" backgroundColor="yellow" />
<Image src="res://theneverfoundunicorn.png" margin="1" width="30" height="30" backgroundColor="yellow" borderRadius="10" /> <Image src="res://theneverfoundunicorn.png" margin="1" width="30" height="30" backgroundColor="yellow" borderRadius="10" />
<Image src="res://testlogo" margin="1" width="30" height="30" backgroundColor="yellow" borderRadius="10" /> <Image src="res://testlogo" margin="1" width="30" height="30" backgroundColor="yellow" borderRadius="10" />

View File

@ -36,8 +36,14 @@ import java.util.concurrent.Executors;
public class Utils { public class Utils {
public static Drawable getDrawable(String uri, Context context){ public static Drawable getDrawable(String uri, Context context){
String resPath = uri.substring("res://".length()); int resId = 0;
int resId = context.getResources().getIdentifier(resPath, "drawable", context.getPackageName()); int resPrefixLength = "res://".length();
if (uri.length() > resPrefixLength) {
String resPath = uri.substring(resPrefixLength);
resId = context.getResources().getIdentifier(resPath, "drawable", context.getPackageName());
}
if (resId > 0) { if (resId > 0) {
return AppCompatResources.getDrawable(context, resId); return AppCompatResources.getDrawable(context, resId);
} else { } else {