update Share

This commit is contained in:
zlzw 2023-02-08 16:01:35 +08:00
parent ce8088059a
commit 3c80038bef
23 changed files with 683 additions and 6 deletions

1
Share/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/build

73
Share/build.gradle Normal file
View File

@ -0,0 +1,73 @@
apply plugin: 'com.android.library'
apply plugin: 'img-optimizer'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion rootProject.ext.android.compileSdkVersion
buildToolsVersion rootProject.ext.android.buildToolsVersion
packagingOptions {
pickFirst "lib/armeabi/libyuvutils.so"
pickFirst "lib/arm64-v8a/libyuvutils.so"
pickFirst "lib/armeabi-v7a/libyuvutils.so"
pickFirst "lib/armeabi/libyuvtools.so"
pickFirst "lib/arm64-v8a/libyuvtools.so"
pickFirst "lib/armeabi-v7a/libyuvtools.so"
exclude "lib/arm64-v8a/libmmcv_api_handgesture.so"
exclude "lib/arm64-v8a/libmmcv_api_express.so"
exclude "lib/arm64-v8a/libMediaEncoder.so"
exclude "lib/arm64-v8a/libarcore_sdk_c.so"
exclude "lib/arm64-v8a/libmediadecoder.so"
exclude "lib/arm64-v8a/libMediaMuxer.so"
exclude "lib/arm64-v8a/libarcore_sdk_jni.so"
exclude "lib/arm64-v8a/libMediaUtils.so"
exclude "lib/arm64-v8a/libcosmosffmpeg.so"
}
defaultConfig {
minSdkVersion rootProject.ext.android.minSdkVersion
targetSdkVersion rootProject.ext.android.targetSdkVersion
versionCode rootProject.ext.android.versionCode
versionName rootProject.ext.android.versionName
manifestPlaceholders = rootProject.ext.manifestPlaceholders
ndk {
abiFilters "armeabi-v7a", "arm64-v8a"
}
}
aaptOptions {
cruncherEnabled = false
useNewCruncher = false
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
repositories {
flatDir {
dirs 'libs', '../libs'
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation rootProject.ext.dependencies["appcompat-androidx"]
implementation rootProject.ext.dependencies["recyclerview-androidx"]
//common
implementation project(path: ':common')
//Twitter
implementation 'com.twitter.sdk.android:twitter:3.1.1'
//facebook & Messenger
implementation 'com.facebook.android:facebook-share:15.2.0'
}

0
Share/consumer-rules.pro Normal file
View File

21
Share/proguard-rules.pro vendored Normal file
View 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

View File

@ -0,0 +1,26 @@
package com.yunbao.share;
import android.content.Context;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
assertEquals("com.yunbao.share.test", appContext.getPackageName());
}
}

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.yunbao.share">
<queries><provider android:authorities="com.facebook.katana.provider.PlatformProvider" /></queries>
<application android:allowBackup="true">
<provider
android:name="com.facebook.FacebookContentProvider"
android:authorities="com.facebook.app.FacebookContentProvider2011402032399020"
android:exported="true" />
<receiver
android:name="com.yunbao.share.receiver.TwitterResultReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.twitter.sdk.android.tweetcomposer.UPLOAD_SUCCESS"/>
<action android:name="com.twitter.sdk.android.tweetcomposer.UPLOAD_FAILURE"/>
<action android:name="com.twitter.sdk.android.tweetcomposer.TWEET_COMPOSE_CANCEL"/>
</intent-filter>
</receiver>
</application>
</manifest>

View File

@ -0,0 +1,25 @@
package com.yunbao.share;
import android.content.Context;
import android.net.Uri;
import androidx.core.content.FileProvider;
import java.io.File;
public abstract class AbsShareInterface {
protected final Context mContext;
public AbsShareInterface(Context context) {
this.mContext = context;
}
public abstract void share(ShareBuilder builder,ICallback callback);
public Uri fileToUri(File file){
return FileProvider.getUriForFile(mContext,
mContext.getPackageName() + ".fileprovider",
file
);
}
}

View File

@ -0,0 +1,6 @@
package com.yunbao.share;
public interface ICallback {
void onSuccess();
void onFailure();
}

View File

@ -0,0 +1,40 @@
package com.yunbao.share;
import java.io.File;
public class ShareBuilder {
private String text;
private String link;
private File file;
public static ShareBuilder builder(){
return new ShareBuilder();
}
public ShareBuilder setText(String text) {
this.text = text;
return this;
}
public ShareBuilder setLink(String link) {
this.link = link;
return this;
}
public ShareBuilder setFile(File file) {
this.file = file;
return this;
}
public String getText() {
return text;
}
public String getLink() {
return link;
}
public File getFile() {
return file;
}
}

View File

@ -0,0 +1,67 @@
package com.yunbao.share;
import android.content.Context;
import android.content.IntentFilter;
import com.twitter.sdk.android.tweetcomposer.TweetUploadService;
import com.yunbao.share.platform.FacebookShare;
import com.yunbao.share.platform.Instagram;
import com.yunbao.share.platform.Line;
import com.yunbao.share.platform.MessengerShare;
import com.yunbao.share.platform.TwitterShare;
import com.yunbao.share.platform.WhatsApp;
import com.yunbao.share.receiver.TwitterResultReceiver;
public class ShareManager {
public static final int SHARE_Twitter = 0;
public static final int SHARE_Facebook = 1;
public static final int SHARE_Instagram = 2;
public static final int SHARE_WhatsApp = 3;
public static final int SHARE_Line = 4;
public static final int SHARE_Messenger = 5;
private static ShareManager manager = null;
public static ShareManager getInstance(Context context) {
if (manager == null) {
manager = new ShareManager(context);
}
return manager;
}
private ShareManager(Context context) {
IntentFilter filter = new IntentFilter(TweetUploadService.UPLOAD_SUCCESS);
filter.addAction(TweetUploadService.UPLOAD_FAILURE);
filter.addAction(TweetUploadService.TWEET_COMPOSE_CANCEL);
context.registerReceiver(new TwitterResultReceiver(), filter);
}
public void createShare(ShareBuilder builder, Context context, int type, ICallback callback) {
AbsShareInterface share = null;
switch (type) {
case SHARE_Twitter:
share = new TwitterShare(context);
break;
case SHARE_Facebook:
share = new FacebookShare(context);
break;
case SHARE_Instagram:
share = new Instagram(context);
break;
case SHARE_WhatsApp:
share = new WhatsApp(context);
break;
case SHARE_Line:
share = new Line(context);
break;
case SHARE_Messenger:
share = new MessengerShare(context);
break;
}
if (share != null) {
share.share(builder, callback);
}
}
}

View File

@ -0,0 +1,52 @@
package com.yunbao.share.platform;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.util.Log;
import androidx.annotation.NonNull;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.share.Sharer;
import com.facebook.share.model.ShareLinkContent;
import com.facebook.share.widget.ShareDialog;
import com.yunbao.common.utils.ToastUtil;
import com.yunbao.share.AbsShareInterface;
import com.yunbao.share.ICallback;
import com.yunbao.share.ShareBuilder;
public class FacebookShare extends AbsShareInterface {
public static CallbackManager callbackManager;
public FacebookShare(Context context) {
super(context);
}
@Override
public void share(ShareBuilder builder, ICallback callback) {
callbackManager= CallbackManager.Factory.create();
ShareLinkContent content = new ShareLinkContent.Builder()
.setContentUrl(Uri.parse(builder.getLink()))
.build();
ShareDialog dialog=new ShareDialog((Activity) mContext);
dialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
@Override
public void onSuccess(Sharer.Result result) {
ToastUtil.show("Facebook分享成功");
}
@Override
public void onCancel() {
ToastUtil.show("Facebook分享取消");
}
@Override
public void onError(@NonNull FacebookException e) {
ToastUtil.show("Facebook分享失败:"+e.toString());
}
});
dialog.show(content);
}
}

View File

@ -0,0 +1,29 @@
package com.yunbao.share.platform;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import androidx.core.content.FileProvider;
import com.yunbao.share.AbsShareInterface;
import com.yunbao.share.ICallback;
import com.yunbao.share.ShareBuilder;
public class Instagram extends AbsShareInterface {
public Instagram(Context context) {
super(context);
}
@Override
public void share(ShareBuilder builder, ICallback callback) {
String type = "image/*";
Intent share = new Intent(Intent.ACTION_SEND);
Uri uri = fileToUri(builder.getFile());
share.setType(type);
share.putExtra(Intent.EXTRA_STREAM, uri);
share.setPackage("com.instagram.android");
mContext.startActivity(Intent.createChooser(share, "Share to"));
callback.onSuccess();
}
}

View File

@ -0,0 +1,33 @@
package com.yunbao.share.platform;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import com.yunbao.share.AbsShareInterface;
import com.yunbao.share.ICallback;
import com.yunbao.share.ShareBuilder;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
public class Line extends AbsShareInterface {
public Line(Context context) {
super(context);
}
@Override
public void share(ShareBuilder builder, ICallback callback) {
try {
Intent share = new Intent(Intent.ACTION_VIEW, Uri.parse("https://line.me/R/share?text=" + URLEncoder.encode(builder.getLink(), "UTF-8")));
mContext.startActivity(share);
callback.onSuccess();
} catch (UnsupportedEncodingException e) {
callback.onFailure();
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,63 @@
package com.yunbao.share.platform;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import androidx.annotation.NonNull;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.share.Sharer;
import com.facebook.share.model.ShareLinkContent;
import com.facebook.share.model.SharePhoto;
import com.facebook.share.model.SharePhotoContent;
import com.facebook.share.widget.MessageDialog;
import com.facebook.share.widget.ShareDialog;
import com.yunbao.common.utils.ToastUtil;
import com.yunbao.share.AbsShareInterface;
import com.yunbao.share.ICallback;
import com.yunbao.share.ShareBuilder;
public class MessengerShare extends AbsShareInterface {
public static CallbackManager callbackManager;
public MessengerShare(Context context) {
super(context);
}
@Override
public void share(ShareBuilder builder, ICallback callback) {
callbackManager = CallbackManager.Factory.create();
/* ShareLinkContent content = new ShareLinkContent.Builder()
.setContentUrl(Uri.parse(builder.getLink()))
.build();*/
SharePhoto photo = new SharePhoto.Builder()
.setImageUrl(fileToUri(builder.getFile()))
.build();
SharePhotoContent content=new SharePhotoContent.Builder()
.addPhoto(photo)
.build();
MessageDialog dialog = new MessageDialog((Activity) mContext);
dialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
@Override
public void onSuccess(Sharer.Result result) {
ToastUtil.show("Messenger分享成功");
}
@Override
public void onCancel() {
ToastUtil.show("Messenger分享取消");
}
@Override
public void onError(@NonNull FacebookException e) {
ToastUtil.show("Messenger分享失败:" + e.toString());
e.printStackTrace();
}
});
ToastUtil.show(dialog.canShow(content)+" <");
dialog.show(content);
}
}

View File

@ -0,0 +1,60 @@
package com.yunbao.share.platform;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import com.twitter.sdk.android.core.Callback;
import com.twitter.sdk.android.core.Result;
import com.twitter.sdk.android.core.Twitter;
import com.twitter.sdk.android.core.TwitterApiClient;
import com.twitter.sdk.android.core.TwitterAuthToken;
import com.twitter.sdk.android.core.TwitterCore;
import com.twitter.sdk.android.core.TwitterException;
import com.twitter.sdk.android.core.TwitterSession;
import com.twitter.sdk.android.core.identity.TwitterAuthClient;
import com.twitter.sdk.android.core.services.AccountService;
import com.twitter.sdk.android.tweetcomposer.ComposerActivity;
import com.twitter.sdk.android.tweetcomposer.TweetComposer;
import com.yunbao.share.AbsShareInterface;
import com.yunbao.share.ICallback;
import com.yunbao.share.ShareBuilder;
public class TwitterShare extends AbsShareInterface {
public TwitterShare(Context context) {
super(context);
Twitter.initialize(context);
}
@Override
public void share(ShareBuilder date, ICallback callback) {
/*
new TwitterAuthClient().authorize((Activity) mContext, new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> result) {
//获取以下登录成功返回信息进行登录验证
//获取登录用户信息
final TwitterSession activeSession=TwitterCore.getInstance().getSessionManager().getActiveSession();
Intent intent = new ComposerActivity.Builder(mContext)
.session(activeSession)
.image(fileToUri(date.getFile()))
.text(date.getText())
.hashtags("#twitter")
.createIntent();
mContext.startActivity(intent);
}
@Override
public void failure(TwitterException e) {
}
});*/
TweetComposer.Builder builder = new TweetComposer.Builder(mContext)
.text(date.getText())
.image(fileToUri(date.getFile()));
builder.show();
}
}

View File

@ -0,0 +1,24 @@
package com.yunbao.share.platform;
import android.content.Context;
import android.content.Intent;
import com.yunbao.share.AbsShareInterface;
import com.yunbao.share.ICallback;
import com.yunbao.share.ShareBuilder;
public class WhatsApp extends AbsShareInterface {
public WhatsApp(Context context) {
super(context);
}
@Override
public void share(ShareBuilder builder, ICallback callback) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, builder.getText());
sendIntent.setType("text/plain");
sendIntent.setPackage("com.whatsapp");
mContext.startActivity(sendIntent);
}
}

View File

@ -0,0 +1,28 @@
package com.yunbao.share.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.twitter.sdk.android.tweetcomposer.TweetUploadService;
import com.yunbao.common.utils.ToastUtil;
public class TwitterResultReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("分享log", "onReceive: "+intent.getAction());
if (TweetUploadService.UPLOAD_SUCCESS.equals(intent.getAction())) {
ToastUtil.show("推特分享成功");
// success
final Long tweetId = intent.getExtras().getLong(TweetUploadService.EXTRA_TWEET_ID);
} else if (TweetUploadService.UPLOAD_FAILURE.equals(intent.getAction())) {
// failure
ToastUtil.show("推特分享失败");
final Intent retryIntent = intent.getExtras().getParcelable(TweetUploadService.EXTRA_RETRY_INTENT);
} else if (TweetUploadService.TWEET_COMPOSE_CANCEL.equals(intent.getAction())) {
// cancel
ToastUtil.show("推特分享取消");
}
}
}

View File

@ -0,0 +1,4 @@
<resources>
<string name="com.twitter.sdk.android.CONSUMER_KEY" translatable="false">ZWRrZnRUNlBlcHVxMXpsMzVmb2k6MTpjaQ</string>
<string name="com.twitter.sdk.android.CONSUMER_SECRET" translatable="false">aq0eV4R1pqMK_AAeKRWnjPr7ErGMGgTPGgZJdm73WeRY-Kluws</string>
</resources>

View File

@ -0,0 +1,17 @@
package com.yunbao.share;
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);
}
}

View File

@ -73,6 +73,7 @@ dependencies {
api project(':video')
api project(':common')
api project(':FaceUnity')//
api project(':Share')//
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
annotationProcessor rootProject.ext.dependencies["arouter-compiler"]

View File

@ -1,23 +1,106 @@
package com.yunbao.main.activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.text.SpannableStringBuilder;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.yunbao.main.R;
import com.yunbao.share.ICallback;
import com.yunbao.share.ShareBuilder;
import com.yunbao.share.ShareManager;
import com.yunbao.share.platform.FacebookShare;
import com.yunbao.share.platform.MessengerShare;
import java.io.File;
public class TestActivity extends AppCompatActivity {
private TextView contextLayout;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
contextLayout = findViewById(R.id.context_layout);
SpannableStringBuilder builder = new SpannableStringBuilder();
listView = new ListView(this);
setContentView(listView);
String[] strs = new String[]{
"Twitter",
"Facebook",
"Instagram",
"WhatsApp",
"Messenger",
"Line"
};
String filename = "/ztest/myPhoto.jpg";
String mediaPath = Environment.getExternalStorageDirectory() + filename;
File media = new File(mediaPath);
ShareBuilder builder = new ShareBuilder()
.setFile(media)
.setLink("http://blog.yutou233.cn")
.setText("测试Test http://blog.yutou233.cn");
ICallback callback = new ICallback() {
@Override
public void onSuccess() {
}
@Override
public void onFailure() {
}
};
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, strs));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
ShareManager.getInstance(TestActivity.this)
.createShare(builder, TestActivity.this, ShareManager.SHARE_Twitter, callback);
break;
case 1:
ShareManager.getInstance(TestActivity.this)
.createShare(builder, TestActivity.this, ShareManager.SHARE_Facebook, callback);
break;
case 4:
ShareManager.getInstance(TestActivity.this)
.createShare(builder, TestActivity.this, ShareManager.SHARE_Messenger, callback);
break;
case 2:
ShareManager.getInstance(TestActivity.this)
.createShare(builder, TestActivity.this, ShareManager.SHARE_Instagram, callback);
break;
case 3:
ShareManager.getInstance(TestActivity.this)
.createShare(builder, TestActivity.this, ShareManager.SHARE_WhatsApp, callback);
break;
case 5:
ShareManager.getInstance(TestActivity.this)
.createShare(builder, TestActivity.this, ShareManager.SHARE_Line, callback);
break;
}
}
});
}
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i("分享log", "onActivityResult() called with: requestCode = [" + requestCode + "], resultCode = [" + resultCode + "], data = [" + data + "]");
if (FacebookShare.callbackManager != null) {
FacebookShare.callbackManager.onActivityResult(requestCode, resultCode, data);
} else if (MessengerShare.callbackManager != null) {
MessengerShare.callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
}

View File

@ -50,6 +50,7 @@ import com.yunbao.live.utils.LiveStorge;
import com.yunbao.live.views.LiveRoomViewHolder;
import com.yunbao.main.R;
import com.yunbao.main.activity.MainActivity;
import com.yunbao.main.activity.TestActivity;
import com.yunbao.main.adapter.MainHomeLiveAdapter;
import com.yunbao.main.adapter.MainHomeLivesClassAdapter;
import com.yunbao.main.http.MainHttpConsts;
@ -368,7 +369,7 @@ public class MainHomeLiveViewHolder extends AbsMainHomeChildViewHolder implement
@Override
public void onBannerClick(List datas, int p) {
if (true) {
System.err.println("cache = " + mContext.getCacheDir().getAbsolutePath());
/* System.err.println("cache = " + mContext.getCacheDir().getAbsolutePath());
String type = "image/*";
String filename = "/ztest/myPhoto.jpg";
String mediaPath = Environment.getExternalStorageDirectory() + filename;
@ -392,7 +393,8 @@ public class MainHomeLiveViewHolder extends AbsMainHomeChildViewHolder implement
// share.setPackage("com.whatsapp"); //WhatsApp
// share = new Intent(Intent.ACTION_VIEW, Uri.parse("https://line.me/R/share?text=test")); //Line
mContext.startActivity(Intent.createChooser(share, "Share to"));
mContext.startActivity(Intent.createChooser(share, "Share to"));*/
mContext.startActivity(new Intent(mContext, TestActivity.class));
return;
}
if (mBannerList != null) {

View File

@ -2,3 +2,5 @@ include ':app', ':video', ':common', ':main', ':baidu', ':live', ':lib_country_
include ':FaceUnity'
//include ':baidu'
//include ':recognizer'//
include ':Share'
include ':Share'