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>
This commit is contained in:
parent
4f1b6749c3
commit
d6b9ae201b
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user