mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 08:02:39 +08:00
Implementation of google play auto login, achievements, PGS Recall API (#18416)
This commit is contained in:
+1
-1
@@ -1,7 +1,7 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2024 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos2d-x.org
|
||||
https://www.cocos.com/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2025 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
package google.play;
|
||||
|
||||
import static androidx.core.app.ActivityCompat.startActivityForResult;
|
||||
|
||||
import android.content.Intent;
|
||||
import androidx.annotation.IntRange;
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.cocos.lib.GlobalObject;
|
||||
import com.google.android.gms.games.AchievementsClient;
|
||||
import com.google.android.gms.games.AnnotatedData;
|
||||
import com.google.android.gms.games.PlayGames;
|
||||
import com.google.android.gms.games.achievement.AchievementBuffer;
|
||||
import com.google.android.gms.tasks.OnSuccessListener;
|
||||
import com.google.android.gms.tasks.Task;
|
||||
|
||||
public final class AchievementsClientHelper {
|
||||
public static void showAchievements() {
|
||||
GlobalObject.runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
PlayGames.getAchievementsClient(GlobalObject.getActivity())
|
||||
.getAchievementsIntent()
|
||||
.addOnSuccessListener(new OnSuccessListener<Intent>() {
|
||||
@Override
|
||||
public void onSuccess(Intent intent) {
|
||||
GlobalObject.getActivity().startActivityForResult(intent, 100);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
public static int incrementImmediate(@NonNull String id, @IntRange(from = 0L) int numSteps) {
|
||||
AchievementsClient achievementsClient = PlayGames.getAchievementsClient(GlobalObject.getActivity());
|
||||
Task<Boolean> task = achievementsClient.incrementImmediate(id, numSteps);
|
||||
return TaskManager.putTask(task);
|
||||
}
|
||||
|
||||
public static int load(boolean forceReload) {
|
||||
AchievementsClient achievementsClient = PlayGames.getAchievementsClient(GlobalObject.getActivity());
|
||||
Task<AnnotatedData<AchievementBuffer>> task = achievementsClient.load(forceReload);
|
||||
return TaskManager.putTask(task);
|
||||
}
|
||||
public static int revealImmediate(@NonNull String id) {
|
||||
AchievementsClient achievementsClient = PlayGames.getAchievementsClient(GlobalObject.getActivity());
|
||||
Task<Void> task = achievementsClient.revealImmediate(id);
|
||||
return TaskManager.putTask(task);
|
||||
}
|
||||
|
||||
public static int setStepsImmediate(@NonNull String id, @IntRange(from = 0L) int numSteps) {
|
||||
AchievementsClient achievementsClient = PlayGames.getAchievementsClient(GlobalObject.getActivity());
|
||||
Task<Boolean> task = achievementsClient.setStepsImmediate(id, numSteps);
|
||||
return TaskManager.putTask(task);
|
||||
}
|
||||
|
||||
public static int unlockImmediate(@NonNull String id) {
|
||||
AchievementsClient achievementsClient = PlayGames.getAchievementsClient(GlobalObject.getActivity());
|
||||
Task<Void> task = achievementsClient.unlockImmediate(id);
|
||||
return TaskManager.putTask(task);
|
||||
}
|
||||
|
||||
public static void increment(@NonNull String id, @IntRange(from = 0L) int numSteps) {
|
||||
AchievementsClient achievementsClient = PlayGames.getAchievementsClient(GlobalObject.getActivity());
|
||||
achievementsClient.increment(id, numSteps);
|
||||
}
|
||||
|
||||
public static void reveal(@NonNull String id) {
|
||||
AchievementsClient achievementsClient = PlayGames.getAchievementsClient(GlobalObject.getActivity());
|
||||
achievementsClient.reveal(id);
|
||||
}
|
||||
|
||||
public static void setSteps(@NonNull String id, @IntRange(from = 0L) int numSteps) {
|
||||
AchievementsClient achievementsClient = PlayGames.getAchievementsClient(GlobalObject.getActivity());
|
||||
achievementsClient.setSteps(id, numSteps);
|
||||
}
|
||||
|
||||
public static void unlock(@NonNull String id) {
|
||||
AchievementsClient achievementsClient = PlayGames.getAchievementsClient(GlobalObject.getActivity());
|
||||
achievementsClient.unlock(id);
|
||||
}
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2025 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
package google.play;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.cocos.lib.GlobalObject;
|
||||
import com.google.android.gms.games.AuthenticationResult;
|
||||
import com.google.android.gms.games.GamesSignInClient;
|
||||
import com.google.android.gms.games.PlayGames;
|
||||
import com.google.android.gms.tasks.Task;
|
||||
|
||||
public final class GamesSignInClientHelper {
|
||||
public static int isAuthenticated() {
|
||||
GamesSignInClient gamesSignInClient = PlayGames.getGamesSignInClient(GlobalObject.getActivity());
|
||||
Task<AuthenticationResult> task = gamesSignInClient.isAuthenticated();
|
||||
return TaskManager.putTask(task);
|
||||
}
|
||||
|
||||
public static int requestServerSideAccess(@NonNull String serverClientId, boolean forceRefreshToken) {
|
||||
GamesSignInClient gamesSignInClient = PlayGames.getGamesSignInClient(GlobalObject.getActivity());
|
||||
Task<String> task = gamesSignInClient.requestServerSideAccess(serverClientId, forceRefreshToken);
|
||||
return TaskManager.putTask(task);
|
||||
}
|
||||
|
||||
public static int signIn() {
|
||||
GamesSignInClient gamesSignInClient = PlayGames.getGamesSignInClient(GlobalObject.getActivity());
|
||||
Task<AuthenticationResult> task = gamesSignInClient.signIn();
|
||||
return TaskManager.putTask(task);
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2025 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
package google.play;
|
||||
import com.cocos.lib.GlobalObject;
|
||||
import com.cocos.lib.R;
|
||||
import com.google.android.gms.games.PlayGamesSdk;
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
public final class PlayGamesSdkHelper {
|
||||
private static final String TAG = TaskManager.class.getSimpleName();
|
||||
public static void initialize() {
|
||||
String gameServicesProjectId = GlobalObject.getActivity().getResources().getString(R.string.game_services_project_id);
|
||||
if(gameServicesProjectId.isEmpty()) {
|
||||
Log.e(TAG, "The game server's project ID has not been configured");
|
||||
}
|
||||
PlayGamesSdk.initialize(GlobalObject.getActivity());
|
||||
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2025 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
package google.play;
|
||||
|
||||
import com.cocos.lib.GlobalObject;
|
||||
import com.google.android.gms.games.PlayGames;
|
||||
import com.google.android.gms.games.RecallAccess;
|
||||
import com.google.android.gms.games.RecallClient;
|
||||
import com.google.android.gms.tasks.Task;
|
||||
|
||||
public final class RecallClientHelper {
|
||||
public static int requestRecallAccess() {
|
||||
RecallClient recallClient = PlayGames.getRecallClient(GlobalObject.getActivity());
|
||||
Task<RecallAccess> task = recallClient.requestRecallAccess();
|
||||
return TaskManager.putTask(task);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,212 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2025 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
https://www.cocos.com/
|
||||
|
||||
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.
|
||||
****************************************************************************/
|
||||
|
||||
package google.play;
|
||||
import android.util.Log;
|
||||
|
||||
import com.cocos.lib.CocosHelper;
|
||||
import com.google.android.gms.tasks.Continuation;
|
||||
import com.google.android.gms.tasks.Task;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
public final class TaskManager {
|
||||
private static final String TAG = TaskManager.class.getSimpleName();
|
||||
private static Map<Integer, Task<?>> _taskMap = new HashMap<>();
|
||||
private static Map<Integer, Exception> _exceptionsMap = new HashMap<>();
|
||||
private static int _nextTaskId = 0;
|
||||
private static int _nextExceptionId = 0;
|
||||
|
||||
private static native void onTaskCanceledNative(int taskId, int listenerId);
|
||||
private static native void onTaskCompleteNative(int taskId, int listenerId, int nextTaskId);
|
||||
private static native void onTaskFailureNative(int taskId, int listenerId, Object obj, int nextExceptionId);
|
||||
private static native void onTaskSuccessNative(int taskId, int listenerId, Object obj);
|
||||
private static native Object onContinueWithNative(int taskId, int listenerId, int nextTaskId);
|
||||
|
||||
static int putTask(Task<?> task) {
|
||||
int taskId = ++_nextTaskId;
|
||||
_taskMap.put(taskId, task);
|
||||
return taskId;
|
||||
}
|
||||
|
||||
static void removeTask(int taskId) {
|
||||
if(_taskMap.containsKey(taskId)) {
|
||||
_taskMap.remove(taskId);
|
||||
}
|
||||
}
|
||||
|
||||
static void removeTaskException(int exceptionId) {
|
||||
if(_exceptionsMap.containsKey(exceptionId)) {
|
||||
_exceptionsMap.remove(exceptionId);
|
||||
}
|
||||
}
|
||||
|
||||
static void printExceptStackTrace(int exceptionId) {
|
||||
if(_exceptionsMap.containsKey(exceptionId)) {
|
||||
_exceptionsMap.get(exceptionId).printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
static Task<?> getTask(int taskId) {
|
||||
if(_taskMap.containsKey(taskId)) {
|
||||
return _taskMap.get(taskId);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
static Object getResult(int taskId) {
|
||||
if(_taskMap.containsKey(taskId)) {
|
||||
return _taskMap.get(taskId).getResult();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
static boolean isCanceled(int taskId) {
|
||||
if(_taskMap.containsKey(taskId)) {
|
||||
return _taskMap.get(taskId).isCanceled();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
static boolean isSuccessful(int taskId) {
|
||||
if(_taskMap.containsKey(taskId)) {
|
||||
return _taskMap.get(taskId).isSuccessful();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
static boolean isComplete(int taskId) {
|
||||
if(_taskMap.containsKey(taskId)) {
|
||||
return _taskMap.get(taskId).isComplete();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static int addOnCanceledListener(int taskId, int listenerId) {
|
||||
if(!_taskMap.containsKey(taskId)) {
|
||||
Log.e(TAG, "Called task id does not exist");
|
||||
return -1;
|
||||
}
|
||||
Task<?> newTask = _taskMap.get(taskId).addOnCanceledListener(()->{
|
||||
CocosHelper.runOnGameThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
onTaskCanceledNative(taskId, listenerId);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return putTask(newTask);
|
||||
}
|
||||
|
||||
static int addOnCompleteListener(int taskId, int listenerId) {
|
||||
if(!_taskMap.containsKey(taskId)) {
|
||||
Log.e(TAG, "Called task id does not exist");
|
||||
return -1;
|
||||
}
|
||||
Task<?> newTask = _taskMap.get(taskId).addOnCompleteListener((task)->{
|
||||
CocosHelper.runOnGameThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
int nextTaskId = putTask(task);
|
||||
onTaskCompleteNative(taskId, listenerId, nextTaskId);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return putTask(newTask);
|
||||
}
|
||||
static int addOnFailureListener(int taskId, int listenerId) {
|
||||
if(!_taskMap.containsKey(taskId)) {
|
||||
Log.e(TAG, "Called task id does not exist");
|
||||
return -1;
|
||||
}
|
||||
Task<?> newTask = _taskMap.get(taskId).addOnFailureListener((e)->{
|
||||
CocosHelper.runOnGameThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
int nextExceptionId = ++_nextExceptionId;
|
||||
_exceptionsMap.put(nextExceptionId, e);
|
||||
onTaskFailureNative(taskId, listenerId, e, nextExceptionId);
|
||||
}
|
||||
});
|
||||
});
|
||||
return putTask(newTask);
|
||||
}
|
||||
static int addOnSuccessListener(int taskId, int listenerId) {
|
||||
if(!_taskMap.containsKey(taskId)) {
|
||||
Log.e(TAG, "Called task id does not exist");
|
||||
return -1;
|
||||
}
|
||||
Task<?> newTask = _taskMap.get(taskId).addOnSuccessListener((result)->{
|
||||
CocosHelper.runOnGameThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
onTaskSuccessNative(taskId, listenerId, result);
|
||||
}
|
||||
});
|
||||
});
|
||||
return putTask(newTask);
|
||||
}
|
||||
|
||||
|
||||
private static <TResult, TContinuationResult> Task<TContinuationResult> continueWithTaskForAny(
|
||||
Task<TResult> task, final Continuation<TResult, TContinuationResult> continuation) {
|
||||
|
||||
return task.continueWith(new Continuation<TResult, TContinuationResult>() {
|
||||
@Override
|
||||
public TContinuationResult then(Task<TResult> task) throws Exception {
|
||||
return continuation.then(task);
|
||||
}
|
||||
});
|
||||
}
|
||||
static int continueWith(int taskId, int listenerId) {
|
||||
if(!_taskMap.containsKey(taskId)) {
|
||||
Log.e(TAG, "Called task id does not exist");
|
||||
return -1;
|
||||
}
|
||||
Task<Object> task = (Task<Object>)_taskMap.get(taskId);
|
||||
Task<?> newTask = task.continueWith(new Continuation<Object, Object>() {
|
||||
@Override
|
||||
public Object then(Task<Object> task) throws Exception {
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
final AtomicReference<Object> resultHolder = new AtomicReference<>();
|
||||
if (task.isSuccessful()) {
|
||||
CocosHelper.runOnGameThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
int calllbackTaskId = putTask(task);
|
||||
resultHolder.set(onContinueWithNative(taskId, listenerId, calllbackTaskId));
|
||||
latch.countDown();
|
||||
}
|
||||
});
|
||||
latch.await();
|
||||
} else {
|
||||
resultHolder.set(null);
|
||||
}
|
||||
return resultHolder.get();
|
||||
}
|
||||
});
|
||||
return putTask(newTask);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user