object PicShareUtils {
val PACKAGE_NAME_FB = "com.facebook.katana"
val PACKAGE_NAME_WHATSAPP = "com.whatsapp"
val PACKAGE_NAME_INSTAGRAM = "com.instagram.android"
private val SHARE_TYPE_TEXT = "text/plain"
private val SHARE_TYPE_IMAGE = "image/*"
val REQUEST_CODE = 10
/**
* 指定包名分享
*
* @param packName 如果==null则跳系统分享
* @param content
* @return
*/
fun doPicShare(context: Activity?, packName: String?, inviteCode: String): Boolean {
if (context == null)
return false
val bitmap = createShareBitmap(context, inviteCode)
val uri = Uri.parse(MediaStore.Images.Media.insertImage(MainApp.S_INSTANCE.contentResolver, bitmap, null, null))
if (uri == null) {
return false
}
val intent: Intent = (if (packName != null)
queryShareIntent(context, packName)
else
Intent("android.intent.action.VIEW")) ?: return false
intent.action = Intent.ACTION_SEND//设置分享行为
intent.type = SHARE_TYPE_IMAGE
intent.putExtra(Intent.EXTRA_SUBJECT, context.getText(R.string.app_name))
intent.putExtra(Intent.EXTRA_TITLE,context.getText(R.string.app_name))
intent.putExtra(Intent.EXTRA_STREAM, uri)
context.startActivityForResult(Intent.createChooser(intent, context.getText(R.string.app_name)), REQUEST_CODE)
return true
}
/**
* 生成分享自定义主题的bitmap图片
* screenShot 对应一个自定义主题bitmap的路径
*/
fun createShareBitmap(context: Context, screenShot: String): Bitmap {
val themeBitmap = BitmapFactory.decodeFile(screenShot)
val inflater = LayoutInflater.from(context)
val inviteView = inflater.inflate(R.layout.custom_share_item, null)
val img = inviteView.findViewById(R.id.custom_theme_img) as ImageView
val draw = BitmapDrawable(themeBitmap)
img.image = draw
inviteView.isDrawingCacheEnabled = true
inviteView.measure(
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED))
inviteView.layout(0, 0,
inviteView.measuredWidth,
inviteView.measuredHeight)
inviteView.buildDrawingCache()
val cacheBitmap = inviteView.drawingCache
return Bitmap.createBitmap(cacheBitmap)
}
private fun getShareIntent(context: Context, type: String): Intent {
// 去掉后缀-魏工
val intent = Intent(Intent.ACTION_SEND)
intent.type = type
intent.putExtra(Intent.EXTRA_SUBJECT, context.getString(R.string.app_name))
intent.putExtra(Intent.EXTRA_TITLE, context.getString(R.string.app_name))
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
return intent
}
private fun queryShareIntent(context: Context, packName: String): Intent? {
val defIntent = getShareIntent(context, SHARE_TYPE_TEXT)
val shareTextApps = context.packageManager.queryIntentActivities(defIntent, 0)
val shareImageIntent = getShareIntent(context, SHARE_TYPE_IMAGE)
val otherApps = context.packageManager.queryIntentActivities(shareImageIntent, 0)
otherApps.addAll(shareTextApps) // 考虑到Instagram只能分享图片,因此需要合并2个可分享的应用列表
for (otherApp in otherApps) {
val otherAppActivity = otherApp.activityInfo
val shareIntent: Intent
if (TextUtils.equals(packName, otherAppActivity.packageName)) {
if (TextUtils.equals(packName, PACKAGE_NAME_FB)) { // 如果是fb则统计修改参数
shareIntent = getShareIntent(context, SHARE_TYPE_TEXT)
} else {
shareIntent = Intent(defIntent)
}
val componentName = ComponentName(
otherAppActivity.applicationInfo.packageName,
otherAppActivity.name
)
shareIntent.component = componentName
return shareIntent
}
}
return null
}
/**
* 手机是否安装某个app
*/
fun isAppInstalled(context: Context, packageName: String?): Boolean {
val pm = context.packageManager
//如果包名为空,返回true,则进行系统分享(其他情况慎用)
if(packageName == null) return true
return try {
pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES)
true
} catch (e: PackageManager.NameNotFoundException) {
false
}
}
}