launch the clicked app on clicking its icon

This commit is contained in:
tibbi 2022-09-17 21:23:12 +02:00
parent 69dc2bcfcd
commit 05484a5b29
5 changed files with 45 additions and 12 deletions

View file

@ -12,6 +12,7 @@ import androidx.core.view.GestureDetectorCompat
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.launcher.BuildConfig
import com.simplemobiletools.launcher.R
import com.simplemobiletools.launcher.extensions.launchApp
import com.simplemobiletools.launcher.fragments.AllAppsFragment
import com.simplemobiletools.launcher.fragments.MyFragment
import com.simplemobiletools.launcher.fragments.WidgetsFragment
@ -152,7 +153,9 @@ class MainActivity : SimpleActivity(), FlingListener {
fun homeScreenClicked(x: Float, y: Float) {
if (x >= home_screen_grid.left && x <= home_screen_grid.right && y >= home_screen_grid.top && y <= home_screen_grid.bottom) {
home_screen_grid.gridClicked(x, y)
home_screen_grid.gridClicked(x, y) { packageName ->
launchApp(packageName)
}
}
}
@ -161,9 +164,9 @@ class MainActivity : SimpleActivity(), FlingListener {
}
private class MyGestureListener(private val flingListener: FlingListener) : GestureDetector.SimpleOnGestureListener() {
override fun onSingleTapConfirmed(event: MotionEvent): Boolean {
override fun onSingleTapUp(event: MotionEvent): Boolean {
(flingListener as MainActivity).homeScreenClicked(event.x, event.y)
return super.onSingleTapConfirmed(event)
return super.onSingleTapUp(event)
}
override fun onFling(event1: MotionEvent, event2: MotionEvent, velocityX: Float, velocityY: Float): Boolean {

View file

@ -0,0 +1,13 @@
package com.simplemobiletools.launcher.extensions
import android.app.Activity
import com.simplemobiletools.commons.extensions.showErrorToast
fun Activity.launchApp(packageName: String) {
val launchIntent = packageManager.getLaunchIntentForPackage(packageName)
try {
startActivity(launchIntent)
} catch (e: Exception) {
showErrorToast(e)
}
}

View file

@ -18,6 +18,7 @@ import com.simplemobiletools.launcher.activities.MainActivity
import com.simplemobiletools.launcher.adapters.LaunchersAdapter
import com.simplemobiletools.launcher.extensions.getColumnCount
import com.simplemobiletools.launcher.extensions.getDrawableForPackageName
import com.simplemobiletools.launcher.extensions.launchApp
import com.simplemobiletools.launcher.interfaces.AllAppsListener
import com.simplemobiletools.launcher.models.AppLauncher
import kotlinx.android.synthetic.main.all_apps_fragment.view.*
@ -98,12 +99,7 @@ class AllAppsFragment(context: Context, attributeSet: AttributeSet) : MyFragment
private fun setupAdapter(launchers: ArrayList<AppLauncher>) {
activity?.runOnUiThread {
LaunchersAdapter(activity!!, launchers, all_apps_fastscroller, this) {
val launchIntent = context.packageManager.getLaunchIntentForPackage((it as AppLauncher).packageName)
try {
activity!!.startActivity(launchIntent)
} catch (e: Exception) {
activity?.showErrorToast(e)
}
activity?.launchApp((it as AppLauncher).packageName)
}.apply {
all_apps_grid.adapter = this
}

View file

@ -0,0 +1,5 @@
package com.simplemobiletools.launcher.models
import android.graphics.Rect
data class HomeScreenGridItem(var rect: Rect, val packageName: String)

View file

@ -1,14 +1,17 @@
package com.simplemobiletools.launcher.views
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.telecom.TelecomManager
import android.util.AttributeSet
import android.view.View
import com.simplemobiletools.launcher.R
import com.simplemobiletools.launcher.extensions.getDrawableForPackageName
import com.simplemobiletools.launcher.models.HomeScreenGridItem
class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : View(context, attrs, defStyle) {
constructor(context: Context, attrs: AttributeSet) : this(context, attrs, 0)
@ -27,6 +30,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie
private var rowWidth = 0
private var rowHeight = 0
private var iconSize = 0
private var gridItems = arrayListOf<HomeScreenGridItem>()
init {
textPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
@ -35,6 +39,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie
}
}
@SuppressLint("DrawAllocation")
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
if (rowXCoords.isEmpty()) {
@ -54,13 +59,24 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Vie
for (i in 0 until COLUMN_COUNT - 1) {
val drawableX = rowXCoords[i] + iconMargin
val drawableY = rowYCoords[COLUMN_COUNT - 1] + rowHeight - iconSize - iconMargin * 2
dialerDrawable!!.setBounds(drawableX, drawableY, drawableX + iconSize, drawableY + iconSize)
val rect = Rect(drawableX, drawableY, drawableX + rowWidth, drawableY + rowHeight)
dialerDrawable!!.setBounds(rect.left, rect.top, drawableX + iconSize, drawableY + iconSize)
dialerDrawable!!.draw(canvas)
val gridItem = HomeScreenGridItem(rect, defaultDialerPackage)
gridItems.add(gridItem)
}
}
}
fun gridClicked(x: Float, y: Float) {
fun gridClicked(x: Float, y: Float, callback: (packageName: String) -> Unit) {
for (gridItem in gridItems) {
val rect = gridItem.rect
if (x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom) {
callback(gridItem.packageName)
break
}
}
}
}