Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shortcut icon #1486

Merged
merged 4 commits into from Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -3,18 +3,27 @@ package io.homeassistant.companion.android.settings.shortcuts
import android.content.Intent
import android.content.pm.ShortcutInfo
import android.content.pm.ShortcutManager
import android.graphics.Bitmap
import android.graphics.PorterDuff
import android.graphics.drawable.Icon
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.Menu
import androidx.annotation.RequiresApi
import androidx.core.graphics.drawable.DrawableCompat
import androidx.core.graphics.drawable.toBitmap
import androidx.preference.EditTextPreference
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceCategory
import androidx.preference.PreferenceFragmentCompat
import com.maltaisn.icondialog.IconDialog
import com.maltaisn.icondialog.IconDialogSettings
import com.maltaisn.icondialog.pack.IconPack
import com.maltaisn.icondialog.pack.IconPackLoader
import com.maltaisn.iconpack.mdi.createMaterialDesignIconPack
import io.homeassistant.companion.android.R
import io.homeassistant.companion.android.common.dagger.GraphComponentAccessor
import io.homeassistant.companion.android.common.data.integration.IntegrationRepository
Expand All @@ -24,7 +33,7 @@ import java.lang.Exception
import javax.inject.Inject
import kotlinx.coroutines.runBlocking

class ManageShortcutsSettingsFragment : PreferenceFragmentCompat() {
class ManageShortcutsSettingsFragment : PreferenceFragmentCompat(), IconDialog.Callback {

companion object {
private const val MAX_SHORTCUTS = 5
Expand All @@ -37,12 +46,17 @@ class ManageShortcutsSettingsFragment : PreferenceFragmentCompat() {
private const val DELETE_SUFFIX = "_delete"
private const val TYPE_SUFFIX = "_type"
private const val ENTITY_SUFFIX = "_entity_list"
private const val ICON_PREFIX = "_icon"
private const val TAG = "ManageShortcutFrag"
private const val ICON_DIALOG_TAG = "icon-dialog"
private var LAST_ICON_SELECT = ""
fun newInstance(): ManageShortcutsSettingsFragment {
return ManageShortcutsSettingsFragment()
}
}

private lateinit var iconPack: IconPack

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
Expand All @@ -67,6 +81,16 @@ class ManageShortcutsSettingsFragment : PreferenceFragmentCompat() {
override fun onResume() {
super.onResume()

val loader = IconPackLoader(requireContext())
iconPack = createMaterialDesignIconPack(loader)
iconPack.loadDrawables(loader.drawableLoader)
val settings = IconDialogSettings {
searchVisibility = IconDialog.SearchVisibility.ALWAYS
}
val iconDialog = IconDialog.newInstance(settings)
val iconId = 62017
onIconDialogIconsSelected(iconDialog, listOf(iconPack.icons[iconId]!!))

activity?.title = getString(R.string.shortcuts)
DaggerSettingsComponent.builder()
.appComponent((activity?.applicationContext as GraphComponentAccessor).appComponent)
Expand Down Expand Up @@ -133,6 +157,14 @@ class ManageShortcutsSettingsFragment : PreferenceFragmentCompat() {
}
}

findPreference<Preference>(SHORTCUT_PREFIX + i + ICON_PREFIX)?.let {
it.setOnPreferenceClickListener {
LAST_ICON_SELECT = SHORTCUT_PREFIX + i
iconDialog.show(childFragmentManager, ICON_DIALOG_TAG)
dshokouhi marked this conversation as resolved.
Show resolved Hide resolved
return@setOnPreferenceClickListener true
}
}

findPreference<EditTextPreference>(SHORTCUT_PREFIX + i + LABEL_SUFFIX)?.let {
it.title = "${getString(R.string.shortcut)} $i ${getString(R.string.label)}"
it.dialogTitle = "${getString(R.string.shortcut)} $i ${getString(R.string.label)}"
Expand Down Expand Up @@ -166,7 +198,7 @@ class ManageShortcutsSettingsFragment : PreferenceFragmentCompat() {
if (!shortcutLabel.isNullOrEmpty() && !shortcutDesc.isNullOrEmpty() && !shortcutPath.isNullOrEmpty()) {
if (shortcutType?.value == getString(R.string.entity_id))
shortcutPath = "entityId:${shortcutEntityList?.value}"
val shortcut = createShortcut(SHORTCUT_PREFIX + i, shortcutLabel!!, shortcutDesc!!, shortcutPath!!)
val shortcut = createShortcut(SHORTCUT_PREFIX + i, shortcutLabel!!, shortcutDesc!!, shortcutPath!!, findPreference<Preference>(SHORTCUT_PREFIX + i + ICON_PREFIX)?.icon?.toBitmap())
shortcutManager!!.addDynamicShortcuts(listOf(shortcut))
}
dynamicShortcuts = shortcutManager.dynamicShortcuts
Expand Down Expand Up @@ -263,6 +295,14 @@ class ManageShortcutsSettingsFragment : PreferenceFragmentCompat() {
}
}

findPreference<Preference>("pinned_shortcut_icon")?.let {
it.setOnPreferenceClickListener {
LAST_ICON_SELECT = "pinned"
iconDialog.show(childFragmentManager, ICON_DIALOG_TAG)
dshokouhi marked this conversation as resolved.
Show resolved Hide resolved
return@setOnPreferenceClickListener true
}
}

findPreference<EditTextPreference>("pinned_shortcut_label")?.let {
it.setOnPreferenceChangeListener { _, newValue ->
pinnedShortcutLabel = newValue.toString()
Expand Down Expand Up @@ -293,7 +333,7 @@ class ManageShortcutsSettingsFragment : PreferenceFragmentCompat() {
Log.d(TAG, "Attempt to add $pinnedShortcutId")
if (pinnedShortcutType?.value == getString(R.string.entity_id))
pinnedShortcutPath = "entityId:${pinnedShortcutEntityList?.value}"
val shortcut = createShortcut(pinnedShortcutId!!, pinnedShortcutLabel!!, pinnedShortcutDesc!!, pinnedShortcutPath!!)
val shortcut = createShortcut(pinnedShortcutId!!, pinnedShortcutLabel!!, pinnedShortcutDesc!!, pinnedShortcutPath!!, findPreference<Preference>("pinned_shortcut_icon")?.icon?.toBitmap())
var isNewPinned = true

for (item in pinnedShortcuts) {
Expand All @@ -318,14 +358,20 @@ class ManageShortcutsSettingsFragment : PreferenceFragmentCompat() {
}

@RequiresApi(Build.VERSION_CODES.N_MR1)
private fun createShortcut(shortcutId: String, shortcutLabel: String, shortcutDesc: String, shortcutPath: String): ShortcutInfo {
private fun createShortcut(shortcutId: String, shortcutLabel: String, shortcutDesc: String, shortcutPath: String, bitmap: Bitmap? = null): ShortcutInfo {
val intent = Intent(WebViewActivity.newInstance(requireContext(), shortcutPath).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
intent.action = shortcutPath
intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK)

return ShortcutInfo.Builder(requireContext(), shortcutId)
.setShortLabel(shortcutLabel)
.setLongLabel(shortcutDesc)
.setIcon(Icon.createWithResource(requireContext(), R.drawable.ic_stat_ic_notification_blue))
.setIcon(
if (bitmap != null)
Icon.createWithBitmap(bitmap)
else
Icon.createWithResource(requireContext(), R.drawable.ic_stat_ic_notification_blue)
)
.setIntent(intent)
.build()
}
Expand Down Expand Up @@ -355,4 +401,28 @@ class ManageShortcutsSettingsFragment : PreferenceFragmentCompat() {
}
}
}

override val iconDialogIconPack: IconPack
get() = iconPack

override fun onIconDialogIconsSelected(dialog: IconDialog, icons: List<com.maltaisn.icondialog.data.Icon>) {
Log.d(TAG, "Selected icon: ${icons.firstOrNull()}")
val selectedIcon = icons.firstOrNull()
if (selectedIcon != null) {
val iconDrawable = selectedIcon.drawable
if (iconDrawable != null) {
val icon = DrawableCompat.wrap(iconDrawable)
icon.setColorFilter(resources.getColor(R.color.colorAccent), PorterDuff.Mode.SRC_IN)
when (LAST_ICON_SELECT) {
dshokouhi marked this conversation as resolved.
Show resolved Hide resolved
"pinned" -> findPreference<Preference>("pinned_shortcut_icon")?.icon = icon
"${SHORTCUT_PREFIX}1" -> findPreference<Preference>(SHORTCUT_PREFIX + "1_icon")?.icon = icon
"${SHORTCUT_PREFIX}2" -> findPreference<Preference>(SHORTCUT_PREFIX + "2_icon")?.icon = icon
"${SHORTCUT_PREFIX}3" -> findPreference<Preference>(SHORTCUT_PREFIX + "3_icon")?.icon = icon
"${SHORTCUT_PREFIX}4" -> findPreference<Preference>(SHORTCUT_PREFIX + "4_icon")?.icon = icon
"${SHORTCUT_PREFIX}5" -> findPreference<Preference>(SHORTCUT_PREFIX + "5_icon")?.icon = icon
}
LAST_ICON_SELECT = ""
}
}
}
}
Expand Up @@ -25,6 +25,7 @@ object NotificationActionContentHandler {
} ?: WebViewActivity.newInstance(context)

intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
intent.flags = Intent.FLAG_ACTIVITY_MULTIPLE_TASK
dshokouhi marked this conversation as resolved.
Show resolved Hide resolved
context.startActivity(intent)
onComplete()
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Expand Up @@ -560,4 +560,5 @@ like to connect to:</string>
<string name="remaining">Remaining</string>
<string name="maximum">Maximum</string>
<string name="resets_at">Resets at</string>
<string name="shortcut_icon">Shortcut Icon</string>
</resources>
24 changes: 24 additions & 0 deletions app/src/main/res/xml/manage_shortcuts.xml
Expand Up @@ -11,6 +11,10 @@
<PreferenceCategory
android:key="shortcut1_category"
app:iconSpaceReserved="false">
<Preference
android:key="shortcut1_icon"
android:title="@string/shortcut_icon"
android:icon="@drawable/ic_stat_ic_notification_blue"/>
<EditTextPreference
android:key="shortcut1_label"
app:iconSpaceReserved="false"
Expand Down Expand Up @@ -49,6 +53,10 @@
<PreferenceCategory
android:key="shortcut2_category"
app:iconSpaceReserved="false">
<Preference
android:key="shortcut2_icon"
android:title="@string/shortcut_icon"
android:icon="@drawable/ic_stat_ic_notification_blue"/>
<EditTextPreference
android:key="shortcut2_label"
app:iconSpaceReserved="false"
Expand Down Expand Up @@ -87,6 +95,10 @@
<PreferenceCategory
android:key="shortcut3_category"
app:iconSpaceReserved="false">
<Preference
android:key="shortcut3_icon"
android:title="@string/shortcut_icon"
android:icon="@drawable/ic_stat_ic_notification_blue"/>
<EditTextPreference
android:key="shortcut3_label"
app:iconSpaceReserved="false"
Expand Down Expand Up @@ -125,6 +137,10 @@
<PreferenceCategory
android:key="shortcut4_category"
app:iconSpaceReserved="false">
<Preference
android:key="shortcut4_icon"
android:title="@string/shortcut_icon"
android:icon="@drawable/ic_stat_ic_notification_blue"/>
<EditTextPreference
android:key="shortcut4_label"
app:iconSpaceReserved="false"
Expand Down Expand Up @@ -168,6 +184,10 @@
android:summary="@string/shortcut5_note"
app:iconSpaceReserved="false"
android:selectable="false" />
<Preference
android:key="shortcut5_icon"
android:title="@string/shortcut_icon"
android:icon="@drawable/ic_stat_ic_notification_blue"/>
<EditTextPreference
android:key="shortcut5_label"
app:iconSpaceReserved="false"
Expand Down Expand Up @@ -224,6 +244,10 @@
android:title="@string/shortcut_pinned_id"
app:useSimpleSummaryProvider="true"
app:iconSpaceReserved="false" />
<Preference
android:key="pinned_shortcut_icon"
android:title="@string/shortcut_icon"
android:icon="@drawable/ic_stat_ic_notification_blue"/>
<EditTextPreference
android:key="pinned_shortcut_label"
android:title="@string/shortcut_pinned_label"
Expand Down