Files
Flexera-Launcher/src/com/android/launcher3/widget/LauncherAppWidgetHost.java
T
Sihua Ma 045165a4ba Add unit test for LauncherAppWidgetHost
Number of methods covered: 3

Test: SysUI studio
Flag: TEST_ONLY
Bug: 353303621
Change-Id: I8fa9ed7b496baa18dac1a721c72a32814d89dd6f
2024-07-19 00:38:32 +00:00

133 lines
4.4 KiB
Java

/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.launcher3.widget;
import static com.android.launcher3.widget.LauncherWidgetHolder.APPWIDGET_HOST_ID;
import android.appwidget.AppWidgetHost;
import android.appwidget.AppWidgetProviderInfo;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import com.android.launcher3.LauncherAppState;
import com.android.launcher3.util.Executors;
import com.android.launcher3.widget.LauncherWidgetHolder.ProviderChangedListener;
import java.util.ArrayList;
import java.util.List;
import java.util.function.IntConsumer;
/**
* Specific {@link AppWidgetHost} that creates our {@link LauncherAppWidgetHostView}
* which correctly captures all long-press events. This ensures that users can
* always pick up and move widgets.
*/
class LauncherAppWidgetHost extends AppWidgetHost {
@NonNull
private final List<ProviderChangedListener> mProviderChangeListeners;
@NonNull
private final Context mContext;
@Nullable
private final IntConsumer mAppWidgetRemovedCallback;
@Nullable
private ListenableHostView mViewToRecycle;
public LauncherAppWidgetHost(@NonNull Context context,
@Nullable IntConsumer appWidgetRemovedCallback,
List<ProviderChangedListener> providerChangeListeners) {
super(context, APPWIDGET_HOST_ID);
mContext = context;
mAppWidgetRemovedCallback = appWidgetRemovedCallback;
mProviderChangeListeners = providerChangeListeners;
}
@Override
protected void onProvidersChanged() {
if (!mProviderChangeListeners.isEmpty()) {
for (LauncherWidgetHolder.ProviderChangedListener callback :
new ArrayList<>(mProviderChangeListeners)) {
callback.notifyWidgetProvidersChanged();
}
}
}
/**
* Sets the view to be recycled for the next widget creation.
*/
public void recycleViewForNextCreation(ListenableHostView viewToRecycle) {
mViewToRecycle = viewToRecycle;
}
@VisibleForTesting
@Nullable ListenableHostView getViewToRecycle() {
return mViewToRecycle;
}
@Override
@NonNull
public LauncherAppWidgetHostView onCreateView(Context context, int appWidgetId,
AppWidgetProviderInfo appWidget) {
ListenableHostView result =
mViewToRecycle != null ? mViewToRecycle : new ListenableHostView(context);
mViewToRecycle = null;
return result;
}
/**
* Called when the AppWidget provider for a AppWidget has been upgraded to a new apk.
*/
@Override
protected void onProviderChanged(int appWidgetId, @NonNull AppWidgetProviderInfo appWidget) {
LauncherAppWidgetProviderInfo info = LauncherAppWidgetProviderInfo.fromProviderInfo(
mContext, appWidget);
super.onProviderChanged(appWidgetId, info);
// The super method updates the dimensions of the providerInfo. Update the
// launcher spans accordingly.
info.initSpans(mContext, LauncherAppState.getIDP(mContext));
}
/**
* Called on an appWidget is removed for a widgetId
*
* @param appWidgetId TODO: make this override when SDK is updated
*/
@Override
public void onAppWidgetRemoved(int appWidgetId) {
if (mAppWidgetRemovedCallback == null) {
return;
}
// Route the call via model thread, in case it comes while a loader-bind is in progress
Executors.MODEL_EXECUTOR.execute(
() -> Executors.MAIN_EXECUTOR.execute(
() -> mAppWidgetRemovedCallback.accept(appWidgetId)));
}
/**
* The same as super.clearViews(), except with the scope exposed
*/
@Override
public void clearViews() {
super.clearViews();
}
}