アプリ選択時のソート順や表記を調整

This commit is contained in:
tateisu 2020-01-31 21:06:11 +09:00
parent b2dceb3efc
commit 947fab0340
2 changed files with 39 additions and 27 deletions

View File

@ -24,25 +24,17 @@ enum class CustomShareTarget {
CustomShare3,
}
object CustomShare {
private val log = LogCategory("CustomShare")
val log = LogCategory("CustomShare")
const val CN_CLIPBOARD = "<InApp>/CopyToClipboard"
private const val translate_app_component_default =
"com.google.android.apps.translate/com.google.android.apps.translate.TranslateActivity"
// convert "pkgName/className" string to ComponentName object.
private fun String.cn() : ComponentName? {
try {
val idx = indexOf('/')
if(idx >= 1) return ComponentName(substring(0 until idx), substring(idx + 1))
} catch(ex : Throwable) {
log.e(ex, "incorrect component name $this")
}
return null
}
fun getCustomShareComponentName(
pref : SharedPreferences,
@ -82,7 +74,8 @@ object CustomShare {
val cnStr = "${cn.packageName}/${cn.className}"
label = cnStr
if(cnStr == CN_CLIPBOARD) {
label = context.getString(R.string.copy_to_clipboard)
label =
"${context.getString(R.string.copy_to_clipboard)}(${context.getString(R.string.app_name)})"
icon = ContextCompat.getDrawable(context, R.drawable.ic_copy)?.mutate()?.apply{
setTint(getAttributeColor(context,R.attr.colorVectorDrawable))
setTintMode(PorterDuff.Mode.SRC_IN)
@ -184,3 +177,14 @@ object CustomShare {
}
}
}
// convert "pkgName/className" string to ComponentName object.
fun String.cn() : ComponentName? {
try {
val idx = indexOf('/')
if(idx >= 1) return ComponentName(substring(0 until idx), substring(idx + 1))
} catch(ex : Throwable) {
CustomShare.log.e(ex, "incorrect component name $this")
}
return null
}

View File

@ -5,6 +5,7 @@ import android.app.AlertDialog
import android.content.Intent
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import android.graphics.PorterDuff
import android.graphics.drawable.Drawable
import android.os.Build
import android.view.View
@ -15,16 +16,20 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import jp.juggler.subwaytooter.R
import jp.juggler.subwaytooter.action.CustomShare
import jp.juggler.subwaytooter.action.cn
import jp.juggler.util.*
class DlgAppPicker(
val activity : AppCompatActivity,
val callback : (String) -> Unit
) {
companion object{
fun Char.isAlpha() = ('A' <= this && this <= 'Z')||('a' <= this && this <= 'z')
}
class ListItem(
val icon : Drawable?,
val text : CharSequence,
val text : String,
val componentName : String
)
@ -32,14 +37,14 @@ class DlgAppPicker(
val pm = activity.packageManager
val listResolveInfo = pm.queryIntentActivities(
Intent().apply{
Intent().apply {
action = Intent.ACTION_SEND
type = "text/plain"
putExtra(Intent.EXTRA_TEXT, activity.getString(R.string.content_sample))
},
if( Build.VERSION.SDK_INT >= 23 ) {
if(Build.VERSION.SDK_INT >= 23) {
PackageManager.MATCH_ALL
}else {
} else {
0 // PackageManager.MATCH_DEFAULT_ONLY
}
)
@ -49,20 +54,23 @@ class DlgAppPicker(
add(
ListItem(
it.loadIcon(pm),
it.loadLabel(pm)?.notEmpty() ?: cn,
(it.loadLabel(pm)?.notEmpty() ?: cn).toString(),
cn
)
)
}
add(
ListItem(
ContextCompat.getDrawable(activity, R.mipmap.ic_launcher),
activity.getString(R.string.copy_to_clipboard),
CustomShare.CN_CLIPBOARD
)
)
sortBy { it.text.toString() }
val (label, icon) = CustomShare.getInfo(activity, CustomShare.CN_CLIPBOARD.cn())
add(ListItem(icon, label.toString(), CustomShare.CN_CLIPBOARD))
sortWith(Comparator { a, b->
val a1 = a.text.firstOrNull() ?: '\u0000'
val b1 = b.text.firstOrNull() ?: '\u0000'
when {
!a1.isAlpha() && b1.isAlpha() -> -1
a1.isAlpha() && !b1.isAlpha() -> 1
else -> a.text.compareTo(b.text,ignoreCase = true)
}
})
}
val dialog : AlertDialog