mirror of
https://github.com/LawnchairLauncher/lawnchair.git
synced 2026-02-20 03:08:19 +00:00
This is a preliminary work for local color extraction. In order to
apply local colors extracted from wallpaper to AppWidgetHostView
during drag, we need to hold a reference of the dragging
AppWidgetHostView.
In this CL, the following changes are made:
1. Instead of using bitmap image directly for icons, folders, shortcuts,
legacy widget drawable preview, a BitmapDrawable wrapper is
introduced.
2. Introduce a WidgetHostViewDraggableDrawable which draws
LauncherAppWidgetHostView directly to canvas. No more bitmap
generation overhead.
3. Remove drag outline from the drag logic because this will be replaced
by a new grid color hint UI: https://screenshot.googleplex.com/7jBEVeuxFecFKKT.png
Test: Add: add widgets, shortcuts from widgets tray.
add icons from all apps.
create folder.
Drag: drag existing widgets, shortcuts, folders and icons around
Bug: 182282587
Change-Id: Ia45ff756ea5bb80cf0761f0727a9453d50c204c0
330 lines
12 KiB
Java
330 lines
12 KiB
Java
/*
|
|
* Copyright (C) 2017 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.dragndrop;
|
|
|
|
import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ADD_EXTERNAL_ITEM_BACK;
|
|
import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ADD_EXTERNAL_ITEM_CANCELLED;
|
|
import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ADD_EXTERNAL_ITEM_DRAGGED;
|
|
import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ADD_EXTERNAL_ITEM_PLACED_AUTOMATICALLY;
|
|
import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_ADD_EXTERNAL_ITEM_START;
|
|
import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;
|
|
|
|
import android.annotation.TargetApi;
|
|
import android.app.ActivityOptions;
|
|
import android.appwidget.AppWidgetManager;
|
|
import android.content.ClipData;
|
|
import android.content.ClipDescription;
|
|
import android.content.Intent;
|
|
import android.content.pm.LauncherApps.PinItemRequest;
|
|
import android.graphics.Canvas;
|
|
import android.graphics.Point;
|
|
import android.graphics.PointF;
|
|
import android.graphics.Rect;
|
|
import android.os.AsyncTask;
|
|
import android.os.Build;
|
|
import android.os.Bundle;
|
|
import android.view.MotionEvent;
|
|
import android.view.View;
|
|
import android.view.View.DragShadowBuilder;
|
|
import android.view.View.OnLongClickListener;
|
|
import android.view.View.OnTouchListener;
|
|
|
|
import com.android.launcher3.BaseActivity;
|
|
import com.android.launcher3.InvariantDeviceProfile;
|
|
import com.android.launcher3.Launcher;
|
|
import com.android.launcher3.LauncherAppState;
|
|
import com.android.launcher3.R;
|
|
import com.android.launcher3.logging.StatsLogManager;
|
|
import com.android.launcher3.model.ItemInstallQueue;
|
|
import com.android.launcher3.model.WidgetItem;
|
|
import com.android.launcher3.model.data.ItemInfo;
|
|
import com.android.launcher3.pm.PinRequestHelper;
|
|
import com.android.launcher3.views.BaseDragLayer;
|
|
import com.android.launcher3.widget.LauncherAppWidgetHost;
|
|
import com.android.launcher3.widget.LauncherAppWidgetProviderInfo;
|
|
import com.android.launcher3.widget.PendingAddShortcutInfo;
|
|
import com.android.launcher3.widget.PendingAddWidgetInfo;
|
|
import com.android.launcher3.widget.WidgetCell;
|
|
import com.android.launcher3.widget.WidgetHostViewLoader;
|
|
import com.android.launcher3.widget.WidgetImageView;
|
|
import com.android.launcher3.widget.WidgetManagerHelper;
|
|
|
|
import java.util.function.Supplier;
|
|
|
|
@TargetApi(Build.VERSION_CODES.O)
|
|
public class AddItemActivity extends BaseActivity implements OnLongClickListener, OnTouchListener {
|
|
|
|
private static final int SHADOW_SIZE = 10;
|
|
|
|
private static final int REQUEST_BIND_APPWIDGET = 1;
|
|
private static final String STATE_EXTRA_WIDGET_ID = "state.widget.id";
|
|
|
|
private final PointF mLastTouchPos = new PointF();
|
|
|
|
private PinItemRequest mRequest;
|
|
private LauncherAppState mApp;
|
|
private InvariantDeviceProfile mIdp;
|
|
|
|
private WidgetCell mWidgetCell;
|
|
|
|
// Widget request specific options.
|
|
private LauncherAppWidgetHost mAppWidgetHost;
|
|
private WidgetManagerHelper mAppWidgetManager;
|
|
private int mPendingBindWidgetId;
|
|
private Bundle mWidgetOptions;
|
|
|
|
private boolean mFinishOnPause = false;
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
|
|
mRequest = PinRequestHelper.getPinItemRequest(getIntent());
|
|
if (mRequest == null) {
|
|
finish();
|
|
return;
|
|
}
|
|
|
|
mApp = LauncherAppState.getInstance(this);
|
|
mIdp = mApp.getInvariantDeviceProfile();
|
|
|
|
// Use the application context to get the device profile, as in multiwindow-mode, the
|
|
// confirmation activity might be rotated.
|
|
mDeviceProfile = mIdp.getDeviceProfile(getApplicationContext());
|
|
|
|
setContentView(R.layout.add_item_confirmation_activity);
|
|
mWidgetCell = findViewById(R.id.widget_cell);
|
|
|
|
if (mRequest.getRequestType() == PinItemRequest.REQUEST_TYPE_SHORTCUT) {
|
|
setupShortcut();
|
|
} else {
|
|
if (!setupWidget()) {
|
|
// TODO: show error toast?
|
|
finish();
|
|
}
|
|
}
|
|
|
|
WidgetImageView preview = mWidgetCell.findViewById(R.id.widget_preview);
|
|
preview.setOnTouchListener(this);
|
|
preview.setOnLongClickListener(this);
|
|
|
|
// savedInstanceState is null when the activity is created the first time (i.e., avoids
|
|
// duplicate logging during rotation)
|
|
if (savedInstanceState == null) {
|
|
logCommand(LAUNCHER_ADD_EXTERNAL_ITEM_START);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean onTouch(View view, MotionEvent motionEvent) {
|
|
mLastTouchPos.set(motionEvent.getX(), motionEvent.getY());
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean onLongClick(View view) {
|
|
// Find the position of the preview relative to the touch location.
|
|
WidgetImageView img = mWidgetCell.getWidgetView();
|
|
|
|
// If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and
|
|
// we abort the drag.
|
|
if (img.getDrawable() == null) {
|
|
return false;
|
|
}
|
|
|
|
Rect bounds = img.getBitmapBounds();
|
|
bounds.offset(img.getLeft() - (int) mLastTouchPos.x, img.getTop() - (int) mLastTouchPos.y);
|
|
|
|
// Start home and pass the draw request params
|
|
PinItemDragListener listener = new PinItemDragListener(mRequest, bounds,
|
|
img.getDrawable().getIntrinsicWidth(), img.getWidth());
|
|
|
|
|
|
// Start a system drag and drop. We use a transparent bitmap as preview for system drag
|
|
// as the preview is handled internally by launcher.
|
|
ClipDescription description = new ClipDescription("", new String[]{listener.getMimeType()});
|
|
ClipData data = new ClipData(description, new ClipData.Item(""));
|
|
view.startDragAndDrop(data, new DragShadowBuilder(view) {
|
|
|
|
@Override
|
|
public void onDrawShadow(Canvas canvas) { }
|
|
|
|
@Override
|
|
public void onProvideShadowMetrics(Point outShadowSize, Point outShadowTouchPoint) {
|
|
outShadowSize.set(SHADOW_SIZE, SHADOW_SIZE);
|
|
outShadowTouchPoint.set(SHADOW_SIZE / 2, SHADOW_SIZE / 2);
|
|
}
|
|
}, null, View.DRAG_FLAG_GLOBAL);
|
|
|
|
Intent homeIntent = new Intent(Intent.ACTION_MAIN)
|
|
.addCategory(Intent.CATEGORY_HOME)
|
|
.setPackage(getPackageName())
|
|
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
Launcher.ACTIVITY_TRACKER.runCallbackWhenActivityExists(listener, homeIntent);
|
|
startActivity(homeIntent,
|
|
ActivityOptions.makeCustomAnimation(this, 0, android.R.anim.fade_out)
|
|
.toBundle());
|
|
logCommand(LAUNCHER_ADD_EXTERNAL_ITEM_DRAGGED);
|
|
mFinishOnPause = true;
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
protected void onPause() {
|
|
super.onPause();
|
|
if (mFinishOnPause) {
|
|
finish();
|
|
}
|
|
}
|
|
|
|
private void setupShortcut() {
|
|
PinShortcutRequestActivityInfo shortcutInfo =
|
|
new PinShortcutRequestActivityInfo(mRequest, this);
|
|
mWidgetCell.getWidgetView().setTag(new PendingAddShortcutInfo(shortcutInfo));
|
|
applyWidgetItemAsync(
|
|
() -> new WidgetItem(shortcutInfo, mApp.getIconCache(), getPackageManager()));
|
|
}
|
|
|
|
private boolean setupWidget() {
|
|
LauncherAppWidgetProviderInfo widgetInfo = LauncherAppWidgetProviderInfo
|
|
.fromProviderInfo(this, mRequest.getAppWidgetProviderInfo(this));
|
|
if (widgetInfo.minSpanX > mIdp.numColumns || widgetInfo.minSpanY > mIdp.numRows) {
|
|
// Cannot add widget
|
|
return false;
|
|
}
|
|
mWidgetCell.setPreview(PinItemDragListener.getPreview(mRequest));
|
|
|
|
mAppWidgetManager = new WidgetManagerHelper(this);
|
|
mAppWidgetHost = new LauncherAppWidgetHost(this);
|
|
|
|
PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(widgetInfo);
|
|
pendingInfo.spanX = Math.min(mIdp.numColumns, widgetInfo.spanX);
|
|
pendingInfo.spanY = Math.min(mIdp.numRows, widgetInfo.spanY);
|
|
mWidgetOptions = WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
|
|
mWidgetCell.getWidgetView().setTag(pendingInfo);
|
|
|
|
applyWidgetItemAsync(() -> new WidgetItem(widgetInfo, mIdp, mApp.getIconCache()));
|
|
return true;
|
|
}
|
|
|
|
private void applyWidgetItemAsync(final Supplier<WidgetItem> itemProvider) {
|
|
new AsyncTask<Void, Void, WidgetItem>() {
|
|
@Override
|
|
protected WidgetItem doInBackground(Void... voids) {
|
|
return itemProvider.get();
|
|
}
|
|
|
|
@Override
|
|
protected void onPostExecute(WidgetItem item) {
|
|
mWidgetCell.applyFromCellItem(item, mApp.getWidgetCache());
|
|
mWidgetCell.ensurePreview();
|
|
}
|
|
}.executeOnExecutor(MODEL_EXECUTOR);
|
|
// TODO: Create a worker looper executor and reuse that everywhere.
|
|
}
|
|
|
|
/**
|
|
* Called when the cancel button is clicked.
|
|
*/
|
|
public void onCancelClick(View v) {
|
|
logCommand(LAUNCHER_ADD_EXTERNAL_ITEM_CANCELLED);
|
|
finish();
|
|
}
|
|
|
|
/**
|
|
* Called when place-automatically button is clicked.
|
|
*/
|
|
public void onPlaceAutomaticallyClick(View v) {
|
|
if (mRequest.getRequestType() == PinItemRequest.REQUEST_TYPE_SHORTCUT) {
|
|
ItemInstallQueue.INSTANCE.get(this).queueItem(mRequest.getShortcutInfo());
|
|
logCommand(LAUNCHER_ADD_EXTERNAL_ITEM_PLACED_AUTOMATICALLY);
|
|
mRequest.accept();
|
|
finish();
|
|
return;
|
|
}
|
|
|
|
mPendingBindWidgetId = mAppWidgetHost.allocateAppWidgetId();
|
|
boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
|
|
mPendingBindWidgetId, mRequest.getAppWidgetProviderInfo(this), mWidgetOptions);
|
|
if (success) {
|
|
acceptWidget(mPendingBindWidgetId);
|
|
return;
|
|
}
|
|
|
|
// request bind widget
|
|
mAppWidgetHost.startBindFlow(this, mPendingBindWidgetId,
|
|
mRequest.getAppWidgetProviderInfo(this), REQUEST_BIND_APPWIDGET);
|
|
}
|
|
|
|
private void acceptWidget(int widgetId) {
|
|
ItemInstallQueue.INSTANCE.get(this)
|
|
.queueItem(mRequest.getAppWidgetProviderInfo(this), widgetId);
|
|
mWidgetOptions.putInt(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
|
|
mRequest.accept(mWidgetOptions);
|
|
logCommand(LAUNCHER_ADD_EXTERNAL_ITEM_PLACED_AUTOMATICALLY);
|
|
finish();
|
|
}
|
|
|
|
@Override
|
|
public void onBackPressed() {
|
|
logCommand(LAUNCHER_ADD_EXTERNAL_ITEM_BACK);
|
|
super.onBackPressed();
|
|
}
|
|
|
|
@Override
|
|
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
if (requestCode == REQUEST_BIND_APPWIDGET) {
|
|
int widgetId = data != null
|
|
? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mPendingBindWidgetId)
|
|
: mPendingBindWidgetId;
|
|
if (resultCode == RESULT_OK) {
|
|
acceptWidget(widgetId);
|
|
} else {
|
|
// Simply wait it out.
|
|
mAppWidgetHost.deleteAppWidgetId(widgetId);
|
|
mPendingBindWidgetId = -1;
|
|
}
|
|
return;
|
|
}
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
}
|
|
|
|
@Override
|
|
protected void onSaveInstanceState(Bundle outState) {
|
|
super.onSaveInstanceState(outState);
|
|
outState.putInt(STATE_EXTRA_WIDGET_ID, mPendingBindWidgetId);
|
|
}
|
|
|
|
@Override
|
|
protected void onRestoreInstanceState(Bundle savedInstanceState) {
|
|
super.onRestoreInstanceState(savedInstanceState);
|
|
mPendingBindWidgetId = savedInstanceState
|
|
.getInt(STATE_EXTRA_WIDGET_ID, mPendingBindWidgetId);
|
|
}
|
|
|
|
@Override
|
|
public BaseDragLayer getDragLayer() {
|
|
throw new UnsupportedOperationException();
|
|
}
|
|
|
|
private void logCommand(StatsLogManager.EventEnum command) {
|
|
getStatsLogManager().logger()
|
|
.withItemInfo((ItemInfo) mWidgetCell.getWidgetView().getTag())
|
|
.log(command);
|
|
}
|
|
}
|