Android: guard getStateCount() with correct VERSION.SDK_INT

The call getStateCount() was introduced in 29, so cases for
lower API should be handled.

Pick-to: 6.2 6.1
Change-Id: I7f58541c0b16fed91835e6f390afa89378a7af3e
Reviewed-by: Ville Voutilainen <ville.voutilainen@qt.io>
bb10
Assam Boudjelthia 2021-08-23 11:11:22 +03:00
parent eec25c7807
commit 760e24e867
1 changed files with 7 additions and 1 deletions

View File

@ -69,6 +69,7 @@ import android.graphics.drawable.RotateDrawable;
import android.graphics.drawable.ScaleDrawable;
import android.graphics.drawable.StateListDrawable;
import android.graphics.drawable.VectorDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
@ -413,7 +414,12 @@ public class ExtractStyle {
try {
StateListDrawable stateList = (StateListDrawable) drawable;
JSONArray array = new JSONArray();
for (int i = 0; i < stateList.getStateCount(); i++) {
final int numStates;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q)
numStates = (Integer) StateListDrawable.class.getMethod("getStateCount").invoke(stateList);
else
numStates = stateList.getStateCount();
for (int i = 0; i < numStates; i++) {
JSONObject stateJson = new JSONObject();
final Drawable d = (Drawable) StateListDrawable.class.getMethod("getStateDrawable", Integer.TYPE).invoke(stateList, i);
final int[] states = (int[]) StateListDrawable.class.getMethod("getStateSet", Integer.TYPE).invoke(stateList, i);