lib_google
lib_huawei
This commit is contained in:
1
lib_google/.gitignore
vendored
Normal file
1
lib_google/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/build
|
||||
37
lib_google/build.gradle
Normal file
37
lib_google/build.gradle
Normal file
@@ -0,0 +1,37 @@
|
||||
apply plugin: 'com.android.library'
|
||||
|
||||
android {
|
||||
compileSdkVersion rootProject.ext.android.compileSdkVersion
|
||||
buildToolsVersion rootProject.ext.android.buildToolsVersion
|
||||
defaultConfig {
|
||||
minSdkVersion minSdkVersion
|
||||
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
consumerProguardFiles "consumer-rules.pro"
|
||||
versionCode versionCode
|
||||
versionName versionName
|
||||
targetSdkVersion targetSdkVersion
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation rootProject.ext.dependencies["appcompat-androidx"]
|
||||
|
||||
//谷歌内购
|
||||
api 'com.android.billingclient:billing:5.0.0'
|
||||
//谷歌登录
|
||||
api 'com.google.android.gms:play-services-auth:15.0.0'
|
||||
api 'com.google.android.gms:play-services-ads-identifier:15.0.1'
|
||||
api 'com.google.firebase:firebase-messaging:23.0.6'
|
||||
}
|
||||
0
lib_google/consumer-rules.pro
Normal file
0
lib_google/consumer-rules.pro
Normal file
21
lib_google/proguard-rules.pro
vendored
Normal file
21
lib_google/proguard-rules.pro
vendored
Normal file
@@ -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
|
||||
5
lib_google/src/main/AndroidManifest.xml
Normal file
5
lib_google/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.shayu.lib_google">
|
||||
|
||||
</manifest>
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.shayu.lib_google.utils;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.google.android.gms.tasks.OnCompleteListener;
|
||||
import com.google.android.gms.tasks.Task;
|
||||
import com.google.firebase.FirebaseApp;
|
||||
import com.google.firebase.messaging.FirebaseMessaging;
|
||||
|
||||
public class FirebaseManage {
|
||||
private Activity mActivity;
|
||||
private Context mContext;
|
||||
|
||||
public FirebaseManage(Activity activity) {
|
||||
this.mActivity = activity;
|
||||
}
|
||||
|
||||
public FirebaseManage(Context mContext) {
|
||||
this.mContext = mContext;
|
||||
}
|
||||
|
||||
String token = "";
|
||||
|
||||
public String getToken() {
|
||||
return token;
|
||||
}
|
||||
|
||||
public void initToken() {
|
||||
FirebaseMessaging.getInstance().getToken().addOnCompleteListener(new OnCompleteListener<String>() {
|
||||
@Override
|
||||
public void onComplete(@NonNull Task<String> task) {
|
||||
if (!task.isSuccessful()) {
|
||||
Log.w("TAG", "Fetching FCM registration token failed", task.getException());
|
||||
return;
|
||||
}
|
||||
// Get new FCM registration token
|
||||
token = task.getResult();
|
||||
// Log and toast
|
||||
Log.d("TAG", token);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void initializeApp() {
|
||||
FirebaseApp.initializeApp(mContext);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,214 @@
|
||||
package com.shayu.lib_google.utils;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.os.Handler;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.android.billingclient.api.BillingClient;
|
||||
import com.android.billingclient.api.BillingClientStateListener;
|
||||
import com.android.billingclient.api.BillingFlowParams;
|
||||
import com.android.billingclient.api.BillingResult;
|
||||
import com.android.billingclient.api.ConsumeParams;
|
||||
import com.android.billingclient.api.ConsumeResponseListener;
|
||||
import com.android.billingclient.api.Purchase;
|
||||
import com.android.billingclient.api.PurchasesResponseListener;
|
||||
import com.android.billingclient.api.PurchasesUpdatedListener;
|
||||
import com.android.billingclient.api.QueryPurchasesParams;
|
||||
import com.android.billingclient.api.SkuDetails;
|
||||
import com.android.billingclient.api.SkuDetailsParams;
|
||||
import com.android.billingclient.api.SkuDetailsResponseListener;
|
||||
import com.google.android.gms.ads.identifier.AdvertisingIdClient;
|
||||
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
|
||||
import com.google.android.gms.common.GooglePlayServicesRepairableException;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 新写Google内付工具类做简单的封装
|
||||
* 支付流程:初始化Google支付服务-->查询商品-->购买-->支付成功回调-->消耗商品
|
||||
*/
|
||||
public class GoogleBillingManage implements PurchasesUpdatedListener {
|
||||
private BillingClient billingClient;
|
||||
private Activity mContext;
|
||||
private String TAG = "GoogleBillingManage";
|
||||
|
||||
public GoogleBillingManage(Activity activity) {
|
||||
this.mContext = activity;
|
||||
}
|
||||
|
||||
public void initGooglePay() {
|
||||
billingClient = BillingClient.newBuilder(mContext).setListener(this).enablePendingPurchases().build();
|
||||
//请求连接到GooglePay
|
||||
billingClient.startConnection(new BillingClientStateListener() {
|
||||
@Override
|
||||
public void onBillingSetupFinished(@NonNull BillingResult billingResult) {
|
||||
int code = billingResult.getResponseCode();
|
||||
if (code != BillingClient.BillingResponseCode.OK) {
|
||||
String msg = billingResult.getDebugMessage();
|
||||
Log.e(TAG, "连接到GooglePay失败 code = " + code + " msg = " + msg);
|
||||
return;
|
||||
}
|
||||
Log.e(TAG, "连接到GooglePay成功");
|
||||
|
||||
}
|
||||
|
||||
//连接失败
|
||||
@Override
|
||||
public void onBillingServiceDisconnected() {
|
||||
Log.e(TAG, "连接到GooglePay失败,请重试");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//查询商品
|
||||
public void checkSku(String id) {
|
||||
List<String> skuList = new ArrayList<>();
|
||||
skuList.add(id);
|
||||
SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder().setSkusList(skuList).setType(BillingClient.SkuType.INAPP);
|
||||
billingClient.querySkuDetailsAsync(params.build(), new SkuDetailsResponseListener() {
|
||||
@Override
|
||||
public void onSkuDetailsResponse(@NonNull BillingResult billingResult, @Nullable List<SkuDetails> list) {
|
||||
int code = billingResult.getResponseCode();
|
||||
if (code != BillingClient.BillingResponseCode.OK || list == null || list.isEmpty()) {
|
||||
String msg = billingResult.getDebugMessage();
|
||||
Log.e(TAG, "查询商品失败 code = " + code + " msg = " + msg);
|
||||
return;
|
||||
}
|
||||
Log.e(TAG, "查询商品成功");
|
||||
buyIt(list.get(0));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//购买
|
||||
private void buyIt(SkuDetails skuDetails) {
|
||||
BillingFlowParams billingFlowParams = BillingFlowParams.newBuilder().setSkuDetails(skuDetails).build();
|
||||
BillingResult billingResult = billingClient.launchBillingFlow(mContext, billingFlowParams);
|
||||
int code = billingResult.getResponseCode();
|
||||
if (code != BillingClient.BillingResponseCode.OK) {
|
||||
String msg = billingResult.getDebugMessage();
|
||||
Log.e(TAG, "购买商品失败 code = " + code + " msg = " + msg);
|
||||
return;
|
||||
}
|
||||
Log.e(TAG, "购买商品" + skuDetails.toString());
|
||||
}
|
||||
|
||||
//消耗商品
|
||||
public void consume(List<Purchase> list) {
|
||||
if (list == null || list.isEmpty() || billingClient == null) {
|
||||
return;
|
||||
}
|
||||
for (Purchase purchase : list) {
|
||||
billingClient.consumeAsync(ConsumeParams.newBuilder().setPurchaseToken(purchase.getPurchaseToken()).build(), new ConsumeResponseListener() {
|
||||
@Override
|
||||
public void onConsumeResponse(BillingResult billingResult, String purchaseToken) {
|
||||
Log.e(TAG, "onConsumeResponse code = " + billingResult.getResponseCode() + " , msg = " + billingResult.getDebugMessage() + " , purchaseToken = " + purchaseToken);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询商品订单
|
||||
*/
|
||||
public void queryPurchasesAsync(PurchasesResponseListener responseListener) {
|
||||
if (billingClient == null) return;
|
||||
//内购商品交易查询
|
||||
//内购商品交易查询
|
||||
QueryPurchasesParams inAppPurchasesQurey = QueryPurchasesParams.newBuilder().setProductType(BillingClient.ProductType.INAPP).build();
|
||||
billingClient.queryPurchasesAsync(inAppPurchasesQurey, responseListener);
|
||||
}
|
||||
|
||||
public String getAdid() {
|
||||
try {
|
||||
return AdvertisingIdClient.getAdvertisingIdInfo(mContext).getId();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (GooglePlayServicesNotAvailableException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (GooglePlayServicesRepairableException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int resultCode = -1;
|
||||
String resultToken = "";
|
||||
String resultOrderId = "";
|
||||
|
||||
public JSONObject getPayResult() {
|
||||
JSONObject object = new JSONObject();
|
||||
try {
|
||||
object.put("resultCode", resultCode);
|
||||
object.put("resultToken", resultToken);
|
||||
object.put("resultOrderId", resultOrderId);
|
||||
} catch (JSONException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return object;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPurchasesUpdated(@NonNull BillingResult billingResult, @Nullable List<Purchase> list) {
|
||||
int code = billingResult.getResponseCode();
|
||||
String msg = billingResult.getDebugMessage();
|
||||
|
||||
Log.e(TAG, "onPurchasesUpdated:code = " + code + " msg = " + msg);
|
||||
|
||||
if (list != null) {
|
||||
for (Purchase purchase : list) {
|
||||
Log.e(TAG, "onPurchasesUpdated:" + purchase.toString());
|
||||
}
|
||||
if (list.get(0) != null) {
|
||||
resultToken = list.get(0).getPurchaseToken();
|
||||
resultOrderId = list.get(0).getOrderId();
|
||||
}
|
||||
}
|
||||
if (code == BillingClient.BillingResponseCode.OK && list != null) {
|
||||
consume(list);
|
||||
resultCode = 0;
|
||||
Log.e(TAG, "支付成功");
|
||||
if (billingListener != null) {
|
||||
billingListener.onPaySuccess(list);
|
||||
}
|
||||
} else if (code == BillingClient.BillingResponseCode.USER_CANCELED) {
|
||||
Log.e(TAG, "支付取消");
|
||||
resultCode = 1;
|
||||
if (billingListener != null) {
|
||||
billingListener.onPayFailed(code, msg);
|
||||
}
|
||||
} else {
|
||||
resultCode = 2;
|
||||
if (billingListener != null) {
|
||||
billingListener.onPayFailed(code, msg);
|
||||
}
|
||||
Log.e(TAG, "支付失败:code = " + code + " msg = " + msg);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 定义接口返回支付结果
|
||||
*/
|
||||
public interface GoogleBillingListener {
|
||||
void onPaySuccess(List<Purchase> list);
|
||||
|
||||
void onPayFailed(int code, String msg);
|
||||
}
|
||||
|
||||
private GoogleBillingListener billingListener;
|
||||
|
||||
public GoogleBillingManage setBillingListener(GoogleBillingListener billingListener) {
|
||||
this.billingListener = billingListener;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.shayu.lib_google.utils;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.util.Log;
|
||||
|
||||
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 org.json.JSONObject;
|
||||
|
||||
public class GoogleManage {
|
||||
|
||||
Activity mActivity;
|
||||
|
||||
public GoogleManage(Activity mContext) {
|
||||
this.mActivity = mContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* 谷歌登录
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Intent googleLogin() {
|
||||
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().build();
|
||||
GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(mActivity, gso);
|
||||
return mGoogleSignInClient.getSignInIntent();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取登录回调
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public JSONObject getLoginResult(Intent data) {
|
||||
JSONObject object = new JSONObject();
|
||||
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
|
||||
try {
|
||||
GoogleSignInAccount account = task.getResult(ApiException.class);
|
||||
//account里可以直接获取到ID,name,和头像
|
||||
String idToken = account.getIdToken();
|
||||
account.getFamilyName();
|
||||
account.getGivenName();
|
||||
|
||||
object.put("displayName", account.getDisplayName());
|
||||
object.put("openId", account.getId());
|
||||
|
||||
} catch (Exception e) {
|
||||
Log.i("Log", "ApiException = " + e);
|
||||
}
|
||||
return object;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.shayu.lib_huawei;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Example local unit test, which will execute on the development machine (host).
|
||||
*
|
||||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
|
||||
*/
|
||||
public class ExampleUnitTest {
|
||||
@Test
|
||||
public void addition_isCorrect() {
|
||||
assertEquals(4, 2 + 2);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user