Skip to content
This repository has been archived by the owner on Mar 28, 2024. It is now read-only.

Commit

Permalink
增加Line平台
Browse files Browse the repository at this point in the history
  • Loading branch information
Yumi committed May 22, 2022
1 parent 7aa5f78 commit 86bc2ed
Show file tree
Hide file tree
Showing 23 changed files with 370 additions and 27 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

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 @@ -15,8 +15,8 @@ import io.github.devzwy.socialhelper.google.GoogleSocialConst.Companion.REQUEST_
import io.github.devzwy.socialhelper.utils.*


lateinit var onGoogleReqAuthError: (String) -> Unit
lateinit var onGoogleReqAuthSuccess: (GoogleSignInAccount) -> Unit
private lateinit var onGoogleReqAuthError: (String) -> Unit
private lateinit var onGoogleReqAuthSuccess: (GoogleSignInAccount) -> Unit

/**
* 在Google授权当前页面的Activity调用,调用处于[reqGoogleAuth]传入的activity保持一致 调用时请判断requestCode==[REQUEST_CODE_GOOGLE_AUTH]
Expand Down
1 change: 1 addition & 0 deletions Line/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
49 changes: 49 additions & 0 deletions Line/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
}

ext {
PUBLISH_GROUP_ID = GROUPID //项目包名
PUBLISH_ARTIFACT_ID = 'socialhelper.line' //项目名
PUBLISH_VERSION = VERSIONNAME //版本号
}

apply from: "../publish.gradle"

android {
compileSdk COMPILESDK

defaultConfig {
minSdk MINSDK
targetSdk TARGETSDK
versionCode VERSIONCODE
versionName VERSIONNAME

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}

dependencies {
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

implementation project(path: ':SocialHelper')
api ('com.linecorp.linesdk:linesdk:5.8.0')
}
Empty file added Line/consumer-rules.pro
Empty file.
21 changes: 21 additions & 0 deletions Line/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.github.devzwy.socialhelper.line

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("io.github.devzwy.socialhelper.line.test", appContext.packageName)
}
}
5 changes: 5 additions & 0 deletions Line/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="io.github.devzwy.socialhelper.line">
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.github.devzwy.socialhelper.line

class LineSocialConst {
companion object{
const val REQUEST_CODE_LINE_AUTH = 888
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package io.github.devzwy.socialhelper.line

import android.app.Activity
import android.content.Intent
import com.linecorp.linesdk.LineApiResponseCode
import com.linecorp.linesdk.api.LineApiClient
import com.linecorp.linesdk.api.LineApiClientBuilder
import com.linecorp.linesdk.auth.LineAuthenticationParams
import com.linecorp.linesdk.auth.LineLoginApi
import com.linecorp.linesdk.auth.LineLoginResult
import io.github.devzwy.socialhelper.SocialHelper
import io.github.devzwy.socialhelper.line.LineSocialConst.Companion.REQUEST_CODE_LINE_AUTH
import io.github.devzwy.socialhelper.utils.logE
import io.github.devzwy.socialhelper.utils.toJsonStr
import java.util.*

private lateinit var onLineReqAuthError: (String) -> Unit
private lateinit var onLineReqAuthSuccess: (LineLoginResult) -> Unit

/**
* 在Line授权当前页面的Activity调用,调用处于[reqLineAuth]传入的activity保持一致 调用时请判断requestCode==[REQUEST_CODE_LINE_AUTH]
*/
fun SocialHelper.onLineAuthResult(intent: Intent?) {
runCatching {
val result = LineLoginApi.getLoginResultFromIntent(intent)
when (result.responseCode) {
LineApiResponseCode.SUCCESS -> {
onLineReqAuthSuccess(result)
}
LineApiResponseCode.CANCEL -> {
//取消了操作
onLineReqAuthError(socialConfig.application.getString(com.linecorp.linesdk.R.string.common_cancel))
}
else -> {
onLineReqAuthError(result.errorData.message ?: result.toJsonStr())
}
}

}.onFailure {
it.printStackTrace()
onLineReqAuthError(
socialConfig.application.getString(
R.string.social_auth_fail_exception,
it.message ?: ""
)
)
}
}

/**
* 发起Line授权 授权成功会在传入Activity对应的onActivityResult中回调,请务必在该回调中调用[onLineAuthResult]
* [activity] Line SDK内部需要
* [onError] 授权失败的回调
* [onSuccess] 授权成功的回调 成功时会回传LineLoginResult
*/
fun SocialHelper.reqLineAuth(
activity: Activity,
onError: (String) -> Unit,
onSuccess: (LineLoginResult) -> Unit
) {
onLineReqAuthError = onError
onLineReqAuthSuccess = onSuccess

kotlin.runCatching {
activity.startActivityForResult(
LineLoginApi.getLoginIntent(
activity,
socialConfig.lineAppId,
LineAuthenticationParams.Builder()
.scopes(Arrays.asList(com.linecorp.linesdk.Scope.PROFILE))
.build()
), REQUEST_CODE_LINE_AUTH
)
}.onFailure {
it.localizedMessage?.logE()
onError(it.message ?: "Line service not found")
}
}
4 changes: 4 additions & 0 deletions Line/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<resources>
<string name="social_auth_fail">授权失败,请重试</string>
<string name="social_auth_fail_exception">授权异常,%s</string>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.github.devzwy.socialhelper.line

import org.junit.Test

import org.junit.Assert.*

/**
* Example local unit test, which will execute on the development machine (host).
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
class ExampleUnitTest {
@Test
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}
}
30 changes: 29 additions & 1 deletion PlatformInfo.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,32 @@ class ShareEntryActivity: AlipaySocialEntryActivity() {

## Google
- src目录同级防止在google平台生成的**google-services.json**
![google_platform](https://download.wdsf.top/dev/image/google_platform.png)
![google_platform](https://download.wdsf.top/dev/image/google_platform.png)

- 调用授权**Activity****onActivityResult**中增加如下代码
```
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
//加入这一行
if (requestCode == REQUEST_CODE_GOOGLE_AUTH) SocialHelper.onGoogleAuthResult(data)
}
```

## Line
-**AndroidManifest.xml**下的**application标签加入如下代码**
```
<application
//加入这两行
xmlns:tools="http://schemas.android.com/tools"
tools:replace="android:allowBackup"
>
```

- 调用授权**Activity****onActivityResult**中增加如下代码
```
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
//加入这一行
if (requestCode == REQUEST_CODE_LINE_AUTH) SocialHelper.onLineAuthResult(data)
}
```
40 changes: 34 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
- [x] 🎉 微信平台 **授权****获取用户资料****分享**[支持文本、图片、音乐、视频、网页、小程序]
- [x] 🎉 支付宝平台 **授权****分享**[支持文本、图片、网页]
- [x] 💃🏻 Google **授权****获取用户资料**
- [ ] 🚑 Line
- [x] 🚑 Line **授权****获取用户资料**
- [ ] 📝 ...

## demo效果图
Expand All @@ -35,24 +35,29 @@
```
//必选
implementation("io.github.devzwy:socialhelper:1.0.6")
implementation("io.github.devzwy:socialhelper:1.0.7")
//微信平台 可选 需要时集成
implementation('com.tencent.mm.opensdk:wechat-sdk-android:6.8.0')
implementation("io.github.devzwy:socialhelper.wechat:1.0.6"){
implementation("io.github.devzwy:socialhelper.wechat:1.0.7"){
transitive = false
}
//支付宝平台 可选 需要时集成
implementation("io.github.devzwy:socialhelper.alipay:1.0.6"){
implementation("io.github.devzwy:socialhelper.alipay:1.0.7"){
transitive = false
}
//Google平台 可选 需要时集成
implementation("com.google.android.gms:play-services-auth:20.2.0")
implementation("io.github.devzwy:socialhelper.google:1.0.6"){
implementation("io.github.devzwy:socialhelper.google:1.0.7"){
transitive = false
}
implementation("io.github.devzwy:socialhelper.line:1.0.7"){
transitive = false
}
```

#### 2.初始化
Expand All @@ -68,6 +73,7 @@ class MyApplication:Application() {
enableWeChatPlatform("微信AppId", "微信secretKey(可选)")
enableAlipayPlatform("支付宝AppId", "支付宝商户号", "支付宝应用私钥")
enableGooglePlatform("Google客户端Id clientId")
enableLinePlatform("Line AppId")
..
})
}
Expand Down Expand Up @@ -155,7 +161,7 @@ SocialHelper.reqGoogleAuth(this, {
})
```

- 获取用户资料
- 获取用户资料(授权返回数据读取)
```
this.mGoogleSignInAccount?.let {
//演示 从对象取出对应用户资料
Expand All @@ -172,3 +178,25 @@ SocialHelper.reqGoogleAuth(this, {
SocialHelper.signOut()
```

#### Line
- 获取授权
```
SocialHelper.reqLineAuth(this, {
appendLog(it)
}, {
//你要的东西应该在这个里面
this.mLineLoginResult = it
appendLog(it.toJsonStr())
btGetUserInfo.isEnabled = true
})
```

- 获取用户资料(授权返回数据读取)
```
this.mLineLoginResult?.lineProfile?.let {
//演示 从对象取出对应用户资料
appendLog("displayName:${it.displayName}")
appendLog("userId:${it.userId}")
appendLog("pictureUrl:${it.pictureUrl}")
}
```
Loading

0 comments on commit 86bc2ed

Please sign in to comment.