mirror of
https://github.com/SimpleMobileTools/Simple-Launcher.git
synced 2024-12-25 06:22:25 +01:00
override AppWidgetHostView to be able to respond to long presses
This commit is contained in:
parent
5fca4ff4f1
commit
05583d1ee0
@ -1,7 +1,6 @@
|
|||||||
package com.simplemobiletools.launcher.views
|
package com.simplemobiletools.launcher.views
|
||||||
|
|
||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
import android.appwidget.AppWidgetHost
|
|
||||||
import android.appwidget.AppWidgetManager
|
import android.appwidget.AppWidgetManager
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.graphics.Canvas
|
import android.graphics.Canvas
|
||||||
@ -263,7 +262,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
|
|||||||
val infoList = AppWidgetManager.getInstance(context).installedProviders
|
val infoList = AppWidgetManager.getInstance(context).installedProviders
|
||||||
val appWidgetProviderInfo = infoList.firstOrNull { it.provider.shortClassName == item.shortClassName }
|
val appWidgetProviderInfo = infoList.firstOrNull { it.provider.shortClassName == item.shortClassName }
|
||||||
if (appWidgetProviderInfo != null) {
|
if (appWidgetProviderInfo != null) {
|
||||||
val appWidgetHost = AppWidgetHost(context, WIDGET_HOST_ID)
|
val appWidgetHost = MyAppWidgetHost(context, WIDGET_HOST_ID)
|
||||||
val appWidgetId = appWidgetHost.allocateAppWidgetId()
|
val appWidgetId = appWidgetHost.allocateAppWidgetId()
|
||||||
val appWidgetManager = AppWidgetManager.getInstance(context)
|
val appWidgetManager = AppWidgetManager.getInstance(context)
|
||||||
val canCreateWidget = appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, appWidgetProviderInfo.provider)
|
val canCreateWidget = appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, appWidgetProviderInfo.provider)
|
||||||
@ -271,7 +270,7 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel
|
|||||||
if (appWidgetProviderInfo.configure != null && !isInitialDrawAfterLaunch) {
|
if (appWidgetProviderInfo.configure != null && !isInitialDrawAfterLaunch) {
|
||||||
appWidgetHost.startAppWidgetConfigureActivityForResult(context as MainActivity, appWidgetId, 0, REQUEST_CONFIGURE_WIDGET, null)
|
appWidgetHost.startAppWidgetConfigureActivityForResult(context as MainActivity, appWidgetId, 0, REQUEST_CONFIGURE_WIDGET, null)
|
||||||
} else {
|
} else {
|
||||||
val widgetView = appWidgetHost.createView(context, appWidgetId, appWidgetProviderInfo)
|
val widgetView = appWidgetHost.createView(context, appWidgetId, appWidgetProviderInfo) as MyAppWidgetHostView
|
||||||
widgetView.x = item.left * rowWidth + sideMargins.left.toFloat()
|
widgetView.x = item.left * rowWidth + sideMargins.left.toFloat()
|
||||||
widgetView.y = item.top * rowHeight + sideMargins.top.toFloat()
|
widgetView.y = item.top * rowHeight + sideMargins.top.toFloat()
|
||||||
val widgetWidth = item.widthCells * rowWidth
|
val widgetWidth = item.widthCells * rowWidth
|
||||||
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.simplemobiletools.launcher.views
|
||||||
|
|
||||||
|
import android.appwidget.AppWidgetHost
|
||||||
|
import android.appwidget.AppWidgetHostView
|
||||||
|
import android.appwidget.AppWidgetProviderInfo
|
||||||
|
import android.content.Context
|
||||||
|
|
||||||
|
class MyAppWidgetHost(context: Context, hostId: Int) : AppWidgetHost(context, hostId) {
|
||||||
|
override fun onCreateView(context: Context, appWidgetId: Int, appWidget: AppWidgetProviderInfo): AppWidgetHostView {
|
||||||
|
return MyAppWidgetHostView(context)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
package com.simplemobiletools.launcher.views
|
||||||
|
|
||||||
|
import android.appwidget.AppWidgetHostView
|
||||||
|
import android.content.Context
|
||||||
|
import android.os.Handler
|
||||||
|
import android.view.MotionEvent
|
||||||
|
import android.view.ViewConfiguration
|
||||||
|
import com.simplemobiletools.commons.extensions.performHapticFeedback
|
||||||
|
|
||||||
|
class MyAppWidgetHostView(context: Context) : AppWidgetHostView(context) {
|
||||||
|
private var longPressHandler = Handler()
|
||||||
|
private var hasLongPressed = false
|
||||||
|
|
||||||
|
override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
|
||||||
|
if (hasLongPressed) {
|
||||||
|
hasLongPressed = false
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
when (event.actionMasked) {
|
||||||
|
MotionEvent.ACTION_DOWN -> longPressHandler.postDelayed(longPressRunnable, ViewConfiguration.getLongPressTimeout().toLong())
|
||||||
|
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> longPressHandler.removeCallbacksAndMessages(null)
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
private val longPressRunnable = Runnable {
|
||||||
|
longPressHandler.removeCallbacksAndMessages(null)
|
||||||
|
hasLongPressed = true
|
||||||
|
performHapticFeedback()
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user