2009-03-03 19:32:27 -08:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2008 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2013-06-05 22:57:57 -04:00
|
|
|
package com.android.launcher3;
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2012-04-04 12:47:08 -07:00
|
|
|
import android.content.Context;
|
2012-03-01 16:09:54 -08:00
|
|
|
import android.graphics.PointF;
|
2009-04-07 21:08:40 -07:00
|
|
|
import android.graphics.Rect;
|
2012-04-24 14:36:11 -07:00
|
|
|
import android.util.Log;
|
2009-04-07 21:08:40 -07:00
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
/**
|
|
|
|
|
* Interface defining an object that can receive a drag.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public interface DropTarget {
|
2011-05-24 14:07:08 -07:00
|
|
|
|
2012-04-24 14:36:11 -07:00
|
|
|
public static final String TAG = "DropTarget";
|
|
|
|
|
|
2011-05-24 14:07:08 -07:00
|
|
|
class DragObject {
|
|
|
|
|
public int x = -1;
|
|
|
|
|
public int y = -1;
|
|
|
|
|
|
|
|
|
|
/** X offset from the upper-left corner of the cell to where we touched. */
|
|
|
|
|
public int xOffset = -1;
|
|
|
|
|
|
|
|
|
|
/** Y offset from the upper-left corner of the cell to where we touched. */
|
|
|
|
|
public int yOffset = -1;
|
|
|
|
|
|
2011-06-13 16:55:12 -07:00
|
|
|
/** This indicates whether a drag is in final stages, either drop or cancel. It
|
|
|
|
|
* differentiates onDragExit, since this is called when the drag is ending, above
|
|
|
|
|
* the current drag target, or when the drag moves off the current drag object.
|
|
|
|
|
*/
|
|
|
|
|
public boolean dragComplete = false;
|
|
|
|
|
|
2011-05-24 14:07:08 -07:00
|
|
|
/** The view that moves around while you drag. */
|
|
|
|
|
public DragView dragView = null;
|
|
|
|
|
|
|
|
|
|
/** The data associated with the object being dragged */
|
|
|
|
|
public Object dragInfo = null;
|
|
|
|
|
|
|
|
|
|
/** Where the drag originated */
|
|
|
|
|
public DragSource dragSource = null;
|
|
|
|
|
|
2015-01-23 16:11:55 -08:00
|
|
|
/** The object is part of an accessible drag operation */
|
|
|
|
|
public boolean accessibleDrag;
|
|
|
|
|
|
2011-07-08 15:34:52 -07:00
|
|
|
/** Post drag animation runnable */
|
|
|
|
|
public Runnable postAnimationRunnable = null;
|
|
|
|
|
|
2011-09-29 17:33:15 -07:00
|
|
|
/** Indicates that the drag operation was cancelled */
|
|
|
|
|
public boolean cancelled = false;
|
|
|
|
|
|
2012-02-13 18:29:29 -08:00
|
|
|
/** Defers removing the DragView from the DragLayer until after the drop animation. */
|
|
|
|
|
public boolean deferDragViewCleanupPostAnimation = true;
|
|
|
|
|
|
2011-05-24 14:07:08 -07:00
|
|
|
public DragObject() {
|
|
|
|
|
}
|
2015-01-29 09:57:16 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This is used to compute the visual center of the dragView. This point is then
|
|
|
|
|
* used to visualize drop locations and determine where to drop an item. The idea is that
|
|
|
|
|
* the visual center represents the user's interpretation of where the item is, and hence
|
|
|
|
|
* is the appropriate point to use when determining drop location.
|
|
|
|
|
*/
|
|
|
|
|
public final float[] getVisualCenter(float[] recycle) {
|
|
|
|
|
final float res[] = (recycle == null) ? new float[2] : recycle;
|
|
|
|
|
|
|
|
|
|
// These represent the visual top and left of drag view if a dragRect was provided.
|
|
|
|
|
// If a dragRect was not provided, then they correspond to the actual view left and
|
|
|
|
|
// top, as the dragRect is in that case taken to be the entire dragView.
|
|
|
|
|
// R.dimen.dragViewOffsetY.
|
|
|
|
|
int left = x - xOffset;
|
|
|
|
|
int top = y - yOffset;
|
|
|
|
|
|
|
|
|
|
// In order to find the visual center, we shift by half the dragRect
|
|
|
|
|
res[0] = left + dragView.getDragRegion().width() / 2;
|
|
|
|
|
res[1] = top + dragView.getDragRegion().height() / 2;
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2011-05-24 14:07:08 -07:00
|
|
|
}
|
|
|
|
|
|
2012-04-04 12:47:08 -07:00
|
|
|
public static class DragEnforcer implements DragController.DragListener {
|
|
|
|
|
int dragParity = 0;
|
|
|
|
|
|
|
|
|
|
public DragEnforcer(Context context) {
|
|
|
|
|
Launcher launcher = (Launcher) context;
|
|
|
|
|
launcher.getDragController().addDragListener(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onDragEnter() {
|
|
|
|
|
dragParity++;
|
|
|
|
|
if (dragParity != 1) {
|
2012-04-24 14:36:11 -07:00
|
|
|
Log.e(TAG, "onDragEnter: Drag contract violated: " + dragParity);
|
2012-04-04 12:47:08 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onDragExit() {
|
|
|
|
|
dragParity--;
|
|
|
|
|
if (dragParity != 0) {
|
2012-04-24 14:36:11 -07:00
|
|
|
Log.e(TAG, "onDragExit: Drag contract violated: " + dragParity);
|
2012-04-04 12:47:08 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onDragStart(DragSource source, Object info, int dragAction) {
|
|
|
|
|
if (dragParity != 0) {
|
2012-04-24 14:36:11 -07:00
|
|
|
Log.e(TAG, "onDragEnter: Drag contract violated: " + dragParity);
|
2012-04-04 12:47:08 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onDragEnd() {
|
|
|
|
|
if (dragParity != 0) {
|
2012-04-24 14:36:11 -07:00
|
|
|
Log.e(TAG, "onDragExit: Drag contract violated: " + dragParity);
|
2012-04-04 12:47:08 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-17 15:00:07 -07:00
|
|
|
/**
|
|
|
|
|
* Used to temporarily disable certain drop targets
|
|
|
|
|
*
|
|
|
|
|
* @return boolean specifying whether this drop target is currently enabled
|
|
|
|
|
*/
|
|
|
|
|
boolean isDropEnabled();
|
2009-03-03 19:32:27 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle an object being dropped on the DropTarget
|
2015-04-10 13:45:42 -07:00
|
|
|
*
|
2009-03-03 19:32:27 -08:00
|
|
|
* @param source DragSource where the drag started
|
|
|
|
|
* @param x X coordinate of the drop location
|
|
|
|
|
* @param y Y coordinate of the drop location
|
2009-08-04 16:04:30 -04:00
|
|
|
* @param xOffset Horizontal offset with the object being dragged where the original
|
|
|
|
|
* touch happened
|
|
|
|
|
* @param yOffset Vertical offset with the object being dragged where the original
|
|
|
|
|
* touch happened
|
|
|
|
|
* @param dragView The DragView that's being dragged around on screen.
|
2009-03-03 19:32:27 -08:00
|
|
|
* @param dragInfo Data associated with the object being dragged
|
2013-07-03 08:23:13 -07:00
|
|
|
*
|
2009-03-03 19:32:27 -08:00
|
|
|
*/
|
2011-05-24 14:07:08 -07:00
|
|
|
void onDrop(DragObject dragObject);
|
|
|
|
|
|
|
|
|
|
void onDragEnter(DragObject dragObject);
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2011-05-24 14:07:08 -07:00
|
|
|
void onDragOver(DragObject dragObject);
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2011-05-24 14:07:08 -07:00
|
|
|
void onDragExit(DragObject dragObject);
|
2009-03-03 19:32:27 -08:00
|
|
|
|
2012-03-01 16:09:54 -08:00
|
|
|
/**
|
|
|
|
|
* Handle an object being dropped as a result of flinging to delete and will be called in place
|
|
|
|
|
* of onDrop(). (This is only called on objects that are set as the DragController's
|
|
|
|
|
* fling-to-delete target.
|
|
|
|
|
*/
|
|
|
|
|
void onFlingToDelete(DragObject dragObject, int x, int y, PointF vec);
|
|
|
|
|
|
2009-03-03 19:32:27 -08:00
|
|
|
/**
|
2009-04-07 21:08:40 -07:00
|
|
|
* Check if a drop action can occur at, or near, the requested location.
|
2010-08-17 15:11:18 -07:00
|
|
|
* This will be called just before onDrop.
|
2015-04-10 13:45:42 -07:00
|
|
|
*
|
2009-03-03 19:32:27 -08:00
|
|
|
* @param source DragSource where the drag started
|
|
|
|
|
* @param x X coordinate of the drop location
|
|
|
|
|
* @param y Y coordinate of the drop location
|
2009-04-07 21:08:40 -07:00
|
|
|
* @param xOffset Horizontal offset with the object being dragged where the
|
|
|
|
|
* original touch happened
|
|
|
|
|
* @param yOffset Vertical offset with the object being dragged where the
|
|
|
|
|
* original touch happened
|
2009-08-04 16:04:30 -04:00
|
|
|
* @param dragView The DragView that's being dragged around on screen.
|
2009-03-03 19:32:27 -08:00
|
|
|
* @param dragInfo Data associated with the object being dragged
|
2009-04-07 21:08:40 -07:00
|
|
|
* @return True if the drop will be accepted, false otherwise.
|
2009-03-03 19:32:27 -08:00
|
|
|
*/
|
2011-05-24 14:07:08 -07:00
|
|
|
boolean acceptDrop(DragObject dragObject);
|
2009-04-07 21:08:40 -07:00
|
|
|
|
2015-04-24 11:44:51 -07:00
|
|
|
void prepareAccessibilityDrop();
|
|
|
|
|
|
2009-08-04 16:04:30 -04:00
|
|
|
// These methods are implemented in Views
|
2013-07-01 17:03:59 -07:00
|
|
|
void getHitRectRelativeToDragLayer(Rect outRect);
|
2011-07-07 16:38:18 -07:00
|
|
|
void getLocationInDragLayer(int[] loc);
|
2009-08-04 16:04:30 -04:00
|
|
|
int getLeft();
|
|
|
|
|
int getTop();
|
2009-03-03 19:32:27 -08:00
|
|
|
}
|