Skip to content

Commit

Permalink
feat(DockService): Basic icon pack support
Browse files Browse the repository at this point in the history
  • Loading branch information
axel358 committed Aug 20, 2024
1 parent f102765 commit 212657f
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ open class LauncherActivity : AppCompatActivity(), OnAppClickListener {

fun loadDesktopApps() {
appsGv.adapter = AppAdapter(this,
AppUtils.getPinnedApps(this, AppUtils.DESKTOP_LIST), this, true)
AppUtils.getPinnedApps(this, AppUtils.DESKTOP_LIST), this, true, null)
}

override fun onResume() {
Expand Down
54 changes: 42 additions & 12 deletions app/src/main/java/cu/axel/smartdock/adapters/AppAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.graphics.Typeface
import android.text.Spannable
import android.text.SpannableString
import android.text.style.StyleSpan
import android.util.Log
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
Expand All @@ -17,11 +18,17 @@ import androidx.recyclerview.widget.RecyclerView
import cu.axel.smartdock.models.App
import cu.axel.smartdock.R
import cu.axel.smartdock.utils.ColorUtils
import cu.axel.smartdock.utils.IconPackUtils
import cu.axel.smartdock.utils.Utils
import java.util.Locale

class AppAdapter(private val context: Context, private var apps: ArrayList<App>,
private val listener: OnAppClickListener, private val large: Boolean) : RecyclerView.Adapter<AppAdapter.ViewHolder>() {
class AppAdapter(
private val context: Context,
private var apps: ArrayList<App>,
private val listener: OnAppClickListener,
private val large: Boolean,
val iconPackUtils: IconPackUtils?
) : RecyclerView.Adapter<AppAdapter.ViewHolder>() {
private val allApps: ArrayList<App> = ArrayList(apps)
private var iconBackground = 0
private val iconPadding: Int
Expand All @@ -35,7 +42,8 @@ class AppAdapter(private val context: Context, private var apps: ArrayList<App>,

init {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
iconPadding = Utils.dpToPx(context, sharedPreferences.getString("icon_padding", "5")!!.toInt())
iconPadding =
Utils.dpToPx(context, sharedPreferences.getString("icon_padding", "5")!!.toInt())
singleLine = sharedPreferences.getBoolean("single_line_labels", true)
when (sharedPreferences.getString("icon_shape", "circle")) {
"circle" -> iconBackground = R.drawable.circle
Expand All @@ -46,32 +54,44 @@ class AppAdapter(private val context: Context, private var apps: ArrayList<App>,

override fun onCreateViewHolder(parent: ViewGroup, arg1: Int): ViewHolder {
val itemLayoutView = LayoutInflater.from(context)
.inflate(if (large) R.layout.app_entry_large else R.layout.app_entry, null)
.inflate(if (large) R.layout.app_entry_large else R.layout.app_entry, null)
return ViewHolder(itemLayoutView)
}

override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
val app = apps[position]
val name = app.name
if (::query.isInitialized) {
val spanStart = name.lowercase(Locale.getDefault()).indexOf(query.lowercase(Locale.getDefault()))
val spanStart =
name.lowercase(Locale.getDefault()).indexOf(query.lowercase(Locale.getDefault()))
val spanEnd = spanStart + query.length
if (spanStart != -1) {
val spannable = SpannableString(name)
spannable.setSpan(StyleSpan(Typeface.BOLD), spanStart, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
spannable.setSpan(
StyleSpan(Typeface.BOLD),
spanStart,
spanEnd,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
viewHolder.nameTv.text = spannable
} else {
viewHolder.nameTv.text = name
}
} else {
viewHolder.nameTv.text = name
}
viewHolder.iconIv.setImageDrawable(app.icon)

if (iconPackUtils != null)
viewHolder.iconIv.setImageDrawable(iconPackUtils.getAppThemedIcon(app.packageName))
else
viewHolder.iconIv.setImageDrawable(app.icon)
if (iconBackground != -1) {
viewHolder.iconIv.setPadding(iconPadding, iconPadding, iconPadding, iconPadding)
viewHolder.iconIv.setBackgroundResource(iconBackground)
ColorUtils.applyColor(viewHolder.iconIv,
ColorUtils.getDrawableDominantColor(viewHolder.iconIv.drawable))
ColorUtils.applyColor(
viewHolder.iconIv,
ColorUtils.getDrawableDominantColor(viewHolder.iconIv.drawable)
)
}
viewHolder.bind(app, listener)
}
Expand All @@ -84,11 +104,21 @@ class AppAdapter(private val context: Context, private var apps: ArrayList<App>,
val results = ArrayList<App>()
if (query.length > 1) {
if (query.matches("^[0-9]+(\\.[0-9]+)?[-+/*][0-9]+(\\.[0-9]+)?".toRegex())) {
results.add(App(Utils.solve(query).toString() + "", context.packageName + ".calc",
ResourcesCompat.getDrawable(context.resources, R.drawable.ic_calculator, context.theme)!!))
results.add(
App(
Utils.solve(query).toString() + "", context.packageName + ".calc",
ResourcesCompat.getDrawable(
context.resources,
R.drawable.ic_calculator,
context.theme
)!!
)
)
} else {
for (app in allApps) {
if (app.name.lowercase(Locale.getDefault()).contains(query.lowercase(Locale.getDefault()))) results.add(app)
if (app.name.lowercase(Locale.getDefault())
.contains(query.lowercase(Locale.getDefault()))
) results.add(app)
}
}
apps = results
Expand Down
30 changes: 21 additions & 9 deletions app/src/main/java/cu/axel/smartdock/adapters/DockAppAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import cu.axel.smartdock.R
import cu.axel.smartdock.models.DockApp
import cu.axel.smartdock.utils.AppUtils
import cu.axel.smartdock.utils.ColorUtils
import cu.axel.smartdock.utils.IconPackUtils
import cu.axel.smartdock.utils.Utils

class DockAppAdapter(private val context: Context, private val apps: ArrayList<DockApp>,
private val listener: OnDockAppClickListener) : RecyclerView.Adapter<DockAppAdapter.ViewHolder>() {
class DockAppAdapter(
private val context: Context, private val apps: ArrayList<DockApp>,
private val listener: OnDockAppClickListener, private val iconPackUtils: IconPackUtils?
) : RecyclerView.Adapter<DockAppAdapter.ViewHolder>() {
private var iconBackground = 0
private val iconPadding: Int
private val iconTheming: Boolean
Expand All @@ -30,7 +33,8 @@ class DockAppAdapter(private val context: Context, private val apps: ArrayList<D
init {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
iconTheming = sharedPreferences.getString("icon_pack", "") != ""
iconPadding = Utils.dpToPx(context, sharedPreferences.getString("icon_padding", "5")!!.toInt())
iconPadding =
Utils.dpToPx(context, sharedPreferences.getString("icon_padding", "5")!!.toInt())
tintIndicators = sharedPreferences.getBoolean("tint_indicators", false)
when (sharedPreferences.getString("icon_shape", "circle")) {
"circle" -> iconBackground = R.drawable.circle
Expand All @@ -40,16 +44,19 @@ class DockAppAdapter(private val context: Context, private val apps: ArrayList<D
}

override fun onCreateViewHolder(parent: ViewGroup, arg1: Int): ViewHolder {
val itemLayoutView = LayoutInflater.from(parent.context).inflate(R.layout.app_task_entry, null)
val itemLayoutView =
LayoutInflater.from(parent.context).inflate(R.layout.app_task_entry, null)
return ViewHolder(itemLayoutView)
}

override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
val app = apps[position]
val size = app.tasks.size
if (size > 0) {
if (tintIndicators) ColorUtils.applyColor(viewHolder.runningIndicator,
ColorUtils.manipulateColor(ColorUtils.getDrawableDominantColor(app.icon), 2f))
if (tintIndicators) ColorUtils.applyColor(
viewHolder.runningIndicator,
ColorUtils.manipulateColor(ColorUtils.getDrawableDominantColor(app.icon), 2f)
)
if (app.tasks[0].id != -1) viewHolder.runningIndicator.alpha = 1f
if (app.packageName == AppUtils.currentApp) {
viewHolder.runningIndicator.layoutParams.width = Utils.dpToPx(context, 16)
Expand All @@ -59,12 +66,17 @@ class DockAppAdapter(private val context: Context, private val apps: ArrayList<D
viewHolder.taskCounter.alpha = 1f
}
}
viewHolder.iconIv.setImageDrawable(app.icon)
if (iconPackUtils != null)
viewHolder.iconIv.setImageDrawable(iconPackUtils.getAppThemedIcon(app.packageName))
else
viewHolder.iconIv.setImageDrawable(app.icon)
if (iconBackground != -1) {
viewHolder.iconIv.setPadding(iconPadding, iconPadding, iconPadding, iconPadding)
viewHolder.iconIv.setBackgroundResource(iconBackground)
ColorUtils.applyColor(viewHolder.iconIv,
ColorUtils.getDrawableDominantColor(viewHolder.iconIv.drawable))
ColorUtils.applyColor(
viewHolder.iconIv,
ColorUtils.getDrawableDominantColor(viewHolder.iconIv.drawable)
)
}
viewHolder.bind(app, listener)
}
Expand Down
24 changes: 19 additions & 5 deletions app/src/main/java/cu/axel/smartdock/services/DockService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import android.os.Handler
import android.os.Looper
import android.os.Process
import android.provider.Settings
import android.util.Log
import android.view.ContextThemeWrapper
import android.view.Display
import android.view.GestureDetector
Expand Down Expand Up @@ -87,6 +88,7 @@ import cu.axel.smartdock.utils.AppUtils
import cu.axel.smartdock.utils.ColorUtils
import cu.axel.smartdock.utils.DeepShortcutManager
import cu.axel.smartdock.utils.DeviceUtils
import cu.axel.smartdock.utils.IconPackUtils
import cu.axel.smartdock.utils.OnSwipeListener
import cu.axel.smartdock.utils.Utils
import cu.axel.smartdock.widgets.HoverInterceptorLayout
Expand Down Expand Up @@ -159,6 +161,7 @@ class DockService : AccessibilityService(), OnSharedPreferenceChangeListener, On
private var dockHeight: Int = 0
private lateinit var handleLayoutParams: WindowManager.LayoutParams
private lateinit var launcherApps: LauncherApps
private var iconPackUtils: IconPackUtils? = null
override fun onCreate() {
super.onCreate()
db = DBHelper(this)
Expand All @@ -172,6 +175,9 @@ class DockService : AccessibilityService(), OnSharedPreferenceChangeListener, On
bluetoothManager = getSystemService(BLUETOOTH_SERVICE) as BluetoothManager
launcherApps = getSystemService(LAUNCHER_APPS_SERVICE) as LauncherApps
dockHandler = Handler(Looper.getMainLooper())
if (sharedPreferences.getString("icon_pack", "")!!.isNotEmpty()) {
iconPackUtils = IconPackUtils(this)
}
}

override fun onServiceConnected() {
Expand Down Expand Up @@ -1171,7 +1177,7 @@ class DockService : AccessibilityService(), OnSharedPreferenceChangeListener, On
//TODO: Implement efficient adapter
appsGv.adapter = AppAdapter(
context, apps, this@DockService,
menuFullscreen && !phoneLayout
menuFullscreen && !phoneLayout, iconPackUtils
)
}
}
Expand Down Expand Up @@ -1391,10 +1397,17 @@ class DockService : AccessibilityService(), OnSharedPreferenceChangeListener, On
applyTheme()
else if (preference == "menu_icon_uri")
updateMenuIcon()
else if (preference.startsWith("icon_") || preference == "tint_indicators") {
else if (preference.startsWith("icon_")) {
val iconPack = sharedPreferences.getString("icon_pack", "")!!
iconPackUtils = if (iconPack.isNotEmpty()) {
IconPackUtils(this)
} else
null
updateRunningTasks()
loadFavoriteApps()
} else if (preference == "lock_landscape")
} else if (preference == "tint_indicators")
updateRunningTasks()
else if (preference == "lock_landscape")
setOrientation()
else if (preference == "center_running_apps") {
placeRunningApps()
Expand Down Expand Up @@ -1495,7 +1508,7 @@ class DockService : AccessibilityService(), OnSharedPreferenceChangeListener, On
if (index != -1) apps[index].addTask(task) else apps.add(DockApp(task))
}
tasksGv.layoutParams.width = gridSize * apps.size
tasksGv.adapter = DockAppAdapter(context, apps, this)
tasksGv.adapter = DockAppAdapter(context, apps, this, iconPackUtils)

//TODO: Move context outta here
wifiBtn.setImageResource(if (wifiManager.isWifiEnabled) R.drawable.ic_wifi_on else R.drawable.ic_wifi_off)
Expand Down Expand Up @@ -1760,7 +1773,8 @@ class DockService : AccessibilityService(), OnSharedPreferenceChangeListener, On
toggleFavorites(apps.size > 0)
val menuFullscreen = sharedPreferences.getBoolean("app_menu_fullscreen", false)
val phoneLayout = sharedPreferences.getInt("dock_layout", -1) == 0
favoritesGv.adapter = AppAdapter(context, apps, this, menuFullscreen && !phoneLayout)
favoritesGv.adapter =
AppAdapter(context, apps, this, menuFullscreen && !phoneLayout, iconPackUtils)
}

fun takeScreenshot() {
Expand Down
46 changes: 17 additions & 29 deletions app/src/main/java/cu/axel/smartdock/utils/IconPackUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cu.axel.smartdock.utils

import android.content.ComponentName
import android.content.Context
import android.content.SharedPreferences
import android.content.pm.ActivityInfo
import android.content.pm.PackageManager
import android.content.res.Resources
Expand Down Expand Up @@ -34,20 +33,10 @@ class IconPackUtils(val context: Context) {
private val iconBackList: MutableList<Drawable> = ArrayList()
private val iconBackStrings: MutableList<String> = ArrayList()
private var iconScale = 0f
private var currentIconPack: String? = ""
private var loading = false

init {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
currentIconPack = sharedPreferences.getString("icon_pack", "")

try {
loadIconPack()
} catch (i: NullPointerException) {
//Icon Pack is not supported so wipe the icon pack data
//sharedPreferences.edit().putString("icon_pack", "").apply()
}

loadIconPack()
}


Expand Down Expand Up @@ -149,24 +138,24 @@ class IconPackUtils(val context: Context) {
}

private fun loadIconPack() {
val packageName = currentIconPack
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
val currentIconPack = sharedPreferences.getString("icon_pack", "")
iconBackList.clear()
iconBackStrings.clear()
if (packageName.isNullOrEmpty())
if (currentIconPack.isNullOrEmpty())
return

loading = true
iconPackResources = getIconPackResources(context, packageName)
iconPackResources = getIconPackResources(context, currentIconPack)
val resources: Resources
try {
resources = context.packageManager.getResourcesForApplication(packageName)
resources = context.packageManager.getResourcesForApplication(currentIconPack)
} catch (e: PackageManager.NameNotFoundException) {
e.printStackTrace()
loading = false
return
}
loadedIconPackResource = resources
loadedIconPackName = packageName
loadedIconPackName = currentIconPack
iconMask = getDrawableForName(ICON_MASK_TAG)
mIconUpon = getDrawableForName(ICON_UPON_TAG)
for (i in iconBackStrings.indices) {
Expand All @@ -191,29 +180,28 @@ class IconPackUtils(val context: Context) {
context: Context,
packageName: String
): Map<String, String?>? {
if (TextUtils.isEmpty(packageName)) {
if (packageName.isEmpty())
return null
}
val res: Resources = try {

val resources: Resources = try {
context.packageManager.getResourcesForApplication(packageName)
} catch (e: PackageManager.NameNotFoundException) {
e.printStackTrace()
return null
}
var parser: XmlPullParser? = null
var inputStream: InputStream? = null
val iconPackResources: MutableMap<String, String?> = HashMap()
try {
inputStream = res.assets.open("appfilter.xml")
inputStream = resources.assets.open("appfilter.xml")
val factory = XmlPullParserFactory.newInstance()
parser = factory.newPullParser()
parser.setInput(inputStream, "UTF-8")
} catch (e: Exception) {
// Catch any exception since we want to fall back to parsing the xml/
// resource in all cases
val resId = res.getIdentifier("appfilter", "xml", packageName)
val resId = resources.getIdentifier("appfilter", "xml", packageName)
if (resId != 0) {
parser = res.getXml(resId)
parser = resources.getXml(resId)
}
}
if (parser != null) {
Expand All @@ -239,12 +227,12 @@ class IconPackUtils(val context: Context) {
}

// Application uses a different theme format (most likely launcher pro)
var arrayId = res.getIdentifier("theme_iconpack", "array", packageName)
var arrayId = resources.getIdentifier("theme_iconpack", "array", packageName)
if (arrayId == 0) {
arrayId = res.getIdentifier("icon_pack", "array", packageName)
arrayId = resources.getIdentifier("icon_pack", "array", packageName)
}
if (arrayId != 0) {
val iconPack = res.getStringArray(arrayId)
val iconPack = resources.getStringArray(arrayId)
for (entry in iconPack) {
if (TextUtils.isEmpty(entry)) {
continue
Expand Down Expand Up @@ -316,7 +304,7 @@ class IconPackUtils(val context: Context) {
return loadedIconPackResource!!.getIdentifier(resource, "drawable", loadedIconPackName)
}

fun getIconPackResources(id: Int, mContext: Context): Drawable? {
private fun getIconPackResources(id: Int, mContext: Context): Drawable? {
return ResourcesCompat.getDrawable(loadedIconPackResource!!, id, mContext.theme)
}

Expand Down

0 comments on commit 212657f

Please sign in to comment.