Android: guard getStateCount() with correct VERSION.SDK_INT

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

Change-Id: I7f58541c0b16fed91835e6f390afa89378a7af3e
Reviewed-by: Ville Voutilainen <ville.voutilainen@qt.io>
(cherry picked from commit 760e24e8676d52e2851ef8556770194a97eae831)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Assam Boudjelthia 2021-08-23 11:11:22 +03:00 committed by Qt Cherry-pick Bot
parent 09cae81bf7
commit 538c7855e7

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);