2023-11-17 16:45:56 -08:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2023 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;
|
|
|
|
|
|
2023-12-20 01:11:10 +00:00
|
|
|
import static android.content.ClipDescription.MIMETYPE_TEXT_INTENT;
|
2023-11-17 16:45:56 -08:00
|
|
|
import static android.view.WindowInsets.Type.navigationBars;
|
|
|
|
|
import static android.view.WindowInsets.Type.statusBars;
|
|
|
|
|
|
|
|
|
|
import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
|
|
|
|
|
import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;
|
|
|
|
|
|
2023-12-01 15:34:59 -08:00
|
|
|
import android.appwidget.AppWidgetProviderInfo;
|
2023-12-20 01:11:10 +00:00
|
|
|
import android.content.ClipData;
|
|
|
|
|
import android.content.ClipDescription;
|
2023-12-01 15:34:59 -08:00
|
|
|
import android.content.Intent;
|
2023-11-17 16:45:56 -08:00
|
|
|
import android.os.Bundle;
|
2024-01-26 14:39:10 -08:00
|
|
|
import android.util.Log;
|
2023-12-01 15:34:59 -08:00
|
|
|
import android.view.View;
|
2023-11-17 16:45:56 -08:00
|
|
|
import android.view.WindowInsetsController;
|
|
|
|
|
import android.view.WindowManager;
|
|
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
|
|
|
|
|
import com.android.launcher3.dragndrop.SimpleDragLayer;
|
2024-01-26 14:39:10 -08:00
|
|
|
import com.android.launcher3.model.WidgetItem;
|
2023-11-17 16:45:56 -08:00
|
|
|
import com.android.launcher3.model.WidgetsModel;
|
|
|
|
|
import com.android.launcher3.popup.PopupDataProvider;
|
|
|
|
|
import com.android.launcher3.widget.BaseWidgetSheet;
|
2023-12-01 15:34:59 -08:00
|
|
|
import com.android.launcher3.widget.WidgetCell;
|
2023-11-17 16:45:56 -08:00
|
|
|
import com.android.launcher3.widget.model.WidgetsListBaseEntry;
|
|
|
|
|
import com.android.launcher3.widget.picker.WidgetsFullSheet;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2024-01-26 14:39:10 -08:00
|
|
|
import java.util.Locale;
|
2023-11-17 16:45:56 -08:00
|
|
|
|
|
|
|
|
/** An Activity that can host Launcher's widget picker. */
|
|
|
|
|
public class WidgetPickerActivity extends BaseActivity {
|
2024-01-26 14:39:10 -08:00
|
|
|
private static final String TAG = "WidgetPickerActivity";
|
|
|
|
|
private static final boolean DEBUG = false;
|
|
|
|
|
|
2023-12-20 01:11:10 +00:00
|
|
|
/**
|
|
|
|
|
* Name of the extra that indicates that a widget being dragged.
|
|
|
|
|
*
|
|
|
|
|
* <p>When set to "true" in the result of startActivityForResult, the client that launched the
|
|
|
|
|
* picker knows that activity was closed due to pending drag.
|
|
|
|
|
*/
|
|
|
|
|
private static final String EXTRA_IS_PENDING_WIDGET_DRAG = "is_pending_widget_drag";
|
|
|
|
|
|
2024-01-26 14:39:10 -08:00
|
|
|
// Intent extras that specify the desired widget width and height. If these are not specified in
|
|
|
|
|
// the intent, then widgets will not be filtered for size.
|
|
|
|
|
private static final String EXTRA_DESIRED_WIDGET_WIDTH = "desired_widget_width";
|
|
|
|
|
private static final String EXTRA_DESIRED_WIDGET_HEIGHT = "desired_widget_height";
|
|
|
|
|
|
|
|
|
|
|
2023-11-17 16:45:56 -08:00
|
|
|
private SimpleDragLayer<WidgetPickerActivity> mDragLayer;
|
|
|
|
|
private WidgetsModel mModel;
|
|
|
|
|
private final PopupDataProvider mPopupDataProvider = new PopupDataProvider(i -> {});
|
|
|
|
|
|
2024-01-26 14:39:10 -08:00
|
|
|
private int mDesiredWidgetWidth;
|
|
|
|
|
private int mDesiredWidgetHeight;
|
|
|
|
|
|
2023-11-17 16:45:56 -08:00
|
|
|
@Override
|
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
|
|
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
|
|
|
|
|
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
|
|
|
|
|
|
|
|
|
|
LauncherAppState app = LauncherAppState.getInstance(this);
|
|
|
|
|
InvariantDeviceProfile idp = app.getInvariantDeviceProfile();
|
|
|
|
|
|
|
|
|
|
mDeviceProfile = idp.getDeviceProfile(this);
|
|
|
|
|
mModel = new WidgetsModel();
|
|
|
|
|
|
|
|
|
|
setContentView(R.layout.widget_picker_activity);
|
|
|
|
|
mDragLayer = findViewById(R.id.drag_layer);
|
|
|
|
|
mDragLayer.recreateControllers();
|
|
|
|
|
|
|
|
|
|
WindowInsetsController wc = mDragLayer.getWindowInsetsController();
|
|
|
|
|
wc.hide(navigationBars() + statusBars());
|
|
|
|
|
|
|
|
|
|
BaseWidgetSheet widgetSheet = WidgetsFullSheet.show(this, true);
|
|
|
|
|
widgetSheet.disableNavBarScrim(true);
|
|
|
|
|
widgetSheet.addOnCloseListener(this::finish);
|
|
|
|
|
|
2024-01-26 14:39:10 -08:00
|
|
|
// A value of 0 for either size means that no filtering will occur in that dimension. If
|
|
|
|
|
// both values are 0, then no size filtering will occur.
|
|
|
|
|
mDesiredWidgetWidth =
|
|
|
|
|
getIntent().getIntExtra(EXTRA_DESIRED_WIDGET_WIDTH, 0);
|
|
|
|
|
mDesiredWidgetHeight =
|
|
|
|
|
getIntent().getIntExtra(EXTRA_DESIRED_WIDGET_HEIGHT, 0);
|
|
|
|
|
|
2023-11-17 16:45:56 -08:00
|
|
|
refreshAndBindWidgets();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@NonNull
|
|
|
|
|
@Override
|
|
|
|
|
public PopupDataProvider getPopupDataProvider() {
|
|
|
|
|
return mPopupDataProvider;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public SimpleDragLayer<WidgetPickerActivity> getDragLayer() {
|
|
|
|
|
return mDragLayer;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-01 15:34:59 -08:00
|
|
|
@Override
|
|
|
|
|
public View.OnClickListener getItemOnClickListener() {
|
|
|
|
|
return v -> {
|
|
|
|
|
final AppWidgetProviderInfo info =
|
|
|
|
|
(v instanceof WidgetCell) ? ((WidgetCell) v).getWidgetItem().widgetInfo : null;
|
|
|
|
|
if (info == null || info.provider == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setResult(RESULT_OK, new Intent()
|
|
|
|
|
.putExtra(Intent.EXTRA_COMPONENT_NAME, info.provider)
|
|
|
|
|
.putExtra(Intent.EXTRA_USER, info.getProfile()));
|
|
|
|
|
|
|
|
|
|
finish();
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-20 01:11:10 +00:00
|
|
|
@Override
|
|
|
|
|
public View.OnLongClickListener getAllAppsItemLongClickListener() {
|
|
|
|
|
return view -> {
|
|
|
|
|
if (!(view instanceof WidgetCell widgetCell)) return false;
|
|
|
|
|
|
|
|
|
|
if (widgetCell.getWidgetView().getDrawable() == null
|
|
|
|
|
&& widgetCell.getAppWidgetHostViewPreview() == null) {
|
|
|
|
|
// The widget preview hasn't been loaded; so, we abort the drag.
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final AppWidgetProviderInfo info = widgetCell.getWidgetItem().widgetInfo;
|
|
|
|
|
if (info == null || info.provider == null) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ClipData clipData = new ClipData(
|
|
|
|
|
new ClipDescription(
|
|
|
|
|
/* label= */ "", // not displayed anywhere; so, set to empty.
|
|
|
|
|
new String[]{MIMETYPE_TEXT_INTENT}
|
|
|
|
|
),
|
|
|
|
|
new ClipData.Item(new Intent()
|
|
|
|
|
.putExtra(Intent.EXTRA_USER, info.getProfile())
|
|
|
|
|
.putExtra(Intent.EXTRA_COMPONENT_NAME, info.provider))
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Set result indicating activity was closed due a widget being dragged.
|
|
|
|
|
setResult(RESULT_OK, new Intent()
|
|
|
|
|
.putExtra(EXTRA_IS_PENDING_WIDGET_DRAG, true));
|
|
|
|
|
|
|
|
|
|
// DRAG_FLAG_GLOBAL permits dragging data beyond app window.
|
|
|
|
|
return view.startDragAndDrop(
|
|
|
|
|
clipData,
|
|
|
|
|
new View.DragShadowBuilder(view),
|
|
|
|
|
/* myLocalState= */ null,
|
|
|
|
|
View.DRAG_FLAG_GLOBAL
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-17 16:45:56 -08:00
|
|
|
private void refreshAndBindWidgets() {
|
|
|
|
|
MODEL_EXECUTOR.execute(() -> {
|
|
|
|
|
LauncherAppState app = LauncherAppState.getInstance(this);
|
|
|
|
|
mModel.update(app, null);
|
|
|
|
|
final ArrayList<WidgetsListBaseEntry> widgets =
|
2024-01-16 19:22:39 +00:00
|
|
|
mModel.getFilteredWidgetsListForPicker(
|
|
|
|
|
app.getContext(),
|
2024-01-26 14:39:10 -08:00
|
|
|
/*widgetItemFilter=*/ widget -> {
|
|
|
|
|
final WidgetAcceptabilityVerdict verdict =
|
|
|
|
|
isWidgetAcceptable(widget);
|
|
|
|
|
verdict.maybeLogVerdict();
|
|
|
|
|
return verdict.isAcceptable;
|
|
|
|
|
}
|
2024-01-16 19:22:39 +00:00
|
|
|
);
|
2023-11-17 16:45:56 -08:00
|
|
|
MAIN_EXECUTOR.execute(() -> mPopupDataProvider.setAllWidgets(widgets));
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-01-26 14:39:10 -08:00
|
|
|
|
|
|
|
|
private WidgetAcceptabilityVerdict isWidgetAcceptable(WidgetItem widget) {
|
|
|
|
|
final AppWidgetProviderInfo info = widget.widgetInfo;
|
|
|
|
|
if (info == null) {
|
|
|
|
|
return rejectWidget(widget, "shortcut");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mDesiredWidgetWidth == 0 && mDesiredWidgetHeight == 0) {
|
|
|
|
|
// Accept the widget if the desired dimensions are unspecified.
|
|
|
|
|
return acceptWidget(widget);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final boolean isHorizontallyResizable =
|
|
|
|
|
(info.resizeMode & AppWidgetProviderInfo.RESIZE_HORIZONTAL) != 0;
|
|
|
|
|
if (mDesiredWidgetWidth > 0 && isHorizontallyResizable) {
|
|
|
|
|
if (info.maxResizeWidth > 0 && info.maxResizeWidth < mDesiredWidgetWidth) {
|
|
|
|
|
return rejectWidget(
|
|
|
|
|
widget,
|
|
|
|
|
String.format(
|
|
|
|
|
Locale.ENGLISH,
|
|
|
|
|
"maxResizeWidth[%d] < mDesiredWidgetWidth[%d]",
|
|
|
|
|
info.maxResizeWidth,
|
|
|
|
|
mDesiredWidgetWidth));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final int minWidth = info.minResizeWidth > 0 ? info.minResizeWidth : info.minWidth;
|
|
|
|
|
if (minWidth > mDesiredWidgetWidth) {
|
|
|
|
|
return rejectWidget(
|
|
|
|
|
widget,
|
|
|
|
|
String.format(
|
|
|
|
|
Locale.ENGLISH,
|
|
|
|
|
"minWidth[%d] > mDesiredWidgetWidth[%d]",
|
|
|
|
|
minWidth,
|
|
|
|
|
mDesiredWidgetWidth));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final boolean isVerticallyResizable =
|
|
|
|
|
(info.resizeMode & AppWidgetProviderInfo.RESIZE_VERTICAL) != 0;
|
|
|
|
|
if (mDesiredWidgetHeight > 0 && isVerticallyResizable) {
|
|
|
|
|
if (info.maxResizeHeight > 0 && info.maxResizeHeight < mDesiredWidgetHeight) {
|
|
|
|
|
return rejectWidget(
|
|
|
|
|
widget,
|
|
|
|
|
String.format(
|
|
|
|
|
Locale.ENGLISH,
|
|
|
|
|
"maxResizeHeight[%d] < mDesiredWidgetHeight[%d]",
|
|
|
|
|
info.maxResizeHeight,
|
|
|
|
|
mDesiredWidgetHeight));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final int minHeight = info.minResizeHeight > 0 ? info.minResizeHeight : info.minHeight;
|
|
|
|
|
if (minHeight > mDesiredWidgetHeight) {
|
|
|
|
|
return rejectWidget(
|
|
|
|
|
widget,
|
|
|
|
|
String.format(
|
|
|
|
|
Locale.ENGLISH,
|
|
|
|
|
"minHeight[%d] > mDesiredWidgetHeight[%d]",
|
|
|
|
|
minHeight,
|
|
|
|
|
mDesiredWidgetHeight));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isHorizontallyResizable
|
|
|
|
|
&& !isVerticallyResizable
|
|
|
|
|
&& (info.minWidth < mDesiredWidgetWidth || info.minHeight < mDesiredWidgetHeight)) {
|
|
|
|
|
return rejectWidget(widget, "too small and not resizeable");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return acceptWidget(widget);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static WidgetAcceptabilityVerdict rejectWidget(
|
|
|
|
|
WidgetItem widget, String rejectionReason) {
|
|
|
|
|
return new WidgetAcceptabilityVerdict(false, widget.label, rejectionReason);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static WidgetAcceptabilityVerdict acceptWidget(WidgetItem widget) {
|
|
|
|
|
return new WidgetAcceptabilityVerdict(true, widget.label, "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private record WidgetAcceptabilityVerdict(
|
|
|
|
|
boolean isAcceptable, String widgetLabel, String reason) {
|
|
|
|
|
void maybeLogVerdict() {
|
|
|
|
|
// Only log a verdict if a reason is specified.
|
|
|
|
|
if (DEBUG && !reason.isEmpty()) {
|
|
|
|
|
Log.i(TAG, String.format(
|
|
|
|
|
Locale.ENGLISH,
|
|
|
|
|
"%s: %s because %s",
|
|
|
|
|
widgetLabel,
|
|
|
|
|
isAcceptable ? "accepted" : "rejected",
|
|
|
|
|
reason));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-17 16:45:56 -08:00
|
|
|
}
|