diff options
author | Christian Strømme <christian.stromme@theqtcompany.com> | 2015-11-28 15:15:22 +0100 |
---|---|---|
committer | Christian Stromme <christian.stromme@theqtcompany.com> | 2015-12-02 17:59:34 +0000 |
commit | d6b9ae201b6473de680a75079978203cfcc60126 (patch) | |
tree | 02ff88c61d55ef40b6a353ea8118f73997a670fb | |
parent | 4f1b6749c3f07bfa44b92f8261a03d650f54166b (diff) |
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 <eskil.abrahamsen-blomfeldt@theqtcompany.com>
-rw-r--r-- | src/android/jar/src/org/qtproject/qt5/android/QtNative.java | 26 |
1 files changed, 26 insertions, 0 deletions
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, |