From d6b9ae201b6473de680a75079978203cfcc60126 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Str=C3=B8mme?= Date: Sat, 28 Nov 2015 15:15:22 +0100 Subject: Android: Add function to check permissions at run-time. This is convenient when we you want to check which permissions the application has been granted at run-time. On Android device running Android 6.0 or newer the permissions might be changed by the user, so there are cases where this will be needed to determine what APIs are available. On older devices or if the application is built for an older SDK version, the old behavior is used, that is, the permissions from the manifest is the permissions the application has. Change-Id: I17ad588b35b26dd7ab26fa4b749764c1602c6854 Reviewed-by: Eskil Abrahamsen Blomfeldt --- .../src/org/qtproject/qt5/android/QtNative.java | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src/android') diff --git a/src/android/jar/src/org/qtproject/qt5/android/QtNative.java b/src/android/jar/src/org/qtproject/qt5/android/QtNative.java index 1b7ec8abbb..602b25eb45 100644 --- a/src/android/jar/src/org/qtproject/qt5/android/QtNative.java +++ b/src/android/jar/src/org/qtproject/qt5/android/QtNative.java @@ -41,6 +41,7 @@ import java.util.concurrent.Semaphore; import android.app.Activity; import android.content.Context; import android.content.Intent; +import android.content.pm.PackageManager; import android.net.Uri; import android.os.Handler; import android.os.Looper; @@ -53,6 +54,7 @@ import android.view.Menu; import android.view.MotionEvent; import android.view.View; +import java.lang.reflect.Method; import java.security.KeyStore; import java.security.cert.X509Certificate; import java.util.Iterator; @@ -81,6 +83,7 @@ public class QtNative private static int m_oldx, m_oldy; private static final int m_moveThreshold = 0; private static ClipboardManager m_clipboardManager = null; + private static Method m_checkSelfPermissionMethod = null; private static ClassLoader m_classLoader = null; public static ClassLoader classLoader() @@ -393,6 +396,29 @@ public class QtNative } } + public static int checkSelfPermission(final String permission) + { + int perm = PackageManager.PERMISSION_DENIED; + synchronized (m_mainActivityMutex) { + if (m_activity == null) + return perm; + try { + if (Build.VERSION.SDK_INT >= 23) { + if (m_checkSelfPermissionMethod == null) + m_checkSelfPermissionMethod = Context.class.getMethod("checkSelfPermission", String.class); + perm = (Integer)m_checkSelfPermissionMethod.invoke(m_activity, permission); + } else { + final PackageManager pm = m_activity.getPackageManager(); + perm = pm.checkPermission(permission, m_activity.getPackageName()); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + return perm; + } + private static void updateSelection(final int selStart, final int selEnd, final int candidatesStart, -- cgit v1.2.3