first commit
This commit is contained in:
9
android/.gitignore
vendored
Normal file
9
android/.gitignore
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
*.iml
|
||||
.gradle
|
||||
/local.properties
|
||||
/.idea/workspace.xml
|
||||
/.idea/libraries
|
||||
.DS_Store
|
||||
/build
|
||||
/captures
|
||||
.cxx
|
52
android/build.gradle
Normal file
52
android/build.gradle
Normal file
@@ -0,0 +1,52 @@
|
||||
group 'com.kd.custom_social_share'
|
||||
version '1.0-SNAPSHOT'
|
||||
|
||||
buildscript {
|
||||
ext.kotlin_version = '1.7.10'
|
||||
repositories {
|
||||
google()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:7.3.0'
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
}
|
||||
}
|
||||
|
||||
allprojects {
|
||||
repositories {
|
||||
google()
|
||||
mavenCentral()
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: 'com.android.library'
|
||||
apply plugin: 'kotlin-android'
|
||||
|
||||
android {
|
||||
if (project.android.hasProperty("namespace")) {
|
||||
namespace 'com.kd.custom_social_share'
|
||||
}
|
||||
|
||||
compileSdk 34
|
||||
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
|
||||
kotlinOptions {
|
||||
jvmTarget = '1.8'
|
||||
}
|
||||
|
||||
sourceSets {
|
||||
main.java.srcDirs += 'src/main/kotlin'
|
||||
}
|
||||
|
||||
defaultConfig {
|
||||
minSdkVersion 19
|
||||
}
|
||||
|
||||
dependencies {}
|
||||
}
|
7
android/gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
7
android/gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
|
||||
networkTimeout=10000
|
||||
validateDistributionUrl=true
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
1
android/settings.gradle
Normal file
1
android/settings.gradle
Normal file
@@ -0,0 +1 @@
|
||||
rootProject.name = 'custom_social_share'
|
1
android/src/main/AndroidManifest.xml
Normal file
1
android/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1 @@
|
||||
<manifest package="com.kd.custom_social_share" />
|
@@ -0,0 +1,144 @@
|
||||
package com.kd.custom_social_share
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.ActivityNotFoundException
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import androidx.core.app.ShareCompat
|
||||
import io.flutter.embedding.engine.plugins.FlutterPlugin
|
||||
import io.flutter.embedding.engine.plugins.activity.ActivityAware
|
||||
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
|
||||
import io.flutter.plugin.common.MethodCall
|
||||
import io.flutter.plugin.common.MethodChannel
|
||||
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
|
||||
import io.flutter.plugin.common.MethodChannel.Result
|
||||
|
||||
/** CustomSocialSharePlugin */
|
||||
class CustomSocialSharePlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
|
||||
/// The MethodChannel that will the communication between Flutter and native Android
|
||||
///
|
||||
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
|
||||
/// when the Flutter Engine is detached from the Activity
|
||||
private lateinit var channel: MethodChannel
|
||||
private lateinit var mActivity: Activity
|
||||
|
||||
private val appsMap = mapOf(
|
||||
"facebook" to "com.facebook.katana",
|
||||
"facebookLite" to "com.facebook.lite",
|
||||
"facebookMessenger" to "com.facebook.orca",
|
||||
"facebookMessengerLite" to "com.facebook.mlite",
|
||||
"instagram" to "com.instagram.android",
|
||||
"line" to "jp.naver.line.android",
|
||||
"linkedin" to "com.linkedin.android",
|
||||
"reddit" to "com.reddit.frontpage",
|
||||
"skype" to "com.skype.raider",
|
||||
"slack" to "com.Slack",
|
||||
"snapchat" to "com.snapchat.android",
|
||||
"telegram" to "org.telegram.messenger",
|
||||
"x" to "com.twitter.android",
|
||||
"viber" to "com.viber.voip",
|
||||
"wechat" to "com.tencent.mm",
|
||||
"whatsapp" to "com.whatsapp",
|
||||
"threads" to "com.instagram.barcelona",
|
||||
)
|
||||
|
||||
override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
|
||||
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "custom_social_share")
|
||||
channel.setMethodCallHandler(this)
|
||||
}
|
||||
|
||||
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
|
||||
channel.setMethodCallHandler(null)
|
||||
}
|
||||
|
||||
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
|
||||
mActivity = binding.activity
|
||||
}
|
||||
|
||||
override fun onDetachedFromActivityForConfigChanges() {}
|
||||
|
||||
override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
|
||||
mActivity = binding.activity
|
||||
}
|
||||
|
||||
override fun onDetachedFromActivity() {}
|
||||
|
||||
override fun onMethodCall(call: MethodCall, result: Result) {
|
||||
val content: String? = call.argument("content")
|
||||
when (call.method) {
|
||||
|
||||
"sms" -> {
|
||||
val intent = Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:")).apply {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) type = "vnd.android-dir/mms-sms"
|
||||
putExtra("sms_body", content)
|
||||
}
|
||||
callIntent(intent, result)
|
||||
}
|
||||
|
||||
"email" -> {
|
||||
callIntent(Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:?body=$content")), result)
|
||||
}
|
||||
|
||||
"toAll" -> {
|
||||
ShareCompat.IntentBuilder(mActivity).setType("text/plain").setText(content).startChooser()
|
||||
}
|
||||
|
||||
"getInstalledApps" -> {
|
||||
//check if the apps exists
|
||||
//creating a mutable map of apps
|
||||
val apps: MutableMap<String, Boolean> = mutableMapOf()
|
||||
|
||||
//assigning package manager
|
||||
val pm: PackageManager = mActivity.packageManager
|
||||
|
||||
//get a list of installed apps.
|
||||
val packages = pm.getInstalledApplications(PackageManager.GET_META_DATA)
|
||||
|
||||
//if sms app exists
|
||||
apps["sms"] = pm.queryIntentActivities(Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:")), 0).isNotEmpty()
|
||||
|
||||
//if email app exists
|
||||
apps["email"] = pm.queryIntentActivities(Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:")), 0).isNotEmpty()
|
||||
|
||||
//if other app exists
|
||||
appsMap.forEach { entry: Map.Entry<String, String> ->
|
||||
apps[entry.key] = packages.any { it.packageName.toString().contentEquals(entry.value) }
|
||||
}
|
||||
|
||||
result.success(apps)
|
||||
}
|
||||
|
||||
"customApp" -> {
|
||||
callShareIntent((call.argument("package") as? String).orEmpty(), content, result)
|
||||
}
|
||||
|
||||
else -> {
|
||||
if (appsMap.containsKey(call.method)) {
|
||||
callShareIntent(appsMap[call.method].orEmpty(), content, result)
|
||||
} else {
|
||||
result.notImplemented()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun callShareIntent(packageName: String, content: String?, result: Result) {
|
||||
val intent = Intent(Intent.ACTION_SEND).apply {
|
||||
putExtra(Intent.EXTRA_TEXT, content)
|
||||
type = "text/plain"
|
||||
setPackage(packageName)
|
||||
}
|
||||
callIntent(intent, result)
|
||||
}
|
||||
|
||||
private fun callIntent(intent: Intent, result: Result) {
|
||||
try {
|
||||
mActivity.startActivity(intent)
|
||||
result.success(true)
|
||||
} catch (ex: ActivityNotFoundException) {
|
||||
result.success(false)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user