添加google oauth

This commit is contained in:
cebgcontract 2022-09-08 15:17:21 +08:00
parent a80cb6c08d
commit 8db5491391
15 changed files with 321 additions and 98 deletions

3
.idea/.gitignore generated vendored
View File

@ -1,3 +0,0 @@
# Default ignored files
/shelf/
/workspace.xml

View File

@ -1,12 +0,0 @@
<component name="ProjectDictionaryState">
<dictionary name="zhl">
<words>
<w>errcode</w>
<w>errmsg</w>
<w>jcfw</w>
<w>jcwallet</w>
<w>ljava</w>
<w>walletconnect</w>
</words>
</dictionary>
</component>

3
.idea/gradle.xml generated
View File

@ -1,15 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
<option name="testRunner" value="GRADLE" />
<option name="disableWrapperSourceDistributionNotification" value="true" />
<option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleHome" value="/usr/local/Cellar/gradle/3.5/libexec" />
<option name="gradleJvm" value="1.8" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />

2
.idea/misc.xml generated
View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="Android Studio default JDK" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

3
.idea/vcs.xml generated
View File

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="$PROJECT_DIR$/../../cocos/cocos2d-x" vcs="Git" />
<mapping directory="" vcs="Git" />
</component>
</project>

35
.vscode/c_cpp_properties.json vendored Normal file
View File

@ -0,0 +1,35 @@
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**",
"/Users/zhl/Documents/workspace/cocos/cocos2d-x/**"
],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX11.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/local/opt/llvm/bin/clang",
"cStandard": "c17",
"cppStandard": "c++14",
"intelliSenseMode": "macos-clang-x64"
},
{
"name": "wallet",
"includePath": [
"${workspaceFolder}/**",
"/Users/zhl/Documents/workspace/cocos/cocos2d-x/**"
],
"defines": [],
"macFrameworkPath": [
"/Library/Developer/CommandLineTools/SDKs/MacOSX11.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/local/opt/llvm/bin/clang",
"cStandard": "c17",
"cppStandard": "c++14",
"intelliSenseMode": "macos-clang-x64"
}
],
"version": 4
}

90
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,90 @@
{
"java.configuration.updateBuildConfiguration": "interactive",
"files.associations": {
"*.cjson": "jsonc",
"*.wxss": "css",
"*.wxs": "javascript",
"__bit_reference": "cpp",
"__config": "cpp",
"__debug": "cpp",
"__errc": "cpp",
"__functional_base": "cpp",
"__hash_table": "cpp",
"__locale": "cpp",
"__mutex_base": "cpp",
"__node_handle": "cpp",
"__nullptr": "cpp",
"__split_buffer": "cpp",
"__string": "cpp",
"__threading_support": "cpp",
"__tree": "cpp",
"__tuple": "cpp",
"algorithm": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"chrono": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"codecvt": "cpp",
"compare": "cpp",
"complex": "cpp",
"concepts": "cpp",
"condition_variable": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"exception": "cpp",
"fstream": "cpp",
"functional": "cpp",
"future": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"iterator": "cpp",
"limits": "cpp",
"list": "cpp",
"locale": "cpp",
"map": "cpp",
"memory": "cpp",
"mutex": "cpp",
"new": "cpp",
"numeric": "cpp",
"optional": "cpp",
"ostream": "cpp",
"queue": "cpp",
"random": "cpp",
"ratio": "cpp",
"regex": "cpp",
"set": "cpp",
"sstream": "cpp",
"stack": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"thread": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"utility": "cpp",
"variant": "cpp",
"vector": "cpp",
"__bits": "cpp"
}
}

124
.vscode/tasks.json vendored
View File

@ -3,75 +3,69 @@
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "AppSignKey",
"type": "shell",
"command": "keytool -genkey -v -keystore my-release-key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias my-alias",
"windows": {
// "command": "${workspaceRoot}\\shell.bat first"
"command": "cd gitoscandroid; del my-release-key.jks; keytool -genkey -v -keystore my-release-key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias my-alias"
// keytool -genkey -v -keystore release.keystore -storepass android -alias androiddebugkey -keypass android -keyalg RSA -keysize 2048 -validity 10000
// keytool -genkey -v -keystore debug.keystore -storepass android -alias androiddebugkey -keypass android -keyalg RSA -keysize 2048 -validity 10000
},
"group": "test",
"presentation": {
"reveal": "always",
// "panel": "new"
}
{
"label": "AppSignKey",
"type": "shell",
"command": "keytool -genkey -v -keystore my-release-key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias my-alias",
"windows": {
"command": "cd gitoscandroid; del my-release-key.jks; keytool -genkey -v -keystore my-release-key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias my-alias"
},
{
"label": "CleanApp",
"type": "shell",
"command": "${workspaceRoot}/gradlew clean",
"windows": {
// "command": "${workspaceRoot}\\shell.bat first"
"command": "${workspaceRoot}\\gradlew.bat clean "
},
"group": "test",
"presentation": {
"reveal": "always",
// "panel": "new"
}
"group": "test",
"presentation": {
"reveal": "always"
}
},
{
"label": "CleanApp",
"type": "shell",
"command": "${workspaceRoot}/gradlew clean",
"windows": {
"command": "${workspaceRoot}\\gradlew.bat clean "
},
{
"label": "BuildDebugApp",
"type": "shell",
"command": "${workspaceRoot}/gradlew assembleDebug ",
"windows": {
// "command": "${workspaceRoot}\\shell.bat first"
"command": "${workspaceRoot}\\gradlew.bat assembleDebug "
},
"group": "test",
"presentation": {
"reveal": "always",
// "panel": "new"
}
"group": "test",
"presentation": {
"reveal": "always"
}
},
{
"label": "BuildDebugApp",
"type": "shell",
"command": "${workspaceRoot}/gradlew assembleDebug ",
"windows": {
"command": "${workspaceRoot}\\gradlew.bat assembleDebug "
},
{
"label": "InstallDebugApp",
"type": "shell",
"command": "${workspaceRoot}/gradlew installDebug ",
"windows": {
"command": "${workspaceRoot}\\gradlew.bat installDebug "
},
"group": "test",
"presentation": {
"reveal": "always",
// "panel": "new"
}
"group": {
"kind": "build",
"isDefault": true
},
{
"label": "InstallReleaseApp",
"type": "shell",
"command": "${workspaceRoot}/gradlew installRelease ",
"windows": {
"command": "${workspaceRoot}\\gradlew.bat installRelease "
},
"group": "test",
"presentation": {
"reveal": "always",
// "panel": "new"
}
"presentation": {
"reveal": "always"
},
"problemMatcher": []
},
{
"label": "InstallDebugApp",
"type": "shell",
"command": "${workspaceRoot}/gradlew installDebug ",
"windows": {
"command": "${workspaceRoot}\\gradlew.bat installDebug "
},
"group": "test",
"presentation": {
"reveal": "always"
}
},
{
"label": "InstallReleaseApp",
"type": "shell",
"command": "${workspaceRoot}/gradlew installRelease ",
"windows": {
"command": "${workspaceRoot}\\gradlew.bat installRelease "
},
"group": "test",
"presentation": {
"reveal": "always"
}
}
]
}

View File

@ -1,8 +1,6 @@
import org.apache.tools.ant.taskdefs.condition.Os
plugins {
id 'com.android.application'
}
apply plugin: 'com.android.application'
android {
compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger()
@ -109,6 +107,8 @@ dependencies {
implementation "androidx.appcompat:appcompat:1.0.2"
implementation "androidx.biometric:biometric:1.1.0"
implementation files('../libs/unity-classes.jar')
implementation 'com.google.android.gms:play-services-auth:20.3.0'
// implementation 'com.xm.permissions:XmPermissions:1.0.1'
implementation 'pub.devrel:easypermissions:3.0.0'

View File

@ -4,6 +4,7 @@ import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
@ -15,6 +16,15 @@ import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Window;
import com.google.android.gms.auth.api.identity.BeginSignInRequest;
import com.google.android.gms.auth.api.identity.BeginSignInResult;
import com.google.android.gms.auth.api.identity.Identity;
import com.google.android.gms.auth.api.identity.SignInClient;
import com.google.android.gms.auth.api.identity.SignInCredential;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.common.api.CommonStatusCodes;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.jc.jcfw.JcSDK;
import com.king.zxing.CameraScan;
import com.king.zxing.CaptureActivity;
@ -27,6 +37,7 @@ import org.cocos2dx.lib.CocosJSHelper;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityOptionsCompat;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;
@ -45,6 +56,7 @@ public class MainActivity extends Activity
public static final int REQUEST_CODE_SCAN = 0X01;
public static final int REQUEST_CODE_PHOTO = 0X02;
public static final int REQ_ONE_TAP = 0X03;
public static final int RC_CAMERA = 0X01;
@ -52,6 +64,9 @@ public class MainActivity extends Activity
private String title;
private String funId;
private SignInClient oneTapClient;
private BeginSignInRequest signInRequest;
protected String updateUnityCommandLineArguments(String cmdLine) {
return cmdLine;
}
@ -73,6 +88,22 @@ public class MainActivity extends Activity
app = this;
Cocos2dxHelper.init(this);
CocosJSHelper.initJSEnv(getApplicationContext());
oneTapClient = Identity.getSignInClient(this);
signInRequest = BeginSignInRequest.builder()
.setPasswordRequestOptions(BeginSignInRequest.PasswordRequestOptions.builder()
.setSupported(true)
.build())
.setGoogleIdTokenRequestOptions(BeginSignInRequest.GoogleIdTokenRequestOptions.builder()
.setSupported(true)
// Your server's client ID, not your Android client ID.
.setServerClientId(getString(R.string.default_web_client_id))
// Only show accounts previously used to sign in.
.setFilterByAuthorizedAccounts(true)
.build())
// Automatically sign in when exactly one credential is retrieved.
.setAutoSelectEnabled(true)
.build();
}
@Override
@ -90,6 +121,39 @@ public class MainActivity extends Activity
case REQUEST_CODE_PHOTO:
parsePhoto(data);
break;
case REQ_ONE_TAP:
try {
SignInCredential credential = oneTapClient.getSignInCredentialFromIntent(data);
String idToken = credential.getGoogleIdToken();
String username = credential.getId();
String password = credential.getPassword();
if (idToken != null) {
// Got an ID token from Google. Use it to authenticate
// with your backend.
Log.d(TAG, "Got ID token: " + idToken);
} else if (password != null) {
// Got a saved username and password. Use them to authenticate
// with your backend.
Log.d(TAG, "Got password.");
}
} catch (ApiException e) {
switch (e.getStatusCode()) {
case CommonStatusCodes.CANCELED:
Log.d(TAG, "One-tap dialog was closed.");
// Don't re-prompt the user.
// showOneTapUI = false;
break;
case CommonStatusCodes.NETWORK_ERROR:
Log.d(TAG, "One-tap encountered a network error.");
// Try again or just ignore.
break;
default:
Log.d(TAG, "Couldn't get credential from result."
+ e.getLocalizedMessage());
break;
}
}
break;
}
} else {
JcSDK.csCallback(funId, "{errcode: 0, errmsg: 'cancel'}");
@ -308,5 +372,35 @@ public class MainActivity extends Activity
checkCameraPermissions();
}
// end of qrcode
public void signWithGoogle(String funId) {
oneTapClient.beginSignIn(signInRequest)
.addOnSuccessListener(this, new OnSuccessListener<BeginSignInResult>() {
@Override
public void onSuccess(BeginSignInResult result) {
try {
startIntentSenderForResult(
result.getPendingIntent().getIntentSender(), REQ_ONE_TAP,
null, 0, 0, 0);
} catch (IntentSender.SendIntentException e) {
Log.e(TAG, "Couldn't start One Tap UI: " + e.getLocalizedMessage());
}
}
})
.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// No saved credentials found. Launch the One Tap sign-up flow, or
// do nothing and continue presenting the signed-out UI.
Log.d(TAG, e.getLocalizedMessage());
}
});
}
public void signOutGoogle(String funId) {
oneTapClient.signOut();
}
}

View File

@ -8,6 +8,7 @@ import android.util.Log;
import com.fitchgc.headlesscocos.MainActivity;
import org.cocos2dx.lib.CocosJSHelper;
import org.cocos2dx.lib.Cocos2dxJavascriptJavaBridge;
public class JcSDK {
private static final String TAG = JcSDK.class.getSimpleName();
@ -25,7 +26,7 @@ public class JcSDK {
public static void initWallet(String password) {
Log.i(TAG, "call init wallet from unity with password: " + password);
CocosJSHelper.initWallet(password);
commonCB.stringCallback("", "wallet init success");
commonCB.nativeCallback("", "wallet init success", 0);
}
/**
@ -34,13 +35,11 @@ public class JcSDK {
* @param msg
*/
public static void csCallback(String funId, String msg) {
commonCB.stringCallback(funId, msg);
// if (funId.indexOf("jscall-") == 0) {
// funId = funId.replace("jscall-", "");
// CocosJSHelper.jniCallback(funId, msg);
// } else {
// commonCB.stringCallback(funId, msg);
// }
if (funId.indexOf("js_") == 0) {
commonCB.nativeCallback(funId, msg, 1);
} else {
commonCB.nativeCallback(funId, msg, 0);
}
}
/**
@ -65,4 +64,12 @@ public class JcSDK {
public static void scanQRCode(String funid, String title) {
MainActivity.app.showQRScan(funid, title);
}
public static void signWithGoogle(String funid) {
MainActivity.app.signWithGoogle(funid);
}
public static void signOutGoogle(String funid) {
MainActivity.app.signOutGoogle(funid);
}
}

View File

@ -1,5 +1,5 @@
package com.jc.jcfw;
public interface UnityCallback {
public void stringCallback(String funId, String str);
public void nativeCallback(String funId, String str, int type);
}

View File

@ -238,6 +238,24 @@ function scanQRCode(funId, title) {
return JSON.stringify({errcode: 1, errmsg: err});
}
}
function signWithGoogle(funId) {
try {
jsb.signWithGoogle(funId);
} catch(err) {
return JSON.stringify({errcode: 1, errmsg: err});
}
}
function signOutGoogle(funId) {
try {
jsb.signOutGoogle(funId);
return JSON.stringify({errcode: 0, data: 'success'});
} catch(err) {
return JSON.stringify({errcode: 1, errmsg: err});
}
}
//function toWalletJNI(funId, url) {
// try {
// jsb.toWallet(url);

View File

@ -16,3 +16,6 @@ window.jumpToWallet = function(url) {
// url || 'wc://'
// )
}
function nativeCallback(funId, msg) {
console.log(`native call back:: funid: ${funId} msg: ${msg}`)
}

View File

@ -2,4 +2,5 @@
<string name="app_name">HeadlessCocos</string>
<string name="game_view_content_description">Game view</string>
<string name="permission_camera">Scan QRCode need camera permission</string>
<string name="default_web_client_id">165555585193-ud80sst45po348ohec2h33t2m6mjnlt0.apps.googleusercontent.com</string>
</resources>