From 05484a5b29278933beaff8806eb884aaff3d5c3d Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 17 Sep 2022 21:23:12 +0200 Subject: [PATCH] launch the clicked app on clicking its icon --- .../launcher/activities/MainActivity.kt | 9 +++++--- .../launcher/extensions/Activity.kt | 13 +++++++++++ .../launcher/fragments/AllAppsFragment.kt | 8 ++----- .../launcher/models/HomeScreenGridItem.kt | 5 +++++ .../launcher/views/HomeScreenGrid.kt | 22 ++++++++++++++++--- 5 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/launcher/extensions/Activity.kt create mode 100644 app/src/main/kotlin/com/simplemobiletools/launcher/models/HomeScreenGridItem.kt diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt index 5d5da27..dbac793 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/activities/MainActivity.kt @@ -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 { diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/extensions/Activity.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/extensions/Activity.kt new file mode 100644 index 0000000..2b1a932 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/extensions/Activity.kt @@ -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) + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/AllAppsFragment.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/AllAppsFragment.kt index 39c0d77..97142a9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/AllAppsFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/fragments/AllAppsFragment.kt @@ -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) { 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 } diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/models/HomeScreenGridItem.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/models/HomeScreenGridItem.kt new file mode 100644 index 0000000..68fa278 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/models/HomeScreenGridItem.kt @@ -0,0 +1,5 @@ +package com.simplemobiletools.launcher.models + +import android.graphics.Rect + +data class HomeScreenGridItem(var rect: Rect, val packageName: String) diff --git a/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt b/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt index 311e06b..0e59c22 100644 --- a/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt +++ b/app/src/main/kotlin/com/simplemobiletools/launcher/views/HomeScreenGrid.kt @@ -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() 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 + } + } } }