Ver código fonte

谷歌登录

徐勤民 3 meses atrás
pai
commit
5ade899336

+ 10 - 1
app/build.gradle

@@ -65,7 +65,12 @@ android {
     }
     namespace 'com.xuqinmin.android.app'
 }
-
+configurations.all {
+    resolutionStrategy {
+        force "androidx.lifecycle:lifecycle-viewmodel:2.5.1"
+        force "androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1"
+    }
+}
 dependencies {
     implementation fileTree(dir: "libs", include: ["*.jar", "*.aar"])
     implementation project(path: ':core')
@@ -114,4 +119,8 @@ dependencies {
     implementation("com.android.billingclient:billing:6.2.0")
 //    implementation("com.android.billingclient:billing-ktx:6.2.0")
 
+   implementation("com.google.android.gms:play-services-base:18.5.0")
+
+    implementation("com.google.android.gms:play-services-auth:21.2.0")
+
 }

+ 68 - 0
app/google-services.json

@@ -0,0 +1,68 @@
+{
+  "project_info": {
+    "project_number": "981878503916",
+    "project_id": "chatlive-6f73e",
+    "storage_bucket": "chatlive-6f73e.appspot.com"
+  },
+  "client": [
+    {
+      "client_info": {
+        "mobilesdk_app_id": "1:981878503916:android:4a1f95a93bc4a29202a8c9",
+        "android_client_info": {
+          "package_name": "com.chat.live"
+        }
+      },
+      "oauth_client": [
+        {
+          "client_id": "981878503916-k8qii5tcfvs8gph4lscrb5tr729ll6eh.apps.googleusercontent.com",
+          "client_type": 3
+        }
+      ],
+      "api_key": [
+        {
+          "current_key": "AIzaSyAQZFkQgBI_sJBKYAzxZrPlJFFvfymI2F4"
+        }
+      ],
+      "services": {
+        "appinvite_service": {
+          "other_platform_oauth_client": [
+            {
+              "client_id": "981878503916-k8qii5tcfvs8gph4lscrb5tr729ll6eh.apps.googleusercontent.com",
+              "client_type": 3
+            }
+          ]
+        }
+      }
+    },
+    {
+      "client_info": {
+        "mobilesdk_app_id": "1:981878503916:android:27d5fec1614a6e0d02a8c9",
+        "android_client_info": {
+          "package_name": "com.chatlive.app"
+        }
+      },
+      "oauth_client": [
+        {
+          "client_id": "981878503916-k8qii5tcfvs8gph4lscrb5tr729ll6eh.apps.googleusercontent.com",
+          "client_type": 3
+        }
+      ],
+      "api_key": [
+        {
+          "current_key": "AIzaSyAQZFkQgBI_sJBKYAzxZrPlJFFvfymI2F4"
+        }
+      ],
+      "services": {
+        "appinvite_service": {
+          "other_platform_oauth_client": [
+            {
+              "client_id": "981878503916-k8qii5tcfvs8gph4lscrb5tr729ll6eh.apps.googleusercontent.com",
+              "client_type": 3
+            }
+          ]
+        }
+      }
+    }
+  ],
+  "configuration_version": "1"
+}

+ 1 - 0
app/src/main/java/com/xuqinmin/android/app/common/SharedPreferencesConfigs.kt

@@ -5,4 +5,5 @@ const val SHARE_RISK_PURE = "share_risk_pure"
 
 const val MCC = "mcc"
 const val DEVICE_ID = "deviceUuid"
+const val GOOGLE_ID = "googleId"
 const val USER_INFO = "user_info"

+ 62 - 6
app/src/main/java/com/xuqinmin/android/app/ui/WelcomeActivity.kt

@@ -1,17 +1,25 @@
 package com.xuqinmin.android.app.ui
 
+import android.R.attr
 import android.content.Intent
 import android.os.Bundle
 import android.telephony.TelephonyManager
+import android.util.Log
 import androidx.activity.viewModels
 import com.bigkoo.alertview.AlertView
+import com.google.android.gms.auth.api.signin.GoogleSignIn
+import com.google.android.gms.auth.api.signin.GoogleSignInAccount
+import com.google.android.gms.auth.api.signin.GoogleSignInClient
+import com.google.android.gms.auth.api.signin.GoogleSignInOptions
+import com.google.android.gms.common.api.ApiException
+import com.google.android.gms.tasks.Task
 import com.xuqinmin.android.app.MyApplication
 import com.xuqinmin.android.app.R
 import com.xuqinmin.android.app.common.DEVICE_ID
+import com.xuqinmin.android.app.common.GOOGLE_ID
 import com.xuqinmin.android.app.common.MCC
 import com.xuqinmin.android.app.databinding.ActivityWelcomeBinding
 import com.xuqinmin.android.app.model.DataLogin
-import com.xuqinmin.android.app.model.ModelHideIn
 import com.xuqinmin.android.app.viewmodel.WelcomeVM
 import com.xuqm.base.common.AppManager
 import com.xuqm.base.common.DeviceUuidFactory
@@ -19,17 +27,17 @@ import com.xuqm.base.common.GsonImplHelp
 import com.xuqm.base.common.SHARE_LOGIN_OBJ
 import com.xuqm.base.common.SHARE_UESR_ID
 import com.xuqm.base.common.SHARE_UESR_TOKEN
+import com.xuqm.base.common.ToolsHelper
 import com.xuqm.base.dialog.loading.LoadingDialog
 import com.xuqm.base.extensions.Fonts
-import com.xuqm.base.extensions.getIntForPreferences
 import com.xuqm.base.extensions.getStringForPreferences
+import com.xuqm.base.extensions.loge
 import com.xuqm.base.extensions.putInt
 import com.xuqm.base.extensions.putString
 import com.xuqm.base.extensions.setFont
 import com.xuqm.base.extensions.showMessage
 import com.xuqm.base.ui.BaseActivity
 import com.xuqm.base.web.XWebViewHelper
-import kotlin.random.Random
 
 
 class WelcomeActivity : BaseActivity<ActivityWelcomeBinding>() {
@@ -38,10 +46,18 @@ class WelcomeActivity : BaseActivity<ActivityWelcomeBinding>() {
     override fun fullscreen(): Boolean = true
 
     private val vm: WelcomeVM by viewModels()
-
+    private lateinit var mGoogleSignInClient: GoogleSignInClient
     override fun initView(savedInstanceState: Bundle?) {
         super.initView(savedInstanceState)
         AppManager.getInstance().logout()
+
+        val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
+            .requestEmail()
+            .build()
+        mGoogleSignInClient = GoogleSignIn.getClient(this, gso)
+        val account = GoogleSignIn.getLastSignedInAccount(this)
+        account?.loge()
+
         binding.world1.setFont(mContext, Fonts.Bold)
         binding.world2.setFont(mContext, Fonts.Bold)
         binding.loginGuest.setFont(mContext, Fonts.Bold)
@@ -71,6 +87,7 @@ class WelcomeActivity : BaseActivity<ActivityWelcomeBinding>() {
         }
         binding.loginGuest.setOnClickListener {
             LoadingDialog.showDialog("")
+            putString(GOOGLE_ID,"")
             vm.thirdLogin(
                 DataLogin(
                     getStringForPreferences(DEVICE_ID),
@@ -80,9 +97,47 @@ class WelcomeActivity : BaseActivity<ActivityWelcomeBinding>() {
             )
         }
         binding.loginGoogle.setOnClickListener {
+            val signInIntent = mGoogleSignInClient.signInIntent
+            startActivityForResult(signInIntent, 10010)
+        }
+    }
+
+    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
+        super.onActivityResult(requestCode, resultCode, data)
+        if (requestCode === 10010) {
+            // The Task returned from this call is always completed, no need to attach
+            // a listener.
+            val task: Task<GoogleSignInAccount> =
+                GoogleSignIn.getSignedInAccountFromIntent(data)
+            handleSignInResult(task)
         }
     }
+    private fun handleSignInResult(completedTask: Task<GoogleSignInAccount>) {
+        try {
+            val account = completedTask.getResult(ApiException::class.java)
+            account.loge()
+            account.id?.let {
+                putString(GOOGLE_ID,it)
+                LoadingDialog.showDialog("")
+                vm.thirdLogin(
+                    DataLogin(
+                        getStringForPreferences(DEVICE_ID),
+                        it,
+                        2
+                    )
+                )
+            }
 
+            // Signed in successfully, show authenticated UI.
+//            updateUI(account)
+        } catch (e: ApiException) {
+            // The ApiException status code indicates the detailed failure reason.
+            // Please refer to the GoogleSignInStatusCodes class reference for more information.
+            Log.w(TAG, "signInResult:failed code=" + e.statusCode)
+//            updateUI(null)
+            "Failed to obtain account information".showMessage()
+        }
+    }
     override fun initData() {
         super.initData()
         val telManager = getSystemService(TELEPHONY_SERVICE) as TelephonyManager
@@ -134,11 +189,12 @@ class WelcomeActivity : BaseActivity<ActivityWelcomeBinding>() {
                 val stringForPreferences = getStringForPreferences(SHARE_LOGIN_OBJ)
                 if (stringForPreferences.isNotEmpty()) {
                     LoadingDialog.showDialog("")
+                    val googleId = getStringForPreferences(GOOGLE_ID)
                     vm.thirdLogin(
                         DataLogin(
                             getStringForPreferences(DEVICE_ID),
-                            getStringForPreferences(DEVICE_ID),
-                            0
+                            if (ToolsHelper.isNull(googleId))getStringForPreferences(DEVICE_ID) else googleId,
+                            if (ToolsHelper.isNull(googleId))0 else 2
                         )
                     )
                 }

+ 1 - 1
config.gradle

@@ -14,7 +14,7 @@ ext {
 
             paging     : '2.1.1',
             room       : '2.2.3',
-            lifecycle  : '2.2.0',
+            lifecycle  : '2.5.1',
 
             retrofit   : '2.4.0',
             glide      : '4.10.0',