From 6283a0a66bc90e7f658c6d46ab3c8e12a5db7fc8 Mon Sep 17 00:00:00 2001 From: zhl Date: Fri, 31 Mar 2023 17:28:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0tiktok=E5=88=86=E4=BA=AB?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/AndroidManifest.xml | 6 +- .../com/cege/games/release/MainActivity.java | 61 +++++++++++++++++-- .../tiktokapi/TikTokEntryActivity.java | 6 ++ res/xml/app_files.xml | 2 +- 4 files changed, 68 insertions(+), 7 deletions(-) diff --git a/app/AndroidManifest.xml b/app/AndroidManifest.xml index 0592b85..fe7da9b 100644 --- a/app/AndroidManifest.xml +++ b/app/AndroidManifest.xml @@ -108,10 +108,12 @@ + android:launchMode="singleTask" + android:taskAffinity="com.cege.games.release" + android:exported="true" /> + android:theme="@style/WebViewTheme" /> diff --git a/app/src/com/cege/games/release/MainActivity.java b/app/src/com/cege/games/release/MainActivity.java index 3cdba68..7da0358 100644 --- a/app/src/com/cege/games/release/MainActivity.java +++ b/app/src/com/cege/games/release/MainActivity.java @@ -18,6 +18,9 @@ import android.widget.Toast; import com.bytedance.sdk.open.tiktok.TikTokOpenApiFactory; import com.bytedance.sdk.open.tiktok.api.TikTokOpenApi; import com.bytedance.sdk.open.tiktok.authorize.model.Authorization; +import com.bytedance.sdk.open.tiktok.base.MediaContent; +import com.bytedance.sdk.open.tiktok.base.VideoObject; +import com.bytedance.sdk.open.tiktok.share.Share; import com.cege.games.release.activity.CustomCaptureActivity; import com.cege.games.release.activity.WebPageActivity; import com.cege.games.release.apple.AppleLoginActivity; @@ -74,6 +77,7 @@ import org.cocos2dx.lib.Cocos2dxHelper; import org.cocos2dx.lib.CocosJSHelper; import org.json.JSONException; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -117,6 +121,8 @@ public class MainActivity extends UnityPlayerActivity public static final int RC_READ_PHOTO = 0X012; public static final int RC_LOAD_KEY = 0X013; + + public static final int FILE_SELECTOR_CODE = 0X014; private String title; private String funId; private String oid; @@ -134,6 +140,8 @@ public class MainActivity extends UnityPlayerActivity private CountDownLatch mAuthIntentLatch = new CountDownLatch(1); private static final String EXTRA_FAILED = "failed"; + private TikTokOpenApi tiktokOpenApi; + public String getFunId() { return funId; } @@ -191,6 +199,8 @@ public class MainActivity extends UnityPlayerActivity Intent appLinkIntent = getIntent(); String appLinkAction = appLinkIntent.getAction(); Uri appLinkData = appLinkIntent.getData(); + + tiktokOpenApi = TikTokOpenApiFactory.create(this); } @Override @@ -229,6 +239,10 @@ public class MainActivity extends UnityPlayerActivity Task task = GoogleSignIn.getSignedInAccountFromIntent(data); handleSignInResult(task); break; + case FILE_SELECTOR_CODE: + Uri uri = data.getData(); + shareToTikTok(uri); + break; } } else { boolean next = false; @@ -771,7 +785,6 @@ public class MainActivity extends UnityPlayerActivity this.funId = funId; Log.i(TAG, "login with tiktok: " + funId); // STEP 1: Create an instance of TiktokOpenApi - TikTokOpenApi tiktokOpenApi = TikTokOpenApiFactory.create(this); // STEP 2: Create an instance of Authorization.Request and set parameters Authorization.Request request = new Authorization.Request(); @@ -893,10 +906,50 @@ public class MainActivity extends UnityPlayerActivity public void showPage(String fid, final String url) { runOnUiThread(() -> { Log.i(TAG, "show page: " + url); - Intent intent = new Intent(this, WebPageActivity.class); - intent.putExtra("url", url); - startActivity(intent); +// Intent intent = new Intent(this, WebPageActivity.class); +// intent.putExtra("url", url); +// startActivity(intent); + openFileSelector(); }); } + public void shareToTikTok(Uri uriToImage) { + grantUriPermission("com.zhiliaoapp.musically", + uriToImage, Intent.FLAG_GRANT_READ_URI_PERMISSION); + grantUriPermission("com.ss.android.ugc.trill", + uriToImage, Intent.FLAG_GRANT_READ_URI_PERMISSION); + Log.i(TAG, "share to tiktok: " + uriToImage.toString()); + if (tiktokOpenApi.isShareSupportFileProvider()) { + Share.Request request = new Share.Request(); + ArrayList mUri = new ArrayList<>(); + mUri.add(uriToImage.toString()); + VideoObject videoObject = new VideoObject(); + videoObject.mVideoPaths = mUri; + MediaContent content = new MediaContent(); + content.mMediaObject = videoObject; + + // 3.set required parameters + request.mMediaContent = content; + request.mShareFormat = Share.Format.DEFAULT; + tiktokOpenApi.share(request); + } +// runOnUiThread(() -> { +// Log.i(TAG, "share to tiktok: " + uriToImage); +// Intent shareIntent = new Intent(); +// shareIntent.setAction(Intent.ACTION_SEND); +// shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage); +// shareIntent.setType("video/*"); +// startActivity(Intent.createChooser(shareIntent, "share")); +// }); + } + + /** + * 打开本地文件器 + */ + private void openFileSelector() { + Intent intent = new Intent(Intent.ACTION_GET_CONTENT); + intent.setType("video/*"); + intent.addCategory(Intent.CATEGORY_OPENABLE); + startActivityForResult(intent, FILE_SELECTOR_CODE); + } } \ No newline at end of file diff --git a/app/src/com/cege/games/release/tiktokapi/TikTokEntryActivity.java b/app/src/com/cege/games/release/tiktokapi/TikTokEntryActivity.java index 50a5c37..bf3dd58 100644 --- a/app/src/com/cege/games/release/tiktokapi/TikTokEntryActivity.java +++ b/app/src/com/cege/games/release/tiktokapi/TikTokEntryActivity.java @@ -3,13 +3,16 @@ package com.cege.games.release.tiktokapi; import android.app.Activity; import android.content.Intent; import android.os.Bundle; +import android.util.Log; +import com.bytedance.sdk.open.tiktok.CommonConstants; import com.bytedance.sdk.open.tiktok.TikTokOpenApiFactory; import com.bytedance.sdk.open.tiktok.api.TikTokOpenApi; import com.bytedance.sdk.open.tiktok.authorize.model.Authorization; import com.bytedance.sdk.open.tiktok.common.handler.IApiEventHandler; import com.bytedance.sdk.open.tiktok.common.model.BaseReq; import com.bytedance.sdk.open.tiktok.common.model.BaseResp; +import com.bytedance.sdk.open.tiktok.share.Share; import com.jc.jcfw.JcSDK; import androidx.annotation.Nullable; @@ -41,6 +44,9 @@ public class TikTokEntryActivity extends Activity implements IApiEventHandler { JcSDK.nativeCb(response.state, response.errorMsg, null); } finish(); + } else if (resp.getType() == CommonConstants.ModeType.SHARE_CONTENT_TO_TT_RESP) { + Share.Response response = (Share.Response) resp; + Log.i(TAG, "share result code:" + response.errorCode + " errorMessage:" + response.errorMsg); } } @Override diff --git a/res/xml/app_files.xml b/res/xml/app_files.xml index 91d0f54..6c4d140 100644 --- a/res/xml/app_files.xml +++ b/res/xml/app_files.xml @@ -9,7 +9,7 @@ name="root_path" path="." /> - +