refactored code for launcher actions

This commit is contained in:
Josia Pietsch 2024-07-07 00:25:27 +02:00
parent e06e62c771
commit 431f7b58ee
Signed by: jrpie
GPG key ID: E70B571D66986A2D
7 changed files with 57 additions and 95 deletions

View file

@ -19,6 +19,7 @@ import android.os.Bundle
import android.os.SystemClock
import android.provider.Settings
import android.util.DisplayMetrics
import android.util.Log
import android.view.KeyEvent
import android.view.View
import android.view.Window
@ -33,6 +34,7 @@ import android.widget.Toast
import de.jrpie.android.launcher.list.ListActivity
import de.jrpie.android.launcher.list.apps.AppInfo
import de.jrpie.android.launcher.list.apps.AppsRecyclerAdapter
import de.jrpie.android.launcher.list.other.LauncherAction
import de.jrpie.android.launcher.settings.SettingsActivity
import de.jrpie.android.launcher.settings.intendedSettingsPause
import de.jrpie.android.launcher.tutorial.TutorialActivity
@ -147,8 +149,7 @@ fun isInstalled(uri: String, context: Context): Boolean {
try {
context.packageManager.getPackageInfo(uri, PackageManager.GET_ACTIVITIES)
return true
} catch (_: PackageManager.NameNotFoundException) {
}
} catch (_: PackageManager.NameNotFoundException) { }
return false
}
@ -163,16 +164,8 @@ fun launch(
animationIn: Int = android.R.anim.fade_in, animationOut: Int = android.R.anim.fade_out
) {
if (data.startsWith("launcher:")) // [type]:[info]
when(data.split(":")[1]) {
"settings" -> openSettings(activity)
"choose" -> openAppsList(activity)
"volumeUp" -> audioVolumeUp(activity)
"volumeDown" -> audioVolumeDown(activity)
"nextTrack" -> audioNextTrack(activity)
"previousTrack" -> audioPreviousTrack(activity)
"tutorial" -> openTutorial(activity)
"nop" -> {}
if (LauncherAction.isOtherAction(data)) { // [type]:[info]
LauncherAction.byId(data)?.let {it.launch(activity) }
}
else launchApp(data, activity) // app
@ -338,6 +331,7 @@ fun openAppsList(activity: Activity){
val intent = Intent(activity, ListActivity::class.java)
intent.putExtra("intention", "view")
intendedSettingsPause = true
Log.i("de.jrpie", "openAppsList")
activity.startActivity(intent)
}

View file

@ -10,6 +10,7 @@ import android.view.MotionEvent
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.GestureDetectorCompat
import de.jrpie.android.launcher.BuildConfig.VERSION_NAME
import de.jrpie.android.launcher.list.other.LauncherAction
import de.jrpie.android.launcher.tutorial.TutorialActivity
import kotlinx.android.synthetic.main.home.*
import java.text.SimpleDateFormat
@ -129,8 +130,7 @@ class HomeActivity: UIObject, AppCompatActivity(),
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
when (keyCode) {
KeyEvent.KEYCODE_BACK -> {
launch("launcher:choose", this) }
KeyEvent.KEYCODE_BACK -> LauncherAction.CHOOSE.launch(this)
KeyEvent.KEYCODE_VOLUME_UP -> launch(volumeUpApp, this,0, 0)
KeyEvent.KEYCODE_VOLUME_DOWN -> launch(volumeDownApp, this,0, 0)
}

View file

@ -12,6 +12,7 @@ import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.google.android.material.tabs.TabLayout
import de.jrpie.android.launcher.list.other.LauncherAction
import de.jrpie.android.launcher.R
import de.jrpie.android.launcher.REQUEST_UNINSTALL
import de.jrpie.android.launcher.UIObject
@ -44,7 +45,8 @@ class ListActivity : AppCompatActivity(), UIObject {
setContentView(R.layout.list)
list_settings.setOnClickListener {
launch("launcher:settings", this, R.anim.bottom_up)
launch(LauncherAction.SETTINGS.id, this@ListActivity, R.anim.bottom_up)
LauncherAction.SETTINGS.launch(this@ListActivity);
}
}

View file

@ -0,0 +1,38 @@
package de.jrpie.android.launcher.list.other
import android.app.Activity
import de.jrpie.android.launcher.R
import de.jrpie.android.launcher.audioNextTrack
import de.jrpie.android.launcher.audioPreviousTrack
import de.jrpie.android.launcher.audioVolumeDown
import de.jrpie.android.launcher.audioVolumeUp
import de.jrpie.android.launcher.openAppsList
import de.jrpie.android.launcher.openSettings
enum class LauncherAction(val id: String, val label: Int, val icon: Int, val launch: (Activity) -> Unit) {
SETTINGS("launcher:settings", R.string.list_other_settings, R.drawable.baseline_settings_24, ::openSettings),
CHOOSE("launcher:choose", R.string.list_other_list, R.drawable.baseline_menu_24, ::openAppsList),
VOLUME_UP("launcher:volumeUp",
R.string.list_other_volume_up,
R.drawable.baseline_volume_up_24, ::audioVolumeUp),
VOLUME_DOWN("launcher:volumeDown",
R.string.list_other_volume_down,
R.drawable.baseline_volume_down_24, ::audioVolumeDown),
TRACK_NEXT("launcher:nextTrack",
R.string.list_other_track_next,
R.drawable.baseline_skip_next_24, ::audioNextTrack),
TRACK_PREV("launcher:previousTrack",
R.string.list_other_track_previous,
R.drawable.baseline_skip_previous_24, ::audioPreviousTrack),
NOP("launcher:nop", R.string.list_other_nop, R.drawable.baseline_not_interested_24, {});
companion object {
fun byId(id: String): LauncherAction? {
return LauncherAction.values().singleOrNull { it.id == id };
}
fun isOtherAction(id: String): Boolean {
return id.startsWith("launcher");
}
}
}

View file

@ -1,14 +0,0 @@
package de.jrpie.android.launcher.list.other
/**
* Stores information used in [OtherRecyclerAdapter] rows.
*
* Represents an `other` action - something that can be selected to be launched
* when an action is recognized.
*
* @param data - a string identifying the thing to be launched
*/
class OtherInfo(label: String, data: String, var icon: Int) {
var label: CharSequence? = label
var data: CharSequence? = data
}

View file

@ -2,7 +2,6 @@ package de.jrpie.android.launcher.list.other
import android.app.Activity
import android.content.Intent
import android.os.Build
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
@ -23,7 +22,7 @@ import de.jrpie.android.launcher.list.forApp
class OtherRecyclerAdapter(val activity: Activity):
RecyclerView.Adapter<OtherRecyclerAdapter.ViewHolder>() {
private val othersList: MutableList<OtherInfo> = ArrayList()
private val othersList: Array<LauncherAction> = LauncherAction.values();
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView),
View.OnClickListener {
@ -35,14 +34,14 @@ class OtherRecyclerAdapter(val activity: Activity):
val pos = adapterPosition
val content = othersList[pos]
returnChoiceIntent(forApp, content.data.toString())
returnChoiceIntent(forApp, content.id)
}
init { itemView.setOnClickListener(this) }
}
override fun onBindViewHolder(viewHolder: ViewHolder, i: Int) {
val otherLabel = othersList[i].label.toString()
val otherLabel = activity.getString(othersList[i].label);
val icon = othersList[i].icon
viewHolder.textView.text = otherLabel
@ -57,48 +56,6 @@ class OtherRecyclerAdapter(val activity: Activity):
return ViewHolder(view)
}
init {
othersList.add(
OtherInfo(activity.getString(R.string.list_other_settings),
"launcher:settings",
R.drawable.baseline_settings_24)
)
othersList.add(
OtherInfo(activity.getString(R.string.list_other_list),
"launcher:choose",
R.drawable.baseline_menu_24)
)
othersList.add(
OtherInfo(activity.getString(R.string.list_other_volume_up),
"launcher:volumeUp",
R.drawable.baseline_volume_up_24)
)
othersList.add(
OtherInfo(activity.getString(R.string.list_other_volume_down),
"launcher:volumeDown",
R.drawable.baseline_volume_down_24)
)
othersList.add(
OtherInfo(
activity.getString(R.string.list_other_track_next),
"launcher:nextTrack",
R.drawable.baseline_skip_next_24
)
)
othersList.add(
OtherInfo(
activity.getString(R.string.list_other_track_previous),
"launcher:previousTrack",
R.drawable.baseline_skip_previous_24
)
)
othersList.add(
OtherInfo(activity.getString(R.string.list_other_nop),
"launcher:nop",
R.drawable.baseline_not_interested_24)
)
}
private fun returnChoiceIntent(forApp: String, value: String) {
val returnIntent = Intent()
returnIntent.putExtra("value", value)

View file

@ -15,6 +15,7 @@ import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import de.jrpie.android.launcher.list.other.LauncherAction
import de.jrpie.android.launcher.settings.intendedSettingsPause
import java.lang.Exception
@ -91,26 +92,10 @@ class ActionsRecyclerAdapter(val activity: Activity):
setButtonColor(viewHolder.chooseButton, vibrantColor)
}
if (content.startsWith("launcher")) {
viewHolder.img.visibility = View.VISIBLE
if (LauncherAction.isOtherAction(content.toString())) {
viewHolder.img.setOnClickListener{ chooseApp(actionName.toString()) }
when (content.split(":")[1]) {
"settings" ->
viewHolder.img.setImageResource(R.drawable.baseline_settings_24)
"choose" ->
viewHolder.img.setImageResource(R.drawable.baseline_menu_24)
"volumeUp" ->
viewHolder.img.setImageResource(R.drawable.baseline_volume_up_24)
"volumeDown" ->
viewHolder.img.setImageResource(R.drawable.baseline_volume_down_24)
"nextTrack" ->
viewHolder.img.setImageResource(R.drawable.baseline_skip_next_24)
"previousTrack" ->
viewHolder.img.setImageResource(R.drawable.baseline_skip_previous_24)
"nop" ->
viewHolder.img.setImageResource(R.drawable.baseline_not_interested_24)
LauncherAction.byId(content.toString())?.let {
viewHolder.img.setImageResource(it.icon)
}
} else {
// Set image icon (by packageName)