mirror of
				https://github.com/SimpleMobileTools/Simple-Launcher.git
				synced 2025-06-05 21:59:15 +02:00 
			
		
		
		
	handle the permission for binding widgets
This commit is contained in:
		| @@ -2,6 +2,9 @@ package com.simplemobiletools.launcher.activities | |||||||
|  |  | ||||||
| import android.animation.ObjectAnimator | import android.animation.ObjectAnimator | ||||||
| import android.annotation.SuppressLint | import android.annotation.SuppressLint | ||||||
|  | import android.app.Activity | ||||||
|  | import android.appwidget.AppWidgetManager | ||||||
|  | import android.appwidget.AppWidgetProviderInfo | ||||||
| import android.content.Context | import android.content.Context | ||||||
| import android.content.Intent | import android.content.Intent | ||||||
| import android.content.pm.PackageManager | import android.content.pm.PackageManager | ||||||
| @@ -25,10 +28,7 @@ import com.simplemobiletools.launcher.extensions.* | |||||||
| import com.simplemobiletools.launcher.fragments.AllAppsFragment | import com.simplemobiletools.launcher.fragments.AllAppsFragment | ||||||
| import com.simplemobiletools.launcher.fragments.MyFragment | import com.simplemobiletools.launcher.fragments.MyFragment | ||||||
| import com.simplemobiletools.launcher.fragments.WidgetsFragment | import com.simplemobiletools.launcher.fragments.WidgetsFragment | ||||||
| import com.simplemobiletools.launcher.helpers.ITEM_TYPE_ICON | import com.simplemobiletools.launcher.helpers.* | ||||||
| import com.simplemobiletools.launcher.helpers.ITEM_TYPE_WIDGET |  | ||||||
| import com.simplemobiletools.launcher.helpers.ROW_COUNT |  | ||||||
| import com.simplemobiletools.launcher.helpers.UNINSTALL_APP_REQUEST_CODE |  | ||||||
| import com.simplemobiletools.launcher.interfaces.FlingListener | import com.simplemobiletools.launcher.interfaces.FlingListener | ||||||
| import com.simplemobiletools.launcher.models.AppLauncher | import com.simplemobiletools.launcher.models.AppLauncher | ||||||
| import com.simplemobiletools.launcher.models.HomeScreenGridItem | import com.simplemobiletools.launcher.models.HomeScreenGridItem | ||||||
| @@ -47,6 +47,7 @@ class MainActivity : SimpleActivity(), FlingListener { | |||||||
|     private var mOpenPopupMenu: PopupMenu? = null |     private var mOpenPopupMenu: PopupMenu? = null | ||||||
|     private var mCachedLaunchers = ArrayList<AppLauncher>() |     private var mCachedLaunchers = ArrayList<AppLauncher>() | ||||||
|     private var mLastTouchCoords = Pair(0f, 0f) |     private var mLastTouchCoords = Pair(0f, 0f) | ||||||
|  |     private var mActionOnCanBindWidget: ((granted: Boolean) -> Unit)? = null | ||||||
|  |  | ||||||
|     private lateinit var mDetector: GestureDetectorCompat |     private lateinit var mDetector: GestureDetectorCompat | ||||||
|  |  | ||||||
| @@ -117,6 +118,8 @@ class MainActivity : SimpleActivity(), FlingListener { | |||||||
|             ensureBackgroundThread { |             ensureBackgroundThread { | ||||||
|                 refetchLaunchers() |                 refetchLaunchers() | ||||||
|             } |             } | ||||||
|  |         } else if (requestCode == REQUEST_ALLOW_BINDING_WIDGET) { | ||||||
|  |             mActionOnCanBindWidget?.invoke(resultCode == Activity.RESULT_OK) | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -482,6 +485,21 @@ class MainActivity : SimpleActivity(), FlingListener { | |||||||
|         homeScreenGridItemsDB.insertAll(homeScreenGridItems) |         homeScreenGridItemsDB.insertAll(homeScreenGridItems) | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     fun handleWidgetBinding(appWidgetManager: AppWidgetManager, appWidgetId: Int, appWidgetInfo: AppWidgetProviderInfo, callback: (canBind: Boolean) -> Unit) { | ||||||
|  |         mActionOnCanBindWidget = null | ||||||
|  |         val canCreateWidget = appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, appWidgetInfo.provider) | ||||||
|  |         if (canCreateWidget) { | ||||||
|  |             callback(true) | ||||||
|  |         } else { | ||||||
|  |             mActionOnCanBindWidget = callback | ||||||
|  |             Intent(AppWidgetManager.ACTION_APPWIDGET_BIND).apply { | ||||||
|  |                 putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId) | ||||||
|  |                 putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, appWidgetInfo.provider) | ||||||
|  |                 startActivityForResult(this, REQUEST_ALLOW_BINDING_WIDGET) | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|     // taken from https://gist.github.com/maxjvh/a6ab15cbba9c82a5065d |     // taken from https://gist.github.com/maxjvh/a6ab15cbba9c82a5065d | ||||||
|     private fun calculateAverageColor(bitmap: Bitmap): Int { |     private fun calculateAverageColor(bitmap: Bitmap): Int { | ||||||
|         var red = 0 |         var red = 0 | ||||||
|   | |||||||
| @@ -12,6 +12,7 @@ const val COLUMN_COUNT = 5 | |||||||
|  |  | ||||||
| const val UNINSTALL_APP_REQUEST_CODE = 50 | const val UNINSTALL_APP_REQUEST_CODE = 50 | ||||||
| const val REQUEST_CONFIGURE_WIDGET = 51 | const val REQUEST_CONFIGURE_WIDGET = 51 | ||||||
|  | const val REQUEST_ALLOW_BINDING_WIDGET = 52 | ||||||
|  |  | ||||||
| const val ITEM_TYPE_ICON = 0 | const val ITEM_TYPE_ICON = 0 | ||||||
| const val ITEM_TYPE_WIDGET = 1 | const val ITEM_TYPE_WIDGET = 1 | ||||||
|   | |||||||
| @@ -248,9 +248,12 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel | |||||||
|                     bottom = widgetRect.bottom |                     bottom = widgetRect.bottom | ||||||
|                 } |                 } | ||||||
|  |  | ||||||
|                 bindWidget(widgetItem, false) |  | ||||||
|                 ensureBackgroundThread { |                 ensureBackgroundThread { | ||||||
|                     context.homeScreenGridItemsDB.insert(widgetItem) |                     val itemId = context.homeScreenGridItemsDB.insert(widgetItem) | ||||||
|  |                     widgetItem.id = itemId | ||||||
|  |                     post { | ||||||
|  |                         bindWidget(widgetItem, false) | ||||||
|  |                     } | ||||||
|                 } |                 } | ||||||
|             } else { |             } else { | ||||||
|                 performHapticFeedback() |                 performHapticFeedback() | ||||||
| @@ -263,23 +266,35 @@ class HomeScreenGrid(context: Context, attrs: AttributeSet, defStyle: Int) : Rel | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     private fun bindWidget(item: HomeScreenGridItem, isInitialDrawAfterLaunch: Boolean) { |     private fun bindWidget(item: HomeScreenGridItem, isInitialDrawAfterLaunch: Boolean) { | ||||||
|         val infoList = appWidgetManager.installedProviders |         val activity = context as MainActivity | ||||||
|  |         val infoList = appWidgetManager!!.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 appWidgetId = appWidgetHost.allocateAppWidgetId() |             val appWidgetId = appWidgetHost.allocateAppWidgetId() | ||||||
|             val canCreateWidget = appWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, appWidgetProviderInfo.provider) |             activity.handleWidgetBinding(appWidgetManager, appWidgetId, appWidgetProviderInfo) { canBind -> | ||||||
|             if (canCreateWidget) { |                 if (canBind) { | ||||||
|                 if (appWidgetProviderInfo.configure != null && !isInitialDrawAfterLaunch) { |                     if (appWidgetProviderInfo.configure != null && !isInitialDrawAfterLaunch) { | ||||||
|                     appWidgetHost.startAppWidgetConfigureActivityForResult(context as MainActivity, appWidgetId, 0, REQUEST_CONFIGURE_WIDGET, null) |                         appWidgetHost.startAppWidgetConfigureActivityForResult( | ||||||
|                 } else { |                             context as MainActivity, | ||||||
|                     val widgetView = appWidgetHost.createView(context, appWidgetId, appWidgetProviderInfo) as MyAppWidgetHostView |                             appWidgetId, | ||||||
|                     widgetView.setAppWidget(appWidgetId, appWidgetProviderInfo) |                             0, | ||||||
|  |                             REQUEST_CONFIGURE_WIDGET, | ||||||
|  |                             null | ||||||
|  |                         ) | ||||||
|  |                     } else { | ||||||
|  |                         val widgetView = appWidgetHost.createView(context, appWidgetId, appWidgetProviderInfo) as MyAppWidgetHostView | ||||||
|  |                         widgetView.setAppWidget(appWidgetId, appWidgetProviderInfo) | ||||||
|  |  | ||||||
|                     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 | ||||||
|                     val widgetHeight = item.heightCells * rowHeight |                         val widgetHeight = item.heightCells * rowHeight | ||||||
|                     addView(widgetView, widgetWidth, widgetHeight) |                         addView(widgetView, widgetWidth, widgetHeight) | ||||||
|  |                     } | ||||||
|  |                 } else if (item.id != null) { | ||||||
|  |                     ensureBackgroundThread { | ||||||
|  |                         context.homeScreenGridItemsDB.deleteById(item.id!!) | ||||||
|  |                     } | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user