1 /* <lambda>null2 * Copyright (C) 2025 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.launcher3.widget 18 19 import android.appwidget.AppWidgetHost 20 import android.appwidget.AppWidgetProviderInfo 21 import android.content.Context 22 import com.android.launcher3.InvariantDeviceProfile 23 import com.android.launcher3.util.Executors 24 import com.android.launcher3.util.Executors.MAIN_EXECUTOR 25 import com.android.launcher3.util.Executors.MODEL_EXECUTOR 26 import com.android.launcher3.util.LooperExecutor 27 28 open class ListenableAppWidgetHost(private val ctx: Context, hostId: Int) : 29 AppWidgetHost(ctx, hostId) { 30 31 protected val holders = mutableListOf<LauncherWidgetHolder>() 32 33 override fun onProvidersChanged() { 34 MAIN_EXECUTOR.execute { 35 holders.forEach { holder -> 36 // Listeners might remove themselves from the list during the iteration. 37 // Creating a copy of the list to avoid exceptions for concurrent modification. 38 holder.mProviderChangedListeners.toList().forEach { 39 it.notifyWidgetProvidersChanged() 40 } 41 } 42 } 43 } 44 45 override fun onAppWidgetRemoved(appWidgetId: Int) { 46 // Route the call via model thread, in case it comes while a loader-bind is in progress 47 MODEL_EXECUTOR.execute { 48 MAIN_EXECUTOR.execute { 49 holders.forEach { it.mAppWidgetRemovedCallback?.accept(appWidgetId) } 50 } 51 } 52 } 53 54 override fun onProviderChanged(appWidgetId: Int, appWidget: AppWidgetProviderInfo) { 55 val info = LauncherAppWidgetProviderInfo.fromProviderInfo(ctx, appWidget) 56 super.onProviderChanged(appWidgetId, info) 57 // The super method updates the dimensions of the providerInfo. Update the 58 // launcher spans accordingly. 59 info.initSpans(ctx, InvariantDeviceProfile.INSTANCE.get(ctx)) 60 } 61 62 /** Listener for getting notifications on provider changes. */ 63 fun interface ProviderChangedListener { 64 /** Notify the listener that the providers have changed */ 65 fun notifyWidgetProvidersChanged() 66 } 67 68 companion object { 69 70 @JvmStatic val widgetHolderExecutor: LooperExecutor = Executors.UI_HELPER_EXECUTOR 71 } 72 } 73