Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
hotchemi authored and shiraji committed Jan 22, 2016
1 parent 16d5d29 commit 7c0836b
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
Expand Down Expand Up @@ -51,7 +52,6 @@ public static boolean verifyPermissions(int... grantResults) {
private static boolean permissionExists(String permission) {
// Check if the permission could potentially be missing on this device
Integer minVersion = MIN_SDK_PERMISSIONS.get(permission);

// If null was returned from the above call, there is no need for a device API level check for the permission;
// otherwise, we check if its minimum API level requirement is met
return minVersion == null || Build.VERSION.SDK_INT >= minVersion;
Expand Down Expand Up @@ -89,4 +89,20 @@ public static boolean shouldShowRequestPermissionRationale(Activity activity, St
return false;
}

/**
* Get target sdk version from context.
*
* @param context context
* @return target sdk version
*/
public static int getTargetSdkVersion(Context context) {
try {
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
return packageInfo.applicationInfo.targetSdkVersion;
}
catch (PackageManager.NameNotFoundException ignored) {
}
return -1;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public abstract class BaseProcessorUnit : ProcessorUnit {
protected val PERMISSION_UTILS = ClassName.get("permissions.dispatcher", "PermissionUtils")
private val MANIFEST_WRITE_SETTING = "android.permission.WRITE_SETTINGS"
private val MANIFEST_SYSTEM_ALERT_WINDOW = "android.permission.SYSTEM_ALERT_WINDOW"
private val BUILD_CLASS = ClassName.get("android.os", "Build")
private val ADD_WITH_CHECK_BODY_MAP = hashMapOf(MANIFEST_SYSTEM_ALERT_WINDOW to SystemAlertWindowHelper(), MANIFEST_WRITE_SETTING to WriteSettingsHelper())

/**
Expand Down Expand Up @@ -273,10 +272,12 @@ public abstract class BaseProcessorUnit : ProcessorUnit {
val onDenied: ExecutableElement? = rpe.findOnDeniedForNeeds(needsMethod)
val hasDenied = onDenied != null
val needsPermissionParameter = needsMethod.getAnnotation(NeedsPermission::class.java).value[0]
if (hasDenied && !ADD_WITH_CHECK_BODY_MAP.containsKey(needsPermissionParameter)) {
builder.beginControlFlow("if (\$T.VERSION.SDK_INT < 23 && !\$T.hasSelfPermissions(\$N, \$N))",
BUILD_CLASS, PERMISSION_UTILS, getActivityName(targetParam), permissionFieldName(needsMethod))
builder.addStatement("\$N.\$N()", targetParam, onDenied!!.simpleString())
if (!ADD_WITH_CHECK_BODY_MAP.containsKey(needsPermissionParameter)) {
builder.beginControlFlow("if (\$T.getTargetSdkVersion(\$N) < 23 && !\$T.hasSelfPermissions(\$N, \$N))",
PERMISSION_UTILS, getActivityName(targetParam), PERMISSION_UTILS, getActivityName(targetParam), permissionFieldName(needsMethod))
if (hasDenied) {
builder.addStatement("\$N.\$N()", targetParam, onDenied!!.simpleString())
}
builder.addStatement("return")
builder.endControlFlow()
}
Expand Down

0 comments on commit 7c0836b

Please sign in to comment.