//
// Copyright (C) 2014 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#if UNITY_ANDROID
namespace GooglePlayGames.BasicApi
{
using GooglePlayGames.OurUtils;
using System.Collections.Generic;
using System.Linq;
///
/// Provides configuration for . If you wish to use either Saved
/// Games or Cloud Save, you must create an instance of this class with those features enabled.
/// Note that Cloud Save is deprecated, and is not available for new games. You should strongly
/// favor Saved Game.
///
public struct PlayGamesClientConfiguration
{
///
/// The default configuration.
///
public static readonly PlayGamesClientConfiguration DefaultConfiguration =
new Builder()
.Build();
///
/// Flag indicating to enable saved games API.
///
private readonly bool mEnableSavedGames;
///
/// Array of scopes to be requested from user. None is considered as 'games_lite'.
///
private readonly string[] mScopes;
///
/// The flag to indicate a server auth code should be requested when authenticating.
///
private readonly bool mRequestAuthCode;
///
/// The flag indicating the auth code should be refresh, causing re-consent and issuing a new refresh token.
///
private readonly bool mForceRefresh;
///
/// The flag indicating popup UIs should be hidden.
///
private readonly bool mHidePopups;
///
/// The flag indicating the email address should returned when authenticating.
///
private readonly bool mRequestEmail;
///
/// The flag indicating the id token should be returned when authenticating.
///
private readonly bool mRequestIdToken;
///
/// The account name to attempt to use when signing in. Null indicates use the default.
///
private readonly string mAccountName;
///
/// Initializes a new instance of the struct.
///
/// Builder for this configuration.
private PlayGamesClientConfiguration(Builder builder)
{
this.mEnableSavedGames = builder.HasEnableSaveGames();
this.mScopes = builder.getScopes();
this.mHidePopups = builder.IsHidingPopups();
this.mRequestAuthCode = builder.IsRequestingAuthCode();
this.mForceRefresh = builder.IsForcingRefresh();
this.mRequestEmail = builder.IsRequestingEmail();
this.mRequestIdToken = builder.IsRequestingIdToken();
this.mAccountName = builder.GetAccountName();
}
///
/// Gets a value indicating whether this
/// enable saved games.
///
/// true if enable saved games; otherwise, false.
public bool EnableSavedGames
{
get { return mEnableSavedGames; }
}
public bool IsHidingPopups
{
get { return mHidePopups; }
}
public bool IsRequestingAuthCode
{
get { return mRequestAuthCode; }
}
public bool IsForcingRefresh
{
get { return mForceRefresh; }
}
public bool IsRequestingEmail
{
get { return mRequestEmail; }
}
public bool IsRequestingIdToken
{
get { return mRequestIdToken; }
}
public string AccountName
{
get { return mAccountName; }
}
///
/// Gets a array of scopes to be requested from the user.
///
/// String array of scopes.
public string[] Scopes
{
get { return mScopes; }
}
public static bool operator ==(PlayGamesClientConfiguration c1, PlayGamesClientConfiguration c2)
{
if (c1.EnableSavedGames != c2.EnableSavedGames ||
c1.IsForcingRefresh != c2.IsForcingRefresh ||
c1.IsHidingPopups != c2.IsHidingPopups ||
c1.IsRequestingEmail != c2.IsRequestingEmail ||
c1.IsRequestingAuthCode != c2.IsRequestingAuthCode ||
!c1.Scopes.SequenceEqual(c2.Scopes) ||
c1.AccountName != c2.AccountName)
{
return false;
}
return true;
}
public static bool operator !=(PlayGamesClientConfiguration c1, PlayGamesClientConfiguration c2)
{
return !(c1 == c2);
}
///
/// Builder class for the configuration.
///
public class Builder
{
///
/// The flag to enable save games. Default is false.
///
private bool mEnableSaveGames = false;
///
/// The scopes to request from the user. Default is none.
///
private List mScopes = null;
///
/// The flag indicating that popup UI should be hidden.
///
private bool mHidePopups = false;
///
/// The flag to indicate a server auth code should be requested when authenticating.
///
private bool mRequestAuthCode = false;
///
/// The flag indicating the auth code should be refresh, causing re-consent and issuing a new refresh token.
///
private bool mForceRefresh = false;
///
/// The flag indicating the email address should returned when authenticating.
///
private bool mRequestEmail = false;
///
/// The flag indicating the id token should be returned when authenticating.
///
private bool mRequestIdToken = false;
///
/// The account name to use as a default when authenticating.
///
///
/// This is only used when requesting auth code or id token.
///
private string mAccountName = null;
///
/// Enables the saved games.
///
/// The builder.
public Builder EnableSavedGames()
{
mEnableSaveGames = true;
return this;
}
///
/// Enables hiding popups. This is recommended for VR apps.
///
/// The hide popups.
public Builder EnableHidePopups()
{
mHidePopups = true;
return this;
}
public Builder RequestServerAuthCode(bool forceRefresh)
{
mRequestAuthCode = true;
mForceRefresh = forceRefresh;
return this;
}
public Builder RequestEmail()
{
mRequestEmail = true;
return this;
}
public Builder RequestIdToken()
{
mRequestIdToken = true;
return this;
}
public Builder SetAccountName(string accountName)
{
mAccountName = accountName;
return this;
}
///
/// Requests an Oauth scope from the user.
///
///
/// Not setting one will default to 'games_lite' and will not show a consent
/// dialog to the user. Valid examples are 'profile' and 'email'.
/// Full list: https://developers.google.com/identity/protocols/googlescopes
/// To exchange the auth code with an id_token (or user id) on your server,
/// you must add at least one scope.
///
/// The builder.
public Builder AddOauthScope(string scope)
{
if (mScopes == null) mScopes = new List();
mScopes.Add(scope);
return this;
}
///
/// Build this instance.
///
/// the client configuration instance
public PlayGamesClientConfiguration Build()
{
return new PlayGamesClientConfiguration(this);
}
///
/// Determines whether this instance has enable save games.
///
/// true if this instance has enable save games; otherwise, false.
internal bool HasEnableSaveGames()
{
return mEnableSaveGames;
}
internal bool IsRequestingAuthCode()
{
return mRequestAuthCode;
}
internal bool IsHidingPopups()
{
return mHidePopups;
}
internal bool IsForcingRefresh()
{
return mForceRefresh;
}
internal bool IsRequestingEmail()
{
return mRequestEmail;
}
internal bool IsRequestingIdToken()
{
return mRequestIdToken;
}
internal string GetAccountName()
{
return mAccountName;
}
///
/// Gets the Oauth scopes to be requested from the user.
///
/// String array of scopes.
internal string[] getScopes()
{
return mScopes == null ? new string[0] : mScopes.ToArray();
}
}
public override int GetHashCode()
{
unchecked
{
int hash = 17;
hash = hash * 31 + EnableSavedGames.GetHashCode();
hash = hash * 31 + IsForcingRefresh.GetHashCode();
hash = hash * 31 + IsHidingPopups.GetHashCode();
hash = hash * 31 + IsRequestingEmail.GetHashCode();
hash = hash * 31 + IsRequestingAuthCode.GetHashCode();
hash = hash * 31 + Scopes.GetHashCode();
hash = hash * 31 + AccountName.GetHashCode();
return hash;
}
}
public override bool Equals(object obj)
{
return this == (PlayGamesClientConfiguration) obj;
}
}
}
#endif