Use Android's View.generateViewId() for generating ids for views/surfaces

Qt Android was using consecutive numbers starting from 1 as ids for View.setId(int). The ids are used internally with an assumption that they are unique. It was potentially leading to collisions and unexpected behavior when adding custom views with id generated by View.generateViewId().

Task-number: QTBUG-98649
Pick-to: 5.15 6.2
Change-Id: I5bf2fe1d196c7adafeec544d8d945ebd82ba5cb6
Reviewed-by: Rami Potinkara <rami.potinkara@qt.io>
Reviewed-by: Ville Voutilainen <ville.voutilainen@qt.io>
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
This commit is contained in:
Michał Cieślak 2021-11-25 18:33:01 +01:00
parent 24e4370414
commit 02955a10fa

View File

@ -105,7 +105,6 @@ static sem_t m_exitSemaphore, m_terminateSemaphore;
QHash<int, AndroidSurfaceClient *> m_surfaces;
static QBasicMutex m_surfacesMutex;
static int m_surfaceId = 1;
static QAndroidPlatformIntegration *m_androidPlatformIntegration = nullptr;
@ -325,6 +324,11 @@ namespace QtAndroid
return manufacturer + QLatin1Char(' ') + model;
}
jint generateViewId()
{
return QJniObject::callStaticMethod<jint>("android/view/View", "generateViewId", "()I");
}
int createSurface(AndroidSurfaceClient *client, const QRect &geometry, bool onTop, int imageDepth)
{
QJniEnvironment env;
@ -332,7 +336,7 @@ namespace QtAndroid
return -1;
m_surfacesMutex.lock();
int surfaceId = m_surfaceId++;
jint surfaceId = generateViewId();
m_surfaces[surfaceId] = client;
m_surfacesMutex.unlock();
@ -355,7 +359,7 @@ namespace QtAndroid
int insertNativeView(jobject view, const QRect &geometry)
{
m_surfacesMutex.lock();
const int surfaceId = m_surfaceId++;
jint surfaceId = generateViewId();
m_surfaces[surfaceId] = nullptr; // dummy
m_surfacesMutex.unlock();