Skip to content

Commit

Permalink
Merge pull request axel358#168 from axel358/icon_packs
Browse files Browse the repository at this point in the history
Restore icon pack support, closes axel358#154
  • Loading branch information
axel358 authored Sep 7, 2024
2 parents d58c23d + 8fe097b commit f75a547
Show file tree
Hide file tree
Showing 13 changed files with 767 additions and 185 deletions.
127 changes: 97 additions & 30 deletions app/src/main/java/cu/axel/smartdock/activities/LauncherActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.SharedPreferences
import android.content.SharedPreferences.OnSharedPreferenceChangeListener
import android.content.pm.ShortcutInfo
import android.net.Uri
import android.os.Bundle
Expand Down Expand Up @@ -41,6 +42,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.Utils
import java.io.BufferedReader
import java.io.File
Expand All @@ -51,7 +53,9 @@ import java.io.IOException
const val LAUNCHER_ACTION = "launcher_action"
const val LAUNCHER_RESUMED = "launcher_resumed"

open class LauncherActivity : AppCompatActivity(), OnAppClickListener {
open class LauncherActivity : AppCompatActivity(), OnAppClickListener,
OnSharedPreferenceChangeListener {
private var iconPackUtils: IconPackUtils? = null
private lateinit var serviceBtn: MaterialButton
private lateinit var appsGv: RecyclerView
private lateinit var notesEt: EditText
Expand All @@ -68,7 +72,7 @@ open class LauncherActivity : AppCompatActivity(), OnAppClickListener {
notesEt = findViewById(R.id.notes_et)
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
serviceBtn
.setOnClickListener { startActivity(Intent(this, MainActivity::class.java)) }
.setOnClickListener { startActivity(Intent(this, MainActivity::class.java)) }
backgroundLayout.setOnLongClickListener {
val view = LayoutInflater.from(this).inflate(R.layout.task_list, null)
val layoutParams = Utils.makeWindowParams(-2, -2, this)
Expand All @@ -92,8 +96,16 @@ open class LauncherActivity : AppCompatActivity(), OnAppClickListener {
actionsLv.adapter = AppActionsAdapter(this, actions)
actionsLv.setOnItemClickListener { adapterView, _, position, _ ->
val action = adapterView.getItemAtPosition(position) as Action
if (action.text == getString(R.string.change_wallpaper)) startActivityForResult(Intent.createChooser(Intent(Intent.ACTION_SET_WALLPAPER),
getString(R.string.change_wallpaper)), 18) else if (action.text == getString(R.string.display_settings)) startActivity(Intent(Settings.ACTION_DISPLAY_SETTINGS))
if (action.text == getString(R.string.change_wallpaper)) startActivityForResult(
Intent.createChooser(
Intent(Intent.ACTION_SET_WALLPAPER),
getString(R.string.change_wallpaper)
), 18
) else if (action.text == getString(R.string.display_settings)) startActivity(
Intent(
Settings.ACTION_DISPLAY_SETTINGS
)
)
windowManager.removeView(view)
}
windowManager.addView(view, layoutParams)
Expand All @@ -104,30 +116,40 @@ open class LauncherActivity : AppCompatActivity(), OnAppClickListener {
y = event.y
false
}
ContextCompat.registerReceiver(this, object : BroadcastReceiver() {
override fun onReceive(p1: Context, intent: Intent) {
when (intent.getStringExtra("action")) {
DOCK_SERVICE_CONNECTED -> serviceBtn.visibility = View.GONE
DESKTOP_APP_PINNED -> loadDesktopApps()
ContextCompat.registerReceiver(
this, object : BroadcastReceiver() {
override fun onReceive(p1: Context, intent: Intent) {
when (intent.getStringExtra("action")) {
DOCK_SERVICE_CONNECTED -> serviceBtn.visibility = View.GONE
DESKTOP_APP_PINNED -> loadDesktopApps()
}
}
}
}, IntentFilter(DOCK_SERVICE_ACTION),
ContextCompat.RECEIVER_NOT_EXPORTED)
}, IntentFilter(DOCK_SERVICE_ACTION),
ContextCompat.RECEIVER_NOT_EXPORTED
)

if (sharedPreferences.getString("icon_pack", "")!!.isNotEmpty()) {
iconPackUtils = IconPackUtils(this)
}
}

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

override fun onResume() {
super.onResume()
sendBroadcast(Intent(LAUNCHER_ACTION)
sendBroadcast(
Intent(LAUNCHER_ACTION)
.setPackage(packageName)
.putExtra("action", LAUNCHER_RESUMED)
)

serviceBtn.visibility = if (DeviceUtils.isAccessibilityServiceEnabled(this)) View.GONE else View.VISIBLE
serviceBtn.visibility =
if (DeviceUtils.isAccessibilityServiceEnabled(this)) View.GONE else View.VISIBLE

loadDesktopApps()

Expand Down Expand Up @@ -176,17 +198,21 @@ open class LauncherActivity : AppCompatActivity(), OnAppClickListener {
}

private fun launchApp(mode: String?, app: String) {
sendBroadcast(Intent(LAUNCHER_ACTION)
sendBroadcast(
Intent(LAUNCHER_ACTION)
.setPackage(packageName)
.putExtra("action", ACTION_LAUNCH_APP)
.putExtra("mode", mode)
.putExtra("app", app))
.putExtra("app", app)
)
}

private fun getAppActions(app: String): ArrayList<Action> {
val actions = ArrayList<Action>()
if (DeepShortcutManager.hasHostPermission(this)) {
if (DeepShortcutManager.getShortcuts(app, this)!!.isNotEmpty()) actions.add(Action(R.drawable.ic_shortcuts, getString(R.string.shortcuts)))
if (DeepShortcutManager.getShortcuts(app, this)!!
.isNotEmpty()
) actions.add(Action(R.drawable.ic_shortcuts, getString(R.string.shortcuts)))
}
actions.add(Action(R.drawable.ic_manage, getString(R.string.manage)))
actions.add(Action(R.drawable.ic_launch_mode, getString(R.string.open_as)))
Expand All @@ -201,7 +227,8 @@ open class LauncherActivity : AppCompatActivity(), OnAppClickListener {
val layoutParams = Utils.makeWindowParams(-2, -2, this)
ColorUtils.applyMainColor(this, sharedPreferences, view)
layoutParams.gravity = Gravity.TOP or Gravity.START
layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
layoutParams.flags =
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
val location = IntArray(2)
anchor.getLocationOnScreen(location)
layoutParams.x = location[0]
Expand All @@ -223,14 +250,24 @@ open class LauncherActivity : AppCompatActivity(), OnAppClickListener {
actions.add(Action(R.drawable.ic_arrow_back, ""))
actions.add(Action(R.drawable.ic_info, getString(R.string.app_info)))
if (!AppUtils.isSystemApp(this, app.packageName)
|| sharedPreferences.getBoolean("allow_sysapp_uninstall", false)) actions.add(Action(R.drawable.ic_uninstall, getString(R.string.uninstall)))
if (sharedPreferences.getBoolean("allow_app_freeze", false)) actions.add(Action(R.drawable.ic_freeze, getString(R.string.freeze)))
|| sharedPreferences.getBoolean("allow_sysapp_uninstall", false)
) actions.add(
Action(
R.drawable.ic_uninstall,
getString(R.string.uninstall)
)
)
if (sharedPreferences.getBoolean("allow_app_freeze", false)) actions.add(
Action(R.drawable.ic_freeze, getString(R.string.freeze))
)
actionsLv.adapter = AppActionsAdapter(this, actions)
}

getString(R.string.shortcuts) -> {
actionsLv.adapter = AppShortcutAdapter(this,
DeepShortcutManager.getShortcuts(app.packageName, this)!!)
actionsLv.adapter = AppShortcutAdapter(
this,
DeepShortcutManager.getShortcuts(app.packageName, this)!!
)
}

"" -> {
Expand All @@ -243,20 +280,34 @@ open class LauncherActivity : AppCompatActivity(), OnAppClickListener {
actions.add(Action(R.drawable.ic_standard, getString(R.string.standard)))
actions.add(Action(R.drawable.ic_maximized, getString(R.string.maximized)))
actions.add(Action(R.drawable.ic_portrait, getString(R.string.portrait)))
actions.add(Action(R.drawable.ic_fullscreen, getString(R.string.fullscreen)))
actions.add(
Action(
R.drawable.ic_fullscreen,
getString(R.string.fullscreen)
)
)
actionsLv.adapter = AppActionsAdapter(this, actions)
}

getString(R.string.app_info) -> {
startActivity(Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
.setData(Uri.parse("package:$app")).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
startActivity(
Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
.setData(Uri.parse("package:$app"))
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
)
windowManager.removeView(view)
}

getString(R.string.uninstall) -> {
@Suppress("DEPRECATION")
if (AppUtils.isSystemApp(this, app.packageName)) DeviceUtils.runAsRoot("pm uninstall --user 0 $app") else startActivity(Intent(Intent.ACTION_UNINSTALL_PACKAGE, Uri.parse("package:$app"))
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
if (AppUtils.isSystemApp(
this,
app.packageName
)
) DeviceUtils.runAsRoot("pm uninstall --user 0 $app") else startActivity(
Intent(Intent.ACTION_UNINSTALL_PACKAGE, Uri.parse("package:$app"))
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
)
windowManager.removeView(view)
}

Expand All @@ -265,7 +316,8 @@ open class LauncherActivity : AppCompatActivity(), OnAppClickListener {
if (status != "error")
Toast.makeText(this, R.string.app_frozen, Toast.LENGTH_SHORT).show()
else
Toast.makeText(this, R.string.something_wrong, Toast.LENGTH_SHORT).show()
Toast.makeText(this, R.string.something_wrong, Toast.LENGTH_SHORT)
.show()
windowManager.removeView(view)
loadDesktopApps()
}
Expand Down Expand Up @@ -312,4 +364,19 @@ open class LauncherActivity : AppCompatActivity(), OnAppClickListener {
override fun onAppLongClicked(app: App, item: View) {
showAppContextMenu(app, item)
}

override fun onSharedPreferenceChanged(
sharedPreferences: SharedPreferences,
preference: String?
) {
if (preference == null)
return
if (preference == "icon_pack") {
val iconPack = sharedPreferences.getString("icon_pack", "")!!
iconPackUtils = if (iconPack.isNotEmpty()) {
IconPackUtils(this)
} else
null
}
}
}
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
Loading

0 comments on commit f75a547

Please sign in to comment.