Skip to content

Commit

Permalink
Implement RequestPermissions API
Browse files Browse the repository at this point in the history
  • Loading branch information
ozdemir08 committed Jan 3, 2020
1 parent 584d360 commit ad1c7c0
Show file tree
Hide file tree
Showing 11 changed files with 323 additions and 25 deletions.
27 changes: 9 additions & 18 deletions samples/SmokeTest/Source/Assets/SmokeTest/MainGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -944,29 +944,20 @@ private void ShowPopupGravityUi()
private void ShowPermissionsUi()
{
DrawStatus();
DrawTitle("PopupGravity Ui");
DrawTitle("Permissions Ui");

if (GUI.Button(CalcGrid(0, 1), "Has Drive - AppData"))
{
Status = "Drive - AppData Permission " + PlayGamesPlatform.Instance.HasPermission(
/* scope= */ "https://www.googleapis.com/auth/drive.appdata");
}
else if (GUI.Button(CalcGrid(1, 1), "Has Games Lite"))
if (GUI.Button(CalcGrid(0, 1), "Has Permission - Email"))
{
Status = "Games Lite Permission " + PlayGamesPlatform.Instance.HasPermission(
/* scope= */ "https://www.googleapis.com/auth/games_lite");
Status = "Email Permission " + PlayGamesPlatform.Instance.HasPermission("email");
}
else if (GUI.Button(CalcGrid(0, 2), "Has User Info - Profile"))
else if (GUI.Button(CalcGrid(1, 1), "Request Permission- Email"))
{
Status = "User Info - Profile Permission " + PlayGamesPlatform.Instance.HasPermission(
/* scope= */ "https://www.googleapis.com/auth/userinfo.profile");
Status = "Asking permission for email";
PlayGamesPlatform.Instance.RequestPermission(
code => { Status = "Result code " + code; },
"email");
}
else if (GUI.Button(CalcGrid(1, 2), "Has Play Games Activity"))
{
Status = "Play Games Activity Permission " + PlayGamesPlatform.Instance.HasPermission(
/* scope= */ "https://www.googleapis.com/auth/games ");
}
else if (GUI.Button(CalcGrid(1, 4), "Back"))
else if (GUI.Button(CalcGrid(1, 6), "Back"))
{
SetUI(Ui.Main);
ShowEffect(true);
Expand Down
14 changes: 13 additions & 1 deletion source/PluginDev/Assets/GooglePlayGames/BasicApi/DummyClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,19 @@ public void SubmitScore(
callback.Invoke(false);
}
}


/// <summary>Asks user to give permissions for the given scopes.</summary>
/// <param name="callback">Callback used to indicate the outcome of the operation.</param>
/// <param name="scopes">Scope to ask permission for</param>
public void RequestPermissions(Action<SignInStatus> callback, string[] scopes)
{
LogUsage();
if (callback != null)
{
callback.Invoke(SignInStatus.Failed);
}
}

/// <summary>Returns whether or not user has given permissions for given scopes.</summary>
/// <seealso cref="GooglePlayGames.BasicApi.IPlayGamesClient.HasPermissions"/>
/// <param name="scopes">array of scopes</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,13 @@ void SubmitScore(string leaderboardId, long score,
void SubmitScore(string leaderboardId, long score, string metadata,
Action<bool> successOrFailureCalllback);

/// <summary>
/// Asks user to give permissions for the given scopes.
/// </summary>
/// <param name="callback">Callback used to indicate the outcome of the operation.</param>
/// <param name="scopes">list of scopes to ask permission for</param>
void RequestPermissions(Action<SignInStatus> callback, string[] scopes);

/// <summary>
/// Returns whether or not user has given permissions for given scopes
/// </summary>
Expand Down
42 changes: 42 additions & 0 deletions source/PluginDev/Assets/GooglePlayGames/BasicApi/SignInStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
namespace GooglePlayGames.BasicApi
{
public enum SignInStatus
{
/// <summary>The operation was successful.</summary>
Success,

/// <summary>
/// The client attempted to connect to the service but the user is not signed in. The client may
/// choose to continue without using the API. Alternately, if {@link Status#hasResolution} returns
/// {@literal true} the client may call {@link Status#startResolutionForResult(Activity, int)} to
/// prompt the user to sign in. After the sign in activity returns with {@link Activity#RESULT_OK}
/// further attempts should succeed.
/// </summary>
UiSignInRequired,

/// <summary>A network error occurred. Retrying should resolve the problem.</summary>
NetworkError,

/// <summary>An internal error occurred.</summary>
InternalError,

/// <summary>The sign in was canceled.</summary>
Canceled,

/// <summary>
/// A sign in process is currently in progress and the current one cannot continue. e.g. the user
/// clicks the SignInButton multiple times and more than one sign in intent was launched.
/// </summary>
AlreadyInProgress,

/// <summary>
/// Failure reason is unknown. Check adb log to see details if any.
/// </summary>
Failed,

/// <summary>
/// Currently not authenticated. Silent or interactive sign in is required.
/// </summary>
NotAuthenticated,
}
}

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

Original file line number Diff line number Diff line change
Expand Up @@ -1280,14 +1280,38 @@ public void LoadScores(ILeaderboard board, Action<bool> callback)
(PlayGamesLeaderboard) board, scoreData, callback));
}

/// <summary>Asks user to give permissions for the given scopes.</summary>
/// <param name="callback">Callback used to indicate the outcome of the operation.</param>
/// <param name="scopes">Scope to ask permission for</param>
public void RequestPermission(Action<SignInStatus> callback, string scope)
{
RequestPermissions(callback, new string[] {scope});
}

/// <summary>Asks user to give permissions for the given scopes.</summary>
/// <param name="callback">Callback used to indicate the outcome of the operation.</param>
/// <param name="scopes">List of scopes to ask permission for</param>
public void RequestPermissions(Action<SignInStatus> callback, string[] scopes)
{
if (!IsAuthenticated())
{
GooglePlayGames.OurUtils.Logger.e(
"HasPermissions can only be called after authentication.");
callback(SignInStatus.NotAuthenticated);
return;
}

mClient.RequestPermissions(callback, scopes);
}

/// <summary>Returns whether or not user has given permissions for given scopes.</summary>
/// <param name="scope">scope</param>
/// <returns><c>true</c>, if given, <c>false</c> otherwise.</returns>
public bool HasPermission(string scope)
{
return HasPermissions(new string[] {scope});
}

/// <summary>Returns whether or not user has given permissions for given scopes.</summary>
/// <param name="scopes">array of scopes</param>
/// <returns><c>true</c>, if given, <c>false</c> otherwise.</returns>
Expand All @@ -1299,7 +1323,7 @@ public bool HasPermissions(string[] scopes)
"HasPermissions can only be called after authentication.");
return false;
}

return mClient.HasPermissions(scopes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,30 @@ public void SubmitScore(string leaderboardId, long score, string metadata,
}
}

public void RequestPermissions(Action<SignInStatus> callback, string[] scopes)
{
callback = AsOnGameThreadCallback(callback);
mTokenClient.RequestPermissions((code =>
{
UpdateClients();
callback(code);
}), scopes);
}

private void UpdateClients()
{
lock (GameServicesLock)
{
var account = mTokenClient.GetAccount();
mSavedGameClient = new AndroidSavedGameClient(account);
mEventsClient = new AndroidEventsClient(account);
mVideoClient = new AndroidVideoClient(mVideoClient.IsCaptureSupported(), account);
mRealTimeClient = new AndroidRealTimeMultiplayerClient(this, account);
mTurnBasedClient = new AndroidTurnBasedMultiplayerClient(this, account);
mTurnBasedClient.RegisterMatchDelegate(mConfiguration.MatchDelegate);
}
}

/// <summary>Returns whether or not user has given permissions for given scopes.</summary>
/// <seealso cref="GooglePlayGames.BasicApi.IPlayGamesClient.HasPermissions"/>
public bool HasPermissions(string[] scopes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace GooglePlayGames.Android
{
using System;
using System.Linq;
using BasicApi;
using OurUtils;
using UnityEngine;
Expand Down Expand Up @@ -132,6 +133,79 @@ public void FetchTokens(bool silent, Action<int> callback)
PlayGamesHelperObject.RunOnGameThread(() => DoFetchToken(silent, callback));
}

public void RequestPermissions(Action<SignInStatus> callback, string[] scopes)
{
using (var bridgeClass = new AndroidJavaClass(HelperFragmentClass))
using (var currentActivity = AndroidHelperFragment.GetActivity())
using (var task =
bridgeClass.CallStatic<AndroidJavaObject>("showRequestPermissionsUi", currentActivity,
oauthScopes.Union(scopes).ToArray()))
{
AndroidTaskUtils.AddOnSuccessListener<AndroidJavaObject>(task, /* disposeResult= */ false,
accountWithNewScopes =>
{
if (accountWithNewScopes == null)
{
callback(SignInStatus.InternalError);
return;
}

account = accountWithNewScopes;
email = account.Call<string>("getEmail");
idToken = account.Call<string>("getIdToken");
authCode = account.Call<string>("getServerAuthCode");
oauthScopes = oauthScopes.Union(scopes).ToList();
callback(SignInStatus.Success);
});

AndroidTaskUtils.AddOnFailureListener(task, e =>
{
var failCode = ToSignInStatus(e.Call<int>("getStatusCode"));
OurUtils.Logger.e("Exception requesting new permissions: " + failCode);
callback(failCode);
});
}
}

private static SignInStatus ToSignInStatus(int code)
{
Dictionary<int, SignInStatus> dictionary = new Dictionary<int, SignInStatus>()
{
{
/* CommonUIStatus.UI_BUSY */ -12, SignInStatus.AlreadyInProgress
},
{
/* CommonStatusCodes.SUCCESS */ 0, SignInStatus.Success
},
{
/* CommonStatusCodes.SIGN_IN_REQUIRED */ 4, SignInStatus.UiSignInRequired
},
{
/* CommonStatusCodes.NETWORK_ERROR */ 7, SignInStatus.NetworkError
},
{
/* CommonStatusCodes.INTERNAL_ERROR */ 8, SignInStatus.InternalError
},
{
/* CommonStatusCodes.CANCELED */ 16, SignInStatus.Canceled
},
{
/* CommonStatusCodes.API_NOT_CONNECTED */ 17, SignInStatus.Failed
},
{
/* GoogleSignInStatusCodes.SIGN_IN_FAILED */ 12500, SignInStatus.Failed
},
{
/* GoogleSignInStatusCodes.SIGN_IN_CANCELLED */ 12501, SignInStatus.Canceled
},
{
/* GoogleSignInStatusCodes.SIGN_IN_CURRENTLY_IN_PROGRESS */ 12502, SignInStatus.AlreadyInProgress
},
};

return dictionary.ContainsKey(code) ? dictionary[code] : SignInStatus.Failed;
}

/// <summary>Returns whether or not user has given permissions for given scopes.</summary>
/// <param name="scopes">array of scopes</param>
/// <returns><c>true</c>, if given, <c>false</c> otherwise.</returns>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#if UNITY_ANDROID
namespace GooglePlayGames
{
using GooglePlayGames.BasicApi;
using System;

internal interface TokenClient
Expand Down Expand Up @@ -68,6 +69,8 @@ void GetAnotherServerAuthCode(bool reAuthenticateIfNeeded,

void FetchTokens(bool silent, Action<int> callback);

void RequestPermissions(Action<SignInStatus> callback, string[] scopes);

bool HasPermissions(string[] scopes);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class HelperFragment extends Fragment
static final int RC_INBOX_UI = 9007;
static final int RC_SHOW_WAITING_ROOM_UI = 9008;
static final int RC_SHOW_INVITATION_INBOX_UI = 9009;
static final int RC_SHOW_REQUEST_PERMISSIONS_UI = 9010;

// Pending token request. There can be only one outstanding request at a
// time.
Expand Down Expand Up @@ -204,7 +205,7 @@ public static Task<ShowInvitationInboxUIRequest.Result> showInvitationInboxUI(Ac
return request.getTask();
}

public static Task<InboxUiRequest.Result> showInboxUi(Activity parentActivity){
public static Task<InboxUiRequest.Result> showInboxUi(Activity parentActivity) {
InboxUiRequest request = new InboxUiRequest();

if(!HelperFragment.startRequest(parentActivity, request)) {
Expand All @@ -214,13 +215,26 @@ public static Task<InboxUiRequest.Result> showInboxUi(Activity parentActivity){
return request.getTask();
}

public static boolean hasPermissions(Activity parentActivity, String[] scopeUris) {
public static Task<GoogleSignInAccount> showRequestPermissionsUi(Activity parentActivity, String[] scopes) {
RequestPermissionsRequest request = new RequestPermissionsRequest(toScopeList(scopes));

if(!HelperFragment.startRequest(parentActivity, request)) {
request.setFailure(CommonUIStatus.UI_BUSY);
}

return request.getTask();
}

public static boolean hasPermissions(Activity parentActivity, String[] scopes) {
return GoogleSignIn.hasPermissions(getAccount(parentActivity), toScopeList(scopes));
}

private static Scope[] toScopeList(String[] scopeUris) {
Scope[] scopes = new Scope[scopeUris.length];
for (int i = 0; i < scopeUris.length; i++) {
scopes[i] = new Scope(scopeUris[i]);
}

return GoogleSignIn.hasPermissions(getAccount(parentActivity), scopes);
return scopes;
}

public static void signOut(Activity activity) {
Expand Down
Loading

0 comments on commit ad1c7c0

Please sign in to comment.