Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Over17 committed Jul 4, 2017
0 parents commit 3e856f2
Show file tree
Hide file tree
Showing 11 changed files with 241 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.meta
16 changes: 16 additions & 0 deletions Assets/Plugins/Android/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.company.productname">
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" android:xlargeScreens="true" />
<application android:icon="@drawable/app_icon" android:label="@string/app_name" android:theme="@style/UnityThemeSelector">
<activity android:label="@string/app_name" android:name="com.unity3d.player.UnityPlayerActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
<meta-data android:name="unityplayer.SkipPermissionsDialog" android:value="true" />
</activity>
</application>
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="25" />
</manifest>
Binary file not shown.
54 changes: 54 additions & 0 deletions Assets/Scripts/AndroidPermissionsManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using UnityEngine;

public class AndroidPermissionCallback : AndroidJavaProxy
{
public AndroidPermissionCallback() : base("com.unity3d.player.UnityAndroidPermissions$IPermissionRequestResult") { }

// Handle permission granted
public virtual void OnPermissionGranted(string permissionName)
{
//Debug.Log("Permission " + permissionName + " GRANTED");
}

// Handle permission denied
public virtual void OnPermissionDenied(string permissionName)
{
//Debug.Log("Permission " + permissionName + " DENIED!");
}
}

public class AndroidPermissionsManager
{
private static AndroidJavaObject m_Activity;
private static AndroidJavaObject m_PermissionService;

private static AndroidJavaObject GetActivity()
{
if (m_Activity == null)
{
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
m_Activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
}
return m_Activity;
}

private static AndroidJavaObject GetPermissionsService()
{
if (m_PermissionService == null)
{
m_PermissionService = new AndroidJavaObject("com.unity3d.player.UnityAndroidPermissions");
}
return m_PermissionService;
}


public static bool IsPermissionGranted(string permissionName)
{
return GetPermissionsService().Call<bool>("IsPermissionGranted", GetActivity(), permissionName);
}

public static void RequestPermission(string[] permissionNames, AndroidPermissionCallback callback)
{
GetPermissionsService().Call("RequestPermissionAsync", GetActivity(), permissionNames, callback);
}
}
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Version 0.1 (2016-07-04)

* Initial version
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Yury Habets

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# UnityAndroidPermissions
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.
However, Unity for Android is not supporting it out of the box because:
- the corresponding Android API requires an Activity, when Unity can run without it. All non-Activity Unity applications are not supported for that reason
- the plugins may add a dangerous permission and not have the code to handle it correctly, thus causing the whole app to crash
This is the reason why Unity prompts the user for all the permissions on startup. This behavior is the safest and most compatible.

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.

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.

## API

`AndroidPermissionsManager` is the class which provides you the following methods:
- `bool IsPermissionGranted(string permissionName)` to check the status of a permission
- `void RequestPermission(string[] permissionNames, AndroidPermissionCallback callback)` to query for an array of permissions. Pass `AndroidPermissionCallback` object or an object of derived class with your own callback implementation. `void AndroidPermissionCallback.OnPermissionGranted(string permissionName)` is called when a permission is granted, `void OnPermissionDenied(string permissionName)` - when a permission is denied.

## Usage
0. Should work with Unity 5.3+. Please report an issue if it does not for you
1. Add the plugin to your project. You need the manifest, the AAR and the C# script (Assets/Plugins/Android/AndroidManifest.xml, Assets/Plugins/Android/unityandroidpermissions.aar and Assets/AndroidPermissionsManager.cs)
2. Please pay attention to the manifest - you may want to use the one provided here or, if you have your own base manifest, please make sure to add "unityplayer.SkipPermissionsDialog"="true" metadata tag to application or activity section
3. Use the C# API in your scripts to check the permission status and request it if necessary, right before you actually need this permission
4. Enjoy

## How to Build
Use Android Studio to build the AAR from the source in src/ directory.

## See Also
Please refer to Google documentation for more details: https://developer.android.com/training/permissions/requesting.html
19 changes: 19 additions & 0 deletions src/UnityShowAndroidStatusBar/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 25
buildToolsVersion "26.0.0"

defaultConfig {
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
25 changes: 25 additions & 0 deletions src/UnityShowAndroidStatusBar/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in C:\android-sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
4 changes: 4 additions & 0 deletions src/UnityShowAndroidStatusBar/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.unityandroidpermissions">

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.unity3d.player;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;

public class UnityAndroidPermissions
{
private static final int PERMISSIONS_REQUEST_CODE = 15887;

interface IPermissionRequestResult
{
void OnPermissionGranted(String permissionName);
void OnPermissionDenied(String permissionName);
}

public boolean IsPermissionGranted (Activity activity, String permissionName)
{
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
return true;
if (activity == null)
return false;
return activity.checkSelfPermission(permissionName) == PackageManager.PERMISSION_GRANTED;
}

public void RequestPermissionAsync(Activity activity, final String[] permissionNames, final IPermissionRequestResult resultCallbacks)
{
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M)
return;
if (activity == null || permissionNames == null || resultCallbacks == null)
return;

final FragmentManager fragmentManager = activity.getFragmentManager();
final Fragment request = new Fragment()
{
@Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestPermissions(permissionNames, PERMISSIONS_REQUEST_CODE);
}

@Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
{
if (requestCode != PERMISSIONS_REQUEST_CODE)
return;

for (int i = 0; i < permissions.length && i < grantResults.length; ++i)
{
if (grantResults[i] == PackageManager.PERMISSION_GRANTED)
resultCallbacks.OnPermissionGranted(permissions[i]);
else
resultCallbacks.OnPermissionDenied(permissions[i]);
}

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(this);
fragmentTransaction.commit();
}
};

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(0, request);
fragmentTransaction.commit();
}
}

0 comments on commit 3e856f2

Please sign in to comment.