1 /**
2  * Copyright (C) 2022 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 package com.android.launcher3.uioverrides;
17 
18 import static com.android.launcher3.widget.LauncherWidgetHolder.APPWIDGET_HOST_ID;
19 
20 import android.appwidget.AppWidgetHost;
21 import android.appwidget.AppWidgetProviderInfo;
22 import android.content.Context;
23 import android.os.Looper;
24 
25 import androidx.annotation.NonNull;
26 
27 import com.android.launcher3.LauncherAppState;
28 import com.android.launcher3.util.Executors;
29 import com.android.launcher3.widget.LauncherAppWidgetProviderInfo;
30 import com.android.launcher3.widget.LauncherWidgetHolder;
31 
32 import java.util.function.IntConsumer;
33 
34 /**
35  * {@link AppWidgetHost} that is used to receive the changes to the widgets without
36  * storing any {@code Activity} info like that of the launcher.
37  */
38 final class QuickstepAppWidgetHost extends AppWidgetHost {
39     private final @NonNull Context mContext;
40     private final @NonNull IntConsumer mAppWidgetRemovedCallback;
41     private final @NonNull LauncherWidgetHolder.ProviderChangedListener mProvidersChangedListener;
42 
QuickstepAppWidgetHost(@onNull Context context, @NonNull IntConsumer appWidgetRemovedCallback, @NonNull LauncherWidgetHolder.ProviderChangedListener listener, @NonNull Looper looper)43     QuickstepAppWidgetHost(@NonNull Context context, @NonNull IntConsumer appWidgetRemovedCallback,
44             @NonNull LauncherWidgetHolder.ProviderChangedListener listener,
45             @NonNull Looper looper) {
46         super(context, APPWIDGET_HOST_ID, null, looper);
47         mContext = context;
48         mAppWidgetRemovedCallback = appWidgetRemovedCallback;
49         mProvidersChangedListener = listener;
50     }
51 
52     @Override
onProvidersChanged()53     protected void onProvidersChanged() {
54         mProvidersChangedListener.notifyWidgetProvidersChanged();
55     }
56 
57     @Override
onAppWidgetRemoved(int appWidgetId)58     public void onAppWidgetRemoved(int appWidgetId) {
59         // Route the call via model thread, in case it comes while a loader-bind is in progress
60         Executors.MODEL_EXECUTOR.execute(
61                 () -> Executors.MAIN_EXECUTOR.execute(
62                         () -> mAppWidgetRemovedCallback.accept(appWidgetId)));
63     }
64 
65     @Override
onProviderChanged(int appWidgetId, @NonNull AppWidgetProviderInfo appWidget)66     protected void onProviderChanged(int appWidgetId, @NonNull AppWidgetProviderInfo appWidget) {
67         LauncherAppWidgetProviderInfo info = LauncherAppWidgetProviderInfo.fromProviderInfo(
68                 mContext, appWidget);
69         super.onProviderChanged(appWidgetId, info);
70         // The super method updates the dimensions of the providerInfo. Update the
71         // launcher spans accordingly.
72         info.initSpans(mContext, LauncherAppState.getIDP(mContext));
73     }
74 }
75