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

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, CustomShare3,
} }
object CustomShare { object CustomShare {
private val log = LogCategory("CustomShare") val log = LogCategory("CustomShare")
const val CN_CLIPBOARD = "<InApp>/CopyToClipboard" const val CN_CLIPBOARD = "<InApp>/CopyToClipboard"
private const val translate_app_component_default = private const val translate_app_component_default =
"com.google.android.apps.translate/com.google.android.apps.translate.TranslateActivity" "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( fun getCustomShareComponentName(
pref : SharedPreferences, pref : SharedPreferences,
@ -82,7 +74,8 @@ object CustomShare {
val cnStr = "${cn.packageName}/${cn.className}" val cnStr = "${cn.packageName}/${cn.className}"
label = cnStr label = cnStr
if(cnStr == CN_CLIPBOARD) { 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{ icon = ContextCompat.getDrawable(context, R.drawable.ic_copy)?.mutate()?.apply{
setTint(getAttributeColor(context,R.attr.colorVectorDrawable)) setTint(getAttributeColor(context,R.attr.colorVectorDrawable))
setTintMode(PorterDuff.Mode.SRC_IN) 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.Intent
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.content.pm.ResolveInfo import android.content.pm.ResolveInfo
import android.graphics.PorterDuff
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.os.Build import android.os.Build
import android.view.View import android.view.View
@ -15,16 +16,20 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import jp.juggler.subwaytooter.R import jp.juggler.subwaytooter.R
import jp.juggler.subwaytooter.action.CustomShare import jp.juggler.subwaytooter.action.CustomShare
import jp.juggler.subwaytooter.action.cn
import jp.juggler.util.* import jp.juggler.util.*
class DlgAppPicker( class DlgAppPicker(
val activity : AppCompatActivity, val activity : AppCompatActivity,
val callback : (String) -> Unit val callback : (String) -> Unit
) { ) {
companion object{
fun Char.isAlpha() = ('A' <= this && this <= 'Z')||('a' <= this && this <= 'z')
}
class ListItem( class ListItem(
val icon : Drawable?, val icon : Drawable?,
val text : CharSequence, val text : String,
val componentName : String val componentName : String
) )
@ -32,14 +37,14 @@ class DlgAppPicker(
val pm = activity.packageManager val pm = activity.packageManager
val listResolveInfo = pm.queryIntentActivities( val listResolveInfo = pm.queryIntentActivities(
Intent().apply{ Intent().apply {
action = Intent.ACTION_SEND action = Intent.ACTION_SEND
type = "text/plain" type = "text/plain"
putExtra(Intent.EXTRA_TEXT, activity.getString(R.string.content_sample)) 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 PackageManager.MATCH_ALL
}else { } else {
0 // PackageManager.MATCH_DEFAULT_ONLY 0 // PackageManager.MATCH_DEFAULT_ONLY
} }
) )
@ -49,20 +54,23 @@ class DlgAppPicker(
add( add(
ListItem( ListItem(
it.loadIcon(pm), it.loadIcon(pm),
it.loadLabel(pm)?.notEmpty() ?: cn, (it.loadLabel(pm)?.notEmpty() ?: cn).toString(),
cn cn
) )
) )
} }
add( val (label, icon) = CustomShare.getInfo(activity, CustomShare.CN_CLIPBOARD.cn())
ListItem( add(ListItem(icon, label.toString(), CustomShare.CN_CLIPBOARD))
ContextCompat.getDrawable(activity, R.mipmap.ic_launcher), sortWith(Comparator { a, b->
activity.getString(R.string.copy_to_clipboard), val a1 = a.text.firstOrNull() ?: '\u0000'
CustomShare.CN_CLIPBOARD val b1 = b.text.firstOrNull() ?: '\u0000'
) when {
) !a1.isAlpha() && b1.isAlpha() -> -1
sortBy { it.text.toString() } a1.isAlpha() && !b1.isAlpha() -> 1
else -> a.text.compareTo(b.text,ignoreCase = true)
}
})
} }
val dialog : AlertDialog val dialog : AlertDialog