Making CellLayout inflatable without LauncherActivtiy

This will allow creating cellLayout with a different device profile
or a different activtiy which can be used when displaying multiple
activities in multi-display or generating a preview for changing
display properties.

Change-Id: I386731d95e33c48bdef270ce9c0d3e9feefb6262
This commit is contained in:
Sunny Goyal 2018-11-14 15:17:26 -08:00
parent 373e2534d3
commit ab770a10b9
7 changed files with 46 additions and 55 deletions

View File

@ -62,6 +62,7 @@ import com.android.launcher3.util.GridOccupancy;
import com.android.launcher3.util.ParcelableSparseArray;
import com.android.launcher3.util.Themes;
import com.android.launcher3.util.Thunk;
import com.android.launcher3.views.ActivityContext;
import com.android.launcher3.widget.LauncherAppWidgetHostView;
import java.lang.annotation.Retention;
@ -82,7 +83,7 @@ public class CellLayout extends ViewGroup {
private static final String TAG = "CellLayout";
private static final boolean LOGD = false;
private final Launcher mLauncher;
protected final ActivityContext mActivity;
@ViewDebug.ExportedProperty(category = "launcher")
@Thunk int mCellWidth;
@ViewDebug.ExportedProperty(category = "launcher")
@ -106,7 +107,6 @@ public class CellLayout extends ViewGroup {
private GridOccupancy mTmpOccupied;
private OnTouchListener mInterceptTouchListener;
private final StylusEventHelper mStylusEventHelper;
private final ArrayList<PreviewBackground> mFolderBackgrounds = new ArrayList<>();
final PreviewBackground mFolderLeaveBehind = new PreviewBackground();
@ -201,9 +201,9 @@ public class CellLayout extends ViewGroup {
// the user where a dragged item will land when dropped.
setWillNotDraw(false);
setClipToPadding(false);
mLauncher = Launcher.getLauncher(context);
mActivity = ActivityContext.lookupContext(context);
DeviceProfile grid = mLauncher.getDeviceProfile();
DeviceProfile grid = mActivity.getDeviceProfile();
mCellWidth = mCellHeight = -1;
mFixedCellWidth = mFixedCellHeight = -1;
@ -286,8 +286,6 @@ public class CellLayout extends ViewGroup {
mShortcutsAndWidgets = new ShortcutAndWidgetContainer(context, mContainerType);
mShortcutsAndWidgets.setCellDimensions(mCellWidth, mCellHeight, mCountX, mCountY);
mStylusEventHelper = new StylusEventHelper(new SimpleOnStylusPressListener(this), this);
addView(mShortcutsAndWidgets);
}
@ -337,20 +335,6 @@ public class CellLayout extends ViewGroup {
return false;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
boolean handled = super.onTouchEvent(ev);
// Stylus button press on a home screen should not switch between overview mode and
// the home screen mode, however, once in overview mode stylus button press should be
// enabled to allow rearranging the different home screens. So check what mode
// the workspace is in, and only perform stylus button presses while in overview mode.
if (mLauncher.isInState(LauncherState.OVERVIEW)
&& mStylusEventHelper.onMotionEvent(ev)) {
return true;
}
return handled;
}
public void enableHardwareLayer(boolean hasLayer) {
mShortcutsAndWidgets.setLayerType(hasLayer ? LAYER_TYPE_HARDWARE : LAYER_TYPE_NONE, sPaint);
}
@ -504,7 +488,7 @@ public class CellLayout extends ViewGroup {
public void setFolderLeaveBehindCell(int x, int y) {
View child = getChildAt(x, y);
mFolderLeaveBehind.setup(mLauncher, null,
mFolderLeaveBehind.setup(getContext(), mActivity, null,
child.getMeasuredWidth(), child.getPaddingTop());
mFolderLeaveBehind.delegateCellX = x;
@ -945,7 +929,7 @@ public class CellLayout extends ViewGroup {
if (resize) {
cellToRect(cellX, cellY, spanX, spanY, r);
if (v instanceof LauncherAppWidgetHostView) {
DeviceProfile profile = mLauncher.getDeviceProfile();
DeviceProfile profile = mActivity.getDeviceProfile();
Utilities.shrinkRect(r, profile.appWidgetScale.x, profile.appWidgetScale.y);
}
} else {
@ -2047,7 +2031,7 @@ public class CellLayout extends ViewGroup {
.translationY(initDeltaY)
.build(child)
.setDuration(REORDER_ANIMATION_DURATION);
mLauncher.getDragController().addFirstFrameAnimationHelper(a);
Launcher.cast(mActivity).getDragController().addFirstFrameAnimationHelper(a);
a.setInterpolator(DEACCEL_1_5);
a.start();
}
@ -2063,7 +2047,7 @@ public class CellLayout extends ViewGroup {
private void commitTempPlacement() {
mTmpOccupied.copyTo(mOccupied);
int screenId = mLauncher.getWorkspace().getIdForScreen(this);
int screenId = Launcher.cast(mActivity).getWorkspace().getIdForScreen(this);
int container = Favorites.CONTAINER_DESKTOP;
if (mContainerType == HOTSEAT) {
@ -2089,8 +2073,8 @@ public class CellLayout extends ViewGroup {
info.spanY = lp.cellVSpan;
if (requiresDbUpdate) {
mLauncher.getModelWriter().modifyItemInDatabase(info, container, screenId,
info.cellX, info.cellY, info.spanX, info.spanY);
Launcher.cast(mActivity).getModelWriter().modifyItemInDatabase(info, container,
screenId, info.cellX, info.cellY, info.spanX, info.spanY);
}
}
}

View File

@ -20,7 +20,6 @@ import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewDebug;
import android.view.ViewGroup;
@ -32,8 +31,6 @@ import com.android.launcher3.userevent.nano.LauncherLogProto.Target;
public class Hotseat extends CellLayout implements LogContainerProvider, Insettable {
private final Launcher mLauncher;
@ViewDebug.ExportedProperty(category = "launcher")
private boolean mHasVerticalHotseat;
@ -47,7 +44,6 @@ public class Hotseat extends CellLayout implements LogContainerProvider, Insetta
public Hotseat(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mLauncher = Launcher.getLauncher(context);
}
/* Get the orientation specific coordinates given an invariant order in the hotseat. */
@ -59,10 +55,10 @@ public class Hotseat extends CellLayout implements LogContainerProvider, Insetta
return mHasVerticalHotseat ? (getCountY() - (rank + 1)) : 0;
}
void resetLayout(boolean hasVerticalHotseat) {
public void resetLayout(boolean hasVerticalHotseat) {
removeAllViewsInLayout();
mHasVerticalHotseat = hasVerticalHotseat;
InvariantDeviceProfile idp = mLauncher.getDeviceProfile().inv;
InvariantDeviceProfile idp = mActivity.getDeviceProfile().inv;
if (hasVerticalHotseat) {
setGridSize(1, idp.numHotseatIcons);
} else {
@ -70,15 +66,6 @@ public class Hotseat extends CellLayout implements LogContainerProvider, Insetta
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// We don't want any clicks to go through to the hotseat unless the workspace is in
// the normal state or an accessible drag is in progress.
return (!mLauncher.getWorkspace().workspaceIconsCanBeDragged()
&& !mLauncher.getAccessibilityDelegate().isInAccessibleDrag())
|| super.onInterceptTouchEvent(ev);
}
@Override
public void fillInLogContainerData(View v, ItemInfo info, Target target, Target targetParent) {
target.gridX = info.cellX;
@ -89,7 +76,7 @@ public class Hotseat extends CellLayout implements LogContainerProvider, Insetta
@Override
public void setInsets(Rect insets) {
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) getLayoutParams();
DeviceProfile grid = mLauncher.getDeviceProfile();
DeviceProfile grid = mActivity.getDeviceProfile();
if (grid.isVerticalBarLayout()) {
lp.height = ViewGroup.LayoutParams.MATCH_PARENT;

View File

@ -122,6 +122,7 @@ import com.android.launcher3.util.Thunk;
import com.android.launcher3.util.TraceHelper;
import com.android.launcher3.util.UiThreadHelper;
import com.android.launcher3.util.ViewOnDrawExecutor;
import com.android.launcher3.views.ActivityContext;
import com.android.launcher3.views.OptionsPopupView;
import com.android.launcher3.widget.LauncherAppWidgetHostView;
import com.android.launcher3.widget.PendingAddShortcutInfo;
@ -940,6 +941,7 @@ public class Launcher extends BaseDraggingActivity implements LauncherExterns,
mDropTargetBar.setup(mDragController);
mAllAppsController.setupViews(mAppsView);
mHotseat.setOnInterceptTouchListener(mWorkspace::onInterceptHotseatTouch);
}
/**
@ -2391,6 +2393,13 @@ public class Launcher extends BaseDraggingActivity implements LauncherExterns,
return (Launcher) fromContext(context);
}
/**
* Just a wrapper around the type cast to allow easier tracking of calls.
*/
public static <T extends Launcher> T cast(ActivityContext activityContext) {
return (T) activityContext;
}
/**
* Callback for listening for onResume
*/

View File

@ -26,6 +26,7 @@ import android.view.View;
import android.view.ViewGroup;
import com.android.launcher3.CellLayout.ContainerType;
import com.android.launcher3.views.ActivityContext;
import com.android.launcher3.widget.LauncherAppWidgetHostView;
public class ShortcutAndWidgetContainer extends ViewGroup {
@ -43,12 +44,12 @@ public class ShortcutAndWidgetContainer extends ViewGroup {
private int mCountX;
private Launcher mLauncher;
private ActivityContext mActivity;
private boolean mInvertIfRtl = false;
public ShortcutAndWidgetContainer(Context context, @ContainerType int containerType) {
super(context);
mLauncher = Launcher.getLauncher(context);
mActivity = ActivityContext.lookupContext(context);
mWallpaperManager = WallpaperManager.getInstance(context);
mContainerType = containerType;
}
@ -92,7 +93,7 @@ public class ShortcutAndWidgetContainer extends ViewGroup {
public void setupLp(View child) {
CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
if (child instanceof LauncherAppWidgetHostView) {
DeviceProfile profile = mLauncher.getDeviceProfile();
DeviceProfile profile = mActivity.getDeviceProfile();
lp.setup(mCellWidth, mCellHeight, invertLayoutHorizontally(), mCountX,
profile.appWidgetScale.x, profile.appWidgetScale.y);
} else {
@ -107,12 +108,12 @@ public class ShortcutAndWidgetContainer extends ViewGroup {
public int getCellContentHeight() {
return Math.min(getMeasuredHeight(),
mLauncher.getDeviceProfile().getCellHeight(mContainerType));
mActivity.getDeviceProfile().getCellHeight(mContainerType));
}
public void measureChild(View child) {
CellLayout.LayoutParams lp = (CellLayout.LayoutParams) child.getLayoutParams();
final DeviceProfile profile = mLauncher.getDeviceProfile();
final DeviceProfile profile = mActivity.getDeviceProfile();
if (child instanceof LauncherAppWidgetHostView) {
lp.setup(mCellWidth, mCellHeight, invertLayoutHorizontally(), mCountX,
@ -149,7 +150,7 @@ public class ShortcutAndWidgetContainer extends ViewGroup {
LauncherAppWidgetHostView lahv = (LauncherAppWidgetHostView) child;
// Scale and center the widget to fit within its cells.
DeviceProfile profile = mLauncher.getDeviceProfile();
DeviceProfile profile = mActivity.getDeviceProfile();
float scaleX = profile.appWidgetScale.x;
float scaleY = profile.appWidgetScale.y;

View File

@ -475,6 +475,13 @@ public class Workspace extends PagedView<WorkspacePageIndicator>
super.onViewAdded(child);
}
protected boolean onInterceptHotseatTouch(View v, MotionEvent ev) {
// We don't want any clicks to go through to the hotseat unless the workspace is in
// the normal state or an accessible drag is in progress.
return !workspaceIconsCanBeDragged()
&& !mLauncher.getAccessibilityDelegate().isInAccessibleDrag();
}
/**
* Initializes and binds the first page
* @param qsb an existing qsb to recycle or null.
@ -2442,7 +2449,7 @@ public class Workspace extends PagedView<WorkspacePageIndicator>
this.cellY = cellY;
BubbleTextView cell = (BubbleTextView) layout.getChildAt(cellX, cellY);
bg.setup(mLauncher, null, cell.getMeasuredWidth(), cell.getPaddingTop());
bg.setup(mLauncher, mLauncher, null, cell.getMeasuredWidth(), cell.getPaddingTop());
// The full preview background should appear behind the icon
bg.isClipping = false;

View File

@ -23,6 +23,7 @@ import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
@ -41,6 +42,7 @@ import com.android.launcher3.DeviceProfile;
import com.android.launcher3.Launcher;
import com.android.launcher3.R;
import com.android.launcher3.util.Themes;
import com.android.launcher3.views.ActivityContext;
/**
* This object represents a FolderIcon preview background. It stores drawing / measurement
@ -121,20 +123,20 @@ public class PreviewBackground {
}
};
public void setup(Launcher launcher, View invalidateDelegate,
public void setup(Context context, ActivityContext activity, View invalidateDelegate,
int availableSpaceX, int topPadding) {
mInvalidateDelegate = invalidateDelegate;
mBgColor = Themes.getAttrColor(launcher, android.R.attr.colorPrimary);
mBadgeColor = Themes.getAttrColor(launcher, R.attr.folderBadgeColor);
mBgColor = Themes.getAttrColor(context, android.R.attr.colorPrimary);
mBadgeColor = Themes.getAttrColor(context, R.attr.folderBadgeColor);
DeviceProfile grid = launcher.getDeviceProfile();
DeviceProfile grid = activity.getDeviceProfile();
previewSize = grid.folderIconSizePx;
basePreviewOffsetX = (availableSpaceX - previewSize) / 2;
basePreviewOffsetY = topPadding + grid.folderIconOffsetYPx;
// Stroke width is 1dp
mStrokeWidth = launcher.getResources().getDisplayMetrics().density;
mStrokeWidth = context.getResources().getDisplayMetrics().density;
float radius = getScaledRadius();
float shadowRadius = radius + mStrokeWidth;

View File

@ -107,7 +107,8 @@ public class PreviewItemManager {
mTotalWidth = totalSize;
mPrevTopPadding = mIcon.getPaddingTop();
mIcon.mBackground.setup(mIcon.mLauncher, mIcon, mTotalWidth, mIcon.getPaddingTop());
mIcon.mBackground.setup(mIcon.mLauncher, mIcon.mLauncher, mIcon, mTotalWidth,
mIcon.getPaddingTop());
mIcon.mPreviewLayoutRule.init(mIcon.mBackground.previewSize, mIntrinsicIconSize,
Utilities.isRtl(mIcon.getResources()));