├── .gitignore ├── Assets ├── Plugins │ └── Android │ │ └── unityandroidpermissions.aar └── Scripts │ ├── AndroidPermissionsManager.cs │ └── AndroidPermissionsUsageExample.cs ├── CHANGELOG ├── LICENSE ├── README.md └── src └── UnityAndroidPermissions ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── proguard-rules.pro └── src └── main ├── AndroidManifest.xml └── java └── com └── unity3d └── plugin ├── PermissionFragment.java └── UnityAndroidPermissions.java /.gitignore: -------------------------------------------------------------------------------- 1 | Library/ 2 | Packages/ 3 | ProjectSettings/ 4 | src/UnityAndroidPermissions/.gradle/ 5 | src/UnityAndroidPermissions/build/ 6 | Temp/ 7 | UnityPackageManager/ 8 | *.meta 9 | *.csproj 10 | *.sln 11 | .vs/ 12 | -------------------------------------------------------------------------------- /Assets/Plugins/Android/unityandroidpermissions.aar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Over17/UnityAndroidPermissions/3736f43b0692908a8a57a3e10c77dcd6b89f983b/Assets/Plugins/Android/unityandroidpermissions.aar -------------------------------------------------------------------------------- /Assets/Scripts/AndroidPermissionsManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEngine; 3 | 4 | public class AndroidPermissionCallback : AndroidJavaProxy 5 | { 6 | private event Action OnPermissionGrantedAction; 7 | private event Action OnPermissionDeniedAction; 8 | private event Action OnPermissionDeniedAndDontAskAgainAction; 9 | 10 | public AndroidPermissionCallback(Action onGrantedCallback, Action onDeniedCallback, Action onDeniedAndDontAskAgainCallback) 11 | : base("com.unity3d.plugin.UnityAndroidPermissions$IPermissionRequestResult2") 12 | { 13 | if (onGrantedCallback != null) 14 | { 15 | OnPermissionGrantedAction += onGrantedCallback; 16 | } 17 | if (onDeniedCallback != null) 18 | { 19 | OnPermissionDeniedAction += onDeniedCallback; 20 | } 21 | if (onDeniedAndDontAskAgainCallback != null) 22 | { 23 | OnPermissionDeniedAndDontAskAgainAction += onDeniedAndDontAskAgainCallback; 24 | } 25 | } 26 | 27 | // Handle permission granted 28 | public virtual void OnPermissionGranted(string permissionName) 29 | { 30 | //Debug.Log("Permission " + permissionName + " GRANTED"); 31 | if (OnPermissionGrantedAction != null) 32 | { 33 | OnPermissionGrantedAction(permissionName); 34 | } 35 | } 36 | 37 | // Handle permission denied 38 | public virtual void OnPermissionDenied(string permissionName) 39 | { 40 | //Debug.Log("Permission " + permissionName + " DENIED!"); 41 | if (OnPermissionDeniedAction != null) 42 | { 43 | OnPermissionDeniedAction(permissionName); 44 | } 45 | } 46 | 47 | // Handle permission denied and 'Dont ask again' selected 48 | // Note: falls back to OnPermissionDenied() if action not registered 49 | public virtual void OnPermissionDeniedAndDontAskAgain(string permissionName) 50 | { 51 | //Debug.Log("Permission " + permissionName + " DENIED and 'Dont ask again' was selected!"); 52 | if (OnPermissionDeniedAndDontAskAgainAction != null) 53 | { 54 | OnPermissionDeniedAndDontAskAgainAction(permissionName); 55 | } 56 | else if (OnPermissionDeniedAction != null) 57 | { 58 | // Fall back to OnPermissionDeniedAction 59 | OnPermissionDeniedAction(permissionName); 60 | } 61 | } 62 | } 63 | 64 | public class AndroidPermissionsManager 65 | { 66 | private static AndroidJavaObject m_Activity; 67 | private static AndroidJavaObject m_PermissionService; 68 | 69 | private static AndroidJavaObject GetActivity() 70 | { 71 | if (m_Activity == null) 72 | { 73 | var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); 74 | m_Activity = unityPlayer.GetStatic("currentActivity"); 75 | } 76 | return m_Activity; 77 | } 78 | 79 | private static AndroidJavaObject GetPermissionsService() 80 | { 81 | return m_PermissionService ?? 82 | (m_PermissionService = new AndroidJavaObject("com.unity3d.plugin.UnityAndroidPermissions")); 83 | } 84 | 85 | public static bool IsPermissionGranted(string permissionName) 86 | { 87 | return GetPermissionsService().Call("IsPermissionGranted", GetActivity(), permissionName); 88 | } 89 | 90 | public static void RequestPermission(string permissionName, AndroidPermissionCallback callback) 91 | { 92 | RequestPermission(new[] {permissionName}, callback); 93 | } 94 | 95 | public static void RequestPermission(string[] permissionNames, AndroidPermissionCallback callback) 96 | { 97 | GetPermissionsService().Call("RequestPermissionAsync", GetActivity(), permissionNames, callback); 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /Assets/Scripts/AndroidPermissionsUsageExample.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using UnityEngine; 3 | 4 | public class AndroidPermissionsUsageExample : MonoBehaviour 5 | { 6 | private const string STORAGE_PERMISSION = "android.permission.READ_EXTERNAL_STORAGE"; 7 | 8 | // Function to be called first (by UI button) 9 | // For example, click on Avatar to change it from the device gallery 10 | public void OnBrowseGalleryButtonPress() 11 | { 12 | if (!CheckPermissions()) 13 | { 14 | Debug.LogWarning("Missing permission to browse device gallery, please grant the permission first"); 15 | 16 | // Your code to show in-game pop-up with the explanation why you need this permission (required for Google Featuring program) 17 | // This pop-up should include a button "Grant Access" linked to the function "OnGrantButtonPress" below 18 | return; 19 | } 20 | 21 | // Your code to browse Android Gallery 22 | Debug.Log("Browsing Gallery..."); 23 | } 24 | 25 | private bool CheckPermissions() 26 | { 27 | if (Application.platform != RuntimePlatform.Android) 28 | { 29 | return true; 30 | } 31 | 32 | return AndroidPermissionsManager.IsPermissionGranted(STORAGE_PERMISSION); 33 | } 34 | 35 | public void OnGrantButtonPress() 36 | { 37 | AndroidPermissionsManager.RequestPermission(new []{STORAGE_PERMISSION}, new AndroidPermissionCallback( 38 | grantedPermission => 39 | { 40 | // The permission was successfully granted, restart the change avatar routine 41 | OnBrowseGalleryButtonPress(); 42 | }, 43 | deniedPermission => 44 | { 45 | // The permission was denied 46 | }, 47 | deniedPermissionAndDontAskAgain => 48 | { 49 | // The permission was denied, and the user has selected "Don't ask again" 50 | // Show in-game pop-up message stating that the user can change permissions in Android Application Settings 51 | // if he changes his mind (also required by Google Featuring program) 52 | })); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /CHANGELOG: -------------------------------------------------------------------------------- 1 | Version 0.5 (2018-12-07) 2 | 3 | * Added a new interface IPermissionRequestResult2 with method OnPermissionDeniedAndDontAskAgainAction() 4 | 5 | 6 | Version 0.4.1 (2017-10-24) 7 | 8 | * Fixed crash on Android Marshmallow when the Fragment is recreated 9 | 10 | 11 | Version 0.4 (2017-08-28) 12 | 13 | * Fixed issues with Android Oreo (IllegalStateException) 14 | 15 | 16 | Version 0.3 (2017-08-24) 17 | 18 | * Moved the plugin code to utilize delegates instead of having to override the callback methods 19 | * Added sample script 20 | * Big thanks to @Briksins for his contribution 21 | 22 | 23 | Version 0.2 (2017-07-07) 24 | 25 | * There's no more need to override the main manifest - the plugin's manifest contains the necessary SkipPermissionsDialog metadata flag 26 | 27 | 28 | Version 0.1 (2017-07-04) 29 | 30 | * Initial version -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Yury Habets 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # UnityAndroidPermissions 2 | Starting with Android Marshmallow (Android 6), Google introduced Runtime permissions system where the user is asked to grant a permission in runtime rather than doing that during installation of the app. 3 | However, Unity for Android is not supporting it out of the box because: 4 | - the corresponding Android API requires an Activity, when Unity can run without it. All non-Activity Unity applications are not supported for that reason 5 | - the plugins may add a dangerous permission and not have the code to handle it correctly, thus causing the whole app to crash 6 | This is the reason why Unity prompts the user for all the permissions on startup. This behavior is the safest and most compatible. 7 | 8 | **NOTE:** Unity 2018.3 introduces runtime permissions support and API, so starting with this version you don't need this plugin in most cases. 9 | 10 | However, Google requires the runtime permission system to be implemented to get featured on Google Play. To let the user implement it (and take the responsibility), the Unity's dialog on startup can be suppressed by adding "unityplayer.SkipPermissionsDialog"="true" metadata tag to application or activity section of the Android Manifest. 11 | 12 | This plugin is one of the Android runtime permissions for Unity implementations. It provides the API to check the status of a permission, request a set of permissions and get a callback with the result. 13 | 14 | ## API 15 | 16 | `AndroidPermissionsManager` is the class which provides you the following methods: 17 | - `bool IsPermissionGranted(string permissionName)` to check the status of a permission 18 | - `void RequestPermission(string[] permissionNames, AndroidPermissionCallback callback)` to query for an array of permissions. Pass `AndroidPermissionCallback` object with your own callback implementations defined in delegates (`Action onGrantedCallback` is called when a permission is granted, `Action onDeniedCallback` - when a permission is denied, `Action onPermissionDeniedAndDontAskAgainAction` - when a permission is denied and the user marks "Don't ask again" checkbox, corresponding permission name is passed as a string param). NOTE: the callbacks are called from Java UI thread (which is different from Unity UI thread), so be careful about the APIs you call from the callback. 19 | 20 | ## Usage 21 | 0. Should work with Unity 5.3+. Please report an issue if it does not for you 22 | 1. Add the plugin to your project. You need the AAR and the C# script (Assets/Plugins/Android/unityandroidpermissions.aar and Assets/Scripts/AndroidPermissionsManager.cs) 23 | 2. Use the C# API in your scripts to check the permission status and request it if necessary, right before you actually need this permission 24 | 3. Enjoy 25 | 26 | For a script sample, please refer to `Assets/Scripts/AndroidPermissionsUsageExample.cs`. 27 | 28 | ## How to Build the Plugin From Source 29 | Run `gradlew assemble` from src/UnityAndroidPermissions/ 30 | 31 | ## See Also 32 | Please refer to Google documentation for more details: https://developer.android.com/training/permissions/requesting.html 33 | -------------------------------------------------------------------------------- /src/UnityAndroidPermissions/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | google() 7 | } 8 | 9 | dependencies { 10 | classpath 'com.android.tools.build:gradle:3.0.1' 11 | } 12 | } 13 | 14 | android { 15 | compileSdkVersion 26 16 | buildToolsVersion "26.0.2" 17 | 18 | defaultConfig { 19 | minSdkVersion 16 20 | targetSdkVersion 26 21 | versionCode 1 22 | versionName "1.0" 23 | consumerProguardFiles 'proguard-rules.pro' 24 | } 25 | buildTypes { 26 | release { 27 | minifyEnabled false 28 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 29 | } 30 | } 31 | } 32 | 33 | afterEvaluate { 34 | generateReleaseBuildConfig.enabled = false 35 | } 36 | -------------------------------------------------------------------------------- /src/UnityAndroidPermissions/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Over17/UnityAndroidPermissions/3736f43b0692908a8a57a3e10c77dcd6b89f983b/src/UnityAndroidPermissions/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /src/UnityAndroidPermissions/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | zipStoreBase=GRADLE_USER_HOME 4 | zipStorePath=wrapper/dists 5 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.2.1-bin.zip 6 | -------------------------------------------------------------------------------- /src/UnityAndroidPermissions/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /src/UnityAndroidPermissions/gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /src/UnityAndroidPermissions/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in C:\android-sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | 27 | -keep class com.unity3d.plugin.PermissionFragment { *; } 28 | -keep class com.unity3d.plugin.UnityAndroidPermissions { *; } 29 | -keep interface com.unity3d.plugin.UnityAndroidPermissions$IPermissionRequestResult { *; } 30 | -keep interface com.unity3d.plugin.UnityAndroidPermissions$IPermissionRequestResult2 { *; } 31 | -------------------------------------------------------------------------------- /src/UnityAndroidPermissions/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /src/UnityAndroidPermissions/src/main/java/com/unity3d/plugin/PermissionFragment.java: -------------------------------------------------------------------------------- 1 | package com.unity3d.plugin; 2 | 3 | import android.app.Activity; 4 | import android.app.Fragment; 5 | import android.app.FragmentTransaction; 6 | import android.content.pm.PackageManager; 7 | import android.os.Bundle; 8 | 9 | public class PermissionFragment extends Fragment 10 | { 11 | public static final String PERMISSION_NAMES = "PermissionNames"; 12 | 13 | private static final int PERMISSIONS_REQUEST_CODE = 15887; 14 | 15 | private final UnityAndroidPermissions.IPermissionRequestResult m_ResultCallbacks; 16 | private final Activity m_Activity; 17 | 18 | public PermissionFragment() 19 | { 20 | m_ResultCallbacks = null; 21 | m_Activity = null; 22 | } 23 | 24 | public PermissionFragment(final Activity activity, final UnityAndroidPermissions.IPermissionRequestResult resultCallbacks) 25 | { 26 | m_ResultCallbacks = resultCallbacks; 27 | m_Activity = activity; 28 | } 29 | 30 | @Override public void onCreate(Bundle savedInstanceState) 31 | { 32 | super.onCreate(savedInstanceState); 33 | if (m_ResultCallbacks == null) 34 | { 35 | getFragmentManager().beginTransaction().remove(this).commit(); 36 | } 37 | else 38 | { 39 | String[] permissionNames = getArguments().getStringArray(PERMISSION_NAMES); 40 | requestPermissions(permissionNames, PERMISSIONS_REQUEST_CODE); 41 | } 42 | } 43 | 44 | @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) 45 | { 46 | if (requestCode != PERMISSIONS_REQUEST_CODE) 47 | return; 48 | 49 | for (int i = 0; i < permissions.length && i < grantResults.length; ++i) 50 | { 51 | if (grantResults[i] == PackageManager.PERMISSION_GRANTED) 52 | m_ResultCallbacks.OnPermissionGranted(permissions[i]); 53 | else 54 | { 55 | if (m_Activity != null && 56 | !m_Activity.shouldShowRequestPermissionRationale(permissions[i]) && 57 | (m_ResultCallbacks instanceof UnityAndroidPermissions.IPermissionRequestResult2)) 58 | { 59 | ((UnityAndroidPermissions.IPermissionRequestResult2) m_ResultCallbacks).OnPermissionDeniedAndDontAskAgain(permissions[i]); 60 | } 61 | else 62 | { 63 | m_ResultCallbacks.OnPermissionDenied(permissions[i]); 64 | } 65 | } 66 | } 67 | 68 | FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); 69 | fragmentTransaction.remove(this); 70 | fragmentTransaction.commit(); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/UnityAndroidPermissions/src/main/java/com/unity3d/plugin/UnityAndroidPermissions.java: -------------------------------------------------------------------------------- 1 | package com.unity3d.plugin; 2 | 3 | import android.app.Activity; 4 | import android.app.Fragment; 5 | import android.app.FragmentTransaction; 6 | import android.content.pm.PackageManager; 7 | import android.os.Build; 8 | import android.os.Bundle; 9 | 10 | public class UnityAndroidPermissions 11 | { 12 | interface IPermissionRequestResult 13 | { 14 | void OnPermissionGranted(String permissionName); 15 | void OnPermissionDenied(String permissionName); 16 | } 17 | 18 | interface IPermissionRequestResult2 extends IPermissionRequestResult 19 | { 20 | void OnPermissionDeniedAndDontAskAgain(String permissionName); 21 | } 22 | 23 | public boolean IsPermissionGranted (Activity activity, String permissionName) 24 | { 25 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) 26 | return true; 27 | if (activity == null) 28 | return false; 29 | return activity.checkSelfPermission(permissionName) == PackageManager.PERMISSION_GRANTED; 30 | } 31 | 32 | public void RequestPermissionAsync(Activity activity, final String[] permissionNames, final IPermissionRequestResult resultCallbacks) 33 | { 34 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) 35 | return; 36 | if (activity == null || permissionNames == null || resultCallbacks == null) 37 | return; 38 | 39 | final Fragment request = new PermissionFragment(activity, resultCallbacks); 40 | Bundle bundle = new Bundle(); 41 | bundle.putStringArray(PermissionFragment.PERMISSION_NAMES, permissionNames); 42 | request.setArguments(bundle); 43 | FragmentTransaction fragmentTransaction = activity.getFragmentManager().beginTransaction(); 44 | fragmentTransaction.add(0, request); 45 | fragmentTransaction.commit(); 46 | } 47 | } 48 | --------------------------------------------------------------------------------