Skip to content

Commit

Permalink
Bugfix for freeze when requesting permissions on Android (Unity 2019)
Browse files Browse the repository at this point in the history
  • Loading branch information
yasirkula committed Jul 19, 2019
1 parent 7a69c56 commit 4719de5
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
23 changes: 21 additions & 2 deletions JAR Source/RuntimePermissionsFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import android.annotation.TargetApi;
import android.app.Fragment;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

import java.util.ArrayList;
import java.util.Random;

/**
* Created by yasirkula on 27.04.2018.
Expand Down Expand Up @@ -42,7 +44,8 @@ public void onCreate( Bundle savedInstanceState )
else
{
m_permissions = getArguments().getStringArray( PERMISSIONS );
requestPermissions( m_permissions, PERMISSIONS_REQUEST_CODE );
if( m_permissions != null )
requestPermissions( m_permissions, PERMISSIONS_REQUEST_CODE );
}
}

Expand All @@ -55,6 +58,8 @@ public void onRequestPermissionsResult( int requestCode, String[] permissions, i
if( permissionReceiver == null || m_permissions == null )
{
Log.e( "Unity", "Fragment data got reset while asking permissions!" );

getFragmentManager().beginTransaction().remove( this ).commit();
return;
}

Expand Down Expand Up @@ -101,5 +106,19 @@ else if( !shouldShowRequestPermissionRationale( permission ) )

permissionReceiver.OnPermissionResult( result );
getFragmentManager().beginTransaction().remove( this ).commit();

// Resolves a bug in Unity 2019 where the calling activity
// doesn't resume automatically after the fragment finishes
// Credit: https://stackoverflow.com/a/12409215/2373034
try
{
Intent resumeUnityActivity = new Intent( getActivity(), getActivity().getClass() );
resumeUnityActivity.setFlags( Intent.FLAG_ACTIVITY_REORDER_TO_FRONT );
getActivity().startActivityIfNeeded( resumeUnityActivity, 0 );
}
catch( Exception e )
{
Log.e( "Unity", "Exception (resume):", e );
}
}
}
}
Binary file modified Plugins/AndroidRuntimePermissions/Android/RuntimePermissions.jar
Binary file not shown.
23 changes: 23 additions & 0 deletions Plugins/AndroidRuntimePermissions/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
= Android Runtime Permissions =

Online documentation & example code available at: https://github.com/yasirkula/UnityAndroidRuntimePermissions
E-mail: [email protected]

1. ABOUT
This plugin helps you query/request runtime permissions synchronously on Android M and later.

2. HOW TO
You can use the following static functions of AndroidRuntimePermissions to manage runtime permissions:

- Permission CheckPermission( string permission ): checks whether or not the permission is granted. Permission is an enum that can take 3 values:
-- Granted: permission is granted
-- ShouldAsk: permission is not granted yet, but we can ask the user for permission via RequestPermission function. As long as the user doesn't select "Don't ask again" while denying the permission, ShouldAsk is returned
-- Denied: we don't have permission and we can't ask the user for permission. In this case, user has to give the permission from app's Settings. This happens when user selects "Don't ask again" while denying the permission or when user is not allowed to give that permission (parental controls etc.)

- Permission[] CheckPermissions( params string[] permissions ): queries multiple permissions simultaneously. The returned array will contain one Permission entry per each queried permission

- Permission RequestPermission( string permission ): requests a permission from the user and returns the result. It is recommended to show a brief explanation before asking the permission so that user understands why the permission is needed and doesn't click Deny or worse, "Don't ask again"

- Permission[] RequestPermissions( params string[] permissions ): requests multiple permissions simultaneously

- void OpenSettings(): opens the settings for this app, from where the user can manually grant permission(s) in case a needed permission's state is Permission.Denied
8 changes: 8 additions & 0 deletions Plugins/AndroidRuntimePermissions/README.txt.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified RuntimePermissions.unitypackage
Binary file not shown.

0 comments on commit 4719de5

Please sign in to comment.