diff options
Diffstat (limited to 'src/android/jar/src/org/qtproject/qt/android/CursorHandle.java')
-rw-r--r-- | src/android/jar/src/org/qtproject/qt/android/CursorHandle.java | 131 |
1 files changed, 50 insertions, 81 deletions
diff --git a/src/android/jar/src/org/qtproject/qt/android/CursorHandle.java b/src/android/jar/src/org/qtproject/qt/android/CursorHandle.java index 980b172444..7e601c0551 100644 --- a/src/android/jar/src/org/qtproject/qt/android/CursorHandle.java +++ b/src/android/jar/src/org/qtproject/qt/android/CursorHandle.java @@ -1,64 +1,28 @@ -/**************************************************************************** -** -** Copyright (C) 2016 Olivier Goffart <ogoffart@woboq.com> -** Contact: http://www.qt.io/licensing/ -** -** This file is part of the Android port of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 Olivier Goffart <ogoffart@woboq.com> +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only package org.qtproject.qt.android; +import android.annotation.SuppressLint; +import android.app.Activity; import android.content.Context; -import android.os.Bundle; -import android.util.DisplayMetrics; -import android.view.LayoutInflater; -import android.view.View; -import android.widget.LinearLayout; -import android.widget.ImageView; import android.content.res.TypedArray; import android.graphics.drawable.Drawable; -import android.view.MotionEvent; -import android.widget.PopupWindow; -import android.app.Activity; +import android.util.DisplayMetrics; +import android.util.Log; import android.util.TypedValue; +import android.view.MotionEvent; +import android.view.View; import android.view.ViewTreeObserver; +import android.widget.ImageView; +import android.widget.PopupWindow; /* This view represents one of the handle (selection or cursor handle) */ +@SuppressLint("ViewConstructor") class CursorView extends ImageView { - private CursorHandle mHandle; - // The coordinare which where clicked + private final CursorHandle mHandle; + // The coordinate which where clicked private float m_offsetX; private float m_offsetY; private boolean m_pressed = false; @@ -68,7 +32,7 @@ class CursorView extends ImageView mHandle = handle; } - // Called when the handle was moved programatically , with the delta amount in pixels + // Called when the handle was moved programmatically , with the delta amount in pixels public void adjusted(int dx, int dy) { m_offsetX += dx; m_offsetY += dy; @@ -79,7 +43,7 @@ class CursorView extends ImageView switch (ev.getActionMasked()) { case MotionEvent.ACTION_DOWN: { m_offsetX = ev.getRawX(); - m_offsetY = ev.getRawY() + getHeight() / 2; + m_offsetY = ev.getRawY() + (float) getHeight() / 2; m_pressed = true; break; } @@ -88,7 +52,7 @@ class CursorView extends ImageView if (!m_pressed) return false; mHandle.updatePosition(Math.round(ev.getRawX() - m_offsetX), - Math.round(ev.getRawY() - m_offsetY)); + Math.round(ev.getRawY() - m_offsetY)); break; } @@ -99,24 +63,24 @@ class CursorView extends ImageView } return true; } - } // Helper class that manages a cursor or selection handle -public class CursorHandle implements ViewTreeObserver.OnPreDrawListener +class CursorHandle implements ViewTreeObserver.OnPreDrawListener { - private View m_layout = null; + private static final String QtTag = "QtCursorHandle"; + private final View m_layout; private CursorView m_cursorView = null; private PopupWindow m_popup = null; - private int m_id; - private int m_attr; - private Activity m_activity; + private final int m_id; + private final int m_attr; + private final Activity m_activity; private int m_posX = 0; private int m_posY = 0; private int m_lastX; private int m_lastY; int tolerance; - private boolean m_rtl; + private final boolean m_rtl; int m_yShift; public CursorHandle(Activity activity, View layout, int id, int attr, boolean rtl) { @@ -124,35 +88,38 @@ public class CursorHandle implements ViewTreeObserver.OnPreDrawListener m_id = id; m_attr = attr; m_layout = layout; - DisplayMetrics metrics = new DisplayMetrics(); - activity.getWindowManager().getDefaultDisplay().getMetrics(metrics); + DisplayMetrics metrics = activity.getResources().getDisplayMetrics(); m_yShift = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 1f, metrics); tolerance = Math.min(1, (int)(m_yShift / 2f)); m_lastX = m_lastY = -1 - tolerance; m_rtl = rtl; } - private boolean initOverlay(){ - if (m_popup == null){ + private void initOverlay(){ + if (m_popup != null) + return; - Context context = m_layout.getContext(); - int[] attrs = {m_attr}; - TypedArray a = context.getTheme().obtainStyledAttributes(attrs); - Drawable drawable = a.getDrawable(0); + Context context = m_layout.getContext(); + int[] attrs = {m_attr}; + TypedArray a = context.getTheme().obtainStyledAttributes(attrs); + Drawable drawable = a.getDrawable(0); - m_cursorView = new CursorView(context, this); - m_cursorView.setImageDrawable(drawable); + m_cursorView = new CursorView(context, this); + m_cursorView.setImageDrawable(drawable); - m_popup = new PopupWindow(context, null, android.R.attr.textSelectHandleWindowStyle); - m_popup.setSplitTouchEnabled(true); - m_popup.setClippingEnabled(false); - m_popup.setContentView(m_cursorView); + m_popup = new PopupWindow(context, null, android.R.attr.textSelectHandleWindowStyle); + m_popup.setSplitTouchEnabled(true); + m_popup.setClippingEnabled(false); + m_popup.setContentView(m_cursorView); + if (drawable != null) { m_popup.setWidth(drawable.getIntrinsicWidth()); m_popup.setHeight(drawable.getIntrinsicHeight()); - - m_layout.getViewTreeObserver().addOnPreDrawListener(this); + } else { + Log.w(QtTag, "initOverlay(): cannot get width/height for popup " + + "from null drawable for attribute " + m_attr); } - return true; + + m_layout.getViewTreeObserver().addOnPreDrawListener(this); } // Show the handle at a given position (or move it if it is already shown) @@ -162,16 +129,18 @@ public class CursorHandle implements ViewTreeObserver.OnPreDrawListener final int[] layoutLocation = new int[2]; m_layout.getLocationOnScreen(layoutLocation); - // This value is used for handling split screen case + // These values are used for handling split screen case final int[] activityLocation = new int[2]; + final int[] activityLocationInWindow = new int[2]; m_activity.getWindow().getDecorView().getLocationOnScreen(activityLocation); + m_activity.getWindow().getDecorView().getLocationInWindow(activityLocationInWindow); int x2 = x + layoutLocation[0] - activityLocation[0]; - int y2 = y + layoutLocation[1] + m_yShift - activityLocation[1]; + int y2 = y + layoutLocation[1] + m_yShift + (activityLocationInWindow[1] - activityLocation[1]); - if (m_id == QtNative.IdCursorHandle) { + if (m_id == QtInputDelegate.IdCursorHandle) { x2 -= m_popup.getWidth() / 2 ; - } else if ((m_id == QtNative.IdLeftHandle && !m_rtl) || (m_id == QtNative.IdRightHandle && m_rtl)) { + } else if ((m_id == QtInputDelegate.IdLeftHandle && !m_rtl) || (m_id == QtInputDelegate.IdRightHandle && m_rtl)) { x2 -= m_popup.getWidth() * 3 / 4; } else { x2 -= m_popup.getWidth() / 4; @@ -211,7 +180,7 @@ public class CursorHandle implements ViewTreeObserver.OnPreDrawListener public void updatePosition(int x, int y) { y -= m_yShift; if (Math.abs(m_lastX - x) > tolerance || Math.abs(m_lastY - y) > tolerance) { - QtNative.handleLocationChanged(m_id, x + m_posX, y + m_posY); + QtInputDelegate.handleLocationChanged(m_id, x + m_posX, y + m_posY); m_lastX = x; m_lastY = y; } |